debuggers.hg

view xen/arch/x86/shutdown.c @ 10989:16aa4b417c6b

[XEN] Clean up shutdown handling and ignore opt_noreboot if dom0
shuts down cleanly. The option is intended only to retain information
on the local console in case of a crash.

Based on a patch from Muli Ben-Yehuda <muli@il.ibm.com>

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Mon Aug 07 15:35:06 2006 +0100 (2006-08-07)
parents dc3c59367403
children 33242afccb32
line source
1 /******************************************************************************
2 * arch/x86/shutdown.c
3 *
4 * x86-specific shutdown handling.
5 */
7 #include <xen/config.h>
8 #include <xen/init.h>
9 #include <xen/lib.h>
10 #include <xen/sched.h>
11 #include <xen/smp.h>
12 #include <xen/delay.h>
13 #include <xen/dmi.h>
14 #include <xen/irq.h>
15 #include <xen/console.h>
16 #include <xen/shutdown.h>
17 #include <asm/msr.h>
18 #include <asm/regs.h>
19 #include <asm/mc146818rtc.h>
20 #include <asm/system.h>
21 #include <asm/io.h>
22 #include <asm/processor.h>
23 #include <asm/mpspec.h>
25 /* reboot_str: comma-separated list of reboot options. */
26 static char __initdata reboot_str[10] = "";
27 string_param("reboot", reboot_str);
29 static long no_idt[2];
30 static int reboot_mode;
32 static inline void kb_wait(void)
33 {
34 int i;
36 for ( i = 0; i < 0x10000; i++ )
37 if ( (inb_p(0x64) & 0x02) == 0 )
38 break;
39 }
41 static void __attribute__((noreturn)) __machine_halt(void *unused)
42 {
43 for ( ; ; )
44 __asm__ __volatile__ ( "hlt" );
45 }
47 void machine_halt(void)
48 {
49 watchdog_disable();
50 console_start_sync();
51 smp_call_function(__machine_halt, NULL, 1, 0);
52 __machine_halt(NULL);
53 }
55 #ifdef __i386__
57 static int reboot_thru_bios;
59 /* The following code and data reboots the machine by switching to real
60 mode and jumping to the BIOS reset entry point, as if the CPU has
61 really been reset. The previous version asked the keyboard
62 controller to pulse the CPU reset line, which is more thorough, but
63 doesn't work with at least one type of 486 motherboard. It is easy
64 to stop this code working; hence the copious comments. */
66 static unsigned long long
67 real_mode_gdt_entries [3] =
68 {
69 0x0000000000000000ULL, /* Null descriptor */
70 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
71 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
72 };
74 static const struct
75 {
76 unsigned short size __attribute__ ((packed));
77 unsigned long long * base __attribute__ ((packed));
78 }
79 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
80 real_mode_idt = { 0x3ff, NULL };
83 /* This is 16-bit protected mode code to disable paging and the cache,
84 switch to real mode and jump to the BIOS reset code.
86 The instruction that switches to real mode by writing to CR0 must be
87 followed immediately by a far jump instruction, which set CS to a
88 valid value for real mode, and flushes the prefetch queue to avoid
89 running instructions that have already been decoded in protected
90 mode.
92 Clears all the flags except ET, especially PG (paging), PE
93 (protected-mode enable) and TS (task switch for coprocessor state
94 save). Flushes the TLB after paging has been disabled. Sets CD and
95 NW, to disable the cache on a 486, and invalidates the cache. This
96 is more like the state of a 486 after reset. I don't know if
97 something else should be done for other chips.
99 More could be done here to set up the registers as if a CPU reset had
100 occurred; hopefully real BIOSs don't assume much. */
102 static const unsigned char real_mode_switch [] =
103 {
104 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
105 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
106 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
107 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
108 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
109 0x0f, 0x20, 0xc2, /* movl %cr0,%edx */
110 0x66, 0x81, 0xe2, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%edx */
111 0x74, 0x02, /* jz f */
112 0x0f, 0x09, /* wbinvd */
113 0x24, 0x10, /* f: andb $0x10,al */
114 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
115 };
116 #define MAX_LENGTH 0x40
117 static const unsigned char jump_to_bios [] =
118 {
119 0xea, 0xf0, 0xff, 0x00, 0xf0 /* ljmp $0xf000,$0xfff0 */
120 };
122 /*
123 * Switch to real mode and then execute the code
124 * specified by the code and length parameters.
125 * We assume that length will aways be less that MAX_LENGTH!
126 */
127 static void machine_real_restart(const unsigned char *code, unsigned length)
128 {
129 local_irq_disable();
131 /* Write zero to CMOS register number 0x0f, which the BIOS POST
132 routine will recognize as telling it to do a proper reboot. (Well
133 that's what this book in front of me says -- it may only apply to
134 the Phoenix BIOS though, it's not clear). At the same time,
135 disable NMIs by setting the top bit in the CMOS address register,
136 as we're about to do peculiar things to the CPU. */
138 spin_lock(&rtc_lock);
139 CMOS_WRITE(0x00, 0x8f);
140 spin_unlock(&rtc_lock);
142 /* Identity-map virtual address zero. */
144 map_pages_to_xen(0, 0, 1, __PAGE_HYPERVISOR|MAP_SMALL_PAGES);
145 set_current(idle_vcpu[0]);
146 write_ptbase(idle_vcpu[0]);
148 /* For the switch to real mode, copy some code to low memory. It has
149 to be in the first 64k because it is running in 16-bit mode, and it
150 has to have the same physical and virtual address, because it turns
151 off paging. Copy it near the end of the first page, out of the way
152 of BIOS variables. */
154 memcpy((void *)(PAGE_SIZE - sizeof(real_mode_switch) - MAX_LENGTH),
155 real_mode_switch, sizeof(real_mode_switch));
156 memcpy((void *)(PAGE_SIZE - MAX_LENGTH), code, length);
158 /* Set up the IDT for real mode. */
160 __asm__ __volatile__("lidt %0": : "m" (real_mode_idt));
162 /* Set up a GDT from which we can load segment descriptors for real
163 mode. The GDT is not used in real mode; it is just needed here to
164 prepare the descriptors. */
166 __asm__ __volatile__("lgdt %0": : "m" (real_mode_gdt));
168 /* Load the data segment registers, and thus the descriptors ready for
169 real mode. The base address of each segment is 0x100, 16 times the
170 selector value being loaded here. This is so that the segment
171 registers don't have to be reloaded after switching to real mode:
172 the values are consistent for real mode operation already. */
174 __asm__ __volatile__ ("\tmov %0,%%ds\n"
175 "\tmov %0,%%es\n"
176 "\tmov %0,%%fs\n"
177 "\tmov %0,%%gs\n"
178 "\tmov %0,%%ss"
179 :
180 : "r" (0x0010));
182 /* Jump to the 16-bit code that we copied earlier. It disables paging
183 and the cache, switches to real mode, and jumps to the BIOS reset
184 entry point. */
186 __asm__ __volatile__ ("ljmp $0x0008,%0"
187 :
188 : "i" ((void *)(PAGE_SIZE -
189 sizeof(real_mode_switch) -
190 MAX_LENGTH)));
191 }
193 #else /* __x86_64__ */
195 #define machine_real_restart(x, y)
196 #define reboot_thru_bios 0
198 #endif
200 void machine_restart(char * __unused)
201 {
202 int i;
204 watchdog_disable();
205 console_start_sync();
207 local_irq_enable();
209 /* Ensure we are the boot CPU. */
210 if ( GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_physical_apicid )
211 {
212 /* Send IPI to the boot CPU (logical cpu 0). */
213 on_selected_cpus(cpumask_of_cpu(0), (void *)machine_restart,
214 NULL, 1, 0);
215 for ( ; ; )
216 safe_halt();
217 }
219 /*
220 * Stop all CPUs and turn off local APICs and the IO-APIC, so
221 * other OSs see a clean IRQ state.
222 */
223 smp_send_stop();
224 disable_IO_APIC();
225 hvm_disable();
227 /* Rebooting needs to touch the page at absolute address 0. */
228 *((unsigned short *)__va(0x472)) = reboot_mode;
230 if (reboot_thru_bios <= 0)
231 {
232 for ( ; ; )
233 {
234 /* Pulse the keyboard reset line. */
235 for ( i = 0; i < 100; i++ )
236 {
237 kb_wait();
238 udelay(50);
239 outb(0xfe,0x64); /* pulse reset low */
240 udelay(50);
241 }
243 /* That didn't work - force a triple fault.. */
244 __asm__ __volatile__("lidt %0": "=m" (no_idt));
245 __asm__ __volatile__("int3");
246 }
247 }
248 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
249 }
251 #ifndef reboot_thru_bios
252 static int __init set_bios_reboot(struct dmi_system_id *d)
253 {
254 if ( !reboot_thru_bios )
255 {
256 reboot_thru_bios = 1;
257 printk("%s series board detected. "
258 "Selecting BIOS-method for reboots.\n", d->ident);
259 }
260 return 0;
261 }
263 static struct dmi_system_id __initdata reboot_dmi_table[] = {
264 { /* Handle problems with rebooting on Dell 1300's */
265 .callback = set_bios_reboot,
266 .ident = "Dell PowerEdge 1300",
267 .matches = {
268 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
269 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
270 },
271 },
272 { /* Handle problems with rebooting on Dell 300's */
273 .callback = set_bios_reboot,
274 .ident = "Dell PowerEdge 300",
275 .matches = {
276 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
277 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
278 },
279 },
280 { /* Handle problems with rebooting on Dell 2400's */
281 .callback = set_bios_reboot,
282 .ident = "Dell PowerEdge 2400",
283 .matches = {
284 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
285 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
286 },
287 },
288 { /* Handle problems with rebooting on HP laptops */
289 .callback = set_bios_reboot,
290 .ident = "HP Compaq Laptop",
291 .matches = {
292 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
293 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
294 },
295 },
296 { }
297 };
298 #endif
300 static int __init reboot_init(void)
301 {
302 const char *str;
304 for ( str = reboot_str; *str != '\0'; str++ )
305 {
306 switch ( *str )
307 {
308 case 'n': /* no reboot */
309 opt_noreboot = 1;
310 break;
311 case 'w': /* "warm" reboot (no memory testing etc) */
312 reboot_mode = 0x1234;
313 break;
314 case 'c': /* "cold" reboot (with memory testing etc) */
315 reboot_mode = 0x0;
316 break;
317 #ifndef reboot_thru_bios
318 case 'b': /* "bios" reboot by jumping through the BIOS */
319 reboot_thru_bios = 1;
320 break;
321 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
322 reboot_thru_bios = -1;
323 break;
324 #endif
325 }
326 if ( (str = strchr(str, ',')) == NULL )
327 break;
328 }
330 #ifndef reboot_thru_bios
331 dmi_check_system(reboot_dmi_table);
332 #endif
333 return 0;
334 }
335 __initcall(reboot_init);