reboot.c 11 KB

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