1 /* 2 * Linux kernel fallback API definitions for MTE and test helpers. 3 * 4 * Copyright (c) 2021 Linaro Ltd 5 * SPDX-License-Identifier: GPL-2.0-or-later 6 */ 7 8 #include <assert.h> 9 #include <string.h> 10 #include <stdlib.h> 11 #include <stdio.h> 12 #include <unistd.h> 13 #include <signal.h> 14 #include <sys/mman.h> 15 #include <sys/prctl.h> 16 17 #ifndef PR_SET_TAGGED_ADDR_CTRL 18 # define PR_SET_TAGGED_ADDR_CTRL 55 19 #endif 20 #ifndef PR_TAGGED_ADDR_ENABLE 21 # define PR_TAGGED_ADDR_ENABLE (1UL << 0) 22 #endif 23 #ifndef PR_MTE_TCF_SHIFT 24 # define PR_MTE_TCF_SHIFT 1 25 # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) 26 # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) 27 # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) 28 # define PR_MTE_TAG_SHIFT 3 29 #endif 30 31 #ifndef PROT_MTE 32 # define PROT_MTE 0x20 33 #endif 34 35 #ifndef SEGV_MTEAERR 36 # define SEGV_MTEAERR 8 37 # define SEGV_MTESERR 9 38 #endif 39 40 static void enable_mte(int tcf) 41 { 42 int r = prctl(PR_SET_TAGGED_ADDR_CTRL, 43 PR_TAGGED_ADDR_ENABLE | tcf | (0xfffe << PR_MTE_TAG_SHIFT), 44 0, 0, 0); 45 if (r < 0) { 46 perror("PR_SET_TAGGED_ADDR_CTRL"); 47 exit(2); 48 } 49 } 50 51 static void *alloc_mte_mem(size_t size) 52 { 53 void *p = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_MTE, 54 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 55 if (p == MAP_FAILED) { 56 perror("mmap PROT_MTE"); 57 exit(2); 58 } 59 return p; 60 } 61