Rev 694 | Rev 696 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 694 | Rev 695 | ||
---|---|---|---|
Line 24... | Line 24... | ||
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 | #include <arch/boot/boot.h> |
|
- | 30 | #include <arch/boot/memmap.h> |
|
29 | #include <arch/mm/page.h> |
31 | #include <arch/mm/page.h> |
30 | #include <arch/mm/ptl.h> |
32 | #include <arch/mm/ptl.h> |
31 | #include <arch/pm.h> |
33 | #include <arch/pm.h> |
32 | #include <arch/cpu.h> |
34 | #include <arch/cpu.h> |
33 | #include <arch/cpuid.h> |
35 | #include <arch/cpuid.h> |
34 | #include <arch/boot/boot.h> |
- | |
35 | 36 | ||
36 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE) |
37 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE) |
37 | #define START_STACK_64 0xffffffff80007c00 |
- | |
38 | 38 | ||
39 | .section K_TEXT_START, "ax" |
39 | .section K_TEXT_START, "ax" |
40 | # .code16 |
40 | # .code16 |
41 | # .global kernel_image_start |
41 | # .global kernel_image_start |
42 | # .global multiboot_image_start |
42 | # .global multiboot_image_start |
43 | # kernel_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 | # |
44 | |
51 | # call memmap_arch_init |
- | |
52 | # |
- | |
53 | # movl $0x80000000, %eax |
45 | # movl $0x80000000, %eax |
54 | # cpuid |
46 | # cpuid |
55 | # cmp $0x80000000, %eax # any function > 80000000h? |
47 | # cmp $0x80000000, %eax # any function > 80000000h? |
56 | # jbe no_long_mode |
48 | # jbe no_long_mode |
57 | # movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001 |
49 | # movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001 |
58 | # cpuid |
50 | # cpuid |
59 | # bt $29, %edx # Test if long mode is supported. |
51 | # bt $29, %edx # Test if long mode is supported. |
60 | # jnc no_long_mode |
52 | # jnc no_long_mode |
61 | # |
53 | # |
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 | # |
54 | # |
71 | # no_long_mode: |
55 | # no_long_mode: |
72 | # 1: |
56 | # 1: |
73 | # jmp 1b |
57 | # jmp 1b |
74 | # |
58 | # |
75 | .code32 |
59 | .code32 |
76 | .align 4 |
60 | .align 4 |
- | 61 | .global multiboot_image_start |
|
77 | multiboot_header: |
62 | multiboot_header: |
78 | .long MULTIBOOT_HEADER_MAGIC |
63 | .long MULTIBOOT_HEADER_MAGIC |
79 | .long MULTIBOOT_HEADER_FLAGS |
64 | .long MULTIBOOT_HEADER_FLAGS |
80 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
65 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
81 | .long multiboot_header |
66 | .long multiboot_header |
Line 83... | Line 68... | ||
83 | .long 0 |
68 | .long 0 |
84 | .long 0 |
69 | .long 0 |
85 | .long multiboot_image_start |
70 | .long multiboot_image_start |
86 | 71 | ||
87 | multiboot_image_start: |
72 | multiboot_image_start: |
88 | movl $START_STACK, %esp # initialize stack pointer |
73 | movl $START_STACK, %esp # initialize stack pointer |
89 | - | ||
90 | lgdt protected_bootstrap_gdtr + BOOT_OFFSET # initialize Global Descriptor Table register |
74 | lgdt bootstrap_gdtr # initialize Global Descriptor Table register |
91 | 75 | ||
92 | movw $gdtselector(KDATA_DES), %cx |
76 | movw $gdtselector(KDATA_DES), %cx |
93 | movw %cx, %es |
77 | movw %cx, %es |
94 | movw %cx, %gs |
78 | movw %cx, %gs |
95 | movw %cx, %fs |
79 | movw %cx, %fs |
96 | movw %cx, %ds # kernel data + stack |
80 | movw %cx, %ds # kernel data + stack |
97 | movw %cx, %ss |
81 | movw %cx, %ss |
98 | 82 | ||
99 | jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point + BOOT_OFFSET |
83 | jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point |
100 | multiboot_meeting_point: |
84 | multiboot_meeting_point: |
101 | 85 | ||
- | 86 | movl %eax, grub_eax # save parameters from GRUB |
|
- | 87 | movl %ebx, grub_ebx |
|
- | 88 | ||
102 | # Protected 32-bit. We want to reuse the code-seg descriptor, |
89 | # Protected 32-bit. We want to reuse the code-seg descriptor, |
103 | # the Default operand size must not be 1 when entering long mode |
90 | # the Default operand size must not be 1 when entering long mode |
104 | 91 | ||
105 | pushl %ebx # save parameters from GRUB |
- | |
106 | pushl %eax |
- | |
107 | - | ||
108 | # Enable 64-bit page transaltion entries - CR4.PAE = 1. |
92 | # Enable 64-bit page transaltion entries - CR4.PAE = 1. |
109 | # Paging is not enabled until after long mode is enabled |
93 | # Paging is not enabled until after long mode is enabled |
110 | 94 | ||
111 | movl %cr4, %eax |
95 | movl %cr4, %eax |
112 | btsl $5, %eax |
96 | btsl $5, %eax |
Line 135... | Line 119... | ||
135 | jmpl $gdtselector(KTEXT_DES), $start64 |
119 | jmpl $gdtselector(KTEXT_DES), $start64 |
136 | 120 | ||
137 | .code64 |
121 | .code64 |
138 | start64: |
122 | start64: |
139 | movq $(PA2KA(START_STACK)), %rsp |
123 | movq $(PA2KA(START_STACK)), %rsp |
- | 124 | movl grub_eax, %eax |
|
- | 125 | movl grub_ebx, %ebx |
|
- | 126 | ||
- | 127 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
|
- | 128 | je valid_boot |
|
- | 129 | ||
- | 130 | xorl %ecx, %ecx # no memory size or map available |
|
- | 131 | movl %ecx, e801memorysize |
|
- | 132 | movl %ecx, e820counter |
|
- | 133 | ||
- | 134 | jmp invalid_boot |
|
- | 135 | ||
- | 136 | valid_boot: |
|
- | 137 | ||
- | 138 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
|
- | 139 | ||
- | 140 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
|
- | 141 | jc mem_valid |
|
- | 142 | ||
- | 143 | xorl %ecx, %ecx |
|
- | 144 | jmp mem_invalid |
|
- | 145 | ||
- | 146 | mem_valid: |
|
- | 147 | movl 4(%ebx), %ecx # mbi->mem_lower |
|
- | 148 | addl 8(%ebx), %ecx # mbi->mem_upper |
|
- | 149 | ||
- | 150 | mem_invalid: |
|
- | 151 | movl %ecx, e801memorysize |
|
- | 152 | ||
- | 153 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
|
- | 154 | jc mods_valid |
|
- | 155 | ||
- | 156 | xorl %ecx, %ecx |
|
- | 157 | xorl %edx, %edx |
|
- | 158 | jmp mods_invalid |
|
- | 159 | ||
- | 160 | mods_valid: |
|
- | 161 | movl 20(%ebx), %ecx # mbi->mods_count |
|
- | 162 | cmpl $0, %ecx |
|
- | 163 | je mods_invalid |
|
- | 164 | ||
- | 165 | movl 24(%ebx), %esi # mbi->mods_addr |
|
- | 166 | movl 0(%esi), %edx # mods->mod_start |
|
- | 167 | movl 4(%esi), %ecx # mods->mod_end |
|
- | 168 | subl %edx, %ecx |
|
- | 169 | addl $0x80000000, %edx |
|
- | 170 | ||
- | 171 | mods_invalid: |
|
- | 172 | movl %ecx, init_size |
|
- | 173 | movl %edx, init_addr |
|
- | 174 | ||
- | 175 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
|
- | 176 | jc mmap_valid |
|
- | 177 | ||
- | 178 | xorl %edx, %edx |
|
- | 179 | jmp mmap_invalid |
|
- | 180 | ||
- | 181 | mmap_valid: |
|
- | 182 | movl 44(%ebx), %ecx # mbi->mmap_length |
|
- | 183 | movl 48(%ebx), %esi # mbi->mmap_addr |
|
- | 184 | movq $e820table, %rdi |
|
- | 185 | xorl %edx, %edx |
|
- | 186 | ||
- | 187 | mmap_loop: |
|
- | 188 | cmpl $0, %ecx |
|
- | 189 | jle mmap_end |
|
- | 190 | ||
- | 191 | movl 4(%esi), %eax # mmap->base_addr_low |
|
- | 192 | movl %eax, (%rdi) |
|
- | 193 | ||
- | 194 | movl 8(%esi), %eax # mmap->base_addr_high |
|
- | 195 | movl %eax, 4(%rdi) |
|
- | 196 | ||
- | 197 | movl 12(%esi), %eax # mmap->length_low |
|
- | 198 | movl %eax, 8(%rdi) |
|
- | 199 | ||
- | 200 | movl 16(%esi), %eax # mmap->length_high |
|
- | 201 | movl %eax, 12(%rdi) |
|
- | 202 | ||
- | 203 | movl 20(%esi), %eax # mmap->type |
|
- | 204 | movl %eax, 16(%rdi) |
|
- | 205 | ||
- | 206 | movl (%esi), %eax # mmap->size |
|
- | 207 | addl $0x4, %eax |
|
- | 208 | addl %eax, %esi |
|
- | 209 | subl %eax, %ecx |
|
- | 210 | addq $MEMMAP_E820_RECORD_SIZE, %rdi |
|
- | 211 | incl %edx |
|
- | 212 | jmp mmap_loop |
|
- | 213 | ||
- | 214 | mmap_end: |
|
- | 215 | ||
- | 216 | mmap_invalid: |
|
- | 217 | movl %edx, e820counter |
|
- | 218 | ||
- | 219 | invalid_boot: |
|
140 | 220 | ||
141 | call main_bsp # never returns |
221 | call main_bsp # never returns |
142 | 222 | ||
143 | cli |
223 | cli |
144 | hlt |
224 | hlt |
145 | 225 | ||
Line 195... | Line 275... | ||
195 | ptl_0: |
275 | ptl_0: |
196 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
276 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
197 | .fill 510,8,0 |
277 | .fill 510,8,0 |
198 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
278 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
199 | 279 | ||
200 | .global protected_bootstrap_gdtr |
280 | .global bootstrap_gdtr |
201 | protected_bootstrap_gdtr: |
281 | bootstrap_gdtr: |
202 | .word gdtselector(GDT_ITEMS) |
282 | .word gdtselector(GDT_ITEMS) |
203 | .long KA2PA(gdt) |
283 | .long KA2PA(gdt) |
- | 284 | ||
- | 285 | grub_eax: |
|
- | 286 | .long 0 |
|
- | 287 | ||
- | 288 | grub_ebx: |
|
- | 289 | .long 0 |