EmuThunk.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*++ @file
  2. Since the SEC is the only program in our emulation we
  3. must use a UEFI/PI mechanism to export APIs to other modules.
  4. This is the role of the EFI_EMU_THUNK_PROTOCOL.
  5. The mUnixThunkTable exists so that a change to EFI_EMU_THUNK_PROTOCOL
  6. will cause an error in initializing the array if all the member functions
  7. are not added. It looks like adding a element to end and not initializing
  8. it may cause the table to be initialized with the members at the end being
  9. set to zero. This is bad as jumping to zero will crash.
  10. Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>
  11. Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
  12. SPDX-License-Identifier: BSD-2-Clause-Patent
  13. **/
  14. #include "Host.h"
  15. #ifdef __APPLE__
  16. #define DebugAssert _Mangle__DebugAssert
  17. #include <assert.h>
  18. #include <CoreServices/CoreServices.h>
  19. #include <mach/mach.h>
  20. #include <mach/mach_time.h>
  21. #undef DebugAssert
  22. #endif
  23. int settimer_initialized;
  24. struct timeval settimer_timeval;
  25. UINTN settimer_callback = 0;
  26. BOOLEAN gEmulatorInterruptEnabled = FALSE;
  27. UINTN
  28. SecWriteStdErr (
  29. IN UINT8 *Buffer,
  30. IN UINTN NumberOfBytes
  31. )
  32. {
  33. ssize_t Return;
  34. Return = write (STDERR_FILENO, (const void *)Buffer, (size_t)NumberOfBytes);
  35. return (Return == -1) ? 0 : Return;
  36. }
  37. EFI_STATUS
  38. SecConfigStdIn (
  39. VOID
  40. )
  41. {
  42. struct termios tty;
  43. //
  44. // Need to turn off line buffering, ECHO, and make it unbuffered.
  45. //
  46. tcgetattr (STDIN_FILENO, &tty);
  47. tty.c_lflag &= ~(ICANON | ECHO);
  48. tcsetattr (STDIN_FILENO, TCSANOW, &tty);
  49. // setvbuf (STDIN_FILENO, NULL, _IONBF, 0);
  50. // now ioctl FIONREAD will do what we need
  51. return EFI_SUCCESS;
  52. }
  53. UINTN
  54. SecWriteStdOut (
  55. IN UINT8 *Buffer,
  56. IN UINTN NumberOfBytes
  57. )
  58. {
  59. ssize_t Return;
  60. Return = write (STDOUT_FILENO, (const void *)Buffer, (size_t)NumberOfBytes);
  61. return (Return == -1) ? 0 : Return;
  62. }
  63. UINTN
  64. SecReadStdIn (
  65. IN UINT8 *Buffer,
  66. IN UINTN NumberOfBytes
  67. )
  68. {
  69. ssize_t Return;
  70. Return = read (STDIN_FILENO, Buffer, (size_t)NumberOfBytes);
  71. return (Return == -1) ? 0 : Return;
  72. }
  73. BOOLEAN
  74. SecPollStdIn (
  75. VOID
  76. )
  77. {
  78. int Result;
  79. int Bytes;
  80. Result = ioctl (STDIN_FILENO, FIONREAD, &Bytes);
  81. if (Result == -1) {
  82. return FALSE;
  83. }
  84. return (BOOLEAN)(Bytes > 0);
  85. }
  86. VOID *
  87. SecMalloc (
  88. IN UINTN Size
  89. )
  90. {
  91. return malloc ((size_t)Size);
  92. }
  93. VOID *
  94. SecValloc (
  95. IN UINTN Size
  96. )
  97. {
  98. return valloc ((size_t)Size);
  99. }
  100. BOOLEAN
  101. SecFree (
  102. IN VOID *Ptr
  103. )
  104. {
  105. if (EfiSystemMemoryRange (Ptr)) {
  106. // If an address range is in the EFI memory map it was alloced via EFI.
  107. // So don't free those ranges and let the caller know.
  108. return FALSE;
  109. }
  110. free (Ptr);
  111. return TRUE;
  112. }
  113. void
  114. settimer_handler (
  115. int sig
  116. )
  117. {
  118. struct timeval timeval;
  119. UINT64 delta;
  120. gettimeofday (&timeval, NULL);
  121. delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
  122. - ((UINT64)settimer_timeval.tv_sec * 1000)
  123. - (settimer_timeval.tv_usec / 1000);
  124. settimer_timeval = timeval;
  125. if (settimer_callback) {
  126. ReverseGasketUint64 (settimer_callback, delta);
  127. }
  128. }
  129. VOID
  130. SecSetTimer (
  131. IN UINT64 PeriodMs,
  132. IN EMU_SET_TIMER_CALLBACK CallBack
  133. )
  134. {
  135. struct itimerval timerval;
  136. UINT32 remainder;
  137. if (!settimer_initialized) {
  138. struct sigaction act;
  139. settimer_initialized = 1;
  140. act.sa_handler = settimer_handler;
  141. act.sa_flags = 0;
  142. sigemptyset (&act.sa_mask);
  143. gEmulatorInterruptEnabled = TRUE;
  144. if (sigaction (SIGALRM, &act, NULL) != 0) {
  145. printf ("SetTimer: sigaction error %s\n", strerror (errno));
  146. }
  147. if (gettimeofday (&settimer_timeval, NULL) != 0) {
  148. printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
  149. }
  150. }
  151. timerval.it_value.tv_sec = DivU64x32 (PeriodMs, 1000);
  152. DivU64x32Remainder (PeriodMs, 1000, &remainder);
  153. timerval.it_value.tv_usec = remainder * 1000;
  154. timerval.it_value.tv_sec = DivU64x32 (PeriodMs, 1000);
  155. timerval.it_interval = timerval.it_value;
  156. if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
  157. printf ("SetTimer: setitimer error %s\n", strerror (errno));
  158. }
  159. settimer_callback = (UINTN)CallBack;
  160. }
  161. VOID
  162. SecEnableInterrupt (
  163. VOID
  164. )
  165. {
  166. sigset_t sigset;
  167. gEmulatorInterruptEnabled = TRUE;
  168. // Since SetTimer() uses SIGALRM we emulate turning on and off interrupts
  169. // by enabling/disabling SIGALRM.
  170. sigemptyset (&sigset);
  171. sigaddset (&sigset, SIGALRM);
  172. pthread_sigmask (SIG_UNBLOCK, &sigset, NULL);
  173. }
  174. VOID
  175. SecDisableInterrupt (
  176. VOID
  177. )
  178. {
  179. sigset_t sigset;
  180. // Since SetTimer() uses SIGALRM we emulate turning on and off interrupts
  181. // by enabling/disabling SIGALRM.
  182. sigemptyset (&sigset);
  183. sigaddset (&sigset, SIGALRM);
  184. pthread_sigmask (SIG_BLOCK, &sigset, NULL);
  185. gEmulatorInterruptEnabled = FALSE;
  186. }
  187. BOOLEAN
  188. SecInterruptEanbled (
  189. void
  190. )
  191. {
  192. return gEmulatorInterruptEnabled;
  193. }
  194. UINT64
  195. QueryPerformanceFrequency (
  196. VOID
  197. )
  198. {
  199. // Hard code to nanoseconds
  200. return 1000000000ULL;
  201. }
  202. UINT64
  203. QueryPerformanceCounter (
  204. VOID
  205. )
  206. {
  207. #if __APPLE__
  208. UINT64 Start;
  209. static mach_timebase_info_data_t sTimebaseInfo;
  210. Start = mach_absolute_time ();
  211. // Convert to nanoseconds.
  212. // If this is the first time we've run, get the timebase.
  213. // We can use denom == 0 to indicate that sTimebaseInfo is
  214. // uninitialised because it makes no sense to have a zero
  215. // denominator is a fraction.
  216. if ( sTimebaseInfo.denom == 0 ) {
  217. (void)mach_timebase_info (&sTimebaseInfo);
  218. }
  219. // Do the maths. We hope that the multiplication doesn't
  220. // overflow; the price you pay for working in fixed point.
  221. return (Start * sTimebaseInfo.numer) / sTimebaseInfo.denom;
  222. #else
  223. // Need to figure out what to do for Linux?
  224. return 0;
  225. #endif
  226. }
  227. VOID
  228. SecSleep (
  229. IN UINT64 Nanoseconds
  230. )
  231. {
  232. struct timespec rq, rm;
  233. struct timeval start, end;
  234. unsigned long MicroSec;
  235. rq.tv_sec = DivU64x32 (Nanoseconds, 1000000000);
  236. rq.tv_nsec = ModU64x32 (Nanoseconds, 1000000000);
  237. //
  238. // nanosleep gets interrupted by our timer tic.
  239. // we need to track wall clock time or we will stall for way too long
  240. //
  241. gettimeofday (&start, NULL);
  242. end.tv_sec = start.tv_sec + rq.tv_sec;
  243. MicroSec = (start.tv_usec + rq.tv_nsec/1000);
  244. end.tv_usec = MicroSec % 1000000;
  245. if (MicroSec > 1000000) {
  246. end.tv_sec++;
  247. }
  248. while (nanosleep (&rq, &rm) == -1) {
  249. if (errno != EINTR) {
  250. break;
  251. }
  252. gettimeofday (&start, NULL);
  253. if (start.tv_sec > end.tv_sec) {
  254. break;
  255. }
  256. if ((start.tv_sec == end.tv_sec) && (start.tv_usec > end.tv_usec)) {
  257. break;
  258. }
  259. rq = rm;
  260. }
  261. }
  262. VOID
  263. SecCpuSleep (
  264. VOID
  265. )
  266. {
  267. struct timespec rq, rm;
  268. // nanosleep gets interrupted by the timer tic
  269. rq.tv_sec = 1;
  270. rq.tv_nsec = 0;
  271. nanosleep (&rq, &rm);
  272. }
  273. VOID
  274. SecExit (
  275. UINTN Status
  276. )
  277. {
  278. exit (Status);
  279. }
  280. VOID
  281. SecGetTime (
  282. OUT EFI_TIME *Time,
  283. OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL
  284. )
  285. {
  286. struct tm *tm;
  287. time_t t;
  288. t = time (NULL);
  289. tm = localtime (&t);
  290. Time->Year = 1900 + tm->tm_year;
  291. Time->Month = tm->tm_mon + 1;
  292. Time->Day = tm->tm_mday;
  293. Time->Hour = tm->tm_hour;
  294. Time->Minute = tm->tm_min;
  295. Time->Second = tm->tm_sec;
  296. Time->Nanosecond = 0;
  297. Time->TimeZone = timezone / 60;
  298. Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
  299. | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
  300. if (Capabilities != NULL) {
  301. Capabilities->Resolution = 1;
  302. Capabilities->Accuracy = 50000000;
  303. Capabilities->SetsToZero = FALSE;
  304. }
  305. }
  306. VOID
  307. SecSetTime (
  308. IN EFI_TIME *Time
  309. )
  310. {
  311. // Don't change the time on the system
  312. // We could save delta to localtime() and have SecGetTime adjust return values?
  313. return;
  314. }
  315. EFI_STATUS
  316. SecGetNextProtocol (
  317. IN BOOLEAN EmuBusDriver,
  318. OUT EMU_IO_THUNK_PROTOCOL **Instance OPTIONAL
  319. )
  320. {
  321. return GetNextThunkProtocol (EmuBusDriver, Instance);
  322. }
  323. EMU_THUNK_PROTOCOL gEmuThunkProtocol = {
  324. GasketSecWriteStdErr,
  325. GasketSecConfigStdIn,
  326. GasketSecWriteStdOut,
  327. GasketSecReadStdIn,
  328. GasketSecPollStdIn,
  329. GasketSecMalloc,
  330. GasketSecValloc,
  331. GasketSecFree,
  332. GasketSecPeCoffGetEntryPoint,
  333. GasketSecPeCoffRelocateImageExtraAction,
  334. GasketSecPeCoffUnloadImageExtraAction,
  335. GasketSecEnableInterrupt,
  336. GasketSecDisableInterrupt,
  337. GasketQueryPerformanceFrequency,
  338. GasketQueryPerformanceCounter,
  339. GasketSecSleep,
  340. GasketSecCpuSleep,
  341. GasketSecExit,
  342. GasketSecGetTime,
  343. GasketSecSetTime,
  344. GasketSecSetTimer,
  345. GasketSecGetNextProtocol
  346. };
  347. VOID
  348. SecInitThunkProtocol (
  349. VOID
  350. )
  351. {
  352. // timezone and daylight lib globals depend on tzset be called 1st.
  353. tzset ();
  354. }