test_emulate_step_exec_instr.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Non-emulated single-stepping support (currently limited to basic integer
  4. * computations) used to validate the instruction emulation infrastructure.
  5. *
  6. * Copyright (C) 2019 IBM Corporation
  7. */
  8. #include <asm/asm-offsets.h>
  9. #include <asm/ppc_asm.h>
  10. #include <asm/code-patching-asm.h>
  11. #include <linux/errno.h>
  12. /* int exec_instr(struct pt_regs *regs) */
  13. _GLOBAL(exec_instr)
  14. /*
  15. * Stack frame layout (INT_FRAME_SIZE bytes)
  16. * In-memory pt_regs (SP + STACK_INT_FRAME_REGS)
  17. * Scratch space (SP + 8)
  18. * Back chain (SP + 0)
  19. */
  20. /*
  21. * Allocate a new stack frame with enough space to hold the register
  22. * states in an in-memory pt_regs and also create the back chain to
  23. * the caller's stack frame.
  24. */
  25. stdu r1, -INT_FRAME_SIZE(r1)
  26. /*
  27. * Save non-volatile GPRs on stack. This includes TOC pointer (GPR2)
  28. * and local variables (GPR14 to GPR31). The register for the pt_regs
  29. * parameter (GPR3) is saved additionally to ensure that the resulting
  30. * register state can still be saved even if GPR3 gets overwritten
  31. * when loading the initial register state for the test instruction.
  32. * The stack pointer (GPR1) and the thread pointer (GPR13) are not
  33. * saved as these should not be modified anyway.
  34. */
  35. SAVE_GPRS(2, 3, r1)
  36. SAVE_NVGPRS(r1)
  37. /*
  38. * Save LR on stack to ensure that the return address is available
  39. * even if it gets overwritten by the test instruction.
  40. */
  41. mflr r0
  42. std r0, _LINK(r1)
  43. /*
  44. * Save CR on stack. For simplicity, the entire register is saved
  45. * even though only fields 2 to 4 are non-volatile.
  46. */
  47. mfcr r0
  48. std r0, _CCR(r1)
  49. /*
  50. * Load register state for the test instruction without touching the
  51. * critical non-volatile registers. The register state is passed as a
  52. * pointer to a pt_regs instance.
  53. */
  54. subi r31, r3, GPR0
  55. /* Load LR from pt_regs */
  56. ld r0, _LINK(r31)
  57. mtlr r0
  58. /* Load CR from pt_regs */
  59. ld r0, _CCR(r31)
  60. mtcr r0
  61. /* Load XER from pt_regs */
  62. ld r0, _XER(r31)
  63. mtxer r0
  64. /* Load GPRs from pt_regs */
  65. REST_GPR(0, r31)
  66. REST_GPRS(2, 12, r31)
  67. REST_NVGPRS(r31)
  68. /* Placeholder for the test instruction */
  69. .balign 64
  70. 1: nop
  71. nop
  72. patch_site 1b patch__exec_instr
  73. /*
  74. * Since GPR3 is overwritten, temporarily restore it back to its
  75. * original state, i.e. the pointer to pt_regs, to ensure that the
  76. * resulting register state can be saved. Before doing this, a copy
  77. * of it is created in the scratch space which is used later on to
  78. * save it to pt_regs.
  79. */
  80. std r3, 8(r1)
  81. REST_GPR(3, r1)
  82. /* Save resulting GPR state to pt_regs */
  83. subi r3, r3, GPR0
  84. SAVE_GPR(0, r3)
  85. SAVE_GPR(2, r3)
  86. SAVE_GPRS(4, 12, r3)
  87. SAVE_NVGPRS(r3)
  88. /* Save resulting LR to pt_regs */
  89. mflr r0
  90. std r0, _LINK(r3)
  91. /* Save resulting CR to pt_regs */
  92. mfcr r0
  93. std r0, _CCR(r3)
  94. /* Save resulting XER to pt_regs */
  95. mfxer r0
  96. std r0, _XER(r3)
  97. /* Restore resulting GPR3 from scratch space and save it to pt_regs */
  98. ld r0, 8(r1)
  99. std r0, GPR3(r3)
  100. /* Set return value to denote execution success */
  101. li r3, 0
  102. /* Continue */
  103. b 3f
  104. /* Set return value to denote execution failure */
  105. 2: li r3, -EFAULT
  106. /* Restore the non-volatile GPRs from stack */
  107. 3: REST_GPR(2, r1)
  108. REST_NVGPRS(r1)
  109. /* Restore LR from stack to be able to return */
  110. ld r0, _LINK(r1)
  111. mtlr r0
  112. /* Restore CR from stack */
  113. ld r0, _CCR(r1)
  114. mtcr r0
  115. /* Tear down stack frame */
  116. addi r1, r1, INT_FRAME_SIZE
  117. /* Return */
  118. blr
  119. /* Setup exception table */
  120. EX_TABLE(1b, 2b)
  121. _ASM_NOKPROBE_SYMBOL(exec_instr)