load_unaligned_zeropad.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. static int segv_error;
  62. static void segv_handler(int signr, siginfo_t *info, void *ptr)
  63. {
  64. ucontext_t *uc = (ucontext_t *)ptr;
  65. unsigned long addr = (unsigned long)info->si_addr;
  66. unsigned long *ip = &UCONTEXT_NIA(uc);
  67. unsigned long *ex_p = (unsigned long *)__start___ex_table;
  68. while (ex_p < (unsigned long *)__stop___ex_table) {
  69. unsigned long insn, fixup;
  70. insn = *ex_p++;
  71. fixup = *ex_p++;
  72. if (insn == *ip) {
  73. *ip = fixup;
  74. return;
  75. }
  76. }
  77. printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
  78. segv_error++;
  79. }
  80. static void setup_segv_handler(void)
  81. {
  82. struct sigaction action;
  83. memset(&action, 0, sizeof(action));
  84. action.sa_sigaction = segv_handler;
  85. action.sa_flags = SA_SIGINFO;
  86. sigaction(SIGSEGV, &action, NULL);
  87. }
  88. static int do_one_test(char *p, int page_offset)
  89. {
  90. unsigned long should;
  91. unsigned long got;
  92. FAIL_IF(unprotect_region());
  93. should = *(unsigned long *)p;
  94. FAIL_IF(protect_region());
  95. got = load_unaligned_zeropad(p);
  96. if (should != got)
  97. printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
  98. return 0;
  99. }
  100. static int test_body(void)
  101. {
  102. unsigned long i;
  103. page_size = getpagesize();
  104. mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
  105. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  106. FAIL_IF(mem_region == MAP_FAILED);
  107. for (i = 0; i < page_size; i++)
  108. mem_region[i] = i;
  109. memset(mem_region+page_size, 0, page_size);
  110. setup_segv_handler();
  111. for (i = 0; i < page_size; i++)
  112. FAIL_IF(do_one_test(mem_region+i, i));
  113. FAIL_IF(segv_error);
  114. return 0;
  115. }
  116. int main(void)
  117. {
  118. return test_harness(test_body, "load_unaligned_zeropad");
  119. }