- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.git] / arch / i386 / kernel / reboot.c
1 /*
2  *  linux/arch/i386/kernel/reboot.c
3  */
4
5 #include <linux/mm.h>
6 #ifdef  CONFIG_KDB
7 #include <linux/kdb.h>
8 #endif  /* CONFIG_KDB */
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/mc146818rtc.h>
14 #include <linux/efi.h>
15 #include <linux/dmi.h>
16 #include <linux/ctype.h>
17 #include <linux/pm.h>
18 #include <linux/reboot.h>
19 #include <asm/uaccess.h>
20 #include <asm/apic.h>
21 #include <asm/desc.h>
22 #include "mach_reboot.h"
23 #include <asm/reboot_fixups.h>
24 #include <asm/reboot.h>
25
26 /*
27  * Power off function, if any
28  */
29 void (*pm_power_off)(void);
30 EXPORT_SYMBOL(pm_power_off);
31
32 static int reboot_mode;
33 static int reboot_thru_bios;
34
35 #ifdef CONFIG_SMP
36 static int reboot_cpu = -1;
37 #endif
38 static int __init reboot_setup(char *str)
39 {
40         while(1) {
41                 switch (*str) {
42                 case 'w': /* "warm" reboot (no memory testing etc) */
43                         reboot_mode = 0x1234;
44                         break;
45                 case 'c': /* "cold" reboot (with memory testing etc) */
46                         reboot_mode = 0x0;
47                         break;
48                 case 'b': /* "bios" reboot by jumping through the BIOS */
49                         reboot_thru_bios = 1;
50                         break;
51                 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
52                         reboot_thru_bios = 0;
53                         break;
54 #ifdef CONFIG_SMP
55                 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
56                         if (isdigit(*(str+1))) {
57                                 reboot_cpu = (int) (*(str+1) - '0');
58                                 if (isdigit(*(str+2)))
59                                         reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
60                         }
61                                 /* we will leave sorting out the final value 
62                                 when we are ready to reboot, since we might not
63                                 have set up boot_cpu_id or smp_num_cpu */
64                         break;
65 #endif
66                 }
67                 if((str = strchr(str,',')) != NULL)
68                         str++;
69                 else
70                         break;
71         }
72         return 1;
73 }
74
75 __setup("reboot=", reboot_setup);
76
77 /*
78  * Reboot options and system auto-detection code provided by
79  * Dell Inc. so their systems "just work". :-)
80  */
81
82 /*
83  * Some machines require the "reboot=b"  commandline option, this quirk makes that automatic.
84  */
85 static int __init set_bios_reboot(struct dmi_system_id *d)
86 {
87         if (!reboot_thru_bios) {
88                 reboot_thru_bios = 1;
89                 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
90         }
91         return 0;
92 }
93
94 static struct dmi_system_id __initdata reboot_dmi_table[] = {
95         {       /* Handle problems with rebooting on Dell 1300's */
96                 .callback = set_bios_reboot,
97                 .ident = "Dell PowerEdge 1300",
98                 .matches = {
99                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
100                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
101                 },
102         },
103         {       /* Handle problems with rebooting on Dell 300's */
104                 .callback = set_bios_reboot,
105                 .ident = "Dell PowerEdge 300",
106                 .matches = {
107                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
108                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
109                 },
110         },
111         {       /* Handle problems with rebooting on Dell 2400's */
112                 .callback = set_bios_reboot,
113                 .ident = "Dell PowerEdge 2400",
114                 .matches = {
115                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
116                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
117                 },
118         },
119         {       /* Handle problems with rebooting on HP laptops */
120                 .callback = set_bios_reboot,
121                 .ident = "HP Compaq Laptop",
122                 .matches = {
123                         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
124                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
125                 },
126         },
127         { }
128 };
129
130 static int __init reboot_init(void)
131 {
132         dmi_check_system(reboot_dmi_table);
133         return 0;
134 }
135
136 core_initcall(reboot_init);
137
138 /* The following code and data reboots the machine by switching to real
139    mode and jumping to the BIOS reset entry point, as if the CPU has
140    really been reset.  The previous version asked the keyboard
141    controller to pulse the CPU reset line, which is more thorough, but
142    doesn't work with at least one type of 486 motherboard.  It is easy
143    to stop this code working; hence the copious comments. */
144
145 static unsigned long long
146 real_mode_gdt_entries [3] =
147 {
148         0x0000000000000000ULL,  /* Null descriptor */
149         0x00009a000000ffffULL,  /* 16-bit real-mode 64k code at 0x00000000 */
150         0x000092000100ffffULL   /* 16-bit real-mode 64k data at 0x00000100 */
151 };
152
153 static struct Xgt_desc_struct
154 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, (long)real_mode_gdt_entries },
155 real_mode_idt = { 0x3ff, 0 },
156 no_idt = { 0, 0 };
157
158
159 /* This is 16-bit protected mode code to disable paging and the cache,
160    switch to real mode and jump to the BIOS reset code.
161
162    The instruction that switches to real mode by writing to CR0 must be
163    followed immediately by a far jump instruction, which set CS to a
164    valid value for real mode, and flushes the prefetch queue to avoid
165    running instructions that have already been decoded in protected
166    mode.
167
168    Clears all the flags except ET, especially PG (paging), PE
169    (protected-mode enable) and TS (task switch for coprocessor state
170    save).  Flushes the TLB after paging has been disabled.  Sets CD and
171    NW, to disable the cache on a 486, and invalidates the cache.  This
172    is more like the state of a 486 after reset.  I don't know if
173    something else should be done for other chips.
174
175    More could be done here to set up the registers as if a CPU reset had
176    occurred; hopefully real BIOSs don't assume much. */
177
178 static unsigned char real_mode_switch [] =
179 {
180         0x66, 0x0f, 0x20, 0xc0,                 /*    movl  %cr0,%eax        */
181         0x66, 0x83, 0xe0, 0x11,                 /*    andl  $0x00000011,%eax */
182         0x66, 0x0d, 0x00, 0x00, 0x00, 0x60,     /*    orl   $0x60000000,%eax */
183         0x66, 0x0f, 0x22, 0xc0,                 /*    movl  %eax,%cr0        */
184         0x66, 0x0f, 0x22, 0xd8,                 /*    movl  %eax,%cr3        */
185         0x66, 0x0f, 0x20, 0xc3,                 /*    movl  %cr0,%ebx        */
186         0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60,       /*    andl  $0x60000000,%ebx */
187         0x74, 0x02,                             /*    jz    f                */
188         0x0f, 0x09,                             /*    wbinvd                 */
189         0x24, 0x10,                             /* f: andb  $0x10,al         */
190         0x66, 0x0f, 0x22, 0xc0                  /*    movl  %eax,%cr0        */
191 };
192 static unsigned char jump_to_bios [] =
193 {
194         0xea, 0x00, 0x00, 0xff, 0xff            /*    ljmp  $0xffff,$0x0000  */
195 };
196
197 /*
198  * Switch to real mode and then execute the code
199  * specified by the code and length parameters.
200  * We assume that length will aways be less that 100!
201  */
202 void machine_real_restart(unsigned char *code, int length)
203 {
204         local_irq_disable();
205
206         /* Write zero to CMOS register number 0x0f, which the BIOS POST
207            routine will recognize as telling it to do a proper reboot.  (Well
208            that's what this book in front of me says -- it may only apply to
209            the Phoenix BIOS though, it's not clear).  At the same time,
210            disable NMIs by setting the top bit in the CMOS address register,
211            as we're about to do peculiar things to the CPU.  I'm not sure if
212            `outb_p' is needed instead of just `outb'.  Use it to be on the
213            safe side.  (Yes, CMOS_WRITE does outb_p's. -  Paul G.)
214          */
215
216         spin_lock(&rtc_lock);
217         CMOS_WRITE(0x00, 0x8f);
218         spin_unlock(&rtc_lock);
219
220         /* Remap the kernel at virtual address zero, as well as offset zero
221            from the kernel segment.  This assumes the kernel segment starts at
222            virtual address PAGE_OFFSET. */
223
224         memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
225                 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
226
227         /*
228          * Use `swapper_pg_dir' as our page directory.
229          */
230         load_cr3(swapper_pg_dir);
231
232         /* Write 0x1234 to absolute memory location 0x472.  The BIOS reads
233            this on booting to tell it to "Bypass memory test (also warm
234            boot)".  This seems like a fairly standard thing that gets set by
235            REBOOT.COM programs, and the previous reset routine did this
236            too. */
237
238         *((unsigned short *)0x472) = reboot_mode;
239
240         /* For the switch to real mode, copy some code to low memory.  It has
241            to be in the first 64k because it is running in 16-bit mode, and it
242            has to have the same physical and virtual address, because it turns
243            off paging.  Copy it near the end of the first page, out of the way
244            of BIOS variables. */
245
246         memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
247                 real_mode_switch, sizeof (real_mode_switch));
248         memcpy ((void *) (0x1000 - 100), code, length);
249
250         /* Set up the IDT for real mode. */
251
252         load_idt(&real_mode_idt);
253
254         /* Set up a GDT from which we can load segment descriptors for real
255            mode.  The GDT is not used in real mode; it is just needed here to
256            prepare the descriptors. */
257
258         load_gdt(&real_mode_gdt);
259
260         /* Load the data segment registers, and thus the descriptors ready for
261            real mode.  The base address of each segment is 0x100, 16 times the
262            selector value being loaded here.  This is so that the segment
263            registers don't have to be reloaded after switching to real mode:
264            the values are consistent for real mode operation already. */
265
266         __asm__ __volatile__ ("movl $0x0010,%%eax\n"
267                                 "\tmovl %%eax,%%ds\n"
268                                 "\tmovl %%eax,%%es\n"
269                                 "\tmovl %%eax,%%fs\n"
270                                 "\tmovl %%eax,%%gs\n"
271                                 "\tmovl %%eax,%%ss" : : : "eax");
272
273         /* Jump to the 16-bit code that we copied earlier.  It disables paging
274            and the cache, switches to real mode, and jumps to the BIOS reset
275            entry point. */
276
277         __asm__ __volatile__ ("ljmp $0x0008,%0"
278                                 :
279                                 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
280 }
281 #ifdef CONFIG_APM_MODULE
282 EXPORT_SYMBOL(machine_real_restart);
283 #endif
284
285 static void native_machine_shutdown(void)
286 {
287 #ifdef CONFIG_SMP
288         int reboot_cpu_id;
289
290         /* The boot cpu is always logical cpu 0 */
291         reboot_cpu_id = 0;
292
293         /* See if there has been given a command line override */
294         if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
295                 cpu_isset(reboot_cpu, cpu_online_map)) {
296                 reboot_cpu_id = reboot_cpu;
297         }
298
299         /* Make certain the cpu I'm rebooting on is online */
300         if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
301                 reboot_cpu_id = smp_processor_id();
302         }
303
304         /* Make certain I only run on the appropriate processor */
305         set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
306
307         /* O.K. Now that I'm on the appropriate processor, stop
308          * all of the others, and disable their local APICs.
309          */
310
311 #ifdef  CONFIG_KDB
312         /*
313          * If this restart is occuring while kdb is running (e.g. reboot
314          * command), the other CPU's are already stopped.  Don't try to
315          * stop them yet again.
316          */
317         if (!KDB_IS_RUNNING())
318 #endif  /* CONFIG_KDB */
319         smp_send_stop();
320 #endif /* CONFIG_SMP */
321
322         lapic_shutdown();
323
324 #ifdef CONFIG_X86_IO_APIC
325         disable_IO_APIC();
326 #endif
327 }
328
329 void __attribute__((weak)) mach_reboot_fixups(void)
330 {
331 }
332
333 static void native_machine_emergency_restart(void)
334 {
335         if (!reboot_thru_bios) {
336                 if (efi_enabled) {
337                         efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
338                         load_idt(&no_idt);
339                         __asm__ __volatile__("int3");
340                 }
341                 /* rebooting needs to touch the page at absolute addr 0 */
342                 *((unsigned short *)__va(0x472)) = reboot_mode;
343                 for (;;) {
344                         mach_reboot_fixups(); /* for board specific fixups */
345                         mach_reboot();
346                         /* That didn't work - force a triple fault.. */
347                         load_idt(&no_idt);
348                         __asm__ __volatile__("int3");
349                 }
350         }
351         if (efi_enabled)
352                 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
353
354         machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
355 }
356
357 static void native_machine_restart(char * __unused)
358 {
359         machine_shutdown();
360         machine_emergency_restart();
361 }
362
363 static void native_machine_halt(void)
364 {
365 }
366
367 static void native_machine_power_off(void)
368 {
369         if (pm_power_off) {
370                 machine_shutdown();
371                 pm_power_off();
372         }
373 }
374
375
376 struct machine_ops machine_ops = {
377         .power_off = native_machine_power_off,
378         .shutdown = native_machine_shutdown,
379         .emergency_restart = native_machine_emergency_restart,
380         .restart = native_machine_restart,
381         .halt = native_machine_halt,
382 };
383
384 void machine_power_off(void)
385 {
386         machine_ops.power_off();
387 }
388
389 void machine_shutdown(void)
390 {
391         machine_ops.shutdown();
392 }
393
394 void machine_emergency_restart(void)
395 {
396         machine_ops.emergency_restart();
397 }
398
399 void machine_restart(char *cmd)
400 {
401         machine_ops.restart(cmd);
402 }
403
404 void machine_halt(void)
405 {
406         machine_ops.halt();
407 }