head_64.S 7.2 KB

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