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