2009-06-22 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / kern / i386 / realmode.S
blob075aa57e901ea05554e7364bf40139b0922518f2
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007 Free Software Foundation, Inc.
4  *
5  *  GRUB is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  GRUB is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17  */
21  * Note: These functions defined in this file may be called from C.
22  *       Be careful of that you must not modify some registers. Quote
23  *       from gcc-2.95.2/gcc/config/i386/i386.h:
25    1 for registers not available across function calls.
26    These must include the FIXED_REGISTERS and also any
27    registers that can be used without being saved.
28    The latter must include the registers where values are returned
29    and the register where structure-value addresses are passed.
30    Aside from that, you can include as many other registers as you like.
32   ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg
33 {  1, 1, 1, 0, 0, 0, 0, 1, 1,  1,  1,  1,  1,  1,  1,  1,  1 }
34  */
37  * Note: GRUB is compiled with the options -mrtd and -mregparm=3.
38  *       So the first three arguments are passed in %eax, %edx, and %ecx,
39  *       respectively, and if a function has a fixed number of arguments
40  *       and the number if greater than three, the function must return
41  *       with "ret $N" where N is ((the number of arguments) - 3) * 4.
42  */
45  *  This is the area for all of the special variables.
46  */
48         .p2align        2       /* force 4-byte alignment */
50 protstack:
51         .long   GRUB_MEMORY_MACHINE_PROT_STACK
54  * This is the Global Descriptor Table
55  *
56  *  An entry, a "Segment Descriptor", looks like this:
57  *
58  * 31          24         19   16                 7           0
59  * ------------------------------------------------------------
60  * |             | |B| |A|       | |   |1|0|E|W|A|            |
61  * | BASE 31..24 |G|/|L|V| LIMIT |P|DPL|  TYPE   | BASE 23:16 |  4
62  * |             | |D| |L| 19..16| |   |1|1|C|R|A|            |
63  * ------------------------------------------------------------
64  * |                             |                            |
65  * |        BASE 15..0           |       LIMIT 15..0          |  0
66  * |                             |                            |
67  * ------------------------------------------------------------
68  *
69  *  Note the ordering of the data items is reversed from the above
70  *  description.
71  */
73         .p2align        2       /* force 4-byte alignment */
74 gdt:
75         .word   0, 0
76         .byte   0, 0, 0, 0
78         /* -- code segment --
79          * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity), present
80          * type = 32bit code execute/read, DPL = 0
81          */
82         .word   0xFFFF, 0
83         .byte   0, 0x9A, 0xCF, 0
85         /* -- data segment --
86          * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
87          * type = 32 bit data read/write, DPL = 0
88          */
89         .word   0xFFFF, 0
90         .byte   0, 0x92, 0xCF, 0
92         /* -- 16 bit real mode CS --
93          * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
94          * type = 16 bit code execute/read only/conforming, DPL = 0
95          */
96         .word   0xFFFF, 0
97         .byte   0, 0x9E, 0, 0
99         /* -- 16 bit real mode DS --
100          * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
101          * type = 16 bit data read/write, DPL = 0
102          */
103         .word   0xFFFF, 0
104         .byte   0, 0x92, 0, 0
107 /* this is the GDT descriptor */
108 gdtdesc:
109         .word   0x27                    /* limit */
110         .long   gdt                     /* addr */
113  *  These next two routines, "real_to_prot" and "prot_to_real" are structured
114  *  in a very specific way.  Be very careful when changing them.
116  *  NOTE:  Use of either one messes up %eax and %ebp.
117  */
119 real_to_prot:
120         .code16
121         cli
123         /* load the GDT register */
124 #ifdef APPLE_CC
125         mov %cs, %ax
126         mov %ax, %ds
127         DATA32  ADDR32  lgdt    gdtdesc
128 #else
129         DATA32  ADDR32  lgdt    %cs:gdtdesc
130 #endif
132         /* turn on protected mode */
133         movl    %cr0, %eax
134         orl     $GRUB_MEMORY_MACHINE_CR0_PE_ON, %eax
135         movl    %eax, %cr0
137         /* jump to relocation, flush prefetch queue, and reload %cs */
138         DATA32  ljmp    $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
140         .code32
141 protcseg:
142         /* reload other segment registers */
143         movw    $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
144         movw    %ax, %ds
145         movw    %ax, %es
146         movw    %ax, %fs
147         movw    %ax, %gs
148         movw    %ax, %ss
150         /* put the return address in a known safe location */
151         movl    (%esp), %eax
152         movl    %eax, GRUB_MEMORY_MACHINE_REAL_STACK
154         /* get protected mode stack */
155         movl    protstack, %eax
156         movl    %eax, %esp
157         movl    %eax, %ebp
159         /* get return address onto the right stack */
160         movl    GRUB_MEMORY_MACHINE_REAL_STACK, %eax
161         movl    %eax, (%esp)
163         /* zero %eax */
164         xorl    %eax, %eax
166         /* return on the old (or initialized) stack! */
167         ret
169 prot_to_real:
170         /* just in case, set GDT */
171         lgdt    gdtdesc
173         /* save the protected mode stack */
174         movl    %esp, %eax
175         movl    %eax, protstack
177         /* get the return address */
178         movl    (%esp), %eax
179         movl    %eax, GRUB_MEMORY_MACHINE_REAL_STACK
181         /* set up new stack */
182         movl    $GRUB_MEMORY_MACHINE_REAL_STACK, %eax
183         movl    %eax, %esp
184         movl    %eax, %ebp
186         /* set up segment limits */
187         movw    $GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
188         movw    %ax, %ds
189         movw    %ax, %es
190         movw    %ax, %fs
191         movw    %ax, %gs
192         movw    %ax, %ss
194         /* this might be an extra step */
195         /* jump to a 16 bit segment */
196         ljmp    $GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
198 tmpcseg:
199         .code16
201         /* clear the PE bit of CR0 */
202         movl    %cr0, %eax
203         andl    $(~GRUB_MEMORY_MACHINE_CR0_PE_ON), %eax
204         movl    %eax, %cr0
206         /* flush prefetch queue, reload %cs */
207         DATA32  ljmp    $0, $realcseg
209 realcseg:
210         /* we are in real mode now
211          * set up the real mode segment registers : DS, SS, ES
212          */
213         /* zero %eax */
214         xorl    %eax, %eax
216         movw    %ax, %ds
217         movw    %ax, %es
218         movw    %ax, %fs
219         movw    %ax, %gs
220         movw    %ax, %ss
222         /* restore interrupts */
223         sti
225         /* return on new stack! */
226         DATA32  ret
228         .code32