head_32.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * 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. * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  22. */
  23. .text
  24. #include <linux/init.h>
  25. #include <linux/linkage.h>
  26. #include <asm/segment.h>
  27. #include <asm/page_types.h>
  28. #include <asm/boot.h>
  29. #include <asm/asm-offsets.h>
  30. #include <asm/bootparam.h>
  31. /*
  32. * These symbols needed to be marked as .hidden to prevent the BFD linker from
  33. * generating R_386_32 (rather than R_386_RELATIVE) relocations for them when
  34. * the 32-bit compressed kernel is linked as PIE. This is no longer necessary,
  35. * but it doesn't hurt to keep them .hidden.
  36. */
  37. .hidden _bss
  38. .hidden _ebss
  39. .hidden _end
  40. __HEAD
  41. SYM_FUNC_START(startup_32)
  42. cld
  43. cli
  44. /*
  45. * Calculate the delta between where we were compiled to run
  46. * at and where we were actually loaded at. This can only be done
  47. * with a short local call on x86. Nothing else will tell us what
  48. * address we are running at. The reserved chunk of the real-mode
  49. * data at 0x1e4 (defined as a scratch field) are used as the stack
  50. * for this calculation. Only 4 bytes are needed.
  51. */
  52. leal (BP_scratch+4)(%esi), %esp
  53. call 1f
  54. 1: popl %edx
  55. addl $_GLOBAL_OFFSET_TABLE_+(.-1b), %edx
  56. /* Load new GDT */
  57. leal gdt@GOTOFF(%edx), %eax
  58. movl %eax, 2(%eax)
  59. lgdt (%eax)
  60. /* Load segment registers with our descriptors */
  61. movl $__BOOT_DS, %eax
  62. movl %eax, %ds
  63. movl %eax, %es
  64. movl %eax, %fs
  65. movl %eax, %gs
  66. movl %eax, %ss
  67. /*
  68. * %edx contains the address we are loaded at by the boot loader (plus the
  69. * offset to the GOT). The below code calculates %ebx to be the address where
  70. * we should move the kernel image temporarily for safe in-place decompression
  71. * (again, plus the offset to the GOT).
  72. *
  73. * %ebp is calculated to be the address that the kernel will be decompressed to.
  74. */
  75. #ifdef CONFIG_RELOCATABLE
  76. leal startup_32@GOTOFF(%edx), %ebx
  77. #ifdef CONFIG_EFI_STUB
  78. /*
  79. * If we were loaded via the EFI LoadImage service, startup_32() will be at an
  80. * offset to the start of the space allocated for the image. efi_pe_entry() will
  81. * set up image_offset to tell us where the image actually starts, so that we
  82. * can use the full available buffer.
  83. * image_offset = startup_32 - image_base
  84. * Otherwise image_offset will be zero and has no effect on the calculations.
  85. */
  86. subl image_offset@GOTOFF(%edx), %ebx
  87. #endif
  88. movl BP_kernel_alignment(%esi), %eax
  89. decl %eax
  90. addl %eax, %ebx
  91. notl %eax
  92. andl %eax, %ebx
  93. cmpl $LOAD_PHYSICAL_ADDR, %ebx
  94. jae 1f
  95. #endif
  96. movl $LOAD_PHYSICAL_ADDR, %ebx
  97. 1:
  98. movl %ebx, %ebp // Save the output address for later
  99. /* Target address to relocate to for decompression */
  100. addl BP_init_size(%esi), %ebx
  101. subl $_end@GOTOFF, %ebx
  102. /* Set up the stack */
  103. leal boot_stack_end@GOTOFF(%ebx), %esp
  104. /* Zero EFLAGS */
  105. pushl $0
  106. popfl
  107. /*
  108. * Copy the compressed kernel to the end of our buffer
  109. * where decompression in place becomes safe.
  110. */
  111. pushl %esi
  112. leal (_bss@GOTOFF-4)(%edx), %esi
  113. leal (_bss@GOTOFF-4)(%ebx), %edi
  114. movl $(_bss - startup_32), %ecx
  115. shrl $2, %ecx
  116. std
  117. rep movsl
  118. cld
  119. popl %esi
  120. /*
  121. * The GDT may get overwritten either during the copy we just did or
  122. * during extract_kernel below. To avoid any issues, repoint the GDTR
  123. * to the new copy of the GDT.
  124. */
  125. leal gdt@GOTOFF(%ebx), %eax
  126. movl %eax, 2(%eax)
  127. lgdt (%eax)
  128. /*
  129. * Jump to the relocated address.
  130. */
  131. leal .Lrelocated@GOTOFF(%ebx), %eax
  132. jmp *%eax
  133. SYM_FUNC_END(startup_32)
  134. #ifdef CONFIG_EFI_STUB
  135. SYM_FUNC_START(efi32_stub_entry)
  136. add $0x4, %esp
  137. movl 8(%esp), %esi /* save boot_params pointer */
  138. call efi_main
  139. /* efi_main returns the possibly relocated address of startup_32 */
  140. jmp *%eax
  141. SYM_FUNC_END(efi32_stub_entry)
  142. SYM_FUNC_ALIAS(efi_stub_entry, efi32_stub_entry)
  143. #endif
  144. .text
  145. SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
  146. /*
  147. * Clear BSS (stack is currently empty)
  148. */
  149. xorl %eax, %eax
  150. leal _bss@GOTOFF(%ebx), %edi
  151. leal _ebss@GOTOFF(%ebx), %ecx
  152. subl %edi, %ecx
  153. shrl $2, %ecx
  154. rep stosl
  155. /*
  156. * Do the extraction, and jump to the new kernel..
  157. */
  158. /* push arguments for extract_kernel: */
  159. pushl output_len@GOTOFF(%ebx) /* decompressed length, end of relocs */
  160. pushl %ebp /* output address */
  161. pushl input_len@GOTOFF(%ebx) /* input_len */
  162. leal input_data@GOTOFF(%ebx), %eax
  163. pushl %eax /* input_data */
  164. leal boot_heap@GOTOFF(%ebx), %eax
  165. pushl %eax /* heap area */
  166. pushl %esi /* real mode pointer */
  167. call extract_kernel /* returns kernel location in %eax */
  168. addl $24, %esp
  169. /*
  170. * Jump to the extracted kernel.
  171. */
  172. xorl %ebx, %ebx
  173. jmp *%eax
  174. SYM_FUNC_END(.Lrelocated)
  175. .data
  176. .balign 8
  177. SYM_DATA_START_LOCAL(gdt)
  178. .word gdt_end - gdt - 1
  179. .long 0
  180. .word 0
  181. .quad 0x0000000000000000 /* Reserved */
  182. .quad 0x00cf9a000000ffff /* __KERNEL_CS */
  183. .quad 0x00cf92000000ffff /* __KERNEL_DS */
  184. SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end)
  185. #ifdef CONFIG_EFI_STUB
  186. SYM_DATA(image_offset, .long 0)
  187. #endif
  188. /*
  189. * Stack and heap for uncompression
  190. */
  191. .bss
  192. .balign 4
  193. boot_heap:
  194. .fill BOOT_HEAP_SIZE, 1, 0
  195. boot_stack:
  196. .fill BOOT_STACK_SIZE, 1, 0
  197. boot_stack_end: