1*043036a2SApple OSS Distributions /* 2*043036a2SApple OSS Distributions * Copyright (c) 2024 Apple Inc. All rights reserved. 3*043036a2SApple OSS Distributions * 4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*043036a2SApple OSS Distributions * 6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*043036a2SApple OSS Distributions * 15*043036a2SApple OSS Distributions * Please obtain a copy of the License at 16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*043036a2SApple OSS Distributions * 18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are 19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and 24*043036a2SApple OSS Distributions * limitations under the License. 25*043036a2SApple OSS Distributions * 26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*043036a2SApple OSS Distributions * 28*043036a2SApple OSS Distributions */ 29*043036a2SApple OSS Distributions 30*043036a2SApple OSS Distributions #include <darwintest.h> 31*043036a2SApple OSS Distributions #include <darwintest_utils.h> 32*043036a2SApple OSS Distributions 33*043036a2SApple OSS Distributions #include <stdlib.h> 34*043036a2SApple OSS Distributions 35*043036a2SApple OSS Distributions T_GLOBAL_META( 36*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.vm"), 37*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 38*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM"), 39*043036a2SApple OSS Distributions T_META_OWNER("jharmening"), 40*043036a2SApple OSS Distributions T_META_CHECK_LEAKS(false), 41*043036a2SApple OSS Distributions T_META_RUN_CONCURRENTLY(true)); 42*043036a2SApple OSS Distributions 43*043036a2SApple OSS Distributions #define NUM_IOVS 7 44*043036a2SApple OSS Distributions 45*043036a2SApple OSS Distributions T_DECL(vm_vector_upl, 46*043036a2SApple OSS Distributions "Test for vector UPLs", 47*043036a2SApple OSS Distributions T_META_TAG_VM_PREFERRED) 48*043036a2SApple OSS Distributions { 49*043036a2SApple OSS Distributions struct { 50*043036a2SApple OSS Distributions uint64_t base; 51*043036a2SApple OSS Distributions uint32_t len; 52*043036a2SApple OSS Distributions } w_iovs[NUM_IOVS]; 53*043036a2SApple OSS Distributions int64_t expected_bytes; 54*043036a2SApple OSS Distributions int w, w_idx; 55*043036a2SApple OSS Distributions 56*043036a2SApple OSS Distributions T_SETUPBEGIN; 57*043036a2SApple OSS Distributions expected_bytes = 0; 58*043036a2SApple OSS Distributions for (w = 0; w < NUM_IOVS; w++) { 59*043036a2SApple OSS Distributions w_iovs[w].len = (uint32_t) ((w + 1) * (int)PAGE_SIZE); 60*043036a2SApple OSS Distributions void *iov_base; 61*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_memalign(&iov_base, PAGE_SIZE, w_iovs[w].len), "alloc(w_iov_base[%d])", w); 62*043036a2SApple OSS Distributions memset(iov_base, 'a' + w, w_iovs[w].len); 63*043036a2SApple OSS Distributions w_iovs[w].base = (uint64_t)iov_base; 64*043036a2SApple OSS Distributions expected_bytes += w_iovs[w].len; 65*043036a2SApple OSS Distributions } 66*043036a2SApple OSS Distributions T_SETUPEND; 67*043036a2SApple OSS Distributions 68*043036a2SApple OSS Distributions struct { 69*043036a2SApple OSS Distributions uint64_t iov; 70*043036a2SApple OSS Distributions uint16_t iovcnt; 71*043036a2SApple OSS Distributions } arg; 72*043036a2SApple OSS Distributions 73*043036a2SApple OSS Distributions arg.iov = (uint64_t) &w_iovs[0]; 74*043036a2SApple OSS Distributions arg.iovcnt = NUM_IOVS; 75*043036a2SApple OSS Distributions 76*043036a2SApple OSS Distributions int64_t addr = (int64_t)&arg; 77*043036a2SApple OSS Distributions int64_t result = 0; 78*043036a2SApple OSS Distributions size_t s = sizeof(result); 79*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.test.vm_vector_upl", &result, &s, &addr, sizeof(addr)), 80*043036a2SApple OSS Distributions "sysctlbyname(debug.test.vm_vector_upl)"); 81*043036a2SApple OSS Distributions 82*043036a2SApple OSS Distributions T_EXPECT_EQ_LLONG(result, expected_bytes, "sysctl output"); 83*043036a2SApple OSS Distributions 84*043036a2SApple OSS Distributions w = 0; 85*043036a2SApple OSS Distributions w_idx = 0; 86*043036a2SApple OSS Distributions 87*043036a2SApple OSS Distributions /* Validate that the kernel sysctl handler mapped and mutated the page contents as expected. */ 88*043036a2SApple OSS Distributions for (w = 0; w < NUM_IOVS; w++) { 89*043036a2SApple OSS Distributions char *iov_base = (char*)w_iovs[w].base; 90*043036a2SApple OSS Distributions for (w_idx = 0; w_idx < w_iovs[w].len; w_idx++) { 91*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_EQ(iov_base[w_idx], 'a' + w + 1, 92*043036a2SApple OSS Distributions "w_iovs[%d].iov_base[%d]='%c' == '%c'", 93*043036a2SApple OSS Distributions w, w_idx, (unsigned char)iov_base[w_idx], (unsigned char)('a' + w + 1)); 94*043036a2SApple OSS Distributions } 95*043036a2SApple OSS Distributions } 96*043036a2SApple OSS Distributions 97*043036a2SApple OSS Distributions T_PASS("%s", __FUNCTION__); 98*043036a2SApple OSS Distributions } 99