VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: tstPin.cpp 98103 2023-01-17 14:15:46Z 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/stream.h>
46#include <iprt/thread.h>
47#include <iprt/string.h>
48
49#include "../SUPLibInternal.h"
50
51
52int main(int argc, char **argv)
53{
54 int rc;
55 int rcRet = 0;
56 RTHCPHYS HCPhys;
57
58 RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_TRY_SUPLIB);
59 rc = SUPR3Init(NULL);
60 RTPrintf("SUPR3Init -> rc=%d\n", rc);
61 rcRet += rc != 0;
62 if (!rc)
63 {
64 /*
65 * Simple test.
66 */
67 void *pv;
68 rc = SUPR3PageAlloc(1, 0, &pv);
69 AssertRC(rc);
70 RTPrintf("pv=%p\n", pv);
71 SUPPAGE aPages[1];
72 rc = supR3PageLock(pv, 1, &aPages[0]);
73 RTPrintf("rc=%d pv=%p aPages[0]=%RHp\n", rc, pv, aPages[0]);
74 RTThreadSleep(1500);
75#if 0
76 RTPrintf("Unlocking...\n");
77 RTThreadSleep(250);
78 rc = SUPPageUnlock(pv);
79 RTPrintf("rc=%d\n", rc);
80 RTThreadSleep(1500);
81#endif
82
83 /*
84 * More extensive.
85 */
86 static struct
87 {
88 void *pv;
89 void *pvAligned;
90 SUPPAGE aPages[16];
91 } aPinnings[500];
92 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
93 {
94 aPinnings[i].pv = NULL;
95 SUPR3PageAlloc(0x10000 >> PAGE_SHIFT, 0, &aPinnings[i].pv);
96 aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
97 rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
98 if (!rc)
99 {
100 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
101 memset(aPinnings[i].pv, 0xfa, 0x10000);
102 unsigned c4GPluss = 0;
103 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
104 if (aPinnings[i].aPages[j].Phys >= _4G)
105 {
106 RTPrintf("%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
107 c4GPluss++;
108 }
109 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
110 }
111 else
112 {
113 RTPrintf("SUPPageLock -> rc=%d\n", rc);
114 rcRet++;
115 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
116 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
117 break;
118 }
119 }
120
121 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
122 {
123 if (aPinnings[i].pvAligned)
124 {
125 rc = supR3PageUnlock(aPinnings[i].pvAligned);
126 if (rc)
127 {
128 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
129 rcRet++;
130 }
131 memset(aPinnings[i].pv, 0xaf, 0x10000);
132 }
133 }
134
135 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
136 {
137 if (aPinnings[i].pv)
138 {
139 memset(aPinnings[i].pv, 0xcc, 0x10000);
140 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
141 aPinnings[i].pv = NULL;
142 }
143 }
144
145
146 /*
147 * Allocate a bit of contiguous memory.
148 */
149 pv = SUPR3ContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
150 rcRet += pv == NULL || HCPhys == 0;
151 if (pv && HCPhys)
152 {
153 RTPrintf("SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
154 void *pv0 = pv;
155 memset(pv0, 0xaf, 15003);
156 pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
157 rcRet += pv == NULL || HCPhys == 0;
158 if (pv && HCPhys)
159 {
160 RTPrintf("SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
161 memset(pv, 0xbf, 12999);
162 rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
163 rcRet += rc != 0;
164 if (rc)
165 RTPrintf("SUPR3ContFree failed! rc=%d\n", rc);
166 }
167 else
168 RTPrintf("SUPR3ContAlloc (2nd) failed!\n");
169 memset(pv0, 0xaf, 15003);
170 /* pv0 is intentionally not freed! */
171 }
172 else
173 RTPrintf("SUPR3ContAlloc failed!\n");
174
175 /*
176 * Allocate a big chunk of virtual memory and then lock it.
177 */
178 #define BIG_SIZE 72*1024*1024
179 #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
180 pv = NULL;
181 SUPR3PageAlloc(BIG_SIZEPP >> PAGE_SHIFT, 0, &pv);
182 if (pv)
183 {
184 static SUPPAGE s_aPages[BIG_SIZE >> PAGE_SHIFT];
185 void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
186 rc = supR3PageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &s_aPages[0]);
187 if (!rc)
188 {
189 /* dump */
190 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
191 memset(pv, 0x42, BIG_SIZEPP);
192 #if 0
193 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
194 RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)s_aPages[j].pvPhys);
195 #endif
196
197 /* unlock */
198 rc = supR3PageUnlock(pvAligned);
199 if (rc)
200 {
201 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
202 rcRet++;
203 }
204 memset(pv, 0xcc, BIG_SIZEPP);
205 }
206 else
207 {
208 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
209 rcRet++;
210 }
211 SUPR3PageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
212 }
213
214 rc = SUPR3Term(false /*fForced*/);
215 RTPrintf("SUPR3Term -> rc=%d\n", rc);
216 rcRet += rc != 0;
217 }
218
219 return rcRet;
220}
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