Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 411 lines (346 sloc) 9.458 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
#include "master.hpp"

namespace factor
{

HMODULE hFactorDll;

void factor_vm::init_ffi()
{
hFactorDll = GetModuleHandle(FACTOR_DLL);
if(!hFactorDll)
fatal_error("GetModuleHandle() failed", 0);
}

void factor_vm::ffi_dlopen(dll *dll)
{
dll->handle = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0);
}

void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
{
return (void *)GetProcAddress(dll ? (HMODULE)dll->handle : hFactorDll, symbol);
}

void *factor_vm::ffi_dlsym_raw(dll *dll, symbol_char *symbol)
{
return ffi_dlsym(dll, symbol);
}

void factor_vm::ffi_dlclose(dll *dll)
{
FreeLibrary((HMODULE)dll->handle);
dll->handle = NULL;
}

BOOL factor_vm::windows_stat(vm_char *path)
{
BY_HANDLE_FILE_INFORMATION bhfi;
HANDLE h = CreateFileW(path,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

if(h == INVALID_HANDLE_VALUE)
{
// FindFirstFile is the only call that can stat c:\pagefile.sys
WIN32_FIND_DATA st;
HANDLE h;

if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
return false;
FindClose(h);
return true;
}
BOOL ret = GetFileInformationByHandle(h, &bhfi);
CloseHandle(h);
return ret;
}

void factor_vm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length)
{
wcsncpy(temp_path, full_path, length - 1);
size_t full_path_len = wcslen(full_path);
if (full_path_len < length - 1)
wcsncat(temp_path, L".image", length - full_path_len - 1);
temp_path[length - 1] = 0;
}

/* You must free() this yourself. */
const vm_char *factor_vm::default_image_path()
{
vm_char full_path[MAX_UNICODE_PATH];
vm_char *ptr;
vm_char temp_path[MAX_UNICODE_PATH];

if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
fatal_error("GetModuleFileName() failed", 0);

if((ptr = wcsrchr(full_path, '.')))
*ptr = 0;

wcsncpy(temp_path, full_path, MAX_UNICODE_PATH - 1);
size_t full_path_len = wcslen(full_path);
if (full_path_len < MAX_UNICODE_PATH - 1)
wcsncat(temp_path, L".image", MAX_UNICODE_PATH - full_path_len - 1);
temp_path[MAX_UNICODE_PATH - 1] = 0;

return safe_strdup(temp_path);
}

/* You must free() this yourself. */
const vm_char *factor_vm::vm_executable_path()
{
vm_char full_path[MAX_UNICODE_PATH];
if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
fatal_error("GetModuleFileName() failed", 0);
return safe_strdup(full_path);
}

void factor_vm::primitive_existsp()
{
vm_char *path = untag_check<byte_array>(ctx->pop())->data<vm_char>();
ctx->push(tag_boolean(windows_stat(path)));
}

segment::segment(cell size_, bool executable_p)
{
size = size_;

char *mem;
DWORD ignore;

if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
MEM_COMMIT, executable_p ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE)) == 0)
out_of_memory();

if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
fatal_error("Cannot allocate low guard page", (cell)mem);

if (!VirtualProtect(mem + size + getpagesize(),
getpagesize(), PAGE_NOACCESS, &ignore))
fatal_error("Cannot allocate high guard page", (cell)mem);

start = (cell)mem + getpagesize();
end = start + size;
}

segment::~segment()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
if(!VirtualFree((void*)(start - si.dwPageSize), 0, MEM_RELEASE))
fatal_error("Segment deallocation failed",0);
}

long getpagesize()
{
static long g_pagesize = 0;
if(!g_pagesize)
{
SYSTEM_INFO system_info;
GetSystemInfo (&system_info);
g_pagesize = system_info.dwPageSize;
}
return g_pagesize;
}

void code_heap::guard_safepoint()
{
DWORD ignore;
if (!VirtualProtect(safepoint_page, getpagesize(), PAGE_NOACCESS, &ignore))
fatal_error("Cannot protect safepoint guard page", (cell)safepoint_page);
}

void code_heap::unguard_safepoint()
{
DWORD ignore;
if (!VirtualProtect(safepoint_page, getpagesize(), PAGE_READWRITE, &ignore))
fatal_error("Cannot unprotect safepoint guard page", (cell)safepoint_page);
}

void factor_vm::move_file(const vm_char *path1, const vm_char *path2)
{
if(MoveFileEx((path1),(path2),MOVEFILE_REPLACE_EXISTING) == false)
general_error(ERROR_IO,tag_fixnum(GetLastError()),false_object);
}

void factor_vm::init_signals() {}

THREADHANDLE start_thread(void *(*start_routine)(void *), void *args)
{
return (void *)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0);
}

u64 nano_count()
{
static double scale_factor;

static u32 hi = 0;
static u32 lo = 0;

LARGE_INTEGER count;
BOOL ret = QueryPerformanceCounter(&count);
if(ret == 0)
fatal_error("QueryPerformanceCounter", 0);

if(scale_factor == 0.0)
{
LARGE_INTEGER frequency;
BOOL ret = QueryPerformanceFrequency(&frequency);
if(ret == 0)
fatal_error("QueryPerformanceFrequency", 0);
scale_factor = (1000000000.0 / frequency.QuadPart);
}

#ifdef FACTOR_64
hi = count.HighPart;
#else
/* On VirtualBox, QueryPerformanceCounter does not increment
the high part every time the low part overflows. Workaround. */
if(lo > count.LowPart)
hi++;
#endif
lo = count.LowPart;

return (u64)((((u64)hi << 32) | (u64)lo) * scale_factor);
}

void sleep_nanos(u64 nsec)
{
Sleep((DWORD)(nsec/1000000));
}

typedef enum _EXCEPTION_DISPOSITION
{
ExceptionContinueExecution = 0,
ExceptionContinueSearch = 1,
ExceptionNestedException = 2,
ExceptionCollidedUnwind = 3
} EXCEPTION_DISPOSITION;

LONG factor_vm::exception_handler(PEXCEPTION_RECORD e, void *frame, PCONTEXT c, void *dispatch)
{
switch (e->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
signal_fault_addr = e->ExceptionInformation[1];
verify_memory_protection_error(signal_fault_addr);
dispatch_signal_handler(
(cell*)&c->ESP,
(cell*)&c->EIP,
(cell)factor::memory_signal_handler_impl
);
break;

case STATUS_FLOAT_DENORMAL_OPERAND:
case STATUS_FLOAT_DIVIDE_BY_ZERO:
case STATUS_FLOAT_INEXACT_RESULT:
case STATUS_FLOAT_INVALID_OPERATION:
case STATUS_FLOAT_OVERFLOW:
case STATUS_FLOAT_STACK_CHECK:
case STATUS_FLOAT_UNDERFLOW:
case STATUS_FLOAT_MULTIPLE_FAULTS:
case STATUS_FLOAT_MULTIPLE_TRAPS:
#ifdef FACTOR_64
signal_fpu_status = fpu_status(MXCSR(c));
#else
signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c));

/* This seems to have no effect */
X87SW(c) = 0;
#endif
MXCSR(c) &= 0xffffffc0;
dispatch_signal_handler(
(cell*)&c->ESP,
(cell*)&c->EIP,
(cell)factor::fp_signal_handler_impl
);
break;
default:
signal_number = e->ExceptionCode;
dispatch_signal_handler(
(cell*)&c->ESP,
(cell*)&c->EIP,
(cell)factor::synchronous_signal_handler_impl
);
break;
}

return ExceptionContinueExecution;
}

VM_C_API LONG exception_handler(PEXCEPTION_RECORD e, void *frame, PCONTEXT c, void *dispatch)
{
if (factor_vm::fatal_erroring_p)
return ExceptionContinueSearch;

factor_vm *vm = current_vm_p();
if (vm)
return vm->exception_handler(e,frame,c,dispatch);
else
return ExceptionContinueSearch;
}

static BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
{
switch (dwCtrlType) {
case CTRL_C_EVENT:
{
/* The CtrlHandler runs in its own thread without stopping the main thread.
Since in practice nobody uses the multi-VM stuff yet, we just grab the first
VM we can get. This will not be a good idea when we actually support native
threads. */
FACTOR_ASSERT(thread_vms.size() == 1);
factor_vm *vm = thread_vms.begin()->second;
vm->safepoint.enqueue_fep(vm);
return TRUE;
}
default:
return FALSE;
}
}

void factor_vm::open_console()
{
handle_ctrl_c();
}

void factor_vm::ignore_ctrl_c()
{
SetConsoleCtrlHandler(factor::ctrl_handler, FALSE);
}

void factor_vm::handle_ctrl_c()
{
SetConsoleCtrlHandler(factor::ctrl_handler, TRUE);
}

void factor_vm::lock_console()
{
}

void factor_vm::unlock_console()
{
}

void factor_vm::close_console()
{
}

void factor_vm::sampler_thread_loop()
{
LARGE_INTEGER counter, new_counter, units_per_second;
DWORD ok;

ok = QueryPerformanceFrequency(&units_per_second);
FACTOR_ASSERT(ok);

ok = QueryPerformanceCounter(&counter);
FACTOR_ASSERT(ok);

counter.QuadPart *= samples_per_second;
while (atomic::load(&sampling_profiler_p))
{
SwitchToThread();
ok = QueryPerformanceCounter(&new_counter);
FACTOR_ASSERT(ok);
new_counter.QuadPart *= samples_per_second;
cell samples = 0;
while (new_counter.QuadPart - counter.QuadPart > units_per_second.QuadPart)
{
++samples;
counter.QuadPart += units_per_second.QuadPart;
}

if (samples > 0)
{
DWORD suscount = SuspendThread(thread);
FACTOR_ASSERT(suscount == 0);

CONTEXT context;
memset((void*)&context, 0, sizeof(CONTEXT));
context.ContextFlags = CONTEXT_CONTROL;
BOOL context_ok = GetThreadContext(thread, &context);
FACTOR_ASSERT(context_ok);

suscount = ResumeThread(thread);
FACTOR_ASSERT(suscount == 1);

safepoint.enqueue_samples(this, samples, context.EIP, false);
}
}
}

static DWORD WINAPI sampler_thread_entry(LPVOID parent_vm)
{
static_cast<factor_vm*>(parent_vm)->sampler_thread_loop();
return 0;
}

void factor_vm::start_sampling_profiler_timer()
{
sampler_thread = CreateThread(
NULL,
0,
&sampler_thread_entry,
static_cast<LPVOID>(this),
0,
NULL
);
}

void factor_vm::end_sampling_profiler_timer()
{
atomic::store(&sampling_profiler_p, false);
DWORD wait_result = WaitForSingleObject(sampler_thread,
3000*(DWORD)samples_per_second);
if (wait_result != WAIT_OBJECT_0)
TerminateThread(sampler_thread, 0);
sampler_thread = NULL;
}

void abort()
{
::abort();
}

}
Something went wrong with that request. Please try again.