///////////////////////////////////////////////////////////////////////// // $Id: icache.h,v 1.42 2009-02-15 18:51:13 sshwarts Exp $ ///////////////////////////////////////////////////////////////////////// // // Copyright (c) 2007 Stanislav Shwartsman // Written by Stanislav Shwartsman [sshwarts at sourceforge net] // // 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., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA // ///////////////////////////////////////////////////////////////////////// #ifndef BX_ICACHE_H #define BX_ICACHE_H #if BX_SUPPORT_ICACHE // bit31: 1=CS is 32/64-bit, 0=CS is 16-bit. // bit30: 1=Long Mode, 0=not Long Mode. // Combination bit31=1 & bit30=1 is invalid (data page) const Bit32u ICacheWriteStampInvalid = 0xffffffff; const Bit32u ICacheWriteStampStart = 0x3fffffff; const Bit32u ICacheWriteStampFetchModeMask = ~ICacheWriteStampStart; #if BX_SUPPORT_TRACE_CACHE extern void handleSMC(void); #endif class bxPageWriteStampTable { // 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; #define PHY_MEM_PAGES (1024*1024) public: bxPageWriteStampTable() { pageWriteStampTable = new Bit32u[PHY_MEM_PAGES]; resetWriteStamps(); } ~bxPageWriteStampTable() { delete [] pageWriteStampTable; } BX_CPP_INLINE Bit32u hash(bx_phy_address pAddr) const { #if BX_PHY_ADDRESS_LONG // can share writeStamps between multiple pages Bit32u lo = (pAddr >> 12) & (PHY_MEM_PAGES-1); Bit32u hi = (pAddr >> 32) & (PHY_MEM_PAGES-1); return lo ^ hi; #else return (Bit32u)(pAddr) >> 12; #endif } BX_CPP_INLINE Bit32u getPageWriteStamp(bx_phy_address pAddr) const { return pageWriteStampTable[hash(pAddr)]; } BX_CPP_INLINE const Bit32u *getPageWriteStampPtr(bx_phy_address pAddr) const { return &pageWriteStampTable[hash(pAddr)]; } BX_CPP_INLINE void setPageWriteStamp(bx_phy_address pAddr, Bit32u pageWriteStamp) { pageWriteStampTable[hash(pAddr)] = pageWriteStamp; } BX_CPP_INLINE void decWriteStamp(bx_phy_address pAddr) { Bit32u index = hash(pAddr); #if BX_SUPPORT_TRACE_CACHE if ((pageWriteStampTable[index] & ICacheWriteStampFetchModeMask) != ICacheWriteStampFetchModeMask) { handleSMC(); // one of the CPUs might be running trace from this page // Decrement page write stamp, so iCache entries with older stamps are // effectively invalidated. pageWriteStampTable[index]--; } #endif #if BX_DEBUGGER BX_DBG_DIRTY_PAGE(index); #endif } BX_CPP_INLINE void resetWriteStamps(void); BX_CPP_INLINE void purgeWriteStamps(void); }; BX_CPP_INLINE void bxPageWriteStampTable::resetWriteStamps(void) { for (Bit32u i=0; i>6)) & (BxICacheEntries-1); return (pAddr) & (BxICacheEntries-1); } #if BX_SUPPORT_TRACE_CACHE BX_CPP_INLINE void alloc_trace(bxICacheEntry_c *e) { if (mempool + BX_MAX_TRACE_LENGTH > BxICacheMemPool) { flushICacheEntries(); } e->i = &pool[mempool]; e->ilen = 0; } BX_CPP_INLINE void commit_trace(unsigned len) { mempool += len; } #endif BX_CPP_INLINE void purgeICacheEntries(void); BX_CPP_INLINE void flushICacheEntries(void); BX_CPP_INLINE bxICacheEntry_c* get_entry(bx_phy_address pAddr) { return &(entry[hash(pAddr)]); } }; BX_CPP_INLINE void bxICache_c::flushICacheEntries(void) { bxICacheEntry_c* e = entry; for (unsigned i=0; iwriteStamp = ICacheWriteStampInvalid; } #if BX_SUPPORT_TRACE_CACHE mempool = 0; #endif } // 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. BX_CPP_INLINE void bxICache_c::purgeICacheEntries(void) { flushICacheEntries(); } extern void purgeICaches(void); extern void flushICaches(void); #endif // BX_SUPPORT_ICACHE #endif