Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  *  linux/boot/head.S
0004  *
0005  *  Copyright (C) 1991, 1992, 1993  Linus Torvalds
0006  */
0007 
0008 /*
0009  *  head.S contains the 32-bit startup code.
0010  *
0011  * NOTE!!! Startup happens at absolute address 0x00001000, which is also where
0012  * the page directory will exist. The startup code will be overwritten by
0013  * the page directory. [According to comments etc elsewhere on a compressed
0014  * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC]
0015  *
0016  * Page 0 is deliberately kept safe, since System Management Mode code in
0017  * laptops may need to access the BIOS data stored there.  This is also
0018  * useful for future device drivers that either access the BIOS via VM86
0019  * mode.
0020  */
0021 
0022 /*
0023  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
0024  */
0025     .text
0026 
0027 #include <linux/init.h>
0028 #include <linux/linkage.h>
0029 #include <asm/segment.h>
0030 #include <asm/page_types.h>
0031 #include <asm/boot.h>
0032 #include <asm/asm-offsets.h>
0033 #include <asm/bootparam.h>
0034 
0035 /*
0036  * These symbols needed to be marked as .hidden to prevent the BFD linker from
0037  * generating R_386_32 (rather than R_386_RELATIVE) relocations for them when
0038  * the 32-bit compressed kernel is linked as PIE. This is no longer necessary,
0039  * but it doesn't hurt to keep them .hidden.
0040  */
0041     .hidden _bss
0042     .hidden _ebss
0043     .hidden _end
0044 
0045     __HEAD
0046 SYM_FUNC_START(startup_32)
0047     cld
0048     cli
0049 
0050 /*
0051  * Calculate the delta between where we were compiled to run
0052  * at and where we were actually loaded at.  This can only be done
0053  * with a short local call on x86.  Nothing  else will tell us what
0054  * address we are running at.  The reserved chunk of the real-mode
0055  * data at 0x1e4 (defined as a scratch field) are used as the stack
0056  * for this calculation. Only 4 bytes are needed.
0057  */
0058     leal    (BP_scratch+4)(%esi), %esp
0059     call    1f
0060 1:  popl    %edx
0061     addl    $_GLOBAL_OFFSET_TABLE_+(.-1b), %edx
0062 
0063     /* Load new GDT */
0064     leal    gdt@GOTOFF(%edx), %eax
0065     movl    %eax, 2(%eax)
0066     lgdt    (%eax)
0067 
0068     /* Load segment registers with our descriptors */
0069     movl    $__BOOT_DS, %eax
0070     movl    %eax, %ds
0071     movl    %eax, %es
0072     movl    %eax, %fs
0073     movl    %eax, %gs
0074     movl    %eax, %ss
0075 
0076 /*
0077  * %edx contains the address we are loaded at by the boot loader (plus the
0078  * offset to the GOT).  The below code calculates %ebx to be the address where
0079  * we should move the kernel image temporarily for safe in-place decompression
0080  * (again, plus the offset to the GOT).
0081  *
0082  * %ebp is calculated to be the address that the kernel will be decompressed to.
0083  */
0084 
0085 #ifdef CONFIG_RELOCATABLE
0086     leal    startup_32@GOTOFF(%edx), %ebx
0087 
0088 #ifdef CONFIG_EFI_STUB
0089 /*
0090  * If we were loaded via the EFI LoadImage service, startup_32() will be at an
0091  * offset to the start of the space allocated for the image. efi_pe_entry() will
0092  * set up image_offset to tell us where the image actually starts, so that we
0093  * can use the full available buffer.
0094  *  image_offset = startup_32 - image_base
0095  * Otherwise image_offset will be zero and has no effect on the calculations.
0096  */
0097     subl    image_offset@GOTOFF(%edx), %ebx
0098 #endif
0099 
0100     movl    BP_kernel_alignment(%esi), %eax
0101     decl    %eax
0102     addl    %eax, %ebx
0103     notl    %eax
0104     andl    %eax, %ebx
0105     cmpl    $LOAD_PHYSICAL_ADDR, %ebx
0106     jae 1f
0107 #endif
0108     movl    $LOAD_PHYSICAL_ADDR, %ebx
0109 1:
0110 
0111     movl    %ebx, %ebp  // Save the output address for later
0112     /* Target address to relocate to for decompression */
0113     addl    BP_init_size(%esi), %ebx
0114     subl    $_end@GOTOFF, %ebx
0115 
0116     /* Set up the stack */
0117     leal    boot_stack_end@GOTOFF(%ebx), %esp
0118 
0119     /* Zero EFLAGS */
0120     pushl   $0
0121     popfl
0122 
0123 /*
0124  * Copy the compressed kernel to the end of our buffer
0125  * where decompression in place becomes safe.
0126  */
0127     pushl   %esi
0128     leal    (_bss@GOTOFF-4)(%edx), %esi
0129     leal    (_bss@GOTOFF-4)(%ebx), %edi
0130     movl    $(_bss - startup_32), %ecx
0131     shrl    $2, %ecx
0132     std
0133     rep movsl
0134     cld
0135     popl    %esi
0136 
0137     /*
0138      * The GDT may get overwritten either during the copy we just did or
0139      * during extract_kernel below. To avoid any issues, repoint the GDTR
0140      * to the new copy of the GDT.
0141      */
0142     leal    gdt@GOTOFF(%ebx), %eax
0143     movl    %eax, 2(%eax)
0144     lgdt    (%eax)
0145 
0146 /*
0147  * Jump to the relocated address.
0148  */
0149     leal    .Lrelocated@GOTOFF(%ebx), %eax
0150     jmp *%eax
0151 SYM_FUNC_END(startup_32)
0152 
0153 #ifdef CONFIG_EFI_STUB
0154 SYM_FUNC_START(efi32_stub_entry)
0155     add $0x4, %esp
0156     movl    8(%esp), %esi   /* save boot_params pointer */
0157     call    efi_main
0158     /* efi_main returns the possibly relocated address of startup_32 */
0159     jmp *%eax
0160 SYM_FUNC_END(efi32_stub_entry)
0161 SYM_FUNC_ALIAS(efi_stub_entry, efi32_stub_entry)
0162 #endif
0163 
0164     .text
0165 SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
0166 
0167 /*
0168  * Clear BSS (stack is currently empty)
0169  */
0170     xorl    %eax, %eax
0171     leal    _bss@GOTOFF(%ebx), %edi
0172     leal    _ebss@GOTOFF(%ebx), %ecx
0173     subl    %edi, %ecx
0174     shrl    $2, %ecx
0175     rep stosl
0176 
0177 /*
0178  * Do the extraction, and jump to the new kernel..
0179  */
0180     /* push arguments for extract_kernel: */
0181 
0182     pushl   output_len@GOTOFF(%ebx) /* decompressed length, end of relocs */
0183     pushl   %ebp            /* output address */
0184     pushl   input_len@GOTOFF(%ebx)  /* input_len */
0185     leal    input_data@GOTOFF(%ebx), %eax
0186     pushl   %eax            /* input_data */
0187     leal    boot_heap@GOTOFF(%ebx), %eax
0188     pushl   %eax            /* heap area */
0189     pushl   %esi            /* real mode pointer */
0190     call    extract_kernel      /* returns kernel location in %eax */
0191     addl    $24, %esp
0192 
0193 /*
0194  * Jump to the extracted kernel.
0195  */
0196     xorl    %ebx, %ebx
0197     jmp *%eax
0198 SYM_FUNC_END(.Lrelocated)
0199 
0200     .data
0201     .balign 8
0202 SYM_DATA_START_LOCAL(gdt)
0203     .word   gdt_end - gdt - 1
0204     .long   0
0205     .word   0
0206     .quad   0x0000000000000000  /* Reserved */
0207     .quad   0x00cf9a000000ffff  /* __KERNEL_CS */
0208     .quad   0x00cf92000000ffff  /* __KERNEL_DS */
0209 SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end)
0210 
0211 #ifdef CONFIG_EFI_STUB
0212 SYM_DATA(image_offset, .long 0)
0213 #endif
0214 
0215 /*
0216  * Stack and heap for uncompression
0217  */
0218     .bss
0219     .balign 4
0220 boot_heap:
0221     .fill BOOT_HEAP_SIZE, 1, 0
0222 boot_stack:
0223     .fill BOOT_STACK_SIZE, 1, 0
0224 boot_stack_end: