Path: blob/master/tools/testing/selftests/arm64/signal/test_signals_utils.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */1/* Copyright (C) 2019 ARM Limited */23#ifndef __TEST_SIGNALS_UTILS_H__4#define __TEST_SIGNALS_UTILS_H__56#include <assert.h>7#include <stdio.h>8#include <stdint.h>9#include <string.h>1011#include <linux/compiler.h>1213#include "test_signals.h"1415int test_init(struct tdescr *td);16int test_setup(struct tdescr *td);17void test_cleanup(struct tdescr *td);18int test_run(struct tdescr *td);19void test_result(struct tdescr *td);2021#ifndef __NR_prctl22#define __NR_prctl 16723#endif2425/*26* The prctl takes 1 argument but we need to ensure that the other27* values passed in registers to the syscall are zero since the kernel28* validates them.29*/30#define gcs_set_state(state) \31({ \32register long _num __asm__ ("x8") = __NR_prctl; \33register long _arg1 __asm__ ("x0") = PR_SET_SHADOW_STACK_STATUS; \34register long _arg2 __asm__ ("x1") = (long)(state); \35register long _arg3 __asm__ ("x2") = 0; \36register long _arg4 __asm__ ("x3") = 0; \37register long _arg5 __asm__ ("x4") = 0; \38\39__asm__ volatile ( \40"svc #0\n" \41: "=r"(_arg1) \42: "r"(_arg1), "r"(_arg2), \43"r"(_arg3), "r"(_arg4), \44"r"(_arg5), "r"(_num) \45: "memory", "cc" \46); \47_arg1; \48})4950static inline __attribute__((always_inline)) uint64_t get_gcspr_el0(void)51{52uint64_t val;5354asm volatile("mrs %0, S3_3_C2_C5_1" : "=r" (val));5556return val;57}5859static inline bool feats_ok(struct tdescr *td)60{61if (td->feats_incompatible & td->feats_supported)62return false;63return (td->feats_required & td->feats_supported) == td->feats_required;64}6566/*67* Obtaining a valid and full-blown ucontext_t from userspace is tricky:68* libc getcontext does() not save all the regs and messes with some of69* them (pstate value in particular is not reliable).70*71* Here we use a service signal to grab the ucontext_t from inside a72* dedicated signal handler, since there, it is populated by Kernel73* itself in setup_sigframe(). The grabbed context is then stored and74* made available in td->live_uc.75*76* As service-signal is used a SIGTRAP induced by a 'brk' instruction,77* because here we have to avoid syscalls to trigger the signal since78* they would cause any SVE sigframe content (if any) to be removed.79*80* Anyway this function really serves a dual purpose:81*82* 1. grab a valid sigcontext into td->live_uc for result analysis: in83* such case it returns 1.84*85* 2. detect if, somehow, a previously grabbed live_uc context has been86* used actively with a sigreturn: in such a case the execution would have87* magically resumed in the middle of this function itself (seen_already==1):88* in such a case return 0, since in fact we have not just simply grabbed89* the context.90*91* This latter case is useful to detect when a fake_sigreturn test-case has92* unexpectedly survived without hitting a SEGV.93*94* Note that the case of runtime dynamically sized sigframes (like in SVE95* context) is still NOT addressed: sigframe size is supposed to be fixed96* at sizeof(ucontext_t).97*/98static __always_inline bool get_current_context(struct tdescr *td,99ucontext_t *dest_uc,100size_t dest_sz)101{102static volatile bool seen_already;103int i;104char *uc = (char *)dest_uc;105106assert(td && dest_uc);107/* it's a genuine invocation..reinit */108seen_already = 0;109td->live_uc_valid = 0;110td->live_sz = dest_sz;111112/*113* This is a memset() but we don't want the compiler to114* optimise it into either instructions or a library call115* which might be incompatible with streaming mode.116*/117for (i = 0; i < td->live_sz; i++) {118uc[i] = 0;119OPTIMIZER_HIDE_VAR(uc[0]);120}121122td->live_uc = dest_uc;123/*124* Grab ucontext_t triggering a SIGTRAP.125*126* Note that:127* - live_uc_valid is declared volatile sig_atomic_t in128* struct tdescr since it will be changed inside the129* sig_copyctx handler130* - the additional 'memory' clobber is there to avoid possible131* compiler's assumption on live_uc_valid and the content132* pointed by dest_uc, which are all changed inside the signal133* handler134* - BRK causes a debug exception which is handled by the Kernel135* and finally causes the SIGTRAP signal to be delivered to this136* test thread. Since such delivery happens on the ret_to_user()137* /do_notify_resume() debug exception return-path, we are sure138* that the registered SIGTRAP handler has been run to completion139* before the execution path is restored here: as a consequence140* we can be sure that the volatile sig_atomic_t live_uc_valid141* carries a meaningful result. Being in a single thread context142* we'll also be sure that any access to memory modified by the143* handler (namely ucontext_t) will be visible once returned.144* - note that since we are using a breakpoint instruction here145* to cause a SIGTRAP, the ucontext_t grabbed from the signal146* handler would naturally contain a PC pointing exactly to this147* BRK line, which means that, on return from the signal handler,148* or if we place the ucontext_t on the stack to fake a sigreturn,149* we'll end up in an infinite loop of BRK-SIGTRAP-handler.150* For this reason we take care to artificially move forward the151* PC to the next instruction while inside the signal handler.152*/153asm volatile ("brk #666"154: "+m" (*dest_uc)155:156: "memory");157158/*159* If we were grabbing a streaming mode context then we may160* have entered streaming mode behind the system's back and161* libc or compiler generated code might decide to do162* something invalid in streaming mode, or potentially even163* the state of ZA. Issue a SMSTOP to exit both now we have164* grabbed the state.165*/166if (td->feats_supported & FEAT_SME)167asm volatile("msr S0_3_C4_C6_3, xzr");168169/*170* If we get here with seen_already==1 it implies the td->live_uc171* context has been used to get back here....this probably means172* a test has failed to cause a SEGV...anyway live_uc does not173* point to a just acquired copy of ucontext_t...so return 0174*/175if (seen_already) {176fprintf(stdout,177"Unexpected successful sigreturn detected: live_uc is stale !\n");178return 0;179}180seen_already = 1;181182return td->live_uc_valid;183}184185int fake_sigreturn(void *sigframe, size_t sz, int misalign_bytes);186#endif187188189