Path: blob/master/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c
26298 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2023 ARM Limited3*/45#include <signal.h>6#include <ucontext.h>7#include <sys/prctl.h>89#include "test_signals_utils.h"10#include "testcases.h"1112static union {13ucontext_t uc;14char buf[1024 * 64];15} context;1617static int gcs_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc)18{19size_t offset;20struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);21struct gcs_context *gcs;22unsigned long expected, gcspr;23uint64_t *u64_val;24int ret;2526ret = prctl(PR_GET_SHADOW_STACK_STATUS, &expected, 0, 0, 0);27if (ret != 0) {28fprintf(stderr, "Unable to query GCS status\n");29return 1;30}3132/* We expect a cap to be added to the GCS in the signal frame */33gcspr = get_gcspr_el0();34gcspr -= 8;35fprintf(stderr, "Expecting GCSPR_EL0 %lx\n", gcspr);3637if (!get_current_context(td, &context.uc, sizeof(context))) {38fprintf(stderr, "Failed getting context\n");39return 1;40}4142/* Ensure that the signal restore token was consumed */43u64_val = (uint64_t *)get_gcspr_el0() + 1;44if (*u64_val) {45fprintf(stderr, "GCS value at %p is %lx not 0\n",46u64_val, *u64_val);47return 1;48}4950fprintf(stderr, "Got context\n");5152head = get_header(head, GCS_MAGIC, GET_BUF_RESV_SIZE(context),53&offset);54if (!head) {55fprintf(stderr, "No GCS context\n");56return 1;57}5859gcs = (struct gcs_context *)head;6061/* Basic size validation is done in get_current_context() */6263if (gcs->features_enabled != expected) {64fprintf(stderr, "Features enabled %llx but expected %lx\n",65gcs->features_enabled, expected);66return 1;67}6869if (gcs->gcspr != gcspr) {70fprintf(stderr, "Got GCSPR %llx but expected %lx\n",71gcs->gcspr, gcspr);72return 1;73}7475fprintf(stderr, "GCS context validated\n");76td->pass = 1;7778return 0;79}8081struct tdescr tde = {82.name = "GCS basics",83.descr = "Validate a GCS signal context",84.feats_required = FEAT_GCS,85.timeout = 3,86.run = gcs_regs,87};888990