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