VirtualBox

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

Last change on this file since 30045 was 29435, checked in by vboxsync, 15 years ago

DevPCNet,DevE1000: Statistics/profiling.

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