WinThunk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**@file
  2. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. Module Name:
  5. WinNtThunk.c
  6. Abstract:
  7. Since the SEC is the only windows program in our emulation we
  8. must use a Tiano mechanism to export Win32 APIs to other modules.
  9. This is the role of the EFI_WIN_NT_THUNK_PROTOCOL.
  10. The mWinNtThunkTable exists so that a change to EFI_WIN_NT_THUNK_PROTOCOL
  11. will cause an error in initializing the array if all the member functions
  12. are not added. It looks like adding a element to end and not initializing
  13. it may cause the table to be initaliized with the members at the end being
  14. set to zero. This is bad as jumping to zero will case the NT32 to crash.
  15. All the member functions in mWinNtThunkTable are Win32
  16. API calls, so please reference Microsoft documentation.
  17. gWinNt is a a public exported global that contains the initialized
  18. data.
  19. **/
  20. #include "WinHost.h"
  21. UINTN
  22. SecWriteStdErr (
  23. IN UINT8 *Buffer,
  24. IN UINTN NumberOfBytes
  25. )
  26. {
  27. BOOL Success;
  28. DWORD CharCount;
  29. CharCount = (DWORD)NumberOfBytes;
  30. Success = WriteFile (
  31. GetStdHandle (STD_ERROR_HANDLE),
  32. Buffer,
  33. CharCount,
  34. &CharCount,
  35. NULL
  36. );
  37. return Success ? CharCount : 0;
  38. }
  39. EFI_STATUS
  40. SecConfigStdIn (
  41. VOID
  42. )
  43. {
  44. BOOL Success;
  45. DWORD Mode;
  46. Success = GetConsoleMode (GetStdHandle (STD_INPUT_HANDLE), &Mode);
  47. if (Success) {
  48. //
  49. // Disable buffer (line input), echo, mouse, window
  50. //
  51. Mode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
  52. #if defined (NTDDI_VERSION) && defined (NTDDI_WIN10_TH2) && (NTDDI_VERSION > NTDDI_WIN10_TH2)
  53. //
  54. // Enable virtual terminal input for Win10 above TH2
  55. //
  56. Mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
  57. #endif
  58. Success = SetConsoleMode (GetStdHandle (STD_INPUT_HANDLE), Mode);
  59. }
  60. #if defined (NTDDI_VERSION) && defined (NTDDI_WIN10_TH2) && (NTDDI_VERSION > NTDDI_WIN10_TH2)
  61. //
  62. // Enable terminal mode for Win10 above TH2
  63. //
  64. if (Success) {
  65. Success = GetConsoleMode (GetStdHandle (STD_OUTPUT_HANDLE), &Mode);
  66. if (Success) {
  67. Success = SetConsoleMode (
  68. GetStdHandle (STD_OUTPUT_HANDLE),
  69. Mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN
  70. );
  71. }
  72. }
  73. #endif
  74. return Success ? EFI_SUCCESS : EFI_DEVICE_ERROR;
  75. }
  76. UINTN
  77. SecWriteStdOut (
  78. IN UINT8 *Buffer,
  79. IN UINTN NumberOfBytes
  80. )
  81. {
  82. BOOL Success;
  83. DWORD CharCount;
  84. CharCount = (DWORD)NumberOfBytes;
  85. Success = WriteFile (
  86. GetStdHandle (STD_OUTPUT_HANDLE),
  87. Buffer,
  88. CharCount,
  89. &CharCount,
  90. NULL
  91. );
  92. return Success ? CharCount : 0;
  93. }
  94. BOOLEAN
  95. SecPollStdIn (
  96. VOID
  97. )
  98. {
  99. BOOL Success;
  100. INPUT_RECORD Record;
  101. DWORD RecordNum;
  102. do {
  103. Success = GetNumberOfConsoleInputEvents (GetStdHandle (STD_INPUT_HANDLE), &RecordNum);
  104. if (!Success || (RecordNum == 0)) {
  105. break;
  106. }
  107. Success = PeekConsoleInput (
  108. GetStdHandle (STD_INPUT_HANDLE),
  109. &Record,
  110. 1,
  111. &RecordNum
  112. );
  113. if (Success && (RecordNum == 1)) {
  114. if ((Record.EventType == KEY_EVENT) && Record.Event.KeyEvent.bKeyDown) {
  115. return TRUE;
  116. } else {
  117. //
  118. // Consume the non-key event.
  119. //
  120. Success = ReadConsoleInput (
  121. GetStdHandle (STD_INPUT_HANDLE),
  122. &Record,
  123. 1,
  124. &RecordNum
  125. );
  126. }
  127. }
  128. } while (Success);
  129. return FALSE;
  130. }
  131. UINTN
  132. SecReadStdIn (
  133. IN UINT8 *Buffer,
  134. IN UINTN NumberOfBytes
  135. )
  136. {
  137. BOOL Success;
  138. INPUT_RECORD Record;
  139. DWORD RecordNum;
  140. UINTN BytesReturn;
  141. if (!SecPollStdIn ()) {
  142. return 0;
  143. }
  144. Success = ReadConsoleInput (
  145. GetStdHandle (STD_INPUT_HANDLE),
  146. &Record,
  147. 1,
  148. &RecordNum
  149. );
  150. ASSERT (Success && (RecordNum == 1) && (Record.EventType == KEY_EVENT) && (Record.Event.KeyEvent.bKeyDown));
  151. NumberOfBytes = MIN (Record.Event.KeyEvent.wRepeatCount, NumberOfBytes);
  152. BytesReturn = NumberOfBytes;
  153. while (NumberOfBytes-- != 0) {
  154. Buffer[NumberOfBytes] = Record.Event.KeyEvent.uChar.AsciiChar;
  155. }
  156. return BytesReturn;
  157. }
  158. VOID *
  159. SecAlloc (
  160. IN UINTN Size
  161. )
  162. {
  163. return malloc ((size_t)Size);
  164. }
  165. BOOLEAN
  166. SecFree (
  167. IN VOID *Ptr
  168. )
  169. {
  170. if (EfiSystemMemoryRange (Ptr)) {
  171. // If an address range is in the EFI memory map it was alloced via EFI.
  172. // So don't free those ranges and let the caller know.
  173. return FALSE;
  174. }
  175. free (Ptr);
  176. return TRUE;
  177. }
  178. //
  179. // Define a global that we can use to shut down the NT timer thread when
  180. // the timer is canceled.
  181. //
  182. BOOLEAN mCancelTimerThread = FALSE;
  183. //
  184. // The notification function to call on every timer interrupt
  185. //
  186. EMU_SET_TIMER_CALLBACK *mTimerNotifyFunction = NULL;
  187. //
  188. // The thread handle for this driver
  189. //
  190. HANDLE mNtMainThreadHandle;
  191. //
  192. // The timer value from the last timer interrupt
  193. //
  194. UINT32 mNtLastTick;
  195. //
  196. // Critical section used to update varibles shared between the main thread and
  197. // the timer interrupt thread.
  198. //
  199. CRITICAL_SECTION mNtCriticalSection;
  200. //
  201. // Worker Functions
  202. //
  203. UINT mMMTimerThreadID = 0;
  204. volatile BOOLEAN mInterruptEnabled = FALSE;
  205. VOID
  206. CALLBACK
  207. MMTimerThread (
  208. UINT wTimerID,
  209. UINT msg,
  210. DWORD dwUser,
  211. DWORD dw1,
  212. DWORD dw2
  213. )
  214. {
  215. UINT32 CurrentTick;
  216. UINT32 Delta;
  217. if (!mCancelTimerThread) {
  218. //
  219. // Suspend the main thread until we are done.
  220. // Enter the critical section before suspending
  221. // and leave the critical section after resuming
  222. // to avoid deadlock between main and timer thread.
  223. //
  224. EnterCriticalSection (&mNtCriticalSection);
  225. SuspendThread (mNtMainThreadHandle);
  226. //
  227. // If the timer thread is being canceled, then bail immediately.
  228. // We check again here because there's a small window of time from when
  229. // this thread was kicked off and when we suspended the main thread above.
  230. //
  231. if (mCancelTimerThread) {
  232. ResumeThread (mNtMainThreadHandle);
  233. LeaveCriticalSection (&mNtCriticalSection);
  234. timeKillEvent (wTimerID);
  235. mMMTimerThreadID = 0;
  236. return;
  237. }
  238. while (!mInterruptEnabled) {
  239. //
  240. // Resume the main thread
  241. //
  242. ResumeThread (mNtMainThreadHandle);
  243. LeaveCriticalSection (&mNtCriticalSection);
  244. //
  245. // Wait for interrupts to be enabled.
  246. //
  247. while (!mInterruptEnabled) {
  248. Sleep (1);
  249. }
  250. //
  251. // Suspend the main thread until we are done
  252. //
  253. EnterCriticalSection (&mNtCriticalSection);
  254. SuspendThread (mNtMainThreadHandle);
  255. }
  256. //
  257. // Get the current system tick
  258. //
  259. CurrentTick = GetTickCount ();
  260. Delta = CurrentTick - mNtLastTick;
  261. mNtLastTick = CurrentTick;
  262. //
  263. // If delay was more then 1 second, ignore it (probably debugging case)
  264. //
  265. if (Delta < 1000) {
  266. //
  267. // Only invoke the callback function if a Non-NULL handler has been
  268. // registered. Assume all other handlers are legal.
  269. //
  270. if (mTimerNotifyFunction != NULL) {
  271. mTimerNotifyFunction (Delta);
  272. }
  273. }
  274. //
  275. // Resume the main thread
  276. //
  277. ResumeThread (mNtMainThreadHandle);
  278. LeaveCriticalSection (&mNtCriticalSection);
  279. } else {
  280. timeKillEvent (wTimerID);
  281. mMMTimerThreadID = 0;
  282. }
  283. }
  284. VOID
  285. SecSetTimer (
  286. IN UINT64 TimerPeriod,
  287. IN EMU_SET_TIMER_CALLBACK Callback
  288. )
  289. {
  290. //
  291. // If TimerPeriod is 0, then the timer thread should be canceled
  292. //
  293. if (TimerPeriod == 0) {
  294. //
  295. // Cancel the timer thread
  296. //
  297. EnterCriticalSection (&mNtCriticalSection);
  298. mCancelTimerThread = TRUE;
  299. LeaveCriticalSection (&mNtCriticalSection);
  300. //
  301. // Wait for the timer thread to exit
  302. //
  303. if (mMMTimerThreadID != 0) {
  304. timeKillEvent (mMMTimerThreadID);
  305. mMMTimerThreadID = 0;
  306. }
  307. } else {
  308. //
  309. // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread
  310. //
  311. EnterCriticalSection (&mNtCriticalSection);
  312. mCancelTimerThread = FALSE;
  313. LeaveCriticalSection (&mNtCriticalSection);
  314. //
  315. // Get the starting tick location if we are just starting the timer thread
  316. //
  317. mNtLastTick = GetTickCount ();
  318. if (mMMTimerThreadID) {
  319. timeKillEvent (mMMTimerThreadID);
  320. }
  321. SetThreadPriority (
  322. GetCurrentThread (),
  323. THREAD_PRIORITY_HIGHEST
  324. );
  325. mMMTimerThreadID = timeSetEvent (
  326. (UINT)TimerPeriod,
  327. 0,
  328. MMTimerThread,
  329. (DWORD_PTR)NULL,
  330. TIME_PERIODIC | TIME_KILL_SYNCHRONOUS | TIME_CALLBACK_FUNCTION
  331. );
  332. }
  333. mTimerNotifyFunction = Callback;
  334. }
  335. VOID
  336. SecInitializeThunk (
  337. VOID
  338. )
  339. {
  340. InitializeCriticalSection (&mNtCriticalSection);
  341. DuplicateHandle (
  342. GetCurrentProcess (),
  343. GetCurrentThread (),
  344. GetCurrentProcess (),
  345. &mNtMainThreadHandle,
  346. 0,
  347. FALSE,
  348. DUPLICATE_SAME_ACCESS
  349. );
  350. }
  351. VOID
  352. SecEnableInterrupt (
  353. VOID
  354. )
  355. {
  356. mInterruptEnabled = TRUE;
  357. }
  358. VOID
  359. SecDisableInterrupt (
  360. VOID
  361. )
  362. {
  363. mInterruptEnabled = FALSE;
  364. }
  365. UINT64
  366. SecQueryPerformanceFrequency (
  367. VOID
  368. )
  369. {
  370. // Hard code to nanoseconds
  371. return 1000000000ULL;
  372. }
  373. UINT64
  374. SecQueryPerformanceCounter (
  375. VOID
  376. )
  377. {
  378. return 0;
  379. }
  380. VOID
  381. SecSleep (
  382. IN UINT64 Nanoseconds
  383. )
  384. {
  385. Sleep ((DWORD)DivU64x32 (Nanoseconds, 1000000));
  386. }
  387. VOID
  388. SecCpuSleep (
  389. VOID
  390. )
  391. {
  392. Sleep (1);
  393. }
  394. VOID
  395. SecExit (
  396. UINTN Status
  397. )
  398. {
  399. exit ((int)Status);
  400. }
  401. VOID
  402. SecGetTime (
  403. OUT EFI_TIME *Time,
  404. OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL
  405. )
  406. {
  407. SYSTEMTIME SystemTime;
  408. TIME_ZONE_INFORMATION TimeZone;
  409. GetLocalTime (&SystemTime);
  410. GetTimeZoneInformation (&TimeZone);
  411. Time->Year = (UINT16)SystemTime.wYear;
  412. Time->Month = (UINT8)SystemTime.wMonth;
  413. Time->Day = (UINT8)SystemTime.wDay;
  414. Time->Hour = (UINT8)SystemTime.wHour;
  415. Time->Minute = (UINT8)SystemTime.wMinute;
  416. Time->Second = (UINT8)SystemTime.wSecond;
  417. Time->Nanosecond = (UINT32)(SystemTime.wMilliseconds * 1000000);
  418. Time->TimeZone = (INT16)TimeZone.Bias;
  419. if (Capabilities != NULL) {
  420. Capabilities->Resolution = 1;
  421. Capabilities->Accuracy = 50000000;
  422. Capabilities->SetsToZero = FALSE;
  423. }
  424. Time->Daylight = 0;
  425. if (TimeZone.StandardDate.wMonth) {
  426. Time->Daylight = (UINT8)TimeZone.StandardDate.wMonth;
  427. }
  428. }
  429. EFI_STATUS
  430. SecSetTime (
  431. IN EFI_TIME *Time
  432. )
  433. {
  434. TIME_ZONE_INFORMATION TimeZone;
  435. SYSTEMTIME SystemTime;
  436. BOOL Flag;
  437. //
  438. // Set Daylight savings time information and Time Zone
  439. //
  440. GetTimeZoneInformation (&TimeZone);
  441. TimeZone.StandardDate.wMonth = Time->Daylight;
  442. TimeZone.Bias = Time->TimeZone;
  443. Flag = SetTimeZoneInformation (&TimeZone);
  444. if (!Flag) {
  445. return EFI_DEVICE_ERROR;
  446. }
  447. SystemTime.wYear = Time->Year;
  448. SystemTime.wMonth = Time->Month;
  449. SystemTime.wDay = Time->Day;
  450. SystemTime.wHour = Time->Hour;
  451. SystemTime.wMinute = Time->Minute;
  452. SystemTime.wSecond = Time->Second;
  453. SystemTime.wMilliseconds = (INT16)(Time->Nanosecond / 1000000);
  454. Flag = SetLocalTime (&SystemTime);
  455. if (!Flag) {
  456. return EFI_DEVICE_ERROR;
  457. } else {
  458. return EFI_SUCCESS;
  459. }
  460. }
  461. EMU_THUNK_PROTOCOL gEmuThunkProtocol = {
  462. SecWriteStdErr,
  463. SecConfigStdIn,
  464. SecWriteStdOut,
  465. SecReadStdIn,
  466. SecPollStdIn,
  467. SecAlloc,
  468. NULL,
  469. SecFree,
  470. SecPeCoffGetEntryPoint,
  471. PeCoffLoaderRelocateImageExtraAction,
  472. PeCoffLoaderUnloadImageExtraAction,
  473. SecEnableInterrupt,
  474. SecDisableInterrupt,
  475. SecQueryPerformanceFrequency,
  476. SecQueryPerformanceCounter,
  477. SecSleep,
  478. SecCpuSleep,
  479. SecExit,
  480. SecGetTime,
  481. SecSetTime,
  482. SecSetTimer,
  483. GetNextThunkProtocol
  484. };
  485. #pragma warning(default : 4996)
  486. #pragma warning(default : 4232)