Debian Codegraph @

unstable/mips64_mem.c

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