ia64/xen-unstable

view xen/arch/x86/shutdown.c @ 9909:d7c99adf8a35

A few functions should be static in shutdown.c.

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