tc.c
8.87 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/****************************************************************
* tc.c - Translation cache
*
* Author: $Author: blythe $
* Date: $Date: 2002/05/29 01:09:09 $
*****************************************************************/
#define _BSD_SIGNALS
#include <stdio.h>
#include <stdlib.h>
#ifdef sgi
#include <sys/cachectl.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <strings.h>
#include "simtypes.h"
#include "sim_error.h"
#include "simutil.h"
#include "tc.h"
#include "userflush.h"
#include "sim_error.h"
#include "cpu_interface.h"
#include "arch_specifics.h"
#define HOST_PAGE_SIZE (getpagesize())
#define NEXT_HOST_PAGE(_x) ( ((size_t)(_x)+HOST_PAGE_SIZE-1) & ~(HOST_PAGE_SIZE-1))
extern int continue_run();
static int logPC = 0;
int tcGenNumber = 0;
static int numCaches;
static Inst breakOpcode;
static struct TC {
char *fixedStart;
char *fixedEnd;
TCA curPos;
char *curEnd;
int annotationBuffer;
int pctcSize;
int mask;
Reg genId;
struct PCTCEntry{
VA vAddr;
MA mAddr;
TCA tca;
Reg genId;
} *pctc;
} real_tc[TC_MAX_CACHES];
/* #define DEBUG_TCFLUSH */
/* User, Kernel and Interface code Translation Caches */
#define TC_USER 0
#define TC_KERN 1
#define TC_GLUE 2
#ifdef DEBUG_TCFLUSH
static char *flushType[4];
#endif
#define TC_START 0x0c000000
#define PCTC_ASSOC 4
#define PCTC_HASH(_code,_ma) ((PTR_TO_UINT(_ma)>>2 & real_tc[_code].mask)*PCTC_ASSOC)
#ifdef sgi
struct {
int count;
VA pc;
SimTime cc;
} lastSigIll;
static void TC_SIGILLHandler(int sig, int code, struct sigcontext *scp)
{
int done = 1;
intPtrSize pc = (intPtrSize)scp->sc_pc;
SimTime cc = CPUVec.CycleCount(0);
/* This should really take into account how many TC's we have,
* which could vary. On the SGI's (this ifdef) we have 3.
*/
if (TC_InTC(TC_USER,(TCA)pc) || TC_InTC(TC_KERN,(TCA)pc) ||
TC_InTC(TC_GLUE,(TCA)pc)) {
if (pc != lastSigIll.pc ||
cc != lastSigIll.cc) {
done = 0;
}
}
lastSigIll.cc = cc;
lastSigIll.pc = pc;
lastSigIll.count++;
if (done) {
CPUError("%lld TC_SIGILL: sig=%d code=%d scp=0x%x pc=0x%x --> abort\n",
CPUVec.CycleCount(0),sig,code,scp,pc);
} else {
CPUWarning("%lld TC_SIGILL: sig=%d code=%d scp=0x%x pc=0x%x (continue) \n",
CPUVec.CycleCount(0),sig,code,scp,pc);
slowcacheflush((char*)pc, 32*1024, BCACHE);
}
}
#endif
#ifdef __alpha
#define BCACHE 12 /* bogus */
static void TC_SIGILLHandler(int sig)
{
CPUError("TC_SIGILLHandler signal=%d\n",sig);
slowcacheflush(0,16*1024,BCACHE);
}
#endif
void TC_init(int numC, int cacheSizes[], int pctcSize[], int annotationBuffer[], Inst breakOp )
{
static int initialized=0;
int malloc_size;
uint *ptr;
int zfd,i;
if (initialized) {
TC_flush( TCFLUSH_ALL );
return;
}
initialized = 1;
numCaches = numC;
breakOpcode = breakOp;
ASSERT(numCaches >= 0 && numCaches < TC_MAX_CACHES);
malloc_size = 3*HOST_PAGE_SIZE;
for (i=0;i<numCaches;i++) {
real_tc[i].annotationBuffer = annotationBuffer[i];
real_tc[i].pctcSize = pctcSize[i];
real_tc[i].pctc = (struct PCTCEntry*)malloc(pctcSize[i]*sizeof(struct PCTCEntry));
bzero((char*)real_tc[i].pctc,pctcSize[i]*sizeof(struct PCTCEntry));
real_tc[i].mask = (pctcSize[i] / PCTC_ASSOC)-1;
ASSERT((1<<GetLog2(pctcSize[i]))==pctcSize[i]);
ASSERT (cacheSizes[i] >=2*annotationBuffer[i]);
malloc_size += cacheSizes[i];
}
#if defined(__alpha)
ptr = (uint*)malloc( malloc_size);
bzero((char*)ptr,malloc_size);
ASSERT(ptr);
#elif defined(_ABIN32)
ptr = (uint *) memalign(HOST_PAGE_SIZE, malloc_size);
if (ptr == NULL) {
perror("malloc of TC");
CPUError("Can't allocate TC\n");
}
/* Make sure TC is in jump inst range of code segment */
ASSERT((((uint)ptr ^ (uint)continue_run) >> 28) == 0);
#else /* O32 */
zfd = open("/dev/zero", O_RDWR, 0);
if (zfd < 0) {
perror( "Opening /dev/zero in TC_init");
CPUError("TC_init can't start");
}
ptr = (uint *)mmap((uint*)TC_START, malloc_size,
PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, zfd, 0);
if (ptr != (uint *) TC_START) {
perror("mapping tc_init");
CPUError("Can't map tc_inits");
}
#endif
for (i=0;i<numCaches;i++) {
real_tc[i].fixedStart = (char *)NEXT_HOST_PAGE(ptr);
real_tc[i].fixedEnd = real_tc[i].fixedStart + cacheSizes[i];
real_tc[i].curPos = (TCA)real_tc[i].fixedEnd; /* force flush */
ptr = (uint *)real_tc[i].fixedEnd +1;
}
if (signal(SIGILL, TC_SIGILLHandler) == SIG_ERR) CPUError("signal(SIGILL)");
/* This sets next and kern_next */
TC_flush( TCFLUSH_ALL );
initialized = 1;
}
static void TC_flush1(int code)
{
TCA tca;
uint size;
/* This is not true for Embra - we have a third cache which
* contains the glue code */
/* ASSERT (code == TC_USER || code == TC_KERN); */
#if 0
CPUPrint("Translation Cache flush from 0x%llx to 0x%llx for %4d KB\n",
(uint64)real_tc[code].fixedStart,
(uint64)real_tc[code].curPos,
((uint64)real_tc[code].curPos-(uint64)real_tc[code].fixedStart)/1024);
#endif
for(tca=(TCA)real_tc[code].fixedStart;
tca !=(TCA)real_tc[code].curPos;
tca++) {
*tca = breakOpcode;
}
real_tc[code].curPos = (TCA)real_tc[code].fixedStart;
real_tc[code].curEnd = real_tc[code].fixedEnd -
real_tc[code].annotationBuffer;
size = PTR_TO_UINT(tca) - PTR_TO_UINT(real_tc[code].fixedStart);
if (size > 0 && slowcacheflush(real_tc[code].fixedStart, size, BCACHE) == -1) {
perror("TC_flush1");
}
real_tc[code].genId++;
}
void TC_flush(int code )
{
#ifdef DEBUG_TCFLUSH
CPUWarning("EMBRA: %10lld TC_flush %s \n",
(uint64)CPUVec.CycleCount(0),
flushType[code]);
#endif
if (code==TCFLUSH_ALL) {
int i;
for (i=0;i<numCaches;i++){
TC_flush1(i);
}
} else {
ASSERT (code>=0 && code < numCaches);
TC_flush1(code);
}
tcGenNumber++;
}
TCA TC_GetTCPtr( int cache )
{
ASSERT( cache>=0 && cache < numCaches);
return real_tc[cache].curPos;
}
void TC_SetTCNext(int cache , TCA start, TCA finish )
{
if( usercacheflush((void *) start,
(char*)finish - (char*)start,
BCACHE) <0) {
CPUWarning("Cacheflush failure\n");
}
ASSERT( start == real_tc[cache].curPos);
ASSERT( PTR_TO_UINT(finish) < PTR_TO_UINT(real_tc[cache].curEnd));
ASSERT( PTR_TO_UINT(finish) < PTR_TO_UINT(real_tc[cache].fixedEnd));
real_tc[cache].curPos = finish;
}
int TC_InTC( int cache, TCA addr)
{
return( PTR_TO_UINT(addr) >= PTR_TO_UINT(real_tc[cache].fixedStart) &&
PTR_TO_UINT(addr) < PTR_TO_UINT(real_tc[cache].curEnd) );
}
int TC_Is_Room( int lenInInstr, int cache )
{
return( TC_InTC(cache,real_tc[cache].curPos+lenInInstr ) );
}
void TC_IncrementSize(int cache, int size)
{
if (real_tc[cache].curEnd==real_tc[cache].fixedEnd) {
/*
* We'we already been here and the warning has been written out
* ignore
*/
return;
}
if (real_tc[cache].curEnd+size >= real_tc[cache].fixedEnd) {
/*
* overflow
*/
CPUWarning("EMBRA: TC annotation overflow buffer full. Repeatability is broken. Increase tc.h:TC_ANNOTATION_SIZE \n");
real_tc[cache].curEnd = real_tc[cache].fixedEnd;
return;
}
real_tc[cache].curEnd += size;
}
void TC_PCInsert(int cache, TCA tca, VA vAddr, MA mAddr)
{
int idx = PCTC_HASH(cache,mAddr);
int set = (PTR_TO_UINT(tca)>>2)%PCTC_ASSOC; /* random */
int i;
struct PCTCEntry *e = &real_tc[cache].pctc[idx];
ASSERT (cache<numCaches);
ASSERT (TC_InTC(cache,tca));
for (i=0;i<PCTC_ASSOC;i++) {
if (e[i].genId!=real_tc[cache].genId) {
set = i;
break;
}
}
e[set].vAddr = vAddr;
e[set].mAddr = mAddr;
e[set].tca = tca;
e[set].genId = real_tc[cache].genId;
}
TCA TC_PCLookup(int cache, VA vAddr, MA mAddr)
{
int idx = PCTC_HASH(cache,mAddr);
int i;
int genId = real_tc[cache].genId;
struct PCTCEntry *e = &real_tc[cache].pctc[idx];
if (logPC) {
CPUPrint("pclog: VA=%llx\n", vAddr);
}
for(i=0;i<PCTC_ASSOC;i++,e++) {
if (e->genId!=genId) {
return 0;
}
if (e->mAddr==mAddr && e->vAddr==vAddr) {
if (logPC) {
CPUPrint("pclog TC=%llx\n", e->tca);
}
return e->tca;
}
}
return 0;
}