1 #define __KERNEL_SYSCALLS__
2 #include <linux/version.h>
3 #include <linux/kernel.h>
5 #include <linux/unistd.h>
6 #include <linux/module.h>
7 #include <linux/reboot.h>
8 #include <linux/syscalls.h>
9 #include <linux/sysrq.h>
10 #include <linux/stringify.h>
12 #include <asm/mmu_context.h>
13 #include <xen/evtchn.h>
14 #include <asm/hypervisor.h>
15 #include <xen/interface/dom0_ops.h>
16 #include <xen/xenbus.h>
17 #include <linux/cpu.h>
18 #include <linux/kthread.h>
19 #include <xen/gnttab.h>
20 #include <xen/xencons.h>
21 #include <xen/cpu_hotplug.h>
23 extern void ctrl_alt_del(void);
25 #define SHUTDOWN_INVALID -1
26 #define SHUTDOWN_POWEROFF 0
27 #define SHUTDOWN_SUSPEND 2
28 /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
29 * report a crash, not be instructed to crash!
30 * HALT is the same as POWEROFF, as far as we're concerned. The tools use
31 * the distinction when we return the reason code to them.
33 #define SHUTDOWN_HALT 4
35 #if defined(__i386__) || defined(__x86_64__)
38 * Power off function, if any
40 void (*pm_power_off)(void);
41 EXPORT_SYMBOL(pm_power_off);
43 void machine_emergency_restart(void)
45 /* We really want to get pending console data out before we die. */
46 xencons_force_flush();
47 HYPERVISOR_shutdown(SHUTDOWN_reboot);
50 void machine_restart(char * __unused)
52 machine_emergency_restart();
55 void machine_halt(void)
60 void machine_power_off(void)
62 /* We really want to get pending console data out before we die. */
63 xencons_force_flush();
66 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
69 int reboot_thru_bios = 0; /* for dmi_scan.c */
70 EXPORT_SYMBOL(machine_restart);
71 EXPORT_SYMBOL(machine_halt);
72 EXPORT_SYMBOL(machine_power_off);
74 #endif /* defined(__i386__) || defined(__x86_64__) */
76 /******************************************************************************
77 * Stop/pickle callback handling.
80 /* Ignore multiple shutdown requests. */
81 static int shutting_down = SHUTDOWN_INVALID;
82 static void __shutdown_handler(struct work_struct *work);
83 static DECLARE_DELAYED_WORK(shutdown_work, __shutdown_handler);
85 #if defined(__i386__) || defined(__x86_64__)
87 /* Ensure we run on the idle task page tables so that we will
88 switch page tables before running user space. This is needed
89 on architectures with separate kernel and user page tables
90 because the user page table pointer is not saved/restored. */
91 static void switch_idle_mm(void)
93 struct mm_struct *mm = current->active_mm;
98 atomic_inc(&init_mm.mm_count);
99 switch_mm(mm, &init_mm, current);
100 current->active_mm = &init_mm;
104 static void pre_suspend(void)
106 HYPERVISOR_shared_info = (shared_info_t *)empty_zero_page;
107 clear_fixmap(FIX_SHARED_INFO);
109 xen_start_info->store_mfn = mfn_to_pfn(xen_start_info->store_mfn);
110 xen_start_info->console.domU.mfn =
111 mfn_to_pfn(xen_start_info->console.domU.mfn);
114 static void post_suspend(void)
117 extern unsigned long max_pfn;
118 extern unsigned long *pfn_to_mfn_frame_list_list;
119 extern unsigned long *pfn_to_mfn_frame_list[];
121 set_fixmap(FIX_SHARED_INFO, xen_start_info->shared_info);
123 HYPERVISOR_shared_info = (shared_info_t *)fix_to_virt(FIX_SHARED_INFO);
125 memset(empty_zero_page, 0, PAGE_SIZE);
127 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
128 virt_to_mfn(pfn_to_mfn_frame_list_list);
130 fpp = PAGE_SIZE/sizeof(unsigned long);
131 for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) {
132 if ((j % fpp) == 0) {
134 pfn_to_mfn_frame_list_list[k] =
135 virt_to_mfn(pfn_to_mfn_frame_list[k]);
138 pfn_to_mfn_frame_list[k][j] =
139 virt_to_mfn(&phys_to_machine_mapping[i]);
141 HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
144 #else /* !(defined(__i386__) || defined(__x86_64__)) */
146 #define switch_idle_mm() ((void)0)
147 #define mm_pin_all() ((void)0)
148 #define pre_suspend() ((void)0)
149 #define post_suspend() ((void)0)
153 static int __do_suspend(void *ignore)
157 extern void time_resume(void);
159 BUG_ON(smp_processor_id() != 0);
160 BUG_ON(in_interrupt());
162 #if defined(__i386__) || defined(__x86_64__)
163 if (xen_feature(XENFEAT_auto_translated_physmap)) {
164 printk(KERN_WARNING "Cannot suspend in "
165 "auto_translated_physmap mode.\n");
187 * We'll stop somewhere inside this hypercall. When it returns,
188 * we'll start resuming after the restore.
190 HYPERVISOR_suspend(virt_to_mfn(xen_start_info));
192 shutting_down = SHUTDOWN_INVALID;
215 static int shutdown_process(void *__unused)
217 static char *envp[] = { "HOME=/", "TERM=linux",
218 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
219 static char *poweroff_argv[] = { "/sbin/poweroff", NULL };
221 extern asmlinkage long sys_reboot(int magic1, int magic2,
222 unsigned int cmd, void *arg);
224 if ((shutting_down == SHUTDOWN_POWEROFF) ||
225 (shutting_down == SHUTDOWN_HALT)) {
226 if (kernel_execve("/sbin/poweroff", poweroff_argv, envp) < 0) {
227 sys_reboot(LINUX_REBOOT_MAGIC1,
229 LINUX_REBOOT_CMD_POWER_OFF,
234 shutting_down = SHUTDOWN_INVALID; /* could try again */
239 static int kthread_create_on_cpu(int (*f)(void *arg),
244 struct task_struct *p;
245 p = kthread_create(f, arg, name);
248 kthread_bind(p, cpu);
253 static void __shutdown_handler(struct work_struct *unused)
257 if (shutting_down != SHUTDOWN_SUSPEND)
258 err = kernel_thread(shutdown_process, NULL,
259 CLONE_FS | CLONE_FILES);
261 err = kthread_create_on_cpu(__do_suspend, NULL, "suspend", 0);
264 printk(KERN_WARNING "Error creating shutdown process (%d): "
265 "retrying...\n", -err);
266 schedule_delayed_work(&shutdown_work, HZ/2);
270 static void shutdown_handler(struct xenbus_watch *watch,
271 const char **vec, unsigned int len)
274 struct xenbus_transaction xbt;
277 if (shutting_down != SHUTDOWN_INVALID)
281 err = xenbus_transaction_start(&xbt);
284 str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
285 /* Ignore read errors and empty reads. */
286 if (XENBUS_IS_ERR_READ(str)) {
287 xenbus_transaction_end(xbt, 1);
291 xenbus_write(xbt, "control", "shutdown", "");
293 err = xenbus_transaction_end(xbt, 0);
294 if (err == -EAGAIN) {
299 if (strcmp(str, "poweroff") == 0)
300 shutting_down = SHUTDOWN_POWEROFF;
301 else if (strcmp(str, "reboot") == 0)
303 else if (strcmp(str, "suspend") == 0)
304 shutting_down = SHUTDOWN_SUSPEND;
305 else if (strcmp(str, "halt") == 0)
306 shutting_down = SHUTDOWN_HALT;
308 printk("Ignoring shutdown request: %s\n", str);
309 shutting_down = SHUTDOWN_INVALID;
312 if (shutting_down != SHUTDOWN_INVALID)
313 schedule_delayed_work(&shutdown_work, 0);
318 static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
321 char sysrq_key = '\0';
322 struct xenbus_transaction xbt;
326 err = xenbus_transaction_start(&xbt);
329 if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
330 printk(KERN_ERR "Unable to read sysrq code in "
332 xenbus_transaction_end(xbt, 1);
336 if (sysrq_key != '\0')
337 xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
339 err = xenbus_transaction_end(xbt, 0);
343 #ifdef CONFIG_MAGIC_SYSRQ
344 if (sysrq_key != '\0')
345 handle_sysrq(sysrq_key, NULL);
349 static struct xenbus_watch shutdown_watch = {
350 .node = "control/shutdown",
351 .callback = shutdown_handler
354 static struct xenbus_watch sysrq_watch = {
355 .node ="control/sysrq",
356 .callback = sysrq_handler
359 static int setup_shutdown_watcher(struct notifier_block *notifier,
365 err = register_xenbus_watch(&shutdown_watch);
367 printk(KERN_ERR "Failed to set shutdown watcher\n");
369 err = register_xenbus_watch(&sysrq_watch);
371 printk(KERN_ERR "Failed to set sysrq watcher\n");
376 static int __init setup_shutdown_event(void)
378 static struct notifier_block xenstore_notifier = {
379 .notifier_call = setup_shutdown_watcher
381 register_xenstore_notifier(&xenstore_notifier);
385 subsys_initcall(setup_shutdown_event);