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