Rev 3802 | Rev 3985 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | # |
2071 | jermar | 2 | # Copyright (c) 2001-2004 Jakub Jermar |
3 | # Copyright (c) 2005-2006 Martin Decky |
||
1 | jermar | 4 | # All rights reserved. |
5 | # |
||
6 | # Redistribution and use in source and binary forms, with or without |
||
7 | # modification, are permitted provided that the following conditions |
||
8 | # are met: |
||
9 | # |
||
10 | # - Redistributions of source code must retain the above copyright |
||
11 | # notice, this list of conditions and the following disclaimer. |
||
12 | # - Redistributions in binary form must reproduce the above copyright |
||
13 | # notice, this list of conditions and the following disclaimer in the |
||
14 | # documentation and/or other materials provided with the distribution. |
||
15 | # - The name of the author may not be used to endorse or promote products |
||
16 | # derived from this software without specific prior written permission. |
||
17 | # |
||
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | # |
||
29 | |||
222 | decky | 30 | #include <arch/boot/boot.h> |
506 | decky | 31 | #include <arch/boot/memmap.h> |
300 | palkovsky | 32 | #include <arch/mm/page.h> |
33 | #include <arch/pm.h> |
||
222 | decky | 34 | |
693 | decky | 35 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE) |
36 | |||
406 | jermar | 37 | .section K_TEXT_START, "ax" |
1 | jermar | 38 | |
112 | jermar | 39 | .code32 |
222 | decky | 40 | .align 4 |
693 | decky | 41 | .global multiboot_image_start |
222 | decky | 42 | multiboot_header: |
43 | .long MULTIBOOT_HEADER_MAGIC |
||
44 | .long MULTIBOOT_HEADER_FLAGS |
||
45 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
||
693 | decky | 46 | .long multiboot_header |
47 | .long unmapped_ktext_start |
||
222 | decky | 48 | .long 0 |
49 | .long 0 |
||
693 | decky | 50 | .long multiboot_image_start |
222 | decky | 51 | |
52 | multiboot_image_start: |
||
2784 | jermar | 53 | cld |
693 | decky | 54 | movl $START_STACK, %esp # initialize stack pointer |
55 | lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register |
||
112 | jermar | 56 | |
1811 | decky | 57 | movw $selector(KDATA_DES), %cx |
280 | decky | 58 | movw %cx, %es |
1639 | decky | 59 | movw %cx, %fs |
280 | decky | 60 | movw %cx, %gs |
693 | decky | 61 | movw %cx, %ds # kernel data + stack |
280 | decky | 62 | movw %cx, %ss |
235 | decky | 63 | |
1811 | decky | 64 | jmpl $selector(KTEXT_DES), $multiboot_meeting_point |
271 | decky | 65 | multiboot_meeting_point: |
66 | |||
2222 | decky | 67 | movl %eax, grub_eax # save parameters from GRUB |
68 | movl %ebx, grub_ebx |
||
1639 | decky | 69 | |
2220 | decky | 70 | xorl %eax, %eax |
71 | cpuid |
||
72 | cmp $0x0, %eax # any function > 0? |
||
73 | jbe pse_unsupported |
||
2221 | decky | 74 | movl $0x1, %eax # Basic function code 1 |
2220 | decky | 75 | cpuid |
76 | bt $3, %edx # Test if PSE is supported |
||
77 | jc pse_supported |
||
78 | |||
79 | pse_unsupported: |
||
80 | movl $pse_msg, %esi |
||
81 | jmp error_halt |
||
82 | |||
83 | pse_supported: |
||
84 | |||
1287 | vana | 85 | #ifdef CONFIG_FB |
1639 | decky | 86 | mov $vesa_init, %esi |
87 | mov $VESA_INIT_SEGMENT << 4, %edi |
||
88 | mov $e_vesa_init - vesa_init, %ecx |
||
89 | rep movsb |
||
1287 | vana | 90 | |
1639 | decky | 91 | mov $VESA_INIT_SEGMENT << 4, %edi |
2089 | decky | 92 | jmpl *%edi |
1639 | decky | 93 | |
94 | vesa_meeting_point: |
||
95 | |||
96 | mov %esi, KA2PA(vesa_ph_addr) |
||
97 | mov %di, KA2PA(vesa_height) |
||
98 | shr $16, %edi |
||
99 | mov %di, KA2PA(vesa_width) |
||
100 | mov %bx, KA2PA(vesa_scanline) |
||
101 | shr $16, %ebx |
||
102 | mov %bx, KA2PA(vesa_bpp) |
||
1287 | vana | 103 | #endif |
3984 | svoboda | 104 | |
323 | jermar | 105 | call map_kernel # map kernel and turn paging on |
3984 | svoboda | 106 | |
2222 | decky | 107 | movl grub_eax, %eax |
108 | movl grub_ebx, %ebx |
||
323 | jermar | 109 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
280 | decky | 110 | je valid_boot |
3984 | svoboda | 111 | |
2725 | decky | 112 | xorl %ecx, %ecx # no memory map available |
285 | decky | 113 | movl %ecx, e820counter |
3984 | svoboda | 114 | |
280 | decky | 115 | jmp invalid_boot |
3984 | svoboda | 116 | |
280 | decky | 117 | valid_boot: |
3984 | svoboda | 118 | |
119 | movl grub_eax, %eax |
||
120 | movl grub_ebx, %ebx |
||
121 | |||
122 | # ia32_boot(grub_eax, grub_ebx) |
||
123 | pushl %ebx |
||
124 | pushl %eax |
||
125 | call ia32_cboot # Does not return. |
||
126 | |||
127 | # Not reached. |
||
128 | |||
280 | decky | 129 | invalid_boot: |
2220 | decky | 130 | |
110 | jermar | 131 | cli |
132 | hlt |
||
133 | |||
134 | .global map_kernel |
||
135 | map_kernel: |
||
105 | jermar | 136 | # |
137 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
||
371 | jermar | 138 | # For simplicity, we map the entire 4G space. |
105 | jermar | 139 | # |
140 | movl %cr4, %ecx |
||
2220 | decky | 141 | orl $(1 << 4), %ecx # turn PSE on |
142 | andl $(~(1 << 5)), %ecx # turn PAE off |
||
143 | movl %ecx, %cr4 |
||
1 | jermar | 144 | |
2220 | decky | 145 | movl $(page_directory + 0), %esi |
146 | movl $(page_directory + 2048), %edi |
||
371 | jermar | 147 | xorl %ecx, %ecx |
148 | xorl %ebx, %ebx |
||
149 | 0: |
||
2220 | decky | 150 | movl $((1 << 7) | (1 << 1) | (1 << 0)), %eax |
371 | jermar | 151 | orl %ebx, %eax |
2220 | decky | 152 | movl %eax, (%esi, %ecx, 4) # mapping 0x00000000 + %ecx * 4M => 0x00000000 + %ecx * 4M |
153 | movl %eax, (%edi, %ecx, 4) # mapping 0x80000000 + %ecx * 4M => 0x00000000 + %ecx * 4M |
||
154 | addl $(4 * 1024 * 1024), %ebx |
||
1 | jermar | 155 | |
371 | jermar | 156 | incl %ecx |
157 | cmpl $512, %ecx |
||
158 | jl 0b |
||
105 | jermar | 159 | |
371 | jermar | 160 | movl %esi, %cr3 |
105 | jermar | 161 | |
162 | movl %cr0, %ebx |
||
2220 | decky | 163 | orl $(1 << 31), %ebx # turn paging on |
105 | jermar | 164 | movl %ebx, %cr0 |
110 | jermar | 165 | ret |
105 | jermar | 166 | |
2220 | decky | 167 | # Print string from %esi to EGA display (in red) and halt |
168 | error_halt: |
||
169 | movl $0xb8000, %edi # base of EGA text mode memory |
||
170 | xorl %eax, %eax |
||
171 | |||
172 | movw $0x3d4, %dx # read bits 8 - 15 of the cursor address |
||
173 | movb $0xe, %al |
||
174 | outb %al, %dx |
||
175 | |||
176 | movw $0x3d5, %dx |
||
177 | inb %dx, %al |
||
178 | shl $8, %ax |
||
179 | |||
180 | movw $0x3d4, %dx # read bits 0 - 7 of the cursor address |
||
181 | movb $0xf, %al |
||
182 | outb %al, %dx |
||
183 | |||
184 | movw $0x3d5, %dx |
||
185 | inb %dx, %al |
||
186 | |||
187 | cmp $1920, %ax |
||
188 | jbe cursor_ok |
||
189 | movw $1920, %ax # sanity check for the cursor on the last line |
||
190 | cursor_ok: |
||
191 | |||
192 | movw %ax, %bx |
||
193 | shl $1, %eax |
||
194 | addl %eax, %edi |
||
195 | |||
196 | movw $0x0c00, %ax # black background, light red foreground |
||
197 | |||
198 | ploop: |
||
199 | lodsb |
||
200 | cmp $0, %al |
||
201 | je ploop_end |
||
202 | stosw |
||
203 | inc %bx |
||
204 | jmp ploop |
||
205 | ploop_end: |
||
206 | |||
207 | movw $0x3d4, %dx # write bits 8 - 15 of the cursor address |
||
208 | movb $0xe, %al |
||
209 | outb %al, %dx |
||
210 | |||
211 | movw $0x3d5, %dx |
||
212 | movb %bh, %al |
||
213 | outb %al, %dx |
||
214 | |||
215 | movw $0x3d4, %dx # write bits 0 - 7 of the cursor address |
||
216 | movb $0xf, %al |
||
217 | outb %al, %dx |
||
218 | |||
219 | movw $0x3d5, %dx |
||
220 | movb %bl, %al |
||
221 | outb %al, %dx |
||
222 | |||
223 | cli |
||
224 | hlt |
||
225 | |||
1287 | vana | 226 | #ifdef CONFIG_FB |
227 | vesa_init: |
||
1639 | decky | 228 | jmp $selector(VESA_INIT_DES), $vesa_init_real - vesa_init |
229 | |||
1287 | vana | 230 | .code16 |
1639 | decky | 231 | vesa_init_real: |
1287 | vana | 232 | |
1639 | decky | 233 | mov %cr0, %eax |
234 | and $~1, %eax |
||
235 | mov %eax, %cr0 |
||
236 | |||
237 | jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init |
||
238 | |||
239 | vesa_init_real2: |
||
240 | |||
241 | mov $VESA_INIT_SEGMENT, %bx |
||
242 | |||
243 | mov %bx, %es |
||
244 | mov %bx, %fs |
||
245 | mov %bx, %gs |
||
246 | mov %bx, %ds |
||
247 | mov %bx, %ss |
||
248 | |||
249 | movl %esp, %eax |
||
250 | movl $0x0000fffc, %esp |
||
251 | movl $0x0000fffc, %ebp |
||
252 | pushl %eax |
||
253 | |||
1287 | vana | 254 | #define VESA_INFO_SIZE 1024 |
255 | |||
2302 | decky | 256 | #define VESA_MODE_ATTRIBUTES_OFFSET 0 |
1639 | decky | 257 | #define VESA_MODE_LIST_PTR_OFFSET 14 |
2302 | decky | 258 | #define VESA_MODE_SCANLINE_OFFSET 16 |
1287 | vana | 259 | #define VESA_MODE_WIDTH_OFFSET 18 |
260 | #define VESA_MODE_HEIGHT_OFFSET 20 |
||
261 | #define VESA_MODE_BPP_OFFSET 25 |
||
262 | #define VESA_MODE_PHADDR_OFFSET 40 |
||
263 | |||
1639 | decky | 264 | #define VESA_END_OF_MODES 0xffff |
1287 | vana | 265 | |
1639 | decky | 266 | #define VESA_OK 0x4f |
1287 | vana | 267 | |
1639 | decky | 268 | #define VESA_GET_INFO 0x4f00 |
1287 | vana | 269 | #define VESA_GET_MODE_INFO 0x4f01 |
270 | #define VESA_SET_MODE 0x4f02 |
||
2302 | decky | 271 | #define VESA_SET_PALETTE 0x4f09 |
1287 | vana | 272 | |
1639 | decky | 273 | #if CONFIG_VESA_BPP == 24 |
2302 | decky | 274 | #define CONFIG_VESA_BPP_VARIANT 32 |
1287 | vana | 275 | #endif |
276 | |||
1639 | decky | 277 | mov $VESA_GET_INFO, %ax |
278 | mov $e_vesa_init - vesa_init, %di |
||
279 | push %di |
||
280 | int $0x10 |
||
281 | |||
282 | pop %di |
||
283 | cmp $VESA_OK, %al |
||
284 | jnz 0f |
||
285 | |||
286 | mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si |
||
287 | mov %si, %gs |
||
288 | mov VESA_MODE_LIST_PTR_OFFSET(%di), %si |
||
289 | |||
290 | add $VESA_INFO_SIZE, %di |
||
1287 | vana | 291 | |
292 | 1:# Try next mode |
||
1639 | decky | 293 | mov %gs:(%si), %cx |
294 | cmp $VESA_END_OF_MODES, %cx |
||
295 | jz 0f |
||
296 | |||
297 | inc %si |
||
298 | inc %si |
||
299 | push %cx |
||
300 | push %di |
||
301 | push %si |
||
302 | mov $VESA_GET_MODE_INFO, %ax |
||
303 | int $0x10 |
||
304 | |||
305 | pop %si |
||
306 | pop %di |
||
307 | pop %cx |
||
308 | cmp $VESA_OK, %al |
||
309 | jnz 0f |
||
310 | |||
311 | mov $CONFIG_VESA_WIDTH, %ax |
||
312 | cmp VESA_MODE_WIDTH_OFFSET(%di), %ax |
||
313 | jnz 1b |
||
314 | |||
2302 | decky | 315 | mov $CONFIG_VESA_HEIGHT, %ax |
1639 | decky | 316 | cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax |
317 | jnz 1b |
||
318 | |||
319 | mov $CONFIG_VESA_BPP, %al |
||
320 | cmp VESA_MODE_BPP_OFFSET(%di), %al |
||
2302 | decky | 321 | |
322 | #ifdef CONFIG_VESA_BPP_VARIANT |
||
1639 | decky | 323 | jz 2f |
324 | |||
2302 | decky | 325 | mov $CONFIG_VESA_BPP_VARIANT, %al |
1639 | decky | 326 | cmp VESA_MODE_BPP_OFFSET(%di), %al |
2302 | decky | 327 | #endif |
1639 | decky | 328 | jnz 1b |
2302 | decky | 329 | |
1287 | vana | 330 | 2: |
331 | |||
1639 | decky | 332 | mov %cx, %bx |
333 | or $0xc000, %bx |
||
334 | push %di |
||
335 | mov $VESA_SET_MODE, %ax |
||
336 | int $0x10 |
||
1287 | vana | 337 | |
1639 | decky | 338 | pop %di |
339 | cmp $VESA_OK, %al |
||
340 | jnz 0f |
||
2302 | decky | 341 | |
342 | #if CONFIG_VESA_BPP == 8 |
||
1287 | vana | 343 | |
2302 | decky | 344 | # Set 3:2:3 VGA palette |
345 | |||
346 | mov VESA_MODE_ATTRIBUTES_OFFSET(%di), %ax |
||
347 | push %di |
||
348 | mov $vga323 - vesa_init, %di |
||
349 | mov $0x100, %ecx |
||
350 | |||
351 | bt $5, %ax # Test if VGA compatible registers are present |
||
352 | jnc vga_compat |
||
353 | |||
354 | # Try VESA routine to set palette |
||
355 | |||
356 | mov $VESA_SET_PALETTE, %ax |
||
357 | xor %bl, %bl |
||
358 | xor %dx, %dx |
||
359 | int $0x10 |
||
360 | |||
3802 | decky | 361 | cmp $0x00, %ah |
362 | je vga_not_compat |
||
2302 | decky | 363 | |
364 | vga_compat: |
||
365 | |||
366 | # Try VGA registers to set palette |
||
367 | |||
368 | movw $0x3c6, %dx # Set palette mask |
||
369 | movb $0xff, %al |
||
370 | outb %al, %dx |
||
371 | |||
372 | movw $0x3c8, %dx # First index to set |
||
373 | xor %al, %al |
||
374 | outb %al, %dx |
||
375 | |||
376 | movw $0x3c9, %dx # Data port |
||
377 | vga_loop: |
||
378 | movb %es:2(%di), %al |
||
379 | outb %al, %dx |
||
380 | |||
381 | movb %es:1(%di), %al |
||
382 | outb %al, %dx |
||
383 | |||
384 | movb %es:(%di), %al |
||
385 | outb %al, %dx |
||
386 | |||
387 | addw $4, %di |
||
388 | loop vga_loop |
||
389 | |||
390 | vga_not_compat: |
||
391 | |||
392 | pop %di |
||
393 | |||
394 | #endif |
||
395 | |||
1639 | decky | 396 | mov VESA_MODE_PHADDR_OFFSET(%di), %esi |
397 | mov VESA_MODE_WIDTH_OFFSET(%di), %ax |
||
398 | shl $16, %eax |
||
399 | mov VESA_MODE_HEIGHT_OFFSET(%di), %ax |
||
400 | mov VESA_MODE_BPP_OFFSET(%di), %bl |
||
401 | xor %bh, %bh |
||
402 | shl $16, %ebx |
||
403 | mov VESA_MODE_SCANLINE_OFFSET(%di), %bx |
||
404 | mov %eax, %edi |
||
405 | |||
1287 | vana | 406 | 8: |
1639 | decky | 407 | |
408 | mov %cr0, %eax |
||
409 | or $1, %eax |
||
1641 | decky | 410 | mov %eax, %cr0 |
1639 | decky | 411 | |
412 | jmp 9f |
||
1287 | vana | 413 | 9: |
1639 | decky | 414 | |
1811 | decky | 415 | ljmpl $selector(KTEXT_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4) |
1287 | vana | 416 | |
1639 | decky | 417 | 0:# No prefered mode found |
418 | mov $0x111, %cx |
||
419 | push %di |
||
420 | push %cx |
||
421 | mov $VESA_GET_MODE_INFO, %ax |
||
422 | int $0x10 |
||
423 | |||
424 | pop %cx |
||
425 | pop %di |
||
426 | cmp $VESA_OK, %al |
||
427 | jnz 1f |
||
428 | jz 2b # Force relative jump |
||
429 | |||
430 | 1: |
||
431 | mov $0x0003, %ax |
||
432 | int $0x10 |
||
433 | mov $0xffffffff, %edi # EGA text mode used, because of problems with VESA |
||
434 | xor %ax, %ax |
||
435 | jz 8b # Force relative jump |
||
1287 | vana | 436 | |
2302 | decky | 437 | vga323: |
438 | #include "vga323.pal" |
||
439 | |||
1639 | decky | 440 | .code32 |
441 | vesa_init_protect: |
||
1811 | decky | 442 | movw $selector(KDATA_DES), %cx |
1639 | decky | 443 | movw %cx, %es |
444 | movw %cx, %fs |
||
445 | movw %cx, %gs |
||
446 | movw %cx, %ds # kernel data + stack |
||
447 | movw %cx, %ss |
||
448 | |||
2222 | decky | 449 | movl $START_STACK, %esp # initialize stack pointer |
450 | |||
1811 | decky | 451 | jmpl $selector(KTEXT_DES), $vesa_meeting_point |
1287 | vana | 452 | |
453 | .align 4 |
||
454 | e_vesa_init: |
||
455 | #endif |
||
456 | |||
406 | jermar | 457 | .section K_DATA_START, "aw", @progbits |
105 | jermar | 458 | |
459 | .align 4096 |
||
460 | page_directory: |
||
461 | .space 4096, 0 |
||
2220 | decky | 462 | |
2222 | decky | 463 | grub_eax: |
464 | .long 0 |
||
465 | |||
466 | grub_ebx: |
||
467 | .long 0 |
||
468 | |||
2220 | decky | 469 | pse_msg: |
2302 | decky | 470 | .asciz "Page Size Extension not supported. System halted." |