1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1996-1999 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
9 * ========================================================================
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation for any purpose is hereby granted without fee,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation, and that the name of the authors not be used
16 * in advertising or publicity pertaining to distribution of the software
17 * without specific, written prior permission. The authors makes no
18 * representations about the suitability of this software for any purpose.
19 * It is provided "as is" without express or implied warranty.
21 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27 * PERFORMANCE OF THIS SOFTWARE.
29 * ========================================================================
33 * Developer: Kendall Bennett
35 * Description: This file includes subroutines which are related to
36 * programmed I/O and memory access. Included in this module
37 * are default functions with limited usefulness. For real
38 * uses these functions will most likely be overriden by the
41 ****************************************************************************/
44 #include "x86emu/x86emui.h"
45 #include "x86emu/regs.h"
46 #include "x86emu/debug.h"
47 #include "x86emu/prim_ops.h"
48 #ifndef NO_SYS_HEADERS
51 /*------------------------- Global Variables ------------------------------*/
53 X86EMU_sysEnv _X86EMU_env
; /* Global emulator machine state */
54 X86EMU_intrFuncs _X86EMU_intrTab
[256];
56 /*----------------------------- Implementation ----------------------------*/
57 #if defined(__alpha__) || defined(__alpha)
58 /* to cope with broken egcs-1.1.2 :-(((( */
62 * inline functions to do unaligned accesses
63 * from linux/include/asm-alpha/unaligned.h
67 * EGCS 1.1 knows about arbitrary unaligned loads. Define some
68 * packed structures to talk about such things with.
71 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
72 struct __una_u64
{ unsigned long x
__attribute__((packed
)); };
73 struct __una_u32
{ unsigned int x
__attribute__((packed
)); };
74 struct __una_u16
{ unsigned short x
__attribute__((packed
)); };
77 static __inline__
unsigned long ldq_u(unsigned long * r11
)
79 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
80 const struct __una_u64
*ptr
= (const struct __una_u64
*) r11
;
84 __asm__("ldq_u %0,%3\n\t"
88 :"=&r" (r1
), "=&r" (r2
)
91 "m" (*(const unsigned long *)(7+(char *) r11
)));
96 static __inline__
unsigned long ldl_u(unsigned int * r11
)
98 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
99 const struct __una_u32
*ptr
= (const struct __una_u32
*) r11
;
103 __asm__("ldq_u %0,%3\n\t"
107 :"=&r" (r1
), "=&r" (r2
)
110 "m" (*(const unsigned long *)(3+(char *) r11
)));
115 static __inline__
unsigned long ldw_u(unsigned short * r11
)
117 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
118 const struct __una_u16
*ptr
= (const struct __una_u16
*) r11
;
122 __asm__("ldq_u %0,%3\n\t"
126 :"=&r" (r1
), "=&r" (r2
)
129 "m" (*(const unsigned long *)(1+(char *) r11
)));
135 * Elemental unaligned stores
138 static __inline__
void stq_u(unsigned long r5
, unsigned long * r11
)
140 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
141 struct __una_u64
*ptr
= (struct __una_u64
*) r11
;
144 unsigned long r1
,r2
,r3
,r4
;
146 __asm__("ldq_u %3,%1\n\t"
157 "=m" (*(unsigned long *)(7+(char *) r11
)),
158 "=&r" (r1
), "=&r" (r2
), "=&r" (r3
), "=&r" (r4
)
159 :"r" (r5
), "r" (r11
));
163 static __inline__
void stl_u(unsigned long r5
, unsigned int * r11
)
165 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
166 struct __una_u32
*ptr
= (struct __una_u32
*) r11
;
169 unsigned long r1
,r2
,r3
,r4
;
171 __asm__("ldq_u %3,%1\n\t"
182 "=m" (*(unsigned long *)(3+(char *) r11
)),
183 "=&r" (r1
), "=&r" (r2
), "=&r" (r3
), "=&r" (r4
)
184 :"r" (r5
), "r" (r11
));
188 static __inline__
void stw_u(unsigned long r5
, unsigned short * r11
)
190 #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC_MINOR__ >= 91))
191 struct __una_u16
*ptr
= (struct __una_u16
*) r11
;
194 unsigned long r1
,r2
,r3
,r4
;
196 __asm__("ldq_u %3,%1\n\t"
207 "=m" (*(unsigned long *)(1+(char *) r11
)),
208 "=&r" (r1
), "=&r" (r2
), "=&r" (r3
), "=&r" (r4
)
209 :"r" (r5
), "r" (r11
));
213 #elif defined(__GNUC__) && ((__GNUC__ < 3)) && \
214 (defined (__ia64__) || defined (ia64__))
217 * EGCS 1.1 knows about arbitrary unaligned loads. Define some
218 * packed structures to talk about such things with.
220 struct __una_u64
{ unsigned long x
__attribute__((packed
)); };
221 struct __una_u32
{ unsigned int x
__attribute__((packed
)); };
222 struct __una_u16
{ unsigned short x
__attribute__((packed
)); };
224 static __inline__
unsigned long
225 __uldq (const unsigned long * r11
)
227 const struct __una_u64
*ptr
= (const struct __una_u64
*) r11
;
231 static __inline__
unsigned long
232 uldl (const unsigned int * r11
)
234 const struct __una_u32
*ptr
= (const struct __una_u32
*) r11
;
238 static __inline__
unsigned long
239 uldw (const unsigned short * r11
)
241 const struct __una_u16
*ptr
= (const struct __una_u16
*) r11
;
245 static __inline__
void
246 ustq (unsigned long r5
, unsigned long * r11
)
248 struct __una_u64
*ptr
= (struct __una_u64
*) r11
;
252 static __inline__
void
253 ustl (unsigned long r5
, unsigned int * r11
)
255 struct __una_u32
*ptr
= (struct __una_u32
*) r11
;
259 static __inline__
void
260 ustw (unsigned long r5
, unsigned short * r11
)
262 struct __una_u16
*ptr
= (struct __una_u16
*) r11
;
268 /****************************************************************************
270 addr - Emulator memory address to read
273 Byte value read from emulator memory.
276 Reads a byte value from the emulator memory.
277 ****************************************************************************/
283 if (addr
> M
.mem_size
- 1) {
284 DB(printk("mem_read: address %#lx out of range!\n", addr
);)
287 val
= *(u8
*)(M
.mem_base
+ addr
);
288 DB( if (DEBUG_MEM_TRACE())
289 printk("%#08x 1 -> %#x\n", addr
, val
);)
293 /****************************************************************************
295 addr - Emulator memory address to read
298 Word value read from emulator memory.
301 Reads a word value from the emulator memory.
302 ****************************************************************************/
308 if (addr
> M
.mem_size
- 2) {
309 DB(printk("mem_read: address %#lx out of range!\n", addr
);)
312 #ifdef __BIG_ENDIAN__
314 val
= (*(u8
*)(M
.mem_base
+ addr
) |
315 (*(u8
*)(M
.mem_base
+ addr
+ 1) << 8));
319 #if defined(ALPHA_UALOADS)
320 val
= ldw_u((u16
*)(M
.mem_base
+ addr
));
321 #elif defined(IA64_UALOADS)
322 val
= uldw((u16
*)(M
.mem_base
+ addr
));
324 val
= *(u16
*)(M
.mem_base
+ addr
);
326 DB( if (DEBUG_MEM_TRACE())
327 printk("%#08x 2 -> %#x\n", addr
, val
);)
331 /****************************************************************************
333 addr - Emulator memory address to read
336 Long value read from emulator memory.
338 Reads a long value from the emulator memory.
339 ****************************************************************************/
345 if (addr
> M
.mem_size
- 4) {
346 DB(printk("mem_read: address %#lx out of range!\n", addr
);)
349 #ifdef __BIG_ENDIAN__
351 val
= (*(u8
*)(M
.mem_base
+ addr
+ 0) |
352 (*(u8
*)(M
.mem_base
+ addr
+ 1) << 8) |
353 (*(u8
*)(M
.mem_base
+ addr
+ 2) << 16) |
354 (*(u8
*)(M
.mem_base
+ addr
+ 3) << 24));
358 #if defined(ALPHA_UALOADS)
359 val
= ldl_u((u32
*)(M
.mem_base
+ addr
));
360 #elif defined(IA64_UALOADS)
361 val
= uldl((u32
*)(M
.mem_base
+ addr
));
363 val
= *(u32
*)(M
.mem_base
+ addr
);
365 DB( if (DEBUG_MEM_TRACE())
366 printk("%#08x 4 -> %#x\n", addr
, val
);)
370 /****************************************************************************
372 addr - Emulator memory address to read
376 Writes a byte value to emulator memory.
377 ****************************************************************************/
382 DB( if (DEBUG_MEM_TRACE())
383 printk("%#08x 1 <- %#x\n", addr
, val
);)
384 if (addr
> M
.mem_size
- 1) {
385 DB(printk("mem_write: address %#lx out of range!\n", addr
);)
388 *(u8
*)(M
.mem_base
+ addr
) = val
;
391 /****************************************************************************
393 addr - Emulator memory address to read
397 Writes a word value to emulator memory.
398 ****************************************************************************/
403 DB( if (DEBUG_MEM_TRACE())
404 printk("%#08x 2 <- %#x\n", addr
, val
);)
405 if (addr
> M
.mem_size
- 2) {
406 DB(printk("mem_write: address %#lx out of range!\n", addr
);)
409 #ifdef __BIG_ENDIAN__
411 *(u8
*)(M
.mem_base
+ addr
+ 0) = (val
>> 0) & 0xff;
412 *(u8
*)(M
.mem_base
+ addr
+ 1) = (val
>> 8) & 0xff;
416 #if defined(ALPHA_UALOADS)
417 stw_u(val
,(u16
*)(M
.mem_base
+ addr
));
418 #elif defined(IA64_UALOADS)
419 ustw(val
,(u16
*)(M
.mem_base
+ addr
));
421 *(u16
*)(M
.mem_base
+ addr
) = val
;
425 /****************************************************************************
427 addr - Emulator memory address to read
431 Writes a long value to emulator memory.
432 ****************************************************************************/
437 DB( if (DEBUG_MEM_TRACE())
438 printk("%#08x 4 <- %#x\n", addr
, val
);)
439 if (addr
> M
.mem_size
- 4) {
440 DB(printk("mem_write: address %#lx out of range!\n", addr
);)
443 #ifdef __BIG_ENDIAN__
445 *(u8
*)(M
.mem_base
+ addr
+ 0) = (val
>> 0) & 0xff;
446 *(u8
*)(M
.mem_base
+ addr
+ 1) = (val
>> 8) & 0xff;
447 *(u8
*)(M
.mem_base
+ addr
+ 2) = (val
>> 16) & 0xff;
448 *(u8
*)(M
.mem_base
+ addr
+ 3) = (val
>> 24) & 0xff;
452 #if defined(ALPHA_UALOADS)
453 stl_u(val
,(u32
*)(M
.mem_base
+ addr
));
454 #elif defined(IA64_UALOADS)
455 ustl(val
,(u32
*)(M
.mem_base
+ addr
));
457 *(u32
*)(M
.mem_base
+ addr
) = val
;
461 /****************************************************************************
463 addr - PIO address to read
467 Default PIO byte read function. Doesn't perform real inb.
468 ****************************************************************************/
469 static u8 X86API
p_inb(
472 DB( if (DEBUG_IO_TRACE())
473 printk("inb %#04x \n", addr
);)
477 /****************************************************************************
479 addr - PIO address to read
483 Default PIO word read function. Doesn't perform real inw.
484 ****************************************************************************/
485 static u16 X86API
p_inw(
488 DB( if (DEBUG_IO_TRACE())
489 printk("inw %#04x \n", addr
);)
493 /****************************************************************************
495 addr - PIO address to read
499 Default PIO long read function. Doesn't perform real inl.
500 ****************************************************************************/
501 static u32 X86API
p_inl(
504 DB( if (DEBUG_IO_TRACE())
505 printk("inl %#04x \n", addr
);)
509 /****************************************************************************
511 addr - PIO address to write
514 Default PIO byte write function. Doesn't perform real outb.
515 ****************************************************************************/
516 static void X86API
p_outb(
520 DB( if (DEBUG_IO_TRACE())
521 printk("outb %#02x -> %#04x \n", val
, addr
);)
525 /****************************************************************************
527 addr - PIO address to write
530 Default PIO word write function. Doesn't perform real outw.
531 ****************************************************************************/
532 static void X86API
p_outw(
536 DB( if (DEBUG_IO_TRACE())
537 printk("outw %#04x -> %#04x \n", val
, addr
);)
541 /****************************************************************************
543 addr - PIO address to write
546 Default PIO ;ong write function. Doesn't perform real outl.
547 ****************************************************************************/
548 static void X86API
p_outl(
552 DB( if (DEBUG_IO_TRACE())
553 printk("outl %#08x -> %#04x \n", val
, addr
);)
557 /*------------------------- Global Variables ------------------------------*/
559 u8 (X86APIP sys_rdb
)(u32 addr
) = rdb
;
560 u16 (X86APIP sys_rdw
)(u32 addr
) = rdw
;
561 u32 (X86APIP sys_rdl
)(u32 addr
) = rdl
;
562 void (X86APIP sys_wrb
)(u32 addr
,u8 val
) = wrb
;
563 void (X86APIP sys_wrw
)(u32 addr
,u16 val
) = wrw
;
564 void (X86APIP sys_wrl
)(u32 addr
,u32 val
) = wrl
;
565 u8 (X86APIP sys_inb
)(X86EMU_pioAddr addr
) = p_inb
;
566 u16 (X86APIP sys_inw
)(X86EMU_pioAddr addr
) = p_inw
;
567 u32 (X86APIP sys_inl
)(X86EMU_pioAddr addr
) = p_inl
;
568 void (X86APIP sys_outb
)(X86EMU_pioAddr addr
, u8 val
) = p_outb
;
569 void (X86APIP sys_outw
)(X86EMU_pioAddr addr
, u16 val
) = p_outw
;
570 void (X86APIP sys_outl
)(X86EMU_pioAddr addr
, u32 val
) = p_outl
;
572 /*----------------------------- Setup -------------------------------------*/
574 /****************************************************************************
576 funcs - New memory function pointers to make active
579 This function is used to set the pointers to functions which access
580 memory space, allowing the user application to override these functions
581 and hook them out as necessary for their application.
582 ****************************************************************************/
583 void X86EMU_setupMemFuncs(
584 X86EMU_memFuncs
*funcs
)
586 sys_rdb
= funcs
->rdb
;
587 sys_rdw
= funcs
->rdw
;
588 sys_rdl
= funcs
->rdl
;
589 sys_wrb
= funcs
->wrb
;
590 sys_wrw
= funcs
->wrw
;
591 sys_wrl
= funcs
->wrl
;
594 /****************************************************************************
596 funcs - New programmed I/O function pointers to make active
599 This function is used to set the pointers to functions which access
600 I/O space, allowing the user application to override these functions
601 and hook them out as necessary for their application.
602 ****************************************************************************/
603 void X86EMU_setupPioFuncs(
604 X86EMU_pioFuncs
*funcs
)
606 sys_inb
= funcs
->inb
;
607 sys_inw
= funcs
->inw
;
608 sys_inl
= funcs
->inl
;
609 sys_outb
= funcs
->outb
;
610 sys_outw
= funcs
->outw
;
611 sys_outl
= funcs
->outl
;
614 /****************************************************************************
616 funcs - New interrupt vector table to make active
619 This function is used to set the pointers to functions which handle
620 interrupt processing in the emulator, allowing the user application to
621 hook interrupts as necessary for their application. Any interrupts that
622 are not hooked by the user application, and reflected and handled internally
623 in the emulator via the interrupt vector table. This allows the application
624 to get control when the code being emulated executes specific software
626 ****************************************************************************/
627 void X86EMU_setupIntrFuncs(
628 X86EMU_intrFuncs funcs
[])
632 for (i
=0; i
< 256; i
++)
633 _X86EMU_intrTab
[i
] = NULL
;
635 for (i
= 0; i
< 256; i
++)
636 _X86EMU_intrTab
[i
] = funcs
[i
];
640 /****************************************************************************
642 int - New software interrupt to prepare for
645 This function is used to set up the emulator state to exceute a software
646 interrupt. This can be used by the user application code to allow an
647 interrupt to be hooked, examined and then reflected back to the emulator
648 so that the code in the emulator will continue processing the software
649 interrupt as per normal. This essentially allows system code to actively
650 hook and handle certain software interrupts as necessary.
651 ****************************************************************************/
652 void X86EMU_prepareForInt(
655 push_word((u16
)M
.x86
.R_FLG
);
658 push_word(M
.x86
.R_CS
);
659 M
.x86
.R_CS
= mem_access_word(num
* 4 + 2);
660 push_word(M
.x86
.R_IP
);
661 M
.x86
.R_IP
= mem_access_word(num
* 4);