relocate_kernel_64.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * relocate_kernel.S - put the kernel image in place to boot
  3. * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
  4. *
  5. * This source code is licensed under the GNU General Public License,
  6. * Version 2. See the file COPYING for more details.
  7. */
  8. #include <linux/linkage.h>
  9. #include <asm/page_types.h>
  10. #include <asm/kexec.h>
  11. #include <asm/processor-flags.h>
  12. #include <asm/pgtable_types.h>
  13. /*
  14. * Must be relocatable PIC code callable as a C function
  15. */
  16. #define PTR(x) (x << 3)
  17. #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  18. .text
  19. .align PAGE_SIZE
  20. .code64
  21. .globl relocate_kernel
  22. relocate_kernel:
  23. /*
  24. * %rdi indirection_page
  25. * %rsi page_list
  26. * %rdx start address
  27. */
  28. /* zero out flags, and disable interrupts */
  29. pushq $0
  30. popfq
  31. /*
  32. * get physical address of control page now
  33. * this is impossible after page table switch
  34. */
  35. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  36. /* get physical address of page table now too */
  37. movq PTR(PA_TABLE_PAGE)(%rsi), %rcx
  38. /* Switch to the identity mapped page tables */
  39. movq %rcx, %cr3
  40. /* setup a new stack at the end of the physical control page */
  41. lea PAGE_SIZE(%r8), %rsp
  42. /* jump to identity mapped page */
  43. addq $(identity_mapped - relocate_kernel), %r8
  44. pushq %r8
  45. ret
  46. identity_mapped:
  47. /* store the start address on the stack */
  48. pushq %rdx
  49. /*
  50. * Set cr0 to a known state:
  51. * - Paging enabled
  52. * - Alignment check disabled
  53. * - Write protect disabled
  54. * - No task switch
  55. * - Don't do FP software emulation.
  56. * - Proctected mode enabled
  57. */
  58. movq %cr0, %rax
  59. andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
  60. orl $(X86_CR0_PG | X86_CR0_PE), %eax
  61. movq %rax, %cr0
  62. /*
  63. * Set cr4 to a known state:
  64. * - physical address extension enabled
  65. */
  66. movq $X86_CR4_PAE, %rax
  67. movq %rax, %cr4
  68. jmp 1f
  69. 1:
  70. /* Flush the TLB (needed?) */
  71. movq %rcx, %cr3
  72. /* Do the copies */
  73. movq %rdi, %rcx /* Put the page_list in %rcx */
  74. xorq %rdi, %rdi
  75. xorq %rsi, %rsi
  76. jmp 1f
  77. 0: /* top, read another word for the indirection page */
  78. movq (%rbx), %rcx
  79. addq $8, %rbx
  80. 1:
  81. testq $0x1, %rcx /* is it a destination page? */
  82. jz 2f
  83. movq %rcx, %rdi
  84. andq $0xfffffffffffff000, %rdi
  85. jmp 0b
  86. 2:
  87. testq $0x2, %rcx /* is it an indirection page? */
  88. jz 2f
  89. movq %rcx, %rbx
  90. andq $0xfffffffffffff000, %rbx
  91. jmp 0b
  92. 2:
  93. testq $0x4, %rcx /* is it the done indicator? */
  94. jz 2f
  95. jmp 3f
  96. 2:
  97. testq $0x8, %rcx /* is it the source indicator? */
  98. jz 0b /* Ignore it otherwise */
  99. movq %rcx, %rsi /* For ever source page do a copy */
  100. andq $0xfffffffffffff000, %rsi
  101. movq $512, %rcx
  102. rep ; movsq
  103. jmp 0b
  104. 3:
  105. /*
  106. * To be certain of avoiding problems with self-modifying code
  107. * I need to execute a serializing instruction here.
  108. * So I flush the TLB by reloading %cr3 here, it's handy,
  109. * and not processor dependent.
  110. */
  111. movq %cr3, %rax
  112. movq %rax, %cr3
  113. /*
  114. * set all of the registers to known values
  115. * leave %rsp alone
  116. */
  117. xorq %rax, %rax
  118. xorq %rbx, %rbx
  119. xorq %rcx, %rcx
  120. xorq %rdx, %rdx
  121. xorq %rsi, %rsi
  122. xorq %rdi, %rdi
  123. xorq %rbp, %rbp
  124. xorq %r8, %r8
  125. xorq %r9, %r9
  126. xorq %r10, %r9
  127. xorq %r11, %r11
  128. xorq %r12, %r12
  129. xorq %r13, %r13
  130. xorq %r14, %r14
  131. xorq %r15, %r15
  132. ret