load_unaligned_zeropad.c 3.3 KB

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