]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/bootstrap_custom/server/src/ARCH-x86/crt0.S
Some minor fixes.
[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 #ifdef JAILHOUSE
79         movl $MSR_MTRR_DEF_TYPE,%ecx
80         rdmsr
81         or $MTRR_ENABLE,%eax
82         wrmsr
83 #endif  //JAILHOUSE
84         cld
85         cli
86         mov     $(3 * 8), %eax
87         mov     %eax, %ds
88         mov     %eax, %es
89         mov     %eax, %fs
90         mov     %eax, %gs
91         mov     %eax, %ss
92         lss     _stack_seg, %esp
93 #else
94         leal    _stack, %esp
95 #endif
96         pushl   $0
97         pushl   $0      /* No Xen start_info pointer */
98         pushl   %eax
99         pushl   %ebx
100
101 #ifndef XEN
102         /* initialize vector for exception 6 */
103         movl    $trap6_entry,%eax
104         movw    %ax, _idt_offset_low
105         shrl    $16, %eax
106         movw    %ax, _idt_offset_high
107         movw    %cs, %ax
108         movw    %ax, _idt_selector
109
110         /* load tiny interrupt descriptor table to catch exception 6 */
111         lidtl   _idtdesc
112 #endif
113
114         pushl   $_exit
115         jmp     __main
116
117         /* Show an error message and wait for keypress to reboot. */
118 trap6_entry:
119         pusha
120         cld
121
122         jmp 8f
123
124         /* printhex
125          * edx: value to print
126          * eax: location on screen
127          */
128 99:     mov     %eax, %edi
129         add     $(2*8), %edi
130 98:     mov     $numbers,%ebx
131         mov     %edx,%ecx
132         shr     $28,%ecx
133         shl     $4,%edx
134         movzbl  (%ebx,%ecx,1),%ecx
135         mov     $0x4, %ch
136         mov     %cx,(%eax)
137         add     $0x2,%eax
138         cmp     %edi,%eax
139         jne     98b
140         ret
141
142 8:
143         /* print out warning */
144         movl    $trap6_warning, %esi
145         movl    $(0xb8000 + (24*80*2)), %edi
146         movb    $0x0F, %ah
147         
148 1:      /* read next character from string */
149         lodsb
150
151         /* check for end-of-string */
152         cmpb    $0, %al
153         je      3f
154
155         /* Check for hex number */
156         cmpb    $'      ', %al
157         jne     4f
158         pusha
159         mov     (32 + 32)(%esp),%edx
160         mov     %edi, %eax
161         call    99b
162         popa
163         add     $(8*2), %edi
164         jmp     1b
165         
166 4:      /* check for newline */
167         cmpb    $'\n', %al
168         jne     2f
169         
170         pushl   %eax
171         pushl   %esi
172         
173         /* move screen upwards 1 line */
174         movl    $((24*80*2)/4), %ecx
175         movl    $(0xb8000 + 1*80*2), %esi
176         movl    $0xb8000, %edi
177         rep     movsl
178
179         /* clear last line of screen */
180         movl    $((1*80*2)/4), %ecx
181         movl    $0x07200720, %eax
182         rep     stosl
183
184         popl    %esi
185         popl    %eax
186         
187         /* jump to new line */
188         movl    $(0xb8000 + (24*80*2)), %edi
189         jmp     1b
190         
191 2:      /* print character */
192         stosw
193         jmp     1b
194
195 3:      /* wait for keypress */
196         inb     $0x64, %al
197         testb   $0x01, %al
198         je      3b
199
200         movb    %al, %ah
201
202         /* empty keyboard buffer */
203         inb     $0x60, %al
204         
205         /* ignore PS/2 mouse events */
206         testb   $0x20, %ah
207         jne     3b
208
209         jmp     reboot
210
211
212         /* the warning text */
213 trap6_warning:
214         .ascii  "\n"
215         .ascii  "Bootstrap: Invalid opcode at    detected!\n"
216         .ascii  "Be sure that you don't use any CPU optimization flags like\n"
217         .ascii  "           -march=i686\n"
218         .ascii  "when compiling Bootstrap and the depending libraries.\n"
219         .ascii  "\n"
220         .asciz  "           Press any key to reboot..."
221
222 numbers:
223         .ascii  "0123456789abcdef"
224
225         /* MultiBoot header - see multiboot.h.  */
226         .p2align(2)
227
228 #define MULTIBOOT_MEMORY_INFO 0x00000002
229 _mb_header:
230         .long   0x1BADB002              /* magic */
231         .long   MULTIBOOT_MEMORY_INFO   /* flags: AOUT_KLUDGE */
232         .long   0 - 0x1BADB002 - MULTIBOOT_MEMORY_INFO
233
234 #ifndef XEN
235         .word   0
236 _idtdesc:
237         .word   (7*8)-1
238         .long   _idt
239
240 _idt:
241         .word   0,0,0,0                 /* trap 0 */
242         .word   0,0,0,0                 /* trap 1 */
243         .word   0,0,0,0                 /* trap 2 */
244         .word   0,0,0,0                 /* trap 3 */
245         .word   0,0,0,0                 /* trap 4 */
246         .word   0,0,0,0                 /* trap 5 */
247
248 _idt_offset_low:
249         .word   0
250 _idt_selector:
251         .word   0
252         .byte   0
253         .byte   0xee
254 _idt_offset_high:
255         .word   0
256 #endif
257
258 #ifdef REALMODE_LOADING
259 _stack_seg:
260         .long   _stack
261         .long   (3 * 8)
262 #endif
263
264         .bss
265         .space  8192
266         .globl  _stack
267 _stack:
268
269