![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 /* Copyright (C) 2019 ARM Limited */ 0003 0004 #ifndef __TEST_SIGNALS_UTILS_H__ 0005 #define __TEST_SIGNALS_UTILS_H__ 0006 0007 #include <assert.h> 0008 #include <stdio.h> 0009 #include <string.h> 0010 0011 #include "test_signals.h" 0012 0013 int test_init(struct tdescr *td); 0014 int test_setup(struct tdescr *td); 0015 void test_cleanup(struct tdescr *td); 0016 int test_run(struct tdescr *td); 0017 void test_result(struct tdescr *td); 0018 0019 static inline bool feats_ok(struct tdescr *td) 0020 { 0021 if (td->feats_incompatible & td->feats_supported) 0022 return false; 0023 return (td->feats_required & td->feats_supported) == td->feats_required; 0024 } 0025 0026 /* 0027 * Obtaining a valid and full-blown ucontext_t from userspace is tricky: 0028 * libc getcontext does() not save all the regs and messes with some of 0029 * them (pstate value in particular is not reliable). 0030 * 0031 * Here we use a service signal to grab the ucontext_t from inside a 0032 * dedicated signal handler, since there, it is populated by Kernel 0033 * itself in setup_sigframe(). The grabbed context is then stored and 0034 * made available in td->live_uc. 0035 * 0036 * As service-signal is used a SIGTRAP induced by a 'brk' instruction, 0037 * because here we have to avoid syscalls to trigger the signal since 0038 * they would cause any SVE sigframe content (if any) to be removed. 0039 * 0040 * Anyway this function really serves a dual purpose: 0041 * 0042 * 1. grab a valid sigcontext into td->live_uc for result analysis: in 0043 * such case it returns 1. 0044 * 0045 * 2. detect if, somehow, a previously grabbed live_uc context has been 0046 * used actively with a sigreturn: in such a case the execution would have 0047 * magically resumed in the middle of this function itself (seen_already==1): 0048 * in such a case return 0, since in fact we have not just simply grabbed 0049 * the context. 0050 * 0051 * This latter case is useful to detect when a fake_sigreturn test-case has 0052 * unexpectedly survived without hitting a SEGV. 0053 * 0054 * Note that the case of runtime dynamically sized sigframes (like in SVE 0055 * context) is still NOT addressed: sigframe size is supposed to be fixed 0056 * at sizeof(ucontext_t). 0057 */ 0058 static __always_inline bool get_current_context(struct tdescr *td, 0059 ucontext_t *dest_uc) 0060 { 0061 static volatile bool seen_already; 0062 0063 assert(td && dest_uc); 0064 /* it's a genuine invocation..reinit */ 0065 seen_already = 0; 0066 td->live_uc_valid = 0; 0067 td->live_sz = sizeof(*dest_uc); 0068 memset(dest_uc, 0x00, td->live_sz); 0069 td->live_uc = dest_uc; 0070 /* 0071 * Grab ucontext_t triggering a SIGTRAP. 0072 * 0073 * Note that: 0074 * - live_uc_valid is declared volatile sig_atomic_t in 0075 * struct tdescr since it will be changed inside the 0076 * sig_copyctx handler 0077 * - the additional 'memory' clobber is there to avoid possible 0078 * compiler's assumption on live_uc_valid and the content 0079 * pointed by dest_uc, which are all changed inside the signal 0080 * handler 0081 * - BRK causes a debug exception which is handled by the Kernel 0082 * and finally causes the SIGTRAP signal to be delivered to this 0083 * test thread. Since such delivery happens on the ret_to_user() 0084 * /do_notify_resume() debug exception return-path, we are sure 0085 * that the registered SIGTRAP handler has been run to completion 0086 * before the execution path is restored here: as a consequence 0087 * we can be sure that the volatile sig_atomic_t live_uc_valid 0088 * carries a meaningful result. Being in a single thread context 0089 * we'll also be sure that any access to memory modified by the 0090 * handler (namely ucontext_t) will be visible once returned. 0091 * - note that since we are using a breakpoint instruction here 0092 * to cause a SIGTRAP, the ucontext_t grabbed from the signal 0093 * handler would naturally contain a PC pointing exactly to this 0094 * BRK line, which means that, on return from the signal handler, 0095 * or if we place the ucontext_t on the stack to fake a sigreturn, 0096 * we'll end up in an infinite loop of BRK-SIGTRAP-handler. 0097 * For this reason we take care to artificially move forward the 0098 * PC to the next instruction while inside the signal handler. 0099 */ 0100 asm volatile ("brk #666" 0101 : "+m" (*dest_uc) 0102 : 0103 : "memory"); 0104 0105 /* 0106 * If we get here with seen_already==1 it implies the td->live_uc 0107 * context has been used to get back here....this probably means 0108 * a test has failed to cause a SEGV...anyway live_uc does not 0109 * point to a just acquired copy of ucontext_t...so return 0 0110 */ 0111 if (seen_already) { 0112 fprintf(stdout, 0113 "Unexpected successful sigreturn detected: live_uc is stale !\n"); 0114 return 0; 0115 } 0116 seen_already = 1; 0117 0118 return td->live_uc_valid; 0119 } 0120 0121 int fake_sigreturn(void *sigframe, size_t sz, int misalign_bytes); 0122 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |