head_32.S 6.1 KB

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