| 1 |
/* |
| 2 |
=========================================================================== |
| 3 |
Copyright (C) 1999-2005 Id Software, Inc. |
| 4 |
|
| 5 |
This file is part of Quake III Arena source code. |
| 6 |
|
| 7 |
Quake III Arena source code is free software; you can redistribute it |
| 8 |
and/or modify it under the terms of the GNU General Public License as |
| 9 |
published by the Free Software Foundation; either version 2 of the License, |
| 10 |
or (at your option) any later version. |
| 11 |
|
| 12 |
Quake III Arena source code is distributed in the hope that it will be |
| 13 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with Quake III Arena source code; if not, write to the Free Software |
| 19 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
=========================================================================== |
| 21 |
*/ |
| 22 |
// vm.c -- virtual machine |
| 23 |
|
| 24 |
/* |
| 25 |
|
| 26 |
|
| 27 |
intermix code and data |
| 28 |
symbol table |
| 29 |
|
| 30 |
a dll has one imported function: VM_SystemCall |
| 31 |
and one exported function: Perform |
| 32 |
|
| 33 |
|
| 34 |
*/ |
| 35 |
|
| 36 |
#include "vm_local.h" |
| 37 |
|
| 38 |
|
| 39 |
vm_t *currentVM = NULL; |
| 40 |
vm_t *lastVM = NULL; |
| 41 |
int vm_debugLevel; |
| 42 |
|
| 43 |
// used by Com_Error to get rid of running vm's before longjmp |
| 44 |
static int forced_unload; |
| 45 |
|
| 46 |
#define MAX_VM 3 |
| 47 |
vm_t vmTable[MAX_VM]; |
| 48 |
|
| 49 |
|
| 50 |
void VM_VmInfo_f( void ); |
| 51 |
void VM_VmProfile_f( void ); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#if 0 // 64bit! |
| 56 |
// converts a VM pointer to a C pointer and |
| 57 |
// checks to make sure that the range is acceptable |
| 58 |
void *VM_VM2C( vmptr_t p, int length ) { |
| 59 |
return (void *)p; |
| 60 |
} |
| 61 |
#endif |
| 62 |
|
| 63 |
void VM_Debug( int level ) { |
| 64 |
vm_debugLevel = level; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
============== |
| 69 |
VM_Init |
| 70 |
============== |
| 71 |
*/ |
| 72 |
void VM_Init( void ) { |
| 73 |
Cvar_Get( "vm_cgame", "2", CVAR_ARCHIVE ); // !@# SHIP WITH SET TO 2 |
| 74 |
Cvar_Get( "vm_game", "2", CVAR_ARCHIVE ); // !@# SHIP WITH SET TO 2 |
| 75 |
Cvar_Get( "vm_ui", "2", CVAR_ARCHIVE ); // !@# SHIP WITH SET TO 2 |
| 76 |
|
| 77 |
Cmd_AddCommand ("vmprofile", VM_VmProfile_f ); |
| 78 |
Cmd_AddCommand ("vminfo", VM_VmInfo_f ); |
| 79 |
|
| 80 |
Com_Memset( vmTable, 0, sizeof( vmTable ) ); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/* |
| 85 |
=============== |
| 86 |
VM_ValueToSymbol |
| 87 |
|
| 88 |
Assumes a program counter value |
| 89 |
=============== |
| 90 |
*/ |
| 91 |
const char *VM_ValueToSymbol( vm_t *vm, int value ) { |
| 92 |
vmSymbol_t *sym; |
| 93 |
static char text[MAX_TOKEN_CHARS]; |
| 94 |
|
| 95 |
sym = vm->symbols; |
| 96 |
if ( !sym ) { |
| 97 |
return "NO SYMBOLS"; |
| 98 |
} |
| 99 |
|
| 100 |
// find the symbol |
| 101 |
while ( sym->next && sym->next->symValue <= value ) { |
| 102 |
sym = sym->next; |
| 103 |
} |
| 104 |
|
| 105 |
if ( value == sym->symValue ) { |
| 106 |
return sym->symName; |
| 107 |
} |
| 108 |
|
| 109 |
Com_sprintf( text, sizeof( text ), "%s+%i", sym->symName, value - sym->symValue ); |
| 110 |
|
| 111 |
return text; |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
=============== |
| 116 |
VM_ValueToFunctionSymbol |
| 117 |
|
| 118 |
For profiling, find the symbol behind this value |
| 119 |
=============== |
| 120 |
*/ |
| 121 |
vmSymbol_t *VM_ValueToFunctionSymbol( vm_t *vm, int value ) { |
| 122 |
vmSymbol_t *sym; |
| 123 |
static vmSymbol_t nullSym; |
| 124 |
|
| 125 |
sym = vm->symbols; |
| 126 |
if ( !sym ) { |
| 127 |
return &nullSym; |
| 128 |
} |
| 129 |
|
| 130 |
while ( sym->next && sym->next->symValue <= value ) { |
| 131 |
sym = sym->next; |
| 132 |
} |
| 133 |
|
| 134 |
return sym; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/* |
| 139 |
=============== |
| 140 |
VM_SymbolToValue |
| 141 |
=============== |
| 142 |
*/ |
| 143 |
int VM_SymbolToValue( vm_t *vm, const char *symbol ) { |
| 144 |
vmSymbol_t *sym; |
| 145 |
|
| 146 |
for ( sym = vm->symbols ; sym ; sym = sym->next ) { |
| 147 |
if ( !strcmp( symbol, sym->symName ) ) { |
| 148 |
return sym->symValue; |
| 149 |
} |
| 150 |
} |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/* |
| 156 |
===================== |
| 157 |
VM_SymbolForCompiledPointer |
| 158 |
===================== |
| 159 |
*/ |
| 160 |
#if 0 // 64bit! |
| 161 |
const char *VM_SymbolForCompiledPointer( vm_t *vm, void *code ) { |
| 162 |
int i; |
| 163 |
|
| 164 |
if ( code < (void *)vm->codeBase ) { |
| 165 |
return "Before code block"; |
| 166 |
} |
| 167 |
if ( code >= (void *)(vm->codeBase + vm->codeLength) ) { |
| 168 |
return "After code block"; |
| 169 |
} |
| 170 |
|
| 171 |
// find which original instruction it is after |
| 172 |
for ( i = 0 ; i < vm->codeLength ; i++ ) { |
| 173 |
if ( (void *)vm->instructionPointers[i] > code ) { |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
i--; |
| 178 |
|
| 179 |
// now look up the bytecode instruction pointer |
| 180 |
return VM_ValueToSymbol( vm, i ); |
| 181 |
} |
| 182 |
#endif |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
/* |
| 187 |
=============== |
| 188 |
ParseHex |
| 189 |
=============== |
| 190 |
*/ |
| 191 |
int ParseHex( const char *text ) { |
| 192 |
int value; |
| 193 |
int c; |
| 194 |
|
| 195 |
value = 0; |
| 196 |
while ( ( c = *text++ ) != 0 ) { |
| 197 |
if ( c >= '0' && c <= '9' ) { |
| 198 |
value = value * 16 + c - '0'; |
| 199 |
continue; |
| 200 |
} |
| 201 |
if ( c >= 'a' && c <= 'f' ) { |
| 202 |
value = value * 16 + 10 + c - 'a'; |
| 203 |
continue; |
| 204 |
} |
| 205 |
if ( c >= 'A' && c <= 'F' ) { |
| 206 |
value = value * 16 + 10 + c - 'A'; |
| 207 |
continue; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return value; |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
=============== |
| 216 |
VM_LoadSymbols |
| 217 |
=============== |
| 218 |
*/ |
| 219 |
void VM_LoadSymbols( vm_t *vm ) { |
| 220 |
union { |
| 221 |
char *c; |
| 222 |
void *v; |
| 223 |
} mapfile; |
| 224 |
char *text_p, *token; |
| 225 |
char name[MAX_QPATH]; |
| 226 |
char symbols[MAX_QPATH]; |
| 227 |
vmSymbol_t **prev, *sym; |
| 228 |
int count; |
| 229 |
int value; |
| 230 |
int chars; |
| 231 |
int segment; |
| 232 |
int numInstructions; |
| 233 |
|
| 234 |
// don't load symbols if not developer |
| 235 |
if ( !com_developer->integer ) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
COM_StripExtension(vm->name, name, sizeof(name)); |
| 240 |
Com_sprintf( symbols, sizeof( symbols ), "vm/%s.map", name ); |
| 241 |
FS_ReadFile( symbols, &mapfile.v ); |
| 242 |
if ( !mapfile.c ) { |
| 243 |
Com_Printf( "Couldn't load symbol file: %s\n", symbols ); |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
numInstructions = vm->instructionCount; |
| 248 |
|
| 249 |
// parse the symbols |
| 250 |
text_p = mapfile.c; |
| 251 |
prev = &vm->symbols; |
| 252 |
count = 0; |
| 253 |
|
| 254 |
while ( 1 ) { |
| 255 |
token = COM_Parse( &text_p ); |
| 256 |
if ( !token[0] ) { |
| 257 |
break; |
| 258 |
} |
| 259 |
segment = ParseHex( token ); |
| 260 |
if ( segment ) { |
| 261 |
COM_Parse( &text_p ); |
| 262 |
COM_Parse( &text_p ); |
| 263 |
continue; // only load code segment values |
| 264 |
} |
| 265 |
|
| 266 |
token = COM_Parse( &text_p ); |
| 267 |
if ( !token[0] ) { |
| 268 |
Com_Printf( "WARNING: incomplete line at end of file\n" ); |
| 269 |
break; |
| 270 |
} |
| 271 |
value = ParseHex( token ); |
| 272 |
|
| 273 |
token = COM_Parse( &text_p ); |
| 274 |
if ( !token[0] ) { |
| 275 |
Com_Printf( "WARNING: incomplete line at end of file\n" ); |
| 276 |
break; |
| 277 |
} |
| 278 |
chars = strlen( token ); |
| 279 |
sym = Hunk_Alloc( sizeof( *sym ) + chars, h_high ); |
| 280 |
*prev = sym; |
| 281 |
prev = &sym->next; |
| 282 |
sym->next = NULL; |
| 283 |
|
| 284 |
// convert value from an instruction number to a code offset |
| 285 |
if ( value >= 0 && value < numInstructions ) { |
| 286 |
value = vm->instructionPointers[value]; |
| 287 |
} |
| 288 |
|
| 289 |
sym->symValue = value; |
| 290 |
Q_strncpyz( sym->symName, token, chars + 1 ); |
| 291 |
|
| 292 |
count++; |
| 293 |
} |
| 294 |
|
| 295 |
vm->numSymbols = count; |
| 296 |
Com_Printf( "%i symbols parsed from %s\n", count, symbols ); |
| 297 |
FS_FreeFile( mapfile.v ); |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
============ |
| 302 |
VM_DllSyscall |
| 303 |
|
| 304 |
Dlls will call this directly |
| 305 |
|
| 306 |
rcg010206 The horror; the horror. |
| 307 |
|
| 308 |
The syscall mechanism relies on stack manipulation to get its args. |
| 309 |
This is likely due to C's inability to pass "..." parameters to |
| 310 |
a function in one clean chunk. On PowerPC Linux, these parameters |
| 311 |
are not necessarily passed on the stack, so while (&arg[0] == arg) |
| 312 |
is true, (&arg[1] == 2nd function parameter) is not necessarily |
| 313 |
accurate, as arg's value might have been stored to the stack or |
| 314 |
other piece of scratch memory to give it a valid address, but the |
| 315 |
next parameter might still be sitting in a register. |
| 316 |
|
| 317 |
Quake's syscall system also assumes that the stack grows downward, |
| 318 |
and that any needed types can be squeezed, safely, into a signed int. |
| 319 |
|
| 320 |
This hack below copies all needed values for an argument to a |
| 321 |
array in memory, so that Quake can get the correct values. This can |
| 322 |
also be used on systems where the stack grows upwards, as the |
| 323 |
presumably standard and safe stdargs.h macros are used. |
| 324 |
|
| 325 |
As for having enough space in a signed int for your datatypes, well, |
| 326 |
it might be better to wait for DOOM 3 before you start porting. :) |
| 327 |
|
| 328 |
The original code, while probably still inherently dangerous, seems |
| 329 |
to work well enough for the platforms it already works on. Rather |
| 330 |
than add the performance hit for those platforms, the original code |
| 331 |
is still in use there. |
| 332 |
|
| 333 |
For speed, we just grab 15 arguments, and don't worry about exactly |
| 334 |
how many the syscall actually needs; the extra is thrown away. |
| 335 |
|
| 336 |
============ |
| 337 |
*/ |
| 338 |
intptr_t QDECL VM_DllSyscall( intptr_t arg, ... ) { |
| 339 |
#if !id386 || defined __clang__ |
| 340 |
// rcg010206 - see commentary above |
| 341 |
intptr_t args[MAX_VMSYSCALL_ARGS]; |
| 342 |
int i; |
| 343 |
va_list ap; |
| 344 |
|
| 345 |
args[0] = arg; |
| 346 |
|
| 347 |
va_start(ap, arg); |
| 348 |
for (i = 1; i < ARRAY_LEN (args); i++) |
| 349 |
args[i] = va_arg(ap, intptr_t); |
| 350 |
va_end(ap); |
| 351 |
|
| 352 |
return currentVM->systemCall( args ); |
| 353 |
#else // original id code |
| 354 |
return currentVM->systemCall( &arg ); |
| 355 |
#endif |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
/* |
| 360 |
================= |
| 361 |
VM_LoadQVM |
| 362 |
|
| 363 |
Load a .qvm file |
| 364 |
================= |
| 365 |
*/ |
| 366 |
vmHeader_t *VM_LoadQVM( vm_t *vm, qboolean alloc, qboolean unpure) |
| 367 |
{ |
| 368 |
int dataLength; |
| 369 |
int i; |
| 370 |
char filename[MAX_QPATH]; |
| 371 |
union { |
| 372 |
vmHeader_t *h; |
| 373 |
void *v; |
| 374 |
} header; |
| 375 |
|
| 376 |
// load the image |
| 377 |
Com_sprintf( filename, sizeof(filename), "vm/%s.qvm", vm->name ); |
| 378 |
Com_Printf( "Loading vm file %s...\n", filename ); |
| 379 |
|
| 380 |
FS_ReadFileDir(filename, vm->searchPath, unpure, &header.v); |
| 381 |
|
| 382 |
if ( !header.h ) { |
| 383 |
Com_Printf( "Failed.\n" ); |
| 384 |
VM_Free( vm ); |
| 385 |
|
| 386 |
Com_Printf(S_COLOR_YELLOW "Warning: Couldn't open VM file %s\n", filename); |
| 387 |
|
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
// show where the qvm was loaded from |
| 392 |
FS_Which(filename, vm->searchPath); |
| 393 |
|
| 394 |
if( LittleLong( header.h->vmMagic ) == VM_MAGIC_VER2 ) { |
| 395 |
Com_Printf( "...which has vmMagic VM_MAGIC_VER2\n" ); |
| 396 |
|
| 397 |
// byte swap the header |
| 398 |
for ( i = 0 ; i < sizeof( vmHeader_t ) / 4 ; i++ ) { |
| 399 |
((int *)header.h)[i] = LittleLong( ((int *)header.h)[i] ); |
| 400 |
} |
| 401 |
|
| 402 |
// validate |
| 403 |
if ( header.h->jtrgLength < 0 |
| 404 |
|| header.h->bssLength < 0 |
| 405 |
|| header.h->dataLength < 0 |
| 406 |
|| header.h->litLength < 0 |
| 407 |
|| header.h->codeLength <= 0 ) |
| 408 |
{ |
| 409 |
VM_Free(vm); |
| 410 |
FS_FreeFile(header.v); |
| 411 |
|
| 412 |
Com_Printf(S_COLOR_YELLOW "Warning: %s has bad header\n", filename); |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
} else if( LittleLong( header.h->vmMagic ) == VM_MAGIC ) { |
| 416 |
// byte swap the header |
| 417 |
// sizeof( vmHeader_t ) - sizeof( int ) is the 1.32b vm header size |
| 418 |
for ( i = 0 ; i < ( sizeof( vmHeader_t ) - sizeof( int ) ) / 4 ; i++ ) { |
| 419 |
((int *)header.h)[i] = LittleLong( ((int *)header.h)[i] ); |
| 420 |
} |
| 421 |
|
| 422 |
// validate |
| 423 |
if ( header.h->bssLength < 0 |
| 424 |
|| header.h->dataLength < 0 |
| 425 |
|| header.h->litLength < 0 |
| 426 |
|| header.h->codeLength <= 0 ) |
| 427 |
{ |
| 428 |
VM_Free(vm); |
| 429 |
FS_FreeFile(header.v); |
| 430 |
|
| 431 |
Com_Printf(S_COLOR_YELLOW "Warning: %s has bad header\n", filename); |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
} else { |
| 435 |
VM_Free( vm ); |
| 436 |
FS_FreeFile(header.v); |
| 437 |
|
| 438 |
Com_Printf(S_COLOR_YELLOW "Warning: %s does not have a recognisable " |
| 439 |
"magic number in its header\n", filename); |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
|
| 443 |
// round up to next power of 2 so all data operations can |
| 444 |
// be mask protected |
| 445 |
dataLength = header.h->dataLength + header.h->litLength + |
| 446 |
header.h->bssLength; |
| 447 |
for ( i = 0 ; dataLength > ( 1 << i ) ; i++ ) { |
| 448 |
} |
| 449 |
dataLength = 1 << i; |
| 450 |
|
| 451 |
if(alloc) |
| 452 |
{ |
| 453 |
// allocate zero filled space for initialized and uninitialized data |
| 454 |
// leave some space beyond data mask so we can secure all mask operations |
| 455 |
vm->dataAlloc = dataLength + 4; |
| 456 |
vm->dataBase = Hunk_Alloc(vm->dataAlloc, h_high); |
| 457 |
vm->dataMask = dataLength - 1; |
| 458 |
} |
| 459 |
else |
| 460 |
{ |
| 461 |
// clear the data, but make sure we're not clearing more than allocated |
| 462 |
if(vm->dataAlloc != dataLength + 4) |
| 463 |
{ |
| 464 |
VM_Free(vm); |
| 465 |
FS_FreeFile(header.v); |
| 466 |
|
| 467 |
Com_Printf(S_COLOR_YELLOW "Warning: Data region size of %s not matching after " |
| 468 |
"VM_Restart()\n", filename); |
| 469 |
return NULL; |
| 470 |
} |
| 471 |
|
| 472 |
Com_Memset(vm->dataBase, 0, vm->dataAlloc); |
| 473 |
} |
| 474 |
|
| 475 |
// copy the intialized data |
| 476 |
Com_Memcpy( vm->dataBase, (byte *)header.h + header.h->dataOffset, |
| 477 |
header.h->dataLength + header.h->litLength ); |
| 478 |
|
| 479 |
// byte swap the longs |
| 480 |
for ( i = 0 ; i < header.h->dataLength ; i += 4 ) { |
| 481 |
*(int *)(vm->dataBase + i) = LittleLong( *(int *)(vm->dataBase + i ) ); |
| 482 |
} |
| 483 |
|
| 484 |
if(header.h->vmMagic == VM_MAGIC_VER2) |
| 485 |
{ |
| 486 |
int previousNumJumpTableTargets = vm->numJumpTableTargets; |
| 487 |
|
| 488 |
header.h->jtrgLength &= ~0x03; |
| 489 |
|
| 490 |
vm->numJumpTableTargets = header.h->jtrgLength >> 2; |
| 491 |
Com_Printf("Loading %d jump table targets\n", vm->numJumpTableTargets); |
| 492 |
|
| 493 |
if(alloc) |
| 494 |
{ |
| 495 |
vm->jumpTableTargets = Hunk_Alloc(header.h->jtrgLength, h_high); |
| 496 |
} |
| 497 |
else |
| 498 |
{ |
| 499 |
if(vm->numJumpTableTargets != previousNumJumpTableTargets) |
| 500 |
{ |
| 501 |
VM_Free(vm); |
| 502 |
FS_FreeFile(header.v); |
| 503 |
|
| 504 |
Com_Printf(S_COLOR_YELLOW "Warning: Jump table size of %s not matching after " |
| 505 |
"VM_Restart()\n", filename); |
| 506 |
return NULL; |
| 507 |
} |
| 508 |
|
| 509 |
Com_Memset(vm->jumpTableTargets, 0, header.h->jtrgLength); |
| 510 |
} |
| 511 |
|
| 512 |
Com_Memcpy(vm->jumpTableTargets, (byte *) header.h + header.h->dataOffset + |
| 513 |
header.h->dataLength + header.h->litLength, header.h->jtrgLength); |
| 514 |
|
| 515 |
// byte swap the longs |
| 516 |
for ( i = 0 ; i < header.h->jtrgLength ; i += 4 ) { |
| 517 |
*(int *)(vm->jumpTableTargets + i) = LittleLong( *(int *)(vm->jumpTableTargets + i ) ); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
return header.h; |
| 522 |
} |
| 523 |
|
| 524 |
/* |
| 525 |
================= |
| 526 |
VM_Restart |
| 527 |
|
| 528 |
Reload the data, but leave everything else in place |
| 529 |
This allows a server to do a map_restart without changing memory allocation |
| 530 |
|
| 531 |
We need to make sure that servers can access unpure QVMs (not contained in any pak) |
| 532 |
even if the client is pure, so take "unpure" as argument. |
| 533 |
================= |
| 534 |
*/ |
| 535 |
vm_t *VM_Restart(vm_t *vm, qboolean unpure) |
| 536 |
{ |
| 537 |
vmHeader_t *header; |
| 538 |
|
| 539 |
// DLL's can't be restarted in place |
| 540 |
if ( vm->dllHandle ) { |
| 541 |
char name[MAX_QPATH]; |
| 542 |
intptr_t (*systemCall)( intptr_t *parms ); |
| 543 |
|
| 544 |
systemCall = vm->systemCall; |
| 545 |
Q_strncpyz( name, vm->name, sizeof( name ) ); |
| 546 |
|
| 547 |
VM_Free( vm ); |
| 548 |
|
| 549 |
vm = VM_Create( name, systemCall, VMI_NATIVE ); |
| 550 |
return vm; |
| 551 |
} |
| 552 |
|
| 553 |
// load the image |
| 554 |
Com_Printf("VM_Restart()\n"); |
| 555 |
|
| 556 |
if(!(header = VM_LoadQVM(vm, qfalse, unpure))) |
| 557 |
{ |
| 558 |
Com_Error(ERR_DROP, "VM_Restart failed"); |
| 559 |
return NULL; |
| 560 |
} |
| 561 |
|
| 562 |
// free the original file |
| 563 |
FS_FreeFile(header); |
| 564 |
|
| 565 |
return vm; |
| 566 |
} |
| 567 |
|
| 568 |
/* |
| 569 |
================ |
| 570 |
VM_Create |
| 571 |
|
| 572 |
If image ends in .qvm it will be interpreted, otherwise |
| 573 |
it will attempt to load as a system dll |
| 574 |
================ |
| 575 |
*/ |
| 576 |
vm_t *VM_Create( const char *module, intptr_t (*systemCalls)(intptr_t *), |
| 577 |
vmInterpret_t interpret ) { |
| 578 |
vm_t *vm; |
| 579 |
vmHeader_t *header = NULL; |
| 580 |
int i, remaining, retval; |
| 581 |
char filename[MAX_OSPATH]; |
| 582 |
void *startSearch = NULL; |
| 583 |
|
| 584 |
if ( !module || !module[0] || !systemCalls ) { |
| 585 |
Com_Error( ERR_FATAL, "VM_Create: bad parms" ); |
| 586 |
} |
| 587 |
|
| 588 |
remaining = Hunk_MemoryRemaining(); |
| 589 |
|
| 590 |
// see if we already have the VM |
| 591 |
for ( i = 0 ; i < MAX_VM ; i++ ) { |
| 592 |
if (!Q_stricmp(vmTable[i].name, module)) { |
| 593 |
vm = &vmTable[i]; |
| 594 |
return vm; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
// find a free vm |
| 599 |
for ( i = 0 ; i < MAX_VM ; i++ ) { |
| 600 |
if ( !vmTable[i].name[0] ) { |
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
if ( i == MAX_VM ) { |
| 606 |
Com_Error( ERR_FATAL, "VM_Create: no free vm_t" ); |
| 607 |
} |
| 608 |
|
| 609 |
vm = &vmTable[i]; |
| 610 |
|
| 611 |
Q_strncpyz(vm->name, module, sizeof(vm->name)); |
| 612 |
|
| 613 |
do |
| 614 |
{ |
| 615 |
retval = FS_FindVM(&startSearch, filename, sizeof(filename), module, (interpret == VMI_NATIVE)); |
| 616 |
|
| 617 |
if(retval == VMI_NATIVE) |
| 618 |
{ |
| 619 |
Com_Printf("Try loading dll file %s\n", filename); |
| 620 |
|
| 621 |
vm->dllHandle = Sys_LoadGameDll(filename, &vm->entryPoint, VM_DllSyscall); |
| 622 |
|
| 623 |
if(vm->dllHandle) |
| 624 |
{ |
| 625 |
vm->systemCall = systemCalls; |
| 626 |
return vm; |
| 627 |
} |
| 628 |
|
| 629 |
Com_Printf("Failed loading dll, trying next\n"); |
| 630 |
} |
| 631 |
else if(retval == VMI_COMPILED) |
| 632 |
{ |
| 633 |
vm->searchPath = startSearch; |
| 634 |
if((header = VM_LoadQVM(vm, qtrue, qfalse))) |
| 635 |
break; |
| 636 |
|
| 637 |
// VM_Free overwrites the name on failed load |
| 638 |
Q_strncpyz(vm->name, module, sizeof(vm->name)); |
| 639 |
} |
| 640 |
} while(retval >= 0); |
| 641 |
|
| 642 |
if(retval < 0) |
| 643 |
return NULL; |
| 644 |
|
| 645 |
vm->systemCall = systemCalls; |
| 646 |
|
| 647 |
// allocate space for the jump targets, which will be filled in by the compile/prep functions |
| 648 |
vm->instructionCount = header->instructionCount; |
| 649 |
vm->instructionPointers = Hunk_Alloc(vm->instructionCount * sizeof(*vm->instructionPointers), h_high); |
| 650 |
|
| 651 |
// copy or compile the instructions |
| 652 |
vm->codeLength = header->codeLength; |
| 653 |
|
| 654 |
vm->compiled = qfalse; |
| 655 |
|
| 656 |
#ifndef HAVE_VM_COMPILED |
| 657 |
if(interpret >= VMI_COMPILED) { |
| 658 |
Com_Printf("Architecture doesn't have a bytecode compiler, using interpreter\n"); |
| 659 |
interpret = VMI_BYTECODE; |
| 660 |
} |
| 661 |
#else |
| 662 |
if(interpret != VMI_BYTECODE) |
| 663 |
{ |
| 664 |
vm->compiled = qtrue; |
| 665 |
VM_Compile( vm, header ); |
| 666 |
} |
| 667 |
#endif |
| 668 |
// VM_Compile may have reset vm->compiled if compilation failed |
| 669 |
if (!vm->compiled) |
| 670 |
{ |
| 671 |
VM_PrepareInterpreter( vm, header ); |
| 672 |
} |
| 673 |
|
| 674 |
// free the original file |
| 675 |
FS_FreeFile( header ); |
| 676 |
|
| 677 |
// load the map file |
| 678 |
VM_LoadSymbols( vm ); |
| 679 |
|
| 680 |
// the stack is implicitly at the end of the image |
| 681 |
vm->programStack = vm->dataMask + 1; |
| 682 |
vm->stackBottom = vm->programStack - PROGRAM_STACK_SIZE; |
| 683 |
|
| 684 |
Com_Printf("%s loaded in %d bytes on the hunk\n", module, remaining - Hunk_MemoryRemaining()); |
| 685 |
|
| 686 |
return vm; |
| 687 |
} |
| 688 |
|
| 689 |
/* |
| 690 |
============== |
| 691 |
VM_Free |
| 692 |
============== |
| 693 |
*/ |
| 694 |
void VM_Free( vm_t *vm ) { |
| 695 |
|
| 696 |
if(!vm) { |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
if(vm->callLevel) { |
| 701 |
if(!forced_unload) { |
| 702 |
Com_Error( ERR_FATAL, "VM_Free(%s) on running vm", vm->name ); |
| 703 |
return; |
| 704 |
} else { |
| 705 |
Com_Printf( "forcefully unloading %s vm\n", vm->name ); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
if(vm->destroy) |
| 710 |
vm->destroy(vm); |
| 711 |
|
| 712 |
if ( vm->dllHandle ) { |
| 713 |
Sys_UnloadDll( vm->dllHandle ); |
| 714 |
Com_Memset( vm, 0, sizeof( *vm ) ); |
| 715 |
} |
| 716 |
#if 0 // now automatically freed by hunk |
| 717 |
if ( vm->codeBase ) { |
| 718 |
Z_Free( vm->codeBase ); |
| 719 |
} |
| 720 |
if ( vm->dataBase ) { |
| 721 |
Z_Free( vm->dataBase ); |
| 722 |
} |
| 723 |
if ( vm->instructionPointers ) { |
| 724 |
Z_Free( vm->instructionPointers ); |
| 725 |
} |
| 726 |
#endif |
| 727 |
Com_Memset( vm, 0, sizeof( *vm ) ); |
| 728 |
|
| 729 |
currentVM = NULL; |
| 730 |
lastVM = NULL; |
| 731 |
} |
| 732 |
|
| 733 |
void VM_Clear(void) { |
| 734 |
int i; |
| 735 |
for (i=0;i<MAX_VM; i++) { |
| 736 |
VM_Free(&vmTable[i]); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
void VM_Forced_Unload_Start(void) { |
| 741 |
forced_unload = 1; |
| 742 |
} |
| 743 |
|
| 744 |
void VM_Forced_Unload_Done(void) { |
| 745 |
forced_unload = 0; |
| 746 |
} |
| 747 |
|
| 748 |
void *VM_ArgPtr( intptr_t intValue ) { |
| 749 |
if ( !intValue ) { |
| 750 |
return NULL; |
| 751 |
} |
| 752 |
// currentVM is missing on reconnect |
| 753 |
if ( currentVM==NULL ) |
| 754 |
return NULL; |
| 755 |
|
| 756 |
if ( currentVM->entryPoint ) { |
| 757 |
return (void *)(currentVM->dataBase + intValue); |
| 758 |
} |
| 759 |
else { |
| 760 |
return (void *)(currentVM->dataBase + (intValue & currentVM->dataMask)); |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
void *VM_ExplicitArgPtr( vm_t *vm, intptr_t intValue ) { |
| 765 |
if ( !intValue ) { |
| 766 |
return NULL; |
| 767 |
} |
| 768 |
|
| 769 |
// currentVM is missing on reconnect here as well? |
| 770 |
if ( currentVM==NULL ) |
| 771 |
return NULL; |
| 772 |
|
| 773 |
// |
| 774 |
if ( vm->entryPoint ) { |
| 775 |
return (void *)(vm->dataBase + intValue); |
| 776 |
} |
| 777 |
else { |
| 778 |
return (void *)(vm->dataBase + (intValue & vm->dataMask)); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
/* |
| 784 |
============== |
| 785 |
VM_Call |
| 786 |
|
| 787 |
|
| 788 |
Upon a system call, the stack will look like: |
| 789 |
|
| 790 |
sp+32 parm1 |
| 791 |
sp+28 parm0 |
| 792 |
sp+24 return value |
| 793 |
sp+20 return address |
| 794 |
sp+16 local1 |
| 795 |
sp+14 local0 |
| 796 |
sp+12 arg1 |
| 797 |
sp+8 arg0 |
| 798 |
sp+4 return stack |
| 799 |
sp return address |
| 800 |
|
| 801 |
An interpreted function will immediately execute |
| 802 |
an OP_ENTER instruction, which will subtract space for |
| 803 |
locals from sp |
| 804 |
============== |
| 805 |
*/ |
| 806 |
|
| 807 |
intptr_t QDECL VM_Call( vm_t *vm, int callnum, ... ) |
| 808 |
{ |
| 809 |
vm_t *oldVM; |
| 810 |
intptr_t r; |
| 811 |
int i; |
| 812 |
|
| 813 |
if(!vm || !vm->name[0]) |
| 814 |
Com_Error(ERR_FATAL, "VM_Call with NULL vm"); |
| 815 |
|
| 816 |
oldVM = currentVM; |
| 817 |
currentVM = vm; |
| 818 |
lastVM = vm; |
| 819 |
|
| 820 |
if ( vm_debugLevel ) { |
| 821 |
Com_Printf( "VM_Call( %d )\n", callnum ); |
| 822 |
} |
| 823 |
|
| 824 |
++vm->callLevel; |
| 825 |
// if we have a dll loaded, call it directly |
| 826 |
if ( vm->entryPoint ) { |
| 827 |
//rcg010207 - see dissertation at top of VM_DllSyscall() in this file. |
| 828 |
int args[MAX_VMMAIN_ARGS-1]; |
| 829 |
va_list ap; |
| 830 |
va_start(ap, callnum); |
| 831 |
for (i = 0; i < ARRAY_LEN(args); i++) { |
| 832 |
args[i] = va_arg(ap, int); |
| 833 |
} |
| 834 |
va_end(ap); |
| 835 |
|
| 836 |
r = vm->entryPoint( callnum, args[0], args[1], args[2], args[3], |
| 837 |
args[4], args[5], args[6], args[7], |
| 838 |
args[8], args[9], args[10], args[11]); |
| 839 |
} else { |
| 840 |
#if ( id386 || idsparc ) && !defined __clang__ // calling convention doesn't need conversion in some cases |
| 841 |
#ifdef HAVE_VM_COMPILED |
| 842 |
if ( vm->compiled ) |
| 843 |
r = VM_CallCompiled( vm, (int*)&callnum ); |
| 844 |
else |
| 845 |
#endif |
| 846 |
r = VM_CallInterpreted( vm, (int*)&callnum ); |
| 847 |
#else |
| 848 |
struct { |
| 849 |
int callnum; |
| 850 |
int args[MAX_VMMAIN_ARGS-1]; |
| 851 |
} a; |
| 852 |
va_list ap; |
| 853 |
|
| 854 |
a.callnum = callnum; |
| 855 |
va_start(ap, callnum); |
| 856 |
for (i = 0; i < ARRAY_LEN(a.args); i++) { |
| 857 |
a.args[i] = va_arg(ap, int); |
| 858 |
} |
| 859 |
va_end(ap); |
| 860 |
#ifdef HAVE_VM_COMPILED |
| 861 |
if ( vm->compiled ) |
| 862 |
r = VM_CallCompiled( vm, &a.callnum ); |
| 863 |
else |
| 864 |
#endif |
| 865 |
r = VM_CallInterpreted( vm, &a.callnum ); |
| 866 |
#endif |
| 867 |
} |
| 868 |
--vm->callLevel; |
| 869 |
|
| 870 |
if ( oldVM != NULL ) |
| 871 |
currentVM = oldVM; |
| 872 |
return r; |
| 873 |
} |
| 874 |
|
| 875 |
//================================================================= |
| 876 |
|
| 877 |
static int QDECL VM_ProfileSort( const void *a, const void *b ) { |
| 878 |
vmSymbol_t *sa, *sb; |
| 879 |
|
| 880 |
sa = *(vmSymbol_t **)a; |
| 881 |
sb = *(vmSymbol_t **)b; |
| 882 |
|
| 883 |
if ( sa->profileCount < sb->profileCount ) { |
| 884 |
return -1; |
| 885 |
} |
| 886 |
if ( sa->profileCount > sb->profileCount ) { |
| 887 |
return 1; |
| 888 |
} |
| 889 |
return 0; |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
============== |
| 894 |
VM_VmProfile_f |
| 895 |
|
| 896 |
============== |
| 897 |
*/ |
| 898 |
void VM_VmProfile_f( void ) { |
| 899 |
vm_t *vm; |
| 900 |
vmSymbol_t **sorted, *sym; |
| 901 |
int i; |
| 902 |
double total; |
| 903 |
|
| 904 |
if ( !lastVM ) { |
| 905 |
return; |
| 906 |
} |
| 907 |
|
| 908 |
vm = lastVM; |
| 909 |
|
| 910 |
if ( !vm->numSymbols ) { |
| 911 |
return; |
| 912 |
} |
| 913 |
|
| 914 |
sorted = Z_Malloc( vm->numSymbols * sizeof( *sorted ) ); |
| 915 |
sorted[0] = vm->symbols; |
| 916 |
total = sorted[0]->profileCount; |
| 917 |
for ( i = 1 ; i < vm->numSymbols ; i++ ) { |
| 918 |
sorted[i] = sorted[i-1]->next; |
| 919 |
total += sorted[i]->profileCount; |
| 920 |
} |
| 921 |
|
| 922 |
qsort( sorted, vm->numSymbols, sizeof( *sorted ), VM_ProfileSort ); |
| 923 |
|
| 924 |
for ( i = 0 ; i < vm->numSymbols ; i++ ) { |
| 925 |
int perc; |
| 926 |
|
| 927 |
sym = sorted[i]; |
| 928 |
|
| 929 |
perc = 100 * (float) sym->profileCount / total; |
| 930 |
Com_Printf( "%2i%% %9i %s\n", perc, sym->profileCount, sym->symName ); |
| 931 |
sym->profileCount = 0; |
| 932 |
} |
| 933 |
|
| 934 |
Com_Printf(" %9.0f total\n", total ); |
| 935 |
|
| 936 |
Z_Free( sorted ); |
| 937 |
} |
| 938 |
|
| 939 |
/* |
| 940 |
============== |
| 941 |
VM_VmInfo_f |
| 942 |
|
| 943 |
============== |
| 944 |
*/ |
| 945 |
void VM_VmInfo_f( void ) { |
| 946 |
vm_t *vm; |
| 947 |
int i; |
| 948 |
|
| 949 |
Com_Printf( "Registered virtual machines:\n" ); |
| 950 |
for ( i = 0 ; i < MAX_VM ; i++ ) { |
| 951 |
vm = &vmTable[i]; |
| 952 |
if ( !vm->name[0] ) { |
| 953 |
break; |
| 954 |
} |
| 955 |
Com_Printf( "%s : ", vm->name ); |
| 956 |
if ( vm->dllHandle ) { |
| 957 |
Com_Printf( "native\n" ); |
| 958 |
continue; |
| 959 |
} |
| 960 |
if ( vm->compiled ) { |
| 961 |
Com_Printf( "compiled on load\n" ); |
| 962 |
} else { |
| 963 |
Com_Printf( "interpreted\n" ); |
| 964 |
} |
| 965 |
Com_Printf( " code length : %7i\n", vm->codeLength ); |
| 966 |
Com_Printf( " table length: %7i\n", vm->instructionCount*4 ); |
| 967 |
Com_Printf( " data length : %7i\n", vm->dataMask + 1 ); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
/* |
| 972 |
=============== |
| 973 |
VM_LogSyscalls |
| 974 |
|
| 975 |
Insert calls to this while debugging the vm compiler |
| 976 |
=============== |
| 977 |
*/ |
| 978 |
void VM_LogSyscalls( int *args ) { |
| 979 |
static int callnum; |
| 980 |
static FILE *f; |
| 981 |
|
| 982 |
if ( !f ) { |
| 983 |
f = fopen("syscalls.log", "w" ); |
| 984 |
} |
| 985 |
callnum++; |
| 986 |
fprintf(f, "%i: %p (%i) = %i %i %i %i\n", callnum, (void*)(args - (int *)currentVM->dataBase), |
| 987 |
args[0], args[1], args[2], args[3], args[4] ); |
| 988 |
} |
| 989 |
|
| 990 |
/* |
| 991 |
================= |
| 992 |
VM_BlockCopy |
| 993 |
Executes a block copy operation within currentVM data space |
| 994 |
================= |
| 995 |
*/ |
| 996 |
|
| 997 |
void VM_BlockCopy(unsigned int dest, unsigned int src, size_t n) |
| 998 |
{ |
| 999 |
unsigned int dataMask = currentVM->dataMask; |
| 1000 |
|
| 1001 |
if ((dest & dataMask) != dest |
| 1002 |
|| (src & dataMask) != src |
| 1003 |
|| ((dest + n) & dataMask) != dest + n |
| 1004 |
|| ((src + n) & dataMask) != src + n) |
| 1005 |
{ |
| 1006 |
Com_Error(ERR_DROP, "OP_BLOCK_COPY out of range!"); |
| 1007 |
} |
| 1008 |
|
| 1009 |
Com_Memcpy(currentVM->dataBase + dest, currentVM->dataBase + src, n); |
| 1010 |
} |