test_signals_utils.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2019 ARM Limited */
  3. #ifndef __TEST_SIGNALS_UTILS_H__
  4. #define __TEST_SIGNALS_UTILS_H__
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <linux/compiler.h>
  9. #include "test_signals.h"
  10. int test_init(struct tdescr *td);
  11. int test_setup(struct tdescr *td);
  12. void test_cleanup(struct tdescr *td);
  13. int test_run(struct tdescr *td);
  14. void test_result(struct tdescr *td);
  15. static inline bool feats_ok(struct tdescr *td)
  16. {
  17. if (td->feats_incompatible & td->feats_supported)
  18. return false;
  19. return (td->feats_required & td->feats_supported) == td->feats_required;
  20. }
  21. /*
  22. * Obtaining a valid and full-blown ucontext_t from userspace is tricky:
  23. * libc getcontext does() not save all the regs and messes with some of
  24. * them (pstate value in particular is not reliable).
  25. *
  26. * Here we use a service signal to grab the ucontext_t from inside a
  27. * dedicated signal handler, since there, it is populated by Kernel
  28. * itself in setup_sigframe(). The grabbed context is then stored and
  29. * made available in td->live_uc.
  30. *
  31. * As service-signal is used a SIGTRAP induced by a 'brk' instruction,
  32. * because here we have to avoid syscalls to trigger the signal since
  33. * they would cause any SVE sigframe content (if any) to be removed.
  34. *
  35. * Anyway this function really serves a dual purpose:
  36. *
  37. * 1. grab a valid sigcontext into td->live_uc for result analysis: in
  38. * such case it returns 1.
  39. *
  40. * 2. detect if, somehow, a previously grabbed live_uc context has been
  41. * used actively with a sigreturn: in such a case the execution would have
  42. * magically resumed in the middle of this function itself (seen_already==1):
  43. * in such a case return 0, since in fact we have not just simply grabbed
  44. * the context.
  45. *
  46. * This latter case is useful to detect when a fake_sigreturn test-case has
  47. * unexpectedly survived without hitting a SEGV.
  48. *
  49. * Note that the case of runtime dynamically sized sigframes (like in SVE
  50. * context) is still NOT addressed: sigframe size is supposed to be fixed
  51. * at sizeof(ucontext_t).
  52. */
  53. static __always_inline bool get_current_context(struct tdescr *td,
  54. ucontext_t *dest_uc,
  55. size_t dest_sz)
  56. {
  57. static volatile bool seen_already;
  58. int i;
  59. char *uc = (char *)dest_uc;
  60. assert(td && dest_uc);
  61. /* it's a genuine invocation..reinit */
  62. seen_already = 0;
  63. td->live_uc_valid = 0;
  64. td->live_sz = dest_sz;
  65. /*
  66. * This is a memset() but we don't want the compiler to
  67. * optimise it into either instructions or a library call
  68. * which might be incompatible with streaming mode.
  69. */
  70. for (i = 0; i < td->live_sz; i++) {
  71. uc[i] = 0;
  72. OPTIMIZER_HIDE_VAR(uc[0]);
  73. }
  74. td->live_uc = dest_uc;
  75. /*
  76. * Grab ucontext_t triggering a SIGTRAP.
  77. *
  78. * Note that:
  79. * - live_uc_valid is declared volatile sig_atomic_t in
  80. * struct tdescr since it will be changed inside the
  81. * sig_copyctx handler
  82. * - the additional 'memory' clobber is there to avoid possible
  83. * compiler's assumption on live_uc_valid and the content
  84. * pointed by dest_uc, which are all changed inside the signal
  85. * handler
  86. * - BRK causes a debug exception which is handled by the Kernel
  87. * and finally causes the SIGTRAP signal to be delivered to this
  88. * test thread. Since such delivery happens on the ret_to_user()
  89. * /do_notify_resume() debug exception return-path, we are sure
  90. * that the registered SIGTRAP handler has been run to completion
  91. * before the execution path is restored here: as a consequence
  92. * we can be sure that the volatile sig_atomic_t live_uc_valid
  93. * carries a meaningful result. Being in a single thread context
  94. * we'll also be sure that any access to memory modified by the
  95. * handler (namely ucontext_t) will be visible once returned.
  96. * - note that since we are using a breakpoint instruction here
  97. * to cause a SIGTRAP, the ucontext_t grabbed from the signal
  98. * handler would naturally contain a PC pointing exactly to this
  99. * BRK line, which means that, on return from the signal handler,
  100. * or if we place the ucontext_t on the stack to fake a sigreturn,
  101. * we'll end up in an infinite loop of BRK-SIGTRAP-handler.
  102. * For this reason we take care to artificially move forward the
  103. * PC to the next instruction while inside the signal handler.
  104. */
  105. asm volatile ("brk #666"
  106. : "+m" (*dest_uc)
  107. :
  108. : "memory");
  109. /*
  110. * If we were grabbing a streaming mode context then we may
  111. * have entered streaming mode behind the system's back and
  112. * libc or compiler generated code might decide to do
  113. * something invalid in streaming mode, or potentially even
  114. * the state of ZA. Issue a SMSTOP to exit both now we have
  115. * grabbed the state.
  116. */
  117. if (td->feats_supported & FEAT_SME)
  118. asm volatile("msr S0_3_C4_C6_3, xzr");
  119. /*
  120. * If we get here with seen_already==1 it implies the td->live_uc
  121. * context has been used to get back here....this probably means
  122. * a test has failed to cause a SEGV...anyway live_uc does not
  123. * point to a just acquired copy of ucontext_t...so return 0
  124. */
  125. if (seen_already) {
  126. fprintf(stdout,
  127. "Unexpected successful sigreturn detected: live_uc is stale !\n");
  128. return 0;
  129. }
  130. seen_already = 1;
  131. return td->live_uc_valid;
  132. }
  133. int fake_sigreturn(void *sigframe, size_t sz, int misalign_bytes);
  134. #endif