xref: /xnu-11417.140.69/tools/tests/superpages/testsp.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * This tests the Mac OS X Superpage API introduced in 10.7
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * Note that most of these calls go through the mach_vm_allocate() interface,
5*43a90889SApple OSS Distributions  * but the actually supported and documented interface is the mmap() one
6*43a90889SApple OSS Distributions  * (see mmap(2)).
7*43a90889SApple OSS Distributions  */
8*43a90889SApple OSS Distributions #include <stdio.h>
9*43a90889SApple OSS Distributions #include <stdlib.h>
10*43a90889SApple OSS Distributions #include <signal.h>
11*43a90889SApple OSS Distributions #include <setjmp.h>
12*43a90889SApple OSS Distributions #include <mach/mach.h>
13*43a90889SApple OSS Distributions #include <mach/mach_vm.h>
14*43a90889SApple OSS Distributions #include <time.h>
15*43a90889SApple OSS Distributions #include <unistd.h>
16*43a90889SApple OSS Distributions #include <fcntl.h>
17*43a90889SApple OSS Distributions #include <sys/mman.h>
18*43a90889SApple OSS Distributions 
19*43a90889SApple OSS Distributions #define SUPERPAGE_SIZE (2*1024*1024)
20*43a90889SApple OSS Distributions #define SUPERPAGE_MASK (-SUPERPAGE_SIZE)
21*43a90889SApple OSS Distributions 
22*43a90889SApple OSS Distributions #ifdef __LP64__
23*43a90889SApple OSS Distributions #define FIXED_ADDRESS1 (0x100000000ULL+500*1024*1024) /* at 4 GB + 500 MB virtual */
24*43a90889SApple OSS Distributions #define FIXED_ADDRESS2 (0x100000000ULL+502*1024*1024 + 4*1024) /* at 4 GB + 502 MB + 4 KB virtual */
25*43a90889SApple OSS Distributions #else
26*43a90889SApple OSS Distributions #define FIXED_ADDRESS1 (500*1024*1024) /* at 500 MB virtual */
27*43a90889SApple OSS Distributions #define FIXED_ADDRESS2 (502*1024*1024 + 4*1024) /* at 502 MB + 4 KB virtual */
28*43a90889SApple OSS Distributions #endif
29*43a90889SApple OSS Distributions 
30*43a90889SApple OSS Distributions char error[100];
31*43a90889SApple OSS Distributions 
32*43a90889SApple OSS Distributions jmp_buf resume;
33*43a90889SApple OSS Distributions void
test_signal_handler(int signo)34*43a90889SApple OSS Distributions test_signal_handler(int signo)
35*43a90889SApple OSS Distributions {
36*43a90889SApple OSS Distributions 	longjmp(resume, signo);
37*43a90889SApple OSS Distributions }
38*43a90889SApple OSS Distributions 
39*43a90889SApple OSS Distributions char *signame[32] = {
40*43a90889SApple OSS Distributions 	[SIGBUS] "SIGBUS",
41*43a90889SApple OSS Distributions 	[SIGSEGV] "SIGSEGV"
42*43a90889SApple OSS Distributions };
43*43a90889SApple OSS Distributions 
44*43a90889SApple OSS Distributions typedef struct {
45*43a90889SApple OSS Distributions 	char *description;
46*43a90889SApple OSS Distributions 	boolean_t (*fn)();
47*43a90889SApple OSS Distributions } test_t;
48*43a90889SApple OSS Distributions 
49*43a90889SApple OSS Distributions boolean_t
check_kr(int kr,char * fn)50*43a90889SApple OSS Distributions check_kr(int kr, char *fn)
51*43a90889SApple OSS Distributions {
52*43a90889SApple OSS Distributions 	if (kr) {
53*43a90889SApple OSS Distributions 		sprintf(error, "%s() returned %d", fn, kr);
54*43a90889SApple OSS Distributions 		return FALSE;
55*43a90889SApple OSS Distributions 	}
56*43a90889SApple OSS Distributions 	return TRUE;
57*43a90889SApple OSS Distributions }
58*43a90889SApple OSS Distributions 
59*43a90889SApple OSS Distributions boolean_t
check_addr0(mach_vm_address_t addr,char * fn)60*43a90889SApple OSS Distributions check_addr0(mach_vm_address_t addr, char *fn)
61*43a90889SApple OSS Distributions {
62*43a90889SApple OSS Distributions 	if (!addr) {
63*43a90889SApple OSS Distributions 		sprintf(error, "%s() returned address 0", fn);
64*43a90889SApple OSS Distributions 		return FALSE;
65*43a90889SApple OSS Distributions 	}
66*43a90889SApple OSS Distributions 	return TRUE;
67*43a90889SApple OSS Distributions }
68*43a90889SApple OSS Distributions 
69*43a90889SApple OSS Distributions boolean_t
check_addr(mach_vm_address_t addr1,mach_vm_address_t addr2,char * fn)70*43a90889SApple OSS Distributions check_addr(mach_vm_address_t addr1, mach_vm_address_t addr2, char *fn)
71*43a90889SApple OSS Distributions {
72*43a90889SApple OSS Distributions 	if (addr1 != addr2) {
73*43a90889SApple OSS Distributions 		sprintf(error, "%s() returned address %llx instead of %llx", fn, addr1, addr2);
74*43a90889SApple OSS Distributions 		return FALSE;
75*43a90889SApple OSS Distributions 	}
76*43a90889SApple OSS Distributions 	return TRUE;
77*43a90889SApple OSS Distributions }
78*43a90889SApple OSS Distributions 
79*43a90889SApple OSS Distributions boolean_t
check_align(mach_vm_address_t addr)80*43a90889SApple OSS Distributions check_align(mach_vm_address_t addr)
81*43a90889SApple OSS Distributions {
82*43a90889SApple OSS Distributions 	if (addr & !SUPERPAGE_MASK) {
83*43a90889SApple OSS Distributions 		sprintf(error, "address not aligned properly: 0x%llx", addr);
84*43a90889SApple OSS Distributions 		return FALSE;
85*43a90889SApple OSS Distributions 	}
86*43a90889SApple OSS Distributions 	return TRUE;
87*43a90889SApple OSS Distributions }
88*43a90889SApple OSS Distributions 
89*43a90889SApple OSS Distributions boolean_t
check_r(mach_vm_address_t addr,mach_vm_size_t size,int * res)90*43a90889SApple OSS Distributions check_r(mach_vm_address_t addr, mach_vm_size_t size, int *res)
91*43a90889SApple OSS Distributions {
92*43a90889SApple OSS Distributions 	volatile char *data = (char*)(uintptr_t)addr;
93*43a90889SApple OSS Distributions 	int i, sig, test;
94*43a90889SApple OSS Distributions 
95*43a90889SApple OSS Distributions 	if ((sig = setjmp(resume)) != 0) {
96*43a90889SApple OSS Distributions 		sprintf(error, "%s when reading", signame[sig]);
97*43a90889SApple OSS Distributions 		return FALSE;
98*43a90889SApple OSS Distributions 	}
99*43a90889SApple OSS Distributions 	test = 0;
100*43a90889SApple OSS Distributions 	for (i = 0; i < size; i++) {
101*43a90889SApple OSS Distributions 		test += (data)[i];
102*43a90889SApple OSS Distributions 	}
103*43a90889SApple OSS Distributions 
104*43a90889SApple OSS Distributions 	if (res) {
105*43a90889SApple OSS Distributions 		*res = test;
106*43a90889SApple OSS Distributions 	}
107*43a90889SApple OSS Distributions 
108*43a90889SApple OSS Distributions 	return TRUE;
109*43a90889SApple OSS Distributions }
110*43a90889SApple OSS Distributions 
111*43a90889SApple OSS Distributions /* check that no subpage of the superpage is readable */
112*43a90889SApple OSS Distributions boolean_t
check_nr(mach_vm_address_t addr,mach_vm_size_t size,int * res)113*43a90889SApple OSS Distributions check_nr(mach_vm_address_t addr, mach_vm_size_t size, int *res)
114*43a90889SApple OSS Distributions {
115*43a90889SApple OSS Distributions 	int i;
116*43a90889SApple OSS Distributions 	boolean_t ret;
117*43a90889SApple OSS Distributions 	for (i = 0; i < size / PAGE_SIZE; i++) {
118*43a90889SApple OSS Distributions 		if ((ret = check_r(addr + i * PAGE_SIZE, PAGE_SIZE, res))) {
119*43a90889SApple OSS Distributions 			sprintf(error, "page still readable");
120*43a90889SApple OSS Distributions 			return FALSE;
121*43a90889SApple OSS Distributions 		}
122*43a90889SApple OSS Distributions 	}
123*43a90889SApple OSS Distributions 	return TRUE;
124*43a90889SApple OSS Distributions }
125*43a90889SApple OSS Distributions 
126*43a90889SApple OSS Distributions boolean_t
check_w(mach_vm_address_t addr,mach_vm_size_t size)127*43a90889SApple OSS Distributions check_w(mach_vm_address_t addr, mach_vm_size_t size)
128*43a90889SApple OSS Distributions {
129*43a90889SApple OSS Distributions 	char *data = (char*)(uintptr_t)addr;
130*43a90889SApple OSS Distributions 	int i, sig;
131*43a90889SApple OSS Distributions 
132*43a90889SApple OSS Distributions 	if ((sig = setjmp(resume)) != 0) {
133*43a90889SApple OSS Distributions 		sprintf(error, "%s when writing", signame[sig]);
134*43a90889SApple OSS Distributions 		return FALSE;
135*43a90889SApple OSS Distributions 	}
136*43a90889SApple OSS Distributions 
137*43a90889SApple OSS Distributions 	for (i = 0; i < size; i++) {
138*43a90889SApple OSS Distributions 		(data)[i] = i & 0xFF;
139*43a90889SApple OSS Distributions 	}
140*43a90889SApple OSS Distributions 
141*43a90889SApple OSS Distributions 	return TRUE;
142*43a90889SApple OSS Distributions }
143*43a90889SApple OSS Distributions 
144*43a90889SApple OSS Distributions boolean_t
check_nw(mach_vm_address_t addr,mach_vm_size_t size)145*43a90889SApple OSS Distributions check_nw(mach_vm_address_t addr, mach_vm_size_t size)
146*43a90889SApple OSS Distributions {
147*43a90889SApple OSS Distributions 	int i;
148*43a90889SApple OSS Distributions 	boolean_t ret;
149*43a90889SApple OSS Distributions 
150*43a90889SApple OSS Distributions 	for (i = 0; i < size / PAGE_SIZE; i++) {
151*43a90889SApple OSS Distributions 		if ((ret = check_w(addr + i * PAGE_SIZE, PAGE_SIZE))) {
152*43a90889SApple OSS Distributions 			sprintf(error, "page still writable");
153*43a90889SApple OSS Distributions 			return FALSE;
154*43a90889SApple OSS Distributions 		}
155*43a90889SApple OSS Distributions 	}
156*43a90889SApple OSS Distributions 	return TRUE;
157*43a90889SApple OSS Distributions }
158*43a90889SApple OSS Distributions 
159*43a90889SApple OSS Distributions boolean_t
check_rw(mach_vm_address_t addr,mach_vm_size_t size)160*43a90889SApple OSS Distributions check_rw(mach_vm_address_t addr, mach_vm_size_t size)
161*43a90889SApple OSS Distributions {
162*43a90889SApple OSS Distributions 	int ret;
163*43a90889SApple OSS Distributions 	int res;
164*43a90889SApple OSS Distributions 	if (!(ret = check_w(addr, size))) {
165*43a90889SApple OSS Distributions 		return ret;
166*43a90889SApple OSS Distributions 	}
167*43a90889SApple OSS Distributions 	if (!(ret = check_r(addr, size, &res))) {
168*43a90889SApple OSS Distributions 		return ret;
169*43a90889SApple OSS Distributions 	}
170*43a90889SApple OSS Distributions 	if ((size == SUPERPAGE_SIZE) && (res != 0xfff00000)) {
171*43a90889SApple OSS Distributions 		sprintf(error, "checksum error");
172*43a90889SApple OSS Distributions 		return FALSE;
173*43a90889SApple OSS Distributions 	}
174*43a90889SApple OSS Distributions 
175*43a90889SApple OSS Distributions 	return TRUE;
176*43a90889SApple OSS Distributions }
177*43a90889SApple OSS Distributions 
178*43a90889SApple OSS Distributions mach_vm_address_t global_addr = 0;
179*43a90889SApple OSS Distributions mach_vm_size_t  global_size = 0;
180*43a90889SApple OSS Distributions 
181*43a90889SApple OSS Distributions /*
182*43a90889SApple OSS Distributions  * If we allocate a 2 MB superpage read-write without specifying an address,
183*43a90889SApple OSS Distributions  * - the call should succeed
184*43a90889SApple OSS Distributions  * - not return 0
185*43a90889SApple OSS Distributions  * - return a 2 MB aligned address
186*43a90889SApple OSS Distributions  * - the memory should be readable and writable
187*43a90889SApple OSS Distributions  */
188*43a90889SApple OSS Distributions boolean_t
test_allocate()189*43a90889SApple OSS Distributions test_allocate()
190*43a90889SApple OSS Distributions {
191*43a90889SApple OSS Distributions 	int kr, ret;
192*43a90889SApple OSS Distributions 
193*43a90889SApple OSS Distributions 	global_addr = 0;
194*43a90889SApple OSS Distributions 	global_size = SUPERPAGE_SIZE;
195*43a90889SApple OSS Distributions 
196*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &global_addr, global_size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
197*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
198*43a90889SApple OSS Distributions 		return ret;
199*43a90889SApple OSS Distributions 	}
200*43a90889SApple OSS Distributions 	if (!(ret = check_addr0(global_addr, "mach_vm_allocate"))) {
201*43a90889SApple OSS Distributions 		return ret;
202*43a90889SApple OSS Distributions 	}
203*43a90889SApple OSS Distributions 	if (!(ret = check_align(global_addr))) {
204*43a90889SApple OSS Distributions 		return ret;
205*43a90889SApple OSS Distributions 	}
206*43a90889SApple OSS Distributions 	if (!(ret = check_rw(global_addr, global_size))) {
207*43a90889SApple OSS Distributions 		return ret;
208*43a90889SApple OSS Distributions 	}
209*43a90889SApple OSS Distributions 
210*43a90889SApple OSS Distributions 	return TRUE;
211*43a90889SApple OSS Distributions }
212*43a90889SApple OSS Distributions 
213*43a90889SApple OSS Distributions /*
214*43a90889SApple OSS Distributions  * If we deallocate a superpage,
215*43a90889SApple OSS Distributions  * - the call should succeed
216*43a90889SApple OSS Distributions  * - make the memory inaccessible
217*43a90889SApple OSS Distributions  */
218*43a90889SApple OSS Distributions boolean_t
test_deallocate()219*43a90889SApple OSS Distributions test_deallocate()
220*43a90889SApple OSS Distributions {
221*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
222*43a90889SApple OSS Distributions 	int kr, ret;
223*43a90889SApple OSS Distributions 
224*43a90889SApple OSS Distributions 	if (!global_addr) {
225*43a90889SApple OSS Distributions 		sprintf(error, "skipped deallocation");
226*43a90889SApple OSS Distributions 		return FALSE;
227*43a90889SApple OSS Distributions 	}
228*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), global_addr, global_size);
229*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
230*43a90889SApple OSS Distributions 		return ret;
231*43a90889SApple OSS Distributions 	}
232*43a90889SApple OSS Distributions 	if (!(ret = check_nr(global_addr, size, NULL))) {
233*43a90889SApple OSS Distributions 		return ret;
234*43a90889SApple OSS Distributions 	}
235*43a90889SApple OSS Distributions 	return TRUE;
236*43a90889SApple OSS Distributions }
237*43a90889SApple OSS Distributions 
238*43a90889SApple OSS Distributions /*
239*43a90889SApple OSS Distributions  * If we allocate a superpage of any size read-write without specifying an address
240*43a90889SApple OSS Distributions  * - the call should succeed
241*43a90889SApple OSS Distributions  * - not return 0
242*43a90889SApple OSS Distributions  * - the memory should be readable and writable
243*43a90889SApple OSS Distributions  * If we deallocate it,
244*43a90889SApple OSS Distributions  * - the call should succeed
245*43a90889SApple OSS Distributions  * - make the memory inaccessible
246*43a90889SApple OSS Distributions  */
247*43a90889SApple OSS Distributions boolean_t
test_allocate_size_any()248*43a90889SApple OSS Distributions test_allocate_size_any()
249*43a90889SApple OSS Distributions {
250*43a90889SApple OSS Distributions 	int kr;
251*43a90889SApple OSS Distributions 	int ret;
252*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
253*43a90889SApple OSS Distributions 	mach_vm_size_t  size = 2 * PAGE_SIZE; /* will be rounded up to some superpage size */
254*43a90889SApple OSS Distributions 
255*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_ANY);
256*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
257*43a90889SApple OSS Distributions 		return ret;
258*43a90889SApple OSS Distributions 	}
259*43a90889SApple OSS Distributions 	if (!(ret = check_addr0(addr, "mach_vm_allocate"))) {
260*43a90889SApple OSS Distributions 		return ret;
261*43a90889SApple OSS Distributions 	}
262*43a90889SApple OSS Distributions 	if (!(ret = check_rw(addr, size))) {
263*43a90889SApple OSS Distributions 		return ret;
264*43a90889SApple OSS Distributions 	}
265*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
266*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
267*43a90889SApple OSS Distributions 		return ret;
268*43a90889SApple OSS Distributions 	}
269*43a90889SApple OSS Distributions 	if (!(ret = check_nr(addr, size, NULL))) {
270*43a90889SApple OSS Distributions 		return ret;
271*43a90889SApple OSS Distributions 	}
272*43a90889SApple OSS Distributions 	return TRUE;
273*43a90889SApple OSS Distributions }
274*43a90889SApple OSS Distributions 
275*43a90889SApple OSS Distributions /*
276*43a90889SApple OSS Distributions  * If we allocate a 2 MB superpage read-write at a 2 MB aligned address,
277*43a90889SApple OSS Distributions  * - the call should succeed
278*43a90889SApple OSS Distributions  * - return the address we wished for
279*43a90889SApple OSS Distributions  * - the memory should be readable and writable
280*43a90889SApple OSS Distributions  * If we deallocate it,
281*43a90889SApple OSS Distributions  * - the call should succeed
282*43a90889SApple OSS Distributions  * - make the memory inaccessible
283*43a90889SApple OSS Distributions  */
284*43a90889SApple OSS Distributions boolean_t
test_allocatefixed()285*43a90889SApple OSS Distributions test_allocatefixed()
286*43a90889SApple OSS Distributions {
287*43a90889SApple OSS Distributions 	int kr;
288*43a90889SApple OSS Distributions 	int ret;
289*43a90889SApple OSS Distributions 	mach_vm_address_t addr = FIXED_ADDRESS1;
290*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
291*43a90889SApple OSS Distributions 
292*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_SUPERPAGE_SIZE_2MB);
293*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
294*43a90889SApple OSS Distributions 		return ret;
295*43a90889SApple OSS Distributions 	}
296*43a90889SApple OSS Distributions 	if (!(ret = check_addr(addr, FIXED_ADDRESS1, "mach_vm_allocate"))) {
297*43a90889SApple OSS Distributions 		return ret;
298*43a90889SApple OSS Distributions 	}
299*43a90889SApple OSS Distributions 	if (!(ret = check_rw(addr, size))) {
300*43a90889SApple OSS Distributions 		return ret;
301*43a90889SApple OSS Distributions 	}
302*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
303*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
304*43a90889SApple OSS Distributions 		return ret;
305*43a90889SApple OSS Distributions 	}
306*43a90889SApple OSS Distributions 	if (!(ret = check_nr(addr, size, NULL))) {
307*43a90889SApple OSS Distributions 		return ret;
308*43a90889SApple OSS Distributions 	}
309*43a90889SApple OSS Distributions 	return TRUE;
310*43a90889SApple OSS Distributions }
311*43a90889SApple OSS Distributions 
312*43a90889SApple OSS Distributions /*
313*43a90889SApple OSS Distributions  * If we allocate a 2 MB superpage read-write at an unaligned address,
314*43a90889SApple OSS Distributions  * - the call should fail
315*43a90889SApple OSS Distributions  */
316*43a90889SApple OSS Distributions boolean_t
test_allocateunalignedfixed()317*43a90889SApple OSS Distributions test_allocateunalignedfixed()
318*43a90889SApple OSS Distributions {
319*43a90889SApple OSS Distributions 	int kr;
320*43a90889SApple OSS Distributions 	int ret;
321*43a90889SApple OSS Distributions 	mach_vm_address_t addr = FIXED_ADDRESS2;
322*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
323*43a90889SApple OSS Distributions 
324*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_SUPERPAGE_SIZE_2MB);
325*43a90889SApple OSS Distributions 	/* is supposed to fail */
326*43a90889SApple OSS Distributions 	if ((ret = check_kr(kr, "mach_vm_allocate"))) {
327*43a90889SApple OSS Distributions 		sprintf(error, "mach_vm_allocate() should have failed");
328*43a90889SApple OSS Distributions 		return FALSE;
329*43a90889SApple OSS Distributions 	}
330*43a90889SApple OSS Distributions 	return TRUE;
331*43a90889SApple OSS Distributions }
332*43a90889SApple OSS Distributions 
333*43a90889SApple OSS Distributions /*
334*43a90889SApple OSS Distributions  * If we allocate an amount of memory not divisible by 2 MB as a 2 MB superpage
335*43a90889SApple OSS Distributions  * - the call should fail
336*43a90889SApple OSS Distributions  */
337*43a90889SApple OSS Distributions boolean_t
test_allocateoddsize()338*43a90889SApple OSS Distributions test_allocateoddsize()
339*43a90889SApple OSS Distributions {
340*43a90889SApple OSS Distributions 	int kr;
341*43a90889SApple OSS Distributions 	int ret;
342*43a90889SApple OSS Distributions 	mach_vm_address_t addr = FIXED_ADDRESS1;
343*43a90889SApple OSS Distributions 	mach_vm_size_t  size = PAGE_SIZE; /* != 2 MB */
344*43a90889SApple OSS Distributions 
345*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_SUPERPAGE_SIZE_2MB);
346*43a90889SApple OSS Distributions 	/* is supposed to fail */
347*43a90889SApple OSS Distributions 	if ((ret = check_kr(kr, "mach_vm_allocate"))) {
348*43a90889SApple OSS Distributions 		sprintf(error, "mach_vm_allocate() should have failed");
349*43a90889SApple OSS Distributions 		return FALSE;
350*43a90889SApple OSS Distributions 	}
351*43a90889SApple OSS Distributions 	return TRUE;
352*43a90889SApple OSS Distributions }
353*43a90889SApple OSS Distributions 
354*43a90889SApple OSS Distributions /*
355*43a90889SApple OSS Distributions  * If we deallocate a sub-page of a superpage,
356*43a90889SApple OSS Distributions  * - the call should succeed
357*43a90889SApple OSS Distributions  * - make the complete memory inaccessible
358*43a90889SApple OSS Distributions  */
359*43a90889SApple OSS Distributions boolean_t
test_deallocatesubpage()360*43a90889SApple OSS Distributions test_deallocatesubpage()
361*43a90889SApple OSS Distributions {
362*43a90889SApple OSS Distributions 	int kr;
363*43a90889SApple OSS Distributions 	int ret;
364*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
365*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
366*43a90889SApple OSS Distributions 
367*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
368*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
369*43a90889SApple OSS Distributions 		return ret;
370*43a90889SApple OSS Distributions 	}
371*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr + PAGE_SIZE, size);
372*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
373*43a90889SApple OSS Distributions 		return ret;
374*43a90889SApple OSS Distributions 	}
375*43a90889SApple OSS Distributions 	if (!(ret = check_nr(addr, size, NULL))) {
376*43a90889SApple OSS Distributions 		return ret;
377*43a90889SApple OSS Distributions 	}
378*43a90889SApple OSS Distributions 	return TRUE;
379*43a90889SApple OSS Distributions }
380*43a90889SApple OSS Distributions 
381*43a90889SApple OSS Distributions /*
382*43a90889SApple OSS Distributions  * If we try to allocate memory occupied by superpages as normal pages
383*43a90889SApple OSS Distributions  * - the call should fail
384*43a90889SApple OSS Distributions  */
385*43a90889SApple OSS Distributions boolean_t
test_reallocate()386*43a90889SApple OSS Distributions test_reallocate()
387*43a90889SApple OSS Distributions {
388*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0, addr2;
389*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
390*43a90889SApple OSS Distributions 	int kr, ret;
391*43a90889SApple OSS Distributions 	int i;
392*43a90889SApple OSS Distributions 
393*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
394*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
395*43a90889SApple OSS Distributions 		return ret;
396*43a90889SApple OSS Distributions 	}
397*43a90889SApple OSS Distributions 
398*43a90889SApple OSS Distributions 	/* attempt to allocate every sub-page of superpage */
399*43a90889SApple OSS Distributions 	for (i = 0; i < SUPERPAGE_SIZE / PAGE_SIZE; i++) {
400*43a90889SApple OSS Distributions 		addr2 = addr + i * PAGE_SIZE;
401*43a90889SApple OSS Distributions 		size = PAGE_SIZE;
402*43a90889SApple OSS Distributions 		kr = mach_vm_allocate(mach_task_self(), &addr2, size, 0);
403*43a90889SApple OSS Distributions 		if ((ret = check_kr(kr, "mach_vm_allocate"))) {
404*43a90889SApple OSS Distributions 			sprintf(error, "could allocate already allocated space, page %d", i);
405*43a90889SApple OSS Distributions 			mach_vm_deallocate(mach_task_self(), addr, size);
406*43a90889SApple OSS Distributions 			return FALSE;
407*43a90889SApple OSS Distributions 		}
408*43a90889SApple OSS Distributions 	}
409*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
410*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
411*43a90889SApple OSS Distributions 		return ret;
412*43a90889SApple OSS Distributions 	}
413*43a90889SApple OSS Distributions 	return TRUE;
414*43a90889SApple OSS Distributions }
415*43a90889SApple OSS Distributions 
416*43a90889SApple OSS Distributions /*
417*43a90889SApple OSS Distributions  * If we try to wire superpages
418*43a90889SApple OSS Distributions  * - the call should succeed
419*43a90889SApple OSS Distributions  * - the memory should remain readable and writable
420*43a90889SApple OSS Distributions  */
421*43a90889SApple OSS Distributions boolean_t
test_wire()422*43a90889SApple OSS Distributions test_wire()
423*43a90889SApple OSS Distributions {
424*43a90889SApple OSS Distributions 	int kr;
425*43a90889SApple OSS Distributions 	int ret;
426*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
427*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
428*43a90889SApple OSS Distributions 
429*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
430*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
431*43a90889SApple OSS Distributions 		return ret;
432*43a90889SApple OSS Distributions 	}
433*43a90889SApple OSS Distributions 
434*43a90889SApple OSS Distributions 	kr = mach_vm_wire(mach_host_self(), mach_task_self(), addr, size, VM_PROT_WRITE | VM_PROT_READ);
435*43a90889SApple OSS Distributions 
436*43a90889SApple OSS Distributions 	if (!geteuid()) { /* may fail as user */
437*43a90889SApple OSS Distributions 		if (!(ret = check_kr(kr, "mach_vm_wire"))) {
438*43a90889SApple OSS Distributions 			return ret;
439*43a90889SApple OSS Distributions 		}
440*43a90889SApple OSS Distributions 	}
441*43a90889SApple OSS Distributions 
442*43a90889SApple OSS Distributions 	if (!(ret = check_rw(addr, size))) {
443*43a90889SApple OSS Distributions 		return ret;
444*43a90889SApple OSS Distributions 	}
445*43a90889SApple OSS Distributions 
446*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
447*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
448*43a90889SApple OSS Distributions 		return ret;
449*43a90889SApple OSS Distributions 	}
450*43a90889SApple OSS Distributions 
451*43a90889SApple OSS Distributions 	return TRUE;
452*43a90889SApple OSS Distributions }
453*43a90889SApple OSS Distributions 
454*43a90889SApple OSS Distributions /*
455*43a90889SApple OSS Distributions  * If we try to wire superpages
456*43a90889SApple OSS Distributions  * - the call should fail
457*43a90889SApple OSS Distributions  * - the memory should remain readable and writable
458*43a90889SApple OSS Distributions  * Currently, superpages are always wired.
459*43a90889SApple OSS Distributions  */
460*43a90889SApple OSS Distributions boolean_t
test_unwire()461*43a90889SApple OSS Distributions test_unwire()
462*43a90889SApple OSS Distributions {
463*43a90889SApple OSS Distributions 	int kr;
464*43a90889SApple OSS Distributions 	int ret;
465*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
466*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
467*43a90889SApple OSS Distributions 
468*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
469*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
470*43a90889SApple OSS Distributions 		return ret;
471*43a90889SApple OSS Distributions 	}
472*43a90889SApple OSS Distributions 
473*43a90889SApple OSS Distributions 	kr = mach_vm_wire(mach_host_self(), mach_task_self(), addr, size, VM_PROT_NONE);
474*43a90889SApple OSS Distributions 	if ((ret = check_kr(kr, "mach_vm_wire"))) {
475*43a90889SApple OSS Distributions 		sprintf(error, "could unwire");
476*43a90889SApple OSS Distributions 		return FALSE;
477*43a90889SApple OSS Distributions 	}
478*43a90889SApple OSS Distributions 
479*43a90889SApple OSS Distributions 	if (!(ret = check_rw(addr, size))) {
480*43a90889SApple OSS Distributions 		return ret;
481*43a90889SApple OSS Distributions 	}
482*43a90889SApple OSS Distributions 
483*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
484*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
485*43a90889SApple OSS Distributions 		return ret;
486*43a90889SApple OSS Distributions 	}
487*43a90889SApple OSS Distributions 
488*43a90889SApple OSS Distributions 	return TRUE;
489*43a90889SApple OSS Distributions }
490*43a90889SApple OSS Distributions 
491*43a90889SApple OSS Distributions /*
492*43a90889SApple OSS Distributions  * If we try to write-protect superpages
493*43a90889SApple OSS Distributions  * - the call should succeed
494*43a90889SApple OSS Distributions  * - the memory should remain readable
495*43a90889SApple OSS Distributions  * - the memory should not be writable
496*43a90889SApple OSS Distributions  */
497*43a90889SApple OSS Distributions boolean_t
test_readonly()498*43a90889SApple OSS Distributions test_readonly()
499*43a90889SApple OSS Distributions {
500*43a90889SApple OSS Distributions 	int kr;
501*43a90889SApple OSS Distributions 	int ret;
502*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
503*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
504*43a90889SApple OSS Distributions 
505*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
506*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
507*43a90889SApple OSS Distributions 		return ret;
508*43a90889SApple OSS Distributions 	}
509*43a90889SApple OSS Distributions 
510*43a90889SApple OSS Distributions 	mach_vm_protect(mach_task_self(), addr, size, 0, VM_PROT_READ);
511*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_protect"))) {
512*43a90889SApple OSS Distributions 		return ret;
513*43a90889SApple OSS Distributions 	}
514*43a90889SApple OSS Distributions 
515*43a90889SApple OSS Distributions 	if (!(ret = check_r(addr, size, NULL))) {
516*43a90889SApple OSS Distributions 		return ret;
517*43a90889SApple OSS Distributions 	}
518*43a90889SApple OSS Distributions 	if (!(ret = check_nw(addr, size))) {
519*43a90889SApple OSS Distributions 		return ret;
520*43a90889SApple OSS Distributions 	}
521*43a90889SApple OSS Distributions 
522*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
523*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
524*43a90889SApple OSS Distributions 		return ret;
525*43a90889SApple OSS Distributions 	}
526*43a90889SApple OSS Distributions 
527*43a90889SApple OSS Distributions 	return TRUE;
528*43a90889SApple OSS Distributions }
529*43a90889SApple OSS Distributions 
530*43a90889SApple OSS Distributions /*
531*43a90889SApple OSS Distributions  * If we try to write-protect a sub-page of a superpage
532*43a90889SApple OSS Distributions  * - the call should succeed
533*43a90889SApple OSS Distributions  * - the complete memory should remain readable
534*43a90889SApple OSS Distributions  * - the complete memory should not be writable
535*43a90889SApple OSS Distributions  */
536*43a90889SApple OSS Distributions boolean_t
test_readonlysubpage()537*43a90889SApple OSS Distributions test_readonlysubpage()
538*43a90889SApple OSS Distributions {
539*43a90889SApple OSS Distributions 	int kr;
540*43a90889SApple OSS Distributions 	int ret;
541*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
542*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
543*43a90889SApple OSS Distributions 
544*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
545*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
546*43a90889SApple OSS Distributions 		return ret;
547*43a90889SApple OSS Distributions 	}
548*43a90889SApple OSS Distributions 
549*43a90889SApple OSS Distributions 	mach_vm_protect(mach_task_self(), addr + PAGE_SIZE, PAGE_SIZE, 0, VM_PROT_READ);
550*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_protect"))) {
551*43a90889SApple OSS Distributions 		return ret;
552*43a90889SApple OSS Distributions 	}
553*43a90889SApple OSS Distributions 
554*43a90889SApple OSS Distributions 	if (!(ret = check_r(addr, size, NULL))) {
555*43a90889SApple OSS Distributions 		return ret;
556*43a90889SApple OSS Distributions 	}
557*43a90889SApple OSS Distributions 	if (!(ret = check_nw(addr, size))) {
558*43a90889SApple OSS Distributions 		return ret;
559*43a90889SApple OSS Distributions 	}
560*43a90889SApple OSS Distributions 
561*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
562*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
563*43a90889SApple OSS Distributions 		return ret;
564*43a90889SApple OSS Distributions 	}
565*43a90889SApple OSS Distributions 
566*43a90889SApple OSS Distributions 	return TRUE;
567*43a90889SApple OSS Distributions }
568*43a90889SApple OSS Distributions 
569*43a90889SApple OSS Distributions /*
570*43a90889SApple OSS Distributions  * If we fork with active superpages
571*43a90889SApple OSS Distributions  * - the parent should still be able to access the superpages
572*43a90889SApple OSS Distributions  * - the child should not be able to access the superpages
573*43a90889SApple OSS Distributions  */
574*43a90889SApple OSS Distributions boolean_t
test_fork()575*43a90889SApple OSS Distributions test_fork()
576*43a90889SApple OSS Distributions {
577*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
578*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
579*43a90889SApple OSS Distributions 	int kr, ret;
580*43a90889SApple OSS Distributions 	pid_t pid;
581*43a90889SApple OSS Distributions 
582*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
583*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
584*43a90889SApple OSS Distributions 		return ret;
585*43a90889SApple OSS Distributions 	}
586*43a90889SApple OSS Distributions 
587*43a90889SApple OSS Distributions 	fflush(stdout);
588*43a90889SApple OSS Distributions 	if ((pid = fork())) { /* parent */
589*43a90889SApple OSS Distributions 		if (!(ret = check_rw(addr, size))) {
590*43a90889SApple OSS Distributions 			return ret;
591*43a90889SApple OSS Distributions 		}
592*43a90889SApple OSS Distributions 		waitpid(pid, &ret, 0);
593*43a90889SApple OSS Distributions 		if (!ret) {
594*43a90889SApple OSS Distributions 			sprintf(error, "child could access superpage");
595*43a90889SApple OSS Distributions 			return ret;
596*43a90889SApple OSS Distributions 		}
597*43a90889SApple OSS Distributions 	} else { /* child */
598*43a90889SApple OSS Distributions 		if (!(ret = check_nr(addr, size, NULL))) {
599*43a90889SApple OSS Distributions 			exit(ret);
600*43a90889SApple OSS Distributions 		}
601*43a90889SApple OSS Distributions 		exit(TRUE);
602*43a90889SApple OSS Distributions 	}
603*43a90889SApple OSS Distributions 
604*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
605*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
606*43a90889SApple OSS Distributions 		return ret;
607*43a90889SApple OSS Distributions 	}
608*43a90889SApple OSS Distributions 	return TRUE;
609*43a90889SApple OSS Distributions }
610*43a90889SApple OSS Distributions 
611*43a90889SApple OSS Distributions /*
612*43a90889SApple OSS Distributions  * Doing file I/O with superpages
613*43a90889SApple OSS Distributions  * - should succeed
614*43a90889SApple OSS Distributions  * - should behave the same as with base pages (i.e. no bad data)
615*43a90889SApple OSS Distributions  */
616*43a90889SApple OSS Distributions #define FILENAME "/System/Library/Kernels/kernel"
617*43a90889SApple OSS Distributions boolean_t
test_fileio()618*43a90889SApple OSS Distributions test_fileio()
619*43a90889SApple OSS Distributions {
620*43a90889SApple OSS Distributions 	mach_vm_address_t addr1 = 0;
621*43a90889SApple OSS Distributions 	mach_vm_address_t addr2 = 0;
622*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
623*43a90889SApple OSS Distributions 	int kr, ret;
624*43a90889SApple OSS Distributions 	int fd;
625*43a90889SApple OSS Distributions 	unsigned int bytes;
626*43a90889SApple OSS Distributions 
627*43a90889SApple OSS Distributions 	/* allocate one superpage */
628*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr1, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
629*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate (1)"))) {
630*43a90889SApple OSS Distributions 		return ret;
631*43a90889SApple OSS Distributions 	}
632*43a90889SApple OSS Distributions 
633*43a90889SApple OSS Distributions 	/* allocate base pages (superpage-sized) */
634*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr2, size, VM_FLAGS_ANYWHERE);
635*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate (2)"))) {
636*43a90889SApple OSS Distributions 		return ret;
637*43a90889SApple OSS Distributions 	}
638*43a90889SApple OSS Distributions 
639*43a90889SApple OSS Distributions 	if ((fd = open(FILENAME, O_RDONLY)) < 0) {
640*43a90889SApple OSS Distributions 		sprintf(error, "couldn't open %s", FILENAME);
641*43a90889SApple OSS Distributions 		return FALSE;
642*43a90889SApple OSS Distributions 	}
643*43a90889SApple OSS Distributions 	fcntl(fd, F_NOCACHE, 1);
644*43a90889SApple OSS Distributions 	/* read kernel into superpage */
645*43a90889SApple OSS Distributions 	if ((bytes = read(fd, (void*)(uintptr_t)addr1, SUPERPAGE_SIZE)) < SUPERPAGE_SIZE) {
646*43a90889SApple OSS Distributions 		sprintf(error, "short read (1)");
647*43a90889SApple OSS Distributions 		return FALSE;
648*43a90889SApple OSS Distributions 	}
649*43a90889SApple OSS Distributions 	lseek(fd, 0, SEEK_SET);
650*43a90889SApple OSS Distributions 	/* read kernel into base pages */
651*43a90889SApple OSS Distributions 	if ((bytes = read(fd, (void*)(uintptr_t)addr2, SUPERPAGE_SIZE)) < SUPERPAGE_SIZE) {
652*43a90889SApple OSS Distributions 		sprintf(error, "short read (2)");
653*43a90889SApple OSS Distributions 		return FALSE;
654*43a90889SApple OSS Distributions 	}
655*43a90889SApple OSS Distributions 	close(fd);
656*43a90889SApple OSS Distributions 
657*43a90889SApple OSS Distributions 	/* compare */
658*43a90889SApple OSS Distributions 	if (memcmp((void*)(uintptr_t)addr1, (void*)(uintptr_t)addr2, bytes)) {
659*43a90889SApple OSS Distributions 		sprintf(error, "read data corrupt");
660*43a90889SApple OSS Distributions 		return FALSE;
661*43a90889SApple OSS Distributions 	}
662*43a90889SApple OSS Distributions 
663*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr1, size);
664*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate (1)"))) {
665*43a90889SApple OSS Distributions 		return ret;
666*43a90889SApple OSS Distributions 	}
667*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr2, size);
668*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate (2)"))) {
669*43a90889SApple OSS Distributions 		return ret;
670*43a90889SApple OSS Distributions 	}
671*43a90889SApple OSS Distributions 	return TRUE;
672*43a90889SApple OSS Distributions }
673*43a90889SApple OSS Distributions 
674*43a90889SApple OSS Distributions /*
675*43a90889SApple OSS Distributions  * The mmap() interface should work just as well!
676*43a90889SApple OSS Distributions  */
677*43a90889SApple OSS Distributions boolean_t
test_mmap()678*43a90889SApple OSS Distributions test_mmap()
679*43a90889SApple OSS Distributions {
680*43a90889SApple OSS Distributions 	int kr, ret;
681*43a90889SApple OSS Distributions 	uintptr_t addr = 0;
682*43a90889SApple OSS Distributions 	int size = SUPERPAGE_SIZE;
683*43a90889SApple OSS Distributions 
684*43a90889SApple OSS Distributions 	addr = (uintptr_t)mmap((void*)addr, size, PROT_READ, MAP_ANON | MAP_PRIVATE, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
685*43a90889SApple OSS Distributions 	if (addr == (uintptr_t)MAP_FAILED) {
686*43a90889SApple OSS Distributions 		sprintf(error, "mmap()");
687*43a90889SApple OSS Distributions 		return FALSE;
688*43a90889SApple OSS Distributions 	}
689*43a90889SApple OSS Distributions 	if (!(ret = check_addr0(addr, "mach_vm_allocate"))) {
690*43a90889SApple OSS Distributions 		return ret;
691*43a90889SApple OSS Distributions 	}
692*43a90889SApple OSS Distributions 	if (!(ret = check_align(addr))) {
693*43a90889SApple OSS Distributions 		return ret;
694*43a90889SApple OSS Distributions 	}
695*43a90889SApple OSS Distributions 	if (!(ret = check_r(addr, SUPERPAGE_SIZE, NULL))) {
696*43a90889SApple OSS Distributions 		return ret;
697*43a90889SApple OSS Distributions 	}
698*43a90889SApple OSS Distributions 	if (!(ret = check_nw(addr, SUPERPAGE_SIZE))) {
699*43a90889SApple OSS Distributions 		return ret;
700*43a90889SApple OSS Distributions 	}
701*43a90889SApple OSS Distributions 	kr = munmap((void*)addr, size);
702*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "munmap"))) {
703*43a90889SApple OSS Distributions 		return ret;
704*43a90889SApple OSS Distributions 	}
705*43a90889SApple OSS Distributions 	if (!(ret = check_nr(addr, size, NULL))) {
706*43a90889SApple OSS Distributions 		return ret;
707*43a90889SApple OSS Distributions 	}
708*43a90889SApple OSS Distributions 
709*43a90889SApple OSS Distributions 	return TRUE;
710*43a90889SApple OSS Distributions }
711*43a90889SApple OSS Distributions 
712*43a90889SApple OSS Distributions /*
713*43a90889SApple OSS Distributions  * Tests one allocation/deallocaton cycle; used in a loop this tests for leaks
714*43a90889SApple OSS Distributions  */
715*43a90889SApple OSS Distributions boolean_t
test_alloc_dealloc()716*43a90889SApple OSS Distributions test_alloc_dealloc()
717*43a90889SApple OSS Distributions {
718*43a90889SApple OSS Distributions 	mach_vm_address_t addr = 0;
719*43a90889SApple OSS Distributions 	mach_vm_size_t  size = SUPERPAGE_SIZE;
720*43a90889SApple OSS Distributions 	int kr, ret;
721*43a90889SApple OSS Distributions 
722*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB);
723*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_allocate"))) {
724*43a90889SApple OSS Distributions 		return ret;
725*43a90889SApple OSS Distributions 	}
726*43a90889SApple OSS Distributions 	if (!(ret = check_addr0(addr, "mach_vm_allocate"))) {
727*43a90889SApple OSS Distributions 		return ret;
728*43a90889SApple OSS Distributions 	}
729*43a90889SApple OSS Distributions 	if (!(ret = check_align(addr))) {
730*43a90889SApple OSS Distributions 		return ret;
731*43a90889SApple OSS Distributions 	}
732*43a90889SApple OSS Distributions 	if (!(ret = check_rw(addr, size))) {
733*43a90889SApple OSS Distributions 		return ret;
734*43a90889SApple OSS Distributions 	}
735*43a90889SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), addr, size);
736*43a90889SApple OSS Distributions 	if (!(ret = check_kr(kr, "mach_vm_deallocate"))) {
737*43a90889SApple OSS Distributions 		return ret;
738*43a90889SApple OSS Distributions 	}
739*43a90889SApple OSS Distributions 	return TRUE;
740*43a90889SApple OSS Distributions }
741*43a90889SApple OSS Distributions 
742*43a90889SApple OSS Distributions test_t test[] = {
743*43a90889SApple OSS Distributions 	{ "allocate one page anywhere", test_allocate },
744*43a90889SApple OSS Distributions 	{ "deallocate a page", test_deallocate },
745*43a90889SApple OSS Distributions 	{ "allocate a SIZE_ANY page anywhere", test_allocate_size_any },
746*43a90889SApple OSS Distributions 	{ "allocate one page at a fixed address", test_allocatefixed },
747*43a90889SApple OSS Distributions 	{ "allocate one page at an unaligned fixed address", test_allocateunalignedfixed },
748*43a90889SApple OSS Distributions 	{ "deallocate sub-page", test_deallocatesubpage },
749*43a90889SApple OSS Distributions 	{ "allocate already allocated subpage", test_reallocate },
750*43a90889SApple OSS Distributions 	{ "wire a page", test_wire },
751*43a90889SApple OSS Distributions 	{ "unwire a page", test_unwire },
752*43a90889SApple OSS Distributions 	{ "make page readonly", test_readonly },
753*43a90889SApple OSS Distributions 	{ "make sub-page readonly", test_readonlysubpage },
754*43a90889SApple OSS Distributions 	{ "file I/O", test_fileio },
755*43a90889SApple OSS Distributions 	{ "mmap()", test_mmap },
756*43a90889SApple OSS Distributions 	{ "fork", test_fork },
757*43a90889SApple OSS Distributions };
758*43a90889SApple OSS Distributions #define TESTS ((int)(sizeof(test)/sizeof(*test)))
759*43a90889SApple OSS Distributions 
760*43a90889SApple OSS Distributions boolean_t
testit(int i)761*43a90889SApple OSS Distributions testit(int i)
762*43a90889SApple OSS Distributions {
763*43a90889SApple OSS Distributions 	boolean_t ret;
764*43a90889SApple OSS Distributions 
765*43a90889SApple OSS Distributions 	error[0] = 0;
766*43a90889SApple OSS Distributions 	printf("Test #%d \"%s\"...", i + 1, test[i].description);
767*43a90889SApple OSS Distributions 	ret = test[i].fn();
768*43a90889SApple OSS Distributions 	if (ret) {
769*43a90889SApple OSS Distributions 		printf("OK\n");
770*43a90889SApple OSS Distributions 	} else {
771*43a90889SApple OSS Distributions 		printf("FAILED!");
772*43a90889SApple OSS Distributions 		if (error[0]) {
773*43a90889SApple OSS Distributions 			printf(" (%s)\n", error);
774*43a90889SApple OSS Distributions 		} else {
775*43a90889SApple OSS Distributions 			printf("\n");
776*43a90889SApple OSS Distributions 		}
777*43a90889SApple OSS Distributions 	}
778*43a90889SApple OSS Distributions }
779*43a90889SApple OSS Distributions 
780*43a90889SApple OSS Distributions int
main(int argc,char ** argv)781*43a90889SApple OSS Distributions main(int argc, char **argv)
782*43a90889SApple OSS Distributions {
783*43a90889SApple OSS Distributions 	int i;
784*43a90889SApple OSS Distributions 	uint64_t time1, time2;
785*43a90889SApple OSS Distributions 
786*43a90889SApple OSS Distributions 	int mode = 0;
787*43a90889SApple OSS Distributions 	if (argc > 1) {
788*43a90889SApple OSS Distributions 		if (!strcmp(argv[1], "-h")) {
789*43a90889SApple OSS Distributions 			printf("Usage: %s <mode>\n", argv[0]);
790*43a90889SApple OSS Distributions 			printf("\tmode = 0:  test all cases\n");
791*43a90889SApple OSS Distributions 			printf("\tmode = -1: allocate/deallocate until failure\n");
792*43a90889SApple OSS Distributions 			printf("\tmode > 0:  run test <tmode>\n");
793*43a90889SApple OSS Distributions 			exit(0);
794*43a90889SApple OSS Distributions 		}
795*43a90889SApple OSS Distributions 		mode = atoi(argv[1]);
796*43a90889SApple OSS Distributions 	}
797*43a90889SApple OSS Distributions 
798*43a90889SApple OSS Distributions 	/* install SIGBUS handler */
799*43a90889SApple OSS Distributions 	struct sigaction my_sigaction;
800*43a90889SApple OSS Distributions 	my_sigaction.sa_handler = test_signal_handler;
801*43a90889SApple OSS Distributions 	my_sigaction.sa_flags = SA_RESTART;
802*43a90889SApple OSS Distributions 	my_sigaction.sa_mask = 0;
803*43a90889SApple OSS Distributions 	sigaction( SIGBUS, &my_sigaction, NULL );
804*43a90889SApple OSS Distributions 	sigaction( SIGSEGV, &my_sigaction, NULL );
805*43a90889SApple OSS Distributions 
806*43a90889SApple OSS Distributions 	if (mode > 0) {           /* one specific test */
807*43a90889SApple OSS Distributions 		testit(mode - 1);
808*43a90889SApple OSS Distributions 	}
809*43a90889SApple OSS Distributions 
810*43a90889SApple OSS Distributions 	if (mode == 0) {  /* test all cases */
811*43a90889SApple OSS Distributions 		printf("Running %d tests:\n", TESTS);
812*43a90889SApple OSS Distributions 		for (i = 0; i < TESTS; i++) {
813*43a90889SApple OSS Distributions 			testit(i);
814*43a90889SApple OSS Distributions 		}
815*43a90889SApple OSS Distributions 	}
816*43a90889SApple OSS Distributions 	if (mode == -1) { /* alloc/dealloc */
817*43a90889SApple OSS Distributions 		boolean_t ret;
818*43a90889SApple OSS Distributions 		do {
819*43a90889SApple OSS Distributions 			ret = test_alloc_dealloc(TRUE);
820*43a90889SApple OSS Distributions 			printf(".");
821*43a90889SApple OSS Distributions 			fflush(stdout);
822*43a90889SApple OSS Distributions 		} while (ret);
823*43a90889SApple OSS Distributions 		if (error[0]) {
824*43a90889SApple OSS Distributions 			printf(" (%s)\n", error);
825*43a90889SApple OSS Distributions 		}
826*43a90889SApple OSS Distributions 	}
827*43a90889SApple OSS Distributions 	return 0;
828*43a90889SApple OSS Distributions }
829