1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #include <geekos/reboot.h>
5 #include <libc/string.h>
9 #define RTC_PORT(x) (0x70 + (x))
11 /* The following code and data reboots the machine by switching to real
12 mode and jumping to the BIOS reset entry point, as if the CPU has
13 really been reset. The previous version asked the keyboard
14 controller to pulse the CPU reset line, which is more thorough, but
15 doesn't work with at least one type of 486 motherboard. It is easy
16 to stop this code working; hence the copious comments. */
18 static unsigned long long
19 real_mode_gdt_entries [3] =
21 0x0000000000000000ULL, /* Null descriptor */
22 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
23 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
28 unsigned short size __attribute__ ((packed));
29 unsigned long long * base __attribute__ ((packed));
31 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
32 real_mode_idt = { 0x3ff, 0 };
37 static unsigned char real_mode_switch [] =
39 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
40 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
41 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
42 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
43 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
44 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
45 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
46 0x74, 0x02, /* jz f */
47 0x0f, 0x09, /* wbinvd */
48 0x24, 0x10, /* f: andb $0x10,al */
49 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
51 static unsigned char jump_to_bios [] =
53 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
58 * Switch to real mode and then execute the code
59 * specified by the code and length parameters.
60 * We assume that length will aways be less that 100!
62 void machine_real_restart() {
63 // unsigned long flags;
65 unsigned short rtp1, rtp2;
66 unsigned char val1, val2;
68 //JRL// local_irq_disable();
70 /* Write zero to CMOS register number 0x0f, which the BIOS POST
71 routine will recognize as telling it to do a proper reboot. (Well
72 that's what this book in front of me says -- it may only apply to
73 the Phoenix BIOS though, it's not clear). At the same time,
74 disable NMIs by setting the top bit in the CMOS address register,
75 as we're about to do peculiar things to the CPU. I'm not sure if
76 `outb_p' is needed instead of just `outb'. Use it to be on the
77 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
80 //JRL// spin_lock_irqsave(&rtc_lock, flags);
81 //JRL// CMOS_WRITE(0x00, 0x8f);
87 // outb_p(0x8f, RTC_PORT(0));
88 // outb_p(0x00, RTC_PORT(1));
90 __asm__ __volatile__ (
93 : "a" (val1), "Nd" (rtp1)
96 __asm__ __volatile__ (
99 : "a" (val2), "Nd" (rtp2)
102 //JRL// spin_unlock_irqrestore(&rtc_lock, flags);
104 /* Remap the kernel at virtual address zero, as well as offset zero
105 from the kernel segment. This assumes the kernel segment starts at
106 virtual address PAGE_OFFSET. */
108 //JRL// memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
109 //JRL// sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
112 * Use `swapper_pg_dir' as our page directory.
114 //JRL// load_cr3(swapper_pg_dir);
116 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
117 this on booting to tell it to "Bypass memory test (also warm
118 boot)". This seems like a fairly standard thing that gets set by
119 REBOOT.COM programs, and the previous reset routine did this
122 *((unsigned short *)0x472) = 0x1234;
124 /* For the switch to real mode, copy some code to low memory. It has
125 to be in the first 64k because it is running in 16-bit mode, and it
126 has to have the same physical and virtual address, because it turns
127 off paging. Copy it near the end of the first page, out of the way
128 of BIOS variables. */
130 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
131 real_mode_switch, sizeof (real_mode_switch));
132 memcpy ((void *) (0x1000 - 100), jump_to_bios, sizeof(jump_to_bios));
134 /* Set up the IDT for real mode. */
136 __asm__ __volatile__ ("lidt %0" : : "m" (real_mode_idt));
138 /* Set up a GDT from which we can load segment descriptors for real
139 mode. The GDT is not used in real mode; it is just needed here to
140 prepare the descriptors. */
142 __asm__ __volatile__ ("lgdt %0" : : "m" (real_mode_gdt));
144 /* Load the data segment registers, and thus the descriptors ready for
145 real mode. The base address of each segment is 0x100, 16 times the
146 selector value being loaded here. This is so that the segment
147 registers don't have to be reloaded after switching to real mode:
148 the values are consistent for real mode operation already. */
150 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
151 "\tmovl %%eax,%%ds\n"
152 "\tmovl %%eax,%%es\n"
153 "\tmovl %%eax,%%fs\n"
154 "\tmovl %%eax,%%gs\n"
155 "\tmovl %%eax,%%ss" : : : "eax");
157 /* Jump to the 16-bit code that we copied earlier. It disables paging
158 and the cache, switches to real mode, and jumps to the BIOS reset
161 __asm__ __volatile__ ("ljmp $0x0008,%0"
163 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));