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