]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/bootstrap_custom/server/src/ARCH-x86/crt0.S
l4: bootstrap: Add some commented code for experiments with paging.
[l4.git] / l4 / pkg / bootstrap_custom / server / src / ARCH-x86 / crt0.S
1 /*
2  * (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *          Frank Mehnert <fm3@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10 #include <l4/sys/compiler.h>
11 #ifdef JAILHOUSE
12 #include "inmate.h" 
13
14 #define X86_CR0_PE              0x00000001
15 #define X86_CR0_WP              0x00010000
16 #define X86_CR0_PG              0x80000000
17
18 #define X86_CR4_PSE             0x00000010
19
20 #define MSR_MTRR_DEF_TYPE       0x000002ff
21 #define MTRR_ENABLE             0x00000800
22
23 .code16
24     .section ".jh.boot", "ax"
25
26     .globl __reset_entry
27 __reset_entry:
28     ljmp $0xf000,$start16
29
30     .section ".jh.startup", "ax"
31
32 start16:
33     lgdtl %cs:gdt_ptr
34
35     mov %cr0,%eax
36     or $X86_CR0_PE,%al
37     mov %eax,%cr0
38
39     ljmpl $INMATE_CS32,$_start
40
41 .code32
42
43 .global loader_gdt
44 loader_gdt:
45         .quad   0
46         .quad   0x00cf9b000000ffff
47         .quad   0x00af9b000000ffff
48         .quad   0x00cf93000000ffff
49
50 gdt_ptr:
51         .short  gdt_ptr - loader_gdt - 1
52         .long   loader_gdt + FSEGMENT_BASE
53
54         .align(4096)
55         .global loader_pdpt
56 loader_pdpt:
57         .long   0x00000083
58         .align(4096)
59 #endif 
60
61         
62         .section .init
63
64         .globl  _start
65 _start:
66 #ifdef REALMODE_LOADING 
67         /*
68         mov %cr4,%eax
69         or $X86_CR4_PSE,%eax
70         mov %eax,%cr4
71
72         mov $loader_pdpt + FSEGMENT_BASE,%eax
73         mov %eax,%cr3
74
75         mov $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE),%eax
76         mov %eax,%cr0
77         
78         movl $MSR_MTRR_DEF_TYPE,%ecx
79         rdmsr
80         or $MTRR_ENABLE,%eax
81         wrmsr
82         */
83         cld
84         cli
85         mov     $(3 * 8), %eax
86         mov     %eax, %ds
87         mov     %eax, %es
88         mov     %eax, %fs
89         mov     %eax, %gs
90         mov     %eax, %ss
91         lss     _stack_seg, %esp
92 #else
93         leal    _stack, %esp
94 #endif
95         pushl   $0
96         pushl   %esi    /* Pointer to real mode or Xen start_info pointer */
97         pushl   %eax
98         pushl   %ebx
99
100 #ifndef XEN
101         /* initialize vector for exception 6 */
102         movl    $trap6_entry,%eax
103         movw    %ax, _idt_offset_low
104         shrl    $16, %eax
105         movw    %ax, _idt_offset_high
106         movw    %cs, %ax
107         movw    %ax, _idt_selector
108
109         /* load tiny interrupt descriptor table to catch exception 6 */
110         lidtl   _idtdesc
111 #endif
112
113         pushl   $_exit
114         jmp     __main
115
116         /* Show an error message and wait for keypress to reboot. */
117 trap6_entry:
118         pusha
119         cld
120
121         jmp 8f
122
123         /* printhex
124          * edx: value to print
125          * eax: location on screen
126          */
127 99:     mov     %eax, %edi
128         add     $(2*8), %edi
129 98:     mov     $numbers,%ebx
130         mov     %edx,%ecx
131         shr     $28,%ecx
132         shl     $4,%edx
133         movzbl  (%ebx,%ecx,1),%ecx
134         mov     $0x4, %ch
135         mov     %cx,(%eax)
136         add     $0x2,%eax
137         cmp     %edi,%eax
138         jne     98b
139         ret
140
141 8:
142         /* print out warning */
143         movl    $trap6_warning, %esi
144         movl    $(0xb8000 + (24*80*2)), %edi
145         movb    $0x0F, %ah
146         
147 1:      /* read next character from string */
148         lodsb
149
150         /* check for end-of-string */
151         cmpb    $0, %al
152         je      3f
153
154         /* Check for hex number */
155         cmpb    $'      ', %al
156         jne     4f
157         pusha
158         mov     (32 + 32)(%esp),%edx
159         mov     %edi, %eax
160         call    99b
161         popa
162         add     $(8*2), %edi
163         jmp     1b
164         
165 4:      /* check for newline */
166         cmpb    $'\n', %al
167         jne     2f
168         
169         pushl   %eax
170         pushl   %esi
171         
172         /* move screen upwards 1 line */
173         movl    $((24*80*2)/4), %ecx
174         movl    $(0xb8000 + 1*80*2), %esi
175         movl    $0xb8000, %edi
176         rep     movsl
177
178         /* clear last line of screen */
179         movl    $((1*80*2)/4), %ecx
180         movl    $0x07200720, %eax
181         rep     stosl
182
183         popl    %esi
184         popl    %eax
185         
186         /* jump to new line */
187         movl    $(0xb8000 + (24*80*2)), %edi
188         jmp     1b
189         
190 2:      /* print character */
191         stosw
192         jmp     1b
193
194 3:      /* wait for keypress */
195         inb     $0x64, %al
196         testb   $0x01, %al
197         je      3b
198
199         movb    %al, %ah
200
201         /* empty keyboard buffer */
202         inb     $0x60, %al
203         
204         /* ignore PS/2 mouse events */
205         testb   $0x20, %ah
206         jne     3b
207
208         jmp     reboot
209
210
211         /* the warning text */
212 trap6_warning:
213         .ascii  "\n"
214         .ascii  "Bootstrap: Invalid opcode at    detected!\n"
215         .ascii  "Be sure that you don't use any CPU optimization flags like\n"
216         .ascii  "           -march=i686\n"
217         .ascii  "when compiling Bootstrap and the depending libraries.\n"
218         .ascii  "\n"
219         .asciz  "           Press any key to reboot..."
220
221 numbers:
222         .ascii  "0123456789abcdef"
223
224         /* MultiBoot header - see multiboot.h.  */
225         .p2align(2)
226
227 #define MULTIBOOT_MEMORY_INFO 0x00000002
228 _mb_header:
229         .long   0x1BADB002              /* magic */
230         .long   MULTIBOOT_MEMORY_INFO   /* flags: AOUT_KLUDGE */
231         .long   0 - 0x1BADB002 - MULTIBOOT_MEMORY_INFO
232
233 #ifndef XEN
234         .word   0
235 _idtdesc:
236         .word   (7*8)-1
237         .long   _idt
238
239 _idt:
240         .word   0,0,0,0                 /* trap 0 */
241         .word   0,0,0,0                 /* trap 1 */
242         .word   0,0,0,0                 /* trap 2 */
243         .word   0,0,0,0                 /* trap 3 */
244         .word   0,0,0,0                 /* trap 4 */
245         .word   0,0,0,0                 /* trap 5 */
246
247 _idt_offset_low:
248         .word   0
249 _idt_selector:
250         .word   0
251         .byte   0
252         .byte   0xee
253 _idt_offset_high:
254         .word   0
255 #endif
256
257 #ifdef REALMODE_LOADING
258 _stack_seg:
259         .long   _stack
260         .long   (3 * 8)
261 #endif
262
263         .bss
264         .space  8192
265         .globl  _stack
266 _stack:
267
268