Debian Codegraph @

stable/mips64_mem.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 */
5
6 #define _GNU_SOURCE
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <assert.h>
15
16 #include "cpu.h"
17 #include "mips64_jit.h"
18 #include "vm.h"
19 #include "dynamips.h"
20 #include "memory.h"
21 #include "device.h"
22
23 /* MTS access with special access mask */
24 void mips64_access_special(cpu_mips_t *cpu,m_uint64_t vaddr,m_uint32_t mask,
25 u_int op_code,u_int op_type,u_int op_size,
26 m_uint64_t *data)
27 {
28 switch(mask) {
29 case MTS_ACC_U:
30 if (op_type == MTS_READ)
31 *data = 0;
32
33 if (cpu->gen->undef_mem_handler != NULL) {
34 if (cpu->gen->undef_mem_handler(cpu->gen,vaddr,op_size,op_type,
35 data))
36 return;
37 }
38
39 #if DEBUG_MTS_ACC_U
40 if (op_type == MTS_READ)
41 cpu_log(cpu->gen,
42 "MTS","read access to undefined address 0x%llx at "
43 "pc=0x%llx (size=%u)\n",vaddr,cpu->pc,op_size);
44 else
45 cpu_log(cpu->gen,
46 "MTS","write access to undefined address 0x%llx at "
47 "pc=0x%llx, value=0x%8.8llx (size=%u)\n",
48 vaddr,cpu->pc,*data,op_size);
49 #endif
50 break;
51
52 case MTS_ACC_T:
53 if (op_code != MIPS_MEMOP_LOOKUP) {
54
55 /* GR edit */
56 /* If the IOS tries to access memory at addr 0x0, it is probably */
57 /* a reload. Shut the vm down, otherwise 100% cpu and livespin */
58 if (vaddr == 0) {
59 cpu_log(cpu->gen,
60 "MTS","TLB exception suggests RELOAD for address 0x%llx at pc=0x%llx "
61 "(%s access, size=%u)\n",
62 vaddr,cpu->pc,(op_type == MTS_READ) ?
63 "read":"write",op_size);
64 vm_stop(cpu->vm);
65 }
66 /* GR edit end */
67
68 #if DEBUG_MTS_ACC_T
69 cpu_log(cpu->gen,
70 "MTS","TLB exception for address 0x%llx at pc=0x%llx "
71 "(%s access, size=%u)\n",
72 vaddr,cpu->pc,(op_type == MTS_READ) ?
73 "read":"write",op_size);
74 mips64_dump_regs(cpu->gen);
75 #if MEMLOG_ENABLE
76 memlog_dump(cpu->gen);
77 #endif
78 #endif
79
80 cpu->cp0.reg[MIPS_CP0_BADVADDR] = vaddr;
81
82 if (op_type == MTS_READ)
83 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_TLB_LOAD,0);
84 else
85 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_TLB_SAVE,0);
86
87 cpu_exec_loop_enter(cpu->gen);
88 }
89 break;
90
91 case MTS_ACC_AE:
92 if (op_code != MIPS_MEMOP_LOOKUP) {
93 #if DEBUG_MTS_ACC_AE
94 cpu_log(cpu->gen,
95 "MTS","AE exception for address 0x%llx at pc=0x%llx "
96 "(%s access)\n",
97 vaddr,cpu->pc,(op_type == MTS_READ) ? "read":"write");
98 #endif
99 cpu->cp0.reg[MIPS_CP0_BADVADDR] = vaddr;
100
101 if (op_type == MTS_READ)
102 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_ADDR_LOAD,0);
103 else
104 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_ADDR_SAVE,0);
105
106 cpu_exec_loop_enter(cpu->gen);
107 }
108 break;
109 }
110 }
111
112 /* === MTS for 64-bit address space ======================================= */
113 #define MTS_ADDR_SIZE 64
114 #define MTS_NAME(name) mts64_##name
115 #define MTS_NAME_UP(name) MTS64_##name
116 #define MTS_PROTO(name) mips64_mts64_##name
117 #define MTS_PROTO_UP(name) MIPS64_MTS64_##name
118
119 #include "mips_mts.c"
120
121 /* === MTS for 32-bit address space ======================================= */
122 #define MTS_ADDR_SIZE 32
123 #define MTS_NAME(name) mts32_##name
124 #define MTS_NAME_UP(name) MTS32_##name
125 #define MTS_PROTO(name) mips64_mts32_##name
126 #define MTS_PROTO_UP(name) MIPS64_MTS32_##name
127
128 #include "mips_mts.c"
129
130 /* === Specific operations for MTS64 ====================================== */
131
132 /* MTS64 slow lookup */
133 static mts64_entry_t *
134 mips64_mts64_slow_lookup(cpu_mips_t *cpu,m_uint64_t vaddr,
135 u_int op_code,u_int op_size,
136 u_int op_type,m_uint64_t *data,
137 mts64_entry_t *alt_entry)
138 {
139 m_uint32_t hash_bucket,zone,sub_zone,cca;
140 mts64_entry_t *entry;
141 mts_map_t map;
142
143 map.tlb_index = -1;
144 hash_bucket = MTS64_HASH(vaddr);
145 entry = &cpu->mts_u.mts64_cache[hash_bucket];
146 zone = vaddr >> 40;
147
148 #if DEBUG_MTS_STATS
149 cpu->mts_misses++;
150 #endif
151
152 switch(zone) {
153 case 0x000000: /* xkuseg */
154 case 0x400000: /* xksseg */
155 case 0xc00000: /* xkseg */
156 /* trigger TLB exception if no matching entry found */
157 if (!mips64_cp0_tlb_lookup(cpu,vaddr,&map))
158 goto err_tlb;
159
160 if (!(entry = mips64_mts64_map(cpu,op_type,&map,entry,alt_entry)))
161 goto err_undef;
162
163 return(entry);
164
165 case 0xffffff:
166 sub_zone = (vaddr >> 29) & 0x7FF;
167
168 switch(sub_zone) {
169 case 0x7fc: /* ckseg0 */
170 map.vaddr = vaddr & MIPS_MIN_PAGE_MASK;
171 map.paddr = map.vaddr - 0xFFFFFFFF80000000ULL;
172 map.offset = vaddr & MIPS_MIN_PAGE_IMASK;
173 map.cached = TRUE;
174
175 if (!(entry = mips64_mts64_map(cpu,op_type,&map,
176 entry,alt_entry)))
177 goto err_undef;
178
179 return(entry);
180
181 case 0x7fd: /* ckseg1 */
182 map.vaddr = vaddr & MIPS_MIN_PAGE_MASK;
183 map.paddr = map.vaddr - 0xFFFFFFFFA0000000ULL;
184 map.offset = vaddr & MIPS_MIN_PAGE_IMASK;
185 map.cached = FALSE;
186
187 if (!(entry = mips64_mts64_map(cpu,op_type,&map,
188 entry,alt_entry)))
189 goto err_undef;
190
191 return(entry);
192
193 case 0x7fe: /* cksseg */
194 case 0x7ff: /* ckseg3 */
195 /* trigger TLB exception if no matching entry found */
196 if (!mips64_cp0_tlb_lookup(cpu,vaddr,&map))
197 goto err_tlb;
198
199 if (!(entry = mips64_mts64_map(cpu,op_type,
200 &map,entry,alt_entry)))
201 goto err_undef;
202
203 return(entry);
204
205 default:
206 /* Invalid zone: generate Address Error (AE) exception */
207 goto err_address;
208 }
209 break;
210
211 /* xkphys */
212 case 0x800000:
213 case 0x880000:
214 case 0x900000:
215 case 0x980000:
216 case 0xa00000:
217 case 0xa80000:
218 case 0xb00000:
219 case 0xb80000:
220 cca = (vaddr >> MIPS64_XKPHYS_CCA_SHIFT) & 0x03;
221 map.cached = mips64_cca_cached(cca);
222 map.vaddr = vaddr & MIPS_MIN_PAGE_MASK;
223 map.paddr = (vaddr & MIPS64_XKPHYS_PHYS_MASK);
224 map.paddr &= MIPS_MIN_PAGE_MASK;
225 map.offset = vaddr & MIPS_MIN_PAGE_IMASK;
226
227 if (!(entry = mips64_mts64_map(cpu,op_type,&map,entry,alt_entry)))
228 goto err_undef;
229
230 return(entry);
231
232 default:
233 /* Invalid zone: generate Address Error (AE) exception */
234 goto err_address;
235 }
236
237 err_undef:
238 mips64_access_special(cpu,vaddr,MTS_ACC_U,op_code,op_type,op_size,data);
239 return NULL;
240 err_address:
241 mips64_access_special(cpu,vaddr,MTS_ACC_AE,op_code,op_type,op_size,data);
242 return NULL;
243 err_tlb:
244 mips64_access_special(cpu,vaddr,MTS_ACC_T,op_code,op_type,op_size,data);
245 return NULL;
246 }
247
248 /* MTS64 access */
249 static forced_inline
250 void *mips64_mts64_access(cpu_mips_t *cpu,m_uint64_t vaddr,
251 u_int op_code,u_int op_size,
252 u_int op_type,m_uint64_t *data)
253 {
254 mts64_entry_t *entry,alt_entry;
255 m_uint32_t hash_bucket;
256 m_iptr_t haddr;
257 u_int dev_id;
258 int cow;
259
260 #if MEMLOG_ENABLE
261 /* Record the memory access */
262 memlog_rec_access(cpu->gen,vaddr,*data,op_size,op_type);
263 #endif
264
265 hash_bucket = MTS64_HASH(vaddr);
266 entry = &cpu->mts_u.mts64_cache[hash_bucket];
267
268 #if DEBUG_MTS_STATS
269 cpu->mts_lookups++;
270 #endif
271
272 /* Copy-On-Write for sparse device ? */
273 cow = (op_type == MTS_WRITE) && (entry->flags & MTS_FLAG_COW);
274
275 /* Slow lookup if nothing found in cache */
276 if (unlikely(((vaddr & MIPS_MIN_PAGE_MASK) != entry->gvpa) || cow)) {
277 entry = mips64_mts64_slow_lookup(cpu,vaddr,op_code,op_size,op_type,
278 data,&alt_entry);
279 if (!entry)
280 return NULL;
281
282 if (entry->flags & MTS_FLAG_DEV) {
283 dev_id = (entry->hpa & MTS_DEVID_MASK) >> MTS_DEVID_SHIFT;
284 haddr = entry->hpa & MTS_DEVOFF_MASK;
285 return(dev_access_fast(cpu->gen,dev_id,haddr,op_size,op_type,data));
286 }
287 }
288
289 /* Raw memory access */
290 haddr = entry->hpa + (vaddr & MIPS_MIN_PAGE_IMASK);
291 #if MEMLOG_ENABLE
292 memlog_update_read(cpu->gen,haddr);
293 #endif
294 return((void *)haddr);
295 }
296
297 /* MTS64 virtual address to physical page translation */
298 static fastcall int mips64_mts64_translate(cpu_mips_t *cpu,m_uint64_t vaddr,
299 m_uint32_t *phys_page)
300 {
301 mts64_entry_t *entry,alt_entry;
302 m_uint32_t hash_bucket;
303 m_uint64_t data = 0;
304
305 hash_bucket = MTS64_HASH(vaddr);
306 entry = &cpu->mts_u.mts64_cache[hash_bucket];
307
308 /* Slow lookup if nothing found in cache */
309 if (unlikely((vaddr & MIPS_MIN_PAGE_MASK) != entry->gvpa)) {
310 entry = mips64_mts64_slow_lookup(cpu,vaddr,MIPS_MEMOP_LOOKUP,4,MTS_READ,
311 &data,&alt_entry);
312 if (!entry)
313 return(-1);
314 }
315
316 *phys_page = entry->gppa >> MIPS_MIN_PAGE_SHIFT;
317 return(0);
318 }
319
320 /* === Specific operations for MTS32 ====================================== */
321
322 /* MTS32 slow lookup */
323 static mts32_entry_t *
324 mips64_mts32_slow_lookup(cpu_mips_t *cpu,m_uint64_t vaddr,
325 u_int op_code,u_int op_size,
326 u_int op_type,m_uint64_t *data,
327 mts32_entry_t *alt_entry)
328 {
329 m_uint32_t hash_bucket,zone;
330 mts32_entry_t *entry;
331 mts_map_t map;
332
333 map.tlb_index = -1;
334 hash_bucket = MTS32_HASH(vaddr);
335 entry = &cpu->mts_u.mts32_cache[hash_bucket];
336 zone = (vaddr >> 29) & 0x7;
337
338 #if DEBUG_MTS_STATS
339 cpu->mts_misses++;
340 #endif
341
342 switch(zone) {
343 case 0x00 ... 0x03: /* kuseg */
344 /* trigger TLB exception if no matching entry found */
345 if (!mips64_cp0_tlb_lookup(cpu,vaddr,&map))
346 goto err_tlb;
347
348 if (!(entry = mips64_mts32_map(cpu,op_type,&map,entry,alt_entry)))
349 goto err_undef;
350
351 return(entry);
352
353 case 0x04: /* kseg0 */
354 map.vaddr = vaddr & MIPS_MIN_PAGE_MASK;
355 map.paddr = map.vaddr - 0xFFFFFFFF80000000ULL;
356 map.offset = vaddr & MIPS_MIN_PAGE_IMASK;
357 map.cached = TRUE;
358
359 if (!(entry = mips64_mts32_map(cpu,op_type,&map,entry,alt_entry)))
360 goto err_undef;
361
362 return(entry);
363
364 case 0x05: /* kseg1 */
365 map.vaddr = vaddr & MIPS_MIN_PAGE_MASK;
366 map.paddr = map.vaddr - 0xFFFFFFFFA0000000ULL;
367 map.offset = vaddr & MIPS_MIN_PAGE_IMASK;
368 map.cached = FALSE;
369
370 if (!(entry = mips64_mts32_map(cpu,op_type,&map,entry,alt_entry)))
371 goto err_undef;
372
373 return(entry);
374
375 case 0x06: /* ksseg */
376 case 0x07: /* kseg3 */
377 /* trigger TLB exception if no matching entry found */
378 if (!mips64_cp0_tlb_lookup(cpu,vaddr,&map))
379 goto err_tlb;
380
381 if (!(entry = mips64_mts32_map(cpu,op_type,&map,entry,alt_entry)))
382 goto err_undef;
383
384 return(entry);
385 }
386
387 err_undef:
388 mips64_access_special(cpu,vaddr,MTS_ACC_U,op_code,op_type,op_size,data);
389 return NULL;
390 #if 0
391 err_address:
392 mips64_access_special(cpu,vaddr,MTS_ACC_AE,op_code,op_type,op_size,data);
393 return NULL;
394 #endif
395 err_tlb:
396 mips64_access_special(cpu,vaddr,MTS_ACC_T,op_code,op_type,op_size,data);
397 return NULL;
398 }
399
400 /* MTS32 access */
401 static forced_inline
402 void *mips64_mts32_access(cpu_mips_t *cpu,m_uint64_t vaddr,
403 u_int op_code,u_int op_size,
404 u_int op_type,m_uint64_t *data)
405 {
406 mts32_entry_t *entry,alt_entry;
407 m_uint32_t hash_bucket;
408 m_iptr_t haddr;
409 u_int dev_id;
410 int cow;
411
412 #if MEMLOG_ENABLE
413 /* Record the memory access */
414 memlog_rec_access(cpu->gen,vaddr,*data,op_size,op_type);
415 #endif
416
417 hash_bucket = MTS32_HASH(vaddr);
418 entry = &cpu->mts_u.mts32_cache[hash_bucket];
419
420 #if DEBUG_MTS_STATS
421 cpu->mts_lookups++;
422 #endif
423
424 /* Copy-On-Write for sparse device ? */
425 cow = (op_type == MTS_WRITE) && (entry->flags & MTS_FLAG_COW);
426
427 /* Slow lookup if nothing found in cache */
428 if (unlikely((((m_uint32_t)vaddr & MIPS_MIN_PAGE_MASK) != entry->gvpa) ||
429 cow))
430 {
431 entry = mips64_mts32_slow_lookup(cpu,vaddr,op_code,op_size,op_type,
432 data,&alt_entry);
433 if (!entry)
434 return NULL;
435
436 if (entry->flags & MTS_FLAG_DEV) {
437 dev_id = (entry->hpa & MTS_DEVID_MASK) >> MTS_DEVID_SHIFT;
438 haddr = entry->hpa & MTS_DEVOFF_MASK;
439 return(dev_access_fast(cpu->gen,dev_id,haddr,op_size,op_type,data));
440 }
441 }
442
443 /* Raw memory access */
444 haddr = entry->hpa + (vaddr & MIPS_MIN_PAGE_IMASK);
445 #if MEMLOG_ENABLE
446 memlog_update_read(cpu->gen,haddr);
447 #endif
448 return((void *)haddr);
449 }
450
451 /* MTS32 virtual address to physical page translation */
452 static fastcall int mips64_mts32_translate(cpu_mips_t *cpu,m_uint64_t vaddr,
453 m_uint32_t *phys_page)
454 {
455 mts32_entry_t *entry,alt_entry;
456 m_uint32_t hash_bucket;
457 m_uint64_t data = 0;
458
459 hash_bucket = MTS32_HASH(vaddr);
460 entry = &cpu->mts_u.mts32_cache[hash_bucket];
461
462 /* Slow lookup if nothing found in cache */
463 if (unlikely(((m_uint32_t)vaddr & MIPS_MIN_PAGE_MASK) != entry->gvpa)) {
464 entry = mips64_mts32_slow_lookup(cpu,vaddr,MIPS_MEMOP_LOOKUP,4,MTS_READ,
465 &data,&alt_entry);
466 if (!entry)
467 return(-1);
468 }
469
470 *phys_page = entry->gppa >> MIPS_MIN_PAGE_SHIFT;
471 return(0);
472 }
473
474 /* ======================================================================== */
475
476 /* Shutdown MTS subsystem */
477 void mips64_mem_shutdown(cpu_mips_t *cpu)
478 {
479 if (cpu->mts_shutdown != NULL)
480 cpu->mts_shutdown(cpu);
481 }
482
483 /* Set the address mode */
484 int mips64_set_addr_mode(cpu_mips_t *cpu,u_int addr_mode)
485 {
486 if (cpu->addr_mode != addr_mode) {
487 mips64_mem_shutdown(cpu);
488
489 switch(addr_mode) {
490 case 32:
491 mips64_mts32_init(cpu);
492 mips64_mts32_init_memop_vectors(cpu);
493 break;
494 case 64:
495 mips64_mts64_init(cpu);
496 mips64_mts64_init_memop_vectors(cpu);
497 break;
498 default:
499 fprintf(stderr,
500 "mts_set_addr_mode: internal error (addr_mode=%u)\n",
501 addr_mode);
502 exit(EXIT_FAILURE);
503 }
504 }
505
506 return(0);
507 }