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.domU.mfn =
110 mfn_to_pfn(xen_start_info->console.domU.mfn);
113 static void post_suspend(void)
116 extern unsigned long max_pfn;
117 extern unsigned long *pfn_to_mfn_frame_list_list;
118 extern unsigned long *pfn_to_mfn_frame_list[];
120 set_fixmap(FIX_SHARED_INFO, xen_start_info->shared_info);
122 HYPERVISOR_shared_info = (shared_info_t *)fix_to_virt(FIX_SHARED_INFO);
124 memset(empty_zero_page, 0, PAGE_SIZE);
126 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
127 virt_to_mfn(pfn_to_mfn_frame_list_list);
129 fpp = PAGE_SIZE/sizeof(unsigned long);
130 for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) {
131 if ((j % fpp) == 0) {
133 pfn_to_mfn_frame_list_list[k] =
134 virt_to_mfn(pfn_to_mfn_frame_list[k]);
137 pfn_to_mfn_frame_list[k][j] =
138 virt_to_mfn(&phys_to_machine_mapping[i]);
140 HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
143 #else /* !(defined(__i386__) || defined(__x86_64__)) */
145 #define switch_idle_mm() ((void)0)
146 #define mm_pin_all() ((void)0)
147 #define pre_suspend() ((void)0)
148 #define post_suspend() ((void)0)
152 static int __do_suspend(void *ignore)
156 extern void time_resume(void);
158 BUG_ON(smp_processor_id() != 0);
159 BUG_ON(in_interrupt());
161 #if defined(__i386__) || defined(__x86_64__)
162 if (xen_feature(XENFEAT_auto_translated_physmap)) {
163 printk(KERN_WARNING "Cannot suspend in "
164 "auto_translated_physmap mode.\n");
186 * We'll stop somewhere inside this hypercall. When it returns,
187 * we'll start resuming after the restore.
189 HYPERVISOR_suspend(virt_to_mfn(xen_start_info));
191 shutting_down = SHUTDOWN_INVALID;
214 static int shutdown_process(void *__unused)
216 static char *envp[] = { "HOME=/", "TERM=linux",
217 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
218 static char *poweroff_argv[] = { "/sbin/poweroff", NULL };
220 extern asmlinkage long sys_reboot(int magic1, int magic2,
221 unsigned int cmd, void *arg);
223 if ((shutting_down == SHUTDOWN_POWEROFF) ||
224 (shutting_down == SHUTDOWN_HALT)) {
225 if (execve("/sbin/poweroff", poweroff_argv, envp) < 0) {
226 sys_reboot(LINUX_REBOOT_MAGIC1,
228 LINUX_REBOOT_CMD_POWER_OFF,
233 shutting_down = SHUTDOWN_INVALID; /* could try again */
238 static int kthread_create_on_cpu(int (*f)(void *arg),
243 struct task_struct *p;
244 p = kthread_create(f, arg, name);
247 kthread_bind(p, cpu);
252 static void __shutdown_handler(void *unused)
256 if (shutting_down != SHUTDOWN_SUSPEND)
257 err = kernel_thread(shutdown_process, NULL,
258 CLONE_FS | CLONE_FILES);
260 err = kthread_create_on_cpu(__do_suspend, NULL, "suspend", 0);
263 printk(KERN_WARNING "Error creating shutdown process (%d): "
264 "retrying...\n", -err);
265 schedule_delayed_work(&shutdown_work, HZ/2);
269 static void shutdown_handler(struct xenbus_watch *watch,
270 const char **vec, unsigned int len)
273 struct xenbus_transaction xbt;
276 if (shutting_down != SHUTDOWN_INVALID)
280 err = xenbus_transaction_start(&xbt);
283 str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
284 /* Ignore read errors and empty reads. */
285 if (XENBUS_IS_ERR_READ(str)) {
286 xenbus_transaction_end(xbt, 1);
290 xenbus_write(xbt, "control", "shutdown", "");
292 err = xenbus_transaction_end(xbt, 0);
293 if (err == -EAGAIN) {
298 if (strcmp(str, "poweroff") == 0)
299 shutting_down = SHUTDOWN_POWEROFF;
300 else if (strcmp(str, "reboot") == 0)
302 else if (strcmp(str, "suspend") == 0)
303 shutting_down = SHUTDOWN_SUSPEND;
304 else if (strcmp(str, "halt") == 0)
305 shutting_down = SHUTDOWN_HALT;
307 printk("Ignoring shutdown request: %s\n", str);
308 shutting_down = SHUTDOWN_INVALID;
311 if (shutting_down != SHUTDOWN_INVALID)
312 schedule_work(&shutdown_work);
317 static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
320 char sysrq_key = '\0';
321 struct xenbus_transaction xbt;
325 err = xenbus_transaction_start(&xbt);
328 if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
329 printk(KERN_ERR "Unable to read sysrq code in "
331 xenbus_transaction_end(xbt, 1);
335 if (sysrq_key != '\0')
336 xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
338 err = xenbus_transaction_end(xbt, 0);
342 #ifdef CONFIG_MAGIC_SYSRQ
343 if (sysrq_key != '\0')
344 handle_sysrq(sysrq_key, NULL, NULL);
348 static struct xenbus_watch shutdown_watch = {
349 .node = "control/shutdown",
350 .callback = shutdown_handler
353 static struct xenbus_watch sysrq_watch = {
354 .node ="control/sysrq",
355 .callback = sysrq_handler
358 static int setup_shutdown_watcher(struct notifier_block *notifier,
364 err = register_xenbus_watch(&shutdown_watch);
366 printk(KERN_ERR "Failed to set shutdown watcher\n");
368 err = register_xenbus_watch(&sysrq_watch);
370 printk(KERN_ERR "Failed to set sysrq watcher\n");
375 static int __init setup_shutdown_event(void)
377 static struct notifier_block xenstore_notifier = {
378 .notifier_call = setup_shutdown_watcher
380 register_xenstore_notifier(&xenstore_notifier);
384 subsys_initcall(setup_shutdown_event);