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