1 /* libc-internal interface for tagged (colored) memory support.
2    Copyright (C) 2020-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 #ifndef _AARCH64_LIBC_MTAG_H
20 #define _AARCH64_LIBC_MTAG_H 1
21 
22 #ifndef USE_MTAG
23 /* Generic bindings for systems that do not support memory tagging.  */
24 #include_next "libc-mtag.h"
25 #else
26 
27 /* Used to ensure additional alignment when objects need to have distinct
28    tags.  */
29 #define __MTAG_GRANULE_SIZE 16
30 
31 /* Non-zero if memory obtained via morecore (sbrk) is not tagged.  */
32 #define __MTAG_SBRK_UNTAGGED 1
33 
34 /* Extra flags to pass to mmap to get tagged pages.  */
35 #define __MTAG_MMAP_FLAGS PROT_MTE
36 
37 /* Set the tags for a region of memory, which must have size and alignment
38    that are multiples of __MTAG_GRANULE_SIZE.  Size cannot be zero.  */
39 void *__libc_mtag_tag_region (void *, size_t);
40 
41 /* Optimized equivalent to __libc_mtag_tag_region followed by memset to 0.  */
42 void *__libc_mtag_tag_zero_region (void *, size_t);
43 
44 /* Convert address P to a pointer that is tagged correctly for that
45    location.  */
46 static __always_inline void *
__libc_mtag_address_get_tag(void * p)47 __libc_mtag_address_get_tag (void *p)
48 {
49   register void *x0 asm ("x0") = p;
50   asm (".inst 0xd9600000 /* ldg x0, [x0] */" : "+r" (x0));
51   return x0;
52 }
53 
54 /* Assign a new (random) tag to a pointer P (does not adjust the tag on
55    the memory addressed).  */
56 static __always_inline void *
__libc_mtag_new_tag(void * p)57 __libc_mtag_new_tag (void *p)
58 {
59   register void *x0 asm ("x0") = p;
60   register unsigned long x1 asm ("x1");
61   /* Guarantee that the new tag is not the same as now.  */
62   asm (".inst 0x9adf1401 /* gmi x1, x0, xzr */\n"
63        ".inst 0x9ac11000 /* irg x0, x0, x1 */" : "+r" (x0), "=r" (x1));
64   return x0;
65 }
66 
67 #endif /* USE_MTAG */
68 
69 #endif /* _AARCH64_LIBC_MTAG_H */
70