VirtualBox

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

Last change on this file since 2471 was 2014, checked in by vboxsync, 18 years ago

r=bird: some cleanup of the cPages change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 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 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <VBox/sup.h>
28#include <VBox/param.h>
29#include <iprt/runtime.h>
30#include <iprt/stream.h>
31#include <stdlib.h>
32#include <string.h>
33
34
35int main(int argc, char **argv)
36{
37 int rc;
38 int rcRet = 0;
39 RTHCPHYS HCPhys;
40 void *pv;
41
42 RTR3Init(true, ~0);
43 rc = SUPInit(NULL, ~0);
44 RTPrintf("SUPInit -> rc=%d\n", rc);
45 rcRet += rc != 0;
46 if (!rc)
47 {
48 static struct
49 {
50 void *pv;
51 void *pvAligned;
52 SUPPAGE aPages[16];
53 } aPinnings[500];
54 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
55 {
56 aPinnings[i].pv = NULL;
57 SUPPageAlloc(0x10000 >> PAGE_SHIFT, &aPinnings[i].pv);
58 aPinnings[i].pvAligned = ALIGNP(aPinnings[i].pv, PAGE_SIZE);
59 rc = SUPPageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
60 if (!rc)
61 {
62 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
63 memset(aPinnings[i].pv, 0xfa, 0x10000);
64 unsigned c4GPluss = 0;
65 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
66 if (aPinnings[i].aPages[j].Phys >= _4G)
67 {
68 RTPrintf("%2d: vrt=%p phys=%VHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
69 c4GPluss++;
70 }
71 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
72 }
73 else
74 {
75 RTPrintf("SUPPageLock -> rc=%d\n", rc);
76 rcRet++;
77 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
78 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
79 break;
80 }
81 }
82
83 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
84 {
85 if (aPinnings[i].pvAligned)
86 {
87 rc = SUPPageUnlock(aPinnings[i].pvAligned);
88 if (rc)
89 {
90 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
91 rcRet++;
92 }
93 memset(aPinnings[i].pv, 0xaf, 0x10000);
94 }
95 }
96
97 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
98 {
99 if (aPinnings[i].pv)
100 {
101 memset(aPinnings[i].pv, 0xcc, 0x10000);
102 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
103 aPinnings[i].pv = NULL;
104 }
105 }
106
107
108 /*
109 * Allocate a bit of contiguous memory.
110 */
111 pv = SUPContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
112 rcRet += pv == NULL || HCPhys == 0;
113 if (pv && HCPhys)
114 {
115 RTPrintf("SUPContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
116 void *pv0 = pv;
117 memset(pv0, 0xaf, 15003);
118 pv = SUPContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
119 rcRet += pv == NULL || HCPhys == 0;
120 if (pv && HCPhys)
121 {
122 RTPrintf("SUPContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
123 memset(pv, 0xbf, 12999);
124 rc = SUPContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
125 rcRet += rc != 0;
126 if (rc)
127 RTPrintf("SUPContFree failed! rc=%d\n", rc);
128 }
129 else
130 RTPrintf("SUPContAlloc (2nd) failed!\n");
131 memset(pv0, 0xaf, 15003);
132 /* pv0 is intentionally not freed! */
133 }
134 else
135 RTPrintf("SUPContAlloc failed!\n");
136
137 /*
138 * Allocate a big chunk of virtual memory and then lock it.
139 */
140 #define BIG_SIZE 72*1024*1024
141 #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
142 pv = NULL;
143 SUPPageAlloc(BIG_SIZEPP >> PAGE_SHIFT, &pv);
144 if (pv)
145 {
146 static SUPPAGE aPages[BIG_SIZE >> PAGE_SHIFT];
147 void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
148 rc = SUPPageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &aPages[0]);
149 if (!rc)
150 {
151 /* dump */
152 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
153 memset(pv, 0x42, BIG_SIZEPP);
154 #if 0
155 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
156 RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)aPages[j].pvPhys);
157 #endif
158
159 /* unlock */
160 rc = SUPPageUnlock(pvAligned);
161 if (rc)
162 {
163 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
164 rcRet++;
165 }
166 memset(pv, 0xcc, BIG_SIZEPP);
167 }
168 else
169 {
170 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
171 rcRet++;
172 }
173 SUPPageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
174 }
175
176 rc = SUPTerm();
177 RTPrintf("SUPTerm -> rc=%d\n", rc);
178 rcRet += rc != 0;
179 }
180
181 return rcRet;
182}
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