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.
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.
23 #include <sys/ptrace.h>
24 #include <sys/syscall.h>
37 static void segv(int signum
, siginfo_t
*info
, void *ctxt_v
)
39 ucontext_t
*ctxt
= (ucontext_t
*)ctxt_v
;
40 struct pt_regs
*regs
= ctxt
->uc_mcontext
.regs
;
43 fprintf(stderr
, "Segfault outside of test !\n");
48 dar
= (void *)regs
->dar
;
52 static inline void do_read(const volatile void *addr
)
56 asm volatile("lwz %0,0(%1); twi 0,%0,0; isync;\n"
57 : "=r" (ret
) : "r" (addr
) : "memory");
60 static inline void do_write(const volatile void *addr
)
64 asm volatile("stw %0,0(%1); sync; \n"
65 : : "r" (val
), "r" (addr
) : "memory");
68 static inline void check_faulted(void *addr
, long page
, long subpage
, int write
)
70 int want_fault
= (subpage
== ((page
+ 3) % 16));
73 want_fault
|= (subpage
== ((page
+ 1) % 16));
75 if (faulted
!= want_fault
) {
76 printf("Failed at 0x%p (p=%ld,sp=%ld,w=%d), want=%s, got=%s !\n",
77 addr
, page
, subpage
, write
,
78 want_fault
? "fault" : "pass",
79 faulted
? "fault" : "pass");
85 printf("Fault expected at 0x%p and happened at 0x%p !\n",
89 asm volatile("sync" : : : "memory");
93 static int run_test(void *addr
, unsigned long size
)
96 long i
, j
, pages
, err
;
98 pages
= size
/ 0x10000;
99 map
= malloc(pages
* 4);
103 * for each page, mark subpage i % 16 read only and subpage
104 * (i + 3) % 16 inaccessible
106 for (i
= 0; i
< pages
; i
++) {
107 map
[i
] = (0x40000000 >> (((i
+ 1) * 2) % 32)) |
108 (0xc0000000 >> (((i
+ 3) * 2) % 32));
111 err
= syscall(__NR_subpage_prot
, addr
, size
, map
);
113 perror("subpage_perm");
120 for (i
= 0; i
< pages
; i
++) {
121 for (j
= 0; j
< 16; j
++, addr
+= 0x1000) {
123 check_faulted(addr
, i
, j
, 0);
125 check_faulted(addr
, i
, j
, 1);
131 printf("%d errors detected\n", errors
);
141 struct sigaction act
= {
142 .sa_sigaction
= segv
,
143 .sa_flags
= SA_SIGINFO
146 unsigned long mallocsize
;
148 if (getpagesize() != 0x10000) {
149 fprintf(stderr
, "Kernel page size must be 64K!\n");
153 sigaction(SIGSEGV
, &act
, NULL
);
155 mallocsize
= 4 * 16 * 1024 * 1024;
157 FAIL_IF(posix_memalign(&mallocblock
, 64 * 1024, mallocsize
));
159 align
= (unsigned long)mallocblock
;
161 align
= (align
| 0xffff) + 1;
163 mallocblock
= (void *)align
;
165 printf("allocated malloc block of 0x%lx bytes at 0x%p\n",
166 mallocsize
, mallocblock
);
168 printf("testing malloc block...\n");
170 return run_test(mallocblock
, mallocsize
);
175 struct sigaction act
= {
176 .sa_sigaction
= segv
,
177 .sa_flags
= SA_SIGINFO
183 fd
= open(file_name
, O_RDWR
);
185 perror("failed to open file");
188 sigaction(SIGSEGV
, &act
, NULL
);
190 filesize
= lseek(fd
, 0, SEEK_END
);
191 if (filesize
& 0xffff)
192 filesize
&= ~0xfffful
;
194 fileblock
= mmap(NULL
, filesize
, PROT_READ
| PROT_WRITE
,
196 if (fileblock
== MAP_FAILED
) {
197 perror("failed to map file");
200 printf("allocated %s for 0x%lx bytes at 0x%p\n",
201 file_name
, filesize
, fileblock
);
203 printf("testing file map...\n");
205 return run_test(fileblock
, filesize
);
208 int main(int argc
, char *argv
[])
210 test_harness(test_anon
, "subpage_prot_anon");
215 file_name
= "tempfile";
217 test_harness(test_file
, "subpage_prot_file");