Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/boot/head.S |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992, 1993 Linus Torvalds |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * head.S contains the 32-bit startup code. |
| 9 | * |
| 10 | * NOTE!!! Startup happens at absolute address 0x00001000, which is also where |
| 11 | * the page directory will exist. The startup code will be overwritten by |
| 12 | * the page directory. [According to comments etc elsewhere on a compressed |
| 13 | * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC] |
| 14 | * |
| 15 | * Page 0 is deliberately kept safe, since System Management Mode code in |
| 16 | * laptops may need to access the BIOS data stored there. This is also |
| 17 | * useful for future device drivers that either access the BIOS via VM86 |
| 18 | * mode. |
| 19 | */ |
| 20 | |
| 21 | /* |
Domen Puncer | f454944 | 2005-06-25 14:58:59 -0700 | [diff] [blame] | 22 | * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | */ |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 24 | .code32 |
| 25 | .text |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Tim Abbott | 1dc818c | 2009-09-16 16:44:27 -0400 | [diff] [blame] | 27 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/linkage.h> |
| 29 | #include <asm/segment.h> |
Alexander van Heukelum | 7c53976 | 2008-04-08 12:54:30 +0200 | [diff] [blame] | 30 | #include <asm/boot.h> |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 31 | #include <asm/msr.h> |
Cyrill Gorcunov | e83e31f4 | 2008-05-12 15:43:39 +0200 | [diff] [blame] | 32 | #include <asm/processor-flags.h> |
Eric W. Biederman | bd53147 | 2007-10-26 11:29:04 -0600 | [diff] [blame] | 33 | #include <asm/asm-offsets.h> |
Alexander Kuleshov | fb148d8 | 2015-02-19 13:34:58 +0600 | [diff] [blame] | 34 | #include <asm/bootparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Tim Abbott | 1dc818c | 2009-09-16 16:44:27 -0400 | [diff] [blame] | 36 | __HEAD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | .code32 |
Cyrill Gorcunov | 2d4eeecb | 2009-02-14 00:50:22 +0300 | [diff] [blame] | 38 | ENTRY(startup_32) |
Yinghai Lu | 8ee2f2d | 2013-01-24 12:20:07 -0800 | [diff] [blame] | 39 | /* |
| 40 | * 32bit entry is 0 and it is ABI so immutable! |
| 41 | * If we come here directly from a bootloader, |
| 42 | * kernel(text+data+bss+brk) ramdisk, zero_page, command line |
| 43 | * all need to be under the 4G limit. |
| 44 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | cld |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Test KEEP_SEGMENTS flag to see if the bootloader is asking |
| 48 | * us to not reload segments |
| 49 | */ |
Alexander Kuleshov | fb148d8 | 2015-02-19 13:34:58 +0600 | [diff] [blame] | 50 | testb $KEEP_SEGMENTS, BP_loadflags(%esi) |
Eric W. Biederman | bd53147 | 2007-10-26 11:29:04 -0600 | [diff] [blame] | 51 | jnz 1f |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | cli |
Lans Zhang | 2dead15 | 2013-03-01 09:20:39 +0800 | [diff] [blame] | 54 | movl $(__BOOT_DS), %eax |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 55 | movl %eax, %ds |
| 56 | movl %eax, %es |
| 57 | movl %eax, %ss |
Eric W. Biederman | bd53147 | 2007-10-26 11:29:04 -0600 | [diff] [blame] | 58 | 1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Calculate the delta between where we were compiled to run |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 62 | * at and where we were actually loaded at. This can only be done |
| 63 | * with a short local call on x86. Nothing else will tell us what |
| 64 | * address we are running at. The reserved chunk of the real-mode |
H. Peter Anvin | 85414b6 | 2007-07-11 12:18:33 -0700 | [diff] [blame] | 65 | * data at 0x1e4 (defined as a scratch field) are used as the stack |
| 66 | * for this calculation. Only 4 bytes are needed. |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 67 | */ |
H. Peter Anvin | bd2a369 | 2009-05-05 23:24:50 -0700 | [diff] [blame] | 68 | leal (BP_scratch+4)(%esi), %esp |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 69 | call 1f |
| 70 | 1: popl %ebp |
| 71 | subl $1b, %ebp |
| 72 | |
Vivek Goyal | a4831e0 | 2007-05-02 19:27:08 +0200 | [diff] [blame] | 73 | /* setup a stack and make sure cpu supports long mode. */ |
Alexander van Heukelum | 7c53976 | 2008-04-08 12:54:30 +0200 | [diff] [blame] | 74 | movl $boot_stack_end, %eax |
Vivek Goyal | a4831e0 | 2007-05-02 19:27:08 +0200 | [diff] [blame] | 75 | addl %ebp, %eax |
| 76 | movl %eax, %esp |
| 77 | |
| 78 | call verify_cpu |
| 79 | testl %eax, %eax |
| 80 | jnz no_longmode |
| 81 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Compute the delta between where we were compiled to run at |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 84 | * and where the code will actually run at. |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 85 | * |
| 86 | * %ebp contains the address we are loaded at by the boot loader and %ebx |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 87 | * contains the address where we should move the kernel image temporarily |
| 88 | * for safe in-place decompression. |
| 89 | */ |
| 90 | |
| 91 | #ifdef CONFIG_RELOCATABLE |
| 92 | movl %ebp, %ebx |
H. Peter Anvin | 37ba7ab | 2009-05-11 15:56:08 -0700 | [diff] [blame] | 93 | movl BP_kernel_alignment(%esi), %eax |
| 94 | decl %eax |
| 95 | addl %eax, %ebx |
| 96 | notl %eax |
| 97 | andl %eax, %ebx |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 98 | cmpl $LOAD_PHYSICAL_ADDR, %ebx |
| 99 | jge 1f |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 100 | #endif |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 101 | movl $LOAD_PHYSICAL_ADDR, %ebx |
| 102 | 1: |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 103 | |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 104 | /* Target address to relocate to for decompression */ |
| 105 | addl $z_extract_offset, %ebx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 108 | * Prepare for entering 64 bit mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | */ |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 110 | |
| 111 | /* Load new GDT with the 64bit segments using 32bit descriptor */ |
| 112 | leal gdt(%ebp), %eax |
| 113 | movl %eax, gdt+2(%ebp) |
| 114 | lgdt gdt(%ebp) |
| 115 | |
| 116 | /* Enable PAE mode */ |
Matt Fleming | 108d3f4 | 2014-02-24 13:37:29 +0000 | [diff] [blame] | 117 | movl %cr4, %eax |
| 118 | orl $X86_CR4_PAE, %eax |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 119 | movl %eax, %cr4 |
| 120 | |
| 121 | /* |
| 122 | * Build early 4G boot pagetable |
| 123 | */ |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 124 | /* Initialize Page tables to 0 */ |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 125 | leal pgtable(%ebx), %edi |
| 126 | xorl %eax, %eax |
| 127 | movl $((4096*6)/4), %ecx |
| 128 | rep stosl |
| 129 | |
| 130 | /* Build Level 4 */ |
| 131 | leal pgtable + 0(%ebx), %edi |
| 132 | leal 0x1007 (%edi), %eax |
| 133 | movl %eax, 0(%edi) |
| 134 | |
| 135 | /* Build Level 3 */ |
| 136 | leal pgtable + 0x1000(%ebx), %edi |
| 137 | leal 0x1007(%edi), %eax |
| 138 | movl $4, %ecx |
| 139 | 1: movl %eax, 0x00(%edi) |
| 140 | addl $0x00001000, %eax |
| 141 | addl $8, %edi |
| 142 | decl %ecx |
| 143 | jnz 1b |
| 144 | |
| 145 | /* Build Level 2 */ |
| 146 | leal pgtable + 0x2000(%ebx), %edi |
| 147 | movl $0x00000183, %eax |
| 148 | movl $2048, %ecx |
| 149 | 1: movl %eax, 0(%edi) |
| 150 | addl $0x00200000, %eax |
| 151 | addl $8, %edi |
| 152 | decl %ecx |
| 153 | jnz 1b |
| 154 | |
| 155 | /* Enable the boot page tables */ |
| 156 | leal pgtable(%ebx), %eax |
| 157 | movl %eax, %cr3 |
| 158 | |
| 159 | /* Enable Long mode in EFER (Extended Feature Enable Register) */ |
| 160 | movl $MSR_EFER, %ecx |
| 161 | rdmsr |
| 162 | btsl $_EFER_LME, %eax |
| 163 | wrmsr |
| 164 | |
Yinghai Lu | d3c433b | 2013-01-24 12:20:01 -0800 | [diff] [blame] | 165 | /* After gdt is loaded */ |
| 166 | xorl %eax, %eax |
| 167 | lldt %ax |
Denys Vlasenko | 40e4f2d1 | 2015-04-01 16:50:58 +0200 | [diff] [blame^] | 168 | movl $__BOOT_TSS, %eax |
Yinghai Lu | d3c433b | 2013-01-24 12:20:01 -0800 | [diff] [blame] | 169 | ltr %ax |
| 170 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Setup for the jump to 64bit mode |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 173 | * |
| 174 | * When the jump is performend we will be in long mode but |
| 175 | * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1 |
| 176 | * (and in turn EFER.LMA = 1). To jump into 64bit mode we use |
| 177 | * the new gdt/idt that has __KERNEL_CS with CS.L = 1. |
| 178 | * We place all of the values on our mini stack so lret can |
| 179 | * used to perform that far jump. |
| 180 | */ |
| 181 | pushl $__KERNEL_CS |
| 182 | leal startup_64(%ebp), %eax |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 183 | #ifdef CONFIG_EFI_MIXED |
| 184 | movl efi32_config(%ebp), %ebx |
| 185 | cmp $0, %ebx |
| 186 | jz 1f |
| 187 | leal handover_entry(%ebp), %eax |
| 188 | 1: |
| 189 | #endif |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 190 | pushl %eax |
| 191 | |
| 192 | /* Enter paged protected Mode, activating Long Mode */ |
Cyrill Gorcunov | e83e31f4 | 2008-05-12 15:43:39 +0200 | [diff] [blame] | 193 | movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */ |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 194 | movl %eax, %cr0 |
| 195 | |
| 196 | /* Jump from 32bit compatibility mode into 64bit mode. */ |
| 197 | lret |
Cyrill Gorcunov | 2d4eeecb | 2009-02-14 00:50:22 +0300 | [diff] [blame] | 198 | ENDPROC(startup_32) |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 199 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 200 | #ifdef CONFIG_EFI_MIXED |
| 201 | .org 0x190 |
| 202 | ENTRY(efi32_stub_entry) |
| 203 | add $0x4, %esp /* Discard return address */ |
| 204 | popl %ecx |
| 205 | popl %edx |
| 206 | popl %esi |
| 207 | |
| 208 | leal (BP_scratch+4)(%esi), %esp |
| 209 | call 1f |
| 210 | 1: pop %ebp |
| 211 | subl $1b, %ebp |
| 212 | |
| 213 | movl %ecx, efi32_config(%ebp) |
| 214 | movl %edx, efi32_config+8(%ebp) |
| 215 | sgdtl efi32_boot_gdt(%ebp) |
| 216 | |
| 217 | leal efi32_config(%ebp), %eax |
| 218 | movl %eax, efi_config(%ebp) |
| 219 | |
| 220 | jmp startup_32 |
| 221 | ENDPROC(efi32_stub_entry) |
| 222 | #endif |
| 223 | |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 224 | .code64 |
Vivek Goyal | a4831e0 | 2007-05-02 19:27:08 +0200 | [diff] [blame] | 225 | .org 0x200 |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 226 | ENTRY(startup_64) |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 227 | /* |
Yinghai Lu | 8ee2f2d | 2013-01-24 12:20:07 -0800 | [diff] [blame] | 228 | * 64bit entry is 0x200 and it is ABI so immutable! |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 229 | * We come here either from startup_32 or directly from a |
Yinghai Lu | 8ee2f2d | 2013-01-24 12:20:07 -0800 | [diff] [blame] | 230 | * 64bit bootloader. |
| 231 | * If we come here from a bootloader, kernel(text+data+bss+brk), |
| 232 | * ramdisk, zero_page, command line could be above 4G. |
| 233 | * We depend on an identity mapped page table being provided |
| 234 | * that maps our entire kernel(text+data+bss+brk), zero page |
| 235 | * and command line. |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 236 | */ |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 237 | #ifdef CONFIG_EFI_STUB |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 238 | /* |
David Woodhouse | 99f857d | 2013-01-10 14:31:59 +0000 | [diff] [blame] | 239 | * The entry point for the PE/COFF executable is efi_pe_entry, so |
| 240 | * only legacy boot loaders will execute this jmp. |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 241 | */ |
| 242 | jmp preferred_addr |
| 243 | |
David Woodhouse | 99f857d | 2013-01-10 14:31:59 +0000 | [diff] [blame] | 244 | ENTRY(efi_pe_entry) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 245 | movq %rcx, efi64_config(%rip) /* Handle */ |
| 246 | movq %rdx, efi64_config+8(%rip) /* EFI System table pointer */ |
| 247 | |
| 248 | leaq efi64_config(%rip), %rax |
| 249 | movq %rax, efi_config(%rip) |
| 250 | |
| 251 | call 1f |
| 252 | 1: popq %rbp |
| 253 | subq $1b, %rbp |
| 254 | |
| 255 | /* |
Linus Torvalds | f367039 | 2014-09-22 23:05:49 -0700 | [diff] [blame] | 256 | * Relocate efi_config->call(). |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 257 | */ |
| 258 | addq %rbp, efi64_config+88(%rip) |
| 259 | |
| 260 | movq %rax, %rdi |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 261 | call make_boot_params |
| 262 | cmpq $0,%rax |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 263 | je fail |
| 264 | mov %rax, %rsi |
Matt Fleming | 7e8213c | 2014-04-08 13:14:00 +0100 | [diff] [blame] | 265 | leaq startup_32(%rip), %rax |
| 266 | movl %eax, BP_code32_start(%rsi) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 267 | jmp 2f /* Skip the relocation */ |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 268 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 269 | handover_entry: |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 270 | call 1f |
| 271 | 1: popq %rbp |
| 272 | subq $1b, %rbp |
| 273 | |
| 274 | /* |
Linus Torvalds | f367039 | 2014-09-22 23:05:49 -0700 | [diff] [blame] | 275 | * Relocate efi_config->call(). |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 276 | */ |
| 277 | movq efi_config(%rip), %rax |
| 278 | addq %rbp, 88(%rax) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 279 | 2: |
| 280 | movq efi_config(%rip), %rdi |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 281 | call efi_main |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 282 | movq %rax,%rsi |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 283 | cmpq $0,%rax |
| 284 | jne 2f |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 285 | fail: |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 286 | /* EFI init failed, so hang. */ |
| 287 | hlt |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 288 | jmp fail |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 289 | 2: |
Matt Fleming | 7e8213c | 2014-04-08 13:14:00 +0100 | [diff] [blame] | 290 | movl BP_code32_start(%esi), %eax |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 291 | leaq preferred_addr(%rax), %rax |
| 292 | jmp *%rax |
| 293 | |
| 294 | preferred_addr: |
| 295 | #endif |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 296 | |
| 297 | /* Setup data segments. */ |
| 298 | xorl %eax, %eax |
| 299 | movl %eax, %ds |
| 300 | movl %eax, %es |
| 301 | movl %eax, %ss |
Zachary Amsden | 08da5a2 | 2007-08-10 22:31:05 +0200 | [diff] [blame] | 302 | movl %eax, %fs |
| 303 | movl %eax, %gs |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 304 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 305 | /* |
| 306 | * Compute the decompressed kernel start address. It is where |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 307 | * we were loaded at aligned to a 2M boundary. %rbp contains the |
| 308 | * decompressed kernel start address. |
| 309 | * |
| 310 | * If it is a relocatable kernel then decompress and run the kernel |
| 311 | * from load address aligned to 2MB addr, otherwise decompress and |
H. Peter Anvin | 40b387a | 2009-05-11 14:41:55 -0700 | [diff] [blame] | 312 | * run the kernel from LOAD_PHYSICAL_ADDR |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 313 | * |
| 314 | * We cannot rely on the calculation done in 32-bit mode, since we |
| 315 | * may have been invoked via the 64-bit entry point. |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 316 | */ |
| 317 | |
| 318 | /* Start with the delta to where the kernel will run at. */ |
| 319 | #ifdef CONFIG_RELOCATABLE |
| 320 | leaq startup_32(%rip) /* - $startup_32 */, %rbp |
H. Peter Anvin | 37ba7ab | 2009-05-11 15:56:08 -0700 | [diff] [blame] | 321 | movl BP_kernel_alignment(%rsi), %eax |
| 322 | decl %eax |
| 323 | addq %rax, %rbp |
| 324 | notq %rax |
| 325 | andq %rax, %rbp |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 326 | cmpq $LOAD_PHYSICAL_ADDR, %rbp |
| 327 | jge 1f |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 328 | #endif |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 329 | movq $LOAD_PHYSICAL_ADDR, %rbp |
| 330 | 1: |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 331 | |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 332 | /* Target address to relocate to for decompression */ |
| 333 | leaq z_extract_offset(%rbp), %rbx |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 334 | |
H. Peter Anvin | 0a13773 | 2009-05-08 16:27:41 -0700 | [diff] [blame] | 335 | /* Set up the stack */ |
| 336 | leaq boot_stack_end(%rbx), %rsp |
| 337 | |
| 338 | /* Zero EFLAGS */ |
| 339 | pushq $0 |
| 340 | popfq |
| 341 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 342 | /* |
| 343 | * Copy the compressed kernel to the end of our buffer |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 344 | * where decompression in place becomes safe. |
| 345 | */ |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 346 | pushq %rsi |
| 347 | leaq (_bss-8)(%rip), %rsi |
| 348 | leaq (_bss-8)(%rbx), %rdi |
H. Peter Anvin | 5b11f1c | 2009-05-08 16:20:34 -0700 | [diff] [blame] | 349 | movq $_bss /* - $startup_32 */, %rcx |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 350 | shrq $3, %rcx |
| 351 | std |
| 352 | rep movsq |
| 353 | cld |
| 354 | popq %rsi |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 355 | |
| 356 | /* |
| 357 | * Jump to the relocated address. |
| 358 | */ |
| 359 | leaq relocated(%rbx), %rax |
| 360 | jmp *%rax |
| 361 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 362 | #ifdef CONFIG_EFI_STUB |
| 363 | .org 0x390 |
| 364 | ENTRY(efi64_stub_entry) |
| 365 | movq %rdi, efi64_config(%rip) /* Handle */ |
| 366 | movq %rsi, efi64_config+8(%rip) /* EFI System table pointer */ |
| 367 | |
| 368 | leaq efi64_config(%rip), %rax |
| 369 | movq %rax, efi_config(%rip) |
| 370 | |
| 371 | movq %rdx, %rsi |
| 372 | jmp handover_entry |
| 373 | ENDPROC(efi64_stub_entry) |
| 374 | #endif |
| 375 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 376 | .text |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 377 | relocated: |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | /* |
H. Peter Anvin | 0a13773 | 2009-05-08 16:27:41 -0700 | [diff] [blame] | 380 | * Clear BSS (stack is currently empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | */ |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 382 | xorl %eax, %eax |
| 383 | leaq _bss(%rip), %rdi |
| 384 | leaq _ebss(%rip), %rcx |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 385 | subq %rdi, %rcx |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 386 | shrq $3, %rcx |
| 387 | rep stosq |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 388 | |
Linus Torvalds | f367039 | 2014-09-22 23:05:49 -0700 | [diff] [blame] | 389 | /* |
| 390 | * Adjust our own GOT |
| 391 | */ |
| 392 | leaq _got(%rip), %rdx |
| 393 | leaq _egot(%rip), %rcx |
| 394 | 1: |
| 395 | cmpq %rcx, %rdx |
| 396 | jae 2f |
| 397 | addq %rbx, (%rdx) |
| 398 | addq $8, %rdx |
| 399 | jmp 1b |
| 400 | 2: |
| 401 | |
H. Peter Anvin | 22a57f5 | 2010-08-02 15:34:44 -0700 | [diff] [blame] | 402 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * Do the decompression, and jump to the new kernel.. |
| 404 | */ |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 405 | pushq %rsi /* Save the real mode argument */ |
Junjie Mao | e602336 | 2014-10-31 21:40:38 +0800 | [diff] [blame] | 406 | movq $z_run_size, %r9 /* size of kernel with .bss and .brk */ |
| 407 | pushq %r9 |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 408 | movq %rsi, %rdi /* real mode address */ |
| 409 | leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ |
| 410 | leaq input_data(%rip), %rdx /* input_data */ |
| 411 | movl $z_input_len, %ecx /* input_len */ |
| 412 | movq %rbp, %r8 /* output target address */ |
Junjie Mao | e602336 | 2014-10-31 21:40:38 +0800 | [diff] [blame] | 413 | movq $z_output_len, %r9 /* decompressed length, end of relocs */ |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 414 | call decompress_kernel /* returns kernel location in %rax */ |
Junjie Mao | e602336 | 2014-10-31 21:40:38 +0800 | [diff] [blame] | 415 | popq %r9 |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 416 | popq %rsi |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 419 | * Jump to the decompressed kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | */ |
Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 421 | jmp *%rax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Yinghai Lu | 187a8a7 | 2013-01-24 12:20:00 -0800 | [diff] [blame] | 423 | .code32 |
| 424 | no_longmode: |
| 425 | /* This isn't an x86-64 CPU so hang */ |
| 426 | 1: |
| 427 | hlt |
| 428 | jmp 1b |
| 429 | |
| 430 | #include "../../kernel/verify_cpu.S" |
| 431 | |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 432 | .data |
| 433 | gdt: |
| 434 | .word gdt_end - gdt |
| 435 | .long gdt |
| 436 | .word 0 |
| 437 | .quad 0x0000000000000000 /* NULL descriptor */ |
| 438 | .quad 0x00af9a000000ffff /* __KERNEL_CS */ |
| 439 | .quad 0x00cf92000000ffff /* __KERNEL_DS */ |
Zachary Amsden | 08da5a2 | 2007-08-10 22:31:05 +0200 | [diff] [blame] | 440 | .quad 0x0080890000000000 /* TS descriptor */ |
| 441 | .quad 0x0000000000000000 /* TS continued */ |
Vivek Goyal | 1ab60e0 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 442 | gdt_end: |
Alexander van Heukelum | 7c53976 | 2008-04-08 12:54:30 +0200 | [diff] [blame] | 443 | |
Matt Fleming | 3db4caf | 2014-03-05 10:15:55 +0000 | [diff] [blame] | 444 | #ifdef CONFIG_EFI_STUB |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 445 | efi_config: |
| 446 | .quad 0 |
| 447 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 448 | #ifdef CONFIG_EFI_MIXED |
| 449 | .global efi32_config |
| 450 | efi32_config: |
| 451 | .fill 11,8,0 |
| 452 | .quad efi64_thunk |
| 453 | .byte 0 |
| 454 | #endif |
| 455 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 456 | .global efi64_config |
| 457 | efi64_config: |
| 458 | .fill 11,8,0 |
Matt Fleming | 62fa6e6 | 2014-03-27 15:10:39 -0700 | [diff] [blame] | 459 | .quad efi_call |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 460 | .byte 1 |
Matt Fleming | 3db4caf | 2014-03-05 10:15:55 +0000 | [diff] [blame] | 461 | #endif /* CONFIG_EFI_STUB */ |
| 462 | |
H. Peter Anvin | b40d68d | 2009-05-08 15:59:13 -0700 | [diff] [blame] | 463 | /* |
| 464 | * Stack and heap for uncompression |
| 465 | */ |
| 466 | .bss |
| 467 | .balign 4 |
Alexander van Heukelum | 7c53976 | 2008-04-08 12:54:30 +0200 | [diff] [blame] | 468 | boot_heap: |
| 469 | .fill BOOT_HEAP_SIZE, 1, 0 |
| 470 | boot_stack: |
| 471 | .fill BOOT_STACK_SIZE, 1, 0 |
| 472 | boot_stack_end: |
H. Peter Anvin | 5b11f1c | 2009-05-08 16:20:34 -0700 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * Space for page tables (not in .bss so not zeroed) |
| 476 | */ |
| 477 | .section ".pgtable","a",@nobits |
| 478 | .balign 4096 |
| 479 | pgtable: |
| 480 | .fill 6*4096, 1, 0 |