load_unaligned_zeropad.c 3.4 KB

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