VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstCompiler.cpp@ 26600

Last change on this file since 26600 was 26296, checked in by vboxsync, 15 years ago

more warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1/* $Id: tstCompiler.cpp 26296 2010-02-05 14:53:35Z vboxsync $ */
2/** @file
3 * Testing how the compiler deals with various things.
4 *
5 * This is testcase requires manual inspection and might not be very useful
6 * in non-optimized compiler modes.
7 */
8
9/*
10 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
21 * Clara, CA 95054 USA or visit http://www.sun.com if you need
22 * additional information or have any questions.
23 */
24
25
26/*******************************************************************************
27* Header Files *
28*******************************************************************************/
29#include <VBox/dis.h>
30#include <VBox/disopcode.h>
31#include <iprt/stream.h>
32#include <iprt/err.h>
33#include <VBox/x86.h>
34#include <iprt/string.h>
35
36#if 1
37
38/**
39 * PAE page table entry.
40 */
41#ifdef __GNUC__
42__extension__ /* Makes it shut up about the 40 bit uint64_t field. */
43#endif
44typedef struct X86PTEPAEBITS64
45{
46 /** Flags whether(=1) or not the page is present. */
47 uint64_t u1Present : 1;
48 /** Read(=0) / Write(=1) flag. */
49 uint64_t u1Write : 1;
50 /** User(=1) / Supervisor(=0) flag. */
51 uint64_t u1User : 1;
52 /** Write Thru flag. If PAT enabled, bit 0 of the index. */
53 uint64_t u1WriteThru : 1;
54 /** Cache disabled flag. If PAT enabled, bit 1 of the index. */
55 uint64_t u1CacheDisable : 1;
56 /** Accessed flag.
57 * Indicates that the page have been read or written to. */
58 uint64_t u1Accessed : 1;
59 /** Dirty flag.
60 * Indicates that the page have been written to. */
61 uint64_t u1Dirty : 1;
62 /** Reserved / If PAT enabled, bit 2 of the index. */
63 uint64_t u1PAT : 1;
64 /** Global flag. (Ignored in all but final level.) */
65 uint64_t u1Global : 1;
66 /** Available for use to system software. */
67 uint64_t u3Available : 3;
68 /** Physical Page number of the next level. */
69 uint64_t u40PageNo : 40;
70 /** MBZ bits */
71 uint64_t u11Reserved : 11;
72 /** No Execute flag. */
73 uint64_t u1NoExecute : 1;
74} X86PTEPAEBITS64;
75/** Pointer to a page table entry. */
76typedef X86PTEPAEBITS64 *PX86PTEPAEBITS64;
77
78/**
79 * PAE Page table entry.
80 */
81typedef union X86PTEPAE64
82{
83 /** Bit field view. */
84 X86PTEPAEBITS64 n;
85 /** Unsigned integer view */
86 X86PGPAEUINT u;
87 /** 32-bit view. */
88 uint32_t au32[2];
89 /** 16-bit view. */
90 uint16_t au16[4];
91 /** 8-bit view. */
92 uint8_t au8[8];
93} X86PTEPAE64;
94/** Pointer to a PAE page table entry. */
95typedef X86PTEPAE64 *PX86PTEPAE64;
96/** @} */
97
98#else /* use current (uint32_t based) PAE structures */
99
100#define X86PTEPAE64 X86PTEPAE
101#define PX86PTEPAE64 PX86PTEPAE
102
103#endif
104
105
106void SetPresent(PX86PTE pPte)
107{
108 pPte->n.u1Present = 1;
109}
110
111
112void SetPresent64(PX86PTEPAE64 pPte)
113{
114 pPte->n.u1Present = 1;
115}
116
117
118void SetWriteDirtyAccessed(PX86PTE pPte)
119{
120 pPte->n.u1Write = 1;
121 pPte->n.u1Dirty = 1;
122 pPte->n.u1Accessed = 1;
123}
124
125
126void SetWriteDirtyAccessed64(PX86PTEPAE64 pPte)
127{
128 pPte->n.u1Write = 1;
129 pPte->n.u1Dirty = 1;
130 pPte->n.u1Accessed = 1;
131}
132
133
134void SetWriteDirtyAccessedClearAVL(PX86PTE pPte)
135{
136 pPte->n.u1Write = 1;
137 pPte->n.u1Dirty = 1;
138 pPte->n.u1Accessed = 1;
139 pPte->u &= ~RT_BIT(10);
140}
141
142
143void SetWriteDirtyAccessedClearAVL64(PX86PTEPAE64 pPte)
144{
145 pPte->n.u1Write = 1;
146 pPte->n.u1Dirty = 1;
147 pPte->n.u1Accessed = 1;
148 pPte->u &= ~RT_BIT(10); /* bad, but serves as demonstration. */
149}
150
151
152bool Test3232(X86PTEPAE Pte)
153{
154 return !!(Pte.u & RT_BIT(10));
155}
156
157
158bool Test3264(X86PTEPAE Pte)
159{
160 return !!(Pte.u & RT_BIT_64(10));
161}
162
163
164bool Test6432(X86PTEPAE64 Pte)
165{
166 return !!(Pte.u & RT_BIT(10));
167}
168
169
170bool Test6464(X86PTEPAE64 Pte)
171{
172 return !!(Pte.u & RT_BIT_64(10));
173}
174
175
176void Mix6432Consts(PX86PTEPAE64 pPteDst, PX86PTEPAE64 pPteSrc)
177{
178 pPteDst->u = pPteSrc->u & ~(X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PAT | X86_PTE_PCD | X86_PTE_PWT);
179}
180
181
182void Mix32Var64Const64Data(PX86PTEPAE64 pPteDst, uint32_t fMask, uint32_t fFlags)
183{
184 pPteDst->u = (pPteDst->u & (fMask | X86_PTE_PAE_PG_MASK)) | (fFlags & ~X86_PTE_PAE_PG_MASK);
185}
186
187
188X86PTE Return32BitStruct(PX86PTE paPT)
189{
190 return paPT[10];
191}
192
193
194X86PTEPAE64 Return64BitStruct(PX86PTEPAE64 paPT)
195{
196 return paPT[10];
197}
198
199
200static void DisasFunction(const char *pszName, PFNRT pv)
201{
202 RTPrintf("tstBitFields: Disassembly of %s:\n", pszName);
203 RTUINTPTR uCur = (uintptr_t)pv;
204 RTUINTPTR uCurMax = uCur + 256;
205 DISCPUSTATE Cpu;
206
207 memset(&Cpu, 0, sizeof(Cpu));
208 Cpu.mode = CPUMODE_32BIT;
209 do
210 {
211 char sz[256];
212 uint32_t cbInstr = 0;
213 if (RT_SUCCESS(DISInstr(&Cpu, uCur, 0, &cbInstr, sz)))
214 {
215 RTPrintf("tstBitFields: %s", sz);
216 uCur += cbInstr;
217 }
218 else
219 {
220 RTPrintf("tstBitFields: %p: %02x - DISInstr failed!\n", uCur, *(uint8_t *)(uintptr_t)uCur);
221 uCur += 1;
222 }
223 } while (Cpu.pCurInstr->opcode != OP_RETN || uCur > uCurMax);
224}
225
226
227int main()
228{
229 RTPrintf("tstBitFields: This testcase requires manual inspection of the output!\n"
230 "\n"
231 "tstBitFields: The compiler must be able to combine operations when\n"
232 "tstBitFields: optimizing, if not we're screwed.\n"
233 "\n");
234 DisasFunction("SetPresent", (PFNRT)&SetPresent);
235 RTPrintf("\n");
236 DisasFunction("SetPresent64", (PFNRT)&SetPresent64);
237 RTPrintf("\n");
238 DisasFunction("SetWriteDirtyAccessed", (PFNRT)&SetWriteDirtyAccessed);
239 RTPrintf("\n");
240 DisasFunction("SetWriteDirtyAccessed64", (PFNRT)&SetWriteDirtyAccessed64);
241 RTPrintf("\n");
242 DisasFunction("SetWriteDirtyAccessedClearAVL", (PFNRT)&SetWriteDirtyAccessedClearAVL);
243 RTPrintf("\n");
244 DisasFunction("SetWriteDirtyAccessedClearAVL64", (PFNRT)&SetWriteDirtyAccessedClearAVL64);
245 RTPrintf("\n");
246 DisasFunction("Test3232", (PFNRT)&Test3232);
247 DisasFunction("Test3264", (PFNRT)&Test3264);
248 DisasFunction("Test6432", (PFNRT)&Test6432);
249 DisasFunction("Test6464", (PFNRT)&Test6464);
250 RTPrintf("\n");
251 DisasFunction("Mix6432Consts", (PFNRT)&Mix6432Consts);
252 RTPrintf("\n");
253 DisasFunction("Mix32Var64Const64Data", (PFNRT)&Mix32Var64Const64Data);
254 RTPrintf("\n");
255 DisasFunction("Return32BitStruct", (PFNRT)&Return32BitStruct);
256 RTPrintf("\n");
257 DisasFunction("Return64BitStruct", (PFNRT)&Return64BitStruct);
258 return 0;
259}
260
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