GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / x86 / boot / compressed / head_64.S
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  linux/boot/head.S
4  *
5  *  Copyright (C) 1991, 1992, 1993  Linus Torvalds
6  */
7
8 /*
9  *  head.S contains the 32-bit startup code.
10  *
11  * NOTE!!! Startup happens at absolute address 0x00001000, which is also where
12  * the page directory will exist. The startup code will be overwritten by
13  * the page directory. [According to comments etc elsewhere on a compressed
14  * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC]
15  *
16  * Page 0 is deliberately kept safe, since System Management Mode code in 
17  * laptops may need to access the BIOS data stored there.  This is also
18  * useful for future device drivers that either access the BIOS via VM86 
19  * mode.
20  */
21
22 /*
23  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
24  */
25         .code32
26         .text
27
28 #include <linux/init.h>
29 #include <linux/linkage.h>
30 #include <asm/segment.h>
31 #include <asm/boot.h>
32 #include <asm/msr.h>
33 #include <asm/processor-flags.h>
34 #include <asm/asm-offsets.h>
35 #include <asm/bootparam.h>
36
37 /*
38  * Locally defined symbols should be marked hidden:
39  */
40         .hidden _bss
41         .hidden _ebss
42         .hidden _got
43         .hidden _egot
44         .hidden _end
45
46         __HEAD
47         .code32
48 ENTRY(startup_32)
49         /*
50          * 32bit entry is 0 and it is ABI so immutable!
51          * If we come here directly from a bootloader,
52          * kernel(text+data+bss+brk) ramdisk, zero_page, command line
53          * all need to be under the 4G limit.
54          */
55         cld
56         /*
57          * Test KEEP_SEGMENTS flag to see if the bootloader is asking
58          * us to not reload segments
59          */
60         testb $KEEP_SEGMENTS, BP_loadflags(%esi)
61         jnz 1f
62
63         cli
64         movl    $(__BOOT_DS), %eax
65         movl    %eax, %ds
66         movl    %eax, %es
67         movl    %eax, %ss
68 1:
69
70 /*
71  * Calculate the delta between where we were compiled to run
72  * at and where we were actually loaded at.  This can only be done
73  * with a short local call on x86.  Nothing  else will tell us what
74  * address we are running at.  The reserved chunk of the real-mode
75  * data at 0x1e4 (defined as a scratch field) are used as the stack
76  * for this calculation. Only 4 bytes are needed.
77  */
78         leal    (BP_scratch+4)(%esi), %esp
79         call    1f
80 1:      popl    %ebp
81         subl    $1b, %ebp
82
83 /* setup a stack and make sure cpu supports long mode. */
84         movl    $boot_stack_end, %eax
85         addl    %ebp, %eax
86         movl    %eax, %esp
87
88         call    verify_cpu
89         testl   %eax, %eax
90         jnz     no_longmode
91
92 /*
93  * Compute the delta between where we were compiled to run at
94  * and where the code will actually run at.
95  *
96  * %ebp contains the address we are loaded at by the boot loader and %ebx
97  * contains the address where we should move the kernel image temporarily
98  * for safe in-place decompression.
99  */
100
101 #ifdef CONFIG_RELOCATABLE
102         movl    %ebp, %ebx
103         movl    BP_kernel_alignment(%esi), %eax
104         decl    %eax
105         addl    %eax, %ebx
106         notl    %eax
107         andl    %eax, %ebx
108         cmpl    $LOAD_PHYSICAL_ADDR, %ebx
109         jae     1f
110 #endif
111         movl    $LOAD_PHYSICAL_ADDR, %ebx
112 1:
113
114         /* Target address to relocate to for decompression */
115         movl    BP_init_size(%esi), %eax
116         subl    $_end, %eax
117         addl    %eax, %ebx
118
119 /*
120  * Prepare for entering 64 bit mode
121  */
122
123         /* Load new GDT with the 64bit segments using 32bit descriptor */
124         addl    %ebp, gdt+2(%ebp)
125         lgdt    gdt(%ebp)
126
127         /* Enable PAE mode */
128         movl    %cr4, %eax
129         orl     $X86_CR4_PAE, %eax
130         movl    %eax, %cr4
131
132  /*
133   * Build early 4G boot pagetable
134   */
135         /* Initialize Page tables to 0 */
136         leal    pgtable(%ebx), %edi
137         xorl    %eax, %eax
138         movl    $(BOOT_INIT_PGT_SIZE/4), %ecx
139         rep     stosl
140
141         /* Build Level 4 */
142         leal    pgtable + 0(%ebx), %edi
143         leal    0x1007 (%edi), %eax
144         movl    %eax, 0(%edi)
145
146         /* Build Level 3 */
147         leal    pgtable + 0x1000(%ebx), %edi
148         leal    0x1007(%edi), %eax
149         movl    $4, %ecx
150 1:      movl    %eax, 0x00(%edi)
151         addl    $0x00001000, %eax
152         addl    $8, %edi
153         decl    %ecx
154         jnz     1b
155
156         /* Build Level 2 */
157         leal    pgtable + 0x2000(%ebx), %edi
158         movl    $0x00000183, %eax
159         movl    $2048, %ecx
160 1:      movl    %eax, 0(%edi)
161         addl    $0x00200000, %eax
162         addl    $8, %edi
163         decl    %ecx
164         jnz     1b
165
166         /* Enable the boot page tables */
167         leal    pgtable(%ebx), %eax
168         movl    %eax, %cr3
169
170         /* Enable Long mode in EFER (Extended Feature Enable Register) */
171         movl    $MSR_EFER, %ecx
172         rdmsr
173         btsl    $_EFER_LME, %eax
174         wrmsr
175
176         /* After gdt is loaded */
177         xorl    %eax, %eax
178         lldt    %ax
179         movl    $__BOOT_TSS, %eax
180         ltr     %ax
181
182         /*
183          * Setup for the jump to 64bit mode
184          *
185          * When the jump is performend we will be in long mode but
186          * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1
187          * (and in turn EFER.LMA = 1).  To jump into 64bit mode we use
188          * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
189          * We place all of the values on our mini stack so lret can
190          * used to perform that far jump.
191          */
192         pushl   $__KERNEL_CS
193         leal    startup_64(%ebp), %eax
194 #ifdef CONFIG_EFI_MIXED
195         movl    efi32_config(%ebp), %ebx
196         cmp     $0, %ebx
197         jz      1f
198         leal    handover_entry(%ebp), %eax
199 1:
200 #endif
201         pushl   %eax
202
203         /* Enter paged protected Mode, activating Long Mode */
204         movl    $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */
205         movl    %eax, %cr0
206
207         /* Jump from 32bit compatibility mode into 64bit mode. */
208         lret
209 ENDPROC(startup_32)
210
211 #ifdef CONFIG_EFI_MIXED
212         .org 0x190
213 ENTRY(efi32_stub_entry)
214         add     $0x4, %esp              /* Discard return address */
215         popl    %ecx
216         popl    %edx
217         popl    %esi
218
219         leal    (BP_scratch+4)(%esi), %esp
220         call    1f
221 1:      pop     %ebp
222         subl    $1b, %ebp
223
224         movl    %ecx, efi32_config(%ebp)
225         movl    %edx, efi32_config+8(%ebp)
226         sgdtl   efi32_boot_gdt(%ebp)
227
228         leal    efi32_config(%ebp), %eax
229         movl    %eax, efi_config(%ebp)
230
231         /* Disable paging */
232         movl    %cr0, %eax
233         btrl    $X86_CR0_PG_BIT, %eax
234         movl    %eax, %cr0
235
236         jmp     startup_32
237 ENDPROC(efi32_stub_entry)
238 #endif
239
240         .code64
241         .org 0x200
242 ENTRY(startup_64)
243         /*
244          * 64bit entry is 0x200 and it is ABI so immutable!
245          * We come here either from startup_32 or directly from a
246          * 64bit bootloader.
247          * If we come here from a bootloader, kernel(text+data+bss+brk),
248          * ramdisk, zero_page, command line could be above 4G.
249          * We depend on an identity mapped page table being provided
250          * that maps our entire kernel(text+data+bss+brk), zero page
251          * and command line.
252          */
253
254         /* Setup data segments. */
255         xorl    %eax, %eax
256         movl    %eax, %ds
257         movl    %eax, %es
258         movl    %eax, %ss
259         movl    %eax, %fs
260         movl    %eax, %gs
261
262         /*
263          * Compute the decompressed kernel start address.  It is where
264          * we were loaded at aligned to a 2M boundary. %rbp contains the
265          * decompressed kernel start address.
266          *
267          * If it is a relocatable kernel then decompress and run the kernel
268          * from load address aligned to 2MB addr, otherwise decompress and
269          * run the kernel from LOAD_PHYSICAL_ADDR
270          *
271          * We cannot rely on the calculation done in 32-bit mode, since we
272          * may have been invoked via the 64-bit entry point.
273          */
274
275         /* Start with the delta to where the kernel will run at. */
276 #ifdef CONFIG_RELOCATABLE
277         leaq    startup_32(%rip) /* - $startup_32 */, %rbp
278         movl    BP_kernel_alignment(%rsi), %eax
279         decl    %eax
280         addq    %rax, %rbp
281         notq    %rax
282         andq    %rax, %rbp
283         cmpq    $LOAD_PHYSICAL_ADDR, %rbp
284         jae     1f
285 #endif
286         movq    $LOAD_PHYSICAL_ADDR, %rbp
287 1:
288
289         /* Target address to relocate to for decompression */
290         movl    BP_init_size(%rsi), %ebx
291         subl    $_end, %ebx
292         addq    %rbp, %rbx
293
294         /* Set up the stack */
295         leaq    boot_stack_end(%rbx), %rsp
296
297 #ifdef CONFIG_X86_5LEVEL
298         /*
299          * Check if we need to enable 5-level paging.
300          * RSI holds real mode data and need to be preserved across
301          * a function call.
302          */
303         pushq   %rsi
304         call    l5_paging_required
305         popq    %rsi
306
307         /* If l5_paging_required() returned zero, we're done here. */
308         cmpq    $0, %rax
309         je      lvl5
310
311         /*
312          * At this point we are in long mode with 4-level paging enabled,
313          * but we want to enable 5-level paging.
314          *
315          * The problem is that we cannot do it directly. Setting LA57 in
316          * long mode would trigger #GP. So we need to switch off long mode
317          * first.
318          *
319          * NOTE: This is not going to work if bootloader put us above 4G
320          * limit.
321          *
322          * The first step is go into compatibility mode.
323          */
324
325         /* Clear additional page table */
326         leaq    lvl5_pgtable(%rbx), %rdi
327         xorq    %rax, %rax
328         movq    $(PAGE_SIZE/8), %rcx
329         rep     stosq
330
331         /*
332          * Setup current CR3 as the first and only entry in a new top level
333          * page table.
334          */
335         movq    %cr3, %rdi
336         leaq    0x7 (%rdi), %rax
337         movq    %rax, lvl5_pgtable(%rbx)
338
339         /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */
340         pushq   $__KERNEL32_CS
341         leaq    compatible_mode(%rip), %rax
342         pushq   %rax
343         lretq
344 lvl5:
345 #endif
346
347         /* Zero EFLAGS */
348         pushq   $0
349         popfq
350
351 /*
352  * Copy the compressed kernel to the end of our buffer
353  * where decompression in place becomes safe.
354  */
355         pushq   %rsi
356         leaq    (_bss-8)(%rip), %rsi
357         leaq    (_bss-8)(%rbx), %rdi
358         movq    $_bss /* - $startup_32 */, %rcx
359         shrq    $3, %rcx
360         std
361         rep     movsq
362         cld
363         popq    %rsi
364
365 /*
366  * Jump to the relocated address.
367  */
368         leaq    relocated(%rbx), %rax
369         jmp     *%rax
370
371 #ifdef CONFIG_EFI_STUB
372
373 /* The entry point for the PE/COFF executable is efi_pe_entry. */
374 ENTRY(efi_pe_entry)
375         movq    %rcx, efi64_config(%rip)        /* Handle */
376         movq    %rdx, efi64_config+8(%rip) /* EFI System table pointer */
377
378         leaq    efi64_config(%rip), %rax
379         movq    %rax, efi_config(%rip)
380
381         call    1f
382 1:      popq    %rbp
383         subq    $1b, %rbp
384
385         /*
386          * Relocate efi_config->call().
387          */
388         addq    %rbp, efi64_config+40(%rip)
389
390         movq    %rax, %rdi
391         call    make_boot_params
392         cmpq    $0,%rax
393         je      fail
394         mov     %rax, %rsi
395         leaq    startup_32(%rip), %rax
396         movl    %eax, BP_code32_start(%rsi)
397         jmp     2f              /* Skip the relocation */
398
399 handover_entry:
400         call    1f
401 1:      popq    %rbp
402         subq    $1b, %rbp
403
404         /*
405          * Relocate efi_config->call().
406          */
407         movq    efi_config(%rip), %rax
408         addq    %rbp, 40(%rax)
409 2:
410         movq    efi_config(%rip), %rdi
411         call    efi_main
412         movq    %rax,%rsi
413         cmpq    $0,%rax
414         jne     2f
415 fail:
416         /* EFI init failed, so hang. */
417         hlt
418         jmp     fail
419 2:
420         movl    BP_code32_start(%esi), %eax
421         leaq    startup_64(%rax), %rax
422         jmp     *%rax
423 ENDPROC(efi_pe_entry)
424
425         .org 0x390
426 ENTRY(efi64_stub_entry)
427         movq    %rdi, efi64_config(%rip)        /* Handle */
428         movq    %rsi, efi64_config+8(%rip) /* EFI System table pointer */
429
430         leaq    efi64_config(%rip), %rax
431         movq    %rax, efi_config(%rip)
432
433         movq    %rdx, %rsi
434         jmp     handover_entry
435 ENDPROC(efi64_stub_entry)
436 #endif
437
438         .text
439 relocated:
440
441 /*
442  * Clear BSS (stack is currently empty)
443  */
444         xorl    %eax, %eax
445         leaq    _bss(%rip), %rdi
446         leaq    _ebss(%rip), %rcx
447         subq    %rdi, %rcx
448         shrq    $3, %rcx
449         rep     stosq
450
451 /*
452  * Adjust our own GOT
453  */
454         leaq    _got(%rip), %rdx
455         leaq    _egot(%rip), %rcx
456 1:
457         cmpq    %rcx, %rdx
458         jae     2f
459         addq    %rbx, (%rdx)
460         addq    $8, %rdx
461         jmp     1b
462 2:
463         
464 /*
465  * Do the extraction, and jump to the new kernel..
466  */
467         pushq   %rsi                    /* Save the real mode argument */
468         movq    %rsi, %rdi              /* real mode address */
469         leaq    boot_heap(%rip), %rsi   /* malloc area for uncompression */
470         leaq    input_data(%rip), %rdx  /* input_data */
471         movl    $z_input_len, %ecx      /* input_len */
472         movq    %rbp, %r8               /* output target address */
473         movq    $z_output_len, %r9      /* decompressed length, end of relocs */
474         call    extract_kernel          /* returns kernel location in %rax */
475         popq    %rsi
476
477 /*
478  * Jump to the decompressed kernel.
479  */
480         jmp     *%rax
481
482         .code32
483 #ifdef CONFIG_X86_5LEVEL
484 compatible_mode:
485         /* Setup data and stack segments */
486         movl    $__KERNEL_DS, %eax
487         movl    %eax, %ds
488         movl    %eax, %ss
489
490         /* Disable paging */
491         movl    %cr0, %eax
492         btrl    $X86_CR0_PG_BIT, %eax
493         movl    %eax, %cr0
494
495         /* Point CR3 to 5-level paging */
496         leal    lvl5_pgtable(%ebx), %eax
497         movl    %eax, %cr3
498
499         /* Enable PAE and LA57 mode */
500         movl    %cr4, %eax
501         orl     $(X86_CR4_PAE | X86_CR4_LA57), %eax
502         movl    %eax, %cr4
503
504         /* Calculate address we are running at */
505         call    1f
506 1:      popl    %edi
507         subl    $1b, %edi
508
509         /* Prepare stack for far return to Long Mode */
510         pushl   $__KERNEL_CS
511         leal    lvl5(%edi), %eax
512         push    %eax
513
514         /* Enable paging back */
515         movl    $(X86_CR0_PG | X86_CR0_PE), %eax
516         movl    %eax, %cr0
517
518         lret
519 #endif
520
521 no_longmode:
522         /* This isn't an x86-64 CPU so hang */
523 1:
524         hlt
525         jmp     1b
526
527 #include "../../kernel/verify_cpu.S"
528
529         .data
530 gdt:
531         .word   gdt_end - gdt
532         .long   gdt
533         .word   0
534         .quad   0x00cf9a000000ffff      /* __KERNEL32_CS */
535         .quad   0x00af9a000000ffff      /* __KERNEL_CS */
536         .quad   0x00cf92000000ffff      /* __KERNEL_DS */
537         .quad   0x0080890000000000      /* TS descriptor */
538         .quad   0x0000000000000000      /* TS continued */
539 gdt_end:
540
541 #ifdef CONFIG_EFI_STUB
542 efi_config:
543         .quad   0
544
545 #ifdef CONFIG_EFI_MIXED
546         .global efi32_config
547 efi32_config:
548         .fill   5,8,0
549         .quad   efi64_thunk
550         .byte   0
551 #endif
552
553         .global efi64_config
554 efi64_config:
555         .fill   5,8,0
556         .quad   efi_call
557         .byte   1
558 #endif /* CONFIG_EFI_STUB */
559
560 /*
561  * Stack and heap for uncompression
562  */
563         .bss
564         .balign 4
565 boot_heap:
566         .fill BOOT_HEAP_SIZE, 1, 0
567 boot_stack:
568         .fill BOOT_STACK_SIZE, 1, 0
569 boot_stack_end:
570
571 /*
572  * Space for page tables (not in .bss so not zeroed)
573  */
574         .section ".pgtable","a",@nobits
575         .balign 4096
576 pgtable:
577         .fill BOOT_PGT_SIZE, 1, 0
578 #ifdef CONFIG_X86_5LEVEL
579 lvl5_pgtable:
580         .fill PAGE_SIZE, 1, 0
581 #endif