1 /////////////////////////////////////////////////////////////////////////
2 // $Id: memory.cc,v 1.62 2007/11/01 18:03:48 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
30 #include "iodev/iodev.h"
31 #define LOG_THIS BX_MEM_THIS
33 #if BX_PROVIDE_CPU_MEMORY
36 // Memory map inside the 1st megabyte:
38 // 0x00000 - 0x7ffff DOS area (512K)
39 // 0x80000 - 0x9ffff Optional fixed memory hole (128K)
40 // 0xa0000 - 0xbffff Standard PCI/ISA Video Mem / SMMRAM (128K)
41 // 0xc0000 - 0xdffff Expansion Card BIOS and Buffer Area (128K)
42 // 0xe0000 - 0xeffff Lower BIOS Area (64K)
43 // 0xf0000 - 0xfffff Upper BIOS Area (64K)
46 void BX_CPP_AttrRegparmN(3)
47 BX_MEM_C::writePhysicalPage(BX_CPU_C
*cpu
, bx_phy_address addr
, unsigned len
, void *data
)
50 bx_phy_address a20addr
= A20ADDR(addr
);
51 struct memory_handler_struct
*memory_handler
= NULL
;
53 // Note: accesses should always be contained within a single page now
56 #if BX_SUPPORT_IODEBUG
57 bx_iodebug_c::mem_write(cpu
, a20addr
, len
, data
);
60 BX_INSTR_PHY_WRITE(cpu
->which_cpu(), a20addr
, len
);
63 // (mch) Check for physical write break points, TODO
64 // (bbd) Each breakpoint should have an associated CPU#, TODO
65 for (unsigned i
= 0; i
< num_write_watchpoints
; i
++) {
66 if (write_watchpoint
[i
] == a20addr
) {
67 cpu
->watchpoint
= a20addr
;
68 cpu
->break_point
= BREAK_POINT_WRITE
;
75 bx_generic_apic_c
*local_apic
= &cpu
->local_apic
;
76 if (local_apic
->is_selected(a20addr
, len
)) {
77 local_apic
->write(a20addr
, (Bit32u
*)data
, len
);
82 if ((a20addr
& 0xfffe0000) == 0x000a0000 && (BX_MEM_THIS smram_available
))
85 if (BX_MEM_THIS smram_enable
|| (cpu
->smm_mode() && !BX_MEM_THIS smram_restricted
))
90 #if BX_SUPPORT_MONITOR_MWAIT
91 BX_MEM_THIS
check_monitor(a20addr
, len
);
94 memory_handler
= BX_MEM_THIS memory_handlers
[a20addr
>> 20];
95 while (memory_handler
) {
96 if (memory_handler
->begin
<= a20addr
&&
97 memory_handler
->end
>= a20addr
&&
98 memory_handler
->write_handler(a20addr
, len
, data
, memory_handler
->param
))
102 memory_handler
= memory_handler
->next
;
107 // all memory access feets in single 4K page
108 if (a20addr
< BX_MEM_THIS len
) {
109 #if BX_SUPPORT_ICACHE
110 pageWriteStampTable
.decWriteStamp(a20addr
);
112 // all of data is within limits of physical memory
113 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
116 WriteHostQWordToLittleEndian(&BX_MEM_THIS vector
[a20addr
], *(Bit64u
*)data
);
117 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
121 WriteHostDWordToLittleEndian(&BX_MEM_THIS vector
[a20addr
], *(Bit32u
*)data
);
122 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
126 WriteHostWordToLittleEndian(&BX_MEM_THIS vector
[a20addr
], *(Bit16u
*)data
);
127 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
131 * ((Bit8u
*) (&BX_MEM_THIS vector
[a20addr
])) = * (Bit8u
*) data
;
132 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
135 // len == other, just fall thru to special cases handling
138 #ifdef BX_LITTLE_ENDIAN
139 data_ptr
= (Bit8u
*) data
;
140 #else // BX_BIG_ENDIAN
141 data_ptr
= (Bit8u
*) data
+ (len
- 1);
145 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
147 // addr *not* in range 000A0000 .. 000FFFFF
148 BX_MEM_THIS vector
[a20addr
] = *data_ptr
;
149 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
151 if (len
== 1) return;
154 #ifdef BX_LITTLE_ENDIAN
156 #else // BX_BIG_ENDIAN
162 // addr must be in range 000A0000 .. 000FFFFF
165 if (a20addr
<= 0x000bffff) {
166 // devices are not allowed to access SMMRAM under VGA memory
168 BX_MEM_THIS vector
[a20addr
] = *data_ptr
;
169 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
174 // adapter ROM C0000 .. DFFFF
175 // ROM BIOS memory E0000 .. FFFFF
176 #if BX_SUPPORT_PCI == 0
177 // ignore write to ROM
179 // Write Based on 440fx Programming
180 if (BX_MEM_THIS pci_enabled
&& ((a20addr
& 0xfffc0000) == 0x000c0000))
182 switch (DEV_pci_wr_memtype(a20addr
)) {
183 case 0x1: // Writes to ShadowRAM
184 BX_DEBUG(("Writing to ShadowRAM: address %08x, data %02x", (unsigned) a20addr
, *data_ptr
));
185 BX_MEM_THIS vector
[a20addr
] = *data_ptr
;
186 BX_DBG_DIRTY_PAGE(a20addr
>> 12);
189 case 0x0: // Writes to ROM, Inhibit
190 BX_DEBUG(("Write to ROM ignored: address %08x, data %02x", (unsigned) a20addr
, *data_ptr
));
194 BX_PANIC(("writePhysicalPage: default case"));
202 // access outside limits of physical memory, ignore
203 BX_DEBUG(("Write outside the limits of physical memory (0x%08x) (ignore)", a20addr
));
207 void BX_CPP_AttrRegparmN(3)
208 BX_MEM_C::readPhysicalPage(BX_CPU_C
*cpu
, bx_phy_address addr
, unsigned len
, void *data
)
211 bx_phy_address a20addr
= A20ADDR(addr
);
212 struct memory_handler_struct
*memory_handler
= NULL
;
214 // Note: accesses should always be contained within a single page now
217 #if BX_SUPPORT_IODEBUG
218 bx_iodebug_c::mem_read(cpu
, a20addr
, len
, data
);
221 BX_INSTR_PHY_READ(cpu
->which_cpu(), a20addr
, len
);
224 // (mch) Check for physical read break points, TODO
225 // (bbd) Each breakpoint should have an associated CPU#, TODO
226 for (unsigned i
= 0; i
< num_read_watchpoints
; i
++) {
227 if (read_watchpoint
[i
] == a20addr
) {
228 cpu
->watchpoint
= a20addr
;
229 cpu
->break_point
= BREAK_POINT_READ
;
236 bx_generic_apic_c
*local_apic
= &cpu
->local_apic
;
237 if (local_apic
->is_selected (a20addr
, len
)) {
238 local_apic
->read(a20addr
, data
, len
);
243 if ((a20addr
& 0xfffe0000) == 0x000a0000 && (BX_MEM_THIS smram_available
))
245 // SMRAM memory space
246 if (BX_MEM_THIS smram_enable
|| (cpu
->smm_mode() && !BX_MEM_THIS smram_restricted
))
251 memory_handler
= BX_MEM_THIS memory_handlers
[a20addr
>> 20];
252 while (memory_handler
) {
253 if (memory_handler
->begin
<= a20addr
&&
254 memory_handler
->end
>= a20addr
&&
255 memory_handler
->read_handler(a20addr
, len
, data
, memory_handler
->param
))
259 memory_handler
= memory_handler
->next
;
264 if (a20addr
<= BX_MEM_THIS len
) {
265 // all of data is within limits of physical memory
266 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
269 ReadHostQWordFromLittleEndian(&BX_MEM_THIS vector
[a20addr
], * (Bit64u
*) data
);
273 ReadHostDWordFromLittleEndian(&BX_MEM_THIS vector
[a20addr
], * (Bit32u
*) data
);
277 ReadHostWordFromLittleEndian(&BX_MEM_THIS vector
[a20addr
], * (Bit16u
*) data
);
281 * (Bit8u
*) data
= * ((Bit8u
*) (&BX_MEM_THIS vector
[a20addr
]));
284 // len == other case can just fall thru to special cases handling
287 #ifdef BX_LITTLE_ENDIAN
288 data_ptr
= (Bit8u
*) data
;
289 #else // BX_BIG_ENDIAN
290 data_ptr
= (Bit8u
*) data
+ (len
- 1);
294 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
296 // addr *not* in range 00080000 .. 000FFFFF
297 *data_ptr
= BX_MEM_THIS vector
[a20addr
];
299 if (len
== 1) return;
302 #ifdef BX_LITTLE_ENDIAN
304 #else // BX_BIG_ENDIAN
310 // addr must be in range 000A0000 .. 000FFFFF
313 if (a20addr
<= 0x000bffff) {
314 // devices are not allowed to access SMMRAM under VGA memory
315 if (cpu
) *data_ptr
= BX_MEM_THIS vector
[a20addr
];
320 if (BX_MEM_THIS pci_enabled
&& ((a20addr
& 0xfffc0000) == 0x000c0000))
322 switch (DEV_pci_rd_memtype(a20addr
)) {
323 case 0x0: // Read from ROM
324 if ((a20addr
& 0xfffe0000) == 0x000e0000)
326 *data_ptr
= BX_MEM_THIS rom
[a20addr
& BIOS_MASK
];
330 *data_ptr
= BX_MEM_THIS rom
[(a20addr
& EXROM_MASK
) + BIOSROMSZ
];
333 case 0x1: // Read from ShadowRAM
334 *data_ptr
= BX_MEM_THIS vector
[a20addr
];
337 BX_PANIC(("readPhysicalPage: default case"));
342 #endif // #if BX_SUPPORT_PCI
344 if ((a20addr
& 0xfffc0000) != 0x000c0000) {
345 *data_ptr
= BX_MEM_THIS vector
[a20addr
];
347 else if ((a20addr
& 0xfffe0000) == 0x000e0000)
349 *data_ptr
= BX_MEM_THIS rom
[a20addr
& BIOS_MASK
];
353 *data_ptr
= BX_MEM_THIS rom
[(a20addr
& EXROM_MASK
) + BIOSROMSZ
];
359 { // access outside limits of physical memory
361 #ifdef BX_LITTLE_ENDIAN
362 data_ptr
= (Bit8u
*) data
;
363 #else // BX_BIG_ENDIAN
364 data_ptr
= (Bit8u
*) data
+ (len
- 1);
367 for (unsigned i
= 0; i
< len
; i
++) {
368 if (a20addr
>= (bx_phy_address
)~BIOS_MASK
)
369 *data_ptr
= BX_MEM_THIS rom
[a20addr
& BIOS_MASK
];
374 #ifdef BX_LITTLE_ENDIAN
376 #else // BX_BIG_ENDIAN
383 #endif // #if BX_PROVIDE_CPU_MEMORY