Debian Codegraph @

unstable/ppc32_mem.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * PowerPC MMU.
6 */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <assert.h>
17
18 #include "cpu.h"
19 #include "vm.h"
20 #include "dynamips.h"
21 #include "memory.h"
22 #include "device.h"
23 #include "ppc32_jit.h"
24
25 #define DEBUG_ICBI 0
26
27 /* Memory access with special access mask */
28 void ppc32_access_special(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int cid,
29 m_uint32_t mask,u_int op_code,u_int op_type,
30 u_int op_size,m_uint64_t *data)
31 {
32 switch(mask) {
33 case MTS_ACC_T:
34 if (op_code != PPC_MEMOP_LOOKUP) {
35 #if DEBUG_MTS_ACC_T
36 cpu_log(cpu->gen,
37 "MTS","MMU exception for address 0x%8.8x at ia=0x%8.8x "
38 "(%s access, size=%u)\n",
39 vaddr,cpu->ia,(op_type == MTS_READ) ?
40 "read":"write",op_size);
41 //ppc32_dump_regs(cpu->gen);
42 #if MEMLOG_ENABLE
43 memlog_dump(cpu->gen);
44 #endif
45 #endif
46 if (cid == PPC32_MTS_DCACHE) {
47 cpu->dsisr = PPC32_DSISR_NOTRANS;
48
49 if (op_type == MTS_WRITE)
50 cpu->dsisr |= PPC32_DSISR_STORE;
51
52 cpu->dar = vaddr;
53 ppc32_trigger_exception(cpu,PPC32_EXC_DSI);
54 cpu_exec_loop_enter(cpu->gen);
55 } else {
56 ppc32_trigger_exception(cpu,PPC32_EXC_ISI);
57 cpu_exec_loop_enter(cpu->gen);
58 }
59 }
60 break;
61
62 case MTS_ACC_U:
63 if (op_type == MTS_READ)
64 *data = 0;
65
66 if (cpu->gen->undef_mem_handler != NULL) {
67 if (cpu->gen->undef_mem_handler(cpu->gen,(m_uint64_t)vaddr,
68 op_size,op_type,data))
69 return;
70 }
71
72 #if DEBUG_MTS_ACC_U
73 if (op_type == MTS_READ)
74 cpu_log(cpu->gen,
75 "MTS","read access to undefined address 0x%8.8x at "
76 "ia=0x%8.8x (size=%u)\n",vaddr,cpu->ia,op_size);
77 else
78 cpu_log(cpu->gen,
79 "MTS","write access to undefined address 0x%8.8x at "
80 "ia=0x%8.8x, value=0x%8.8llx (size=%u)\n",
81 vaddr,cpu->ia,*data,op_size);
82 #endif
83 break;
84 }
85 }
86
87 /* Initialize the MTS subsystem for the specified CPU */
88 int ppc32_mem_init(cpu_ppc_t *cpu)
89 {
90 size_t len;
91
92 /* Initialize the cache entries to 0 (empty) */
93 len = MTS32_HASH_SIZE * sizeof(mts32_entry_t);
94
95 if (!(cpu->mts_cache[PPC32_MTS_ICACHE] = malloc(len)))
96 return(-1);
97
98 if (!(cpu->mts_cache[PPC32_MTS_DCACHE] = malloc(len)))
99 return(-1);
100
101 memset(cpu->mts_cache[PPC32_MTS_ICACHE],0xFF,len);
102 memset(cpu->mts_cache[PPC32_MTS_DCACHE],0xFF,len);
103
104 cpu->mts_lookups = 0;
105 cpu->mts_misses = 0;
106 return(0);
107 }
108
109 /* Free memory used by MTS */
110 void ppc32_mem_shutdown(cpu_ppc_t *cpu)
111 {
112 if (cpu != NULL) {
113 /* Free the caches themselves */
114 free(cpu->mts_cache[PPC32_MTS_ICACHE]);
115 free(cpu->mts_cache[PPC32_MTS_DCACHE]);
116 cpu->mts_cache[PPC32_MTS_ICACHE] = NULL;
117 cpu->mts_cache[PPC32_MTS_DCACHE] = NULL;
118 }
119 }
120
121 /* Show MTS detailed information (debugging only!) */
122 void ppc32_mem_show_stats(cpu_gen_t *gen_cpu)
123 {
124 cpu_ppc_t *cpu = CPU_PPC32(gen_cpu);
125 #if DEBUG_MTS_MAP_VIRT
126 mts32_entry_t *entry;
127 u_int i,count;
128 #endif
129
130 printf("\nCPU%u: MTS statistics:\n",cpu->gen->id);
131
132 #if DEBUG_MTS_MAP_VIRT
133 printf("Instruction cache:\n");
134
135 /* Valid hash entries for Instruction Cache */
136 for(count=0,i=0;i<MTS32_HASH_SIZE;i++) {
137 entry = &cpu->mts_cache[PPC32_MTS_ICACHE][i];
138
139 if (!(entry->gvpa & MTS_INV_ENTRY_MASK)) {
140 printf(" %4u: vaddr=0x%8.8x, paddr=0x%8.8x, hpa=%p\n",
141 i,entry->gvpa,entry->gppa,(void *)entry->hpa);
142 count++;
143 }
144 }
145
146 printf(" %u/%u valid hash entries for icache.\n",count,MTS32_HASH_SIZE);
147
148
149 printf("Data cache:\n");
150
151 /* Valid hash entries for Instruction Cache */
152 for(count=0,i=0;i<MTS32_HASH_SIZE;i++) {
153 entry = &cpu->mts_cache[PPC32_MTS_DCACHE][i];
154
155 if (!(entry->gvpa & MTS_INV_ENTRY_MASK)) {
156 printf(" %4u: vaddr=0x%8.8x, paddr=0x%8.8x, hpa=%p\n",
157 i,entry->gvpa,entry->gppa,(void *)entry->hpa);
158 count++;
159 }
160 }
161
162 printf(" %u/%u valid hash entries for dcache.\n",count,MTS32_HASH_SIZE);
163 #endif
164
165 printf("\n Total lookups: %llu, misses: %llu, efficiency: %g%%\n",
166 cpu->mts_lookups, cpu->mts_misses,
167 100 - ((double)(cpu->mts_misses*100)/
168 (double)cpu->mts_lookups));
169 }
170
171 /* Invalidate the MTS caches (instruction and data) */
172 void ppc32_mem_invalidate_cache(cpu_ppc_t *cpu)
173 {
174 size_t len;
175
176 len = MTS32_HASH_SIZE * sizeof(mts32_entry_t);
177 memset(cpu->mts_cache[PPC32_MTS_ICACHE],0xFF,len);
178 memset(cpu->mts_cache[PPC32_MTS_DCACHE],0xFF,len);
179 }
180
181 /*
182 * MTS mapping.
183 *
184 * It is NOT inlined since it triggers a GCC bug on my config (x86, GCC 3.3.5)
185 */
186 static no_inline struct mts32_entry *
187 ppc32_mem_map(cpu_ppc_t *cpu,u_int op_type,mts_map_t *map,
188 mts32_entry_t *entry,mts32_entry_t *alt_entry)
189 {
190 ppc32_jit_tcb_t *tcb;
191 struct vdevice *dev;
192 m_uint32_t offset;
193 m_iptr_t host_ptr;
194 m_uint32_t exec_flag = 0;
195 int cow;
196
197 if (!(dev = dev_lookup(cpu->vm,map->paddr+map->offset,map->cached)))
198 return NULL;
199
200 if (cpu->tcb_phys_hash != NULL) {
201 tcb = ppc32_jit_find_by_phys_page(cpu,map->paddr >> VM_PAGE_SHIFT);
202
203 if ((tcb != NULL) && !(tcb->flags & PPC32_JIT_TCB_FLAG_SMC))
204 exec_flag = MTS_FLAG_EXEC;
205 }
206
207 if (dev->flags & VDEVICE_FLAG_SPARSE) {
208 host_ptr = dev_sparse_get_host_addr(cpu->vm,dev,map->paddr,
209 op_type,&cow);
210 entry->gvpa = map->vaddr;
211 entry->gppa = map->paddr;
212 entry->hpa = host_ptr;
213 entry->flags = (cow) ? MTS_FLAG_COW : 0;
214 entry->flags |= exec_flag;
215 return entry;
216 }
217
218 if (!dev->host_addr || (dev->flags & VDEVICE_FLAG_NO_MTS_MMAP)) {
219 offset = (map->paddr + map->offset) - dev->phys_addr;
220
221 /* device entries are never stored in virtual TLB */
222 alt_entry->gppa = dev->id;
223 alt_entry->hpa = offset;
224 alt_entry->flags = MTS_FLAG_DEV;
225 return alt_entry;
226 }
227
228 entry->gvpa = map->vaddr;
229 entry->gppa = map->paddr;
230 entry->hpa = dev->host_addr + (map->paddr - dev->phys_addr);
231 entry->flags = exec_flag;
232 return entry;
233 }
234
235 /* BAT lookup */
236 static forced_inline int ppc32_bat_lookup(cpu_ppc_t *cpu,m_uint32_t vaddr,
237 u_int cid,mts_map_t *map)
238 {
239 m_uint32_t bepi,mask,bl,pr,ubat;
240 int i;
241
242 pr = (cpu->msr & PPC32_MSR_PR) >> PPC32_MSR_PR_SHIFT;
243 pr = ((~pr << 1) | pr) & 0x03;
244
245 for(i=0;i<PPC32_BAT_NR;i++) {
246 ubat = cpu->bat[cid][i].reg[0];
247
248 if (!(ubat & pr))
249 continue;
250
251 //bl = (ubat & PPC32_UBAT_BL_MASK) >> PPC32_UBAT_BL_SHIFT;
252 bl = (ubat & PPC32_UBAT_XBL_MASK) >> PPC32_UBAT_XBL_SHIFT;
253
254 mask = ~bl << PPC32_BAT_ADDR_SHIFT;
255 bepi = ubat & PPC32_UBAT_BEPI_MASK;
256
257 if (bepi == (vaddr & mask)) {
258 map->vaddr = vaddr & PPC32_MIN_PAGE_MASK;
259 map->paddr = cpu->bat[cid][i].reg[1] & PPC32_LBAT_BRPN_MASK;
260 map->paddr += map->vaddr - bepi;
261 map->offset = vaddr & PPC32_MIN_PAGE_IMASK;
262 map->cached = FALSE;
263 return(TRUE);
264 }
265 }
266
267 return(FALSE);
268 }
269
270 /* Memory slow lookup */
271 static mts32_entry_t *ppc32_slow_lookup(cpu_ppc_t *cpu,m_uint32_t vaddr,
272 u_int cid,u_int op_code,u_int op_size,
273 u_int op_type,m_uint64_t *data,
274 mts32_entry_t *alt_entry)
275 {
276 m_uint32_t hash_bucket,segment,vsid;
277 m_uint32_t hash,tmp,pteg_offset,pte_key,key,pte2;
278 mts32_entry_t *entry;
279 m_uint8_t *pte_haddr;
280 m_uint64_t paddr;
281 mts_map_t map;
282 int i;
283
284 #if DEBUG_MTS_STATS
285 cpu->mts_misses++;
286 #endif
287
288 hash_bucket = MTS32_HASH(vaddr);
289 entry = &cpu->mts_cache[cid][hash_bucket];
290
291 /* No translation - cover the 4GB space */
292 if (((cid == PPC32_MTS_ICACHE) && !(cpu->msr & PPC32_MSR_IR)) ||
293 ((cid == PPC32_MTS_DCACHE) && !(cpu->msr & PPC32_MSR_DR)))
294 {
295 map.vaddr = vaddr & PPC32_MIN_PAGE_MASK;
296 map.paddr = vaddr & PPC32_MIN_PAGE_MASK;
297 map.offset = vaddr & PPC32_MIN_PAGE_IMASK;
298 map.cached = FALSE;
299
300 if (!(entry = ppc32_mem_map(cpu,op_type,&map,entry,alt_entry)))
301 goto err_undef;
302
303 return entry;
304 }
305
306 /* Walk through the BAT registers */
307 if (ppc32_bat_lookup(cpu,vaddr,cid,&map)) {
308 if (!(entry = ppc32_mem_map(cpu,op_type,&map,entry,alt_entry)))
309 goto err_undef;
310
311 return entry;
312 }
313
314 if (unlikely(!cpu->sdr1))
315 goto no_pte;
316
317 /* Get the virtual segment identifier */
318 segment = vaddr >> 28;
319 vsid = cpu->sr[segment] & PPC32_SD_VSID_MASK;
320
321 /* Compute the first hash value */
322 hash = (vaddr >> PPC32_MIN_PAGE_SHIFT) & 0xFFFF;
323 hash ^= vsid;
324 hash &= 0x7FFFFF;
325
326 tmp = (hash >> 10) & (cpu->sdr1 & PPC32_SDR1_HTMEXT_MASK);
327 pteg_offset = (hash & 0x3FF) << 6;
328 pteg_offset |= tmp << 16;
329 pte_haddr = cpu->sdr1_hptr + pteg_offset;
330
331 pte_key = 0x80000000 | (vsid << 7);
332 pte_key |= (vaddr >> 22) & 0x3F;
333
334 for(i=0;i<8;i++,pte_haddr+=PPC32_PTE_SIZE) {
335 key = vmtoh32(*(m_uint32_t *)pte_haddr);
336
337 if (key == pte_key)
338 goto pte_lookup_done;
339 }
340
341 /* Secondary hash value */
342 hash = (~hash) & 0x7FFFFF;
343
344 tmp = (hash >> 10) & (cpu->sdr1 & PPC32_SDR1_HTMEXT_MASK);
345 pteg_offset = (hash & 0x3FF) << 6;
346 pteg_offset |= tmp << 16;
347 pte_haddr = cpu->sdr1_hptr + pteg_offset;
348
349 pte_key = 0x80000040 | (vsid << 7);
350 pte_key |= (vaddr >> 22) & 0x3F;
351
352 for(i=0;i<8;i++,pte_haddr+=PPC32_PTE_SIZE) {
353 key = vmtoh32(*(m_uint32_t *)pte_haddr);
354
355 if (key == pte_key)
356 goto pte_lookup_done;
357 }
358
359 no_pte:
360 /* No matching PTE for this virtual address */
361 ppc32_access_special(cpu,vaddr,cid,MTS_ACC_T,op_code,op_type,op_size,data);
362 return NULL;
363
364 pte_lookup_done:
365 pte2 = vmtoh32(*(m_uint32_t *)(pte_haddr + sizeof(m_uint32_t)));
366 paddr = pte2 & PPC32_PTEL_RPN_MASK;
367 paddr |= (pte2 & PPC32_PTEL_XPN_MASK) << (33 - PPC32_PTEL_XPN_SHIFT);
368 paddr |= (pte2 & PPC32_PTEL_X_MASK) << (32 - PPC32_PTEL_X_SHIFT);
369
370 map.vaddr = vaddr & ~PPC32_MIN_PAGE_IMASK;
371 map.paddr = paddr;
372 map.offset = vaddr & PPC32_MIN_PAGE_IMASK;
373 map.cached = FALSE;
374
375 if ((entry = ppc32_mem_map(cpu,op_type,&map,entry,alt_entry)))
376 return entry;
377
378 err_undef:
379 ppc32_access_special(cpu,vaddr,cid,MTS_ACC_U,op_code,op_type,op_size,data);
380 return NULL;
381 }
382
383 /* Invalidate TCB related to a physical page marked as executable */
384 static void ppc32_mem_invalidate_tcb(cpu_ppc_t *cpu,mts32_entry_t *entry)
385 {
386 ppc32_jit_tcb_t *tcb,**tcbp,*tcb_next;
387 m_uint32_t hp,hv,phys_page,ia_phys_page;
388
389 if (cpu->tcb_phys_hash == NULL)
390 return;
391
392 phys_page = entry->gppa >> VM_PAGE_SHIFT;
393 hp = ppc32_jit_get_phys_hash(phys_page);
394
395 cpu->translate(cpu,cpu->ia,PPC32_MTS_ICACHE,&ia_phys_page);
396
397 if (phys_page != ia_phys_page) {
398 /* Clear all TCB matching the physical page being modified */
399 for(tcbp=&cpu->tcb_phys_hash[hp];*tcbp;)
400 if ((*tcbp)->phys_page == phys_page) {
401 hv = ppc32_jit_get_virt_hash((*tcbp)->start_ia);
402
403 if (cpu->tcb_virt_hash[hv] == *tcbp)
404 cpu->tcb_virt_hash[hv] = NULL;
405
406 tcb_next = (*tcbp)->phys_next;
407 ppc32_jit_tcb_free(cpu,(*tcbp),TRUE);
408 *tcbp = tcb_next;
409 } else {
410 tcbp = &(*tcbp)->phys_next;
411 }
412 entry->flags &= ~MTS_FLAG_EXEC;
413 } else {
414 /* Self-modifying page */
415 for(tcb=cpu->tcb_phys_hash[hp];tcb;tcb=tcb->phys_next)
416 if (tcb->phys_page == phys_page)
417 ppc32_jit_mark_smc(cpu,tcb);
418
419 entry->flags &= ~MTS_FLAG_EXEC;
420 cpu_exec_loop_enter(cpu->gen);
421 }
422 }
423
424 /* Memory access */
425 static inline void *ppc32_mem_access(cpu_ppc_t *cpu,m_uint32_t vaddr,
426 u_int cid,u_int op_code,u_int op_size,
427 u_int op_type,m_uint64_t *data)
428 {
429 mts32_entry_t *entry,alt_entry;
430 m_uint32_t hash_bucket;
431 m_iptr_t haddr;
432 u_int dev_id;
433 int cow;
434
435 #if MEMLOG_ENABLE
436 /* Record the memory access */
437 memlog_rec_access(cpu->gen,vaddr,*data,op_size,op_type);
438 #endif
439
440 hash_bucket = MTS32_HASH(vaddr);
441 entry = &cpu->mts_cache[cid][hash_bucket];
442
443 #if DEBUG_MTS_STATS
444 cpu->mts_lookups++;
445 #endif
446
447 /* Copy-On-Write for sparse device ? */
448 cow = (op_type == MTS_WRITE) && (entry->flags & MTS_FLAG_COW);
449
450 /* Slow lookup if nothing found in cache */
451 if (unlikely(((vaddr & PPC32_MIN_PAGE_MASK) != entry->gvpa) || cow)) {
452 entry = cpu->mts_slow_lookup(cpu,vaddr,cid,op_code,op_size,op_type,
453 data,&alt_entry);
454 if (!entry)
455 return NULL;
456
457 if (entry->flags & MTS_FLAG_DEV) {
458 dev_id = entry->gppa;
459 haddr = entry->hpa;
460 return(dev_access_fast(cpu->gen,dev_id,haddr,op_size,op_type,data));
461 }
462 }
463
464 /* Invalidate JIT code for written pages */
465 if ((op_type == MTS_WRITE) && (entry->flags & MTS_FLAG_EXEC))
466 ppc32_mem_invalidate_tcb(cpu,entry);
467
468 /* Raw memory access */
469 haddr = entry->hpa + (vaddr & PPC32_MIN_PAGE_IMASK);
470 #if MEMLOG_ENABLE
471 memlog_update_read(cpu->gen,haddr);
472 #endif
473 return((void *)haddr);
474 }
475
476 /* Memory data access */
477 #define PPC32_MEM_DACCESS(cpu,vaddr,op_code,op_size,op_type,data) \
478 ppc32_mem_access((cpu),(vaddr),PPC32_MTS_DCACHE,(op_code),(op_size),\
479 (op_type),(data))
480
481 /* Virtual address to physical page translation */
482 static fastcall int ppc32_translate(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int cid,
483 m_uint32_t *phys_page)
484 {
485 mts32_entry_t *entry,alt_entry;
486 m_uint32_t hash_bucket;
487 m_uint64_t data = 0;
488
489 hash_bucket = MTS32_HASH(vaddr);
490 entry = &cpu->mts_cache[cid][hash_bucket];
491
492 /* Slow lookup if nothing found in cache */
493 if (unlikely((vaddr & PPC32_MIN_PAGE_MASK) != entry->gvpa)) {
494 entry = cpu->mts_slow_lookup(cpu,vaddr,cid,PPC_MEMOP_LOOKUP,4,MTS_READ,
495 &data,&alt_entry);
496 if (!entry)
497 return(-1);
498 }
499
500 *phys_page = entry->gppa >> PPC32_MIN_PAGE_SHIFT;
501 return(0);
502 }
503
504 /* Virtual address lookup */
505 static void *ppc32_mem_lookup(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int cid)
506 {
507 m_uint64_t data;
508 return(ppc32_mem_access(cpu,vaddr,cid,PPC_MEMOP_LOOKUP,4,MTS_READ,&data));
509 }
510
511 /* Instruction fetch */
512 static void *ppc32_mem_ifetch(cpu_ppc_t *cpu,m_uint32_t vaddr)
513 {
514 m_uint64_t data;
515 return(ppc32_mem_access(cpu,vaddr,PPC32_MTS_ICACHE,
516 PPC_MEMOP_IFETCH,4,MTS_READ,&data));
517 }
518
519 /* Set a BAT register */
520 int ppc32_set_bat(cpu_ppc_t *cpu,struct ppc32_bat_prog *bp)
521 {
522 struct ppc32_bat_reg *bat;
523
524 if ((bp->type != PPC32_IBAT_IDX) && (bp->type != PPC32_DBAT_IDX))
525 return(-1);
526
527 if (bp->index >= PPC32_BAT_NR)
528 return(-1);
529
530 bat = &cpu->bat[bp->type][bp->index];
531 bat->reg[0] = bp->hi;
532 bat->reg[1] = bp->lo;
533 return(0);
534 }
535
536 /* Load BAT registers from a BAT array */
537 void ppc32_load_bat_array(cpu_ppc_t *cpu,struct ppc32_bat_prog *bp)
538 {
539 while(bp->index != -1) {
540 ppc32_set_bat(cpu,bp);
541 bp++;
542 }
543 }
544
545 /* Get the host address for SDR1 */
546 int ppc32_set_sdr1(cpu_ppc_t *cpu,m_uint32_t sdr1)
547 {
548 struct vdevice *dev;
549 m_uint64_t pt_addr;
550
551 cpu->sdr1 = sdr1;
552 pt_addr = sdr1 & PPC32_SDR1_HTABORG_MASK;
553 pt_addr |= ((m_uint64_t)(sdr1 & PPC32_SDR1_HTABEXT_MASK) << 20);
554
555 if (!(dev = dev_lookup(cpu->vm,pt_addr,TRUE))) {
556 vm_error(cpu->vm,
557 "ppc32_set_sdr1: unable to find haddr for SDR1=0x%8.8x\n",
558 sdr1);
559 return(-1);
560 }
561
562 cpu->sdr1_hptr = (char *)dev->host_addr + (pt_addr - dev->phys_addr);
563
564 /* Flush the virtual TLB */
565 ppc32_mem_invalidate_cache(cpu);
566 return(0);
567 }
568
569 /* Initialize the page table */
570 int ppc32_init_page_table(cpu_ppc_t *cpu)
571 {
572 m_uint32_t pt_size;
573
574 if (!cpu->sdr1_hptr)
575 return(-1);
576
577 pt_size = (1 + (cpu->sdr1 & PPC32_SDR1_HTMEXT_MASK)) << 16;
578 memset(cpu->sdr1_hptr,0,pt_size);
579 return(0);
580 }
581
582 /* Map a page */
583 int ppc32_map_page(cpu_ppc_t *cpu,u_int vsid,m_uint32_t vaddr,m_uint64_t paddr,
584 u_int wimg,u_int pp)
585 {
586 m_uint32_t hash,tmp,pteg_offset,key;
587 m_uint8_t *pte_haddr;
588 int i;
589
590 /* Compute the first hash value */
591 hash = (vaddr >> PPC32_MIN_PAGE_SHIFT) & 0xFFFF;
592 hash ^= vsid;
593 hash &= 0x7FFFFF;
594
595 tmp = (hash >> 10) & (cpu->sdr1 & PPC32_SDR1_HTMEXT_MASK);
596 pteg_offset = (hash & 0x3FF) << 6;
597 pteg_offset |= tmp << 16;
598 pte_haddr = cpu->sdr1_hptr + pteg_offset;
599
600 for(i=0;i<8;i++,pte_haddr+=PPC32_PTE_SIZE) {
601 key = vmtoh32(*(m_uint32_t *)pte_haddr);
602
603 if (!(key & PPC32_PTEU_V)) {
604 hash = 0;
605 goto free_pte_found;
606 }
607 }
608
609 /* Secondary hash value */
610 hash = (~hash) & 0x7FFFFF;
611
612 tmp = (hash >> 10) & (cpu->sdr1 & PPC32_SDR1_HTMEXT_MASK);
613 pteg_offset = (hash & 0x3FF) << 6;
614 pteg_offset |= tmp << 16;
615 pte_haddr = cpu->sdr1_hptr + pteg_offset;
616
617 for(i=0;i<8;i++,pte_haddr+=PPC32_PTE_SIZE) {
618 key = vmtoh32(*(m_uint32_t *)pte_haddr);
619
620 if (!(key & PPC32_PTEU_V)) {
621 hash = PPC32_PTEU_H;
622 goto free_pte_found;
623 }
624 }
625
626 /* No free PTE found */
627 return(-1);
628
629 free_pte_found:
630 tmp = PPC32_PTEU_V | (vsid << PPC32_PTEU_VSID_SHIFT) | hash;
631 tmp |= (vaddr >> 22) & 0x3F;
632 *(m_uint32_t *)pte_haddr = htovm32(tmp);
633
634 tmp = vaddr & PPC32_PTEL_RPN_MASK;
635 tmp |= (vaddr >> (32 - PPC32_PTEL_X_SHIFT)) & PPC32_PTEL_X_MASK;
636 tmp |= (vaddr >> (33 - PPC32_PTEL_XPN_SHIFT)) & PPC32_PTEL_XPN_MASK;
637
638 tmp |= (wimg << PPC32_PTEL_WIMG_SHIFT) + pp;
639 *(m_uint32_t *)(pte_haddr+sizeof(m_uint32_t)) = htovm32(tmp);
640 return(0);
641 }
642
643 /* Map a memory zone */
644 int ppc32_map_zone(cpu_ppc_t *cpu,u_int vsid,m_uint32_t vaddr,m_uint64_t paddr,
645 m_uint32_t size,u_int wimg,u_int pp)
646 {
647 while(size > 0) {
648 if (ppc32_map_page(cpu,vsid,vaddr,paddr,wimg,pp) == -1)
649 return(-1);
650
651 size -= PPC32_MIN_PAGE_SIZE;
652 vaddr += PPC32_MIN_PAGE_SIZE;
653 paddr += PPC32_MIN_PAGE_SIZE;
654 }
655
656 return(0);
657 }
658
659 /* PowerPC 405 TLB masks */
660 static m_uint32_t ppc405_tlb_masks[8] = {
661 0xFFFFFC00, 0xFFFFF000, 0xFFFFC000, 0xFFFF0000,
662 0xFFFC0000, 0xFFF00000, 0xFFC00000, 0xFF000000,
663 };
664
665 /* PowerPC 405 slow lookup */
666 static mts32_entry_t *ppc405_slow_lookup(cpu_ppc_t *cpu,m_uint32_t vaddr,
667 u_int cid,u_int op_code,u_int op_size,
668 u_int op_type,m_uint64_t *data,
669 mts32_entry_t *alt_entry)
670 {
671 struct ppc405_tlb_entry *tlb_entry;
672 m_uint32_t hash_bucket,mask;
673 m_uint32_t page_size;
674 mts32_entry_t *entry;
675 mts_map_t map;
676 int i;
677
678 #if DEBUG_MTS_STATS
679 cpu->mts_misses++;
680 #endif
681
682 hash_bucket = MTS32_HASH(vaddr);
683 entry = &cpu->mts_cache[cid][hash_bucket];
684
685 /* No translation - cover the 4GB space */
686 if (((cid == PPC32_MTS_ICACHE) && !(cpu->msr & PPC32_MSR_IR)) ||
687 ((cid == PPC32_MTS_DCACHE) && !(cpu->msr & PPC32_MSR_DR)))
688 {
689 map.vaddr = vaddr & PPC32_MIN_PAGE_MASK;
690 map.paddr = vaddr & PPC32_MIN_PAGE_MASK;
691 map.cached = FALSE;
692
693 if (!(entry = ppc32_mem_map(cpu,op_type,&map,entry,alt_entry)))
694 goto err_undef;
695
696 return entry;
697 }
698
699 /* Walk through the unified TLB */
700 for(i=0;i<PPC405_TLB_ENTRIES;i++)
701 {
702 tlb_entry = &cpu->ppc405_tlb[i];
703
704 /* We want a valid entry with TID = PID */
705 if (!(tlb_entry->tlb_hi & PPC405_TLBHI_V) ||
706 (tlb_entry->tid != cpu->ppc405_pid))
707 continue;
708
709 /* Get the address mask corresponding to this entry */
710 page_size = tlb_entry->tlb_hi & PPC405_TLBHI_SIZE_MASK;
711 page_size >>= PPC405_TLBHI_SIZE_SHIFT;
712 mask = ppc405_tlb_masks[page_size];
713
714 /* Matching entry ? */
715 if ((vaddr & mask) == (tlb_entry->tlb_hi & mask)) {
716 map.vaddr = vaddr & mask;
717 map.paddr = tlb_entry->tlb_lo & mask;
718 map.cached = FALSE;
719
720 if (!(entry = ppc32_mem_map(cpu,op_type,&map,entry,alt_entry)))
721 goto err_undef;
722
723 return entry;
724 }
725 }
726
727 /* No matching TLB entry for this virtual address */
728 ppc32_access_special(cpu,vaddr,cid,MTS_ACC_T,op_code,op_type,op_size,data);
729 return NULL;
730
731 err_undef:
732 ppc32_access_special(cpu,vaddr,cid,MTS_ACC_U,op_code,op_type,op_size,data);
733 return NULL;
734 }
735
736 /* Dump a PowerPC 405 TLB entry */
737 static void ppc405_dump_tlb_entry(cpu_ppc_t *cpu,u_int index)
738 {
739 struct ppc405_tlb_entry *entry;
740
741 entry = &cpu->ppc405_tlb[index];
742
743 printf(" %2d: hi=0x%8.8x lo=0x%8.8x tid=0x%2.2x\n",
744 index,entry->tlb_hi,entry->tlb_lo,entry->tid);
745 }
746
747 /* Dump the PowerPC 405 TLB */
748 static void ppc405_dump_tlb(cpu_gen_t *cpu)
749 {
750 cpu_ppc_t *pcpu = CPU_PPC32(cpu);
751 u_int i;
752
753 for(i=0;i<PPC405_TLB_ENTRIES;i++)
754 ppc405_dump_tlb_entry(pcpu,i);
755 }
756
757 /* === PPC Memory Operations ============================================= */
758
759 /* LBZ: Load Byte Zero */
760 fastcall void ppc32_lbz(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
761 {
762 m_uint64_t data;
763 void *haddr;
764
765 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LBZ,1,MTS_READ,&data);
766 if (likely(haddr != NULL)) data = *(m_uint8_t *)haddr;
767 cpu->gpr[reg] = data & 0xFF;
768 }
769
770 /* LHZ: Load Half-Word Zero */
771 fastcall void ppc32_lhz(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
772 {
773 m_uint64_t data;
774 void *haddr;
775
776 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LHZ,2,MTS_READ,&data);
777 if (likely(haddr != NULL)) data = vmtoh16(*(m_uint16_t *)haddr);
778 cpu->gpr[reg] = data & 0xFFFF;
779 }
780
781 /* LWZ: Load Word Zero */
782 fastcall void ppc32_lwz(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
783 {
784 m_uint64_t data;
785 void *haddr;
786
787 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LWZ,4,MTS_READ,&data);
788 if (likely(haddr != NULL)) data = vmtoh32(*(m_uint32_t *)haddr);
789 cpu->gpr[reg] = data;
790 }
791
792 /* LWBR: Load Word Byte Reverse */
793 fastcall void ppc32_lwbr(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
794 {
795 m_uint64_t data;
796 void *haddr;
797
798 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LWBR,4,MTS_READ,&data);
799 if (likely(haddr != NULL)) data = vmtoh32(*(m_uint32_t *)haddr);
800 cpu->gpr[reg] = swap32(data);
801 }
802
803 /* LHA: Load Half-Word Algebraic */
804 fastcall void ppc32_lha(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
805 {
806 m_uint64_t data;
807 void *haddr;
808
809 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LHZ,2,MTS_READ,&data);
810 if (likely(haddr != NULL)) data = vmtoh16(*(m_uint16_t *)haddr);
811 cpu->gpr[reg] = sign_extend_32(data,16);
812 }
813
814 /* STB: Store Byte */
815 fastcall void ppc32_stb(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
816 {
817 m_uint64_t data;
818 void *haddr;
819
820 data = cpu->gpr[reg] & 0xff;
821 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_STB,1,MTS_WRITE,&data);
822 if (likely(haddr != NULL)) *(m_uint8_t *)haddr = data;
823 }
824
825 /* STH: Store Half-Word */
826 fastcall void ppc32_sth(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
827 {
828 m_uint64_t data;
829 void *haddr;
830
831 data = cpu->gpr[reg] & 0xffff;
832 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_STH,2,MTS_WRITE,&data);
833 if (likely(haddr != NULL)) *(m_uint16_t *)haddr = htovm16(data);
834 }
835
836 /* STW: Store Word */
837 fastcall void ppc32_stw(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
838 {
839 m_uint64_t data;
840 void *haddr;
841
842 data = cpu->gpr[reg];
843 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_STW,4,MTS_WRITE,&data);
844 if (likely(haddr != NULL)) *(m_uint32_t *)haddr = htovm32(data);
845 }
846
847 /* STWBR: Store Word Byte Reversed */
848 fastcall void ppc32_stwbr(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
849 {
850 m_uint64_t data;
851 void *haddr;
852
853 data = swap32(cpu->gpr[reg]);
854 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_STWBR,4,MTS_WRITE,&data);
855 if (likely(haddr != NULL)) *(m_uint32_t *)haddr = htovm32(data);
856 }
857
858 /* LSW: Load String Word */
859 fastcall void ppc32_lsw(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
860 {
861 m_uint64_t data;
862 void *haddr;
863
864 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LSW,1,MTS_READ,&data);
865 if (likely(haddr != NULL)) data = *(m_uint8_t *)haddr;
866 cpu->gpr[reg] |= (data & 0xFF) << (24 - cpu->sw_pos);
867 }
868
869 /* STW: Store String Word */
870 fastcall void ppc32_stsw(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
871 {
872 m_uint64_t data;
873 void *haddr;
874
875 data = (cpu->gpr[reg] >> (24 - cpu->sw_pos)) & 0xFF;
876 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_STSW,1,MTS_WRITE,&data);
877 if (likely(haddr != NULL)) *(m_uint8_t *)haddr = data;
878 }
879
880 /* LFD: Load Floating-Point Double */
881 fastcall void ppc32_lfd(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
882 {
883 m_uint64_t data;
884 void *haddr;
885
886 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_LWZ,8,MTS_READ,&data);
887 if (likely(haddr != NULL)) data = vmtoh64(*(m_uint64_t *)haddr);
888 cpu->fpu.reg[reg] = data;
889 }
890
891 /* STFD: Store Floating-Point Double */
892 fastcall void ppc32_stfd(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int reg)
893 {
894 m_uint64_t data;
895 void *haddr;
896
897 data = cpu->fpu.reg[reg];
898 haddr = PPC32_MEM_DACCESS(cpu,vaddr,PPC_MEMOP_STW,8,MTS_WRITE,&data);
899 if (likely(haddr != NULL)) *(m_uint64_t *)haddr = htovm64(data);
900 }
901
902 /* ICBI: Instruction Cache Block Invalidate */
903 fastcall void ppc32_icbi(cpu_ppc_t *cpu,m_uint32_t vaddr,u_int op)
904 {
905 ppc32_jit_tcb_t *tcb;
906 m_uint32_t phys_page;
907
908 #if DEBUG_ICBI
909 cpu_log(cpu->gen,"MTS","ICBI: ia=0x%8.8x, vaddr=0x%8.8x\n",cpu->ia,vaddr);
910 #endif
911
912 if (!cpu->translate(cpu,vaddr,PPC32_MTS_ICACHE,&phys_page)) {
913 if (cpu->tcb_phys_hash) {
914 tcb = ppc32_jit_find_by_phys_page(cpu,phys_page);
915
916 if (tcb && (tcb->start_ia == (vaddr & PPC32_MIN_PAGE_MASK))) {
917 #if DEBUG_ICBI
918 cpu_log(cpu->gen,"MTS",
919 "ICBI: removing compiled page at 0x%8.8x, pc=0x%8.8x\n",
920 tcb->start_ia,cpu->ia);
921 #endif
922 ppc32_jit_tcb_free(cpu,tcb,TRUE);
923 cpu->tcb_virt_hash[ppc32_jit_get_virt_hash(vaddr)] = NULL;
924 }
925 else
926 {
927 #if DEBUG_ICBI
928 cpu_log(cpu->gen,"MTS",
929 "ICBI: trying to remove page 0x%llx with pc=0x%llx\n",
930 block->start_ia,cpu->ia);
931 #endif
932 }
933 }
934 }
935 }
936
937 /* ======================================================================== */
938
939 /* Get a BAT register pointer given a SPR index */
940 static inline m_uint32_t *ppc32_get_bat_spr_ptr(cpu_ppc_t *cpu,u_int spr)
941 {
942 m_uint32_t spr_cat,cid,index;
943
944 spr_cat = spr >> 5;
945 if ((spr_cat != 0x10) && (spr_cat != 0x11))
946 return NULL;
947
948 cid = (spr >> 3) & 0x1;
949 index = (spr >> 1) & 0x3;
950
951 if (spr & 0x20)
952 index += 4;
953
954 //printf("GET_BAT_SPR: SPR=%u => cid=%u, index=%u\n",spr,cid,index);
955
956 return(&cpu->bat[cid][index].reg[spr & 0x1]);
957 }
958
959 /* Get a BAT SPR */
960 m_uint32_t ppc32_get_bat_spr(cpu_ppc_t *cpu,u_int spr)
961 {
962 m_uint32_t *p;
963
964 if (!(p = ppc32_get_bat_spr_ptr(cpu,spr)))
965 return(0);
966
967 return(*p);
968 }
969
970 /* Set a BAT SPR */
971 void ppc32_set_bat_spr(cpu_ppc_t *cpu,u_int spr,m_uint32_t val)
972 {
973 m_uint32_t *p;
974
975 if ((p = ppc32_get_bat_spr_ptr(cpu,spr))) {
976 *p = val;
977 ppc32_mem_invalidate_cache(cpu);
978 }
979 }
980
981 /* ======================================================================== */
982
983 /* Rebuild MTS data structures */
984 static void ppc32_mem_rebuild_mts(cpu_gen_t *gen_cpu)
985 {
986 ppc32_mem_invalidate_cache(CPU_PPC32(gen_cpu));
987 }
988
989 /* Initialize memory access vectors */
990 void ppc32_init_memop_vectors(cpu_ppc_t *cpu)
991 {
992 /* MTS slow lookup */
993 cpu->mts_slow_lookup = ppc32_slow_lookup;
994
995 /* MTS rebuild */
996 cpu->gen->mts_rebuild = ppc32_mem_rebuild_mts;
997
998 /* MTS statistics */
999 cpu->gen->mts_show_stats = ppc32_mem_show_stats;
1000
1001 /* Memory lookup and instruction fetch operations */
1002 cpu->mem_op_lookup = ppc32_mem_lookup;
1003 cpu->mem_op_ifetch = ppc32_mem_ifetch;
1004
1005 /* Translation operation */
1006 cpu->translate = ppc32_translate;
1007
1008 /* Load Operations */
1009 cpu->mem_op_fn[PPC_MEMOP_LBZ] = ppc32_lbz;
1010 cpu->mem_op_fn[PPC_MEMOP_LHZ] = ppc32_lhz;
1011 cpu->mem_op_fn[PPC_MEMOP_LWZ] = ppc32_lwz;
1012
1013 /* Load Operation with sign-extension */
1014 cpu->mem_op_fn[PPC_MEMOP_LHA] = ppc32_lha;
1015
1016 /* Store Operations */
1017 cpu->mem_op_fn[PPC_MEMOP_STB] = ppc32_stb;
1018 cpu->mem_op_fn[PPC_MEMOP_STH] = ppc32_sth;
1019 cpu->mem_op_fn[PPC_MEMOP_STW] = ppc32_stw;
1020
1021 /* Byte-Reversed operations */
1022 cpu->mem_op_fn[PPC_MEMOP_LWBR] = ppc32_lwbr;
1023 cpu->mem_op_fn[PPC_MEMOP_STWBR] = ppc32_stwbr;
1024
1025 /* String operations */
1026 cpu->mem_op_fn[PPC_MEMOP_LSW] = ppc32_lsw;
1027 cpu->mem_op_fn[PPC_MEMOP_STSW] = ppc32_stsw;
1028
1029 /* FPU operations */
1030 cpu->mem_op_fn[PPC_MEMOP_LFD] = ppc32_lfd;
1031 cpu->mem_op_fn[PPC_MEMOP_STFD] = ppc32_stfd;
1032
1033 /* ICBI - Instruction Cache Block Invalidate */
1034 cpu->mem_op_fn[PPC_MEMOP_ICBI] = ppc32_icbi;
1035 }
1036
1037 /* Restart the memory subsystem */
1038 int ppc32_mem_restart(cpu_ppc_t *cpu)
1039 {
1040 m_uint32_t family;
1041
1042 ppc32_mem_shutdown(cpu);
1043 ppc32_mem_init(cpu);
1044 ppc32_init_memop_vectors(cpu);
1045
1046 /* Override the MTS lookup vector depending on the cpu type */
1047 family = cpu->pvr & 0xFFFF0000;
1048
1049 if (family == PPC32_PVR_405) {
1050 cpu->mts_slow_lookup = ppc405_slow_lookup;
1051 cpu->gen->mmu_dump = ppc405_dump_tlb;
1052 cpu->gen->mmu_raw_dump = ppc405_dump_tlb;
1053 }
1054
1055 return(0);
1056 }