head_32.S 6.2 KB

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