2004-11-14 22:39:01 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2006-02-28 20:47:33 +03:00
|
|
|
// $Id: icache.h,v 1.12 2006-02-28 17:47:33 sshwarts Exp $
|
2005-03-19 23:44:01 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-11-14 22:39:01 +03:00
|
|
|
//
|
|
|
|
// Copyright (C) 2001 MandrakeSoft S.A.
|
|
|
|
//
|
|
|
|
// MandrakeSoft S.A.
|
|
|
|
// 43, rue d'Aboukir
|
|
|
|
// 75002 Paris - France
|
|
|
|
// http://www.linux-mandrake.com/
|
|
|
|
// http://www.mandrakesoft.com/
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef BX_ICACHE_H
|
2005-04-10 23:42:48 +04:00
|
|
|
#define BX_ICACHE_H
|
2004-11-14 22:39:01 +03:00
|
|
|
|
2006-02-28 20:47:33 +03:00
|
|
|
#include <assert.h>
|
|
|
|
|
2004-11-19 02:16:36 +03:00
|
|
|
// bit31: 1=CS is 32/64-bit, 0=CS is 16-bit.
|
|
|
|
// bit30: 1=Long Mode, 0=not Long Mode.
|
|
|
|
// bit29: 1=iCache page, 0=Data.
|
2005-08-10 22:18:57 +04:00
|
|
|
const Bit32u ICacheWriteStampInvalid = 0x1fffffff;
|
|
|
|
const Bit32u ICacheWriteStampMax = 0x1fffffff;
|
|
|
|
const Bit32u ICacheWriteStampMask = 0x1fffffff;
|
|
|
|
const Bit32u ICacheFetchModeMask = ~ICacheWriteStampMask;
|
2004-11-14 22:39:01 +03:00
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
class bxPageWriteStampTable
|
2005-04-10 23:42:48 +04:00
|
|
|
{
|
|
|
|
// A table (dynamically allocated) to store write-stamp generation IDs.
|
|
|
|
// Each time a write occurs to a physical page, a generation ID is
|
|
|
|
// decremented. Only iCache entries which have write stamps matching
|
|
|
|
// the physical page write stamp are valid.
|
|
|
|
|
|
|
|
Bit32u *pageWriteStampTable;
|
|
|
|
Bit32u memSizeInBytes;
|
|
|
|
|
|
|
|
public:
|
|
|
|
bxPageWriteStampTable(): pageWriteStampTable(NULL), memSizeInBytes(0) {}
|
|
|
|
bxPageWriteStampTable(Bit32u memSize) { alloc(memSize); }
|
|
|
|
~bxPageWriteStampTable() { delete [] pageWriteStampTable; }
|
|
|
|
|
|
|
|
BX_CPP_INLINE void alloc(Bit32u memSize)
|
|
|
|
{
|
|
|
|
memSizeInBytes = memSize;
|
|
|
|
pageWriteStampTable = new Bit32u [memSizeInBytes>>12];
|
2005-06-17 00:28:27 +04:00
|
|
|
resetWriteStamps();
|
2005-04-10 23:42:48 +04:00
|
|
|
}
|
|
|
|
|
2004-11-19 12:39:30 +03:00
|
|
|
BX_CPP_INLINE Bit32u getPageWriteStamp(Bit32u pAddr) const
|
|
|
|
{
|
|
|
|
if (pAddr < memSizeInBytes)
|
|
|
|
return pageWriteStampTable[pAddr>>12];
|
|
|
|
else
|
|
|
|
return ICacheWriteStampInvalid;
|
|
|
|
}
|
|
|
|
|
2005-08-10 22:18:57 +04:00
|
|
|
BX_CPP_INLINE const Bit32u *getPageWriteStampPtr(Bit32u pAddr) const
|
|
|
|
{
|
|
|
|
if (pAddr < memSizeInBytes)
|
|
|
|
return &pageWriteStampTable[pAddr>>12];
|
|
|
|
else
|
|
|
|
return &ICacheWriteStampInvalid;
|
|
|
|
}
|
|
|
|
|
2004-11-19 12:39:30 +03:00
|
|
|
BX_CPP_INLINE void setPageWriteStamp(Bit32u pAddr, Bit32u pageWriteStamp)
|
|
|
|
{
|
2006-02-28 20:47:33 +03:00
|
|
|
if (pAddr < memSizeInBytes)
|
2004-11-19 12:39:30 +03:00
|
|
|
pageWriteStampTable[pAddr>>12] = pageWriteStamp;
|
|
|
|
}
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2006-02-28 20:47:33 +03:00
|
|
|
BX_CPP_INLINE void decWriteStamp(Bit32u pAddr)
|
2005-06-17 00:28:27 +04:00
|
|
|
{
|
2006-02-28 20:47:33 +03:00
|
|
|
assert(pAddr < memSizeInBytes);
|
|
|
|
// Decrement page write stamp, so iCache entries with older stamps
|
2005-06-17 00:28:27 +04:00
|
|
|
// are effectively invalidated.
|
2006-02-28 20:47:33 +03:00
|
|
|
pageWriteStampTable[pAddr >> 12]--;
|
2005-06-17 00:28:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BX_CPP_INLINE void resetWriteStamps(void);
|
2004-11-14 22:39:01 +03:00
|
|
|
};
|
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
BX_CPP_INLINE void bxPageWriteStampTable::resetWriteStamps(void)
|
2004-11-14 22:39:01 +03:00
|
|
|
{
|
2005-06-17 00:28:27 +04:00
|
|
|
for (Bit32u i=0; i<(memSizeInBytes>>12); i++) {
|
|
|
|
pageWriteStampTable[i] = ICacheWriteStampInvalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern bxPageWriteStampTable pageWriteStampTable;
|
|
|
|
|
|
|
|
#define BxICacheEntries (32 * 1024) // Must be a power of 2.
|
|
|
|
|
2006-02-28 20:47:33 +03:00
|
|
|
struct bxICacheEntry_c
|
|
|
|
{
|
|
|
|
Bit32u pAddr; // Physical address of the instruction
|
|
|
|
Bit32u writeStamp; // Generation ID. Each write to a physical page
|
|
|
|
// decrements this value
|
|
|
|
bxInstruction_c i; // The instruction decode information
|
2005-06-17 00:28:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class BOCHSAPI bxICache_c {
|
|
|
|
public:
|
|
|
|
bxICacheEntry_c entry[BxICacheEntries];
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
public:
|
2006-02-28 20:47:33 +03:00
|
|
|
bxICache_c() { flushICacheEntries(); }
|
2005-06-17 00:28:27 +04:00
|
|
|
|
|
|
|
BX_CPP_INLINE unsigned hash(Bit32u pAddr) const
|
|
|
|
{
|
|
|
|
// A pretty dumb hash function for now.
|
|
|
|
return pAddr & (BxICacheEntries-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_CPP_INLINE void purgeICacheEntries(void);
|
2005-10-18 22:07:52 +04:00
|
|
|
BX_CPP_INLINE void flushICacheEntries(void);
|
2005-06-17 00:28:27 +04:00
|
|
|
};
|
|
|
|
|
2005-10-18 22:07:52 +04:00
|
|
|
BX_CPP_INLINE void bxICache_c::flushICacheEntries(void)
|
2006-02-28 20:47:33 +03:00
|
|
|
{
|
|
|
|
bxICacheEntry_c* e = entry;
|
2005-12-13 17:18:34 +03:00
|
|
|
for (unsigned i=0; i<BxICacheEntries; i++, e++) {
|
|
|
|
e->writeStamp = ICacheWriteStampInvalid;
|
2005-10-18 22:07:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
BX_CPP_INLINE void bxICache_c::purgeICacheEntries(void)
|
2006-02-28 20:47:33 +03:00
|
|
|
{
|
|
|
|
bxICacheEntry_c* e = entry;
|
2005-12-13 17:18:34 +03:00
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
// Since the write stamps may overflow if we always simply decrese them,
|
|
|
|
// this function has to be called often enough that we can reset them
|
|
|
|
// (without invalidating the cache).
|
2006-02-28 20:47:33 +03:00
|
|
|
for (unsigned i=0;i<BxICacheEntries;i++,e++)
|
2005-06-17 00:28:27 +04:00
|
|
|
{
|
|
|
|
Bit32u pageWriteStamp = pageWriteStampTable.getPageWriteStamp(e->pAddr);
|
|
|
|
if (e->writeStamp != pageWriteStamp)
|
|
|
|
e->writeStamp = ICacheWriteStampInvalid; // invalidate entry
|
|
|
|
else
|
|
|
|
e->writeStamp |= 0x1fffffff;
|
2004-11-14 22:39:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-18 22:07:52 +04:00
|
|
|
extern void purgeICaches(void);
|
|
|
|
extern void flushICaches(void);
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2004-11-14 22:39:01 +03:00
|
|
|
#endif
|