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