summaryrefslogtreecommitdiff
path: root/InOsEmuPkg/Unix/Sec/EmuThunk.c
blob: fe68603d3f678e3660040de9f6bbee9c899c6b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*++ @file
  Since the SEC is the only program in our emulation we 
  must use a UEFI/PI mechanism to export APIs to other modules.
  This is the role of the EFI_EMU_THUNK_PROTOCOL.

  The mUnixThunkTable exists so that a change to EFI_EMU_THUNK_PROTOCOL
  will cause an error in initializing the array if all the member functions
  are not added. It looks like adding a element to end and not initializing
  it may cause the table to be initaliized with the members at the end being
  set to zero. This is bad as jumping to zero will crash.

Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
Portions copyright (c) 2008 - 2011, 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                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

**/

#include "SecMain.h"

#ifdef __APPLE__
#define DebugAssert _Mangle__DebugAssert

#include <assert.h>
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>

#undef DebugAssert
#endif

int settimer_initialized;
struct timeval settimer_timeval;
void (*settimer_callback)(UINT64 delta);

BOOLEAN gEmulatorInterruptEnabled = FALSE;


UINTN
SecWriteStdErr (
  IN UINT8     *Buffer,
  IN UINTN     NumberOfBytes
  )
{
  ssize_t Return;
  
  Return = write (STDERR_FILENO, (const void *)Buffer, (size_t)NumberOfBytes);
  
  return (Return == -1) ? 0 : Return;
}


EFI_STATUS
SecConfigStdIn (
  VOID
  )
{
  struct termios tty;
  
  //
  // Need to turn off line buffering, ECHO, and make it unbuffered.
  //
  tcgetattr (STDIN_FILENO, &tty);
  tty.c_lflag &= ~(ICANON | ECHO);
  tcsetattr (STDIN_FILENO, TCSANOW, &tty);
  
//  setvbuf (STDIN_FILENO, NULL, _IONBF, 0);
  
  // now ioctl FIONREAD will do what we need
  return EFI_SUCCESS;
}

UINTN
SecWriteStdOut (
  IN UINT8     *Buffer,
  IN UINTN     NumberOfBytes
  )
{
  ssize_t Return;
  
  Return = write (STDOUT_FILENO, (const void *)Buffer, (size_t)NumberOfBytes);
  
  return (Return == -1) ? 0 : Return;
}

UINTN
SecReadStdIn (
  IN UINT8     *Buffer,
  IN UINTN     NumberOfBytes
  )
{
  ssize_t Return;
  
  Return = read (STDIN_FILENO, Buffer, (size_t)NumberOfBytes);
  
  return (Return == -1) ? 0 : Return;
}

BOOLEAN
SecPollStdIn (
  VOID
  )
{
  int Result;
  int Bytes;
  
  Result = ioctl (STDIN_FILENO, FIONREAD, &Bytes);
  if (Result == -1) {
    return FALSE;
  }
  
  return (BOOLEAN)(Bytes > 0);
}


VOID *
SecMalloc (
  IN  UINTN Size
  )
{
  return malloc ((size_t)Size);
}

VOID *
SecValloc (
  IN  UINTN Size
  )
{
  return valloc ((size_t)Size);
}

BOOLEAN
SecFree (
  IN  VOID *Ptr
  )
{
  if (EfiSystemMemoryRange (Ptr)) {
    // If an address range is in the EFI memory map it was alloced via EFI.
    // So don't free those ranges and let the caller know.
    return FALSE;
  }
  
  free (Ptr);
  return TRUE;
}


void
settimer_handler (int sig)
{
  struct timeval timeval;
  UINT64 delta;

  gettimeofday (&timeval, NULL);
  delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
    - ((UINT64)settimer_timeval.tv_sec * 1000) 
    - (settimer_timeval.tv_usec / 1000);
  settimer_timeval = timeval;
  
  if (settimer_callback) {
    ReverseGasketUint64 (settimer_callback, delta);
  }
}

VOID
SecSetTimer (
  IN  UINT64                  PeriodMs,
  IN  EMU_SET_TIMER_CALLBACK  CallBack
  )
{
  struct itimerval timerval;
  UINT32 remainder;

  if (!settimer_initialized) {
    struct sigaction act;

    settimer_initialized = 1;
    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));
    }
    if (gettimeofday (&settimer_timeval, NULL) != 0) {
      printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
    }
  }
  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) {
    printf ("SetTimer: setitimer error %s\n", strerror (errno));
  }
  settimer_callback = CallBack;
}


VOID
SecEnableInterrupt (
  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);
  pthread_sigmask (SIG_UNBLOCK, &sigset, NULL);
}


VOID
SecDisableInterrupt (
  VOID
  )
{
  sigset_t  sigset;

  // Since SetTimer() uses SIGALRM we emulate turning on and off interrupts 
  // by enabling/disabling SIGALRM.
  sigemptyset (&sigset);
  sigaddset (&sigset, SIGALRM);
  pthread_sigmask (SIG_BLOCK, &sigset, NULL);
  gEmulatorInterruptEnabled = FALSE;
}


BOOLEAN
SecInterruptEanbled (void)
{
  return gEmulatorInterruptEnabled;
}


UINT64
QueryPerformanceFrequency (
  VOID
  )
{
  // Hard code to nanoseconds
  return 1000000000ULL;
}

UINT64
QueryPerformanceCounter (
  VOID
  )
{
#if __APPLE__
  UINT64          Start;
  Nanoseconds     elapsedNano;

  Start = mach_absolute_time ();
  
  // Convert to nanoseconds.

  // Have to do some pointer fun because AbsoluteToNanoseconds 
  // works in terms of UnsignedWide, which is a structure rather 
    // than a proper 64-bit integer.
  elapsedNano = AbsoluteToNanoseconds (*(AbsoluteTime *) &Start);

  return *(uint64_t *) &elapsedNano;
#else
  // Need to figure out what to do for Linux?
  return 0;
#endif
}
  


VOID
SecSleep (
  IN  UINT64 Nanoseconds
  )
{
  struct timespec rq, rm;
  struct timeval  start, end;
  unsigned long  MicroSec;
  
  rq.tv_sec  = DivU64x32 (Nanoseconds, 1000000000);
  rq.tv_nsec = ModU64x32 (Nanoseconds, 1000000000);

  //
  // 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 (&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
SecCpuSleep (
  VOID
  )
{
  struct timespec rq, rm;

  // nanosleep gets interrupted by the timer tic
  rq.tv_sec  = 1;
  rq.tv_nsec = 0;
  
  nanosleep (&rq, &rm);
}


VOID
SecExit (
  UINTN   Status
  )
{
  exit (Status);
}


VOID
SecGetTime (
  OUT  EFI_TIME               *Time,
  OUT EFI_TIME_CAPABILITIES   *Capabilities OPTIONAL
  )
{
  struct tm *tm;
  time_t t;

  t = time (NULL);
  tm = localtime (&t);

  Time->Year = 1900 + tm->tm_year;
  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->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
    | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
    
  if (Capabilities != NULL) {
    Capabilities->Resolution  = 1;
    Capabilities->Accuracy    = 50000000;
    Capabilities->SetsToZero  = FALSE;
  }
}



VOID
SecSetTime (
  IN  EFI_TIME               *Time
  )
{
  // Don't change the time on the system
  // We could save delta to localtime() and have SecGetTime adjust return values?
  return;
}


EFI_STATUS
SecGetNextProtocol (
  IN  BOOLEAN                 EmuBusDriver,
  OUT EMU_IO_THUNK_PROTOCOL   **Instance   OPTIONAL
  )
{
  return GetNextThunkProtocol (EmuBusDriver, Instance);
}


EMU_THUNK_PROTOCOL gEmuThunkProtocol = {
  GasketSecWriteStdErr,
  GasketSecConfigStdIn,
  GasketSecWriteStdOut,
  GasketSecReadStdIn,
  GasketSecPollStdIn,
  GasketSecMalloc,
  GasketSecValloc,
  GasketSecFree,
  GasketSecPeCoffGetEntryPoint,
  GasketSecPeCoffRelocateImageExtraAction,
  GasketSecPeCoffUnloadImageExtraAction,
  GasketSecEnableInterrupt,
  GasketSecDisableInterrupt,
  GasketQueryPerformanceFrequency,
  GasketQueryPerformanceCounter,
  GasketSecSleep,
  GasketSecCpuSleep,
  GasketSecExit,
  GasketSecGetTime,                
  GasketSecSetTime,
  GasketSecSetTimer,  
  GasketSecGetNextProtocol
};


VOID
SecInitThunkProtocol (
  VOID
  )
{
  // timezone and daylight lib globals depend on tzset be called 1st.
  tzset ();
}