VirtualBox

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

Last change on this file since 20864 was 20864, checked in by vboxsync, 16 years ago

SUP,*: API cleanup.

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

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