File: icache.h

package info (click to toggle)
bochs 2.3-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,116 kB
  • ctags: 16,927
  • sloc: cpp: 130,524; ansic: 18,822; sh: 7,922; makefile: 3,836; yacc: 1,056; asm: 463; perl: 381; lex: 280; csh: 3
file content (168 lines) | stat: -rwxr-xr-x 5,199 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/////////////////////////////////////////////////////////////////////////
// $Id: icache.h,v 1.17 2006/05/19 20:04:33 sshwarts Exp $
/////////////////////////////////////////////////////////////////////////
//
//  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
#define BX_ICACHE_H

#include <assert.h>

// 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.
const Bit32u ICacheWriteStampInvalid = 0xffffffff;
const Bit32u ICacheWriteStampStart   = 0x3fffffff;
const Bit32u ICacheWriteStampMask    = 0x3fffffff;
const Bit32u ICacheFetchModeMask     = ~ICacheWriteStampMask;

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;
  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];
    resetWriteStamps();
  }

  BX_CPP_INLINE Bit32u getPageWriteStamp(bx_phy_address pAddr) const
  {
    if (pAddr < memSizeInBytes) 
       return pageWriteStampTable[pAddr>>12];
    else 
       return ICacheWriteStampInvalid;
  }

  BX_CPP_INLINE const Bit32u *getPageWriteStampPtr(bx_phy_address pAddr) const
  {
    if (pAddr < memSizeInBytes) 
       return &pageWriteStampTable[pAddr>>12];
    else 
       return &ICacheWriteStampInvalid;
  }

  BX_CPP_INLINE void setPageWriteStamp(bx_phy_address pAddr, Bit32u pageWriteStamp)
  {
    if (pAddr < memSizeInBytes)
       pageWriteStampTable[pAddr>>12] = pageWriteStamp;
  }

  BX_CPP_INLINE void decWriteStamp(bx_phy_address pAddr)
  {
    assert(pAddr < memSizeInBytes);
    // Decrement page write stamp, so iCache entries with older stamps
    // are effectively invalidated.
    pageWriteStampTable[pAddr >> 12]--;
  }

  BX_CPP_INLINE void resetWriteStamps(void);
  BX_CPP_INLINE void purgeWriteStamps(void);
};

BX_CPP_INLINE void bxPageWriteStampTable::resetWriteStamps(void)
{
  for (Bit32u i=0; i<(memSizeInBytes>>12); i++) {
    pageWriteStampTable[i] = ICacheWriteStampInvalid;
  }
}

BX_CPP_INLINE void bxPageWriteStampTable::purgeWriteStamps(void)
{
  for (Bit32u i=0; i<(memSizeInBytes>>12); i++) {
    pageWriteStampTable[i] |= ICacheWriteStampMask;
  }
}

extern bxPageWriteStampTable pageWriteStampTable;

#define BxICacheEntries (32 * 1024)  // Must be a power of 2.

struct bxICacheEntry_c
{
  bx_phy_address 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
};

class BOCHSAPI bxICache_c {
public:
  bxICacheEntry_c entry[BxICacheEntries];

public:
  bxICache_c() { flushICacheEntries(); }

  BX_CPP_INLINE unsigned hash(bx_phy_address pAddr) const
  {
    // A pretty dumb hash function for now.
    return pAddr & (BxICacheEntries-1);
  }

  BX_CPP_INLINE void purgeICacheEntries(void);
  BX_CPP_INLINE void flushICacheEntries(void);
};

BX_CPP_INLINE void bxICache_c::flushICacheEntries(void)
{
    bxICacheEntry_c* e = entry;
    for (unsigned i=0; i<BxICacheEntries; i++, e++) {
      e->writeStamp = ICacheWriteStampInvalid;
    }
}

BX_CPP_INLINE void bxICache_c::purgeICacheEntries(void)
{
  bxICacheEntry_c* e = entry;
    
  // 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).
  for (unsigned i=0;i<BxICacheEntries;i++,e++)
  {
    Bit32u pageWriteStamp = pageWriteStampTable.getPageWriteStamp(e->pAddr);
    if (e->writeStamp != pageWriteStamp)
      e->writeStamp = ICacheWriteStampInvalid;	// invalidate entry
    else
      e->writeStamp |= ICacheWriteStampMask;
  }
}

extern void purgeICaches(void);
extern void flushICaches(void);

#endif