1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2020 ARM Limited */
3 
4 #ifndef _MTE_COMMON_UTIL_H
5 #define _MTE_COMMON_UTIL_H
6 
7 #include <signal.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <sys/auxv.h>
11 #include <sys/mman.h>
12 #include <sys/prctl.h>
13 #include "mte_def.h"
14 #include "kselftest.h"
15 
16 enum mte_mem_type {
17 	USE_MALLOC,
18 	USE_MMAP,
19 	USE_MPROTECT,
20 };
21 
22 enum mte_mode {
23 	MTE_NONE_ERR,
24 	MTE_SYNC_ERR,
25 	MTE_ASYNC_ERR,
26 };
27 
28 struct mte_fault_cxt {
29 	/* Address start which triggers mte tag fault */
30 	unsigned long trig_addr;
31 	/* Address range for mte tag fault and negative value means underflow */
32 	ssize_t trig_range;
33 	/* siginfo si code */
34 	unsigned long trig_si_code;
35 	/* Flag to denote if correct fault caught */
36 	bool fault_valid;
37 };
38 
39 extern struct mte_fault_cxt cur_mte_cxt;
40 
41 /* MTE utility functions */
42 void mte_default_handler(int signum, siginfo_t *si, void *uc);
43 void mte_register_signal(int signal, void (*handler)(int, siginfo_t *, void *));
44 void mte_wait_after_trig(void);
45 void *mte_allocate_memory(size_t size, int mem_type, int mapping, bool tags);
46 void *mte_allocate_memory_tag_range(size_t size, int mem_type, int mapping,
47 				    size_t range_before, size_t range_after);
48 void *mte_allocate_file_memory(size_t size, int mem_type, int mapping,
49 			       bool tags, int fd);
50 void *mte_allocate_file_memory_tag_range(size_t size, int mem_type, int mapping,
51 					 size_t range_before, size_t range_after, int fd);
52 void mte_free_memory(void *ptr, size_t size, int mem_type, bool tags);
53 void mte_free_memory_tag_range(void *ptr, size_t size, int mem_type,
54 			       size_t range_before, size_t range_after);
55 void *mte_insert_tags(void *ptr, size_t size);
56 void mte_clear_tags(void *ptr, size_t size);
57 int mte_default_setup(void);
58 void mte_restore_setup(void);
59 int mte_switch_mode(int mte_option, unsigned long incl_mask);
60 void mte_initialize_current_context(int mode, uintptr_t ptr, ssize_t range);
61 
62 /* Common utility functions */
63 int create_temp_file(void);
64 
65 /* Assembly MTE utility functions */
66 void *mte_insert_random_tag(void *ptr);
67 void *mte_insert_new_tag(void *ptr);
68 void *mte_get_tag_address(void *ptr);
69 void mte_set_tag_address_range(void *ptr, int range);
70 void mte_clear_tag_address_range(void *ptr, int range);
71 void mte_disable_pstate_tco(void);
72 void mte_enable_pstate_tco(void);
73 unsigned int mte_get_pstate_tco(void);
74 
75 /* Test framework static inline functions/macros */
evaluate_test(int err,const char * msg)76 static inline void evaluate_test(int err, const char *msg)
77 {
78 	if (err == KSFT_PASS)
79 		ksft_test_result_pass(msg);
80 	else if (err == KSFT_FAIL)
81 		ksft_test_result_fail(msg);
82 }
83 
check_allocated_memory(void * ptr,size_t size,int mem_type,bool tags)84 static inline int check_allocated_memory(void *ptr, size_t size,
85 					 int mem_type, bool tags)
86 {
87 	if (ptr == NULL) {
88 		ksft_print_msg("FAIL: memory allocation\n");
89 		return KSFT_FAIL;
90 	}
91 
92 	if (tags && !MT_FETCH_TAG((uintptr_t)ptr)) {
93 		ksft_print_msg("FAIL: tag not found at addr(%p)\n", ptr);
94 		mte_free_memory((void *)ptr, size, mem_type, false);
95 		return KSFT_FAIL;
96 	}
97 
98 	return KSFT_PASS;
99 }
100 
check_allocated_memory_range(void * ptr,size_t size,int mem_type,size_t range_before,size_t range_after)101 static inline int check_allocated_memory_range(void *ptr, size_t size, int mem_type,
102 					       size_t range_before, size_t range_after)
103 {
104 	if (ptr == NULL) {
105 		ksft_print_msg("FAIL: memory allocation\n");
106 		return KSFT_FAIL;
107 	}
108 
109 	if (!MT_FETCH_TAG((uintptr_t)ptr)) {
110 		ksft_print_msg("FAIL: tag not found at addr(%p)\n", ptr);
111 		mte_free_memory_tag_range((void *)ptr, size, mem_type, range_before,
112 					  range_after);
113 		return KSFT_FAIL;
114 	}
115 	return KSFT_PASS;
116 }
117 
118 #endif /* _MTE_COMMON_UTIL_H */
119