4 * Copyright (c) 2006-2007 CodeSourcery
5 * Written by Paul Brook
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "exec/exec-all.h"
24 #include "exec/gdbstub.h"
25 #include "exec/helper-proto.h"
26 #include "fpu/softfloat.h"
27 #include "qemu/qemu-print.h"
29 #define SIGNBIT (1u << 31)
31 /* Sort alphabetically, except for "any". */
32 static gint
m68k_cpu_list_compare(gconstpointer a
, gconstpointer b
)
34 ObjectClass
*class_a
= (ObjectClass
*)a
;
35 ObjectClass
*class_b
= (ObjectClass
*)b
;
36 const char *name_a
, *name_b
;
38 name_a
= object_class_get_name(class_a
);
39 name_b
= object_class_get_name(class_b
);
40 if (strcmp(name_a
, "any-" TYPE_M68K_CPU
) == 0) {
42 } else if (strcmp(name_b
, "any-" TYPE_M68K_CPU
) == 0) {
45 return strcasecmp(name_a
, name_b
);
49 static void m68k_cpu_list_entry(gpointer data
, gpointer user_data
)
51 ObjectClass
*c
= data
;
55 typename
= object_class_get_name(c
);
56 name
= g_strndup(typename
, strlen(typename
) - strlen("-" TYPE_M68K_CPU
));
57 qemu_printf("%s\n", name
);
61 void m68k_cpu_list(void)
65 list
= object_class_get_list(TYPE_M68K_CPU
, false);
66 list
= g_slist_sort(list
, m68k_cpu_list_compare
);
67 g_slist_foreach(list
, m68k_cpu_list_entry
, NULL
);
71 static int cf_fpu_gdb_get_reg(CPUM68KState
*env
, GByteArray
*mem_buf
, int n
)
75 return gdb_get_reg64(mem_buf
, floatx80_to_float64(env
->fregs
[n
].d
, &s
));
78 case 8: /* fpcontrol */
79 return gdb_get_reg32(mem_buf
, env
->fpcr
);
80 case 9: /* fpstatus */
81 return gdb_get_reg32(mem_buf
, env
->fpsr
);
82 case 10: /* fpiar, not implemented */
83 return gdb_get_reg32(mem_buf
, 0);
88 static int cf_fpu_gdb_set_reg(CPUM68KState
*env
, uint8_t *mem_buf
, int n
)
92 env
->fregs
[n
].d
= float64_to_floatx80(ldq_p(mem_buf
), &s
);
96 case 8: /* fpcontrol */
97 cpu_m68k_set_fpcr(env
, ldl_p(mem_buf
));
99 case 9: /* fpstatus */
100 env
->fpsr
= ldl_p(mem_buf
);
102 case 10: /* fpiar, not implemented */
108 static int m68k_fpu_gdb_get_reg(CPUM68KState
*env
, GByteArray
*mem_buf
, int n
)
111 int len
= gdb_get_reg16(mem_buf
, env
->fregs
[n
].l
.upper
);
112 len
+= gdb_get_reg16(mem_buf
, 0);
113 len
+= gdb_get_reg64(mem_buf
, env
->fregs
[n
].l
.lower
);
117 case 8: /* fpcontrol */
118 return gdb_get_reg32(mem_buf
, env
->fpcr
);
119 case 9: /* fpstatus */
120 return gdb_get_reg32(mem_buf
, env
->fpsr
);
121 case 10: /* fpiar, not implemented */
122 return gdb_get_reg32(mem_buf
, 0);
127 static int m68k_fpu_gdb_set_reg(CPUM68KState
*env
, uint8_t *mem_buf
, int n
)
130 env
->fregs
[n
].l
.upper
= lduw_be_p(mem_buf
);
131 env
->fregs
[n
].l
.lower
= ldq_be_p(mem_buf
+ 4);
135 case 8: /* fpcontrol */
136 cpu_m68k_set_fpcr(env
, ldl_p(mem_buf
));
138 case 9: /* fpstatus */
139 env
->fpsr
= ldl_p(mem_buf
);
141 case 10: /* fpiar, not implemented */
147 void m68k_cpu_init_gdb(M68kCPU
*cpu
)
149 CPUState
*cs
= CPU(cpu
);
150 CPUM68KState
*env
= &cpu
->env
;
152 if (m68k_feature(env
, M68K_FEATURE_CF_FPU
)) {
153 gdb_register_coprocessor(cs
, cf_fpu_gdb_get_reg
, cf_fpu_gdb_set_reg
,
154 11, "cf-fp.xml", 18);
155 } else if (m68k_feature(env
, M68K_FEATURE_FPU
)) {
156 gdb_register_coprocessor(cs
, m68k_fpu_gdb_get_reg
,
157 m68k_fpu_gdb_set_reg
, 11, "m68k-fp.xml", 18);
159 /* TODO: Add [E]MAC registers. */
162 void HELPER(cf_movec_to
)(CPUM68KState
*env
, uint32_t reg
, uint32_t val
)
173 /* TODO: Implement Access Control Registers. */
178 /* TODO: Implement control registers. */
180 cpu_abort(env_cpu(env
),
181 "Unimplemented control register write 0x%x = 0x%x\n",
186 static void raise_exception_ra(CPUM68KState
*env
, int tt
, uintptr_t raddr
)
188 CPUState
*cs
= env_cpu(env
);
190 cs
->exception_index
= tt
;
191 cpu_loop_exit_restore(cs
, raddr
);
194 void HELPER(m68k_movec_to
)(CPUM68KState
*env
, uint32_t reg
, uint32_t val
)
211 if (m68k_feature(env
, M68K_FEATURE_M68020
)) {
212 env
->cacr
= val
& 0x0000000f;
213 } else if (m68k_feature(env
, M68K_FEATURE_M68030
)) {
214 env
->cacr
= val
& 0x00003f1f;
215 } else if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
216 env
->cacr
= val
& 0x80008000;
217 } else if (m68k_feature(env
, M68K_FEATURE_M68060
)) {
218 env
->cacr
= val
& 0xf8e0e000;
226 if (m68k_feature(env
, M68K_FEATURE_M68040
)
227 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
234 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
235 env
->mmu
.mmusr
= val
;
241 if (m68k_feature(env
, M68K_FEATURE_M68040
)
242 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
249 if (m68k_feature(env
, M68K_FEATURE_M68040
)
250 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
257 env
->sp
[M68K_USP
] = val
;
261 if (m68k_feature(env
, M68K_FEATURE_M68020
)
262 || m68k_feature(env
, M68K_FEATURE_M68030
)
263 || m68k_feature(env
, M68K_FEATURE_M68040
)) {
264 env
->sp
[M68K_SSP
] = val
;
270 if (m68k_feature(env
, M68K_FEATURE_M68020
)
271 || m68k_feature(env
, M68K_FEATURE_M68030
)
272 || m68k_feature(env
, M68K_FEATURE_M68040
)) {
273 env
->sp
[M68K_ISP
] = val
;
277 /* MC68040/MC68LC040 */
278 case M68K_CR_ITT0
: /* MC68EC040 only: M68K_CR_IACR0 */
279 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
280 env
->mmu
.ttr
[M68K_ITTR0
] = val
;
284 /* MC68040/MC68LC040 */
285 case M68K_CR_ITT1
: /* MC68EC040 only: M68K_CR_IACR1 */
286 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
287 env
->mmu
.ttr
[M68K_ITTR1
] = val
;
291 /* MC68040/MC68LC040 */
292 case M68K_CR_DTT0
: /* MC68EC040 only: M68K_CR_DACR0 */
293 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
294 env
->mmu
.ttr
[M68K_DTTR0
] = val
;
298 /* MC68040/MC68LC040 */
299 case M68K_CR_DTT1
: /* MC68EC040 only: M68K_CR_DACR1 */
300 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
301 env
->mmu
.ttr
[M68K_DTTR1
] = val
;
305 /* Unimplemented Registers */
309 cpu_abort(env_cpu(env
),
310 "Unimplemented control register write 0x%x = 0x%x\n",
314 /* Invalid control registers will generate an exception. */
315 raise_exception_ra(env
, EXCP_ILLEGAL
, 0);
319 uint32_t HELPER(m68k_movec_from
)(CPUM68KState
*env
, uint32_t reg
)
333 if (m68k_feature(env
, M68K_FEATURE_M68020
)
334 || m68k_feature(env
, M68K_FEATURE_M68030
)
335 || m68k_feature(env
, M68K_FEATURE_M68040
)
336 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
342 if (m68k_feature(env
, M68K_FEATURE_M68040
)
343 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
349 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
350 return env
->mmu
.mmusr
;
355 if (m68k_feature(env
, M68K_FEATURE_M68040
)
356 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
360 /* MC68040/MC68LC040 */
362 if (m68k_feature(env
, M68K_FEATURE_M68040
)
363 || m68k_feature(env
, M68K_FEATURE_M68060
)) {
369 return env
->sp
[M68K_USP
];
372 if (m68k_feature(env
, M68K_FEATURE_M68020
)
373 || m68k_feature(env
, M68K_FEATURE_M68030
)
374 || m68k_feature(env
, M68K_FEATURE_M68040
)) {
375 return env
->sp
[M68K_SSP
];
380 if (m68k_feature(env
, M68K_FEATURE_M68020
)
381 || m68k_feature(env
, M68K_FEATURE_M68030
)
382 || m68k_feature(env
, M68K_FEATURE_M68040
)) {
383 return env
->sp
[M68K_ISP
];
386 /* MC68040/MC68LC040 */
387 case M68K_CR_ITT0
: /* MC68EC040 only: M68K_CR_IACR0 */
388 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
389 return env
->mmu
.ttr
[M68K_ITTR0
];
392 /* MC68040/MC68LC040 */
393 case M68K_CR_ITT1
: /* MC68EC040 only: M68K_CR_IACR1 */
394 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
395 return env
->mmu
.ttr
[M68K_ITTR1
];
398 /* MC68040/MC68LC040 */
399 case M68K_CR_DTT0
: /* MC68EC040 only: M68K_CR_DACR0 */
400 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
401 return env
->mmu
.ttr
[M68K_DTTR0
];
404 /* MC68040/MC68LC040 */
405 case M68K_CR_DTT1
: /* MC68EC040 only: M68K_CR_DACR1 */
406 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
407 return env
->mmu
.ttr
[M68K_DTTR1
];
410 /* Unimplemented Registers */
414 cpu_abort(env_cpu(env
), "Unimplemented control register read 0x%x\n",
418 /* Invalid control registers will generate an exception. */
419 raise_exception_ra(env
, EXCP_ILLEGAL
, 0);
424 void HELPER(set_macsr
)(CPUM68KState
*env
, uint32_t val
)
431 if ((env
->macsr
^ val
) & (MACSR_FI
| MACSR_SU
)) {
432 for (i
= 0; i
< 4; i
++) {
433 regval
= env
->macc
[i
];
434 exthigh
= regval
>> 40;
435 if (env
->macsr
& MACSR_FI
) {
440 extlow
= regval
>> 32;
442 if (env
->macsr
& MACSR_FI
) {
443 regval
= (((uint64_t)acc
) << 8) | extlow
;
444 regval
|= ((int64_t)exthigh
) << 40;
445 } else if (env
->macsr
& MACSR_SU
) {
446 regval
= acc
| (((int64_t)extlow
) << 32);
447 regval
|= ((int64_t)exthigh
) << 40;
449 regval
= acc
| (((uint64_t)extlow
) << 32);
450 regval
|= ((uint64_t)(uint8_t)exthigh
) << 40;
452 env
->macc
[i
] = regval
;
458 void m68k_switch_sp(CPUM68KState
*env
)
462 env
->sp
[env
->current_sp
] = env
->aregs
[7];
463 if (m68k_feature(env
, M68K_FEATURE_M68000
)) {
464 if (env
->sr
& SR_S
) {
465 /* SR:Master-Mode bit unimplemented then ISP is not available */
466 if (!m68k_feature(env
, M68K_FEATURE_MSP
) || env
->sr
& SR_M
) {
475 new_sp
= (env
->sr
& SR_S
&& env
->cacr
& M68K_CACR_EUSP
)
476 ? M68K_SSP
: M68K_USP
;
478 env
->aregs
[7] = env
->sp
[new_sp
];
479 env
->current_sp
= new_sp
;
482 #if !defined(CONFIG_USER_ONLY)
483 /* MMU: 68040 only */
485 static void print_address_zone(uint32_t logical
, uint32_t physical
,
486 uint32_t size
, int attr
)
488 qemu_printf("%08x - %08x -> %08x - %08x %c ",
489 logical
, logical
+ size
- 1,
490 physical
, physical
+ size
- 1,
491 attr
& 4 ? 'W' : '-');
494 qemu_printf("(%d KiB)\n", size
);
498 qemu_printf("(%d MiB)\n", size
);
501 qemu_printf("(%d GiB)\n", size
);
506 static void dump_address_map(CPUM68KState
*env
, uint32_t root_pointer
)
509 int tic_size
, tic_shift
;
511 uint32_t tia
, tib
, tic
;
512 uint32_t logical
= 0xffffffff, physical
= 0xffffffff;
513 uint32_t first_logical
= 0xffffffff, first_physical
= 0xffffffff;
514 uint32_t last_logical
, last_physical
;
516 int last_attr
= -1, attr
= -1;
517 CPUState
*cs
= env_cpu(env
);
520 if (env
->mmu
.tcr
& M68K_TCR_PAGE_8K
) {
524 tib_mask
= M68K_8K_PAGE_MASK
;
529 tib_mask
= M68K_4K_PAGE_MASK
;
531 for (i
= 0; i
< M68K_ROOT_POINTER_ENTRIES
; i
++) {
532 tia
= address_space_ldl(cs
->as
, M68K_POINTER_BASE(root_pointer
) + i
* 4,
533 MEMTXATTRS_UNSPECIFIED
, &txres
);
534 if (txres
!= MEMTX_OK
|| !M68K_UDT_VALID(tia
)) {
537 for (j
= 0; j
< M68K_ROOT_POINTER_ENTRIES
; j
++) {
538 tib
= address_space_ldl(cs
->as
, M68K_POINTER_BASE(tia
) + j
* 4,
539 MEMTXATTRS_UNSPECIFIED
, &txres
);
540 if (txres
!= MEMTX_OK
|| !M68K_UDT_VALID(tib
)) {
543 for (k
= 0; k
< tic_size
; k
++) {
544 tic
= address_space_ldl(cs
->as
, (tib
& tib_mask
) + k
* 4,
545 MEMTXATTRS_UNSPECIFIED
, &txres
);
546 if (txres
!= MEMTX_OK
|| !M68K_PDT_VALID(tic
)) {
549 if (M68K_PDT_INDIRECT(tic
)) {
550 tic
= address_space_ldl(cs
->as
, M68K_INDIRECT_POINTER(tic
),
551 MEMTXATTRS_UNSPECIFIED
, &txres
);
552 if (txres
!= MEMTX_OK
) {
557 last_logical
= logical
;
558 logical
= (i
<< M68K_TTS_ROOT_SHIFT
) |
559 (j
<< M68K_TTS_POINTER_SHIFT
) |
562 last_physical
= physical
;
563 physical
= tic
& ~((1 << tic_shift
) - 1);
566 attr
= tic
& ((1 << tic_shift
) - 1);
568 if ((logical
!= (last_logical
+ (1 << tic_shift
))) ||
569 (physical
!= (last_physical
+ (1 << tic_shift
))) ||
570 (attr
& 4) != (last_attr
& 4)) {
572 if (first_logical
!= 0xffffffff) {
573 size
= last_logical
+ (1 << tic_shift
) -
575 print_address_zone(first_logical
,
576 first_physical
, size
, last_attr
);
578 first_logical
= logical
;
579 first_physical
= physical
;
584 if (first_logical
!= logical
|| (attr
& 4) != (last_attr
& 4)) {
585 size
= logical
+ (1 << tic_shift
) - first_logical
;
586 print_address_zone(first_logical
, first_physical
, size
, last_attr
);
590 #define DUMP_CACHEFLAGS(a) \
591 switch (a & M68K_DESC_CACHEMODE) { \
592 case M68K_DESC_CM_WRTHRU: /* cachable, write-through */ \
595 case M68K_DESC_CM_COPYBK: /* cachable, copyback */ \
598 case M68K_DESC_CM_SERIAL: /* noncachable, serialized */ \
601 case M68K_DESC_CM_NCACHE: /* noncachable */ \
606 static void dump_ttr(uint32_t ttr
)
608 if ((ttr
& M68K_TTR_ENABLED
) == 0) {
609 qemu_printf("disabled\n");
612 qemu_printf("Base: 0x%08x Mask: 0x%08x Control: ",
613 ttr
& M68K_TTR_ADDR_BASE
,
614 (ttr
& M68K_TTR_ADDR_MASK
) << M68K_TTR_ADDR_MASK_SHIFT
);
615 switch (ttr
& M68K_TTR_SFIELD
) {
616 case M68K_TTR_SFIELD_USER
:
619 case M68K_TTR_SFIELD_SUPER
:
626 DUMP_CACHEFLAGS(ttr
);
627 if (ttr
& M68K_DESC_WRITEPROT
) {
632 qemu_printf(" U: %d\n", (ttr
& M68K_DESC_USERATTR
) >>
633 M68K_DESC_USERATTR_SHIFT
);
636 void dump_mmu(CPUM68KState
*env
)
638 if ((env
->mmu
.tcr
& M68K_TCR_ENABLED
) == 0) {
639 qemu_printf("Translation disabled\n");
642 qemu_printf("Page Size: ");
643 if (env
->mmu
.tcr
& M68K_TCR_PAGE_8K
) {
644 qemu_printf("8kB\n");
646 qemu_printf("4kB\n");
649 qemu_printf("MMUSR: ");
650 if (env
->mmu
.mmusr
& M68K_MMU_B_040
) {
651 qemu_printf("BUS ERROR\n");
653 qemu_printf("Phy=%08x Flags: ", env
->mmu
.mmusr
& 0xfffff000);
654 /* flags found on the page descriptor */
655 if (env
->mmu
.mmusr
& M68K_MMU_G_040
) {
656 qemu_printf("G"); /* Global */
660 if (env
->mmu
.mmusr
& M68K_MMU_S_040
) {
661 qemu_printf("S"); /* Supervisor */
665 if (env
->mmu
.mmusr
& M68K_MMU_M_040
) {
666 qemu_printf("M"); /* Modified */
670 if (env
->mmu
.mmusr
& M68K_MMU_WP_040
) {
671 qemu_printf("W"); /* Write protect */
675 if (env
->mmu
.mmusr
& M68K_MMU_T_040
) {
676 qemu_printf("T"); /* Transparent */
680 if (env
->mmu
.mmusr
& M68K_MMU_R_040
) {
681 qemu_printf("R"); /* Resident */
685 qemu_printf(" Cache: ");
686 DUMP_CACHEFLAGS(env
->mmu
.mmusr
);
687 qemu_printf(" U: %d\n", (env
->mmu
.mmusr
>> 8) & 3);
691 qemu_printf("ITTR0: ");
692 dump_ttr(env
->mmu
.ttr
[M68K_ITTR0
]);
693 qemu_printf("ITTR1: ");
694 dump_ttr(env
->mmu
.ttr
[M68K_ITTR1
]);
695 qemu_printf("DTTR0: ");
696 dump_ttr(env
->mmu
.ttr
[M68K_DTTR0
]);
697 qemu_printf("DTTR1: ");
698 dump_ttr(env
->mmu
.ttr
[M68K_DTTR1
]);
700 qemu_printf("SRP: 0x%08x\n", env
->mmu
.srp
);
701 dump_address_map(env
, env
->mmu
.srp
);
703 qemu_printf("URP: 0x%08x\n", env
->mmu
.urp
);
704 dump_address_map(env
, env
->mmu
.urp
);
707 static int check_TTR(uint32_t ttr
, int *prot
, target_ulong addr
,
712 /* check if transparent translation is enabled */
713 if ((ttr
& M68K_TTR_ENABLED
) == 0) {
717 /* check mode access */
718 switch (ttr
& M68K_TTR_SFIELD
) {
719 case M68K_TTR_SFIELD_USER
:
720 /* match only if user */
721 if ((access_type
& ACCESS_SUPER
) != 0) {
725 case M68K_TTR_SFIELD_SUPER
:
726 /* match only if supervisor */
727 if ((access_type
& ACCESS_SUPER
) == 0) {
732 /* all other values disable mode matching (FC2) */
736 /* check address matching */
738 base
= ttr
& M68K_TTR_ADDR_BASE
;
739 mask
= (ttr
& M68K_TTR_ADDR_MASK
) ^ M68K_TTR_ADDR_MASK
;
740 mask
<<= M68K_TTR_ADDR_MASK_SHIFT
;
742 if ((addr
& mask
) != (base
& mask
)) {
746 *prot
= PAGE_READ
| PAGE_EXEC
;
747 if ((ttr
& M68K_DESC_WRITEPROT
) == 0) {
754 static int get_physical_address(CPUM68KState
*env
, hwaddr
*physical
,
755 int *prot
, target_ulong address
,
756 int access_type
, target_ulong
*page_size
)
758 CPUState
*cs
= env_cpu(env
);
761 target_ulong page_mask
;
762 bool debug
= access_type
& ACCESS_DEBUG
;
767 /* Transparent Translation (physical = logical) */
768 for (i
= 0; i
< M68K_MAX_TTR
; i
++) {
769 if (check_TTR(env
->mmu
.TTR(access_type
, i
),
770 prot
, address
, access_type
)) {
771 if (access_type
& ACCESS_PTEST
) {
772 /* Transparent Translation Register bit */
773 env
->mmu
.mmusr
= M68K_MMU_T_040
| M68K_MMU_R_040
;
776 *page_size
= TARGET_PAGE_SIZE
;
781 /* Page Table Root Pointer */
782 *prot
= PAGE_READ
| PAGE_WRITE
;
783 if (access_type
& ACCESS_CODE
) {
786 if (access_type
& ACCESS_SUPER
) {
793 entry
= M68K_POINTER_BASE(next
) | M68K_ROOT_INDEX(address
);
795 next
= address_space_ldl(cs
->as
, entry
, MEMTXATTRS_UNSPECIFIED
, &txres
);
796 if (txres
!= MEMTX_OK
) {
799 if (!M68K_UDT_VALID(next
)) {
802 if (!(next
& M68K_DESC_USED
) && !debug
) {
803 address_space_stl(cs
->as
, entry
, next
| M68K_DESC_USED
,
804 MEMTXATTRS_UNSPECIFIED
, &txres
);
805 if (txres
!= MEMTX_OK
) {
809 if (next
& M68K_DESC_WRITEPROT
) {
810 if (access_type
& ACCESS_PTEST
) {
811 env
->mmu
.mmusr
|= M68K_MMU_WP_040
;
813 *prot
&= ~PAGE_WRITE
;
814 if (access_type
& ACCESS_STORE
) {
820 entry
= M68K_POINTER_BASE(next
) | M68K_POINTER_INDEX(address
);
822 next
= address_space_ldl(cs
->as
, entry
, MEMTXATTRS_UNSPECIFIED
, &txres
);
823 if (txres
!= MEMTX_OK
) {
826 if (!M68K_UDT_VALID(next
)) {
829 if (!(next
& M68K_DESC_USED
) && !debug
) {
830 address_space_stl(cs
->as
, entry
, next
| M68K_DESC_USED
,
831 MEMTXATTRS_UNSPECIFIED
, &txres
);
832 if (txres
!= MEMTX_OK
) {
836 if (next
& M68K_DESC_WRITEPROT
) {
837 if (access_type
& ACCESS_PTEST
) {
838 env
->mmu
.mmusr
|= M68K_MMU_WP_040
;
840 *prot
&= ~PAGE_WRITE
;
841 if (access_type
& ACCESS_STORE
) {
847 if (env
->mmu
.tcr
& M68K_TCR_PAGE_8K
) {
848 entry
= M68K_8K_PAGE_BASE(next
) | M68K_8K_PAGE_INDEX(address
);
850 entry
= M68K_4K_PAGE_BASE(next
) | M68K_4K_PAGE_INDEX(address
);
853 next
= address_space_ldl(cs
->as
, entry
, MEMTXATTRS_UNSPECIFIED
, &txres
);
854 if (txres
!= MEMTX_OK
) {
858 if (!M68K_PDT_VALID(next
)) {
861 if (M68K_PDT_INDIRECT(next
)) {
862 next
= address_space_ldl(cs
->as
, M68K_INDIRECT_POINTER(next
),
863 MEMTXATTRS_UNSPECIFIED
, &txres
);
864 if (txres
!= MEMTX_OK
) {
868 if (access_type
& ACCESS_STORE
) {
869 if (next
& M68K_DESC_WRITEPROT
) {
870 if (!(next
& M68K_DESC_USED
) && !debug
) {
871 address_space_stl(cs
->as
, entry
, next
| M68K_DESC_USED
,
872 MEMTXATTRS_UNSPECIFIED
, &txres
);
873 if (txres
!= MEMTX_OK
) {
877 } else if ((next
& (M68K_DESC_MODIFIED
| M68K_DESC_USED
)) !=
878 (M68K_DESC_MODIFIED
| M68K_DESC_USED
) && !debug
) {
879 address_space_stl(cs
->as
, entry
,
880 next
| (M68K_DESC_MODIFIED
| M68K_DESC_USED
),
881 MEMTXATTRS_UNSPECIFIED
, &txres
);
882 if (txres
!= MEMTX_OK
) {
887 if (!(next
& M68K_DESC_USED
) && !debug
) {
888 address_space_stl(cs
->as
, entry
, next
| M68K_DESC_USED
,
889 MEMTXATTRS_UNSPECIFIED
, &txres
);
890 if (txres
!= MEMTX_OK
) {
896 if (env
->mmu
.tcr
& M68K_TCR_PAGE_8K
) {
901 *page_size
= 1 << page_bits
;
902 page_mask
= ~(*page_size
- 1);
903 *physical
= (next
& page_mask
) + (address
& (*page_size
- 1));
905 if (access_type
& ACCESS_PTEST
) {
906 env
->mmu
.mmusr
|= next
& M68K_MMU_SR_MASK_040
;
907 env
->mmu
.mmusr
|= *physical
& 0xfffff000;
908 env
->mmu
.mmusr
|= M68K_MMU_R_040
;
911 if (next
& M68K_DESC_WRITEPROT
) {
912 *prot
&= ~PAGE_WRITE
;
913 if (access_type
& ACCESS_STORE
) {
917 if (next
& M68K_DESC_SUPERONLY
) {
918 if ((access_type
& ACCESS_SUPER
) == 0) {
927 * A page table load/store failed. TODO: we should really raise a
928 * suitable guest fault here if this is not a debug access.
929 * For now just return that the translation failed.
934 hwaddr
m68k_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
936 M68kCPU
*cpu
= M68K_CPU(cs
);
937 CPUM68KState
*env
= &cpu
->env
;
941 target_ulong page_size
;
943 if ((env
->mmu
.tcr
& M68K_TCR_ENABLED
) == 0) {
948 access_type
= ACCESS_DATA
| ACCESS_DEBUG
;
949 if (env
->sr
& SR_S
) {
950 access_type
|= ACCESS_SUPER
;
953 if (get_physical_address(env
, &phys_addr
, &prot
,
954 addr
, access_type
, &page_size
) != 0) {
962 * Notify CPU of a pending interrupt. Prioritization and vectoring should
963 * be handled by the interrupt controller. Real hardware only requests
964 * the vector when the interrupt is acknowledged by the CPU. For
965 * simplicity we calculate it when the interrupt is signalled.
967 void m68k_set_irq_level(M68kCPU
*cpu
, int level
, uint8_t vector
)
969 CPUState
*cs
= CPU(cpu
);
970 CPUM68KState
*env
= &cpu
->env
;
972 env
->pending_level
= level
;
973 env
->pending_vector
= vector
;
975 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
977 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
983 bool m68k_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
984 MMUAccessType qemu_access_type
, int mmu_idx
,
985 bool probe
, uintptr_t retaddr
)
987 M68kCPU
*cpu
= M68K_CPU(cs
);
988 CPUM68KState
*env
= &cpu
->env
;
990 #ifndef CONFIG_USER_ONLY
995 target_ulong page_size
;
997 if ((env
->mmu
.tcr
& M68K_TCR_ENABLED
) == 0) {
999 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
1000 address
& TARGET_PAGE_MASK
,
1001 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
,
1002 mmu_idx
, TARGET_PAGE_SIZE
);
1006 if (qemu_access_type
== MMU_INST_FETCH
) {
1007 access_type
= ACCESS_CODE
;
1009 access_type
= ACCESS_DATA
;
1010 if (qemu_access_type
== MMU_DATA_STORE
) {
1011 access_type
|= ACCESS_STORE
;
1014 if (mmu_idx
!= MMU_USER_IDX
) {
1015 access_type
|= ACCESS_SUPER
;
1018 ret
= get_physical_address(&cpu
->env
, &physical
, &prot
,
1019 address
, access_type
, &page_size
);
1020 if (likely(ret
== 0)) {
1021 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
1022 physical
& TARGET_PAGE_MASK
, prot
, mmu_idx
, page_size
);
1031 env
->mmu
.ssw
= M68K_ATC_040
;
1034 env
->mmu
.ssw
|= M68K_BA_SIZE_BYTE
;
1037 env
->mmu
.ssw
|= M68K_BA_SIZE_WORD
;
1040 env
->mmu
.ssw
|= M68K_BA_SIZE_LONG
;
1043 if (access_type
& ACCESS_SUPER
) {
1044 env
->mmu
.ssw
|= M68K_TM_040_SUPER
;
1046 if (access_type
& ACCESS_CODE
) {
1047 env
->mmu
.ssw
|= M68K_TM_040_CODE
;
1049 env
->mmu
.ssw
|= M68K_TM_040_DATA
;
1051 if (!(access_type
& ACCESS_STORE
)) {
1052 env
->mmu
.ssw
|= M68K_RW_040
;
1056 cs
->exception_index
= EXCP_ACCESS
;
1057 env
->mmu
.ar
= address
;
1058 cpu_loop_exit_restore(cs
, retaddr
);
1061 uint32_t HELPER(bitrev
)(uint32_t x
)
1063 x
= ((x
>> 1) & 0x55555555u
) | ((x
<< 1) & 0xaaaaaaaau
);
1064 x
= ((x
>> 2) & 0x33333333u
) | ((x
<< 2) & 0xccccccccu
);
1065 x
= ((x
>> 4) & 0x0f0f0f0fu
) | ((x
<< 4) & 0xf0f0f0f0u
);
1069 uint32_t HELPER(ff1
)(uint32_t x
)
1072 for (n
= 32; x
; n
--)
1077 uint32_t HELPER(sats
)(uint32_t val
, uint32_t v
)
1079 /* The result has the opposite sign to the original value. */
1080 if ((int32_t)v
< 0) {
1081 val
= (((int32_t)val
) >> 31) ^ SIGNBIT
;
1086 void cpu_m68k_set_sr(CPUM68KState
*env
, uint32_t sr
)
1088 env
->sr
= sr
& 0xffe0;
1089 cpu_m68k_set_ccr(env
, sr
);
1090 m68k_switch_sp(env
);
1093 void HELPER(set_sr
)(CPUM68KState
*env
, uint32_t val
)
1095 cpu_m68k_set_sr(env
, val
);
1100 * FIXME: The MAC unit implementation is a bit of a mess. Some helpers
1101 * take values, others take register numbers and manipulate the contents
1104 void HELPER(mac_move
)(CPUM68KState
*env
, uint32_t dest
, uint32_t src
)
1107 env
->macc
[dest
] = env
->macc
[src
];
1108 mask
= MACSR_PAV0
<< dest
;
1109 if (env
->macsr
& (MACSR_PAV0
<< src
))
1112 env
->macsr
&= ~mask
;
1115 uint64_t HELPER(macmuls
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
1120 product
= (uint64_t)op1
* op2
;
1121 res
= (product
<< 24) >> 24;
1122 if (res
!= product
) {
1123 env
->macsr
|= MACSR_V
;
1124 if (env
->macsr
& MACSR_OMC
) {
1125 /* Make sure the accumulate operation overflows. */
1135 uint64_t HELPER(macmulu
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
1139 product
= (uint64_t)op1
* op2
;
1140 if (product
& (0xffffffull
<< 40)) {
1141 env
->macsr
|= MACSR_V
;
1142 if (env
->macsr
& MACSR_OMC
) {
1143 /* Make sure the accumulate operation overflows. */
1144 product
= 1ll << 50;
1146 product
&= ((1ull << 40) - 1);
1152 uint64_t HELPER(macmulf
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
1157 product
= (uint64_t)op1
* op2
;
1158 if (env
->macsr
& MACSR_RT
) {
1159 remainder
= product
& 0xffffff;
1161 if (remainder
> 0x800000)
1163 else if (remainder
== 0x800000)
1164 product
+= (product
& 1);
1171 void HELPER(macsats
)(CPUM68KState
*env
, uint32_t acc
)
1175 tmp
= env
->macc
[acc
];
1176 result
= ((tmp
<< 16) >> 16);
1177 if (result
!= tmp
) {
1178 env
->macsr
|= MACSR_V
;
1180 if (env
->macsr
& MACSR_V
) {
1181 env
->macsr
|= MACSR_PAV0
<< acc
;
1182 if (env
->macsr
& MACSR_OMC
) {
1184 * The result is saturated to 32 bits, despite overflow occurring
1185 * at 48 bits. Seems weird, but that's what the hardware docs
1188 result
= (result
>> 63) ^ 0x7fffffff;
1191 env
->macc
[acc
] = result
;
1194 void HELPER(macsatu
)(CPUM68KState
*env
, uint32_t acc
)
1198 val
= env
->macc
[acc
];
1199 if (val
& (0xffffull
<< 48)) {
1200 env
->macsr
|= MACSR_V
;
1202 if (env
->macsr
& MACSR_V
) {
1203 env
->macsr
|= MACSR_PAV0
<< acc
;
1204 if (env
->macsr
& MACSR_OMC
) {
1205 if (val
> (1ull << 53))
1208 val
= (1ull << 48) - 1;
1210 val
&= ((1ull << 48) - 1);
1213 env
->macc
[acc
] = val
;
1216 void HELPER(macsatf
)(CPUM68KState
*env
, uint32_t acc
)
1221 sum
= env
->macc
[acc
];
1222 result
= (sum
<< 16) >> 16;
1223 if (result
!= sum
) {
1224 env
->macsr
|= MACSR_V
;
1226 if (env
->macsr
& MACSR_V
) {
1227 env
->macsr
|= MACSR_PAV0
<< acc
;
1228 if (env
->macsr
& MACSR_OMC
) {
1229 result
= (result
>> 63) ^ 0x7fffffffffffll
;
1232 env
->macc
[acc
] = result
;
1235 void HELPER(mac_set_flags
)(CPUM68KState
*env
, uint32_t acc
)
1238 val
= env
->macc
[acc
];
1240 env
->macsr
|= MACSR_Z
;
1241 } else if (val
& (1ull << 47)) {
1242 env
->macsr
|= MACSR_N
;
1244 if (env
->macsr
& (MACSR_PAV0
<< acc
)) {
1245 env
->macsr
|= MACSR_V
;
1247 if (env
->macsr
& MACSR_FI
) {
1248 val
= ((int64_t)val
) >> 40;
1249 if (val
!= 0 && val
!= -1)
1250 env
->macsr
|= MACSR_EV
;
1251 } else if (env
->macsr
& MACSR_SU
) {
1252 val
= ((int64_t)val
) >> 32;
1253 if (val
!= 0 && val
!= -1)
1254 env
->macsr
|= MACSR_EV
;
1256 if ((val
>> 32) != 0)
1257 env
->macsr
|= MACSR_EV
;
1261 #define EXTSIGN(val, index) ( \
1262 (index == 0) ? (int8_t)(val) : ((index == 1) ? (int16_t)(val) : (val)) \
1265 #define COMPUTE_CCR(op, x, n, z, v, c) { \
1268 /* Everything in place. */ \
1275 src1 = EXTSIGN(res - src2, op - CC_OP_ADDB); \
1278 v = (res ^ src1) & ~(src1 ^ src2); \
1285 src1 = EXTSIGN(res + src2, op - CC_OP_SUBB); \
1288 v = (res ^ src1) & (src1 ^ src2); \
1295 res = EXTSIGN(src1 - src2, op - CC_OP_CMPB); \
1299 v = (res ^ src1) & (src1 ^ src2); \
1306 cpu_abort(env_cpu(env), "Bad CC_OP %d", op); \
1310 uint32_t cpu_m68k_get_ccr(CPUM68KState
*env
)
1312 uint32_t x
, c
, n
, z
, v
;
1313 uint32_t res
, src1
, src2
;
1321 COMPUTE_CCR(env
->cc_op
, x
, n
, z
, v
, c
);
1327 return x
* CCF_X
+ n
* CCF_N
+ z
* CCF_Z
+ v
* CCF_V
+ c
* CCF_C
;
1330 uint32_t HELPER(get_ccr
)(CPUM68KState
*env
)
1332 return cpu_m68k_get_ccr(env
);
1335 void cpu_m68k_set_ccr(CPUM68KState
*env
, uint32_t ccr
)
1337 env
->cc_x
= (ccr
& CCF_X
? 1 : 0);
1338 env
->cc_n
= (ccr
& CCF_N
? -1 : 0);
1339 env
->cc_z
= (ccr
& CCF_Z
? 0 : 1);
1340 env
->cc_v
= (ccr
& CCF_V
? -1 : 0);
1341 env
->cc_c
= (ccr
& CCF_C
? 1 : 0);
1342 env
->cc_op
= CC_OP_FLAGS
;
1345 void HELPER(set_ccr
)(CPUM68KState
*env
, uint32_t ccr
)
1347 cpu_m68k_set_ccr(env
, ccr
);
1350 void HELPER(flush_flags
)(CPUM68KState
*env
, uint32_t cc_op
)
1352 uint32_t res
, src1
, src2
;
1354 COMPUTE_CCR(cc_op
, env
->cc_x
, env
->cc_n
, env
->cc_z
, env
->cc_v
, env
->cc_c
);
1355 env
->cc_op
= CC_OP_FLAGS
;
1358 uint32_t HELPER(get_macf
)(CPUM68KState
*env
, uint64_t val
)
1363 if (env
->macsr
& MACSR_SU
) {
1364 /* 16-bit rounding. */
1365 rem
= val
& 0xffffff;
1366 val
= (val
>> 24) & 0xffffu
;
1369 else if (rem
== 0x800000)
1371 } else if (env
->macsr
& MACSR_RT
) {
1372 /* 32-bit rounding. */
1377 else if (rem
== 0x80)
1383 if (env
->macsr
& MACSR_OMC
) {
1385 if (env
->macsr
& MACSR_SU
) {
1386 if (val
!= (uint16_t) val
) {
1387 result
= ((val
>> 63) ^ 0x7fff) & 0xffff;
1389 result
= val
& 0xffff;
1392 if (val
!= (uint32_t)val
) {
1393 result
= ((uint32_t)(val
>> 63) & 0x7fffffff);
1395 result
= (uint32_t)val
;
1399 /* No saturation. */
1400 if (env
->macsr
& MACSR_SU
) {
1401 result
= val
& 0xffff;
1403 result
= (uint32_t)val
;
1409 uint32_t HELPER(get_macs
)(uint64_t val
)
1411 if (val
== (int32_t)val
) {
1412 return (int32_t)val
;
1414 return (val
>> 61) ^ ~SIGNBIT
;
1418 uint32_t HELPER(get_macu
)(uint64_t val
)
1420 if ((val
>> 32) == 0) {
1421 return (uint32_t)val
;
1427 uint32_t HELPER(get_mac_extf
)(CPUM68KState
*env
, uint32_t acc
)
1430 val
= env
->macc
[acc
] & 0x00ff;
1431 val
|= (env
->macc
[acc
] >> 32) & 0xff00;
1432 val
|= (env
->macc
[acc
+ 1] << 16) & 0x00ff0000;
1433 val
|= (env
->macc
[acc
+ 1] >> 16) & 0xff000000;
1437 uint32_t HELPER(get_mac_exti
)(CPUM68KState
*env
, uint32_t acc
)
1440 val
= (env
->macc
[acc
] >> 32) & 0xffff;
1441 val
|= (env
->macc
[acc
+ 1] >> 16) & 0xffff0000;
1445 void HELPER(set_mac_extf
)(CPUM68KState
*env
, uint32_t val
, uint32_t acc
)
1449 res
= env
->macc
[acc
] & 0xffffffff00ull
;
1450 tmp
= (int16_t)(val
& 0xff00);
1451 res
|= ((int64_t)tmp
) << 32;
1453 env
->macc
[acc
] = res
;
1454 res
= env
->macc
[acc
+ 1] & 0xffffffff00ull
;
1455 tmp
= (val
& 0xff000000);
1456 res
|= ((int64_t)tmp
) << 16;
1457 res
|= (val
>> 16) & 0xff;
1458 env
->macc
[acc
+ 1] = res
;
1461 void HELPER(set_mac_exts
)(CPUM68KState
*env
, uint32_t val
, uint32_t acc
)
1465 res
= (uint32_t)env
->macc
[acc
];
1467 res
|= ((int64_t)tmp
) << 32;
1468 env
->macc
[acc
] = res
;
1469 res
= (uint32_t)env
->macc
[acc
+ 1];
1470 tmp
= val
& 0xffff0000;
1471 res
|= (int64_t)tmp
<< 16;
1472 env
->macc
[acc
+ 1] = res
;
1475 void HELPER(set_mac_extu
)(CPUM68KState
*env
, uint32_t val
, uint32_t acc
)
1478 res
= (uint32_t)env
->macc
[acc
];
1479 res
|= ((uint64_t)(val
& 0xffff)) << 32;
1480 env
->macc
[acc
] = res
;
1481 res
= (uint32_t)env
->macc
[acc
+ 1];
1482 res
|= (uint64_t)(val
& 0xffff0000) << 16;
1483 env
->macc
[acc
+ 1] = res
;
1486 #if defined(CONFIG_SOFTMMU)
1487 void HELPER(ptest
)(CPUM68KState
*env
, uint32_t addr
, uint32_t is_read
)
1493 target_ulong page_size
;
1495 access_type
= ACCESS_PTEST
;
1497 access_type
|= ACCESS_SUPER
;
1499 if ((env
->dfc
& 3) == 2) {
1500 access_type
|= ACCESS_CODE
;
1503 access_type
|= ACCESS_STORE
;
1508 ret
= get_physical_address(env
, &physical
, &prot
, addr
,
1509 access_type
, &page_size
);
1511 tlb_set_page(env_cpu(env
), addr
& TARGET_PAGE_MASK
,
1512 physical
& TARGET_PAGE_MASK
,
1513 prot
, access_type
& ACCESS_SUPER
?
1514 MMU_KERNEL_IDX
: MMU_USER_IDX
, page_size
);
1518 void HELPER(pflush
)(CPUM68KState
*env
, uint32_t addr
, uint32_t opmode
)
1520 CPUState
*cs
= env_cpu(env
);
1523 case 0: /* Flush page entry if not global */
1524 case 1: /* Flush page entry */
1525 tlb_flush_page(cs
, addr
);
1527 case 2: /* Flush all except global entries */
1530 case 3: /* Flush all entries */
1536 void HELPER(reset
)(CPUM68KState
*env
)
1538 /* FIXME: reset all except CPU */