Skip to content

Commit ed434c6

Browse files
LeviYeoReumctmarinas
authored andcommitted
kselftest/arm64/mte: Add address tag related macro and function
Add address tag related macro and function to test MTE_FAR feature. Signed-off-by: Yeoreum Yun <[email protected]> Reviewed-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 2e3e356 commit ed434c6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

tools/testing/selftests/arm64/mte/mte_common_util.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <signal.h>
77
#include <stdio.h>
88
#include <stdlib.h>
9+
#include <time.h>
910
#include <unistd.h>
1011

1112
#include <linux/auxvec.h>
@@ -130,6 +131,19 @@ void mte_clear_tags(void *ptr, size_t size)
130131
mte_clear_tag_address_range(ptr, size);
131132
}
132133

134+
void *mte_insert_atag(void *ptr)
135+
{
136+
unsigned char atag;
137+
138+
atag = mtefar_support ? (random() % MT_ATAG_MASK) + 1 : 0;
139+
return (void *)MT_SET_ATAG((unsigned long)ptr, atag);
140+
}
141+
142+
void *mte_clear_atag(void *ptr)
143+
{
144+
return (void *)MT_CLEAR_ATAG((unsigned long)ptr);
145+
}
146+
133147
static void *__mte_allocate_memory_range(size_t size, int mem_type, int mapping,
134148
size_t range_before, size_t range_after,
135149
bool tags, int fd)
@@ -330,6 +344,9 @@ int mte_default_setup(void)
330344
unsigned long en = 0;
331345
int ret;
332346

347+
/* To generate random address tag */
348+
srandom(time(NULL));
349+
333350
if (!(hwcaps2 & HWCAP2_MTE))
334351
ksft_exit_skip("MTE features unavailable\n");
335352

tools/testing/selftests/arm64/mte/mte_common_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void mte_free_memory_tag_range(void *ptr, size_t size, int mem_type,
5656
size_t range_before, size_t range_after);
5757
void *mte_insert_tags(void *ptr, size_t size);
5858
void mte_clear_tags(void *ptr, size_t size);
59+
void *mte_insert_atag(void *ptr);
60+
void *mte_clear_atag(void *ptr);
5961
int mte_default_setup(void);
6062
void mte_restore_setup(void);
6163
int mte_switch_mode(int mte_option, unsigned long incl_mask);

tools/testing/selftests/arm64/mte/mte_def.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,21 @@
4242
#define MT_TAG_COUNT 16
4343
#define MT_INCLUDE_TAG_MASK 0xFFFF
4444
#define MT_EXCLUDE_TAG_MASK 0x0
45+
#define MT_ATAG_SHIFT 60
46+
#define MT_ATAG_MASK 0xFUL
4547

4648
#define MT_ALIGN_GRANULE (MT_GRANULE_SIZE - 1)
4749
#define MT_CLEAR_TAG(x) ((x) & ~(MT_TAG_MASK << MT_TAG_SHIFT))
4850
#define MT_SET_TAG(x, y) ((x) | (y << MT_TAG_SHIFT))
4951
#define MT_FETCH_TAG(x) ((x >> MT_TAG_SHIFT) & (MT_TAG_MASK))
5052
#define MT_ALIGN_UP(x) ((x + MT_ALIGN_GRANULE) & ~(MT_ALIGN_GRANULE))
5153

54+
#define MT_CLEAR_ATAG(x) ((x) & ~(MT_TAG_MASK << MT_ATAG_SHIFT))
55+
#define MT_SET_ATAG(x, y) ((x) | (((y) & MT_ATAG_MASK) << MT_ATAG_SHIFT))
56+
#define MT_FETCH_ATAG(x) ((x >> MT_ATAG_SHIFT) & (MT_ATAG_MASK))
57+
58+
#define MT_CLEAR_TAGS(x) (MT_CLEAR_ATAG(MT_CLEAR_TAG(x)))
59+
5260
#define MT_PSTATE_TCO_SHIFT 25
5361
#define MT_PSTATE_TCO_MASK ~(0x1 << MT_PSTATE_TCO_SHIFT)
5462
#define MT_PSTATE_TCO_EN 1

0 commit comments

Comments
 (0)