hibernate_asm_64.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Hibernation support for x86-64
  4. *
  5. * Copyright 2007 Rafael J. Wysocki <[email protected]>
  6. * Copyright 2005 Andi Kleen <[email protected]>
  7. * Copyright 2004 Pavel Machek <[email protected]>
  8. *
  9. * swsusp_arch_resume must not use any stack or any nonlocal variables while
  10. * copying pages:
  11. *
  12. * Its rewriting one kernel image with another. What is stack in "old"
  13. * image could very well be data page in "new" image, and overwriting
  14. * your own stack under you is bad idea.
  15. */
  16. .text
  17. #include <linux/linkage.h>
  18. #include <asm/segment.h>
  19. #include <asm/page_types.h>
  20. #include <asm/asm-offsets.h>
  21. #include <asm/processor-flags.h>
  22. #include <asm/frame.h>
  23. #include <asm/nospec-branch.h>
  24. /* code below belongs to the image kernel */
  25. .align PAGE_SIZE
  26. SYM_FUNC_START(restore_registers)
  27. /* go back to the original page tables */
  28. movq %r9, %cr3
  29. /* Flush TLB, including "global" things (vmalloc) */
  30. movq mmu_cr4_features(%rip), %rax
  31. movq %rax, %rdx
  32. andq $~(X86_CR4_PGE), %rdx
  33. movq %rdx, %cr4; # turn off PGE
  34. movq %cr3, %rcx; # flush TLB
  35. movq %rcx, %cr3
  36. movq %rax, %cr4; # turn PGE back on
  37. /* We don't restore %rax, it must be 0 anyway */
  38. movq $saved_context, %rax
  39. movq pt_regs_sp(%rax), %rsp
  40. movq pt_regs_bp(%rax), %rbp
  41. movq pt_regs_si(%rax), %rsi
  42. movq pt_regs_di(%rax), %rdi
  43. movq pt_regs_bx(%rax), %rbx
  44. movq pt_regs_cx(%rax), %rcx
  45. movq pt_regs_dx(%rax), %rdx
  46. movq pt_regs_r8(%rax), %r8
  47. movq pt_regs_r9(%rax), %r9
  48. movq pt_regs_r10(%rax), %r10
  49. movq pt_regs_r11(%rax), %r11
  50. movq pt_regs_r12(%rax), %r12
  51. movq pt_regs_r13(%rax), %r13
  52. movq pt_regs_r14(%rax), %r14
  53. movq pt_regs_r15(%rax), %r15
  54. pushq pt_regs_flags(%rax)
  55. popfq
  56. /* Saved in save_processor_state. */
  57. lgdt saved_context_gdt_desc(%rax)
  58. xorl %eax, %eax
  59. /* tell the hibernation core that we've just restored the memory */
  60. movq %rax, in_suspend(%rip)
  61. RET
  62. SYM_FUNC_END(restore_registers)
  63. SYM_FUNC_START(swsusp_arch_suspend)
  64. movq $saved_context, %rax
  65. movq %rsp, pt_regs_sp(%rax)
  66. movq %rbp, pt_regs_bp(%rax)
  67. movq %rsi, pt_regs_si(%rax)
  68. movq %rdi, pt_regs_di(%rax)
  69. movq %rbx, pt_regs_bx(%rax)
  70. movq %rcx, pt_regs_cx(%rax)
  71. movq %rdx, pt_regs_dx(%rax)
  72. movq %r8, pt_regs_r8(%rax)
  73. movq %r9, pt_regs_r9(%rax)
  74. movq %r10, pt_regs_r10(%rax)
  75. movq %r11, pt_regs_r11(%rax)
  76. movq %r12, pt_regs_r12(%rax)
  77. movq %r13, pt_regs_r13(%rax)
  78. movq %r14, pt_regs_r14(%rax)
  79. movq %r15, pt_regs_r15(%rax)
  80. pushfq
  81. popq pt_regs_flags(%rax)
  82. /* save cr3 */
  83. movq %cr3, %rax
  84. movq %rax, restore_cr3(%rip)
  85. FRAME_BEGIN
  86. call swsusp_save
  87. FRAME_END
  88. RET
  89. SYM_FUNC_END(swsusp_arch_suspend)
  90. SYM_FUNC_START(restore_image)
  91. /* prepare to jump to the image kernel */
  92. movq restore_jump_address(%rip), %r8
  93. movq restore_cr3(%rip), %r9
  94. /* prepare to switch to temporary page tables */
  95. movq temp_pgt(%rip), %rax
  96. movq mmu_cr4_features(%rip), %rbx
  97. /* prepare to copy image data to their original locations */
  98. movq restore_pblist(%rip), %rdx
  99. /* jump to relocated restore code */
  100. movq relocated_restore_code(%rip), %rcx
  101. ANNOTATE_RETPOLINE_SAFE
  102. jmpq *%rcx
  103. SYM_FUNC_END(restore_image)
  104. /* code below has been relocated to a safe page */
  105. SYM_FUNC_START(core_restore_code)
  106. /* switch to temporary page tables */
  107. movq %rax, %cr3
  108. /* flush TLB */
  109. movq %rbx, %rcx
  110. andq $~(X86_CR4_PGE), %rcx
  111. movq %rcx, %cr4; # turn off PGE
  112. movq %cr3, %rcx; # flush TLB
  113. movq %rcx, %cr3;
  114. movq %rbx, %cr4; # turn PGE back on
  115. .Lloop:
  116. testq %rdx, %rdx
  117. jz .Ldone
  118. /* get addresses from the pbe and copy the page */
  119. movq pbe_address(%rdx), %rsi
  120. movq pbe_orig_address(%rdx), %rdi
  121. movq $(PAGE_SIZE >> 3), %rcx
  122. rep
  123. movsq
  124. /* progress to the next pbe */
  125. movq pbe_next(%rdx), %rdx
  126. jmp .Lloop
  127. .Ldone:
  128. /* jump to the restore_registers address from the image header */
  129. ANNOTATE_RETPOLINE_SAFE
  130. jmpq *%r8
  131. SYM_FUNC_END(core_restore_code)