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