0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <assert.h>
0015 #include <errno.h>
0016 #include <fcntl.h>
0017 #include <signal.h>
0018 #include <stdarg.h>
0019 #include <stdio.h>
0020 #include <stdlib.h>
0021 #include <string.h>
0022 #include <sys/mman.h>
0023 #include <sys/ptrace.h>
0024 #include <sys/syscall.h>
0025 #include <ucontext.h>
0026 #include <unistd.h>
0027
0028 #include "utils.h"
0029
0030 char *file_name;
0031
0032 int in_test;
0033 volatile int faulted;
0034 volatile void *dar;
0035 int errors;
0036
0037 static void segv(int signum, siginfo_t *info, void *ctxt_v)
0038 {
0039 ucontext_t *ctxt = (ucontext_t *)ctxt_v;
0040 struct pt_regs *regs = ctxt->uc_mcontext.regs;
0041
0042 if (!in_test) {
0043 fprintf(stderr, "Segfault outside of test !\n");
0044 exit(1);
0045 }
0046
0047 faulted = 1;
0048 dar = (void *)regs->dar;
0049 regs->nip += 4;
0050 }
0051
0052 static inline void do_read(const volatile void *addr)
0053 {
0054 int ret;
0055
0056 asm volatile("lwz %0,0(%1); twi 0,%0,0; isync;\n"
0057 : "=r" (ret) : "r" (addr) : "memory");
0058 }
0059
0060 static inline void do_write(const volatile void *addr)
0061 {
0062 int val = 0x1234567;
0063
0064 asm volatile("stw %0,0(%1); sync; \n"
0065 : : "r" (val), "r" (addr) : "memory");
0066 }
0067
0068 static inline void check_faulted(void *addr, long page, long subpage, int write)
0069 {
0070 int want_fault = (subpage == ((page + 3) % 16));
0071
0072 if (write)
0073 want_fault |= (subpage == ((page + 1) % 16));
0074
0075 if (faulted != want_fault) {
0076 printf("Failed at %p (p=%ld,sp=%ld,w=%d), want=%s, got=%s !\n",
0077 addr, page, subpage, write,
0078 want_fault ? "fault" : "pass",
0079 faulted ? "fault" : "pass");
0080 ++errors;
0081 }
0082
0083 if (faulted) {
0084 if (dar != addr) {
0085 printf("Fault expected at %p and happened at %p !\n",
0086 addr, dar);
0087 }
0088 faulted = 0;
0089 asm volatile("sync" : : : "memory");
0090 }
0091 }
0092
0093 static int run_test(void *addr, unsigned long size)
0094 {
0095 unsigned int *map;
0096 long i, j, pages, err;
0097
0098 pages = size / 0x10000;
0099 map = malloc(pages * 4);
0100 assert(map);
0101
0102
0103
0104
0105
0106 for (i = 0; i < pages; i++) {
0107 map[i] = (0x40000000 >> (((i + 1) * 2) % 32)) |
0108 (0xc0000000 >> (((i + 3) * 2) % 32));
0109 }
0110
0111 err = syscall(__NR_subpage_prot, addr, size, map);
0112 if (err) {
0113 perror("subpage_perm");
0114 return 1;
0115 }
0116 free(map);
0117
0118 in_test = 1;
0119 errors = 0;
0120 for (i = 0; i < pages; i++) {
0121 for (j = 0; j < 16; j++, addr += 0x1000) {
0122 do_read(addr);
0123 check_faulted(addr, i, j, 0);
0124 do_write(addr);
0125 check_faulted(addr, i, j, 1);
0126 }
0127 }
0128
0129 in_test = 0;
0130 if (errors) {
0131 printf("%d errors detected\n", errors);
0132 return 1;
0133 }
0134
0135 return 0;
0136 }
0137
0138 static int syscall_available(void)
0139 {
0140 int rc;
0141
0142 errno = 0;
0143 rc = syscall(__NR_subpage_prot, 0, 0, 0);
0144
0145 return rc == 0 || (errno != ENOENT && errno != ENOSYS);
0146 }
0147
0148 int test_anon(void)
0149 {
0150 unsigned long align;
0151 struct sigaction act = {
0152 .sa_sigaction = segv,
0153 .sa_flags = SA_SIGINFO
0154 };
0155 void *mallocblock;
0156 unsigned long mallocsize;
0157
0158 SKIP_IF(!syscall_available());
0159
0160 if (getpagesize() != 0x10000) {
0161 fprintf(stderr, "Kernel page size must be 64K!\n");
0162 return 1;
0163 }
0164
0165 sigaction(SIGSEGV, &act, NULL);
0166
0167 mallocsize = 4 * 16 * 1024 * 1024;
0168
0169 FAIL_IF(posix_memalign(&mallocblock, 64 * 1024, mallocsize));
0170
0171 align = (unsigned long)mallocblock;
0172 if (align & 0xffff)
0173 align = (align | 0xffff) + 1;
0174
0175 mallocblock = (void *)align;
0176
0177 printf("allocated malloc block of 0x%lx bytes at %p\n",
0178 mallocsize, mallocblock);
0179
0180 printf("testing malloc block...\n");
0181
0182 return run_test(mallocblock, mallocsize);
0183 }
0184
0185 int test_file(void)
0186 {
0187 struct sigaction act = {
0188 .sa_sigaction = segv,
0189 .sa_flags = SA_SIGINFO
0190 };
0191 void *fileblock;
0192 off_t filesize;
0193 int fd;
0194
0195 SKIP_IF(!syscall_available());
0196
0197 fd = open(file_name, O_RDWR);
0198 if (fd == -1) {
0199 perror("failed to open file");
0200 return 1;
0201 }
0202 sigaction(SIGSEGV, &act, NULL);
0203
0204 filesize = lseek(fd, 0, SEEK_END);
0205 if (filesize & 0xffff)
0206 filesize &= ~0xfffful;
0207
0208 fileblock = mmap(NULL, filesize, PROT_READ | PROT_WRITE,
0209 MAP_SHARED, fd, 0);
0210 if (fileblock == MAP_FAILED) {
0211 perror("failed to map file");
0212 return 1;
0213 }
0214 printf("allocated %s for 0x%lx bytes at %p\n",
0215 file_name, filesize, fileblock);
0216
0217 printf("testing file map...\n");
0218
0219 return run_test(fileblock, filesize);
0220 }
0221
0222 int main(int argc, char *argv[])
0223 {
0224 int rc;
0225
0226 rc = test_harness(test_anon, "subpage_prot_anon");
0227 if (rc)
0228 return rc;
0229
0230 if (argc > 1)
0231 file_name = argv[1];
0232 else
0233 file_name = "tempfile";
0234
0235 return test_harness(test_file, "subpage_prot_file");
0236 }