head.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. .code32
  23. .text
  24. #include <linux/linkage.h>
  25. #include <asm/segment.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/page.h>
  28. #include <asm/msr.h>
  29. .section ".text.head"
  30. .code32
  31. .globl startup_32
  32. startup_32:
  33. cld
  34. cli
  35. movl $(__KERNEL_DS), %eax
  36. movl %eax, %ds
  37. movl %eax, %es
  38. movl %eax, %ss
  39. /* Calculate the delta between where we were compiled to run
  40. * at and where we were actually loaded at. This can only be done
  41. * with a short local call on x86. Nothing else will tell us what
  42. * address we are running at. The reserved chunk of the real-mode
  43. * data at 0x34-0x3f are used as the stack for this calculation.
  44. * Only 4 bytes are needed.
  45. */
  46. leal 0x40(%esi), %esp
  47. call 1f
  48. 1: popl %ebp
  49. subl $1b, %ebp
  50. /* Compute the delta between where we were compiled to run at
  51. * and where the code will actually run at.
  52. */
  53. /* %ebp contains the address we are loaded at by the boot loader and %ebx
  54. * contains the address where we should move the kernel image temporarily
  55. * for safe in-place decompression.
  56. */
  57. #ifdef CONFIG_RELOCATABLE
  58. movl %ebp, %ebx
  59. addl $(LARGE_PAGE_SIZE -1), %ebx
  60. andl $LARGE_PAGE_MASK, %ebx
  61. #else
  62. movl $CONFIG_PHYSICAL_START, %ebx
  63. #endif
  64. /* Replace the compressed data size with the uncompressed size */
  65. subl input_len(%ebp), %ebx
  66. movl output_len(%ebp), %eax
  67. addl %eax, %ebx
  68. /* Add 8 bytes for every 32K input block */
  69. shrl $12, %eax
  70. addl %eax, %ebx
  71. /* Add 32K + 18 bytes of extra slack and align on a 4K boundary */
  72. addl $(32768 + 18 + 4095), %ebx
  73. andl $~4095, %ebx
  74. /*
  75. * Prepare for entering 64 bit mode
  76. */
  77. /* Load new GDT with the 64bit segments using 32bit descriptor */
  78. leal gdt(%ebp), %eax
  79. movl %eax, gdt+2(%ebp)
  80. lgdt gdt(%ebp)
  81. /* Enable PAE mode */
  82. xorl %eax, %eax
  83. orl $(1 << 5), %eax
  84. movl %eax, %cr4
  85. /*
  86. * Build early 4G boot pagetable
  87. */
  88. /* Initialize Page tables to 0*/
  89. leal pgtable(%ebx), %edi
  90. xorl %eax, %eax
  91. movl $((4096*6)/4), %ecx
  92. rep stosl
  93. /* Build Level 4 */
  94. leal pgtable + 0(%ebx), %edi
  95. leal 0x1007 (%edi), %eax
  96. movl %eax, 0(%edi)
  97. /* Build Level 3 */
  98. leal pgtable + 0x1000(%ebx), %edi
  99. leal 0x1007(%edi), %eax
  100. movl $4, %ecx
  101. 1: movl %eax, 0x00(%edi)
  102. addl $0x00001000, %eax
  103. addl $8, %edi
  104. decl %ecx
  105. jnz 1b
  106. /* Build Level 2 */
  107. leal pgtable + 0x2000(%ebx), %edi
  108. movl $0x00000183, %eax
  109. movl $2048, %ecx
  110. 1: movl %eax, 0(%edi)
  111. addl $0x00200000, %eax
  112. addl $8, %edi
  113. decl %ecx
  114. jnz 1b
  115. /* Enable the boot page tables */
  116. leal pgtable(%ebx), %eax
  117. movl %eax, %cr3
  118. /* Enable Long mode in EFER (Extended Feature Enable Register) */
  119. movl $MSR_EFER, %ecx
  120. rdmsr
  121. btsl $_EFER_LME, %eax
  122. wrmsr
  123. /* Setup for the jump to 64bit mode
  124. *
  125. * When the jump is performend we will be in long mode but
  126. * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1
  127. * (and in turn EFER.LMA = 1). To jump into 64bit mode we use
  128. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  129. * We place all of the values on our mini stack so lret can
  130. * used to perform that far jump.
  131. */
  132. pushl $__KERNEL_CS
  133. leal startup_64(%ebp), %eax
  134. pushl %eax
  135. /* Enter paged protected Mode, activating Long Mode */
  136. movl $0x80000001, %eax /* Enable Paging and Protected mode */
  137. movl %eax, %cr0
  138. /* Jump from 32bit compatibility mode into 64bit mode. */
  139. lret
  140. /* Be careful here startup_64 needs to be at a predictable
  141. * address so I can export it in an ELF header. Bootloaders
  142. * should look at the ELF header to find this address, as
  143. * it may change in the future.
  144. */
  145. .code64
  146. .org 0x100
  147. ENTRY(startup_64)
  148. /* We come here either from startup_32 or directly from a
  149. * 64bit bootloader. If we come here from a bootloader we depend on
  150. * an identity mapped page table being provied that maps our
  151. * entire text+data+bss and hopefully all of memory.
  152. */
  153. /* Setup data segments. */
  154. xorl %eax, %eax
  155. movl %eax, %ds
  156. movl %eax, %es
  157. movl %eax, %ss
  158. /* Compute the decompressed kernel start address. It is where
  159. * we were loaded at aligned to a 2M boundary. %rbp contains the
  160. * decompressed kernel start address.
  161. *
  162. * If it is a relocatable kernel then decompress and run the kernel
  163. * from load address aligned to 2MB addr, otherwise decompress and
  164. * run the kernel from CONFIG_PHYSICAL_START
  165. */
  166. /* Start with the delta to where the kernel will run at. */
  167. #ifdef CONFIG_RELOCATABLE
  168. leaq startup_32(%rip) /* - $startup_32 */, %rbp
  169. addq $(LARGE_PAGE_SIZE - 1), %rbp
  170. andq $LARGE_PAGE_MASK, %rbp
  171. movq %rbp, %rbx
  172. #else
  173. movq $CONFIG_PHYSICAL_START, %rbp
  174. movq %rbp, %rbx
  175. #endif
  176. /* Replace the compressed data size with the uncompressed size */
  177. movl input_len(%rip), %eax
  178. subq %rax, %rbx
  179. movl output_len(%rip), %eax
  180. addq %rax, %rbx
  181. /* Add 8 bytes for every 32K input block */
  182. shrq $12, %rax
  183. addq %rax, %rbx
  184. /* Add 32K + 18 bytes of extra slack and align on a 4K boundary */
  185. addq $(32768 + 18 + 4095), %rbx
  186. andq $~4095, %rbx
  187. /* Copy the compressed kernel to the end of our buffer
  188. * where decompression in place becomes safe.
  189. */
  190. leaq _end(%rip), %r8
  191. leaq _end(%rbx), %r9
  192. movq $_end /* - $startup_32 */, %rcx
  193. 1: subq $8, %r8
  194. subq $8, %r9
  195. movq 0(%r8), %rax
  196. movq %rax, 0(%r9)
  197. subq $8, %rcx
  198. jnz 1b
  199. /*
  200. * Jump to the relocated address.
  201. */
  202. leaq relocated(%rbx), %rax
  203. jmp *%rax
  204. .section ".text"
  205. relocated:
  206. /*
  207. * Clear BSS
  208. */
  209. xorq %rax, %rax
  210. leaq _edata(%rbx), %rdi
  211. leaq _end(%rbx), %rcx
  212. subq %rdi, %rcx
  213. cld
  214. rep
  215. stosb
  216. /* Setup the stack */
  217. leaq user_stack_end(%rip), %rsp
  218. /* zero EFLAGS after setting rsp */
  219. pushq $0
  220. popfq
  221. /*
  222. * Do the decompression, and jump to the new kernel..
  223. */
  224. pushq %rsi # Save the real mode argument
  225. movq %rsi, %rdi # real mode address
  226. leaq _heap(%rip), %rsi # _heap
  227. leaq input_data(%rip), %rdx # input_data
  228. movl input_len(%rip), %eax
  229. movq %rax, %rcx # input_len
  230. movq %rbp, %r8 # output
  231. call decompress_kernel
  232. popq %rsi
  233. /*
  234. * Jump to the decompressed kernel.
  235. */
  236. jmp *%rbp
  237. .data
  238. gdt:
  239. .word gdt_end - gdt
  240. .long gdt
  241. .word 0
  242. .quad 0x0000000000000000 /* NULL descriptor */
  243. .quad 0x00af9a000000ffff /* __KERNEL_CS */
  244. .quad 0x00cf92000000ffff /* __KERNEL_DS */
  245. gdt_end:
  246. .bss
  247. /* Stack for uncompression */
  248. .balign 4
  249. user_stack:
  250. .fill 4096,4,0
  251. user_stack_end: