-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathX86Emulator.c
271 lines (223 loc) · 6.34 KB
/
X86Emulator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// Copyright (c) 2017, Linaro, Ltd. <ard.biesheuvel@linaro.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
#include "X86Emulator.h"
#include "main.h"
STATIC EFI_CPU_ARCH_PROTOCOL *mCpu;
STATIC EFI_CPU_IO2_PROTOCOL *mCpuIo2;
STATIC LIST_ENTRY mX86ImageList;
STATIC BOOLEAN gX86EmulatorIsInitialized;
X86_IMAGE_RECORD*
EFIAPI
FindImageRecord (
IN EFI_PHYSICAL_ADDRESS Address
)
{
LIST_ENTRY *Entry;
X86_IMAGE_RECORD *Record;
for (Entry = GetFirstNode (&mX86ImageList);
!IsNull (&mX86ImageList, Entry);
Entry = GetNextNode (&mX86ImageList, Entry)) {
Record = BASE_CR (Entry, X86_IMAGE_RECORD, Link);
if (Address >= Record->ImageBase &&
Address < Record->ImageBase + Record->ImageSize) {
return Record;
}
}
return NULL;
}
BOOLEAN
pc_is_native_call (
IN UINT64 Pc
)
{
return FindImageRecord ((EFI_PHYSICAL_ADDRESS)Pc) == NULL;
}
STATIC
BOOLEAN
EFIAPI
IsX86ImageSupported (
IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This,
IN UINT16 ImageType,
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL
)
{
if (ImageType != EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION &&
ImageType != EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) {
return FALSE;
}
return TRUE;
}
STATIC
EFI_STATUS
EFIAPI
RegisterX86Image (
IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This,
IN EFI_PHYSICAL_ADDRESS ImageBase,
IN UINT64 ImageSize,
IN OUT EFI_IMAGE_ENTRY_POINT *EntryPoint
)
{
X86_IMAGE_RECORD *Record;
DEBUG_CODE_BEGIN ();
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
EFI_STATUS Status;
ZeroMem (&ImageContext, sizeof (ImageContext));
ImageContext.Handle = (VOID *)(UINTN)ImageBase;
ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
Status = PeCoffLoaderGetImageInfo (&ImageContext);
if (EFI_ERROR (Status)) {
return Status;
}
ASSERT (ImageContext.Machine == EFI_IMAGE_MACHINE_X64);
ASSERT (ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION ||
ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER);
DEBUG_CODE_END ();
Record = AllocatePool (sizeof *Record);
if (Record == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Record->ImageBase = ImageBase;
Record->ImageSize = ImageSize;
InsertTailList (&mX86ImageList, &Record->Link);
return mCpu->SetMemoryAttributes (mCpu, ImageBase, ImageSize, EFI_MEMORY_XP);
}
STATIC
EFI_STATUS
EFIAPI
UnregisterX86Image (
IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This,
IN EFI_PHYSICAL_ADDRESS ImageBase
)
{
X86_IMAGE_RECORD *Record;
EFI_STATUS Status;
Record = FindImageRecord (ImageBase);
if (Record == NULL) {
return EFI_NOT_FOUND;
}
// remove non-exec protection
Status = mCpu->SetMemoryAttributes (mCpu, Record->ImageBase,
Record->ImageSize, 0);
RemoveEntryList (&Record->Link);
FreePool (Record);
return Status;
}
STATIC EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL mX86EmulatorProtocol = {
IsX86ImageSupported,
RegisterX86Image,
UnregisterX86Image,
EDKII_PECOFF_IMAGE_EMULATOR_VERSION,
EFI_IMAGE_MACHINE_X64
};
UINT8
cpu_inb (
IN UINTN addr
)
{
UINT8 Result = -1;
mCpuIo2->Io.Read(mCpuIo2, EfiCpuIoWidthUint8, addr, 1, &Result);
return Result;
}
UINT16
cpu_inw (
IN UINTN addr
)
{
UINT16 Result = -1;
mCpuIo2->Io.Read(mCpuIo2, EfiCpuIoWidthUint16, addr, 1, &Result);
return Result;
}
UINT32
cpu_inl (
IN UINTN addr
)
{
UINT32 Result = -1;
mCpuIo2->Io.Read(mCpuIo2, EfiCpuIoWidthUint32, addr, 1, &Result);
return Result;
}
VOID
cpu_outb (
IN UINTN addr,
IN UINT8 val
)
{
mCpuIo2->Io.Write(mCpuIo2, EfiCpuIoWidthUint8, addr, 1, &val);
}
VOID
cpu_outw (
IN UINTN addr,
IN UINT16 val
)
{
mCpuIo2->Io.Write(mCpuIo2, EfiCpuIoWidthUint16, addr, 1, &val);
}
VOID
cpu_outl (
IN UINTN addr,
IN UINT32 val
)
{
mCpuIo2->Io.Write(mCpuIo2, EfiCpuIoWidthUint32, addr, 1, &val);
}
UINT64
X86EmulatorVmEntry (
IN UINT64 Pc,
IN UINT64 *Args,
IN X86_IMAGE_RECORD *Record,
IN UINT64 Lr
)
{
if (!gX86EmulatorIsInitialized) {
x86emu_init();
gX86EmulatorIsInitialized = TRUE;
}
return run_x86_func((void*)Pc, (uint64_t *)Args);
}
extern EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL **stdout;
extern EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL **stderr;
UINT8 *code_gen_prologue;
EFI_STATUS
EFIAPI
X86EmulatorDxeEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Alloc;
InitializeListHead (&mX86ImageList);
Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesCode,
CODE_GEN_BUFFER_PAGES + 1, &Alloc);
if (EFI_ERROR (Status)) {
return Status;
}
code_gen_prologue = (UINT8 *)(UINTN)Alloc;
static_code_gen_buffer = code_gen_prologue + EFI_PAGE_SIZE;
Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);
ASSERT_EFI_ERROR(Status);
Status = gBS->LocateProtocol (&gEfiCpuIo2ProtocolGuid, NULL,
(VOID **)&mCpuIo2);
ASSERT_EFI_ERROR(Status);
Status = mCpu->RegisterInterruptHandler (mCpu, X86_EMU_EXCEPTION_TYPE,
&X86InterpreterSyncExceptionCallback);
if (EFI_ERROR (Status)) {
return Status;
}
Status = gBS->InstallProtocolInterface (&ImageHandle,
&gEdkiiPeCoffImageEmulatorProtocolGuid,
EFI_NATIVE_INTERFACE,
&mX86EmulatorProtocol);
if (EFI_ERROR (Status)) {
mCpu->RegisterInterruptHandler (mCpu, X86_EMU_EXCEPTION_TYPE, NULL);
}
stdout = &gST->ConOut;
stderr = &gST->StdErr;
return Status;
}