2004-11-14 22:39:01 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 00:54:04 +03:00
|
|
|
// $Id$
|
2005-03-19 23:44:01 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-11-14 22:39:01 +03:00
|
|
|
//
|
2011-01-12 22:53:47 +03:00
|
|
|
// Copyright (c) 2007-2011 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
|
|
|
|
2011-01-23 18:54:54 +03:00
|
|
|
extern void handleSMC(bx_phy_address pAddr, Bit32u mask);
|
2004-11-14 22:39:01 +03:00
|
|
|
|
2011-01-04 19:17:20 +03:00
|
|
|
class bxPageWriteStampTable
|
|
|
|
{
|
2008-03-30 00:03:38 +03:00
|
|
|
#define PHY_MEM_PAGES (1024*1024)
|
2011-01-12 21:49:11 +03:00
|
|
|
Bit32u *fineGranularityMapping;
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2008-03-30 00:01:25 +03:00
|
|
|
public:
|
|
|
|
bxPageWriteStampTable() {
|
2011-01-12 21:49:11 +03:00
|
|
|
fineGranularityMapping = new Bit32u[PHY_MEM_PAGES];
|
2005-06-17 00:28:27 +04:00
|
|
|
resetWriteStamps();
|
2005-04-10 23:42:48 +04:00
|
|
|
}
|
2011-01-12 21:49:11 +03:00
|
|
|
~bxPageWriteStampTable() { delete [] fineGranularityMapping; }
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2012-09-23 23:35:46 +04:00
|
|
|
BX_CPP_INLINE static Bit32u hash(bx_phy_address pAddr) {
|
2010-05-12 18:55:12 +04:00
|
|
|
// can share writeStamps between multiple pages if >32 bit phy address
|
|
|
|
return ((Bit32u) pAddr) >> 12;
|
2009-02-15 21:51:13 +03:00
|
|
|
}
|
|
|
|
|
2011-01-04 19:17:20 +03:00
|
|
|
BX_CPP_INLINE Bit32u getFineGranularityMapping(bx_phy_address pAddr) const
|
|
|
|
{
|
2011-01-12 21:49:11 +03:00
|
|
|
return fineGranularityMapping[hash(pAddr)];
|
2004-11-19 12:39:30 +03:00
|
|
|
}
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2011-01-04 19:17:20 +03:00
|
|
|
BX_CPP_INLINE void markICache(bx_phy_address pAddr, unsigned len)
|
|
|
|
{
|
|
|
|
Bit32u mask = 1 << (PAGE_OFFSET((Bit32u) pAddr) >> 7);
|
|
|
|
mask |= 1 << (PAGE_OFFSET((Bit32u) pAddr + len - 1) >> 7);
|
|
|
|
|
2011-01-12 21:49:11 +03:00
|
|
|
fineGranularityMapping[hash(pAddr)] |= mask;
|
2009-03-23 00:12:35 +03:00
|
|
|
}
|
|
|
|
|
2011-01-23 18:54:54 +03:00
|
|
|
BX_CPP_INLINE void markICacheMask(bx_phy_address pAddr, Bit32u mask)
|
|
|
|
{
|
|
|
|
fineGranularityMapping[hash(pAddr)] |= mask;
|
|
|
|
}
|
|
|
|
|
2011-01-04 19:17:20 +03:00
|
|
|
// whole page is being altered
|
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);
|
2011-01-04 19:17:20 +03:00
|
|
|
|
2011-01-12 21:49:11 +03:00
|
|
|
if (fineGranularityMapping[index]) {
|
2011-01-23 18:54:54 +03:00
|
|
|
handleSMC(pAddr, 0xffffffff); // one of the CPUs might be running trace from this page
|
2011-01-12 21:49:11 +03:00
|
|
|
fineGranularityMapping[index] = 0;
|
2011-01-04 19:17:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assumption: write does not split 4K page
|
|
|
|
BX_CPP_INLINE void decWriteStamp(bx_phy_address pAddr, unsigned len)
|
|
|
|
{
|
|
|
|
Bit32u index = hash(pAddr);
|
|
|
|
|
2011-01-12 21:49:11 +03:00
|
|
|
if (fineGranularityMapping[index]) {
|
2011-01-04 19:17:20 +03:00
|
|
|
Bit32u mask = 1 << (PAGE_OFFSET((Bit32u) pAddr) >> 7);
|
|
|
|
mask |= 1 << (PAGE_OFFSET((Bit32u) pAddr + len - 1) >> 7);
|
|
|
|
|
2011-01-12 21:49:11 +03:00
|
|
|
if (fineGranularityMapping[index] & mask) {
|
2011-01-23 18:54:54 +03:00
|
|
|
// one of the CPUs might be running trace from this page
|
|
|
|
handleSMC(pAddr, mask);
|
|
|
|
fineGranularityMapping[index] &= ~mask;
|
2011-01-04 19:17:20 +03:00
|
|
|
}
|
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++) {
|
2011-01-12 21:49:11 +03:00
|
|
|
fineGranularityMapping[i] = 0;
|
2005-06-17 00:28:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern bxPageWriteStampTable pageWriteStampTable;
|
|
|
|
|
2013-06-29 14:25:56 +04:00
|
|
|
#define BxICacheEntries (64 * 1024) // Must be a power of 2.
|
2012-09-01 23:13:01 +04:00
|
|
|
#define BxICacheMemPool (576 * 1024)
|
2005-06-17 00:28:27 +04:00
|
|
|
|
2011-08-21 20:44:02 +04:00
|
|
|
#define BX_MAX_TRACE_LENGTH 32
|
2007-12-09 21:36:05 +03:00
|
|
|
|
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
|
2011-01-23 18:54:54 +03:00
|
|
|
Bit32u traceMask;
|
2011-01-12 21:49:11 +03:00
|
|
|
|
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;
|
2005-06-17 00:28:27 +04:00
|
|
|
};
|
|
|
|
|
2010-05-08 12:30:04 +04:00
|
|
|
#define BX_ICACHE_INVALID_PHY_ADDRESS (bx_phy_address(-1))
|
|
|
|
|
2012-09-04 19:45:05 +04:00
|
|
|
BX_CPP_INLINE void flushSMC(bxICacheEntry_c *e)
|
|
|
|
{
|
2012-10-01 22:19:09 +04:00
|
|
|
if (e->pAddr != BX_ICACHE_INVALID_PHY_ADDRESS) {
|
|
|
|
e->pAddr = BX_ICACHE_INVALID_PHY_ADDRESS;
|
2012-09-04 19:45:05 +04:00
|
|
|
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
|
2012-10-01 22:19:09 +04:00
|
|
|
extern void genDummyICacheEntry(bxInstruction_c *i);
|
2013-06-28 11:51:42 +04:00
|
|
|
// for (unsigned instr=0;instr < e->tlen; instr++)
|
|
|
|
// genDummyICacheEntry(e->i + instr);
|
|
|
|
genDummyICacheEntry(e->i);
|
2012-09-04 19:45:05 +04:00
|
|
|
#endif
|
2012-10-01 22:19:09 +04:00
|
|
|
}
|
2012-09-04 19:45:05 +04:00
|
|
|
}
|
|
|
|
|
2005-06-17 00:28:27 +04:00
|
|
|
class BOCHSAPI bxICache_c {
|
|
|
|
public:
|
|
|
|
bxICacheEntry_c entry[BxICacheEntries];
|
2009-03-23 00:12:35 +03:00
|
|
|
bxInstruction_c mpool[BxICacheMemPool];
|
|
|
|
unsigned mpindex;
|
2010-05-08 12:30:04 +04:00
|
|
|
|
2013-06-29 14:16:28 +04:00
|
|
|
Bit32u traceLinkTimeStamp;
|
|
|
|
|
2010-05-08 12:30:04 +04:00
|
|
|
#define BX_ICACHE_PAGE_SPLIT_ENTRIES 8 /* must be power of two */
|
|
|
|
struct pageSplitEntryIndex {
|
|
|
|
bx_phy_address ppf; // Physical address of 2nd page of the trace
|
|
|
|
bxICacheEntry_c *e; // Pointer to icache entry
|
|
|
|
} pageSplitIndex[BX_ICACHE_PAGE_SPLIT_ENTRIES];
|
|
|
|
int nextPageSplitIndex;
|
2011-03-26 02:06:34 +03:00
|
|
|
|
|
|
|
#define BX_ICACHE_VICTIM_ENTRIES 8 /* must be power of two */
|
|
|
|
struct bxVictimCacheEntry {
|
|
|
|
Bit32u fetchModeMask;
|
|
|
|
bxICacheEntry_c vc_entry;
|
|
|
|
} victimCache[BX_ICACHE_VICTIM_ENTRIES];
|
|
|
|
int nextVictimCacheIndex;
|
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
|
|
|
|
2012-09-23 23:35:46 +04:00
|
|
|
BX_CPP_INLINE static unsigned hash(bx_phy_address pAddr, unsigned fetchModeMask)
|
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
|
|
|
BX_CPP_INLINE void alloc_trace(bxICacheEntry_c *e)
|
|
|
|
{
|
2011-08-21 18:31:08 +04:00
|
|
|
// took +1 garbend for instruction chaining speedup (end-of-trace opcode)
|
|
|
|
if ((mpindex + BX_MAX_TRACE_LENGTH + 1) > 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; }
|
2010-05-08 12:30:04 +04:00
|
|
|
|
|
|
|
BX_CPP_INLINE void commit_page_split_trace(bx_phy_address paddr, bxICacheEntry_c *entry)
|
|
|
|
{
|
2011-08-21 18:31:08 +04:00
|
|
|
mpindex += entry->tlen;
|
2010-05-08 12:30:04 +04:00
|
|
|
|
|
|
|
// register page split entry
|
|
|
|
if (pageSplitIndex[nextPageSplitIndex].ppf != BX_ICACHE_INVALID_PHY_ADDRESS)
|
2011-01-12 21:49:11 +03:00
|
|
|
pageSplitIndex[nextPageSplitIndex].e->pAddr = BX_ICACHE_INVALID_PHY_ADDRESS;
|
2010-05-08 12:30:04 +04:00
|
|
|
|
|
|
|
pageSplitIndex[nextPageSplitIndex].ppf = paddr;
|
|
|
|
pageSplitIndex[nextPageSplitIndex].e = entry;
|
|
|
|
|
|
|
|
nextPageSplitIndex = (nextPageSplitIndex+1) & (BX_ICACHE_PAGE_SPLIT_ENTRIES-1);
|
|
|
|
}
|
2011-03-26 02:06:34 +03:00
|
|
|
|
|
|
|
BX_CPP_INLINE bxICacheEntry_c *lookup_victim_cache(bx_phy_address pAddr, Bit32u fetchModeMask)
|
|
|
|
{
|
|
|
|
for (int i=0; i < BX_ICACHE_VICTIM_ENTRIES;i++) {
|
|
|
|
bxVictimCacheEntry *e = &victimCache[i];
|
|
|
|
if (e->vc_entry.pAddr == pAddr && e->fetchModeMask == fetchModeMask) {
|
|
|
|
return &e->vc_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_CPP_INLINE void victim_entry(bxICacheEntry_c *entry, Bit32u fetchModeMask)
|
|
|
|
{
|
|
|
|
if (entry->pAddr != BX_ICACHE_INVALID_PHY_ADDRESS) {
|
|
|
|
victimCache[nextVictimCacheIndex].fetchModeMask = fetchModeMask;
|
|
|
|
victimCache[nextVictimCacheIndex].vc_entry = *entry;
|
|
|
|
nextVictimCacheIndex = (nextVictimCacheIndex+1) & (BX_ICACHE_VICTIM_ENTRIES-1);
|
|
|
|
}
|
|
|
|
}
|
2010-05-08 12:30:04 +04:00
|
|
|
|
2011-01-23 18:54:54 +03:00
|
|
|
BX_CPP_INLINE void handleSMC(bx_phy_address pAddr, Bit32u mask);
|
2008-05-04 09:37:36 +04:00
|
|
|
|
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
|
|
|
}
|
2012-09-04 19:45:05 +04:00
|
|
|
|
|
|
|
BX_CPP_INLINE bxICacheEntry_c* find_entry(bx_phy_address pAddr, unsigned fetchModeMask)
|
|
|
|
{
|
|
|
|
bxICacheEntry_c* e = &entry[hash(pAddr, fetchModeMask)];
|
|
|
|
if (e->pAddr != pAddr)
|
|
|
|
e = lookup_victim_cache(pAddr, fetchModeMask);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
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;
|
2010-05-08 12:30:04 +04:00
|
|
|
unsigned i;
|
|
|
|
|
2011-01-23 18:54:54 +03:00
|
|
|
for (i=0; i<BxICacheEntries; i++, e++) {
|
2011-01-12 21:49:11 +03:00
|
|
|
e->pAddr = BX_ICACHE_INVALID_PHY_ADDRESS;
|
2011-01-23 18:54:54 +03:00
|
|
|
e->traceMask = 0;
|
|
|
|
}
|
2010-05-08 12:30:04 +04:00
|
|
|
|
2011-03-26 02:06:34 +03:00
|
|
|
nextPageSplitIndex = 0;
|
2010-05-08 12:30:04 +04:00
|
|
|
for (i=0;i<BX_ICACHE_PAGE_SPLIT_ENTRIES;i++)
|
|
|
|
pageSplitIndex[i].ppf = BX_ICACHE_INVALID_PHY_ADDRESS;
|
|
|
|
|
2011-03-26 02:06:34 +03:00
|
|
|
nextVictimCacheIndex = 0;
|
|
|
|
for (i=0;i<BX_ICACHE_VICTIM_ENTRIES;i++)
|
|
|
|
victimCache[i].vc_entry.pAddr = BX_ICACHE_INVALID_PHY_ADDRESS;
|
|
|
|
|
2009-03-23 00:12:35 +03:00
|
|
|
mpindex = 0;
|
2013-06-29 14:16:28 +04:00
|
|
|
|
|
|
|
traceLinkTimeStamp = 0;
|
2005-10-18 22:07:52 +04:00
|
|
|
}
|
|
|
|
|
2011-01-23 18:54:54 +03:00
|
|
|
BX_CPP_INLINE void bxICache_c::handleSMC(bx_phy_address pAddr, Bit32u mask)
|
2010-05-08 12:30:04 +04:00
|
|
|
{
|
2012-10-01 22:19:09 +04:00
|
|
|
Bit32u pAddrIndex = bxPageWriteStampTable::hash(pAddr);
|
2010-05-08 12:30:04 +04:00
|
|
|
|
2013-06-29 14:16:28 +04:00
|
|
|
// break all links bewteen traces
|
|
|
|
if (++traceLinkTimeStamp == 0xffffffff) {
|
|
|
|
flushICacheEntries();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:19:09 +04:00
|
|
|
// Need to invalidate all traces in the trace cache that might include an
|
|
|
|
// instruction that was modified. But this is not enough, it is possible
|
|
|
|
// that some another trace is linked into invalidated trace and it won't
|
|
|
|
// be invalidated. In order to solve this issue replace all instructions
|
2012-09-06 23:51:33 +04:00
|
|
|
// from the invalidated trace with dummy EndOfTrace opcodes.
|
2011-05-30 00:42:47 +04:00
|
|
|
|
2012-10-01 22:19:09 +04:00
|
|
|
// Another corner case that has to be handled - pageWriteStampTable wrap.
|
|
|
|
// Multiple physical addresses could be mapped into single pageWriteStampTable
|
|
|
|
// entry and all of them have to be invalidated here now.
|
|
|
|
|
2011-01-23 18:54:54 +03:00
|
|
|
if (mask & 0x1) {
|
|
|
|
// the store touched 1st cache line in the page, check for
|
|
|
|
// page split traces to invalidate.
|
2012-09-06 23:51:33 +04:00
|
|
|
for (unsigned i=0;i<BX_ICACHE_PAGE_SPLIT_ENTRIES;i++) {
|
2012-10-01 22:19:09 +04:00
|
|
|
if (pageSplitIndex[i].ppf != BX_ICACHE_INVALID_PHY_ADDRESS) {
|
|
|
|
if (pAddrIndex == bxPageWriteStampTable::hash(pageSplitIndex[i].ppf)) {
|
|
|
|
pageSplitIndex[i].ppf = BX_ICACHE_INVALID_PHY_ADDRESS;
|
|
|
|
flushSMC(pageSplitIndex[i].e);
|
|
|
|
}
|
2011-01-23 18:54:54 +03:00
|
|
|
}
|
2010-05-08 12:30:04 +04:00
|
|
|
}
|
|
|
|
}
|
2011-03-26 02:06:34 +03:00
|
|
|
|
2012-09-06 23:51:33 +04:00
|
|
|
for (unsigned i=0;i < BX_ICACHE_VICTIM_ENTRIES; i++) {
|
|
|
|
bxICacheEntry_c *e = &victimCache[i].vc_entry;
|
2012-10-01 22:19:09 +04:00
|
|
|
if (pAddrIndex == bxPageWriteStampTable::hash(e->pAddr) && (e->traceMask & mask) != 0) {
|
2012-09-06 23:51:33 +04:00
|
|
|
flushSMC(e);
|
|
|
|
}
|
2011-03-26 02:06:34 +03:00
|
|
|
}
|
2011-01-12 22:53:47 +03:00
|
|
|
|
2012-10-01 22:19:09 +04:00
|
|
|
bxICacheEntry_c *e = get_entry(LPFOf(pAddr), 0);
|
2011-01-12 22:53:47 +03:00
|
|
|
|
2012-08-21 23:58:41 +04:00
|
|
|
// go over 32 "cache lines" of 128 byte each
|
2011-01-23 20:21:34 +03:00
|
|
|
for (unsigned n=0; n < 32; n++) {
|
|
|
|
Bit32u line_mask = (1 << n);
|
|
|
|
if (line_mask > mask) break;
|
|
|
|
for (unsigned index=0; index < 128; index++, e++) {
|
2012-10-01 22:19:09 +04:00
|
|
|
if (pAddrIndex == bxPageWriteStampTable::hash(e->pAddr) && (e->traceMask & mask) != 0) {
|
2012-09-04 19:45:05 +04:00
|
|
|
flushSMC(e);
|
2011-01-23 20:21:34 +03:00
|
|
|
}
|
2011-01-23 18:54:54 +03:00
|
|
|
}
|
2011-01-12 22:53:47 +03:00
|
|
|
}
|
2010-05-08 12:30:04 +04:00
|
|
|
}
|
|
|
|
|
2005-10-18 22:07:52 +04:00
|
|
|
extern void flushICaches(void);
|
2005-04-10 23:42:48 +04:00
|
|
|
|
2004-11-14 22:39:01 +03:00
|
|
|
#endif
|