1 | /* $Id: tstPin.cpp 99818 2023-05-16 21:11:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Memory locking interface (ring 3).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 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 |
|
---|
49 |
|
---|
50 | #include "../SUPLibInternal.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | int main(int argc, char **argv)
|
---|
54 | {
|
---|
55 | RTTEST hTest;
|
---|
56 |
|
---|
57 | uint32_t fFlags = RTR3INIT_FLAGS_TRY_SUPLIB;
|
---|
58 | #if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32) /* For M1 at least. */
|
---|
59 | fFlags |= SUPR3INIT_F_DRIVERLESS << RTR3INIT_FLAGS_SUPLIB_SHIFT;
|
---|
60 | #endif
|
---|
61 | RTEXITCODE rcExit = RTTestInitExAndCreate(argc, &argv, fFlags, "tstPin", &hTest);
|
---|
62 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
63 | return rcExit;
|
---|
64 | RTTestBanner(hTest);
|
---|
65 |
|
---|
66 | RTHCPHYS HCPhys;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Simple test.
|
---|
70 | */
|
---|
71 | RTTestISub("Simple");
|
---|
72 |
|
---|
73 | void *pv;
|
---|
74 | int rc = SUPR3PageAlloc(1, 0, &pv);
|
---|
75 | RTTESTI_CHECK_RC_OK(rc);
|
---|
76 | RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc, pv=%p\n", rc, pv);
|
---|
77 | SUPPAGE aPages[1];
|
---|
78 | rc = supR3PageLock(pv, 1, &aPages[0]);
|
---|
79 | RTTESTI_CHECK_RC_OK(rc);
|
---|
80 | RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc pv=%p aPages[0]=%RHp\n", rc, pv, aPages[0]);
|
---|
81 | RTThreadSleep(1500);
|
---|
82 | #if 0
|
---|
83 | RTTestIPrintf(RTTESTLVL_DEBUG, "Unlocking...\n");
|
---|
84 | RTThreadSleep(250);
|
---|
85 | rc = SUPPageUnlock(pv);
|
---|
86 | RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc\n", rc);
|
---|
87 | RTThreadSleep(1500);
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | RTTestISubDone();
|
---|
91 |
|
---|
92 | RTTestISub("Extensive");
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * More extensive.
|
---|
96 | */
|
---|
97 | static struct
|
---|
98 | {
|
---|
99 | void *pv;
|
---|
100 | void *pvAligned;
|
---|
101 | SUPPAGE aPages[16];
|
---|
102 | } aPinnings[500];
|
---|
103 |
|
---|
104 | for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i++)
|
---|
105 | {
|
---|
106 | aPinnings[i].pv = NULL;
|
---|
107 | SUPR3PageAlloc(0x10000 >> PAGE_SHIFT, 0, &aPinnings[i].pv);
|
---|
108 | aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
|
---|
109 | rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | {
|
---|
112 | RTTestIPrintf(RTTESTLVL_DEBUG, "i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
|
---|
113 | memset(aPinnings[i].pv, 0xfa, 0x10000);
|
---|
114 | unsigned c4GPluss = 0;
|
---|
115 | for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
|
---|
116 | if (aPinnings[i].aPages[j].Phys >= _4G)
|
---|
117 | {
|
---|
118 | RTTestIPrintf(RTTESTLVL_DEBUG, "%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
|
---|
119 | c4GPluss++;
|
---|
120 | }
|
---|
121 | RTTestIPrintf(RTTESTLVL_DEBUG, "i=%d: c4GPluss=%d\n", i, c4GPluss);
|
---|
122 | }
|
---|
123 | else
|
---|
124 | {
|
---|
125 | RTTestIFailed("SUPPageLock() failed with rc=%Rrc\n", rc);
|
---|
126 | SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
|
---|
127 | aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
|
---|
128 | break;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i += 2)
|
---|
133 | {
|
---|
134 | if (aPinnings[i].pvAligned)
|
---|
135 | {
|
---|
136 | rc = supR3PageUnlock(aPinnings[i].pvAligned);
|
---|
137 | RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("SUPPageUnlock(%p) -> rc=%Rrc\n", aPinnings[i].pvAligned, rc));
|
---|
138 | memset(aPinnings[i].pv, 0xaf, 0x10000);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i += 2)
|
---|
143 | {
|
---|
144 | if (aPinnings[i].pv)
|
---|
145 | {
|
---|
146 | memset(aPinnings[i].pv, 0xcc, 0x10000);
|
---|
147 | SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
|
---|
148 | aPinnings[i].pv = NULL;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | RTTestISubDone();
|
---|
153 |
|
---|
154 |
|
---|
155 | /* Support for allocating Ring-0 executable memory with contiguous physical backing isn't implemented on Solaris. */
|
---|
156 | #if !defined(RT_OS_SOLARIS)
|
---|
157 | /*
|
---|
158 | * Allocate a bit of contiguous memory.
|
---|
159 | */
|
---|
160 | RTTestISub("Contiguous memory");
|
---|
161 |
|
---|
162 | size_t cPages = RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT;
|
---|
163 |
|
---|
164 | pv = SUPR3ContAlloc(cPages, NULL, &HCPhys);
|
---|
165 | if (pv && HCPhys)
|
---|
166 | {
|
---|
167 | RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
|
---|
168 | void *pv0 = pv;
|
---|
169 | memset(pv0, 0xaf, 15003);
|
---|
170 | pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
|
---|
171 | if (pv && HCPhys)
|
---|
172 | {
|
---|
173 | RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
|
---|
174 | memset(pv, 0xbf, 12999);
|
---|
175 | rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
|
---|
176 | if (RT_FAILURE(rc))
|
---|
177 | RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContFree failed! rc=%Rrc\n", rc);
|
---|
178 | }
|
---|
179 | else
|
---|
180 | RTTestIFailed("SUPR3ContAlloc (2nd) failed!\n");
|
---|
181 | memset(pv0, 0xaf, 15003);
|
---|
182 | /* pv0 is intentionally not freed! */
|
---|
183 | }
|
---|
184 | else
|
---|
185 | RTTestIFailed("SUPR3ContAlloc(%zu pages) failed!\n", cPages);
|
---|
186 |
|
---|
187 | RTTestISubDone();
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Allocate a big chunk of virtual memory and then lock it.
|
---|
192 | */
|
---|
193 | RTTestISub("Big chunk");
|
---|
194 |
|
---|
195 | #define BIG_SIZE 72*1024*1024
|
---|
196 | #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
|
---|
197 | #if defined(RT_OS_SOLARIS)
|
---|
198 | size_t cPages = RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT;
|
---|
199 | #endif
|
---|
200 | pv = NULL;
|
---|
201 | cPages = BIG_SIZEPP >> PAGE_SHIFT;
|
---|
202 | rc = SUPR3PageAlloc(cPages, 0, &pv);
|
---|
203 | if (RT_SUCCESS(rc))
|
---|
204 | {
|
---|
205 | AssertPtr(pv);
|
---|
206 |
|
---|
207 | static SUPPAGE s_aPages[BIG_SIZE >> PAGE_SHIFT];
|
---|
208 | void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
|
---|
209 | rc = supR3PageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &s_aPages[0]);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | /* dump */
|
---|
213 | RTTestIPrintf(RTTESTLVL_DEBUG, "SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
|
---|
214 | memset(pv, 0x42, BIG_SIZEPP);
|
---|
215 | #if 0
|
---|
216 | for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
|
---|
217 | RTTestIPrintf(RTTESTLVL_DEBUG, "%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)s_aPages[j].pvPhys);
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | /* unlock */
|
---|
221 | rc = supR3PageUnlock(pvAligned);
|
---|
222 | if (RT_FAILURE(rc))
|
---|
223 | RTTestIFailed("SUPPageUnlock(%p) failed with rc=%Rrc\n", pvAligned, rc);
|
---|
224 | memset(pv, 0xcc, BIG_SIZEPP);
|
---|
225 | }
|
---|
226 | else
|
---|
227 | RTTestIFailed("SUPPageLock(%p) failed with rc=%Rrc\n", pvAligned, rc);
|
---|
228 | SUPR3PageFree(pv, cPages);
|
---|
229 | }
|
---|
230 | else
|
---|
231 | RTTestIFailed("SUPPageAlloc(%zu pages) failed with rc=%Rrc\n", cPages, rc);
|
---|
232 |
|
---|
233 | RTTestISubDone();
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Summary.
|
---|
237 | */
|
---|
238 | return RTTestSummaryAndDestroy(hTest);
|
---|
239 | }
|
---|