blob: e38315afd286677828dff3dc6f6851116e98ccd8 [file] [log] [blame]
Li-Ta Lo81521262004-07-08 17:18:27 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/io.h>
5#include <sys/mman.h>
6#include <fcntl.h>
7#include <getopt.h>
Richard Smithff8c08e2006-04-23 19:16:09 +00008#include <string.h>
Li-Ta Lo81521262004-07-08 17:18:27 +00009
10#define die(x) { perror(x); exit(1); }
Ronald G. Minnich284c27f2004-11-28 04:39:45 +000011#define warn(x) { perror(x); }
Li-Ta Lo81521262004-07-08 17:18:27 +000012
13#include <x86emu.h>
Luc Verhaegene6e899d2009-05-27 11:39:16 +000014#include "helper_exec.h"
Li-Ta Lo81521262004-07-08 17:18:27 +000015#include "test.h"
16#include "pci-userspace.h"
17
18void x86emu_dump_xregs(void);
19int int15_handler(void);
20int int16_handler(void);
21int int1A_handler(void);
22#ifndef _PC
23int int42_handler(void);
24#endif
25int intE6_handler(void);
26
27void pushw(u16 val);
28
Richard Smithff8c08e2006-04-23 19:16:09 +000029unsigned short get_device(char *arg_val);
30
Li-Ta Lo81521262004-07-08 17:18:27 +000031extern int teststart, testend;
32
33_ptr p;
34ptr current = 0;
35unsigned char biosmem[1024 * 1024];
36
37int verbose = 0;
38
39
40/* Interrupt multiplexer */
41
42void do_int(int num)
43{
44 int ret = 0;
45
46 printf("int%x vector at %x\n", num, getIntVect(num));
47
48 /* This is a pInt leftover */
49 current->num = num;
50
51 switch (num) {
52#ifndef _PC
53 case 0x10:
54 case 0x42:
55 case 0x6D:
56
57 if (getIntVect(num) == 0xFF065) {
58 ret = int42_handler();
59 }
60 break;
61#endif
62 case 0x15:
63 ret = int15_handler();
64 break;
65 case 0x16:
66 ret = int16_handler();
67 break;
68 case 0x1A:
69 ret = int1A_handler();
70 break;
71 case 0xe6:
72 ret = intE6_handler();
73 break;
74 default:
75 break;
76 }
77
78 if (!ret)
79 ret = run_bios_int(num);
80
81 if (!ret) {
82 printf("\nint%x: not implemented\n", num);
Li-Ta Lo8b0356c2005-01-11 03:18:39 +000083 //x86emu_dump_xregs();
Li-Ta Lo81521262004-07-08 17:18:27 +000084 }
85}
86
87unsigned char *mapitin(char *file, off_t where, size_t size)
88{
89 void *z;
90
91 int fd = open(file, O_RDWR, 0);
92
93 if (fd < 0)
94 die(file);
95 z = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, where);
96 if (z == (void *) -1)
97 die("mmap");
98 close(fd);
99
100 return z;
101
102}
103
104u8 x_inb(u16 port);
105u16 x_inw(u16 port);
106void x_outb(u16 port, u8 val);
107void x_outw(u16 port, u16 val);
108u32 x_inl(u16 port);
109void x_outl(u16 port, u32 val);
110
111
112X86EMU_pioFuncs myfuncs = {
113 x_inb, x_inw, x_inl,
114 x_outb, x_outw, x_outl
115};
116
117
118void usage(char *name)
119{
120 printf
121 ("Usage: %s [-c codesegment] [-s size] [-b base] [-i ip] [-t] <filename> ... \n",
122 name);
123}
124
125int main(int argc, char **argv)
126{
127 char *absegname = 0;
128 void *abseg = 0;
129 int i, c, trace = 0;
130 unsigned char *cp;
131 char *filename;
132 size_t size = 0;
133 int base = 0;
134 int have_size = 0, have_base = 0, have_ip = 0, have_cs = 0;
Richard Smithff8c08e2006-04-23 19:16:09 +0000135 int have_devfn = 0;
Li-Ta Lo81521262004-07-08 17:18:27 +0000136 int parse_rom = 0;
137 char *fsegname = 0;
138 unsigned char *fsegptr;
139 unsigned short initialip = 0, initialcs = 0, devfn = 0;
140 X86EMU_intrFuncs intFuncs[256];
141 void X86EMU_setMemBase(void *base, size_t size);
142 void X86EMU_setabseg(void *abseg);
143 void x86emu_dump_xregs(void);
144 int X86EMU_set_debug(int debug);
145 int debugflag = 0;
146
147 const char *optstring = "vh?b:i:c:s:tpd:";
148 while (1) {
149 int option_index = 0;
150 static struct option long_options[] = {
151 {"verbose", 0, 0, 'v'},
152 {"help", 0, 0, 'h'},
153 {"trace", 0, 0, 't'},
154 {"base", 1, 0, 'b'},
155 {"fseg", 1, 0, 'f'},
156 {"instructionpointer", 1, 0, 'i'},
157 {"codesegment", 1, 0, 'c'},
158 {"absegment", 1, 0, 'a'},
159 {"size", 1, 0, 's'},
160 {"parserom", 0, 0, 'p'},
161 {"device", 1, 0, 'd'},
162 {"debug", 1, 0, 'D'},
163 {0, 0, 0, 0}
164 };
165 c = getopt_long(argc, argv, optstring, long_options, &option_index);
166 if (c == -1)
167 break;
168 switch (c) {
169 case 'v':
170 verbose = 1;
171 break;
172 case 'h':
173 case '?':
174 usage(argv[0]);
175 return 0;
176 case 't':
177 trace = 1;
178 break;
179 case 'b':
180 base = strtol(optarg, 0, 0);
181 have_base = 1;
182 break;
183 case 'i':
184 initialip = strtol(optarg, 0, 0);
185 have_ip = 1;
186 break;
187 case 'c':
188 initialcs = strtol(optarg, 0, 0);
189 have_cs = 1;
190 break;
191 case 's':
192 size = strtol(optarg, 0, 0);
193 have_size = 1;
194 break;
195 case 'p':
196 printf("Parsing rom images not implemented.\n");
197 parse_rom = 1;
198 break;
199 case 'f':
200 fsegname = optarg;
201 break;
202 case 'a':
203 absegname = optarg;
204 break;
205 case 'd':
Richard Smithff8c08e2006-04-23 19:16:09 +0000206 devfn = get_device(optarg);
207 have_devfn = 1;
Li-Ta Lo81521262004-07-08 17:18:27 +0000208 break;
209 case 'D':
210 debugflag = strtol(optarg, 0, 0);
211 break;
212 default:
213 printf("Unknown option \n");
214 usage(argv[0]);
215 return 1;
216 }
217 }
218
219 if (optind >= argc) {
220 printf("Filename missing.\n");
221 usage(argv[0]);
222 return 1;
223 }
224
225 while (optind < argc) {
226 printf("running file %s\n", argv[optind]);
227 filename = argv[optind];
228 optind++;
229 /* normally we would do continue, but for
230 * now only one filename is supported.
231 */
232 /* continue; */
233 break;
234 }
235
236 if (!have_size) {
237 printf("No size specified. defaulting to 32k\n");
238 size = 32 * 1024;
239 }
240 if (!have_base) {
241 printf("No base specified. defaulting to 0xc0000\n");
242 base = 0xc0000;
243 }
244 if (!have_cs) {
245 printf("No initial code segment specified. defaulting to 0xc000\n");
246 initialcs = 0xc000;
247 }
248 if (!have_ip) {
249 printf
250 ("No initial instruction pointer specified. defaulting to 0x0003\n");
251 initialip = 0x0003;
252 }
253
254 //printf("Point 1 int%x vector at %x\n", 0x42, getIntVect(0x42));
255
Richard Smithff8c08e2006-04-23 19:16:09 +0000256 if (initialip == 0x0003) {
257 if ((devfn == 0) || (have_devfn == 0)) {
258 printf("WARNING! It appears you are trying to run an option ROM.\n");
259 printf(" (initial ip = 0x0003)\n");
260 if (have_devfn) {
261 printf(" However, the device you have specified is 0x00\n");
262 printf(" It is very unlikely that your device is at this address\n");
263 printf(" Please check your -d option\n");
264 }
265 else {
266 printf(" Please specify a device with -d\n");
267 printf(" The default is not likely to work\n");
268 }
269 }
270 }
271
Li-Ta Lo81521262004-07-08 17:18:27 +0000272 if (absegname) {
273 abseg = mapitin(absegname, (off_t) 0xa0000, 0x20000);
274 if (!abseg)
275 die(absegname);
276 }
277
278 current = &p;
279 X86EMU_setMemBase(biosmem, sizeof(biosmem));
280 X86EMU_setabseg(abseg);
281 X86EMU_setupPioFuncs(&myfuncs);
282 ioperm(0, 0x400, 1);
283
Ronald G. Minnich284c27f2004-11-28 04:39:45 +0000284 if (iopl(3) < 0) {
285 warn("iopl failed, continuing anyway");
286 }
Li-Ta Lo81521262004-07-08 17:18:27 +0000287
288 /* Emergency sync ;-) */
289 sync();
290 sync();
291
292 /* Setting up interrupt environment.
293 * basically this means initializing PCI and
294 * intXX handlers.
295 */
296 pciInit();
297
298 for (i = 0; i < 256; i++)
299 intFuncs[i] = do_int;
300 X86EMU_setupIntrFuncs(intFuncs);
301 cp = mapitin(filename, (off_t) 0, size);
302
Richard Smithff8c08e2006-04-23 19:16:09 +0000303 if (devfn) {
304 printf("Loading ax with BusDevFn = %x\n",devfn);
305 }
306
Li-Ta Lo81521262004-07-08 17:18:27 +0000307 current->ax = devfn ? devfn : 0xff;
308 current->dx = 0x80;
309 // current->ip = 0;
310 for (i = 0; i < size; i++)
311 wrb(base + i, cp[i]);
312
313 if (fsegname) {
314 fsegptr = mapitin(fsegname, (off_t) 0, 0x10000);
315 for (i = 0; i < 0x10000; i++)
316 wrb(0xf0000 + i, fsegptr[i]);
317 } else {
318 char *date = "01/01/99";
319 for (i = i; date[i]; i++)
320 wrb(0xffff5 + i, date[i]);
321 wrb(0xffff7, '/');
322 wrb(0xffffa, '/');
323 }
324 /* cpu setup */
325 X86_AX = devfn ? devfn : 0xff;
326 X86_DX = 0x80;
327 X86_EIP = initialip;
328 X86_CS = initialcs;
329
330 /* Initialize stack and data segment */
331 X86_SS = 0x0030;
332 X86_DS = 0x0040;
333 X86_SP = 0xfffe;
334 /* We need a sane way to return from bios
335 * execution. A hlt instruction and a pointer
336 * to it, both kept on the stack, will do.
337 */
338 pushw(0xf4f4); /* hlt; hlt */
339 pushw(X86_SS);
340 pushw(X86_SP + 2);
341
342 X86_ES = 0x0000;
343
344 if (trace) {
345 printf("Switching to single step mode.\n");
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000346 //X86EMU_trace_on();
Li-Ta Lo81521262004-07-08 17:18:27 +0000347 }
348 if (debugflag) {
Li-Ta Lo8b0356c2005-01-11 03:18:39 +0000349 //X86EMU_set_debug(debugflag);
Li-Ta Lo81521262004-07-08 17:18:27 +0000350 }
351 X86EMU_exec();
352 /* Cleaning up */
353 pciExit();
354
355 return 0;
356}
Richard Smithff8c08e2006-04-23 19:16:09 +0000357
358unsigned short get_device(char *arg_val)
359{
360 unsigned short devfn=0;
361 long bus=0,dev=0,fn=0,need_pack=0;
362 char *tok;
363
364 tok = strsep(&arg_val,":");
365 if (arg_val != NULL) {
366 bus = strtol(tok,0,16);
367 need_pack = 1;
368 }
369 else {
370 arg_val = tok;
371 }
372
373 tok = strsep(&arg_val,".");
374 if (arg_val != NULL) {
375 dev = strtol(tok,0,16);
376 fn = strtol(arg_val,0,16);
377 need_pack = 1;
378 }
379 else {
380 if (need_pack ==1 && (strlen(tok))) {
381 dev = strtol(tok,0,16);
382 }
383 }
384
385 if ( need_pack == 1) {
386 devfn = bus<<8 | (dev<<3) | fn;
387 }
388 else {
389 devfn = strtol(tok, 0, 0);
390 }
391
392
393 return devfn;
394}
395