Linux Audio

Check our new training course

Loading...
  1/* SPDX-License-Identifier: GPL-2.0 */
  2/* Copyright (C) 2019 ARM Limited */
  3#ifndef __TESTCASES_H__
  4#define __TESTCASES_H__
  5
  6#include <stddef.h>
  7#include <stdio.h>
  8#include <stdbool.h>
  9#include <stdint.h>
 10#include <stdlib.h>
 11#include <ucontext.h>
 12#include <signal.h>
 13
 14/* Architecture specific sigframe definitions */
 15#include <asm/sigcontext.h>
 16
 17#define FPSIMD_CTX	(1 << 0)
 18#define SVE_CTX		(1 << 1)
 19#define ZA_CTX		(1 << 2)
 20#define EXTRA_CTX	(1 << 3)
 21#define ZT_CTX		(1 << 4)
 22#define FPMR_CTX	(1 << 5)
 23
 24#define KSFT_BAD_MAGIC	0xdeadbeef
 25
 26#define HDR_SZ \
 27	sizeof(struct _aarch64_ctx)
 28
 29#define GET_SF_RESV_HEAD(sf) \
 30	(struct _aarch64_ctx *)(&(sf).uc.uc_mcontext.__reserved)
 31
 32#define GET_SF_RESV_SIZE(sf) \
 33	sizeof((sf).uc.uc_mcontext.__reserved)
 34
 35#define GET_BUF_RESV_HEAD(buf) \
 36	(struct _aarch64_ctx *)(&(buf).uc.uc_mcontext.__reserved)
 37
 38#define GET_BUF_RESV_SIZE(buf) \
 39	(sizeof(buf) - sizeof(buf.uc) +	\
 40	 sizeof((buf).uc.uc_mcontext.__reserved))
 41
 42#define GET_UCP_RESV_SIZE(ucp) \
 43	sizeof((ucp)->uc_mcontext.__reserved)
 44
 45#define ASSERT_BAD_CONTEXT(uc) do {					\
 46	char *err = NULL;						\
 47	if (!validate_reserved((uc), GET_UCP_RESV_SIZE((uc)), &err)) {	\
 48		if (err)						\
 49			fprintf(stderr,					\
 50				"Using badly built context - ERR: %s\n",\
 51				err);					\
 52	} else {							\
 53		abort();						\
 54	}								\
 55} while (0)
 56
 57#define ASSERT_GOOD_CONTEXT(uc) do {					 \
 58	char *err = NULL;						 \
 59	if (!validate_reserved((uc), GET_UCP_RESV_SIZE((uc)), &err)) {	 \
 60		if (err)						 \
 61			fprintf(stderr,					 \
 62				"Detected BAD context - ERR: %s\n", err);\
 63		abort();						 \
 64	} else {							 \
 65		fprintf(stderr, "uc context validated.\n");		 \
 66	}								 \
 67} while (0)
 68
 69/*
 70 * A simple record-walker for __reserved area: it walks through assuming
 71 * only to find a proper struct __aarch64_ctx header descriptor.
 72 *
 73 * Instead it makes no assumptions on the content and ordering of the
 74 * records, any needed bounds checking must be enforced by the caller
 75 * if wanted: this way can be used by caller on any maliciously built bad
 76 * contexts.
 77 *
 78 * head->size accounts both for payload and header _aarch64_ctx size !
 79 */
 80#define GET_RESV_NEXT_HEAD(h) \
 81	(struct _aarch64_ctx *)((char *)(h) + (h)->size)
 82
 83struct fake_sigframe {
 84	siginfo_t	info;
 85	ucontext_t	uc;
 86};
 87
 88
 89bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err);
 90
 91struct _aarch64_ctx *get_header(struct _aarch64_ctx *head, uint32_t magic,
 92				size_t resv_sz, size_t *offset);
 93
 94static inline struct _aarch64_ctx *get_terminator(struct _aarch64_ctx *head,
 95						  size_t resv_sz,
 96						  size_t *offset)
 97{
 98	return get_header(head, 0, resv_sz, offset);
 99}
100
101static inline void write_terminator_record(struct _aarch64_ctx *tail)
102{
103	if (tail) {
104		tail->magic = 0;
105		tail->size = 0;
106	}
107}
108
109struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead,
110				       size_t need_sz, size_t resv_sz,
111				       size_t *offset);
112#endif