-
Notifications
You must be signed in to change notification settings - Fork 34
/
qemm.c
347 lines (324 loc) · 9.78 KB
/
qemm.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <fcntl.h>
#include <assert.h>
#ifdef DJGPP
#include <sys/ioctl.h>
#endif
#include "qemm.h"
#include <dpmi/dpmi.h>
#include <dpmi/dbgutil.h>
#include <untrapio.h>
#define HANDLE_IN_388H_DIRECTLY 1
int QEMM_TrapFlags;
static QEMM_IODT_LINK QEMM_IODT_header;
static QEMM_IODT_LINK* QEMM_IODT_Link = &QEMM_IODT_header;
static uint16_t QEMM_EntryIP;
static uint16_t QEMM_EntryCS;
static BOOL QEMM_InCallback;
static uint16_t QEMM_OldCallbackIP;
static uint16_t QEMM_OldCallbackCS;
static uint32_t QEMM_DOSMEM;
static void __NAKED QEMM_RM_Wrapper()
{//al=data,cl=out,dx=port
_ASM_BEGIN16
//_ASM(pushf)
//_ASM(cli)
#if HANDLE_IN_388H_DIRECTLY
_ASM(cmp dx, 0x388)
_ASM(je next)
_ASM(cmp dx, 0x389)
_ASM(jne normal)
_ASM(test cl, cl)
_ASM(jnz OUT389H)
_ASM(jmp normal) //in 389h
_ASM(next:)
_ASM(test cl, cl)
_ASM(jnz OUT388H)
_ASM(mov al, cs:[5]) //in 388h
_ASM(and al, 0x03)
_ASM(test al, 0x01)
_ASM(jz nexttimer)
_ASM(mov al, 0xC0)
_ASM(jmp ret)
_ASMLBL(nexttimer:)
_ASM(test al, 0x02)
_ASM(jz ret0)
_ASM(mov al, 0xA0)
_ASM(jmp ret)
_ASMLBL(ret0:)
_ASM(xor al,al)
_ASM(jmp ret)
_ASMLBL(OUT389H:)
_ASM(cmp byte ptr cs:[4], 4) //timer reg?
_ASM(jne normal)
_ASM(mov cs:[5], al)
_ASM(jmp normal)
_ASMLBL(OUT388H:)
_ASM(mov cs:[4], al)
_ASMLBL(normal:)
#endif
_ASM(call dword ptr cs:[0])
_ASMLBL(ret:)
//_ASM(popf)
_ASM(retf)
_ASM_END16
}
static void __NAKED QEMM_RM_WrapperEnd() {}
static DPMI_REG QEMM_TrapHandlerREG;
static void QEMM_TrapHandler()
{
uint16_t port = QEMM_TrapHandlerREG.w.dx;
uint8_t val = QEMM_TrapHandlerREG.h.al;
uint8_t out = QEMM_TrapHandlerREG.h.cl;
QEMM_IODT_LINK* link = QEMM_IODT_header.next;
//_LOG("Port trap: %s %x\n", out ? "out" : "in", port);
QEMM_TrapFlags &= ~QEMM_TF_PM;
QEMM_InCallback = TRUE;
while(link)
{
for(int i = 0; i < link->count; ++i)
{
if((link->iodt[i].port&0xFFFF) == port)
{
QEMM_TrapHandlerREG.w.flags &= ~CPU_CFLAG;
//uint8_t val2 = link->iodt[i].handler(port, val, out);
//QEMM_TrapHandlerREG.h.al = out ? QEMM_TrapHandlerREG.h.al : val2;
QEMM_TrapHandlerREG.h.al = link->iodt[i].handler(port, val, out);
return;
}
}
link = link->next;
}
QEMM_InCallback = FALSE;
//QEMM_TrapHandlerREG.w.flags |= CPU_CFLAG;
DPMI_REG r = QEMM_TrapHandlerREG;
r.w.cs = QEMM_OldCallbackCS;
r.w.ip = QEMM_OldCallbackIP;
r.w.ss = 0; r.w.sp = 0;
DPMI_CallRealModeRETF(&r);
QEMM_TrapHandlerREG.w.flags |= r.w.flags&CPU_CFLAG;
QEMM_TrapHandlerREG.h.al = r.h.al;
}
//https://www.cs.cmu.edu/~ralf/papers/qpi.txt
//https://fd.lod.bz/rbil/interrup/memory/673f_cx5145.html
//http://mirror.cs.msu.ru/oldlinux.org/Linux.old/docs/interrupts/int-html/rb-7414.htm
uint16_t QEMM_GetVersion(void)
{
//http://mirror.cs.msu.ru/oldlinux.org/Linux.old/docs/interrupts/int-html/rb-2830.htm
int fd = 0;
unsigned int result = _dos_open("QEMM386$", O_RDONLY, &fd);
uint32_t entryfar = 0;
//ioctl - read from character device control channel
DPMI_REG r = {0};
if (result == 0) //QEMM detected
{
int count = ioctl(fd, DOS_RCVDATA, 4, &entryfar);
_dos_close(fd);
if(count != 4)
return 0;
r.w.cs = entryfar>>16;
r.w.ip = entryfar&0xFFFF;
}
else //check QPIEMU for JEMM
{
/* getting the entry point of QPIEMU is non-trivial in protected-mode, since
* the int 2Fh must be executed as interrupt ( not just "simulated" ). Here
* a small ( 3 bytes ) helper proc is constructed on the fly, at 0040:00D0,
* which is the INT 2Fh, followed by an RETF.
*/
asm volatile(
"push ds \n\t"
"push ebx \n\t"
"push $0x40 \n\t"
"pop ds \n\t"
"mov $0xd0, bx \n\t"
"movl $0xcb2fcd, (bx) \n\t"
"pop ebx \n\t"
"pop ds \n\t"
);
r.w.ax = 0x1684;
r.w.bx = 0x4354;
r.w.sp = 0; r.w.ss = 0;
r.w.cs = 0x40;
r.w.ip = 0xd0;
if( DPMI_CallRealModeRETF(&r) != 0 || (r.w.ax & 0xff))
return 0;
r.w.ip = r.w.di;
r.w.cs = r.w.es;
}
r.h.ah = 0x03;
QEMM_EntryIP = r.w.ip;
QEMM_EntryCS = r.w.cs;
if( DPMI_CallRealModeRETF(&r) == 0)
return r.w.ax;
return 0;
}
BOOL QEMM_GetIOPrtTrap_Context(DPMI_REG* regs)
{
if(!QEMM_InCallback)
return FALSE;
if(QEMM_TrapFlags&QEMM_TF_PM)
return FALSE;
*regs = QEMM_TrapHandlerREG;
return TRUE;
}
BOOL QEMM_Install_IOPortTrap(QEMM_IODT* inputp iodt, uint16_t count, QEMM_IOPT* outputp iopt)
{
if(QEMM_IODT_header.next == NULL) //no entries
{
DPMI_REG r = {0};
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A06;
if(DPMI_CallRealModeRETF(&r) != 0 || (r.w.flags&CPU_CFLAG))
return FALSE;
QEMM_OldCallbackIP = r.w.es;
QEMM_OldCallbackCS = r.w.di;
//_LOG("QEMM old callback: %04x:%04x\n",r.w.es, r.w.di);
if(QEMM_DOSMEM == 0)
{
uint32_t codesize = (uintptr_t)&QEMM_RM_WrapperEnd - (uintptr_t)&QEMM_RM_Wrapper;
//_LOG("QEMM dos mem size: %d\n", codesize);
QEMM_DOSMEM = DPMI_HighMalloc((codesize + 4 + 2 + 15)>>4, TRUE);
uint32_t rmcb = DPMI_AllocateRMCB_RETF(&QEMM_TrapHandler, &QEMM_TrapHandlerREG);
if(rmcb == 0)
{
DPMI_HighFree(QEMM_DOSMEM);
QEMM_DOSMEM = 0;
return FALSE;
}
DPMI_CopyLinear(DPMI_SEGOFF2L(QEMM_DOSMEM, 0), DPMI_PTR2L(&rmcb), 4);
void* buf = malloc(codesize);
memcpy_c2d(buf, &QEMM_RM_Wrapper, codesize); //copy to ds seg in case cs&ds are not same
DPMI_CopyLinear(DPMI_SEGOFF2L(QEMM_DOSMEM, 4+2), DPMI_PTR2L(buf), codesize);
free(buf);
}
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A07;
r.w.es = QEMM_DOSMEM&0xFFFF;
r.w.di = 4+2;
if( DPMI_CallRealModeRETF(&r) != 0 || (r.w.flags&CPU_CFLAG))
{
DPMI_HighFree(QEMM_DOSMEM);
QEMM_DOSMEM = 0;
return FALSE;
}
}
assert(QEMM_IODT_Link->next == NULL);
QEMM_IODT* mem = (QEMM_IODT*)malloc(sizeof(QEMM_IODT)*count);
memcpy(mem, iodt, sizeof(QEMM_IODT)*count);
for(int i = 0; i < count; ++i)
{
DPMI_REG r = {0};
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A08;
r.w.dx = mem[i].port;
DPMI_CallRealModeRETF(&r);
mem[i].port |= (r.h.bl)<<16; //previously trapped state
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A09;
r.w.dx = mem[i].port&0xFFFF;
DPMI_CallRealModeRETF(&r); //set port trapped
}
QEMM_IODT_LINK* newlink = (QEMM_IODT_LINK*)malloc(sizeof(QEMM_IODT_LINK));
newlink->iodt = mem;
newlink->count = count;
newlink->prev = QEMM_IODT_Link;
newlink->next = NULL;
CLIS();
QEMM_IODT_Link->next = newlink;
QEMM_IODT_Link = newlink;
STIL();
iopt->memory = (uintptr_t)newlink;
return TRUE;
}
BOOL QEMM_Uninstall_IOPortTrap(QEMM_IOPT* inputp iopt)
{
CLIS();
QEMM_IODT_LINK* link = (QEMM_IODT_LINK*)iopt->memory;
link->prev->next = link->next;
if(link->next) link->next->prev = link->prev;
if(QEMM_IODT_Link == link)
QEMM_IODT_Link = link->prev;
STIL();
for(int i = 0; i < link->count; ++i)
{
if(!(link->iodt[i].port&0xFFFF0000L)) //previously not trapped
{
DPMI_REG r = {0};
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A0A; //clear trapped
r.w.dx = link->iodt[i].port&0xFFFF;
DPMI_CallRealModeRETF(&r);
}
}
free(link->iodt);
free(link);
if(QEMM_IODT_header.next == NULL) //empty
{
DPMI_REG r = {0};
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A07;
r.w.es = QEMM_OldCallbackCS;
r.w.di = QEMM_OldCallbackIP;
//DPMI_HighFree(QEMM_DOSMEM);
//QEMM_DOSMEM = 0;
if( DPMI_CallRealModeRETF(&r) != 0) //restore old handler
return FALSE;
}
return TRUE;
}
void QEMM_UntrappedIO_Write(uint16_t port, uint8_t value)
{
#if 0
if(QEMM_InCallback && port == QEMM_TrapHandlerREG.w.dx && value == QEMM_TrapHandlerREG.h.al)
{
DPMI_REG r = QEMM_TrapHandlerREG;
r.w.cs = QEMM_OldCallbackCS;
r.w.ip = QEMM_OldCallbackIP;
r.w.ss = 0; r.w.sp = 0;
DPMI_CallRealModeRETF(&r);
}
else
#endif
{
DPMI_REG r = {0};
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A01; //QPI_UntrappedIOWrite
r.h.bl = value;
r.w.dx = port;
DPMI_CallRealModeRETF(&r);
}
}
uint8_t QEMM_UntrappedIO_Read(uint16_t port)
{
#if 0
if(QEMM_InCallback && port == QEMM_TrapHandlerREG.w.dx)
{
DPMI_REG r = QEMM_TrapHandlerREG;
r.w.cs = QEMM_OldCallbackCS;
r.w.ip = QEMM_OldCallbackIP;
r.w.ss = 0; r.w.sp = 0;
DPMI_CallRealModeRETF(&r);
return r.h.al;
}
else
#endif
{
DPMI_REG r = {0};
r.w.cs = QEMM_EntryCS;
r.w.ip = QEMM_EntryIP;
r.w.ax = 0x1A00; //QPI_UntrappedIORead
r.w.dx = port;
return DPMI_CallRealModeRETF(&r) == 0 && !(r.w.flags&CPU_CFLAG) ? r.h.bl : 0;
}
}