reboot.c 10 KB

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