Rev 273 | Rev 277 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
164 | palkovsky | 1 | # |
194 | palkovsky | 2 | # Copyright (C) 2001-2004 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 | #define __ASM__ |
30 | |||
31 | #include <arch/mm/page.h> |
||
188 | palkovsky | 32 | #include <arch/mm/ptl.h> |
194 | palkovsky | 33 | #include <arch/pm.h> |
251 | palkovsky | 34 | #include <arch/cpu.h> |
275 | palkovsky | 35 | #include <arch/cpuid.h> |
36 | #include <arch/boot/boot.h> |
||
164 | palkovsky | 37 | |
242 | palkovsky | 38 | #define START_STACK_64 0xffffffff80007c00 |
188 | palkovsky | 39 | |
164 | palkovsky | 40 | # |
41 | # This is where we require any SPARTAN-kernel-compatible boot loader |
||
42 | # to pass control in real mode. |
||
43 | # |
||
44 | # Protected mode tables are statically initialised during compile |
||
45 | # time. So we can just load the respective table registers and |
||
46 | # switch to protected mode. |
||
47 | # |
||
275 | palkovsky | 48 | |
49 | #define START_STACK (BOOTSTRAP_OFFSET-0x400) |
||
50 | |||
188 | palkovsky | 51 | .section K_TEXT_START |
52 | .code16 |
||
53 | .global kernel_image_start |
||
164 | palkovsky | 54 | kernel_image_start: |
178 | palkovsky | 55 | cli |
56 | xorw %ax,%ax |
||
57 | movw %ax,%ds |
||
58 | movw %ax,%ss # initialize stack segment register |
||
275 | palkovsky | 59 | movl $(START_STACK), %esp # initialize stack pointer |
178 | palkovsky | 60 | |
242 | palkovsky | 61 | call memmap_arch_init |
178 | palkovsky | 62 | |
194 | palkovsky | 63 | movl $0x80000000, %eax |
178 | palkovsky | 64 | cpuid |
65 | cmp $0x80000000, %eax # any function > 80000000h? |
||
66 | jbe no_long_mode |
||
275 | palkovsky | 67 | movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001 |
178 | palkovsky | 68 | cpuid |
69 | bt $29, %edx # Test if long mode is supported. |
||
70 | jnc no_long_mode |
||
164 | palkovsky | 71 | |
188 | palkovsky | 72 | # Load gdtr, idtr |
273 | palkovsky | 73 | lgdt bsp_bootstrap_gdtr |
188 | palkovsky | 74 | |
75 | movl %cr0,%eax |
||
76 | orl $0x1,%eax |
||
77 | movl %eax,%cr0 # switch to protected mode |
||
164 | palkovsky | 78 | |
206 | palkovsky | 79 | jmpl $gdtselector(KTEXT32_DES), $now_in_prot |
164 | palkovsky | 80 | |
188 | palkovsky | 81 | no_long_mode: |
82 | 1: |
||
83 | jmp 1b |
||
84 | |||
85 | # Protected 16-bit. We want to reuse the code-seg descriptor, |
||
86 | # the Default operand size must not be 1 when entering long mode |
||
87 | now_in_prot: |
||
88 | # Set up stack & data descriptors |
||
206 | palkovsky | 89 | movw $gdtselector(KDATA_DES), %ax |
188 | palkovsky | 90 | movw %ax, %ds |
91 | movw %ax, %fs |
||
92 | movw %ax, %gs |
||
93 | movw %ax, %ss |
||
94 | |||
275 | palkovsky | 95 | movb $0xd1, %al # enable A20 using the keyboard controller |
96 | outb %al, $0x64 |
||
97 | movb $0xdf, %al |
||
98 | outb %al, $0x60 |
||
99 | |||
100 | |||
188 | palkovsky | 101 | # Enable 64-bit page transaltion entries - CR4.PAE = 1. |
102 | # Paging is not enabled until after long mode is enabled |
||
103 | movl %cr4, %eax |
||
104 | btsl $5, %eax |
||
105 | movl %eax, %cr4 |
||
106 | |||
107 | # Set up paging tables |
||
108 | leal ptl_0, %eax |
||
109 | movl %eax, %cr3 |
||
275 | palkovsky | 110 | |
188 | palkovsky | 111 | # Enable long mode |
251 | palkovsky | 112 | movl $EFER_MSR_NUM, %ecx # EFER MSR number |
188 | palkovsky | 113 | rdmsr # Read EFER |
251 | palkovsky | 114 | btsl $AMD_LME_FLAG, %eax # Set LME=1 |
188 | palkovsky | 115 | wrmsr # Write EFER |
178 | palkovsky | 116 | |
188 | palkovsky | 117 | # Enable paging to activate long mode (set CR0.PG=1) |
118 | movl %cr0, %eax |
||
119 | btsl $31, %eax |
||
120 | movl %eax, %cr0 |
||
121 | |||
122 | # At this point we are in compatibility mode |
||
206 | palkovsky | 123 | jmpl $gdtselector(KTEXT_DES), $start64 |
164 | palkovsky | 124 | |
188 | palkovsky | 125 | .code64 |
126 | start64: |
||
275 | palkovsky | 127 | movq $(PA2KA(START_STACK)), %rsp |
128 | |||
129 | # Copy kernel to higher physical memory |
||
130 | movq $BOOTSTRAP_OFFSET, %rsi |
||
131 | movq $BOOTSTRAP_OFFSET + BOOT_OFFSET, %rdi |
||
132 | movq $_hardcoded_kernel_size, %rcx |
||
133 | cld |
||
134 | rep movsb |
||
178 | palkovsky | 135 | |
188 | palkovsky | 136 | call main_bsp # never returns |
178 | palkovsky | 137 | 1: |
138 | jmp 1b |
||
206 | palkovsky | 139 | |
188 | palkovsky | 140 | .section K_DATA_START |
164 | palkovsky | 141 | .align 4096 |
206 | palkovsky | 142 | |
143 | # Identical mapping of first 16MB and the same of -2GB -> 0 |
||
188 | palkovsky | 144 | .global ptl_2 |
145 | ptl_2: |
||
146 | .quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
147 | .quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
148 | .quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
149 | .quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
150 | .quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
151 | .quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
152 | .quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
153 | .quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
154 | |||
155 | .align 4096 |
||
156 | .global ptl_1 |
||
157 | ptl_1: |
||
158 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT) |
||
159 | .fill 509,8,0 |
||
160 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT) |
||
161 | .fill 2,8,0 |
||
162 | |||
163 | .align 4096 |
||
164 | .global ptl_0 |
||
165 | ptl_0: |
||
166 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
||
167 | .fill 510,8,0 |
||
168 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
||
178 | palkovsky | 169 | |
188 | palkovsky | 170 | .global gdtr_inst |
273 | palkovsky | 171 | bsp_bootstrap_gdtr: |
206 | palkovsky | 172 | .word gdtselector(GDT_ITEMS) |
275 | palkovsky | 173 | .long KA2PA(gdt)-BOOT_OFFSET |
188 | palkovsky | 174 | |
273 | palkovsky | 175 | .global ap_bootstrap_gdtr |
176 | ap_bootstrap_gdtr: |
||
177 | .word gdtselector(GDT_ITEMS) |
||
275 | palkovsky | 178 | .long KA2PA(gdt)-BOOT_OFFSET |