Rev 685 | Rev 695 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
164 | palkovsky | 1 | # |
288 | jermar | 2 | # Copyright (C) 2005 Ondrej Palkovsky |
164 | palkovsky | 3 | # All rights reserved. |
4 | # |
||
5 | # Redistribution and use in source and binary forms, with or without |
||
6 | # modification, are permitted provided that the following conditions |
||
7 | # are met: |
||
8 | # |
||
9 | # - Redistributions of source code must retain the above copyright |
||
10 | # notice, this list of conditions and the following disclaimer. |
||
11 | # - Redistributions in binary form must reproduce the above copyright |
||
12 | # notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | # derived from this software without specific prior written permission. |
||
16 | # |
||
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 |
||
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | # |
||
28 | |||
194 | palkovsky | 29 | #include <arch/mm/page.h> |
188 | palkovsky | 30 | #include <arch/mm/ptl.h> |
194 | palkovsky | 31 | #include <arch/pm.h> |
251 | palkovsky | 32 | #include <arch/cpu.h> |
275 | palkovsky | 33 | #include <arch/cpuid.h> |
34 | #include <arch/boot/boot.h> |
||
164 | palkovsky | 35 | |
694 | decky | 36 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE) |
242 | palkovsky | 37 | #define START_STACK_64 0xffffffff80007c00 |
275 | palkovsky | 38 | |
406 | jermar | 39 | .section K_TEXT_START, "ax" |
694 | decky | 40 | # .code16 |
41 | # .global kernel_image_start |
||
42 | # .global multiboot_image_start |
||
43 | # kernel_image_start: |
||
44 | # cli |
||
45 | # xorw %ax,%ax |
||
46 | # movw %ax,%ds |
||
47 | # movw %ax,%es |
||
48 | # movw %ax,%ss # initialize stack segment register |
||
49 | # movl $(START_STACK), %esp # initialize stack pointer |
||
50 | # |
||
51 | # call memmap_arch_init |
||
52 | # |
||
53 | # movl $0x80000000, %eax |
||
54 | # cpuid |
||
55 | # cmp $0x80000000, %eax # any function > 80000000h? |
||
56 | # jbe no_long_mode |
||
57 | # movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001 |
||
58 | # cpuid |
||
59 | # bt $29, %edx # Test if long mode is supported. |
||
60 | # jnc no_long_mode |
||
61 | # |
||
62 | # # Load gdtr, idtr |
||
63 | # lgdt real_bootstrap_gdtr_boot |
||
64 | # |
||
65 | # movl %cr0,%eax |
||
66 | # orl $0x1,%eax |
||
67 | # movl %eax,%cr0 # switch to protected mode |
||
68 | # |
||
69 | # jmpl $gdtselector(KTEXT32_DES), $now_in_prot |
||
70 | # |
||
71 | # no_long_mode: |
||
72 | # 1: |
||
73 | # jmp 1b |
||
74 | # |
||
680 | decky | 75 | .code32 |
76 | .align 4 |
||
77 | multiboot_header: |
||
78 | .long MULTIBOOT_HEADER_MAGIC |
||
79 | .long MULTIBOOT_HEADER_FLAGS |
||
80 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
||
694 | decky | 81 | .long multiboot_header |
82 | .long unmapped_ktext_start |
||
680 | decky | 83 | .long 0 |
84 | .long 0 |
||
694 | decky | 85 | .long multiboot_image_start |
188 | palkovsky | 86 | |
680 | decky | 87 | multiboot_image_start: |
88 | movl $START_STACK, %esp # initialize stack pointer |
||
685 | decky | 89 | |
90 | lgdt protected_bootstrap_gdtr + BOOT_OFFSET # initialize Global Descriptor Table register |
||
680 | decky | 91 | |
685 | decky | 92 | movw $gdtselector(KDATA_DES), %cx |
93 | movw %cx, %es |
||
94 | movw %cx, %gs |
||
95 | movw %cx, %fs |
||
96 | movw %cx, %ds # kernel data + stack |
||
97 | movw %cx, %ss |
||
98 | |||
99 | jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point + BOOT_OFFSET |
||
100 | multiboot_meeting_point: |
||
101 | |||
694 | decky | 102 | # Protected 32-bit. We want to reuse the code-seg descriptor, |
103 | # the Default operand size must not be 1 when entering long mode |
||
275 | palkovsky | 104 | |
694 | decky | 105 | pushl %ebx # save parameters from GRUB |
106 | pushl %eax |
||
107 | |||
188 | palkovsky | 108 | # Enable 64-bit page transaltion entries - CR4.PAE = 1. |
109 | # Paging is not enabled until after long mode is enabled |
||
694 | decky | 110 | |
188 | palkovsky | 111 | movl %cr4, %eax |
112 | btsl $5, %eax |
||
113 | movl %eax, %cr4 |
||
114 | |||
115 | # Set up paging tables |
||
694 | decky | 116 | |
188 | palkovsky | 117 | leal ptl_0, %eax |
118 | movl %eax, %cr3 |
||
275 | palkovsky | 119 | |
188 | palkovsky | 120 | # Enable long mode |
178 | palkovsky | 121 | |
694 | decky | 122 | movl $EFER_MSR_NUM, %ecx # EFER MSR number |
123 | rdmsr # Read EFER |
||
124 | btsl $AMD_LME_FLAG, %eax # Set LME=1 |
||
125 | wrmsr # Write EFER |
||
126 | |||
188 | palkovsky | 127 | # Enable paging to activate long mode (set CR0.PG=1) |
694 | decky | 128 | |
188 | palkovsky | 129 | movl %cr0, %eax |
130 | btsl $31, %eax |
||
131 | movl %eax, %cr0 |
||
132 | |||
133 | # At this point we are in compatibility mode |
||
694 | decky | 134 | |
206 | palkovsky | 135 | jmpl $gdtselector(KTEXT_DES), $start64 |
164 | palkovsky | 136 | |
188 | palkovsky | 137 | .code64 |
138 | start64: |
||
275 | palkovsky | 139 | movq $(PA2KA(START_STACK)), %rsp |
140 | |||
694 | decky | 141 | call main_bsp # never returns |
178 | palkovsky | 142 | |
694 | decky | 143 | cli |
144 | hlt |
||
206 | palkovsky | 145 | |
406 | jermar | 146 | .section K_DATA_START, "aw", @progbits |
164 | palkovsky | 147 | .align 4096 |
206 | palkovsky | 148 | |
417 | palkovsky | 149 | # Identical mapping of first 64MB and the same of -2GB -> 0 |
188 | palkovsky | 150 | .global ptl_2 |
151 | ptl_2: |
||
152 | .quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
153 | .quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
154 | .quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
155 | .quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
156 | .quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
157 | .quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
158 | .quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
159 | .quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
417 | palkovsky | 160 | .quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
161 | .quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
162 | .quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
163 | .quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
164 | .quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
165 | .quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
166 | .quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
167 | .quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
168 | .quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
169 | .quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
170 | .quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
171 | .quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
172 | .quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
173 | .quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
174 | .quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
175 | .quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
176 | .quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
177 | .quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
178 | .quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
179 | .quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
180 | .quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
181 | .quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
182 | .quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
183 | .quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
188 | palkovsky | 184 | |
185 | .align 4096 |
||
186 | .global ptl_1 |
||
187 | ptl_1: |
||
188 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT) |
||
189 | .fill 509,8,0 |
||
190 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT) |
||
293 | palkovsky | 191 | .fill 1,8,0 |
188 | palkovsky | 192 | |
193 | .align 4096 |
||
194 | .global ptl_0 |
||
195 | ptl_0: |
||
196 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
||
197 | .fill 510,8,0 |
||
198 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
||
178 | palkovsky | 199 | |
685 | decky | 200 | .global protected_bootstrap_gdtr |
201 | protected_bootstrap_gdtr: |
||
202 | .word gdtselector(GDT_ITEMS) |
||
203 | .long KA2PA(gdt) |