1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "cpu.h"
22#include "tcg.h"
23#include "exec/helper-proto.h"
24#include "exec/exec-all.h"
25#include "exec/cpu_ldst.h"
26#include "asi.h"
27
28
29
30
31
32
33
34
35#ifdef DEBUG_MMU
36#define DPRINTF_MMU(fmt, ...) \
37 do { printf("MMU: " fmt , ## __VA_ARGS__); } while (0)
38#else
39#define DPRINTF_MMU(fmt, ...) do {} while (0)
40#endif
41
42#ifdef DEBUG_MXCC
43#define DPRINTF_MXCC(fmt, ...) \
44 do { printf("MXCC: " fmt , ## __VA_ARGS__); } while (0)
45#else
46#define DPRINTF_MXCC(fmt, ...) do {} while (0)
47#endif
48
49#ifdef DEBUG_ASI
50#define DPRINTF_ASI(fmt, ...) \
51 do { printf("ASI: " fmt , ## __VA_ARGS__); } while (0)
52#endif
53
54#ifdef DEBUG_CACHE_CONTROL
55#define DPRINTF_CACHE_CONTROL(fmt, ...) \
56 do { printf("CACHE_CONTROL: " fmt , ## __VA_ARGS__); } while (0)
57#else
58#define DPRINTF_CACHE_CONTROL(fmt, ...) do {} while (0)
59#endif
60
61#ifdef TARGET_SPARC64
62#ifndef TARGET_ABI32
63#define AM_CHECK(env1) ((env1)->pstate & PS_AM)
64#else
65#define AM_CHECK(env1) (1)
66#endif
67#endif
68
69#define QT0 (env->qt0)
70#define QT1 (env->qt1)
71
72#if defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
73
74
75
76static uint64_t ultrasparc_tsb_pointer(CPUSPARCState *env,
77 const SparcV9MMU *mmu, const int idx)
78{
79 uint64_t tsb_register;
80 int page_size;
81 if (cpu_has_hypervisor(env)) {
82 int tsb_index = 0;
83 int ctx = mmu->tag_access & 0x1fffULL;
84 uint64_t ctx_register = mmu->sun4v_ctx_config[ctx ? 1 : 0];
85 tsb_index = idx;
86 tsb_index |= ctx ? 2 : 0;
87 page_size = idx ? ctx_register >> 8 : ctx_register;
88 page_size &= 7;
89 tsb_register = mmu->sun4v_tsb_pointers[tsb_index];
90 } else {
91 page_size = idx;
92 tsb_register = mmu->tsb;
93 }
94 int tsb_split = (tsb_register & 0x1000ULL) ? 1 : 0;
95 int tsb_size = tsb_register & 0xf;
96
97 uint64_t tsb_base_mask = (~0x1fffULL) << tsb_size;
98
99
100
101 uint64_t va = mmu->tag_access >> (3 * page_size + 9);
102
103
104 if (tsb_split) {
105 if (idx == 0) {
106 va &= ~(1ULL << (13 + tsb_size));
107 } else {
108 va |= (1ULL << (13 + tsb_size));
109 }
110 tsb_base_mask <<= 1;
111 }
112
113 return ((tsb_register & tsb_base_mask) | (va & ~tsb_base_mask)) & ~0xfULL;
114}
115
116
117
118static uint64_t ultrasparc_tag_target(uint64_t tag_access_register)
119{
120 return ((tag_access_register & 0x1fff) << 48) | (tag_access_register >> 22);
121}
122
123static void replace_tlb_entry(SparcTLBEntry *tlb,
124 uint64_t tlb_tag, uint64_t tlb_tte,
125 CPUSPARCState *env1)
126{
127 target_ulong mask, size, va, offset;
128
129
130 if (TTE_IS_VALID(tlb->tte)) {
131 CPUState *cs = CPU(sparc_env_get_cpu(env1));
132
133 size = 8192ULL << 3 * TTE_PGSIZE(tlb->tte);
134 mask = 1ULL + ~size;
135
136 va = tlb->tag & mask;
137
138 for (offset = 0; offset < size; offset += TARGET_PAGE_SIZE) {
139 tlb_flush_page(cs, va + offset);
140 }
141 }
142
143 tlb->tag = tlb_tag;
144 tlb->tte = tlb_tte;
145}
146
147static void demap_tlb(SparcTLBEntry *tlb, target_ulong demap_addr,
148 const char *strmmu, CPUSPARCState *env1)
149{
150 unsigned int i;
151 target_ulong mask;
152 uint64_t context;
153
154 int is_demap_context = (demap_addr >> 6) & 1;
155
156
157 switch ((demap_addr >> 4) & 3) {
158 case 0:
159 context = env1->dmmu.mmu_primary_context;
160 break;
161 case 1:
162 context = env1->dmmu.mmu_secondary_context;
163 break;
164 case 2:
165 context = 0;
166 break;
167 case 3:
168 default:
169 return;
170 }
171
172 for (i = 0; i < 64; i++) {
173 if (TTE_IS_VALID(tlb[i].tte)) {
174
175 if (is_demap_context) {
176
177 if (TTE_IS_GLOBAL(tlb[i].tte) ||
178 !tlb_compare_context(&tlb[i], context)) {
179 continue;
180 }
181 } else {
182
183
184 mask = 0xffffffffffffe000ULL;
185 mask <<= 3 * ((tlb[i].tte >> 61) & 3);
186
187 if (!compare_masked(demap_addr, tlb[i].tag, mask)) {
188 continue;
189 }
190
191
192 if (!TTE_IS_GLOBAL(tlb[i].tte) &&
193 !tlb_compare_context(&tlb[i], context)) {
194 continue;
195 }
196 }
197
198 replace_tlb_entry(&tlb[i], 0, 0, env1);
199#ifdef DEBUG_MMU
200 DPRINTF_MMU("%s demap invalidated entry [%02u]\n", strmmu, i);
201 dump_mmu(stdout, fprintf, env1);
202#endif
203 }
204 }
205}
206
207static uint64_t sun4v_tte_to_sun4u(CPUSPARCState *env, uint64_t tag,
208 uint64_t sun4v_tte)
209{
210 uint64_t sun4u_tte;
211 if (!(cpu_has_hypervisor(env) && (tag & TLB_UST1_IS_SUN4V_BIT))) {
212
213 return sun4v_tte;
214 }
215 sun4u_tte = TTE_PA(sun4v_tte) | (sun4v_tte & TTE_VALID_BIT);
216 sun4u_tte |= (sun4v_tte & 3ULL) << 61;
217 sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_NFO_BIT_UA2005, TTE_NFO_BIT);
218 sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_USED_BIT_UA2005, TTE_USED_BIT);
219 sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_W_OK_BIT_UA2005, TTE_W_OK_BIT);
220 sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_SIDEEFFECT_BIT_UA2005,
221 TTE_SIDEEFFECT_BIT);
222 sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_PRIV_BIT_UA2005, TTE_PRIV_BIT);
223 sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_LOCKED_BIT_UA2005, TTE_LOCKED_BIT);
224 return sun4u_tte;
225}
226
227static void replace_tlb_1bit_lru(SparcTLBEntry *tlb,
228 uint64_t tlb_tag, uint64_t tlb_tte,
229 const char *strmmu, CPUSPARCState *env1,
230 uint64_t addr)
231{
232 unsigned int i, replace_used;
233
234 tlb_tte = sun4v_tte_to_sun4u(env1, addr, tlb_tte);
235 if (cpu_has_hypervisor(env1)) {
236 uint64_t new_vaddr = tlb_tag & ~0x1fffULL;
237 uint64_t new_size = 8192ULL << 3 * TTE_PGSIZE(tlb_tte);
238 uint32_t new_ctx = tlb_tag & 0x1fffU;
239 for (i = 0; i < 64; i++) {
240 uint32_t ctx = tlb[i].tag & 0x1fffU;
241
242 if (new_ctx == ctx) {
243 uint64_t vaddr = tlb[i].tag & ~0x1fffULL;
244 uint64_t size = 8192ULL << 3 * TTE_PGSIZE(tlb[i].tte);
245 if (new_vaddr == vaddr
246 || (new_vaddr < vaddr + size
247 && vaddr < new_vaddr + new_size)) {
248 DPRINTF_MMU("auto demap entry [%d] %lx->%lx\n", i, vaddr,
249 new_vaddr);
250 replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
251 return;
252 }
253 }
254
255 }
256 }
257
258 for (i = 0; i < 64; i++) {
259 if (!TTE_IS_VALID(tlb[i].tte)) {
260 replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
261#ifdef DEBUG_MMU
262 DPRINTF_MMU("%s lru replaced invalid entry [%i]\n", strmmu, i);
263 dump_mmu(stdout, fprintf, env1);
264#endif
265 return;
266 }
267 }
268
269
270
271 for (replace_used = 0; replace_used < 2; ++replace_used) {
272
273
274
275 for (i = 0; i < 64; i++) {
276 if (!TTE_IS_LOCKED(tlb[i].tte) && !TTE_IS_USED(tlb[i].tte)) {
277
278 replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
279#ifdef DEBUG_MMU
280 DPRINTF_MMU("%s lru replaced unlocked %s entry [%i]\n",
281 strmmu, (replace_used ? "used" : "unused"), i);
282 dump_mmu(stdout, fprintf, env1);
283#endif
284 return;
285 }
286 }
287
288
289
290 for (i = 0; i < 64; i++) {
291 TTE_SET_UNUSED(tlb[i].tte);
292 }
293 }
294
295#ifdef DEBUG_MMU
296 DPRINTF_MMU("%s lru replacement: no free entries available, "
297 "replacing the last one\n", strmmu);
298#endif
299
300 replace_tlb_entry(&tlb[63], tlb_tag, tlb_tte, env1);
301}
302
303#endif
304
305#ifdef TARGET_SPARC64
306
307
308
309static inline int is_translating_asi(int asi)
310{
311
312
313
314 switch (asi) {
315 case 0x04 ... 0x11:
316 case 0x16 ... 0x19:
317 case 0x1E ... 0x1F:
318 case 0x24 ... 0x2C:
319 case 0x70 ... 0x73:
320 case 0x78 ... 0x79:
321 case 0x80 ... 0xFF:
322 return 1;
323
324 default:
325 return 0;
326 }
327}
328
329static inline target_ulong address_mask(CPUSPARCState *env1, target_ulong addr)
330{
331 if (AM_CHECK(env1)) {
332 addr &= 0xffffffffULL;
333 }
334 return addr;
335}
336
337static inline target_ulong asi_address_mask(CPUSPARCState *env,
338 int asi, target_ulong addr)
339{
340 if (is_translating_asi(asi)) {
341 addr = address_mask(env, addr);
342 }
343 return addr;
344}
345
346#ifndef CONFIG_USER_ONLY
347static inline void do_check_asi(CPUSPARCState *env, int asi, uintptr_t ra)
348{
349
350
351
352
353 if (asi < 0x80
354 && !cpu_hypervisor_mode(env)
355 && (!cpu_supervisor_mode(env)
356 || (asi >= 0x30 && cpu_has_hypervisor(env)))) {
357 cpu_raise_exception_ra(env, TT_PRIV_ACT, ra);
358 }
359}
360#endif
361#endif
362
363static void do_check_align(CPUSPARCState *env, target_ulong addr,
364 uint32_t align, uintptr_t ra)
365{
366 if (addr & align) {
367#ifdef DEBUG_UNALIGNED
368 printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
369 "\n", addr, env->pc);
370#endif
371 cpu_raise_exception_ra(env, TT_UNALIGNED, ra);
372 }
373}
374
375void helper_check_align(CPUSPARCState *env, target_ulong addr, uint32_t align)
376{
377 do_check_align(env, addr, align, GETPC());
378}
379
380#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
381 defined(DEBUG_MXCC)
382static void dump_mxcc(CPUSPARCState *env)
383{
384 printf("mxccdata: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
385 "\n",
386 env->mxccdata[0], env->mxccdata[1],
387 env->mxccdata[2], env->mxccdata[3]);
388 printf("mxccregs: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
389 "\n"
390 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
391 "\n",
392 env->mxccregs[0], env->mxccregs[1],
393 env->mxccregs[2], env->mxccregs[3],
394 env->mxccregs[4], env->mxccregs[5],
395 env->mxccregs[6], env->mxccregs[7]);
396}
397#endif
398
399#if (defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)) \
400 && defined(DEBUG_ASI)
401static void dump_asi(const char *txt, target_ulong addr, int asi, int size,
402 uint64_t r1)
403{
404 switch (size) {
405 case 1:
406 DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
407 addr, asi, r1 & 0xff);
408 break;
409 case 2:
410 DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
411 addr, asi, r1 & 0xffff);
412 break;
413 case 4:
414 DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
415 addr, asi, r1 & 0xffffffff);
416 break;
417 case 8:
418 DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
419 addr, asi, r1);
420 break;
421 }
422}
423#endif
424
425#ifndef TARGET_SPARC64
426#ifndef CONFIG_USER_ONLY
427
428
429
430
431static void leon3_cache_control_st(CPUSPARCState *env, target_ulong addr,
432 uint64_t val, int size)
433{
434 DPRINTF_CACHE_CONTROL("st addr:%08x, val:%" PRIx64 ", size:%d\n",
435 addr, val, size);
436
437 if (size != 4) {
438 DPRINTF_CACHE_CONTROL("32bits only\n");
439 return;
440 }
441
442 switch (addr) {
443 case 0x00:
444
445
446 val &= ~CACHE_CTRL_FD;
447 val &= ~CACHE_CTRL_FI;
448 val &= ~CACHE_CTRL_IB;
449 val &= ~CACHE_CTRL_IP;
450 val &= ~CACHE_CTRL_DP;
451
452 env->cache_control = val;
453 break;
454 case 0x04:
455 case 0x08:
456
457 break;
458 default:
459 DPRINTF_CACHE_CONTROL("write unknown register %08x\n", addr);
460 break;
461 };
462}
463
464static uint64_t leon3_cache_control_ld(CPUSPARCState *env, target_ulong addr,
465 int size)
466{
467 uint64_t ret = 0;
468
469 if (size != 4) {
470 DPRINTF_CACHE_CONTROL("32bits only\n");
471 return 0;
472 }
473
474 switch (addr) {
475 case 0x00:
476 ret = env->cache_control;
477 break;
478
479
480
481
482 case 0x04:
483 ret = 0x10220000;
484 break;
485 case 0x08:
486 ret = 0x18220000;
487 break;
488 default:
489 DPRINTF_CACHE_CONTROL("read unknown register %08x\n", addr);
490 break;
491 };
492 DPRINTF_CACHE_CONTROL("ld addr:%08x, ret:0x%" PRIx64 ", size:%d\n",
493 addr, ret, size);
494 return ret;
495}
496
497uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
498 int asi, uint32_t memop)
499{
500 int size = 1 << (memop & MO_SIZE);
501 int sign = memop & MO_SIGN;
502 CPUState *cs = CPU(sparc_env_get_cpu(env));
503 uint64_t ret = 0;
504#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
505 uint32_t last_addr = addr;
506#endif
507
508 do_check_align(env, addr, size - 1, GETPC());
509 switch (asi) {
510 case ASI_M_MXCC:
511
512 switch (addr) {
513 case 0x00:
514 case 0x08:
515 case 0x0C:
516 if (env->def.features & CPU_FEATURE_CACHE_CTRL) {
517 ret = leon3_cache_control_ld(env, addr, size);
518 }
519 break;
520 case 0x01c00a00:
521 if (size == 8) {
522 ret = env->mxccregs[3];
523 } else {
524 qemu_log_mask(LOG_UNIMP,
525 "%08x: unimplemented access size: %d\n", addr,
526 size);
527 }
528 break;
529 case 0x01c00a04:
530 if (size == 4) {
531 ret = env->mxccregs[3];
532 } else {
533 qemu_log_mask(LOG_UNIMP,
534 "%08x: unimplemented access size: %d\n", addr,
535 size);
536 }
537 break;
538 case 0x01c00c00:
539 if (size == 8) {
540 ret = env->mxccregs[5];
541
542 } else {
543 qemu_log_mask(LOG_UNIMP,
544 "%08x: unimplemented access size: %d\n", addr,
545 size);
546 }
547 break;
548 case 0x01c00f00:
549 if (size == 8) {
550 ret = env->mxccregs[7];
551 } else {
552 qemu_log_mask(LOG_UNIMP,
553 "%08x: unimplemented access size: %d\n", addr,
554 size);
555 }
556 break;
557 default:
558 qemu_log_mask(LOG_UNIMP,
559 "%08x: unimplemented address, size: %d\n", addr,
560 size);
561 break;
562 }
563 DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
564 "addr = %08x -> ret = %" PRIx64 ","
565 "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
566#ifdef DEBUG_MXCC
567 dump_mxcc(env);
568#endif
569 break;
570 case ASI_M_FLUSH_PROBE:
571 case ASI_LEON_MMUFLUSH:
572 {
573 int mmulev;
574
575 mmulev = (addr >> 8) & 15;
576 if (mmulev > 4) {
577 ret = 0;
578 } else {
579 ret = mmu_probe(env, addr, mmulev);
580 }
581 DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
582 addr, mmulev, ret);
583 }
584 break;
585 case ASI_M_MMUREGS:
586 case ASI_LEON_MMUREGS:
587 {
588 int reg = (addr >> 8) & 0x1f;
589
590 ret = env->mmuregs[reg];
591 if (reg == 3) {
592 env->mmuregs[3] = 0;
593 } else if (reg == 0x13) {
594 ret = env->mmuregs[3];
595 } else if (reg == 0x14) {
596 ret = env->mmuregs[4];
597 }
598 DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
599 }
600 break;
601 case ASI_M_TLBDIAG:
602 case ASI_M_DIAGS:
603 case ASI_M_IODIAG:
604 break;
605 case ASI_KERNELTXT:
606 switch (size) {
607 case 1:
608 ret = cpu_ldub_code(env, addr);
609 break;
610 case 2:
611 ret = cpu_lduw_code(env, addr);
612 break;
613 default:
614 case 4:
615 ret = cpu_ldl_code(env, addr);
616 break;
617 case 8:
618 ret = cpu_ldq_code(env, addr);
619 break;
620 }
621 break;
622 case ASI_M_TXTC_TAG:
623 case ASI_M_TXTC_DATA:
624 case ASI_M_DATAC_TAG:
625 case ASI_M_DATAC_DATA:
626 break;
627 case 0x21 ... 0x2f:
628 switch (size) {
629 case 1:
630 ret = ldub_phys(cs->as, (hwaddr)addr
631 | ((hwaddr)(asi & 0xf) << 32));
632 break;
633 case 2:
634 ret = lduw_phys(cs->as, (hwaddr)addr
635 | ((hwaddr)(asi & 0xf) << 32));
636 break;
637 default:
638 case 4:
639 ret = ldl_phys(cs->as, (hwaddr)addr
640 | ((hwaddr)(asi & 0xf) << 32));
641 break;
642 case 8:
643 ret = ldq_phys(cs->as, (hwaddr)addr
644 | ((hwaddr)(asi & 0xf) << 32));
645 break;
646 }
647 break;
648 case 0x30:
649 case 0x31:
650 case 0x32:
651 case 0x39:
652 ret = 0;
653 break;
654 case 0x38:
655 {
656 int reg = (addr >> 8) & 3;
657
658 switch (reg) {
659 case 0:
660 ret = env->mmubpregs[reg];
661 break;
662 case 1:
663 ret = env->mmubpregs[reg];
664 break;
665 case 2:
666 ret = env->mmubpregs[reg];
667 break;
668 case 3:
669 ret = env->mmubpregs[reg];
670 env->mmubpregs[reg] = 0ULL;
671 break;
672 }
673 DPRINTF_MMU("read breakpoint reg[%d] 0x%016" PRIx64 "\n", reg,
674 ret);
675 }
676 break;
677 case 0x49:
678 ret = env->mmubpctrv;
679 break;
680 case 0x4a:
681 ret = env->mmubpctrc;
682 break;
683 case 0x4b:
684 ret = env->mmubpctrs;
685 break;
686 case 0x4c:
687 ret = env->mmubpaction;
688 break;
689 case ASI_USERTXT:
690 default:
691 cpu_unassigned_access(cs, addr, false, false, asi, size);
692 ret = 0;
693 break;
694
695 case ASI_USERDATA:
696 case ASI_KERNELDATA:
697 case ASI_P:
698 case ASI_M_BYPASS:
699 case ASI_LEON_BYPASS:
700
701 g_assert_not_reached();
702 }
703 if (sign) {
704 switch (size) {
705 case 1:
706 ret = (int8_t) ret;
707 break;
708 case 2:
709 ret = (int16_t) ret;
710 break;
711 case 4:
712 ret = (int32_t) ret;
713 break;
714 default:
715 break;
716 }
717 }
718#ifdef DEBUG_ASI
719 dump_asi("read ", last_addr, asi, size, ret);
720#endif
721 return ret;
722}
723
724void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val,
725 int asi, uint32_t memop)
726{
727 int size = 1 << (memop & MO_SIZE);
728 SPARCCPU *cpu = sparc_env_get_cpu(env);
729 CPUState *cs = CPU(cpu);
730
731 do_check_align(env, addr, size - 1, GETPC());
732 switch (asi) {
733 case ASI_M_MXCC:
734
735 switch (addr) {
736 case 0x00:
737 case 0x08:
738 case 0x0C:
739 if (env->def.features & CPU_FEATURE_CACHE_CTRL) {
740 leon3_cache_control_st(env, addr, val, size);
741 }
742 break;
743
744 case 0x01c00000:
745 if (size == 8) {
746 env->mxccdata[0] = val;
747 } else {
748 qemu_log_mask(LOG_UNIMP,
749 "%08x: unimplemented access size: %d\n", addr,
750 size);
751 }
752 break;
753 case 0x01c00008:
754 if (size == 8) {
755 env->mxccdata[1] = val;
756 } else {
757 qemu_log_mask(LOG_UNIMP,
758 "%08x: unimplemented access size: %d\n", addr,
759 size);
760 }
761 break;
762 case 0x01c00010:
763 if (size == 8) {
764 env->mxccdata[2] = val;
765 } else {
766 qemu_log_mask(LOG_UNIMP,
767 "%08x: unimplemented access size: %d\n", addr,
768 size);
769 }
770 break;
771 case 0x01c00018:
772 if (size == 8) {
773 env->mxccdata[3] = val;
774 } else {
775 qemu_log_mask(LOG_UNIMP,
776 "%08x: unimplemented access size: %d\n", addr,
777 size);
778 }
779 break;
780 case 0x01c00100:
781 if (size == 8) {
782 env->mxccregs[0] = val;
783 } else {
784 qemu_log_mask(LOG_UNIMP,
785 "%08x: unimplemented access size: %d\n", addr,
786 size);
787 }
788 env->mxccdata[0] = ldq_phys(cs->as,
789 (env->mxccregs[0] & 0xffffffffULL) +
790 0);
791 env->mxccdata[1] = ldq_phys(cs->as,
792 (env->mxccregs[0] & 0xffffffffULL) +
793 8);
794 env->mxccdata[2] = ldq_phys(cs->as,
795 (env->mxccregs[0] & 0xffffffffULL) +
796 16);
797 env->mxccdata[3] = ldq_phys(cs->as,
798 (env->mxccregs[0] & 0xffffffffULL) +
799 24);
800 break;
801 case 0x01c00200:
802 if (size == 8) {
803 env->mxccregs[1] = val;
804 } else {
805 qemu_log_mask(LOG_UNIMP,
806 "%08x: unimplemented access size: %d\n", addr,
807 size);
808 }
809 stq_phys(cs->as, (env->mxccregs[1] & 0xffffffffULL) + 0,
810 env->mxccdata[0]);
811 stq_phys(cs->as, (env->mxccregs[1] & 0xffffffffULL) + 8,
812 env->mxccdata[1]);
813 stq_phys(cs->as, (env->mxccregs[1] & 0xffffffffULL) + 16,
814 env->mxccdata[2]);
815 stq_phys(cs->as, (env->mxccregs[1] & 0xffffffffULL) + 24,
816 env->mxccdata[3]);
817 break;
818 case 0x01c00a00:
819 if (size == 8) {
820 env->mxccregs[3] = val;
821 } else {
822 qemu_log_mask(LOG_UNIMP,
823 "%08x: unimplemented access size: %d\n", addr,
824 size);
825 }
826 break;
827 case 0x01c00a04:
828 if (size == 4) {
829 env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
830 | val;
831 } else {
832 qemu_log_mask(LOG_UNIMP,
833 "%08x: unimplemented access size: %d\n", addr,
834 size);
835 }
836 break;
837 case 0x01c00e00:
838
839 if (size == 8) {
840 env->mxccregs[6] &= ~val;
841 } else {
842 qemu_log_mask(LOG_UNIMP,
843 "%08x: unimplemented access size: %d\n", addr,
844 size);
845 }
846 break;
847 case 0x01c00f00:
848 if (size == 8) {
849 env->mxccregs[7] = val;
850 } else {
851 qemu_log_mask(LOG_UNIMP,
852 "%08x: unimplemented access size: %d\n", addr,
853 size);
854 }
855 break;
856 default:
857 qemu_log_mask(LOG_UNIMP,
858 "%08x: unimplemented address, size: %d\n", addr,
859 size);
860 break;
861 }
862 DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
863 asi, size, addr, val);
864#ifdef DEBUG_MXCC
865 dump_mxcc(env);
866#endif
867 break;
868 case ASI_M_FLUSH_PROBE:
869 case ASI_LEON_MMUFLUSH:
870 {
871 int mmulev;
872
873 mmulev = (addr >> 8) & 15;
874 DPRINTF_MMU("mmu flush level %d\n", mmulev);
875 switch (mmulev) {
876 case 0:
877 tlb_flush_page(CPU(cpu), addr & 0xfffff000);
878 break;
879 case 1:
880 case 2:
881 case 3:
882 case 4:
883 tlb_flush(CPU(cpu));
884 break;
885 default:
886 break;
887 }
888#ifdef DEBUG_MMU
889 dump_mmu(stdout, fprintf, env);
890#endif
891 }
892 break;
893 case ASI_M_MMUREGS:
894 case ASI_LEON_MMUREGS:
895 {
896 int reg = (addr >> 8) & 0x1f;
897 uint32_t oldreg;
898
899 oldreg = env->mmuregs[reg];
900 switch (reg) {
901 case 0:
902 env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
903 (val & 0x00ffffff);
904
905
906 if ((oldreg ^ env->mmuregs[reg])
907 & (MMU_NF | env->def.mmu_bm)) {
908 tlb_flush(CPU(cpu));
909 }
910 break;
911 case 1:
912 env->mmuregs[reg] = val & env->def.mmu_ctpr_mask;
913 break;
914 case 2:
915 env->mmuregs[reg] = val & env->def.mmu_cxr_mask;
916 if (oldreg != env->mmuregs[reg]) {
917
918
919 tlb_flush(CPU(cpu));
920 }
921 break;
922 case 3:
923 case 4:
924 break;
925 case 0x10:
926 env->mmuregs[reg] = val & env->def.mmu_trcr_mask;
927 break;
928 case 0x13:
929
930 env->mmuregs[3] = val & env->def.mmu_sfsr_mask;
931 break;
932 case 0x14:
933 env->mmuregs[4] = val;
934 break;
935 default:
936 env->mmuregs[reg] = val;
937 break;
938 }
939 if (oldreg != env->mmuregs[reg]) {
940 DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
941 reg, oldreg, env->mmuregs[reg]);
942 }
943#ifdef DEBUG_MMU
944 dump_mmu(stdout, fprintf, env);
945#endif
946 }
947 break;
948 case ASI_M_TLBDIAG:
949 case ASI_M_DIAGS:
950 case ASI_M_IODIAG:
951 break;
952 case ASI_M_TXTC_TAG:
953 case ASI_M_TXTC_DATA:
954 case ASI_M_DATAC_TAG:
955 case ASI_M_DATAC_DATA:
956 case ASI_M_FLUSH_PAGE:
957 case ASI_M_FLUSH_SEG:
958 case ASI_M_FLUSH_REGION:
959 case ASI_M_FLUSH_CTX:
960 case ASI_M_FLUSH_USER:
961 break;
962 case 0x21 ... 0x2f:
963 {
964 switch (size) {
965 case 1:
966 stb_phys(cs->as, (hwaddr)addr
967 | ((hwaddr)(asi & 0xf) << 32), val);
968 break;
969 case 2:
970 stw_phys(cs->as, (hwaddr)addr
971 | ((hwaddr)(asi & 0xf) << 32), val);
972 break;
973 case 4:
974 default:
975 stl_phys(cs->as, (hwaddr)addr
976 | ((hwaddr)(asi & 0xf) << 32), val);
977 break;
978 case 8:
979 stq_phys(cs->as, (hwaddr)addr
980 | ((hwaddr)(asi & 0xf) << 32), val);
981 break;
982 }
983 }
984 break;
985 case 0x30:
986 case 0x31:
987
988 case 0x32:
989
990 case 0x36:
991 case 0x37:
992 break;
993 case 0x38:
994 {
995 int reg = (addr >> 8) & 3;
996
997 switch (reg) {
998 case 0:
999 env->mmubpregs[reg] = (val & 0xfffffffffULL);
1000 break;
1001 case 1:
1002 env->mmubpregs[reg] = (val & 0xfffffffffULL);
1003 break;
1004 case 2:
1005 env->mmubpregs[reg] = (val & 0x7fULL);
1006 break;
1007 case 3:
1008 env->mmubpregs[reg] = (val & 0xfULL);
1009 break;
1010 }
1011 DPRINTF_MMU("write breakpoint reg[%d] 0x%016x\n", reg,
1012 env->mmuregs[reg]);
1013 }
1014 break;
1015 case 0x49:
1016 env->mmubpctrv = val & 0xffffffff;
1017 break;
1018 case 0x4a:
1019 env->mmubpctrc = val & 0x3;
1020 break;
1021 case 0x4b:
1022 env->mmubpctrs = val & 0x3;
1023 break;
1024 case 0x4c:
1025 env->mmubpaction = val & 0x1fff;
1026 break;
1027 case ASI_USERTXT:
1028 case ASI_KERNELTXT:
1029 default:
1030 cpu_unassigned_access(CPU(sparc_env_get_cpu(env)),
1031 addr, true, false, asi, size);
1032 break;
1033
1034 case ASI_USERDATA:
1035 case ASI_KERNELDATA:
1036 case ASI_P:
1037 case ASI_M_BYPASS:
1038 case ASI_LEON_BYPASS:
1039 case ASI_M_BCOPY:
1040 case ASI_M_BFILL:
1041
1042 g_assert_not_reached();
1043 }
1044#ifdef DEBUG_ASI
1045 dump_asi("write", addr, asi, size, val);
1046#endif
1047}
1048
1049#endif
1050#else
1051
1052#ifdef CONFIG_USER_ONLY
1053uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
1054 int asi, uint32_t memop)
1055{
1056 int size = 1 << (memop & MO_SIZE);
1057 int sign = memop & MO_SIGN;
1058 uint64_t ret = 0;
1059
1060 if (asi < 0x80) {
1061 cpu_raise_exception_ra(env, TT_PRIV_ACT, GETPC());
1062 }
1063 do_check_align(env, addr, size - 1, GETPC());
1064 addr = asi_address_mask(env, asi, addr);
1065
1066 switch (asi) {
1067 case ASI_PNF:
1068 case ASI_PNFL:
1069 case ASI_SNF:
1070 case ASI_SNFL:
1071 if (page_check_range(addr, size, PAGE_READ) == -1) {
1072 ret = 0;
1073 break;
1074 }
1075 switch (size) {
1076 case 1:
1077 ret = cpu_ldub_data(env, addr);
1078 break;
1079 case 2:
1080 ret = cpu_lduw_data(env, addr);
1081 break;
1082 case 4:
1083 ret = cpu_ldl_data(env, addr);
1084 break;
1085 case 8:
1086 ret = cpu_ldq_data(env, addr);
1087 break;
1088 default:
1089 g_assert_not_reached();
1090 }
1091 break;
1092 break;
1093
1094 case ASI_P:
1095 case ASI_PL:
1096 case ASI_S:
1097 case ASI_SL:
1098
1099 g_assert_not_reached();
1100
1101 default:
1102 cpu_raise_exception_ra(env, TT_DATA_ACCESS, GETPC());
1103 }
1104
1105
1106 switch (asi) {
1107 case ASI_PNFL:
1108 case ASI_SNFL:
1109 switch (size) {
1110 case 2:
1111 ret = bswap16(ret);
1112 break;
1113 case 4:
1114 ret = bswap32(ret);
1115 break;
1116 case 8:
1117 ret = bswap64(ret);
1118 break;
1119 }
1120 }
1121
1122
1123 if (sign) {
1124 switch (size) {
1125 case 1:
1126 ret = (int8_t) ret;
1127 break;
1128 case 2:
1129 ret = (int16_t) ret;
1130 break;
1131 case 4:
1132 ret = (int32_t) ret;
1133 break;
1134 }
1135 }
1136#ifdef DEBUG_ASI
1137 dump_asi("read", addr, asi, size, ret);
1138#endif
1139 return ret;
1140}
1141
1142void helper_st_asi(CPUSPARCState *env, target_ulong addr, target_ulong val,
1143 int asi, uint32_t memop)
1144{
1145 int size = 1 << (memop & MO_SIZE);
1146#ifdef DEBUG_ASI
1147 dump_asi("write", addr, asi, size, val);
1148#endif
1149 if (asi < 0x80) {
1150 cpu_raise_exception_ra(env, TT_PRIV_ACT, GETPC());
1151 }
1152 do_check_align(env, addr, size - 1, GETPC());
1153
1154 switch (asi) {
1155 case ASI_P:
1156 case ASI_PL:
1157 case ASI_S:
1158 case ASI_SL:
1159
1160 g_assert_not_reached();
1161
1162 case ASI_PNF:
1163 case ASI_SNF:
1164 case ASI_PNFL:
1165 case ASI_SNFL:
1166 default:
1167 cpu_raise_exception_ra(env, TT_DATA_ACCESS, GETPC());
1168 }
1169}
1170
1171#else
1172
1173uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
1174 int asi, uint32_t memop)
1175{
1176 int size = 1 << (memop & MO_SIZE);
1177 int sign = memop & MO_SIGN;
1178 CPUState *cs = CPU(sparc_env_get_cpu(env));
1179 uint64_t ret = 0;
1180#if defined(DEBUG_ASI)
1181 target_ulong last_addr = addr;
1182#endif
1183
1184 asi &= 0xff;
1185
1186 do_check_asi(env, asi, GETPC());
1187 do_check_align(env, addr, size - 1, GETPC());
1188 addr = asi_address_mask(env, asi, addr);
1189
1190 switch (asi) {
1191 case ASI_PNF:
1192 case ASI_PNFL:
1193 case ASI_SNF:
1194 case ASI_SNFL:
1195 {
1196 TCGMemOpIdx oi;
1197 int idx = (env->pstate & PS_PRIV
1198 ? (asi & 1 ? MMU_KERNEL_SECONDARY_IDX : MMU_KERNEL_IDX)
1199 : (asi & 1 ? MMU_USER_SECONDARY_IDX : MMU_USER_IDX));
1200
1201 if (cpu_get_phys_page_nofault(env, addr, idx) == -1ULL) {
1202#ifdef DEBUG_ASI
1203 dump_asi("read ", last_addr, asi, size, ret);
1204#endif
1205
1206 cpu_raise_exception_ra(env, cs->exception_index, GETPC());
1207 }
1208 oi = make_memop_idx(memop, idx);
1209 switch (size) {
1210 case 1:
1211 ret = helper_ret_ldub_mmu(env, addr, oi, GETPC());
1212 break;
1213 case 2:
1214 if (asi & 8) {
1215 ret = helper_le_lduw_mmu(env, addr, oi, GETPC());
1216 } else {
1217 ret = helper_be_lduw_mmu(env, addr, oi, GETPC());
1218 }
1219 break;
1220 case 4:
1221 if (asi & 8) {
1222 ret = helper_le_ldul_mmu(env, addr, oi, GETPC());
1223 } else {
1224 ret = helper_be_ldul_mmu(env, addr, oi, GETPC());
1225 }
1226 break;
1227 case 8:
1228 if (asi & 8) {
1229 ret = helper_le_ldq_mmu(env, addr, oi, GETPC());
1230 } else {
1231 ret = helper_be_ldq_mmu(env, addr, oi, GETPC());
1232 }
1233 break;
1234 default:
1235 g_assert_not_reached();
1236 }
1237 }
1238 break;
1239
1240 case ASI_AIUP:
1241 case ASI_AIUS:
1242 case ASI_AIUPL:
1243 case ASI_AIUSL:
1244 case ASI_P:
1245 case ASI_S:
1246 case ASI_PL:
1247 case ASI_SL:
1248 case ASI_REAL:
1249 case ASI_REAL_IO:
1250 case ASI_REAL_L:
1251 case ASI_REAL_IO_L:
1252 case ASI_N:
1253 case ASI_NL:
1254 case ASI_NUCLEUS_QUAD_LDD:
1255 case ASI_NUCLEUS_QUAD_LDD_L:
1256 case ASI_TWINX_AIUP:
1257 case ASI_TWINX_AIUS:
1258 case ASI_TWINX_REAL:
1259 case ASI_TWINX_AIUP_L:
1260 case ASI_TWINX_AIUS_L:
1261 case ASI_TWINX_REAL_L:
1262 case ASI_TWINX_N:
1263 case ASI_TWINX_NL:
1264
1265 case ASI_TWINX_P:
1266 case ASI_TWINX_PL:
1267 case ASI_TWINX_S:
1268 case ASI_TWINX_SL:
1269
1270 g_assert_not_reached();
1271
1272 case ASI_UPA_CONFIG:
1273
1274 break;
1275 case ASI_LSU_CONTROL:
1276 ret = env->lsu;
1277 break;
1278 case ASI_IMMU:
1279 {
1280 int reg = (addr >> 3) & 0xf;
1281 switch (reg) {
1282 case 0:
1283
1284 ret = ultrasparc_tag_target(env->immu.tag_access);
1285 break;
1286 case 3:
1287 ret = env->immu.sfsr;
1288 break;
1289 case 5:
1290 ret = env->immu.tsb;
1291 break;
1292 case 6:
1293
1294 ret = env->immu.tag_access;
1295 break;
1296 default:
1297 cpu_unassigned_access(cs, addr, false, false, 1, size);
1298 ret = 0;
1299 }
1300 break;
1301 }
1302 case ASI_IMMU_TSB_8KB_PTR:
1303 {
1304
1305
1306 ret = ultrasparc_tsb_pointer(env, &env->immu, 0);
1307 break;
1308 }
1309 case ASI_IMMU_TSB_64KB_PTR:
1310 {
1311
1312
1313 ret = ultrasparc_tsb_pointer(env, &env->immu, 1);
1314 break;
1315 }
1316 case ASI_ITLB_DATA_ACCESS:
1317 {
1318 int reg = (addr >> 3) & 0x3f;
1319
1320 ret = env->itlb[reg].tte;
1321 break;
1322 }
1323 case ASI_ITLB_TAG_READ:
1324 {
1325 int reg = (addr >> 3) & 0x3f;
1326
1327 ret = env->itlb[reg].tag;
1328 break;
1329 }
1330 case ASI_DMMU:
1331 {
1332 int reg = (addr >> 3) & 0xf;
1333 switch (reg) {
1334 case 0:
1335
1336 ret = ultrasparc_tag_target(env->dmmu.tag_access);
1337 break;
1338 case 1:
1339 ret = env->dmmu.mmu_primary_context;
1340 break;
1341 case 2:
1342 ret = env->dmmu.mmu_secondary_context;
1343 break;
1344 case 3:
1345 ret = env->dmmu.sfsr;
1346 break;
1347 case 4:
1348 ret = env->dmmu.sfar;
1349 break;
1350 case 5:
1351 ret = env->dmmu.tsb;
1352 break;
1353 case 6:
1354 ret = env->dmmu.tag_access;
1355 break;
1356 case 7:
1357 ret = env->dmmu.virtual_watchpoint;
1358 break;
1359 case 8:
1360 ret = env->dmmu.physical_watchpoint;
1361 break;
1362 default:
1363 cpu_unassigned_access(cs, addr, false, false, 1, size);
1364 ret = 0;
1365 }
1366 break;
1367 }
1368 case ASI_DMMU_TSB_8KB_PTR:
1369 {
1370
1371
1372 ret = ultrasparc_tsb_pointer(env, &env->dmmu, 0);
1373 break;
1374 }
1375 case ASI_DMMU_TSB_64KB_PTR:
1376 {
1377
1378
1379 ret = ultrasparc_tsb_pointer(env, &env->dmmu, 1);
1380 break;
1381 }
1382 case ASI_DTLB_DATA_ACCESS:
1383 {
1384 int reg = (addr >> 3) & 0x3f;
1385
1386 ret = env->dtlb[reg].tte;
1387 break;
1388 }
1389 case ASI_DTLB_TAG_READ:
1390 {
1391 int reg = (addr >> 3) & 0x3f;
1392
1393 ret = env->dtlb[reg].tag;
1394 break;
1395 }
1396 case ASI_INTR_DISPATCH_STAT:
1397 break;
1398 case ASI_INTR_RECEIVE:
1399 ret = env->ivec_status;
1400 break;
1401 case ASI_INTR_R:
1402 {
1403 int reg = (addr >> 4) & 0x3;
1404 if (reg < 3) {
1405 ret = env->ivec_data[reg];
1406 }
1407 break;
1408 }
1409 case ASI_SCRATCHPAD:
1410 if (unlikely((addr >= 0x20) && (addr < 0x30))) {
1411
1412 cpu_unassigned_access(cs, addr, false, false, 1, size);
1413 }
1414
1415 case ASI_HYP_SCRATCHPAD:
1416 {
1417 unsigned int i = (addr >> 3) & 0x7;
1418 ret = env->scratch[i];
1419 break;
1420 }
1421 case ASI_MMU:
1422 switch ((addr >> 3) & 0x3) {
1423 case 1:
1424 ret = env->dmmu.mmu_primary_context;
1425 break;
1426 case 2:
1427 ret = env->dmmu.mmu_secondary_context;
1428 break;
1429 default:
1430 cpu_unassigned_access(cs, addr, true, false, 1, size);
1431 }
1432 break;
1433 case ASI_DCACHE_DATA:
1434 case ASI_DCACHE_TAG:
1435 case ASI_ESTATE_ERROR_EN:
1436 case ASI_AFSR:
1437 case ASI_AFAR:
1438 case ASI_EC_TAG_DATA:
1439 case ASI_IC_INSTR:
1440 case ASI_IC_TAG:
1441 case ASI_IC_PRE_DECODE:
1442 case ASI_IC_NEXT_FIELD:
1443 case ASI_EC_W:
1444 case ASI_EC_R:
1445 break;
1446 case ASI_DMMU_TSB_DIRECT_PTR:
1447 case ASI_ITLB_DATA_IN:
1448 case ASI_IMMU_DEMAP:
1449 case ASI_DTLB_DATA_IN:
1450 case ASI_DMMU_DEMAP:
1451 case ASI_INTR_W:
1452 default:
1453 cpu_unassigned_access(cs, addr, false, false, 1, size);
1454 ret = 0;
1455 break;
1456 }
1457
1458
1459 if (sign) {
1460 switch (size) {
1461 case 1:
1462 ret = (int8_t) ret;
1463 break;
1464 case 2:
1465 ret = (int16_t) ret;
1466 break;
1467 case 4:
1468 ret = (int32_t) ret;
1469 break;
1470 default:
1471 break;
1472 }
1473 }
1474#ifdef DEBUG_ASI
1475 dump_asi("read ", last_addr, asi, size, ret);
1476#endif
1477 return ret;
1478}
1479
1480void helper_st_asi(CPUSPARCState *env, target_ulong addr, target_ulong val,
1481 int asi, uint32_t memop)
1482{
1483 int size = 1 << (memop & MO_SIZE);
1484 SPARCCPU *cpu = sparc_env_get_cpu(env);
1485 CPUState *cs = CPU(cpu);
1486
1487#ifdef DEBUG_ASI
1488 dump_asi("write", addr, asi, size, val);
1489#endif
1490
1491 asi &= 0xff;
1492
1493 do_check_asi(env, asi, GETPC());
1494 do_check_align(env, addr, size - 1, GETPC());
1495 addr = asi_address_mask(env, asi, addr);
1496
1497 switch (asi) {
1498 case ASI_AIUP:
1499 case ASI_AIUS:
1500 case ASI_AIUPL:
1501 case ASI_AIUSL:
1502 case ASI_P:
1503 case ASI_S:
1504 case ASI_PL:
1505 case ASI_SL:
1506 case ASI_REAL:
1507 case ASI_REAL_IO:
1508 case ASI_REAL_L:
1509 case ASI_REAL_IO_L:
1510 case ASI_N:
1511 case ASI_NL:
1512 case ASI_NUCLEUS_QUAD_LDD:
1513 case ASI_NUCLEUS_QUAD_LDD_L:
1514 case ASI_TWINX_AIUP:
1515 case ASI_TWINX_AIUS:
1516 case ASI_TWINX_REAL:
1517 case ASI_TWINX_AIUP_L:
1518 case ASI_TWINX_AIUS_L:
1519 case ASI_TWINX_REAL_L:
1520 case ASI_TWINX_N:
1521 case ASI_TWINX_NL:
1522
1523 case ASI_TWINX_P:
1524 case ASI_TWINX_PL:
1525 case ASI_TWINX_S:
1526 case ASI_TWINX_SL:
1527
1528 g_assert_not_reached();
1529
1530
1531
1532 case 0x31:
1533 case 0x32:
1534 case 0x39:
1535 case 0x3a:
1536 if (cpu_has_hypervisor(env)) {
1537
1538
1539
1540
1541
1542
1543 int idx = ((asi & 2) >> 1) | ((asi & 8) >> 2);
1544 env->dmmu.sun4v_tsb_pointers[idx] = val;
1545 } else {
1546 helper_raise_exception(env, TT_ILL_INSN);
1547 }
1548 break;
1549 case 0x33:
1550 case 0x3b:
1551 if (cpu_has_hypervisor(env)) {
1552
1553
1554
1555
1556 env->dmmu.sun4v_ctx_config[(asi & 8) >> 3] = val;
1557 } else {
1558 helper_raise_exception(env, TT_ILL_INSN);
1559 }
1560 break;
1561 case 0x35:
1562 case 0x36:
1563 case 0x3d:
1564 case 0x3e:
1565 if (cpu_has_hypervisor(env)) {
1566
1567
1568
1569
1570
1571
1572 int idx = ((asi & 2) >> 1) | ((asi & 8) >> 2);
1573 env->immu.sun4v_tsb_pointers[idx] = val;
1574 } else {
1575 helper_raise_exception(env, TT_ILL_INSN);
1576 }
1577 break;
1578 case 0x37:
1579 case 0x3f:
1580 if (cpu_has_hypervisor(env)) {
1581
1582
1583
1584
1585 env->immu.sun4v_ctx_config[(asi & 8) >> 3] = val;
1586 } else {
1587 helper_raise_exception(env, TT_ILL_INSN);
1588 }
1589 break;
1590 case ASI_UPA_CONFIG:
1591
1592 return;
1593 case ASI_LSU_CONTROL:
1594 env->lsu = val & (DMMU_E | IMMU_E);
1595 return;
1596 case ASI_IMMU:
1597 {
1598 int reg = (addr >> 3) & 0xf;
1599 uint64_t oldreg;
1600
1601 oldreg = env->immu.mmuregs[reg];
1602 switch (reg) {
1603 case 0:
1604 return;
1605 case 1:
1606 case 2:
1607 return;
1608 case 3:
1609 if ((val & 1) == 0) {
1610 val = 0;
1611 }
1612 env->immu.sfsr = val;
1613 break;
1614 case 4:
1615 return;
1616 case 5:
1617 DPRINTF_MMU("immu TSB write: 0x%016" PRIx64 " -> 0x%016"
1618 PRIx64 "\n", env->immu.tsb, val);
1619 env->immu.tsb = val;
1620 break;
1621 case 6:
1622 env->immu.tag_access = val;
1623 break;
1624 case 7:
1625 case 8:
1626 return;
1627 default:
1628 cpu_unassigned_access(cs, addr, true, false, 1, size);
1629 break;
1630 }
1631
1632 if (oldreg != env->immu.mmuregs[reg]) {
1633 DPRINTF_MMU("immu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
1634 PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
1635 }
1636#ifdef DEBUG_MMU
1637 dump_mmu(stdout, fprintf, env);
1638#endif
1639 return;
1640 }
1641 case ASI_ITLB_DATA_IN:
1642
1643 if (!(addr & TLB_UST1_IS_REAL_BIT)) {
1644 replace_tlb_1bit_lru(env->itlb, env->immu.tag_access,
1645 val, "immu", env, addr);
1646 }
1647 return;
1648 case ASI_ITLB_DATA_ACCESS:
1649 {
1650
1651
1652 unsigned int i = (addr >> 3) & 0x3f;
1653
1654
1655 if (!(addr & TLB_UST1_IS_REAL_BIT)) {
1656 replace_tlb_entry(&env->itlb[i], env->immu.tag_access,
1657 sun4v_tte_to_sun4u(env, addr, val), env);
1658 }
1659#ifdef DEBUG_MMU
1660 DPRINTF_MMU("immu data access replaced entry [%i]\n", i);
1661 dump_mmu(stdout, fprintf, env);
1662#endif
1663 return;
1664 }
1665 case ASI_IMMU_DEMAP:
1666 demap_tlb(env->itlb, addr, "immu", env);
1667 return;
1668 case ASI_DMMU:
1669 {
1670 int reg = (addr >> 3) & 0xf;
1671 uint64_t oldreg;
1672
1673 oldreg = env->dmmu.mmuregs[reg];
1674 switch (reg) {
1675 case 0:
1676 case 4:
1677 return;
1678 case 3:
1679 if ((val & 1) == 0) {
1680 val = 0;
1681 env->dmmu.sfar = 0;
1682 }
1683 env->dmmu.sfsr = val;
1684 break;
1685 case 1:
1686 env->dmmu.mmu_primary_context = val;
1687
1688
1689 tlb_flush(CPU(cpu));
1690 break;
1691 case 2:
1692 env->dmmu.mmu_secondary_context = val;
1693
1694
1695 tlb_flush(CPU(cpu));
1696 break;
1697 case 5:
1698 DPRINTF_MMU("dmmu TSB write: 0x%016" PRIx64 " -> 0x%016"
1699 PRIx64 "\n", env->dmmu.tsb, val);
1700 env->dmmu.tsb = val;
1701 break;
1702 case 6:
1703 env->dmmu.tag_access = val;
1704 break;
1705 case 7:
1706 env->dmmu.virtual_watchpoint = val;
1707 break;
1708 case 8:
1709 env->dmmu.physical_watchpoint = val;
1710 break;
1711 default:
1712 cpu_unassigned_access(cs, addr, true, false, 1, size);
1713 break;
1714 }
1715
1716 if (oldreg != env->dmmu.mmuregs[reg]) {
1717 DPRINTF_MMU("dmmu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
1718 PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
1719 }
1720#ifdef DEBUG_MMU
1721 dump_mmu(stdout, fprintf, env);
1722#endif
1723 return;
1724 }
1725 case ASI_DTLB_DATA_IN:
1726
1727 if (!(addr & TLB_UST1_IS_REAL_BIT)) {
1728 replace_tlb_1bit_lru(env->dtlb, env->dmmu.tag_access,
1729 val, "dmmu", env, addr);
1730 }
1731 return;
1732 case ASI_DTLB_DATA_ACCESS:
1733 {
1734 unsigned int i = (addr >> 3) & 0x3f;
1735
1736
1737 if (!(addr & TLB_UST1_IS_REAL_BIT)) {
1738 replace_tlb_entry(&env->dtlb[i], env->dmmu.tag_access,
1739 sun4v_tte_to_sun4u(env, addr, val), env);
1740 }
1741#ifdef DEBUG_MMU
1742 DPRINTF_MMU("dmmu data access replaced entry [%i]\n", i);
1743 dump_mmu(stdout, fprintf, env);
1744#endif
1745 return;
1746 }
1747 case ASI_DMMU_DEMAP:
1748 demap_tlb(env->dtlb, addr, "dmmu", env);
1749 return;
1750 case ASI_INTR_RECEIVE:
1751 env->ivec_status = val & 0x20;
1752 return;
1753 case ASI_SCRATCHPAD:
1754 if (unlikely((addr >= 0x20) && (addr < 0x30))) {
1755
1756 cpu_unassigned_access(cs, addr, true, false, 1, size);
1757 }
1758
1759 case ASI_HYP_SCRATCHPAD:
1760 {
1761 unsigned int i = (addr >> 3) & 0x7;
1762 env->scratch[i] = val;
1763 return;
1764 }
1765 case ASI_MMU:
1766 {
1767 switch ((addr >> 3) & 0x3) {
1768 case 1:
1769 env->dmmu.mmu_primary_context = val;
1770 env->immu.mmu_primary_context = val;
1771 tlb_flush_by_mmuidx(CPU(cpu),
1772 (1 << MMU_USER_IDX) | (1 << MMU_KERNEL_IDX));
1773 break;
1774 case 2:
1775 env->dmmu.mmu_secondary_context = val;
1776 env->immu.mmu_secondary_context = val;
1777 tlb_flush_by_mmuidx(CPU(cpu),
1778 (1 << MMU_USER_SECONDARY_IDX) |
1779 (1 << MMU_KERNEL_SECONDARY_IDX));
1780 break;
1781 default:
1782 cpu_unassigned_access(cs, addr, true, false, 1, size);
1783 }
1784 }
1785 return;
1786 case ASI_QUEUE:
1787 case ASI_DCACHE_DATA:
1788 case ASI_DCACHE_TAG:
1789 case ASI_ESTATE_ERROR_EN:
1790 case ASI_AFSR:
1791 case ASI_AFAR:
1792 case ASI_EC_TAG_DATA:
1793 case ASI_IC_INSTR:
1794 case ASI_IC_TAG:
1795 case ASI_IC_PRE_DECODE:
1796 case ASI_IC_NEXT_FIELD:
1797 case ASI_EC_W:
1798 case ASI_EC_R:
1799 return;
1800 case ASI_IMMU_TSB_8KB_PTR:
1801 case ASI_IMMU_TSB_64KB_PTR:
1802 case ASI_ITLB_TAG_READ:
1803 case ASI_DMMU_TSB_8KB_PTR:
1804 case ASI_DMMU_TSB_64KB_PTR:
1805 case ASI_DMMU_TSB_DIRECT_PTR:
1806 case ASI_DTLB_TAG_READ:
1807 case ASI_INTR_DISPATCH_STAT:
1808 case ASI_INTR_R:
1809 case ASI_PNF:
1810 case ASI_SNF:
1811 case ASI_PNFL:
1812 case ASI_SNFL:
1813 default:
1814 cpu_unassigned_access(cs, addr, true, false, 1, size);
1815 return;
1816 }
1817}
1818#endif
1819#endif
1820
1821#if !defined(CONFIG_USER_ONLY)
1822#ifndef TARGET_SPARC64
1823void sparc_cpu_unassigned_access(CPUState *cs, hwaddr addr,
1824 bool is_write, bool is_exec, int is_asi,
1825 unsigned size)
1826{
1827 SPARCCPU *cpu = SPARC_CPU(cs);
1828 CPUSPARCState *env = &cpu->env;
1829 int fault_type;
1830
1831#ifdef DEBUG_UNASSIGNED
1832 if (is_asi) {
1833 printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
1834 " asi 0x%02x from " TARGET_FMT_lx "\n",
1835 is_exec ? "exec" : is_write ? "write" : "read", size,
1836 size == 1 ? "" : "s", addr, is_asi, env->pc);
1837 } else {
1838 printf("Unassigned mem %s access of %d byte%s to " TARGET_FMT_plx
1839 " from " TARGET_FMT_lx "\n",
1840 is_exec ? "exec" : is_write ? "write" : "read", size,
1841 size == 1 ? "" : "s", addr, env->pc);
1842 }
1843#endif
1844
1845 fault_type = (env->mmuregs[3] & 0x1c) >> 2;
1846 if ((fault_type > 4) || (fault_type == 0)) {
1847 env->mmuregs[3] = 0;
1848 if (is_asi) {
1849 env->mmuregs[3] |= 1 << 16;
1850 }
1851 if (env->psrs) {
1852 env->mmuregs[3] |= 1 << 5;
1853 }
1854 if (is_exec) {
1855 env->mmuregs[3] |= 1 << 6;
1856 }
1857 if (is_write) {
1858 env->mmuregs[3] |= 1 << 7;
1859 }
1860 env->mmuregs[3] |= (5 << 2) | 2;
1861
1862 if (!is_exec) {
1863 env->mmuregs[4] = addr;
1864 }
1865 }
1866
1867 if (fault_type == ((env->mmuregs[3] & 0x1c)) >> 2) {
1868 env->mmuregs[3] |= 1;
1869 }
1870
1871 if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
1872 int tt = is_exec ? TT_CODE_ACCESS : TT_DATA_ACCESS;
1873 cpu_raise_exception_ra(env, tt, GETPC());
1874 }
1875
1876
1877
1878 if (env->mmuregs[0] & MMU_NF) {
1879 tlb_flush(cs);
1880 }
1881}
1882#else
1883void sparc_cpu_unassigned_access(CPUState *cs, hwaddr addr,
1884 bool is_write, bool is_exec, int is_asi,
1885 unsigned size)
1886{
1887 SPARCCPU *cpu = SPARC_CPU(cs);
1888 CPUSPARCState *env = &cpu->env;
1889
1890#ifdef DEBUG_UNASSIGNED
1891 printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
1892 "\n", addr, env->pc);
1893#endif
1894
1895 if (is_exec) {
1896 if (env->lsu & (IMMU_E)) {
1897 cpu_raise_exception_ra(env, TT_CODE_ACCESS, GETPC());
1898 } else if (cpu_has_hypervisor(env) && !(env->hpstate & HS_PRIV)) {
1899 cpu_raise_exception_ra(env, TT_INSN_REAL_TRANSLATION_MISS, GETPC());
1900 }
1901 } else {
1902 if (env->lsu & (DMMU_E)) {
1903 cpu_raise_exception_ra(env, TT_DATA_ACCESS, GETPC());
1904 } else if (cpu_has_hypervisor(env) && !(env->hpstate & HS_PRIV)) {
1905 cpu_raise_exception_ra(env, TT_DATA_REAL_TRANSLATION_MISS, GETPC());
1906 }
1907 }
1908}
1909#endif
1910#endif
1911
1912#if !defined(CONFIG_USER_ONLY)
1913void QEMU_NORETURN sparc_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
1914 MMUAccessType access_type,
1915 int mmu_idx,
1916 uintptr_t retaddr)
1917{
1918 SPARCCPU *cpu = SPARC_CPU(cs);
1919 CPUSPARCState *env = &cpu->env;
1920
1921#ifdef DEBUG_UNALIGNED
1922 printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
1923 "\n", addr, env->pc);
1924#endif
1925 cpu_raise_exception_ra(env, TT_UNALIGNED, retaddr);
1926}
1927
1928
1929
1930
1931
1932void tlb_fill(CPUState *cs, target_ulong addr, MMUAccessType access_type,
1933 int mmu_idx, uintptr_t retaddr)
1934{
1935 int ret;
1936
1937 ret = sparc_cpu_handle_mmu_fault(cs, addr, access_type, mmu_idx);
1938 if (ret) {
1939 cpu_loop_exit_restore(cs, retaddr);
1940 }
1941}
1942#endif
1943