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