subpage_prot.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 %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 %p and happened at %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. static int syscall_available(void)
  116. {
  117. int rc;
  118. errno = 0;
  119. rc = syscall(__NR_subpage_prot, 0, 0, 0);
  120. return rc == 0 || (errno != ENOENT && errno != ENOSYS);
  121. }
  122. int test_anon(void)
  123. {
  124. unsigned long align;
  125. struct sigaction act = {
  126. .sa_sigaction = segv,
  127. .sa_flags = SA_SIGINFO
  128. };
  129. void *mallocblock;
  130. unsigned long mallocsize;
  131. SKIP_IF(!syscall_available());
  132. if (getpagesize() != 0x10000) {
  133. fprintf(stderr, "Kernel page size must be 64K!\n");
  134. return 1;
  135. }
  136. sigaction(SIGSEGV, &act, NULL);
  137. mallocsize = 4 * 16 * 1024 * 1024;
  138. FAIL_IF(posix_memalign(&mallocblock, 64 * 1024, mallocsize));
  139. align = (unsigned long)mallocblock;
  140. if (align & 0xffff)
  141. align = (align | 0xffff) + 1;
  142. mallocblock = (void *)align;
  143. printf("allocated malloc block of 0x%lx bytes at %p\n",
  144. mallocsize, mallocblock);
  145. printf("testing malloc block...\n");
  146. return run_test(mallocblock, mallocsize);
  147. }
  148. int test_file(void)
  149. {
  150. struct sigaction act = {
  151. .sa_sigaction = segv,
  152. .sa_flags = SA_SIGINFO
  153. };
  154. void *fileblock;
  155. off_t filesize;
  156. int fd;
  157. SKIP_IF(!syscall_available());
  158. fd = open(file_name, O_RDWR);
  159. if (fd == -1) {
  160. perror("failed to open file");
  161. return 1;
  162. }
  163. sigaction(SIGSEGV, &act, NULL);
  164. filesize = lseek(fd, 0, SEEK_END);
  165. if (filesize & 0xffff)
  166. filesize &= ~0xfffful;
  167. fileblock = mmap(NULL, filesize, PROT_READ | PROT_WRITE,
  168. MAP_SHARED, fd, 0);
  169. if (fileblock == MAP_FAILED) {
  170. perror("failed to map file");
  171. return 1;
  172. }
  173. printf("allocated %s for 0x%lx bytes at %p\n",
  174. file_name, filesize, fileblock);
  175. printf("testing file map...\n");
  176. return run_test(fileblock, filesize);
  177. }
  178. int main(int argc, char *argv[])
  179. {
  180. int rc;
  181. rc = test_harness(test_anon, "subpage_prot_anon");
  182. if (rc)
  183. return rc;
  184. if (argc > 1)
  185. file_name = argv[1];
  186. else
  187. file_name = "tempfile";
  188. return test_harness(test_file, "subpage_prot_file");
  189. }