Rev 501 | Rev 532 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 501 | Rev 506 | ||
---|---|---|---|
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/memmapasm.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 |
35 | .global kernel_image_start |
36 | 36 | ||
37 | KTEXT=8 |
37 | KTEXT=8 |
38 | KDATA=16 |
38 | KDATA=16 |
39 | 39 | ||
40 | .code16 |
40 | .code16 |
41 | # |
41 | # |
42 | # This is where we require any SPARTAN-kernel-compatible boot loader |
42 | # This is where we require any SPARTAN-kernel-compatible boot loader |
43 | # to pass control in real mode. |
43 | # to pass control in real mode. |
44 | # |
44 | # |
45 | # Protected mode tables are statically initialised during compile |
45 | # Protected mode tables are statically initialised during compile |
46 | # time. So we can just load the respective table registers and |
46 | # time. So we can just load the respective table registers and |
47 | # switch to protected mode. |
47 | # switch to protected mode. |
48 | # |
48 | # |
49 | kernel_image_start: |
49 | kernel_image_start: |
50 | cli |
50 | cli |
51 | xorw %ax, %ax |
51 | xorw %ax, %ax |
52 | movw %ax, %ds |
52 | movw %ax, %ds |
53 | movw %ax, %es |
53 | movw %ax, %es |
54 | movw %ax, %ss # initialize stack segment register |
54 | movw %ax, %ss # initialize stack segment register |
55 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
55 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
56 | 56 | ||
57 | call memmap_arch_init |
57 | call memmap_arch_init |
58 | 58 | ||
59 | lgdt real_bootstrap_gdtr_boot # initialize Global Descriptor Table register |
59 | lgdt real_bootstrap_gdtr_boot # initialize Global Descriptor Table register |
60 | 60 | ||
61 | movl %cr0, %eax |
61 | movl %cr0, %eax |
62 | orl $0x1, %eax |
62 | orl $0x1, %eax |
63 | movl %eax, %cr0 # switch to protected mode |
63 | movl %eax, %cr0 # switch to protected mode |
64 | 64 | ||
65 | jmpl $KTEXT, $boot_image_start |
65 | jmpl $KTEXT, $boot_image_start |
66 | 66 | ||
67 | .code32 |
67 | .code32 |
68 | .align 4 |
68 | .align 4 |
69 | multiboot_header: |
69 | multiboot_header: |
70 | .long MULTIBOOT_HEADER_MAGIC |
70 | .long MULTIBOOT_HEADER_MAGIC |
71 | .long MULTIBOOT_HEADER_FLAGS |
71 | .long MULTIBOOT_HEADER_FLAGS |
72 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
72 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
73 | .long multiboot_header + BOOT_OFFSET |
73 | .long multiboot_header + BOOT_OFFSET |
74 | .long unmapped_ktext_start + BOOT_OFFSET |
74 | .long unmapped_ktext_start + BOOT_OFFSET |
75 | .long 0 |
75 | .long 0 |
76 | .long 0 |
76 | .long 0 |
77 | .long multiboot_image_start + BOOT_OFFSET |
77 | .long multiboot_image_start + BOOT_OFFSET |
78 | 78 | ||
79 | boot_image_start: |
79 | boot_image_start: |
80 | movw $KDATA, %ax |
80 | movw $KDATA, %ax |
81 | movw %ax, %es |
81 | movw %ax, %es |
82 | movw %ax, %gs |
82 | movw %ax, %gs |
83 | movw %ax, %fs |
83 | movw %ax, %fs |
84 | movw %ax, %ds # kernel data + stack |
84 | movw %ax, %ds # kernel data + stack |
85 | movw %ax, %ss |
85 | movw %ax, %ss |
86 | 86 | ||
87 | movb $0xd1, %al # enable A20 using i8042 controller |
87 | movb $0xd1, %al # enable A20 using i8042 controller |
88 | outb %al, $0x64 |
88 | outb %al, $0x64 |
89 | movb $0xdf, %al |
89 | movb $0xdf, %al |
90 | outb %al, $0x60 |
90 | outb %al, $0x60 |
91 | 91 | ||
92 | movl $BOOTSTRAP_OFFSET, %esi |
92 | movl $BOOTSTRAP_OFFSET, %esi |
93 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %edi |
93 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %edi |
94 | movl $_hardcoded_kernel_size, %ecx |
94 | movl $_hardcoded_kernel_size, %ecx |
95 | cld |
95 | cld |
96 | rep movsb |
96 | rep movsb |
97 | 97 | ||
98 | call map_kernel # map kernel and turn paging on |
98 | call map_kernel # map kernel and turn paging on |
99 | 99 | ||
100 | call main_bsp # never returns |
100 | call main_bsp # never returns |
101 | 101 | ||
102 | cli |
102 | cli |
103 | hlt |
103 | hlt |
104 | 104 | ||
105 | multiboot_image_start: |
105 | multiboot_image_start: |
106 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
106 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
107 | 107 | ||
108 | lgdt protected_bootstrap_gdtr - 0x80000000 # initialize Global Descriptor Table register |
108 | lgdt protected_bootstrap_gdtr - 0x80000000 # initialize Global Descriptor Table register |
109 | 109 | ||
110 | movw $KDATA, %cx |
110 | movw $KDATA, %cx |
111 | movw %cx, %es |
111 | movw %cx, %es |
112 | movw %cx, %gs |
112 | movw %cx, %gs |
113 | movw %cx, %fs |
113 | movw %cx, %fs |
114 | movw %cx, %ds # kernel data + stack |
114 | movw %cx, %ds # kernel data + stack |
115 | movw %cx, %ss |
115 | movw %cx, %ss |
116 | 116 | ||
117 | jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET |
117 | jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET |
118 | multiboot_meeting_point: |
118 | multiboot_meeting_point: |
119 | 119 | ||
120 | pushl %ebx # save parameters from GRUB |
120 | pushl %ebx # save parameters from GRUB |
121 | pushl %eax |
121 | pushl %eax |
122 | 122 | ||
123 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi |
123 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi |
124 | movl $BOOTSTRAP_OFFSET, %edi |
124 | movl $BOOTSTRAP_OFFSET, %edi |
125 | movl $_hardcoded_unmapped_size, %ecx |
125 | movl $_hardcoded_unmapped_size, %ecx |
126 | cld |
126 | cld |
127 | rep movsb |
127 | rep movsb |
128 | 128 | ||
129 | call map_kernel # map kernel and turn paging on |
129 | call map_kernel # map kernel and turn paging on |
130 | 130 | ||
131 | popl %eax |
131 | popl %eax |
132 | popl %ebx |
132 | popl %ebx |
133 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
133 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
134 | je valid_boot |
134 | je valid_boot |
135 | 135 | ||
136 | xorl %ecx, %ecx # no memory size or map available |
136 | xorl %ecx, %ecx # no memory size or map available |
137 | movl %ecx, e801memorysize |
137 | movl %ecx, e801memorysize |
138 | movl %ecx, e820counter |
138 | movl %ecx, e820counter |
139 | 139 | ||
140 | jmp invalid_boot |
140 | jmp invalid_boot |
141 | 141 | ||
142 | valid_boot: |
142 | valid_boot: |
143 | 143 | ||
144 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
144 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
145 | 145 | ||
146 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
146 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
147 | jc mem_valid |
147 | jc mem_valid |
148 | 148 | ||
149 | xorl %ecx, %ecx |
149 | xorl %ecx, %ecx |
150 | jmp mem_invalid |
150 | jmp mem_invalid |
151 | 151 | ||
152 | mem_valid: |
152 | mem_valid: |
153 | movl 4(%ebx), %ecx # mbi->mem_lower |
153 | movl 4(%ebx), %ecx # mbi->mem_lower |
154 | addl 8(%ebx), %ecx # mbi->mem_upper |
154 | addl 8(%ebx), %ecx # mbi->mem_upper |
155 | 155 | ||
156 | mem_invalid: |
156 | mem_invalid: |
157 | movl %ecx, e801memorysize |
157 | movl %ecx, e801memorysize |
158 | 158 | ||
- | 159 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
|
- | 160 | jc mods_valid |
|
- | 161 | ||
- | 162 | xorl %ecx, %ecx |
|
- | 163 | xorl %edx, %edx |
|
- | 164 | jmp mods_invalid |
|
- | 165 | ||
- | 166 | mods_valid: |
|
- | 167 | ||
- | 168 | mods_invalid: |
|
- | 169 | movl %ecx, init_addr |
|
- | 170 | movl %edx, init_size |
|
- | 171 | ||
159 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
172 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
160 | jc mmap_valid |
173 | jc mmap_valid |
161 | 174 | ||
162 | xorl %edx, %edx |
175 | xorl %edx, %edx |
163 | jmp mmap_invalid |
176 | jmp mmap_invalid |
164 | 177 | ||
165 | mmap_valid: |
178 | mmap_valid: |
166 | movl 44(%ebx), %ecx # mbi->mmap_length |
179 | movl 44(%ebx), %ecx # mbi->mmap_length |
167 | movl 48(%ebx), %esi # mbi->mmap_addr |
180 | movl 48(%ebx), %esi # mbi->mmap_addr |
168 | movl $e820table, %edi |
181 | movl $e820table, %edi |
169 | xorl %edx, %edx |
182 | xorl %edx, %edx |
170 | 183 | ||
171 | mmap_loop: |
184 | mmap_loop: |
172 | cmpl $0, %ecx |
185 | cmpl $0, %ecx |
173 | jle mmap_end |
186 | jle mmap_end |
174 | 187 | ||
175 | movl 4(%esi), %eax # mmap->base_addr_low |
188 | movl 4(%esi), %eax # mmap->base_addr_low |
176 | movl %eax, (%edi) |
189 | movl %eax, (%edi) |
177 | 190 | ||
178 | movl 8(%esi), %eax # mmap->base_addr_high |
191 | movl 8(%esi), %eax # mmap->base_addr_high |
179 | movl %eax, 4(%edi) |
192 | movl %eax, 4(%edi) |
180 | 193 | ||
181 | movl 12(%esi), %eax # mmap->length_low |
194 | movl 12(%esi), %eax # mmap->length_low |
182 | movl %eax, 8(%edi) |
195 | movl %eax, 8(%edi) |
183 | 196 | ||
184 | movl 16(%esi), %eax # mmap->length_high |
197 | movl 16(%esi), %eax # mmap->length_high |
185 | movl %eax, 12(%edi) |
198 | movl %eax, 12(%edi) |
186 | 199 | ||
187 | movl 20(%esi), %eax # mmap->type |
200 | movl 20(%esi), %eax # mmap->type |
188 | movl %eax, 16(%edi) |
201 | movl %eax, 16(%edi) |
189 | 202 | ||
190 | movl (%esi), %eax # mmap->size |
203 | movl (%esi), %eax # mmap->size |
191 | addl $0x4, %eax |
204 | addl $0x4, %eax |
192 | addl %eax, %esi |
205 | addl %eax, %esi |
193 | subl %eax, %ecx |
206 | subl %eax, %ecx |
194 | addl $MEMMAP_E820_RECORD_SIZE, %edi |
207 | addl $MEMMAP_E820_RECORD_SIZE, %edi |
195 | incl %edx |
208 | incl %edx |
196 | jmp mmap_loop |
209 | jmp mmap_loop |
197 | 210 | ||
198 | mmap_end: |
211 | mmap_end: |
199 | 212 | ||
200 | mmap_invalid: |
213 | mmap_invalid: |
201 | movl %edx, e820counter |
214 | movl %edx, e820counter |
202 | 215 | ||
203 | invalid_boot: |
216 | invalid_boot: |
204 | 217 | ||
205 | call main_bsp - BOOT_OFFSET # never returns |
218 | call main_bsp - BOOT_OFFSET # never returns |
206 | 219 | ||
207 | cli |
220 | cli |
208 | hlt |
221 | hlt |
209 | 222 | ||
210 | .global map_kernel |
223 | .global map_kernel |
211 | map_kernel: |
224 | map_kernel: |
212 | # |
225 | # |
213 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
226 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
214 | # For simplicity, we map the entire 4G space. |
227 | # For simplicity, we map the entire 4G space. |
215 | # |
228 | # |
216 | movl %cr4, %ecx |
229 | movl %cr4, %ecx |
217 | orl $(1<<4), %ecx |
230 | orl $(1<<4), %ecx |
218 | movl %ecx, %cr4 # turn PSE on |
231 | movl %ecx, %cr4 # turn PSE on |
219 | 232 | ||
220 | movl $(page_directory+0), %esi |
233 | movl $(page_directory+0), %esi |
221 | movl $(page_directory+2048), %edi |
234 | movl $(page_directory+2048), %edi |
222 | xorl %ecx, %ecx |
235 | xorl %ecx, %ecx |
223 | xorl %ebx, %ebx |
236 | xorl %ebx, %ebx |
224 | 0: |
237 | 0: |
225 | movl $((1<<7)|(1<<0)), %eax |
238 | movl $((1<<7)|(1<<0)), %eax |
226 | orl %ebx, %eax |
239 | orl %ebx, %eax |
227 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M |
240 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M |
228 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M |
241 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M |
229 | addl $(4*1024*1024), %ebx |
242 | addl $(4*1024*1024), %ebx |
230 | 243 | ||
231 | incl %ecx |
244 | incl %ecx |
232 | cmpl $512, %ecx |
245 | cmpl $512, %ecx |
233 | jl 0b |
246 | jl 0b |
234 | 247 | ||
235 | movl %esi, %cr3 |
248 | movl %esi, %cr3 |
236 | 249 | ||
237 | # turn paging on |
250 | # turn paging on |
238 | movl %cr0, %ebx |
251 | movl %cr0, %ebx |
239 | orl $(1<<31), %ebx |
252 | orl $(1<<31), %ebx |
240 | movl %ebx, %cr0 |
253 | movl %ebx, %cr0 |
241 | ret |
254 | ret |
242 | 255 | ||
243 | 256 | ||
244 | .section K_DATA_START, "aw", @progbits |
257 | .section K_DATA_START, "aw", @progbits |
245 | 258 | ||
246 | .align 4096 |
259 | .align 4096 |
247 | page_directory: |
260 | page_directory: |
248 | .space 4096, 0 |
261 | .space 4096, 0 |
249 | 262 | ||
250 | .global real_bootstrap_gdtr_boot |
263 | .global real_bootstrap_gdtr_boot |
251 | real_bootstrap_gdtr_boot: |
264 | real_bootstrap_gdtr_boot: |
252 | .word selector(GDT_ITEMS) |
265 | .word selector(GDT_ITEMS) |
253 | .long KA2PA(gdt)-BOOT_OFFSET |
266 | .long KA2PA(gdt)-BOOT_OFFSET |
254 | 267 | ||
255 | 268 |