]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/EbcDxe/Ia32/EbcSupport.c
Added incremental support in GenFds
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / Ia32 / EbcSupport.c
CommitLineData
fb0b259e 1/** @file\r
53c71d09 2 This module contains EBC support routines that are customized based on\r
3 the target processor.\r
4\r
fb0b259e 5Copyright (c) 2006, Intel Corporation\r
6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
53c71d09 15\r
16#include "EbcInt.h"\r
17#include "EbcExecute.h"\r
18\r
19//\r
20// NOTE: This is the stack size allocated for the interpreter\r
21// when it executes an EBC image. The requirements can change\r
22// based on whether or not a debugger is present, and other\r
23// platform-specific configurations.\r
24//\r
25#define VM_STACK_SIZE (1024 * 4)\r
26#define EBC_THUNK_SIZE 32\r
27\r
28#define STACK_REMAIN_SIZE (1024 * 4)\r
53c71d09 29\r
53c71d09 30\r
fb0b259e 31/**\r
32 This function is called to execute an EBC CALLEX instruction.\r
53c71d09 33 The function check the callee's content to see whether it is common native\r
34 code or a thunk to another piece of EBC code.\r
35 If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
36 otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
37 be startup which cost time and stack space.\r
53c71d09 38\r
fb0b259e 39 @parm VmPtr Pointer to a VM context.\r
40 @parm FuncAddr Callee's address\r
41 @parm NewStackPointer New stack pointer after the call\r
42 @parm FramePtr New frame pointer after the call\r
43 @parm Size The size of call instruction\r
53c71d09 44\r
fb0b259e 45 @return None.\r
53c71d09 46\r
fb0b259e 47**/\r
48VOID\r
49EbcLLCALLEX (\r
50 IN VM_CONTEXT *VmPtr,\r
51 IN UINTN FuncAddr,\r
52 IN UINTN NewStackPointer,\r
53 IN VOID *FramePtr,\r
54 IN UINT8 Size\r
55 )\r
53c71d09 56{\r
57 UINTN IsThunk;\r
58 UINTN TargetEbcAddr;\r
59\r
60 IsThunk = 1;\r
61 TargetEbcAddr = 0;\r
62\r
63 //\r
64 // Processor specific code to check whether the callee is a thunk to EBC.\r
65 //\r
66 if (*((UINT8 *)FuncAddr) != 0xB8) {\r
67 IsThunk = 0;\r
68 goto Action;\r
69 }\r
70 if (*((UINT8 *)FuncAddr + 1) != 0xBC) {\r
71 IsThunk = 0;\r
72 goto Action;\r
73 }\r
74 if (*((UINT8 *)FuncAddr + 2) != 0x2E) {\r
75 IsThunk = 0;\r
76 goto Action;\r
77 }\r
78 if (*((UINT8 *)FuncAddr + 3) != 0x11) {\r
79 IsThunk = 0;\r
80 goto Action;\r
81 }\r
82 if (*((UINT8 *)FuncAddr + 4) != 0xCA) {\r
83 IsThunk = 0;\r
84 goto Action;\r
85 }\r
86 if (*((UINT8 *)FuncAddr + 5) != 0xB8) {\r
87 IsThunk = 0;\r
88 goto Action;\r
89 }\r
90 if (*((UINT8 *)FuncAddr + 10) != 0xB9) {\r
91 IsThunk = 0;\r
92 goto Action;\r
93 }\r
94 if (*((UINT8 *)FuncAddr + 15) != 0xFF) {\r
95 IsThunk = 0;\r
96 goto Action;\r
97 }\r
98 if (*((UINT8 *)FuncAddr + 16) != 0xE1) {\r
99 IsThunk = 0;\r
100 goto Action;\r
101 }\r
102\r
103 TargetEbcAddr = ((UINTN)(*((UINT8 *)FuncAddr + 9)) << 24) + ((UINTN)(*((UINT8 *)FuncAddr + 8)) << 16) +\r
104 ((UINTN)(*((UINT8 *)FuncAddr + 7)) << 8) + ((UINTN)(*((UINT8 *)FuncAddr + 6)));\r
105\r
106Action:\r
107 if (IsThunk == 1){\r
108 //\r
109 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
110 // put our return address and frame pointer on the VM stack.\r
111 // Then set the VM's IP to new EBC code.\r
112 //\r
113 VmPtr->R[0] -= 8;\r
114 VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
115 VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
116 VmPtr->R[0] -= 8;\r
117 VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r
118\r
119 VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
120 } else {\r
121 //\r
122 // The callee is not a thunk to EBC, call native code.\r
123 //\r
124 EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
fb0b259e 125\r
53c71d09 126 //\r
127 // Get return value and advance the IP.\r
128 //\r
129 VmPtr->R[7] = EbcLLGetReturnValue ();\r
130 VmPtr->Ip += Size;\r
131 }\r
132}\r
133\r
fb0b259e 134\r
135/**\r
136 Begin executing an EBC image. The address of the entry point is passed\r
137 in via a processor register, so we'll need to make a call to get the\r
138 value.\r
139\r
140 None. Since we're called from a fixed up thunk (which we want to keep\r
141 small), our only so-called argument is the EBC entry point passed in\r
142 to us in a processor register.\r
143\r
144 @return The value returned by the EBC application we're going to run.\r
145\r
146**/\r
53c71d09 147STATIC\r
148UINT64\r
149EbcInterpret (\r
150 IN OUT UINTN Arg1,\r
151 IN OUT UINTN Arg2,\r
152 IN OUT UINTN Arg3,\r
153 IN OUT UINTN Arg4,\r
154 IN OUT UINTN Arg5,\r
155 IN OUT UINTN Arg6,\r
156 IN OUT UINTN Arg7,\r
157 IN OUT UINTN Arg8,\r
158 IN OUT UINTN Arg9,\r
159 IN OUT UINTN Arg10,\r
160 IN OUT UINTN Arg11,\r
161 IN OUT UINTN Arg12,\r
162 IN OUT UINTN Arg13,\r
163 IN OUT UINTN Arg14,\r
164 IN OUT UINTN Arg15,\r
165 IN OUT UINTN Arg16\r
166 )\r
53c71d09 167{\r
168 //\r
169 // Create a new VM context on the stack\r
170 //\r
171 VM_CONTEXT VmContext;\r
172 UINTN Addr;\r
173 EFI_STATUS Status;\r
174 UINTN StackIndex;\r
175\r
176 //\r
177 // Get the EBC entry point from the processor register.\r
178 //\r
179 Addr = EbcLLGetEbcEntryPoint ();\r
180\r
181 //\r
182 // Now clear out our context\r
183 //\r
184 ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
185\r
186 //\r
187 // Set the VM instruction pointer to the correct location in memory.\r
188 //\r
189 VmContext.Ip = (VMIP) Addr;\r
190 //\r
191 // Initialize the stack pointer for the EBC. Get the current system stack\r
192 // pointer and adjust it down by the max needed for the interpreter.\r
193 //\r
194\r
195 //\r
196 // Align the stack on a natural boundary\r
197 //\r
198\r
199 //\r
200 // Allocate stack pool\r
201 //\r
202 Status = GetEBCStack((EFI_HANDLE)-1, &VmContext.StackPool, &StackIndex);\r
203 if (EFI_ERROR(Status)) {\r
204 return Status;\r
205 }\r
206 VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
207 VmContext.R[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
208 VmContext.HighStackBottom = (UINTN)VmContext.R[0];\r
209 VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
210 VmContext.R[0] -= sizeof (UINTN);\r
211\r
212 //\r
213 // Put a magic value in the stack gap, then adjust down again\r
214 //\r
215 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
216 VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
217 VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
218\r
219 //\r
220 // For IA32, this is where we say our return address is\r
221 //\r
222 VmContext.R[0] -= sizeof (UINTN);\r
223 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg16;\r
224 VmContext.R[0] -= sizeof (UINTN);\r
225 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg15;\r
226 VmContext.R[0] -= sizeof (UINTN);\r
227 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg14;\r
228 VmContext.R[0] -= sizeof (UINTN);\r
229 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg13;\r
230 VmContext.R[0] -= sizeof (UINTN);\r
231 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg12;\r
232 VmContext.R[0] -= sizeof (UINTN);\r
233 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg11;\r
234 VmContext.R[0] -= sizeof (UINTN);\r
235 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg10;\r
236 VmContext.R[0] -= sizeof (UINTN);\r
237 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg9;\r
238 VmContext.R[0] -= sizeof (UINTN);\r
239 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg8;\r
240 VmContext.R[0] -= sizeof (UINTN);\r
241 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg7;\r
242 VmContext.R[0] -= sizeof (UINTN);\r
243 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg6;\r
244 VmContext.R[0] -= sizeof (UINTN);\r
245 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg5;\r
246 VmContext.R[0] -= sizeof (UINTN);\r
247 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg4;\r
248 VmContext.R[0] -= sizeof (UINTN);\r
249 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg3;\r
250 VmContext.R[0] -= sizeof (UINTN);\r
251 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg2;\r
252 VmContext.R[0] -= sizeof (UINTN);\r
253 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg1;\r
254 VmContext.R[0] -= 16;\r
255 VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
256\r
257 //\r
258 // We need to keep track of where the EBC stack starts. This way, if the EBC\r
259 // accesses any stack variables above its initial stack setting, then we know\r
260 // it's accessing variables passed into it, which means the data is on the\r
261 // VM's stack.\r
262 // When we're called, on the stack (high to low) we have the parameters, the\r
263 // return address, then the saved ebp. Save the pointer to the return address.\r
264 // EBC code knows that's there, so should look above it for function parameters.\r
265 // The offset is the size of locals (VMContext + Addr + saved ebp).\r
266 // Note that the interpreter assumes there is a 16 bytes of return address on\r
267 // the stack too, so adjust accordingly.\r
268 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
269 //\r
270\r
271 //\r
272 // Begin executing the EBC code\r
273 //\r
274 EbcExecute (&VmContext);\r
275\r
276 //\r
277 // Return the value in R[7] unless there was an error\r
278 //\r
279 ReturnEBCStack(StackIndex);\r
280 return (UINT64) VmContext.R[7];\r
281}\r
282\r
53c71d09 283\r
fb0b259e 284/**\r
53c71d09 285 Begin executing an EBC image. The address of the entry point is passed\r
286 in via a processor register, so we'll need to make a call to get the\r
287 value.\r
53c71d09 288\r
fb0b259e 289 @param ImageHandle image handle for the EBC application we're executing\r
290 @param SystemTable standard system table passed into an driver's entry point\r
53c71d09 291\r
fb0b259e 292 @return The value returned by the EBC application we're going to run.\r
53c71d09 293\r
fb0b259e 294**/\r
295STATIC\r
296UINT64\r
297ExecuteEbcImageEntryPoint (\r
298 IN EFI_HANDLE ImageHandle,\r
299 IN EFI_SYSTEM_TABLE *SystemTable\r
300 )\r
53c71d09 301{\r
302 //\r
303 // Create a new VM context on the stack\r
304 //\r
305 VM_CONTEXT VmContext;\r
306 UINTN Addr;\r
307 EFI_STATUS Status;\r
308 UINTN StackIndex;\r
309\r
310 //\r
311 // Get the EBC entry point from the processor register. Make sure you don't\r
312 // call any functions before this or you could mess up the register the\r
313 // entry point is passed in.\r
314 //\r
315 Addr = EbcLLGetEbcEntryPoint ();\r
316\r
317 //\r
318 // Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);\r
319 // Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);\r
320 //\r
321 // Now clear out our context\r
322 //\r
323 ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
324\r
325 //\r
326 // Save the image handle so we can track the thunks created for this image\r
327 //\r
328 VmContext.ImageHandle = ImageHandle;\r
329 VmContext.SystemTable = SystemTable;\r
330\r
331 //\r
332 // Set the VM instruction pointer to the correct location in memory.\r
333 //\r
334 VmContext.Ip = (VMIP) Addr;\r
335\r
336 //\r
337 // Initialize the stack pointer for the EBC. Get the current system stack\r
338 // pointer and adjust it down by the max needed for the interpreter.\r
339 //\r
340\r
341 //\r
342 // Allocate stack pool\r
343 //\r
344 Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
345 if (EFI_ERROR(Status)) {\r
346 return Status;\r
347 }\r
348 VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
349 VmContext.R[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
350 VmContext.HighStackBottom = (UINTN)VmContext.R[0];\r
351 VmContext.R[0] -= sizeof (UINTN);\r
fb0b259e 352\r
53c71d09 353 //\r
354 // Put a magic value in the stack gap, then adjust down again\r
355 //\r
356 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
357 VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
358\r
359 //\r
360 // Align the stack on a natural boundary\r
361 // VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
362 //\r
363 VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
364 VmContext.R[0] -= sizeof (UINTN);\r
365 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) SystemTable;\r
366 VmContext.R[0] -= sizeof (UINTN);\r
367 *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) ImageHandle;\r
368\r
fb0b259e 369 VmContext.R[0] -= 16;\r
53c71d09 370 VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
371 //\r
372 // VM pushes 16-bytes for return address. Simulate that here.\r
373 //\r
374\r
375 //\r
376 // Begin executing the EBC code\r
377 //\r
378 EbcExecute (&VmContext);\r
379\r
380 //\r
381 // Return the value in R[7] unless there was an error\r
382 //\r
383 return (UINT64) VmContext.R[7];\r
384}\r
385\r
fb0b259e 386\r
387/**\r
388 Create an IA32 thunk for the given EBC entry point.\r
389\r
390 @param ImageHandle Handle of image for which this thunk is being created\r
391 @param EbcEntryPoint Address of the EBC code that the thunk is to call\r
392 @param Thunk Returned thunk we create here\r
393\r
394 @return Standard EFI status.\r
395\r
396**/\r
53c71d09 397EFI_STATUS\r
398EbcCreateThunks (\r
399 IN EFI_HANDLE ImageHandle,\r
400 IN VOID *EbcEntryPoint,\r
401 OUT VOID **Thunk,\r
402 IN UINT32 Flags\r
403 )\r
53c71d09 404{\r
405 UINT8 *Ptr;\r
406 UINT8 *ThunkBase;\r
407 UINT32 I;\r
408 UINT32 Addr;\r
409 INT32 Size;\r
410 INT32 ThunkSize;\r
411\r
412 //\r
413 // Check alignment of pointer to EBC code\r
414 //\r
415 if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
416 return EFI_INVALID_PARAMETER;\r
417 }\r
418\r
419 Size = EBC_THUNK_SIZE;\r
420 ThunkSize = Size;\r
421\r
422 Ptr = AllocatePool (Size);\r
423\r
424 if (Ptr == NULL) {\r
425 return EFI_OUT_OF_RESOURCES;\r
426 }\r
427 //\r
428 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
429 //\r
430 // Save the start address so we can add a pointer to it to a list later.\r
431 //\r
432 ThunkBase = Ptr;\r
433\r
434 //\r
435 // Give them the address of our buffer we're going to fix up\r
436 //\r
437 *Thunk = (VOID *) Ptr;\r
438\r
439 //\r
440 // Add a magic code here to help the VM recognize the thunk..\r
441 // mov eax, 0xca112ebc => B8 BC 2E 11 CA\r
442 //\r
443 *Ptr = 0xB8;\r
444 Ptr++;\r
445 Size--;\r
446 Addr = (UINT32) 0xCA112EBC;\r
447 for (I = 0; I < sizeof (Addr); I++) {\r
448 *Ptr = (UINT8) (UINTN) Addr;\r
449 Addr >>= 8;\r
450 Ptr++;\r
451 Size--;\r
452 }\r
453\r
454 //\r
455 // Add code bytes to load up a processor register with the EBC entry point.\r
456 // mov eax, 0xaa55aa55 => B8 55 AA 55 AA\r
457 // The first 8 bytes of the thunk entry is the address of the EBC\r
458 // entry point.\r
459 //\r
460 *Ptr = 0xB8;\r
461 Ptr++;\r
462 Size--;\r
463 Addr = (UINT32) EbcEntryPoint;\r
464 for (I = 0; I < sizeof (Addr); I++) {\r
465 *Ptr = (UINT8) (UINTN) Addr;\r
466 Addr >>= 8;\r
467 Ptr++;\r
468 Size--;\r
469 }\r
470 //\r
471 // Stick in a load of ecx with the address of appropriate VM function.\r
472 // mov ecx 12345678h => 0xB9 0x78 0x56 0x34 0x12\r
473 //\r
474 if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
475 Addr = (UINT32) (UINTN) ExecuteEbcImageEntryPoint;\r
476 } else {\r
477 Addr = (UINT32) (UINTN) EbcInterpret;\r
478 }\r
479\r
480 //\r
481 // MOV ecx\r
482 //\r
483 *Ptr = 0xB9;\r
484 Ptr++;\r
485 Size--;\r
486 for (I = 0; I < sizeof (Addr); I++) {\r
487 *Ptr = (UINT8) Addr;\r
488 Addr >>= 8;\r
489 Ptr++;\r
490 Size--;\r
491 }\r
492 //\r
493 // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1\r
494 //\r
495 *Ptr = 0xFF;\r
496 Ptr++;\r
497 Size--;\r
498 *Ptr = 0xE1;\r
499 Size--;\r
500\r
501 //\r
502 // Double check that our defined size is ok (application error)\r
503 //\r
504 if (Size < 0) {\r
505 ASSERT (FALSE);\r
506 return EFI_BUFFER_TOO_SMALL;\r
507 }\r
508 //\r
509 // Add the thunk to the list for this image. Do this last since the add\r
510 // function flushes the cache for us.\r
511 //\r
512 EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
513\r
514 return EFI_SUCCESS;\r
515}\r