Rev 629 | Rev 693 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 629 | Rev 680 | ||
---|---|---|---|
1 | # |
1 | # |
2 | # Copyright (C) 2001-2004 Jakub Jermar |
2 | # Copyright (C) 2001-2004 Jakub Jermar |
3 | # All rights reserved. |
3 | # All rights reserved. |
4 | # |
4 | # |
5 | # Redistribution and use in source and binary forms, with or without |
5 | # Redistribution and use in source and binary forms, with or without |
6 | # modification, are permitted provided that the following conditions |
6 | # modification, are permitted provided that the following conditions |
7 | # are met: |
7 | # are met: |
8 | # |
8 | # |
9 | # - Redistributions of source code must retain the above copyright |
9 | # - Redistributions of source code must retain the above copyright |
10 | # notice, this list of conditions and the following disclaimer. |
10 | # notice, this list of conditions and the following disclaimer. |
11 | # - Redistributions in binary form must reproduce the above copyright |
11 | # - Redistributions in binary form must reproduce the above copyright |
12 | # notice, this list of conditions and the following disclaimer in the |
12 | # notice, this list of conditions and the following disclaimer in the |
13 | # documentation and/or other materials provided with the distribution. |
13 | # documentation and/or other materials provided with the distribution. |
14 | # - The name of the author may not be used to endorse or promote products |
14 | # - The name of the author may not be used to endorse or promote products |
15 | # derived from this software without specific prior written permission. |
15 | # derived from this software without specific prior written permission. |
16 | # |
16 | # |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | # |
27 | # |
28 | 28 | ||
29 | #include <arch/boot/boot.h> |
29 | #include <arch/boot/boot.h> |
30 | #include <arch/boot/memmap.h> |
30 | #include <arch/boot/memmap.h> |
31 | #include <arch/mm/page.h> |
31 | #include <arch/mm/page.h> |
32 | #include <arch/pm.h> |
32 | #include <arch/pm.h> |
33 | 33 | ||
34 | .section K_TEXT_START, "ax" |
34 | .section K_TEXT_START, "ax" |
35 | .global kernel_image_start |
- | |
36 | 35 | ||
37 | KTEXT=8 |
36 | KTEXT=8 |
38 | KDATA=16 |
37 | KDATA=16 |
39 | 38 | ||
40 | .code16 |
- | |
41 | # |
- | |
42 | # This is where we require any SPARTAN-kernel-compatible boot loader |
- | |
43 | # to pass control in real mode. |
- | |
44 | # |
- | |
45 | # Protected mode tables are statically initialised during compile |
- | |
46 | # time. So we can just load the respective table registers and |
- | |
47 | # switch to protected mode. |
- | |
48 | # |
- | |
49 | kernel_image_start: |
- | |
50 | cli |
- | |
51 | xorw %ax, %ax |
- | |
52 | movw %ax, %ds |
- | |
53 | movw %ax, %es |
- | |
54 | movw %ax, %ss # initialize stack segment register |
- | |
55 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
- | |
56 | - | ||
57 | call memmap_arch_init |
- | |
58 | - | ||
59 | lgdt real_bootstrap_gdtr_boot # initialize Global Descriptor Table register |
- | |
60 | - | ||
61 | movl %cr0, %eax |
- | |
62 | orl $0x1, %eax |
- | |
63 | movl %eax, %cr0 # switch to protected mode |
- | |
64 | - | ||
65 | jmpl $KTEXT, $boot_image_start |
- | |
66 | - | ||
67 | .code32 |
39 | .code32 |
68 | .align 4 |
40 | .align 4 |
69 | multiboot_header: |
41 | multiboot_header: |
70 | .long MULTIBOOT_HEADER_MAGIC |
42 | .long MULTIBOOT_HEADER_MAGIC |
71 | .long MULTIBOOT_HEADER_FLAGS |
43 | .long MULTIBOOT_HEADER_FLAGS |
72 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
44 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
73 | .long multiboot_header + BOOT_OFFSET |
45 | .long multiboot_header + BOOT_OFFSET |
74 | .long unmapped_ktext_start + BOOT_OFFSET |
46 | .long unmapped_ktext_start + BOOT_OFFSET |
75 | .long 0 |
47 | .long 0 |
76 | .long 0 |
48 | .long 0 |
77 | .long multiboot_image_start + BOOT_OFFSET |
49 | .long multiboot_image_start + BOOT_OFFSET |
78 | 50 | ||
79 | boot_image_start: |
- | |
80 | movw $KDATA, %ax |
- | |
81 | movw %ax, %es |
- | |
82 | movw %ax, %gs |
- | |
83 | movw %ax, %fs |
- | |
84 | movw %ax, %ds # kernel data + stack |
- | |
85 | movw %ax, %ss |
- | |
86 | - | ||
87 | movb $0xd1, %al # enable A20 using i8042 controller |
- | |
88 | outb %al, $0x64 |
- | |
89 | movb $0xdf, %al |
- | |
90 | outb %al, $0x60 |
- | |
91 | - | ||
92 | movl $BOOTSTRAP_OFFSET, %esi |
- | |
93 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %edi |
- | |
94 | movl $_hardcoded_kernel_size, %ecx |
- | |
95 | cld |
- | |
96 | rep movsb |
- | |
97 | - | ||
98 | call map_kernel # map kernel and turn paging on |
- | |
99 | - | ||
100 | call main_bsp # never returns |
- | |
101 | - | ||
102 | cli |
- | |
103 | hlt |
- | |
104 | - | ||
105 | multiboot_image_start: |
51 | multiboot_image_start: |
106 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
52 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
107 | 53 | ||
108 | lgdt protected_bootstrap_gdtr - 0x80000000 # initialize Global Descriptor Table register |
54 | lgdt protected_bootstrap_gdtr - 0x80000000 # initialize Global Descriptor Table register |
109 | 55 | ||
110 | movw $KDATA, %cx |
56 | movw $KDATA, %cx |
111 | movw %cx, %es |
57 | movw %cx, %es |
112 | movw %cx, %gs |
58 | movw %cx, %gs |
113 | movw %cx, %fs |
59 | movw %cx, %fs |
114 | movw %cx, %ds # kernel data + stack |
60 | movw %cx, %ds # kernel data + stack |
115 | movw %cx, %ss |
61 | movw %cx, %ss |
116 | 62 | ||
117 | jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET |
63 | jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET |
118 | multiboot_meeting_point: |
64 | multiboot_meeting_point: |
119 | 65 | ||
120 | pushl %ebx # save parameters from GRUB |
66 | pushl %ebx # save parameters from GRUB |
121 | pushl %eax |
67 | pushl %eax |
122 | 68 | ||
123 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi |
69 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi |
124 | movl $BOOTSTRAP_OFFSET, %edi |
70 | movl $BOOTSTRAP_OFFSET, %edi |
125 | movl $_hardcoded_unmapped_size, %ecx |
71 | movl $_hardcoded_unmapped_size, %ecx |
126 | cld |
72 | cld |
127 | rep movsb |
73 | rep movsb |
128 | 74 | ||
129 | call map_kernel # map kernel and turn paging on |
75 | call map_kernel # map kernel and turn paging on |
130 | 76 | ||
131 | popl %eax |
77 | popl %eax |
132 | popl %ebx |
78 | popl %ebx |
133 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
79 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
134 | je valid_boot |
80 | je valid_boot |
135 | 81 | ||
136 | xorl %ecx, %ecx # no memory size or map available |
82 | xorl %ecx, %ecx # no memory size or map available |
137 | movl %ecx, e801memorysize |
83 | movl %ecx, e801memorysize |
138 | movl %ecx, e820counter |
84 | movl %ecx, e820counter |
139 | 85 | ||
140 | jmp invalid_boot |
86 | jmp invalid_boot |
141 | 87 | ||
142 | valid_boot: |
88 | valid_boot: |
143 | 89 | ||
144 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
90 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
145 | 91 | ||
146 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
92 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
147 | jc mem_valid |
93 | jc mem_valid |
148 | 94 | ||
149 | xorl %ecx, %ecx |
95 | xorl %ecx, %ecx |
150 | jmp mem_invalid |
96 | jmp mem_invalid |
151 | 97 | ||
152 | mem_valid: |
98 | mem_valid: |
153 | movl 4(%ebx), %ecx # mbi->mem_lower |
99 | movl 4(%ebx), %ecx # mbi->mem_lower |
154 | addl 8(%ebx), %ecx # mbi->mem_upper |
100 | addl 8(%ebx), %ecx # mbi->mem_upper |
155 | 101 | ||
156 | mem_invalid: |
102 | mem_invalid: |
157 | movl %ecx, e801memorysize |
103 | movl %ecx, e801memorysize |
158 | 104 | ||
159 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
105 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
160 | jc mods_valid |
106 | jc mods_valid |
161 | 107 | ||
162 | xorl %ecx, %ecx |
108 | xorl %ecx, %ecx |
163 | xorl %edx, %edx |
109 | xorl %edx, %edx |
164 | jmp mods_invalid |
110 | jmp mods_invalid |
165 | 111 | ||
166 | mods_valid: |
112 | mods_valid: |
167 | movl 20(%ebx), %ecx # mbi->mods_count |
113 | movl 20(%ebx), %ecx # mbi->mods_count |
168 | cmpl $0, %ecx |
114 | cmpl $0, %ecx |
169 | je mods_invalid |
115 | je mods_invalid |
170 | 116 | ||
171 | movl 24(%ebx), %esi # mbi->mods_addr |
117 | movl 24(%ebx), %esi # mbi->mods_addr |
172 | movl 0(%esi), %edx # mods->mod_start |
118 | movl 0(%esi), %edx # mods->mod_start |
173 | movl 4(%esi), %ecx # mods->mod_end |
119 | movl 4(%esi), %ecx # mods->mod_end |
174 | subl %edx, %ecx |
120 | subl %edx, %ecx |
175 | addl $0x80000000, %edx |
121 | addl $0x80000000, %edx |
176 | 122 | ||
177 | mods_invalid: |
123 | mods_invalid: |
178 | movl %ecx, init_size |
124 | movl %ecx, init_size |
179 | movl %edx, init_addr |
125 | movl %edx, init_addr |
180 | 126 | ||
181 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
127 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
182 | jc mmap_valid |
128 | jc mmap_valid |
183 | 129 | ||
184 | xorl %edx, %edx |
130 | xorl %edx, %edx |
185 | jmp mmap_invalid |
131 | jmp mmap_invalid |
186 | 132 | ||
187 | mmap_valid: |
133 | mmap_valid: |
188 | movl 44(%ebx), %ecx # mbi->mmap_length |
134 | movl 44(%ebx), %ecx # mbi->mmap_length |
189 | movl 48(%ebx), %esi # mbi->mmap_addr |
135 | movl 48(%ebx), %esi # mbi->mmap_addr |
190 | movl $e820table, %edi |
136 | movl $e820table, %edi |
191 | xorl %edx, %edx |
137 | xorl %edx, %edx |
192 | 138 | ||
193 | mmap_loop: |
139 | mmap_loop: |
194 | cmpl $0, %ecx |
140 | cmpl $0, %ecx |
195 | jle mmap_end |
141 | jle mmap_end |
196 | 142 | ||
197 | movl 4(%esi), %eax # mmap->base_addr_low |
143 | movl 4(%esi), %eax # mmap->base_addr_low |
198 | movl %eax, (%edi) |
144 | movl %eax, (%edi) |
199 | 145 | ||
200 | movl 8(%esi), %eax # mmap->base_addr_high |
146 | movl 8(%esi), %eax # mmap->base_addr_high |
201 | movl %eax, 4(%edi) |
147 | movl %eax, 4(%edi) |
202 | 148 | ||
203 | movl 12(%esi), %eax # mmap->length_low |
149 | movl 12(%esi), %eax # mmap->length_low |
204 | movl %eax, 8(%edi) |
150 | movl %eax, 8(%edi) |
205 | 151 | ||
206 | movl 16(%esi), %eax # mmap->length_high |
152 | movl 16(%esi), %eax # mmap->length_high |
207 | movl %eax, 12(%edi) |
153 | movl %eax, 12(%edi) |
208 | 154 | ||
209 | movl 20(%esi), %eax # mmap->type |
155 | movl 20(%esi), %eax # mmap->type |
210 | movl %eax, 16(%edi) |
156 | movl %eax, 16(%edi) |
211 | 157 | ||
212 | movl (%esi), %eax # mmap->size |
158 | movl (%esi), %eax # mmap->size |
213 | addl $0x4, %eax |
159 | addl $0x4, %eax |
214 | addl %eax, %esi |
160 | addl %eax, %esi |
215 | subl %eax, %ecx |
161 | subl %eax, %ecx |
216 | addl $MEMMAP_E820_RECORD_SIZE, %edi |
162 | addl $MEMMAP_E820_RECORD_SIZE, %edi |
217 | incl %edx |
163 | incl %edx |
218 | jmp mmap_loop |
164 | jmp mmap_loop |
219 | 165 | ||
220 | mmap_end: |
166 | mmap_end: |
221 | 167 | ||
222 | mmap_invalid: |
168 | mmap_invalid: |
223 | movl %edx, e820counter |
169 | movl %edx, e820counter |
224 | 170 | ||
225 | invalid_boot: |
171 | invalid_boot: |
226 | 172 | ||
227 | call main_bsp - BOOT_OFFSET # never returns |
173 | call main_bsp - BOOT_OFFSET # never returns |
228 | 174 | ||
229 | cli |
175 | cli |
230 | hlt |
176 | hlt |
231 | 177 | ||
232 | .global map_kernel |
178 | .global map_kernel |
233 | map_kernel: |
179 | map_kernel: |
234 | # |
180 | # |
235 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
181 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
236 | # For simplicity, we map the entire 4G space. |
182 | # For simplicity, we map the entire 4G space. |
237 | # |
183 | # |
238 | movl %cr4, %ecx |
184 | movl %cr4, %ecx |
239 | orl $(1<<4), %ecx |
185 | orl $(1<<4), %ecx |
240 | movl %ecx, %cr4 # turn PSE on |
186 | movl %ecx, %cr4 # turn PSE on |
241 | 187 | ||
242 | movl $(page_directory+0), %esi |
188 | movl $(page_directory+0), %esi |
243 | movl $(page_directory+2048), %edi |
189 | movl $(page_directory+2048), %edi |
244 | xorl %ecx, %ecx |
190 | xorl %ecx, %ecx |
245 | xorl %ebx, %ebx |
191 | xorl %ebx, %ebx |
246 | 0: |
192 | 0: |
247 | movl $((1<<7)|(1<<0)), %eax |
193 | movl $((1<<7)|(1<<0)), %eax |
248 | orl %ebx, %eax |
194 | orl %ebx, %eax |
249 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M |
195 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M |
250 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M |
196 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M |
251 | addl $(4*1024*1024), %ebx |
197 | addl $(4*1024*1024), %ebx |
252 | 198 | ||
253 | incl %ecx |
199 | incl %ecx |
254 | cmpl $512, %ecx |
200 | cmpl $512, %ecx |
255 | jl 0b |
201 | jl 0b |
256 | 202 | ||
257 | movl %esi, %cr3 |
203 | movl %esi, %cr3 |
258 | 204 | ||
259 | # turn paging on |
205 | # turn paging on |
260 | movl %cr0, %ebx |
206 | movl %cr0, %ebx |
261 | orl $(1<<31), %ebx |
207 | orl $(1<<31), %ebx |
262 | movl %ebx, %cr0 |
208 | movl %ebx, %cr0 |
263 | ret |
209 | ret |
264 | 210 | ||
265 | 211 | ||
266 | .section K_DATA_START, "aw", @progbits |
212 | .section K_DATA_START, "aw", @progbits |
267 | 213 | ||
268 | .align 4096 |
214 | .align 4096 |
269 | page_directory: |
215 | page_directory: |
270 | .space 4096, 0 |
216 | .space 4096, 0 |
271 | 217 | ||
272 | .global real_bootstrap_gdtr_boot |
218 | .global real_bootstrap_gdtr_boot |
273 | real_bootstrap_gdtr_boot: |
219 | real_bootstrap_gdtr_boot: |
274 | .word selector(GDT_ITEMS) |
220 | .word selector(GDT_ITEMS) |
275 | .long KA2PA(gdt)-BOOT_OFFSET |
221 | .long KA2PA(gdt)-BOOT_OFFSET |
276 | 222 | ||
277 | 223 |