load_unaligned_zeropad.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "word-at-a-time.h"
  26. #include "utils.h"
  27. static int page_size;
  28. static char *mem_region;
  29. static int protect_region(void)
  30. {
  31. if (mprotect(mem_region + page_size, page_size, PROT_NONE)) {
  32. perror("mprotect");
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. static int unprotect_region(void)
  38. {
  39. if (mprotect(mem_region + page_size, page_size, PROT_READ|PROT_WRITE)) {
  40. perror("mprotect");
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. extern char __start___ex_table[];
  46. extern char __stop___ex_table[];
  47. #if defined(__powerpc64__)
  48. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
  49. #elif defined(__powerpc__)
  50. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
  51. #else
  52. #error implement UCONTEXT_NIA
  53. #endif
  54. static int segv_error;
  55. static void segv_handler(int signr, siginfo_t *info, void *ptr)
  56. {
  57. ucontext_t *uc = (ucontext_t *)ptr;
  58. unsigned long addr = (unsigned long)info->si_addr;
  59. unsigned long *ip = &UCONTEXT_NIA(uc);
  60. unsigned long *ex_p = (unsigned long *)__start___ex_table;
  61. while (ex_p < (unsigned long *)__stop___ex_table) {
  62. unsigned long insn, fixup;
  63. insn = *ex_p++;
  64. fixup = *ex_p++;
  65. if (insn == *ip) {
  66. *ip = fixup;
  67. return;
  68. }
  69. }
  70. printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr);
  71. segv_error++;
  72. }
  73. static void setup_segv_handler(void)
  74. {
  75. struct sigaction action;
  76. memset(&action, 0, sizeof(action));
  77. action.sa_sigaction = segv_handler;
  78. action.sa_flags = SA_SIGINFO;
  79. sigaction(SIGSEGV, &action, NULL);
  80. }
  81. static int do_one_test(char *p, int page_offset)
  82. {
  83. unsigned long should;
  84. unsigned long got;
  85. FAIL_IF(unprotect_region());
  86. should = *(unsigned long *)p;
  87. FAIL_IF(protect_region());
  88. got = load_unaligned_zeropad(p);
  89. if (should != got)
  90. printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should);
  91. return 0;
  92. }
  93. static int test_body(void)
  94. {
  95. unsigned long i;
  96. page_size = getpagesize();
  97. mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,
  98. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  99. FAIL_IF(mem_region == MAP_FAILED);
  100. for (i = 0; i < page_size; i++)
  101. mem_region[i] = i;
  102. memset(mem_region+page_size, 0, page_size);
  103. setup_segv_handler();
  104. for (i = 0; i < page_size; i++)
  105. FAIL_IF(do_one_test(mem_region+i, i));
  106. FAIL_IF(segv_error);
  107. return 0;
  108. }
  109. int main(void)
  110. {
  111. return test_harness(test_body, "load_unaligned_zeropad");
  112. }