|
| 1 | +/* |
| 2 | + * Windows.cpp |
| 3 | + * Original Gull code is in the "public domain". |
| 4 | + * New code: Copyright (c) 2015 the copyright holders |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | + * copy of this software and associated documentation files (the "Software"), |
| 8 | + * to deal in the Software without restriction, including without limitation |
| 9 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | + * Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | + * DEALINGS IN THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <setjmp.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <math.h> |
| 29 | +#include <windows.h> |
| 30 | + |
| 31 | +#include <xmmintrin.h> |
| 32 | +#include <popcntintrin.h> |
| 33 | + |
| 34 | +#define builtin_cpuid(f, ax, bx, cx, dx) \ |
| 35 | + __asm__ __volatile__ ("cpuid" : "=a" (ax), "=b" (bx), "=c" (cx), \ |
| 36 | + "=d" (dx) : "a" (f)) |
| 37 | + |
| 38 | +#define builtin_popcnt_u64(x) _mm_popcnt_u64((x)) |
| 39 | + |
| 40 | +#define builtin_sync_fetch_and_add(x, y) \ |
| 41 | + __sync_fetch_and_add((x), (y)) |
| 42 | +#define builtin_sync_fetch_and_or(x, y) \ |
| 43 | + __sync_fetch_and_or((x), (y)) |
| 44 | +#define builtin_sync_fetch_and_and(x, y) \ |
| 45 | + __sync_fetch_and_and((x), (y)) |
| 46 | +#define builtin_sync_bit_test_and_reset(x, y) \ |
| 47 | + ((__sync_fetch_and_and((x), ~(1 << (y))) & (1 << (y))) != 0) |
| 48 | +#define builtin_sync_lock_test_and_set(x, y) \ |
| 49 | + __sync_lock_test_and_set((x), (y)) |
| 50 | +#define builtin_sync_val_compare_and_swap(x, y, z) \ |
| 51 | + __sync_val_compare_and_swap((x), (z), (y)) |
| 52 | + |
| 53 | +void init_hash() { |
| 54 | +#ifdef TUNER |
| 55 | + return; |
| 56 | +#endif |
| 57 | + char name[256]; |
| 58 | + sint64 size = (hash_size * sizeof(GEntry)); |
| 59 | + int min_page_size; |
| 60 | + HINSTANCE hDll; |
| 61 | + sprintf(name, "GULL_HASH_%d", WinParId); |
| 62 | + int initialized = 0; |
| 63 | + if (parent && HASH != NULL) { |
| 64 | + initialized = 1; |
| 65 | + UnmapViewOfFile(Hash); |
| 66 | + CloseHandle(HASH); |
| 67 | + } |
| 68 | + if (parent) { |
| 69 | + if (!LargePages) goto no_lp; |
| 70 | +#ifndef LARGE_PAGES |
| 71 | + goto no_lp; |
| 72 | +#endif |
| 73 | + typedef int(*GETLARGEPAGEMINIMUM)(void); |
| 74 | + GETLARGEPAGEMINIMUM pGetLargePageMinimum; |
| 75 | + hDll = LoadLibrary(TEXT("kernel32.dll")); |
| 76 | + if (hDll == NULL) goto no_lp; |
| 77 | + pGetLargePageMinimum = (GETLARGEPAGEMINIMUM)GetProcAddress(hDll, "GetLargePageMinimum"); |
| 78 | + if (pGetLargePageMinimum == NULL) goto no_lp; |
| 79 | + min_page_size = (*pGetLargePageMinimum)(); |
| 80 | + if (size < min_page_size) size = min_page_size; |
| 81 | + if (!initialized) { |
| 82 | + TOKEN_PRIVILEGES tp; |
| 83 | + HANDLE hToken; |
| 84 | + OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); |
| 85 | + LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &tp.Privileges[0].Luid); |
| 86 | + tp.PrivilegeCount = 1; |
| 87 | + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 88 | + AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0); |
| 89 | + } |
| 90 | + HASH = NULL; |
| 91 | + HASH = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES, size >> 32, size & 0xFFFFFFFF, name); |
| 92 | + if (HASH != NULL) { |
| 93 | + fprintf(stdout, "Large page hash\n"); |
| 94 | + goto hash_allocated; |
| 95 | + } |
| 96 | + no_lp: |
| 97 | + HASH = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size >> 32, size & 0xFFFFFFFF, name); |
| 98 | + } else HASH = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, name); |
| 99 | +hash_allocated: |
| 100 | + Hash = (GEntry*)MapViewOfFile(HASH, FILE_MAP_ALL_ACCESS, 0, 0, size); |
| 101 | + if (parent) memset(Hash, 0, size); |
| 102 | + hash_mask = hash_size - 4; |
| 103 | +} |
| 104 | + |
| 105 | +void init_shared() { |
| 106 | +#ifdef TUNER |
| 107 | + return; |
| 108 | +#endif |
| 109 | + char name[256]; |
| 110 | + sint64 size = SharedPVHashOffset + pv_hash_size * sizeof(GPVEntry); |
| 111 | + sprintf(name, "GULL_SHARED_%d", WinParId); |
| 112 | + if (parent && SHARED != NULL) { |
| 113 | + UnmapViewOfFile(Smpi); |
| 114 | + CloseHandle(SHARED); |
| 115 | + } |
| 116 | + if (parent) SHARED = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, name); |
| 117 | + else SHARED = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, name); |
| 118 | + Smpi = (GSMPI*)MapViewOfFile(SHARED, FILE_MAP_ALL_ACCESS, 0, 0, size); |
| 119 | + if (parent) memset(Smpi, 0, size); |
| 120 | + Material = (GMaterial*)(((char*)Smpi) + SharedMaterialOffset); |
| 121 | + MagicAttacks = (uint64*)(((char*)Smpi) + SharedMagicOffset); |
| 122 | + PVHash = (GPVEntry*)(((char*)Smpi) + SharedPVHashOffset); |
| 123 | + if (parent) memset(PVHash, 0, pv_hash_size * sizeof(GPVEntry)); |
| 124 | +} |
| 125 | + |
| 126 | +int input() { |
| 127 | + if (child) return 0; |
| 128 | + DWORD p; |
| 129 | + if (F(Input)) return 0; |
| 130 | + if (F(Console)) { |
| 131 | + if (PeekNamedPipe(StreamHandle,NULL,0,NULL,&p,NULL)) return (p > 0); |
| 132 | + else return 1; |
| 133 | + } else return 0; |
| 134 | +} |
| 135 | + |
| 136 | +HANDLE CreateChildProcess(int child_id) { |
| 137 | + char name[1024]; |
| 138 | + TCHAR szCmdline[1024]; |
| 139 | + PROCESS_INFORMATION piProcInfo; |
| 140 | + STARTUPINFO siStartInfo; |
| 141 | + BOOL bSuccess = FALSE; |
| 142 | + |
| 143 | + ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION)); |
| 144 | + ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 145 | + ZeroMemory(szCmdline, 1024 * sizeof(TCHAR)); |
| 146 | + ZeroMemory(name, 1024); |
| 147 | + |
| 148 | + siStartInfo.cb = sizeof(STARTUPINFO); |
| 149 | + siStartInfo.dwFlags |= STARTF_USESTDHANDLES; |
| 150 | + |
| 151 | + GetModuleFileName(NULL, name, 1024); |
| 152 | + sprintf(szCmdline, " child %d %d", WinParId, child_id); |
| 153 | + |
| 154 | + bSuccess = CreateProcess(TEXT(name), TEXT(szCmdline), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo); |
| 155 | + |
| 156 | + if (bSuccess) { |
| 157 | + CloseHandle(piProcInfo.hThread); |
| 158 | + return piProcInfo.hProcess; |
| 159 | + } else { |
| 160 | + fprintf(stdout, "Error %d\n", GetLastError()); |
| 161 | + return NULL; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +static void msleep(unsigned ms) |
| 166 | +{ |
| 167 | + Sleep(ms); |
| 168 | +} |
| 169 | + |
| 170 | +sint64 get_time() { |
| 171 | +#ifdef CPU_TIMING |
| 172 | +#ifndef TIMING |
| 173 | + if (CpuTiming) { |
| 174 | +#endif |
| 175 | + uint64 ctime; |
| 176 | + QueryProcessCycleTime(GetCurrentProcess(), &ctime); |
| 177 | +#ifdef TIMING |
| 178 | + return ctime / (CyclesPerMSec / 1000); |
| 179 | +#endif |
| 180 | + return (ctime / CyclesPerMSec); |
| 181 | +#ifndef TIMING |
| 182 | + } |
| 183 | +#endif |
| 184 | +#endif |
| 185 | + return GetTickCount(); |
| 186 | +} |
| 187 | + |
0 commit comments