subpage_prot.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2.1 of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. */
  13. #include <assert.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <signal.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/mman.h>
  22. #include <sys/ptrace.h>
  23. #include <sys/syscall.h>
  24. #include <ucontext.h>
  25. #include <unistd.h>
  26. #include "utils.h"
  27. char *file_name;
  28. int in_test;
  29. volatile int faulted;
  30. volatile void *dar;
  31. int errors;
  32. static void segv(int signum, siginfo_t *info, void *ctxt_v)
  33. {
  34. ucontext_t *ctxt = (ucontext_t *)ctxt_v;
  35. struct pt_regs *regs = ctxt->uc_mcontext.regs;
  36. if (!in_test) {
  37. fprintf(stderr, "Segfault outside of test !\n");
  38. exit(1);
  39. }
  40. faulted = 1;
  41. dar = (void *)regs->dar;
  42. regs->nip += 4;
  43. }
  44. static inline void do_read(const volatile void *addr)
  45. {
  46. int ret;
  47. asm volatile("lwz %0,0(%1); twi 0,%0,0; isync;\n"
  48. : "=r" (ret) : "r" (addr) : "memory");
  49. }
  50. static inline void do_write(const volatile void *addr)
  51. {
  52. int val = 0x1234567;
  53. asm volatile("stw %0,0(%1); sync; \n"
  54. : : "r" (val), "r" (addr) : "memory");
  55. }
  56. static inline void check_faulted(void *addr, long page, long subpage, int write)
  57. {
  58. int want_fault = (subpage == ((page + 3) % 16));
  59. if (write)
  60. want_fault |= (subpage == ((page + 1) % 16));
  61. if (faulted != want_fault) {
  62. printf("Failed at 0x%p (p=%ld,sp=%ld,w=%d), want=%s, got=%s !\n",
  63. addr, page, subpage, write,
  64. want_fault ? "fault" : "pass",
  65. faulted ? "fault" : "pass");
  66. ++errors;
  67. }
  68. if (faulted) {
  69. if (dar != addr) {
  70. printf("Fault expected at 0x%p and happened at 0x%p !\n",
  71. addr, dar);
  72. }
  73. faulted = 0;
  74. asm volatile("sync" : : : "memory");
  75. }
  76. }
  77. static int run_test(void *addr, unsigned long size)
  78. {
  79. unsigned int *map;
  80. long i, j, pages, err;
  81. pages = size / 0x10000;
  82. map = malloc(pages * 4);
  83. assert(map);
  84. /*
  85. * for each page, mark subpage i % 16 read only and subpage
  86. * (i + 3) % 16 inaccessible
  87. */
  88. for (i = 0; i < pages; i++) {
  89. map[i] = (0x40000000 >> (((i + 1) * 2) % 32)) |
  90. (0xc0000000 >> (((i + 3) * 2) % 32));
  91. }
  92. err = syscall(__NR_subpage_prot, addr, size, map);
  93. if (err) {
  94. perror("subpage_perm");
  95. return 1;
  96. }
  97. free(map);
  98. in_test = 1;
  99. errors = 0;
  100. for (i = 0; i < pages; i++) {
  101. for (j = 0; j < 16; j++, addr += 0x1000) {
  102. do_read(addr);
  103. check_faulted(addr, i, j, 0);
  104. do_write(addr);
  105. check_faulted(addr, i, j, 1);
  106. }
  107. }
  108. in_test = 0;
  109. if (errors) {
  110. printf("%d errors detected\n", errors);
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. int test_anon(void)
  116. {
  117. unsigned long align;
  118. struct sigaction act = {
  119. .sa_sigaction = segv,
  120. .sa_flags = SA_SIGINFO
  121. };
  122. void *mallocblock;
  123. unsigned long mallocsize;
  124. if (getpagesize() != 0x10000) {
  125. fprintf(stderr, "Kernel page size must be 64K!\n");
  126. return 1;
  127. }
  128. sigaction(SIGSEGV, &act, NULL);
  129. mallocsize = 4 * 16 * 1024 * 1024;
  130. FAIL_IF(posix_memalign(&mallocblock, 64 * 1024, mallocsize));
  131. align = (unsigned long)mallocblock;
  132. if (align & 0xffff)
  133. align = (align | 0xffff) + 1;
  134. mallocblock = (void *)align;
  135. printf("allocated malloc block of 0x%lx bytes at 0x%p\n",
  136. mallocsize, mallocblock);
  137. printf("testing malloc block...\n");
  138. return run_test(mallocblock, mallocsize);
  139. }
  140. int test_file(void)
  141. {
  142. struct sigaction act = {
  143. .sa_sigaction = segv,
  144. .sa_flags = SA_SIGINFO
  145. };
  146. void *fileblock;
  147. off_t filesize;
  148. int fd;
  149. fd = open(file_name, O_RDWR);
  150. if (fd == -1) {
  151. perror("failed to open file");
  152. return 1;
  153. }
  154. sigaction(SIGSEGV, &act, NULL);
  155. filesize = lseek(fd, 0, SEEK_END);
  156. if (filesize & 0xffff)
  157. filesize &= ~0xfffful;
  158. fileblock = mmap(NULL, filesize, PROT_READ | PROT_WRITE,
  159. MAP_SHARED, fd, 0);
  160. if (fileblock == MAP_FAILED) {
  161. perror("failed to map file");
  162. return 1;
  163. }
  164. printf("allocated %s for 0x%lx bytes at 0x%p\n",
  165. file_name, filesize, fileblock);
  166. printf("testing file map...\n");
  167. return run_test(fileblock, filesize);
  168. }
  169. int main(int argc, char *argv[])
  170. {
  171. test_harness(test_anon, "subpage_prot_anon");
  172. if (argc > 1)
  173. file_name = argv[1];
  174. else
  175. file_name = "tempfile";
  176. test_harness(test_file, "subpage_prot_file");
  177. return 0;
  178. }