Rev 680 | Rev 1037 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 680 | Rev 693 | ||
---|---|---|---|
1 | # |
1 | # |
2 | # Copyright (C) 2001-2004 Jakub Jermar |
2 | # Copyright (C) 2001-2004 Jakub Jermar |
- | 3 | # Copyright (C) 2005-2006 Martin Decky |
|
3 | # All rights reserved. |
4 | # All rights reserved. |
4 | # |
5 | # |
5 | # Redistribution and use in source and binary forms, with or without |
6 | # Redistribution and use in source and binary forms, with or without |
6 | # modification, are permitted provided that the following conditions |
7 | # modification, are permitted provided that the following conditions |
7 | # are met: |
8 | # are met: |
8 | # |
9 | # |
9 | # - Redistributions of source code must retain the above copyright |
10 | # - Redistributions of source code must retain the above copyright |
10 | # notice, this list of conditions and the following disclaimer. |
11 | # notice, this list of conditions and the following disclaimer. |
11 | # - Redistributions in binary form must reproduce the above copyright |
12 | # - Redistributions in binary form must reproduce the above copyright |
12 | # notice, this list of conditions and the following disclaimer in the |
13 | # notice, this list of conditions and the following disclaimer in the |
13 | # documentation and/or other materials provided with the distribution. |
14 | # 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 | # - 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 | # derived from this software without specific prior written permission. |
16 | # |
17 | # |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | # 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 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | # 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 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | # 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 | # (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 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | # |
28 | # |
28 | 29 | ||
29 | #include <arch/boot/boot.h> |
30 | #include <arch/boot/boot.h> |
30 | #include <arch/boot/memmap.h> |
31 | #include <arch/boot/memmap.h> |
31 | #include <arch/mm/page.h> |
32 | #include <arch/mm/page.h> |
32 | #include <arch/pm.h> |
33 | #include <arch/pm.h> |
33 | 34 | ||
- | 35 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE) |
|
- | 36 | ||
34 | .section K_TEXT_START, "ax" |
37 | .section K_TEXT_START, "ax" |
35 | 38 | ||
36 | KTEXT=8 |
39 | KTEXT=8 |
37 | KDATA=16 |
40 | KDATA=16 |
38 | 41 | ||
39 | .code32 |
42 | .code32 |
40 | .align 4 |
43 | .align 4 |
- | 44 | .global multiboot_image_start |
|
41 | multiboot_header: |
45 | multiboot_header: |
42 | .long MULTIBOOT_HEADER_MAGIC |
46 | .long MULTIBOOT_HEADER_MAGIC |
43 | .long MULTIBOOT_HEADER_FLAGS |
47 | .long MULTIBOOT_HEADER_FLAGS |
44 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
48 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
45 | .long multiboot_header + BOOT_OFFSET |
49 | .long multiboot_header |
46 | .long unmapped_ktext_start + BOOT_OFFSET |
50 | .long unmapped_ktext_start |
47 | .long 0 |
51 | .long 0 |
48 | .long 0 |
52 | .long 0 |
49 | .long multiboot_image_start + BOOT_OFFSET |
53 | .long multiboot_image_start |
50 | 54 | ||
51 | multiboot_image_start: |
55 | multiboot_image_start: |
52 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer |
56 | movl $START_STACK, %esp # initialize stack pointer |
53 | - | ||
54 | lgdt protected_bootstrap_gdtr - 0x80000000 # initialize Global Descriptor Table register |
57 | lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register |
55 | 58 | ||
56 | movw $KDATA, %cx |
59 | movw $KDATA, %cx |
57 | movw %cx, %es |
60 | movw %cx, %es |
58 | movw %cx, %gs |
61 | movw %cx, %gs |
59 | movw %cx, %fs |
62 | movw %cx, %fs |
60 | movw %cx, %ds # kernel data + stack |
63 | movw %cx, %ds # kernel data + stack |
61 | movw %cx, %ss |
64 | movw %cx, %ss |
62 | 65 | ||
63 | jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET |
66 | jmpl $KTEXT, $multiboot_meeting_point |
64 | multiboot_meeting_point: |
67 | multiboot_meeting_point: |
65 | 68 | ||
66 | pushl %ebx # save parameters from GRUB |
69 | pushl %ebx # save parameters from GRUB |
67 | pushl %eax |
70 | pushl %eax |
68 | 71 | ||
69 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi |
- | |
70 | movl $BOOTSTRAP_OFFSET, %edi |
- | |
71 | movl $_hardcoded_unmapped_size, %ecx |
- | |
72 | cld |
- | |
73 | rep movsb |
- | |
74 | - | ||
75 | call map_kernel # map kernel and turn paging on |
72 | call map_kernel # map kernel and turn paging on |
76 | 73 | ||
77 | popl %eax |
74 | popl %eax |
78 | popl %ebx |
75 | popl %ebx |
79 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
76 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
80 | je valid_boot |
77 | je valid_boot |
81 | 78 | ||
82 | xorl %ecx, %ecx # no memory size or map available |
79 | xorl %ecx, %ecx # no memory size or map available |
83 | movl %ecx, e801memorysize |
80 | movl %ecx, e801memorysize |
84 | movl %ecx, e820counter |
81 | movl %ecx, e820counter |
85 | 82 | ||
86 | jmp invalid_boot |
83 | jmp invalid_boot |
87 | 84 | ||
88 | valid_boot: |
85 | valid_boot: |
89 | 86 | ||
90 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
87 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
91 | 88 | ||
92 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
89 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
93 | jc mem_valid |
90 | jc mem_valid |
94 | 91 | ||
95 | xorl %ecx, %ecx |
92 | xorl %ecx, %ecx |
96 | jmp mem_invalid |
93 | jmp mem_invalid |
97 | 94 | ||
98 | mem_valid: |
95 | mem_valid: |
99 | movl 4(%ebx), %ecx # mbi->mem_lower |
96 | movl 4(%ebx), %ecx # mbi->mem_lower |
100 | addl 8(%ebx), %ecx # mbi->mem_upper |
97 | addl 8(%ebx), %ecx # mbi->mem_upper |
101 | 98 | ||
102 | mem_invalid: |
99 | mem_invalid: |
103 | movl %ecx, e801memorysize |
100 | movl %ecx, e801memorysize |
104 | 101 | ||
105 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
102 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
106 | jc mods_valid |
103 | jc mods_valid |
107 | 104 | ||
108 | xorl %ecx, %ecx |
105 | xorl %ecx, %ecx |
109 | xorl %edx, %edx |
106 | xorl %edx, %edx |
110 | jmp mods_invalid |
107 | jmp mods_invalid |
111 | 108 | ||
112 | mods_valid: |
109 | mods_valid: |
113 | movl 20(%ebx), %ecx # mbi->mods_count |
110 | movl 20(%ebx), %ecx # mbi->mods_count |
114 | cmpl $0, %ecx |
111 | cmpl $0, %ecx |
115 | je mods_invalid |
112 | je mods_invalid |
116 | 113 | ||
117 | movl 24(%ebx), %esi # mbi->mods_addr |
114 | movl 24(%ebx), %esi # mbi->mods_addr |
118 | movl 0(%esi), %edx # mods->mod_start |
115 | movl 0(%esi), %edx # mods->mod_start |
119 | movl 4(%esi), %ecx # mods->mod_end |
116 | movl 4(%esi), %ecx # mods->mod_end |
120 | subl %edx, %ecx |
117 | subl %edx, %ecx |
121 | addl $0x80000000, %edx |
118 | addl $0x80000000, %edx |
122 | 119 | ||
123 | mods_invalid: |
120 | mods_invalid: |
124 | movl %ecx, init_size |
121 | movl %ecx, init_size |
125 | movl %edx, init_addr |
122 | movl %edx, init_addr |
126 | 123 | ||
127 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
124 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
128 | jc mmap_valid |
125 | jc mmap_valid |
129 | 126 | ||
130 | xorl %edx, %edx |
127 | xorl %edx, %edx |
131 | jmp mmap_invalid |
128 | jmp mmap_invalid |
132 | 129 | ||
133 | mmap_valid: |
130 | mmap_valid: |
134 | movl 44(%ebx), %ecx # mbi->mmap_length |
131 | movl 44(%ebx), %ecx # mbi->mmap_length |
135 | movl 48(%ebx), %esi # mbi->mmap_addr |
132 | movl 48(%ebx), %esi # mbi->mmap_addr |
136 | movl $e820table, %edi |
133 | movl $e820table, %edi |
137 | xorl %edx, %edx |
134 | xorl %edx, %edx |
138 | 135 | ||
139 | mmap_loop: |
136 | mmap_loop: |
140 | cmpl $0, %ecx |
137 | cmpl $0, %ecx |
141 | jle mmap_end |
138 | jle mmap_end |
142 | 139 | ||
143 | movl 4(%esi), %eax # mmap->base_addr_low |
140 | movl 4(%esi), %eax # mmap->base_addr_low |
144 | movl %eax, (%edi) |
141 | movl %eax, (%edi) |
145 | 142 | ||
146 | movl 8(%esi), %eax # mmap->base_addr_high |
143 | movl 8(%esi), %eax # mmap->base_addr_high |
147 | movl %eax, 4(%edi) |
144 | movl %eax, 4(%edi) |
148 | 145 | ||
149 | movl 12(%esi), %eax # mmap->length_low |
146 | movl 12(%esi), %eax # mmap->length_low |
150 | movl %eax, 8(%edi) |
147 | movl %eax, 8(%edi) |
151 | 148 | ||
152 | movl 16(%esi), %eax # mmap->length_high |
149 | movl 16(%esi), %eax # mmap->length_high |
153 | movl %eax, 12(%edi) |
150 | movl %eax, 12(%edi) |
154 | 151 | ||
155 | movl 20(%esi), %eax # mmap->type |
152 | movl 20(%esi), %eax # mmap->type |
156 | movl %eax, 16(%edi) |
153 | movl %eax, 16(%edi) |
157 | 154 | ||
158 | movl (%esi), %eax # mmap->size |
155 | movl (%esi), %eax # mmap->size |
159 | addl $0x4, %eax |
156 | addl $0x4, %eax |
160 | addl %eax, %esi |
157 | addl %eax, %esi |
161 | subl %eax, %ecx |
158 | subl %eax, %ecx |
162 | addl $MEMMAP_E820_RECORD_SIZE, %edi |
159 | addl $MEMMAP_E820_RECORD_SIZE, %edi |
163 | incl %edx |
160 | incl %edx |
164 | jmp mmap_loop |
161 | jmp mmap_loop |
165 | 162 | ||
166 | mmap_end: |
163 | mmap_end: |
167 | 164 | ||
168 | mmap_invalid: |
165 | mmap_invalid: |
169 | movl %edx, e820counter |
166 | movl %edx, e820counter |
170 | 167 | ||
171 | invalid_boot: |
168 | invalid_boot: |
172 | 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 | ||
173 | call main_bsp - BOOT_OFFSET # never returns |
182 | call main_bsp # never returns |
174 | 183 | ||
175 | cli |
184 | cli |
176 | hlt |
185 | hlt |
177 | 186 | ||
178 | .global map_kernel |
187 | .global map_kernel |
179 | map_kernel: |
188 | map_kernel: |
180 | # |
189 | # |
181 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
190 | # Here we setup mapping for both the unmapped and mapped sections of the kernel. |
182 | # For simplicity, we map the entire 4G space. |
191 | # For simplicity, we map the entire 4G space. |
183 | # |
192 | # |
184 | movl %cr4, %ecx |
193 | movl %cr4, %ecx |
185 | orl $(1<<4), %ecx |
194 | orl $(1<<4), %ecx |
186 | movl %ecx, %cr4 # turn PSE on |
195 | movl %ecx, %cr4 # turn PSE on |
187 | 196 | ||
188 | movl $(page_directory+0), %esi |
197 | movl $(page_directory+0), %esi |
189 | movl $(page_directory+2048), %edi |
198 | movl $(page_directory+2048), %edi |
190 | xorl %ecx, %ecx |
199 | xorl %ecx, %ecx |
191 | xorl %ebx, %ebx |
200 | xorl %ebx, %ebx |
192 | 0: |
201 | 0: |
193 | movl $((1<<7)|(1<<0)), %eax |
202 | movl $((1<<7)|(1<<0)), %eax |
194 | orl %ebx, %eax |
203 | orl %ebx, %eax |
195 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M |
204 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M |
196 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M |
205 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M |
197 | addl $(4*1024*1024), %ebx |
206 | addl $(4*1024*1024), %ebx |
198 | 207 | ||
199 | incl %ecx |
208 | incl %ecx |
200 | cmpl $512, %ecx |
209 | cmpl $512, %ecx |
201 | jl 0b |
210 | jl 0b |
202 | 211 | ||
203 | movl %esi, %cr3 |
212 | movl %esi, %cr3 |
204 | 213 | ||
205 | # turn paging on |
214 | # turn paging on |
206 | movl %cr0, %ebx |
215 | movl %cr0, %ebx |
207 | orl $(1<<31), %ebx |
216 | orl $(1<<31), %ebx |
208 | movl %ebx, %cr0 |
217 | movl %ebx, %cr0 |
209 | ret |
218 | ret |
210 | 219 | ||
211 | 220 | ||
212 | .section K_DATA_START, "aw", @progbits |
221 | .section K_DATA_START, "aw", @progbits |
213 | 222 | ||
214 | .align 4096 |
223 | .align 4096 |
215 | page_directory: |
224 | page_directory: |
216 | .space 4096, 0 |
225 | .space 4096, 0 |
217 | - | ||
218 | .global real_bootstrap_gdtr_boot |
- | |
219 | real_bootstrap_gdtr_boot: |
- | |
220 | .word selector(GDT_ITEMS) |
- | |
221 | .long KA2PA(gdt)-BOOT_OFFSET |
- | |
222 | - | ||
223 | 226 |