source: mainline/kernel/arch/ia32/src/boot/boot.S@ f476e76

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f476e76 was f476e76, checked in by Martin Decky <martin@…>, 19 years ago

unify bootstrapping

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[f761f1eb]1#
2# Copyright (C) 2001-2004 Jakub Jermar
[66def8d]3# Copyright (C) 2005-2006 Martin Decky
[f761f1eb]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
[f9447155]30#include <arch/boot/boot.h>
[aa72859]31#include <arch/boot/memmap.h>
[8f2153b]32#include <arch/mm/page.h>
33#include <arch/pm.h>
[f9447155]34
[66def8d]35#define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
36
[874e312a]37.section K_TEXT_START, "ax"
[f761f1eb]38
[b0bf501]39.code32
[f9447155]40.align 4
[66def8d]41.global multiboot_image_start
[f9447155]42multiboot_header:
43 .long MULTIBOOT_HEADER_MAGIC
44 .long MULTIBOOT_HEADER_FLAGS
45 .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
[66def8d]46 .long multiboot_header
47 .long unmapped_ktext_start
[f9447155]48 .long 0
49 .long 0
[66def8d]50 .long multiboot_image_start
[5dce48b9]51
[f9447155]52multiboot_image_start:
[66def8d]53 movl $START_STACK, %esp # initialize stack pointer
54 lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register
[b0bf501]55
[f476e76]56 movw $selector(KDATA_DES), %cx
[5eb1379]57 movw %cx, %es
58 movw %cx, %fs
[ff133e2]59 movw %cx, %gs
[66def8d]60 movw %cx, %ds # kernel data + stack
[5eb1379]61 movw %cx, %ss
[5dce48b9]62
[f476e76]63 jmpl $selector(KTEXT_DES), $multiboot_meeting_point
[4533601]64 multiboot_meeting_point:
65
[b0edf3b2]66 pushl %ebx # save parameters from GRUB
[5eb1379]67 pushl %eax
[ff133e2]68
[22cf454d]69#ifdef CONFIG_FB
[ff133e2]70 mov $vesa_init, %esi
71 mov $VESA_INIT_SEGMENT << 4, %edi
72 mov $e_vesa_init - vesa_init, %ecx
73 cld
74 rep movsb
[22cf454d]75
[ff133e2]76 mov $VESA_INIT_SEGMENT << 4, %edi
77 jmpl %edi
78
79 vesa_meeting_point:
80
81 mov %esi, KA2PA(vesa_ph_addr)
82 mov %di, KA2PA(vesa_height)
83 shr $16, %edi
84 mov %di, KA2PA(vesa_width)
85 mov %bx, KA2PA(vesa_scanline)
86 shr $16, %ebx
87 mov %bx, KA2PA(vesa_bpp)
[22cf454d]88#endif
[5eb1379]89
[b0edf3b2]90 call map_kernel # map kernel and turn paging on
[5dce48b9]91
[5eb1379]92 popl %eax
93 popl %ebx
[b0edf3b2]94 cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
[5eb1379]95 je valid_boot
[339e053]96
97 xorl %ecx, %ecx # no memory size or map available
98 movl %ecx, e801memorysize
99 movl %ecx, e820counter
[5eb1379]100
101 jmp invalid_boot
[339e053]102
[5eb1379]103 valid_boot:
104
[339e053]105 movl (%ebx), %eax # ebx = physical address of struct multiboot_info
[5eb1379]106
[339e053]107 bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
108 jc mem_valid
109
110 xorl %ecx, %ecx
111 jmp mem_invalid
112
113 mem_valid:
114 movl 4(%ebx), %ecx # mbi->mem_lower
115 addl 8(%ebx), %ecx # mbi->mem_upper
[5eb1379]116
[339e053]117 mem_invalid:
[5eb1379]118 movl %ecx, e801memorysize
119
[b6b576c]120 bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
[aa72859]121 jc mods_valid
122
123 xorl %ecx, %ecx
[b6b576c]124 movl %ecx, init
125 jmp mods_end
[aa72859]126
127 mods_valid:
[b6b576c]128
[6c68b97]129 movl 20(%ebx), %ecx # mbi->mods_count
[b6b576c]130 movl %ecx, init
131
[6c68b97]132 cmpl $0, %ecx
[b6b576c]133 je mods_end
[6c68b97]134
135 movl 24(%ebx), %esi # mbi->mods_addr
[b6b576c]136 movl $init, %edi
137
138 mods_loop:
139
140 movl 0(%esi), %edx # mods->mod_start
141 addl $0x80000000, %edx
142 movl %edx, 4(%edi)
143
144 movl 4(%esi), %edx
145 subl 0(%esi), %edx # mods->mod_end - mods->mod_start
146 movl %edx, 8(%edi)
147
148 addl $16, %esi
149 addl $8 , %edi
150
151 loop mods_loop
152
153 mods_end:
[aa72859]154
[339e053]155 bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
156 jc mmap_valid
157
158 xorl %edx, %edx
159 jmp mmap_invalid
160
161 mmap_valid:
162 movl 44(%ebx), %ecx # mbi->mmap_length
163 movl 48(%ebx), %esi # mbi->mmap_addr
164 movl $e820table, %edi
165 xorl %edx, %edx
166
167 mmap_loop:
168 cmpl $0, %ecx
169 jle mmap_end
170
171 movl 4(%esi), %eax # mmap->base_addr_low
172 movl %eax, (%edi)
173
174 movl 8(%esi), %eax # mmap->base_addr_high
175 movl %eax, 4(%edi)
176
177 movl 12(%esi), %eax # mmap->length_low
178 movl %eax, 8(%edi)
179
180 movl 16(%esi), %eax # mmap->length_high
181 movl %eax, 12(%edi)
182
183 movl 20(%esi), %eax # mmap->type
184 movl %eax, 16(%edi)
185
186 movl (%esi), %eax # mmap->size
187 addl $0x4, %eax
188 addl %eax, %esi
189 subl %eax, %ecx
190 addl $MEMMAP_E820_RECORD_SIZE, %edi
191 incl %edx
192 jmp mmap_loop
193
194 mmap_end:
195
196 mmap_invalid:
197 movl %edx, e820counter
[5eb1379]198
199 invalid_boot:
200
[66def8d]201#ifdef CONFIG_SMP
202
203 # copy AP bootstrap routines below 1 MB
204
205 movl $BOOT_OFFSET, %esi
206 movl $AP_BOOT_OFFSET, %edi
207 movl $_hardcoded_unmapped_size, %ecx
208 cld
209 rep movsb
210
211#endif
212
213 call main_bsp # never returns
[d47f0e1]214
215 cli
216 hlt
217
218.global map_kernel
219map_kernel:
[dcbc8be]220 #
221 # Here we setup mapping for both the unmapped and mapped sections of the kernel.
[cf27a6cb]222 # For simplicity, we map the entire 4G space.
[dcbc8be]223 #
224 movl %cr4, %ecx
225 orl $(1<<4), %ecx
[b0edf3b2]226 movl %ecx, %cr4 # turn PSE on
[dcbc8be]227
[cf27a6cb]228 movl $(page_directory+0), %esi
229 movl $(page_directory+2048), %edi
230 xorl %ecx, %ecx
231 xorl %ebx, %ebx
2320:
[dcbc8be]233 movl $((1<<7)|(1<<0)), %eax
[cf27a6cb]234 orl %ebx, %eax
235 movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
236 movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
237 addl $(4*1024*1024), %ebx
[dcbc8be]238
[cf27a6cb]239 incl %ecx
240 cmpl $512, %ecx
241 jl 0b
[dcbc8be]242
[cf27a6cb]243 movl %esi, %cr3
[f761f1eb]244
[b52da8d7]245 # turn paging on
[dcbc8be]246 movl %cr0, %ebx
247 orl $(1<<31), %ebx
248 movl %ebx, %cr0
[d47f0e1]249 ret
[dcbc8be]250
[22cf454d]251#ifdef CONFIG_FB
252vesa_init:
[ff133e2]253 jmp $selector(VESA_INIT_DES), $vesa_init_real - vesa_init
254
[22cf454d]255.code16
[ff133e2]256vesa_init_real:
257
258 mov %cr0, %eax
259 and $~1, %eax
260 mov %eax, %cr0
261
262 jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
263
264vesa_init_real2:
265
266 mov $VESA_INIT_SEGMENT, %bx
267
268 mov %bx, %es
269 mov %bx, %fs
270 mov %bx, %gs
271 mov %bx, %ds
272 mov %bx, %ss
273
274 movl %esp, %eax
275 movl $0x0000fffc, %esp
276 movl $0x0000fffc, %ebp
277 pushl %eax
[22cf454d]278
279#define VESA_INFO_SIZE 1024
280
[ff133e2]281#define VESA_MODE_LIST_PTR_OFFSET 14
[22cf454d]282#define VESA_MODE_WIDTH_OFFSET 18
283#define VESA_MODE_HEIGHT_OFFSET 20
284#define VESA_MODE_BPP_OFFSET 25
285#define VESA_MODE_SCANLINE_OFFSET 16
286#define VESA_MODE_PHADDR_OFFSET 40
287
[ff133e2]288#define VESA_END_OF_MODES 0xffff
[22cf454d]289
[ff133e2]290#define VESA_OK 0x4f
[22cf454d]291
[ff133e2]292#define VESA_GET_INFO 0x4f00
[22cf454d]293#define VESA_GET_MODE_INFO 0x4f01
294#define VESA_SET_MODE 0x4f02
295
296#define CONFIG_VESA_BPP_a 255
297
[ff133e2]298#if CONFIG_VESA_BPP == 24
[22cf454d]299#undef CONFIG_VESA_BPP_a
300#define CONFIG_VESA_BPP_a 32
301#endif
302
[ff133e2]303 mov $VESA_GET_INFO, %ax
304 mov $e_vesa_init - vesa_init, %di
305 push %di
306 int $0x10
307
308 pop %di
309 cmp $VESA_OK, %al
310 jnz 0f
311
312 mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
313 mov %si, %gs
314 mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
315
316 add $VESA_INFO_SIZE, %di
[22cf454d]317
3181:# Try next mode
[ff133e2]319 mov %gs:(%si), %cx
320 cmp $VESA_END_OF_MODES, %cx
321 jz 0f
322
323 inc %si
324 inc %si
325 push %cx
326 push %di
327 push %si
328 mov $VESA_GET_MODE_INFO, %ax
329 int $0x10
330
331 pop %si
332 pop %di
333 pop %cx
334 cmp $VESA_OK, %al
335 jnz 0f
336
337 mov $CONFIG_VESA_WIDTH, %ax
338 cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
339 jnz 1b
340
341 mov $CONFIG_VESA_HEIGHT,%ax
342 cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
343 jnz 1b
344
345 mov $CONFIG_VESA_BPP, %al
346 cmp VESA_MODE_BPP_OFFSET(%di), %al
347 jz 2f
348
349 mov $CONFIG_VESA_BPP_a, %al
350 cmp VESA_MODE_BPP_OFFSET(%di), %al
351 jnz 1b
352
[22cf454d]3532:
354
[ff133e2]355 mov %cx, %bx
356 or $0xc000, %bx
357 push %di
358 mov $VESA_SET_MODE, %ax
359 int $0x10
360
361 pop %di
362 cmp $VESA_OK, %al
363 jnz 0f
364
365 mov VESA_MODE_PHADDR_OFFSET(%di), %esi
366 mov VESA_MODE_WIDTH_OFFSET(%di), %ax
367 shl $16, %eax
368 mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
369 mov VESA_MODE_BPP_OFFSET(%di), %bl
370 xor %bh, %bh
371 shl $16, %ebx
372 mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
373 mov %eax, %edi
[22cf454d]374
3758:
[ff133e2]376
377 mov %cr0, %eax
378 or $1, %eax
[8778271]379 mov %eax, %cr0
[ff133e2]380
381 jmp 9f
[22cf454d]3829:
[ff133e2]383
[f476e76]384 ljmpl $selector(KTEXT_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
[ff133e2]385
3860:# No prefered mode found
387 mov $0x111, %cx
388 push %di
389 push %cx
390 mov $VESA_GET_MODE_INFO, %ax
391 int $0x10
392
393 pop %cx
394 pop %di
395 cmp $VESA_OK, %al
396 jnz 1f
397 jz 2b # Force relative jump
398
3991:
400 mov $0x0003, %ax
401 int $0x10
402 mov $0xffffffff, %edi # EGA text mode used, because of problems with VESA
403 xor %ax, %ax
404 jz 8b # Force relative jump
405
[22cf454d]406
[203dcd45]407.code32
[ff133e2]408vesa_init_protect:
409 popl %esp
410
[f476e76]411 movw $selector(KDATA_DES), %cx
[ff133e2]412 movw %cx, %es
413 movw %cx, %fs
414 movw %cx, %gs
415 movw %cx, %ds # kernel data + stack
416 movw %cx, %ss
417
[f476e76]418 jmpl $selector(KTEXT_DES), $vesa_meeting_point
[22cf454d]419
420.align 4
421e_vesa_init:
422#endif
423
[874e312a]424.section K_DATA_START, "aw", @progbits
[dcbc8be]425
426.align 4096
427page_directory:
428 .space 4096, 0
Note: See TracBrowser for help on using the repository browser.