head_32.S 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * The 32-bit x86 assembler in binutils 2.26 will generate R_386_GOT32X
  33. * relocation to get the symbol address in PIC. When the compressed x86
  34. * kernel isn't built as PIC, the linker optimizes R_386_GOT32X
  35. * relocations to their fixed symbol addresses. However, when the
  36. * compressed x86 kernel is loaded at a different address, it leads
  37. * to the following load failure:
  38. *
  39. * Failed to allocate space for phdrs
  40. *
  41. * during the decompression stage.
  42. *
  43. * If the compressed x86 kernel is relocatable at run-time, it should be
  44. * compiled with -fPIE, instead of -fPIC, if possible and should be built as
  45. * Position Independent Executable (PIE) so that linker won't optimize
  46. * R_386_GOT32X relocation to its fixed symbol address. Older
  47. * linkers generate R_386_32 relocations against locally defined symbols,
  48. * _bss, _ebss, _got and _egot, in PIE. It isn't wrong, just less
  49. * optimal than R_386_RELATIVE. But the x86 kernel fails to properly handle
  50. * R_386_32 relocations when relocating the kernel. To generate
  51. * R_386_RELATIVE relocations, we mark _bss, _ebss, _got and _egot as
  52. * hidden:
  53. */
  54. .hidden _bss
  55. .hidden _ebss
  56. .hidden _got
  57. .hidden _egot
  58. __HEAD
  59. ENTRY(startup_32)
  60. cld
  61. /*
  62. * Test KEEP_SEGMENTS flag to see if the bootloader is asking
  63. * us to not reload segments
  64. */
  65. testb $KEEP_SEGMENTS, BP_loadflags(%esi)
  66. jnz 1f
  67. cli
  68. movl $__BOOT_DS, %eax
  69. movl %eax, %ds
  70. movl %eax, %es
  71. movl %eax, %fs
  72. movl %eax, %gs
  73. movl %eax, %ss
  74. 1:
  75. /*
  76. * Calculate the delta between where we were compiled to run
  77. * at and where we were actually loaded at. This can only be done
  78. * with a short local call on x86. Nothing else will tell us what
  79. * address we are running at. The reserved chunk of the real-mode
  80. * data at 0x1e4 (defined as a scratch field) are used as the stack
  81. * for this calculation. Only 4 bytes are needed.
  82. */
  83. leal (BP_scratch+4)(%esi), %esp
  84. call 1f
  85. 1: popl %ebp
  86. subl $1b, %ebp
  87. /*
  88. * %ebp contains the address we are loaded at by the boot loader and %ebx
  89. * contains the address where we should move the kernel image temporarily
  90. * for safe in-place decompression.
  91. */
  92. #ifdef CONFIG_RELOCATABLE
  93. movl %ebp, %ebx
  94. movl BP_kernel_alignment(%esi), %eax
  95. decl %eax
  96. addl %eax, %ebx
  97. notl %eax
  98. andl %eax, %ebx
  99. cmpl $LOAD_PHYSICAL_ADDR, %ebx
  100. jge 1f
  101. #endif
  102. movl $LOAD_PHYSICAL_ADDR, %ebx
  103. 1:
  104. /* Target address to relocate to for decompression */
  105. movl BP_init_size(%esi), %eax
  106. subl $_end, %eax
  107. addl %eax, %ebx
  108. /* Set up the stack */
  109. leal boot_stack_end(%ebx), %esp
  110. /* Zero EFLAGS */
  111. pushl $0
  112. popfl
  113. /*
  114. * Copy the compressed kernel to the end of our buffer
  115. * where decompression in place becomes safe.
  116. */
  117. pushl %esi
  118. leal (_bss-4)(%ebp), %esi
  119. leal (_bss-4)(%ebx), %edi
  120. movl $(_bss - startup_32), %ecx
  121. shrl $2, %ecx
  122. std
  123. rep movsl
  124. cld
  125. popl %esi
  126. /*
  127. * Jump to the relocated address.
  128. */
  129. leal relocated(%ebx), %eax
  130. jmp *%eax
  131. ENDPROC(startup_32)
  132. #ifdef CONFIG_EFI_STUB
  133. /*
  134. * We don't need the return address, so set up the stack so efi_main() can find
  135. * its arguments.
  136. */
  137. ENTRY(efi_pe_entry)
  138. add $0x4, %esp
  139. call 1f
  140. 1: popl %esi
  141. subl $1b, %esi
  142. popl %ecx
  143. movl %ecx, efi32_config(%esi) /* Handle */
  144. popl %ecx
  145. movl %ecx, efi32_config+8(%esi) /* EFI System table pointer */
  146. /* Relocate efi_config->call() */
  147. leal efi32_config(%esi), %eax
  148. add %esi, 40(%eax)
  149. pushl %eax
  150. call make_boot_params
  151. cmpl $0, %eax
  152. je fail
  153. movl %esi, BP_code32_start(%eax)
  154. popl %ecx
  155. pushl %eax
  156. pushl %ecx
  157. jmp 2f /* Skip efi_config initialization */
  158. ENDPROC(efi_pe_entry)
  159. ENTRY(efi32_stub_entry)
  160. add $0x4, %esp
  161. popl %ecx
  162. popl %edx
  163. call 1f
  164. 1: popl %esi
  165. subl $1b, %esi
  166. movl %ecx, efi32_config(%esi) /* Handle */
  167. movl %edx, efi32_config+8(%esi) /* EFI System table pointer */
  168. /* Relocate efi_config->call() */
  169. leal efi32_config(%esi), %eax
  170. add %esi, 40(%eax)
  171. pushl %eax
  172. 2:
  173. call efi_main
  174. cmpl $0, %eax
  175. movl %eax, %esi
  176. jne 2f
  177. fail:
  178. /* EFI init failed, so hang. */
  179. hlt
  180. jmp fail
  181. 2:
  182. movl BP_code32_start(%esi), %eax
  183. leal startup_32(%eax), %eax
  184. jmp *%eax
  185. ENDPROC(efi32_stub_entry)
  186. #endif
  187. .text
  188. relocated:
  189. /*
  190. * Clear BSS (stack is currently empty)
  191. */
  192. xorl %eax, %eax
  193. leal _bss(%ebx), %edi
  194. leal _ebss(%ebx), %ecx
  195. subl %edi, %ecx
  196. shrl $2, %ecx
  197. rep stosl
  198. /*
  199. * Adjust our own GOT
  200. */
  201. leal _got(%ebx), %edx
  202. leal _egot(%ebx), %ecx
  203. 1:
  204. cmpl %ecx, %edx
  205. jae 2f
  206. addl %ebx, (%edx)
  207. addl $4, %edx
  208. jmp 1b
  209. 2:
  210. /*
  211. * Do the extraction, and jump to the new kernel..
  212. */
  213. /* push arguments for extract_kernel: */
  214. pushl $z_output_len /* decompressed length, end of relocs */
  215. movl BP_init_size(%esi), %eax
  216. subl $_end, %eax
  217. movl %ebx, %ebp
  218. subl %eax, %ebp
  219. pushl %ebp /* output address */
  220. pushl $z_input_len /* input_len */
  221. leal input_data(%ebx), %eax
  222. pushl %eax /* input_data */
  223. leal boot_heap(%ebx), %eax
  224. pushl %eax /* heap area */
  225. pushl %esi /* real mode pointer */
  226. call extract_kernel /* returns kernel location in %eax */
  227. addl $24, %esp
  228. /*
  229. * Jump to the extracted kernel.
  230. */
  231. xorl %ebx, %ebx
  232. jmp *%eax
  233. #ifdef CONFIG_EFI_STUB
  234. .data
  235. efi32_config:
  236. .fill 5,8,0
  237. .long efi_call_phys
  238. .long 0
  239. .byte 0
  240. #endif
  241. /*
  242. * Stack and heap for uncompression
  243. */
  244. .bss
  245. .balign 4
  246. boot_heap:
  247. .fill BOOT_HEAP_SIZE, 1, 0
  248. boot_stack:
  249. .fill BOOT_STACK_SIZE, 1, 0
  250. boot_stack_end: