head_64.S 7.4 KB

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