VirtualBox

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

Last change on this file since 25336 was 25003, checked in by vboxsync, 15 years ago

tstInt,tstPin: -Wshadow warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: tstPin.cpp 25003 2009-11-26 14:27:19Z vboxsync $ */
2/** @file
3 * SUP Testcase - Memory locking interface (ring 3).
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <VBox/sup.h>
36#include <VBox/param.h>
37#include <VBox/err.h>
38#include <iprt/initterm.h>
39#include <iprt/stream.h>
40#include <iprt/thread.h>
41#include <iprt/string.h>
42
43#include "../SUPLibInternal.h"
44
45
46int main(int argc, char **argv)
47{
48 int rc;
49 int rcRet = 0;
50 RTHCPHYS HCPhys;
51
52 RTR3InitAndSUPLib();
53 rc = SUPR3Init(NULL);
54 RTPrintf("SUPR3Init -> rc=%d\n", rc);
55 rcRet += rc != 0;
56 if (!rc)
57 {
58 /*
59 * Simple test.
60 */
61 void *pv;
62 rc = SUPR3PageAlloc(1, &pv);
63 AssertRC(rc);
64 RTPrintf("pv=%p\n", pv);
65 SUPPAGE aPages[1];
66 rc = supR3PageLock(pv, 1, &aPages[0]);
67 RTPrintf("rc=%d aPages[0]=%RHp\n", rc, pv, aPages[0]);
68 RTThreadSleep(1500);
69#if 0
70 RTPrintf("Unlocking...\n");
71 RTThreadSleep(250);
72 rc = SUPPageUnlock(pv);
73 RTPrintf("rc=%d\n", rc);
74 RTThreadSleep(1500);
75#endif
76
77 /*
78 * More extensive.
79 */
80 static struct
81 {
82 void *pv;
83 void *pvAligned;
84 SUPPAGE aPages[16];
85 } aPinnings[500];
86 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
87 {
88 aPinnings[i].pv = NULL;
89 SUPR3PageAlloc(0x10000 >> PAGE_SHIFT, &aPinnings[i].pv);
90 aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
91 rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
92 if (!rc)
93 {
94 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
95 memset(aPinnings[i].pv, 0xfa, 0x10000);
96 unsigned c4GPluss = 0;
97 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
98 if (aPinnings[i].aPages[j].Phys >= _4G)
99 {
100 RTPrintf("%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
101 c4GPluss++;
102 }
103 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
104 }
105 else
106 {
107 RTPrintf("SUPPageLock -> rc=%d\n", rc);
108 rcRet++;
109 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
110 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
111 break;
112 }
113 }
114
115 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
116 {
117 if (aPinnings[i].pvAligned)
118 {
119 rc = supR3PageUnlock(aPinnings[i].pvAligned);
120 if (rc)
121 {
122 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
123 rcRet++;
124 }
125 memset(aPinnings[i].pv, 0xaf, 0x10000);
126 }
127 }
128
129 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
130 {
131 if (aPinnings[i].pv)
132 {
133 memset(aPinnings[i].pv, 0xcc, 0x10000);
134 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
135 aPinnings[i].pv = NULL;
136 }
137 }
138
139
140 /*
141 * Allocate a bit of contiguous memory.
142 */
143 pv = SUPR3ContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, NIL_RTR0PTR, &HCPhys);
144 rcRet += pv == NULL || HCPhys == 0;
145 if (pv && HCPhys)
146 {
147 RTPrintf("SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
148 void *pv0 = pv;
149 memset(pv0, 0xaf, 15003);
150 pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, NIL_RTR0PTR, &HCPhys);
151 rcRet += pv == NULL || HCPhys == 0;
152 if (pv && HCPhys)
153 {
154 RTPrintf("SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
155 memset(pv, 0xbf, 12999);
156 rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
157 rcRet += rc != 0;
158 if (rc)
159 RTPrintf("SUPR3ContFree failed! rc=%d\n", rc);
160 }
161 else
162 RTPrintf("SUPR3ContAlloc (2nd) failed!\n");
163 memset(pv0, 0xaf, 15003);
164 /* pv0 is intentionally not freed! */
165 }
166 else
167 RTPrintf("SUPR3ContAlloc failed!\n");
168
169 /*
170 * Allocate a big chunk of virtual memory and then lock it.
171 */
172 #define BIG_SIZE 72*1024*1024
173 #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
174 pv = NULL;
175 SUPR3PageAlloc(BIG_SIZEPP >> PAGE_SHIFT, &pv);
176 if (pv)
177 {
178 static SUPPAGE s_aPages[BIG_SIZE >> PAGE_SHIFT];
179 void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
180 rc = supR3PageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &s_aPages[0]);
181 if (!rc)
182 {
183 /* dump */
184 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
185 memset(pv, 0x42, BIG_SIZEPP);
186 #if 0
187 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
188 RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)s_aPages[j].pvPhys);
189 #endif
190
191 /* unlock */
192 rc = supR3PageUnlock(pvAligned);
193 if (rc)
194 {
195 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
196 rcRet++;
197 }
198 memset(pv, 0xcc, BIG_SIZEPP);
199 }
200 else
201 {
202 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
203 rcRet++;
204 }
205 SUPR3PageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
206 }
207
208 rc = SUPR3Term(false /*fForced*/);
209 RTPrintf("SUPR3Term -> rc=%d\n", rc);
210 rcRet += rc != 0;
211 }
212
213 return rcRet;
214}
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