head_64.S 7.6 KB

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