trampoline_64.S 3.6 KB

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