1 // arch/x86/entry.S - x64 entry points (booting and traps)
3 // This software is copyright (c) 2006 Scott Wood.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
23 #include <arch/addrs.h>
30 .long 0x1badb002 // Multi-boot magic
33 // bit 0: 4KiB-align all boot modules
34 // bit 1: must include memory size and map
35 // bit 2: must include video mode table
36 // bit 16: load addresses in this header are valid
37 // and should be used instead of the ELF header
41 // checksum: -(magic + flags), update if flags change
44 .long multiboot_hdr - KERNEL_START // header_addr
45 .long 0x00200000 // load_addr
46 .long 0 // load_end_addr: load whole file
47 .long bss_end - KERNEL_START // bss_end_addr
48 .long _start - KERNEL_START // entry_addr
52 cmpl $0x2badb002, %eax
55 lgdt x64_gdtr_phys + 6 - KERNEL_START
56 ljmp $0x10, $using_our_gdt - KERNEL_START
66 movl %ebx, %esi // Save the multiboot pointer somewhere
67 // it won't be clobbered by CPUID
69 // This gives 256 bytes to Threads::Thread; if it gets larger
70 // this needs to be updated (as well as the code at high_vaddr).
72 movl $x64_init_stack + 3840 - KERNEL_START, %esp
78 xorl $0x00200000, %eax
87 movl $0x80000000, %eax
89 cmpl $0x80000000, %eax
91 movl $0x80000001, %eax
96 movl $0xc0000080, %ecx // Extended Feature Enable Register (EFER)
98 movl $0x100, %eax // Enable long mode
106 // Set page table attributes
107 orl $7, x64_init_ptbl_l4 - KERNEL_START
108 orl $7, x64_init_ptbl_l3 - KERNEL_START
109 // orl $7, x64_init_ptbl_l2 - KERNEL_START
111 // Load the initial page table
112 movl $x64_init_ptbl_l4 - KERNEL_START, %eax
115 // enable paging, kernel write-protect,
116 // and internal floating point error handling
118 orl $0x80010020, %eax
120 ljmp $0x18, $in_code64 - KERNEL_START
124 // Set up high page tables for 0xffffffff80000000 mapping,
125 // reusing the tables previously used for the low identity
128 movq x64_init_ptbl_l4 - KERNEL_START, %rax
129 movq %rax, x64_init_ptbl_l4 - KERNEL_START + 0xff8
131 movq x64_init_ptbl_l3 - KERNEL_START, %rax
132 movq %rax, x64_init_ptbl_l3 - KERNEL_START + 0xff0
135 movl %esi, x64_boot_info_phys
137 movq $high_vaddr, %rax
141 movq $x64_init_stack + 3840, %rsp
146 movl $no_multiboot_str - KERNEL_START, %esi
149 movl $no_long_mode_str - KERNEL_START, %esi
155 1: movb %al, (%edi, %ecx, 2)
156 movb $14, 1(%edi, %ecx, 2) // It's not at the cursor, so use
157 // yellow to make it stand out.
159 movb (%esi, %ecx), %al
166 .string "This CPU does not support long (64-bit) mode. Use a 32-bit kernel."
169 .string "Unrecognized bootloader; a multiboot-compliant loader is required."
173 .macro pushvolatilesnordi
201 // Non-volatile registers must be pushed if the handler will
202 // need to access all of the interrupted code's registers,
203 // such as when producing an error dump. Does not include
204 // r15, as that is usually swapped with the error code.
258 .global x64_invalid_insn
263 call x64_do_invalid_insn
270 xchgq %r15, (%rsp) // get error code
280 .global x64_page_fault
282 xchgq %r15, (%rsp) // get error code
288 call x64_do_page_fault
296 subq $8, %rsp // Keep the stack frame 16-byte aligned
299 movl need_resched, %eax
309 // The cli is to make sure interrupts don't get re-enabled in
310 // this thread context between the schedule and the ret from
343 .global x64_new_thread
359 call sched_new_thread