[PATCH] i386: fix bound check IDT gate
[linux-2.6/verdex.git] / arch / i386 / kernel / reboot.c
blob2afe0f8d555adf4b67130421e1750449f7f1c92a
1 /*
2 * linux/arch/i386/kernel/reboot.c
3 */
5 #include <linux/config.h>
6 #include <linux/mm.h>
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/mc146818rtc.h>
12 #include <linux/efi.h>
13 #include <linux/dmi.h>
14 #include <linux/ctype.h>
15 #include <asm/uaccess.h>
16 #include <asm/apic.h>
17 #include <asm/desc.h>
18 #include "mach_reboot.h"
19 #include <linux/reboot_fixups.h>
22 * Power off function, if any
24 void (*pm_power_off)(void);
25 EXPORT_SYMBOL(pm_power_off);
27 static int reboot_mode;
28 static int reboot_thru_bios;
30 #ifdef CONFIG_SMP
31 static int reboot_cpu = -1;
32 #endif
33 static int __init reboot_setup(char *str)
35 while(1) {
36 switch (*str) {
37 case 'w': /* "warm" reboot (no memory testing etc) */
38 reboot_mode = 0x1234;
39 break;
40 case 'c': /* "cold" reboot (with memory testing etc) */
41 reboot_mode = 0x0;
42 break;
43 case 'b': /* "bios" reboot by jumping through the BIOS */
44 reboot_thru_bios = 1;
45 break;
46 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
47 reboot_thru_bios = 0;
48 break;
49 #ifdef CONFIG_SMP
50 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
51 if (isdigit(*(str+1))) {
52 reboot_cpu = (int) (*(str+1) - '0');
53 if (isdigit(*(str+2)))
54 reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
56 /* we will leave sorting out the final value
57 when we are ready to reboot, since we might not
58 have set up boot_cpu_id or smp_num_cpu */
59 break;
60 #endif
62 if((str = strchr(str,',')) != NULL)
63 str++;
64 else
65 break;
67 return 1;
70 __setup("reboot=", reboot_setup);
73 * Reboot options and system auto-detection code provided by
74 * Dell Inc. so their systems "just work". :-)
78 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
80 static int __init set_bios_reboot(struct dmi_system_id *d)
82 if (!reboot_thru_bios) {
83 reboot_thru_bios = 1;
84 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
86 return 0;
89 static struct dmi_system_id __initdata reboot_dmi_table[] = {
90 { /* Handle problems with rebooting on Dell 1300's */
91 .callback = set_bios_reboot,
92 .ident = "Dell PowerEdge 1300",
93 .matches = {
94 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
95 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
98 { /* Handle problems with rebooting on Dell 300's */
99 .callback = set_bios_reboot,
100 .ident = "Dell PowerEdge 300",
101 .matches = {
102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
103 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
106 { /* Handle problems with rebooting on Dell 2400's */
107 .callback = set_bios_reboot,
108 .ident = "Dell PowerEdge 2400",
109 .matches = {
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
111 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
114 { /* Handle problems with rebooting on HP nc6120 */
115 .callback = set_bios_reboot,
116 .ident = "HP Compaq nc6120",
117 .matches = {
118 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
119 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nc6120"),
125 static int __init reboot_init(void)
127 dmi_check_system(reboot_dmi_table);
128 return 0;
131 core_initcall(reboot_init);
133 /* The following code and data reboots the machine by switching to real
134 mode and jumping to the BIOS reset entry point, as if the CPU has
135 really been reset. The previous version asked the keyboard
136 controller to pulse the CPU reset line, which is more thorough, but
137 doesn't work with at least one type of 486 motherboard. It is easy
138 to stop this code working; hence the copious comments. */
140 static unsigned long long
141 real_mode_gdt_entries [3] =
143 0x0000000000000000ULL, /* Null descriptor */
144 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
145 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
148 static struct
150 unsigned short size __attribute__ ((packed));
151 unsigned long long * base __attribute__ ((packed));
153 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
154 real_mode_idt = { 0x3ff, NULL },
155 no_idt = { 0, NULL };
158 /* This is 16-bit protected mode code to disable paging and the cache,
159 switch to real mode and jump to the BIOS reset code.
161 The instruction that switches to real mode by writing to CR0 must be
162 followed immediately by a far jump instruction, which set CS to a
163 valid value for real mode, and flushes the prefetch queue to avoid
164 running instructions that have already been decoded in protected
165 mode.
167 Clears all the flags except ET, especially PG (paging), PE
168 (protected-mode enable) and TS (task switch for coprocessor state
169 save). Flushes the TLB after paging has been disabled. Sets CD and
170 NW, to disable the cache on a 486, and invalidates the cache. This
171 is more like the state of a 486 after reset. I don't know if
172 something else should be done for other chips.
174 More could be done here to set up the registers as if a CPU reset had
175 occurred; hopefully real BIOSs don't assume much. */
177 static unsigned char real_mode_switch [] =
179 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
180 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
181 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
182 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
183 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
184 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
185 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
186 0x74, 0x02, /* jz f */
187 0x0f, 0x09, /* wbinvd */
188 0x24, 0x10, /* f: andb $0x10,al */
189 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
191 static unsigned char jump_to_bios [] =
193 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
197 * Switch to real mode and then execute the code
198 * specified by the code and length parameters.
199 * We assume that length will aways be less that 100!
201 void machine_real_restart(unsigned char *code, int length)
203 unsigned long flags;
205 local_irq_disable();
207 /* Write zero to CMOS register number 0x0f, which the BIOS POST
208 routine will recognize as telling it to do a proper reboot. (Well
209 that's what this book in front of me says -- it may only apply to
210 the Phoenix BIOS though, it's not clear). At the same time,
211 disable NMIs by setting the top bit in the CMOS address register,
212 as we're about to do peculiar things to the CPU. I'm not sure if
213 `outb_p' is needed instead of just `outb'. Use it to be on the
214 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
217 spin_lock_irqsave(&rtc_lock, flags);
218 CMOS_WRITE(0x00, 0x8f);
219 spin_unlock_irqrestore(&rtc_lock, flags);
221 /* Remap the kernel at virtual address zero, as well as offset zero
222 from the kernel segment. This assumes the kernel segment starts at
223 virtual address PAGE_OFFSET. */
225 memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
226 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
229 * Use `swapper_pg_dir' as our page directory.
231 load_cr3(swapper_pg_dir);
233 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
234 this on booting to tell it to "Bypass memory test (also warm
235 boot)". This seems like a fairly standard thing that gets set by
236 REBOOT.COM programs, and the previous reset routine did this
237 too. */
239 *((unsigned short *)0x472) = reboot_mode;
241 /* For the switch to real mode, copy some code to low memory. It has
242 to be in the first 64k because it is running in 16-bit mode, and it
243 has to have the same physical and virtual address, because it turns
244 off paging. Copy it near the end of the first page, out of the way
245 of BIOS variables. */
247 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
248 real_mode_switch, sizeof (real_mode_switch));
249 memcpy ((void *) (0x1000 - 100), code, length);
251 /* Set up the IDT for real mode. */
253 load_idt(&real_mode_idt);
255 /* Set up a GDT from which we can load segment descriptors for real
256 mode. The GDT is not used in real mode; it is just needed here to
257 prepare the descriptors. */
259 load_gdt(&real_mode_gdt);
261 /* Load the data segment registers, and thus the descriptors ready for
262 real mode. The base address of each segment is 0x100, 16 times the
263 selector value being loaded here. This is so that the segment
264 registers don't have to be reloaded after switching to real mode:
265 the values are consistent for real mode operation already. */
267 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
268 "\tmovl %%eax,%%ds\n"
269 "\tmovl %%eax,%%es\n"
270 "\tmovl %%eax,%%fs\n"
271 "\tmovl %%eax,%%gs\n"
272 "\tmovl %%eax,%%ss" : : : "eax");
274 /* Jump to the 16-bit code that we copied earlier. It disables paging
275 and the cache, switches to real mode, and jumps to the BIOS reset
276 entry point. */
278 __asm__ __volatile__ ("ljmp $0x0008,%0"
280 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
282 #ifdef CONFIG_APM_MODULE
283 EXPORT_SYMBOL(machine_real_restart);
284 #endif
286 void machine_shutdown(void)
288 #ifdef CONFIG_SMP
289 int reboot_cpu_id;
291 /* The boot cpu is always logical cpu 0 */
292 reboot_cpu_id = 0;
294 /* See if there has been given a command line override */
295 if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
296 cpu_isset(reboot_cpu, cpu_online_map)) {
297 reboot_cpu_id = reboot_cpu;
300 /* Make certain the cpu I'm rebooting on is online */
301 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
302 reboot_cpu_id = smp_processor_id();
305 /* Make certain I only run on the appropriate processor */
306 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
308 /* O.K. Now that I'm on the appropriate processor, stop
309 * all of the others, and disable their local APICs.
312 smp_send_stop();
313 #endif /* CONFIG_SMP */
315 lapic_shutdown();
317 #ifdef CONFIG_X86_IO_APIC
318 disable_IO_APIC();
319 #endif
322 void machine_emergency_restart(void)
324 if (!reboot_thru_bios) {
325 if (efi_enabled) {
326 efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
327 load_idt(&no_idt);
328 __asm__ __volatile__("int3");
330 /* rebooting needs to touch the page at absolute addr 0 */
331 *((unsigned short *)__va(0x472)) = reboot_mode;
332 for (;;) {
333 mach_reboot_fixups(); /* for board specific fixups */
334 mach_reboot();
335 /* That didn't work - force a triple fault.. */
336 load_idt(&no_idt);
337 __asm__ __volatile__("int3");
340 if (efi_enabled)
341 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
343 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
346 void machine_restart(char * __unused)
348 machine_shutdown();
349 machine_emergency_restart();
352 void machine_halt(void)
356 void machine_power_off(void)
358 machine_shutdown();
360 if (pm_power_off)
361 pm_power_off();