1
/*********************************************************
2
* Copyright (C) 2003 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License as published by the
6
* Free Software Foundation version 2 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* You should have received a copy of the GNU General Public License along
14
* with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
*********************************************************/
22
* Standard macros for VMware source code.
25
#ifndef _VM_BASIC_DEFS_H_
26
#define _VM_BASIC_DEFS_H_
28
#define INCLUDE_ALLOW_USERLEVEL
29
#define INCLUDE_ALLOW_VMMEXT
30
#define INCLUDE_ALLOW_MODULE
31
#define INCLUDE_ALLOW_VMMON
32
#define INCLUDE_ALLOW_VMNIXMOD
33
#define INCLUDE_ALLOW_VMKERNEL
34
#define INCLUDE_ALLOW_VMKDRIVERS
35
#define INCLUDE_ALLOW_VMK_MODULE
36
#define INCLUDE_ALLOW_DISTRIBUTE
37
#define INCLUDE_ALLOW_VMCORE
38
#define INCLUDE_ALLOW_VMIROM
39
#include "includeCheck.h"
40
#include "vm_basic_types.h" // For INLINE.
42
/* Checks for FreeBSD, filtering out VMKERNEL. */
43
#define __IS_FREEBSD__ (!defined(VMKERNEL) && defined(__FreeBSD__))
44
#define __IS_FREEBSD_VER__(ver) (__IS_FREEBSD__ && __FreeBSD_version >= (ver))
46
#if defined _WIN32 && defined USERLEVEL
47
#include <stddef.h> /*
48
* We re-define offsetof macro from stddef, make
49
* sure that its already defined before we do it
51
#include <windows.h> // for Sleep() and LOWORD() etc.
59
#if (defined __APPLE__ || defined __FreeBSD__) && \
60
(!defined KERNEL && !defined _KERNEL && !defined VMKERNEL && !defined __KERNEL__)
63
// XXX the __cplusplus one matches that of VC++, to prevent redefinition warning
64
// XXX the other one matches that of gcc3.3.3/glibc2.2.4 to prevent redefinition warnings
67
#define offsetof(s,m) (size_t)&(((s *)0)->m)
69
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
75
#define ARRAYSIZE(a) (sizeof (a) / sizeof *(a))
79
#define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
82
/* The Solaris 9 cross-compiler complains about these not being used */
92
#define MAX(_a, _b) (((_a) > (_b)) ? (_a) : (_b))
103
#define ROUNDUP(x,y) (((x) + (y) - 1) / (y) * (y))
104
#define ROUNDDOWN(x,y) ((x) / (y) * (y))
105
#define ROUNDUPBITS(x, bits) (((uintptr_t) (x) + MASK(bits)) & ~MASK(bits))
106
#define ROUNDDOWNBITS(x, bits) ((uintptr_t) (x) & ~MASK(bits))
107
#define CEILING(x, y) (((x) + (y) - 1) / (y))
108
#if defined __APPLE__
109
#include <machine/param.h>
112
#define MASK(n) ((1 << (n)) - 1) /* make an n-bit mask */
113
#define DWORD_ALIGN(x) ((((x)+3) >> 2) << 2)
114
#define QWORD_ALIGN(x) ((((x)+4) >> 3) << 3)
116
#define IMPLIES(a,b) (!(a) || (b))
119
* Not everybody (e.g., the monitor) has NULL
126
#define NULL ((void *)0)
132
* Token concatenation
134
* The C preprocessor doesn't prescan arguments when they are
135
* concatenated or stringified. So we need extra levels of
136
* indirection to convince the preprocessor to expand its
140
#define CONC(x, y) x##y
141
#define XCONC(x, y) CONC(x, y)
142
#define XXCONC(x, y) XCONC(x, y)
143
#define MAKESTR(x) #x
144
#define XSTR(x) MAKESTR(x)
150
* It has been suggested that these definitions belong elsewhere
151
* (like x86types.h). However, I deem them common enough
152
* (since even regular user-level programs may want to do
153
* page-based memory manipulation) to be here.
157
#ifndef PAGE_SHIFT // {
159
#define PAGE_SHIFT 12
160
#elif defined __APPLE__
161
#define PAGE_SHIFT 12
168
#define PAGE_SIZE (1<<PAGE_SHIFT)
172
#define PAGE_MASK (PAGE_SIZE - 1)
176
#define PAGE_OFFSET(_addr) ((uintptr_t)(_addr)&(PAGE_SIZE-1))
180
#define VM_PAGE_BASE(_addr) ((_addr)&~(PAGE_SIZE-1))
183
#ifndef VM_PAGES_SPANNED
184
#define VM_PAGES_SPANNED(_addr, _size) \
185
((((_addr) & (PAGE_SIZE - 1)) + (_size) + (PAGE_SIZE - 1)) >> PAGE_SHIFT)
188
#ifndef BYTES_2_PAGES
189
#define BYTES_2_PAGES(_nbytes) ((_nbytes) >> PAGE_SHIFT)
192
#ifndef PAGES_2_BYTES
193
#define PAGES_2_BYTES(_npages) (((uint64)(_npages)) << PAGE_SHIFT)
196
#ifndef MBYTES_2_PAGES
197
#define MBYTES_2_PAGES(_nbytes) ((_nbytes) << (20 - PAGE_SHIFT))
200
#ifndef PAGES_2_MBYTES
201
#define PAGES_2_MBYTES(_npages) ((_npages) >> (20 - PAGE_SHIFT))
204
#ifndef VM_PAE_LARGE_PAGE_SHIFT
205
#define VM_PAE_LARGE_PAGE_SHIFT 21
208
#ifndef VM_PAE_LARGE_PAGE_SIZE
209
#define VM_PAE_LARGE_PAGE_SIZE (1 << VM_PAE_LARGE_PAGE_SHIFT)
212
#ifndef VM_PAE_LARGE_PAGE_MASK
213
#define VM_PAE_LARGE_PAGE_MASK (VM_PAE_LARGE_PAGE_SIZE - 1)
216
#ifndef VM_PAE_LARGE_2_SMALL_PAGES
217
#define VM_PAE_LARGE_2_SMALL_PAGES (BYTES_2_PAGES(VM_PAE_LARGE_PAGE_SIZE))
225
#define LOWORD(_dw) ((_dw) & 0xffff)
228
#define HIWORD(_dw) (((_dw) >> 16) & 0xffff)
232
#define LOBYTE(_w) ((_w) & 0xff)
235
#define HIBYTE(_w) (((_w) >> 8) & 0xff)
238
#define HIDWORD(_qw) ((uint32)((_qw) >> 32))
239
#define LODWORD(_qw) ((uint32)(_qw))
240
#define QWORD(_hi, _lo) ((((uint64)(_hi)) << 32) | ((uint32)(_lo)))
244
* Deposit a field _src at _pos bits from the right,
245
* with a length of _len, into the integer _target.
248
#define DEPOSIT_BITS(_src,_pos,_len,_target) { \
249
unsigned mask = ((1 << _len) - 1); \
250
unsigned shiftedmask = ((1 << _len) - 1) << _pos; \
251
_target = (_target & ~shiftedmask) | ((_src & mask) << _pos); \
256
* Get return address.
263
void *_ReturnAddress(void);
264
#pragma intrinsic(_ReturnAddress)
265
#define GetReturnAddress() _ReturnAddress()
267
#define GetReturnAddress() __builtin_return_address(0)
275
* Get the frame pointer. We use this assembly hack instead of
276
* __builtin_frame_address() due to a bug introduced in gcc 4.1.1
278
static INLINE_SINGLE_CALLER uintptr_t
282
#if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0))
283
bp = (uintptr_t)__builtin_frame_address(0);
284
#elif (__GNUC__ == 4 && __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ <= 3)
285
# if defined(VMM64) || defined(VM_X86_64)
286
__asm__ __volatile__("movq %%rbp, %0\n" : "=g" (bp));
288
__asm__ __volatile__("movl %%ebp, %0\n" : "=g" (bp));
291
__asm__ __volatile__(
293
".print \"This newer version of GCC may or may not have the "
294
"__builtin_frame_address bug. Need to update this. "
295
"See bug 147638.\"\n"
298
".abort \"This newer version of GCC may or may not have the "
299
"__builtin_frame_address bug. Need to update this. "
300
"See bug 147638.\"\n"
310
* Returns the frame pointer of the calling function.
311
* Equivalent to __builtin_frame_address(1).
313
static INLINE_SINGLE_CALLER uintptr_t
314
GetCallerFrameAddr(void)
316
return *(uintptr_t*)GetFrameAddr();
323
* Data prefetch was added in gcc 3.1.1
324
* http://www.gnu.org/software/gcc/gcc-3.1/changes.html
327
# if ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 1) || \
328
(__GNUC__ == 3 && __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ >= 1))
329
# define PREFETCH_R(var) __builtin_prefetch((var), 0 /* read */, \
330
3 /* high temporal locality */)
331
# define PREFETCH_W(var) __builtin_prefetch((var), 1 /* write */, \
332
3 /* high temporal locality */)
334
# define PREFETCH_R(var) ((void)(var))
335
# define PREFETCH_W(var) ((void)(var))
337
#endif /* __GNUC__ */
340
#ifdef USERLEVEL // {
343
* Note this might be a problem on NT b/c while sched_yield guarantees it
344
* moves you to the end of your priority list, Sleep(0) offers no such
345
* guarantee. Bummer. --Jeremy.
348
#if defined(N_PLAT_NLM)
349
/* We do not have YIELD() as we do not need it yet... */
350
#elif defined(_WIN32)
351
# define YIELD() Sleep(0)
353
# include <sched.h> // For sched_yield. Don't ask. --Jeremy.
354
# define YIELD() sched_yield()
359
* Standardize some Posix names on Windows.
364
#define snprintf _snprintf
365
#define vsnprintf _vsnprintf
366
#define strtok_r strtok_s
372
sleep(unsigned int sec)
378
usleep(unsigned long usec)
380
Sleep(CEILING(usec, 1000));
392
* Macro for username comparison.
396
#define USERCMP(x,y) Str_Strcasecmp(x,y)
398
#define USERCMP(x,y) strcmp(x,y)
409
* Windows needs va_copy. This works for both 32 and 64-bit Windows
410
* based on inspection of how varags.h from the Visual C CRTL is
411
* implemented. (Future versions of the RTL may break this).
414
#define va_copy(dest, src) ((dest) = (src))
416
#elif defined(__APPLE__) && defined(KERNEL)
419
* MacOS kernel-mode needs va_copy. Based on inspection of stdarg.h
420
* from the MacOSX10.4u.sdk kernel framework, this should work.
421
* (Future versions of the SDK may break this).
424
#define va_copy(dest, src) ((dest) = (src))
426
#elif defined(__GNUC__) && (__GNUC__ < 3)
429
* Old versions of gcc recognize __va_copy, but not va_copy.
432
#define va_copy(dest, src) __va_copy(dest, src)
439
* This one is outside USERLEVEL because it's used by
440
* files compiled into the Windows hgfs driver or the display
447
#define strcasecmp(_s1,_s2) _stricmp((_s1),(_s2))
450
#define strncasecmp(_s1,_s2,_n) _strnicmp((_s1),(_s2),(_n))
455
* Convenience macro for COMMUNITY_SOURCE
457
#undef EXCLUDE_COMMUNITY_SOURCE
458
#ifdef COMMUNITY_SOURCE
459
#define EXCLUDE_COMMUNITY_SOURCE(x)
461
#define EXCLUDE_COMMUNITY_SOURCE(x) x
464
#undef COMMUNITY_SOURCE_INTEL_SECRET
465
#if !defined(COMMUNITY_SOURCE) || defined(INTEL_SOURCE)
467
* It's ok to include INTEL_SECRET source code for non-commsrc,
468
* or for drops directed at Intel.
470
#define COMMUNITY_SOURCE_INTEL_SECRET
474
* Convenience macros and definitions. Can often be used instead of #ifdef.
479
#undef VMX86_SL_DEBUG
481
#define vmx86_debug 1
482
#define DEBUG_ONLY(x) x
484
* Be very, very, very careful with SL_DEBUG. Pls ask ganesh or min before
487
#define VMX86_SL_DEBUG
488
#define vmx86_sl_debug 1
489
#define SL_DEBUG_ONLY(x) x
491
#define vmx86_debug 0
492
#define DEBUG_ONLY(x)
493
#define vmx86_sl_debug 0
494
#define SL_DEBUG_ONLY(x)
498
#define vmx86_stats 1
499
#define STATS_ONLY(x) x
501
#define vmx86_stats 0
502
#define STATS_ONLY(x)
506
#define vmx86_devel 1
507
#define DEVEL_ONLY(x) x
509
#define vmx86_devel 0
510
#define DEVEL_ONLY(x)
515
#define LOG_ONLY(x) x
521
#ifdef VMX86_VMM_SERIAL_LOGGING
522
#define vmx86_vmm_serial_log 1
523
#define VMM_SERIAL_LOG_ONLY(x) x
525
#define vmx86_vmm_serial_log 0
526
#define VMM_SERIAL_LOG_ONLY(x)
530
#define vmx86_server 1
531
#define SERVER_ONLY(x) x
532
#define HOSTED_ONLY(x)
534
#define vmx86_server 0
535
#define SERVER_ONLY(x)
536
#define HOSTED_ONLY(x) x
541
#define WGS_ONLY(x) x
549
#define VMKERNEL_ONLY(x) x
552
#define VMKERNEL_ONLY(x)
556
#define WIN32_ONLY(x) x
557
#define POSIX_ONLY(x)
559
#define WIN32_ONLY(x)
560
#define POSIX_ONLY(x) x
564
#define VMM_ONLY(x) x
568
#define USER_ONLY(x) x
571
/* VMVISOR ifdef only allowed in the vmkernel */
575
#define VMVISOR_ONLY(x) x
578
#define VMVISOR_ONLY(x)
583
#define VMW_INVALID_HANDLE INVALID_HANDLE_VALUE
585
#define VMW_INVALID_HANDLE (-1)
589
#define fsync(fd) _commit(fd)
590
#define fileno(f) _fileno(f)
595
* Debug output macros for Windows drivers (the Eng variant is for
596
* display/printer drivers only.
599
#ifndef USES_OLD_WINDDK
600
#if defined(VMX86_DEBUG) || defined(ASSERT_ALWAYS_AVAILABLE)
601
#define WinDrvPrint(arg, ...) DbgPrint(arg, __VA_ARGS__)
602
#define WinDrvEngPrint(arg, ...) EngDbgPrint(arg, __VA_ARGS__)
604
#define WinDrvPrint(arg, ...)
605
#define WinDrvEngPrint(arg, ...)
610
#endif // ifndef _VM_BASIC_DEFS_H_