21#define DebugAssert _Mangle__DebugAssert
24 #include <CoreServices/CoreServices.h>
25 #include <mach/mach.h>
26 #include <mach/mach_time.h>
31int settimer_initialized;
33UINTN settimer_callback = 0;
35BOOLEAN gEmulatorInterruptEnabled =
FALSE;
38STATIC struct termios mOldTty;
48 Return = write (STDERR_FILENO, (
const void *)Buffer, (
size_t)NumberOfBytes);
50 return (Return == -1) ? 0 : Return;
63 tcgetattr (STDIN_FILENO, &tty);
64 if (!mEmulatorStdInConfigured) {
68 CopyMem (&mOldTty, &tty,
sizeof (
struct termios));
71 tty.c_lflag &= ~(ICANON | ECHO);
72 tcsetattr (STDIN_FILENO, TCSANOW, &tty);
73 mEmulatorStdInConfigured =
TRUE;
89 Return = write (STDOUT_FILENO, (
const void *)Buffer, (
size_t)NumberOfBytes);
91 return (Return == -1) ? 0 : Return;
102 Return = read (STDIN_FILENO, Buffer, (
size_t)NumberOfBytes);
104 return (Return == -1) ? 0 : Return;
115 Result = ioctl (STDIN_FILENO, FIONREAD, &Bytes);
120 return (BOOLEAN)(Bytes > 0);
128 return malloc ((
size_t)Size);
136 return valloc ((
size_t)Size);
144 if (EfiSystemMemoryRange (Ptr)) {
163 delta = ((UINT64)
timeval.tv_sec * 1000) + (
timeval.tv_usec / 1000)
164 - ((UINT64)settimer_timeval.tv_sec * 1000)
165 - (settimer_timeval.tv_usec / 1000);
168 if (settimer_callback) {
169 ReverseGasketUint64 (settimer_callback, delta);
176 IN EMU_SET_TIMER_CALLBACK CallBack
179 struct itimerval timerval;
182 if (!settimer_initialized) {
183 struct sigaction act;
185 settimer_initialized = 1;
186 act.sa_handler = settimer_handler;
188 sigemptyset (&act.sa_mask);
189 gEmulatorInterruptEnabled =
TRUE;
190 if (sigaction (SIGALRM, &act,
NULL) != 0) {
191 printf (
"SetTimer: sigaction error %s\n", strerror (errno));
194 if (gettimeofday (&settimer_timeval,
NULL) != 0) {
195 printf (
"SetTimer: gettimeofday error %s\n", strerror (errno));
199 timerval.it_value.tv_sec =
DivU64x32 (PeriodMs, 1000);
201 timerval.it_value.tv_usec = remainder * 1000;
202 timerval.it_value.tv_sec =
DivU64x32 (PeriodMs, 1000);
203 timerval.it_interval = timerval.it_value;
205 if (setitimer (ITIMER_REAL, &timerval,
NULL) != 0) {
206 printf (
"SetTimer: setitimer error %s\n", strerror (errno));
209 settimer_callback = (
UINTN)CallBack;
219 gEmulatorInterruptEnabled =
TRUE;
222 sigemptyset (&sigset);
223 sigaddset (&sigset, SIGALRM);
224 pthread_sigmask (SIG_UNBLOCK, &sigset,
NULL);
236 sigemptyset (&sigset);
237 sigaddset (&sigset, SIGALRM);
238 pthread_sigmask (SIG_BLOCK, &sigset,
NULL);
239 gEmulatorInterruptEnabled =
FALSE;
247 return gEmulatorInterruptEnabled;
251QueryPerformanceFrequency (
256 return 1000000000ULL;
260QueryPerformanceCounter (
266 static mach_timebase_info_data_t sTimebaseInfo;
268 Start = mach_absolute_time ();
277 if ( sTimebaseInfo.denom == 0 ) {
278 (void)mach_timebase_info (&sTimebaseInfo);
284 return (Start * sTimebaseInfo.numer) / sTimebaseInfo.denom;
293 IN UINT64 Nanoseconds
296 struct timespec rq, rm;
298 unsigned long MicroSec;
300 rq.tv_sec =
DivU64x32 (Nanoseconds, 1000000000);
301 rq.tv_nsec =
ModU64x32 (Nanoseconds, 1000000000);
307 gettimeofday (&start,
NULL);
308 end.tv_sec = start.tv_sec + rq.tv_sec;
309 MicroSec = (start.tv_usec + rq.tv_nsec/1000);
310 end.tv_usec = MicroSec % 1000000;
311 if (MicroSec > 1000000) {
315 while (nanosleep (&rq, &rm) == -1) {
316 if (
errno != EINTR) {
320 gettimeofday (&start,
NULL);
321 if (start.tv_sec > end.tv_sec) {
325 if ((start.tv_sec == end.tv_sec) && (start.tv_usec > end.tv_usec)) {
338 struct timespec rq, rm;
344 nanosleep (&rq, &rm);
353 if (mEmulatorStdInConfigured) {
354 tcsetattr (STDIN_FILENO, TCSANOW, &mOldTty);
372 Time->Year = 1900 +
tm->tm_year;
373 Time->Month =
tm->tm_mon + 1;
374 Time->Day =
tm->tm_mday;
375 Time->Hour =
tm->tm_hour;
376 Time->Minute =
tm->tm_min;
377 Time->Second =
tm->tm_sec;
378 Time->Nanosecond = 0;
379 Time->TimeZone = timezone / 60;
380 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
381 | (
tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
383 if (Capabilities !=
NULL) {
384 Capabilities->Resolution = 1;
385 Capabilities->Accuracy = 50000000;
386 Capabilities->SetsToZero =
FALSE;
397 return EFI_UNSUPPORTED;
402 IN BOOLEAN EmuBusDriver,
406 return GetNextThunkProtocol (EmuBusDriver, Instance);
410 GasketSecWriteStdErr,
411 GasketSecConfigStdIn,
412 GasketSecWriteStdOut,
418 GasketSecPeCoffGetEntryPoint,
419 GasketSecPeCoffRelocateImageExtraAction,
420 GasketSecPeCoffUnloadImageExtraAction,
421 GasketSecEnableInterrupt,
422 GasketSecDisableInterrupt,
423 GasketQueryPerformanceFrequency,
424 GasketQueryPerformanceCounter,
431 GasketSecGetNextProtocol
435SecInitThunkProtocol (
UINT64 EFIAPI DivU64x32(IN UINT64 Dividend, IN UINT32 Divisor)
UINT32 EFIAPI ModU64x32(IN UINT64 Dividend, IN UINT32 Divisor)
UINT64 EFIAPI DivU64x32Remainder(IN UINT64 Dividend, IN UINT32 Divisor, OUT UINT32 *Remainder OPTIONAL)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)