VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstPin.cpp

Last change on this file was 108662, checked in by vboxsync, 6 weeks ago

HostDrivers/Support/testcase/tstPin.cpp: Make it work for hosts where we don't know the host page size when building, bugref:10391 [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/* $Id: tstPin.cpp 108662 2025-03-20 15:49:49Z vboxsync $ */
2/** @file
3 * SUP Testcase - Memory locking interface (ring 3).
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <VBox/sup.h>
42#include <VBox/param.h>
43#include <iprt/errcore.h>
44#include <iprt/initterm.h>
45#include <iprt/string.h>
46#include <iprt/test.h>
47#include <iprt/thread.h>
48#include <iprt/system.h>
49
50
51#include "../SUPLibInternal.h"
52
53
54int main(int argc, char **argv)
55{
56 RTTEST hTest;
57
58 uint32_t fFlags = RTR3INIT_FLAGS_TRY_SUPLIB;
59#if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32) /* For M1 at least. */
60 fFlags |= SUPR3INIT_F_DRIVERLESS << RTR3INIT_FLAGS_SUPLIB_SHIFT;
61#endif
62 RTEXITCODE rcExit = RTTestInitExAndCreate(argc, &argv, fFlags, "tstPin", &hTest);
63 if (rcExit != RTEXITCODE_SUCCESS)
64 return rcExit;
65 RTTestBanner(hTest);
66
67 RTHCPHYS HCPhys;
68
69 /*
70 * Simple test.
71 */
72 RTTestISub("Simple");
73
74 void *pv;
75 int rc = SUPR3PageAlloc(1, 0, &pv);
76 RTTESTI_CHECK_RC_OK(rc);
77 RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc, pv=%p\n", rc, pv);
78 SUPPAGE aPages[1];
79 rc = supR3PageLock(pv, 1, &aPages[0]);
80 RTTESTI_CHECK_RC_OK(rc);
81 RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc pv=%p aPages[0]=%RHp\n", rc, pv, aPages[0]);
82 RTThreadSleep(1500);
83#if 0
84 RTTestIPrintf(RTTESTLVL_DEBUG, "Unlocking...\n");
85 RTThreadSleep(250);
86 rc = SUPPageUnlock(pv);
87 RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc\n", rc);
88 RTThreadSleep(1500);
89#endif
90
91 RTTestISubDone();
92
93 RTTestISub("Extensive");
94
95 /*
96 * More extensive.
97 */
98 static struct
99 {
100 void *pv;
101 void *pvAligned;
102 SUPPAGE aPages[16];
103 } aPinnings[500];
104
105 uint32_t const cPageShift = RTSystemGetPageShift();
106 uint32_t const cbPage = RTSystemGetPageSize();
107
108 for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i++)
109 {
110 aPinnings[i].pv = NULL;
111 SUPR3PageAlloc(0x10000 >> cPageShift, 0, &aPinnings[i].pv);
112 aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, cbPage);
113 rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> cPageShift, &aPinnings[i].aPages[0]);
114 if (RT_SUCCESS(rc))
115 {
116 RTTestIPrintf(RTTESTLVL_DEBUG, "i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
117 memset(aPinnings[i].pv, 0xfa, 0x10000);
118 unsigned c4GPluss = 0;
119 for (unsigned j = 0; j < (UINT32_C(0xf000) >> cPageShift); j++)
120 if (aPinnings[i].aPages[j].Phys >= _4G)
121 {
122 RTTestIPrintf(RTTESTLVL_DEBUG, "%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << cPageShift), aPinnings[i].aPages[j].Phys);
123 c4GPluss++;
124 }
125 RTTestIPrintf(RTTESTLVL_DEBUG, "i=%d: c4GPluss=%d\n", i, c4GPluss);
126 }
127 else
128 {
129 RTTestIFailed("SUPPageLock() failed with rc=%Rrc\n", rc);
130 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> cPageShift);
131 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
132 break;
133 }
134 }
135
136 for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i += 2)
137 {
138 if (aPinnings[i].pvAligned)
139 {
140 rc = supR3PageUnlock(aPinnings[i].pvAligned);
141 RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("SUPPageUnlock(%p) -> rc=%Rrc\n", aPinnings[i].pvAligned, rc));
142 memset(aPinnings[i].pv, 0xaf, 0x10000);
143 }
144 }
145
146 for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i += 2)
147 {
148 if (aPinnings[i].pv)
149 {
150 memset(aPinnings[i].pv, 0xcc, 0x10000);
151 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> cPageShift);
152 aPinnings[i].pv = NULL;
153 }
154 }
155
156 RTTestISubDone();
157
158
159/* Support for allocating Ring-0 executable memory with contiguous physical backing isn't implemented on Solaris. */
160#if !defined(RT_OS_SOLARIS)
161 /*
162 * Allocate a bit of contiguous memory.
163 */
164 RTTestISub("Contiguous memory");
165
166 size_t cPages = RT_ALIGN_Z(15003, cbPage) >> cPageShift;
167
168 pv = SUPR3ContAlloc(cPages, NULL, &HCPhys);
169 if (pv && HCPhys)
170 {
171 RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
172 void *pv0 = pv;
173 memset(pv0, 0xaf, 15003);
174 pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, cbPage) >> cPageShift, NULL, &HCPhys);
175 if (pv && HCPhys)
176 {
177 RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
178 memset(pv, 0xbf, 12999);
179 rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, cbPage) >> cPageShift);
180 if (RT_FAILURE(rc))
181 RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContFree failed! rc=%Rrc\n", rc);
182 }
183 else
184 RTTestIFailed("SUPR3ContAlloc (2nd) failed!\n");
185 memset(pv0, 0xaf, 15003);
186 /* pv0 is intentionally not freed! */
187 }
188 else
189 RTTestIFailed("SUPR3ContAlloc(%zu pages) failed!\n", cPages);
190
191 RTTestISubDone();
192#endif
193
194 /*
195 * Allocate a big chunk of virtual memory and then lock it.
196 */
197 RTTestISub("Big chunk");
198
199 #define BIG_SIZE 72*1024*1024
200 #define BIG_SIZEPP (BIG_SIZE + cbPage)
201#if defined(RT_OS_SOLARIS)
202 size_t cPages = RT_ALIGN_Z(15003, cbPage) >> cPageShift;
203#endif
204 pv = NULL;
205 cPages = BIG_SIZEPP >> cPageShift;
206 rc = SUPR3PageAlloc(cPages, 0, &pv);
207 if (RT_SUCCESS(rc))
208 {
209 AssertPtr(pv);
210
211 static SUPPAGE s_aPages[18432 + 1]; /* Smallest page size is _4K, so BIG_SIZE will require BIG_SIZE / _4K pages (+ 1 for BIG_SIZEPP). */
212 void *pvAligned = RT_ALIGN_P(pv, cbPage);
213 rc = supR3PageLock(pvAligned, BIG_SIZE >> cPageShift, &s_aPages[0]);
214 if (RT_SUCCESS(rc))
215 {
216 /* dump */
217 RTTestIPrintf(RTTESTLVL_DEBUG, "SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
218 memset(pv, 0x42, BIG_SIZEPP);
219 #if 0
220 for (unsigned j = 0; j < (BIG_SIZE >> cPageShift); j++)
221 RTTestIPrintf(RTTESTLVL_DEBUG, "%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << cPageShift), (uintptr_t)s_aPages[j].pvPhys);
222 #endif
223
224 /* unlock */
225 rc = supR3PageUnlock(pvAligned);
226 if (RT_FAILURE(rc))
227 RTTestIFailed("SUPPageUnlock(%p) failed with rc=%Rrc\n", pvAligned, rc);
228 memset(pv, 0xcc, BIG_SIZEPP);
229 }
230 else
231 RTTestIFailed("SUPPageLock(%p) failed with rc=%Rrc\n", pvAligned, rc);
232 SUPR3PageFree(pv, cPages);
233 }
234 else
235 RTTestIFailed("SUPPageAlloc(%zu pages) failed with rc=%Rrc\n", cPages, rc);
236
237 RTTestISubDone();
238
239 /*
240 * Summary.
241 */
242 return RTTestSummaryAndDestroy(hTest);
243}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette