1*f334afcfSToomas Soome /** @file
2*f334afcfSToomas Soome   Describes the protocol interface to the EBC interpreter.
3*f334afcfSToomas Soome 
4*f334afcfSToomas Soome   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5*f334afcfSToomas Soome   SPDX-License-Identifier: BSD-2-Clause-Patent
6*f334afcfSToomas Soome 
7*f334afcfSToomas Soome **/
8*f334afcfSToomas Soome 
9*f334afcfSToomas Soome #ifndef __EFI_EBC_PROTOCOL_H__
10*f334afcfSToomas Soome #define __EFI_EBC_PROTOCOL_H__
11*f334afcfSToomas Soome 
12*f334afcfSToomas Soome #define EFI_EBC_INTERPRETER_PROTOCOL_GUID \
13*f334afcfSToomas Soome   { \
14*f334afcfSToomas Soome     0x13AC6DD1, 0x73D0, 0x11D4, {0xB0, 0x6B, 0x00, 0xAA, 0x00, 0xBD, 0x6D, 0xE7 } \
15*f334afcfSToomas Soome   }
16*f334afcfSToomas Soome 
17*f334afcfSToomas Soome //
18*f334afcfSToomas Soome // Define OPCODES
19*f334afcfSToomas Soome //
20*f334afcfSToomas Soome #define OPCODE_BREAK    0x00
21*f334afcfSToomas Soome #define OPCODE_JMP      0x01
22*f334afcfSToomas Soome #define OPCODE_JMP8     0x02
23*f334afcfSToomas Soome #define OPCODE_CALL     0x03
24*f334afcfSToomas Soome #define OPCODE_RET      0x04
25*f334afcfSToomas Soome #define OPCODE_CMPEQ    0x05
26*f334afcfSToomas Soome #define OPCODE_CMPLTE   0x06
27*f334afcfSToomas Soome #define OPCODE_CMPGTE   0x07
28*f334afcfSToomas Soome #define OPCODE_CMPULTE  0x08
29*f334afcfSToomas Soome #define OPCODE_CMPUGTE  0x09
30*f334afcfSToomas Soome #define OPCODE_NOT      0x0A
31*f334afcfSToomas Soome #define OPCODE_NEG      0x0B
32*f334afcfSToomas Soome #define OPCODE_ADD      0x0C
33*f334afcfSToomas Soome #define OPCODE_SUB      0x0D
34*f334afcfSToomas Soome #define OPCODE_MUL      0x0E
35*f334afcfSToomas Soome #define OPCODE_MULU     0x0F
36*f334afcfSToomas Soome #define OPCODE_DIV      0x10
37*f334afcfSToomas Soome #define OPCODE_DIVU     0x11
38*f334afcfSToomas Soome #define OPCODE_MOD      0x12
39*f334afcfSToomas Soome #define OPCODE_MODU     0x13
40*f334afcfSToomas Soome #define OPCODE_AND      0x14
41*f334afcfSToomas Soome #define OPCODE_OR       0x15
42*f334afcfSToomas Soome #define OPCODE_XOR      0x16
43*f334afcfSToomas Soome #define OPCODE_SHL      0x17
44*f334afcfSToomas Soome #define OPCODE_SHR      0x18
45*f334afcfSToomas Soome #define OPCODE_ASHR     0x19
46*f334afcfSToomas Soome #define OPCODE_EXTNDB   0x1A
47*f334afcfSToomas Soome #define OPCODE_EXTNDW   0x1B
48*f334afcfSToomas Soome #define OPCODE_EXTNDD   0x1C
49*f334afcfSToomas Soome #define OPCODE_MOVBW    0x1D
50*f334afcfSToomas Soome #define OPCODE_MOVWW    0x1E
51*f334afcfSToomas Soome #define OPCODE_MOVDW    0x1F
52*f334afcfSToomas Soome #define OPCODE_MOVQW    0x20
53*f334afcfSToomas Soome #define OPCODE_MOVBD    0x21
54*f334afcfSToomas Soome #define OPCODE_MOVWD    0x22
55*f334afcfSToomas Soome #define OPCODE_MOVDD    0x23
56*f334afcfSToomas Soome #define OPCODE_MOVQD    0x24
57*f334afcfSToomas Soome #define OPCODE_MOVSNW   0x25  // Move signed natural with word index
58*f334afcfSToomas Soome #define OPCODE_MOVSND   0x26  // Move signed natural with dword index
59*f334afcfSToomas Soome //
60*f334afcfSToomas Soome // #define OPCODE_27         0x27
61*f334afcfSToomas Soome //
62*f334afcfSToomas Soome #define OPCODE_MOVQQ     0x28 // Does this go away?
63*f334afcfSToomas Soome #define OPCODE_LOADSP    0x29
64*f334afcfSToomas Soome #define OPCODE_STORESP   0x2A
65*f334afcfSToomas Soome #define OPCODE_PUSH      0x2B
66*f334afcfSToomas Soome #define OPCODE_POP       0x2C
67*f334afcfSToomas Soome #define OPCODE_CMPIEQ    0x2D
68*f334afcfSToomas Soome #define OPCODE_CMPILTE   0x2E
69*f334afcfSToomas Soome #define OPCODE_CMPIGTE   0x2F
70*f334afcfSToomas Soome #define OPCODE_CMPIULTE  0x30
71*f334afcfSToomas Soome #define OPCODE_CMPIUGTE  0x31
72*f334afcfSToomas Soome #define OPCODE_MOVNW     0x32
73*f334afcfSToomas Soome #define OPCODE_MOVND     0x33
74*f334afcfSToomas Soome //
75*f334afcfSToomas Soome // #define OPCODE_34         0x34
76*f334afcfSToomas Soome //
77*f334afcfSToomas Soome #define OPCODE_PUSHN   0x35
78*f334afcfSToomas Soome #define OPCODE_POPN    0x36
79*f334afcfSToomas Soome #define OPCODE_MOVI    0x37
80*f334afcfSToomas Soome #define OPCODE_MOVIN   0x38
81*f334afcfSToomas Soome #define OPCODE_MOVREL  0x39
82*f334afcfSToomas Soome 
83*f334afcfSToomas Soome //
84*f334afcfSToomas Soome // Bit masks for opcode encodings
85*f334afcfSToomas Soome //
86*f334afcfSToomas Soome #define OPCODE_M_OPCODE       0x3F  // bits of interest for first level decode
87*f334afcfSToomas Soome #define OPCODE_M_IMMDATA      0x80
88*f334afcfSToomas Soome #define OPCODE_M_IMMDATA64    0x40
89*f334afcfSToomas Soome #define OPCODE_M_64BIT        0x40  // for CMP
90*f334afcfSToomas Soome #define OPCODE_M_RELADDR      0x10  // for CALL instruction
91*f334afcfSToomas Soome #define OPCODE_M_CMPI32_DATA  0x80  // for CMPI
92*f334afcfSToomas Soome #define OPCODE_M_CMPI64       0x40  // for CMPI 32 or 64 bit comparison
93*f334afcfSToomas Soome #define OPERAND_M_MOVIN_N     0x80
94*f334afcfSToomas Soome #define OPERAND_M_CMPI_INDEX  0x10
95*f334afcfSToomas Soome 
96*f334afcfSToomas Soome //
97*f334afcfSToomas Soome // Masks for instructions that encode presence of indexes for operand1 and/or
98*f334afcfSToomas Soome // operand2.
99*f334afcfSToomas Soome //
100*f334afcfSToomas Soome #define OPCODE_M_IMMED_OP1  0x80
101*f334afcfSToomas Soome #define OPCODE_M_IMMED_OP2  0x40
102*f334afcfSToomas Soome 
103*f334afcfSToomas Soome //
104*f334afcfSToomas Soome // Bit masks for operand encodings
105*f334afcfSToomas Soome //
106*f334afcfSToomas Soome #define OPERAND_M_INDIRECT1  0x08
107*f334afcfSToomas Soome #define OPERAND_M_INDIRECT2  0x80
108*f334afcfSToomas Soome #define OPERAND_M_OP1        0x07
109*f334afcfSToomas Soome #define OPERAND_M_OP2        0x70
110*f334afcfSToomas Soome 
111*f334afcfSToomas Soome //
112*f334afcfSToomas Soome // Masks for data manipulation instructions
113*f334afcfSToomas Soome //
114*f334afcfSToomas Soome #define DATAMANIP_M_64       0x40 // 64-bit width operation
115*f334afcfSToomas Soome #define DATAMANIP_M_IMMDATA  0x80
116*f334afcfSToomas Soome 
117*f334afcfSToomas Soome //
118*f334afcfSToomas Soome // For MOV instructions, need a mask for the opcode when immediate
119*f334afcfSToomas Soome // data applies to R2.
120*f334afcfSToomas Soome //
121*f334afcfSToomas Soome #define OPCODE_M_IMMED_OP2  0x40
122*f334afcfSToomas Soome 
123*f334afcfSToomas Soome //
124*f334afcfSToomas Soome // The MOVI/MOVIn instructions use bit 6 of operands byte to indicate
125*f334afcfSToomas Soome // if an index is present. Then bits 4 and 5 are used to indicate the width
126*f334afcfSToomas Soome // of the move.
127*f334afcfSToomas Soome //
128*f334afcfSToomas Soome #define MOVI_M_IMMDATA    0x40
129*f334afcfSToomas Soome #define MOVI_M_DATAWIDTH  0xC0
130*f334afcfSToomas Soome #define MOVI_DATAWIDTH16  0x40
131*f334afcfSToomas Soome #define MOVI_DATAWIDTH32  0x80
132*f334afcfSToomas Soome #define MOVI_DATAWIDTH64  0xC0
133*f334afcfSToomas Soome #define MOVI_M_MOVEWIDTH  0x30
134*f334afcfSToomas Soome #define MOVI_MOVEWIDTH8   0x00
135*f334afcfSToomas Soome #define MOVI_MOVEWIDTH16  0x10
136*f334afcfSToomas Soome #define MOVI_MOVEWIDTH32  0x20
137*f334afcfSToomas Soome #define MOVI_MOVEWIDTH64  0x30
138*f334afcfSToomas Soome 
139*f334afcfSToomas Soome //
140*f334afcfSToomas Soome // Masks for CALL instruction encodings
141*f334afcfSToomas Soome //
142*f334afcfSToomas Soome #define OPERAND_M_RELATIVE_ADDR  0x10
143*f334afcfSToomas Soome #define OPERAND_M_NATIVE_CALL    0x20
144*f334afcfSToomas Soome 
145*f334afcfSToomas Soome //
146*f334afcfSToomas Soome // Masks for decoding push/pop instructions
147*f334afcfSToomas Soome //
148*f334afcfSToomas Soome #define PUSHPOP_M_IMMDATA  0x80 // opcode bit indicating immediate data
149*f334afcfSToomas Soome #define PUSHPOP_M_64       0x40 // opcode bit indicating 64-bit operation
150*f334afcfSToomas Soome //
151*f334afcfSToomas Soome // Mask for operand of JMP instruction
152*f334afcfSToomas Soome //
153*f334afcfSToomas Soome #define JMP_M_RELATIVE     0x10
154*f334afcfSToomas Soome #define JMP_M_CONDITIONAL  0x80
155*f334afcfSToomas Soome #define JMP_M_CS           0x40
156*f334afcfSToomas Soome 
157*f334afcfSToomas Soome //
158*f334afcfSToomas Soome // Macros to determine if a given operand is indirect
159*f334afcfSToomas Soome //
160*f334afcfSToomas Soome #define OPERAND1_INDIRECT(op)  ((op) & OPERAND_M_INDIRECT1)
161*f334afcfSToomas Soome #define OPERAND2_INDIRECT(op)  ((op) & OPERAND_M_INDIRECT2)
162*f334afcfSToomas Soome 
163*f334afcfSToomas Soome //
164*f334afcfSToomas Soome // Macros to extract the operands from second byte of instructions
165*f334afcfSToomas Soome //
166*f334afcfSToomas Soome #define OPERAND1_REGNUM(op)  ((op) & OPERAND_M_OP1)
167*f334afcfSToomas Soome #define OPERAND2_REGNUM(op)  (((op) & OPERAND_M_OP2) >> 4)
168*f334afcfSToomas Soome 
169*f334afcfSToomas Soome #define OPERAND1_CHAR(op)  ('0' + OPERAND1_REGNUM (op))
170*f334afcfSToomas Soome #define OPERAND2_CHAR(op)  ('0' + OPERAND2_REGNUM (op))
171*f334afcfSToomas Soome 
172*f334afcfSToomas Soome //
173*f334afcfSToomas Soome // Condition masks usually for byte 1 encodings of code
174*f334afcfSToomas Soome //
175*f334afcfSToomas Soome #define CONDITION_M_CONDITIONAL  0x80
176*f334afcfSToomas Soome #define CONDITION_M_CS           0x40
177*f334afcfSToomas Soome 
178*f334afcfSToomas Soome ///
179*f334afcfSToomas Soome /// Protocol Guid Name defined in spec.
180*f334afcfSToomas Soome ///
181*f334afcfSToomas Soome #define EFI_EBC_PROTOCOL_GUID  EFI_EBC_INTERPRETER_PROTOCOL_GUID
182*f334afcfSToomas Soome 
183*f334afcfSToomas Soome ///
184*f334afcfSToomas Soome /// Define for forward reference.
185*f334afcfSToomas Soome ///
186*f334afcfSToomas Soome typedef struct _EFI_EBC_PROTOCOL EFI_EBC_PROTOCOL;
187*f334afcfSToomas Soome 
188*f334afcfSToomas Soome /**
189*f334afcfSToomas Soome   Creates a thunk for an EBC entry point, returning the address of the thunk.
190*f334afcfSToomas Soome 
191*f334afcfSToomas Soome   A PE32+ EBC image, like any other PE32+ image, contains an optional header that specifies the
192*f334afcfSToomas Soome   entry point for image execution. However, for EBC images, this is the entry point of EBC
193*f334afcfSToomas Soome   instructions, so is not directly executable by the native processor. Therefore, when an EBC image is
194*f334afcfSToomas Soome   loaded, the loader must call this service to get a pointer to native code (thunk) that can be executed,
195*f334afcfSToomas Soome   which will invoke the interpreter to begin execution at the original EBC entry point.
196*f334afcfSToomas Soome 
197*f334afcfSToomas Soome   @param  This          A pointer to the EFI_EBC_PROTOCOL instance.
198*f334afcfSToomas Soome   @param  ImageHandle   Handle of image for which the thunk is being created.
199*f334afcfSToomas Soome   @param  EbcEntryPoint Address of the actual EBC entry point or protocol service the thunk should call.
200*f334afcfSToomas Soome   @param  Thunk         Returned pointer to a thunk created.
201*f334afcfSToomas Soome 
202*f334afcfSToomas Soome   @retval EFI_SUCCESS            The function completed successfully.
203*f334afcfSToomas Soome   @retval EFI_INVALID_PARAMETER  Image entry point is not 2-byte aligned.
204*f334afcfSToomas Soome   @retval EFI_OUT_OF_RESOURCES   Memory could not be allocated for the thunk.
205*f334afcfSToomas Soome **/
206*f334afcfSToomas Soome typedef
207*f334afcfSToomas Soome EFI_STATUS
208*f334afcfSToomas Soome (EFIAPI *EFI_EBC_CREATE_THUNK)(
209*f334afcfSToomas Soome   IN EFI_EBC_PROTOCOL           *This,
210*f334afcfSToomas Soome   IN EFI_HANDLE                 ImageHandle,
211*f334afcfSToomas Soome   IN VOID                       *EbcEntryPoint,
212*f334afcfSToomas Soome   OUT VOID                      **Thunk
213*f334afcfSToomas Soome   );
214*f334afcfSToomas Soome 
215*f334afcfSToomas Soome /**
216*f334afcfSToomas Soome   Called prior to unloading an EBC image from memory.
217*f334afcfSToomas Soome 
218*f334afcfSToomas Soome   This function is called after an EBC image has exited, but before the image is actually unloaded. It
219*f334afcfSToomas Soome   is intended to provide the interpreter with the opportunity to perform any cleanup that may be
220*f334afcfSToomas Soome   necessary as a result of loading and executing the image.
221*f334afcfSToomas Soome 
222*f334afcfSToomas Soome   @param  This          A pointer to the EFI_EBC_PROTOCOL instance.
223*f334afcfSToomas Soome   @param  ImageHandle   Image handle of the EBC image that is being unloaded from memory.
224*f334afcfSToomas Soome 
225*f334afcfSToomas Soome   @retval EFI_SUCCESS            The function completed successfully.
226*f334afcfSToomas Soome   @retval EFI_INVALID_PARAMETER  Image handle is not recognized as belonging
227*f334afcfSToomas Soome                                  to an EBC image that has been executed.
228*f334afcfSToomas Soome **/
229*f334afcfSToomas Soome typedef
230*f334afcfSToomas Soome EFI_STATUS
231*f334afcfSToomas Soome (EFIAPI *EFI_EBC_UNLOAD_IMAGE)(
232*f334afcfSToomas Soome   IN EFI_EBC_PROTOCOL           *This,
233*f334afcfSToomas Soome   IN EFI_HANDLE                 ImageHandle
234*f334afcfSToomas Soome   );
235*f334afcfSToomas Soome 
236*f334afcfSToomas Soome /**
237*f334afcfSToomas Soome   This is the prototype for the Flush callback routine. A pointer to a routine
238*f334afcfSToomas Soome   of this type is passed to the EBC EFI_EBC_REGISTER_ICACHE_FLUSH protocol service.
239*f334afcfSToomas Soome 
240*f334afcfSToomas Soome   @param  Start  The beginning physical address to flush from the processor's instruction cache.
241*f334afcfSToomas Soome   @param  Length The number of bytes to flush from the processor's instruction cache.
242*f334afcfSToomas Soome 
243*f334afcfSToomas Soome   @retval EFI_SUCCESS            The function completed successfully.
244*f334afcfSToomas Soome 
245*f334afcfSToomas Soome **/
246*f334afcfSToomas Soome typedef
247*f334afcfSToomas Soome EFI_STATUS
248*f334afcfSToomas Soome (EFIAPI *EBC_ICACHE_FLUSH)(
249*f334afcfSToomas Soome   IN EFI_PHYSICAL_ADDRESS     Start,
250*f334afcfSToomas Soome   IN UINT64                   Length
251*f334afcfSToomas Soome   );
252*f334afcfSToomas Soome 
253*f334afcfSToomas Soome /**
254*f334afcfSToomas Soome   Registers a callback function that the EBC interpreter calls to flush
255*f334afcfSToomas Soome   the processor instruction cache following creation of thunks.
256*f334afcfSToomas Soome 
257*f334afcfSToomas Soome   @param  This       A pointer to the EFI_EBC_PROTOCOL instance.
258*f334afcfSToomas Soome   @param  Flush      Pointer to a function of type EBC_ICACH_FLUSH.
259*f334afcfSToomas Soome 
260*f334afcfSToomas Soome   @retval EFI_SUCCESS            The function completed successfully.
261*f334afcfSToomas Soome 
262*f334afcfSToomas Soome **/
263*f334afcfSToomas Soome typedef
264*f334afcfSToomas Soome EFI_STATUS
265*f334afcfSToomas Soome (EFIAPI *EFI_EBC_REGISTER_ICACHE_FLUSH)(
266*f334afcfSToomas Soome   IN EFI_EBC_PROTOCOL           *This,
267*f334afcfSToomas Soome   IN EBC_ICACHE_FLUSH           Flush
268*f334afcfSToomas Soome   );
269*f334afcfSToomas Soome 
270*f334afcfSToomas Soome /**
271*f334afcfSToomas Soome   Called to get the version of the interpreter.
272*f334afcfSToomas Soome 
273*f334afcfSToomas Soome   This function is called to get the version of the loaded EBC interpreter. The value and format of the
274*f334afcfSToomas Soome   returned version is identical to that returned by the EBC BREAK 1 instruction.
275*f334afcfSToomas Soome 
276*f334afcfSToomas Soome   @param  This       A pointer to the EFI_EBC_PROTOCOL instance.
277*f334afcfSToomas Soome   @param  Version    Pointer to where to store the returned version of the interpreter.
278*f334afcfSToomas Soome 
279*f334afcfSToomas Soome   @retval EFI_SUCCESS            The function completed successfully.
280*f334afcfSToomas Soome   @retval EFI_INVALID_PARAMETER  Version pointer is NULL.
281*f334afcfSToomas Soome 
282*f334afcfSToomas Soome **/
283*f334afcfSToomas Soome typedef
284*f334afcfSToomas Soome EFI_STATUS
285*f334afcfSToomas Soome (EFIAPI *EFI_EBC_GET_VERSION)(
286*f334afcfSToomas Soome   IN EFI_EBC_PROTOCOL           *This,
287*f334afcfSToomas Soome   IN OUT UINT64                 *Version
288*f334afcfSToomas Soome   );
289*f334afcfSToomas Soome 
290*f334afcfSToomas Soome ///
291*f334afcfSToomas Soome /// The EFI EBC protocol provides services to load and execute EBC images, which will typically be
292*f334afcfSToomas Soome /// loaded into option ROMs. The image loader will load the EBC image, perform standard relocations,
293*f334afcfSToomas Soome /// and invoke the CreateThunk() service to create a thunk for the EBC image's entry point. The
294*f334afcfSToomas Soome /// image can then be run using the standard EFI start image services.
295*f334afcfSToomas Soome ///
296*f334afcfSToomas Soome struct _EFI_EBC_PROTOCOL {
297*f334afcfSToomas Soome   EFI_EBC_CREATE_THUNK             CreateThunk;
298*f334afcfSToomas Soome   EFI_EBC_UNLOAD_IMAGE             UnloadImage;
299*f334afcfSToomas Soome   EFI_EBC_REGISTER_ICACHE_FLUSH    RegisterICacheFlush;
300*f334afcfSToomas Soome   EFI_EBC_GET_VERSION              GetVersion;
301*f334afcfSToomas Soome };
302*f334afcfSToomas Soome 
303*f334afcfSToomas Soome //
304*f334afcfSToomas Soome // Extern the global EBC protocol GUID
305*f334afcfSToomas Soome //
306*f334afcfSToomas Soome extern EFI_GUID  gEfiEbcProtocolGuid;
307*f334afcfSToomas Soome 
308*f334afcfSToomas Soome #endif
309