GNU Linux-libre 4.19.264-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 #include "pgtable.h"
37
38 /*
39  * Locally defined symbols should be marked hidden:
40  */
41         .hidden _bss
42         .hidden _ebss
43         .hidden _got
44         .hidden _egot
45         .hidden _end
46
47         __HEAD
48         .code32
49 ENTRY(startup_32)
50         /*
51          * 32bit entry is 0 and it is ABI so immutable!
52          * If we come here directly from a bootloader,
53          * kernel(text+data+bss+brk) ramdisk, zero_page, command line
54          * all need to be under the 4G limit.
55          */
56         cld
57         /*
58          * Test KEEP_SEGMENTS flag to see if the bootloader is asking
59          * us to not reload segments
60          */
61         testb $KEEP_SEGMENTS, BP_loadflags(%esi)
62         jnz 1f
63
64         cli
65         movl    $(__BOOT_DS), %eax
66         movl    %eax, %ds
67         movl    %eax, %es
68         movl    %eax, %ss
69 1:
70
71 /*
72  * Calculate the delta between where we were compiled to run
73  * at and where we were actually loaded at.  This can only be done
74  * with a short local call on x86.  Nothing  else will tell us what
75  * address we are running at.  The reserved chunk of the real-mode
76  * data at 0x1e4 (defined as a scratch field) are used as the stack
77  * for this calculation. Only 4 bytes are needed.
78  */
79         leal    (BP_scratch+4)(%esi), %esp
80         call    1f
81 1:      popl    %ebp
82         subl    $1b, %ebp
83
84 /* setup a stack and make sure cpu supports long mode. */
85         movl    $boot_stack_end, %eax
86         addl    %ebp, %eax
87         movl    %eax, %esp
88
89         call    verify_cpu
90         testl   %eax, %eax
91         jnz     no_longmode
92
93 /*
94  * Compute the delta between where we were compiled to run at
95  * and where the code will actually run at.
96  *
97  * %ebp contains the address we are loaded at by the boot loader and %ebx
98  * contains the address where we should move the kernel image temporarily
99  * for safe in-place decompression.
100  */
101
102 #ifdef CONFIG_RELOCATABLE
103         movl    %ebp, %ebx
104         movl    BP_kernel_alignment(%esi), %eax
105         decl    %eax
106         addl    %eax, %ebx
107         notl    %eax
108         andl    %eax, %ebx
109         cmpl    $LOAD_PHYSICAL_ADDR, %ebx
110         jae     1f
111 #endif
112         movl    $LOAD_PHYSICAL_ADDR, %ebx
113 1:
114
115         /* Target address to relocate to for decompression */
116         movl    BP_init_size(%esi), %eax
117         subl    $_end, %eax
118         addl    %eax, %ebx
119
120 /*
121  * Prepare for entering 64 bit mode
122  */
123
124         /* Load new GDT with the 64bit segments using 32bit descriptor */
125         addl    %ebp, gdt+2(%ebp)
126         lgdt    gdt(%ebp)
127
128         /* Enable PAE mode */
129         movl    %cr4, %eax
130         orl     $X86_CR4_PAE, %eax
131         movl    %eax, %cr4
132
133  /*
134   * Build early 4G boot pagetable
135   */
136         /*
137          * If SEV is active then set the encryption mask in the page tables.
138          * This will insure that when the kernel is copied and decompressed
139          * it will be done so encrypted.
140          */
141         call    get_sev_encryption_bit
142         xorl    %edx, %edx
143         testl   %eax, %eax
144         jz      1f
145         subl    $32, %eax       /* Encryption bit is always above bit 31 */
146         bts     %eax, %edx      /* Set encryption mask for page tables */
147 1:
148
149         /* Initialize Page tables to 0 */
150         leal    pgtable(%ebx), %edi
151         xorl    %eax, %eax
152         movl    $(BOOT_INIT_PGT_SIZE/4), %ecx
153         rep     stosl
154
155         /* Build Level 4 */
156         leal    pgtable + 0(%ebx), %edi
157         leal    0x1007 (%edi), %eax
158         movl    %eax, 0(%edi)
159         addl    %edx, 4(%edi)
160
161         /* Build Level 3 */
162         leal    pgtable + 0x1000(%ebx), %edi
163         leal    0x1007(%edi), %eax
164         movl    $4, %ecx
165 1:      movl    %eax, 0x00(%edi)
166         addl    %edx, 0x04(%edi)
167         addl    $0x00001000, %eax
168         addl    $8, %edi
169         decl    %ecx
170         jnz     1b
171
172         /* Build Level 2 */
173         leal    pgtable + 0x2000(%ebx), %edi
174         movl    $0x00000183, %eax
175         movl    $2048, %ecx
176 1:      movl    %eax, 0(%edi)
177         addl    %edx, 4(%edi)
178         addl    $0x00200000, %eax
179         addl    $8, %edi
180         decl    %ecx
181         jnz     1b
182
183         /* Enable the boot page tables */
184         leal    pgtable(%ebx), %eax
185         movl    %eax, %cr3
186
187         /* Enable Long mode in EFER (Extended Feature Enable Register) */
188         movl    $MSR_EFER, %ecx
189         rdmsr
190         btsl    $_EFER_LME, %eax
191         wrmsr
192
193         /* After gdt is loaded */
194         xorl    %eax, %eax
195         lldt    %ax
196         movl    $__BOOT_TSS, %eax
197         ltr     %ax
198
199         /*
200          * Setup for the jump to 64bit mode
201          *
202          * When the jump is performend we will be in long mode but
203          * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1
204          * (and in turn EFER.LMA = 1).  To jump into 64bit mode we use
205          * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
206          * We place all of the values on our mini stack so lret can
207          * used to perform that far jump.
208          */
209         pushl   $__KERNEL_CS
210         leal    startup_64(%ebp), %eax
211 #ifdef CONFIG_EFI_MIXED
212         movl    efi32_config(%ebp), %ebx
213         cmp     $0, %ebx
214         jz      1f
215         leal    handover_entry(%ebp), %eax
216 1:
217 #endif
218         pushl   %eax
219
220         /* Enter paged protected Mode, activating Long Mode */
221         movl    $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */
222         movl    %eax, %cr0
223
224         /* Jump from 32bit compatibility mode into 64bit mode. */
225         lret
226 ENDPROC(startup_32)
227
228 #ifdef CONFIG_EFI_MIXED
229         .org 0x190
230 ENTRY(efi32_stub_entry)
231         add     $0x4, %esp              /* Discard return address */
232         popl    %ecx
233         popl    %edx
234         popl    %esi
235
236         leal    (BP_scratch+4)(%esi), %esp
237         call    1f
238 1:      pop     %ebp
239         subl    $1b, %ebp
240
241         movl    %ecx, efi32_config(%ebp)
242         movl    %edx, efi32_config+8(%ebp)
243         sgdtl   efi32_boot_gdt(%ebp)
244
245         leal    efi32_config(%ebp), %eax
246         movl    %eax, efi_config(%ebp)
247
248         /* Disable paging */
249         movl    %cr0, %eax
250         btrl    $X86_CR0_PG_BIT, %eax
251         movl    %eax, %cr0
252
253         jmp     startup_32
254 ENDPROC(efi32_stub_entry)
255 #endif
256
257         .code64
258         .org 0x200
259 ENTRY(startup_64)
260         /*
261          * 64bit entry is 0x200 and it is ABI so immutable!
262          * We come here either from startup_32 or directly from a
263          * 64bit bootloader.
264          * If we come here from a bootloader, kernel(text+data+bss+brk),
265          * ramdisk, zero_page, command line could be above 4G.
266          * We depend on an identity mapped page table being provided
267          * that maps our entire kernel(text+data+bss+brk), zero page
268          * and command line.
269          */
270
271         /* Setup data segments. */
272         xorl    %eax, %eax
273         movl    %eax, %ds
274         movl    %eax, %es
275         movl    %eax, %ss
276         movl    %eax, %fs
277         movl    %eax, %gs
278
279         /*
280          * Compute the decompressed kernel start address.  It is where
281          * we were loaded at aligned to a 2M boundary. %rbp contains the
282          * decompressed kernel start address.
283          *
284          * If it is a relocatable kernel then decompress and run the kernel
285          * from load address aligned to 2MB addr, otherwise decompress and
286          * run the kernel from LOAD_PHYSICAL_ADDR
287          *
288          * We cannot rely on the calculation done in 32-bit mode, since we
289          * may have been invoked via the 64-bit entry point.
290          */
291
292         /* Start with the delta to where the kernel will run at. */
293 #ifdef CONFIG_RELOCATABLE
294         leaq    startup_32(%rip) /* - $startup_32 */, %rbp
295         movl    BP_kernel_alignment(%rsi), %eax
296         decl    %eax
297         addq    %rax, %rbp
298         notq    %rax
299         andq    %rax, %rbp
300         cmpq    $LOAD_PHYSICAL_ADDR, %rbp
301         jae     1f
302 #endif
303         movq    $LOAD_PHYSICAL_ADDR, %rbp
304 1:
305
306         /* Target address to relocate to for decompression */
307         movl    BP_init_size(%rsi), %ebx
308         subl    $_end, %ebx
309         addq    %rbp, %rbx
310
311         /* Set up the stack */
312         leaq    boot_stack_end(%rbx), %rsp
313
314         /*
315          * paging_prepare() and cleanup_trampoline() below can have GOT
316          * references. Adjust the table with address we are running at.
317          *
318          * Zero RAX for adjust_got: the GOT was not adjusted before;
319          * there's no adjustment to undo.
320          */
321         xorq    %rax, %rax
322
323         /*
324          * Calculate the address the binary is loaded at and use it as
325          * a GOT adjustment.
326          */
327         call    1f
328 1:      popq    %rdi
329         subq    $1b, %rdi
330
331         call    adjust_got
332
333         /*
334          * At this point we are in long mode with 4-level paging enabled,
335          * but we might want to enable 5-level paging or vice versa.
336          *
337          * The problem is that we cannot do it directly. Setting or clearing
338          * CR4.LA57 in long mode would trigger #GP. So we need to switch off
339          * long mode and paging first.
340          *
341          * We also need a trampoline in lower memory to switch over from
342          * 4- to 5-level paging for cases when the bootloader puts the kernel
343          * above 4G, but didn't enable 5-level paging for us.
344          *
345          * The same trampoline can be used to switch from 5- to 4-level paging
346          * mode, like when starting 4-level paging kernel via kexec() when
347          * original kernel worked in 5-level paging mode.
348          *
349          * For the trampoline, we need the top page table to reside in lower
350          * memory as we don't have a way to load 64-bit values into CR3 in
351          * 32-bit mode.
352          *
353          * We go though the trampoline even if we don't have to: if we're
354          * already in a desired paging mode. This way the trampoline code gets
355          * tested on every boot.
356          */
357
358         /* Make sure we have GDT with 32-bit code segment */
359         leaq    gdt(%rip), %rax
360         movq    %rax, gdt64+2(%rip)
361         lgdt    gdt64(%rip)
362
363         /*
364          * paging_prepare() sets up the trampoline and checks if we need to
365          * enable 5-level paging.
366          *
367          * Address of the trampoline is returned in RAX.
368          * Non zero RDX on return means we need to enable 5-level paging.
369          *
370          * RSI holds real mode data and needs to be preserved across
371          * this function call.
372          */
373         pushq   %rsi
374         movq    %rsi, %rdi              /* real mode address */
375         call    paging_prepare
376         popq    %rsi
377
378         /* Save the trampoline address in RCX */
379         movq    %rax, %rcx
380
381         /*
382          * Load the address of trampoline_return() into RDI.
383          * It will be used by the trampoline to return to the main code.
384          */
385         leaq    trampoline_return(%rip), %rdi
386
387         /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */
388         pushq   $__KERNEL32_CS
389         leaq    TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax
390         pushq   %rax
391         lretq
392 trampoline_return:
393         /* Restore the stack, the 32-bit trampoline uses its own stack */
394         leaq    boot_stack_end(%rbx), %rsp
395
396         /*
397          * cleanup_trampoline() would restore trampoline memory.
398          *
399          * RDI is address of the page table to use instead of page table
400          * in trampoline memory (if required).
401          *
402          * RSI holds real mode data and needs to be preserved across
403          * this function call.
404          */
405         pushq   %rsi
406         leaq    top_pgtable(%rbx), %rdi
407         call    cleanup_trampoline
408         popq    %rsi
409
410         /* Zero EFLAGS */
411         pushq   $0
412         popfq
413
414         /*
415          * Previously we've adjusted the GOT with address the binary was
416          * loaded at. Now we need to re-adjust for relocation address.
417          *
418          * Calculate the address the binary is loaded at, so that we can
419          * undo the previous GOT adjustment.
420          */
421         call    1f
422 1:      popq    %rax
423         subq    $1b, %rax
424
425         /* The new adjustment is the relocation address */
426         movq    %rbx, %rdi
427         call    adjust_got
428
429 /*
430  * Copy the compressed kernel to the end of our buffer
431  * where decompression in place becomes safe.
432  */
433         pushq   %rsi
434         leaq    (_bss-8)(%rip), %rsi
435         leaq    (_bss-8)(%rbx), %rdi
436         movq    $_bss /* - $startup_32 */, %rcx
437         shrq    $3, %rcx
438         std
439         rep     movsq
440         cld
441         popq    %rsi
442
443 /*
444  * Jump to the relocated address.
445  */
446         leaq    relocated(%rbx), %rax
447         jmp     *%rax
448
449 #ifdef CONFIG_EFI_STUB
450
451 /* The entry point for the PE/COFF executable is efi_pe_entry. */
452 ENTRY(efi_pe_entry)
453         movq    %rcx, efi64_config(%rip)        /* Handle */
454         movq    %rdx, efi64_config+8(%rip) /* EFI System table pointer */
455
456         leaq    efi64_config(%rip), %rax
457         movq    %rax, efi_config(%rip)
458
459         call    1f
460 1:      popq    %rbp
461         subq    $1b, %rbp
462
463         /*
464          * Relocate efi_config->call().
465          */
466         addq    %rbp, efi64_config+40(%rip)
467
468         movq    %rax, %rdi
469         call    make_boot_params
470         cmpq    $0,%rax
471         je      fail
472         mov     %rax, %rsi
473         leaq    startup_32(%rip), %rax
474         movl    %eax, BP_code32_start(%rsi)
475         jmp     2f              /* Skip the relocation */
476
477 handover_entry:
478         call    1f
479 1:      popq    %rbp
480         subq    $1b, %rbp
481
482         /*
483          * Relocate efi_config->call().
484          */
485         movq    efi_config(%rip), %rax
486         addq    %rbp, 40(%rax)
487 2:
488         movq    efi_config(%rip), %rdi
489         call    efi_main
490         movq    %rax,%rsi
491         cmpq    $0,%rax
492         jne     2f
493 fail:
494         /* EFI init failed, so hang. */
495         hlt
496         jmp     fail
497 2:
498         movl    BP_code32_start(%esi), %eax
499         leaq    startup_64(%rax), %rax
500         jmp     *%rax
501 ENDPROC(efi_pe_entry)
502
503         .org 0x390
504 ENTRY(efi64_stub_entry)
505         movq    %rdi, efi64_config(%rip)        /* Handle */
506         movq    %rsi, efi64_config+8(%rip) /* EFI System table pointer */
507
508         leaq    efi64_config(%rip), %rax
509         movq    %rax, efi_config(%rip)
510
511         movq    %rdx, %rsi
512         jmp     handover_entry
513 ENDPROC(efi64_stub_entry)
514 #endif
515
516         .text
517 relocated:
518
519 /*
520  * Clear BSS (stack is currently empty)
521  */
522         xorl    %eax, %eax
523         leaq    _bss(%rip), %rdi
524         leaq    _ebss(%rip), %rcx
525         subq    %rdi, %rcx
526         shrq    $3, %rcx
527         rep     stosq
528
529 /*
530  * Do the extraction, and jump to the new kernel..
531  */
532         pushq   %rsi                    /* Save the real mode argument */
533         movq    %rsi, %rdi              /* real mode address */
534         leaq    boot_heap(%rip), %rsi   /* malloc area for uncompression */
535         leaq    input_data(%rip), %rdx  /* input_data */
536         movl    $z_input_len, %ecx      /* input_len */
537         movq    %rbp, %r8               /* output target address */
538         movq    $z_output_len, %r9      /* decompressed length, end of relocs */
539         call    extract_kernel          /* returns kernel location in %rax */
540         popq    %rsi
541
542 /*
543  * Jump to the decompressed kernel.
544  */
545         jmp     *%rax
546
547 /*
548  * Adjust the global offset table
549  *
550  * RAX is the previous adjustment of the table to undo (use 0 if it's the
551  * first time we touch GOT).
552  * RDI is the new adjustment to apply.
553  */
554 adjust_got:
555         /* Walk through the GOT adding the address to the entries */
556         leaq    _got(%rip), %rdx
557         leaq    _egot(%rip), %rcx
558 1:
559         cmpq    %rcx, %rdx
560         jae     2f
561         subq    %rax, (%rdx)    /* Undo previous adjustment */
562         addq    %rdi, (%rdx)    /* Apply the new adjustment */
563         addq    $8, %rdx
564         jmp     1b
565 2:
566         ret
567
568         .code32
569 /*
570  * This is the 32-bit trampoline that will be copied over to low memory.
571  *
572  * RDI contains the return address (might be above 4G).
573  * ECX contains the base address of the trampoline memory.
574  * Non zero RDX on return means we need to enable 5-level paging.
575  */
576 ENTRY(trampoline_32bit_src)
577         /* Set up data and stack segments */
578         movl    $__KERNEL_DS, %eax
579         movl    %eax, %ds
580         movl    %eax, %ss
581
582         /* Set up new stack */
583         leal    TRAMPOLINE_32BIT_STACK_END(%ecx), %esp
584
585         /* Disable paging */
586         movl    %cr0, %eax
587         btrl    $X86_CR0_PG_BIT, %eax
588         movl    %eax, %cr0
589
590         /* Check what paging mode we want to be in after the trampoline */
591         cmpl    $0, %edx
592         jz      1f
593
594         /* We want 5-level paging: don't touch CR3 if it already points to 5-level page tables */
595         movl    %cr4, %eax
596         testl   $X86_CR4_LA57, %eax
597         jnz     3f
598         jmp     2f
599 1:
600         /* We want 4-level paging: don't touch CR3 if it already points to 4-level page tables */
601         movl    %cr4, %eax
602         testl   $X86_CR4_LA57, %eax
603         jz      3f
604 2:
605         /* Point CR3 to the trampoline's new top level page table */
606         leal    TRAMPOLINE_32BIT_PGTABLE_OFFSET(%ecx), %eax
607         movl    %eax, %cr3
608 3:
609         /* Set EFER.LME=1 as a precaution in case hypervsior pulls the rug */
610         pushl   %ecx
611         pushl   %edx
612         movl    $MSR_EFER, %ecx
613         rdmsr
614         btsl    $_EFER_LME, %eax
615         wrmsr
616         popl    %edx
617         popl    %ecx
618
619         /* Enable PAE and LA57 (if required) paging modes */
620         movl    $X86_CR4_PAE, %eax
621         cmpl    $0, %edx
622         jz      1f
623         orl     $X86_CR4_LA57, %eax
624 1:
625         movl    %eax, %cr4
626
627         /* Calculate address of paging_enabled() once we are executing in the trampoline */
628         leal    paging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%ecx), %eax
629
630         /* Prepare the stack for far return to Long Mode */
631         pushl   $__KERNEL_CS
632         pushl   %eax
633
634         /* Enable paging again */
635         movl    $(X86_CR0_PG | X86_CR0_PE), %eax
636         movl    %eax, %cr0
637
638         lret
639
640         .code64
641 paging_enabled:
642         /* Return from the trampoline */
643         jmp     *%rdi
644
645         /*
646          * The trampoline code has a size limit.
647          * Make sure we fail to compile if the trampoline code grows
648          * beyond TRAMPOLINE_32BIT_CODE_SIZE bytes.
649          */
650         .org    trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_SIZE
651
652         .code32
653 no_longmode:
654         /* This isn't an x86-64 CPU, so hang intentionally, we cannot continue */
655 1:
656         hlt
657         jmp     1b
658
659 #include "../../kernel/verify_cpu.S"
660
661         .data
662 gdt64:
663         .word   gdt_end - gdt
664         .long   0
665         .word   0
666         .quad   0
667 gdt:
668         .word   gdt_end - gdt
669         .long   gdt
670         .word   0
671         .quad   0x00cf9a000000ffff      /* __KERNEL32_CS */
672         .quad   0x00af9a000000ffff      /* __KERNEL_CS */
673         .quad   0x00cf92000000ffff      /* __KERNEL_DS */
674         .quad   0x0080890000000000      /* TS descriptor */
675         .quad   0x0000000000000000      /* TS continued */
676 gdt_end:
677
678 #ifdef CONFIG_EFI_STUB
679 efi_config:
680         .quad   0
681
682 #ifdef CONFIG_EFI_MIXED
683         .global efi32_config
684 efi32_config:
685         .fill   5,8,0
686         .quad   efi64_thunk
687         .byte   0
688 #endif
689
690         .global efi64_config
691 efi64_config:
692         .fill   5,8,0
693         .quad   efi_call
694         .byte   1
695 #endif /* CONFIG_EFI_STUB */
696
697 /*
698  * Stack and heap for uncompression
699  */
700         .bss
701         .balign 4
702 boot_heap:
703         .fill BOOT_HEAP_SIZE, 1, 0
704 boot_stack:
705         .fill BOOT_STACK_SIZE, 1, 0
706 boot_stack_end:
707
708 /*
709  * Space for page tables (not in .bss so not zeroed)
710  */
711         .section ".pgtable","a",@nobits
712         .balign 4096
713 pgtable:
714         .fill BOOT_PGT_SIZE, 1, 0
715
716 /*
717  * The page table is going to be used instead of page table in the trampoline
718  * memory.
719  */
720 top_pgtable:
721         .fill PAGE_SIZE, 1, 0