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