First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / x86emu / sys.c
blob4d90ea315982bb6646073ceb7b3a0d0bae6061a9
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 * ========================================================================
31 * Language: ANSI C
32 * Environment: Any
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
39 * user library.
41 ****************************************************************************/
43 #include "x86emu.h"
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
49 #include <string.h>
50 #endif
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 :-(((( */
60 #define ALPHA_UALOADS
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)); };
75 #endif
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;
81 return ptr->x;
82 #else
83 unsigned long r1,r2;
84 __asm__("ldq_u %0,%3\n\t"
85 "ldq_u %1,%4\n\t"
86 "extql %0,%2,%0\n\t"
87 "extqh %1,%2,%1"
88 :"=&r" (r1), "=&r" (r2)
89 :"r" (r11),
90 "m" (*r11),
91 "m" (*(const unsigned long *)(7+(char *) r11)));
92 return r1 | r2;
93 #endif
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;
100 return ptr->x;
101 #else
102 unsigned long r1,r2;
103 __asm__("ldq_u %0,%3\n\t"
104 "ldq_u %1,%4\n\t"
105 "extll %0,%2,%0\n\t"
106 "extlh %1,%2,%1"
107 :"=&r" (r1), "=&r" (r2)
108 :"r" (r11),
109 "m" (*r11),
110 "m" (*(const unsigned long *)(3+(char *) r11)));
111 return r1 | r2;
112 #endif
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;
119 return ptr->x;
120 #else
121 unsigned long r1,r2;
122 __asm__("ldq_u %0,%3\n\t"
123 "ldq_u %1,%4\n\t"
124 "extwl %0,%2,%0\n\t"
125 "extwh %1,%2,%1"
126 :"=&r" (r1), "=&r" (r2)
127 :"r" (r11),
128 "m" (*r11),
129 "m" (*(const unsigned long *)(1+(char *) r11)));
130 return r1 | r2;
131 #endif
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;
142 ptr->x = r5;
143 #else
144 unsigned long r1,r2,r3,r4;
146 __asm__("ldq_u %3,%1\n\t"
147 "ldq_u %2,%0\n\t"
148 "insqh %6,%7,%5\n\t"
149 "insql %6,%7,%4\n\t"
150 "mskqh %3,%7,%3\n\t"
151 "mskql %2,%7,%2\n\t"
152 "bis %3,%5,%3\n\t"
153 "bis %2,%4,%2\n\t"
154 "stq_u %3,%1\n\t"
155 "stq_u %2,%0"
156 :"=m" (*r11),
157 "=m" (*(unsigned long *)(7+(char *) r11)),
158 "=&r" (r1), "=&r" (r2), "=&r" (r3), "=&r" (r4)
159 :"r" (r5), "r" (r11));
160 #endif
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;
167 ptr->x = r5;
168 #else
169 unsigned long r1,r2,r3,r4;
171 __asm__("ldq_u %3,%1\n\t"
172 "ldq_u %2,%0\n\t"
173 "inslh %6,%7,%5\n\t"
174 "insll %6,%7,%4\n\t"
175 "msklh %3,%7,%3\n\t"
176 "mskll %2,%7,%2\n\t"
177 "bis %3,%5,%3\n\t"
178 "bis %2,%4,%2\n\t"
179 "stq_u %3,%1\n\t"
180 "stq_u %2,%0"
181 :"=m" (*r11),
182 "=m" (*(unsigned long *)(3+(char *) r11)),
183 "=&r" (r1), "=&r" (r2), "=&r" (r3), "=&r" (r4)
184 :"r" (r5), "r" (r11));
185 #endif
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;
192 ptr->x = r5;
193 #else
194 unsigned long r1,r2,r3,r4;
196 __asm__("ldq_u %3,%1\n\t"
197 "ldq_u %2,%0\n\t"
198 "inswh %6,%7,%5\n\t"
199 "inswl %6,%7,%4\n\t"
200 "mskwh %3,%7,%3\n\t"
201 "mskwl %2,%7,%2\n\t"
202 "bis %3,%5,%3\n\t"
203 "bis %2,%4,%2\n\t"
204 "stq_u %3,%1\n\t"
205 "stq_u %2,%0"
206 :"=m" (*r11),
207 "=m" (*(unsigned long *)(1+(char *) r11)),
208 "=&r" (r1), "=&r" (r2), "=&r" (r3), "=&r" (r4)
209 :"r" (r5), "r" (r11));
210 #endif
213 #elif defined(__GNUC__) && ((__GNUC__ < 3)) && \
214 (defined (__ia64__) || defined (ia64__))
215 #define IA64_UALOADS
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;
228 return ptr->x;
231 static __inline__ unsigned long
232 uldl (const unsigned int * r11)
234 const struct __una_u32 *ptr = (const struct __una_u32 *) r11;
235 return ptr->x;
238 static __inline__ unsigned long
239 uldw (const unsigned short * r11)
241 const struct __una_u16 *ptr = (const struct __una_u16 *) r11;
242 return ptr->x;
245 static __inline__ void
246 ustq (unsigned long r5, unsigned long * r11)
248 struct __una_u64 *ptr = (struct __una_u64 *) r11;
249 ptr->x = r5;
252 static __inline__ void
253 ustl (unsigned long r5, unsigned int * r11)
255 struct __una_u32 *ptr = (struct __una_u32 *) r11;
256 ptr->x = r5;
259 static __inline__ void
260 ustw (unsigned long r5, unsigned short * r11)
262 struct __una_u16 *ptr = (struct __una_u16 *) r11;
263 ptr->x = r5;
266 #endif
268 /****************************************************************************
269 PARAMETERS:
270 addr - Emulator memory address to read
272 RETURNS:
273 Byte value read from emulator memory.
275 REMARKS:
276 Reads a byte value from the emulator memory.
277 ****************************************************************************/
278 u8 X86API rdb(
279 u32 addr)
281 u8 val;
283 if (addr > M.mem_size - 1) {
284 DB(printk("mem_read: address %#lx out of range!\n", addr);)
285 HALT_SYS();
287 val = *(u8*)(M.mem_base + addr);
288 DB( if (DEBUG_MEM_TRACE())
289 printk("%#08x 1 -> %#x\n", addr, val);)
290 return val;
293 /****************************************************************************
294 PARAMETERS:
295 addr - Emulator memory address to read
297 RETURNS:
298 Word value read from emulator memory.
300 REMARKS:
301 Reads a word value from the emulator memory.
302 ****************************************************************************/
303 u16 X86API rdw(
304 u32 addr)
306 u16 val = 0;
308 if (addr > M.mem_size - 2) {
309 DB(printk("mem_read: address %#lx out of range!\n", addr);)
310 HALT_SYS();
312 #ifdef __BIG_ENDIAN__
313 if (addr & 0x1) {
314 val = (*(u8*)(M.mem_base + addr) |
315 (*(u8*)(M.mem_base + addr + 1) << 8));
317 else
318 #endif
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));
323 #else
324 val = *(u16*)(M.mem_base + addr);
325 #endif
326 DB( if (DEBUG_MEM_TRACE())
327 printk("%#08x 2 -> %#x\n", addr, val);)
328 return val;
331 /****************************************************************************
332 PARAMETERS:
333 addr - Emulator memory address to read
335 RETURNS:
336 Long value read from emulator memory.
337 REMARKS:
338 Reads a long value from the emulator memory.
339 ****************************************************************************/
340 u32 X86API rdl(
341 u32 addr)
343 u32 val = 0;
345 if (addr > M.mem_size - 4) {
346 DB(printk("mem_read: address %#lx out of range!\n", addr);)
347 HALT_SYS();
349 #ifdef __BIG_ENDIAN__
350 if (addr & 0x3) {
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));
356 else
357 #endif
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));
362 #else
363 val = *(u32*)(M.mem_base + addr);
364 #endif
365 DB( if (DEBUG_MEM_TRACE())
366 printk("%#08x 4 -> %#x\n", addr, val);)
367 return val;
370 /****************************************************************************
371 PARAMETERS:
372 addr - Emulator memory address to read
373 val - Value to store
375 REMARKS:
376 Writes a byte value to emulator memory.
377 ****************************************************************************/
378 void X86API wrb(
379 u32 addr,
380 u8 val)
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);)
386 HALT_SYS();
388 *(u8*)(M.mem_base + addr) = val;
391 /****************************************************************************
392 PARAMETERS:
393 addr - Emulator memory address to read
394 val - Value to store
396 REMARKS:
397 Writes a word value to emulator memory.
398 ****************************************************************************/
399 void X86API wrw(
400 u32 addr,
401 u16 val)
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);)
407 HALT_SYS();
409 #ifdef __BIG_ENDIAN__
410 if (addr & 0x1) {
411 *(u8*)(M.mem_base + addr + 0) = (val >> 0) & 0xff;
412 *(u8*)(M.mem_base + addr + 1) = (val >> 8) & 0xff;
414 else
415 #endif
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));
420 #else
421 *(u16*)(M.mem_base + addr) = val;
422 #endif
425 /****************************************************************************
426 PARAMETERS:
427 addr - Emulator memory address to read
428 val - Value to store
430 REMARKS:
431 Writes a long value to emulator memory.
432 ****************************************************************************/
433 void X86API wrl(
434 u32 addr,
435 u32 val)
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);)
441 HALT_SYS();
443 #ifdef __BIG_ENDIAN__
444 if (addr & 0x1) {
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;
450 else
451 #endif
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));
456 #else
457 *(u32*)(M.mem_base + addr) = val;
458 #endif
461 /****************************************************************************
462 PARAMETERS:
463 addr - PIO address to read
464 RETURN:
466 REMARKS:
467 Default PIO byte read function. Doesn't perform real inb.
468 ****************************************************************************/
469 static u8 X86API p_inb(
470 X86EMU_pioAddr addr)
472 DB( if (DEBUG_IO_TRACE())
473 printk("inb %#04x \n", addr);)
474 return 0;
477 /****************************************************************************
478 PARAMETERS:
479 addr - PIO address to read
480 RETURN:
482 REMARKS:
483 Default PIO word read function. Doesn't perform real inw.
484 ****************************************************************************/
485 static u16 X86API p_inw(
486 X86EMU_pioAddr addr)
488 DB( if (DEBUG_IO_TRACE())
489 printk("inw %#04x \n", addr);)
490 return 0;
493 /****************************************************************************
494 PARAMETERS:
495 addr - PIO address to read
496 RETURN:
498 REMARKS:
499 Default PIO long read function. Doesn't perform real inl.
500 ****************************************************************************/
501 static u32 X86API p_inl(
502 X86EMU_pioAddr addr)
504 DB( if (DEBUG_IO_TRACE())
505 printk("inl %#04x \n", addr);)
506 return 0;
509 /****************************************************************************
510 PARAMETERS:
511 addr - PIO address to write
512 val - Value to store
513 REMARKS:
514 Default PIO byte write function. Doesn't perform real outb.
515 ****************************************************************************/
516 static void X86API p_outb(
517 X86EMU_pioAddr addr,
518 u8 val)
520 DB( if (DEBUG_IO_TRACE())
521 printk("outb %#02x -> %#04x \n", val, addr);)
522 return;
525 /****************************************************************************
526 PARAMETERS:
527 addr - PIO address to write
528 val - Value to store
529 REMARKS:
530 Default PIO word write function. Doesn't perform real outw.
531 ****************************************************************************/
532 static void X86API p_outw(
533 X86EMU_pioAddr addr,
534 u16 val)
536 DB( if (DEBUG_IO_TRACE())
537 printk("outw %#04x -> %#04x \n", val, addr);)
538 return;
541 /****************************************************************************
542 PARAMETERS:
543 addr - PIO address to write
544 val - Value to store
545 REMARKS:
546 Default PIO ;ong write function. Doesn't perform real outl.
547 ****************************************************************************/
548 static void X86API p_outl(
549 X86EMU_pioAddr addr,
550 u32 val)
552 DB( if (DEBUG_IO_TRACE())
553 printk("outl %#08x -> %#04x \n", val, addr);)
554 return;
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 /****************************************************************************
575 PARAMETERS:
576 funcs - New memory function pointers to make active
578 REMARKS:
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 /****************************************************************************
595 PARAMETERS:
596 funcs - New programmed I/O function pointers to make active
598 REMARKS:
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 /****************************************************************************
615 PARAMETERS:
616 funcs - New interrupt vector table to make active
618 REMARKS:
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
625 interrupts.
626 ****************************************************************************/
627 void X86EMU_setupIntrFuncs(
628 X86EMU_intrFuncs funcs[])
630 int i;
632 for (i=0; i < 256; i++)
633 _X86EMU_intrTab[i] = NULL;
634 if (funcs) {
635 for (i = 0; i < 256; i++)
636 _X86EMU_intrTab[i] = funcs[i];
640 /****************************************************************************
641 PARAMETERS:
642 int - New software interrupt to prepare for
644 REMARKS:
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(
653 int num)
655 push_word((u16)M.x86.R_FLG);
656 CLEAR_FLAG(F_IF);
657 CLEAR_FLAG(F_TF);
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);
662 M.x86.intr = 0;