load_unaligned_zeropad.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Userspace test harness for load_unaligned_zeropad. Creates two
  4. * pages and uses mprotect to prevent access to the second page and
  5. * a SEGV handler that walks the exception tables and runs the fixup
  6. * routine.
  7. *
  8. * The results are compared against a normal load that is that is
  9. * performed while access to the second page is enabled via mprotect.
  10. *
  11. * Copyright (C) 2014 Anton Blanchard <anton@au.ibm.com>, IBM
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdbool.h>
  17. #include <signal.h>
  18. #include <unistd.h>
  19. #include <sys/mman.h>
  20. #define FIXUP_SECTION ".ex_fixup"
  21. static inline unsigned long __fls(unsigned long x);
  22. #include "word-at-a-time.h"
  23. #include "utils.h"
  24. static inline unsigned long __fls(unsigned long x)
  25. {
  26. int lz;
  27. asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));
  28. return sizeof(unsigned long) - 1 - lz;
  29. }
  30. static int page_size;
  31. static char *mem_region;
  32. static int protect_region(void)
  33. {
  34. if (mprotect(mem_region + page_size, page_size, PROT_NONE)) {
  35. perror("mprotect");
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static int unprotect_region(void)
  41. {
  42. if (mprotect(mem_region + page_size, page_size, PROT_READ|PROT_WRITE)) {
  43. perror("mprotect");
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. extern char __start___ex_table[];
  49. extern char __stop___ex_table[];
  50. struct extbl_entry {
  51. int insn;
  52. int fixup;
  53. };
  54. static void segv_handler(int signr, siginfo_t *info, void *ptr)
  55. {
  56. ucontext_t *uc = (ucontext_t *)ptr;
  57. unsigned long addr = (unsigned long)info->si_addr;
  58. unsigned long *ip = &UCONTEXT_NIA(uc);
  59. struct extbl_entry *entry = (struct extbl_entry *)__start___ex_table;
  60. while (entry < (struct extbl_entry *)__stop___ex_table) {
  61. unsigned long insn, fixup;
  62. insn = (unsigned long)&entry->insn + entry->insn;
  63. fixup = (unsigned long)&entry->fixup + entry->fixup;
  64. if (insn == *ip) {
  65. *ip = fixup;
  66. return;
  67. }
  68. }
  69. printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
  70. abort();
  71. }
  72. static void setup_segv_handler(void)
  73. {
  74. struct sigaction action;
  75. memset(&action, 0, sizeof(action));
  76. action.sa_sigaction = segv_handler;
  77. action.sa_flags = SA_SIGINFO;
  78. sigaction(SIGSEGV, &action, NULL);
  79. }
  80. static int do_one_test(char *p, int page_offset)
  81. {
  82. unsigned long should;
  83. unsigned long got;
  84. FAIL_IF(unprotect_region());
  85. should = *(unsigned long *)p;
  86. FAIL_IF(protect_region());
  87. got = load_unaligned_zeropad(p);
  88. if (should != got) {
  89. printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static int test_body(void)
  95. {
  96. unsigned long i;
  97. page_size = getpagesize();
  98. mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
  99. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  100. FAIL_IF(mem_region == MAP_FAILED);
  101. for (i = 0; i < page_size; i++)
  102. mem_region[i] = i;
  103. memset(mem_region+page_size, 0, page_size);
  104. setup_segv_handler();
  105. for (i = 0; i < page_size; i++)
  106. FAIL_IF(do_one_test(mem_region+i, i));
  107. return 0;
  108. }
  109. int main(void)
  110. {
  111. return test_harness(test_body, "load_unaligned_zeropad");
  112. }