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