File: cl-tlb.c

package info (click to toggle)
spim 6.2-3
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 2,248 kB
  • ctags: 3,837
  • sloc: ansic: 14,284; asm: 10,355; yacc: 1,872; makefile: 742; lex: 610; sh: 95
file content (330 lines) | stat: -rw-r--r-- 6,159 bytes parent folder | download
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
!/* SPIM S20 MIPS Cycle Level simulator.
   Definitions for the SPIM S20 Cycle Level Simulator (SPIM-CL).
   Copyright (C) 1991-1992 by Anne Rogers (amr@cs.princeton.edu) and
   Scott Rosenberg (scottr@cs.princeton.edu)
   ALL RIGHTS RESERVED.

   SPIM-CL is distributed under the following conditions:

     You may make copies of SPIM-CL for your own use and modify those copies.

     All copies of SPIM-CL must retain our names and copyright notice.

     You may not sell SPIM-CL or distributed SPIM-CL in conjunction with a
     commerical product or service without the expressed written consent of
     Anne Rogers.

   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   PURPOSE.
*/

#include "spim.h"
#include "inst.h"
#include "reg.h"
#include "mem.h"
#include "mips-syscall.h"
#include "cl-mem.h"
#include "cl-tlb.h"


struct tlb_entry {
 unsigned int upper, lower;
};


/* Exported Variables: */

int tlb_on;			/* !0 -> tlb handling is on */


/* Local Functions: */
#ifdef __STDC__
unsigned int find_page (unsigned int vpn);
#else
unsigned int find_page ();
#endif


#define TLB_SIZE 64		/* tlb size */
#define PT_INCR 100		/* increment size for external page table */
#define tlb_VPN(i) (((TLB[i].upper) >> 12) & 0xfffff)
#define tlb_PID(i) (((TLB[i].upper) >> 6) & (0x3f))
#define tlb_PFN(i) (((TLB[i].lower) >> 12) & (0xfffff))
#define tlb_N(i) (((TLB[i].lower) >> 11) & 0x1)
#define tlb_D(i) (((TLB[i].lower) >> 10) & 0x1)
#define tlb_V(i) (((TLB[i].lower) >> 9) & 0x1)
#define tlb_G(i) (((TLB[i].lower) >> 8) & 0x1)

#define tlb_u_update(i, VPN, PID)          \
{ TLB[i].upper = ((VPN << 12) | ((PID & 0x3f) << 6)); }

#define tlb_l_update(i, PFN, N, D, V, G)   \
{ TLB[i].lower = ((PFN << 12) | (N << 11) | (D << 10) | (V << 9) | (G << 1)); }

#define tbl_update(i, VPN, PID, PFN, N, D, V, G)  \
{ tlb_u_update(i, VPN, PID);                      \
  tlb_l_update(i, PFN, N, D, V, G);               \
}


/* local variables */

static struct tlb_entry TLB[TLB_SIZE];	/* tlb's register-based page table */
static struct tlb_entry *PT = NULL;	/* external page table information */
static int pt_high;
static int pt_size;



#ifdef __STDC__
void
tlb_init (void)
#else
void
tlb_init ()
#endif
{
  int i;

  for (i=1; i < TLB_SIZE; i++) {
    TLB[i].upper = 0;
    TLB[i].lower = 0;
  }

  Random = 63 << 8;

  free (PT);
  PT = NULL;
  pt_high = 0;
  pt_size = 0;
}


/* Virtual Address Translation  -- See Kane Ch. 4*/
/* VPN is in the low order 20 bits of the variable vpn */
/* PID is in the low order 6 bits of the variable pid */
#ifdef __STDC__
int
tlb_vat (mem_addr addr, unsigned int pid, int l_or_s, mem_addr *paddr)
#else
int
tlb_vat(addr, pid, l_or_s, paddr)
  mem_addr addr, *paddr;
  unsigned int pid;
  int l_or_s;
#endif
{
  int i, msb;
  mem_addr temp_badvaddr;
  unsigned int vpn;

  if (!tlb_on) {
    *paddr = addr;
    return CACHEABLE;
  }

  vpn = addr >> 12;
  temp_badvaddr = BadVAddr;
  BadVAddr = addr & 0xfffff000;

  msb = (vpn >> 19) & 0x1;


  if (msb & (Status_Reg & 0x2))
    return ((l_or_s) ? ADDRL_EXCPT : ADDRS_EXCPT);


  for (i=0; i < TLB_SIZE; i++) {
    if (tlb_VPN(i) == vpn)
      break;
  }

  if (i >= TLB_SIZE)  {
    return ((l_or_s) ? TLBL_EXCPT : TLBS_EXCPT);
  }


  if ((!tlb_G(i)) && (tlb_PID(i) != pid))
    return ((l_or_s) ? TLBL_EXCPT : TLBS_EXCPT);


  if (!(tlb_V(i))) {
    return ((l_or_s) ? TLBL_EXCPT : TLBS_EXCPT);
  }

  if (!(tlb_D(i)) && !(l_or_s)) {
    return (MOD_EXCPT);
  }

  BadVAddr = temp_badvaddr;
  *paddr = (tlb_PFN(i) << 12) | (addr & 0xfff);

  return ((tlb_N(i)) ? CACHEABLE : NOT_CACHEABLE);
}




/* TLB Probe */

#ifdef __STDC__
void
tlbp (void)
#else
void
tlbp ()
#endif
{
  int vpn, pid;
  int i;

  vpn = EntryHI >> 12;
  pid = (EntryHI >> 6) & 0x3f;

  for (i=0; i < TLB_SIZE; i++)
    if (tlb_VPN(i) == vpn)
      break;

  if ((i == TLB_SIZE) || (tlb_PID(i) != pid))
    /* no match found */
    Index = (1 << 31);
  else
    Index = (i << 8);
}


/* TLB Read */

#ifdef __STDC__
void
tlbr (void)
#else
void
tlbr ()
#endif
{
  int index;

  index = (Index >> 8) & 0x3f;
  EntryHI = TLB[index].upper;
  EntryLO = TLB[index].lower;
}


/* TLB Write Indexed */

#ifdef __STDC__
void
tlbwi (void)
#else
void
tlbwi ()
#endif
{
  int index;

  index = (Index >> 8) & 0x3f;
  TLB[index].upper = EntryHI;
  TLB[index].lower = EntryLO;
}


/* TLB Write Random */

#ifdef __STDC__
void
tlbwr (void)
#else
void
tlbwr ()
#endif
{
  int index;

  index = (Random >> 8) & 0x3f;
  TLB[index].upper = EntryHI;
  TLB[index].lower = EntryLO;
}



/* Service a TLB miss */

#ifdef __STDC__
void
tlb_service (unsigned int pid, int l_or_s)
#else
void
tlb_service (pid, l_or_s)
  unsigned int pid;
  int l_or_s;
#endif
{
  unsigned int vpn, pfn;

  vpn = (BadVAddr >> 12) & 0xfffff;
  pfn = find_page(vpn);
  EntryHI = ((vpn << 12) | (pid << 6));
  EntryLO = ((pfn << 12) | (1 << 11) | (!l_or_s << 10) | (1 << 9));

  tlbp();
  if (Index >> 31)
    /* No match found */
    tlbwr();
  else
    tlbwi();

}


#ifdef __STDC__
unsigned int
find_page (unsigned int vpn)
#else
unsigned int find_page (vpn)
  unsigned int vpn;
#endif
{
  int i;

  for (i = 0; i < pt_high; i++)
    if ((((PT[i].upper >> 12) & 0xfffff) == vpn) &&
	((PT[i].lower >> 9) & 0x1))
      {
	return i;
      }

  if (pt_high >= pt_size) {
    /* Initialization sets PT to NULL so must use malloc for the first time */
    if (PT == NULL)
      PT = (struct tlb_entry *) malloc(PT_INCR * sizeof(struct tlb_entry));
    else
      PT = (struct tlb_entry *)
	realloc(PT, (pt_size + PT_INCR) * sizeof(struct tlb_entry));
    if (PT == NULL) {
      printf("malloc/realloc in find_page failed -- %d\n",
	     (pt_size+PT_INCR) * sizeof(struct tlb_entry));
      exit(1);
    }

    /* Set pt_size to new boundary */
    pt_size += PT_INCR;

    /* initialize entries in new_space, that is, make them invalid. */
    for (i=pt_high; i < pt_size; i++)
      PT[i].lower = 0;
  }

  PT[pt_high].upper = vpn << 12;
  PT[pt_high].lower = 1 << 9;
  return pt_high++;

}