mte_common_util.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2020 ARM Limited
  3. #include <fcntl.h>
  4. #include <sched.h>
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <linux/auxvec.h>
  10. #include <sys/auxv.h>
  11. #include <sys/mman.h>
  12. #include <sys/prctl.h>
  13. #include <asm/hwcap.h>
  14. #include "kselftest.h"
  15. #include "mte_common_util.h"
  16. #include "mte_def.h"
  17. #define INIT_BUFFER_SIZE 256
  18. struct mte_fault_cxt cur_mte_cxt;
  19. static unsigned int mte_cur_mode;
  20. static unsigned int mte_cur_pstate_tco;
  21. void mte_default_handler(int signum, siginfo_t *si, void *uc)
  22. {
  23. unsigned long addr = (unsigned long)si->si_addr;
  24. if (signum == SIGSEGV) {
  25. #ifdef DEBUG
  26. ksft_print_msg("INFO: SIGSEGV signal at pc=%lx, fault addr=%lx, si_code=%lx\n",
  27. ((ucontext_t *)uc)->uc_mcontext.pc, addr, si->si_code);
  28. #endif
  29. if (si->si_code == SEGV_MTEAERR) {
  30. if (cur_mte_cxt.trig_si_code == si->si_code)
  31. cur_mte_cxt.fault_valid = true;
  32. else
  33. ksft_print_msg("Got unexpected SEGV_MTEAERR at pc=%llx, fault addr=%lx\n",
  34. ((ucontext_t *)uc)->uc_mcontext.pc,
  35. addr);
  36. return;
  37. }
  38. /* Compare the context for precise error */
  39. else if (si->si_code == SEGV_MTESERR) {
  40. if (cur_mte_cxt.trig_si_code == si->si_code &&
  41. ((cur_mte_cxt.trig_range >= 0 &&
  42. addr >= MT_CLEAR_TAG(cur_mte_cxt.trig_addr) &&
  43. addr <= (MT_CLEAR_TAG(cur_mte_cxt.trig_addr) + cur_mte_cxt.trig_range)) ||
  44. (cur_mte_cxt.trig_range < 0 &&
  45. addr <= MT_CLEAR_TAG(cur_mte_cxt.trig_addr) &&
  46. addr >= (MT_CLEAR_TAG(cur_mte_cxt.trig_addr) + cur_mte_cxt.trig_range)))) {
  47. cur_mte_cxt.fault_valid = true;
  48. /* Adjust the pc by 4 */
  49. ((ucontext_t *)uc)->uc_mcontext.pc += 4;
  50. } else {
  51. ksft_print_msg("Invalid MTE synchronous exception caught!\n");
  52. exit(1);
  53. }
  54. } else {
  55. ksft_print_msg("Unknown SIGSEGV exception caught!\n");
  56. exit(1);
  57. }
  58. } else if (signum == SIGBUS) {
  59. ksft_print_msg("INFO: SIGBUS signal at pc=%llx, fault addr=%lx, si_code=%x\n",
  60. ((ucontext_t *)uc)->uc_mcontext.pc, addr, si->si_code);
  61. if ((cur_mte_cxt.trig_range >= 0 &&
  62. addr >= MT_CLEAR_TAG(cur_mte_cxt.trig_addr) &&
  63. addr <= (MT_CLEAR_TAG(cur_mte_cxt.trig_addr) + cur_mte_cxt.trig_range)) ||
  64. (cur_mte_cxt.trig_range < 0 &&
  65. addr <= MT_CLEAR_TAG(cur_mte_cxt.trig_addr) &&
  66. addr >= (MT_CLEAR_TAG(cur_mte_cxt.trig_addr) + cur_mte_cxt.trig_range))) {
  67. cur_mte_cxt.fault_valid = true;
  68. /* Adjust the pc by 4 */
  69. ((ucontext_t *)uc)->uc_mcontext.pc += 4;
  70. }
  71. }
  72. }
  73. void mte_register_signal(int signal, void (*handler)(int, siginfo_t *, void *))
  74. {
  75. struct sigaction sa;
  76. sa.sa_sigaction = handler;
  77. sa.sa_flags = SA_SIGINFO;
  78. sigemptyset(&sa.sa_mask);
  79. sigaction(signal, &sa, NULL);
  80. }
  81. void mte_wait_after_trig(void)
  82. {
  83. sched_yield();
  84. }
  85. void *mte_insert_tags(void *ptr, size_t size)
  86. {
  87. void *tag_ptr;
  88. int align_size;
  89. if (!ptr || (unsigned long)(ptr) & MT_ALIGN_GRANULE) {
  90. ksft_print_msg("FAIL: Addr=%lx: invalid\n", ptr);
  91. return NULL;
  92. }
  93. align_size = MT_ALIGN_UP(size);
  94. tag_ptr = mte_insert_random_tag(ptr);
  95. mte_set_tag_address_range(tag_ptr, align_size);
  96. return tag_ptr;
  97. }
  98. void mte_clear_tags(void *ptr, size_t size)
  99. {
  100. if (!ptr || (unsigned long)(ptr) & MT_ALIGN_GRANULE) {
  101. ksft_print_msg("FAIL: Addr=%lx: invalid\n", ptr);
  102. return;
  103. }
  104. size = MT_ALIGN_UP(size);
  105. ptr = (void *)MT_CLEAR_TAG((unsigned long)ptr);
  106. mte_clear_tag_address_range(ptr, size);
  107. }
  108. static void *__mte_allocate_memory_range(size_t size, int mem_type, int mapping,
  109. size_t range_before, size_t range_after,
  110. bool tags, int fd)
  111. {
  112. void *ptr;
  113. int prot_flag, map_flag;
  114. size_t entire_size = size + range_before + range_after;
  115. switch (mem_type) {
  116. case USE_MALLOC:
  117. return malloc(entire_size) + range_before;
  118. case USE_MMAP:
  119. case USE_MPROTECT:
  120. break;
  121. default:
  122. ksft_print_msg("FAIL: Invalid allocate request\n");
  123. return NULL;
  124. }
  125. prot_flag = PROT_READ | PROT_WRITE;
  126. if (mem_type == USE_MMAP)
  127. prot_flag |= PROT_MTE;
  128. map_flag = mapping;
  129. if (fd == -1)
  130. map_flag = MAP_ANONYMOUS | map_flag;
  131. if (!(mapping & MAP_SHARED))
  132. map_flag |= MAP_PRIVATE;
  133. ptr = mmap(NULL, entire_size, prot_flag, map_flag, fd, 0);
  134. if (ptr == MAP_FAILED) {
  135. ksft_print_msg("FAIL: mmap allocation\n");
  136. return NULL;
  137. }
  138. if (mem_type == USE_MPROTECT) {
  139. if (mprotect(ptr, entire_size, prot_flag | PROT_MTE)) {
  140. munmap(ptr, size);
  141. ksft_print_msg("FAIL: mprotect PROT_MTE property\n");
  142. return NULL;
  143. }
  144. }
  145. if (tags)
  146. ptr = mte_insert_tags(ptr + range_before, size);
  147. return ptr;
  148. }
  149. void *mte_allocate_memory_tag_range(size_t size, int mem_type, int mapping,
  150. size_t range_before, size_t range_after)
  151. {
  152. return __mte_allocate_memory_range(size, mem_type, mapping, range_before,
  153. range_after, true, -1);
  154. }
  155. void *mte_allocate_memory(size_t size, int mem_type, int mapping, bool tags)
  156. {
  157. return __mte_allocate_memory_range(size, mem_type, mapping, 0, 0, tags, -1);
  158. }
  159. void *mte_allocate_file_memory(size_t size, int mem_type, int mapping, bool tags, int fd)
  160. {
  161. int index;
  162. char buffer[INIT_BUFFER_SIZE];
  163. if (mem_type != USE_MPROTECT && mem_type != USE_MMAP) {
  164. ksft_print_msg("FAIL: Invalid mmap file request\n");
  165. return NULL;
  166. }
  167. /* Initialize the file for mappable size */
  168. lseek(fd, 0, SEEK_SET);
  169. for (index = INIT_BUFFER_SIZE; index < size; index += INIT_BUFFER_SIZE) {
  170. if (write(fd, buffer, INIT_BUFFER_SIZE) != INIT_BUFFER_SIZE) {
  171. perror("initialising buffer");
  172. return NULL;
  173. }
  174. }
  175. index -= INIT_BUFFER_SIZE;
  176. if (write(fd, buffer, size - index) != size - index) {
  177. perror("initialising buffer");
  178. return NULL;
  179. }
  180. return __mte_allocate_memory_range(size, mem_type, mapping, 0, 0, tags, fd);
  181. }
  182. void *mte_allocate_file_memory_tag_range(size_t size, int mem_type, int mapping,
  183. size_t range_before, size_t range_after, int fd)
  184. {
  185. int index;
  186. char buffer[INIT_BUFFER_SIZE];
  187. int map_size = size + range_before + range_after;
  188. if (mem_type != USE_MPROTECT && mem_type != USE_MMAP) {
  189. ksft_print_msg("FAIL: Invalid mmap file request\n");
  190. return NULL;
  191. }
  192. /* Initialize the file for mappable size */
  193. lseek(fd, 0, SEEK_SET);
  194. for (index = INIT_BUFFER_SIZE; index < map_size; index += INIT_BUFFER_SIZE)
  195. if (write(fd, buffer, INIT_BUFFER_SIZE) != INIT_BUFFER_SIZE) {
  196. perror("initialising buffer");
  197. return NULL;
  198. }
  199. index -= INIT_BUFFER_SIZE;
  200. if (write(fd, buffer, map_size - index) != map_size - index) {
  201. perror("initialising buffer");
  202. return NULL;
  203. }
  204. return __mte_allocate_memory_range(size, mem_type, mapping, range_before,
  205. range_after, true, fd);
  206. }
  207. static void __mte_free_memory_range(void *ptr, size_t size, int mem_type,
  208. size_t range_before, size_t range_after, bool tags)
  209. {
  210. switch (mem_type) {
  211. case USE_MALLOC:
  212. free(ptr - range_before);
  213. break;
  214. case USE_MMAP:
  215. case USE_MPROTECT:
  216. if (tags)
  217. mte_clear_tags(ptr, size);
  218. munmap(ptr - range_before, size + range_before + range_after);
  219. break;
  220. default:
  221. ksft_print_msg("FAIL: Invalid free request\n");
  222. break;
  223. }
  224. }
  225. void mte_free_memory_tag_range(void *ptr, size_t size, int mem_type,
  226. size_t range_before, size_t range_after)
  227. {
  228. __mte_free_memory_range(ptr, size, mem_type, range_before, range_after, true);
  229. }
  230. void mte_free_memory(void *ptr, size_t size, int mem_type, bool tags)
  231. {
  232. __mte_free_memory_range(ptr, size, mem_type, 0, 0, tags);
  233. }
  234. void mte_initialize_current_context(int mode, uintptr_t ptr, ssize_t range)
  235. {
  236. cur_mte_cxt.fault_valid = false;
  237. cur_mte_cxt.trig_addr = ptr;
  238. cur_mte_cxt.trig_range = range;
  239. if (mode == MTE_SYNC_ERR)
  240. cur_mte_cxt.trig_si_code = SEGV_MTESERR;
  241. else if (mode == MTE_ASYNC_ERR)
  242. cur_mte_cxt.trig_si_code = SEGV_MTEAERR;
  243. else
  244. cur_mte_cxt.trig_si_code = 0;
  245. }
  246. int mte_switch_mode(int mte_option, unsigned long incl_mask)
  247. {
  248. unsigned long en = 0;
  249. switch (mte_option) {
  250. case MTE_NONE_ERR:
  251. case MTE_SYNC_ERR:
  252. case MTE_ASYNC_ERR:
  253. break;
  254. default:
  255. ksft_print_msg("FAIL: Invalid MTE option %x\n", mte_option);
  256. return -EINVAL;
  257. }
  258. if (incl_mask & ~MT_INCLUDE_TAG_MASK) {
  259. ksft_print_msg("FAIL: Invalid incl_mask %lx\n", incl_mask);
  260. return -EINVAL;
  261. }
  262. en = PR_TAGGED_ADDR_ENABLE;
  263. switch (mte_option) {
  264. case MTE_SYNC_ERR:
  265. en |= PR_MTE_TCF_SYNC;
  266. break;
  267. case MTE_ASYNC_ERR:
  268. en |= PR_MTE_TCF_ASYNC;
  269. break;
  270. case MTE_NONE_ERR:
  271. en |= PR_MTE_TCF_NONE;
  272. break;
  273. }
  274. en |= (incl_mask << PR_MTE_TAG_SHIFT);
  275. /* Enable address tagging ABI, mte error reporting mode and tag inclusion mask. */
  276. if (prctl(PR_SET_TAGGED_ADDR_CTRL, en, 0, 0, 0) != 0) {
  277. ksft_print_msg("FAIL:prctl PR_SET_TAGGED_ADDR_CTRL for mte mode\n");
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. int mte_default_setup(void)
  283. {
  284. unsigned long hwcaps2 = getauxval(AT_HWCAP2);
  285. unsigned long en = 0;
  286. int ret;
  287. if (!(hwcaps2 & HWCAP2_MTE)) {
  288. ksft_print_msg("SKIP: MTE features unavailable\n");
  289. return KSFT_SKIP;
  290. }
  291. /* Get current mte mode */
  292. ret = prctl(PR_GET_TAGGED_ADDR_CTRL, en, 0, 0, 0);
  293. if (ret < 0) {
  294. ksft_print_msg("FAIL:prctl PR_GET_TAGGED_ADDR_CTRL with error =%d\n", ret);
  295. return KSFT_FAIL;
  296. }
  297. if (ret & PR_MTE_TCF_SYNC)
  298. mte_cur_mode = MTE_SYNC_ERR;
  299. else if (ret & PR_MTE_TCF_ASYNC)
  300. mte_cur_mode = MTE_ASYNC_ERR;
  301. else if (ret & PR_MTE_TCF_NONE)
  302. mte_cur_mode = MTE_NONE_ERR;
  303. mte_cur_pstate_tco = mte_get_pstate_tco();
  304. /* Disable PSTATE.TCO */
  305. mte_disable_pstate_tco();
  306. return 0;
  307. }
  308. void mte_restore_setup(void)
  309. {
  310. mte_switch_mode(mte_cur_mode, MTE_ALLOW_NON_ZERO_TAG);
  311. if (mte_cur_pstate_tco == MT_PSTATE_TCO_EN)
  312. mte_enable_pstate_tco();
  313. else if (mte_cur_pstate_tco == MT_PSTATE_TCO_DIS)
  314. mte_disable_pstate_tco();
  315. }
  316. int create_temp_file(void)
  317. {
  318. int fd;
  319. char filename[] = "/dev/shm/tmp_XXXXXX";
  320. /* Create a file in the tmpfs filesystem */
  321. fd = mkstemp(&filename[0]);
  322. if (fd == -1) {
  323. perror(filename);
  324. ksft_print_msg("FAIL: Unable to open temporary file\n");
  325. return 0;
  326. }
  327. unlink(&filename[0]);
  328. return fd;
  329. }