Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Initial revision
[palacios.git] / palacios / src / geekos / reboot.c
1 #include <geekos/reboot.h>
2 #include <libc/string.h>
3 // from linux...
4
5
6 #define RTC_PORT(x)     (0x70 + (x))
7
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. */
14
15 static unsigned long long
16 real_mode_gdt_entries [3] =
17 {
18         0x0000000000000000ULL,  /* Null descriptor */
19         0x00009a000000ffffULL,  /* 16-bit real-mode 64k code at 0x00000000 */
20         0x000092000100ffffULL   /* 16-bit real-mode 64k data at 0x00000100 */
21 };
22
23 static struct
24 {
25         unsigned short       size __attribute__ ((packed));
26         unsigned long long * base __attribute__ ((packed));
27 }
28 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
29   real_mode_idt = { 0x3ff, 0 };
30 //no_idt = { 0, 0 };
31
32
33
34 static unsigned char real_mode_switch [] =
35 {
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        */
47 };
48 static unsigned char jump_to_bios [] =
49 {
50         0xea, 0x00, 0x00, 0xff, 0xff            /*    ljmp  $0xffff,$0x0000  */
51 };
52
53
54 /*
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!
58  */
59 void machine_real_restart() {
60   //    unsigned long flags;
61
62         unsigned short rtp1, rtp2;
63         unsigned char val1, val2;
64
65         //JRL// local_irq_disable();
66
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.)
75          */
76
77 //JRL//  spin_lock_irqsave(&rtc_lock, flags);
78 //JRL// CMOS_WRITE(0x00, 0x8f);
79         val1 = 0x8f;
80         val2 = 0x00;
81         rtp1 = RTC_PORT(0);
82         rtp2 = RTC_PORT(1);
83
84         //      outb_p(0x8f, RTC_PORT(0));
85         //      outb_p(0x00, RTC_PORT(1));
86
87         __asm__ __volatile__ (
88                               "outb %b0, %w1"
89                               :
90                               : "a" (val1), "Nd" (rtp1)
91                               );
92
93         __asm__ __volatile__ (
94                               "outb %b0, %w1"
95                               :
96                               : "a" (val2), "Nd" (rtp2)
97                               );
98
99 //JRL//  spin_unlock_irqrestore(&rtc_lock, flags);
100
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. */
104
105 //JRL//  memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
106 //JRL//         sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
107
108         /*
109          * Use `swapper_pg_dir' as our page directory.
110          */
111 //JRL//         load_cr3(swapper_pg_dir);
112
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
117            too. */
118
119         *((unsigned short *)0x472) = 0x1234;
120
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. */
126
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));
130
131         /* Set up the IDT for real mode. */
132
133         __asm__ __volatile__ ("lidt %0" : : "m" (real_mode_idt));
134
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. */
138
139         __asm__ __volatile__ ("lgdt %0" : : "m" (real_mode_gdt));
140
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. */
146
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");
153
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
156            entry point. */
157
158         __asm__ __volatile__ ("ljmp $0x0008,%0"
159                                 :
160                                 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
161 }