Bochs/bochs/cpu/icache.cc

281 lines
8.7 KiB
C++
Raw Normal View History

2008-01-18 00:16:23 +03:00
/////////////////////////////////////////////////////////////////////////
// $Id$
2008-01-18 00:16:23 +03:00
/////////////////////////////////////////////////////////////////////////
//
2011-01-12 22:53:47 +03:00
// Copyright (c) 2007-2011 Stanislav Shwartsman
2008-01-18 00:16:23 +03:00
// 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
2009-01-16 21:18:59 +03:00
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
2008-01-18 00:16:23 +03:00
//
/////////////////////////////////////////////////////////////////////////
#define NEED_CPU_REG_SHORTCUTS 1
#include "bochs.h"
#include "cpu.h"
#define LOG_THIS BX_CPU_THIS_PTR
#include "param_names.h"
2008-01-18 00:16:23 +03:00
bxPageWriteStampTable pageWriteStampTable;
void flushICaches(void)
{
for (unsigned i=0; i<BX_SMP_PROCESSORS; i++) {
2008-01-18 00:16:23 +03:00
BX_CPU(i)->iCache.flushICacheEntries();
BX_CPU(i)->async_event |= BX_ASYNC_EVENT_STOP_TRACE;
}
2008-01-18 00:16:23 +03:00
pageWriteStampTable.resetWriteStamps();
}
void handleSMC(bx_phy_address pAddr, Bit32u mask)
{
for (unsigned i=0; i<BX_SMP_PROCESSORS; i++) {
BX_CPU(i)->async_event |= BX_ASYNC_EVENT_STOP_TRACE;
BX_CPU(i)->iCache.handleSMC(pAddr, mask);
}
}
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
BX_INSF_TYPE BX_CPU_C::BxEndTrace(bxInstruction_c *i)
{
// do nothing, return to main cpu_loop
}
void genDummyICacheEntry(bxInstruction_c *i, BxExecutePtr_tR execute)
{
i->setILen(0);
i->setIaOpcode(BX_INSERTED_OPCODE);
i->execute = execute;
}
#endif
bxICacheEntry_c* BX_CPU_C::serveICacheMiss(bxICacheEntry_c *entry, Bit32u eipBiased, bx_phy_address pAddr)
2008-01-18 00:16:23 +03:00
{
bxICacheEntry_c *vc_hit = BX_CPU_THIS_PTR iCache.lookup_victim_cache(pAddr, BX_CPU_THIS_PTR fetchModeMask);
if (vc_hit) {
return vc_hit;
}
BX_CPU_THIS_PTR iCache.victim_entry(entry, BX_CPU_THIS_PTR fetchModeMask);
2009-11-05 19:51:06 +03:00
BX_CPU_THIS_PTR iCache.alloc_trace(entry);
// Cache miss. We weren't so lucky, but let's be optimistic - try to build
// trace from incoming instruction bytes stream !
2009-11-05 19:51:06 +03:00
entry->pAddr = pAddr;
entry->traceMask = 0;
2008-01-18 00:16:23 +03:00
2008-01-22 19:20:30 +03:00
unsigned remainingInPage = BX_CPU_THIS_PTR eipPageWindowSize - eipBiased;
const Bit8u *fetchPtr = BX_CPU_THIS_PTR eipFetchPtr + eipBiased;
2009-12-21 16:38:06 +03:00
int ret;
2008-01-18 00:16:23 +03:00
2009-11-05 19:51:06 +03:00
bxInstruction_c *i = entry->i;
2008-01-18 00:16:23 +03:00
Bit32u pageOffset = PAGE_OFFSET((Bit32u) pAddr);
2011-01-25 23:59:26 +03:00
Bit32u traceMask = 0;
// Don't allow traces longer than cpu_loop can execute
static unsigned quantum =
#if BX_SUPPORT_SMP
(BX_SMP_PROCESSORS > 1) ? SIM->get_param_num(BXPN_SMP_QUANTUM)->get() :
#endif
BX_MAX_TRACE_LENGTH;
for (unsigned n=0;n < quantum;n++)
2008-01-18 00:16:23 +03:00
{
#if BX_SUPPORT_X86_64
if (BX_CPU_THIS_PTR cpu_mode == BX_MODE_LONG_64)
ret = fetchDecode64(fetchPtr, i, remainingInPage);
else
#endif
ret = fetchDecode32(fetchPtr, i, remainingInPage);
2009-12-21 16:38:06 +03:00
if (ret < 0) {
2008-01-18 00:16:23 +03:00
// Fetching instruction on segment/page boundary
if (n > 0) {
// The trace is already valid, it has several instructions inside,
// in this case just drop the boundary instruction and stop
// tracing.
break;
}
// First instruction is boundary fetch, leave the trace cache entry
// invalid for now because boundaryFetch() can fault
entry->pAddr = ~entry->pAddr;
2010-02-13 12:41:51 +03:00
entry->tlen = 1;
boundaryFetch(fetchPtr, remainingInPage, i);
// Add the instruction to trace cache
entry->pAddr = ~entry->pAddr;
entry->traceMask = 0x80000000; /* last line in page */
pageWriteStampTable.markICacheMask(entry->pAddr, entry->traceMask);
2012-03-13 19:18:21 +04:00
pageWriteStampTable.markICacheMask(BX_CPU_THIS_PTR pAddrFetchPage, 0x1);
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
entry->tlen++; /* Add the inserted end of trace opcode */
genDummyICacheEntry(++i, &BX_CPU_C::BxEndTrace);
#endif
2012-03-13 19:18:21 +04:00
BX_CPU_THIS_PTR iCache.commit_page_split_trace(BX_CPU_THIS_PTR pAddrFetchPage, entry);
return entry;
2008-01-18 00:16:23 +03:00
}
// add instruction to the trace
2008-01-18 00:16:23 +03:00
unsigned iLen = i->ilen();
2010-02-13 12:41:51 +03:00
entry->tlen++;
2008-01-18 00:16:23 +03:00
#ifdef BX_INSTR_STORE_OPCODE_BYTES
i->set_opcode_bytes(fetchPtr);
#endif
BX_INSTR_OPCODE(BX_CPU_ID, i, fetchPtr, iLen,
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b, long64_mode());
i++;
2011-01-25 23:59:26 +03:00
traceMask |= 1 << (pageOffset >> 7);
traceMask |= 1 << ((pageOffset + iLen - 1) >> 7);
// continue to the next instruction
2008-01-18 00:16:23 +03:00
remainingInPage -= iLen;
2009-12-21 16:38:06 +03:00
if (ret != 0 /* stop trace indication */ || remainingInPage == 0) break;
2008-01-18 00:16:23 +03:00
pAddr += iLen;
pageOffset += iLen;
2008-01-18 00:16:23 +03:00
fetchPtr += iLen;
// try to find a trace starting from current pAddr and merge
if (remainingInPage >= 15) { // avoid merging with page split trace
if (mergeTraces(entry, i, pAddr)) {
entry->traceMask |= traceMask;
pageWriteStampTable.markICacheMask(pAddr, entry->traceMask);
BX_CPU_THIS_PTR iCache.commit_trace(entry->tlen);
return entry;
}
}
2008-01-18 00:16:23 +03:00
}
//BX_INFO(("commit trace %08x len=%d mask %08x", (Bit32u) entry->pAddr, entry->tlen, pageWriteStampTable.getFineGranularityMapping(entry->pAddr)));
2011-01-25 23:59:26 +03:00
entry->traceMask |= traceMask;
pageWriteStampTable.markICacheMask(pAddr, entry->traceMask);
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
entry->tlen++; /* Add the inserted end of trace opcode */
genDummyICacheEntry(i, &BX_CPU_C::BxEndTrace);
#endif
2010-02-13 12:41:51 +03:00
BX_CPU_THIS_PTR iCache.commit_trace(entry->tlen);
return entry;
2008-01-18 00:16:23 +03:00
}
2008-03-03 18:34:03 +03:00
bx_bool BX_CPU_C::mergeTraces(bxICacheEntry_c *entry, bxInstruction_c *i, bx_phy_address pAddr)
2008-01-18 00:16:23 +03:00
{
bxICacheEntry_c *e = BX_CPU_THIS_PTR iCache.get_entry(pAddr, BX_CPU_THIS_PTR fetchModeMask);
2008-01-18 00:16:23 +03:00
if (e->pAddr == pAddr)
2008-01-18 00:16:23 +03:00
{
// determine max amount of instruction to take from another entry
2010-02-13 12:41:51 +03:00
unsigned max_length = e->tlen;
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
if (max_length + entry->tlen > BX_MAX_TRACE_LENGTH)
return 0;
#else
2010-02-13 12:41:51 +03:00
if (max_length + entry->tlen > BX_MAX_TRACE_LENGTH)
max_length = BX_MAX_TRACE_LENGTH - entry->tlen;
2008-01-18 00:16:23 +03:00
if(max_length == 0) return 0;
#endif
2008-01-18 00:16:23 +03:00
memcpy(i, e->i, sizeof(bxInstruction_c)*max_length);
2010-02-13 12:41:51 +03:00
entry->tlen += max_length;
BX_ASSERT(entry->tlen <= BX_MAX_TRACE_LENGTH);
2008-01-18 00:16:23 +03:00
entry->traceMask |= e->traceMask;
2008-01-18 00:16:23 +03:00
return 1;
}
return 0;
}
2010-02-13 13:35:51 +03:00
void BX_CPU_C::boundaryFetch(const Bit8u *fetchPtr, unsigned remainingInPage, bxInstruction_c *i)
{
unsigned j, k;
Bit8u fetchBuffer[32];
int ret;
if (remainingInPage >= 15) {
BX_ERROR(("boundaryFetch #GP(0): too many instruction prefixes"));
exception(BX_GP_EXCEPTION, 0);
2010-02-13 13:35:51 +03:00
}
// Read all leftover bytes in current page up to boundary.
for (j=0; j<remainingInPage; j++) {
fetchBuffer[j] = *fetchPtr++;
}
// The 2nd chunk of the instruction is on the next page.
// Set RIP to the 0th byte of the 2nd page, and force a
// prefetch so direct access of that physical page is possible, and
// all the associated info is updated.
RIP += remainingInPage;
prefetch();
unsigned fetchBufferLimit = 15;
if (BX_CPU_THIS_PTR eipPageWindowSize < 15) {
BX_DEBUG(("boundaryFetch: small window size after prefetch=%d bytes, remainingInPage=%d bytes", BX_CPU_THIS_PTR eipPageWindowSize, remainingInPage));
fetchBufferLimit = BX_CPU_THIS_PTR eipPageWindowSize;
}
// We can fetch straight from the 0th byte, which is eipFetchPtr;
fetchPtr = BX_CPU_THIS_PTR eipFetchPtr;
// read leftover bytes in next page
for (k=0; k<fetchBufferLimit; k++, j++) {
fetchBuffer[j] = *fetchPtr++;
}
#if BX_SUPPORT_X86_64
if (BX_CPU_THIS_PTR cpu_mode == BX_MODE_LONG_64)
ret = fetchDecode64(fetchBuffer, i, remainingInPage+fetchBufferLimit);
else
#endif
ret = fetchDecode32(fetchBuffer, i, remainingInPage+fetchBufferLimit);
if (ret < 0) {
BX_INFO(("boundaryFetch #GP(0): failed to complete instruction decoding"));
exception(BX_GP_EXCEPTION, 0);
2010-02-13 13:35:51 +03:00
}
// Restore EIP since we fudged it to start at the 2nd page boundary.
RIP = BX_CPU_THIS_PTR prev_rip;
// Since we cross an instruction boundary, note that we need a prefetch()
// again on the next instruction. Perhaps we can optimize this to
// eliminate the extra prefetch() since we do it above, but have to
// think about repeated instructions, etc.
// invalidate_prefetch_q();
#ifdef BX_INSTR_STORE_OPCODE_BYTES
i->set_opcode_bytes(fetchBuffer);
#endif
BX_INSTR_OPCODE(BX_CPU_ID, i, fetchBuffer, i->ilen(),
2010-02-13 13:35:51 +03:00
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b, long64_mode());
}