1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (c) 2022, Linaro Limited
*/
#ifndef __MEMTAG_H
#define __MEMTAG_H
#include <assert.h>
#include <string.h>
#include <types_ext.h>
#include <util.h>
#if defined(CFG_MEMTAG) && defined(__aarch64__)
#define MEMTAG_IS_ENABLED 1
#define MEMTAG_TAG_SHIFT 56
#define MEMTAG_TAG_WIDTH 4
#define MEMTAG_TAG_MASK (BIT(MEMTAG_TAG_WIDTH) - 1)
#define MEMTAG_GRANULE_SIZE 16
#else
#define MEMTAG_IS_ENABLED 0
#define MEMTAG_GRANULE_SIZE 1
#define MEMTAG_TAG_WIDTH 0
#endif
#define MEMTAG_GRANULE_MASK (MEMTAG_GRANULE_SIZE - 1)
struct __memtag_ops {
void *(*set_tags)(void *addr, size_t size, uint8_t tag);
void *(*set_random_tags)(void *addr, size_t size);
void (*clear_mem)(void *addr, size_t size);
uint8_t (*read_tag)(const void *addr);
};
extern const struct __memtag_ops __memtag_ops_disabled;
extern const struct __memtag_ops *__memtag_ops;
static inline void *__memtag_disabled_set_tags(void *addr, size_t size __unused,
uint8_t tag __unused)
{
return addr;
}
static inline void *__memtag_disabled_set_random_tags(void *addr,
size_t size __unused)
{
return addr;
}
static inline void __memtag_disabled_clear_mem(void *addr, size_t size)
{
memset(addr, 0, size);
}
static inline uint8_t __memtag_disabled_read_tag(const void *addr __unused)
{
return 0;
}
/*
* memtag_set_tags() - Tag a memory range
* @addr: Start of memory range
* @size: Size of memory range
* @tag: Tag to use
*
* The memory range is updated with the supplied tag. An eventual tag
* already present in the upper bits of the address in @addr is ignored.
*
* @addr and @size must be aligned/multiple of MEMTAG_GRANULE_SIZE.
*
* Returns an address with the new tag inserted to be used to access this
* memory area.
*/
static inline void *memtag_set_tags(void *addr, size_t size, uint8_t tag)
{
#if MEMTAG_IS_ENABLED
return __memtag_ops->set_tags(addr, size, tag);
#else
return __memtag_disabled_set_tags(addr, size, tag);
#endif
}
/*
* memtag_set_random_tags() - Tag a memory range with a random tag
* @addr: Start of memory range
* @size: Size of memory range
*
* The memory range is updated with a randomly generated tag. An eventual
* tag already present in the upper bits of the address in @addr is
* ignored.
*
* @addr and @size must be aligned/multiple of MEMTAG_GRANULE_SIZE.
*
* Returns an address with the new tag inserted to be used to access this
* memory area.
*/
static inline void *memtag_set_random_tags(void *addr, size_t size)
{
#if MEMTAG_IS_ENABLED
return __memtag_ops->set_random_tags(addr, size);
#else
return __memtag_disabled_set_random_tags(addr, size);
#endif
}
static inline void memtag_clear_mem(void *addr, size_t size)
{
#if MEMTAG_IS_ENABLED
__memtag_ops->clear_mem(addr, size);
#else
__memtag_disabled_clear_mem(addr, size);
#endif
}
/*
* memtag_strip_tag_vaddr() - Removes an eventual tag from an address
* @addr: Address to strip
*
* Returns a vaddr_t without an eventual tag.
*/
static inline vaddr_t memtag_strip_tag_vaddr(const void *addr)
{
vaddr_t va = (vaddr_t)addr;
#if MEMTAG_IS_ENABLED
va &= ~SHIFT_U64(MEMTAG_TAG_MASK, MEMTAG_TAG_SHIFT);
#endif
return va;
}
/*
* memtag_strip_tag_const() - Removes an eventual tag from an address
* @addr: Address to strip
*
* Returns the address without an eventual tag.
*/
static inline const void *memtag_strip_tag_const(const void *addr)
{
return (const void *)memtag_strip_tag_vaddr(addr);
}
/*
* memtag_strip_tag() - Removes an eventual tag from an address
* @addr: Address to strip
*
* Returns the address without an eventual tag.
*/
static inline void *memtag_strip_tag(void *addr)
{
return (void *)memtag_strip_tag_vaddr(addr);
}
/*
* memtag_insert_tag_vaddr() - Inserts a tag into an address
* @addr: Address to transform
* @tag: Tag to insert
*
* Returns the address with the new tag inserted.
*/
static inline vaddr_t memtag_insert_tag_vaddr(vaddr_t addr,
uint8_t tag __maybe_unused)
{
vaddr_t va = memtag_strip_tag_vaddr((void *)addr);
#if MEMTAG_IS_ENABLED
va |= SHIFT_U64(tag, MEMTAG_TAG_SHIFT);
#endif
return va;
}
/*
* memtag_insert_tag() - Inserts a tag into an address
* @addr: Address to transform
* @tag: Tag to insert
*
* Returns the address with the new tag inserted.
*/
static inline void *memtag_insert_tag(void *addr, uint8_t tag)
{
return (void *)memtag_insert_tag_vaddr((vaddr_t)addr, tag);
}
/*
* memtag_get_tag() - Extract a tag from an address
* @addr: Address with an eventual tag
*
* Returns the extracted tag.
*/
static inline uint8_t memtag_get_tag(const void *addr __maybe_unused)
{
#if MEMTAG_IS_ENABLED
uint64_t va = (vaddr_t)addr;
return (va >> MEMTAG_TAG_SHIFT) & MEMTAG_TAG_MASK;
#else
return 0;
#endif
}
static inline uint8_t memtag_read_tag(const void *addr)
{
#if MEMTAG_IS_ENABLED
return __memtag_ops->read_tag(addr);
#else
return __memtag_disabled_read_tag(addr);
#endif
}
static inline void memtag_assert_tag(const void *addr __maybe_unused)
{
assert(memtag_get_tag(addr) == memtag_read_tag(addr));
}
#if MEMTAG_IS_ENABLED
void memtag_init_ops(unsigned int memtag_impl);
#else
static inline void memtag_init_ops(unsigned int memtag_impl __unused)
{
}
#endif
static inline bool memtag_is_enabled(void)
{
#if MEMTAG_IS_ENABLED
return __memtag_ops != &__memtag_ops_disabled;
#else
return false;
#endif
}
#endif /*__MEMTAG_H*/
|