1 | /* $Id: tstPin.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Memory locking interface (ring 3).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <VBox/sup.h>
|
---|
32 | #include <VBox/param.h>
|
---|
33 | #include <iprt/errcore.h>
|
---|
34 | #include <iprt/initterm.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 | #include "../SUPLibInternal.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | int main(int argc, char **argv)
|
---|
43 | {
|
---|
44 | int rc;
|
---|
45 | int rcRet = 0;
|
---|
46 | RTHCPHYS HCPhys;
|
---|
47 |
|
---|
48 | RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_TRY_SUPLIB);
|
---|
49 | rc = SUPR3Init(NULL);
|
---|
50 | RTPrintf("SUPR3Init -> rc=%d\n", rc);
|
---|
51 | rcRet += rc != 0;
|
---|
52 | if (!rc)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Simple test.
|
---|
56 | */
|
---|
57 | void *pv;
|
---|
58 | rc = SUPR3PageAlloc(1, 0, &pv);
|
---|
59 | AssertRC(rc);
|
---|
60 | RTPrintf("pv=%p\n", pv);
|
---|
61 | SUPPAGE aPages[1];
|
---|
62 | rc = supR3PageLock(pv, 1, &aPages[0]);
|
---|
63 | RTPrintf("rc=%d pv=%p aPages[0]=%RHp\n", rc, pv, aPages[0]);
|
---|
64 | RTThreadSleep(1500);
|
---|
65 | #if 0
|
---|
66 | RTPrintf("Unlocking...\n");
|
---|
67 | RTThreadSleep(250);
|
---|
68 | rc = SUPPageUnlock(pv);
|
---|
69 | RTPrintf("rc=%d\n", rc);
|
---|
70 | RTThreadSleep(1500);
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * More extensive.
|
---|
75 | */
|
---|
76 | static struct
|
---|
77 | {
|
---|
78 | void *pv;
|
---|
79 | void *pvAligned;
|
---|
80 | SUPPAGE aPages[16];
|
---|
81 | } aPinnings[500];
|
---|
82 | for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
|
---|
83 | {
|
---|
84 | aPinnings[i].pv = NULL;
|
---|
85 | SUPR3PageAlloc(0x10000 >> PAGE_SHIFT, 0, &aPinnings[i].pv);
|
---|
86 | aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
|
---|
87 | rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
|
---|
88 | if (!rc)
|
---|
89 | {
|
---|
90 | RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
|
---|
91 | memset(aPinnings[i].pv, 0xfa, 0x10000);
|
---|
92 | unsigned c4GPluss = 0;
|
---|
93 | for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
|
---|
94 | if (aPinnings[i].aPages[j].Phys >= _4G)
|
---|
95 | {
|
---|
96 | RTPrintf("%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
|
---|
97 | c4GPluss++;
|
---|
98 | }
|
---|
99 | RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | RTPrintf("SUPPageLock -> rc=%d\n", rc);
|
---|
104 | rcRet++;
|
---|
105 | SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
|
---|
106 | aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
|
---|
107 | break;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
|
---|
112 | {
|
---|
113 | if (aPinnings[i].pvAligned)
|
---|
114 | {
|
---|
115 | rc = supR3PageUnlock(aPinnings[i].pvAligned);
|
---|
116 | if (rc)
|
---|
117 | {
|
---|
118 | RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
|
---|
119 | rcRet++;
|
---|
120 | }
|
---|
121 | memset(aPinnings[i].pv, 0xaf, 0x10000);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
|
---|
126 | {
|
---|
127 | if (aPinnings[i].pv)
|
---|
128 | {
|
---|
129 | memset(aPinnings[i].pv, 0xcc, 0x10000);
|
---|
130 | SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
|
---|
131 | aPinnings[i].pv = NULL;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Allocate a bit of contiguous memory.
|
---|
138 | */
|
---|
139 | pv = SUPR3ContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
|
---|
140 | rcRet += pv == NULL || HCPhys == 0;
|
---|
141 | if (pv && HCPhys)
|
---|
142 | {
|
---|
143 | RTPrintf("SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
|
---|
144 | void *pv0 = pv;
|
---|
145 | memset(pv0, 0xaf, 15003);
|
---|
146 | pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
|
---|
147 | rcRet += pv == NULL || HCPhys == 0;
|
---|
148 | if (pv && HCPhys)
|
---|
149 | {
|
---|
150 | RTPrintf("SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
|
---|
151 | memset(pv, 0xbf, 12999);
|
---|
152 | rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
|
---|
153 | rcRet += rc != 0;
|
---|
154 | if (rc)
|
---|
155 | RTPrintf("SUPR3ContFree failed! rc=%d\n", rc);
|
---|
156 | }
|
---|
157 | else
|
---|
158 | RTPrintf("SUPR3ContAlloc (2nd) failed!\n");
|
---|
159 | memset(pv0, 0xaf, 15003);
|
---|
160 | /* pv0 is intentionally not freed! */
|
---|
161 | }
|
---|
162 | else
|
---|
163 | RTPrintf("SUPR3ContAlloc failed!\n");
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Allocate a big chunk of virtual memory and then lock it.
|
---|
167 | */
|
---|
168 | #define BIG_SIZE 72*1024*1024
|
---|
169 | #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
|
---|
170 | pv = NULL;
|
---|
171 | SUPR3PageAlloc(BIG_SIZEPP >> PAGE_SHIFT, 0, &pv);
|
---|
172 | if (pv)
|
---|
173 | {
|
---|
174 | static SUPPAGE s_aPages[BIG_SIZE >> PAGE_SHIFT];
|
---|
175 | void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
|
---|
176 | rc = supR3PageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &s_aPages[0]);
|
---|
177 | if (!rc)
|
---|
178 | {
|
---|
179 | /* dump */
|
---|
180 | RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
|
---|
181 | memset(pv, 0x42, BIG_SIZEPP);
|
---|
182 | #if 0
|
---|
183 | for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
|
---|
184 | RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)s_aPages[j].pvPhys);
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | /* unlock */
|
---|
188 | rc = supR3PageUnlock(pvAligned);
|
---|
189 | if (rc)
|
---|
190 | {
|
---|
191 | RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
|
---|
192 | rcRet++;
|
---|
193 | }
|
---|
194 | memset(pv, 0xcc, BIG_SIZEPP);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
|
---|
199 | rcRet++;
|
---|
200 | }
|
---|
201 | SUPR3PageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
|
---|
202 | }
|
---|
203 |
|
---|
204 | rc = SUPR3Term(false /*fForced*/);
|
---|
205 | RTPrintf("SUPR3Term -> rc=%d\n", rc);
|
---|
206 | rcRet += rc != 0;
|
---|
207 | }
|
---|
208 |
|
---|
209 | return rcRet;
|
---|
210 | }
|
---|