136cd5fbdSRichard Henderson /* 236cd5fbdSRichard Henderson * Linux kernel fallback API definitions for MTE and test helpers. 336cd5fbdSRichard Henderson * 436cd5fbdSRichard Henderson * Copyright (c) 2021 Linaro Ltd 536cd5fbdSRichard Henderson * SPDX-License-Identifier: GPL-2.0-or-later 636cd5fbdSRichard Henderson */ 736cd5fbdSRichard Henderson 836cd5fbdSRichard Henderson #include <assert.h> 936cd5fbdSRichard Henderson #include <string.h> 1036cd5fbdSRichard Henderson #include <stdlib.h> 1136cd5fbdSRichard Henderson #include <stdio.h> 1236cd5fbdSRichard Henderson #include <unistd.h> 1336cd5fbdSRichard Henderson #include <signal.h> 1436cd5fbdSRichard Henderson #include <sys/mman.h> 1536cd5fbdSRichard Henderson #include <sys/prctl.h> 1636cd5fbdSRichard Henderson 1736cd5fbdSRichard Henderson #ifndef PR_SET_TAGGED_ADDR_CTRL 1836cd5fbdSRichard Henderson # define PR_SET_TAGGED_ADDR_CTRL 55 1936cd5fbdSRichard Henderson #endif 2036cd5fbdSRichard Henderson #ifndef PR_TAGGED_ADDR_ENABLE 2136cd5fbdSRichard Henderson # define PR_TAGGED_ADDR_ENABLE (1UL << 0) 2236cd5fbdSRichard Henderson #endif 2336cd5fbdSRichard Henderson #ifndef PR_MTE_TCF_SHIFT 2436cd5fbdSRichard Henderson # define PR_MTE_TCF_SHIFT 1 2536cd5fbdSRichard Henderson # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) 2636cd5fbdSRichard Henderson # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) 2736cd5fbdSRichard Henderson # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) 2836cd5fbdSRichard Henderson # define PR_MTE_TAG_SHIFT 3 2936cd5fbdSRichard Henderson #endif 3036cd5fbdSRichard Henderson 3136cd5fbdSRichard Henderson #ifndef PROT_MTE 3236cd5fbdSRichard Henderson # define PROT_MTE 0x20 3336cd5fbdSRichard Henderson #endif 3436cd5fbdSRichard Henderson 3536cd5fbdSRichard Henderson #ifndef SEGV_MTEAERR 3636cd5fbdSRichard Henderson # define SEGV_MTEAERR 8 3736cd5fbdSRichard Henderson # define SEGV_MTESERR 9 3836cd5fbdSRichard Henderson #endif 3936cd5fbdSRichard Henderson enable_mte(int tcf)4036cd5fbdSRichard Hendersonstatic void enable_mte(int tcf) 4136cd5fbdSRichard Henderson { 4236cd5fbdSRichard Henderson int r = prctl(PR_SET_TAGGED_ADDR_CTRL, 4336cd5fbdSRichard Henderson PR_TAGGED_ADDR_ENABLE | tcf | (0xfffe << PR_MTE_TAG_SHIFT), 4436cd5fbdSRichard Henderson 0, 0, 0); 4536cd5fbdSRichard Henderson if (r < 0) { 4636cd5fbdSRichard Henderson perror("PR_SET_TAGGED_ADDR_CTRL"); 4736cd5fbdSRichard Henderson exit(2); 4836cd5fbdSRichard Henderson } 4936cd5fbdSRichard Henderson } 5036cd5fbdSRichard Henderson 51*eb42297aSRichard Henderson static void * alloc_mte_mem(size_t size) __attribute__((unused)); alloc_mte_mem(size_t size)5236cd5fbdSRichard Hendersonstatic void * alloc_mte_mem(size_t size) 5336cd5fbdSRichard Henderson { 5436cd5fbdSRichard Henderson void *p = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_MTE, 5536cd5fbdSRichard Henderson MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 5636cd5fbdSRichard Henderson if (p == MAP_FAILED) { 5736cd5fbdSRichard Henderson perror("mmap PROT_MTE"); 5836cd5fbdSRichard Henderson exit(2); 5936cd5fbdSRichard Henderson } 6036cd5fbdSRichard Henderson return p; 6136cd5fbdSRichard Henderson } 62