xref: /xnu-11417.121.6/tests/vm/vm_direct_write_wired.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions /*
2*a1e26a70SApple OSS Distributions  * Copyright (c) 2024 Apple Inc. All rights reserved.
3*a1e26a70SApple OSS Distributions  *
4*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*a1e26a70SApple OSS Distributions  *
6*a1e26a70SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a1e26a70SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a1e26a70SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a1e26a70SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*a1e26a70SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*a1e26a70SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*a1e26a70SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*a1e26a70SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*a1e26a70SApple OSS Distributions  *
15*a1e26a70SApple OSS Distributions  * Please obtain a copy of the License at
16*a1e26a70SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*a1e26a70SApple OSS Distributions  *
18*a1e26a70SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*a1e26a70SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*a1e26a70SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*a1e26a70SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*a1e26a70SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*a1e26a70SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*a1e26a70SApple OSS Distributions  * limitations under the License.
25*a1e26a70SApple OSS Distributions  *
26*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*a1e26a70SApple OSS Distributions  */
28*a1e26a70SApple OSS Distributions 
29*a1e26a70SApple OSS Distributions #include <darwintest.h>
30*a1e26a70SApple OSS Distributions #include <darwintest_utils.h>
31*a1e26a70SApple OSS Distributions 
32*a1e26a70SApple OSS Distributions #include <errno.h>
33*a1e26a70SApple OSS Distributions #include <stdio.h>
34*a1e26a70SApple OSS Distributions #include <fcntl.h>
35*a1e26a70SApple OSS Distributions #include <stdlib.h>
36*a1e26a70SApple OSS Distributions #include <string.h>
37*a1e26a70SApple OSS Distributions #include <sys/mman.h>
38*a1e26a70SApple OSS Distributions #include <sys/uio.h>
39*a1e26a70SApple OSS Distributions 
40*a1e26a70SApple OSS Distributions T_GLOBAL_META(
41*a1e26a70SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
42*a1e26a70SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
43*a1e26a70SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
44*a1e26a70SApple OSS Distributions 	T_META_OWNER("francois"),
45*a1e26a70SApple OSS Distributions 	T_META_CHECK_LEAKS(true),
46*a1e26a70SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
47*a1e26a70SApple OSS Distributions 
48*a1e26a70SApple OSS Distributions T_DECL(direct_write_wired,
49*a1e26a70SApple OSS Distributions     "test direct write() to wired file", T_META_TAG_VM_PREFERRED)
50*a1e26a70SApple OSS Distributions {
51*a1e26a70SApple OSS Distributions 	int fd;
52*a1e26a70SApple OSS Distributions 	int ret;
53*a1e26a70SApple OSS Distributions 	char tmpf[PATH_MAX] = "";
54*a1e26a70SApple OSS Distributions 	char *map_addr;
55*a1e26a70SApple OSS Distributions 	size_t file_size;
56*a1e26a70SApple OSS Distributions 	ssize_t num_bytes;
57*a1e26a70SApple OSS Distributions 	char *buf;
58*a1e26a70SApple OSS Distributions 
59*a1e26a70SApple OSS Distributions 	T_SETUPBEGIN;
60*a1e26a70SApple OSS Distributions 
61*a1e26a70SApple OSS Distributions 	strlcpy(tmpf, dt_tmpdir(), PATH_MAX);
62*a1e26a70SApple OSS Distributions 	strlcat(tmpf, "/wire_direct_write.txt", PATH_MAX);
63*a1e26a70SApple OSS Distributions 	T_LOG("file name: <%s>\n", tmpf);
64*a1e26a70SApple OSS Distributions 	fd = open(tmpf, O_RDWR | O_CREAT | O_TRUNC, 0644);
65*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open()");
66*a1e26a70SApple OSS Distributions 
67*a1e26a70SApple OSS Distributions 	file_size = PAGE_SIZE;
68*a1e26a70SApple OSS Distributions 	ret = ftruncate(fd, (off_t) file_size);
69*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "ftruncate()");
70*a1e26a70SApple OSS Distributions 
71*a1e26a70SApple OSS Distributions 	map_addr = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
72*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(map_addr, "mmap()");
73*a1e26a70SApple OSS Distributions 	memset(map_addr, 'a', file_size);
74*a1e26a70SApple OSS Distributions 
75*a1e26a70SApple OSS Distributions 	ret = mlock(map_addr, file_size);
76*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mlock()");
77*a1e26a70SApple OSS Distributions 
78*a1e26a70SApple OSS Distributions 	ret = fcntl(fd, F_NOCACHE, true);
79*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "fcntl(F_NOCACHE)");
80*a1e26a70SApple OSS Distributions 
81*a1e26a70SApple OSS Distributions 	buf = valloc(file_size);
82*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(buf, "valloc()");
83*a1e26a70SApple OSS Distributions 	memset(buf, 'b', file_size);
84*a1e26a70SApple OSS Distributions 
85*a1e26a70SApple OSS Distributions 	T_SETUPEND;
86*a1e26a70SApple OSS Distributions 
87*a1e26a70SApple OSS Distributions 	num_bytes = pwrite(fd, buf, file_size, 0);
88*a1e26a70SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(num_bytes, "write()");
89*a1e26a70SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(num_bytes, (ssize_t) file_size, "wrote <file_size> bytes");
90*a1e26a70SApple OSS Distributions 
91*a1e26a70SApple OSS Distributions 	ret = munlock(map_addr, file_size);
92*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "munlock()");
93*a1e26a70SApple OSS Distributions 
94*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(map_addr[0], 'b', "write() worked");
95*a1e26a70SApple OSS Distributions 
96*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd), "close()");
97*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(unlink(tmpf), "unlink(%s)", tmpf);
98*a1e26a70SApple OSS Distributions }
99*a1e26a70SApple OSS Distributions 
100*a1e26a70SApple OSS Distributions 
101*a1e26a70SApple OSS Distributions #define NUM_IOVS 7
102*a1e26a70SApple OSS Distributions 
103*a1e26a70SApple OSS Distributions T_DECL(direct_write_wired_vector_upl,
104*a1e26a70SApple OSS Distributions     "test direct write() to wired file with vector UIO",
105*a1e26a70SApple OSS Distributions     T_META_TAG_VM_PREFERRED)
106*a1e26a70SApple OSS Distributions {
107*a1e26a70SApple OSS Distributions 	char tmpf[PATH_MAX] = "";
108*a1e26a70SApple OSS Distributions 	struct iovec w_iovs[NUM_IOVS];
109*a1e26a70SApple OSS Distributions 	struct iovec r_iovs[NUM_IOVS];
110*a1e26a70SApple OSS Distributions 	int fd;
111*a1e26a70SApple OSS Distributions 	ssize_t bytes, expected_bytes;
112*a1e26a70SApple OSS Distributions 	int w, r;
113*a1e26a70SApple OSS Distributions 	char *w_base, *r_base;
114*a1e26a70SApple OSS Distributions 	int w_cnt, r_cnt;
115*a1e26a70SApple OSS Distributions 	int w_idx, r_idx;
116*a1e26a70SApple OSS Distributions 	char *map_addr;
117*a1e26a70SApple OSS Distributions 	int ret;
118*a1e26a70SApple OSS Distributions 	char *buf;
119*a1e26a70SApple OSS Distributions 
120*a1e26a70SApple OSS Distributions 	T_SETUPBEGIN;
121*a1e26a70SApple OSS Distributions 
122*a1e26a70SApple OSS Distributions 	expected_bytes = 0;
123*a1e26a70SApple OSS Distributions 	for (w = 0, r = NUM_IOVS - 1; w < NUM_IOVS; w++, r--) {
124*a1e26a70SApple OSS Distributions 		w_iovs[w].iov_len = (size_t) ((w + 1) * (int)PAGE_SIZE);
125*a1e26a70SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_memalign(&w_iovs[w].iov_base, PAGE_SIZE, w_iovs[w].iov_len), "alloc(w_iov_base[%d])", w);
126*a1e26a70SApple OSS Distributions 		memset(w_iovs[w].iov_base, 'a' + w, w_iovs[w].iov_len);
127*a1e26a70SApple OSS Distributions 		expected_bytes += w_iovs[w].iov_len;
128*a1e26a70SApple OSS Distributions 
129*a1e26a70SApple OSS Distributions 		r_iovs[r].iov_len = w_iovs[w].iov_len;
130*a1e26a70SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_memalign(&r_iovs[r].iov_base, PAGE_SIZE, r_iovs[r].iov_len), "alloc(r_iov_base[%d])", r);
131*a1e26a70SApple OSS Distributions 	}
132*a1e26a70SApple OSS Distributions 	strlcpy(tmpf, dt_tmpdir(), PATH_MAX);
133*a1e26a70SApple OSS Distributions 	strlcat(tmpf, "/wire_direct_write.txt", PATH_MAX);
134*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_LT(strlen(tmpf), (unsigned long)PATH_MAX,
135*a1e26a70SApple OSS Distributions 	    "path exceeds PATH_MAX");
136*a1e26a70SApple OSS Distributions 	fd = open(tmpf, O_RDWR | O_CREAT | O_TRUNC);
137*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open()");
138*a1e26a70SApple OSS Distributions 	/* use F_NOCACHE to get direct I/O */
139*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_NOCACHE, 1), "fcntl(F_NOCACHE)");
140*a1e26a70SApple OSS Distributions 
141*a1e26a70SApple OSS Distributions 	buf = valloc(expected_bytes);
142*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(buf, "valloc()");
143*a1e26a70SApple OSS Distributions 	memset(buf, '?', expected_bytes);
144*a1e26a70SApple OSS Distributions 	bytes = pwrite(fd, buf, expected_bytes, 0);
145*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(bytes, "pwrite()");
146*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(bytes, expected_bytes,
147*a1e26a70SApple OSS Distributions 	    "pwrite() wrote %ld bytes", bytes);
148*a1e26a70SApple OSS Distributions 
149*a1e26a70SApple OSS Distributions 	T_SETUPEND;
150*a1e26a70SApple OSS Distributions 
151*a1e26a70SApple OSS Distributions 	map_addr = mmap(NULL, expected_bytes, PROT_READ | PROT_WRITE,
152*a1e26a70SApple OSS Distributions 	    MAP_FILE | MAP_SHARED, fd, 0);
153*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(map_addr, "mmap()");
154*a1e26a70SApple OSS Distributions 	ret = mlock(&map_addr[(NUM_IOVS / 2) * PAGE_SIZE], PAGE_SIZE);
155*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "mlock(0x%llx)", (uint64_t)((NUM_IOVS / 2) * PAGE_SIZE));
156*a1e26a70SApple OSS Distributions 
157*a1e26a70SApple OSS Distributions 	bytes = pwritev(fd, w_iovs, NUM_IOVS, 0);
158*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(bytes, expected_bytes,
159*a1e26a70SApple OSS Distributions 	    "pwritev() wrote %ld bytes", bytes);
160*a1e26a70SApple OSS Distributions 
161*a1e26a70SApple OSS Distributions 	bytes = preadv(fd, r_iovs, NUM_IOVS, 0);
162*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(bytes, expected_bytes,
163*a1e26a70SApple OSS Distributions 	    "preadv() read %ld bytes", bytes);
164*a1e26a70SApple OSS Distributions 
165*a1e26a70SApple OSS Distributions 	w = 0;
166*a1e26a70SApple OSS Distributions 	w_base = w_iovs[w].iov_base;
167*a1e26a70SApple OSS Distributions 	w_cnt = (int) w_iovs[w].iov_len;
168*a1e26a70SApple OSS Distributions 	w_idx = 0;
169*a1e26a70SApple OSS Distributions 	for (r = 0; r < NUM_IOVS; r++) {
170*a1e26a70SApple OSS Distributions 		r_base = r_iovs[r].iov_base;
171*a1e26a70SApple OSS Distributions 		r_cnt = (int) r_iovs[r].iov_len;
172*a1e26a70SApple OSS Distributions 		for (r_idx = 0; r_idx < r_cnt; r_idx++) {
173*a1e26a70SApple OSS Distributions 			T_QUIET; T_ASSERT_EQ(r_base[r_idx], w_base[w_idx],
174*a1e26a70SApple OSS Distributions 			    "r_iovs[%d].iov_base[%d]='%c' == w_iovs[%d].iov_base[%d]='%c'",
175*a1e26a70SApple OSS Distributions 			    r, r_idx, (unsigned char)r_base[r_idx],
176*a1e26a70SApple OSS Distributions 			    w, w_idx, (unsigned char) w_base[w_idx]);
177*a1e26a70SApple OSS Distributions 			if (++w_idx == w_cnt) {
178*a1e26a70SApple OSS Distributions 				w++;
179*a1e26a70SApple OSS Distributions 				w_base = w_iovs[w].iov_base;
180*a1e26a70SApple OSS Distributions 				w_cnt = (int) w_iovs[w].iov_len;
181*a1e26a70SApple OSS Distributions 				w_idx = 0;
182*a1e26a70SApple OSS Distributions 			}
183*a1e26a70SApple OSS Distributions 		}
184*a1e26a70SApple OSS Distributions 	}
185*a1e26a70SApple OSS Distributions 
186*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd), "close()");
187*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(unlink(tmpf), "unlink(%s)", tmpf);
188*a1e26a70SApple OSS Distributions 
189*a1e26a70SApple OSS Distributions 	T_PASS("%s", __FUNCTION__);
190*a1e26a70SApple OSS Distributions }
191