2004-11-14 22:39:01 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2010-02-13 12:41:51 +03:00
|
|
|
// $Id: icache.h,v 1.51 2010-02-13 09:41:51 sshwarts Exp $
|
2005-03-19 23:44:01 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-11-14 22:39:01 +03:00
|
|
|
//
|
2009-10-15 00:45:29 +04:00
|
|
|
// Copyright (c) 2007-2009 Stanislav Shwartsman
|
2008-05-04 19:07:08 +04:00
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
2004-11-14 22:39:01 +03:00
|
|
|
//
|
|
|
|
// 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
|
2009-01-16 21:18:59 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
|
2008-01-29 20:13:10 +03:00
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-11-14 22:39:01 +03:00
|
|
|
|
|
|
|
#ifndef BX_ICACHE_H
|
2005-04-10 23:42:48 +04:00
|
|
|
#define BX_ICACHE_H
|
2004-11-14 22:39:01 +03:00
|
|
|
|
2009-03-23 00:12:35 +03:00
|
|
|
// bit 31 indicates code page
|
2009-03-26 12:44:23 +03:00
|
|
|
const Bit32u ICacheWriteStampInvalid = 0xffffffff;
|
2009-03-23 00:12:35 +03:00
|
|
|
const Bit32u ICacheWriteStampStart = 0x7fffffff;
|
2008-03-30 00:01:25 +03:00
|
|
|
const Bit32u ICacheWriteStampFetchModeMask = ~ICacheWriteStampStart;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
2008-07-13 17:24:36 +04:00
|
|
|
extern void handleSMC(void);
|
2008-03-30 00:01:25 +03:00
|
|
|
#endif
|
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
|
|
|
{
|
2008-02-03 00:46:54 +03: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
|
2005-04-10 23:42:48 +04:00
|
|
|
// the physical page write stamp are valid.
|
|
|
|
Bit32u *pageWriteStampTable;
|
|
|
|
|
2008-03-30 00:03:38 +03:00
|
|
|
#define PHY_MEM_PAGES (1024*1024)
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2008-03-30 00:01:25 +03:00
|
|
|
public:
|
|
|
|
bxPageWriteStampTable() {
|
2008-03-30 00:03:38 +03:00
|
|
|
pageWriteStampTable = new Bit32u[PHY_MEM_PAGES];
|
2005-06-17 00:28:27 +04:00
|
|
|
resetWriteStamps();
|
2005-04-10 23:42:48 +04:00
|
|
|
}
|
2008-03-30 00:01:25 +03:00
|
|
|
~bxPageWriteStampTable() { delete [] pageWriteStampTable; }
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2009-02-15 21:51:13 +03:00
|
|
|
BX_CPP_INLINE Bit32u hash(bx_phy_address pAddr) const {
|
|
|
|
#if BX_PHY_ADDRESS_LONG
|
|
|
|
// can share writeStamps between multiple pages
|
2009-03-24 15:37:28 +03:00
|
|
|
return (Bit32u) ((pAddr >> 12) & (PHY_MEM_PAGES-1));
|
2009-02-15 21:51:13 +03:00
|
|
|
#else
|
2009-03-24 15:37:28 +03:00
|
|
|
return (Bit32u) (pAddr) >> 12;
|
2009-02-15 21:51:13 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-05-12 21:04:19 +04:00
|
|
|
BX_CPP_INLINE Bit32u getPageWriteStamp(bx_phy_address pAddr) const
|
2004-11-19 12:39:30 +03:00
|
|
|
{
|
2009-02-15 21:51:13 +03:00
|
|
|
return pageWriteStampTable[hash(pAddr)];
|
2004-11-19 12:39:30 +03:00
|
|
|
}
|
|
|
|
|
2006-05-12 21:04:19 +04:00
|
|
|
BX_CPP_INLINE const Bit32u *getPageWriteStampPtr(bx_phy_address pAddr) const
|
2005-08-10 22:18:57 +04:00
|
|
|
{
|
2009-02-15 21:51:13 +03:00
|
|
|
return &pageWriteStampTable[hash(pAddr)];
|
2005-08-10 22:18:57 +04:00
|
|
|
}
|
|
|
|
|
2006-05-12 21:04:19 +04:00
|
|
|
BX_CPP_INLINE void setPageWriteStamp(bx_phy_address pAddr, Bit32u pageWriteStamp)
|
2004-11-19 12:39:30 +03:00
|
|
|
{
|
2009-02-15 21:51:13 +03:00
|
|
|
pageWriteStampTable[hash(pAddr)] = pageWriteStamp;
|
2004-11-19 12:39:30 +03:00
|
|
|
}
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2009-03-23 00:12:35 +03:00
|
|
|
BX_CPP_INLINE void markICache(bx_phy_address pAddr)
|
|
|
|
{
|
|
|
|
pageWriteStampTable[hash(pAddr)] |= ICacheWriteStampFetchModeMask;
|
|
|
|
}
|
|
|
|
|
2006-05-12 21:04:19 +04:00
|
|
|
BX_CPP_INLINE void decWriteStamp(bx_phy_address pAddr)
|
2005-06-17 00:28:27 +04:00
|
|
|
{
|
2009-02-15 21:51:13 +03:00
|
|
|
Bit32u index = hash(pAddr);
|
2009-04-06 22:27:30 +04:00
|
|
|
if (pageWriteStampTable[index] & ICacheWriteStampFetchModeMask) {
|
2008-03-30 00:01:25 +03:00
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
2008-07-13 17:24:36 +04:00
|
|
|
handleSMC(); // one of the CPUs might be running trace from this page
|
2009-04-06 22:27:30 +04:00
|
|
|
#endif
|
2008-07-26 19:07:14 +04:00
|
|
|
// Decrement page write stamp, so iCache entries with older stamps are
|
|
|
|
// effectively invalidated.
|
2009-03-24 15:37:28 +03:00
|
|
|
pageWriteStampTable[index] = (pageWriteStampTable[index] - 1) & ~ICacheWriteStampFetchModeMask;
|
2008-03-30 00:01:25 +03:00
|
|
|
}
|
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
|
|
|
{
|
2008-03-30 00:03:38 +03:00
|
|
|
for (Bit32u i=0; i<PHY_MEM_PAGES; i++) {
|
2009-03-26 12:44:23 +03:00
|
|
|
pageWriteStampTable[i] = ICacheWriteStampStart - 1;
|
2005-06-17 00:28:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern bxPageWriteStampTable pageWriteStampTable;
|
|
|
|
|
2008-05-04 09:37:36 +04:00
|
|
|
#define BxICacheEntries (64 * 1024) // Must be a power of 2.
|
|
|
|
#define BxICacheMemPool (384 * 1024)
|
2005-06-17 00:28:27 +04:00
|
|
|
|
2007-12-09 21:36:05 +03:00
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
2008-05-04 09:37:36 +04:00
|
|
|
#define BX_MAX_TRACE_LENGTH 32
|
2007-12-09 21:36:05 +03:00
|
|
|
#endif
|
|
|
|
|
2006-02-28 20:47:33 +03:00
|
|
|
struct bxICacheEntry_c
|
|
|
|
{
|
2006-05-12 21:04:19 +04:00
|
|
|
bx_phy_address pAddr; // Physical address of the instruction
|
|
|
|
Bit32u writeStamp; // Generation ID. Each write to a physical page
|
|
|
|
// decrements this value
|
2007-12-09 21:36:05 +03:00
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
2010-02-13 12:41:51 +03:00
|
|
|
Bit32u tlen; // Trace length in instructions
|
2008-05-04 09:37:36 +04:00
|
|
|
bxInstruction_c *i;
|
2007-12-09 21:36:05 +03:00
|
|
|
#else
|
2008-03-03 17:35:36 +03:00
|
|
|
// ... define as array of 1 to simplify merge with trace cache code
|
|
|
|
bxInstruction_c i[1];
|
2007-12-09 21:36:05 +03:00
|
|
|
#endif
|
2005-06-17 00:28:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class BOCHSAPI bxICache_c {
|
|
|
|
public:
|
|
|
|
bxICacheEntry_c entry[BxICacheEntries];
|
2008-05-04 09:37:36 +04:00
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
2009-03-23 00:12:35 +03:00
|
|
|
bxInstruction_c mpool[BxICacheMemPool];
|
|
|
|
unsigned mpindex;
|
2008-05-04 09:37:36 +04:00
|
|
|
#endif
|
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
|
|
|
|
2009-03-23 00:12:35 +03:00
|
|
|
BX_CPP_INLINE unsigned hash(bx_phy_address pAddr, unsigned fetchModeMask) const
|
2005-06-17 00:28:27 +04:00
|
|
|
{
|
2009-03-23 00:12:35 +03:00
|
|
|
// return ((pAddr + (pAddr << 2) + (pAddr>>6)) & (BxICacheEntries-1)) ^ fetchModeMask;
|
|
|
|
return ((pAddr) & (BxICacheEntries-1)) ^ fetchModeMask;
|
2005-06-17 00:28:27 +04:00
|
|
|
}
|
|
|
|
|
2008-05-04 09:37:36 +04:00
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
|
|
|
BX_CPP_INLINE void alloc_trace(bxICacheEntry_c *e)
|
|
|
|
{
|
2009-03-23 00:12:35 +03:00
|
|
|
if (mpindex + BX_MAX_TRACE_LENGTH > BxICacheMemPool) {
|
2008-06-23 06:56:31 +04:00
|
|
|
flushICacheEntries();
|
|
|
|
}
|
2009-03-23 00:12:35 +03:00
|
|
|
e->i = &mpool[mpindex];
|
2010-02-13 12:41:51 +03:00
|
|
|
e->tlen = 0;
|
2008-05-04 09:37:36 +04:00
|
|
|
}
|
|
|
|
|
2009-03-23 00:12:35 +03:00
|
|
|
BX_CPP_INLINE void commit_trace(unsigned len) { mpindex += len; }
|
2008-05-04 09:37:36 +04:00
|
|
|
#endif
|
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
BX_CPP_INLINE void purgeICacheEntries(void);
|
2005-10-18 22:07:52 +04:00
|
|
|
BX_CPP_INLINE void flushICacheEntries(void);
|
2008-03-03 19:22:31 +03:00
|
|
|
|
2009-03-23 00:12:35 +03:00
|
|
|
BX_CPP_INLINE bxICacheEntry_c* get_entry(bx_phy_address pAddr, unsigned fetchModeMask)
|
2008-03-03 19:22:31 +03:00
|
|
|
{
|
2009-03-23 00:12:35 +03:00
|
|
|
return &(entry[hash(pAddr, fetchModeMask)]);
|
2008-03-03 19:22:31 +03:00
|
|
|
}
|
2008-05-04 09:37:36 +04:00
|
|
|
|
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
|
|
|
{
|
2008-05-04 09:37:36 +04:00
|
|
|
bxICacheEntry_c* e = entry;
|
|
|
|
for (unsigned i=0; i<BxICacheEntries; i++, e++) {
|
|
|
|
e->writeStamp = ICacheWriteStampInvalid;
|
|
|
|
}
|
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
2009-03-23 00:12:35 +03:00
|
|
|
mpindex = 0;
|
2008-05-04 09:37:36 +04:00
|
|
|
#endif
|
2005-10-18 22:07:52 +04:00
|
|
|
}
|
|
|
|
|
2008-05-04 09:37:36 +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.
|
2005-06-17 00:28:27 +04:00
|
|
|
BX_CPP_INLINE void bxICache_c::purgeICacheEntries(void)
|
2006-02-28 20:47:33 +03:00
|
|
|
{
|
2008-05-04 09:37:36 +04:00
|
|
|
flushICacheEntries();
|
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
|