1 /* 2 * Parts copyright Michael Brown <mbrown@fensystems.co.uk> 3 * 4 * Copyright 2020 Joyent, Inc. 5 */ 6 7 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ) 8 9 /* CR0: protection enabled */ 10 #define CR0_PE ( 1 << 0 ) 11 12 /* CR0: paging */ 13 #define CR0_PG ( 1 << 31 ) 14 15 /* CR4: physical address extensions */ 16 #define CR4_PSE ( 1 << 4 ) 17 #define CR4_PAE ( 1 << 5 ) 18 #define CR4_PGE ( 1 << 7 ) 19 20 /* Extended feature enable MSR (EFER) */ 21 #define MSR_EFER 0xc0000080 22 23 /* EFER: long mode enable */ 24 #define EFER_LME ( 1 << 8 ) 25 26 #define GDTSEL_CODE 0x8 27 #define GDTSEL_DATA 0x10 28 29 #if defined(PXE_EFI) && defined(__x86_64__) 30 31 .section ".text", "ax", @progbits 32 33 .data 34 35 entry_gdt: 36 /* null entry */ 37 .word 0x0, 0x0 38 .byte 0x0, 0x0, 0x0, 0x0 39 40 /* 32 bit protected mode code segment */ 41 .word 0xffff, 0x0 42 .byte 0x0, 0x9f, 0xcf, 0x0 43 44 /* 32 bit protected mode data segment */ 45 .word 0xffff, 0x0 46 .byte 0x0, 0x93, 0xcf, 0x0 47 48 entry_gdt_end: 49 .equ entry_gdt_length, entry_gdt_end - entry_gdt 50 51 entry_gdtr: 52 .word entry_gdt_length - 1 53 entry_gdt_base: 54 .quad 0 55 56 /* 57 * %rdi -> struct mb2 * 58 * %rsi -> stack pointer to switch to 59 * %rdx -> &multiboot2_enter_kernel 60 */ 61 .code64 62 .globl multiboot2_bounce 63 64 multiboot2_bounce: 65 movq %rsi, %rsp 66 jmp *%rdx 67 68 /* 69 * %rdi -> multiboot2 magic 70 * %rsi -> multiboot info pointer 71 * %rdx -> entry address (32 bits) 72 * 73 * 74 * We need to transition from our 64-bit environment into the one 75 * defined by the multiboot2 spec, section 3.3. Namely, drop down to 76 * 32-bit protected mode with a basic GDT, paging disabled, interrupts 77 * off, and 78 * 79 * %eax -> multiboot2 magic 80 * %ebx -> multiboot info pointer (physical) 81 */ 82 .globl multiboot2_entry 83 84 multiboot2_entry: 85 cli 86 87 movq %rsi, %rbx /* mb2 infop */ 88 movq %rdx, %rsi /* entry address */ 89 90 /* Load the mb2-mandated code and data segments. */ 91 leaq entry_gdt_base(%rip), %rcx 92 leaq entry_gdt(%rip), %rax 93 movq %rax, (%rcx) 94 95 leaq entry_gdtr(%rip), %rax 96 lgdt (%rax) 97 98 /* Load our new %cs. */ 99 movq %rsp, %rax 100 pushq $GDTSEL_DATA 101 pushq %rax 102 pushf 103 pushq $GDTSEL_CODE 104 lea newcs(%rip), %rax 105 pushq %rax 106 iretq 107 108 109 .code32 110 newcs: 111 112 movw $GDTSEL_DATA, %ax 113 movw %ax, %ds 114 movw %ax, %es 115 movw %ax, %fs 116 movw %ax, %gs 117 movw %ax, %ss 118 119 /* Disable paging */ 120 movl %cr0, %eax 121 andl $~CR0_PG, %eax 122 movl %eax, %cr0 123 124 movl %cr4, %eax 125 andb $~(CR4_PAE | CR4_PGE | CR4_PSE), %al 126 movl %eax, %cr4 127 128 /* Disable long mode (clobbers %eax, %edx) */ 129 movl $MSR_EFER, %ecx 130 rdmsr 131 andw $~EFER_LME, %ax 132 wrmsr 133 134 /* %ebx still has our infop */ 135 movl %edi, %eax 136 jmp *%esi 137 138 #endif /* PXE_EFI && __x86_64__ */