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