relocate_kernel_64.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /* %rdi indirection_page
  24. * %rsi page_list
  25. * %rdx start address
  26. */
  27. /* zero out flags, and disable interrupts */
  28. pushq $0
  29. popfq
  30. /* get physical address of control page now */
  31. /* this is impossible after page table switch */
  32. movq PTR(PA_CONTROL_PAGE)(%rsi), %r8
  33. /* get physical address of page table now too */
  34. movq PTR(PA_TABLE_PAGE)(%rsi), %rcx
  35. /* Switch to the identity mapped page tables */
  36. movq %rcx, %cr3
  37. /* setup a new stack at the end of the physical control page */
  38. lea PAGE_SIZE(%r8), %rsp
  39. /* jump to identity mapped page */
  40. addq $(identity_mapped - relocate_kernel), %r8
  41. pushq %r8
  42. ret
  43. identity_mapped:
  44. /* store the start address on the stack */
  45. pushq %rdx
  46. /* Set cr0 to a known state:
  47. * - Paging enabled
  48. * - Alignment check disabled
  49. * - Write protect disabled
  50. * - No task switch
  51. * - Don't do FP software emulation.
  52. * - Proctected mode enabled
  53. */
  54. movq %cr0, %rax
  55. andq $~(X86_CR0_AM | X86_CR0_WP | X86_CR0_TS | X86_CR0_EM), %rax
  56. orl $(X86_CR0_PG | X86_CR0_PE), %eax
  57. movq %rax, %cr0
  58. /* Set cr4 to a known state:
  59. * - physical address extension enabled
  60. */
  61. movq $X86_CR4_PAE, %rax
  62. movq %rax, %cr4
  63. jmp 1f
  64. 1:
  65. /* Flush the TLB (needed?) */
  66. movq %rcx, %cr3
  67. /* Do the copies */
  68. movq %rdi, %rcx /* Put the page_list in %rcx */
  69. xorq %rdi, %rdi
  70. xorq %rsi, %rsi
  71. jmp 1f
  72. 0: /* top, read another word for the indirection page */
  73. movq (%rbx), %rcx
  74. addq $8, %rbx
  75. 1:
  76. testq $0x1, %rcx /* is it a destination page? */
  77. jz 2f
  78. movq %rcx, %rdi
  79. andq $0xfffffffffffff000, %rdi
  80. jmp 0b
  81. 2:
  82. testq $0x2, %rcx /* is it an indirection page? */
  83. jz 2f
  84. movq %rcx, %rbx
  85. andq $0xfffffffffffff000, %rbx
  86. jmp 0b
  87. 2:
  88. testq $0x4, %rcx /* is it the done indicator? */
  89. jz 2f
  90. jmp 3f
  91. 2:
  92. testq $0x8, %rcx /* is it the source indicator? */
  93. jz 0b /* Ignore it otherwise */
  94. movq %rcx, %rsi /* For ever source page do a copy */
  95. andq $0xfffffffffffff000, %rsi
  96. movq $512, %rcx
  97. rep ; movsq
  98. jmp 0b
  99. 3:
  100. /* To be certain of avoiding problems with self-modifying code
  101. * I need to execute a serializing instruction here.
  102. * So I flush the TLB by reloading %cr3 here, it's handy,
  103. * and not processor dependent.
  104. */
  105. movq %cr3, %rax
  106. movq %rax, %cr3
  107. /* set all of the registers to known values */
  108. /* leave %rsp alone */
  109. xorq %rax, %rax
  110. xorq %rbx, %rbx
  111. xorq %rcx, %rcx
  112. xorq %rdx, %rdx
  113. xorq %rsi, %rsi
  114. xorq %rdi, %rdi
  115. xorq %rbp, %rbp
  116. xorq %r8, %r8
  117. xorq %r9, %r9
  118. xorq %r10, %r9
  119. xorq %r11, %r11
  120. xorq %r12, %r12
  121. xorq %r13, %r13
  122. xorq %r14, %r14
  123. xorq %r15, %r15
  124. ret