]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UnixPkg/Sec/UnixThunk.c
MdePkg/BaseSynchronizationLib: Added ARM Aarch64 architecture support
[mirror_edk2.git] / UnixPkg / Sec / UnixThunk.c
index 500568ad52e6ea1479eb984a2c7732c505b680e2..eb9e53698923d133d33761b9e6a670b4fe1c4401 100644 (file)
@@ -1,7 +1,8 @@
 /*++
 
-Copyright (c) 2004 - 2006, Intel Corporation                                                         
-All rights reserved. This program and the accompanying materials                          
+Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
+Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
+This program and the accompanying materials                          
 are licensed and made available under the terms and conditions of the BSD License         
 which accompanies this distribution.  The full text of the license may be found at        
 http://opensource.org/licenses/bsd-license.php                                            
@@ -32,13 +33,21 @@ Abstract:
 --*/
 
 #include "SecMain.h"
+#include "Uefi.h"
 #include "Library/UnixLib.h"
 
-static int settimer_initialized;
-static struct timeval settimer_timeval;
-static void (*settimer_callback)(UINT64 delta);
+#if defined(__APPLE__) || defined(MDE_CPU_X64)
+#include "Gasket.h"
+#endif
 
-static void
+int settimer_initialized;
+struct timeval settimer_timeval;
+void (*settimer_callback)(UINT64 delta);
+
+BOOLEAN gEmulatorInterruptEnabled = FALSE;
+
+
+void
 settimer_handler (int sig)
 {
   struct timeval timeval;
@@ -49,15 +58,21 @@ settimer_handler (int sig)
     - ((UINT64)settimer_timeval.tv_sec * 1000) 
     - (settimer_timeval.tv_usec / 1000);
   settimer_timeval = timeval;
-  if (settimer_callback)
+  
+  if (settimer_callback) {
+#if defined(__APPLE__) || defined(MDE_CPU_X64)
+   ReverseGasketUint64 (settimer_callback, delta);
+#else
     (*settimer_callback)(delta);
+#endif
+  }
 }
 
-static
 VOID
 SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
 {
   struct itimerval timerval;
+  UINT32 remainder;
 
   if (!settimer_initialized) {
     struct sigaction act;
@@ -66,6 +81,7 @@ SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
     act.sa_handler = settimer_handler;
     act.sa_flags = 0;
     sigemptyset (&act.sa_mask);
+    gEmulatorInterruptEnabled = TRUE;
     if (sigaction (SIGALRM, &act, NULL) != 0) {
       printf ("SetTimer: sigaction error %s\n", strerror (errno));
     }
@@ -73,9 +89,10 @@ SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
       printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
     }
   }
-  timerval.it_value.tv_sec = PeriodMs / 1000;
-  timerval.it_value.tv_usec = (PeriodMs % 1000) * 1000;
-  timerval.it_value.tv_sec = PeriodMs / 1000;
+  timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
+  DivU64x32Remainder(PeriodMs, 1000, &remainder);
+  timerval.it_value.tv_usec = remainder * 1000;
+  timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
   timerval.it_interval = timerval.it_value;
   
   if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
@@ -84,16 +101,77 @@ SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
   settimer_callback = CallBack;
 }
 
+
+void
+UnixEnableInterrupt (void)
+{
+  sigset_t  sigset;
+
+  gEmulatorInterruptEnabled = TRUE;
+  // Since SetTimer() uses SIGALRM we emulate turning on and off interrupts 
+  // by enabling/disabling SIGALRM.
+  sigemptyset (&sigset);
+  sigaddset (&sigset, SIGALRM);
+  sigprocmask (SIG_UNBLOCK, &sigset, NULL);
+}
+
+
+void
+UnixDisableInterrupt (void)
+{
+  sigset_t  sigset;
+
+  // Since SetTimer() uses SIGALRM we emulate turning on and off interrupts 
+  // by enabling/disabling SIGALRM.
+  sigemptyset (&sigset);
+  sigaddset (&sigset, SIGALRM);
+  sigprocmask (SIG_BLOCK, &sigset, NULL);
+  gEmulatorInterruptEnabled = FALSE;
+}
+
+
+BOOLEAN
+UnixInterruptEanbled (void)
+{
+  return gEmulatorInterruptEnabled;
+}
+
+
+
 void
 msSleep (unsigned long Milliseconds)
 {
-  struct timespec ts;
+  struct timespec rq, rm;
+  struct timeval  start, end;
+  unsigned long  MicroSec;
+  
+  rq.tv_sec = Milliseconds / 1000;
+  rq.tv_nsec = (Milliseconds % 1000) * 1000000;
 
-  ts.tv_sec = Milliseconds / 1000;
-  ts.tv_nsec = (Milliseconds % 1000) * 1000000;
+  //
+  // nanosleep gets interrupted by our timer tic. 
+  // we need to track wall clock time or we will stall for way too long
+  //
+  gettimeofday (&start, NULL);
+  end.tv_sec  = start.tv_sec + rq.tv_sec;
+  MicroSec = (start.tv_usec + rq.tv_nsec/1000);
+  end.tv_usec = MicroSec % 1000000;
+  if (MicroSec > 1000000) {
+    end.tv_sec++;
+  }
 
-  while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
-    ;
+  while (nanosleep (&rq, &rm) == -1) {
+    if (errno != EINTR) {
+      break;
+    }
+    gettimeofday (&start, NULL);
+    if (start.tv_sec > end.tv_sec) {
+      break;
+    } if ((start.tv_sec == end.tv_sec) && (start.tv_usec > end.tv_usec)) {
+      break;
+    }
+    rq = rm;
+  } 
 }
 
 void
@@ -106,21 +184,21 @@ GetLocalTime (EFI_TIME *Time)
   tm = localtime (&t);
 
   Time->Year = 1900 + tm->tm_year;
-  Time->Month = tm->tm_mon;
+  Time->Month = tm->tm_mon + 1;
   Time->Day = tm->tm_mday;
   Time->Hour = tm->tm_hour;
   Time->Minute = tm->tm_min;
   Time->Second = tm->tm_sec;
   Time->Nanosecond = 0;
-  Time->TimeZone = timezone;
+  Time->TimeZone = GetTimeZone ();
   Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
     | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
 }
 
-static void
+void
 TzSet (void)
 {
-  static int done = 0;
+  STATIC int done = 0;
   if (!done) {
     tzset ();
     done = 1;
@@ -147,11 +225,70 @@ GetErrno(void)
   return errno;
 }
 
+
 extern EFI_STATUS
 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
 
 EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
+#if defined(__APPLE__) || defined(MDE_CPU_X64)
+//
+// Mac OS X requires the stack to be 16-byte aligned for IA-32. So on an OS X build
+// we add an assembly wrapper that makes sure the stack ges aligned. 
+// This has the nice benfit of being able to run EFI ABI code, like the EFI shell
+// that is checked in to source control in the OS X version of the emulator
+//
+  GasketmsSleep, /* Sleep */
+  Gasketexit, /* Exit */
+  GasketSetTimer,
+  GasketGetLocalTime,
+  Gasketgmtime,
+  GasketGetTimeZone,
+  GasketGetDayLight,
+  Gasketpoll,
+  Gasketread,
+  Gasketwrite,
+  Gasketgetenv,
+  Gasketopen,
+  Gasketlseek,
+  Gasketftruncate,
+  Gasketclose,
+  Gasketmkdir,
+  Gasketrmdir,
+  Gasketunlink,
+  GasketGetErrno,
+  Gasketopendir,
+  Gasketrewinddir,
+  Gasketreaddir,
+  Gasketclosedir,
+  Gasketstat,
+  Gasketstatfs,
+  Gasketrename,
+  Gasketmktime,
+  Gasketfsync,
+  Gasketchmod,
+  Gasketutime,
+  Gaskettcflush,
+  GasketUgaCreate,
+  Gasketperror,
+  Gasketioctl,
+  Gasketfcntl,
+  Gasketcfsetispeed,
+  Gasketcfsetospeed,
+  Gaskettcgetattr,
+  Gaskettcsetattr,
+  GasketUnixPeCoffGetEntryPoint,                
+  GasketUnixPeCoffRelocateImageExtraAction,     
+  GasketUnixPeCoffUnloadImageExtraAction,  
+  
+  GasketUnixEnableInterrupt,
+  GasketUnixDisableInterrupt,
+
+  Gasketgetifaddrs,
+  Gasketfreeifaddrs,
+  Gasketsocket,
+
+#else
   msSleep, /* Sleep */
   exit, /* Exit */
   SetTimer,
@@ -164,8 +301,8 @@ EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   (UnixWrite)write,
   getenv,
   (UnixOpen)open,
-  lseek,
-  ftruncate,
+  (UnixSeek)lseek,
+  (UnixFtruncate)ftruncate,
   close,
   mkdir,
   rmdir,
@@ -175,7 +312,7 @@ EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   rewinddir,
   readdir,
   closedir,
-  stat,
+  (UnixStat)stat,
   statfs,
   rename,
   mktime,
@@ -190,7 +327,16 @@ EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   cfsetispeed,
   cfsetospeed,
   tcgetattr,
-  tcsetattr
+  tcsetattr,
+  SecPeCoffGetEntryPoint,
+  SecPeCoffRelocateImageExtraAction,
+  SecPeCoffLoaderUnloadImageExtraAction,
+  UnixEnableInterrupt,
+  UnixDisableInterrupt,
+  getifaddrs,
+  freeifaddrs,
+  socket
+#endif
 };