head_64.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/init.h>
  25. #include <linux/linkage.h>
  26. #include <asm/segment.h>
  27. #include <asm/boot.h>
  28. #include <asm/msr.h>
  29. #include <asm/processor-flags.h>
  30. #include <asm/asm-offsets.h>
  31. #include <asm/bootparam.h>
  32. /*
  33. * Locally defined symbols should be marked hidden:
  34. */
  35. .hidden _bss
  36. .hidden _ebss
  37. .hidden _got
  38. .hidden _egot
  39. __HEAD
  40. .code32
  41. ENTRY(startup_32)
  42. /*
  43. * 32bit entry is 0 and it is ABI so immutable!
  44. * If we come here directly from a bootloader,
  45. * kernel(text+data+bss+brk) ramdisk, zero_page, command line
  46. * all need to be under the 4G limit.
  47. */
  48. cld
  49. /*
  50. * Test KEEP_SEGMENTS flag to see if the bootloader is asking
  51. * us to not reload segments
  52. */
  53. testb $KEEP_SEGMENTS, BP_loadflags(%esi)
  54. jnz 1f
  55. cli
  56. movl $(__BOOT_DS), %eax
  57. movl %eax, %ds
  58. movl %eax, %es
  59. movl %eax, %ss
  60. 1:
  61. /*
  62. * Calculate the delta between where we were compiled to run
  63. * at and where we were actually loaded at. This can only be done
  64. * with a short local call on x86. Nothing else will tell us what
  65. * address we are running at. The reserved chunk of the real-mode
  66. * data at 0x1e4 (defined as a scratch field) are used as the stack
  67. * for this calculation. Only 4 bytes are needed.
  68. */
  69. leal (BP_scratch+4)(%esi), %esp
  70. call 1f
  71. 1: popl %ebp
  72. subl $1b, %ebp
  73. /* setup a stack and make sure cpu supports long mode. */
  74. movl $boot_stack_end, %eax
  75. addl %ebp, %eax
  76. movl %eax, %esp
  77. call verify_cpu
  78. testl %eax, %eax
  79. jnz no_longmode
  80. /*
  81. * Compute the delta between where we were compiled to run at
  82. * and where the code will actually run at.
  83. *
  84. * %ebp contains the address we are loaded at by the boot loader and %ebx
  85. * contains the address where we should move the kernel image temporarily
  86. * for safe in-place decompression.
  87. */
  88. #ifdef CONFIG_RELOCATABLE
  89. movl %ebp, %ebx
  90. movl BP_kernel_alignment(%esi), %eax
  91. decl %eax
  92. addl %eax, %ebx
  93. notl %eax
  94. andl %eax, %ebx
  95. cmpl $LOAD_PHYSICAL_ADDR, %ebx
  96. jge 1f
  97. #endif
  98. movl $LOAD_PHYSICAL_ADDR, %ebx
  99. 1:
  100. /* Target address to relocate to for decompression */
  101. movl BP_init_size(%esi), %eax
  102. subl $_end, %eax
  103. addl %eax, %ebx
  104. /*
  105. * Prepare for entering 64 bit mode
  106. */
  107. /* Load new GDT with the 64bit segments using 32bit descriptor */
  108. leal gdt(%ebp), %eax
  109. movl %eax, gdt+2(%ebp)
  110. lgdt gdt(%ebp)
  111. /* Enable PAE mode */
  112. movl %cr4, %eax
  113. orl $X86_CR4_PAE, %eax
  114. movl %eax, %cr4
  115. /*
  116. * Build early 4G boot pagetable
  117. */
  118. /* Initialize Page tables to 0 */
  119. leal pgtable(%ebx), %edi
  120. xorl %eax, %eax
  121. movl $(BOOT_INIT_PGT_SIZE/4), %ecx
  122. rep stosl
  123. /* Build Level 4 */
  124. leal pgtable + 0(%ebx), %edi
  125. leal 0x1007 (%edi), %eax
  126. movl %eax, 0(%edi)
  127. /* Build Level 3 */
  128. leal pgtable + 0x1000(%ebx), %edi
  129. leal 0x1007(%edi), %eax
  130. movl $4, %ecx
  131. 1: movl %eax, 0x00(%edi)
  132. addl $0x00001000, %eax
  133. addl $8, %edi
  134. decl %ecx
  135. jnz 1b
  136. /* Build Level 2 */
  137. leal pgtable + 0x2000(%ebx), %edi
  138. movl $0x00000183, %eax
  139. movl $2048, %ecx
  140. 1: movl %eax, 0(%edi)
  141. addl $0x00200000, %eax
  142. addl $8, %edi
  143. decl %ecx
  144. jnz 1b
  145. /* Enable the boot page tables */
  146. leal pgtable(%ebx), %eax
  147. movl %eax, %cr3
  148. /* Enable Long mode in EFER (Extended Feature Enable Register) */
  149. movl $MSR_EFER, %ecx
  150. rdmsr
  151. btsl $_EFER_LME, %eax
  152. wrmsr
  153. /* After gdt is loaded */
  154. xorl %eax, %eax
  155. lldt %ax
  156. movl $__BOOT_TSS, %eax
  157. ltr %ax
  158. /*
  159. * Setup for the jump to 64bit mode
  160. *
  161. * When the jump is performend we will be in long mode but
  162. * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1
  163. * (and in turn EFER.LMA = 1). To jump into 64bit mode we use
  164. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  165. * We place all of the values on our mini stack so lret can
  166. * used to perform that far jump.
  167. */
  168. pushl $__KERNEL_CS
  169. leal startup_64(%ebp), %eax
  170. #ifdef CONFIG_EFI_MIXED
  171. movl efi32_config(%ebp), %ebx
  172. cmp $0, %ebx
  173. jz 1f
  174. leal handover_entry(%ebp), %eax
  175. 1:
  176. #endif
  177. pushl %eax
  178. /* Enter paged protected Mode, activating Long Mode */
  179. movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */
  180. movl %eax, %cr0
  181. /* Jump from 32bit compatibility mode into 64bit mode. */
  182. lret
  183. ENDPROC(startup_32)
  184. #ifdef CONFIG_EFI_MIXED
  185. .org 0x190
  186. ENTRY(efi32_stub_entry)
  187. add $0x4, %esp /* Discard return address */
  188. popl %ecx
  189. popl %edx
  190. popl %esi
  191. leal (BP_scratch+4)(%esi), %esp
  192. call 1f
  193. 1: pop %ebp
  194. subl $1b, %ebp
  195. movl %ecx, efi32_config(%ebp)
  196. movl %edx, efi32_config+8(%ebp)
  197. sgdtl efi32_boot_gdt(%ebp)
  198. leal efi32_config(%ebp), %eax
  199. movl %eax, efi_config(%ebp)
  200. jmp startup_32
  201. ENDPROC(efi32_stub_entry)
  202. #endif
  203. .code64
  204. .org 0x200
  205. ENTRY(startup_64)
  206. /*
  207. * 64bit entry is 0x200 and it is ABI so immutable!
  208. * We come here either from startup_32 or directly from a
  209. * 64bit bootloader.
  210. * If we come here from a bootloader, kernel(text+data+bss+brk),
  211. * ramdisk, zero_page, command line could be above 4G.
  212. * We depend on an identity mapped page table being provided
  213. * that maps our entire kernel(text+data+bss+brk), zero page
  214. * and command line.
  215. */
  216. #ifdef CONFIG_EFI_STUB
  217. /*
  218. * The entry point for the PE/COFF executable is efi_pe_entry, so
  219. * only legacy boot loaders will execute this jmp.
  220. */
  221. jmp preferred_addr
  222. ENTRY(efi_pe_entry)
  223. movq %rcx, efi64_config(%rip) /* Handle */
  224. movq %rdx, efi64_config+8(%rip) /* EFI System table pointer */
  225. leaq efi64_config(%rip), %rax
  226. movq %rax, efi_config(%rip)
  227. call 1f
  228. 1: popq %rbp
  229. subq $1b, %rbp
  230. /*
  231. * Relocate efi_config->call().
  232. */
  233. addq %rbp, efi64_config+32(%rip)
  234. movq %rax, %rdi
  235. call make_boot_params
  236. cmpq $0,%rax
  237. je fail
  238. mov %rax, %rsi
  239. leaq startup_32(%rip), %rax
  240. movl %eax, BP_code32_start(%rsi)
  241. jmp 2f /* Skip the relocation */
  242. handover_entry:
  243. call 1f
  244. 1: popq %rbp
  245. subq $1b, %rbp
  246. /*
  247. * Relocate efi_config->call().
  248. */
  249. movq efi_config(%rip), %rax
  250. addq %rbp, 32(%rax)
  251. 2:
  252. movq efi_config(%rip), %rdi
  253. call efi_main
  254. movq %rax,%rsi
  255. cmpq $0,%rax
  256. jne 2f
  257. fail:
  258. /* EFI init failed, so hang. */
  259. hlt
  260. jmp fail
  261. 2:
  262. movl BP_code32_start(%esi), %eax
  263. leaq preferred_addr(%rax), %rax
  264. jmp *%rax
  265. preferred_addr:
  266. #endif
  267. /* Setup data segments. */
  268. xorl %eax, %eax
  269. movl %eax, %ds
  270. movl %eax, %es
  271. movl %eax, %ss
  272. movl %eax, %fs
  273. movl %eax, %gs
  274. /*
  275. * Compute the decompressed kernel start address. It is where
  276. * we were loaded at aligned to a 2M boundary. %rbp contains the
  277. * decompressed kernel start address.
  278. *
  279. * If it is a relocatable kernel then decompress and run the kernel
  280. * from load address aligned to 2MB addr, otherwise decompress and
  281. * run the kernel from LOAD_PHYSICAL_ADDR
  282. *
  283. * We cannot rely on the calculation done in 32-bit mode, since we
  284. * may have been invoked via the 64-bit entry point.
  285. */
  286. /* Start with the delta to where the kernel will run at. */
  287. #ifdef CONFIG_RELOCATABLE
  288. leaq startup_32(%rip) /* - $startup_32 */, %rbp
  289. movl BP_kernel_alignment(%rsi), %eax
  290. decl %eax
  291. addq %rax, %rbp
  292. notq %rax
  293. andq %rax, %rbp
  294. cmpq $LOAD_PHYSICAL_ADDR, %rbp
  295. jge 1f
  296. #endif
  297. movq $LOAD_PHYSICAL_ADDR, %rbp
  298. 1:
  299. /* Target address to relocate to for decompression */
  300. movl BP_init_size(%rsi), %ebx
  301. subl $_end, %ebx
  302. addq %rbp, %rbx
  303. /* Set up the stack */
  304. leaq boot_stack_end(%rbx), %rsp
  305. /* Zero EFLAGS */
  306. pushq $0
  307. popfq
  308. /*
  309. * Copy the compressed kernel to the end of our buffer
  310. * where decompression in place becomes safe.
  311. */
  312. pushq %rsi
  313. leaq (_bss-8)(%rip), %rsi
  314. leaq (_bss-8)(%rbx), %rdi
  315. movq $_bss /* - $startup_32 */, %rcx
  316. shrq $3, %rcx
  317. std
  318. rep movsq
  319. cld
  320. popq %rsi
  321. /*
  322. * Jump to the relocated address.
  323. */
  324. leaq relocated(%rbx), %rax
  325. jmp *%rax
  326. #ifdef CONFIG_EFI_STUB
  327. .org 0x390
  328. ENTRY(efi64_stub_entry)
  329. movq %rdi, efi64_config(%rip) /* Handle */
  330. movq %rsi, efi64_config+8(%rip) /* EFI System table pointer */
  331. leaq efi64_config(%rip), %rax
  332. movq %rax, efi_config(%rip)
  333. movq %rdx, %rsi
  334. jmp handover_entry
  335. ENDPROC(efi64_stub_entry)
  336. #endif
  337. .text
  338. relocated:
  339. /*
  340. * Clear BSS (stack is currently empty)
  341. */
  342. xorl %eax, %eax
  343. leaq _bss(%rip), %rdi
  344. leaq _ebss(%rip), %rcx
  345. subq %rdi, %rcx
  346. shrq $3, %rcx
  347. rep stosq
  348. /*
  349. * Adjust our own GOT
  350. */
  351. leaq _got(%rip), %rdx
  352. leaq _egot(%rip), %rcx
  353. 1:
  354. cmpq %rcx, %rdx
  355. jae 2f
  356. addq %rbx, (%rdx)
  357. addq $8, %rdx
  358. jmp 1b
  359. 2:
  360. /*
  361. * Do the extraction, and jump to the new kernel..
  362. */
  363. pushq %rsi /* Save the real mode argument */
  364. movq %rsi, %rdi /* real mode address */
  365. leaq boot_heap(%rip), %rsi /* malloc area for uncompression */
  366. leaq input_data(%rip), %rdx /* input_data */
  367. movl $z_input_len, %ecx /* input_len */
  368. movq %rbp, %r8 /* output target address */
  369. movq $z_output_len, %r9 /* decompressed length, end of relocs */
  370. call extract_kernel /* returns kernel location in %rax */
  371. popq %rsi
  372. /*
  373. * Jump to the decompressed kernel.
  374. */
  375. jmp *%rax
  376. .code32
  377. no_longmode:
  378. /* This isn't an x86-64 CPU so hang */
  379. 1:
  380. hlt
  381. jmp 1b
  382. #include "../../kernel/verify_cpu.S"
  383. .data
  384. gdt:
  385. .word gdt_end - gdt
  386. .long gdt
  387. .word 0
  388. .quad 0x0000000000000000 /* NULL descriptor */
  389. .quad 0x00af9a000000ffff /* __KERNEL_CS */
  390. .quad 0x00cf92000000ffff /* __KERNEL_DS */
  391. .quad 0x0080890000000000 /* TS descriptor */
  392. .quad 0x0000000000000000 /* TS continued */
  393. gdt_end:
  394. #ifdef CONFIG_EFI_STUB
  395. efi_config:
  396. .quad 0
  397. #ifdef CONFIG_EFI_MIXED
  398. .global efi32_config
  399. efi32_config:
  400. .fill 4,8,0
  401. .quad efi64_thunk
  402. .byte 0
  403. #endif
  404. .global efi64_config
  405. efi64_config:
  406. .fill 4,8,0
  407. .quad efi_call
  408. .byte 1
  409. #endif /* CONFIG_EFI_STUB */
  410. /*
  411. * Stack and heap for uncompression
  412. */
  413. .bss
  414. .balign 4
  415. boot_heap:
  416. .fill BOOT_HEAP_SIZE, 1, 0
  417. boot_stack:
  418. .fill BOOT_STACK_SIZE, 1, 0
  419. boot_stack_end:
  420. /*
  421. * Space for page tables (not in .bss so not zeroed)
  422. */
  423. .section ".pgtable","a",@nobits
  424. .balign 4096
  425. pgtable:
  426. .fill BOOT_PGT_SIZE, 1, 0