1 /////////////////////////////////////////////////////////////////////////
2 // $Id: memory.cc,v 1.71 2008/10/18 18:10:14 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 /////////////////////////////////////////////////////////////////////////
31 #include "iodev/iodev.h"
32 #define LOG_THIS BX_MEM_THIS
34 #if BX_PROVIDE_CPU_MEMORY
37 // Memory map inside the 1st megabyte:
39 // 0x00000 - 0x7ffff DOS area (512K)
40 // 0x80000 - 0x9ffff Optional fixed memory hole (128K)
41 // 0xa0000 - 0xbffff Standard PCI/ISA Video Mem / SMMRAM (128K)
42 // 0xc0000 - 0xdffff Expansion Card BIOS and Buffer Area (128K)
43 // 0xe0000 - 0xeffff Lower BIOS Area (64K)
44 // 0xf0000 - 0xfffff Upper BIOS Area (64K)
47 void 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
54 if ((addr
>>12) != ((addr
+len
-1)>>12)) {
55 BX_PANIC(("writePhysicalPage: cross page access at address 0x" FMT_PHY_ADDRX
", len=%d", addr
, len
));
58 #if BX_SUPPORT_MONITOR_MWAIT
59 BX_MEM_THIS
check_monitor(a20addr
, len
);
63 #if BX_SUPPORT_IODEBUG
64 bx_iodebug_c::mem_write(cpu
, a20addr
, len
, data
);
67 BX_INSTR_PHY_WRITE(cpu
->which_cpu(), a20addr
, len
);
70 bx_generic_apic_c
*local_apic
= &cpu
->local_apic
;
71 if (local_apic
->is_selected(a20addr
, len
)) {
72 local_apic
->write(a20addr
, (Bit32u
*)data
, len
);
77 if ((a20addr
& 0xfffe0000) == 0x000a0000 && (BX_MEM_THIS smram_available
))
80 if (BX_MEM_THIS smram_enable
|| (cpu
->smm_mode() && !BX_MEM_THIS smram_restricted
))
85 memory_handler
= BX_MEM_THIS memory_handlers
[a20addr
>> 20];
86 while (memory_handler
) {
87 if (memory_handler
->begin
<= a20addr
&&
88 memory_handler
->end
>= a20addr
&&
89 memory_handler
->write_handler(a20addr
, len
, data
, memory_handler
->param
))
93 memory_handler
= memory_handler
->next
;
98 // all memory access feets in single 4K page
99 if (a20addr
< BX_MEM_THIS len
) {
100 #if BX_SUPPORT_ICACHE
101 pageWriteStampTable
.decWriteStamp(a20addr
);
103 // all of data is within limits of physical memory
104 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
107 WriteHostQWordToLittleEndian(BX_MEM_THIS
get_vector(a20addr
), *(Bit64u
*)data
);
111 WriteHostDWordToLittleEndian(BX_MEM_THIS
get_vector(a20addr
), *(Bit32u
*)data
);
115 WriteHostWordToLittleEndian(BX_MEM_THIS
get_vector(a20addr
), *(Bit16u
*)data
);
119 * (BX_MEM_THIS
get_vector(a20addr
)) = * (Bit8u
*) data
;
122 // len == other, just fall thru to special cases handling
125 #ifdef BX_LITTLE_ENDIAN
126 data_ptr
= (Bit8u
*) data
;
127 #else // BX_BIG_ENDIAN
128 data_ptr
= (Bit8u
*) data
+ (len
- 1);
131 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
134 // addr *not* in range 000A0000 .. 000FFFFF
135 *(BX_MEM_THIS
get_vector(a20addr
)) = *data_ptr
;
136 if (len
== 1) return;
139 #ifdef BX_LITTLE_ENDIAN
141 #else // BX_BIG_ENDIAN
147 // addr must be in range 000A0000 .. 000FFFFF
149 for(unsigned i
=0; i
<len
; i
++) {
152 if (a20addr
<= 0x000bffff) {
153 // devices are not allowed to access SMMRAM under VGA memory
155 *(BX_MEM_THIS
get_vector(a20addr
)) = *data_ptr
;
160 // adapter ROM C0000 .. DFFFF
161 // ROM BIOS memory E0000 .. FFFFF
162 #if BX_SUPPORT_PCI == 0
163 // ignore write to ROM
165 // Write Based on 440fx Programming
166 if (BX_MEM_THIS pci_enabled
&& ((a20addr
& 0xfffc0000) == 0x000c0000))
168 switch (DEV_pci_wr_memtype(a20addr
)) {
169 case 0x1: // Writes to ShadowRAM
170 BX_DEBUG(("Writing to ShadowRAM: address 0x" FMT_PHY_ADDRX
", data %02x", a20addr
, *data_ptr
));
171 *(BX_MEM_THIS
get_vector(a20addr
)) = *data_ptr
;
174 case 0x0: // Writes to ROM, Inhibit
175 BX_DEBUG(("Write to ROM ignored: address 0x" FMT_PHY_ADDRX
", data %02x", a20addr
, *data_ptr
));
179 BX_PANIC(("writePhysicalPage: default case"));
186 #ifdef BX_LITTLE_ENDIAN
188 #else // BX_BIG_ENDIAN
195 // access outside limits of physical memory, ignore
196 BX_DEBUG(("Write outside the limits of physical memory (0x"FMT_PHY_ADDRX
") (ignore)", a20addr
));
200 void BX_MEM_C::readPhysicalPage(BX_CPU_C
*cpu
, bx_phy_address addr
, unsigned len
, void *data
)
203 bx_phy_address a20addr
= A20ADDR(addr
);
204 struct memory_handler_struct
*memory_handler
= NULL
;
206 // Note: accesses should always be contained within a single page now
207 if ((addr
>>12) != ((addr
+len
-1)>>12)) {
208 BX_PANIC(("readPhysicalPage: cross page access at address 0x" FMT_PHY_ADDRX
", len=%d", addr
, len
));
212 #if BX_SUPPORT_IODEBUG
213 bx_iodebug_c::mem_read(cpu
, a20addr
, len
, data
);
216 BX_INSTR_PHY_READ(cpu
->which_cpu(), a20addr
, len
);
219 bx_generic_apic_c
*local_apic
= &cpu
->local_apic
;
220 if (local_apic
->is_selected (a20addr
, len
)) {
221 local_apic
->read(a20addr
, data
, len
);
226 if ((a20addr
& 0xfffe0000) == 0x000a0000 && (BX_MEM_THIS smram_available
))
228 // SMRAM memory space
229 if (BX_MEM_THIS smram_enable
|| (cpu
->smm_mode() && !BX_MEM_THIS smram_restricted
))
234 memory_handler
= BX_MEM_THIS memory_handlers
[a20addr
>> 20];
235 while (memory_handler
) {
236 if (memory_handler
->begin
<= a20addr
&&
237 memory_handler
->end
>= a20addr
&&
238 memory_handler
->read_handler(a20addr
, len
, data
, memory_handler
->param
))
242 memory_handler
= memory_handler
->next
;
247 if (a20addr
<= BX_MEM_THIS len
) {
248 // all of data is within limits of physical memory
249 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
252 ReadHostQWordFromLittleEndian(BX_MEM_THIS
get_vector(a20addr
), * (Bit64u
*) data
);
256 ReadHostDWordFromLittleEndian(BX_MEM_THIS
get_vector(a20addr
), * (Bit32u
*) data
);
260 ReadHostWordFromLittleEndian(BX_MEM_THIS
get_vector(a20addr
), * (Bit16u
*) data
);
264 * (Bit8u
*) data
= * (BX_MEM_THIS
get_vector(a20addr
));
267 // len == other case can just fall thru to special cases handling
270 #ifdef BX_LITTLE_ENDIAN
271 data_ptr
= (Bit8u
*) data
;
272 #else // BX_BIG_ENDIAN
273 data_ptr
= (Bit8u
*) data
+ (len
- 1);
276 if ((a20addr
& 0xfff80000) != 0x00080000 || (a20addr
<= 0x0009ffff))
279 // addr *not* in range 00080000 .. 000FFFFF
280 *data_ptr
= *(BX_MEM_THIS
get_vector(a20addr
));
281 if (len
== 1) return;
284 #ifdef BX_LITTLE_ENDIAN
286 #else // BX_BIG_ENDIAN
292 // addr must be in range 000A0000 .. 000FFFFF
294 for (unsigned i
=0; i
<len
; i
++) {
297 if (a20addr
<= 0x000bffff) {
298 // devices are not allowed to access SMMRAM under VGA memory
299 if (cpu
) *data_ptr
= *(BX_MEM_THIS
get_vector(a20addr
));
304 if (BX_MEM_THIS pci_enabled
&& ((a20addr
& 0xfffc0000) == 0x000c0000))
306 switch (DEV_pci_rd_memtype(a20addr
)) {
307 case 0x0: // Read from ROM
308 if ((a20addr
& 0xfffe0000) == 0x000e0000) {
309 *data_ptr
= BX_MEM_THIS rom
[a20addr
& BIOS_MASK
];
312 *data_ptr
= BX_MEM_THIS rom
[(a20addr
& EXROM_MASK
) + BIOSROMSZ
];
315 case 0x1: // Read from ShadowRAM
316 *data_ptr
= *(BX_MEM_THIS
get_vector(a20addr
));
319 BX_PANIC(("readPhysicalPage: default case"));
323 #endif // #if BX_SUPPORT_PCI
325 if ((a20addr
& 0xfffc0000) != 0x000c0000) {
326 *data_ptr
= *(BX_MEM_THIS
get_vector(a20addr
));
328 else if ((a20addr
& 0xfffe0000) == 0x000e0000) {
329 *data_ptr
= BX_MEM_THIS rom
[a20addr
& BIOS_MASK
];
332 *data_ptr
= BX_MEM_THIS rom
[(a20addr
& EXROM_MASK
) + BIOSROMSZ
];
338 #ifdef BX_LITTLE_ENDIAN
340 #else // BX_BIG_ENDIAN
347 { // access outside limits of physical memory
349 #ifdef BX_LITTLE_ENDIAN
350 data_ptr
= (Bit8u
*) data
;
351 #else // BX_BIG_ENDIAN
352 data_ptr
= (Bit8u
*) data
+ (len
- 1);
355 for (unsigned i
= 0; i
< len
; i
++) {
356 #if BX_PHY_ADDRESS_LONG
357 if (a20addr
>= BX_CONST64(0xFFFFFFFF))
361 if (a20addr
>= (bx_phy_address
)~BIOS_MASK
)
362 *data_ptr
= BX_MEM_THIS rom
[a20addr
& BIOS_MASK
];
367 #ifdef BX_LITTLE_ENDIAN
369 #else // BX_BIG_ENDIAN
376 #endif // #if BX_PROVIDE_CPU_MEMORY