1 | #
|
---|
2 | # Copyright (C) 2001-2004 Jakub Jermar
|
---|
3 | # Copyright (C) 2005-2006 Martin Decky
|
---|
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 |
|
---|
30 | #include <arch/boot/boot.h>
|
---|
31 | #include <arch/boot/memmap.h>
|
---|
32 | #include <arch/mm/page.h>
|
---|
33 | #include <arch/pm.h>
|
---|
34 |
|
---|
35 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
36 |
|
---|
37 | .section K_TEXT_START, "ax"
|
---|
38 |
|
---|
39 | KTEXT=8
|
---|
40 | KDATA=16
|
---|
41 |
|
---|
42 | .code32
|
---|
43 | .align 4
|
---|
44 | .global multiboot_image_start
|
---|
45 | multiboot_header:
|
---|
46 | .long MULTIBOOT_HEADER_MAGIC
|
---|
47 | .long MULTIBOOT_HEADER_FLAGS
|
---|
48 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
49 | .long multiboot_header
|
---|
50 | .long unmapped_ktext_start
|
---|
51 | .long 0
|
---|
52 | .long 0
|
---|
53 | .long multiboot_image_start
|
---|
54 |
|
---|
55 | multiboot_image_start:
|
---|
56 | movl $START_STACK, %esp # initialize stack pointer
|
---|
57 | lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register
|
---|
58 |
|
---|
59 | movw $KDATA, %cx
|
---|
60 | movw %cx, %es
|
---|
61 | movw %cx, %gs
|
---|
62 | movw %cx, %fs
|
---|
63 | movw %cx, %ds # kernel data + stack
|
---|
64 | movw %cx, %ss
|
---|
65 |
|
---|
66 | jmpl $KTEXT, $multiboot_meeting_point
|
---|
67 | multiboot_meeting_point:
|
---|
68 |
|
---|
69 | pushl %ebx # save parameters from GRUB
|
---|
70 | pushl %eax
|
---|
71 |
|
---|
72 | call map_kernel # map kernel and turn paging on
|
---|
73 |
|
---|
74 | popl %eax
|
---|
75 | popl %ebx
|
---|
76 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
|
---|
77 | je valid_boot
|
---|
78 |
|
---|
79 | xorl %ecx, %ecx # no memory size or map available
|
---|
80 | movl %ecx, e801memorysize
|
---|
81 | movl %ecx, e820counter
|
---|
82 |
|
---|
83 | jmp invalid_boot
|
---|
84 |
|
---|
85 | valid_boot:
|
---|
86 |
|
---|
87 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info
|
---|
88 |
|
---|
89 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
|
---|
90 | jc mem_valid
|
---|
91 |
|
---|
92 | xorl %ecx, %ecx
|
---|
93 | jmp mem_invalid
|
---|
94 |
|
---|
95 | mem_valid:
|
---|
96 | movl 4(%ebx), %ecx # mbi->mem_lower
|
---|
97 | addl 8(%ebx), %ecx # mbi->mem_upper
|
---|
98 |
|
---|
99 | mem_invalid:
|
---|
100 | movl %ecx, e801memorysize
|
---|
101 |
|
---|
102 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
|
---|
103 | jc mods_valid
|
---|
104 |
|
---|
105 | xorl %ecx, %ecx
|
---|
106 | xorl %edx, %edx
|
---|
107 | jmp mods_invalid
|
---|
108 |
|
---|
109 | mods_valid:
|
---|
110 | movl 20(%ebx), %ecx # mbi->mods_count
|
---|
111 | cmpl $0, %ecx
|
---|
112 | je mods_invalid
|
---|
113 |
|
---|
114 | movl 24(%ebx), %esi # mbi->mods_addr
|
---|
115 | movl 0(%esi), %edx # mods->mod_start
|
---|
116 | movl 4(%esi), %ecx # mods->mod_end
|
---|
117 | subl %edx, %ecx
|
---|
118 | addl $0x80000000, %edx
|
---|
119 |
|
---|
120 | mods_invalid:
|
---|
121 | movl %ecx, init_size
|
---|
122 | movl %edx, init_addr
|
---|
123 |
|
---|
124 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
|
---|
125 | jc mmap_valid
|
---|
126 |
|
---|
127 | xorl %edx, %edx
|
---|
128 | jmp mmap_invalid
|
---|
129 |
|
---|
130 | mmap_valid:
|
---|
131 | movl 44(%ebx), %ecx # mbi->mmap_length
|
---|
132 | movl 48(%ebx), %esi # mbi->mmap_addr
|
---|
133 | movl $e820table, %edi
|
---|
134 | xorl %edx, %edx
|
---|
135 |
|
---|
136 | mmap_loop:
|
---|
137 | cmpl $0, %ecx
|
---|
138 | jle mmap_end
|
---|
139 |
|
---|
140 | movl 4(%esi), %eax # mmap->base_addr_low
|
---|
141 | movl %eax, (%edi)
|
---|
142 |
|
---|
143 | movl 8(%esi), %eax # mmap->base_addr_high
|
---|
144 | movl %eax, 4(%edi)
|
---|
145 |
|
---|
146 | movl 12(%esi), %eax # mmap->length_low
|
---|
147 | movl %eax, 8(%edi)
|
---|
148 |
|
---|
149 | movl 16(%esi), %eax # mmap->length_high
|
---|
150 | movl %eax, 12(%edi)
|
---|
151 |
|
---|
152 | movl 20(%esi), %eax # mmap->type
|
---|
153 | movl %eax, 16(%edi)
|
---|
154 |
|
---|
155 | movl (%esi), %eax # mmap->size
|
---|
156 | addl $0x4, %eax
|
---|
157 | addl %eax, %esi
|
---|
158 | subl %eax, %ecx
|
---|
159 | addl $MEMMAP_E820_RECORD_SIZE, %edi
|
---|
160 | incl %edx
|
---|
161 | jmp mmap_loop
|
---|
162 |
|
---|
163 | mmap_end:
|
---|
164 |
|
---|
165 | mmap_invalid:
|
---|
166 | movl %edx, e820counter
|
---|
167 |
|
---|
168 | invalid_boot:
|
---|
169 |
|
---|
170 | #ifdef CONFIG_SMP
|
---|
171 |
|
---|
172 | # copy AP bootstrap routines below 1 MB
|
---|
173 |
|
---|
174 | movl $BOOT_OFFSET, %esi
|
---|
175 | movl $AP_BOOT_OFFSET, %edi
|
---|
176 | movl $_hardcoded_unmapped_size, %ecx
|
---|
177 | cld
|
---|
178 | rep movsb
|
---|
179 |
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | call main_bsp # never returns
|
---|
183 |
|
---|
184 | cli
|
---|
185 | hlt
|
---|
186 |
|
---|
187 | .global map_kernel
|
---|
188 | map_kernel:
|
---|
189 | #
|
---|
190 | # Here we setup mapping for both the unmapped and mapped sections of the kernel.
|
---|
191 | # For simplicity, we map the entire 4G space.
|
---|
192 | #
|
---|
193 | movl %cr4, %ecx
|
---|
194 | orl $(1<<4), %ecx
|
---|
195 | movl %ecx, %cr4 # turn PSE on
|
---|
196 |
|
---|
197 | movl $(page_directory+0), %esi
|
---|
198 | movl $(page_directory+2048), %edi
|
---|
199 | xorl %ecx, %ecx
|
---|
200 | xorl %ebx, %ebx
|
---|
201 | 0:
|
---|
202 | movl $((1<<7)|(1<<0)), %eax
|
---|
203 | orl %ebx, %eax
|
---|
204 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
|
---|
205 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
|
---|
206 | addl $(4*1024*1024), %ebx
|
---|
207 |
|
---|
208 | incl %ecx
|
---|
209 | cmpl $512, %ecx
|
---|
210 | jl 0b
|
---|
211 |
|
---|
212 | movl %esi, %cr3
|
---|
213 |
|
---|
214 | # turn paging on
|
---|
215 | movl %cr0, %ebx
|
---|
216 | orl $(1<<31), %ebx
|
---|
217 | movl %ebx, %cr0
|
---|
218 | ret
|
---|
219 |
|
---|
220 |
|
---|
221 | .section K_DATA_START, "aw", @progbits
|
---|
222 |
|
---|
223 | .align 4096
|
---|
224 | page_directory:
|
---|
225 | .space 4096, 0
|
---|