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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/** @file
This module contains EBC support routines that are customized based on
the target x64 processor.
Copyright (c) 2006 - 2011, Intel Corporation. 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 "EbcInt.h"
#include "EbcExecute.h"
//
// NOTE: This is the stack size allocated for the interpreter
// when it executes an EBC image. The requirements can change
// based on whether or not a debugger is present, and other
// platform-specific configurations.
//
#define VM_STACK_SIZE (1024 * 8)
#define EBC_THUNK_SIZE 64
#define STACK_REMAIN_SIZE (1024 * 4)
/**
Begin executing an EBC image.
This is used for Ebc Thunk call.
@return The value returned by the EBC application we're going to run.
**/
UINT64
EFIAPI
EbcLLEbcInterpret (
VOID
);
/**
Begin executing an EBC image.
This is used for Ebc image entrypoint.
@return The value returned by the EBC application we're going to run.
**/
UINT64
EFIAPI
EbcLLExecuteEbcImageEntryPoint (
VOID
);
/**
Pushes a 64 bit unsigned value to the VM stack.
@param VmPtr The pointer to current VM context.
@param Arg The value to be pushed.
**/
VOID
PushU64 (
IN VM_CONTEXT *VmPtr,
IN UINT64 Arg
)
{
//
// Advance the VM stack down, and then copy the argument to the stack.
// Hope it's aligned.
//
VmPtr->Gpr[0] -= sizeof (UINT64);
*(UINT64 *) VmPtr->Gpr[0] = Arg;
return;
}
/**
Begin executing an EBC image.
This is a thunk function. Microsoft x64 compiler only provide fast_call
calling convention, so the first four arguments are passed by rcx, rdx,
r8, and r9, while other arguments are passed in stack.
@param EntryPoint The entrypoint of EBC code.
@param Arg1 The 1st argument.
@param Arg2 The 2nd argument.
@param Arg3 The 3rd argument.
@param Arg4 The 4th argument.
@param Arg5 The 5th argument.
@param Arg6 The 6th argument.
@param Arg7 The 7th argument.
@param Arg8 The 8th argument.
@param Arg9 The 9th argument.
@param Arg10 The 10th argument.
@param Arg11 The 11th argument.
@param Arg12 The 12th argument.
@param Arg13 The 13th argument.
@param Arg14 The 14th argument.
@param Arg15 The 15th argument.
@param Arg16 The 16th argument.
@return The value returned by the EBC application we're going to run.
**/
UINT64
EFIAPI
EbcInterpret (
IN UINTN EntryPoint,
IN UINTN Arg1,
IN UINTN Arg2,
IN UINTN Arg3,
IN UINTN Arg4,
IN UINTN Arg5,
IN UINTN Arg6,
IN UINTN Arg7,
IN UINTN Arg8,
IN UINTN Arg9,
IN UINTN Arg10,
IN UINTN Arg11,
IN UINTN Arg12,
IN UINTN Arg13,
IN UINTN Arg14,
IN UINTN Arg15,
IN UINTN Arg16
)
{
//
// Create a new VM context on the stack
//
VM_CONTEXT VmContext;
UINTN Addr;
EFI_STATUS Status;
UINTN StackIndex;
//
// Get the EBC entry point
//
Addr = EntryPoint;
//
// Now clear out our context
//
ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));
//
// Set the VM instruction pointer to the correct location in memory.
//
VmContext.Ip = (VMIP) Addr;
//
// Initialize the stack pointer for the EBC. Get the current system stack
// pointer and adjust it down by the max needed for the interpreter.
//
//
// Adjust the VM's stack pointer down.
//
Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);
if (EFI_ERROR(Status)) {
return Status;
}
VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);
VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);
VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];
VmContext.Gpr[0] -= sizeof (UINTN);
//
// Align the stack on a natural boundary.
//
VmContext.Gpr[0] &= ~(sizeof (UINTN) - 1);
//
// Put a magic value in the stack gap, then adjust down again.
//
*(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;
VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];
//
// The stack upper to LowStackTop is belong to the VM.
//
VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];
//
// For the worst case, assume there are 4 arguments passed in registers, store
// them to VM's stack.
//
PushU64 (&VmContext, (UINT64) Arg16);
PushU64 (&VmContext, (UINT64) Arg15);
PushU64 (&VmContext, (UINT64) Arg14);
PushU64 (&VmContext, (UINT64) Arg13);
PushU64 (&VmContext, (UINT64) Arg12);
PushU64 (&VmContext, (UINT64) Arg11);
PushU64 (&VmContext, (UINT64) Arg10);
PushU64 (&VmContext, (UINT64) Arg9);
PushU64 (&VmContext, (UINT64) Arg8);
PushU64 (&VmContext, (UINT64) Arg7);
PushU64 (&VmContext, (UINT64) Arg6);
PushU64 (&VmContext, (UINT64) Arg5);
PushU64 (&VmContext, (UINT64) Arg4);
PushU64 (&VmContext, (UINT64) Arg3);
PushU64 (&VmContext, (UINT64) Arg2);
PushU64 (&VmContext, (UINT64) Arg1);
//
// Interpreter assumes 64-bit return address is pushed on the stack.
// The x64 does not do this so pad the stack accordingly.
//
PushU64 (&VmContext, (UINT64) 0);
PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);
//
// For x64, this is where we say our return address is
//
VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];
//
// We need to keep track of where the EBC stack starts. This way, if the EBC
// accesses any stack variables above its initial stack setting, then we know
// it's accessing variables passed into it, which means the data is on the
// VM's stack.
// When we're called, on the stack (high to low) we have the parameters, the
// return address, then the saved ebp. Save the pointer to the return address.
// EBC code knows that's there, so should look above it for function parameters.
// The offset is the size of locals (VMContext + Addr + saved ebp).
// Note that the interpreter assumes there is a 16 bytes of return address on
// the stack too, so adjust accordingly.
// VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
//
//
// Begin executing the EBC code
//
EbcExecute (&VmContext);
//
// Return the value in R[7] unless there was an error
//
ReturnEBCStack(StackIndex);
return (UINT64) VmContext.Gpr[7];
}
/**
Begin executing an EBC image.
@param EntryPoint The entrypoint of EBC code.
@param ImageHandle image handle for the EBC application we're executing
@param SystemTable standard system table passed into an driver's entry
point
@return The value returned by the EBC application we're going to run.
**/
UINT64
EFIAPI
ExecuteEbcImageEntryPoint (
IN UINTN EntryPoint,
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
//
// Create a new VM context on the stack
//
VM_CONTEXT VmContext;
UINTN Addr;
EFI_STATUS Status;
UINTN StackIndex;
//
// Get the EBC entry point
//
Addr = EntryPoint;
//
// Now clear out our context
//
ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));
//
// Save the image handle so we can track the thunks created for this image
//
VmContext.ImageHandle = ImageHandle;
VmContext.SystemTable = SystemTable;
//
// Set the VM instruction pointer to the correct location in memory.
//
VmContext.Ip = (VMIP) Addr;
//
// Initialize the stack pointer for the EBC. Get the current system stack
// pointer and adjust it down by the max needed for the interpreter.
//
Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);
if (EFI_ERROR(Status)) {
return Status;
}
VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);
VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);
VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];
VmContext.Gpr[0] -= sizeof (UINTN);
//
// Put a magic value in the stack gap, then adjust down again
//
*(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;
VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];
//
// Align the stack on a natural boundary
VmContext.Gpr[0] &= ~(sizeof(UINTN) - 1);
//
VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];
//
// Simply copy the image handle and system table onto the EBC stack.
// Greatly simplifies things by not having to spill the args.
//
PushU64 (&VmContext, (UINT64) SystemTable);
PushU64 (&VmContext, (UINT64) ImageHandle);
//
// VM pushes 16-bytes for return address. Simulate that here.
//
PushU64 (&VmContext, (UINT64) 0);
PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);
//
// For x64, this is where we say our return address is
//
VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];
//
// Entry function needn't access high stack context, simply
// put the stack pointer here.
//
//
// Begin executing the EBC code
//
EbcExecute (&VmContext);
//
// Return the value in R[7] unless there was an error
//
ReturnEBCStack(StackIndex);
return (UINT64) VmContext.Gpr[7];
}
/**
Create thunks for an EBC image entry point, or an EBC protocol service.
@param ImageHandle Image handle for the EBC image. If not null, then
we're creating a thunk for an image entry point.
@param EbcEntryPoint Address of the EBC code that the thunk is to call
@param Thunk Returned thunk we create here
@param Flags Flags indicating options for creating the thunk
@retval EFI_SUCCESS The thunk was created successfully.
@retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
aligned.
@retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
Thunk.
@retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
**/
EFI_STATUS
EbcCreateThunks (
IN EFI_HANDLE ImageHandle,
IN VOID *EbcEntryPoint,
OUT VOID **Thunk,
IN UINT32 Flags
)
{
UINT8 *Ptr;
UINT8 *ThunkBase;
UINT32 Index;
UINT64 Addr;
INT32 Size;
INT32 ThunkSize;
//
// Check alignment of pointer to EBC code
//
if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {
return EFI_INVALID_PARAMETER;
}
Size = EBC_THUNK_SIZE;
ThunkSize = Size;
Ptr = AllocatePool (Size);
if (Ptr == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);
//
// Save the start address so we can add a pointer to it to a list later.
//
ThunkBase = Ptr;
//
// Give them the address of our buffer we're going to fix up
//
*Thunk = (VOID *) Ptr;
//
// Add a magic code here to help the VM recognize the thunk..
// mov rax, ca112ebccall2ebch => 48 B8 BC 2E 11 CA BC 2E 11 CA
//
*Ptr = 0x48;
Ptr++;
Size--;
*Ptr = 0xB8;
Ptr++;
Size--;
Addr = (UINT64) 0xCA112EBCCA112EBCULL;
for (Index = 0; Index < sizeof (Addr); Index++) {
*Ptr = (UINT8) (UINTN) Addr;
Addr >>= 8;
Ptr++;
Size--;
}
//
// Add code bytes to load up a processor register with the EBC entry point.
// mov r10, 123456789abcdef0h => 49 BA F0 DE BC 9A 78 56 34 12
// The first 8 bytes of the thunk entry is the address of the EBC
// entry point.
//
*Ptr = 0x49;
Ptr++;
Size--;
*Ptr = 0xBA;
Ptr++;
Size--;
Addr = (UINT64) EbcEntryPoint;
for (Index = 0; Index < sizeof (Addr); Index++) {
*Ptr = (UINT8) (UINTN) Addr;
Addr >>= 8;
Ptr++;
Size--;
}
//
// Stick in a load of ecx with the address of appropriate VM function.
// Using r11 because it's a volatile register and won't be used in this
// point.
// mov r11 123456789abcdef0h => 49 BB F0 DE BC 9A 78 56 34 12
//
if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {
Addr = (UINTN) EbcLLExecuteEbcImageEntryPoint;
} else {
Addr = (UINTN) EbcLLEbcInterpret;
}
//
// mov r11 Addr => 0x49 0xBB
//
*Ptr = 0x49;
Ptr++;
Size--;
*Ptr = 0xBB;
Ptr++;
Size--;
for (Index = 0; Index < sizeof (Addr); Index++) {
*Ptr = (UINT8) Addr;
Addr >>= 8;
Ptr++;
Size--;
}
//
// Stick in jump opcode bytes for jmp r11 => 0x41 0xFF 0xE3
//
*Ptr = 0x41;
Ptr++;
Size--;
*Ptr = 0xFF;
Ptr++;
Size--;
*Ptr = 0xE3;
Size--;
//
// Double check that our defined size is ok (application error)
//
if (Size < 0) {
ASSERT (FALSE);
return EFI_BUFFER_TOO_SMALL;
}
//
// Add the thunk to the list for this image. Do this last since the add
// function flushes the cache for us.
//
EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);
return EFI_SUCCESS;
}
/**
This function is called to execute an EBC CALLEX instruction.
The function check the callee's content to see whether it is common native
code or a thunk to another piece of EBC code.
If the callee is common native code, use EbcLLCAllEXASM to manipulate,
otherwise, set the VM->IP to target EBC code directly to avoid another VM
be startup which cost time and stack space.
@param VmPtr Pointer to a VM context.
@param FuncAddr Callee's address
@param NewStackPointer New stack pointer after the call
@param FramePtr New frame pointer after the call
@param Size The size of call instruction
**/
VOID
EbcLLCALLEX (
IN VM_CONTEXT *VmPtr,
IN UINTN FuncAddr,
IN UINTN NewStackPointer,
IN VOID *FramePtr,
IN UINT8 Size
)
{
UINTN IsThunk;
UINTN TargetEbcAddr;
IsThunk = 1;
TargetEbcAddr = 0;
//
// Processor specific code to check whether the callee is a thunk to EBC.
//
if (*((UINT8 *)FuncAddr) != 0x48) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 1) != 0xB8) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 2) != 0xBC) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 3) != 0x2E) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 4) != 0x11) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 5) != 0xCA) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 6) != 0xBC) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 7) != 0x2E) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 8) != 0x11) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 9) != 0xCA) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 10) != 0x49) {
IsThunk = 0;
goto Action;
}
if (*((UINT8 *)FuncAddr + 11) != 0xBA) {
IsThunk = 0;
goto Action;
}
CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + 12, 8);
Action:
if (IsThunk == 1){
//
// The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
// put our return address and frame pointer on the VM stack.
// Then set the VM's IP to new EBC code.
//
VmPtr->Gpr[0] -= 8;
VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], (UINTN) FramePtr);
VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->Gpr[0];
VmPtr->Gpr[0] -= 8;
VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], (UINT64) (VmPtr->Ip + Size));
VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;
} else {
//
// The callee is not a thunk to EBC, call native code,
// and get return value.
//
VmPtr->Gpr[7] = EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);
//
// Advance the IP.
//
VmPtr->Ip += Size;
}
}
|