]> git.proxmox.com Git - qemu.git/blob - target-sparc/helper.c
sparc64: unify mmu tag matching code
[qemu.git] / target-sparc / helper.c
1 /*
2 * sparc helpers
3 *
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
19 */
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <signal.h>
26
27 #include "cpu.h"
28 #include "exec-all.h"
29 #include "qemu-common.h"
30
31 //#define DEBUG_MMU
32 //#define DEBUG_FEATURES
33
34 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model);
35
36 /* Sparc MMU emulation */
37
38 /* thread support */
39
40 static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
41
42 void cpu_lock(void)
43 {
44 spin_lock(&global_cpu_lock);
45 }
46
47 void cpu_unlock(void)
48 {
49 spin_unlock(&global_cpu_lock);
50 }
51
52 #if defined(CONFIG_USER_ONLY)
53
54 int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
55 int mmu_idx, int is_softmmu)
56 {
57 if (rw & 2)
58 env1->exception_index = TT_TFAULT;
59 else
60 env1->exception_index = TT_DFAULT;
61 return 1;
62 }
63
64 #else
65
66 #ifndef TARGET_SPARC64
67 /*
68 * Sparc V8 Reference MMU (SRMMU)
69 */
70 static const int access_table[8][8] = {
71 { 0, 0, 0, 0, 8, 0, 12, 12 },
72 { 0, 0, 0, 0, 8, 0, 0, 0 },
73 { 8, 8, 0, 0, 0, 8, 12, 12 },
74 { 8, 8, 0, 0, 0, 8, 0, 0 },
75 { 8, 0, 8, 0, 8, 8, 12, 12 },
76 { 8, 0, 8, 0, 8, 0, 8, 0 },
77 { 8, 8, 8, 0, 8, 8, 12, 12 },
78 { 8, 8, 8, 0, 8, 8, 8, 0 }
79 };
80
81 static const int perm_table[2][8] = {
82 {
83 PAGE_READ,
84 PAGE_READ | PAGE_WRITE,
85 PAGE_READ | PAGE_EXEC,
86 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
87 PAGE_EXEC,
88 PAGE_READ | PAGE_WRITE,
89 PAGE_READ | PAGE_EXEC,
90 PAGE_READ | PAGE_WRITE | PAGE_EXEC
91 },
92 {
93 PAGE_READ,
94 PAGE_READ | PAGE_WRITE,
95 PAGE_READ | PAGE_EXEC,
96 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
97 PAGE_EXEC,
98 PAGE_READ,
99 0,
100 0,
101 }
102 };
103
104 static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
105 int *prot, int *access_index,
106 target_ulong address, int rw, int mmu_idx)
107 {
108 int access_perms = 0;
109 target_phys_addr_t pde_ptr;
110 uint32_t pde;
111 target_ulong virt_addr;
112 int error_code = 0, is_dirty, is_user;
113 unsigned long page_offset;
114
115 is_user = mmu_idx == MMU_USER_IDX;
116 virt_addr = address & TARGET_PAGE_MASK;
117
118 if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
119 // Boot mode: instruction fetches are taken from PROM
120 if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
121 *physical = env->prom_addr | (address & 0x7ffffULL);
122 *prot = PAGE_READ | PAGE_EXEC;
123 return 0;
124 }
125 *physical = address;
126 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
127 return 0;
128 }
129
130 *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
131 *physical = 0xffffffffffff0000ULL;
132
133 /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
134 /* Context base + context number */
135 pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
136 pde = ldl_phys(pde_ptr);
137
138 /* Ctx pde */
139 switch (pde & PTE_ENTRYTYPE_MASK) {
140 default:
141 case 0: /* Invalid */
142 return 1 << 2;
143 case 2: /* L0 PTE, maybe should not happen? */
144 case 3: /* Reserved */
145 return 4 << 2;
146 case 1: /* L0 PDE */
147 pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
148 pde = ldl_phys(pde_ptr);
149
150 switch (pde & PTE_ENTRYTYPE_MASK) {
151 default:
152 case 0: /* Invalid */
153 return (1 << 8) | (1 << 2);
154 case 3: /* Reserved */
155 return (1 << 8) | (4 << 2);
156 case 1: /* L1 PDE */
157 pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
158 pde = ldl_phys(pde_ptr);
159
160 switch (pde & PTE_ENTRYTYPE_MASK) {
161 default:
162 case 0: /* Invalid */
163 return (2 << 8) | (1 << 2);
164 case 3: /* Reserved */
165 return (2 << 8) | (4 << 2);
166 case 1: /* L2 PDE */
167 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
168 pde = ldl_phys(pde_ptr);
169
170 switch (pde & PTE_ENTRYTYPE_MASK) {
171 default:
172 case 0: /* Invalid */
173 return (3 << 8) | (1 << 2);
174 case 1: /* PDE, should not happen */
175 case 3: /* Reserved */
176 return (3 << 8) | (4 << 2);
177 case 2: /* L3 PTE */
178 virt_addr = address & TARGET_PAGE_MASK;
179 page_offset = (address & TARGET_PAGE_MASK) &
180 (TARGET_PAGE_SIZE - 1);
181 }
182 break;
183 case 2: /* L2 PTE */
184 virt_addr = address & ~0x3ffff;
185 page_offset = address & 0x3ffff;
186 }
187 break;
188 case 2: /* L1 PTE */
189 virt_addr = address & ~0xffffff;
190 page_offset = address & 0xffffff;
191 }
192 }
193
194 /* update page modified and dirty bits */
195 is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
196 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
197 pde |= PG_ACCESSED_MASK;
198 if (is_dirty)
199 pde |= PG_MODIFIED_MASK;
200 stl_phys_notdirty(pde_ptr, pde);
201 }
202 /* check access */
203 access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
204 error_code = access_table[*access_index][access_perms];
205 if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
206 return error_code;
207
208 /* the page can be put in the TLB */
209 *prot = perm_table[is_user][access_perms];
210 if (!(pde & PG_MODIFIED_MASK)) {
211 /* only set write access if already dirty... otherwise wait
212 for dirty access */
213 *prot &= ~PAGE_WRITE;
214 }
215
216 /* Even if large ptes, we map only one 4KB page in the cache to
217 avoid filling it too fast */
218 *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
219 return error_code;
220 }
221
222 /* Perform address translation */
223 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
224 int mmu_idx, int is_softmmu)
225 {
226 target_phys_addr_t paddr;
227 target_ulong vaddr;
228 int error_code = 0, prot, ret = 0, access_index;
229
230 error_code = get_physical_address(env, &paddr, &prot, &access_index,
231 address, rw, mmu_idx);
232 if (error_code == 0) {
233 vaddr = address & TARGET_PAGE_MASK;
234 paddr &= TARGET_PAGE_MASK;
235 #ifdef DEBUG_MMU
236 printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
237 TARGET_FMT_lx "\n", address, paddr, vaddr);
238 #endif
239 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
240 return ret;
241 }
242
243 if (env->mmuregs[3]) /* Fault status register */
244 env->mmuregs[3] = 1; /* overflow (not read before another fault) */
245 env->mmuregs[3] |= (access_index << 5) | error_code | 2;
246 env->mmuregs[4] = address; /* Fault address register */
247
248 if ((env->mmuregs[0] & MMU_NF) || env->psret == 0) {
249 // No fault mode: if a mapping is available, just override
250 // permissions. If no mapping is available, redirect accesses to
251 // neverland. Fake/overridden mappings will be flushed when
252 // switching to normal mode.
253 vaddr = address & TARGET_PAGE_MASK;
254 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
255 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
256 return ret;
257 } else {
258 if (rw & 2)
259 env->exception_index = TT_TFAULT;
260 else
261 env->exception_index = TT_DFAULT;
262 return 1;
263 }
264 }
265
266 target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
267 {
268 target_phys_addr_t pde_ptr;
269 uint32_t pde;
270
271 /* Context base + context number */
272 pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
273 (env->mmuregs[2] << 2);
274 pde = ldl_phys(pde_ptr);
275
276 switch (pde & PTE_ENTRYTYPE_MASK) {
277 default:
278 case 0: /* Invalid */
279 case 2: /* PTE, maybe should not happen? */
280 case 3: /* Reserved */
281 return 0;
282 case 1: /* L1 PDE */
283 if (mmulev == 3)
284 return pde;
285 pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
286 pde = ldl_phys(pde_ptr);
287
288 switch (pde & PTE_ENTRYTYPE_MASK) {
289 default:
290 case 0: /* Invalid */
291 case 3: /* Reserved */
292 return 0;
293 case 2: /* L1 PTE */
294 return pde;
295 case 1: /* L2 PDE */
296 if (mmulev == 2)
297 return pde;
298 pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
299 pde = ldl_phys(pde_ptr);
300
301 switch (pde & PTE_ENTRYTYPE_MASK) {
302 default:
303 case 0: /* Invalid */
304 case 3: /* Reserved */
305 return 0;
306 case 2: /* L2 PTE */
307 return pde;
308 case 1: /* L3 PDE */
309 if (mmulev == 1)
310 return pde;
311 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
312 pde = ldl_phys(pde_ptr);
313
314 switch (pde & PTE_ENTRYTYPE_MASK) {
315 default:
316 case 0: /* Invalid */
317 case 1: /* PDE, should not happen */
318 case 3: /* Reserved */
319 return 0;
320 case 2: /* L3 PTE */
321 return pde;
322 }
323 }
324 }
325 }
326 return 0;
327 }
328
329 #ifdef DEBUG_MMU
330 void dump_mmu(CPUState *env)
331 {
332 target_ulong va, va1, va2;
333 unsigned int n, m, o;
334 target_phys_addr_t pde_ptr, pa;
335 uint32_t pde;
336
337 printf("MMU dump:\n");
338 pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
339 pde = ldl_phys(pde_ptr);
340 printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
341 (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
342 for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
343 pde = mmu_probe(env, va, 2);
344 if (pde) {
345 pa = cpu_get_phys_page_debug(env, va);
346 printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
347 " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
348 for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
349 pde = mmu_probe(env, va1, 1);
350 if (pde) {
351 pa = cpu_get_phys_page_debug(env, va1);
352 printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
353 " PDE: " TARGET_FMT_lx "\n", va1, pa, pde);
354 for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
355 pde = mmu_probe(env, va2, 0);
356 if (pde) {
357 pa = cpu_get_phys_page_debug(env, va2);
358 printf(" VA: " TARGET_FMT_lx ", PA: "
359 TARGET_FMT_plx " PTE: " TARGET_FMT_lx "\n",
360 va2, pa, pde);
361 }
362 }
363 }
364 }
365 }
366 }
367 printf("MMU dump ends\n");
368 }
369 #endif /* DEBUG_MMU */
370
371 #else /* !TARGET_SPARC64 */
372
373 // 41 bit physical address space
374 static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
375 {
376 return x & 0x1ffffffffffULL;
377 }
378
379 /*
380 * UltraSparc IIi I/DMMUs
381 */
382
383 static inline int compare_masked(uint64_t x, uint64_t y, uint64_t mask)
384 {
385 return (x & mask) == (y & mask);
386 }
387
388 // Returns true if TTE tag is valid and matches virtual address value in context
389 // requires virtual address mask value calculated from TTE entry size
390 static inline int ultrasparc_tag_match(uint64_t tlb_tag, uint64_t tlb_tte,
391 uint64_t address, uint64_t context,
392 target_phys_addr_t *physical)
393 {
394 uint64_t mask;
395
396 switch ((tlb_tte >> 61) & 3) {
397 default:
398 case 0x0: // 8k
399 mask = 0xffffffffffffe000ULL;
400 break;
401 case 0x1: // 64k
402 mask = 0xffffffffffff0000ULL;
403 break;
404 case 0x2: // 512k
405 mask = 0xfffffffffff80000ULL;
406 break;
407 case 0x3: // 4M
408 mask = 0xffffffffffc00000ULL;
409 break;
410 }
411
412 // valid, context match, virtual address match?
413 if ((tlb_tte & 0x8000000000000000ULL) &&
414 compare_masked(context, tlb_tag, 0x1fff) &&
415 compare_masked(address, tlb_tag, mask))
416 {
417 // decode physical address
418 *physical = ((tlb_tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
419 return 1;
420 }
421
422 return 0;
423 }
424
425 static int get_physical_address_data(CPUState *env,
426 target_phys_addr_t *physical, int *prot,
427 target_ulong address, int rw, int is_user)
428 {
429 unsigned int i;
430 uint64_t context;
431
432 if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
433 *physical = ultrasparc_truncate_physical(address);
434 *prot = PAGE_READ | PAGE_WRITE;
435 return 0;
436 }
437
438 context = env->dmmuregs[1] & 0x1fff;
439
440 for (i = 0; i < 64; i++) {
441 // ctx match, vaddr match, valid?
442 if (ultrasparc_tag_match(env->dtlb_tag[i], env->dtlb_tte[i],
443 address, context, physical)
444 ) {
445 // access ok?
446 if (((env->dtlb_tte[i] & 0x4) && is_user) ||
447 (!(env->dtlb_tte[i] & 0x2) && (rw == 1))) {
448 if (env->dmmuregs[3]) /* Fault status register */
449 env->dmmuregs[3] = 2; /* overflow (not read before
450 another fault) */
451 env->dmmuregs[3] |= (is_user << 3) | ((rw == 1) << 2) | 1;
452 env->dmmuregs[4] = address; /* Fault address register */
453 env->exception_index = TT_DFAULT;
454 #ifdef DEBUG_MMU
455 printf("DFAULT at 0x%" PRIx64 "\n", address);
456 #endif
457 return 1;
458 }
459 *prot = PAGE_READ;
460 if (env->dtlb_tte[i] & 0x2)
461 *prot |= PAGE_WRITE;
462 return 0;
463 }
464 }
465 #ifdef DEBUG_MMU
466 printf("DMISS at 0x%" PRIx64 "\n", address);
467 #endif
468 env->dmmuregs[6] = (address & ~0x1fffULL) | context;
469 env->exception_index = TT_DMISS;
470 return 1;
471 }
472
473 static int get_physical_address_code(CPUState *env,
474 target_phys_addr_t *physical, int *prot,
475 target_ulong address, int is_user)
476 {
477 unsigned int i;
478 uint64_t context;
479
480 if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
481 /* IMMU disabled */
482 *physical = ultrasparc_truncate_physical(address);
483 *prot = PAGE_EXEC;
484 return 0;
485 }
486
487 context = env->dmmuregs[1] & 0x1fff;
488
489 for (i = 0; i < 64; i++) {
490 // ctx match, vaddr match, valid?
491 if (ultrasparc_tag_match(env->itlb_tag[i], env->itlb_tte[i],
492 address, context, physical)
493 ) {
494 // access ok?
495 if ((env->itlb_tte[i] & 0x4) && is_user) {
496 if (env->immuregs[3]) /* Fault status register */
497 env->immuregs[3] = 2; /* overflow (not read before
498 another fault) */
499 env->immuregs[3] |= (is_user << 3) | 1;
500 env->exception_index = TT_TFAULT;
501 #ifdef DEBUG_MMU
502 printf("TFAULT at 0x%" PRIx64 "\n", address);
503 #endif
504 return 1;
505 }
506 *prot = PAGE_EXEC;
507 return 0;
508 }
509 }
510 #ifdef DEBUG_MMU
511 printf("TMISS at 0x%" PRIx64 "\n", address);
512 #endif
513 /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
514 env->immuregs[6] = (address & ~0x1fffULL) | context;
515 env->exception_index = TT_TMISS;
516 return 1;
517 }
518
519 static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
520 int *prot, int *access_index,
521 target_ulong address, int rw, int mmu_idx)
522 {
523 int is_user = mmu_idx == MMU_USER_IDX;
524
525 if (rw == 2)
526 return get_physical_address_code(env, physical, prot, address,
527 is_user);
528 else
529 return get_physical_address_data(env, physical, prot, address, rw,
530 is_user);
531 }
532
533 /* Perform address translation */
534 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
535 int mmu_idx, int is_softmmu)
536 {
537 target_ulong virt_addr, vaddr;
538 target_phys_addr_t paddr;
539 int error_code = 0, prot, ret = 0, access_index;
540
541 error_code = get_physical_address(env, &paddr, &prot, &access_index,
542 address, rw, mmu_idx);
543 if (error_code == 0) {
544 virt_addr = address & TARGET_PAGE_MASK;
545 vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
546 (TARGET_PAGE_SIZE - 1));
547 #ifdef DEBUG_MMU
548 printf("Translate at 0x%" PRIx64 " -> 0x%" PRIx64 ", vaddr 0x%" PRIx64
549 "\n", address, paddr, vaddr);
550 #endif
551 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
552 return ret;
553 }
554 // XXX
555 return 1;
556 }
557
558 #ifdef DEBUG_MMU
559 void dump_mmu(CPUState *env)
560 {
561 unsigned int i;
562 const char *mask;
563
564 printf("MMU contexts: Primary: %" PRId64 ", Secondary: %" PRId64 "\n",
565 env->dmmuregs[1], env->dmmuregs[2]);
566 if ((env->lsu & DMMU_E) == 0) {
567 printf("DMMU disabled\n");
568 } else {
569 printf("DMMU dump:\n");
570 for (i = 0; i < 64; i++) {
571 switch ((env->dtlb_tte[i] >> 61) & 3) {
572 default:
573 case 0x0:
574 mask = " 8k";
575 break;
576 case 0x1:
577 mask = " 64k";
578 break;
579 case 0x2:
580 mask = "512k";
581 break;
582 case 0x3:
583 mask = " 4M";
584 break;
585 }
586 if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0) {
587 printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx
588 ", %s, %s, %s, %s, ctx %" PRId64 "\n",
589 env->dtlb_tag[i] & ~0x1fffULL,
590 env->dtlb_tte[i] & 0x1ffffffe000ULL,
591 mask,
592 env->dtlb_tte[i] & 0x4? "priv": "user",
593 env->dtlb_tte[i] & 0x2? "RW": "RO",
594 env->dtlb_tte[i] & 0x40? "locked": "unlocked",
595 env->dtlb_tag[i] & 0x1fffULL);
596 }
597 }
598 }
599 if ((env->lsu & IMMU_E) == 0) {
600 printf("IMMU disabled\n");
601 } else {
602 printf("IMMU dump:\n");
603 for (i = 0; i < 64; i++) {
604 switch ((env->itlb_tte[i] >> 61) & 3) {
605 default:
606 case 0x0:
607 mask = " 8k";
608 break;
609 case 0x1:
610 mask = " 64k";
611 break;
612 case 0x2:
613 mask = "512k";
614 break;
615 case 0x3:
616 mask = " 4M";
617 break;
618 }
619 if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0) {
620 printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx
621 ", %s, %s, %s, ctx %" PRId64 "\n",
622 env->itlb_tag[i] & ~0x1fffULL,
623 env->itlb_tte[i] & 0x1ffffffe000ULL,
624 mask,
625 env->itlb_tte[i] & 0x4? "priv": "user",
626 env->itlb_tte[i] & 0x40? "locked": "unlocked",
627 env->itlb_tag[i] & 0x1fffULL);
628 }
629 }
630 }
631 }
632 #endif /* DEBUG_MMU */
633
634 #endif /* TARGET_SPARC64 */
635 #endif /* !CONFIG_USER_ONLY */
636
637
638 #if defined(CONFIG_USER_ONLY)
639 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
640 {
641 return addr;
642 }
643
644 #else
645 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
646 {
647 target_phys_addr_t phys_addr;
648 int prot, access_index;
649
650 if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
651 MMU_KERNEL_IDX) != 0)
652 if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
653 0, MMU_KERNEL_IDX) != 0)
654 return -1;
655 if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
656 return -1;
657 return phys_addr;
658 }
659 #endif
660
661 void cpu_reset(CPUSPARCState *env)
662 {
663 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
664 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
665 log_cpu_state(env, 0);
666 }
667
668 tlb_flush(env, 1);
669 env->cwp = 0;
670 env->wim = 1;
671 env->regwptr = env->regbase + (env->cwp * 16);
672 #if defined(CONFIG_USER_ONLY)
673 #ifdef TARGET_SPARC64
674 env->cleanwin = env->nwindows - 2;
675 env->cansave = env->nwindows - 2;
676 env->pstate = PS_RMO | PS_PEF | PS_IE;
677 env->asi = 0x82; // Primary no-fault
678 #endif
679 #else
680 env->psret = 0;
681 env->psrs = 1;
682 env->psrps = 1;
683 CC_OP = CC_OP_FLAGS;
684 #ifdef TARGET_SPARC64
685 env->pstate = PS_PRIV;
686 env->hpstate = HS_PRIV;
687 env->tsptr = &env->ts[env->tl & MAXTL_MASK];
688 env->lsu = 0;
689 #else
690 env->mmuregs[0] &= ~(MMU_E | MMU_NF);
691 env->mmuregs[0] |= env->def->mmu_bm;
692 #endif
693 env->pc = 0;
694 env->npc = env->pc + 4;
695 #endif
696 }
697
698 static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
699 {
700 sparc_def_t def1, *def = &def1;
701
702 if (cpu_sparc_find_by_name(def, cpu_model) < 0)
703 return -1;
704
705 env->def = qemu_mallocz(sizeof(*def));
706 memcpy(env->def, def, sizeof(*def));
707 #if defined(CONFIG_USER_ONLY)
708 if ((env->def->features & CPU_FEATURE_FLOAT))
709 env->def->features |= CPU_FEATURE_FLOAT128;
710 #endif
711 env->cpu_model_str = cpu_model;
712 env->version = def->iu_version;
713 env->fsr = def->fpu_version;
714 env->nwindows = def->nwindows;
715 #if !defined(TARGET_SPARC64)
716 env->mmuregs[0] |= def->mmu_version;
717 cpu_sparc_set_id(env, 0);
718 env->mxccregs[7] |= def->mxcc_version;
719 #else
720 env->mmu_version = def->mmu_version;
721 env->maxtl = def->maxtl;
722 env->version |= def->maxtl << 8;
723 env->version |= def->nwindows - 1;
724 #endif
725 return 0;
726 }
727
728 static void cpu_sparc_close(CPUSPARCState *env)
729 {
730 free(env->def);
731 free(env);
732 }
733
734 CPUSPARCState *cpu_sparc_init(const char *cpu_model)
735 {
736 CPUSPARCState *env;
737
738 env = qemu_mallocz(sizeof(CPUSPARCState));
739 cpu_exec_init(env);
740
741 gen_intermediate_code_init(env);
742
743 if (cpu_sparc_register(env, cpu_model) < 0) {
744 cpu_sparc_close(env);
745 return NULL;
746 }
747 cpu_reset(env);
748 qemu_init_vcpu(env);
749
750 return env;
751 }
752
753 void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu)
754 {
755 #if !defined(TARGET_SPARC64)
756 env->mxccregs[7] = ((cpu + 8) & 0xf) << 24;
757 #endif
758 }
759
760 static const sparc_def_t sparc_defs[] = {
761 #ifdef TARGET_SPARC64
762 {
763 .name = "Fujitsu Sparc64",
764 .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
765 .fpu_version = 0x00000000,
766 .mmu_version = mmu_us_12,
767 .nwindows = 4,
768 .maxtl = 4,
769 .features = CPU_DEFAULT_FEATURES,
770 },
771 {
772 .name = "Fujitsu Sparc64 III",
773 .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
774 .fpu_version = 0x00000000,
775 .mmu_version = mmu_us_12,
776 .nwindows = 5,
777 .maxtl = 4,
778 .features = CPU_DEFAULT_FEATURES,
779 },
780 {
781 .name = "Fujitsu Sparc64 IV",
782 .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
783 .fpu_version = 0x00000000,
784 .mmu_version = mmu_us_12,
785 .nwindows = 8,
786 .maxtl = 5,
787 .features = CPU_DEFAULT_FEATURES,
788 },
789 {
790 .name = "Fujitsu Sparc64 V",
791 .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
792 .fpu_version = 0x00000000,
793 .mmu_version = mmu_us_12,
794 .nwindows = 8,
795 .maxtl = 5,
796 .features = CPU_DEFAULT_FEATURES,
797 },
798 {
799 .name = "TI UltraSparc I",
800 .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
801 .fpu_version = 0x00000000,
802 .mmu_version = mmu_us_12,
803 .nwindows = 8,
804 .maxtl = 5,
805 .features = CPU_DEFAULT_FEATURES,
806 },
807 {
808 .name = "TI UltraSparc II",
809 .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
810 .fpu_version = 0x00000000,
811 .mmu_version = mmu_us_12,
812 .nwindows = 8,
813 .maxtl = 5,
814 .features = CPU_DEFAULT_FEATURES,
815 },
816 {
817 .name = "TI UltraSparc IIi",
818 .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
819 .fpu_version = 0x00000000,
820 .mmu_version = mmu_us_12,
821 .nwindows = 8,
822 .maxtl = 5,
823 .features = CPU_DEFAULT_FEATURES,
824 },
825 {
826 .name = "TI UltraSparc IIe",
827 .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
828 .fpu_version = 0x00000000,
829 .mmu_version = mmu_us_12,
830 .nwindows = 8,
831 .maxtl = 5,
832 .features = CPU_DEFAULT_FEATURES,
833 },
834 {
835 .name = "Sun UltraSparc III",
836 .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
837 .fpu_version = 0x00000000,
838 .mmu_version = mmu_us_12,
839 .nwindows = 8,
840 .maxtl = 5,
841 .features = CPU_DEFAULT_FEATURES,
842 },
843 {
844 .name = "Sun UltraSparc III Cu",
845 .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
846 .fpu_version = 0x00000000,
847 .mmu_version = mmu_us_3,
848 .nwindows = 8,
849 .maxtl = 5,
850 .features = CPU_DEFAULT_FEATURES,
851 },
852 {
853 .name = "Sun UltraSparc IIIi",
854 .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
855 .fpu_version = 0x00000000,
856 .mmu_version = mmu_us_12,
857 .nwindows = 8,
858 .maxtl = 5,
859 .features = CPU_DEFAULT_FEATURES,
860 },
861 {
862 .name = "Sun UltraSparc IV",
863 .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
864 .fpu_version = 0x00000000,
865 .mmu_version = mmu_us_4,
866 .nwindows = 8,
867 .maxtl = 5,
868 .features = CPU_DEFAULT_FEATURES,
869 },
870 {
871 .name = "Sun UltraSparc IV+",
872 .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
873 .fpu_version = 0x00000000,
874 .mmu_version = mmu_us_12,
875 .nwindows = 8,
876 .maxtl = 5,
877 .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
878 },
879 {
880 .name = "Sun UltraSparc IIIi+",
881 .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
882 .fpu_version = 0x00000000,
883 .mmu_version = mmu_us_3,
884 .nwindows = 8,
885 .maxtl = 5,
886 .features = CPU_DEFAULT_FEATURES,
887 },
888 {
889 .name = "Sun UltraSparc T1",
890 // defined in sparc_ifu_fdp.v and ctu.h
891 .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
892 .fpu_version = 0x00000000,
893 .mmu_version = mmu_sun4v,
894 .nwindows = 8,
895 .maxtl = 6,
896 .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
897 | CPU_FEATURE_GL,
898 },
899 {
900 .name = "Sun UltraSparc T2",
901 // defined in tlu_asi_ctl.v and n2_revid_cust.v
902 .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
903 .fpu_version = 0x00000000,
904 .mmu_version = mmu_sun4v,
905 .nwindows = 8,
906 .maxtl = 6,
907 .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
908 | CPU_FEATURE_GL,
909 },
910 {
911 .name = "NEC UltraSparc I",
912 .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
913 .fpu_version = 0x00000000,
914 .mmu_version = mmu_us_12,
915 .nwindows = 8,
916 .maxtl = 5,
917 .features = CPU_DEFAULT_FEATURES,
918 },
919 #else
920 {
921 .name = "Fujitsu MB86900",
922 .iu_version = 0x00 << 24, /* Impl 0, ver 0 */
923 .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
924 .mmu_version = 0x00 << 24, /* Impl 0, ver 0 */
925 .mmu_bm = 0x00004000,
926 .mmu_ctpr_mask = 0x007ffff0,
927 .mmu_cxr_mask = 0x0000003f,
928 .mmu_sfsr_mask = 0xffffffff,
929 .mmu_trcr_mask = 0xffffffff,
930 .nwindows = 7,
931 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_FSMULD,
932 },
933 {
934 .name = "Fujitsu MB86904",
935 .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
936 .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
937 .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
938 .mmu_bm = 0x00004000,
939 .mmu_ctpr_mask = 0x00ffffc0,
940 .mmu_cxr_mask = 0x000000ff,
941 .mmu_sfsr_mask = 0x00016fff,
942 .mmu_trcr_mask = 0x00ffffff,
943 .nwindows = 8,
944 .features = CPU_DEFAULT_FEATURES,
945 },
946 {
947 .name = "Fujitsu MB86907",
948 .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
949 .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
950 .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
951 .mmu_bm = 0x00004000,
952 .mmu_ctpr_mask = 0xffffffc0,
953 .mmu_cxr_mask = 0x000000ff,
954 .mmu_sfsr_mask = 0x00016fff,
955 .mmu_trcr_mask = 0xffffffff,
956 .nwindows = 8,
957 .features = CPU_DEFAULT_FEATURES,
958 },
959 {
960 .name = "LSI L64811",
961 .iu_version = 0x10 << 24, /* Impl 1, ver 0 */
962 .fpu_version = 1 << 17, /* FPU version 1 (LSI L64814) */
963 .mmu_version = 0x10 << 24,
964 .mmu_bm = 0x00004000,
965 .mmu_ctpr_mask = 0x007ffff0,
966 .mmu_cxr_mask = 0x0000003f,
967 .mmu_sfsr_mask = 0xffffffff,
968 .mmu_trcr_mask = 0xffffffff,
969 .nwindows = 8,
970 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
971 CPU_FEATURE_FSMULD,
972 },
973 {
974 .name = "Cypress CY7C601",
975 .iu_version = 0x11 << 24, /* Impl 1, ver 1 */
976 .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
977 .mmu_version = 0x10 << 24,
978 .mmu_bm = 0x00004000,
979 .mmu_ctpr_mask = 0x007ffff0,
980 .mmu_cxr_mask = 0x0000003f,
981 .mmu_sfsr_mask = 0xffffffff,
982 .mmu_trcr_mask = 0xffffffff,
983 .nwindows = 8,
984 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
985 CPU_FEATURE_FSMULD,
986 },
987 {
988 .name = "Cypress CY7C611",
989 .iu_version = 0x13 << 24, /* Impl 1, ver 3 */
990 .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
991 .mmu_version = 0x10 << 24,
992 .mmu_bm = 0x00004000,
993 .mmu_ctpr_mask = 0x007ffff0,
994 .mmu_cxr_mask = 0x0000003f,
995 .mmu_sfsr_mask = 0xffffffff,
996 .mmu_trcr_mask = 0xffffffff,
997 .nwindows = 8,
998 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
999 CPU_FEATURE_FSMULD,
1000 },
1001 {
1002 .name = "TI MicroSparc I",
1003 .iu_version = 0x41000000,
1004 .fpu_version = 4 << 17,
1005 .mmu_version = 0x41000000,
1006 .mmu_bm = 0x00004000,
1007 .mmu_ctpr_mask = 0x007ffff0,
1008 .mmu_cxr_mask = 0x0000003f,
1009 .mmu_sfsr_mask = 0x00016fff,
1010 .mmu_trcr_mask = 0x0000003f,
1011 .nwindows = 7,
1012 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
1013 CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
1014 CPU_FEATURE_FMUL,
1015 },
1016 {
1017 .name = "TI MicroSparc II",
1018 .iu_version = 0x42000000,
1019 .fpu_version = 4 << 17,
1020 .mmu_version = 0x02000000,
1021 .mmu_bm = 0x00004000,
1022 .mmu_ctpr_mask = 0x00ffffc0,
1023 .mmu_cxr_mask = 0x000000ff,
1024 .mmu_sfsr_mask = 0x00016fff,
1025 .mmu_trcr_mask = 0x00ffffff,
1026 .nwindows = 8,
1027 .features = CPU_DEFAULT_FEATURES,
1028 },
1029 {
1030 .name = "TI MicroSparc IIep",
1031 .iu_version = 0x42000000,
1032 .fpu_version = 4 << 17,
1033 .mmu_version = 0x04000000,
1034 .mmu_bm = 0x00004000,
1035 .mmu_ctpr_mask = 0x00ffffc0,
1036 .mmu_cxr_mask = 0x000000ff,
1037 .mmu_sfsr_mask = 0x00016bff,
1038 .mmu_trcr_mask = 0x00ffffff,
1039 .nwindows = 8,
1040 .features = CPU_DEFAULT_FEATURES,
1041 },
1042 {
1043 .name = "TI SuperSparc 40", // STP1020NPGA
1044 .iu_version = 0x41000000, // SuperSPARC 2.x
1045 .fpu_version = 0 << 17,
1046 .mmu_version = 0x00000800, // SuperSPARC 2.x, no MXCC
1047 .mmu_bm = 0x00002000,
1048 .mmu_ctpr_mask = 0xffffffc0,
1049 .mmu_cxr_mask = 0x0000ffff,
1050 .mmu_sfsr_mask = 0xffffffff,
1051 .mmu_trcr_mask = 0xffffffff,
1052 .nwindows = 8,
1053 .features = CPU_DEFAULT_FEATURES,
1054 },
1055 {
1056 .name = "TI SuperSparc 50", // STP1020PGA
1057 .iu_version = 0x40000000, // SuperSPARC 3.x
1058 .fpu_version = 0 << 17,
1059 .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1060 .mmu_bm = 0x00002000,
1061 .mmu_ctpr_mask = 0xffffffc0,
1062 .mmu_cxr_mask = 0x0000ffff,
1063 .mmu_sfsr_mask = 0xffffffff,
1064 .mmu_trcr_mask = 0xffffffff,
1065 .nwindows = 8,
1066 .features = CPU_DEFAULT_FEATURES,
1067 },
1068 {
1069 .name = "TI SuperSparc 51",
1070 .iu_version = 0x40000000, // SuperSPARC 3.x
1071 .fpu_version = 0 << 17,
1072 .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1073 .mmu_bm = 0x00002000,
1074 .mmu_ctpr_mask = 0xffffffc0,
1075 .mmu_cxr_mask = 0x0000ffff,
1076 .mmu_sfsr_mask = 0xffffffff,
1077 .mmu_trcr_mask = 0xffffffff,
1078 .mxcc_version = 0x00000104,
1079 .nwindows = 8,
1080 .features = CPU_DEFAULT_FEATURES,
1081 },
1082 {
1083 .name = "TI SuperSparc 60", // STP1020APGA
1084 .iu_version = 0x40000000, // SuperSPARC 3.x
1085 .fpu_version = 0 << 17,
1086 .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1087 .mmu_bm = 0x00002000,
1088 .mmu_ctpr_mask = 0xffffffc0,
1089 .mmu_cxr_mask = 0x0000ffff,
1090 .mmu_sfsr_mask = 0xffffffff,
1091 .mmu_trcr_mask = 0xffffffff,
1092 .nwindows = 8,
1093 .features = CPU_DEFAULT_FEATURES,
1094 },
1095 {
1096 .name = "TI SuperSparc 61",
1097 .iu_version = 0x44000000, // SuperSPARC 3.x
1098 .fpu_version = 0 << 17,
1099 .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1100 .mmu_bm = 0x00002000,
1101 .mmu_ctpr_mask = 0xffffffc0,
1102 .mmu_cxr_mask = 0x0000ffff,
1103 .mmu_sfsr_mask = 0xffffffff,
1104 .mmu_trcr_mask = 0xffffffff,
1105 .mxcc_version = 0x00000104,
1106 .nwindows = 8,
1107 .features = CPU_DEFAULT_FEATURES,
1108 },
1109 {
1110 .name = "TI SuperSparc II",
1111 .iu_version = 0x40000000, // SuperSPARC II 1.x
1112 .fpu_version = 0 << 17,
1113 .mmu_version = 0x08000000, // SuperSPARC II 1.x, MXCC
1114 .mmu_bm = 0x00002000,
1115 .mmu_ctpr_mask = 0xffffffc0,
1116 .mmu_cxr_mask = 0x0000ffff,
1117 .mmu_sfsr_mask = 0xffffffff,
1118 .mmu_trcr_mask = 0xffffffff,
1119 .mxcc_version = 0x00000104,
1120 .nwindows = 8,
1121 .features = CPU_DEFAULT_FEATURES,
1122 },
1123 {
1124 .name = "Ross RT625",
1125 .iu_version = 0x1e000000,
1126 .fpu_version = 1 << 17,
1127 .mmu_version = 0x1e000000,
1128 .mmu_bm = 0x00004000,
1129 .mmu_ctpr_mask = 0x007ffff0,
1130 .mmu_cxr_mask = 0x0000003f,
1131 .mmu_sfsr_mask = 0xffffffff,
1132 .mmu_trcr_mask = 0xffffffff,
1133 .nwindows = 8,
1134 .features = CPU_DEFAULT_FEATURES,
1135 },
1136 {
1137 .name = "Ross RT620",
1138 .iu_version = 0x1f000000,
1139 .fpu_version = 1 << 17,
1140 .mmu_version = 0x1f000000,
1141 .mmu_bm = 0x00004000,
1142 .mmu_ctpr_mask = 0x007ffff0,
1143 .mmu_cxr_mask = 0x0000003f,
1144 .mmu_sfsr_mask = 0xffffffff,
1145 .mmu_trcr_mask = 0xffffffff,
1146 .nwindows = 8,
1147 .features = CPU_DEFAULT_FEATURES,
1148 },
1149 {
1150 .name = "BIT B5010",
1151 .iu_version = 0x20000000,
1152 .fpu_version = 0 << 17, /* B5010/B5110/B5120/B5210 */
1153 .mmu_version = 0x20000000,
1154 .mmu_bm = 0x00004000,
1155 .mmu_ctpr_mask = 0x007ffff0,
1156 .mmu_cxr_mask = 0x0000003f,
1157 .mmu_sfsr_mask = 0xffffffff,
1158 .mmu_trcr_mask = 0xffffffff,
1159 .nwindows = 8,
1160 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1161 CPU_FEATURE_FSMULD,
1162 },
1163 {
1164 .name = "Matsushita MN10501",
1165 .iu_version = 0x50000000,
1166 .fpu_version = 0 << 17,
1167 .mmu_version = 0x50000000,
1168 .mmu_bm = 0x00004000,
1169 .mmu_ctpr_mask = 0x007ffff0,
1170 .mmu_cxr_mask = 0x0000003f,
1171 .mmu_sfsr_mask = 0xffffffff,
1172 .mmu_trcr_mask = 0xffffffff,
1173 .nwindows = 8,
1174 .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT |
1175 CPU_FEATURE_FSMULD,
1176 },
1177 {
1178 .name = "Weitek W8601",
1179 .iu_version = 0x90 << 24, /* Impl 9, ver 0 */
1180 .fpu_version = 3 << 17, /* FPU version 3 (Weitek WTL3170/2) */
1181 .mmu_version = 0x10 << 24,
1182 .mmu_bm = 0x00004000,
1183 .mmu_ctpr_mask = 0x007ffff0,
1184 .mmu_cxr_mask = 0x0000003f,
1185 .mmu_sfsr_mask = 0xffffffff,
1186 .mmu_trcr_mask = 0xffffffff,
1187 .nwindows = 8,
1188 .features = CPU_DEFAULT_FEATURES,
1189 },
1190 {
1191 .name = "LEON2",
1192 .iu_version = 0xf2000000,
1193 .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1194 .mmu_version = 0xf2000000,
1195 .mmu_bm = 0x00004000,
1196 .mmu_ctpr_mask = 0x007ffff0,
1197 .mmu_cxr_mask = 0x0000003f,
1198 .mmu_sfsr_mask = 0xffffffff,
1199 .mmu_trcr_mask = 0xffffffff,
1200 .nwindows = 8,
1201 .features = CPU_DEFAULT_FEATURES,
1202 },
1203 {
1204 .name = "LEON3",
1205 .iu_version = 0xf3000000,
1206 .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1207 .mmu_version = 0xf3000000,
1208 .mmu_bm = 0x00004000,
1209 .mmu_ctpr_mask = 0x007ffff0,
1210 .mmu_cxr_mask = 0x0000003f,
1211 .mmu_sfsr_mask = 0xffffffff,
1212 .mmu_trcr_mask = 0xffffffff,
1213 .nwindows = 8,
1214 .features = CPU_DEFAULT_FEATURES,
1215 },
1216 #endif
1217 };
1218
1219 static const char * const feature_name[] = {
1220 "float",
1221 "float128",
1222 "swap",
1223 "mul",
1224 "div",
1225 "flush",
1226 "fsqrt",
1227 "fmul",
1228 "vis1",
1229 "vis2",
1230 "fsmuld",
1231 "hypv",
1232 "cmt",
1233 "gl",
1234 };
1235
1236 static void print_features(FILE *f,
1237 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1238 uint32_t features, const char *prefix)
1239 {
1240 unsigned int i;
1241
1242 for (i = 0; i < ARRAY_SIZE(feature_name); i++)
1243 if (feature_name[i] && (features & (1 << i))) {
1244 if (prefix)
1245 (*cpu_fprintf)(f, "%s", prefix);
1246 (*cpu_fprintf)(f, "%s ", feature_name[i]);
1247 }
1248 }
1249
1250 static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features)
1251 {
1252 unsigned int i;
1253
1254 for (i = 0; i < ARRAY_SIZE(feature_name); i++)
1255 if (feature_name[i] && !strcmp(flagname, feature_name[i])) {
1256 *features |= 1 << i;
1257 return;
1258 }
1259 fprintf(stderr, "CPU feature %s not found\n", flagname);
1260 }
1261
1262 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
1263 {
1264 unsigned int i;
1265 const sparc_def_t *def = NULL;
1266 char *s = strdup(cpu_model);
1267 char *featurestr, *name = strtok(s, ",");
1268 uint32_t plus_features = 0;
1269 uint32_t minus_features = 0;
1270 long long iu_version;
1271 uint32_t fpu_version, mmu_version, nwindows;
1272
1273 for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1274 if (strcasecmp(name, sparc_defs[i].name) == 0) {
1275 def = &sparc_defs[i];
1276 }
1277 }
1278 if (!def)
1279 goto error;
1280 memcpy(cpu_def, def, sizeof(*def));
1281
1282 featurestr = strtok(NULL, ",");
1283 while (featurestr) {
1284 char *val;
1285
1286 if (featurestr[0] == '+') {
1287 add_flagname_to_bitmaps(featurestr + 1, &plus_features);
1288 } else if (featurestr[0] == '-') {
1289 add_flagname_to_bitmaps(featurestr + 1, &minus_features);
1290 } else if ((val = strchr(featurestr, '='))) {
1291 *val = 0; val++;
1292 if (!strcmp(featurestr, "iu_version")) {
1293 char *err;
1294
1295 iu_version = strtoll(val, &err, 0);
1296 if (!*val || *err) {
1297 fprintf(stderr, "bad numerical value %s\n", val);
1298 goto error;
1299 }
1300 cpu_def->iu_version = iu_version;
1301 #ifdef DEBUG_FEATURES
1302 fprintf(stderr, "iu_version %llx\n", iu_version);
1303 #endif
1304 } else if (!strcmp(featurestr, "fpu_version")) {
1305 char *err;
1306
1307 fpu_version = strtol(val, &err, 0);
1308 if (!*val || *err) {
1309 fprintf(stderr, "bad numerical value %s\n", val);
1310 goto error;
1311 }
1312 cpu_def->fpu_version = fpu_version;
1313 #ifdef DEBUG_FEATURES
1314 fprintf(stderr, "fpu_version %llx\n", fpu_version);
1315 #endif
1316 } else if (!strcmp(featurestr, "mmu_version")) {
1317 char *err;
1318
1319 mmu_version = strtol(val, &err, 0);
1320 if (!*val || *err) {
1321 fprintf(stderr, "bad numerical value %s\n", val);
1322 goto error;
1323 }
1324 cpu_def->mmu_version = mmu_version;
1325 #ifdef DEBUG_FEATURES
1326 fprintf(stderr, "mmu_version %llx\n", mmu_version);
1327 #endif
1328 } else if (!strcmp(featurestr, "nwindows")) {
1329 char *err;
1330
1331 nwindows = strtol(val, &err, 0);
1332 if (!*val || *err || nwindows > MAX_NWINDOWS ||
1333 nwindows < MIN_NWINDOWS) {
1334 fprintf(stderr, "bad numerical value %s\n", val);
1335 goto error;
1336 }
1337 cpu_def->nwindows = nwindows;
1338 #ifdef DEBUG_FEATURES
1339 fprintf(stderr, "nwindows %d\n", nwindows);
1340 #endif
1341 } else {
1342 fprintf(stderr, "unrecognized feature %s\n", featurestr);
1343 goto error;
1344 }
1345 } else {
1346 fprintf(stderr, "feature string `%s' not in format "
1347 "(+feature|-feature|feature=xyz)\n", featurestr);
1348 goto error;
1349 }
1350 featurestr = strtok(NULL, ",");
1351 }
1352 cpu_def->features |= plus_features;
1353 cpu_def->features &= ~minus_features;
1354 #ifdef DEBUG_FEATURES
1355 print_features(stderr, fprintf, cpu_def->features, NULL);
1356 #endif
1357 free(s);
1358 return 0;
1359
1360 error:
1361 free(s);
1362 return -1;
1363 }
1364
1365 void sparc_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
1366 {
1367 unsigned int i;
1368
1369 for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1370 (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1371 sparc_defs[i].name,
1372 sparc_defs[i].iu_version,
1373 sparc_defs[i].fpu_version,
1374 sparc_defs[i].mmu_version,
1375 sparc_defs[i].nwindows);
1376 print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
1377 ~sparc_defs[i].features, "-");
1378 print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
1379 sparc_defs[i].features, "+");
1380 (*cpu_fprintf)(f, "\n");
1381 }
1382 (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
1383 print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
1384 (*cpu_fprintf)(f, "\n");
1385 (*cpu_fprintf)(f, "Available CPU feature flags (use '+' to add): ");
1386 print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES, NULL);
1387 (*cpu_fprintf)(f, "\n");
1388 (*cpu_fprintf)(f, "Numerical features (use '=' to set): iu_version "
1389 "fpu_version mmu_version nwindows\n");
1390 }
1391
1392 void cpu_dump_state(CPUState *env, FILE *f,
1393 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1394 int flags)
1395 {
1396 int i, x;
1397
1398 cpu_fprintf(f, "pc: " TARGET_FMT_lx " npc: " TARGET_FMT_lx "\n", env->pc,
1399 env->npc);
1400 cpu_fprintf(f, "General Registers:\n");
1401 for (i = 0; i < 4; i++)
1402 cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
1403 cpu_fprintf(f, "\n");
1404 for (; i < 8; i++)
1405 cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
1406 cpu_fprintf(f, "\nCurrent Register Window:\n");
1407 for (x = 0; x < 3; x++) {
1408 for (i = 0; i < 4; i++)
1409 cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
1410 (x == 0 ? 'o' : (x == 1 ? 'l' : 'i')), i,
1411 env->regwptr[i + x * 8]);
1412 cpu_fprintf(f, "\n");
1413 for (; i < 8; i++)
1414 cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
1415 (x == 0 ? 'o' : x == 1 ? 'l' : 'i'), i,
1416 env->regwptr[i + x * 8]);
1417 cpu_fprintf(f, "\n");
1418 }
1419 cpu_fprintf(f, "\nFloating Point Registers:\n");
1420 for (i = 0; i < 32; i++) {
1421 if ((i & 3) == 0)
1422 cpu_fprintf(f, "%%f%02d:", i);
1423 cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1424 if ((i & 3) == 3)
1425 cpu_fprintf(f, "\n");
1426 }
1427 #ifdef TARGET_SPARC64
1428 cpu_fprintf(f, "pstate: 0x%08x ccr: 0x%02x asi: 0x%02x tl: %d fprs: %d\n",
1429 env->pstate, GET_CCR(env), env->asi, env->tl, env->fprs);
1430 cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate %d "
1431 "cleanwin %d cwp %d\n",
1432 env->cansave, env->canrestore, env->otherwin, env->wstate,
1433 env->cleanwin, env->nwindows - 1 - env->cwp);
1434 #else
1435
1436 #define GET_FLAG(a,b) ((env->psr & a)?b:'-')
1437
1438 cpu_fprintf(f, "psr: 0x%08x -> %c%c%c%c %c%c%c wim: 0x%08x\n",
1439 GET_PSR(env), GET_FLAG(PSR_ZERO, 'Z'), GET_FLAG(PSR_OVF, 'V'),
1440 GET_FLAG(PSR_NEG, 'N'), GET_FLAG(PSR_CARRY, 'C'),
1441 env->psrs?'S':'-', env->psrps?'P':'-',
1442 env->psret?'E':'-', env->wim);
1443 #endif
1444 cpu_fprintf(f, "fsr: 0x%08x\n", env->fsr);
1445 }