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 conditions:
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimers.
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimers in the
17 // documentation and/or other materials provided with the distribution.
19 // * The names of the Software's authors and/or contributors
20 // may not be used to endorse or promote products derived from
21 // this Software without specific prior written permission.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
31 #include <arch/addrs.h>
38 .long 0x1badb002 // Multi-boot magic
41 // bit 0: 4KiB-align all boot modules
42 // bit 1: must include memory size and map
43 // bit 2: must include video mode table
44 // bit 16: load addresses in this header are valid
45 // and should be used instead of the ELF header
49 // checksum: -(magic + flags), update if flags change
52 .long multiboot_hdr - KERNEL_START // header_addr
53 .long 0x00200000 // load_addr
54 .long 0 // load_end_addr: load whole file
55 .long bss_end - KERNEL_START // bss_end_addr
56 .long _start - KERNEL_START // entry_addr
60 cmpl $0x2badb002, %eax
63 lgdt x64_gdtr_phys + 6 - KERNEL_START
64 ljmp $0x10, $using_our_gdt - KERNEL_START
74 movl %ebx, %esi // Save the multiboot pointer somewhere
75 // it won't be clobbered by CPUID
77 // This gives 256 bytes to Threads::Thread; if it gets larger
78 // this needs to be updated (as well as the code at high_vaddr).
80 movl $x64_init_stack + 3840 - KERNEL_START, %esp
86 xorl $0x00200000, %eax
95 movl $0x80000000, %eax
97 cmpl $0x80000000, %eax
99 movl $0x80000001, %eax
104 movl $0xc0000080, %ecx // Extended Feature Enable Register (EFER)
106 movl $0x100, %eax // Enable long mode
114 // Set page table attributes
115 orl $7, x64_init_ptbl_l4 - KERNEL_START
116 orl $7, x64_init_ptbl_l3 - KERNEL_START
117 // orl $7, x64_init_ptbl_l2 - KERNEL_START
119 // Load the initial page table
120 movl $x64_init_ptbl_l4 - KERNEL_START, %eax
123 // enable paging, kernel write-protect,
124 // and internal floating point error handling
126 orl $0x80010020, %eax
128 ljmp $0x18, $in_code64 - KERNEL_START
132 // Set up high page tables for 0xffffffff80000000 mapping,
133 // reusing the tables previously used for the low identity
136 movq x64_init_ptbl_l4 - KERNEL_START, %rax
137 movq %rax, x64_init_ptbl_l4 - KERNEL_START + 0xff8
139 movq x64_init_ptbl_l3 - KERNEL_START, %rax
140 movq %rax, x64_init_ptbl_l3 - KERNEL_START + 0xff0
143 movl %esi, x64_boot_info_phys
145 movq $high_vaddr, %rax
149 movq $x64_init_stack + 3840, %rsp
154 movl $no_multiboot_str - KERNEL_START, %esi
157 movl $no_long_mode_str - KERNEL_START, %esi
163 1: movb %al, (%edi, %ecx, 2)
164 movb $14, 1(%edi, %ecx, 2) // It's not at the cursor, so use
165 // yellow to make it stand out.
167 movb (%esi, %ecx), %al
174 .string "This CPU does not support long (64-bit) mode. Use a 32-bit kernel."
177 .string "Unrecognized bootloader; a multiboot-compliant loader is required."
181 .macro pushvolatilesnordi
209 // Non-volatile registers must be pushed if the handler will
210 // need to access all of the interrupted code's registers,
211 // such as when producing an error dump. Does not include
212 // r15, as that is usually swapped with the error code.
266 .global x64_invalid_insn
271 call x64_do_invalid_insn
278 xchgq %r15, (%rsp) // get error code
288 .global x64_page_fault
290 xchgq %r15, (%rsp) // get error code
296 call x64_do_page_fault
304 subq $8, %rsp // Keep the stack frame 16-byte aligned
307 movl need_resched, %eax
317 // The cli is to make sure interrupts don't get re-enabled in
318 // this thread context between the schedule and the ret from
351 .global x64_new_thread
367 call sched_new_thread