aboutsummaryrefslogtreecommitdiffstats
path: root/linux-2.6.9-xen-sparse/arch/xen/kernel/reboot.c
blob: 9b9761e8417f650abfd7ed9a4469e54fbae429ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#define __KERNEL_SYSCALLS__
static int errno;
#include <linux/errno.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/unistd.h>
#include <linux/module.h>
#include <linux/reboot.h>
#include <asm/irq.h>
#include <asm/mmu_context.h>
#include <asm-xen/ctrl_if.h>
#include <asm-xen/hypervisor.h>
#include <asm-xen/hypervisor-ifs/dom0_ops.h>
#include <asm-xen/suspend.h>
#include <asm-xen/queues.h>

void machine_restart(char * __unused)
{
	/* We really want to get pending console data out before we die. */
	extern void xencons_force_flush(void);
	xencons_force_flush();
	HYPERVISOR_reboot();
}

void machine_halt(void)
{
	machine_power_off();
}

void machine_power_off(void)
{
	/* We really want to get pending console data out before we die. */
	extern void xencons_force_flush(void);
	xencons_force_flush();
	HYPERVISOR_shutdown();
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
int reboot_thru_bios = 0;	/* for dmi_scan.c */
EXPORT_SYMBOL(machine_restart);
EXPORT_SYMBOL(machine_halt);
EXPORT_SYMBOL(machine_power_off);
#endif


/******************************************************************************
 * Stop/pickle callback handling.
 */

#include <asm/suspend.h>

/* Ignore multiple shutdown requests. */
static int shutting_down = -1;

static void __do_suspend(void)
{
    int i, j;
    suspend_record_t *suspend_record;

    /* Hmmm... a cleaner interface to suspend/resume blkdevs would be nice. */
	/* XXX SMH: yes it would :-( */	
#ifdef CONFIG_XEN_BLKDEV_FRONTEND
    extern void blkdev_suspend(void);
    extern void blkdev_resume(void);
#else
#define blkdev_suspend() do{}while(0)
#define blkdev_resume()  do{}while(0)
#endif

#ifdef CONFIG_XEN_NETDEV_FRONTEND
    extern void netif_suspend(void);
    extern void netif_resume(void);  
#else
#define netif_suspend() do{}while(0)
#define netif_resume()  do{}while(0)
#endif

    extern void time_suspend(void);
    extern void time_resume(void);
    extern unsigned long max_pfn;
    extern unsigned long *pfn_to_mfn_frame_list;

    suspend_record = (suspend_record_t *)__get_free_page(GFP_KERNEL);
    if ( suspend_record == NULL )
        goto out;

    suspend_record->nr_pfns = max_pfn; /* final number of pfns */

    __cli();

    netif_suspend();

    blkdev_suspend();

    time_suspend();

    ctrl_if_suspend();

    irq_suspend();

    HYPERVISOR_shared_info = (shared_info_t *)empty_zero_page;
    clear_fixmap(FIX_SHARED_INFO);

    memcpy(&suspend_record->resume_info, &xen_start_info, sizeof(xen_start_info));

    HYPERVISOR_suspend(virt_to_machine(suspend_record) >> PAGE_SHIFT);

    HYPERVISOR_vm_assist(VMASST_CMD_enable,
			 VMASST_TYPE_4gb_segments);
#ifdef CONFIG_XEN_WRITABLE_PAGETABLES
    HYPERVISOR_vm_assist(VMASST_CMD_enable,
			 VMASST_TYPE_writable_pagetables);
#endif

    shutting_down = -1; 

    memcpy(&xen_start_info, &suspend_record->resume_info, sizeof(xen_start_info));

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
    set_fixmap_ma(FIX_SHARED_INFO, xen_start_info.shared_info);
#else
    set_fixmap(FIX_SHARED_INFO, xen_start_info.shared_info);
#endif

    HYPERVISOR_shared_info = (shared_info_t *)fix_to_virt(FIX_SHARED_INFO);

    memset(empty_zero_page, 0, PAGE_SIZE);

    for ( i=0, j=0; i < max_pfn; i+=(PAGE_SIZE/sizeof(unsigned long)), j++ )
    {
        pfn_to_mfn_frame_list[j] = 
            virt_to_machine(&phys_to_machine_mapping[i]) >> PAGE_SHIFT;
    }
    HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list =
        virt_to_machine(pfn_to_mfn_frame_list) >> PAGE_SHIFT;


    irq_resume();

    ctrl_if_resume();

    time_resume();

    blkdev_resume();

    netif_resume();

    __sti();

 out:
    if ( suspend_record != NULL )
        free_page((unsigned long)suspend_record);
}

static int shutdown_process(void *__unused)
{
    static char *envp[] = { "HOME=/", "TERM=linux", 
                            "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
    static char *restart_argv[]  = { "/sbin/shutdown", "-r", "now", NULL };
    static char *poweroff_argv[] = { "/sbin/halt",     "-p",        NULL };

    extern asmlinkage long sys_reboot(int magic1, int magic2,
                                      unsigned int cmd, void *arg);

    daemonize(
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
        "shutdown"
#endif
        );

    switch ( shutting_down )
    {
    case CMSG_SHUTDOWN_POWEROFF:
        if ( execve("/sbin/halt", poweroff_argv, envp) < 0 )
        {
            sys_reboot(LINUX_REBOOT_MAGIC1,
                       LINUX_REBOOT_MAGIC2,
                       LINUX_REBOOT_CMD_POWER_OFF,
                       NULL);
        }
        break;

    case CMSG_SHUTDOWN_REBOOT:
        if ( execve("/sbin/shutdown", restart_argv, envp) < 0 )
        {
            sys_reboot(LINUX_REBOOT_MAGIC1,
                       LINUX_REBOOT_MAGIC2,
                       LINUX_REBOOT_CMD_RESTART,
                       NULL);
        }
        break;
    }

    shutting_down = -1; /* could try again */

    return 0;
}

static void __shutdown_handler(void *unused)
{
    int err;

    if ( shutting_down != CMSG_SHUTDOWN_SUSPEND )
    {
        err = kernel_thread(shutdown_process, NULL, CLONE_FS | CLONE_FILES);
        if ( err < 0 )
            printk(KERN_ALERT "Error creating shutdown process!\n");
    }
    else
    {
        __do_suspend();
    }
}

static void shutdown_handler(ctrl_msg_t *msg, unsigned long id)
{
    static DECLARE_WORK(shutdown_work, __shutdown_handler, NULL);

    if ( (shutting_down == -1) &&
         ((msg->subtype == CMSG_SHUTDOWN_POWEROFF) ||
          (msg->subtype == CMSG_SHUTDOWN_REBOOT) ||
          (msg->subtype == CMSG_SHUTDOWN_SUSPEND)) )
    {
        shutting_down = msg->subtype;
        schedule_work(&shutdown_work);
    }
    else
    {
        printk("Ignore spurious shutdown request\n");
    }

    ctrl_if_send_response(msg);
}

static int __init setup_shutdown_event(void)
{
    ctrl_if_register_receiver(CMSG_SHUTDOWN, shutdown_handler, 0);
    return 0;
}

__initcall(setup_shutdown_event);