2 * Helpers for emulation of CP0-related MIPS instructions.
4 * Copyright (C) 2004-2005 Jocelyn Mayer
5 * Copyright (C) 2020 Wave Computing, Inc.
6 * Copyright (C) 2020 Aleksandar Markovic <amarkovic@wavecomp.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "qemu/osdep.h"
24 #include "qemu/main-loop.h"
27 #include "qemu/host-utils.h"
28 #include "exec/helper-proto.h"
29 #include "exec/exec-all.h"
30 #include "exec/cpu_ldst.h"
31 #include "exec/memop.h"
32 #include "sysemu/kvm.h"
35 #ifndef CONFIG_USER_ONLY
37 static bool mips_vpe_is_wfi(MIPSCPU
*c
)
39 CPUState
*cpu
= CPU(c
);
40 CPUMIPSState
*env
= &c
->env
;
43 * If the VPE is halted but otherwise active, it means it's waiting for
46 return cpu
->halted
&& mips_vpe_active(env
);
49 static bool mips_vp_is_wfi(MIPSCPU
*c
)
51 CPUState
*cpu
= CPU(c
);
52 CPUMIPSState
*env
= &c
->env
;
54 return cpu
->halted
&& mips_vp_active(env
);
57 static inline void mips_vpe_wake(MIPSCPU
*c
)
60 * Don't set ->halted = 0 directly, let it be done via cpu_has_work
61 * because there might be other conditions that state that c should
64 qemu_mutex_lock_iothread();
65 cpu_interrupt(CPU(c
), CPU_INTERRUPT_WAKE
);
66 qemu_mutex_unlock_iothread();
69 static inline void mips_vpe_sleep(MIPSCPU
*cpu
)
71 CPUState
*cs
= CPU(cpu
);
74 * The VPE was shut off, really go to bed.
75 * Reset any old _WAKE requests.
78 cpu_reset_interrupt(cs
, CPU_INTERRUPT_WAKE
);
81 static inline void mips_tc_wake(MIPSCPU
*cpu
, int tc
)
83 CPUMIPSState
*c
= &cpu
->env
;
85 /* FIXME: TC reschedule. */
86 if (mips_vpe_active(c
) && !mips_vpe_is_wfi(cpu
)) {
91 static inline void mips_tc_sleep(MIPSCPU
*cpu
, int tc
)
93 CPUMIPSState
*c
= &cpu
->env
;
95 /* FIXME: TC reschedule. */
96 if (!mips_vpe_active(c
)) {
103 * @env: CPU from which mapping is performed.
104 * @tc: Should point to an int with the value of the global TC index.
106 * This function will transform @tc into a local index within the
107 * returned #CPUMIPSState.
111 * FIXME: This code assumes that all VPEs have the same number of TCs,
112 * which depends on runtime setup. Can probably be fixed by
113 * walking the list of CPUMIPSStates.
115 static CPUMIPSState
*mips_cpu_map_tc(CPUMIPSState
*env
, int *tc
)
123 if (!(env
->CP0_VPEConf0
& (1 << CP0VPEC0_MVP
))) {
124 /* Not allowed to address other CPUs. */
125 *tc
= env
->current_tc
;
130 vpe_idx
= tc_idx
/ cs
->nr_threads
;
131 *tc
= tc_idx
% cs
->nr_threads
;
132 other_cs
= qemu_get_cpu(vpe_idx
);
133 if (other_cs
== NULL
) {
136 cpu
= MIPS_CPU(other_cs
);
141 * The per VPE CP0_Status register shares some fields with the per TC
142 * CP0_TCStatus registers. These fields are wired to the same registers,
143 * so changes to either of them should be reflected on both registers.
145 * Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
147 * These helper call synchronizes the regs for a given cpu.
151 * Called for updates to CP0_Status. Defined in "cpu.h" for gdbstub.c.
152 * static inline void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu,
156 /* Called for updates to CP0_TCStatus. */
157 static void sync_c0_tcstatus(CPUMIPSState
*cpu
, int tc
,
161 uint32_t tcu
, tmx
, tasid
, tksu
;
162 uint32_t mask
= ((1U << CP0St_CU3
)
169 tcu
= (v
>> CP0TCSt_TCU0
) & 0xf;
170 tmx
= (v
>> CP0TCSt_TMX
) & 0x1;
171 tasid
= v
& cpu
->CP0_EntryHi_ASID_mask
;
172 tksu
= (v
>> CP0TCSt_TKSU
) & 0x3;
174 status
= tcu
<< CP0St_CU0
;
175 status
|= tmx
<< CP0St_MX
;
176 status
|= tksu
<< CP0St_KSU
;
178 cpu
->CP0_Status
&= ~mask
;
179 cpu
->CP0_Status
|= status
;
181 /* Sync the TASID with EntryHi. */
182 cpu
->CP0_EntryHi
&= ~cpu
->CP0_EntryHi_ASID_mask
;
183 cpu
->CP0_EntryHi
|= tasid
;
188 /* Called for updates to CP0_EntryHi. */
189 static void sync_c0_entryhi(CPUMIPSState
*cpu
, int tc
)
192 uint32_t asid
, v
= cpu
->CP0_EntryHi
;
194 asid
= v
& cpu
->CP0_EntryHi_ASID_mask
;
196 if (tc
== cpu
->current_tc
) {
197 tcst
= &cpu
->active_tc
.CP0_TCStatus
;
199 tcst
= &cpu
->tcs
[tc
].CP0_TCStatus
;
202 *tcst
&= ~cpu
->CP0_EntryHi_ASID_mask
;
207 target_ulong
helper_mfc0_mvpcontrol(CPUMIPSState
*env
)
209 return env
->mvp
->CP0_MVPControl
;
212 target_ulong
helper_mfc0_mvpconf0(CPUMIPSState
*env
)
214 return env
->mvp
->CP0_MVPConf0
;
217 target_ulong
helper_mfc0_mvpconf1(CPUMIPSState
*env
)
219 return env
->mvp
->CP0_MVPConf1
;
222 target_ulong
helper_mfc0_random(CPUMIPSState
*env
)
224 return (int32_t)cpu_mips_get_random(env
);
227 target_ulong
helper_mfc0_tcstatus(CPUMIPSState
*env
)
229 return env
->active_tc
.CP0_TCStatus
;
232 target_ulong
helper_mftc0_tcstatus(CPUMIPSState
*env
)
234 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
235 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
237 if (other_tc
== other
->current_tc
) {
238 return other
->active_tc
.CP0_TCStatus
;
240 return other
->tcs
[other_tc
].CP0_TCStatus
;
244 target_ulong
helper_mfc0_tcbind(CPUMIPSState
*env
)
246 return env
->active_tc
.CP0_TCBind
;
249 target_ulong
helper_mftc0_tcbind(CPUMIPSState
*env
)
251 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
252 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
254 if (other_tc
== other
->current_tc
) {
255 return other
->active_tc
.CP0_TCBind
;
257 return other
->tcs
[other_tc
].CP0_TCBind
;
261 target_ulong
helper_mfc0_tcrestart(CPUMIPSState
*env
)
263 return env
->active_tc
.PC
;
266 target_ulong
helper_mftc0_tcrestart(CPUMIPSState
*env
)
268 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
269 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
271 if (other_tc
== other
->current_tc
) {
272 return other
->active_tc
.PC
;
274 return other
->tcs
[other_tc
].PC
;
278 target_ulong
helper_mfc0_tchalt(CPUMIPSState
*env
)
280 return env
->active_tc
.CP0_TCHalt
;
283 target_ulong
helper_mftc0_tchalt(CPUMIPSState
*env
)
285 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
286 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
288 if (other_tc
== other
->current_tc
) {
289 return other
->active_tc
.CP0_TCHalt
;
291 return other
->tcs
[other_tc
].CP0_TCHalt
;
295 target_ulong
helper_mfc0_tccontext(CPUMIPSState
*env
)
297 return env
->active_tc
.CP0_TCContext
;
300 target_ulong
helper_mftc0_tccontext(CPUMIPSState
*env
)
302 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
303 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
305 if (other_tc
== other
->current_tc
) {
306 return other
->active_tc
.CP0_TCContext
;
308 return other
->tcs
[other_tc
].CP0_TCContext
;
312 target_ulong
helper_mfc0_tcschedule(CPUMIPSState
*env
)
314 return env
->active_tc
.CP0_TCSchedule
;
317 target_ulong
helper_mftc0_tcschedule(CPUMIPSState
*env
)
319 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
320 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
322 if (other_tc
== other
->current_tc
) {
323 return other
->active_tc
.CP0_TCSchedule
;
325 return other
->tcs
[other_tc
].CP0_TCSchedule
;
329 target_ulong
helper_mfc0_tcschefback(CPUMIPSState
*env
)
331 return env
->active_tc
.CP0_TCScheFBack
;
334 target_ulong
helper_mftc0_tcschefback(CPUMIPSState
*env
)
336 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
337 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
339 if (other_tc
== other
->current_tc
) {
340 return other
->active_tc
.CP0_TCScheFBack
;
342 return other
->tcs
[other_tc
].CP0_TCScheFBack
;
346 target_ulong
helper_mfc0_count(CPUMIPSState
*env
)
348 return (int32_t)cpu_mips_get_count(env
);
351 target_ulong
helper_mfc0_saar(CPUMIPSState
*env
)
353 if ((env
->CP0_SAARI
& 0x3f) < 2) {
354 return (int32_t) env
->CP0_SAAR
[env
->CP0_SAARI
& 0x3f];
359 target_ulong
helper_mfhc0_saar(CPUMIPSState
*env
)
361 if ((env
->CP0_SAARI
& 0x3f) < 2) {
362 return env
->CP0_SAAR
[env
->CP0_SAARI
& 0x3f] >> 32;
367 target_ulong
helper_mftc0_entryhi(CPUMIPSState
*env
)
369 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
370 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
372 return other
->CP0_EntryHi
;
375 target_ulong
helper_mftc0_cause(CPUMIPSState
*env
)
377 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
378 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
380 return other
->CP0_Cause
;
383 target_ulong
helper_mftc0_status(CPUMIPSState
*env
)
385 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
386 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
388 return other
->CP0_Status
;
391 target_ulong
helper_mfc0_lladdr(CPUMIPSState
*env
)
393 return (int32_t)(env
->CP0_LLAddr
>> env
->CP0_LLAddr_shift
);
396 target_ulong
helper_mfc0_maar(CPUMIPSState
*env
)
398 return (int32_t) env
->CP0_MAAR
[env
->CP0_MAARI
];
401 target_ulong
helper_mfhc0_maar(CPUMIPSState
*env
)
403 return env
->CP0_MAAR
[env
->CP0_MAARI
] >> 32;
406 target_ulong
helper_mfc0_watchlo(CPUMIPSState
*env
, uint32_t sel
)
408 return (int32_t)env
->CP0_WatchLo
[sel
];
411 target_ulong
helper_mfc0_watchhi(CPUMIPSState
*env
, uint32_t sel
)
413 return (int32_t) env
->CP0_WatchHi
[sel
];
416 target_ulong
helper_mfhc0_watchhi(CPUMIPSState
*env
, uint32_t sel
)
418 return env
->CP0_WatchHi
[sel
] >> 32;
421 target_ulong
helper_mfc0_debug(CPUMIPSState
*env
)
423 target_ulong t0
= env
->CP0_Debug
;
424 if (env
->hflags
& MIPS_HFLAG_DM
) {
431 target_ulong
helper_mftc0_debug(CPUMIPSState
*env
)
433 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
435 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
437 if (other_tc
== other
->current_tc
) {
438 tcstatus
= other
->active_tc
.CP0_Debug_tcstatus
;
440 tcstatus
= other
->tcs
[other_tc
].CP0_Debug_tcstatus
;
443 /* XXX: Might be wrong, check with EJTAG spec. */
444 return (other
->CP0_Debug
& ~((1 << CP0DB_SSt
) | (1 << CP0DB_Halt
))) |
445 (tcstatus
& ((1 << CP0DB_SSt
) | (1 << CP0DB_Halt
)));
448 #if defined(TARGET_MIPS64)
449 target_ulong
helper_dmfc0_tcrestart(CPUMIPSState
*env
)
451 return env
->active_tc
.PC
;
454 target_ulong
helper_dmfc0_tchalt(CPUMIPSState
*env
)
456 return env
->active_tc
.CP0_TCHalt
;
459 target_ulong
helper_dmfc0_tccontext(CPUMIPSState
*env
)
461 return env
->active_tc
.CP0_TCContext
;
464 target_ulong
helper_dmfc0_tcschedule(CPUMIPSState
*env
)
466 return env
->active_tc
.CP0_TCSchedule
;
469 target_ulong
helper_dmfc0_tcschefback(CPUMIPSState
*env
)
471 return env
->active_tc
.CP0_TCScheFBack
;
474 target_ulong
helper_dmfc0_lladdr(CPUMIPSState
*env
)
476 return env
->CP0_LLAddr
>> env
->CP0_LLAddr_shift
;
479 target_ulong
helper_dmfc0_maar(CPUMIPSState
*env
)
481 return env
->CP0_MAAR
[env
->CP0_MAARI
];
484 target_ulong
helper_dmfc0_watchlo(CPUMIPSState
*env
, uint32_t sel
)
486 return env
->CP0_WatchLo
[sel
];
489 target_ulong
helper_dmfc0_watchhi(CPUMIPSState
*env
, uint32_t sel
)
491 return env
->CP0_WatchHi
[sel
];
494 target_ulong
helper_dmfc0_saar(CPUMIPSState
*env
)
496 if ((env
->CP0_SAARI
& 0x3f) < 2) {
497 return env
->CP0_SAAR
[env
->CP0_SAARI
& 0x3f];
501 #endif /* TARGET_MIPS64 */
503 void helper_mtc0_index(CPUMIPSState
*env
, target_ulong arg1
)
505 uint32_t index_p
= env
->CP0_Index
& 0x80000000;
506 uint32_t tlb_index
= arg1
& 0x7fffffff;
507 if (tlb_index
< env
->tlb
->nb_tlb
) {
508 if (env
->insn_flags
& ISA_MIPS32R6
) {
509 index_p
|= arg1
& 0x80000000;
511 env
->CP0_Index
= index_p
| tlb_index
;
515 void helper_mtc0_mvpcontrol(CPUMIPSState
*env
, target_ulong arg1
)
520 if (env
->CP0_VPEConf0
& (1 << CP0VPEC0_MVP
)) {
521 mask
|= (1 << CP0MVPCo_CPA
) | (1 << CP0MVPCo_VPC
) |
524 if (env
->mvp
->CP0_MVPControl
& (1 << CP0MVPCo_VPC
)) {
525 mask
|= (1 << CP0MVPCo_STLB
);
527 newval
= (env
->mvp
->CP0_MVPControl
& ~mask
) | (arg1
& mask
);
529 /* TODO: Enable/disable shared TLB, enable/disable VPEs. */
531 env
->mvp
->CP0_MVPControl
= newval
;
534 void helper_mtc0_vpecontrol(CPUMIPSState
*env
, target_ulong arg1
)
539 mask
= (1 << CP0VPECo_YSI
) | (1 << CP0VPECo_GSI
) |
540 (1 << CP0VPECo_TE
) | (0xff << CP0VPECo_TargTC
);
541 newval
= (env
->CP0_VPEControl
& ~mask
) | (arg1
& mask
);
544 * Yield scheduler intercept not implemented.
545 * Gating storage scheduler intercept not implemented.
548 /* TODO: Enable/disable TCs. */
550 env
->CP0_VPEControl
= newval
;
553 void helper_mttc0_vpecontrol(CPUMIPSState
*env
, target_ulong arg1
)
555 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
556 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
560 mask
= (1 << CP0VPECo_YSI
) | (1 << CP0VPECo_GSI
) |
561 (1 << CP0VPECo_TE
) | (0xff << CP0VPECo_TargTC
);
562 newval
= (other
->CP0_VPEControl
& ~mask
) | (arg1
& mask
);
564 /* TODO: Enable/disable TCs. */
566 other
->CP0_VPEControl
= newval
;
569 target_ulong
helper_mftc0_vpecontrol(CPUMIPSState
*env
)
571 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
572 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
573 /* FIXME: Mask away return zero on read bits. */
574 return other
->CP0_VPEControl
;
577 target_ulong
helper_mftc0_vpeconf0(CPUMIPSState
*env
)
579 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
580 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
582 return other
->CP0_VPEConf0
;
585 void helper_mtc0_vpeconf0(CPUMIPSState
*env
, target_ulong arg1
)
590 if (env
->CP0_VPEConf0
& (1 << CP0VPEC0_MVP
)) {
591 if (env
->CP0_VPEConf0
& (1 << CP0VPEC0_VPA
)) {
592 mask
|= (0xff << CP0VPEC0_XTC
);
594 mask
|= (1 << CP0VPEC0_MVP
) | (1 << CP0VPEC0_VPA
);
596 newval
= (env
->CP0_VPEConf0
& ~mask
) | (arg1
& mask
);
598 /* TODO: TC exclusive handling due to ERL/EXL. */
600 env
->CP0_VPEConf0
= newval
;
603 void helper_mttc0_vpeconf0(CPUMIPSState
*env
, target_ulong arg1
)
605 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
606 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
610 mask
|= (1 << CP0VPEC0_MVP
) | (1 << CP0VPEC0_VPA
);
611 newval
= (other
->CP0_VPEConf0
& ~mask
) | (arg1
& mask
);
613 /* TODO: TC exclusive handling due to ERL/EXL. */
614 other
->CP0_VPEConf0
= newval
;
617 void helper_mtc0_vpeconf1(CPUMIPSState
*env
, target_ulong arg1
)
622 if (env
->mvp
->CP0_MVPControl
& (1 << CP0MVPCo_VPC
))
623 mask
|= (0xff << CP0VPEC1_NCX
) | (0xff << CP0VPEC1_NCP2
) |
624 (0xff << CP0VPEC1_NCP1
);
625 newval
= (env
->CP0_VPEConf1
& ~mask
) | (arg1
& mask
);
627 /* UDI not implemented. */
628 /* CP2 not implemented. */
630 /* TODO: Handle FPU (CP1) binding. */
632 env
->CP0_VPEConf1
= newval
;
635 void helper_mtc0_yqmask(CPUMIPSState
*env
, target_ulong arg1
)
637 /* Yield qualifier inputs not implemented. */
638 env
->CP0_YQMask
= 0x00000000;
641 void helper_mtc0_vpeopt(CPUMIPSState
*env
, target_ulong arg1
)
643 env
->CP0_VPEOpt
= arg1
& 0x0000ffff;
646 #define MTC0_ENTRYLO_MASK(env) ((env->PAMask >> 6) & 0x3FFFFFFF)
648 void helper_mtc0_entrylo0(CPUMIPSState
*env
, target_ulong arg1
)
650 /* 1k pages not implemented */
651 target_ulong rxi
= arg1
& (env
->CP0_PageGrain
& (3u << CP0PG_XIE
));
652 env
->CP0_EntryLo0
= (arg1
& MTC0_ENTRYLO_MASK(env
))
653 | (rxi
<< (CP0EnLo_XI
- 30));
656 #if defined(TARGET_MIPS64)
657 #define DMTC0_ENTRYLO_MASK(env) (env->PAMask >> 6)
659 void helper_dmtc0_entrylo0(CPUMIPSState
*env
, uint64_t arg1
)
661 uint64_t rxi
= arg1
& ((env
->CP0_PageGrain
& (3ull << CP0PG_XIE
)) << 32);
662 env
->CP0_EntryLo0
= (arg1
& DMTC0_ENTRYLO_MASK(env
)) | rxi
;
666 void helper_mtc0_tcstatus(CPUMIPSState
*env
, target_ulong arg1
)
668 uint32_t mask
= env
->CP0_TCStatus_rw_bitmask
;
671 newval
= (env
->active_tc
.CP0_TCStatus
& ~mask
) | (arg1
& mask
);
673 env
->active_tc
.CP0_TCStatus
= newval
;
674 sync_c0_tcstatus(env
, env
->current_tc
, newval
);
677 void helper_mttc0_tcstatus(CPUMIPSState
*env
, target_ulong arg1
)
679 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
680 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
682 if (other_tc
== other
->current_tc
) {
683 other
->active_tc
.CP0_TCStatus
= arg1
;
685 other
->tcs
[other_tc
].CP0_TCStatus
= arg1
;
687 sync_c0_tcstatus(other
, other_tc
, arg1
);
690 void helper_mtc0_tcbind(CPUMIPSState
*env
, target_ulong arg1
)
692 uint32_t mask
= (1 << CP0TCBd_TBE
);
695 if (env
->mvp
->CP0_MVPControl
& (1 << CP0MVPCo_VPC
)) {
696 mask
|= (1 << CP0TCBd_CurVPE
);
698 newval
= (env
->active_tc
.CP0_TCBind
& ~mask
) | (arg1
& mask
);
699 env
->active_tc
.CP0_TCBind
= newval
;
702 void helper_mttc0_tcbind(CPUMIPSState
*env
, target_ulong arg1
)
704 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
705 uint32_t mask
= (1 << CP0TCBd_TBE
);
707 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
709 if (other
->mvp
->CP0_MVPControl
& (1 << CP0MVPCo_VPC
)) {
710 mask
|= (1 << CP0TCBd_CurVPE
);
712 if (other_tc
== other
->current_tc
) {
713 newval
= (other
->active_tc
.CP0_TCBind
& ~mask
) | (arg1
& mask
);
714 other
->active_tc
.CP0_TCBind
= newval
;
716 newval
= (other
->tcs
[other_tc
].CP0_TCBind
& ~mask
) | (arg1
& mask
);
717 other
->tcs
[other_tc
].CP0_TCBind
= newval
;
721 void helper_mtc0_tcrestart(CPUMIPSState
*env
, target_ulong arg1
)
723 env
->active_tc
.PC
= arg1
;
724 env
->active_tc
.CP0_TCStatus
&= ~(1 << CP0TCSt_TDS
);
727 /* MIPS16 not implemented. */
730 void helper_mttc0_tcrestart(CPUMIPSState
*env
, target_ulong arg1
)
732 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
733 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
735 if (other_tc
== other
->current_tc
) {
736 other
->active_tc
.PC
= arg1
;
737 other
->active_tc
.CP0_TCStatus
&= ~(1 << CP0TCSt_TDS
);
738 other
->CP0_LLAddr
= 0;
740 /* MIPS16 not implemented. */
742 other
->tcs
[other_tc
].PC
= arg1
;
743 other
->tcs
[other_tc
].CP0_TCStatus
&= ~(1 << CP0TCSt_TDS
);
744 other
->CP0_LLAddr
= 0;
746 /* MIPS16 not implemented. */
750 void helper_mtc0_tchalt(CPUMIPSState
*env
, target_ulong arg1
)
752 MIPSCPU
*cpu
= env_archcpu(env
);
754 env
->active_tc
.CP0_TCHalt
= arg1
& 0x1;
756 /* TODO: Halt TC / Restart (if allocated+active) TC. */
757 if (env
->active_tc
.CP0_TCHalt
& 1) {
758 mips_tc_sleep(cpu
, env
->current_tc
);
760 mips_tc_wake(cpu
, env
->current_tc
);
764 void helper_mttc0_tchalt(CPUMIPSState
*env
, target_ulong arg1
)
766 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
767 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
768 MIPSCPU
*other_cpu
= env_archcpu(other
);
770 /* TODO: Halt TC / Restart (if allocated+active) TC. */
772 if (other_tc
== other
->current_tc
) {
773 other
->active_tc
.CP0_TCHalt
= arg1
;
775 other
->tcs
[other_tc
].CP0_TCHalt
= arg1
;
779 mips_tc_sleep(other_cpu
, other_tc
);
781 mips_tc_wake(other_cpu
, other_tc
);
785 void helper_mtc0_tccontext(CPUMIPSState
*env
, target_ulong arg1
)
787 env
->active_tc
.CP0_TCContext
= arg1
;
790 void helper_mttc0_tccontext(CPUMIPSState
*env
, target_ulong arg1
)
792 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
793 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
795 if (other_tc
== other
->current_tc
) {
796 other
->active_tc
.CP0_TCContext
= arg1
;
798 other
->tcs
[other_tc
].CP0_TCContext
= arg1
;
802 void helper_mtc0_tcschedule(CPUMIPSState
*env
, target_ulong arg1
)
804 env
->active_tc
.CP0_TCSchedule
= arg1
;
807 void helper_mttc0_tcschedule(CPUMIPSState
*env
, target_ulong arg1
)
809 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
810 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
812 if (other_tc
== other
->current_tc
) {
813 other
->active_tc
.CP0_TCSchedule
= arg1
;
815 other
->tcs
[other_tc
].CP0_TCSchedule
= arg1
;
819 void helper_mtc0_tcschefback(CPUMIPSState
*env
, target_ulong arg1
)
821 env
->active_tc
.CP0_TCScheFBack
= arg1
;
824 void helper_mttc0_tcschefback(CPUMIPSState
*env
, target_ulong arg1
)
826 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
827 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
829 if (other_tc
== other
->current_tc
) {
830 other
->active_tc
.CP0_TCScheFBack
= arg1
;
832 other
->tcs
[other_tc
].CP0_TCScheFBack
= arg1
;
836 void helper_mtc0_entrylo1(CPUMIPSState
*env
, target_ulong arg1
)
838 /* 1k pages not implemented */
839 target_ulong rxi
= arg1
& (env
->CP0_PageGrain
& (3u << CP0PG_XIE
));
840 env
->CP0_EntryLo1
= (arg1
& MTC0_ENTRYLO_MASK(env
))
841 | (rxi
<< (CP0EnLo_XI
- 30));
844 #if defined(TARGET_MIPS64)
845 void helper_dmtc0_entrylo1(CPUMIPSState
*env
, uint64_t arg1
)
847 uint64_t rxi
= arg1
& ((env
->CP0_PageGrain
& (3ull << CP0PG_XIE
)) << 32);
848 env
->CP0_EntryLo1
= (arg1
& DMTC0_ENTRYLO_MASK(env
)) | rxi
;
852 void helper_mtc0_context(CPUMIPSState
*env
, target_ulong arg1
)
854 env
->CP0_Context
= (env
->CP0_Context
& 0x007FFFFF) | (arg1
& ~0x007FFFFF);
857 void helper_mtc0_memorymapid(CPUMIPSState
*env
, target_ulong arg1
)
860 old
= env
->CP0_MemoryMapID
;
861 env
->CP0_MemoryMapID
= (int32_t) arg1
;
862 /* If the MemoryMapID changes, flush qemu's TLB. */
863 if (old
!= env
->CP0_MemoryMapID
) {
864 cpu_mips_tlb_flush(env
);
868 void update_pagemask(CPUMIPSState
*env
, target_ulong arg1
, int32_t *pagemask
)
870 uint64_t mask
= arg1
>> (TARGET_PAGE_BITS
+ 1);
871 if (!(env
->insn_flags
& ISA_MIPS32R6
) || (arg1
== ~0) ||
872 (mask
== 0x0000 || mask
== 0x0003 || mask
== 0x000F ||
873 mask
== 0x003F || mask
== 0x00FF || mask
== 0x03FF ||
874 mask
== 0x0FFF || mask
== 0x3FFF || mask
== 0xFFFF)) {
875 env
->CP0_PageMask
= arg1
& (0x1FFFFFFF & (TARGET_PAGE_MASK
<< 1));
879 void helper_mtc0_pagemask(CPUMIPSState
*env
, target_ulong arg1
)
881 update_pagemask(env
, arg1
, &env
->CP0_PageMask
);
884 void helper_mtc0_pagegrain(CPUMIPSState
*env
, target_ulong arg1
)
886 /* SmartMIPS not implemented */
887 /* 1k pages not implemented */
888 env
->CP0_PageGrain
= (arg1
& env
->CP0_PageGrain_rw_bitmask
) |
889 (env
->CP0_PageGrain
& ~env
->CP0_PageGrain_rw_bitmask
);
894 void helper_mtc0_segctl0(CPUMIPSState
*env
, target_ulong arg1
)
896 CPUState
*cs
= env_cpu(env
);
898 env
->CP0_SegCtl0
= arg1
& CP0SC0_MASK
;
902 void helper_mtc0_segctl1(CPUMIPSState
*env
, target_ulong arg1
)
904 CPUState
*cs
= env_cpu(env
);
906 env
->CP0_SegCtl1
= arg1
& CP0SC1_MASK
;
910 void helper_mtc0_segctl2(CPUMIPSState
*env
, target_ulong arg1
)
912 CPUState
*cs
= env_cpu(env
);
914 env
->CP0_SegCtl2
= arg1
& CP0SC2_MASK
;
918 void helper_mtc0_pwfield(CPUMIPSState
*env
, target_ulong arg1
)
920 #if defined(TARGET_MIPS64)
921 uint64_t mask
= 0x3F3FFFFFFFULL
;
922 uint32_t old_ptei
= (env
->CP0_PWField
>> CP0PF_PTEI
) & 0x3FULL
;
923 uint32_t new_ptei
= (arg1
>> CP0PF_PTEI
) & 0x3FULL
;
925 if ((env
->insn_flags
& ISA_MIPS32R6
)) {
926 if (((arg1
>> CP0PF_BDI
) & 0x3FULL
) < 12) {
927 mask
&= ~(0x3FULL
<< CP0PF_BDI
);
929 if (((arg1
>> CP0PF_GDI
) & 0x3FULL
) < 12) {
930 mask
&= ~(0x3FULL
<< CP0PF_GDI
);
932 if (((arg1
>> CP0PF_UDI
) & 0x3FULL
) < 12) {
933 mask
&= ~(0x3FULL
<< CP0PF_UDI
);
935 if (((arg1
>> CP0PF_MDI
) & 0x3FULL
) < 12) {
936 mask
&= ~(0x3FULL
<< CP0PF_MDI
);
938 if (((arg1
>> CP0PF_PTI
) & 0x3FULL
) < 12) {
939 mask
&= ~(0x3FULL
<< CP0PF_PTI
);
942 env
->CP0_PWField
= arg1
& mask
;
944 if ((new_ptei
>= 32) ||
945 ((env
->insn_flags
& ISA_MIPS32R6
) &&
946 (new_ptei
== 0 || new_ptei
== 1))) {
947 env
->CP0_PWField
= (env
->CP0_PWField
& ~0x3FULL
) |
948 (old_ptei
<< CP0PF_PTEI
);
951 uint32_t mask
= 0x3FFFFFFF;
952 uint32_t old_ptew
= (env
->CP0_PWField
>> CP0PF_PTEW
) & 0x3F;
953 uint32_t new_ptew
= (arg1
>> CP0PF_PTEW
) & 0x3F;
955 if ((env
->insn_flags
& ISA_MIPS32R6
)) {
956 if (((arg1
>> CP0PF_GDW
) & 0x3F) < 12) {
957 mask
&= ~(0x3F << CP0PF_GDW
);
959 if (((arg1
>> CP0PF_UDW
) & 0x3F) < 12) {
960 mask
&= ~(0x3F << CP0PF_UDW
);
962 if (((arg1
>> CP0PF_MDW
) & 0x3F) < 12) {
963 mask
&= ~(0x3F << CP0PF_MDW
);
965 if (((arg1
>> CP0PF_PTW
) & 0x3F) < 12) {
966 mask
&= ~(0x3F << CP0PF_PTW
);
969 env
->CP0_PWField
= arg1
& mask
;
971 if ((new_ptew
>= 32) ||
972 ((env
->insn_flags
& ISA_MIPS32R6
) &&
973 (new_ptew
== 0 || new_ptew
== 1))) {
974 env
->CP0_PWField
= (env
->CP0_PWField
& ~0x3F) |
975 (old_ptew
<< CP0PF_PTEW
);
980 void helper_mtc0_pwsize(CPUMIPSState
*env
, target_ulong arg1
)
982 #if defined(TARGET_MIPS64)
983 env
->CP0_PWSize
= arg1
& 0x3F7FFFFFFFULL
;
985 env
->CP0_PWSize
= arg1
& 0x3FFFFFFF;
989 void helper_mtc0_wired(CPUMIPSState
*env
, target_ulong arg1
)
991 if (env
->insn_flags
& ISA_MIPS32R6
) {
992 if (arg1
< env
->tlb
->nb_tlb
) {
993 env
->CP0_Wired
= arg1
;
996 env
->CP0_Wired
= arg1
% env
->tlb
->nb_tlb
;
1000 void helper_mtc0_pwctl(CPUMIPSState
*env
, target_ulong arg1
)
1002 #if defined(TARGET_MIPS64)
1003 /* PWEn = 0. Hardware page table walking is not implemented. */
1004 env
->CP0_PWCtl
= (env
->CP0_PWCtl
& 0x000000C0) | (arg1
& 0x5C00003F);
1006 env
->CP0_PWCtl
= (arg1
& 0x800000FF);
1010 void helper_mtc0_srsconf0(CPUMIPSState
*env
, target_ulong arg1
)
1012 env
->CP0_SRSConf0
|= arg1
& env
->CP0_SRSConf0_rw_bitmask
;
1015 void helper_mtc0_srsconf1(CPUMIPSState
*env
, target_ulong arg1
)
1017 env
->CP0_SRSConf1
|= arg1
& env
->CP0_SRSConf1_rw_bitmask
;
1020 void helper_mtc0_srsconf2(CPUMIPSState
*env
, target_ulong arg1
)
1022 env
->CP0_SRSConf2
|= arg1
& env
->CP0_SRSConf2_rw_bitmask
;
1025 void helper_mtc0_srsconf3(CPUMIPSState
*env
, target_ulong arg1
)
1027 env
->CP0_SRSConf3
|= arg1
& env
->CP0_SRSConf3_rw_bitmask
;
1030 void helper_mtc0_srsconf4(CPUMIPSState
*env
, target_ulong arg1
)
1032 env
->CP0_SRSConf4
|= arg1
& env
->CP0_SRSConf4_rw_bitmask
;
1035 void helper_mtc0_hwrena(CPUMIPSState
*env
, target_ulong arg1
)
1037 uint32_t mask
= 0x0000000F;
1039 if ((env
->CP0_Config1
& (1 << CP0C1_PC
)) &&
1040 (env
->insn_flags
& ISA_MIPS32R6
)) {
1043 if (env
->insn_flags
& ISA_MIPS32R6
) {
1046 if (env
->CP0_Config3
& (1 << CP0C3_ULRI
)) {
1049 if (arg1
& (1 << 29)) {
1050 env
->hflags
|= MIPS_HFLAG_HWRENA_ULR
;
1052 env
->hflags
&= ~MIPS_HFLAG_HWRENA_ULR
;
1056 env
->CP0_HWREna
= arg1
& mask
;
1059 void helper_mtc0_count(CPUMIPSState
*env
, target_ulong arg1
)
1061 cpu_mips_store_count(env
, arg1
);
1064 void helper_mtc0_saari(CPUMIPSState
*env
, target_ulong arg1
)
1066 uint32_t target
= arg1
& 0x3f;
1068 env
->CP0_SAARI
= target
;
1072 void helper_mtc0_saar(CPUMIPSState
*env
, target_ulong arg1
)
1074 uint32_t target
= env
->CP0_SAARI
& 0x3f;
1076 env
->CP0_SAAR
[target
] = arg1
& 0x00000ffffffff03fULL
;
1080 itc_reconfigure(env
->itu
);
1087 void helper_mthc0_saar(CPUMIPSState
*env
, target_ulong arg1
)
1089 uint32_t target
= env
->CP0_SAARI
& 0x3f;
1091 env
->CP0_SAAR
[target
] =
1092 (((uint64_t) arg1
<< 32) & 0x00000fff00000000ULL
) |
1093 (env
->CP0_SAAR
[target
] & 0x00000000ffffffffULL
);
1097 itc_reconfigure(env
->itu
);
1104 void helper_mtc0_entryhi(CPUMIPSState
*env
, target_ulong arg1
)
1106 target_ulong old
, val
, mask
;
1107 mask
= (TARGET_PAGE_MASK
<< 1) | env
->CP0_EntryHi_ASID_mask
;
1108 if (((env
->CP0_Config4
>> CP0C4_IE
) & 0x3) >= 2) {
1109 mask
|= 1 << CP0EnHi_EHINV
;
1112 /* 1k pages not implemented */
1113 #if defined(TARGET_MIPS64)
1114 if (env
->insn_flags
& ISA_MIPS32R6
) {
1115 int entryhi_r
= extract64(arg1
, 62, 2);
1116 int config0_at
= extract32(env
->CP0_Config0
, 13, 2);
1117 bool no_supervisor
= (env
->CP0_Status_rw_bitmask
& 0x8) == 0;
1118 if ((entryhi_r
== 2) ||
1119 (entryhi_r
== 1 && (no_supervisor
|| config0_at
== 1))) {
1120 /* skip EntryHi.R field if new value is reserved */
1121 mask
&= ~(0x3ull
<< 62);
1124 mask
&= env
->SEGMask
;
1126 old
= env
->CP0_EntryHi
;
1127 val
= (arg1
& mask
) | (old
& ~mask
);
1128 env
->CP0_EntryHi
= val
;
1129 if (env
->CP0_Config3
& (1 << CP0C3_MT
)) {
1130 sync_c0_entryhi(env
, env
->current_tc
);
1132 /* If the ASID changes, flush qemu's TLB. */
1133 if ((old
& env
->CP0_EntryHi_ASID_mask
) !=
1134 (val
& env
->CP0_EntryHi_ASID_mask
)) {
1135 tlb_flush(env_cpu(env
));
1139 void helper_mttc0_entryhi(CPUMIPSState
*env
, target_ulong arg1
)
1141 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1142 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1144 other
->CP0_EntryHi
= arg1
;
1145 sync_c0_entryhi(other
, other_tc
);
1148 void helper_mtc0_compare(CPUMIPSState
*env
, target_ulong arg1
)
1150 cpu_mips_store_compare(env
, arg1
);
1153 void helper_mtc0_status(CPUMIPSState
*env
, target_ulong arg1
)
1157 old
= env
->CP0_Status
;
1158 cpu_mips_store_status(env
, arg1
);
1159 val
= env
->CP0_Status
;
1161 if (qemu_loglevel_mask(CPU_LOG_EXEC
)) {
1162 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1163 old
, old
& env
->CP0_Cause
& CP0Ca_IP_mask
,
1164 val
, val
& env
->CP0_Cause
& CP0Ca_IP_mask
,
1166 switch (cpu_mmu_index(env
, false)) {
1168 qemu_log(", ERL\n");
1180 cpu_abort(env_cpu(env
), "Invalid MMU mode!\n");
1186 void helper_mttc0_status(CPUMIPSState
*env
, target_ulong arg1
)
1188 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1189 uint32_t mask
= env
->CP0_Status_rw_bitmask
& ~0xf1000018;
1190 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1192 other
->CP0_Status
= (other
->CP0_Status
& ~mask
) | (arg1
& mask
);
1193 sync_c0_status(env
, other
, other_tc
);
1196 void helper_mtc0_intctl(CPUMIPSState
*env
, target_ulong arg1
)
1198 env
->CP0_IntCtl
= (env
->CP0_IntCtl
& ~0x000003e0) | (arg1
& 0x000003e0);
1201 void helper_mtc0_srsctl(CPUMIPSState
*env
, target_ulong arg1
)
1203 uint32_t mask
= (0xf << CP0SRSCtl_ESS
) | (0xf << CP0SRSCtl_PSS
);
1204 env
->CP0_SRSCtl
= (env
->CP0_SRSCtl
& ~mask
) | (arg1
& mask
);
1207 void helper_mtc0_cause(CPUMIPSState
*env
, target_ulong arg1
)
1209 cpu_mips_store_cause(env
, arg1
);
1212 void helper_mttc0_cause(CPUMIPSState
*env
, target_ulong arg1
)
1214 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1215 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1217 cpu_mips_store_cause(other
, arg1
);
1220 target_ulong
helper_mftc0_epc(CPUMIPSState
*env
)
1222 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1223 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1225 return other
->CP0_EPC
;
1228 target_ulong
helper_mftc0_ebase(CPUMIPSState
*env
)
1230 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1231 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1233 return other
->CP0_EBase
;
1236 void helper_mtc0_ebase(CPUMIPSState
*env
, target_ulong arg1
)
1238 target_ulong mask
= 0x3FFFF000 | env
->CP0_EBaseWG_rw_bitmask
;
1239 if (arg1
& env
->CP0_EBaseWG_rw_bitmask
) {
1240 mask
|= ~0x3FFFFFFF;
1242 env
->CP0_EBase
= (env
->CP0_EBase
& ~mask
) | (arg1
& mask
);
1245 void helper_mttc0_ebase(CPUMIPSState
*env
, target_ulong arg1
)
1247 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1248 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1249 target_ulong mask
= 0x3FFFF000 | env
->CP0_EBaseWG_rw_bitmask
;
1250 if (arg1
& env
->CP0_EBaseWG_rw_bitmask
) {
1251 mask
|= ~0x3FFFFFFF;
1253 other
->CP0_EBase
= (other
->CP0_EBase
& ~mask
) | (arg1
& mask
);
1256 target_ulong
helper_mftc0_configx(CPUMIPSState
*env
, target_ulong idx
)
1258 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1259 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1262 case 0: return other
->CP0_Config0
;
1263 case 1: return other
->CP0_Config1
;
1264 case 2: return other
->CP0_Config2
;
1265 case 3: return other
->CP0_Config3
;
1266 /* 4 and 5 are reserved. */
1267 case 6: return other
->CP0_Config6
;
1268 case 7: return other
->CP0_Config7
;
1275 void helper_mtc0_config0(CPUMIPSState
*env
, target_ulong arg1
)
1277 env
->CP0_Config0
= (env
->CP0_Config0
& 0x81FFFFF8) | (arg1
& 0x00000007);
1280 void helper_mtc0_config2(CPUMIPSState
*env
, target_ulong arg1
)
1282 /* tertiary/secondary caches not implemented */
1283 env
->CP0_Config2
= (env
->CP0_Config2
& 0x8FFF0FFF);
1286 void helper_mtc0_config3(CPUMIPSState
*env
, target_ulong arg1
)
1288 if (env
->insn_flags
& ASE_MICROMIPS
) {
1289 env
->CP0_Config3
= (env
->CP0_Config3
& ~(1 << CP0C3_ISA_ON_EXC
)) |
1290 (arg1
& (1 << CP0C3_ISA_ON_EXC
));
1294 void helper_mtc0_config4(CPUMIPSState
*env
, target_ulong arg1
)
1296 env
->CP0_Config4
= (env
->CP0_Config4
& (~env
->CP0_Config4_rw_bitmask
)) |
1297 (arg1
& env
->CP0_Config4_rw_bitmask
);
1300 void helper_mtc0_config5(CPUMIPSState
*env
, target_ulong arg1
)
1302 env
->CP0_Config5
= (env
->CP0_Config5
& (~env
->CP0_Config5_rw_bitmask
)) |
1303 (arg1
& env
->CP0_Config5_rw_bitmask
);
1304 env
->CP0_EntryHi_ASID_mask
= (env
->CP0_Config5
& (1 << CP0C5_MI
)) ?
1305 0x0 : (env
->CP0_Config4
& (1 << CP0C4_AE
)) ? 0x3ff : 0xff;
1306 compute_hflags(env
);
1309 void helper_mtc0_lladdr(CPUMIPSState
*env
, target_ulong arg1
)
1311 target_long mask
= env
->CP0_LLAddr_rw_bitmask
;
1312 arg1
= arg1
<< env
->CP0_LLAddr_shift
;
1313 env
->CP0_LLAddr
= (env
->CP0_LLAddr
& ~mask
) | (arg1
& mask
);
1316 #define MTC0_MAAR_MASK(env) \
1317 ((0x1ULL << 63) | ((env->PAMask >> 4) & ~0xFFFull) | 0x3)
1319 void helper_mtc0_maar(CPUMIPSState
*env
, target_ulong arg1
)
1321 env
->CP0_MAAR
[env
->CP0_MAARI
] = arg1
& MTC0_MAAR_MASK(env
);
1324 void helper_mthc0_maar(CPUMIPSState
*env
, target_ulong arg1
)
1326 env
->CP0_MAAR
[env
->CP0_MAARI
] =
1327 (((uint64_t) arg1
<< 32) & MTC0_MAAR_MASK(env
)) |
1328 (env
->CP0_MAAR
[env
->CP0_MAARI
] & 0x00000000ffffffffULL
);
1331 void helper_mtc0_maari(CPUMIPSState
*env
, target_ulong arg1
)
1333 int index
= arg1
& 0x3f;
1334 if (index
== 0x3f) {
1336 * Software may write all ones to INDEX to determine the
1337 * maximum value supported.
1339 env
->CP0_MAARI
= MIPS_MAAR_MAX
- 1;
1340 } else if (index
< MIPS_MAAR_MAX
) {
1341 env
->CP0_MAARI
= index
;
1344 * Other than the all ones, if the value written is not supported,
1345 * then INDEX is unchanged from its previous value.
1349 void helper_mtc0_watchlo(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1352 * Watch exceptions for instructions, data loads, data stores
1355 env
->CP0_WatchLo
[sel
] = (arg1
& ~0x7);
1358 void helper_mtc0_watchhi(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1360 uint64_t mask
= 0x40000FF8 | (env
->CP0_EntryHi_ASID_mask
<< CP0WH_ASID
);
1361 if ((env
->CP0_Config5
>> CP0C5_MI
) & 1) {
1362 mask
|= 0xFFFFFFFF00000000ULL
; /* MMID */
1364 env
->CP0_WatchHi
[sel
] = arg1
& mask
;
1365 env
->CP0_WatchHi
[sel
] &= ~(env
->CP0_WatchHi
[sel
] & arg1
& 0x7);
1368 void helper_mthc0_watchhi(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1370 env
->CP0_WatchHi
[sel
] = ((uint64_t) (arg1
) << 32) |
1371 (env
->CP0_WatchHi
[sel
] & 0x00000000ffffffffULL
);
1374 void helper_mtc0_xcontext(CPUMIPSState
*env
, target_ulong arg1
)
1376 target_ulong mask
= (1ULL << (env
->SEGBITS
- 7)) - 1;
1377 env
->CP0_XContext
= (env
->CP0_XContext
& mask
) | (arg1
& ~mask
);
1380 void helper_mtc0_framemask(CPUMIPSState
*env
, target_ulong arg1
)
1382 env
->CP0_Framemask
= arg1
; /* XXX */
1385 void helper_mtc0_debug(CPUMIPSState
*env
, target_ulong arg1
)
1387 env
->CP0_Debug
= (env
->CP0_Debug
& 0x8C03FC1F) | (arg1
& 0x13300120);
1388 if (arg1
& (1 << CP0DB_DM
)) {
1389 env
->hflags
|= MIPS_HFLAG_DM
;
1391 env
->hflags
&= ~MIPS_HFLAG_DM
;
1395 void helper_mttc0_debug(CPUMIPSState
*env
, target_ulong arg1
)
1397 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1398 uint32_t val
= arg1
& ((1 << CP0DB_SSt
) | (1 << CP0DB_Halt
));
1399 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1401 /* XXX: Might be wrong, check with EJTAG spec. */
1402 if (other_tc
== other
->current_tc
) {
1403 other
->active_tc
.CP0_Debug_tcstatus
= val
;
1405 other
->tcs
[other_tc
].CP0_Debug_tcstatus
= val
;
1407 other
->CP0_Debug
= (other
->CP0_Debug
&
1408 ((1 << CP0DB_SSt
) | (1 << CP0DB_Halt
))) |
1409 (arg1
& ~((1 << CP0DB_SSt
) | (1 << CP0DB_Halt
)));
1412 void helper_mtc0_performance0(CPUMIPSState
*env
, target_ulong arg1
)
1414 env
->CP0_Performance0
= arg1
& 0x000007ff;
1417 void helper_mtc0_errctl(CPUMIPSState
*env
, target_ulong arg1
)
1419 int32_t wst
= arg1
& (1 << CP0EC_WST
);
1420 int32_t spr
= arg1
& (1 << CP0EC_SPR
);
1421 int32_t itc
= env
->itc_tag
? (arg1
& (1 << CP0EC_ITC
)) : 0;
1423 env
->CP0_ErrCtl
= wst
| spr
| itc
;
1425 if (itc
&& !wst
&& !spr
) {
1426 env
->hflags
|= MIPS_HFLAG_ITC_CACHE
;
1428 env
->hflags
&= ~MIPS_HFLAG_ITC_CACHE
;
1432 void helper_mtc0_taglo(CPUMIPSState
*env
, target_ulong arg1
)
1434 if (env
->hflags
& MIPS_HFLAG_ITC_CACHE
) {
1436 * If CACHE instruction is configured for ITC tags then make all
1437 * CP0.TagLo bits writable. The actual write to ITC Configuration
1438 * Tag will take care of the read-only bits.
1440 env
->CP0_TagLo
= arg1
;
1442 env
->CP0_TagLo
= arg1
& 0xFFFFFCF6;
1446 void helper_mtc0_datalo(CPUMIPSState
*env
, target_ulong arg1
)
1448 env
->CP0_DataLo
= arg1
; /* XXX */
1451 void helper_mtc0_taghi(CPUMIPSState
*env
, target_ulong arg1
)
1453 env
->CP0_TagHi
= arg1
; /* XXX */
1456 void helper_mtc0_datahi(CPUMIPSState
*env
, target_ulong arg1
)
1458 env
->CP0_DataHi
= arg1
; /* XXX */
1461 /* MIPS MT functions */
1462 target_ulong
helper_mftgpr(CPUMIPSState
*env
, uint32_t sel
)
1464 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1465 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1467 if (other_tc
== other
->current_tc
) {
1468 return other
->active_tc
.gpr
[sel
];
1470 return other
->tcs
[other_tc
].gpr
[sel
];
1474 target_ulong
helper_mftlo(CPUMIPSState
*env
, uint32_t sel
)
1476 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1477 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1479 if (other_tc
== other
->current_tc
) {
1480 return other
->active_tc
.LO
[sel
];
1482 return other
->tcs
[other_tc
].LO
[sel
];
1486 target_ulong
helper_mfthi(CPUMIPSState
*env
, uint32_t sel
)
1488 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1489 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1491 if (other_tc
== other
->current_tc
) {
1492 return other
->active_tc
.HI
[sel
];
1494 return other
->tcs
[other_tc
].HI
[sel
];
1498 target_ulong
helper_mftacx(CPUMIPSState
*env
, uint32_t sel
)
1500 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1501 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1503 if (other_tc
== other
->current_tc
) {
1504 return other
->active_tc
.ACX
[sel
];
1506 return other
->tcs
[other_tc
].ACX
[sel
];
1510 target_ulong
helper_mftdsp(CPUMIPSState
*env
)
1512 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1513 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1515 if (other_tc
== other
->current_tc
) {
1516 return other
->active_tc
.DSPControl
;
1518 return other
->tcs
[other_tc
].DSPControl
;
1522 void helper_mttgpr(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1524 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1525 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1527 if (other_tc
== other
->current_tc
) {
1528 other
->active_tc
.gpr
[sel
] = arg1
;
1530 other
->tcs
[other_tc
].gpr
[sel
] = arg1
;
1534 void helper_mttlo(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1536 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1537 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1539 if (other_tc
== other
->current_tc
) {
1540 other
->active_tc
.LO
[sel
] = arg1
;
1542 other
->tcs
[other_tc
].LO
[sel
] = arg1
;
1546 void helper_mtthi(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1548 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1549 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1551 if (other_tc
== other
->current_tc
) {
1552 other
->active_tc
.HI
[sel
] = arg1
;
1554 other
->tcs
[other_tc
].HI
[sel
] = arg1
;
1558 void helper_mttacx(CPUMIPSState
*env
, target_ulong arg1
, uint32_t sel
)
1560 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1561 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1563 if (other_tc
== other
->current_tc
) {
1564 other
->active_tc
.ACX
[sel
] = arg1
;
1566 other
->tcs
[other_tc
].ACX
[sel
] = arg1
;
1570 void helper_mttdsp(CPUMIPSState
*env
, target_ulong arg1
)
1572 int other_tc
= env
->CP0_VPEControl
& (0xff << CP0VPECo_TargTC
);
1573 CPUMIPSState
*other
= mips_cpu_map_tc(env
, &other_tc
);
1575 if (other_tc
== other
->current_tc
) {
1576 other
->active_tc
.DSPControl
= arg1
;
1578 other
->tcs
[other_tc
].DSPControl
= arg1
;
1582 /* MIPS MT functions */
1583 target_ulong
helper_dmt(void)
1589 target_ulong
helper_emt(void)
1595 target_ulong
helper_dvpe(CPUMIPSState
*env
)
1597 CPUState
*other_cs
= first_cpu
;
1598 target_ulong prev
= env
->mvp
->CP0_MVPControl
;
1600 CPU_FOREACH(other_cs
) {
1601 MIPSCPU
*other_cpu
= MIPS_CPU(other_cs
);
1602 /* Turn off all VPEs except the one executing the dvpe. */
1603 if (&other_cpu
->env
!= env
) {
1604 other_cpu
->env
.mvp
->CP0_MVPControl
&= ~(1 << CP0MVPCo_EVP
);
1605 mips_vpe_sleep(other_cpu
);
1611 target_ulong
helper_evpe(CPUMIPSState
*env
)
1613 CPUState
*other_cs
= first_cpu
;
1614 target_ulong prev
= env
->mvp
->CP0_MVPControl
;
1616 CPU_FOREACH(other_cs
) {
1617 MIPSCPU
*other_cpu
= MIPS_CPU(other_cs
);
1619 if (&other_cpu
->env
!= env
1620 /* If the VPE is WFI, don't disturb its sleep. */
1621 && !mips_vpe_is_wfi(other_cpu
)) {
1622 /* Enable the VPE. */
1623 other_cpu
->env
.mvp
->CP0_MVPControl
|= (1 << CP0MVPCo_EVP
);
1624 mips_vpe_wake(other_cpu
); /* And wake it up. */
1629 #endif /* !CONFIG_USER_ONLY */
1631 /* R6 Multi-threading */
1632 #ifndef CONFIG_USER_ONLY
1633 target_ulong
helper_dvp(CPUMIPSState
*env
)
1635 CPUState
*other_cs
= first_cpu
;
1636 target_ulong prev
= env
->CP0_VPControl
;
1638 if (!((env
->CP0_VPControl
>> CP0VPCtl_DIS
) & 1)) {
1639 CPU_FOREACH(other_cs
) {
1640 MIPSCPU
*other_cpu
= MIPS_CPU(other_cs
);
1641 /* Turn off all VPs except the one executing the dvp. */
1642 if (&other_cpu
->env
!= env
) {
1643 mips_vpe_sleep(other_cpu
);
1646 env
->CP0_VPControl
|= (1 << CP0VPCtl_DIS
);
1651 target_ulong
helper_evp(CPUMIPSState
*env
)
1653 CPUState
*other_cs
= first_cpu
;
1654 target_ulong prev
= env
->CP0_VPControl
;
1656 if ((env
->CP0_VPControl
>> CP0VPCtl_DIS
) & 1) {
1657 CPU_FOREACH(other_cs
) {
1658 MIPSCPU
*other_cpu
= MIPS_CPU(other_cs
);
1659 if ((&other_cpu
->env
!= env
) && !mips_vp_is_wfi(other_cpu
)) {
1661 * If the VP is WFI, don't disturb its sleep.
1662 * Otherwise, wake it up.
1664 mips_vpe_wake(other_cpu
);
1667 env
->CP0_VPControl
&= ~(1 << CP0VPCtl_DIS
);
1671 #endif /* !CONFIG_USER_ONLY */