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