summaryrefslogtreecommitdiffstats
path: root/ddcprobe/bioscall.c
blob: f58b3b6c09420d9e2f447e468bca063450cfb77c (plain)
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
347
348
349
350
#include <sys/types.h>
#include <sys/io.h>
#include <sys/stat.h>
#include <sys/vm86.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <netinet/in.h>
#include "bioscall.h"
#ident "$Id$"

#define DFLAG 0x0400
#define IFLAG 0x0200
#define TFLAG 0x0100
#define SFLAG 0x0080
#define ZFLAG 0x0040
#define AFLAG 0x0010
#define PFLAG 0x0004
#define CFLAG 0x0001

/* Dump some of the interesting parts of a register struct to stdout. */
void dump_regs(struct vm86_regs *regs)
{
	printf("ax = 0x%04lx\n", regs->eax & 0xffff);
	printf("bx = 0x%04lx\n", regs->ebx & 0xffff);
	printf("cx = 0x%04lx\n", regs->ecx & 0xffff);
	printf("dx = 0x%04lx\n", regs->edx & 0xffff);
	printf("cs = 0x%04x\n", regs->cs  & 0xffff);
	printf("ip = 0x%08lx\n", regs->eip & 0xffffffff);
	printf("ss = 0x%04x\n", regs->ss  & 0xffff);
	printf("sp = 0x%08lx\n", regs->esp & 0xffffffff);
	printf("%04x:%08lx = (%ld)\n",
	       regs->cs & 0xffff, regs->eip,
	       regs->cs * 16 + regs->eip);
}

/* Call vm86, but do I/O that gets trapped. We could skip vm86() altogether,
   but then I'm not trying to emulate an entire CPU here.  Luckily, none of
   the I/O instructions (or push/pop) affect the flags, so we can leave them
   alone and just deal with performing the I/O operation that caused a return
   to 32-bit mode. */
void do_vm86(struct vm86_struct *vm, char *memory, unsigned stop_eip) {
	int ret;
	unsigned start_cs, start_eip;
	unsigned char *ip = NULL;

	/* Save the starting instruction address. */
	start_cs = vm->regs.cs;
	start_eip = vm->regs.eip;

	/* We'll need to pass I/O through.  PCI devices have higher addresses
	   than we can get access to with ioperm(). */
	if(iopl(3) != 0) {
		return;
	}

	/* Do it. */
	ret = syscall(SYS_vm86old, vm);
	while((vm->regs.cs * 16 + vm->regs.eip) != (start_cs * 16 + stop_eip)) {
		ip = &memory[vm->regs.cs * 16 + vm->regs.eip];
#ifdef DEBUG2
		printf("Unexpected return:\n");
		dump_regs(&vm->regs);
		printf("Offending instructions: %02x %02x %02x %02x\n",
		       ip[0], ip[1], ip[2], ip[3]);
#endif
		switch(ip[0]) {
			case 0xe4: { /* in al, literal */
				vm->regs.eax &= 0xffffff00;
				vm->regs.eax |= inb(ip[1]);
				vm->regs.eip += 2;
				break;
			}
			case 0xe6: { /* out al, literal */
				outb(vm->regs.eax & 0xff, ip[1]);
				vm->regs.eip += 2;
				break;
			}
			case 0xec: { /* in al, dx */
				vm->regs.eax &= 0xffffff00;
				vm->regs.eax |= inb(vm->regs.edx & 0xffff);
				vm->regs.eip++;
				break;
			}
			case 0xed: { /* in ax, dx */
				vm->regs.eax &= 0xffff0000;
				vm->regs.eax |= inw(vm->regs.edx & 0xffff);
				vm->regs.eip++;
				break;
			}
			case 0xee: { /* out al, dx */
				outb(vm->regs.eax & 0xff,
				     vm->regs.edx & 0xffff);
				vm->regs.eip++;
				break;
			}
			case 0xef: { /* out ax, dx */
				outw(vm->regs.eax & 0xffff,
				     vm->regs.edx & 0xffff);
				vm->regs.eip++;
				break;
			}
			case 0x6c: { /* insb */
				unsigned char *result = (unsigned char*)
					&memory[vm->regs.es*16 + vm->regs.edi];
				*result = inb(vm->regs.edx & 0xffff);
				if(vm->regs.eflags & DFLAG) {
					vm->regs.edi -= 1;
				} else {
					vm->regs.edi += 1;
				}
				if(ip[-1] == 0xf3) { /* rep'ped */
					vm->regs.ecx--;
					vm->regs.eip--;
				} else {
					vm->regs.eip++;
				}
				break;
			}
			case 0x6d: { /* insw */
				u_int16_t *result = (u_int16_t*)
					&memory[vm->regs.es*16 + vm->regs.edi];
				*result = inw(vm->regs.edx & 0xffff);
				if(vm->regs.eflags & DFLAG) {
					vm->regs.edi -= 2;
				} else {
					vm->regs.edi += 2;
				}
				if(ip[-1] == 0xf3) { /* rep'ped */
					vm->regs.ecx--;
					vm->regs.eip--;
				} else {
					vm->regs.eip++;
				}
				break;
			}
			case 0x6e: { /* outsb */
				unsigned char *result = (unsigned char*)
					&memory[vm->regs.es*16 + vm->regs.edi];
				outb(*result,
				     vm->regs.edx & 0xffff);
				if(vm->regs.eflags & DFLAG) {
					vm->regs.edi -= 1;
				} else {
					vm->regs.edi += 1;
				}
				if(ip[-1] == 0xf3) { /* rep'ped */
					vm->regs.ecx--;
					vm->regs.eip--;
				} else {
					vm->regs.eip++;
				}
				break;
			}
			case 0x6f: { /* outsw */
				u_int16_t *result = (u_int16_t*)
					&memory[vm->regs.es*16 + vm->regs.edi];
				outw(*result,
				     vm->regs.edx & 0xffff);
				if(vm->regs.eflags & DFLAG) {
					vm->regs.edi -= 2;
				} else {
					vm->regs.edi += 2;
				}
				if(ip[-1] == 0xf3) { /* rep'ped */
					vm->regs.ecx--;
					vm->regs.eip--;
				} else {
					vm->regs.eip++;
				}
				break;
			}
			case 0xfa: { /* cli */
				vm->regs.eflags &= ~(IFLAG);
				vm->regs.eip++;
				break;
			}
			case 0xfb: { /* sti */
				vm->regs.eflags |= ~(IFLAG);
				vm->regs.eip++;
				break;
			}
			case 0x9c: { /* pushf */
				vm->regs.esp -= 2;
				*(u_int16_t*) &memory[vm->regs.ss * 16 +
						      vm->regs.esp]
						    = vm->regs.eflags & 0xffff;
				vm->regs.eip++;
				break;
			}
			case 0x9d: { /* popf */
				vm->regs.esp += 2;
				vm->regs.eflags &= 0xffff0000;
				vm->regs.eflags |= 
				*(u_int16_t*) &memory[vm->regs.ss * 16 +
						      vm->regs.esp];
				vm->regs.eip++;
				break;
			}
			case 0xf0: { /* lock prefix */
				/* ignore it */
				vm->regs.eip++;
				break;
			}
			case 0x66: {
				/* 32-bit extension prefix.  Valid, even in
				   v86 mode.  Weird. */
				vm->regs.eip++;
				ip++;
				switch(ip[0]) {
					case 0xed: { /* in eax, dx */
						vm->regs.eax =
						inl(vm->regs.edx & 0xffff);
						vm->regs.eip++;
						break;
					}
					case 0xef: { /* out eax, dx */
						outl(vm->regs.eax,
						     vm->regs.edx & 0xffff);
						vm->regs.eip++;
						break;
					}
					default: {
						fprintf(stderr, "unhandled "
							"32-bit opcode\n");
						exit(1);
					}
				}
				break;
			}
			case 0x55: { /* push bp */
				vm->regs.esp -= 2;
				*(u_int16_t*) &memory[vm->regs.ss * 16 +
						      vm->regs.esp]
						    = vm->regs.ebp & 0xffff;
				vm->regs.eip++;
				break;
			}
			case 0x5d: { /* pop bp */
				vm->regs.ebp &= 0xffff0000;
				vm->regs.ebp |= *(u_int16_t*)
					&memory[vm->regs.ss*16 + vm->regs.esp];
				vm->regs.esp += 2;
				vm->regs.eip++;
				break;
			}
			case 0x59: { /* pop cx -- Banshee */
				vm->regs.ecx &= 0xffff0000;
				vm->regs.ecx |= *(u_int16_t*)
					&memory[vm->regs.ss*16 + vm->regs.esp];
				vm->regs.esp += 2;
				vm->regs.eip++;
			}
			case 0xc3: { /* ret near, just pop ip */
				vm->regs.eip &= 0xffff0000;
				vm->regs.eip |= *(u_int16_t*)
					&memory[vm->regs.ss*16 + vm->regs.esp];
				vm->regs.esp += 2;
				break;
			}
			case 0xcb: { /* ret far, pop both ip and cs */
				vm->regs.eip &= 0xffff0000;
				vm->regs.eip |= *(u_int16_t*)
					&memory[vm->regs.ss*16 + vm->regs.esp];
				vm->regs.esp += 2;
				vm->regs.cs = *(u_int16_t*)
					&memory[vm->regs.ss*16 + vm->regs.esp];
				vm->regs.esp += 2;
				break;
			}
			default: {
				fprintf(stderr, "Unexpected stop!\n");
				dump_regs(&vm->regs);
				printf("Offending instructions: %02x %02x %02x %02x\n",
				       ip[0], ip[1], ip[2], ip[3]);
				exit(1);
			}
		}
		ip = &memory[vm->regs.cs * 16 + vm->regs.eip];
#ifdef DEBUG
		printf("Resuming execution:\n");
		dump_regs(&vm->regs);
		printf("Offending instructions: %02x %02x %02x %02x\n",
		       ip[0], ip[1], ip[2], ip[3]);
#endif
		ret = syscall(SYS_vm86old, vm);
	}
#ifdef DEBUG
	printf("Reached stopping point, returning.\n");
#endif
	return;
}

/* Get a snapshot of the first megabyte of memory for use with vm86. */
unsigned char *vm86_ram_alloc()
{
	unsigned char *memory;
	int fd;

	/* Grab address 0 for this process.  mmap() 1 megabyte + 64k HMA */
	memory = mmap(0, 0x110000, PROT_READ | PROT_EXEC | PROT_WRITE,
		      MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0x00000);
	if(memory == MAP_FAILED) {
		perror("error mmap()ing memory for the BIOS");
		return MAP_FAILED;
	}

	/* Copy the low megabyte to our mmap()'ed buffer. */
	fd = open("/dev/mem", O_RDONLY);
	if(fd == -1) {
		perror("reading kernel memory");
		return MAP_FAILED;
	}
	// read(fd, memory, 0x110000);
	lseek(fd, 0, SEEK_SET);
	read(fd, &memory[0], 0x10000);
	lseek(fd, 0xa0000, SEEK_SET);
	read(fd, &memory[0xa0000], 0x50000);
	close(fd);

	return memory;
}

void vm86_ram_free(unsigned char *ram)
{
	munmap(ram, 0x110000);
}

void bioscall(unsigned char int_no, struct vm86_regs *regs, unsigned char *mem)
{
	unsigned char call[] = {0xcd, int_no, 0xcd, 0x09};
	struct vm86_struct vm;
	memset(&vm, 0, sizeof(vm));
	memcpy(&vm.regs, regs, sizeof(vm.regs));
	vm.regs.cs  = BIOSCALL_START_SEG;
	vm.regs.eip = BIOSCALL_START_OFS;
	vm.regs.ss  = BIOSCALL_START_SEG;
	vm.regs.esp = 0xfff0 - BIOSCALL_START_OFS;
	vm.regs.eflags = VM_MASK | IOPL_MASK;
	memcpy(&mem[BIOSCALL_START_SEG * 16 + BIOSCALL_START_OFS], call,
	       sizeof(call));
	do_vm86(&vm, mem, BIOSCALL_START_OFS + sizeof(call));
	memcpy(regs, &vm.regs, sizeof(vm.regs));
}