1 /////////////////////////////////////////////////////////////////////////
2 // $Id: io_pro.cc,v 1.39 2008/09/14 20:58:20 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2001 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 /////////////////////////////////////////////////////////////////////////
29 #define NEED_CPU_REG_SHORTCUTS 1
32 #define LOG_THIS BX_CPU_THIS_PTR
34 #include "iodev/iodev.h"
36 Bit16u
BX_CPP_AttrRegparmN(1)
37 BX_CPU_C::inp16(Bit16u port
)
39 if (! BX_CPU_THIS_PTR
allow_io(port
, 2)) {
40 BX_DEBUG(("inp16(): I/O access not allowed !"));
41 exception(BX_GP_EXCEPTION
, 0, 0);
44 Bit16u ret16
= BX_INP(port
, 2);
48 void BX_CPP_AttrRegparmN(2)
49 BX_CPU_C::outp16(Bit16u port
, Bit16u value
)
51 if (! BX_CPU_THIS_PTR
allow_io(port
, 2)) {
52 BX_DEBUG(("outp16(): I/O access not allowed !"));
53 exception(BX_GP_EXCEPTION
, 0, 0);
56 BX_OUTP(port
, value
, 2);
59 Bit32u
BX_CPP_AttrRegparmN(1)
60 BX_CPU_C::inp32(Bit16u port
)
62 if (! BX_CPU_THIS_PTR
allow_io(port
, 4)) {
63 BX_DEBUG(("inp32(): I/O access not allowed !"));
64 exception(BX_GP_EXCEPTION
, 0, 0);
67 Bit32u ret32
= BX_INP(port
, 4);
71 void BX_CPP_AttrRegparmN(2)
72 BX_CPU_C::outp32(Bit16u port
, Bit32u value
)
74 if (! BX_CPU_THIS_PTR
allow_io(port
, 4)) {
75 BX_DEBUG(("outp32(): I/O access not allowed !"));
76 exception(BX_GP_EXCEPTION
, 0, 0);
79 BX_OUTP(port
, value
, 4);
82 Bit8u
BX_CPP_AttrRegparmN(1)
83 BX_CPU_C::inp8(Bit16u port
)
85 if (! BX_CPU_THIS_PTR
allow_io(port
, 1)) {
86 BX_DEBUG(("inp8(): I/O access not allowed !"));
87 exception(BX_GP_EXCEPTION
, 0, 0);
90 Bit8u ret8
= BX_INP(port
, 1);
94 void BX_CPP_AttrRegparmN(2)
95 BX_CPU_C::outp8(Bit16u port
, Bit8u value
)
97 if (! BX_CPU_THIS_PTR
allow_io(port
, 1)) {
98 BX_DEBUG(("outp8(): I/O access not allowed !"));
99 exception(BX_GP_EXCEPTION
, 0, 0);
102 BX_OUTP(port
, value
, 1);
105 bx_bool
BX_CPU_C::allow_io(Bit16u port
, unsigned len
)
107 /* If CPL <= IOPL, then all IO portesses are accessible.
108 * Otherwise, must check the IO permission map on >286.
109 * On the 286, there is no IO permissions map */
111 if (BX_CPU_THIS_PTR cr0
.get_PE() && (BX_CPU_THIS_PTR
get_VM() || (CPL
>BX_CPU_THIS_PTR
get_IOPL())))
113 if (BX_CPU_THIS_PTR tr
.cache
.valid
==0 ||
114 (BX_CPU_THIS_PTR tr
.cache
.type
!= BX_SYS_SEGMENT_AVAIL_386_TSS
&&
115 BX_CPU_THIS_PTR tr
.cache
.type
!= BX_SYS_SEGMENT_BUSY_386_TSS
))
117 BX_ERROR(("allow_io(): TR doesn't point to a valid 32bit TSS, TR.TYPE=%u", BX_CPU_THIS_PTR tr
.cache
.type
));
121 if (BX_CPU_THIS_PTR tr
.cache
.u
.system
.limit_scaled
< 103) {
122 BX_ERROR(("allow_io(): TR.limit < 103"));
126 Bit16u io_base
= system_read_word(BX_CPU_THIS_PTR tr
.cache
.u
.system
.base
+ 102);
128 if ((io_base
+ port
/8) >= BX_CPU_THIS_PTR tr
.cache
.u
.system
.limit_scaled
) {
129 BX_DEBUG(("allow_io(): IO port %x (len %d) outside TSS IO permission map (base=%x, limit=%x) #GP(0)",
130 port
, len
, io_base
, BX_CPU_THIS_PTR tr
.cache
.u
.system
.limit_scaled
));
134 Bit16u permission16
= system_read_word(BX_CPU_THIS_PTR tr
.cache
.u
.system
.base
+ io_base
+ port
/8);
136 unsigned bit_index
= port
& 0x7;
137 unsigned mask
= (1 << len
) - 1;
138 if ((permission16
>> bit_index
) & mask
)
143 iobreakpoint_match(port
, len
);