VirtualBox

source: vbox/trunk/src/VBox/Devices/testcase/tstDeviceStructSize.cpp@ 34434

Last change on this file since 34434 was 32471, checked in by vboxsync, 14 years ago

Devices: refactoring, further PCI work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 32471 2010-09-14 10:26:07Z vboxsync $ */
2/** @file
3 * tstDeviceStructSize - testcase for check structure sizes/alignment
4 * and to verify that HC and RC uses the same
5 * representation of the structures.
6 */
7
8/*
9 * Copyright (C) 2006-2010 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include <VBox/types.h>
24#include <VBox/x86.h>
25
26
27#define VBOX_WITH_HGCM /* grumble */
28#define VBOX_DEVICE_STRUCT_TESTCASE
29#undef LOG_GROUP
30#include "../Bus/DevPCI.cpp"
31#undef LOG_GROUP
32#include "../Graphics/DevVGA.cpp"
33#undef LOG_GROUP
34#include "../Input/DevPS2.cpp"
35#ifdef VBOX_WITH_E1000
36# undef LOG_GROUP
37# include "../Network/DevE1000.cpp"
38#endif
39#undef LOG_GROUP
40#include "../Network/DevPCNet.cpp"
41#ifdef VBOX_WITH_VIRTIO
42# undef LOG_GROUP
43# include "../Network/DevVirtioNet.cpp"
44#endif
45#undef LOG_GROUP
46#include "../PC/DevACPI.cpp"
47#undef LOG_GROUP
48#include "../PC/DevPIC.cpp"
49#undef LOG_GROUP
50#include "../PC/DevPit-i8254.cpp"
51#undef LOG_GROUP
52#include "../PC/DevRTC.cpp"
53#undef LOG_GROUP
54#include "../PC/DevAPIC.cpp"
55#undef LOG_GROUP
56#include "../PC/DevHPET.cpp"
57#undef LOG_GROUP
58#include "../PC/DevLPC.cpp"
59#undef LOG_GROUP
60#include "../PC/DevSMC.cpp"
61#undef LOG_GROUP
62#include "../Storage/DevATA.cpp"
63#ifdef VBOX_WITH_USB
64# undef LOG_GROUP
65# include "../USB/DevOHCI.cpp"
66# ifdef VBOX_WITH_EHCI
67# include "../USB/DevEHCI.cpp"
68# endif
69#endif
70#undef LOG_GROUP
71#include "../VMMDev/VMMDev.cpp"
72#undef LOG_GROUP
73#include "../Parallel/DevParallel.cpp"
74#undef LOG_GROUP
75#include "../Serial/DevSerial.cpp"
76#ifdef VBOX_WITH_AHCI
77# undef LOG_GROUP
78# include "../Storage/DevAHCI.cpp"
79#endif
80#ifdef VBOX_WITH_BUSLOGIC
81# undef LOG_GROUP
82# include "../Storage/DevBusLogic.cpp"
83#endif
84#ifdef VBOX_WITH_LSILOGIC
85# undef LOG_GROUP
86# include "../Storage/DevLsiLogicSCSI.cpp"
87#endif
88
89#include <stdio.h>
90
91
92/*******************************************************************************
93* Defined Constants And Macros *
94*******************************************************************************/
95/**
96 * Checks the offset of a data member.
97 * @param type Type.
98 * @param off Correct offset.
99 * @param m Member name.
100 */
101#define CHECK_OFF(type, off, m) \
102 do { \
103 if (off != RT_OFFSETOF(type, m)) \
104 { \
105 printf("tstDeviceStructSize: error! %#010x %s Off by %d!! (off=%#x)\n", RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off); \
106 rc++; \
107 } \
108 /*else */ \
109 /*printf("%#08x %s\n", RT_OFFSETOF(type, m), #m);*/ \
110 } while (0)
111
112/**
113 * Checks the size of type.
114 * @param type Type.
115 * @param size Correct size.
116 */
117#define CHECK_SIZE(type, size) \
118 do { \
119 if (size != sizeof(type)) \
120 { \
121 printf("tstDeviceStructSize: error! sizeof(%s): %#x (%d) Off by %d!!\n", #type, (int)sizeof(type), (int)sizeof(type), (int)(sizeof(type) - size)); \
122 rc++; \
123 } \
124 else \
125 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
126 } while (0)
127
128/**
129 * Checks the alignment of a struct member.
130 */
131#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
132 do \
133 { \
134 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
135 { \
136 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %x, meaning %#x (%u) off\n", \
137 #strct, #member, \
138 (unsigned)RT_OFFSETOF(strct, member), \
139 (unsigned)RT_OFFSETOF(strct, member), \
140 (unsigned)(align), \
141 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
142 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
143 rc++; \
144 } \
145 } while (0)
146
147/**
148 * Checks that the size of a type is aligned correctly.
149 */
150#define CHECK_SIZE_ALIGNMENT(type, align) \
151 do { \
152 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
153 { \
154 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
155 #type, \
156 (unsigned)sizeof(type), \
157 (unsigned)sizeof(type), \
158 (align), \
159 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
160 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
161 rc++; \
162 } \
163 } while (0)
164
165/**
166 * Checks that a internal struct padding is big enough.
167 */
168#define CHECK_PADDING(strct, member, align) \
169 do \
170 { \
171 strct *p = NULL; NOREF(p); \
172 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
173 { \
174 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
175 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
176 rc++; \
177 } \
178 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
179 { \
180 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
181 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
182 rc++; \
183 } \
184 } while (0)
185
186/**
187 * Checks that a internal struct padding is big enough.
188 */
189#define CHECK_PADDING2(strct) \
190 do \
191 { \
192 strct *p = NULL; NOREF(p); \
193 if (sizeof(p->s) > sizeof(p->padding)) \
194 { \
195 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
196 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
197 rc++; \
198 } \
199 } while (0)
200
201/**
202 * Checks that a internal struct padding is big enough.
203 */
204#define CHECK_PADDING3(strct, member, pad_member) \
205 do \
206 { \
207 strct *p = NULL; NOREF(p); \
208 if (sizeof(p->member) > sizeof(p->pad_member)) \
209 { \
210 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
211 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
212 rc++; \
213 } \
214 } while (0)
215
216/**
217 * Prints the offset of a struct member.
218 */
219#define PRINT_OFFSET(strct, member) \
220 do \
221 { \
222 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
223 } while (0)
224
225
226int main()
227{
228 int rc = 0;
229 printf("tstDeviceStructSize: TESTING\n");
230
231 /* Assert sanity */
232 CHECK_SIZE(uint128_t, 128/8);
233 CHECK_SIZE(int128_t, 128/8);
234 CHECK_SIZE(uint64_t, 64/8);
235 CHECK_SIZE(int64_t, 64/8);
236 CHECK_SIZE(uint32_t, 32/8);
237 CHECK_SIZE(int32_t, 32/8);
238 CHECK_SIZE(uint16_t, 16/8);
239 CHECK_SIZE(int16_t, 16/8);
240 CHECK_SIZE(uint8_t, 8/8);
241 CHECK_SIZE(int8_t, 8/8);
242
243 /* Basic alignment checks. */
244 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
245 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s, 16);
246 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s.aIORegions, 16);
247
248 /*
249 * Misc alignment checks (keep this somewhat alphabetical).
250 */
251 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
252 CHECK_MEMBER_ALIGNMENT(AHCIPort, StatDMA, 8);
253 CHECK_MEMBER_ALIGNMENT(AHCIATACONTROLLER, lock, 8);
254 CHECK_MEMBER_ALIGNMENT(AHCIATACONTROLLER, StatAsyncOps, 8);
255#ifdef VBOX_WITH_STATISTICS
256 CHECK_MEMBER_ALIGNMENT(APICDeviceInfo, StatMMIOReadGC, 8);
257#endif
258 CHECK_MEMBER_ALIGNMENT(ATADevState, cTotalSectors, 8);
259 CHECK_MEMBER_ALIGNMENT(ATADevState, StatATADMA, 8);
260 CHECK_MEMBER_ALIGNMENT(ATADevState, StatReads, 8);
261 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
262 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
263 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
264 CHECK_MEMBER_ALIGNMENT(DEVPARALLELSTATE, CritSect, 8);
265#ifdef VBOX_WITH_STATISTICS
266 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqGC, 8);
267#endif
268#ifdef VBOX_WITH_E1000
269 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
270 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
271 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
272#endif
273#ifdef VBOX_WITH_VIRTIO
274 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
275#endif
276 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
277#ifdef VBOX_WITH_USB
278# ifdef VBOX_WITH_EHCI
279 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
280# ifdef VBOX_WITH_STATISTICS
281 CHECK_MEMBER_ALIGNMENT(EHCI, StatCanceledIsocUrbs, 8);
282# endif
283# endif
284#endif
285 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
286#ifdef VBOX_WITH_STATISTICS
287 CHECK_MEMBER_ALIGNMENT(IOAPICState, StatMMIOReadGC, 8);
288 CHECK_MEMBER_ALIGNMENT(IOAPICState, StatMMIOReadGC, 8);
289#endif
290 CHECK_MEMBER_ALIGNMENT(KBDState, CritSect, 8);
291 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyPostQueueCritSect, 8);
292 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyFreeQueueCritSect, 8);
293#ifdef VBOX_WITH_USB
294 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
295# ifdef VBOX_WITH_STATISTICS
296 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
297# endif
298#endif
299 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
300 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
301 CHECK_MEMBER_ALIGNMENT(PCIGLOBALS, pci_irq_levels, 16);
302 CHECK_MEMBER_ALIGNMENT(PCNetState, u64LastPoll, 8);
303 CHECK_MEMBER_ALIGNMENT(PCNetState, CritSect, 8);
304 CHECK_MEMBER_ALIGNMENT(PCNetState, StatReceiveBytes, 8);
305#ifdef VBOX_WITH_STATISTICS
306 CHECK_MEMBER_ALIGNMENT(PCNetState, StatMMIOReadRZ, 8);
307#endif
308 CHECK_MEMBER_ALIGNMENT(PITState, StatPITIrq, 8);
309 CHECK_MEMBER_ALIGNMENT(SerialState, CritSect, 8);
310 CHECK_MEMBER_ALIGNMENT(VGASTATE, Dev, 8);
311 CHECK_MEMBER_ALIGNMENT(VGASTATE, lock, 8);
312 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
313 CHECK_MEMBER_ALIGNMENT(VMMDevState, CritSect, 8);
314#ifdef VBOX_WITH_VIRTIO
315 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
316 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
317 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
318#endif
319
320#ifdef VBOX_WITH_RAW_MODE
321 /*
322 * Compare HC and RC.
323 */
324 printf("tstDeviceStructSize: Comparing HC and RC...\n");
325# include "tstDeviceStructSizeRC.h"
326#endif
327
328 /*
329 * Report result.
330 */
331 if (rc)
332 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
333 else
334 printf("tstDeviceStructSize: SUCCESS\n");
335 return rc;
336}
337
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