2 Contains code that implements the virtual machine.
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "EbcExecute.h"
20 // Define some useful data size constants to allow switch statements based on
21 // size of operands or data.
23 #define DATA_SIZE_INVALID 0
25 #define DATA_SIZE_16 2
26 #define DATA_SIZE_32 4
27 #define DATA_SIZE_64 8
28 #define DATA_SIZE_N 48 // 4 or 8
30 // Structure we'll use to dispatch opcodes to execute functions.
33 EFI_STATUS (*ExecuteFunction
) (IN VM_CONTEXT
* VmPtr
);
39 (*DATA_MANIP_EXEC_FUNCTION
) (
40 IN VM_CONTEXT
* VmPtr
,
46 Decode a 16-bit index to determine the offset. Given an index value:
49 b14:12 - number of bits in this index assigned to natural units (=a)
50 ba:11 - constant units = ConstUnits
51 b0:a - natural units = NaturalUnits
53 Given this info, the offset can be computed by:
54 offset = sign_bit * (ConstUnits + NaturalUnits * sizeof(UINTN))
56 Max offset is achieved with index = 0x7FFF giving an offset of
57 0x27B (32-bit machine) or 0x477 (64-bit machine).
58 Min offset is achieved with index =
60 @param VmPtr A pointer to VM context.
61 @param CodeOffset Offset from IP of the location of the 16-bit index
64 @return The decoded offset.
74 Decode a 32-bit index to determine the offset.
76 @param VmPtr A pointer to VM context.
77 @param CodeOffset Offset from IP of the location of the 32-bit index
80 @return Converted index per EBC VM specification.
90 Decode a 64-bit index to determine the offset.
92 @param VmPtr A pointer to VM context.s
93 @param CodeOffset Offset from IP of the location of the 64-bit index
96 @return Converted index per EBC VM specification
101 IN VM_CONTEXT
*VmPtr
,
106 Reads 8-bit data form the memory address.
108 @param VmPtr A pointer to VM context.
109 @param Addr The memory address.
111 @return The 8-bit value from the memory address.
116 IN VM_CONTEXT
*VmPtr
,
121 Reads 16-bit data form the memory address.
123 @param VmPtr A pointer to VM context.
124 @param Addr The memory address.
126 @return The 16-bit value from the memory address.
131 IN VM_CONTEXT
*VmPtr
,
136 Reads 32-bit data form the memory address.
138 @param VmPtr A pointer to VM context.
139 @param Addr The memory address.
141 @return The 32-bit value from the memory address.
146 IN VM_CONTEXT
*VmPtr
,
151 Reads 64-bit data form the memory address.
153 @param VmPtr A pointer to VM context.
154 @param Addr The memory address.
156 @return The 64-bit value from the memory address.
161 IN VM_CONTEXT
*VmPtr
,
166 Read a natural value from memory. May or may not be aligned.
168 @param VmPtr current VM context
169 @param Addr the address to read from
171 @return The natural value at address Addr.
176 IN VM_CONTEXT
*VmPtr
,
181 Writes 8-bit data to memory address.
183 This routine is called by the EBC data
184 movement instructions that write to memory. Since these writes
185 may be to the stack, which looks like (high address on top) this,
187 [EBC entry point arguments]
191 we need to detect all attempts to write to the EBC entry point argument
192 stack area and adjust the address (which will initially point into the
193 VM stack) to point into the EBC entry point arguments.
195 @param VmPtr A pointer to a VM context.
196 @param Addr Address to write to.
197 @param Data Value to write to Addr.
199 @retval EFI_SUCCESS The instruction is executed successfully.
200 @retval Other Some error occurs when writing data to the address.
205 IN VM_CONTEXT
*VmPtr
,
211 Writes 16-bit data to memory address.
213 This routine is called by the EBC data
214 movement instructions that write to memory. Since these writes
215 may be to the stack, which looks like (high address on top) this,
217 [EBC entry point arguments]
221 we need to detect all attempts to write to the EBC entry point argument
222 stack area and adjust the address (which will initially point into the
223 VM stack) to point into the EBC entry point arguments.
225 @param VmPtr A pointer to a VM context.
226 @param Addr Address to write to.
227 @param Data Value to write to Addr.
229 @retval EFI_SUCCESS The instruction is executed successfully.
230 @retval Other Some error occurs when writing data to the address.
235 IN VM_CONTEXT
*VmPtr
,
241 Writes 32-bit data to memory address.
243 This routine is called by the EBC data
244 movement instructions that write to memory. Since these writes
245 may be to the stack, which looks like (high address on top) this,
247 [EBC entry point arguments]
251 we need to detect all attempts to write to the EBC entry point argument
252 stack area and adjust the address (which will initially point into the
253 VM stack) to point into the EBC entry point arguments.
255 @param VmPtr A pointer to a VM context.
256 @param Addr Address to write to.
257 @param Data Value to write to Addr.
259 @retval EFI_SUCCESS The instruction is executed successfully.
260 @retval Other Some error occurs when writing data to the address.
265 IN VM_CONTEXT
*VmPtr
,
271 Reads 16-bit unsigned data from the code stream.
273 This routine provides the ability to read raw unsigned data from the code
276 @param VmPtr A pointer to VM context
277 @param Offset Offset from current IP to the raw data to read.
279 @return The raw unsigned 16-bit value from the code stream.
284 IN VM_CONTEXT
*VmPtr
,
289 Reads 32-bit unsigned data from the code stream.
291 This routine provides the ability to read raw unsigned data from the code
294 @param VmPtr A pointer to VM context
295 @param Offset Offset from current IP to the raw data to read.
297 @return The raw unsigned 32-bit value from the code stream.
302 IN VM_CONTEXT
*VmPtr
,
307 Reads 64-bit unsigned data from the code stream.
309 This routine provides the ability to read raw unsigned data from the code
312 @param VmPtr A pointer to VM context
313 @param Offset Offset from current IP to the raw data to read.
315 @return The raw unsigned 64-bit value from the code stream.
320 IN VM_CONTEXT
*VmPtr
,
325 Reads 8-bit immediate value at the offset.
327 This routine is called by the EBC execute
328 functions to read EBC immediate values from the code stream.
329 Since we can't assume alignment, each tries to read in the biggest
330 chunks size available, but will revert to smaller reads if necessary.
332 @param VmPtr A pointer to a VM context.
333 @param Offset offset from IP of the code bytes to read.
335 @return Signed data of the requested size from the specified address.
340 IN VM_CONTEXT
*VmPtr
,
345 Reads 16-bit immediate value at the offset.
347 This routine is called by the EBC execute
348 functions to read EBC immediate values from the code stream.
349 Since we can't assume alignment, each tries to read in the biggest
350 chunks size available, but will revert to smaller reads if necessary.
352 @param VmPtr A pointer to a VM context.
353 @param Offset offset from IP of the code bytes to read.
355 @return Signed data of the requested size from the specified address.
360 IN VM_CONTEXT
*VmPtr
,
365 Reads 32-bit immediate value at the offset.
367 This routine is called by the EBC execute
368 functions to read EBC immediate values from the code stream.
369 Since we can't assume alignment, each tries to read in the biggest
370 chunks size available, but will revert to smaller reads if necessary.
372 @param VmPtr A pointer to a VM context.
373 @param Offset offset from IP of the code bytes to read.
375 @return Signed data of the requested size from the specified address.
380 IN VM_CONTEXT
*VmPtr
,
385 Reads 64-bit immediate value at the offset.
387 This routine is called by the EBC execute
388 functions to read EBC immediate values from the code stream.
389 Since we can't assume alignment, each tries to read in the biggest
390 chunks size available, but will revert to smaller reads if necessary.
392 @param VmPtr A pointer to a VM context.
393 @param Offset offset from IP of the code bytes to read.
395 @return Signed data of the requested size from the specified address.
400 IN VM_CONTEXT
*VmPtr
,
405 Given an address that EBC is going to read from or write to, return
406 an appropriate address that accounts for a gap in the stack.
407 The stack for this application looks like this (high addr on top)
408 [EBC entry point arguments]
411 The EBC assumes that its arguments are at the top of its stack, which
412 is where the VM stack is really. Therefore if the EBC does memory
413 accesses into the VM stack area, then we need to convert the address
414 to point to the EBC entry point arguments area. Do this here.
416 @param VmPtr A Pointer to VM context.
417 @param Addr Address of interest
419 @return The unchanged address if it's not in the VM stack region. Otherwise,
420 adjust for the stack gap and return the modified address.
425 IN VM_CONTEXT
*VmPtr
,
430 Execute all the EBC data manipulation instructions.
431 Since the EBC data manipulation instructions all have the same basic form,
432 they can share the code that does the fetch of operands and the write-back
433 of the result. This function performs the fetch of the operands (even if
434 both are not needed to be fetched, like NOT instruction), dispatches to the
435 appropriate subfunction, then writes back the returned result.
438 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
440 @param VmPtr A pointer to VM context.
441 @param IsSignedOp Indicates whether the operand is signed or not.
443 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
444 @retval EFI_SUCCESS The instruction is executed successfully.
449 IN VM_CONTEXT
*VmPtr
,
450 IN BOOLEAN IsSignedOp
454 // Functions that execute VM opcodes
457 Execute the EBC BREAK instruction.
459 @param VmPtr A pointer to a VM context.
461 @retval EFI_SUCCESS The instruction is executed successfully.
470 Execute the JMP instruction.
474 JMP32{cs|cc} {@}R1 {Immed32|Index32}
477 b0.7 - immediate data present
478 b0.6 - 1 = 64 bit immediate data
479 0 = 32 bit immediate data
480 b1.7 - 1 = conditional
481 b1.6 1 = CS (condition set)
482 0 = CC (condition clear)
483 b1.4 1 = relative address
485 b1.3 1 = operand1 indirect
488 @param VmPtr A pointer to a VM context.
490 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
491 @retval EFI_SUCCESS The instruction is executed successfully.
500 Execute the EBC JMP8 instruction.
505 @param VmPtr A pointer to a VM context.
507 @retval EFI_SUCCESS The instruction is executed successfully.
516 Implements the EBC CALL instruction.
520 CALL32 {@}R1 {Immed32|Index32}
522 CALLEX16 {@}R1 {Immed32}
524 If Rx == R0, then it's a PC relative call to PC = PC + imm32.
526 @param VmPtr A pointer to a VM context.
528 @retval EFI_SUCCESS The instruction is executed successfully.
537 Execute the EBC RET instruction.
542 @param VmPtr A pointer to a VM context.
544 @retval EFI_SUCCESS The instruction is executed successfully.
553 Execute the EBC CMP instruction.
556 CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16}
558 @param VmPtr A pointer to a VM context.
560 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
561 @retval EFI_SUCCESS The instruction is executed successfully.
570 Execute the EBC CMPI instruction
573 CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32
575 @param VmPtr A pointer to a VM context.
577 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
578 @retval EFI_SUCCESS The instruction is executed successfully.
587 Execute the MOVxx instructions.
591 MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32}
592 MOVqq {@}R1 {Index64}, {@}R2 {Index64}
594 Copies contents of [R2] -> [R1], zero extending where required.
596 First character indicates the size of the move.
597 Second character indicates the size of the index(s).
599 Invalid to have R1 direct with index.
601 @param VmPtr A pointer to a VM context.
603 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
604 @retval EFI_SUCCESS The instruction is executed successfully.
613 Execute the EBC MOVI.
617 MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64
619 First variable character specifies the move size
620 Second variable character specifies size of the immediate data
622 Sign-extend the immediate data to the size of the operation, and zero-extend
623 if storing to a register.
625 Operand1 direct with index/immed is invalid.
627 @param VmPtr A pointer to a VM context.
629 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
630 @retval EFI_SUCCESS The instruction is executed successfully.
639 Execute the EBC MOV immediate natural. This instruction moves an immediate
640 index value into a register or memory location.
644 MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64
646 @param VmPtr A pointer to a VM context.
648 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
649 @retval EFI_SUCCESS The instruction is executed successfully.
658 Execute the EBC MOVREL instruction.
663 MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64
665 @param VmPtr A pointer to a VM context.
667 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
668 @retval EFI_SUCCESS The instruction is executed successfully.
677 Execute the EBC PUSHn instruction
680 PUSHn {@}R1 {Index16|Immed16}
682 @param VmPtr A pointer to a VM context.
684 @retval EFI_SUCCESS The instruction is executed successfully.
693 Execute the EBC PUSH instruction.
696 PUSH[32|64] {@}R1 {Index16|Immed16}
698 @param VmPtr A pointer to a VM context.
700 @retval EFI_SUCCESS The instruction is executed successfully.
709 Execute the EBC POPn instruction.
712 POPn {@}R1 {Index16|Immed16}
714 @param VmPtr A pointer to a VM context.
716 @retval EFI_SUCCESS The instruction is executed successfully.
725 Execute the EBC POP instruction.
728 POPn {@}R1 {Index16|Immed16}
730 @param VmPtr A pointer to a VM context.
732 @retval EFI_SUCCESS The instruction is executed successfully.
741 Execute all the EBC signed data manipulation instructions.
742 Since the EBC data manipulation instructions all have the same basic form,
743 they can share the code that does the fetch of operands and the write-back
744 of the result. This function performs the fetch of the operands (even if
745 both are not needed to be fetched, like NOT instruction), dispatches to the
746 appropriate subfunction, then writes back the returned result.
749 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
751 @param VmPtr A pointer to VM context.
753 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
754 @retval EFI_SUCCESS The instruction is executed successfully.
758 ExecuteSignedDataManip (
763 Execute all the EBC unsigned data manipulation instructions.
764 Since the EBC data manipulation instructions all have the same basic form,
765 they can share the code that does the fetch of operands and the write-back
766 of the result. This function performs the fetch of the operands (even if
767 both are not needed to be fetched, like NOT instruction), dispatches to the
768 appropriate subfunction, then writes back the returned result.
771 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
773 @param VmPtr A pointer to VM context.
775 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
776 @retval EFI_SUCCESS The instruction is executed successfully.
780 ExecuteUnsignedDataManip (
785 Execute the EBC LOADSP instruction.
790 @param VmPtr A pointer to a VM context.
792 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
793 @retval EFI_SUCCESS The instruction is executed successfully.
802 Execute the EBC STORESP instruction.
807 @param VmPtr A pointer to a VM context.
809 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
810 @retval EFI_SUCCESS The instruction is executed successfully.
819 Execute the EBC MOVsnw instruction. This instruction loads a signed
820 natural value from memory or register to another memory or register. On
821 32-bit machines, the value gets sign-extended to 64 bits if the destination
826 MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32}
828 0:7 1=>operand1 index present
829 0:6 1=>operand2 index present
831 @param VmPtr A pointer to a VM context.
833 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
834 @retval EFI_SUCCESS The instruction is executed successfully.
843 Execute the EBC MOVsnw instruction. This instruction loads a signed
844 natural value from memory or register to another memory or register. On
845 32-bit machines, the value gets sign-extended to 64 bits if the destination
850 MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16}
852 0:7 1=>operand1 index present
853 0:6 1=>operand2 index present
855 @param VmPtr A pointer to a VM context.
857 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
858 @retval EFI_SUCCESS The instruction is executed successfully.
867 // Data manipulation subfunctions
870 Execute the EBC NOT instruction.s
873 NOT[32|64] {@}R1, {@}R2 {Index16|Immed16}
875 @param VmPtr A pointer to a VM context.
876 @param Op1 Operand 1 from the instruction
877 @param Op2 Operand 2 from the instruction
884 IN VM_CONTEXT
*VmPtr
,
890 Execute the EBC NEG instruction.
893 NEG[32|64] {@}R1, {@}R2 {Index16|Immed16}
895 @param VmPtr A pointer to a VM context.
896 @param Op1 Operand 1 from the instruction
897 @param Op2 Operand 2 from the instruction
904 IN VM_CONTEXT
*VmPtr
,
910 Execute the EBC ADD instruction.
913 ADD[32|64] {@}R1, {@}R2 {Index16}
915 @param VmPtr A pointer to a VM context.
916 @param Op1 Operand 1 from the instruction
917 @param Op2 Operand 2 from the instruction
924 IN VM_CONTEXT
*VmPtr
,
930 Execute the EBC SUB instruction.
933 SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}
935 @param VmPtr A pointer to a VM context.
936 @param Op1 Operand 1 from the instruction
937 @param Op2 Operand 2 from the instruction
944 IN VM_CONTEXT
*VmPtr
,
950 Execute the EBC MUL instruction.
953 SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}
955 @param VmPtr A pointer to a VM context.
956 @param Op1 Operand 1 from the instruction
957 @param Op2 Operand 2 from the instruction
964 IN VM_CONTEXT
*VmPtr
,
970 Execute the EBC MULU instruction
973 MULU[32|64] {@}R1, {@}R2 {Index16|Immed16}
975 @param VmPtr A pointer to a VM context.
976 @param Op1 Operand 1 from the instruction
977 @param Op2 Operand 2 from the instruction
979 @return (unsigned)Op1 * (unsigned)Op2
984 IN VM_CONTEXT
*VmPtr
,
990 Execute the EBC DIV instruction.
993 DIV[32|64] {@}R1, {@}R2 {Index16|Immed16}
995 @param VmPtr A pointer to a VM context.
996 @param Op1 Operand 1 from the instruction
997 @param Op2 Operand 2 from the instruction
1004 IN VM_CONTEXT
*VmPtr
,
1010 Execute the EBC DIVU instruction
1013 DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16}
1015 @param VmPtr A pointer to a VM context.
1016 @param Op1 Operand 1 from the instruction
1017 @param Op2 Operand 2 from the instruction
1019 @return (unsigned)Op1 / (unsigned)Op2
1024 IN VM_CONTEXT
*VmPtr
,
1030 Execute the EBC MOD instruction.
1033 MOD[32|64] {@}R1, {@}R2 {Index16|Immed16}
1035 @param VmPtr A pointer to a VM context.
1036 @param Op1 Operand 1 from the instruction
1037 @param Op2 Operand 2 from the instruction
1039 @return Op1 MODULUS Op2
1044 IN VM_CONTEXT
*VmPtr
,
1050 Execute the EBC MODU instruction.
1053 MODU[32|64] {@}R1, {@}R2 {Index16|Immed16}
1055 @param VmPtr A pointer to a VM context.
1056 @param Op1 Operand 1 from the instruction
1057 @param Op2 Operand 2 from the instruction
1059 @return Op1 UNSIGNED_MODULUS Op2
1064 IN VM_CONTEXT
*VmPtr
,
1070 Execute the EBC AND instruction.
1073 AND[32|64] {@}R1, {@}R2 {Index16|Immed16}
1075 @param VmPtr A pointer to a VM context.
1076 @param Op1 Operand 1 from the instruction
1077 @param Op2 Operand 2 from the instruction
1084 IN VM_CONTEXT
*VmPtr
,
1090 Execute the EBC OR instruction.
1093 OR[32|64] {@}R1, {@}R2 {Index16|Immed16}
1095 @param VmPtr A pointer to a VM context.
1096 @param Op1 Operand 1 from the instruction
1097 @param Op2 Operand 2 from the instruction
1104 IN VM_CONTEXT
*VmPtr
,
1110 Execute the EBC XOR instruction.
1113 XOR[32|64] {@}R1, {@}R2 {Index16|Immed16}
1115 @param VmPtr A pointer to a VM context.
1116 @param Op1 Operand 1 from the instruction
1117 @param Op2 Operand 2 from the instruction
1124 IN VM_CONTEXT
*VmPtr
,
1130 Execute the EBC SHL shift left instruction.
1133 SHL[32|64] {@}R1, {@}R2 {Index16|Immed16}
1135 @param VmPtr A pointer to a VM context.
1136 @param Op1 Operand 1 from the instruction
1137 @param Op2 Operand 2 from the instruction
1144 IN VM_CONTEXT
*VmPtr
,
1150 Execute the EBC SHR instruction.
1153 SHR[32|64] {@}R1, {@}R2 {Index16|Immed16}
1155 @param VmPtr A pointer to a VM context.
1156 @param Op1 Operand 1 from the instruction
1157 @param Op2 Operand 2 from the instruction
1159 @return Op1 >> Op2 (unsigned operands)
1164 IN VM_CONTEXT
*VmPtr
,
1170 Execute the EBC ASHR instruction.
1173 ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16}
1175 @param VmPtr A pointer to a VM context.
1176 @param Op1 Operand 1 from the instruction
1177 @param Op2 Operand 2 from the instruction
1179 @return Op1 >> Op2 (signed)
1184 IN VM_CONTEXT
*VmPtr
,
1190 Execute the EBC EXTNDB instruction to sign-extend a byte value.
1193 EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16}
1195 @param VmPtr A pointer to a VM context.
1196 @param Op1 Operand 1 from the instruction
1197 @param Op2 Operand 2 from the instruction
1199 @return (INT64)(INT8)Op2
1204 IN VM_CONTEXT
*VmPtr
,
1210 Execute the EBC EXTNDW instruction to sign-extend a 16-bit value.
1213 EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16}
1215 @param VmPtr A pointer to a VM context.
1216 @param Op1 Operand 1 from the instruction
1217 @param Op2 Operand 2 from the instruction
1219 @return (INT64)(INT16)Op2
1224 IN VM_CONTEXT
*VmPtr
,
1230 Execute the EBC EXTNDD instruction to sign-extend a 32-bit value.
1233 EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16}
1235 @param VmPtr A pointer to a VM context.
1236 @param Op1 Operand 1 from the instruction
1237 @param Op2 Operand 2 from the instruction
1239 @return (INT64)(INT32)Op2
1244 IN VM_CONTEXT
*VmPtr
,
1250 // Once we retrieve the operands for the data manipulation instructions,
1251 // call these functions to perform the operation.
1253 CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable
[] = {
1275 CONST VM_TABLE_ENTRY mVmOpcodeTable
[] = {
1276 { ExecuteBREAK
}, // opcode 0x00
1277 { ExecuteJMP
}, // opcode 0x01
1278 { ExecuteJMP8
}, // opcode 0x02
1279 { ExecuteCALL
}, // opcode 0x03
1280 { ExecuteRET
}, // opcode 0x04
1281 { ExecuteCMP
}, // opcode 0x05 CMPeq
1282 { ExecuteCMP
}, // opcode 0x06 CMPlte
1283 { ExecuteCMP
}, // opcode 0x07 CMPgte
1284 { ExecuteCMP
}, // opcode 0x08 CMPulte
1285 { ExecuteCMP
}, // opcode 0x09 CMPugte
1286 { ExecuteUnsignedDataManip
}, // opcode 0x0A NOT
1287 { ExecuteSignedDataManip
}, // opcode 0x0B NEG
1288 { ExecuteSignedDataManip
}, // opcode 0x0C ADD
1289 { ExecuteSignedDataManip
}, // opcode 0x0D SUB
1290 { ExecuteSignedDataManip
}, // opcode 0x0E MUL
1291 { ExecuteUnsignedDataManip
}, // opcode 0x0F MULU
1292 { ExecuteSignedDataManip
}, // opcode 0x10 DIV
1293 { ExecuteUnsignedDataManip
}, // opcode 0x11 DIVU
1294 { ExecuteSignedDataManip
}, // opcode 0x12 MOD
1295 { ExecuteUnsignedDataManip
}, // opcode 0x13 MODU
1296 { ExecuteUnsignedDataManip
}, // opcode 0x14 AND
1297 { ExecuteUnsignedDataManip
}, // opcode 0x15 OR
1298 { ExecuteUnsignedDataManip
}, // opcode 0x16 XOR
1299 { ExecuteUnsignedDataManip
}, // opcode 0x17 SHL
1300 { ExecuteUnsignedDataManip
}, // opcode 0x18 SHR
1301 { ExecuteSignedDataManip
}, // opcode 0x19 ASHR
1302 { ExecuteUnsignedDataManip
}, // opcode 0x1A EXTNDB
1303 { ExecuteUnsignedDataManip
}, // opcode 0x1B EXTNDW
1304 { ExecuteUnsignedDataManip
}, // opcode 0x1C EXTNDD
1305 { ExecuteMOVxx
}, // opcode 0x1D MOVBW
1306 { ExecuteMOVxx
}, // opcode 0x1E MOVWW
1307 { ExecuteMOVxx
}, // opcode 0x1F MOVDW
1308 { ExecuteMOVxx
}, // opcode 0x20 MOVQW
1309 { ExecuteMOVxx
}, // opcode 0x21 MOVBD
1310 { ExecuteMOVxx
}, // opcode 0x22 MOVWD
1311 { ExecuteMOVxx
}, // opcode 0x23 MOVDD
1312 { ExecuteMOVxx
}, // opcode 0x24 MOVQD
1313 { ExecuteMOVsnw
}, // opcode 0x25 MOVsnw
1314 { ExecuteMOVsnd
}, // opcode 0x26 MOVsnd
1315 { NULL
}, // opcode 0x27
1316 { ExecuteMOVxx
}, // opcode 0x28 MOVqq
1317 { ExecuteLOADSP
}, // opcode 0x29 LOADSP SP1, R2
1318 { ExecuteSTORESP
}, // opcode 0x2A STORESP R1, SP2
1319 { ExecutePUSH
}, // opcode 0x2B PUSH {@}R1 [imm16]
1320 { ExecutePOP
}, // opcode 0x2C POP {@}R1 [imm16]
1321 { ExecuteCMPI
}, // opcode 0x2D CMPIEQ
1322 { ExecuteCMPI
}, // opcode 0x2E CMPILTE
1323 { ExecuteCMPI
}, // opcode 0x2F CMPIGTE
1324 { ExecuteCMPI
}, // opcode 0x30 CMPIULTE
1325 { ExecuteCMPI
}, // opcode 0x31 CMPIUGTE
1326 { ExecuteMOVxx
}, // opcode 0x32 MOVN
1327 { ExecuteMOVxx
}, // opcode 0x33 MOVND
1328 { NULL
}, // opcode 0x34
1329 { ExecutePUSHn
}, // opcode 0x35
1330 { ExecutePOPn
}, // opcode 0x36
1331 { ExecuteMOVI
}, // opcode 0x37 - mov immediate data
1332 { ExecuteMOVIn
}, // opcode 0x38 - mov immediate natural
1333 { ExecuteMOVREL
}, // opcode 0x39 - move data relative to PC
1334 { NULL
}, // opcode 0x3a
1335 { NULL
}, // opcode 0x3b
1336 { NULL
}, // opcode 0x3c
1337 { NULL
}, // opcode 0x3d
1338 { NULL
}, // opcode 0x3e
1339 { NULL
} // opcode 0x3f
1343 // Length of JMP instructions, depending on upper two bits of opcode.
1345 CONST UINT8 mJMPLen
[] = { 2, 2, 6, 10 };
1348 Given a pointer to a new VM context, execute one or more instructions. This
1349 function is only used for test purposes via the EBC VM test protocol.
1351 @param This A pointer to the EFI_EBC_VM_TEST_PROTOCOL structure.
1352 @param VmPtr A pointer to a VM context.
1353 @param InstructionCount A pointer to a UINTN value holding the number of
1354 instructions to execute. If it holds value of 0,
1355 then the instruction to be executed is 1.
1357 @retval EFI_UNSUPPORTED At least one of the opcodes is not supported.
1358 @retval EFI_SUCCESS All of the instructions are executed successfully.
1363 EbcExecuteInstructions (
1364 IN EFI_EBC_VM_TEST_PROTOCOL
*This
,
1365 IN VM_CONTEXT
*VmPtr
,
1366 IN OUT UINTN
*InstructionCount
1371 UINTN InstructionsLeft
;
1372 UINTN SavedInstructionCount
;
1374 Status
= EFI_SUCCESS
;
1376 if (*InstructionCount
== 0) {
1377 InstructionsLeft
= 1;
1379 InstructionsLeft
= *InstructionCount
;
1382 SavedInstructionCount
= *InstructionCount
;
1383 *InstructionCount
= 0;
1386 // Index into the opcode table using the opcode byte for this instruction.
1387 // This gives you the execute function, which we first test for null, then
1388 // call it if it's not null.
1390 while (InstructionsLeft
!= 0) {
1391 ExecFunc
= (UINTN
) mVmOpcodeTable
[(*VmPtr
->Ip
& OPCODE_M_OPCODE
)].ExecuteFunction
;
1392 if (ExecFunc
== (UINTN
) NULL
) {
1393 EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1394 return EFI_UNSUPPORTED
;
1396 mVmOpcodeTable
[(*VmPtr
->Ip
& OPCODE_M_OPCODE
)].ExecuteFunction (VmPtr
);
1397 *InstructionCount
= *InstructionCount
+ 1;
1401 // Decrement counter if applicable
1403 if (SavedInstructionCount
!= 0) {
1413 Execute an EBC image from an entry point or from a published protocol.
1415 @param VmPtr A pointer to a VM context.
1417 @retval EFI_UNSUPPORTED At least one of the opcodes is not supported.
1418 @retval EFI_SUCCESS All of the instructions are executed successfully.
1423 IN VM_CONTEXT
*VmPtr
1427 UINT8 StackCorrupted
;
1429 EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
*EbcSimpleDebugger
;
1432 EbcSimpleDebugger
= NULL
;
1433 Status
= EFI_SUCCESS
;
1437 // Make sure the magic value has been put on the stack before we got here.
1439 if (*VmPtr
->StackMagicPtr
!= (UINTN
) VM_STACK_KEY_VALUE
) {
1443 VmPtr
->FramePtr
= (VOID
*) ((UINT8
*) (UINTN
) VmPtr
->Gpr
[0] + 8);
1446 // Try to get the debug support for EBC
1448 DEBUG_CODE_BEGIN ();
1449 Status
= gBS
->LocateProtocol (
1450 &gEfiEbcSimpleDebuggerProtocolGuid
,
1452 (VOID
**) &EbcSimpleDebugger
1454 if (EFI_ERROR (Status
)) {
1455 EbcSimpleDebugger
= NULL
;
1460 // Save the start IP for debug. For example, if we take an exception we
1461 // can print out the location of the exception relative to the entry point,
1462 // which could then be used in a disassembly listing to find the problem.
1464 VmPtr
->EntryPoint
= (VOID
*) VmPtr
->Ip
;
1467 // We'll wait for this flag to know when we're done. The RET
1468 // instruction sets it if it runs out of stack.
1470 VmPtr
->StopFlags
= 0;
1471 while ((VmPtr
->StopFlags
& STOPFLAG_APP_DONE
) == 0) {
1473 // If we've found a simple debugger protocol, call it
1475 DEBUG_CODE_BEGIN ();
1476 if (EbcSimpleDebugger
!= NULL
) {
1477 EbcSimpleDebugger
->Debugger (EbcSimpleDebugger
, VmPtr
);
1482 // Use the opcode bits to index into the opcode dispatch table. If the
1483 // function pointer is null then generate an exception.
1485 ExecFunc
= (UINTN
) mVmOpcodeTable
[(*VmPtr
->Ip
& OPCODE_M_OPCODE
)].ExecuteFunction
;
1486 if (ExecFunc
== (UINTN
) NULL
) {
1487 EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1488 Status
= EFI_UNSUPPORTED
;
1492 // The EBC VM is a strongly ordered processor, so perform a fence operation before
1493 // and after each instruction is executed.
1497 mVmOpcodeTable
[(*VmPtr
->Ip
& OPCODE_M_OPCODE
)].ExecuteFunction (VmPtr
);
1502 // If the step flag is set, signal an exception and continue. We don't
1503 // clear it here. Assuming the debugger is responsible for clearing it.
1505 if (VMFLAG_ISSET (VmPtr
, VMFLAGS_STEP
)) {
1506 EbcDebugSignalException (EXCEPT_EBC_STEP
, EXCEPTION_FLAG_NONE
, VmPtr
);
1509 // Make sure stack has not been corrupted. Only report it once though.
1511 if ((StackCorrupted
== 0) && (*VmPtr
->StackMagicPtr
!= (UINTN
) VM_STACK_KEY_VALUE
)) {
1512 EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1515 if ((StackCorrupted
== 0) && ((UINT64
)VmPtr
->Gpr
[0] <= (UINT64
)(UINTN
) VmPtr
->StackTop
)) {
1516 EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1529 Execute the MOVxx instructions.
1533 MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32}
1534 MOVqq {@}R1 {Index64}, {@}R2 {Index64}
1536 Copies contents of [R2] -> [R1], zero extending where required.
1538 First character indicates the size of the move.
1539 Second character indicates the size of the index(s).
1541 Invalid to have R1 direct with index.
1543 @param VmPtr A pointer to a VM context.
1545 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
1546 @retval EFI_SUCCESS The instruction is executed successfully.
1551 IN VM_CONTEXT
*VmPtr
1567 Opcode
= GETOPCODE (VmPtr
);
1568 OpcMasked
= (UINT8
) (Opcode
& OPCODE_M_OPCODE
);
1571 // Get the operands byte so we can get R1 and R2
1573 Operands
= GETOPERANDS (VmPtr
);
1576 // Assume no indexes
1583 // Determine if we have an index/immediate data. Base instruction size
1584 // is 2 (opcode + operands). Add to this size each index specified.
1587 if ((Opcode
& (OPCODE_M_IMMED_OP1
| OPCODE_M_IMMED_OP2
)) != 0) {
1589 // Determine size of the index from the opcode. Then get it.
1591 if ((OpcMasked
<= OPCODE_MOVQW
) || (OpcMasked
== OPCODE_MOVNW
)) {
1593 // MOVBW, MOVWW, MOVDW, MOVQW, and MOVNW have 16-bit immediate index.
1594 // Get one or both index values.
1596 if ((Opcode
& OPCODE_M_IMMED_OP1
) != 0) {
1597 Index16
= VmReadIndex16 (VmPtr
, 2);
1598 Index64Op1
= (INT64
) Index16
;
1599 Size
+= sizeof (UINT16
);
1602 if ((Opcode
& OPCODE_M_IMMED_OP2
) != 0) {
1603 Index16
= VmReadIndex16 (VmPtr
, Size
);
1604 Index64Op2
= (INT64
) Index16
;
1605 Size
+= sizeof (UINT16
);
1607 } else if ((OpcMasked
<= OPCODE_MOVQD
) || (OpcMasked
== OPCODE_MOVND
)) {
1609 // MOVBD, MOVWD, MOVDD, MOVQD, and MOVND have 32-bit immediate index
1611 if ((Opcode
& OPCODE_M_IMMED_OP1
) != 0) {
1612 Index32
= VmReadIndex32 (VmPtr
, 2);
1613 Index64Op1
= (INT64
) Index32
;
1614 Size
+= sizeof (UINT32
);
1617 if ((Opcode
& OPCODE_M_IMMED_OP2
) != 0) {
1618 Index32
= VmReadIndex32 (VmPtr
, Size
);
1619 Index64Op2
= (INT64
) Index32
;
1620 Size
+= sizeof (UINT32
);
1622 } else if (OpcMasked
== OPCODE_MOVQQ
) {
1624 // MOVqq -- only form with a 64-bit index
1626 if ((Opcode
& OPCODE_M_IMMED_OP1
) != 0) {
1627 Index64Op1
= VmReadIndex64 (VmPtr
, 2);
1628 Size
+= sizeof (UINT64
);
1631 if ((Opcode
& OPCODE_M_IMMED_OP2
) != 0) {
1632 Index64Op2
= VmReadIndex64 (VmPtr
, Size
);
1633 Size
+= sizeof (UINT64
);
1637 // Obsolete MOVBQ, MOVWQ, MOVDQ, and MOVNQ have 64-bit immediate index
1639 EbcDebugSignalException (
1640 EXCEPT_EBC_INSTRUCTION_ENCODING
,
1641 EXCEPTION_FLAG_FATAL
,
1644 return EFI_UNSUPPORTED
;
1648 // Determine the size of the move, and create a mask for it so we can
1649 // clear unused bits.
1651 if ((OpcMasked
== OPCODE_MOVBW
) || (OpcMasked
== OPCODE_MOVBD
)) {
1652 MoveSize
= DATA_SIZE_8
;
1654 } else if ((OpcMasked
== OPCODE_MOVWW
) || (OpcMasked
== OPCODE_MOVWD
)) {
1655 MoveSize
= DATA_SIZE_16
;
1657 } else if ((OpcMasked
== OPCODE_MOVDW
) || (OpcMasked
== OPCODE_MOVDD
)) {
1658 MoveSize
= DATA_SIZE_32
;
1659 DataMask
= 0xFFFFFFFF;
1660 } else if ((OpcMasked
== OPCODE_MOVQW
) || (OpcMasked
== OPCODE_MOVQD
) || (OpcMasked
== OPCODE_MOVQQ
)) {
1661 MoveSize
= DATA_SIZE_64
;
1662 DataMask
= (UINT64
)~0;
1663 } else if ((OpcMasked
== OPCODE_MOVNW
) || (OpcMasked
== OPCODE_MOVND
)) {
1664 MoveSize
= DATA_SIZE_N
;
1665 DataMask
= (UINT64
)~0 >> (64 - 8 * sizeof (UINTN
));
1668 // We were dispatched to this function and we don't recognize the opcode
1670 EbcDebugSignalException (EXCEPT_EBC_UNDEFINED
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1671 return EFI_UNSUPPORTED
;
1674 // Now get the source address
1676 if (OPERAND2_INDIRECT (Operands
)) {
1678 // Indirect form @R2. Compute address of operand2
1680 Source
= (UINTN
) (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Index64Op2
);
1682 // Now get the data from the source. Always 0-extend and let the compiler
1683 // sign-extend where required.
1687 Data64
= (UINT64
) (UINT8
) VmReadMem8 (VmPtr
, Source
);
1691 Data64
= (UINT64
) (UINT16
) VmReadMem16 (VmPtr
, Source
);
1695 Data64
= (UINT64
) (UINT32
) VmReadMem32 (VmPtr
, Source
);
1699 Data64
= (UINT64
) VmReadMem64 (VmPtr
, Source
);
1703 Data64
= (UINT64
) (UINTN
) VmReadMemN (VmPtr
, Source
);
1714 // Not indirect source: MOVxx {@}Rx, Ry [Index]
1716 Data64
= (UINT64
) (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Index64Op2
);
1718 // Did Operand2 have an index? If so, treat as two signed values since
1719 // indexes are signed values.
1721 if ((Opcode
& OPCODE_M_IMMED_OP2
) != 0) {
1723 // NOTE: need to find a way to fix this, most likely by changing the VM
1724 // implementation to remove the stack gap. To do that, we'd need to
1725 // allocate stack space for the VM and actually set the system
1726 // stack pointer to the allocated buffer when the VM starts.
1728 // Special case -- if someone took the address of a function parameter
1729 // then we need to make sure it's not in the stack gap. We can identify
1730 // this situation if (Operand2 register == 0) && (Operand2 is direct)
1731 // && (Index applies to Operand2) && (Index > 0) && (Operand1 register != 0)
1732 // Situations that to be aware of:
1733 // * stack adjustments at beginning and end of functions R0 = R0 += stacksize
1735 if ((OPERAND2_REGNUM (Operands
) == 0) &&
1736 (!OPERAND2_INDIRECT (Operands
)) &&
1738 (OPERAND1_REGNUM (Operands
) == 0) &&
1739 (OPERAND1_INDIRECT (Operands
))
1741 Data64
= (UINT64
) ConvertStackAddr (VmPtr
, (UINTN
) (INT64
) Data64
);
1746 // Now write it back
1748 if (OPERAND1_INDIRECT (Operands
)) {
1750 // Reuse the Source variable to now be dest.
1752 Source
= (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index64Op1
);
1754 // Do the write based on the size
1758 VmWriteMem8 (VmPtr
, Source
, (UINT8
) Data64
);
1762 VmWriteMem16 (VmPtr
, Source
, (UINT16
) Data64
);
1766 VmWriteMem32 (VmPtr
, Source
, (UINT32
) Data64
);
1770 VmWriteMem64 (VmPtr
, Source
, Data64
);
1774 VmWriteMemN (VmPtr
, Source
, (UINTN
) Data64
);
1786 // Make sure we didn't have an index on operand1.
1788 if ((Opcode
& OPCODE_M_IMMED_OP1
) != 0) {
1789 EbcDebugSignalException (
1790 EXCEPT_EBC_INSTRUCTION_ENCODING
,
1791 EXCEPTION_FLAG_FATAL
,
1794 return EFI_UNSUPPORTED
;
1797 // Direct storage in register. Clear unused bits and store back to
1800 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = Data64
& DataMask
;
1803 // Advance the instruction pointer
1811 Execute the EBC BREAK instruction.
1813 @param VmPtr A pointer to a VM context.
1815 @retval EFI_SUCCESS The instruction is executed successfully.
1820 IN VM_CONTEXT
*VmPtr
1825 VOID
*EbcEntryPoint
;
1827 UINT64 U64EbcEntryPoint
;
1830 Operands
= GETOPERANDS (VmPtr
);
1833 // Runaway program break. Generate an exception and terminate
1836 EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1840 // Get VM version -- return VM revision number in R7
1846 // 16-8 = Major version
1847 // 7-0 = Minor version
1849 VmPtr
->Gpr
[7] = GetVmVersion ();
1853 // Debugger breakpoint
1856 VmPtr
->StopFlags
|= STOPFLAG_BREAKPOINT
;
1858 // See if someone has registered a handler
1860 EbcDebugSignalException (
1861 EXCEPT_EBC_BREAKPOINT
,
1862 EXCEPTION_FLAG_NONE
,
1868 // System call, which there are none, so NOP it.
1874 // Create a thunk for EBC code. R7 points to a 32-bit (in a 64-bit slot)
1875 // "offset from self" pointer to the EBC entry point.
1876 // After we're done, *(UINT64 *)R7 will be the address of the new thunk.
1879 Offset
= (INT32
) VmReadMem32 (VmPtr
, (UINTN
) VmPtr
->Gpr
[7]);
1880 U64EbcEntryPoint
= (UINT64
) (VmPtr
->Gpr
[7] + Offset
+ 4);
1881 EbcEntryPoint
= (VOID
*) (UINTN
) U64EbcEntryPoint
;
1884 // Now create a new thunk
1886 Status
= EbcCreateThunks (VmPtr
->ImageHandle
, EbcEntryPoint
, &Thunk
, 0);
1887 if (EFI_ERROR (Status
)) {
1892 // Finally replace the EBC entry point memory with the thunk address
1894 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[7], (UINT64
) (UINTN
) Thunk
);
1898 // Compiler setting version per value in R7
1901 VmPtr
->CompilerVersion
= (UINT32
) VmPtr
->Gpr
[7];
1903 // Check compiler version against VM version?
1908 // Unhandled break code. Signal exception.
1911 EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK
, EXCEPTION_FLAG_FATAL
, VmPtr
);
1923 Execute the JMP instruction.
1926 JMP64{cs|cc} Immed64
1927 JMP32{cs|cc} {@}R1 {Immed32|Index32}
1930 b0.7 - immediate data present
1931 b0.6 - 1 = 64 bit immediate data
1932 0 = 32 bit immediate data
1933 b1.7 - 1 = conditional
1934 b1.6 1 = CS (condition set)
1935 0 = CC (condition clear)
1936 b1.4 1 = relative address
1937 0 = absolute address
1938 b1.3 1 = operand1 indirect
1941 @param VmPtr A pointer to a VM context.
1943 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
1944 @retval EFI_SUCCESS The instruction is executed successfully.
1949 IN VM_CONTEXT
*VmPtr
1954 UINT8 ConditionFlag
;
1961 Operand
= GETOPERANDS (VmPtr
);
1962 Opcode
= GETOPCODE (VmPtr
);
1965 // Get instruction length from the opcode. The upper two bits are used here
1966 // to index into the length array.
1968 Size
= mJMPLen
[(Opcode
>> 6) & 0x03];
1971 // Decode instruction conditions
1972 // If we haven't met the condition, then simply advance the IP and return.
1974 CompareSet
= (UINT8
) (((Operand
& JMP_M_CS
) != 0) ? 1 : 0);
1975 ConditionFlag
= (UINT8
) VMFLAG_ISSET (VmPtr
, VMFLAGS_CC
);
1976 if ((Operand
& CONDITION_M_CONDITIONAL
) != 0) {
1977 if (CompareSet
!= ConditionFlag
) {
1983 // Check for 64-bit form and do it right away since it's the most
1984 // straight-forward form.
1986 if ((Opcode
& OPCODE_M_IMMDATA64
) != 0) {
1988 // Double check for immediate-data, which is required. If not there,
1989 // then signal an exception
1991 if ((Opcode
& OPCODE_M_IMMDATA
) == 0) {
1992 EbcDebugSignalException (
1993 EXCEPT_EBC_INSTRUCTION_ENCODING
,
1994 EXCEPTION_FLAG_ERROR
,
1997 return EFI_UNSUPPORTED
;
2000 // 64-bit immediate data is full address. Read the immediate data,
2001 // check for alignment, and jump absolute.
2003 Data64
= (UINT64
) VmReadImmed64 (VmPtr
, 2);
2004 if (!IS_ALIGNED ((UINTN
) Data64
, sizeof (UINT16
))) {
2005 EbcDebugSignalException (
2006 EXCEPT_EBC_ALIGNMENT_CHECK
,
2007 EXCEPTION_FLAG_FATAL
,
2011 return EFI_UNSUPPORTED
;
2015 // Take jump -- relative or absolute
2017 if ((Operand
& JMP_M_RELATIVE
) != 0) {
2018 VmPtr
->Ip
+= (UINTN
) Data64
+ Size
;
2020 VmPtr
->Ip
= (VMIP
) (UINTN
) Data64
;
2027 // Get the index if there is one. May be either an index, or an immediate
2028 // offset depending on indirect operand.
2029 // JMP32 @R1 Index32 -- immediate data is an index
2030 // JMP32 R1 Immed32 -- immedate data is an offset
2032 if ((Opcode
& OPCODE_M_IMMDATA
) != 0) {
2033 if (OPERAND1_INDIRECT (Operand
)) {
2034 Index32
= VmReadIndex32 (VmPtr
, 2);
2036 Index32
= VmReadImmed32 (VmPtr
, 2);
2042 // Get the register data. If R == 0, then special case where it's ignored.
2044 if (OPERAND1_REGNUM (Operand
) == 0) {
2047 Data64
= (UINT64
) OPERAND1_REGDATA (VmPtr
, Operand
);
2052 if (OPERAND1_INDIRECT (Operand
)) {
2054 // Form: JMP32 @Rx {Index32}
2056 Addr
= VmReadMemN (VmPtr
, (UINTN
) Data64
+ Index32
);
2057 if (!IS_ALIGNED ((UINTN
) Addr
, sizeof (UINT16
))) {
2058 EbcDebugSignalException (
2059 EXCEPT_EBC_ALIGNMENT_CHECK
,
2060 EXCEPTION_FLAG_FATAL
,
2064 return EFI_UNSUPPORTED
;
2067 if ((Operand
& JMP_M_RELATIVE
) != 0) {
2068 VmPtr
->Ip
+= (UINTN
) Addr
+ Size
;
2070 VmPtr
->Ip
= (VMIP
) Addr
;
2074 // Form: JMP32 Rx {Immed32}
2076 Addr
= (UINTN
) (Data64
+ Index32
);
2077 if (!IS_ALIGNED ((UINTN
) Addr
, sizeof (UINT16
))) {
2078 EbcDebugSignalException (
2079 EXCEPT_EBC_ALIGNMENT_CHECK
,
2080 EXCEPTION_FLAG_FATAL
,
2084 return EFI_UNSUPPORTED
;
2087 if ((Operand
& JMP_M_RELATIVE
) != 0) {
2088 VmPtr
->Ip
+= (UINTN
) Addr
+ Size
;
2090 VmPtr
->Ip
= (VMIP
) Addr
;
2099 Execute the EBC JMP8 instruction.
2102 JMP8{cs|cc} Offset/2
2104 @param VmPtr A pointer to a VM context.
2106 @retval EFI_SUCCESS The instruction is executed successfully.
2111 IN VM_CONTEXT
*VmPtr
2115 UINT8 ConditionFlag
;
2120 // Decode instruction.
2122 Opcode
= GETOPCODE (VmPtr
);
2123 CompareSet
= (UINT8
) (((Opcode
& JMP_M_CS
) != 0) ? 1 : 0);
2124 ConditionFlag
= (UINT8
) VMFLAG_ISSET (VmPtr
, VMFLAGS_CC
);
2127 // If we haven't met the condition, then simply advance the IP and return
2129 if ((Opcode
& CONDITION_M_CONDITIONAL
) != 0) {
2130 if (CompareSet
!= ConditionFlag
) {
2136 // Get the offset from the instruction stream. It's relative to the
2137 // following instruction, and divided by 2.
2139 Offset
= VmReadImmed8 (VmPtr
, 1);
2141 // Want to check for offset == -2 and then raise an exception?
2143 VmPtr
->Ip
+= (Offset
* 2) + 2;
2149 Execute the EBC MOVI.
2153 MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64
2155 First variable character specifies the move size
2156 Second variable character specifies size of the immediate data
2158 Sign-extend the immediate data to the size of the operation, and zero-extend
2159 if storing to a register.
2161 Operand1 direct with index/immed is invalid.
2163 @param VmPtr A pointer to a VM context.
2165 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
2166 @retval EFI_SUCCESS The instruction is executed successfully.
2171 IN VM_CONTEXT
*VmPtr
2183 // Get the opcode and operands byte so we can get R1 and R2
2185 Opcode
= GETOPCODE (VmPtr
);
2186 Operands
= GETOPERANDS (VmPtr
);
2189 // Get the index (16-bit) if present
2191 if ((Operands
& MOVI_M_IMMDATA
) != 0) {
2192 Index16
= VmReadIndex16 (VmPtr
, 2);
2199 // Extract the immediate data. Sign-extend always.
2201 if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH16
) {
2202 ImmData64
= (INT64
) (INT16
) VmReadImmed16 (VmPtr
, Size
);
2204 } else if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH32
) {
2205 ImmData64
= (INT64
) (INT32
) VmReadImmed32 (VmPtr
, Size
);
2207 } else if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH64
) {
2208 ImmData64
= (INT64
) VmReadImmed64 (VmPtr
, Size
);
2214 EbcDebugSignalException (
2215 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2216 EXCEPTION_FLAG_FATAL
,
2219 return EFI_UNSUPPORTED
;
2222 // Now write back the result
2224 if (!OPERAND1_INDIRECT (Operands
)) {
2226 // Operand1 direct. Make sure it didn't have an index.
2228 if ((Operands
& MOVI_M_IMMDATA
) != 0) {
2229 EbcDebugSignalException (
2230 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2231 EXCEPTION_FLAG_FATAL
,
2234 return EFI_UNSUPPORTED
;
2237 // Writing directly to a register. Clear unused bits.
2239 if ((Operands
& MOVI_M_MOVEWIDTH
) == MOVI_MOVEWIDTH8
) {
2240 Mask64
= 0x000000FF;
2241 } else if ((Operands
& MOVI_M_MOVEWIDTH
) == MOVI_MOVEWIDTH16
) {
2242 Mask64
= 0x0000FFFF;
2243 } else if ((Operands
& MOVI_M_MOVEWIDTH
) == MOVI_MOVEWIDTH32
) {
2244 Mask64
= 0x00000000FFFFFFFF;
2246 Mask64
= (UINT64
)~0;
2249 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = ImmData64
& Mask64
;
2252 // Get the address then write back based on size of the move
2254 Op1
= (UINT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
;
2255 if ((Operands
& MOVI_M_MOVEWIDTH
) == MOVI_MOVEWIDTH8
) {
2256 VmWriteMem8 (VmPtr
, (UINTN
) Op1
, (UINT8
) ImmData64
);
2257 } else if ((Operands
& MOVI_M_MOVEWIDTH
) == MOVI_MOVEWIDTH16
) {
2258 VmWriteMem16 (VmPtr
, (UINTN
) Op1
, (UINT16
) ImmData64
);
2259 } else if ((Operands
& MOVI_M_MOVEWIDTH
) == MOVI_MOVEWIDTH32
) {
2260 VmWriteMem32 (VmPtr
, (UINTN
) Op1
, (UINT32
) ImmData64
);
2262 VmWriteMem64 (VmPtr
, (UINTN
) Op1
, (UINT64
) ImmData64
);
2266 // Advance the instruction pointer
2274 Execute the EBC MOV immediate natural. This instruction moves an immediate
2275 index value into a register or memory location.
2279 MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64
2281 @param VmPtr A pointer to a VM context.
2283 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
2284 @retval EFI_SUCCESS The instruction is executed successfully.
2289 IN VM_CONTEXT
*VmPtr
2302 // Get the opcode and operands byte so we can get R1 and R2
2304 Opcode
= GETOPCODE (VmPtr
);
2305 Operands
= GETOPERANDS (VmPtr
);
2308 // Get the operand1 index (16-bit) if present
2310 if ((Operands
& MOVI_M_IMMDATA
) != 0) {
2311 Index16
= VmReadIndex16 (VmPtr
, 2);
2318 // Extract the immediate data and convert to a 64-bit index.
2320 if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH16
) {
2321 ImmedIndex16
= VmReadIndex16 (VmPtr
, Size
);
2322 ImmedIndex64
= (INT64
) ImmedIndex16
;
2324 } else if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH32
) {
2325 ImmedIndex32
= VmReadIndex32 (VmPtr
, Size
);
2326 ImmedIndex64
= (INT64
) ImmedIndex32
;
2328 } else if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH64
) {
2329 ImmedIndex64
= VmReadIndex64 (VmPtr
, Size
);
2335 EbcDebugSignalException (
2336 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2337 EXCEPTION_FLAG_FATAL
,
2340 return EFI_UNSUPPORTED
;
2343 // Now write back the result
2345 if (!OPERAND1_INDIRECT (Operands
)) {
2347 // Check for MOVIn R1 Index16, Immed (not indirect, with index), which
2350 if ((Operands
& MOVI_M_IMMDATA
) != 0) {
2351 EbcDebugSignalException (
2352 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2353 EXCEPTION_FLAG_FATAL
,
2356 return EFI_UNSUPPORTED
;
2359 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = ImmedIndex64
;
2364 Op1
= (UINT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
;
2365 VmWriteMemN (VmPtr
, (UINTN
) Op1
, (UINTN
)(INTN
) ImmedIndex64
);
2368 // Advance the instruction pointer
2376 Execute the EBC MOVREL instruction.
2377 Dest <- Ip + ImmData
2381 MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64
2383 @param VmPtr A pointer to a VM context.
2385 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
2386 @retval EFI_SUCCESS The instruction is executed successfully.
2391 IN VM_CONTEXT
*VmPtr
2403 // Get the opcode and operands byte so we can get R1 and R2
2405 Opcode
= GETOPCODE (VmPtr
);
2406 Operands
= GETOPERANDS (VmPtr
);
2409 // Get the Operand 1 index (16-bit) if present
2411 if ((Operands
& MOVI_M_IMMDATA
) != 0) {
2412 Index16
= VmReadIndex16 (VmPtr
, 2);
2419 // Get the immediate data.
2421 if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH16
) {
2422 ImmData64
= (INT64
) VmReadImmed16 (VmPtr
, Size
);
2424 } else if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH32
) {
2425 ImmData64
= (INT64
) VmReadImmed32 (VmPtr
, Size
);
2427 } else if ((Opcode
& MOVI_M_DATAWIDTH
) == MOVI_DATAWIDTH64
) {
2428 ImmData64
= VmReadImmed64 (VmPtr
, Size
);
2434 EbcDebugSignalException (
2435 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2436 EXCEPTION_FLAG_FATAL
,
2439 return EFI_UNSUPPORTED
;
2442 // Compute the value and write back the result
2444 Op2
= (UINT64
) ((INT64
) ((UINT64
) (UINTN
) VmPtr
->Ip
) + (INT64
) ImmData64
+ Size
);
2445 if (!OPERAND1_INDIRECT (Operands
)) {
2447 // Check for illegal combination of operand1 direct with immediate data
2449 if ((Operands
& MOVI_M_IMMDATA
) != 0) {
2450 EbcDebugSignalException (
2451 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2452 EXCEPTION_FLAG_FATAL
,
2455 return EFI_UNSUPPORTED
;
2458 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = (VM_REGISTER
) Op2
;
2461 // Get the address = [Rx] + Index16
2462 // Write back the result. Always a natural size write, since
2463 // we're talking addresses here.
2465 Op1
= (UINT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
;
2466 VmWriteMemN (VmPtr
, (UINTN
) Op1
, (UINTN
) Op2
);
2469 // Advance the instruction pointer
2477 Execute the EBC MOVsnw instruction. This instruction loads a signed
2478 natural value from memory or register to another memory or register. On
2479 32-bit machines, the value gets sign-extended to 64 bits if the destination
2484 MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16}
2486 0:7 1=>operand1 index present
2487 0:6 1=>operand2 index present
2489 @param VmPtr A pointer to a VM context.
2491 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
2492 @retval EFI_SUCCESS The instruction is executed successfully.
2497 IN VM_CONTEXT
*VmPtr
2508 // Get the opcode and operand bytes
2510 Opcode
= GETOPCODE (VmPtr
);
2511 Operands
= GETOPERANDS (VmPtr
);
2513 Op1Index
= Op2Index
= 0;
2516 // Get the indexes if present.
2519 if ((Opcode
& OPCODE_M_IMMED_OP1
) !=0) {
2520 if (OPERAND1_INDIRECT (Operands
)) {
2521 Op1Index
= VmReadIndex16 (VmPtr
, 2);
2524 // Illegal form operand1 direct with index: MOVsnw R1 Index16, {@}R2
2526 EbcDebugSignalException (
2527 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2528 EXCEPTION_FLAG_FATAL
,
2531 return EFI_UNSUPPORTED
;
2534 Size
+= sizeof (UINT16
);
2537 if ((Opcode
& OPCODE_M_IMMED_OP2
) != 0) {
2538 if (OPERAND2_INDIRECT (Operands
)) {
2539 Op2Index
= VmReadIndex16 (VmPtr
, Size
);
2541 Op2Index
= VmReadImmed16 (VmPtr
, Size
);
2544 Size
+= sizeof (UINT16
);
2547 // Get the data from the source.
2549 Op2
= (UINT64
) (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Op2Index
);
2550 if (OPERAND2_INDIRECT (Operands
)) {
2551 Op2
= (UINT64
) VmReadMemN (VmPtr
, (UINTN
) Op2
);
2554 // Now write back the result.
2556 if (!OPERAND1_INDIRECT (Operands
)) {
2557 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = Op2
;
2559 VmWriteMemN (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Op1Index
), (UINTN
) Op2
);
2562 // Advance the instruction pointer
2570 Execute the EBC MOVsnw instruction. This instruction loads a signed
2571 natural value from memory or register to another memory or register. On
2572 32-bit machines, the value gets sign-extended to 64 bits if the destination
2577 MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32}
2579 0:7 1=>operand1 index present
2580 0:6 1=>operand2 index present
2582 @param VmPtr A pointer to a VM context.
2584 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
2585 @retval EFI_SUCCESS The instruction is executed successfully.
2590 IN VM_CONTEXT
*VmPtr
2601 // Get the opcode and operand bytes
2603 Opcode
= GETOPCODE (VmPtr
);
2604 Operands
= GETOPERANDS (VmPtr
);
2606 Op1Index
= Op2Index
= 0;
2609 // Get the indexes if present.
2612 if ((Opcode
& OPCODE_M_IMMED_OP1
) != 0) {
2613 if (OPERAND1_INDIRECT (Operands
)) {
2614 Op1Index
= VmReadIndex32 (VmPtr
, 2);
2617 // Illegal form operand1 direct with index: MOVsnd R1 Index16,..
2619 EbcDebugSignalException (
2620 EXCEPT_EBC_INSTRUCTION_ENCODING
,
2621 EXCEPTION_FLAG_FATAL
,
2624 return EFI_UNSUPPORTED
;
2627 Size
+= sizeof (UINT32
);
2630 if ((Opcode
& OPCODE_M_IMMED_OP2
) != 0) {
2631 if (OPERAND2_INDIRECT (Operands
)) {
2632 Op2Index
= VmReadIndex32 (VmPtr
, Size
);
2634 Op2Index
= VmReadImmed32 (VmPtr
, Size
);
2637 Size
+= sizeof (UINT32
);
2640 // Get the data from the source.
2642 Op2
= (UINT64
) (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Op2Index
);
2643 if (OPERAND2_INDIRECT (Operands
)) {
2644 Op2
= (UINT64
) VmReadMemN (VmPtr
, (UINTN
) Op2
);
2647 // Now write back the result.
2649 if (!OPERAND1_INDIRECT (Operands
)) {
2650 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = Op2
;
2652 VmWriteMemN (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Op1Index
), (UINTN
) Op2
);
2655 // Advance the instruction pointer
2663 Execute the EBC PUSHn instruction
2666 PUSHn {@}R1 {Index16|Immed16}
2668 @param VmPtr A pointer to a VM context.
2670 @retval EFI_SUCCESS The instruction is executed successfully.
2675 IN VM_CONTEXT
*VmPtr
2684 // Get opcode and operands
2686 Opcode
= GETOPCODE (VmPtr
);
2687 Operands
= GETOPERANDS (VmPtr
);
2690 // Get index if present
2692 if ((Opcode
& PUSHPOP_M_IMMDATA
) != 0) {
2693 if (OPERAND1_INDIRECT (Operands
)) {
2694 Index16
= VmReadIndex16 (VmPtr
, 2);
2696 Index16
= VmReadImmed16 (VmPtr
, 2);
2705 // Get the data to push
2707 if (OPERAND1_INDIRECT (Operands
)) {
2708 DataN
= VmReadMemN (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
));
2710 DataN
= (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
);
2713 // Adjust the stack down.
2715 VmPtr
->Gpr
[0] -= sizeof (UINTN
);
2716 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], DataN
);
2722 Execute the EBC PUSH instruction.
2725 PUSH[32|64] {@}R1 {Index16|Immed16}
2727 @param VmPtr A pointer to a VM context.
2729 @retval EFI_SUCCESS The instruction is executed successfully.
2734 IN VM_CONTEXT
*VmPtr
2744 // Get opcode and operands
2746 Opcode
= GETOPCODE (VmPtr
);
2747 Operands
= GETOPERANDS (VmPtr
);
2749 // Get immediate index if present, then advance the IP.
2751 if ((Opcode
& PUSHPOP_M_IMMDATA
) != 0) {
2752 if (OPERAND1_INDIRECT (Operands
)) {
2753 Index16
= VmReadIndex16 (VmPtr
, 2);
2755 Index16
= VmReadImmed16 (VmPtr
, 2);
2764 // Get the data to push
2766 if ((Opcode
& PUSHPOP_M_64
) != 0) {
2767 if (OPERAND1_INDIRECT (Operands
)) {
2768 Data64
= VmReadMem64 (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
));
2770 Data64
= (UINT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
;
2773 // Adjust the stack down, then write back the data
2775 VmPtr
->Gpr
[0] -= sizeof (UINT64
);
2776 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], Data64
);
2781 if (OPERAND1_INDIRECT (Operands
)) {
2782 Data32
= VmReadMem32 (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
));
2784 Data32
= (UINT32
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
;
2787 // Adjust the stack down and write the data
2789 VmPtr
->Gpr
[0] -= sizeof (UINT32
);
2790 VmWriteMem32 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], Data32
);
2798 Execute the EBC POPn instruction.
2801 POPn {@}R1 {Index16|Immed16}
2803 @param VmPtr A pointer to a VM context.
2805 @retval EFI_SUCCESS The instruction is executed successfully.
2810 IN VM_CONTEXT
*VmPtr
2819 // Get opcode and operands
2821 Opcode
= GETOPCODE (VmPtr
);
2822 Operands
= GETOPERANDS (VmPtr
);
2824 // Get immediate data if present, and advance the IP
2826 if ((Opcode
& PUSHPOP_M_IMMDATA
) != 0) {
2827 if (OPERAND1_INDIRECT (Operands
)) {
2828 Index16
= VmReadIndex16 (VmPtr
, 2);
2830 Index16
= VmReadImmed16 (VmPtr
, 2);
2839 // Read the data off the stack, then adjust the stack pointer
2841 DataN
= VmReadMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0]);
2842 VmPtr
->Gpr
[0] += sizeof (UINTN
);
2844 // Do the write-back
2846 if (OPERAND1_INDIRECT (Operands
)) {
2847 VmWriteMemN (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
), DataN
);
2849 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = (INT64
) (UINT64
) ((UINTN
) DataN
+ Index16
);
2857 Execute the EBC POP instruction.
2860 POPn {@}R1 {Index16|Immed16}
2862 @param VmPtr A pointer to a VM context.
2864 @retval EFI_SUCCESS The instruction is executed successfully.
2869 IN VM_CONTEXT
*VmPtr
2879 // Get opcode and operands
2881 Opcode
= GETOPCODE (VmPtr
);
2882 Operands
= GETOPERANDS (VmPtr
);
2884 // Get immediate data if present, and advance the IP.
2886 if ((Opcode
& PUSHPOP_M_IMMDATA
) != 0) {
2887 if (OPERAND1_INDIRECT (Operands
)) {
2888 Index16
= VmReadIndex16 (VmPtr
, 2);
2890 Index16
= VmReadImmed16 (VmPtr
, 2);
2899 // Get the data off the stack, then write it to the appropriate location
2901 if ((Opcode
& PUSHPOP_M_64
) != 0) {
2903 // Read the data off the stack, then adjust the stack pointer
2905 Data64
= VmReadMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0]);
2906 VmPtr
->Gpr
[0] += sizeof (UINT64
);
2908 // Do the write-back
2910 if (OPERAND1_INDIRECT (Operands
)) {
2911 VmWriteMem64 (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
), Data64
);
2913 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = Data64
+ Index16
;
2917 // 32-bit pop. Read it off the stack and adjust the stack pointer
2919 Data32
= (INT32
) VmReadMem32 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0]);
2920 VmPtr
->Gpr
[0] += sizeof (UINT32
);
2922 // Do the write-back
2924 if (OPERAND1_INDIRECT (Operands
)) {
2925 VmWriteMem32 (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] + Index16
), Data32
);
2927 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = (INT64
) Data32
+ Index16
;
2936 Implements the EBC CALL instruction.
2940 CALL32 {@}R1 {Immed32|Index32}
2942 CALLEX16 {@}R1 {Immed32}
2944 If Rx == R0, then it's a PC relative call to PC = PC + imm32.
2946 @param VmPtr A pointer to a VM context.
2948 @retval EFI_SUCCESS The instruction is executed successfully.
2953 IN VM_CONTEXT
*VmPtr
2964 // Get opcode and operands
2966 Opcode
= GETOPCODE (VmPtr
);
2967 Operands
= GETOPERANDS (VmPtr
);
2969 // Assign these as well to avoid compiler warnings
2974 FramePtr
= VmPtr
->FramePtr
;
2976 // Determine the instruction size, and get immediate data if present
2978 if ((Opcode
& OPCODE_M_IMMDATA
) != 0) {
2979 if ((Opcode
& OPCODE_M_IMMDATA64
) != 0) {
2980 Immed64
= VmReadImmed64 (VmPtr
, 2);
2984 // If register operand is indirect, then the immediate data is an index
2986 if (OPERAND1_INDIRECT (Operands
)) {
2987 Immed32
= VmReadIndex32 (VmPtr
, 2);
2989 Immed32
= VmReadImmed32 (VmPtr
, 2);
2998 // If it's a call to EBC, adjust the stack pointer down 16 bytes and
2999 // put our return address and frame pointer on the VM stack.
3001 if ((Operands
& OPERAND_M_NATIVE_CALL
) == 0) {
3003 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINTN
) FramePtr
);
3004 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->Gpr
[0];
3006 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
3009 // If 64-bit data, then absolute jump only
3011 if ((Opcode
& OPCODE_M_IMMDATA64
) != 0) {
3013 // Native or EBC call?
3015 if ((Operands
& OPERAND_M_NATIVE_CALL
) == 0) {
3016 VmPtr
->Ip
= (VMIP
) (UINTN
) Immed64
;
3019 // Call external function, get the return value, and advance the IP
3021 EbcLLCALLEX (VmPtr
, (UINTN
) Immed64
, (UINTN
) VmPtr
->Gpr
[0], FramePtr
, Size
);
3025 // Get the register data. If operand1 == 0, then ignore register and
3026 // take immediate data as relative or absolute address.
3027 // Compiler should take care of upper bits if 32-bit machine.
3029 if (OPERAND1_REGNUM (Operands
) != 0) {
3030 Immed64
= (UINT64
) (UINTN
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)];
3033 // Get final address
3035 if (OPERAND1_INDIRECT (Operands
)) {
3036 Immed64
= (INT64
) (UINT64
) (UINTN
) VmReadMemN (VmPtr
, (UINTN
) (Immed64
+ Immed32
));
3041 // Now determine if external call, and then if relative or absolute
3043 if ((Operands
& OPERAND_M_NATIVE_CALL
) == 0) {
3045 // EBC call. Relative or absolute? If relative, then it's relative to the
3046 // start of the next instruction.
3048 if ((Operands
& OPERAND_M_RELATIVE_ADDR
) != 0) {
3049 VmPtr
->Ip
+= Immed64
+ Size
;
3051 VmPtr
->Ip
= (VMIP
) (UINTN
) Immed64
;
3055 // Native call. Relative or absolute?
3057 if ((Operands
& OPERAND_M_RELATIVE_ADDR
) != 0) {
3058 EbcLLCALLEX (VmPtr
, (UINTN
) (Immed64
+ VmPtr
->Ip
+ Size
), (UINTN
) VmPtr
->Gpr
[0], FramePtr
, Size
);
3060 if ((VmPtr
->StopFlags
& STOPFLAG_BREAK_ON_CALLEX
) != 0) {
3064 EbcLLCALLEX (VmPtr
, (UINTN
) Immed64
, (UINTN
) VmPtr
->Gpr
[0], FramePtr
, Size
);
3074 Execute the EBC RET instruction.
3079 @param VmPtr A pointer to a VM context.
3081 @retval EFI_SUCCESS The instruction is executed successfully.
3086 IN VM_CONTEXT
*VmPtr
3090 // If we're at the top of the stack, then simply set the done
3093 if (VmPtr
->StackRetAddr
== (UINT64
) VmPtr
->Gpr
[0]) {
3094 VmPtr
->StopFlags
|= STOPFLAG_APP_DONE
;
3097 // Pull the return address off the VM app's stack and set the IP
3100 if (!IS_ALIGNED ((UINTN
) VmPtr
->Gpr
[0], sizeof (UINT16
))) {
3101 EbcDebugSignalException (
3102 EXCEPT_EBC_ALIGNMENT_CHECK
,
3103 EXCEPTION_FLAG_FATAL
,
3108 // Restore the IP and frame pointer from the stack
3110 VmPtr
->Ip
= (VMIP
) (UINTN
) VmReadMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0]);
3112 VmPtr
->FramePtr
= (VOID
*) VmReadMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0]);
3121 Execute the EBC CMP instruction.
3124 CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16}
3126 @param VmPtr A pointer to a VM context.
3128 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
3129 @retval EFI_SUCCESS The instruction is executed successfully.
3134 IN VM_CONTEXT
*VmPtr
3146 // Get opcode and operands
3148 Opcode
= GETOPCODE (VmPtr
);
3149 Operands
= GETOPERANDS (VmPtr
);
3151 // Get the register data we're going to compare to
3153 Op1
= VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)];
3155 // Get immediate data
3157 if ((Opcode
& OPCODE_M_IMMDATA
) != 0) {
3158 if (OPERAND2_INDIRECT (Operands
)) {
3159 Index16
= VmReadIndex16 (VmPtr
, 2);
3161 Index16
= VmReadImmed16 (VmPtr
, 2);
3172 if (OPERAND2_INDIRECT (Operands
)) {
3173 if ((Opcode
& OPCODE_M_64BIT
) != 0) {
3174 Op2
= (INT64
) VmReadMem64 (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Index16
));
3177 // 32-bit operations. 0-extend the values for all cases.
3179 Op2
= (INT64
) (UINT64
) ((UINT32
) VmReadMem32 (VmPtr
, (UINTN
) (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Index16
)));
3182 Op2
= VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Index16
;
3185 // Now do the compare
3188 if ((Opcode
& OPCODE_M_64BIT
) != 0) {
3192 switch (Opcode
& OPCODE_M_OPCODE
) {
3211 case OPCODE_CMPULTE
:
3212 if ((UINT64
) Op1
<= (UINT64
) Op2
) {
3217 case OPCODE_CMPUGTE
:
3218 if ((UINT64
) Op1
>= (UINT64
) Op2
) {
3230 switch (Opcode
& OPCODE_M_OPCODE
) {
3232 if ((INT32
) Op1
== (INT32
) Op2
) {
3238 if ((INT32
) Op1
<= (INT32
) Op2
) {
3244 if ((INT32
) Op1
>= (INT32
) Op2
) {
3249 case OPCODE_CMPULTE
:
3250 if ((UINT32
) Op1
<= (UINT32
) Op2
) {
3255 case OPCODE_CMPUGTE
:
3256 if ((UINT32
) Op1
>= (UINT32
) Op2
) {
3266 // Now set the flag accordingly for the comparison
3269 VMFLAG_SET (VmPtr
, VMFLAGS_CC
);
3271 VMFLAG_CLEAR (VmPtr
, VMFLAGS_CC
);
3282 Execute the EBC CMPI instruction
3285 CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32
3287 @param VmPtr A pointer to a VM context.
3289 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
3290 @retval EFI_SUCCESS The instruction is executed successfully.
3295 IN VM_CONTEXT
*VmPtr
3307 // Get opcode and operands
3309 Opcode
= GETOPCODE (VmPtr
);
3310 Operands
= GETOPERANDS (VmPtr
);
3313 // Get operand1 index if present
3316 if ((Operands
& OPERAND_M_CMPI_INDEX
) != 0) {
3317 Index16
= VmReadIndex16 (VmPtr
, 2);
3323 // Get operand1 data we're going to compare to
3325 Op1
= (INT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)];
3326 if (OPERAND1_INDIRECT (Operands
)) {
3328 // Indirect operand1. Fetch 32 or 64-bit value based on compare size.
3330 if ((Opcode
& OPCODE_M_CMPI64
) != 0) {
3331 Op1
= (INT64
) VmReadMem64 (VmPtr
, (UINTN
) Op1
+ Index16
);
3333 Op1
= (INT64
) VmReadMem32 (VmPtr
, (UINTN
) Op1
+ Index16
);
3337 // Better not have been an index with direct. That is, CMPI R1 Index,...
3340 if ((Operands
& OPERAND_M_CMPI_INDEX
) != 0) {
3341 EbcDebugSignalException (
3342 EXCEPT_EBC_INSTRUCTION_ENCODING
,
3343 EXCEPTION_FLAG_ERROR
,
3347 return EFI_UNSUPPORTED
;
3351 // Get immediate data -- 16- or 32-bit sign extended
3353 if ((Opcode
& OPCODE_M_CMPI32_DATA
) != 0) {
3354 Op2
= (INT64
) VmReadImmed32 (VmPtr
, Size
);
3358 // 16-bit immediate data. Sign extend always.
3360 Op2
= (INT64
) ((INT16
) VmReadImmed16 (VmPtr
, Size
));
3364 // Now do the compare
3367 if ((Opcode
& OPCODE_M_CMPI64
) != 0) {
3369 // 64 bit comparison
3371 switch (Opcode
& OPCODE_M_OPCODE
) {
3373 if (Op1
== (INT64
) Op2
) {
3378 case OPCODE_CMPILTE
:
3379 if (Op1
<= (INT64
) Op2
) {
3384 case OPCODE_CMPIGTE
:
3385 if (Op1
>= (INT64
) Op2
) {
3390 case OPCODE_CMPIULTE
:
3391 if ((UINT64
) Op1
<= (UINT64
) ((UINT32
) Op2
)) {
3396 case OPCODE_CMPIUGTE
:
3397 if ((UINT64
) Op1
>= (UINT64
) ((UINT32
) Op2
)) {
3407 // 32-bit comparisons
3409 switch (Opcode
& OPCODE_M_OPCODE
) {
3411 if ((INT32
) Op1
== Op2
) {
3416 case OPCODE_CMPILTE
:
3417 if ((INT32
) Op1
<= Op2
) {
3422 case OPCODE_CMPIGTE
:
3423 if ((INT32
) Op1
>= Op2
) {
3428 case OPCODE_CMPIULTE
:
3429 if ((UINT32
) Op1
<= (UINT32
) Op2
) {
3434 case OPCODE_CMPIUGTE
:
3435 if ((UINT32
) Op1
>= (UINT32
) Op2
) {
3445 // Now set the flag accordingly for the comparison
3448 VMFLAG_SET (VmPtr
, VMFLAGS_CC
);
3450 VMFLAG_CLEAR (VmPtr
, VMFLAGS_CC
);
3461 Execute the EBC NOT instruction.s
3464 NOT[32|64] {@}R1, {@}R2 {Index16|Immed16}
3466 @param VmPtr A pointer to a VM context.
3467 @param Op1 Operand 1 from the instruction
3468 @param Op2 Operand 2 from the instruction
3475 IN VM_CONTEXT
*VmPtr
,
3485 Execute the EBC NEG instruction.
3488 NEG[32|64] {@}R1, {@}R2 {Index16|Immed16}
3490 @param VmPtr A pointer to a VM context.
3491 @param Op1 Operand 1 from the instruction
3492 @param Op2 Operand 2 from the instruction
3499 IN VM_CONTEXT
*VmPtr
,
3509 Execute the EBC ADD instruction.
3512 ADD[32|64] {@}R1, {@}R2 {Index16}
3514 @param VmPtr A pointer to a VM context.
3515 @param Op1 Operand 1 from the instruction
3516 @param Op2 Operand 2 from the instruction
3523 IN VM_CONTEXT
*VmPtr
,
3533 Execute the EBC SUB instruction.
3536 SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}
3538 @param VmPtr A pointer to a VM context.
3539 @param Op1 Operand 1 from the instruction
3540 @param Op2 Operand 2 from the instruction
3547 IN VM_CONTEXT
*VmPtr
,
3552 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3553 return (UINT64
) ((INT64
) ((INT64
) Op1
- (INT64
) Op2
));
3555 return (UINT64
) ((INT64
) ((INT32
) Op1
- (INT32
) Op2
));
3561 Execute the EBC MUL instruction.
3564 SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}
3566 @param VmPtr A pointer to a VM context.
3567 @param Op1 Operand 1 from the instruction
3568 @param Op2 Operand 2 from the instruction
3575 IN VM_CONTEXT
*VmPtr
,
3580 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3581 return MultS64x64 ((INT64
)Op1
, (INT64
)Op2
);
3583 return (UINT64
) ((INT64
) ((INT32
) Op1
* (INT32
) Op2
));
3589 Execute the EBC MULU instruction
3592 MULU[32|64] {@}R1, {@}R2 {Index16|Immed16}
3594 @param VmPtr A pointer to a VM context.
3595 @param Op1 Operand 1 from the instruction
3596 @param Op2 Operand 2 from the instruction
3598 @return (unsigned)Op1 * (unsigned)Op2
3603 IN VM_CONTEXT
*VmPtr
,
3608 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3609 return MultU64x64 (Op1
, Op2
);
3611 return (UINT64
) ((UINT32
) Op1
* (UINT32
) Op2
);
3617 Execute the EBC DIV instruction.
3620 DIV[32|64] {@}R1, {@}R2 {Index16|Immed16}
3622 @param VmPtr A pointer to a VM context.
3623 @param Op1 Operand 1 from the instruction
3624 @param Op2 Operand 2 from the instruction
3631 IN VM_CONTEXT
*VmPtr
,
3639 // Check for divide-by-0
3642 EbcDebugSignalException (
3643 EXCEPT_EBC_DIVIDE_ERROR
,
3644 EXCEPTION_FLAG_FATAL
,
3650 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3651 return (UINT64
) (DivS64x64Remainder (Op1
, Op2
, &Remainder
));
3653 return (UINT64
) ((INT64
) ((INT32
) Op1
/ (INT32
) Op2
));
3660 Execute the EBC DIVU instruction
3663 DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16}
3665 @param VmPtr A pointer to a VM context.
3666 @param Op1 Operand 1 from the instruction
3667 @param Op2 Operand 2 from the instruction
3669 @return (unsigned)Op1 / (unsigned)Op2
3674 IN VM_CONTEXT
*VmPtr
,
3682 // Check for divide-by-0
3685 EbcDebugSignalException (
3686 EXCEPT_EBC_DIVIDE_ERROR
,
3687 EXCEPTION_FLAG_FATAL
,
3693 // Get the destination register
3695 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3696 return (UINT64
) (DivU64x64Remainder (Op1
, Op2
, &Remainder
));
3698 return (UINT64
) ((UINT32
) Op1
/ (UINT32
) Op2
);
3705 Execute the EBC MOD instruction.
3708 MOD[32|64] {@}R1, {@}R2 {Index16|Immed16}
3710 @param VmPtr A pointer to a VM context.
3711 @param Op1 Operand 1 from the instruction
3712 @param Op2 Operand 2 from the instruction
3714 @return Op1 MODULUS Op2
3719 IN VM_CONTEXT
*VmPtr
,
3727 // Check for divide-by-0
3730 EbcDebugSignalException (
3731 EXCEPT_EBC_DIVIDE_ERROR
,
3732 EXCEPTION_FLAG_FATAL
,
3737 DivS64x64Remainder ((INT64
)Op1
, (INT64
)Op2
, &Remainder
);
3744 Execute the EBC MODU instruction.
3747 MODU[32|64] {@}R1, {@}R2 {Index16|Immed16}
3749 @param VmPtr A pointer to a VM context.
3750 @param Op1 Operand 1 from the instruction
3751 @param Op2 Operand 2 from the instruction
3753 @return Op1 UNSIGNED_MODULUS Op2
3758 IN VM_CONTEXT
*VmPtr
,
3766 // Check for divide-by-0
3769 EbcDebugSignalException (
3770 EXCEPT_EBC_DIVIDE_ERROR
,
3771 EXCEPTION_FLAG_FATAL
,
3776 DivU64x64Remainder (Op1
, Op2
, &Remainder
);
3783 Execute the EBC AND instruction.
3786 AND[32|64] {@}R1, {@}R2 {Index16|Immed16}
3788 @param VmPtr A pointer to a VM context.
3789 @param Op1 Operand 1 from the instruction
3790 @param Op2 Operand 2 from the instruction
3797 IN VM_CONTEXT
*VmPtr
,
3807 Execute the EBC OR instruction.
3810 OR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3812 @param VmPtr A pointer to a VM context.
3813 @param Op1 Operand 1 from the instruction
3814 @param Op2 Operand 2 from the instruction
3821 IN VM_CONTEXT
*VmPtr
,
3831 Execute the EBC XOR instruction.
3834 XOR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3836 @param VmPtr A pointer to a VM context.
3837 @param Op1 Operand 1 from the instruction
3838 @param Op2 Operand 2 from the instruction
3845 IN VM_CONTEXT
*VmPtr
,
3855 Execute the EBC SHL shift left instruction.
3858 SHL[32|64] {@}R1, {@}R2 {Index16|Immed16}
3860 @param VmPtr A pointer to a VM context.
3861 @param Op1 Operand 1 from the instruction
3862 @param Op2 Operand 2 from the instruction
3869 IN VM_CONTEXT
*VmPtr
,
3874 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3875 return LShiftU64 (Op1
, (UINTN
)Op2
);
3877 return (UINT64
) ((UINT32
) ((UINT32
) Op1
<< (UINT32
) Op2
));
3883 Execute the EBC SHR instruction.
3886 SHR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3888 @param VmPtr A pointer to a VM context.
3889 @param Op1 Operand 1 from the instruction
3890 @param Op2 Operand 2 from the instruction
3892 @return Op1 >> Op2 (unsigned operands)
3897 IN VM_CONTEXT
*VmPtr
,
3902 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3903 return RShiftU64 (Op1
, (UINTN
)Op2
);
3905 return (UINT64
) ((UINT32
) Op1
>> (UINT32
) Op2
);
3911 Execute the EBC ASHR instruction.
3914 ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3916 @param VmPtr A pointer to a VM context.
3917 @param Op1 Operand 1 from the instruction
3918 @param Op2 Operand 2 from the instruction
3920 @return Op1 >> Op2 (signed)
3925 IN VM_CONTEXT
*VmPtr
,
3930 if ((*VmPtr
->Ip
& DATAMANIP_M_64
) != 0) {
3931 return ARShiftU64 (Op1
, (UINTN
)Op2
);
3933 return (UINT64
) ((INT64
) ((INT32
) Op1
>> (UINT32
) Op2
));
3939 Execute the EBC EXTNDB instruction to sign-extend a byte value.
3942 EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16}
3944 @param VmPtr A pointer to a VM context.
3945 @param Op1 Operand 1 from the instruction
3946 @param Op2 Operand 2 from the instruction
3948 @return (INT64)(INT8)Op2
3953 IN VM_CONTEXT
*VmPtr
,
3961 // Convert to byte, then return as 64-bit signed value to let compiler
3962 // sign-extend the value
3965 Data64
= (INT64
) Data8
;
3967 return (UINT64
) Data64
;
3972 Execute the EBC EXTNDW instruction to sign-extend a 16-bit value.
3975 EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16}
3977 @param VmPtr A pointer to a VM context.
3978 @param Op1 Operand 1 from the instruction
3979 @param Op2 Operand 2 from the instruction
3981 @return (INT64)(INT16)Op2
3986 IN VM_CONTEXT
*VmPtr
,
3994 // Convert to word, then return as 64-bit signed value to let compiler
3995 // sign-extend the value
3997 Data16
= (INT16
) Op2
;
3998 Data64
= (INT64
) Data16
;
4000 return (UINT64
) Data64
;
4003 // Execute the EBC EXTNDD instruction.
4005 // Format: EXTNDD {@}Rx, {@}Ry [Index16|Immed16]
4006 // EXTNDD Dest, Source
4008 // Operation: Dest <- SignExtended((DWORD)Source))
4012 Execute the EBC EXTNDD instruction to sign-extend a 32-bit value.
4015 EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16}
4017 @param VmPtr A pointer to a VM context.
4018 @param Op1 Operand 1 from the instruction
4019 @param Op2 Operand 2 from the instruction
4021 @return (INT64)(INT32)Op2
4026 IN VM_CONTEXT
*VmPtr
,
4034 // Convert to 32-bit value, then return as 64-bit signed value to let compiler
4035 // sign-extend the value
4037 Data32
= (INT32
) Op2
;
4038 Data64
= (INT64
) Data32
;
4040 return (UINT64
) Data64
;
4045 Execute all the EBC signed data manipulation instructions.
4046 Since the EBC data manipulation instructions all have the same basic form,
4047 they can share the code that does the fetch of operands and the write-back
4048 of the result. This function performs the fetch of the operands (even if
4049 both are not needed to be fetched, like NOT instruction), dispatches to the
4050 appropriate subfunction, then writes back the returned result.
4053 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
4055 @param VmPtr A pointer to VM context.
4057 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
4058 @retval EFI_SUCCESS The instruction is executed successfully.
4062 ExecuteSignedDataManip (
4063 IN VM_CONTEXT
*VmPtr
4067 // Just call the data manipulation function with a flag indicating this
4068 // is a signed operation.
4070 return ExecuteDataManip (VmPtr
, TRUE
);
4075 Execute all the EBC unsigned data manipulation instructions.
4076 Since the EBC data manipulation instructions all have the same basic form,
4077 they can share the code that does the fetch of operands and the write-back
4078 of the result. This function performs the fetch of the operands (even if
4079 both are not needed to be fetched, like NOT instruction), dispatches to the
4080 appropriate subfunction, then writes back the returned result.
4083 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
4085 @param VmPtr A pointer to VM context.
4087 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
4088 @retval EFI_SUCCESS The instruction is executed successfully.
4092 ExecuteUnsignedDataManip (
4093 IN VM_CONTEXT
*VmPtr
4097 // Just call the data manipulation function with a flag indicating this
4098 // is not a signed operation.
4100 return ExecuteDataManip (VmPtr
, FALSE
);
4105 Execute all the EBC data manipulation instructions.
4106 Since the EBC data manipulation instructions all have the same basic form,
4107 they can share the code that does the fetch of operands and the write-back
4108 of the result. This function performs the fetch of the operands (even if
4109 both are not needed to be fetched, like NOT instruction), dispatches to the
4110 appropriate subfunction, then writes back the returned result.
4113 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
4115 @param VmPtr A pointer to VM context.
4116 @param IsSignedOp Indicates whether the operand is signed or not.
4118 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
4119 @retval EFI_SUCCESS The instruction is executed successfully.
4124 IN VM_CONTEXT
*VmPtr
,
4125 IN BOOLEAN IsSignedOp
4134 INTN DataManipDispatchTableIndex
;
4137 // Get opcode and operands
4139 Opcode
= GETOPCODE (VmPtr
);
4140 Operands
= GETOPERANDS (VmPtr
);
4143 // Determine if we have immediate data by the opcode
4145 if ((Opcode
& DATAMANIP_M_IMMDATA
) != 0) {
4147 // Index16 if Ry is indirect, or Immed16 if Ry direct.
4149 if (OPERAND2_INDIRECT (Operands
)) {
4150 Index16
= VmReadIndex16 (VmPtr
, 2);
4152 Index16
= VmReadImmed16 (VmPtr
, 2);
4161 // Now get operand2 (source). It's of format {@}R2 {Index16|Immed16}
4163 Op2
= (UINT64
) VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] + Index16
;
4164 if (OPERAND2_INDIRECT (Operands
)) {
4166 // Indirect form: @R2 Index16. Fetch as 32- or 64-bit data
4168 if ((Opcode
& DATAMANIP_M_64
) != 0) {
4169 Op2
= VmReadMem64 (VmPtr
, (UINTN
) Op2
);
4172 // Read as signed value where appropriate.
4175 Op2
= (UINT64
) (INT64
) ((INT32
) VmReadMem32 (VmPtr
, (UINTN
) Op2
));
4177 Op2
= (UINT64
) VmReadMem32 (VmPtr
, (UINTN
) Op2
);
4181 if ((Opcode
& DATAMANIP_M_64
) == 0) {
4183 Op2
= (UINT64
) (INT64
) ((INT32
) Op2
);
4185 Op2
= (UINT64
) ((UINT32
) Op2
);
4190 // Get operand1 (destination and sometimes also an actual operand)
4193 Op1
= (UINT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)];
4194 if (OPERAND1_INDIRECT (Operands
)) {
4195 if ((Opcode
& DATAMANIP_M_64
) != 0) {
4196 Op1
= VmReadMem64 (VmPtr
, (UINTN
) Op1
);
4199 Op1
= (UINT64
) (INT64
) ((INT32
) VmReadMem32 (VmPtr
, (UINTN
) Op1
));
4201 Op1
= (UINT64
) VmReadMem32 (VmPtr
, (UINTN
) Op1
);
4205 if ((Opcode
& DATAMANIP_M_64
) == 0) {
4207 Op1
= (UINT64
) (INT64
) ((INT32
) Op1
);
4209 Op1
= (UINT64
) ((UINT32
) Op1
);
4214 // Dispatch to the computation function
4216 DataManipDispatchTableIndex
= (Opcode
& OPCODE_M_OPCODE
) - OPCODE_NOT
;
4217 if ((DataManipDispatchTableIndex
< 0) ||
4218 (DataManipDispatchTableIndex
>= sizeof (mDataManipDispatchTable
) / sizeof (mDataManipDispatchTable
[0]))) {
4219 EbcDebugSignalException (
4220 EXCEPT_EBC_INVALID_OPCODE
,
4221 EXCEPTION_FLAG_ERROR
,
4225 // Advance and return
4228 return EFI_UNSUPPORTED
;
4230 Op2
= mDataManipDispatchTable
[DataManipDispatchTableIndex
](VmPtr
, Op1
, Op2
);
4233 // Write back the result.
4235 if (OPERAND1_INDIRECT (Operands
)) {
4236 Op1
= (UINT64
) VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)];
4237 if ((Opcode
& DATAMANIP_M_64
) != 0) {
4238 VmWriteMem64 (VmPtr
, (UINTN
) Op1
, Op2
);
4240 VmWriteMem32 (VmPtr
, (UINTN
) Op1
, (UINT32
) Op2
);
4244 // Storage back to a register. Write back, clearing upper bits (as per
4245 // the specification) if 32-bit operation.
4247 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = Op2
;
4248 if ((Opcode
& DATAMANIP_M_64
) == 0) {
4249 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] &= 0xFFFFFFFF;
4253 // Advance the instruction pointer
4261 Execute the EBC LOADSP instruction.
4266 @param VmPtr A pointer to a VM context.
4268 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
4269 @retval EFI_SUCCESS The instruction is executed successfully.
4274 IN VM_CONTEXT
*VmPtr
4282 Operands
= GETOPERANDS (VmPtr
);
4287 switch (OPERAND1_REGNUM (Operands
)) {
4293 // Spec states that this instruction will not modify reserved bits in
4294 // the flags register.
4296 VmPtr
->Flags
= (VmPtr
->Flags
&~VMFLAGS_ALL_VALID
) | (VmPtr
->Gpr
[OPERAND2_REGNUM (Operands
)] & VMFLAGS_ALL_VALID
);
4300 EbcDebugSignalException (
4301 EXCEPT_EBC_INSTRUCTION_ENCODING
,
4302 EXCEPTION_FLAG_WARNING
,
4306 return EFI_UNSUPPORTED
;
4315 Execute the EBC STORESP instruction.
4318 STORESP Rx, FLAGS|IP
4320 @param VmPtr A pointer to a VM context.
4322 @retval EFI_UNSUPPORTED The opcodes/operands is not supported.
4323 @retval EFI_SUCCESS The instruction is executed successfully.
4328 IN VM_CONTEXT
*VmPtr
4336 Operands
= GETOPERANDS (VmPtr
);
4341 switch (OPERAND2_REGNUM (Operands
)) {
4347 // Retrieve the value in the flags register, then clear reserved bits
4349 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = (UINT64
) (VmPtr
->Flags
& VMFLAGS_ALL_VALID
);
4353 // Get IP -- address of following instruction
4356 VmPtr
->Gpr
[OPERAND1_REGNUM (Operands
)] = (UINT64
) (UINTN
) VmPtr
->Ip
+ 2;
4360 EbcDebugSignalException (
4361 EXCEPT_EBC_INSTRUCTION_ENCODING
,
4362 EXCEPTION_FLAG_WARNING
,
4366 return EFI_UNSUPPORTED
;
4376 Decode a 16-bit index to determine the offset. Given an index value:
4379 b14:12 - number of bits in this index assigned to natural units (=a)
4380 ba:11 - constant units = ConstUnits
4381 b0:a - natural units = NaturalUnits
4383 Given this info, the offset can be computed by:
4384 offset = sign_bit * (ConstUnits + NaturalUnits * sizeof(UINTN))
4386 Max offset is achieved with index = 0x7FFF giving an offset of
4387 0x27B (32-bit machine) or 0x477 (64-bit machine).
4388 Min offset is achieved with index =
4390 @param VmPtr A pointer to VM context.
4391 @param CodeOffset Offset from IP of the location of the 16-bit index
4394 @return The decoded offset.
4399 IN VM_CONTEXT
*VmPtr
,
4400 IN UINT32 CodeOffset
4411 // First read the index from the code stream
4413 Index
= VmReadCode16 (VmPtr
, CodeOffset
);
4416 // Get the mask for NaturalUnits. First get the number of bits from the index.
4418 NBits
= (INT16
) ((Index
& 0x7000) >> 12);
4421 // Scale it for 16-bit indexes
4426 // Now using the number of bits, create a mask.
4428 Mask
= (INT16
) ((INT16
)~0 << NBits
);
4431 // Now using the mask, extract NaturalUnits from the lower bits of the index.
4433 NaturalUnits
= (INT16
) (Index
&~Mask
);
4436 // Now compute ConstUnits
4438 ConstUnits
= (INT16
) (((Index
&~0xF000) & Mask
) >> NBits
);
4440 Offset
= (INT16
) (NaturalUnits
* sizeof (UINTN
) + ConstUnits
);
4445 if ((Index
& 0x8000) != 0) {
4447 // Do it the hard way to work around a bogus compiler warning
4449 // Offset = -1 * Offset;
4451 Offset
= (INT16
) ((INT32
) Offset
* -1);
4459 Decode a 32-bit index to determine the offset.
4461 @param VmPtr A pointer to VM context.
4462 @param CodeOffset Offset from IP of the location of the 32-bit index
4465 @return Converted index per EBC VM specification.
4470 IN VM_CONTEXT
*VmPtr
,
4471 IN UINT32 CodeOffset
4481 Index
= VmReadImmed32 (VmPtr
, CodeOffset
);
4484 // Get the mask for NaturalUnits. First get the number of bits from the index.
4486 NBits
= (Index
& 0x70000000) >> 28;
4489 // Scale it for 32-bit indexes
4494 // Now using the number of bits, create a mask.
4496 Mask
= (INT32
)~0 << NBits
;
4499 // Now using the mask, extract NaturalUnits from the lower bits of the index.
4501 NaturalUnits
= Index
&~Mask
;
4504 // Now compute ConstUnits
4506 ConstUnits
= ((Index
&~0xF0000000) & Mask
) >> NBits
;
4508 Offset
= NaturalUnits
* sizeof (UINTN
) + ConstUnits
;
4513 if ((Index
& 0x80000000) != 0) {
4514 Offset
= Offset
* -1;
4522 Decode a 64-bit index to determine the offset.
4524 @param VmPtr A pointer to VM context.s
4525 @param CodeOffset Offset from IP of the location of the 64-bit index
4528 @return Converted index per EBC VM specification
4533 IN VM_CONTEXT
*VmPtr
,
4534 IN UINT32 CodeOffset
4544 Index
= VmReadCode64 (VmPtr
, CodeOffset
);
4547 // Get the mask for NaturalUnits. First get the number of bits from the index.
4549 NBits
= RShiftU64 ((Index
& 0x7000000000000000ULL
), 60);
4552 // Scale it for 64-bit indexes (multiply by 8 by shifting left 3)
4554 NBits
= LShiftU64 ((UINT64
)NBits
, 3);
4557 // Now using the number of bits, create a mask.
4559 Mask
= (LShiftU64 ((UINT64
)~0, (UINTN
)NBits
));
4562 // Now using the mask, extract NaturalUnits from the lower bits of the index.
4564 NaturalUnits
= Index
&~Mask
;
4567 // Now compute ConstUnits
4569 ConstUnits
= ARShiftU64 (((Index
&~0xF000000000000000ULL
) & Mask
), (UINTN
)NBits
);
4571 Offset
= MultU64x64 ((UINT64
) NaturalUnits
, sizeof (UINTN
)) + ConstUnits
;
4576 if ((Index
& 0x8000000000000000ULL
) != 0) {
4577 Offset
= MultS64x64 (Offset
, -1);
4585 Writes 8-bit data to memory address.
4587 This routine is called by the EBC data
4588 movement instructions that write to memory. Since these writes
4589 may be to the stack, which looks like (high address on top) this,
4591 [EBC entry point arguments]
4595 we need to detect all attempts to write to the EBC entry point argument
4596 stack area and adjust the address (which will initially point into the
4597 VM stack) to point into the EBC entry point arguments.
4599 @param VmPtr A pointer to a VM context.
4600 @param Addr Address to write to.
4601 @param Data Value to write to Addr.
4603 @retval EFI_SUCCESS The instruction is executed successfully.
4604 @retval Other Some error occurs when writing data to the address.
4609 IN VM_CONTEXT
*VmPtr
,
4615 // Convert the address if it's in the stack gap
4617 Addr
= ConvertStackAddr (VmPtr
, Addr
);
4618 *(UINT8
*) Addr
= Data
;
4623 Writes 16-bit data to memory address.
4625 This routine is called by the EBC data
4626 movement instructions that write to memory. Since these writes
4627 may be to the stack, which looks like (high address on top) this,
4629 [EBC entry point arguments]
4633 we need to detect all attempts to write to the EBC entry point argument
4634 stack area and adjust the address (which will initially point into the
4635 VM stack) to point into the EBC entry point arguments.
4637 @param VmPtr A pointer to a VM context.
4638 @param Addr Address to write to.
4639 @param Data Value to write to Addr.
4641 @retval EFI_SUCCESS The instruction is executed successfully.
4642 @retval Other Some error occurs when writing data to the address.
4647 IN VM_CONTEXT
*VmPtr
,
4655 // Convert the address if it's in the stack gap
4657 Addr
= ConvertStackAddr (VmPtr
, Addr
);
4660 // Do a simple write if aligned
4662 if (IS_ALIGNED (Addr
, sizeof (UINT16
))) {
4663 *(UINT16
*) Addr
= Data
;
4666 // Write as two bytes
4669 if ((Status
= VmWriteMem8 (VmPtr
, Addr
, (UINT8
) Data
)) != EFI_SUCCESS
) {
4674 if ((Status
= VmWriteMem8 (VmPtr
, Addr
+ 1, (UINT8
) (Data
>> 8))) != EFI_SUCCESS
) {
4686 Writes 32-bit data to memory address.
4688 This routine is called by the EBC data
4689 movement instructions that write to memory. Since these writes
4690 may be to the stack, which looks like (high address on top) this,
4692 [EBC entry point arguments]
4696 we need to detect all attempts to write to the EBC entry point argument
4697 stack area and adjust the address (which will initially point into the
4698 VM stack) to point into the EBC entry point arguments.
4700 @param VmPtr A pointer to a VM context.
4701 @param Addr Address to write to.
4702 @param Data Value to write to Addr.
4704 @retval EFI_SUCCESS The instruction is executed successfully.
4705 @retval Other Some error occurs when writing data to the address.
4710 IN VM_CONTEXT
*VmPtr
,
4718 // Convert the address if it's in the stack gap
4720 Addr
= ConvertStackAddr (VmPtr
, Addr
);
4723 // Do a simple write if aligned
4725 if (IS_ALIGNED (Addr
, sizeof (UINT32
))) {
4726 *(UINT32
*) Addr
= Data
;
4729 // Write as two words
4732 if ((Status
= VmWriteMem16 (VmPtr
, Addr
, (UINT16
) Data
)) != EFI_SUCCESS
) {
4737 if ((Status
= VmWriteMem16 (VmPtr
, Addr
+ sizeof (UINT16
), (UINT16
) (Data
>> 16))) != EFI_SUCCESS
) {
4749 Writes 64-bit data to memory address.
4751 This routine is called by the EBC data
4752 movement instructions that write to memory. Since these writes
4753 may be to the stack, which looks like (high address on top) this,
4755 [EBC entry point arguments]
4759 we need to detect all attempts to write to the EBC entry point argument
4760 stack area and adjust the address (which will initially point into the
4761 VM stack) to point into the EBC entry point arguments.
4763 @param VmPtr A pointer to a VM context.
4764 @param Addr Address to write to.
4765 @param Data Value to write to Addr.
4767 @retval EFI_SUCCESS The instruction is executed successfully.
4768 @retval Other Some error occurs when writing data to the address.
4773 IN VM_CONTEXT
*VmPtr
,
4781 // Convert the address if it's in the stack gap
4783 Addr
= ConvertStackAddr (VmPtr
, Addr
);
4786 // Do a simple write if aligned
4788 if (IS_ALIGNED (Addr
, sizeof (UINT64
))) {
4789 *(UINT64
*) Addr
= Data
;
4792 // Write as two 32-bit words
4795 if ((Status
= VmWriteMem32 (VmPtr
, Addr
, (UINT32
) Data
)) != EFI_SUCCESS
) {
4800 if ((Status
= VmWriteMem32 (VmPtr
, Addr
+ sizeof (UINT32
), (UINT32
) RShiftU64(Data
, 32))) != EFI_SUCCESS
) {
4812 Writes UINTN data to memory address.
4814 This routine is called by the EBC data
4815 movement instructions that write to memory. Since these writes
4816 may be to the stack, which looks like (high address on top) this,
4818 [EBC entry point arguments]
4822 we need to detect all attempts to write to the EBC entry point argument
4823 stack area and adjust the address (which will initially point into the
4824 VM stack) to point into the EBC entry point arguments.
4826 @param VmPtr A pointer to a VM context.
4827 @param Addr Address to write to.
4828 @param Data Value to write to Addr.
4830 @retval EFI_SUCCESS The instruction is executed successfully.
4831 @retval Other Some error occurs when writing data to the address.
4836 IN VM_CONTEXT
*VmPtr
,
4844 Status
= EFI_SUCCESS
;
4847 // Convert the address if it's in the stack gap
4849 Addr
= ConvertStackAddr (VmPtr
, Addr
);
4852 // Do a simple write if aligned
4854 if (IS_ALIGNED (Addr
, sizeof (UINTN
))) {
4855 *(UINTN
*) Addr
= Data
;
4857 for (Index
= 0; Index
< sizeof (UINTN
) / sizeof (UINT32
); Index
++) {
4859 Status
= VmWriteMem32 (VmPtr
, Addr
+ Index
* sizeof (UINT32
), (UINT32
) Data
);
4861 Data
= (UINTN
) RShiftU64 ((UINT64
)Data
, 32);
4870 Reads 8-bit immediate value at the offset.
4872 This routine is called by the EBC execute
4873 functions to read EBC immediate values from the code stream.
4874 Since we can't assume alignment, each tries to read in the biggest
4875 chunks size available, but will revert to smaller reads if necessary.
4877 @param VmPtr A pointer to a VM context.
4878 @param Offset offset from IP of the code bytes to read.
4880 @return Signed data of the requested size from the specified address.
4885 IN VM_CONTEXT
*VmPtr
,
4890 // Simply return the data in flat memory space
4892 return * (INT8
*) (VmPtr
->Ip
+ Offset
);
4896 Reads 16-bit immediate value at the offset.
4898 This routine is called by the EBC execute
4899 functions to read EBC immediate values from the code stream.
4900 Since we can't assume alignment, each tries to read in the biggest
4901 chunks size available, but will revert to smaller reads if necessary.
4903 @param VmPtr A pointer to a VM context.
4904 @param Offset offset from IP of the code bytes to read.
4906 @return Signed data of the requested size from the specified address.
4911 IN VM_CONTEXT
*VmPtr
,
4916 // Read direct if aligned
4918 if (IS_ALIGNED ((UINTN
) VmPtr
->Ip
+ Offset
, sizeof (INT16
))) {
4919 return * (INT16
*) (VmPtr
->Ip
+ Offset
);
4922 // All code word reads should be aligned
4924 EbcDebugSignalException (
4925 EXCEPT_EBC_ALIGNMENT_CHECK
,
4926 EXCEPTION_FLAG_WARNING
,
4931 // Return unaligned data
4933 return (INT16
) (*(UINT8
*) (VmPtr
->Ip
+ Offset
) + (*(UINT8
*) (VmPtr
->Ip
+ Offset
+ 1) << 8));
4938 Reads 32-bit immediate value at the offset.
4940 This routine is called by the EBC execute
4941 functions to read EBC immediate values from the code stream.
4942 Since we can't assume alignment, each tries to read in the biggest
4943 chunks size available, but will revert to smaller reads if necessary.
4945 @param VmPtr A pointer to a VM context.
4946 @param Offset offset from IP of the code bytes to read.
4948 @return Signed data of the requested size from the specified address.
4953 IN VM_CONTEXT
*VmPtr
,
4960 // Read direct if aligned
4962 if (IS_ALIGNED ((UINTN
) VmPtr
->Ip
+ Offset
, sizeof (UINT32
))) {
4963 return * (INT32
*) (VmPtr
->Ip
+ Offset
);
4966 // Return unaligned data
4968 Data
= (UINT32
) VmReadCode16 (VmPtr
, Offset
);
4969 Data
|= (UINT32
)(VmReadCode16 (VmPtr
, Offset
+ 2) << 16);
4975 Reads 64-bit immediate value at the offset.
4977 This routine is called by the EBC execute
4978 functions to read EBC immediate values from the code stream.
4979 Since we can't assume alignment, each tries to read in the biggest
4980 chunks size available, but will revert to smaller reads if necessary.
4982 @param VmPtr A pointer to a VM context.
4983 @param Offset offset from IP of the code bytes to read.
4985 @return Signed data of the requested size from the specified address.
4990 IN VM_CONTEXT
*VmPtr
,
4999 // Read direct if aligned
5001 if (IS_ALIGNED ((UINTN
) VmPtr
->Ip
+ Offset
, sizeof (UINT64
))) {
5002 return * (UINT64
*) (VmPtr
->Ip
+ Offset
);
5005 // Return unaligned data.
5007 Ptr
= (UINT8
*) &Data64
;
5008 Data32
= VmReadCode32 (VmPtr
, Offset
);
5009 *(UINT32
*) Ptr
= Data32
;
5010 Ptr
+= sizeof (Data32
);
5011 Data32
= VmReadCode32 (VmPtr
, Offset
+ sizeof (UINT32
));
5012 *(UINT32
*) Ptr
= Data32
;
5018 Reads 16-bit unsigned data from the code stream.
5020 This routine provides the ability to read raw unsigned data from the code
5023 @param VmPtr A pointer to VM context
5024 @param Offset Offset from current IP to the raw data to read.
5026 @return The raw unsigned 16-bit value from the code stream.
5031 IN VM_CONTEXT
*VmPtr
,
5036 // Read direct if aligned
5038 if (IS_ALIGNED ((UINTN
) VmPtr
->Ip
+ Offset
, sizeof (UINT16
))) {
5039 return * (UINT16
*) (VmPtr
->Ip
+ Offset
);
5042 // All code word reads should be aligned
5044 EbcDebugSignalException (
5045 EXCEPT_EBC_ALIGNMENT_CHECK
,
5046 EXCEPTION_FLAG_WARNING
,
5051 // Return unaligned data
5053 return (UINT16
) (*(UINT8
*) (VmPtr
->Ip
+ Offset
) + (*(UINT8
*) (VmPtr
->Ip
+ Offset
+ 1) << 8));
5058 Reads 32-bit unsigned data from the code stream.
5060 This routine provides the ability to read raw unsigned data from the code
5063 @param VmPtr A pointer to VM context
5064 @param Offset Offset from current IP to the raw data to read.
5066 @return The raw unsigned 32-bit value from the code stream.
5071 IN VM_CONTEXT
*VmPtr
,
5077 // Read direct if aligned
5079 if (IS_ALIGNED ((UINTN
) VmPtr
->Ip
+ Offset
, sizeof (UINT32
))) {
5080 return * (UINT32
*) (VmPtr
->Ip
+ Offset
);
5083 // Return unaligned data
5085 Data
= (UINT32
) VmReadCode16 (VmPtr
, Offset
);
5086 Data
|= (VmReadCode16 (VmPtr
, Offset
+ 2) << 16);
5092 Reads 64-bit unsigned data from the code stream.
5094 This routine provides the ability to read raw unsigned data from the code
5097 @param VmPtr A pointer to VM context
5098 @param Offset Offset from current IP to the raw data to read.
5100 @return The raw unsigned 64-bit value from the code stream.
5105 IN VM_CONTEXT
*VmPtr
,
5114 // Read direct if aligned
5116 if (IS_ALIGNED ((UINTN
) VmPtr
->Ip
+ Offset
, sizeof (UINT64
))) {
5117 return * (UINT64
*) (VmPtr
->Ip
+ Offset
);
5120 // Return unaligned data.
5122 Ptr
= (UINT8
*) &Data64
;
5123 Data32
= VmReadCode32 (VmPtr
, Offset
);
5124 *(UINT32
*) Ptr
= Data32
;
5125 Ptr
+= sizeof (Data32
);
5126 Data32
= VmReadCode32 (VmPtr
, Offset
+ sizeof (UINT32
));
5127 *(UINT32
*) Ptr
= Data32
;
5133 Reads 8-bit data form the memory address.
5135 @param VmPtr A pointer to VM context.
5136 @param Addr The memory address.
5138 @return The 8-bit value from the memory address.
5143 IN VM_CONTEXT
*VmPtr
,
5148 // Convert the address if it's in the stack gap
5150 Addr
= ConvertStackAddr (VmPtr
, Addr
);
5152 // Simply return the data in flat memory space
5154 return * (UINT8
*) Addr
;
5158 Reads 16-bit data form the memory address.
5160 @param VmPtr A pointer to VM context.
5161 @param Addr The memory address.
5163 @return The 16-bit value from the memory address.
5168 IN VM_CONTEXT
*VmPtr
,
5173 // Convert the address if it's in the stack gap
5175 Addr
= ConvertStackAddr (VmPtr
, Addr
);
5177 // Read direct if aligned
5179 if (IS_ALIGNED (Addr
, sizeof (UINT16
))) {
5180 return * (UINT16
*) Addr
;
5183 // Return unaligned data
5185 return (UINT16
) (*(UINT8
*) Addr
+ (*(UINT8
*) (Addr
+ 1) << 8));
5189 Reads 32-bit data form the memory address.
5191 @param VmPtr A pointer to VM context.
5192 @param Addr The memory address.
5194 @return The 32-bit value from the memory address.
5199 IN VM_CONTEXT
*VmPtr
,
5206 // Convert the address if it's in the stack gap
5208 Addr
= ConvertStackAddr (VmPtr
, Addr
);
5210 // Read direct if aligned
5212 if (IS_ALIGNED (Addr
, sizeof (UINT32
))) {
5213 return * (UINT32
*) Addr
;
5216 // Return unaligned data
5218 Data
= (UINT32
) VmReadMem16 (VmPtr
, Addr
);
5219 Data
|= (VmReadMem16 (VmPtr
, Addr
+ 2) << 16);
5224 Reads 64-bit data form the memory address.
5226 @param VmPtr A pointer to VM context.
5227 @param Addr The memory address.
5229 @return The 64-bit value from the memory address.
5234 IN VM_CONTEXT
*VmPtr
,
5242 // Convert the address if it's in the stack gap
5244 Addr
= ConvertStackAddr (VmPtr
, Addr
);
5247 // Read direct if aligned
5249 if (IS_ALIGNED (Addr
, sizeof (UINT64
))) {
5250 return * (UINT64
*) Addr
;
5253 // Return unaligned data. Assume little endian.
5255 Data32
= VmReadMem32 (VmPtr
, Addr
);
5256 Data
= (UINT64
) VmReadMem32 (VmPtr
, Addr
+ sizeof (UINT32
));
5257 Data
= LShiftU64 (Data
, 32) | Data32
;
5263 Given an address that EBC is going to read from or write to, return
5264 an appropriate address that accounts for a gap in the stack.
5265 The stack for this application looks like this (high addr on top)
5266 [EBC entry point arguments]
5269 The EBC assumes that its arguments are at the top of its stack, which
5270 is where the VM stack is really. Therefore if the EBC does memory
5271 accesses into the VM stack area, then we need to convert the address
5272 to point to the EBC entry point arguments area. Do this here.
5274 @param VmPtr A Pointer to VM context.
5275 @param Addr Address of interest
5277 @return The unchanged address if it's not in the VM stack region. Otherwise,
5278 adjust for the stack gap and return the modified address.
5283 IN VM_CONTEXT
*VmPtr
,
5287 ASSERT(((Addr
< VmPtr
->LowStackTop
) || (Addr
> VmPtr
->HighStackBottom
)));
5293 Read a natural value from memory. May or may not be aligned.
5295 @param VmPtr current VM context
5296 @param Addr the address to read from
5298 @return The natural value at address Addr.
5303 IN VM_CONTEXT
*VmPtr
,
5308 volatile UINT32 Size
;
5312 // Convert the address if it's in the stack gap
5314 Addr
= ConvertStackAddr (VmPtr
, Addr
);
5316 // Read direct if aligned
5318 if (IS_ALIGNED (Addr
, sizeof (UINTN
))) {
5319 return * (UINTN
*) Addr
;
5322 // Return unaligned data
5325 FromPtr
= (UINT8
*) Addr
;
5326 ToPtr
= (UINT8
*) &Data
;
5328 for (Size
= 0; Size
< sizeof (Data
); Size
++) {
5338 Returns the version of the EBC virtual machine.
5340 @return The 64-bit version of EBC virtual machine.
5348 return (UINT64
) (((VM_MAJOR_VERSION
& 0xFFFF) << 16) | ((VM_MINOR_VERSION
& 0xFFFF)));