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 | #include <arch/cpuid.h>
|
---|
35 |
|
---|
36 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
37 |
|
---|
38 | .section K_TEXT_START, "ax"
|
---|
39 |
|
---|
40 | .code32
|
---|
41 | .align 4
|
---|
42 | .global multiboot_image_start
|
---|
43 | multiboot_header:
|
---|
44 | .long MULTIBOOT_HEADER_MAGIC
|
---|
45 | .long MULTIBOOT_HEADER_FLAGS
|
---|
46 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
47 | .long multiboot_header
|
---|
48 | .long unmapped_ktext_start
|
---|
49 | .long 0
|
---|
50 | .long 0
|
---|
51 | .long multiboot_image_start
|
---|
52 |
|
---|
53 | multiboot_image_start:
|
---|
54 | cld
|
---|
55 | movl $START_STACK, %esp # initialize stack pointer
|
---|
56 | lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register
|
---|
57 |
|
---|
58 | movw $gdtselector(KDATA_DES), %cx
|
---|
59 | movw %cx, %es
|
---|
60 | movw %cx, %fs
|
---|
61 | movw %cx, %gs
|
---|
62 | movw %cx, %ds # kernel data + stack
|
---|
63 | movw %cx, %ss
|
---|
64 |
|
---|
65 | jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point
|
---|
66 | multiboot_meeting_point:
|
---|
67 |
|
---|
68 | movl %eax, grub_eax # save parameters from GRUB
|
---|
69 | movl %ebx, grub_ebx
|
---|
70 |
|
---|
71 | movl $(INTEL_CPUID_LEVEL), %eax
|
---|
72 | cpuid
|
---|
73 | cmp $0x0, %eax # any function > 0?
|
---|
74 | jbe pse_unsupported
|
---|
75 |
|
---|
76 | movl $(INTEL_CPUID_STANDARD), %eax
|
---|
77 | cpuid
|
---|
78 | bt $(INTEL_PSE), %edx
|
---|
79 | jc pse_supported
|
---|
80 |
|
---|
81 | pse_unsupported:
|
---|
82 | movl $pse_msg, %esi
|
---|
83 | jmp error_halt
|
---|
84 |
|
---|
85 | pse_supported:
|
---|
86 |
|
---|
87 | bt $(INTEL_SEP), %edx
|
---|
88 | jc sep_supported
|
---|
89 |
|
---|
90 | movl $sep_msg, %esi
|
---|
91 | jmp error_halt
|
---|
92 |
|
---|
93 | sep_supported:
|
---|
94 |
|
---|
95 | #include "vesa_prot.inc"
|
---|
96 |
|
---|
97 | # map kernel and turn paging on
|
---|
98 | call map_kernel
|
---|
99 |
|
---|
100 | # call arch_pre_main(grub_eax, grub_ebx)
|
---|
101 | pushl grub_ebx
|
---|
102 | pushl grub_eax
|
---|
103 | call arch_pre_main
|
---|
104 |
|
---|
105 | call main_bsp
|
---|
106 |
|
---|
107 | # not reached
|
---|
108 | cli
|
---|
109 | hlt0:
|
---|
110 | hlt
|
---|
111 | jmp hlt0
|
---|
112 |
|
---|
113 | .global map_kernel
|
---|
114 | map_kernel:
|
---|
115 | #
|
---|
116 | # Here we setup mapping for both the unmapped and mapped sections of the kernel.
|
---|
117 | # For simplicity, we map the entire 4G space.
|
---|
118 | #
|
---|
119 | movl %cr4, %ecx
|
---|
120 | orl $(1 << 4), %ecx # turn PSE on
|
---|
121 | andl $(~(1 << 5)), %ecx # turn PAE off
|
---|
122 | movl %ecx, %cr4
|
---|
123 |
|
---|
124 | movl $(page_directory + 0), %esi
|
---|
125 | movl $(page_directory + 2048), %edi
|
---|
126 | xorl %ecx, %ecx
|
---|
127 | xorl %ebx, %ebx
|
---|
128 |
|
---|
129 | floop:
|
---|
130 | movl $((1 << 7) | (1 << 1) | (1 << 0)), %eax
|
---|
131 | orl %ebx, %eax
|
---|
132 | movl %eax, (%esi, %ecx, 4) # mapping 0x00000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
|
---|
133 | movl %eax, (%edi, %ecx, 4) # mapping 0x80000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
|
---|
134 | addl $(4 * 1024 * 1024), %ebx
|
---|
135 |
|
---|
136 | incl %ecx
|
---|
137 | cmpl $512, %ecx
|
---|
138 | jl floop
|
---|
139 |
|
---|
140 | movl %esi, %cr3
|
---|
141 |
|
---|
142 | movl %cr0, %ebx
|
---|
143 | orl $(1 << 31), %ebx # turn paging on
|
---|
144 | movl %ebx, %cr0
|
---|
145 | ret
|
---|
146 |
|
---|
147 | # Print string from %esi to EGA display (in red) and halt
|
---|
148 | error_halt:
|
---|
149 | movl $0xb8000, %edi # base of EGA text mode memory
|
---|
150 | xorl %eax, %eax
|
---|
151 |
|
---|
152 | movw $0x3d4, %dx # read bits 8 - 15 of the cursor address
|
---|
153 | movb $0xe, %al
|
---|
154 | outb %al, %dx
|
---|
155 |
|
---|
156 | movw $0x3d5, %dx
|
---|
157 | inb %dx, %al
|
---|
158 | shl $8, %ax
|
---|
159 |
|
---|
160 | movw $0x3d4, %dx # read bits 0 - 7 of the cursor address
|
---|
161 | movb $0xf, %al
|
---|
162 | outb %al, %dx
|
---|
163 |
|
---|
164 | movw $0x3d5, %dx
|
---|
165 | inb %dx, %al
|
---|
166 |
|
---|
167 | cmp $1920, %ax
|
---|
168 | jbe cursor_ok
|
---|
169 |
|
---|
170 | movw $1920, %ax # sanity check for the cursor on the last line
|
---|
171 |
|
---|
172 | cursor_ok:
|
---|
173 |
|
---|
174 | movw %ax, %bx
|
---|
175 | shl $1, %eax
|
---|
176 | addl %eax, %edi
|
---|
177 |
|
---|
178 | movw $0x0c00, %ax # black background, light red foreground
|
---|
179 |
|
---|
180 | ploop:
|
---|
181 | lodsb
|
---|
182 | cmp $0, %al
|
---|
183 | je ploop_end
|
---|
184 | stosw
|
---|
185 | inc %bx
|
---|
186 | jmp ploop
|
---|
187 | ploop_end:
|
---|
188 |
|
---|
189 | movw $0x3d4, %dx # write bits 8 - 15 of the cursor address
|
---|
190 | movb $0xe, %al
|
---|
191 | outb %al, %dx
|
---|
192 |
|
---|
193 | movw $0x3d5, %dx
|
---|
194 | movb %bh, %al
|
---|
195 | outb %al, %dx
|
---|
196 |
|
---|
197 | movw $0x3d4, %dx # write bits 0 - 7 of the cursor address
|
---|
198 | movb $0xf, %al
|
---|
199 | outb %al, %dx
|
---|
200 |
|
---|
201 | movw $0x3d5, %dx
|
---|
202 | movb %bl, %al
|
---|
203 | outb %al, %dx
|
---|
204 |
|
---|
205 | cli
|
---|
206 | hlt1:
|
---|
207 | hlt
|
---|
208 | jmp hlt1
|
---|
209 |
|
---|
210 | #include "vesa_real.inc"
|
---|
211 |
|
---|
212 | .section K_DATA_START, "aw", @progbits
|
---|
213 |
|
---|
214 | .align 4096
|
---|
215 | page_directory:
|
---|
216 | .space 4096, 0
|
---|
217 |
|
---|
218 | grub_eax:
|
---|
219 | .long 0
|
---|
220 |
|
---|
221 | grub_ebx:
|
---|
222 | .long 0
|
---|
223 |
|
---|
224 | pse_msg:
|
---|
225 | .asciz "Page Size Extension not supported. System halted."
|
---|
226 |
|
---|
227 | sep_msg:
|
---|
228 | .asciz "SYSENTER/SYSEXIT not supported. System halted."
|
---|