trampoline_64.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. *
  4. * Trampoline.S Derived from Setup.S by Linus Torvalds
  5. *
  6. * 4 Jan 1997 Michael Chastain: changed to gnu as.
  7. * 15 Sept 2005 Eric Biederman: 64bit PIC support
  8. *
  9. * Entry: CS:IP point to the start of our code, we are
  10. * in real mode with no stack, but the rest of the
  11. * trampoline page to make our stack and everything else
  12. * is a mystery.
  13. *
  14. * On entry to trampoline_start, the processor is in real mode
  15. * with 16-bit addressing and 16-bit data. CS has some value
  16. * and IP is zero. Thus, data addresses need to be absolute
  17. * (no relocation) and are taken with regard to r_base.
  18. *
  19. * With the addition of trampoline_level4_pgt this code can
  20. * now enter a 64bit kernel that lives at arbitrary 64bit
  21. * physical addresses.
  22. *
  23. * If you work on this file, check the object module with objdump
  24. * --full-contents --reloc to make sure there are no relocation
  25. * entries.
  26. */
  27. #include <linux/linkage.h>
  28. #include <asm/pgtable_types.h>
  29. #include <asm/page_types.h>
  30. #include <asm/msr.h>
  31. #include <asm/segment.h>
  32. #include <asm/processor-flags.h>
  33. #include <asm/realmode.h>
  34. #include "realmode.h"
  35. .text
  36. .code16
  37. .balign PAGE_SIZE
  38. ENTRY(trampoline_start)
  39. cli # We should be safe anyway
  40. wbinvd
  41. LJMPW_RM(1f)
  42. 1:
  43. mov %cs, %ax # Code and data in the same place
  44. mov %ax, %ds
  45. mov %ax, %es
  46. mov %ax, %ss
  47. movl $0xA5A5A5A5, trampoline_status
  48. # write marker for master knows we're running
  49. # Setup stack
  50. movl $rm_stack_end, %esp
  51. call verify_cpu # Verify the cpu supports long mode
  52. testl %eax, %eax # Check for return code
  53. jnz no_longmode
  54. /*
  55. * GDT tables in non default location kernel can be beyond 16MB and
  56. * lgdt will not be able to load the address as in real mode default
  57. * operand size is 16bit. Use lgdtl instead to force operand size
  58. * to 32 bit.
  59. */
  60. lidtl tr_idt # load idt with 0, 0
  61. lgdtl tr_gdt # load gdt with whatever is appropriate
  62. movw $__KERNEL_DS, %dx # Data segment descriptor
  63. # Enable protected mode
  64. movl $X86_CR0_PE, %eax # protected mode (PE) bit
  65. movl %eax, %cr0 # into protected mode
  66. # flush prefetch and jump to startup_32
  67. ljmpl $__KERNEL32_CS, $pa_startup_32
  68. no_longmode:
  69. hlt
  70. jmp no_longmode
  71. #include "../kernel/verify_cpu.S"
  72. .section ".text32","ax"
  73. .code32
  74. .balign 4
  75. ENTRY(startup_32)
  76. movl %edx, %ss
  77. addl $pa_real_mode_base, %esp
  78. movl %edx, %ds
  79. movl %edx, %es
  80. movl %edx, %fs
  81. movl %edx, %gs
  82. /*
  83. * Check for memory encryption support. This is a safety net in
  84. * case BIOS hasn't done the necessary step of setting the bit in
  85. * the MSR for this AP. If SME is active and we've gotten this far
  86. * then it is safe for us to set the MSR bit and continue. If we
  87. * don't we'll eventually crash trying to execute encrypted
  88. * instructions.
  89. */
  90. btl $TH_FLAGS_SME_ACTIVE_BIT, pa_tr_flags
  91. jnc .Ldone
  92. movl $MSR_K8_SYSCFG, %ecx
  93. rdmsr
  94. bts $MSR_K8_SYSCFG_MEM_ENCRYPT_BIT, %eax
  95. jc .Ldone
  96. /*
  97. * Memory encryption is enabled but the SME enable bit for this
  98. * CPU has has not been set. It is safe to set it, so do so.
  99. */
  100. wrmsr
  101. .Ldone:
  102. movl pa_tr_cr4, %eax
  103. movl %eax, %cr4 # Enable PAE mode
  104. # Setup trampoline 4 level pagetables
  105. movl $pa_trampoline_pgd, %eax
  106. movl %eax, %cr3
  107. # Set up EFER
  108. movl pa_tr_efer, %eax
  109. movl pa_tr_efer + 4, %edx
  110. movl $MSR_EFER, %ecx
  111. wrmsr
  112. # Enable paging and in turn activate Long Mode
  113. movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax
  114. movl %eax, %cr0
  115. /*
  116. * At this point we're in long mode but in 32bit compatibility mode
  117. * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
  118. * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
  119. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  120. */
  121. ljmpl $__KERNEL_CS, $pa_startup_64
  122. .section ".text64","ax"
  123. .code64
  124. .balign 4
  125. ENTRY(startup_64)
  126. # Now jump into the kernel using virtual addresses
  127. jmpq *tr_start(%rip)
  128. .section ".rodata","a"
  129. # Duplicate the global descriptor table
  130. # so the kernel can live anywhere
  131. .balign 16
  132. .globl tr_gdt
  133. tr_gdt:
  134. .short tr_gdt_end - tr_gdt - 1 # gdt limit
  135. .long pa_tr_gdt
  136. .short 0
  137. .quad 0x00cf9b000000ffff # __KERNEL32_CS
  138. .quad 0x00af9b000000ffff # __KERNEL_CS
  139. .quad 0x00cf93000000ffff # __KERNEL_DS
  140. tr_gdt_end:
  141. .bss
  142. .balign PAGE_SIZE
  143. GLOBAL(trampoline_pgd) .space PAGE_SIZE
  144. .balign 8
  145. GLOBAL(trampoline_header)
  146. tr_start: .space 8
  147. GLOBAL(tr_efer) .space 8
  148. GLOBAL(tr_cr4) .space 4
  149. GLOBAL(tr_flags) .space 4
  150. END(trampoline_header)
  151. #include "trampoline_common.S"