2 * linux/arch/i386/kernel/reboot.c
6 #include <linux/module.h>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/mc146818rtc.h>
11 #include <linux/efi.h>
12 #include <asm/uaccess.h>
14 #include "mach_reboot.h"
17 * Power off function, if any
19 void (*pm_power_off)(void);
21 static int reboot_mode;
26 static int reboot_cpu = -1;
27 /* shamelessly grabbed from lib/vsprintf.c for readability */
28 #define is_digit(c) ((c) >= '0' && (c) <= '9')
30 static int __init reboot_setup(char *str)
34 case 'w': /* "warm" reboot (no memory testing etc) */
37 case 'c': /* "cold" reboot (with memory testing etc) */
40 case 'b': /* "bios" reboot by jumping through the BIOS */
43 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
47 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
49 if (is_digit(*(str+1))) {
50 reboot_cpu = (int) (*(str+1) - '0');
51 if (is_digit(*(str+2)))
52 reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
54 /* we will leave sorting out the final value
55 when we are ready to reboot, since we might not
56 have set up boot_cpu_id or smp_num_cpu */
60 if((str = strchr(str,',')) != NULL)
68 __setup("reboot=", reboot_setup);
70 /* The following code and data reboots the machine by switching to real
71 mode and jumping to the BIOS reset entry point, as if the CPU has
72 really been reset. The previous version asked the keyboard
73 controller to pulse the CPU reset line, which is more thorough, but
74 doesn't work with at least one type of 486 motherboard. It is easy
75 to stop this code working; hence the copious comments. */
77 static unsigned long long
78 real_mode_gdt_entries [3] =
80 0x0000000000000000ULL, /* Null descriptor */
81 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
82 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
87 unsigned short size __attribute__ ((packed));
88 unsigned long long * base __attribute__ ((packed));
90 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
91 real_mode_idt = { 0x3ff, 0 },
95 /* This is 16-bit protected mode code to disable paging and the cache,
96 switch to real mode and jump to the BIOS reset code.
98 The instruction that switches to real mode by writing to CR0 must be
99 followed immediately by a far jump instruction, which set CS to a
100 valid value for real mode, and flushes the prefetch queue to avoid
101 running instructions that have already been decoded in protected
104 Clears all the flags except ET, especially PG (paging), PE
105 (protected-mode enable) and TS (task switch for coprocessor state
106 save). Flushes the TLB after paging has been disabled. Sets CD and
107 NW, to disable the cache on a 486, and invalidates the cache. This
108 is more like the state of a 486 after reset. I don't know if
109 something else should be done for other chips.
111 More could be done here to set up the registers as if a CPU reset had
112 occurred; hopefully real BIOSs don't assume much. */
114 static unsigned char real_mode_switch [] =
116 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
117 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
118 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
119 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
120 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
121 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
122 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
123 0x74, 0x02, /* jz f */
124 0x0f, 0x09, /* wbinvd */
125 0x24, 0x10, /* f: andb $0x10,al */
126 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
128 static unsigned char jump_to_bios [] =
130 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
134 * Switch to real mode and then execute the code
135 * specified by the code and length parameters.
136 * We assume that length will aways be less that 100!
138 void machine_real_restart(unsigned char *code, int length)
144 /* Write zero to CMOS register number 0x0f, which the BIOS POST
145 routine will recognize as telling it to do a proper reboot. (Well
146 that's what this book in front of me says -- it may only apply to
147 the Phoenix BIOS though, it's not clear). At the same time,
148 disable NMIs by setting the top bit in the CMOS address register,
149 as we're about to do peculiar things to the CPU. I'm not sure if
150 `outb_p' is needed instead of just `outb'. Use it to be on the
151 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
154 spin_lock_irqsave(&rtc_lock, flags);
155 CMOS_WRITE(0x00, 0x8f);
156 spin_unlock_irqrestore(&rtc_lock, flags);
159 * Remap the first 16 MB of RAM (which includes the kernel image)
160 * at virtual address zero:
162 setup_identity_mappings(swapper_pg_dir, 0, LOW_MAPPINGS_SIZE);
165 * Use `swapper_pg_dir' as our page directory.
167 load_cr3(swapper_pg_dir);
169 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
170 this on booting to tell it to "Bypass memory test (also warm
171 boot)". This seems like a fairly standard thing that gets set by
172 REBOOT.COM programs, and the previous reset routine did this
175 *((unsigned short *)0x472) = reboot_mode;
177 /* For the switch to real mode, copy some code to low memory. It has
178 to be in the first 64k because it is running in 16-bit mode, and it
179 has to have the same physical and virtual address, because it turns
180 off paging. Copy it near the end of the first page, out of the way
181 of BIOS variables. */
183 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
184 real_mode_switch, sizeof (real_mode_switch));
185 memcpy ((void *) (0x1000 - 100), code, length);
187 /* Set up the IDT for real mode. */
189 __asm__ __volatile__ ("lidt %0" : : "m" (real_mode_idt));
191 /* Set up a GDT from which we can load segment descriptors for real
192 mode. The GDT is not used in real mode; it is just needed here to
193 prepare the descriptors. */
195 __asm__ __volatile__ ("lgdt %0" : : "m" (real_mode_gdt));
197 /* Load the data segment registers, and thus the descriptors ready for
198 real mode. The base address of each segment is 0x100, 16 times the
199 selector value being loaded here. This is so that the segment
200 registers don't have to be reloaded after switching to real mode:
201 the values are consistent for real mode operation already. */
203 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
204 "\tmovl %%eax,%%ds\n"
205 "\tmovl %%eax,%%es\n"
206 "\tmovl %%eax,%%fs\n"
207 "\tmovl %%eax,%%gs\n"
208 "\tmovl %%eax,%%ss" : : : "eax");
210 /* Jump to the 16-bit code that we copied earlier. It disables paging
211 and the cache, switches to real mode, and jumps to the BIOS reset
214 __asm__ __volatile__ ("ljmp $0x0008,%0"
216 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
219 void machine_restart(char * __unused)
224 cpuid = GET_APIC_ID(apic_read(APIC_ID));
228 /* check to see if reboot_cpu is valid
229 if its not, default to the BSP */
230 if ((reboot_cpu == -1) ||
231 (reboot_cpu > (NR_CPUS -1)) ||
232 !physid_isset(cpuid, phys_cpu_present_map))
233 reboot_cpu = boot_cpu_physical_apicid;
235 reboot_smp = 0; /* use this as a flag to only go through this once*/
236 /* re-run this function on the other CPUs
237 it will fall though this section since we have
238 cleared reboot_smp, and do the reboot if it is the
239 correct CPU, otherwise it halts. */
240 if (reboot_cpu != cpuid)
241 smp_call_function((void *)machine_restart , NULL, 1, 0);
244 /* if reboot_cpu is still -1, then we want a tradional reboot,
245 and if we are not running on the reboot_cpu,, halt */
246 if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
248 __asm__ __volatile__ ("hlt");
251 * Stop all CPUs and turn off local APICs and the IO-APIC, so
252 * other OSs see a clean IRQ state.
255 #elif defined(CONFIG_X86_LOCAL_APIC)
258 disable_local_APIC();
262 #ifdef CONFIG_X86_IO_APIC
266 if (!reboot_thru_bios) {
268 efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, 0);
269 __asm__ __volatile__("lidt %0": :"m" (no_idt));
270 __asm__ __volatile__("int3");
272 /* rebooting needs to touch the page at absolute addr 0 */
273 *((unsigned short *)__va(0x472)) = reboot_mode;
276 /* That didn't work - force a triple fault.. */
277 __asm__ __volatile__("lidt %0": :"m" (no_idt));
278 __asm__ __volatile__("int3");
282 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, 0);
284 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
287 EXPORT_SYMBOL(machine_restart);
289 void machine_halt(void)
293 EXPORT_SYMBOL(machine_halt);
295 void machine_power_off(void)
298 efi.reset_system(EFI_RESET_SHUTDOWN, EFI_SUCCESS, 0, 0);
303 EXPORT_SYMBOL(machine_power_off);