Debian Codegraph @

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