| 1 |
2 |
alfik |
/////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
// $Id: osdep.h 11689 2013-05-24 17:58:49Z vruppert $
|
| 3 |
|
|
/////////////////////////////////////////////////////////////////////////
|
| 4 |
|
|
//
|
| 5 |
|
|
// Copyright (C) 2001-2013 The Bochs Project
|
| 6 |
|
|
//
|
| 7 |
|
|
// This library is free software; you can redistribute it and/or
|
| 8 |
|
|
// modify it under the terms of the GNU Lesser General Public
|
| 9 |
|
|
// License as published by the Free Software Foundation; either
|
| 10 |
|
|
// version 2 of the License, or (at your option) any later version.
|
| 11 |
|
|
//
|
| 12 |
|
|
// This library is distributed in the hope that it will be useful,
|
| 13 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 15 |
|
|
// Lesser General Public License for more details.
|
| 16 |
|
|
//
|
| 17 |
|
|
// You should have received a copy of the GNU Lesser General Public
|
| 18 |
|
|
// License along with this library; if not, write to the Free Software
|
| 19 |
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
| 20 |
|
|
/////////////////////////////////////////////////////////////////////////
|
| 21 |
|
|
|
| 22 |
|
|
//
|
| 23 |
|
|
// osdep.h
|
| 24 |
|
|
//
|
| 25 |
|
|
// requires Bit32u/Bit64u from config.h, size_t from stdio.h
|
| 26 |
|
|
//
|
| 27 |
|
|
// Operating system dependent includes and defines for Bochs. These
|
| 28 |
|
|
// declarations can be included by C or C++., but they require definition of
|
| 29 |
|
|
// size_t beforehand. This makes it difficult to place them into either
|
| 30 |
|
|
// config.h or bochs.h. If in config.h, size_t is not always available yet.
|
| 31 |
|
|
// If in bochs.h, they can't be included by C programs so they lose.
|
| 32 |
|
|
//
|
| 33 |
|
|
|
| 34 |
|
|
#ifndef BX_OSDEP_H
|
| 35 |
|
|
#define BX_OSDEP_H
|
| 36 |
|
|
|
| 37 |
|
|
#ifdef __cplusplus
|
| 38 |
|
|
extern "C" {
|
| 39 |
|
|
#endif /* __cplusplus */
|
| 40 |
|
|
|
| 41 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 42 |
|
|
// Hacks for win32, but exclude MINGW32 because it doesn't need them.
|
| 43 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 44 |
|
|
#ifdef WIN32
|
| 45 |
|
|
|
| 46 |
|
|
// Definitions that are needed for all WIN32 compilers.
|
| 47 |
|
|
# define ssize_t long
|
| 48 |
|
|
|
| 49 |
|
|
#ifndef __MINGW32__
|
| 50 |
|
|
|
| 51 |
|
|
// Definitions that are needed for WIN32 compilers EXCEPT FOR
|
| 52 |
|
|
// cygwin compiling with -mno-cygwin. e.g. VC++.
|
| 53 |
|
|
|
| 54 |
|
|
#if !defined(_MSC_VER) // gcc without -mno-cygwin
|
| 55 |
|
|
#define FMT_LL "%ll"
|
| 56 |
|
|
#define FMT_TICK "%011llu"
|
| 57 |
|
|
#define FMT_ADDRX64 "%016llx"
|
| 58 |
|
|
#define FMT_PHY_ADDRX64 "%012llx"
|
| 59 |
|
|
#else
|
| 60 |
|
|
#define FMT_LL "%I64"
|
| 61 |
|
|
#define FMT_TICK "%011I64u"
|
| 62 |
|
|
#define FMT_ADDRX64 "%016I64x"
|
| 63 |
|
|
#define FMT_PHY_ADDRX64 "%012I64x"
|
| 64 |
|
|
#endif
|
| 65 |
|
|
|
| 66 |
|
|
// always return regular file.
|
| 67 |
|
|
#ifndef S_ISREG
|
| 68 |
|
|
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
| 69 |
|
|
#endif
|
| 70 |
|
|
#ifndef S_ISCHR
|
| 71 |
|
|
# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
|
| 72 |
|
|
#endif
|
| 73 |
|
|
|
| 74 |
|
|
// win32 has snprintf though with different name.
|
| 75 |
|
|
#define snprintf _snprintf
|
| 76 |
|
|
#define vsnprintf _vsnprintf
|
| 77 |
|
|
#undef BX_HAVE_SNPRINTF
|
| 78 |
|
|
#undef BX_HAVE_VSNPRINTF
|
| 79 |
|
|
#define BX_HAVE_SNPRINTF 1
|
| 80 |
|
|
#define BX_HAVE_VSNPRINTF 1
|
| 81 |
|
|
|
| 82 |
|
|
#if defined(_MSC_VER)
|
| 83 |
|
|
#define access _access
|
| 84 |
|
|
#define fdopen _fdopen
|
| 85 |
|
|
#define mktemp _mktemp
|
| 86 |
|
|
#define off_t __int64
|
| 87 |
|
|
#define lseek _lseeki64
|
| 88 |
|
|
#define fseeko64 _fseeki64
|
| 89 |
|
|
#define fstat _fstati64
|
| 90 |
|
|
#define stat _stati64
|
| 91 |
|
|
#define read _read
|
| 92 |
|
|
#define write _write
|
| 93 |
|
|
#define open _open
|
| 94 |
|
|
#define close _close
|
| 95 |
|
|
#define unlink _unlink
|
| 96 |
|
|
#define strdup _strdup
|
| 97 |
|
|
#define strrev _strrev
|
| 98 |
|
|
#define stricmp _stricmp
|
| 99 |
|
|
#define getch _getch
|
| 100 |
|
|
#define strtoull _strtoui64
|
| 101 |
|
|
#endif
|
| 102 |
|
|
|
| 103 |
|
|
#else /* __MINGW32__ defined */
|
| 104 |
|
|
// Definitions for cygwin compiled with -mno-cygwin
|
| 105 |
|
|
#define FMT_LL "%I64"
|
| 106 |
|
|
#define FMT_TICK "%011I64u"
|
| 107 |
|
|
#define FMT_ADDRX64 "%016I64x"
|
| 108 |
|
|
#define FMT_PHY_ADDRX64 "%012I64x"
|
| 109 |
|
|
|
| 110 |
|
|
#define off_t __int64
|
| 111 |
|
|
// mingw gcc 4.6.1 already has lseek defined
|
| 112 |
|
|
#ifndef lseek
|
| 113 |
|
|
#define lseek _lseeki64
|
| 114 |
|
|
#endif
|
| 115 |
|
|
#endif /* __MINGW32__ defined */
|
| 116 |
|
|
|
| 117 |
|
|
#else /* not WIN32 definitions */
|
| 118 |
|
|
#if SIZEOF_UNSIGNED_LONG == 8
|
| 119 |
|
|
#define FMT_LL "%l"
|
| 120 |
|
|
#define FMT_TICK "%011lu"
|
| 121 |
|
|
#define FMT_ADDRX64 "%016lx"
|
| 122 |
|
|
#define FMT_PHY_ADDRX64 "%012lx"
|
| 123 |
|
|
#else
|
| 124 |
|
|
#define FMT_LL "%ll"
|
| 125 |
|
|
#define FMT_TICK "%011llu"
|
| 126 |
|
|
#define FMT_ADDRX64 "%016llx"
|
| 127 |
|
|
#define FMT_PHY_ADDRX64 "%012llx"
|
| 128 |
|
|
#endif
|
| 129 |
|
|
#endif /* not WIN32 definitions */
|
| 130 |
|
|
|
| 131 |
|
|
#define FMT_ADDRX32 "%08x"
|
| 132 |
|
|
|
| 133 |
|
|
// Missing defines for open
|
| 134 |
|
|
#ifndef S_IRUSR
|
| 135 |
|
|
#define S_IRUSR 0400
|
| 136 |
|
|
#define S_IWUSR 0200
|
| 137 |
|
|
#endif
|
| 138 |
|
|
#ifndef S_IRGRP
|
| 139 |
|
|
#define S_IRGRP 0040
|
| 140 |
|
|
#define S_IWGRP 0020
|
| 141 |
|
|
#endif
|
| 142 |
|
|
#ifndef S_IROTH
|
| 143 |
|
|
#define S_IROTH 0004
|
| 144 |
|
|
#define S_IWOTH 0002
|
| 145 |
|
|
#endif
|
| 146 |
|
|
|
| 147 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 148 |
|
|
// Missing library functions.
|
| 149 |
|
|
// These should work on any platform that needs them.
|
| 150 |
|
|
//
|
| 151 |
|
|
// A missing library function is renamed to a bx_* function, so that when
|
| 152 |
|
|
// debugging and linking there's no confusion over which version is used.
|
| 153 |
|
|
// Because of renaming, the bx_* replacement functions can be tested on
|
| 154 |
|
|
// machines which have the real library function without duplicate symbols.
|
| 155 |
|
|
//
|
| 156 |
|
|
// If you're considering implementing a missing library function, note
|
| 157 |
|
|
// that it might be cleaner to conditionally disable the function call!
|
| 158 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 159 |
|
|
|
| 160 |
|
|
#if !BX_HAVE_SNPRINTF
|
| 161 |
|
|
#define snprintf bx_snprintf
|
| 162 |
|
|
extern int bx_snprintf (char *s, size_t maxlen, const char *format, ...);
|
| 163 |
|
|
#endif
|
| 164 |
|
|
|
| 165 |
|
|
#if !BX_HAVE_VSNPRINTF
|
| 166 |
|
|
#define vsnprintf bx_vsnprintf
|
| 167 |
|
|
extern int bx_vsnprintf (char *s, size_t maxlen, const char *format, va_list arg);
|
| 168 |
|
|
#endif
|
| 169 |
|
|
|
| 170 |
|
|
#if BX_HAVE_STRTOULL
|
| 171 |
|
|
// great, just use the usual function
|
| 172 |
|
|
#elif BX_HAVE_STRTOUQ
|
| 173 |
|
|
// they have strtouq and not strtoull
|
| 174 |
|
|
#define strtoull strtouq
|
| 175 |
|
|
#else
|
| 176 |
|
|
#define strtoull bx_strtoull
|
| 177 |
|
|
extern Bit64u bx_strtoull (const char *nptr, char **endptr, int baseignore);
|
| 178 |
|
|
#endif
|
| 179 |
|
|
|
| 180 |
|
|
#if !BX_HAVE_STRDUP
|
| 181 |
|
|
#define strdup bx_strdup
|
| 182 |
|
|
extern char *bx_strdup(const char *str);
|
| 183 |
|
|
#endif
|
| 184 |
|
|
|
| 185 |
|
|
#if !BX_HAVE_STRREV
|
| 186 |
|
|
#define strrev bx_strrev
|
| 187 |
|
|
extern char *bx_strrev(char *str);
|
| 188 |
|
|
#endif
|
| 189 |
|
|
|
| 190 |
|
|
#if BX_HAVE_STRICMP
|
| 191 |
|
|
// great, just use the usual function
|
| 192 |
|
|
#elif BX_HAVE_STRCASECMP
|
| 193 |
|
|
#define stricmp strcasecmp
|
| 194 |
|
|
#else
|
| 195 |
|
|
// FIXME: for now using case sensitive function
|
| 196 |
|
|
#define stricmp strcmp
|
| 197 |
|
|
#endif
|
| 198 |
|
|
|
| 199 |
|
|
#if !BX_HAVE_SOCKLEN_T
|
| 200 |
|
|
// needed on MacOS X 10.1
|
| 201 |
|
|
typedef int socklen_t;
|
| 202 |
|
|
#endif
|
| 203 |
|
|
|
| 204 |
|
|
#if !BX_HAVE_MKSTEMP
|
| 205 |
|
|
#define mkstemp bx_mkstemp
|
| 206 |
|
|
BOCHSAPI_MSVCONLY extern int bx_mkstemp(char *tpl);
|
| 207 |
|
|
#endif
|
| 208 |
|
|
|
| 209 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 210 |
|
|
// Missing library functions, implemented for MacOS only
|
| 211 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 212 |
|
|
|
| 213 |
|
|
#if BX_WITH_MACOS
|
| 214 |
|
|
// fd_read and fd_write are called by floppy.cc to access the Mac
|
| 215 |
|
|
// floppy drive directly, since the MacOS doesn't have "special"
|
| 216 |
|
|
// pathnames which map directly to IO devices
|
| 217 |
|
|
|
| 218 |
|
|
int fd_read(char *buffer, Bit32u offset, Bit32u bytes);
|
| 219 |
|
|
int fd_write(char *buffer, Bit32u offset, Bit32u bytes);
|
| 220 |
|
|
int fd_stat(struct stat *buf);
|
| 221 |
|
|
FILE * fdopen(int fd, const char *type);
|
| 222 |
|
|
|
| 223 |
|
|
typedef long ssize_t ;
|
| 224 |
|
|
#endif
|
| 225 |
|
|
|
| 226 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 227 |
|
|
// Missing library functions and byte-swapping stuff,
|
| 228 |
|
|
// implemented for MorphOS only
|
| 229 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 230 |
|
|
|
| 231 |
|
|
#ifdef __MORPHOS__
|
| 232 |
|
|
int fseeko(FILE *stream, off_t offset, int whence);
|
| 233 |
|
|
struct tm *localtime_r(const time_t *timep, struct tm *result);
|
| 234 |
|
|
|
| 235 |
|
|
BX_CPP_INLINE Bit16u bx_ppc_bswap16(Bit16u val)
|
| 236 |
|
|
{
|
| 237 |
|
|
Bit32u res;
|
| 238 |
|
|
|
| 239 |
|
|
__asm__("rlwimi %0,%0,16,8,15"
|
| 240 |
|
|
: "=r" (res)
|
| 241 |
|
|
: "0" (val));
|
| 242 |
|
|
|
| 243 |
|
|
return (Bit16u)(res >> 8);
|
| 244 |
|
|
}
|
| 245 |
|
|
|
| 246 |
|
|
BX_CPP_INLINE Bit32u bx_ppc_bswap32(Bit32u val)
|
| 247 |
|
|
{
|
| 248 |
|
|
Bit32u res;
|
| 249 |
|
|
|
| 250 |
|
|
__asm__("rotlwi %0,%1,8\n\t"
|
| 251 |
|
|
"rlwimi %0,%1,24,0,7\n\t"
|
| 252 |
|
|
"rlwimi %0,%1,24,16,23"
|
| 253 |
|
|
: "=&r" (res)
|
| 254 |
|
|
: "r" (val));
|
| 255 |
|
|
|
| 256 |
|
|
return res;
|
| 257 |
|
|
}
|
| 258 |
|
|
|
| 259 |
|
|
BX_CPP_INLINE Bit64u bx_ppc_bswap64(Bit64u val)
|
| 260 |
|
|
{
|
| 261 |
|
|
Bit32u hi, lo;
|
| 262 |
|
|
|
| 263 |
|
|
__asm__("rotlwi %0,%2,8\n\t"
|
| 264 |
|
|
"rlwimi %0,%2,24,0,7\n\t"
|
| 265 |
|
|
"rlwimi %0,%2,24,16,23\n\t"
|
| 266 |
|
|
"rotlwi %1,%3,8\n\t"
|
| 267 |
|
|
"rlwimi %1,%3,24,0,7\n\t"
|
| 268 |
|
|
"rlwimi %1,%3,24,16,23"
|
| 269 |
|
|
: "=&r" (hi), "=&r" (lo)
|
| 270 |
|
|
: "r" ((Bit32u)(val & 0xffffffff)), "r" ((Bit32u)(val >> 32)));
|
| 271 |
|
|
|
| 272 |
|
|
return ((Bit64u)hi << 32) | (Bit64u)lo;
|
| 273 |
|
|
}
|
| 274 |
|
|
|
| 275 |
|
|
BX_CPP_INLINE Bit16u bx_ppc_load_le16(const Bit16u *p)
|
| 276 |
|
|
{
|
| 277 |
|
|
Bit16u v;
|
| 278 |
|
|
__asm__("lhbrx %0, 0, %1"
|
| 279 |
|
|
: "=r" (v)
|
| 280 |
|
|
: "r" (p), "m" (*p));
|
| 281 |
|
|
return v;
|
| 282 |
|
|
}
|
| 283 |
|
|
|
| 284 |
|
|
BX_CPP_INLINE void bx_ppc_store_le16(Bit16u *p, Bit16u v)
|
| 285 |
|
|
{
|
| 286 |
|
|
__asm__("sthbrx %1, 0, %2"
|
| 287 |
|
|
: "=m" (*p)
|
| 288 |
|
|
: "r" (v), "r" (p));
|
| 289 |
|
|
}
|
| 290 |
|
|
|
| 291 |
|
|
BX_CPP_INLINE Bit32u bx_ppc_load_le32(const Bit32u *p)
|
| 292 |
|
|
{
|
| 293 |
|
|
Bit32u v;
|
| 294 |
|
|
__asm__("lwbrx %0, 0, %1"
|
| 295 |
|
|
: "=r" (v)
|
| 296 |
|
|
: "r" (p), "m" (*p));
|
| 297 |
|
|
return v;
|
| 298 |
|
|
}
|
| 299 |
|
|
|
| 300 |
|
|
BX_CPP_INLINE void bx_ppc_store_le32(Bit32u *p, Bit32u v)
|
| 301 |
|
|
{
|
| 302 |
|
|
__asm__("stwbrx %1, 0, %2"
|
| 303 |
|
|
: "=m" (*p)
|
| 304 |
|
|
: "r" (v), "r" (p));
|
| 305 |
|
|
}
|
| 306 |
|
|
|
| 307 |
|
|
BX_CPP_INLINE Bit64u bx_ppc_load_le64(const Bit64u *p)
|
| 308 |
|
|
{
|
| 309 |
|
|
Bit32u hi, lo;
|
| 310 |
|
|
|
| 311 |
|
|
__asm__("lwbrx %0, 0, %2\n\t"
|
| 312 |
|
|
"lwbrx %1, 0, %3"
|
| 313 |
|
|
: "=&r" (lo), "=&r" (hi)
|
| 314 |
|
|
: "r" ((Bit32u *)p), "r" ((Bit32u *)p+1));
|
| 315 |
|
|
|
| 316 |
|
|
return ((Bit64u)hi << 32) | (Bit64u)lo;
|
| 317 |
|
|
}
|
| 318 |
|
|
|
| 319 |
|
|
BX_CPP_INLINE void bx_ppc_store_le64(Bit64u *p, Bit64u v)
|
| 320 |
|
|
{
|
| 321 |
|
|
__asm__("stwbrx %1, 0, %3\n\t"
|
| 322 |
|
|
"stwbrx %2, 0, %4"
|
| 323 |
|
|
: "=m" (*p)
|
| 324 |
|
|
: "r" ((Bit32u)(v & 0xffffffff)), "r" ((Bit32u)(v >> 32)),
|
| 325 |
|
|
"r" ((Bit32u *)p), "r" ((Bit32u *)p+1));
|
| 326 |
|
|
}
|
| 327 |
|
|
#endif
|
| 328 |
|
|
|
| 329 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 330 |
|
|
// New functions to replace library functions
|
| 331 |
|
|
// with OS-independent versions
|
| 332 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 333 |
|
|
|
| 334 |
|
|
#if BX_HAVE_REALTIME_USEC
|
| 335 |
|
|
// 64-bit time in useconds.
|
| 336 |
|
|
extern Bit64u bx_get_realtime64_usec (void);
|
| 337 |
|
|
#endif
|
| 338 |
|
|
|
| 339 |
|
|
#ifdef WIN32
|
| 340 |
|
|
#undef BX_HAVE_MSLEEP
|
| 341 |
|
|
#define BX_HAVE_MSLEEP 1
|
| 342 |
|
|
#if !defined(__MINGW32__) && !defined(_MSC_VER)
|
| 343 |
|
|
#define msleep(msec) _sleep(msec)
|
| 344 |
|
|
#else
|
| 345 |
|
|
#define msleep(msec) Sleep(msec)
|
| 346 |
|
|
#endif
|
| 347 |
|
|
#endif
|
| 348 |
|
|
|
| 349 |
|
|
#ifdef __cplusplus
|
| 350 |
|
|
}
|
| 351 |
|
|
#endif /* __cplusplus */
|
| 352 |
|
|
|
| 353 |
|
|
#if BX_LARGE_RAMFILE
|
| 354 |
|
|
|
| 355 |
|
|
// these macros required for large ramfile option functionality
|
| 356 |
|
|
#if BX_HAVE_TMPFILE64 == 0
|
| 357 |
|
|
#define tmpfile64 tmpfile /* use regular tmpfile() function */
|
| 358 |
|
|
#endif
|
| 359 |
|
|
|
| 360 |
|
|
#if BX_HAVE_FSEEKO64 == 0
|
| 361 |
|
|
#if BX_HAVE_FSEEK64
|
| 362 |
|
|
#define fseeko64 fseek64 /* use fseek64() function */
|
| 363 |
|
|
#elif !defined(_MSC_VER)
|
| 364 |
|
|
#define fseeko64 fseeko /* use regular fseeko() function */
|
| 365 |
|
|
#endif
|
| 366 |
|
|
#endif
|
| 367 |
|
|
|
| 368 |
|
|
#endif // BX_LARGE_RAMFILE
|
| 369 |
|
|
|
| 370 |
|
|
#endif /* ifdef BX_OSDEP_H */
|