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/sysrq.h>
9 #include <linux/stringify.h>
11 #include <asm/mmu_context.h>
12 #include <xen/evtchn.h>
13 #include <asm/hypervisor.h>
14 #include <xen/interface/dom0_ops.h>
15 #include <xen/xenbus.h>
16 #include <linux/cpu.h>
17 #include <linux/kthread.h>
18 #include <xen/gnttab.h>
19 #include <xen/xencons.h>
20 #include <xen/cpu_hotplug.h>
22 extern void ctrl_alt_del(void);
24 #define SHUTDOWN_INVALID -1
25 #define SHUTDOWN_POWEROFF 0
26 #define SHUTDOWN_SUSPEND 2
27 /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
28 * report a crash, not be instructed to crash!
29 * HALT is the same as POWEROFF, as far as we're concerned. The tools use
30 * the distinction when we return the reason code to them.
32 #define SHUTDOWN_HALT 4
34 #if defined(__i386__) || defined(__x86_64__)
37 * Power off function, if any
39 void (*pm_power_off)(void);
40 EXPORT_SYMBOL(pm_power_off);
42 void machine_emergency_restart(void)
44 /* We really want to get pending console data out before we die. */
45 xencons_force_flush();
46 HYPERVISOR_shutdown(SHUTDOWN_reboot);
49 void machine_restart(char * __unused)
51 machine_emergency_restart();
54 void machine_halt(void)
59 void machine_power_off(void)
61 /* We really want to get pending console data out before we die. */
62 xencons_force_flush();
65 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
68 int reboot_thru_bios = 0; /* for dmi_scan.c */
69 EXPORT_SYMBOL(machine_restart);
70 EXPORT_SYMBOL(machine_halt);
71 EXPORT_SYMBOL(machine_power_off);
73 #endif /* defined(__i386__) || defined(__x86_64__) */
75 /******************************************************************************
76 * Stop/pickle callback handling.
79 /* Ignore multiple shutdown requests. */
80 static int shutting_down = SHUTDOWN_INVALID;
81 static void __shutdown_handler(void *unused);
82 static DECLARE_WORK(shutdown_work, __shutdown_handler, NULL);
84 #if defined(__i386__) || defined(__x86_64__)
86 /* Ensure we run on the idle task page tables so that we will
87 switch page tables before running user space. This is needed
88 on architectures with separate kernel and user page tables
89 because the user page table pointer is not saved/restored. */
90 static void switch_idle_mm(void)
92 struct mm_struct *mm = current->active_mm;
97 atomic_inc(&init_mm.mm_count);
98 switch_mm(mm, &init_mm, current);
99 current->active_mm = &init_mm;
103 static void pre_suspend(void)
105 HYPERVISOR_shared_info = (shared_info_t *)empty_zero_page;
106 clear_fixmap(FIX_SHARED_INFO);
108 xen_start_info->store_mfn = mfn_to_pfn(xen_start_info->store_mfn);
109 xen_start_info->console_mfn = mfn_to_pfn(xen_start_info->console_mfn);
112 static void post_suspend(void)
115 extern unsigned long max_pfn;
116 extern unsigned long *pfn_to_mfn_frame_list_list;
117 extern unsigned long *pfn_to_mfn_frame_list[];
119 set_fixmap(FIX_SHARED_INFO, xen_start_info->shared_info);
121 HYPERVISOR_shared_info = (shared_info_t *)fix_to_virt(FIX_SHARED_INFO);
123 memset(empty_zero_page, 0, PAGE_SIZE);
125 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
126 virt_to_mfn(pfn_to_mfn_frame_list_list);
128 fpp = PAGE_SIZE/sizeof(unsigned long);
129 for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) {
130 if ((j % fpp) == 0) {
132 pfn_to_mfn_frame_list_list[k] =
133 virt_to_mfn(pfn_to_mfn_frame_list[k]);
136 pfn_to_mfn_frame_list[k][j] =
137 virt_to_mfn(&phys_to_machine_mapping[i]);
139 HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
142 #else /* !(defined(__i386__) || defined(__x86_64__)) */
144 #define switch_idle_mm() ((void)0)
145 #define mm_pin_all() ((void)0)
146 #define pre_suspend() ((void)0)
147 #define post_suspend() ((void)0)
151 static int __do_suspend(void *ignore)
155 extern void time_resume(void);
157 BUG_ON(smp_processor_id() != 0);
158 BUG_ON(in_interrupt());
160 #if defined(__i386__) || defined(__x86_64__)
161 if (xen_feature(XENFEAT_auto_translated_physmap)) {
162 printk(KERN_WARNING "Cannot suspend in "
163 "auto_translated_physmap mode.\n");
185 * We'll stop somewhere inside this hypercall. When it returns,
186 * we'll start resuming after the restore.
188 HYPERVISOR_suspend(virt_to_mfn(xen_start_info));
190 shutting_down = SHUTDOWN_INVALID;
213 static int shutdown_process(void *__unused)
215 static char *envp[] = { "HOME=/", "TERM=linux",
216 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
217 static char *poweroff_argv[] = { "/sbin/poweroff", NULL };
219 extern asmlinkage long sys_reboot(int magic1, int magic2,
220 unsigned int cmd, void *arg);
222 if ((shutting_down == SHUTDOWN_POWEROFF) ||
223 (shutting_down == SHUTDOWN_HALT)) {
224 if (execve("/sbin/poweroff", poweroff_argv, envp) < 0) {
225 sys_reboot(LINUX_REBOOT_MAGIC1,
227 LINUX_REBOOT_CMD_POWER_OFF,
232 shutting_down = SHUTDOWN_INVALID; /* could try again */
237 static int kthread_create_on_cpu(int (*f)(void *arg),
242 struct task_struct *p;
243 p = kthread_create(f, arg, name);
246 kthread_bind(p, cpu);
251 static void __shutdown_handler(void *unused)
255 if (shutting_down != SHUTDOWN_SUSPEND)
256 err = kernel_thread(shutdown_process, NULL,
257 CLONE_FS | CLONE_FILES);
259 err = kthread_create_on_cpu(__do_suspend, NULL, "suspend", 0);
262 printk(KERN_WARNING "Error creating shutdown process (%d): "
263 "retrying...\n", -err);
264 schedule_delayed_work(&shutdown_work, HZ/2);
268 static void shutdown_handler(struct xenbus_watch *watch,
269 const char **vec, unsigned int len)
272 struct xenbus_transaction xbt;
275 if (shutting_down != SHUTDOWN_INVALID)
279 err = xenbus_transaction_start(&xbt);
282 str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
283 /* Ignore read errors and empty reads. */
284 if (XENBUS_IS_ERR_READ(str)) {
285 xenbus_transaction_end(xbt, 1);
289 xenbus_write(xbt, "control", "shutdown", "");
291 err = xenbus_transaction_end(xbt, 0);
292 if (err == -EAGAIN) {
297 if (strcmp(str, "poweroff") == 0)
298 shutting_down = SHUTDOWN_POWEROFF;
299 else if (strcmp(str, "reboot") == 0)
301 else if (strcmp(str, "suspend") == 0)
302 shutting_down = SHUTDOWN_SUSPEND;
303 else if (strcmp(str, "halt") == 0)
304 shutting_down = SHUTDOWN_HALT;
306 printk("Ignoring shutdown request: %s\n", str);
307 shutting_down = SHUTDOWN_INVALID;
310 if (shutting_down != SHUTDOWN_INVALID)
311 schedule_work(&shutdown_work);
316 static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
319 char sysrq_key = '\0';
320 struct xenbus_transaction xbt;
324 err = xenbus_transaction_start(&xbt);
327 if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
328 printk(KERN_ERR "Unable to read sysrq code in "
330 xenbus_transaction_end(xbt, 1);
334 if (sysrq_key != '\0')
335 xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
337 err = xenbus_transaction_end(xbt, 0);
341 #ifdef CONFIG_MAGIC_SYSRQ
342 if (sysrq_key != '\0')
343 handle_sysrq(sysrq_key, NULL, NULL);
347 static struct xenbus_watch shutdown_watch = {
348 .node = "control/shutdown",
349 .callback = shutdown_handler
352 static struct xenbus_watch sysrq_watch = {
353 .node ="control/sysrq",
354 .callback = sysrq_handler
357 static int setup_shutdown_watcher(struct notifier_block *notifier,
363 err = register_xenbus_watch(&shutdown_watch);
365 printk(KERN_ERR "Failed to set shutdown watcher\n");
367 err = register_xenbus_watch(&sysrq_watch);
369 printk(KERN_ERR "Failed to set sysrq watcher\n");
374 static int __init setup_shutdown_event(void)
376 static struct notifier_block xenstore_notifier = {
377 .notifier_call = setup_shutdown_watcher
379 register_xenstore_notifier(&xenstore_notifier);
383 subsys_initcall(setup_shutdown_event);