]> git.proxmox.com Git - mirror_qemu.git/blob - target/mips/op_helper.c
target/mips: compare virtual addresses in LL/SC sequence
[mirror_qemu.git] / target / mips / op_helper.c
1 /*
2 * MIPS emulation helpers for qemu.
3 *
4 * Copyright (c) 2004-2005 Jocelyn Mayer
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, see <http://www.gnu.org/licenses/>.
18 */
19 #include "qemu/osdep.h"
20 #include "qemu/main-loop.h"
21 #include "cpu.h"
22 #include "internal.h"
23 #include "qemu/host-utils.h"
24 #include "exec/helper-proto.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "sysemu/kvm.h"
28
29 /*****************************************************************************/
30 /* Exceptions processing helpers */
31
32 void helper_raise_exception_err(CPUMIPSState *env, uint32_t exception,
33 int error_code)
34 {
35 do_raise_exception_err(env, exception, error_code, 0);
36 }
37
38 void helper_raise_exception(CPUMIPSState *env, uint32_t exception)
39 {
40 do_raise_exception(env, exception, GETPC());
41 }
42
43 void helper_raise_exception_debug(CPUMIPSState *env)
44 {
45 do_raise_exception(env, EXCP_DEBUG, 0);
46 }
47
48 static void raise_exception(CPUMIPSState *env, uint32_t exception)
49 {
50 do_raise_exception(env, exception, 0);
51 }
52
53 #if defined(CONFIG_USER_ONLY)
54 #define HELPER_LD(name, insn, type) \
55 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
56 int mem_idx, uintptr_t retaddr) \
57 { \
58 return (type) cpu_##insn##_data_ra(env, addr, retaddr); \
59 }
60 #else
61 #define HELPER_LD(name, insn, type) \
62 static inline type do_##name(CPUMIPSState *env, target_ulong addr, \
63 int mem_idx, uintptr_t retaddr) \
64 { \
65 switch (mem_idx) \
66 { \
67 case 0: return (type) cpu_##insn##_kernel_ra(env, addr, retaddr); \
68 case 1: return (type) cpu_##insn##_super_ra(env, addr, retaddr); \
69 default: \
70 case 2: return (type) cpu_##insn##_user_ra(env, addr, retaddr); \
71 case 3: return (type) cpu_##insn##_error_ra(env, addr, retaddr); \
72 } \
73 }
74 #endif
75 HELPER_LD(lw, ldl, int32_t)
76 #if defined(TARGET_MIPS64)
77 HELPER_LD(ld, ldq, int64_t)
78 #endif
79 #undef HELPER_LD
80
81 #if defined(CONFIG_USER_ONLY)
82 #define HELPER_ST(name, insn, type) \
83 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
84 type val, int mem_idx, uintptr_t retaddr) \
85 { \
86 cpu_##insn##_data_ra(env, addr, val, retaddr); \
87 }
88 #else
89 #define HELPER_ST(name, insn, type) \
90 static inline void do_##name(CPUMIPSState *env, target_ulong addr, \
91 type val, int mem_idx, uintptr_t retaddr) \
92 { \
93 switch (mem_idx) \
94 { \
95 case 0: cpu_##insn##_kernel_ra(env, addr, val, retaddr); break; \
96 case 1: cpu_##insn##_super_ra(env, addr, val, retaddr); break; \
97 default: \
98 case 2: cpu_##insn##_user_ra(env, addr, val, retaddr); break; \
99 case 3: \
100 cpu_##insn##_error_ra(env, addr, val, retaddr); \
101 break; \
102 } \
103 }
104 #endif
105 HELPER_ST(sb, stb, uint8_t)
106 HELPER_ST(sw, stl, uint32_t)
107 #if defined(TARGET_MIPS64)
108 HELPER_ST(sd, stq, uint64_t)
109 #endif
110 #undef HELPER_ST
111
112 /* 64 bits arithmetic for 32 bits hosts */
113 static inline uint64_t get_HILO(CPUMIPSState *env)
114 {
115 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
116 }
117
118 static inline target_ulong set_HIT0_LO(CPUMIPSState *env, uint64_t HILO)
119 {
120 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
121 return env->active_tc.HI[0] = (int32_t)(HILO >> 32);
122 }
123
124 static inline target_ulong set_HI_LOT0(CPUMIPSState *env, uint64_t HILO)
125 {
126 target_ulong tmp = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
127 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
128 return tmp;
129 }
130
131 /* Multiplication variants of the vr54xx. */
132 target_ulong helper_muls(CPUMIPSState *env, target_ulong arg1,
133 target_ulong arg2)
134 {
135 return set_HI_LOT0(env, 0 - ((int64_t)(int32_t)arg1 *
136 (int64_t)(int32_t)arg2));
137 }
138
139 target_ulong helper_mulsu(CPUMIPSState *env, target_ulong arg1,
140 target_ulong arg2)
141 {
142 return set_HI_LOT0(env, 0 - (uint64_t)(uint32_t)arg1 *
143 (uint64_t)(uint32_t)arg2);
144 }
145
146 target_ulong helper_macc(CPUMIPSState *env, target_ulong arg1,
147 target_ulong arg2)
148 {
149 return set_HI_LOT0(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
150 (int64_t)(int32_t)arg2);
151 }
152
153 target_ulong helper_macchi(CPUMIPSState *env, target_ulong arg1,
154 target_ulong arg2)
155 {
156 return set_HIT0_LO(env, (int64_t)get_HILO(env) + (int64_t)(int32_t)arg1 *
157 (int64_t)(int32_t)arg2);
158 }
159
160 target_ulong helper_maccu(CPUMIPSState *env, target_ulong arg1,
161 target_ulong arg2)
162 {
163 return set_HI_LOT0(env, (uint64_t)get_HILO(env) +
164 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
165 }
166
167 target_ulong helper_macchiu(CPUMIPSState *env, target_ulong arg1,
168 target_ulong arg2)
169 {
170 return set_HIT0_LO(env, (uint64_t)get_HILO(env) +
171 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
172 }
173
174 target_ulong helper_msac(CPUMIPSState *env, target_ulong arg1,
175 target_ulong arg2)
176 {
177 return set_HI_LOT0(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
178 (int64_t)(int32_t)arg2);
179 }
180
181 target_ulong helper_msachi(CPUMIPSState *env, target_ulong arg1,
182 target_ulong arg2)
183 {
184 return set_HIT0_LO(env, (int64_t)get_HILO(env) - (int64_t)(int32_t)arg1 *
185 (int64_t)(int32_t)arg2);
186 }
187
188 target_ulong helper_msacu(CPUMIPSState *env, target_ulong arg1,
189 target_ulong arg2)
190 {
191 return set_HI_LOT0(env, (uint64_t)get_HILO(env) -
192 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
193 }
194
195 target_ulong helper_msachiu(CPUMIPSState *env, target_ulong arg1,
196 target_ulong arg2)
197 {
198 return set_HIT0_LO(env, (uint64_t)get_HILO(env) -
199 (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
200 }
201
202 target_ulong helper_mulhi(CPUMIPSState *env, target_ulong arg1,
203 target_ulong arg2)
204 {
205 return set_HIT0_LO(env, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
206 }
207
208 target_ulong helper_mulhiu(CPUMIPSState *env, target_ulong arg1,
209 target_ulong arg2)
210 {
211 return set_HIT0_LO(env, (uint64_t)(uint32_t)arg1 *
212 (uint64_t)(uint32_t)arg2);
213 }
214
215 target_ulong helper_mulshi(CPUMIPSState *env, target_ulong arg1,
216 target_ulong arg2)
217 {
218 return set_HIT0_LO(env, 0 - (int64_t)(int32_t)arg1 *
219 (int64_t)(int32_t)arg2);
220 }
221
222 target_ulong helper_mulshiu(CPUMIPSState *env, target_ulong arg1,
223 target_ulong arg2)
224 {
225 return set_HIT0_LO(env, 0 - (uint64_t)(uint32_t)arg1 *
226 (uint64_t)(uint32_t)arg2);
227 }
228
229 static inline target_ulong bitswap(target_ulong v)
230 {
231 v = ((v >> 1) & (target_ulong)0x5555555555555555ULL) |
232 ((v & (target_ulong)0x5555555555555555ULL) << 1);
233 v = ((v >> 2) & (target_ulong)0x3333333333333333ULL) |
234 ((v & (target_ulong)0x3333333333333333ULL) << 2);
235 v = ((v >> 4) & (target_ulong)0x0F0F0F0F0F0F0F0FULL) |
236 ((v & (target_ulong)0x0F0F0F0F0F0F0F0FULL) << 4);
237 return v;
238 }
239
240 #ifdef TARGET_MIPS64
241 target_ulong helper_dbitswap(target_ulong rt)
242 {
243 return bitswap(rt);
244 }
245 #endif
246
247 target_ulong helper_bitswap(target_ulong rt)
248 {
249 return (int32_t)bitswap(rt);
250 }
251
252 target_ulong helper_rotx(target_ulong rs, uint32_t shift, uint32_t shiftx,
253 uint32_t stripe)
254 {
255 int i;
256 uint64_t tmp0 = ((uint64_t)rs) << 32 | ((uint64_t)rs & 0xffffffff);
257 uint64_t tmp1 = tmp0;
258 for (i = 0; i <= 46; i++) {
259 int s;
260 if (i & 0x8) {
261 s = shift;
262 } else {
263 s = shiftx;
264 }
265
266 if (stripe != 0 && !(i & 0x4)) {
267 s = ~s;
268 }
269 if (s & 0x10) {
270 if (tmp0 & (1LL << (i + 16))) {
271 tmp1 |= 1LL << i;
272 } else {
273 tmp1 &= ~(1LL << i);
274 }
275 }
276 }
277
278 uint64_t tmp2 = tmp1;
279 for (i = 0; i <= 38; i++) {
280 int s;
281 if (i & 0x4) {
282 s = shift;
283 } else {
284 s = shiftx;
285 }
286
287 if (s & 0x8) {
288 if (tmp1 & (1LL << (i + 8))) {
289 tmp2 |= 1LL << i;
290 } else {
291 tmp2 &= ~(1LL << i);
292 }
293 }
294 }
295
296 uint64_t tmp3 = tmp2;
297 for (i = 0; i <= 34; i++) {
298 int s;
299 if (i & 0x2) {
300 s = shift;
301 } else {
302 s = shiftx;
303 }
304 if (s & 0x4) {
305 if (tmp2 & (1LL << (i + 4))) {
306 tmp3 |= 1LL << i;
307 } else {
308 tmp3 &= ~(1LL << i);
309 }
310 }
311 }
312
313 uint64_t tmp4 = tmp3;
314 for (i = 0; i <= 32; i++) {
315 int s;
316 if (i & 0x1) {
317 s = shift;
318 } else {
319 s = shiftx;
320 }
321 if (s & 0x2) {
322 if (tmp3 & (1LL << (i + 2))) {
323 tmp4 |= 1LL << i;
324 } else {
325 tmp4 &= ~(1LL << i);
326 }
327 }
328 }
329
330 uint64_t tmp5 = tmp4;
331 for (i = 0; i <= 31; i++) {
332 int s;
333 s = shift;
334 if (s & 0x1) {
335 if (tmp4 & (1LL << (i + 1))) {
336 tmp5 |= 1LL << i;
337 } else {
338 tmp5 &= ~(1LL << i);
339 }
340 }
341 }
342
343 return (int64_t)(int32_t)(uint32_t)tmp5;
344 }
345
346 #ifndef CONFIG_USER_ONLY
347
348 static inline hwaddr do_translate_address(CPUMIPSState *env,
349 target_ulong address,
350 int rw, uintptr_t retaddr)
351 {
352 hwaddr paddr;
353 CPUState *cs = CPU(mips_env_get_cpu(env));
354
355 paddr = cpu_mips_translate_address(env, address, rw);
356
357 if (paddr == -1LL) {
358 cpu_loop_exit_restore(cs, retaddr);
359 } else {
360 return paddr;
361 }
362 }
363
364 #define HELPER_LD_ATOMIC(name, insn, almask) \
365 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx) \
366 { \
367 if (arg & almask) { \
368 if (!(env->hflags & MIPS_HFLAG_DM)) { \
369 env->CP0_BadVAddr = arg; \
370 } \
371 do_raise_exception(env, EXCP_AdEL, GETPC()); \
372 } \
373 env->CP0_LLAddr = do_translate_address(env, arg, 0, GETPC()); \
374 env->lladdr = arg; \
375 env->llval = do_##insn(env, arg, mem_idx, GETPC()); \
376 return env->llval; \
377 }
378 HELPER_LD_ATOMIC(ll, lw, 0x3)
379 #ifdef TARGET_MIPS64
380 HELPER_LD_ATOMIC(lld, ld, 0x7)
381 #endif
382 #undef HELPER_LD_ATOMIC
383
384 #define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask) \
385 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg1, \
386 target_ulong arg2, int mem_idx) \
387 { \
388 target_long tmp; \
389 \
390 if (arg2 & almask) { \
391 if (!(env->hflags & MIPS_HFLAG_DM)) { \
392 env->CP0_BadVAddr = arg2; \
393 } \
394 do_raise_exception(env, EXCP_AdES, GETPC()); \
395 } \
396 if (arg2 == env->lladdr) { \
397 tmp = do_##ld_insn(env, arg2, mem_idx, GETPC()); \
398 if (tmp == env->llval) { \
399 do_##st_insn(env, arg2, arg1, mem_idx, GETPC()); \
400 return 1; \
401 } \
402 } \
403 return 0; \
404 }
405 HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
406 #ifdef TARGET_MIPS64
407 HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
408 #endif
409 #undef HELPER_ST_ATOMIC
410 #endif
411
412 #ifdef TARGET_WORDS_BIGENDIAN
413 #define GET_LMASK(v) ((v) & 3)
414 #define GET_OFFSET(addr, offset) (addr + (offset))
415 #else
416 #define GET_LMASK(v) (((v) & 3) ^ 3)
417 #define GET_OFFSET(addr, offset) (addr - (offset))
418 #endif
419
420 void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
421 int mem_idx)
422 {
423 do_sb(env, arg2, (uint8_t)(arg1 >> 24), mem_idx, GETPC());
424
425 if (GET_LMASK(arg2) <= 2) {
426 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx,
427 GETPC());
428 }
429
430 if (GET_LMASK(arg2) <= 1) {
431 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx,
432 GETPC());
433 }
434
435 if (GET_LMASK(arg2) == 0) {
436 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx,
437 GETPC());
438 }
439 }
440
441 void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
442 int mem_idx)
443 {
444 do_sb(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
445
446 if (GET_LMASK(arg2) >= 1) {
447 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx,
448 GETPC());
449 }
450
451 if (GET_LMASK(arg2) >= 2) {
452 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx,
453 GETPC());
454 }
455
456 if (GET_LMASK(arg2) == 3) {
457 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx,
458 GETPC());
459 }
460 }
461
462 #if defined(TARGET_MIPS64)
463 /* "half" load and stores. We must do the memory access inline,
464 or fault handling won't work. */
465
466 #ifdef TARGET_WORDS_BIGENDIAN
467 #define GET_LMASK64(v) ((v) & 7)
468 #else
469 #define GET_LMASK64(v) (((v) & 7) ^ 7)
470 #endif
471
472 void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
473 int mem_idx)
474 {
475 do_sb(env, arg2, (uint8_t)(arg1 >> 56), mem_idx, GETPC());
476
477 if (GET_LMASK64(arg2) <= 6) {
478 do_sb(env, GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx,
479 GETPC());
480 }
481
482 if (GET_LMASK64(arg2) <= 5) {
483 do_sb(env, GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx,
484 GETPC());
485 }
486
487 if (GET_LMASK64(arg2) <= 4) {
488 do_sb(env, GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx,
489 GETPC());
490 }
491
492 if (GET_LMASK64(arg2) <= 3) {
493 do_sb(env, GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx,
494 GETPC());
495 }
496
497 if (GET_LMASK64(arg2) <= 2) {
498 do_sb(env, GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx,
499 GETPC());
500 }
501
502 if (GET_LMASK64(arg2) <= 1) {
503 do_sb(env, GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx,
504 GETPC());
505 }
506
507 if (GET_LMASK64(arg2) <= 0) {
508 do_sb(env, GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx,
509 GETPC());
510 }
511 }
512
513 void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
514 int mem_idx)
515 {
516 do_sb(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
517
518 if (GET_LMASK64(arg2) >= 1) {
519 do_sb(env, GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx,
520 GETPC());
521 }
522
523 if (GET_LMASK64(arg2) >= 2) {
524 do_sb(env, GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx,
525 GETPC());
526 }
527
528 if (GET_LMASK64(arg2) >= 3) {
529 do_sb(env, GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx,
530 GETPC());
531 }
532
533 if (GET_LMASK64(arg2) >= 4) {
534 do_sb(env, GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx,
535 GETPC());
536 }
537
538 if (GET_LMASK64(arg2) >= 5) {
539 do_sb(env, GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx,
540 GETPC());
541 }
542
543 if (GET_LMASK64(arg2) >= 6) {
544 do_sb(env, GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx,
545 GETPC());
546 }
547
548 if (GET_LMASK64(arg2) == 7) {
549 do_sb(env, GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx,
550 GETPC());
551 }
552 }
553 #endif /* TARGET_MIPS64 */
554
555 static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };
556
557 void helper_lwm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
558 uint32_t mem_idx)
559 {
560 target_ulong base_reglist = reglist & 0xf;
561 target_ulong do_r31 = reglist & 0x10;
562
563 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
564 target_ulong i;
565
566 for (i = 0; i < base_reglist; i++) {
567 env->active_tc.gpr[multiple_regs[i]] =
568 (target_long)do_lw(env, addr, mem_idx, GETPC());
569 addr += 4;
570 }
571 }
572
573 if (do_r31) {
574 env->active_tc.gpr[31] = (target_long)do_lw(env, addr, mem_idx,
575 GETPC());
576 }
577 }
578
579 void helper_swm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
580 uint32_t mem_idx)
581 {
582 target_ulong base_reglist = reglist & 0xf;
583 target_ulong do_r31 = reglist & 0x10;
584
585 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
586 target_ulong i;
587
588 for (i = 0; i < base_reglist; i++) {
589 do_sw(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx,
590 GETPC());
591 addr += 4;
592 }
593 }
594
595 if (do_r31) {
596 do_sw(env, addr, env->active_tc.gpr[31], mem_idx, GETPC());
597 }
598 }
599
600 #if defined(TARGET_MIPS64)
601 void helper_ldm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
602 uint32_t mem_idx)
603 {
604 target_ulong base_reglist = reglist & 0xf;
605 target_ulong do_r31 = reglist & 0x10;
606
607 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
608 target_ulong i;
609
610 for (i = 0; i < base_reglist; i++) {
611 env->active_tc.gpr[multiple_regs[i]] = do_ld(env, addr, mem_idx,
612 GETPC());
613 addr += 8;
614 }
615 }
616
617 if (do_r31) {
618 env->active_tc.gpr[31] = do_ld(env, addr, mem_idx, GETPC());
619 }
620 }
621
622 void helper_sdm(CPUMIPSState *env, target_ulong addr, target_ulong reglist,
623 uint32_t mem_idx)
624 {
625 target_ulong base_reglist = reglist & 0xf;
626 target_ulong do_r31 = reglist & 0x10;
627
628 if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
629 target_ulong i;
630
631 for (i = 0; i < base_reglist; i++) {
632 do_sd(env, addr, env->active_tc.gpr[multiple_regs[i]], mem_idx,
633 GETPC());
634 addr += 8;
635 }
636 }
637
638 if (do_r31) {
639 do_sd(env, addr, env->active_tc.gpr[31], mem_idx, GETPC());
640 }
641 }
642 #endif
643
644 #ifndef CONFIG_USER_ONLY
645 /* SMP helpers. */
646 static bool mips_vpe_is_wfi(MIPSCPU *c)
647 {
648 CPUState *cpu = CPU(c);
649 CPUMIPSState *env = &c->env;
650
651 /* If the VPE is halted but otherwise active, it means it's waiting for
652 an interrupt. */
653 return cpu->halted && mips_vpe_active(env);
654 }
655
656 static bool mips_vp_is_wfi(MIPSCPU *c)
657 {
658 CPUState *cpu = CPU(c);
659 CPUMIPSState *env = &c->env;
660
661 return cpu->halted && mips_vp_active(env);
662 }
663
664 static inline void mips_vpe_wake(MIPSCPU *c)
665 {
666 /* Don't set ->halted = 0 directly, let it be done via cpu_has_work
667 because there might be other conditions that state that c should
668 be sleeping. */
669 cpu_interrupt(CPU(c), CPU_INTERRUPT_WAKE);
670 }
671
672 static inline void mips_vpe_sleep(MIPSCPU *cpu)
673 {
674 CPUState *cs = CPU(cpu);
675
676 /* The VPE was shut off, really go to bed.
677 Reset any old _WAKE requests. */
678 cs->halted = 1;
679 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
680 }
681
682 static inline void mips_tc_wake(MIPSCPU *cpu, int tc)
683 {
684 CPUMIPSState *c = &cpu->env;
685
686 /* FIXME: TC reschedule. */
687 if (mips_vpe_active(c) && !mips_vpe_is_wfi(cpu)) {
688 mips_vpe_wake(cpu);
689 }
690 }
691
692 static inline void mips_tc_sleep(MIPSCPU *cpu, int tc)
693 {
694 CPUMIPSState *c = &cpu->env;
695
696 /* FIXME: TC reschedule. */
697 if (!mips_vpe_active(c)) {
698 mips_vpe_sleep(cpu);
699 }
700 }
701
702 /**
703 * mips_cpu_map_tc:
704 * @env: CPU from which mapping is performed.
705 * @tc: Should point to an int with the value of the global TC index.
706 *
707 * This function will transform @tc into a local index within the
708 * returned #CPUMIPSState.
709 */
710 /* FIXME: This code assumes that all VPEs have the same number of TCs,
711 which depends on runtime setup. Can probably be fixed by
712 walking the list of CPUMIPSStates. */
713 static CPUMIPSState *mips_cpu_map_tc(CPUMIPSState *env, int *tc)
714 {
715 MIPSCPU *cpu;
716 CPUState *cs;
717 CPUState *other_cs;
718 int vpe_idx;
719 int tc_idx = *tc;
720
721 if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))) {
722 /* Not allowed to address other CPUs. */
723 *tc = env->current_tc;
724 return env;
725 }
726
727 cs = CPU(mips_env_get_cpu(env));
728 vpe_idx = tc_idx / cs->nr_threads;
729 *tc = tc_idx % cs->nr_threads;
730 other_cs = qemu_get_cpu(vpe_idx);
731 if (other_cs == NULL) {
732 return env;
733 }
734 cpu = MIPS_CPU(other_cs);
735 return &cpu->env;
736 }
737
738 /* The per VPE CP0_Status register shares some fields with the per TC
739 CP0_TCStatus registers. These fields are wired to the same registers,
740 so changes to either of them should be reflected on both registers.
741
742 Also, EntryHi shares the bottom 8 bit ASID with TCStauts.
743
744 These helper call synchronizes the regs for a given cpu. */
745
746 /* Called for updates to CP0_Status. Defined in "cpu.h" for gdbstub.c. */
747 /* static inline void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu,
748 int tc); */
749
750 /* Called for updates to CP0_TCStatus. */
751 static void sync_c0_tcstatus(CPUMIPSState *cpu, int tc,
752 target_ulong v)
753 {
754 uint32_t status;
755 uint32_t tcu, tmx, tasid, tksu;
756 uint32_t mask = ((1U << CP0St_CU3)
757 | (1 << CP0St_CU2)
758 | (1 << CP0St_CU1)
759 | (1 << CP0St_CU0)
760 | (1 << CP0St_MX)
761 | (3 << CP0St_KSU));
762
763 tcu = (v >> CP0TCSt_TCU0) & 0xf;
764 tmx = (v >> CP0TCSt_TMX) & 0x1;
765 tasid = v & cpu->CP0_EntryHi_ASID_mask;
766 tksu = (v >> CP0TCSt_TKSU) & 0x3;
767
768 status = tcu << CP0St_CU0;
769 status |= tmx << CP0St_MX;
770 status |= tksu << CP0St_KSU;
771
772 cpu->CP0_Status &= ~mask;
773 cpu->CP0_Status |= status;
774
775 /* Sync the TASID with EntryHi. */
776 cpu->CP0_EntryHi &= ~cpu->CP0_EntryHi_ASID_mask;
777 cpu->CP0_EntryHi |= tasid;
778
779 compute_hflags(cpu);
780 }
781
782 /* Called for updates to CP0_EntryHi. */
783 static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
784 {
785 int32_t *tcst;
786 uint32_t asid, v = cpu->CP0_EntryHi;
787
788 asid = v & cpu->CP0_EntryHi_ASID_mask;
789
790 if (tc == cpu->current_tc) {
791 tcst = &cpu->active_tc.CP0_TCStatus;
792 } else {
793 tcst = &cpu->tcs[tc].CP0_TCStatus;
794 }
795
796 *tcst &= ~cpu->CP0_EntryHi_ASID_mask;
797 *tcst |= asid;
798 }
799
800 /* CP0 helpers */
801 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
802 {
803 return env->mvp->CP0_MVPControl;
804 }
805
806 target_ulong helper_mfc0_mvpconf0(CPUMIPSState *env)
807 {
808 return env->mvp->CP0_MVPConf0;
809 }
810
811 target_ulong helper_mfc0_mvpconf1(CPUMIPSState *env)
812 {
813 return env->mvp->CP0_MVPConf1;
814 }
815
816 target_ulong helper_mfc0_random(CPUMIPSState *env)
817 {
818 return (int32_t)cpu_mips_get_random(env);
819 }
820
821 target_ulong helper_mfc0_tcstatus(CPUMIPSState *env)
822 {
823 return env->active_tc.CP0_TCStatus;
824 }
825
826 target_ulong helper_mftc0_tcstatus(CPUMIPSState *env)
827 {
828 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
829 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
830
831 if (other_tc == other->current_tc)
832 return other->active_tc.CP0_TCStatus;
833 else
834 return other->tcs[other_tc].CP0_TCStatus;
835 }
836
837 target_ulong helper_mfc0_tcbind(CPUMIPSState *env)
838 {
839 return env->active_tc.CP0_TCBind;
840 }
841
842 target_ulong helper_mftc0_tcbind(CPUMIPSState *env)
843 {
844 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
845 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
846
847 if (other_tc == other->current_tc)
848 return other->active_tc.CP0_TCBind;
849 else
850 return other->tcs[other_tc].CP0_TCBind;
851 }
852
853 target_ulong helper_mfc0_tcrestart(CPUMIPSState *env)
854 {
855 return env->active_tc.PC;
856 }
857
858 target_ulong helper_mftc0_tcrestart(CPUMIPSState *env)
859 {
860 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
861 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
862
863 if (other_tc == other->current_tc)
864 return other->active_tc.PC;
865 else
866 return other->tcs[other_tc].PC;
867 }
868
869 target_ulong helper_mfc0_tchalt(CPUMIPSState *env)
870 {
871 return env->active_tc.CP0_TCHalt;
872 }
873
874 target_ulong helper_mftc0_tchalt(CPUMIPSState *env)
875 {
876 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
877 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
878
879 if (other_tc == other->current_tc)
880 return other->active_tc.CP0_TCHalt;
881 else
882 return other->tcs[other_tc].CP0_TCHalt;
883 }
884
885 target_ulong helper_mfc0_tccontext(CPUMIPSState *env)
886 {
887 return env->active_tc.CP0_TCContext;
888 }
889
890 target_ulong helper_mftc0_tccontext(CPUMIPSState *env)
891 {
892 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
893 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
894
895 if (other_tc == other->current_tc)
896 return other->active_tc.CP0_TCContext;
897 else
898 return other->tcs[other_tc].CP0_TCContext;
899 }
900
901 target_ulong helper_mfc0_tcschedule(CPUMIPSState *env)
902 {
903 return env->active_tc.CP0_TCSchedule;
904 }
905
906 target_ulong helper_mftc0_tcschedule(CPUMIPSState *env)
907 {
908 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
909 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
910
911 if (other_tc == other->current_tc)
912 return other->active_tc.CP0_TCSchedule;
913 else
914 return other->tcs[other_tc].CP0_TCSchedule;
915 }
916
917 target_ulong helper_mfc0_tcschefback(CPUMIPSState *env)
918 {
919 return env->active_tc.CP0_TCScheFBack;
920 }
921
922 target_ulong helper_mftc0_tcschefback(CPUMIPSState *env)
923 {
924 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
925 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
926
927 if (other_tc == other->current_tc)
928 return other->active_tc.CP0_TCScheFBack;
929 else
930 return other->tcs[other_tc].CP0_TCScheFBack;
931 }
932
933 target_ulong helper_mfc0_count(CPUMIPSState *env)
934 {
935 int32_t count;
936 qemu_mutex_lock_iothread();
937 count = (int32_t) cpu_mips_get_count(env);
938 qemu_mutex_unlock_iothread();
939 return count;
940 }
941
942 target_ulong helper_mfc0_saar(CPUMIPSState *env)
943 {
944 if ((env->CP0_SAARI & 0x3f) < 2) {
945 return (int32_t) env->CP0_SAAR[env->CP0_SAARI & 0x3f];
946 }
947 return 0;
948 }
949
950 target_ulong helper_mfhc0_saar(CPUMIPSState *env)
951 {
952 if ((env->CP0_SAARI & 0x3f) < 2) {
953 return env->CP0_SAAR[env->CP0_SAARI & 0x3f] >> 32;
954 }
955 return 0;
956 }
957
958 target_ulong helper_mftc0_entryhi(CPUMIPSState *env)
959 {
960 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
961 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
962
963 return other->CP0_EntryHi;
964 }
965
966 target_ulong helper_mftc0_cause(CPUMIPSState *env)
967 {
968 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
969 int32_t tccause;
970 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
971
972 if (other_tc == other->current_tc) {
973 tccause = other->CP0_Cause;
974 } else {
975 tccause = other->CP0_Cause;
976 }
977
978 return tccause;
979 }
980
981 target_ulong helper_mftc0_status(CPUMIPSState *env)
982 {
983 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
984 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
985
986 return other->CP0_Status;
987 }
988
989 target_ulong helper_mfc0_lladdr(CPUMIPSState *env)
990 {
991 return (int32_t)(env->CP0_LLAddr >> env->CP0_LLAddr_shift);
992 }
993
994 target_ulong helper_mfc0_maar(CPUMIPSState *env)
995 {
996 return (int32_t) env->CP0_MAAR[env->CP0_MAARI];
997 }
998
999 target_ulong helper_mfhc0_maar(CPUMIPSState *env)
1000 {
1001 return env->CP0_MAAR[env->CP0_MAARI] >> 32;
1002 }
1003
1004 target_ulong helper_mfc0_watchlo(CPUMIPSState *env, uint32_t sel)
1005 {
1006 return (int32_t)env->CP0_WatchLo[sel];
1007 }
1008
1009 target_ulong helper_mfc0_watchhi(CPUMIPSState *env, uint32_t sel)
1010 {
1011 return env->CP0_WatchHi[sel];
1012 }
1013
1014 target_ulong helper_mfc0_debug(CPUMIPSState *env)
1015 {
1016 target_ulong t0 = env->CP0_Debug;
1017 if (env->hflags & MIPS_HFLAG_DM)
1018 t0 |= 1 << CP0DB_DM;
1019
1020 return t0;
1021 }
1022
1023 target_ulong helper_mftc0_debug(CPUMIPSState *env)
1024 {
1025 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1026 int32_t tcstatus;
1027 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1028
1029 if (other_tc == other->current_tc)
1030 tcstatus = other->active_tc.CP0_Debug_tcstatus;
1031 else
1032 tcstatus = other->tcs[other_tc].CP0_Debug_tcstatus;
1033
1034 /* XXX: Might be wrong, check with EJTAG spec. */
1035 return (other->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1036 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1037 }
1038
1039 #if defined(TARGET_MIPS64)
1040 target_ulong helper_dmfc0_tcrestart(CPUMIPSState *env)
1041 {
1042 return env->active_tc.PC;
1043 }
1044
1045 target_ulong helper_dmfc0_tchalt(CPUMIPSState *env)
1046 {
1047 return env->active_tc.CP0_TCHalt;
1048 }
1049
1050 target_ulong helper_dmfc0_tccontext(CPUMIPSState *env)
1051 {
1052 return env->active_tc.CP0_TCContext;
1053 }
1054
1055 target_ulong helper_dmfc0_tcschedule(CPUMIPSState *env)
1056 {
1057 return env->active_tc.CP0_TCSchedule;
1058 }
1059
1060 target_ulong helper_dmfc0_tcschefback(CPUMIPSState *env)
1061 {
1062 return env->active_tc.CP0_TCScheFBack;
1063 }
1064
1065 target_ulong helper_dmfc0_lladdr(CPUMIPSState *env)
1066 {
1067 return env->CP0_LLAddr >> env->CP0_LLAddr_shift;
1068 }
1069
1070 target_ulong helper_dmfc0_maar(CPUMIPSState *env)
1071 {
1072 return env->CP0_MAAR[env->CP0_MAARI];
1073 }
1074
1075 target_ulong helper_dmfc0_watchlo(CPUMIPSState *env, uint32_t sel)
1076 {
1077 return env->CP0_WatchLo[sel];
1078 }
1079
1080 target_ulong helper_dmfc0_saar(CPUMIPSState *env)
1081 {
1082 if ((env->CP0_SAARI & 0x3f) < 2) {
1083 return env->CP0_SAAR[env->CP0_SAARI & 0x3f];
1084 }
1085 return 0;
1086 }
1087 #endif /* TARGET_MIPS64 */
1088
1089 void helper_mtc0_index(CPUMIPSState *env, target_ulong arg1)
1090 {
1091 uint32_t index_p = env->CP0_Index & 0x80000000;
1092 uint32_t tlb_index = arg1 & 0x7fffffff;
1093 if (tlb_index < env->tlb->nb_tlb) {
1094 if (env->insn_flags & ISA_MIPS32R6) {
1095 index_p |= arg1 & 0x80000000;
1096 }
1097 env->CP0_Index = index_p | tlb_index;
1098 }
1099 }
1100
1101 void helper_mtc0_mvpcontrol(CPUMIPSState *env, target_ulong arg1)
1102 {
1103 uint32_t mask = 0;
1104 uint32_t newval;
1105
1106 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
1107 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
1108 (1 << CP0MVPCo_EVP);
1109 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1110 mask |= (1 << CP0MVPCo_STLB);
1111 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
1112
1113 // TODO: Enable/disable shared TLB, enable/disable VPEs.
1114
1115 env->mvp->CP0_MVPControl = newval;
1116 }
1117
1118 void helper_mtc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
1119 {
1120 uint32_t mask;
1121 uint32_t newval;
1122
1123 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
1124 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1125 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
1126
1127 /* Yield scheduler intercept not implemented. */
1128 /* Gating storage scheduler intercept not implemented. */
1129
1130 // TODO: Enable/disable TCs.
1131
1132 env->CP0_VPEControl = newval;
1133 }
1134
1135 void helper_mttc0_vpecontrol(CPUMIPSState *env, target_ulong arg1)
1136 {
1137 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1138 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1139 uint32_t mask;
1140 uint32_t newval;
1141
1142 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
1143 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
1144 newval = (other->CP0_VPEControl & ~mask) | (arg1 & mask);
1145
1146 /* TODO: Enable/disable TCs. */
1147
1148 other->CP0_VPEControl = newval;
1149 }
1150
1151 target_ulong helper_mftc0_vpecontrol(CPUMIPSState *env)
1152 {
1153 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1154 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1155 /* FIXME: Mask away return zero on read bits. */
1156 return other->CP0_VPEControl;
1157 }
1158
1159 target_ulong helper_mftc0_vpeconf0(CPUMIPSState *env)
1160 {
1161 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1162 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1163
1164 return other->CP0_VPEConf0;
1165 }
1166
1167 void helper_mtc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1168 {
1169 uint32_t mask = 0;
1170 uint32_t newval;
1171
1172 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
1173 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
1174 mask |= (0xff << CP0VPEC0_XTC);
1175 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1176 }
1177 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1178
1179 // TODO: TC exclusive handling due to ERL/EXL.
1180
1181 env->CP0_VPEConf0 = newval;
1182 }
1183
1184 void helper_mttc0_vpeconf0(CPUMIPSState *env, target_ulong arg1)
1185 {
1186 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1187 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1188 uint32_t mask = 0;
1189 uint32_t newval;
1190
1191 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
1192 newval = (other->CP0_VPEConf0 & ~mask) | (arg1 & mask);
1193
1194 /* TODO: TC exclusive handling due to ERL/EXL. */
1195 other->CP0_VPEConf0 = newval;
1196 }
1197
1198 void helper_mtc0_vpeconf1(CPUMIPSState *env, target_ulong arg1)
1199 {
1200 uint32_t mask = 0;
1201 uint32_t newval;
1202
1203 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1204 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
1205 (0xff << CP0VPEC1_NCP1);
1206 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1207
1208 /* UDI not implemented. */
1209 /* CP2 not implemented. */
1210
1211 // TODO: Handle FPU (CP1) binding.
1212
1213 env->CP0_VPEConf1 = newval;
1214 }
1215
1216 void helper_mtc0_yqmask(CPUMIPSState *env, target_ulong arg1)
1217 {
1218 /* Yield qualifier inputs not implemented. */
1219 env->CP0_YQMask = 0x00000000;
1220 }
1221
1222 void helper_mtc0_vpeopt(CPUMIPSState *env, target_ulong arg1)
1223 {
1224 env->CP0_VPEOpt = arg1 & 0x0000ffff;
1225 }
1226
1227 #define MTC0_ENTRYLO_MASK(env) ((env->PAMask >> 6) & 0x3FFFFFFF)
1228
1229 void helper_mtc0_entrylo0(CPUMIPSState *env, target_ulong arg1)
1230 {
1231 /* 1k pages not implemented */
1232 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1233 env->CP0_EntryLo0 = (arg1 & MTC0_ENTRYLO_MASK(env))
1234 | (rxi << (CP0EnLo_XI - 30));
1235 }
1236
1237 #if defined(TARGET_MIPS64)
1238 #define DMTC0_ENTRYLO_MASK(env) (env->PAMask >> 6)
1239
1240 void helper_dmtc0_entrylo0(CPUMIPSState *env, uint64_t arg1)
1241 {
1242 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1243 env->CP0_EntryLo0 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1244 }
1245 #endif
1246
1247 void helper_mtc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1248 {
1249 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
1250 uint32_t newval;
1251
1252 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1253
1254 env->active_tc.CP0_TCStatus = newval;
1255 sync_c0_tcstatus(env, env->current_tc, newval);
1256 }
1257
1258 void helper_mttc0_tcstatus(CPUMIPSState *env, target_ulong arg1)
1259 {
1260 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1261 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1262
1263 if (other_tc == other->current_tc)
1264 other->active_tc.CP0_TCStatus = arg1;
1265 else
1266 other->tcs[other_tc].CP0_TCStatus = arg1;
1267 sync_c0_tcstatus(other, other_tc, arg1);
1268 }
1269
1270 void helper_mtc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1271 {
1272 uint32_t mask = (1 << CP0TCBd_TBE);
1273 uint32_t newval;
1274
1275 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1276 mask |= (1 << CP0TCBd_CurVPE);
1277 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1278 env->active_tc.CP0_TCBind = newval;
1279 }
1280
1281 void helper_mttc0_tcbind(CPUMIPSState *env, target_ulong arg1)
1282 {
1283 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1284 uint32_t mask = (1 << CP0TCBd_TBE);
1285 uint32_t newval;
1286 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1287
1288 if (other->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
1289 mask |= (1 << CP0TCBd_CurVPE);
1290 if (other_tc == other->current_tc) {
1291 newval = (other->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1292 other->active_tc.CP0_TCBind = newval;
1293 } else {
1294 newval = (other->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1295 other->tcs[other_tc].CP0_TCBind = newval;
1296 }
1297 }
1298
1299 void helper_mtc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1300 {
1301 env->active_tc.PC = arg1;
1302 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1303 env->CP0_LLAddr = 0;
1304 env->lladdr = 0;
1305 /* MIPS16 not implemented. */
1306 }
1307
1308 void helper_mttc0_tcrestart(CPUMIPSState *env, target_ulong arg1)
1309 {
1310 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1311 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1312
1313 if (other_tc == other->current_tc) {
1314 other->active_tc.PC = arg1;
1315 other->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1316 other->CP0_LLAddr = 0;
1317 other->lladdr = 0;
1318 /* MIPS16 not implemented. */
1319 } else {
1320 other->tcs[other_tc].PC = arg1;
1321 other->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1322 other->CP0_LLAddr = 0;
1323 other->lladdr = 0;
1324 /* MIPS16 not implemented. */
1325 }
1326 }
1327
1328 void helper_mtc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1329 {
1330 MIPSCPU *cpu = mips_env_get_cpu(env);
1331
1332 env->active_tc.CP0_TCHalt = arg1 & 0x1;
1333
1334 // TODO: Halt TC / Restart (if allocated+active) TC.
1335 if (env->active_tc.CP0_TCHalt & 1) {
1336 mips_tc_sleep(cpu, env->current_tc);
1337 } else {
1338 mips_tc_wake(cpu, env->current_tc);
1339 }
1340 }
1341
1342 void helper_mttc0_tchalt(CPUMIPSState *env, target_ulong arg1)
1343 {
1344 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1345 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1346 MIPSCPU *other_cpu = mips_env_get_cpu(other);
1347
1348 // TODO: Halt TC / Restart (if allocated+active) TC.
1349
1350 if (other_tc == other->current_tc)
1351 other->active_tc.CP0_TCHalt = arg1;
1352 else
1353 other->tcs[other_tc].CP0_TCHalt = arg1;
1354
1355 if (arg1 & 1) {
1356 mips_tc_sleep(other_cpu, other_tc);
1357 } else {
1358 mips_tc_wake(other_cpu, other_tc);
1359 }
1360 }
1361
1362 void helper_mtc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1363 {
1364 env->active_tc.CP0_TCContext = arg1;
1365 }
1366
1367 void helper_mttc0_tccontext(CPUMIPSState *env, target_ulong arg1)
1368 {
1369 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1370 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1371
1372 if (other_tc == other->current_tc)
1373 other->active_tc.CP0_TCContext = arg1;
1374 else
1375 other->tcs[other_tc].CP0_TCContext = arg1;
1376 }
1377
1378 void helper_mtc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1379 {
1380 env->active_tc.CP0_TCSchedule = arg1;
1381 }
1382
1383 void helper_mttc0_tcschedule(CPUMIPSState *env, target_ulong arg1)
1384 {
1385 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1386 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1387
1388 if (other_tc == other->current_tc)
1389 other->active_tc.CP0_TCSchedule = arg1;
1390 else
1391 other->tcs[other_tc].CP0_TCSchedule = arg1;
1392 }
1393
1394 void helper_mtc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1395 {
1396 env->active_tc.CP0_TCScheFBack = arg1;
1397 }
1398
1399 void helper_mttc0_tcschefback(CPUMIPSState *env, target_ulong arg1)
1400 {
1401 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1402 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1403
1404 if (other_tc == other->current_tc)
1405 other->active_tc.CP0_TCScheFBack = arg1;
1406 else
1407 other->tcs[other_tc].CP0_TCScheFBack = arg1;
1408 }
1409
1410 void helper_mtc0_entrylo1(CPUMIPSState *env, target_ulong arg1)
1411 {
1412 /* 1k pages not implemented */
1413 target_ulong rxi = arg1 & (env->CP0_PageGrain & (3u << CP0PG_XIE));
1414 env->CP0_EntryLo1 = (arg1 & MTC0_ENTRYLO_MASK(env))
1415 | (rxi << (CP0EnLo_XI - 30));
1416 }
1417
1418 #if defined(TARGET_MIPS64)
1419 void helper_dmtc0_entrylo1(CPUMIPSState *env, uint64_t arg1)
1420 {
1421 uint64_t rxi = arg1 & ((env->CP0_PageGrain & (3ull << CP0PG_XIE)) << 32);
1422 env->CP0_EntryLo1 = (arg1 & DMTC0_ENTRYLO_MASK(env)) | rxi;
1423 }
1424 #endif
1425
1426 void helper_mtc0_context(CPUMIPSState *env, target_ulong arg1)
1427 {
1428 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1429 }
1430
1431 void update_pagemask(CPUMIPSState *env, target_ulong arg1, int32_t *pagemask)
1432 {
1433 uint64_t mask = arg1 >> (TARGET_PAGE_BITS + 1);
1434 if (!(env->insn_flags & ISA_MIPS32R6) || (arg1 == ~0) ||
1435 (mask == 0x0000 || mask == 0x0003 || mask == 0x000F ||
1436 mask == 0x003F || mask == 0x00FF || mask == 0x03FF ||
1437 mask == 0x0FFF || mask == 0x3FFF || mask == 0xFFFF)) {
1438 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1439 }
1440 }
1441
1442 void helper_mtc0_pagemask(CPUMIPSState *env, target_ulong arg1)
1443 {
1444 update_pagemask(env, arg1, &env->CP0_PageMask);
1445 }
1446
1447 void helper_mtc0_pagegrain(CPUMIPSState *env, target_ulong arg1)
1448 {
1449 /* SmartMIPS not implemented */
1450 /* 1k pages not implemented */
1451 env->CP0_PageGrain = (arg1 & env->CP0_PageGrain_rw_bitmask) |
1452 (env->CP0_PageGrain & ~env->CP0_PageGrain_rw_bitmask);
1453 compute_hflags(env);
1454 restore_pamask(env);
1455 }
1456
1457 void helper_mtc0_segctl0(CPUMIPSState *env, target_ulong arg1)
1458 {
1459 CPUState *cs = CPU(mips_env_get_cpu(env));
1460
1461 env->CP0_SegCtl0 = arg1 & CP0SC0_MASK;
1462 tlb_flush(cs);
1463 }
1464
1465 void helper_mtc0_segctl1(CPUMIPSState *env, target_ulong arg1)
1466 {
1467 CPUState *cs = CPU(mips_env_get_cpu(env));
1468
1469 env->CP0_SegCtl1 = arg1 & CP0SC1_MASK;
1470 tlb_flush(cs);
1471 }
1472
1473 void helper_mtc0_segctl2(CPUMIPSState *env, target_ulong arg1)
1474 {
1475 CPUState *cs = CPU(mips_env_get_cpu(env));
1476
1477 env->CP0_SegCtl2 = arg1 & CP0SC2_MASK;
1478 tlb_flush(cs);
1479 }
1480
1481 void helper_mtc0_pwfield(CPUMIPSState *env, target_ulong arg1)
1482 {
1483 #if defined(TARGET_MIPS64)
1484 uint64_t mask = 0x3F3FFFFFFFULL;
1485 uint32_t old_ptei = (env->CP0_PWField >> CP0PF_PTEI) & 0x3FULL;
1486 uint32_t new_ptei = (arg1 >> CP0PF_PTEI) & 0x3FULL;
1487
1488 if ((env->insn_flags & ISA_MIPS32R6)) {
1489 if (((arg1 >> CP0PF_BDI) & 0x3FULL) < 12) {
1490 mask &= ~(0x3FULL << CP0PF_BDI);
1491 }
1492 if (((arg1 >> CP0PF_GDI) & 0x3FULL) < 12) {
1493 mask &= ~(0x3FULL << CP0PF_GDI);
1494 }
1495 if (((arg1 >> CP0PF_UDI) & 0x3FULL) < 12) {
1496 mask &= ~(0x3FULL << CP0PF_UDI);
1497 }
1498 if (((arg1 >> CP0PF_MDI) & 0x3FULL) < 12) {
1499 mask &= ~(0x3FULL << CP0PF_MDI);
1500 }
1501 if (((arg1 >> CP0PF_PTI) & 0x3FULL) < 12) {
1502 mask &= ~(0x3FULL << CP0PF_PTI);
1503 }
1504 }
1505 env->CP0_PWField = arg1 & mask;
1506
1507 if ((new_ptei >= 32) ||
1508 ((env->insn_flags & ISA_MIPS32R6) &&
1509 (new_ptei == 0 || new_ptei == 1))) {
1510 env->CP0_PWField = (env->CP0_PWField & ~0x3FULL) |
1511 (old_ptei << CP0PF_PTEI);
1512 }
1513 #else
1514 uint32_t mask = 0x3FFFFFFF;
1515 uint32_t old_ptew = (env->CP0_PWField >> CP0PF_PTEW) & 0x3F;
1516 uint32_t new_ptew = (arg1 >> CP0PF_PTEW) & 0x3F;
1517
1518 if ((env->insn_flags & ISA_MIPS32R6)) {
1519 if (((arg1 >> CP0PF_GDW) & 0x3F) < 12) {
1520 mask &= ~(0x3F << CP0PF_GDW);
1521 }
1522 if (((arg1 >> CP0PF_UDW) & 0x3F) < 12) {
1523 mask &= ~(0x3F << CP0PF_UDW);
1524 }
1525 if (((arg1 >> CP0PF_MDW) & 0x3F) < 12) {
1526 mask &= ~(0x3F << CP0PF_MDW);
1527 }
1528 if (((arg1 >> CP0PF_PTW) & 0x3F) < 12) {
1529 mask &= ~(0x3F << CP0PF_PTW);
1530 }
1531 }
1532 env->CP0_PWField = arg1 & mask;
1533
1534 if ((new_ptew >= 32) ||
1535 ((env->insn_flags & ISA_MIPS32R6) &&
1536 (new_ptew == 0 || new_ptew == 1))) {
1537 env->CP0_PWField = (env->CP0_PWField & ~0x3F) |
1538 (old_ptew << CP0PF_PTEW);
1539 }
1540 #endif
1541 }
1542
1543 void helper_mtc0_pwsize(CPUMIPSState *env, target_ulong arg1)
1544 {
1545 #if defined(TARGET_MIPS64)
1546 env->CP0_PWSize = arg1 & 0x3F7FFFFFFFULL;
1547 #else
1548 env->CP0_PWSize = arg1 & 0x3FFFFFFF;
1549 #endif
1550 }
1551
1552 void helper_mtc0_wired(CPUMIPSState *env, target_ulong arg1)
1553 {
1554 if (env->insn_flags & ISA_MIPS32R6) {
1555 if (arg1 < env->tlb->nb_tlb) {
1556 env->CP0_Wired = arg1;
1557 }
1558 } else {
1559 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1560 }
1561 }
1562
1563 void helper_mtc0_pwctl(CPUMIPSState *env, target_ulong arg1)
1564 {
1565 #if defined(TARGET_MIPS64)
1566 /* PWEn = 0. Hardware page table walking is not implemented. */
1567 env->CP0_PWCtl = (env->CP0_PWCtl & 0x000000C0) | (arg1 & 0x5C00003F);
1568 #else
1569 env->CP0_PWCtl = (arg1 & 0x800000FF);
1570 #endif
1571 }
1572
1573 void helper_mtc0_srsconf0(CPUMIPSState *env, target_ulong arg1)
1574 {
1575 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1576 }
1577
1578 void helper_mtc0_srsconf1(CPUMIPSState *env, target_ulong arg1)
1579 {
1580 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1581 }
1582
1583 void helper_mtc0_srsconf2(CPUMIPSState *env, target_ulong arg1)
1584 {
1585 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1586 }
1587
1588 void helper_mtc0_srsconf3(CPUMIPSState *env, target_ulong arg1)
1589 {
1590 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1591 }
1592
1593 void helper_mtc0_srsconf4(CPUMIPSState *env, target_ulong arg1)
1594 {
1595 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1596 }
1597
1598 void helper_mtc0_hwrena(CPUMIPSState *env, target_ulong arg1)
1599 {
1600 uint32_t mask = 0x0000000F;
1601
1602 if ((env->CP0_Config1 & (1 << CP0C1_PC)) &&
1603 (env->insn_flags & ISA_MIPS32R6)) {
1604 mask |= (1 << 4);
1605 }
1606 if (env->insn_flags & ISA_MIPS32R6) {
1607 mask |= (1 << 5);
1608 }
1609 if (env->CP0_Config3 & (1 << CP0C3_ULRI)) {
1610 mask |= (1 << 29);
1611
1612 if (arg1 & (1 << 29)) {
1613 env->hflags |= MIPS_HFLAG_HWRENA_ULR;
1614 } else {
1615 env->hflags &= ~MIPS_HFLAG_HWRENA_ULR;
1616 }
1617 }
1618
1619 env->CP0_HWREna = arg1 & mask;
1620 }
1621
1622 void helper_mtc0_count(CPUMIPSState *env, target_ulong arg1)
1623 {
1624 qemu_mutex_lock_iothread();
1625 cpu_mips_store_count(env, arg1);
1626 qemu_mutex_unlock_iothread();
1627 }
1628
1629 void helper_mtc0_saari(CPUMIPSState *env, target_ulong arg1)
1630 {
1631 uint32_t target = arg1 & 0x3f;
1632 if (target <= 1) {
1633 env->CP0_SAARI = target;
1634 }
1635 }
1636
1637 void helper_mtc0_saar(CPUMIPSState *env, target_ulong arg1)
1638 {
1639 uint32_t target = env->CP0_SAARI & 0x3f;
1640 if (target < 2) {
1641 env->CP0_SAAR[target] = arg1 & 0x00000ffffffff03fULL;
1642 switch (target) {
1643 case 0:
1644 if (env->itu) {
1645 itc_reconfigure(env->itu);
1646 }
1647 break;
1648 }
1649 }
1650 }
1651
1652 void helper_mthc0_saar(CPUMIPSState *env, target_ulong arg1)
1653 {
1654 uint32_t target = env->CP0_SAARI & 0x3f;
1655 if (target < 2) {
1656 env->CP0_SAAR[target] =
1657 (((uint64_t) arg1 << 32) & 0x00000fff00000000ULL) |
1658 (env->CP0_SAAR[target] & 0x00000000ffffffffULL);
1659 switch (target) {
1660 case 0:
1661 if (env->itu) {
1662 itc_reconfigure(env->itu);
1663 }
1664 break;
1665 }
1666 }
1667 }
1668
1669 void helper_mtc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1670 {
1671 target_ulong old, val, mask;
1672 mask = (TARGET_PAGE_MASK << 1) | env->CP0_EntryHi_ASID_mask;
1673 if (((env->CP0_Config4 >> CP0C4_IE) & 0x3) >= 2) {
1674 mask |= 1 << CP0EnHi_EHINV;
1675 }
1676
1677 /* 1k pages not implemented */
1678 #if defined(TARGET_MIPS64)
1679 if (env->insn_flags & ISA_MIPS32R6) {
1680 int entryhi_r = extract64(arg1, 62, 2);
1681 int config0_at = extract32(env->CP0_Config0, 13, 2);
1682 bool no_supervisor = (env->CP0_Status_rw_bitmask & 0x8) == 0;
1683 if ((entryhi_r == 2) ||
1684 (entryhi_r == 1 && (no_supervisor || config0_at == 1))) {
1685 /* skip EntryHi.R field if new value is reserved */
1686 mask &= ~(0x3ull << 62);
1687 }
1688 }
1689 mask &= env->SEGMask;
1690 #endif
1691 old = env->CP0_EntryHi;
1692 val = (arg1 & mask) | (old & ~mask);
1693 env->CP0_EntryHi = val;
1694 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1695 sync_c0_entryhi(env, env->current_tc);
1696 }
1697 /* If the ASID changes, flush qemu's TLB. */
1698 if ((old & env->CP0_EntryHi_ASID_mask) !=
1699 (val & env->CP0_EntryHi_ASID_mask)) {
1700 tlb_flush(CPU(mips_env_get_cpu(env)));
1701 }
1702 }
1703
1704 void helper_mttc0_entryhi(CPUMIPSState *env, target_ulong arg1)
1705 {
1706 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1707 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1708
1709 other->CP0_EntryHi = arg1;
1710 sync_c0_entryhi(other, other_tc);
1711 }
1712
1713 void helper_mtc0_compare(CPUMIPSState *env, target_ulong arg1)
1714 {
1715 qemu_mutex_lock_iothread();
1716 cpu_mips_store_compare(env, arg1);
1717 qemu_mutex_unlock_iothread();
1718 }
1719
1720 void helper_mtc0_status(CPUMIPSState *env, target_ulong arg1)
1721 {
1722 MIPSCPU *cpu = mips_env_get_cpu(env);
1723 uint32_t val, old;
1724
1725 old = env->CP0_Status;
1726 cpu_mips_store_status(env, arg1);
1727 val = env->CP0_Status;
1728
1729 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1730 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1731 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1732 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1733 env->CP0_Cause);
1734 switch (cpu_mmu_index(env, false)) {
1735 case 3:
1736 qemu_log(", ERL\n");
1737 break;
1738 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1739 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1740 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1741 default:
1742 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
1743 break;
1744 }
1745 }
1746 }
1747
1748 void helper_mttc0_status(CPUMIPSState *env, target_ulong arg1)
1749 {
1750 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1751 uint32_t mask = env->CP0_Status_rw_bitmask & ~0xf1000018;
1752 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1753
1754 other->CP0_Status = (other->CP0_Status & ~mask) | (arg1 & mask);
1755 sync_c0_status(env, other, other_tc);
1756 }
1757
1758 void helper_mtc0_intctl(CPUMIPSState *env, target_ulong arg1)
1759 {
1760 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000003e0) | (arg1 & 0x000003e0);
1761 }
1762
1763 void helper_mtc0_srsctl(CPUMIPSState *env, target_ulong arg1)
1764 {
1765 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1766 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1767 }
1768
1769 void helper_mtc0_cause(CPUMIPSState *env, target_ulong arg1)
1770 {
1771 qemu_mutex_lock_iothread();
1772 cpu_mips_store_cause(env, arg1);
1773 qemu_mutex_unlock_iothread();
1774 }
1775
1776 void helper_mttc0_cause(CPUMIPSState *env, target_ulong arg1)
1777 {
1778 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1779 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1780
1781 cpu_mips_store_cause(other, arg1);
1782 }
1783
1784 target_ulong helper_mftc0_epc(CPUMIPSState *env)
1785 {
1786 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1787 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1788
1789 return other->CP0_EPC;
1790 }
1791
1792 target_ulong helper_mftc0_ebase(CPUMIPSState *env)
1793 {
1794 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1795 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1796
1797 return other->CP0_EBase;
1798 }
1799
1800 void helper_mtc0_ebase(CPUMIPSState *env, target_ulong arg1)
1801 {
1802 target_ulong mask = 0x3FFFF000 | env->CP0_EBaseWG_rw_bitmask;
1803 if (arg1 & env->CP0_EBaseWG_rw_bitmask) {
1804 mask |= ~0x3FFFFFFF;
1805 }
1806 env->CP0_EBase = (env->CP0_EBase & ~mask) | (arg1 & mask);
1807 }
1808
1809 void helper_mttc0_ebase(CPUMIPSState *env, target_ulong arg1)
1810 {
1811 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1812 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1813 target_ulong mask = 0x3FFFF000 | env->CP0_EBaseWG_rw_bitmask;
1814 if (arg1 & env->CP0_EBaseWG_rw_bitmask) {
1815 mask |= ~0x3FFFFFFF;
1816 }
1817 other->CP0_EBase = (other->CP0_EBase & ~mask) | (arg1 & mask);
1818 }
1819
1820 target_ulong helper_mftc0_configx(CPUMIPSState *env, target_ulong idx)
1821 {
1822 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1823 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1824
1825 switch (idx) {
1826 case 0: return other->CP0_Config0;
1827 case 1: return other->CP0_Config1;
1828 case 2: return other->CP0_Config2;
1829 case 3: return other->CP0_Config3;
1830 /* 4 and 5 are reserved. */
1831 case 6: return other->CP0_Config6;
1832 case 7: return other->CP0_Config7;
1833 default:
1834 break;
1835 }
1836 return 0;
1837 }
1838
1839 void helper_mtc0_config0(CPUMIPSState *env, target_ulong arg1)
1840 {
1841 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1842 }
1843
1844 void helper_mtc0_config2(CPUMIPSState *env, target_ulong arg1)
1845 {
1846 /* tertiary/secondary caches not implemented */
1847 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1848 }
1849
1850 void helper_mtc0_config3(CPUMIPSState *env, target_ulong arg1)
1851 {
1852 if (env->insn_flags & ASE_MICROMIPS) {
1853 env->CP0_Config3 = (env->CP0_Config3 & ~(1 << CP0C3_ISA_ON_EXC)) |
1854 (arg1 & (1 << CP0C3_ISA_ON_EXC));
1855 }
1856 }
1857
1858 void helper_mtc0_config4(CPUMIPSState *env, target_ulong arg1)
1859 {
1860 env->CP0_Config4 = (env->CP0_Config4 & (~env->CP0_Config4_rw_bitmask)) |
1861 (arg1 & env->CP0_Config4_rw_bitmask);
1862 }
1863
1864 void helper_mtc0_config5(CPUMIPSState *env, target_ulong arg1)
1865 {
1866 env->CP0_Config5 = (env->CP0_Config5 & (~env->CP0_Config5_rw_bitmask)) |
1867 (arg1 & env->CP0_Config5_rw_bitmask);
1868 compute_hflags(env);
1869 }
1870
1871 void helper_mtc0_lladdr(CPUMIPSState *env, target_ulong arg1)
1872 {
1873 target_long mask = env->CP0_LLAddr_rw_bitmask;
1874 arg1 = arg1 << env->CP0_LLAddr_shift;
1875 env->CP0_LLAddr = (env->CP0_LLAddr & ~mask) | (arg1 & mask);
1876 }
1877
1878 #define MTC0_MAAR_MASK(env) \
1879 ((0x1ULL << 63) | ((env->PAMask >> 4) & ~0xFFFull) | 0x3)
1880
1881 void helper_mtc0_maar(CPUMIPSState *env, target_ulong arg1)
1882 {
1883 env->CP0_MAAR[env->CP0_MAARI] = arg1 & MTC0_MAAR_MASK(env);
1884 }
1885
1886 void helper_mthc0_maar(CPUMIPSState *env, target_ulong arg1)
1887 {
1888 env->CP0_MAAR[env->CP0_MAARI] =
1889 (((uint64_t) arg1 << 32) & MTC0_MAAR_MASK(env)) |
1890 (env->CP0_MAAR[env->CP0_MAARI] & 0x00000000ffffffffULL);
1891 }
1892
1893 void helper_mtc0_maari(CPUMIPSState *env, target_ulong arg1)
1894 {
1895 int index = arg1 & 0x3f;
1896 if (index == 0x3f) {
1897 /* Software may write all ones to INDEX to determine the
1898 maximum value supported. */
1899 env->CP0_MAARI = MIPS_MAAR_MAX - 1;
1900 } else if (index < MIPS_MAAR_MAX) {
1901 env->CP0_MAARI = index;
1902 }
1903 /* Other than the all ones, if the
1904 value written is not supported, then INDEX is unchanged
1905 from its previous value. */
1906 }
1907
1908 void helper_mtc0_watchlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1909 {
1910 /* Watch exceptions for instructions, data loads, data stores
1911 not implemented. */
1912 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1913 }
1914
1915 void helper_mtc0_watchhi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
1916 {
1917 int mask = 0x40000FF8 | (env->CP0_EntryHi_ASID_mask << CP0WH_ASID);
1918 env->CP0_WatchHi[sel] = arg1 & mask;
1919 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1920 }
1921
1922 void helper_mtc0_xcontext(CPUMIPSState *env, target_ulong arg1)
1923 {
1924 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1925 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1926 }
1927
1928 void helper_mtc0_framemask(CPUMIPSState *env, target_ulong arg1)
1929 {
1930 env->CP0_Framemask = arg1; /* XXX */
1931 }
1932
1933 void helper_mtc0_debug(CPUMIPSState *env, target_ulong arg1)
1934 {
1935 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1936 if (arg1 & (1 << CP0DB_DM))
1937 env->hflags |= MIPS_HFLAG_DM;
1938 else
1939 env->hflags &= ~MIPS_HFLAG_DM;
1940 }
1941
1942 void helper_mttc0_debug(CPUMIPSState *env, target_ulong arg1)
1943 {
1944 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1945 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1946 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
1947
1948 /* XXX: Might be wrong, check with EJTAG spec. */
1949 if (other_tc == other->current_tc)
1950 other->active_tc.CP0_Debug_tcstatus = val;
1951 else
1952 other->tcs[other_tc].CP0_Debug_tcstatus = val;
1953 other->CP0_Debug = (other->CP0_Debug &
1954 ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1955 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1956 }
1957
1958 void helper_mtc0_performance0(CPUMIPSState *env, target_ulong arg1)
1959 {
1960 env->CP0_Performance0 = arg1 & 0x000007ff;
1961 }
1962
1963 void helper_mtc0_errctl(CPUMIPSState *env, target_ulong arg1)
1964 {
1965 int32_t wst = arg1 & (1 << CP0EC_WST);
1966 int32_t spr = arg1 & (1 << CP0EC_SPR);
1967 int32_t itc = env->itc_tag ? (arg1 & (1 << CP0EC_ITC)) : 0;
1968
1969 env->CP0_ErrCtl = wst | spr | itc;
1970
1971 if (itc && !wst && !spr) {
1972 env->hflags |= MIPS_HFLAG_ITC_CACHE;
1973 } else {
1974 env->hflags &= ~MIPS_HFLAG_ITC_CACHE;
1975 }
1976 }
1977
1978 void helper_mtc0_taglo(CPUMIPSState *env, target_ulong arg1)
1979 {
1980 if (env->hflags & MIPS_HFLAG_ITC_CACHE) {
1981 /* If CACHE instruction is configured for ITC tags then make all
1982 CP0.TagLo bits writable. The actual write to ITC Configuration
1983 Tag will take care of the read-only bits. */
1984 env->CP0_TagLo = arg1;
1985 } else {
1986 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1987 }
1988 }
1989
1990 void helper_mtc0_datalo(CPUMIPSState *env, target_ulong arg1)
1991 {
1992 env->CP0_DataLo = arg1; /* XXX */
1993 }
1994
1995 void helper_mtc0_taghi(CPUMIPSState *env, target_ulong arg1)
1996 {
1997 env->CP0_TagHi = arg1; /* XXX */
1998 }
1999
2000 void helper_mtc0_datahi(CPUMIPSState *env, target_ulong arg1)
2001 {
2002 env->CP0_DataHi = arg1; /* XXX */
2003 }
2004
2005 /* MIPS MT functions */
2006 target_ulong helper_mftgpr(CPUMIPSState *env, uint32_t sel)
2007 {
2008 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2009 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2010
2011 if (other_tc == other->current_tc)
2012 return other->active_tc.gpr[sel];
2013 else
2014 return other->tcs[other_tc].gpr[sel];
2015 }
2016
2017 target_ulong helper_mftlo(CPUMIPSState *env, uint32_t sel)
2018 {
2019 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2020 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2021
2022 if (other_tc == other->current_tc)
2023 return other->active_tc.LO[sel];
2024 else
2025 return other->tcs[other_tc].LO[sel];
2026 }
2027
2028 target_ulong helper_mfthi(CPUMIPSState *env, uint32_t sel)
2029 {
2030 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2031 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2032
2033 if (other_tc == other->current_tc)
2034 return other->active_tc.HI[sel];
2035 else
2036 return other->tcs[other_tc].HI[sel];
2037 }
2038
2039 target_ulong helper_mftacx(CPUMIPSState *env, uint32_t sel)
2040 {
2041 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2042 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2043
2044 if (other_tc == other->current_tc)
2045 return other->active_tc.ACX[sel];
2046 else
2047 return other->tcs[other_tc].ACX[sel];
2048 }
2049
2050 target_ulong helper_mftdsp(CPUMIPSState *env)
2051 {
2052 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2053 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2054
2055 if (other_tc == other->current_tc)
2056 return other->active_tc.DSPControl;
2057 else
2058 return other->tcs[other_tc].DSPControl;
2059 }
2060
2061 void helper_mttgpr(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
2062 {
2063 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2064 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2065
2066 if (other_tc == other->current_tc)
2067 other->active_tc.gpr[sel] = arg1;
2068 else
2069 other->tcs[other_tc].gpr[sel] = arg1;
2070 }
2071
2072 void helper_mttlo(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
2073 {
2074 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2075 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2076
2077 if (other_tc == other->current_tc)
2078 other->active_tc.LO[sel] = arg1;
2079 else
2080 other->tcs[other_tc].LO[sel] = arg1;
2081 }
2082
2083 void helper_mtthi(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
2084 {
2085 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2086 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2087
2088 if (other_tc == other->current_tc)
2089 other->active_tc.HI[sel] = arg1;
2090 else
2091 other->tcs[other_tc].HI[sel] = arg1;
2092 }
2093
2094 void helper_mttacx(CPUMIPSState *env, target_ulong arg1, uint32_t sel)
2095 {
2096 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2097 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2098
2099 if (other_tc == other->current_tc)
2100 other->active_tc.ACX[sel] = arg1;
2101 else
2102 other->tcs[other_tc].ACX[sel] = arg1;
2103 }
2104
2105 void helper_mttdsp(CPUMIPSState *env, target_ulong arg1)
2106 {
2107 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
2108 CPUMIPSState *other = mips_cpu_map_tc(env, &other_tc);
2109
2110 if (other_tc == other->current_tc)
2111 other->active_tc.DSPControl = arg1;
2112 else
2113 other->tcs[other_tc].DSPControl = arg1;
2114 }
2115
2116 /* MIPS MT functions */
2117 target_ulong helper_dmt(void)
2118 {
2119 // TODO
2120 return 0;
2121 }
2122
2123 target_ulong helper_emt(void)
2124 {
2125 // TODO
2126 return 0;
2127 }
2128
2129 target_ulong helper_dvpe(CPUMIPSState *env)
2130 {
2131 CPUState *other_cs = first_cpu;
2132 target_ulong prev = env->mvp->CP0_MVPControl;
2133
2134 CPU_FOREACH(other_cs) {
2135 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
2136 /* Turn off all VPEs except the one executing the dvpe. */
2137 if (&other_cpu->env != env) {
2138 other_cpu->env.mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
2139 mips_vpe_sleep(other_cpu);
2140 }
2141 }
2142 return prev;
2143 }
2144
2145 target_ulong helper_evpe(CPUMIPSState *env)
2146 {
2147 CPUState *other_cs = first_cpu;
2148 target_ulong prev = env->mvp->CP0_MVPControl;
2149
2150 CPU_FOREACH(other_cs) {
2151 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
2152
2153 if (&other_cpu->env != env
2154 /* If the VPE is WFI, don't disturb its sleep. */
2155 && !mips_vpe_is_wfi(other_cpu)) {
2156 /* Enable the VPE. */
2157 other_cpu->env.mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
2158 mips_vpe_wake(other_cpu); /* And wake it up. */
2159 }
2160 }
2161 return prev;
2162 }
2163 #endif /* !CONFIG_USER_ONLY */
2164
2165 void helper_fork(target_ulong arg1, target_ulong arg2)
2166 {
2167 // arg1 = rt, arg2 = rs
2168 // TODO: store to TC register
2169 }
2170
2171 target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
2172 {
2173 target_long arg1 = arg;
2174
2175 if (arg1 < 0) {
2176 /* No scheduling policy implemented. */
2177 if (arg1 != -2) {
2178 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
2179 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
2180 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
2181 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
2182 do_raise_exception(env, EXCP_THREAD, GETPC());
2183 }
2184 }
2185 } else if (arg1 == 0) {
2186 if (0 /* TODO: TC underflow */) {
2187 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
2188 do_raise_exception(env, EXCP_THREAD, GETPC());
2189 } else {
2190 // TODO: Deallocate TC
2191 }
2192 } else if (arg1 > 0) {
2193 /* Yield qualifier inputs not implemented. */
2194 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
2195 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
2196 do_raise_exception(env, EXCP_THREAD, GETPC());
2197 }
2198 return env->CP0_YQMask;
2199 }
2200
2201 /* R6 Multi-threading */
2202 #ifndef CONFIG_USER_ONLY
2203 target_ulong helper_dvp(CPUMIPSState *env)
2204 {
2205 CPUState *other_cs = first_cpu;
2206 target_ulong prev = env->CP0_VPControl;
2207
2208 if (!((env->CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
2209 CPU_FOREACH(other_cs) {
2210 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
2211 /* Turn off all VPs except the one executing the dvp. */
2212 if (&other_cpu->env != env) {
2213 mips_vpe_sleep(other_cpu);
2214 }
2215 }
2216 env->CP0_VPControl |= (1 << CP0VPCtl_DIS);
2217 }
2218 return prev;
2219 }
2220
2221 target_ulong helper_evp(CPUMIPSState *env)
2222 {
2223 CPUState *other_cs = first_cpu;
2224 target_ulong prev = env->CP0_VPControl;
2225
2226 if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
2227 CPU_FOREACH(other_cs) {
2228 MIPSCPU *other_cpu = MIPS_CPU(other_cs);
2229 if ((&other_cpu->env != env) && !mips_vp_is_wfi(other_cpu)) {
2230 /* If the VP is WFI, don't disturb its sleep.
2231 * Otherwise, wake it up. */
2232 mips_vpe_wake(other_cpu);
2233 }
2234 }
2235 env->CP0_VPControl &= ~(1 << CP0VPCtl_DIS);
2236 }
2237 return prev;
2238 }
2239 #endif /* !CONFIG_USER_ONLY */
2240
2241 #ifndef CONFIG_USER_ONLY
2242 /* TLB management */
2243 static void r4k_mips_tlb_flush_extra (CPUMIPSState *env, int first)
2244 {
2245 /* Discard entries from env->tlb[first] onwards. */
2246 while (env->tlb->tlb_in_use > first) {
2247 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
2248 }
2249 }
2250
2251 static inline uint64_t get_tlb_pfn_from_entrylo(uint64_t entrylo)
2252 {
2253 #if defined(TARGET_MIPS64)
2254 return extract64(entrylo, 6, 54);
2255 #else
2256 return extract64(entrylo, 6, 24) | /* PFN */
2257 (extract64(entrylo, 32, 32) << 24); /* PFNX */
2258 #endif
2259 }
2260
2261 static void r4k_fill_tlb(CPUMIPSState *env, int idx)
2262 {
2263 r4k_tlb_t *tlb;
2264 uint64_t mask = env->CP0_PageMask >> (TARGET_PAGE_BITS + 1);
2265
2266 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
2267 tlb = &env->tlb->mmu.r4k.tlb[idx];
2268 if (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) {
2269 tlb->EHINV = 1;
2270 return;
2271 }
2272 tlb->EHINV = 0;
2273 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
2274 #if defined(TARGET_MIPS64)
2275 tlb->VPN &= env->SEGMask;
2276 #endif
2277 tlb->ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
2278 tlb->PageMask = env->CP0_PageMask;
2279 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
2280 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
2281 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
2282 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
2283 tlb->XI0 = (env->CP0_EntryLo0 >> CP0EnLo_XI) & 1;
2284 tlb->RI0 = (env->CP0_EntryLo0 >> CP0EnLo_RI) & 1;
2285 tlb->PFN[0] = (get_tlb_pfn_from_entrylo(env->CP0_EntryLo0) & ~mask) << 12;
2286 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
2287 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
2288 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
2289 tlb->XI1 = (env->CP0_EntryLo1 >> CP0EnLo_XI) & 1;
2290 tlb->RI1 = (env->CP0_EntryLo1 >> CP0EnLo_RI) & 1;
2291 tlb->PFN[1] = (get_tlb_pfn_from_entrylo(env->CP0_EntryLo1) & ~mask) << 12;
2292 }
2293
2294 void r4k_helper_tlbinv(CPUMIPSState *env)
2295 {
2296 int idx;
2297 r4k_tlb_t *tlb;
2298 uint16_t ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
2299
2300 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
2301 tlb = &env->tlb->mmu.r4k.tlb[idx];
2302 if (!tlb->G && tlb->ASID == ASID) {
2303 tlb->EHINV = 1;
2304 }
2305 }
2306 cpu_mips_tlb_flush(env);
2307 }
2308
2309 void r4k_helper_tlbinvf(CPUMIPSState *env)
2310 {
2311 int idx;
2312
2313 for (idx = 0; idx < env->tlb->nb_tlb; idx++) {
2314 env->tlb->mmu.r4k.tlb[idx].EHINV = 1;
2315 }
2316 cpu_mips_tlb_flush(env);
2317 }
2318
2319 void r4k_helper_tlbwi(CPUMIPSState *env)
2320 {
2321 r4k_tlb_t *tlb;
2322 int idx;
2323 target_ulong VPN;
2324 uint16_t ASID;
2325 bool EHINV, G, V0, D0, V1, D1, XI0, XI1, RI0, RI1;
2326
2327 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
2328 tlb = &env->tlb->mmu.r4k.tlb[idx];
2329 VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
2330 #if defined(TARGET_MIPS64)
2331 VPN &= env->SEGMask;
2332 #endif
2333 ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
2334 EHINV = (env->CP0_EntryHi & (1 << CP0EnHi_EHINV)) != 0;
2335 G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
2336 V0 = (env->CP0_EntryLo0 & 2) != 0;
2337 D0 = (env->CP0_EntryLo0 & 4) != 0;
2338 XI0 = (env->CP0_EntryLo0 >> CP0EnLo_XI) &1;
2339 RI0 = (env->CP0_EntryLo0 >> CP0EnLo_RI) &1;
2340 V1 = (env->CP0_EntryLo1 & 2) != 0;
2341 D1 = (env->CP0_EntryLo1 & 4) != 0;
2342 XI1 = (env->CP0_EntryLo1 >> CP0EnLo_XI) &1;
2343 RI1 = (env->CP0_EntryLo1 >> CP0EnLo_RI) &1;
2344
2345 /* Discard cached TLB entries, unless tlbwi is just upgrading access
2346 permissions on the current entry. */
2347 if (tlb->VPN != VPN || tlb->ASID != ASID || tlb->G != G ||
2348 (!tlb->EHINV && EHINV) ||
2349 (tlb->V0 && !V0) || (tlb->D0 && !D0) ||
2350 (!tlb->XI0 && XI0) || (!tlb->RI0 && RI0) ||
2351 (tlb->V1 && !V1) || (tlb->D1 && !D1) ||
2352 (!tlb->XI1 && XI1) || (!tlb->RI1 && RI1)) {
2353 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
2354 }
2355
2356 r4k_invalidate_tlb(env, idx, 0);
2357 r4k_fill_tlb(env, idx);
2358 }
2359
2360 void r4k_helper_tlbwr(CPUMIPSState *env)
2361 {
2362 int r = cpu_mips_get_random(env);
2363
2364 r4k_invalidate_tlb(env, r, 1);
2365 r4k_fill_tlb(env, r);
2366 }
2367
2368 void r4k_helper_tlbp(CPUMIPSState *env)
2369 {
2370 r4k_tlb_t *tlb;
2371 target_ulong mask;
2372 target_ulong tag;
2373 target_ulong VPN;
2374 uint16_t ASID;
2375 int i;
2376
2377 ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
2378 for (i = 0; i < env->tlb->nb_tlb; i++) {
2379 tlb = &env->tlb->mmu.r4k.tlb[i];
2380 /* 1k pages are not supported. */
2381 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
2382 tag = env->CP0_EntryHi & ~mask;
2383 VPN = tlb->VPN & ~mask;
2384 #if defined(TARGET_MIPS64)
2385 tag &= env->SEGMask;
2386 #endif
2387 /* Check ASID, virtual page number & size */
2388 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag && !tlb->EHINV) {
2389 /* TLB match */
2390 env->CP0_Index = i;
2391 break;
2392 }
2393 }
2394 if (i == env->tlb->nb_tlb) {
2395 /* No match. Discard any shadow entries, if any of them match. */
2396 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
2397 tlb = &env->tlb->mmu.r4k.tlb[i];
2398 /* 1k pages are not supported. */
2399 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
2400 tag = env->CP0_EntryHi & ~mask;
2401 VPN = tlb->VPN & ~mask;
2402 #if defined(TARGET_MIPS64)
2403 tag &= env->SEGMask;
2404 #endif
2405 /* Check ASID, virtual page number & size */
2406 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
2407 r4k_mips_tlb_flush_extra (env, i);
2408 break;
2409 }
2410 }
2411
2412 env->CP0_Index |= 0x80000000;
2413 }
2414 }
2415
2416 static inline uint64_t get_entrylo_pfn_from_tlb(uint64_t tlb_pfn)
2417 {
2418 #if defined(TARGET_MIPS64)
2419 return tlb_pfn << 6;
2420 #else
2421 return (extract64(tlb_pfn, 0, 24) << 6) | /* PFN */
2422 (extract64(tlb_pfn, 24, 32) << 32); /* PFNX */
2423 #endif
2424 }
2425
2426 void r4k_helper_tlbr(CPUMIPSState *env)
2427 {
2428 r4k_tlb_t *tlb;
2429 uint16_t ASID;
2430 int idx;
2431
2432 ASID = env->CP0_EntryHi & env->CP0_EntryHi_ASID_mask;
2433 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
2434 tlb = &env->tlb->mmu.r4k.tlb[idx];
2435
2436 /* If this will change the current ASID, flush qemu's TLB. */
2437 if (ASID != tlb->ASID)
2438 cpu_mips_tlb_flush(env);
2439
2440 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
2441
2442 if (tlb->EHINV) {
2443 env->CP0_EntryHi = 1 << CP0EnHi_EHINV;
2444 env->CP0_PageMask = 0;
2445 env->CP0_EntryLo0 = 0;
2446 env->CP0_EntryLo1 = 0;
2447 } else {
2448 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
2449 env->CP0_PageMask = tlb->PageMask;
2450 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
2451 ((uint64_t)tlb->RI0 << CP0EnLo_RI) |
2452 ((uint64_t)tlb->XI0 << CP0EnLo_XI) | (tlb->C0 << 3) |
2453 get_entrylo_pfn_from_tlb(tlb->PFN[0] >> 12);
2454 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
2455 ((uint64_t)tlb->RI1 << CP0EnLo_RI) |
2456 ((uint64_t)tlb->XI1 << CP0EnLo_XI) | (tlb->C1 << 3) |
2457 get_entrylo_pfn_from_tlb(tlb->PFN[1] >> 12);
2458 }
2459 }
2460
2461 void helper_tlbwi(CPUMIPSState *env)
2462 {
2463 env->tlb->helper_tlbwi(env);
2464 }
2465
2466 void helper_tlbwr(CPUMIPSState *env)
2467 {
2468 env->tlb->helper_tlbwr(env);
2469 }
2470
2471 void helper_tlbp(CPUMIPSState *env)
2472 {
2473 env->tlb->helper_tlbp(env);
2474 }
2475
2476 void helper_tlbr(CPUMIPSState *env)
2477 {
2478 env->tlb->helper_tlbr(env);
2479 }
2480
2481 void helper_tlbinv(CPUMIPSState *env)
2482 {
2483 env->tlb->helper_tlbinv(env);
2484 }
2485
2486 void helper_tlbinvf(CPUMIPSState *env)
2487 {
2488 env->tlb->helper_tlbinvf(env);
2489 }
2490
2491 /* Specials */
2492 target_ulong helper_di(CPUMIPSState *env)
2493 {
2494 target_ulong t0 = env->CP0_Status;
2495
2496 env->CP0_Status = t0 & ~(1 << CP0St_IE);
2497 return t0;
2498 }
2499
2500 target_ulong helper_ei(CPUMIPSState *env)
2501 {
2502 target_ulong t0 = env->CP0_Status;
2503
2504 env->CP0_Status = t0 | (1 << CP0St_IE);
2505 return t0;
2506 }
2507
2508 static void debug_pre_eret(CPUMIPSState *env)
2509 {
2510 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2511 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2512 env->active_tc.PC, env->CP0_EPC);
2513 if (env->CP0_Status & (1 << CP0St_ERL))
2514 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2515 if (env->hflags & MIPS_HFLAG_DM)
2516 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2517 qemu_log("\n");
2518 }
2519 }
2520
2521 static void debug_post_eret(CPUMIPSState *env)
2522 {
2523 MIPSCPU *cpu = mips_env_get_cpu(env);
2524
2525 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
2526 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
2527 env->active_tc.PC, env->CP0_EPC);
2528 if (env->CP0_Status & (1 << CP0St_ERL))
2529 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
2530 if (env->hflags & MIPS_HFLAG_DM)
2531 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
2532 switch (cpu_mmu_index(env, false)) {
2533 case 3:
2534 qemu_log(", ERL\n");
2535 break;
2536 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
2537 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
2538 case MIPS_HFLAG_KM: qemu_log("\n"); break;
2539 default:
2540 cpu_abort(CPU(cpu), "Invalid MMU mode!\n");
2541 break;
2542 }
2543 }
2544 }
2545
2546 static void set_pc(CPUMIPSState *env, target_ulong error_pc)
2547 {
2548 env->active_tc.PC = error_pc & ~(target_ulong)1;
2549 if (error_pc & 1) {
2550 env->hflags |= MIPS_HFLAG_M16;
2551 } else {
2552 env->hflags &= ~(MIPS_HFLAG_M16);
2553 }
2554 }
2555
2556 static inline void exception_return(CPUMIPSState *env)
2557 {
2558 debug_pre_eret(env);
2559 if (env->CP0_Status & (1 << CP0St_ERL)) {
2560 set_pc(env, env->CP0_ErrorEPC);
2561 env->CP0_Status &= ~(1 << CP0St_ERL);
2562 } else {
2563 set_pc(env, env->CP0_EPC);
2564 env->CP0_Status &= ~(1 << CP0St_EXL);
2565 }
2566 compute_hflags(env);
2567 debug_post_eret(env);
2568 }
2569
2570 void helper_eret(CPUMIPSState *env)
2571 {
2572 exception_return(env);
2573 env->CP0_LLAddr = 1;
2574 env->lladdr = 1;
2575 }
2576
2577 void helper_eretnc(CPUMIPSState *env)
2578 {
2579 exception_return(env);
2580 }
2581
2582 void helper_deret(CPUMIPSState *env)
2583 {
2584 debug_pre_eret(env);
2585
2586 env->hflags &= ~MIPS_HFLAG_DM;
2587 compute_hflags(env);
2588
2589 set_pc(env, env->CP0_DEPC);
2590
2591 debug_post_eret(env);
2592 }
2593 #endif /* !CONFIG_USER_ONLY */
2594
2595 static inline void check_hwrena(CPUMIPSState *env, int reg, uintptr_t pc)
2596 {
2597 if ((env->hflags & MIPS_HFLAG_CP0) || (env->CP0_HWREna & (1 << reg))) {
2598 return;
2599 }
2600 do_raise_exception(env, EXCP_RI, pc);
2601 }
2602
2603 target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
2604 {
2605 check_hwrena(env, 0, GETPC());
2606 return env->CP0_EBase & 0x3ff;
2607 }
2608
2609 target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
2610 {
2611 check_hwrena(env, 1, GETPC());
2612 return env->SYNCI_Step;
2613 }
2614
2615 target_ulong helper_rdhwr_cc(CPUMIPSState *env)
2616 {
2617 int32_t count;
2618 check_hwrena(env, 2, GETPC());
2619 #ifdef CONFIG_USER_ONLY
2620 count = env->CP0_Count;
2621 #else
2622 qemu_mutex_lock_iothread();
2623 count = (int32_t)cpu_mips_get_count(env);
2624 qemu_mutex_unlock_iothread();
2625 #endif
2626 return count;
2627 }
2628
2629 target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
2630 {
2631 check_hwrena(env, 3, GETPC());
2632 return env->CCRes;
2633 }
2634
2635 target_ulong helper_rdhwr_performance(CPUMIPSState *env)
2636 {
2637 check_hwrena(env, 4, GETPC());
2638 return env->CP0_Performance0;
2639 }
2640
2641 target_ulong helper_rdhwr_xnp(CPUMIPSState *env)
2642 {
2643 check_hwrena(env, 5, GETPC());
2644 return (env->CP0_Config5 >> CP0C5_XNP) & 1;
2645 }
2646
2647 void helper_pmon(CPUMIPSState *env, int function)
2648 {
2649 function /= 2;
2650 switch (function) {
2651 case 2: /* TODO: char inbyte(int waitflag); */
2652 if (env->active_tc.gpr[4] == 0)
2653 env->active_tc.gpr[2] = -1;
2654 /* Fall through */
2655 case 11: /* TODO: char inbyte (void); */
2656 env->active_tc.gpr[2] = -1;
2657 break;
2658 case 3:
2659 case 12:
2660 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
2661 break;
2662 case 17:
2663 break;
2664 case 158:
2665 {
2666 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
2667 printf("%s", fmt);
2668 }
2669 break;
2670 }
2671 }
2672
2673 void helper_wait(CPUMIPSState *env)
2674 {
2675 CPUState *cs = CPU(mips_env_get_cpu(env));
2676
2677 cs->halted = 1;
2678 cpu_reset_interrupt(cs, CPU_INTERRUPT_WAKE);
2679 /* Last instruction in the block, PC was updated before
2680 - no need to recover PC and icount */
2681 raise_exception(env, EXCP_HLT);
2682 }
2683
2684 #if !defined(CONFIG_USER_ONLY)
2685
2686 void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
2687 MMUAccessType access_type,
2688 int mmu_idx, uintptr_t retaddr)
2689 {
2690 MIPSCPU *cpu = MIPS_CPU(cs);
2691 CPUMIPSState *env = &cpu->env;
2692 int error_code = 0;
2693 int excp;
2694
2695 if (!(env->hflags & MIPS_HFLAG_DM)) {
2696 env->CP0_BadVAddr = addr;
2697 }
2698
2699 if (access_type == MMU_DATA_STORE) {
2700 excp = EXCP_AdES;
2701 } else {
2702 excp = EXCP_AdEL;
2703 if (access_type == MMU_INST_FETCH) {
2704 error_code |= EXCP_INST_NOTAVAIL;
2705 }
2706 }
2707
2708 do_raise_exception_err(env, excp, error_code, retaddr);
2709 }
2710
2711 void tlb_fill(CPUState *cs, target_ulong addr, int size,
2712 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
2713 {
2714 int ret;
2715
2716 ret = mips_cpu_handle_mmu_fault(cs, addr, size, access_type, mmu_idx);
2717 if (ret) {
2718 MIPSCPU *cpu = MIPS_CPU(cs);
2719 CPUMIPSState *env = &cpu->env;
2720
2721 do_raise_exception_err(env, cs->exception_index,
2722 env->error_code, retaddr);
2723 }
2724 }
2725
2726 void mips_cpu_unassigned_access(CPUState *cs, hwaddr addr,
2727 bool is_write, bool is_exec, int unused,
2728 unsigned size)
2729 {
2730 MIPSCPU *cpu = MIPS_CPU(cs);
2731 CPUMIPSState *env = &cpu->env;
2732
2733 /*
2734 * Raising an exception with KVM enabled will crash because it won't be from
2735 * the main execution loop so the longjmp won't have a matching setjmp.
2736 * Until we can trigger a bus error exception through KVM lets just ignore
2737 * the access.
2738 */
2739 if (kvm_enabled()) {
2740 return;
2741 }
2742
2743 if (is_exec) {
2744 raise_exception(env, EXCP_IBE);
2745 } else {
2746 raise_exception(env, EXCP_DBE);
2747 }
2748 }
2749 #endif /* !CONFIG_USER_ONLY */
2750
2751 /* Complex FPU operations which may need stack space. */
2752
2753 #define FLOAT_TWO32 make_float32(1 << 30)
2754 #define FLOAT_TWO64 make_float64(1ULL << 62)
2755
2756 #define FP_TO_INT32_OVERFLOW 0x7fffffff
2757 #define FP_TO_INT64_OVERFLOW 0x7fffffffffffffffULL
2758
2759 /* convert MIPS rounding mode in FCR31 to IEEE library */
2760 unsigned int ieee_rm[] = {
2761 float_round_nearest_even,
2762 float_round_to_zero,
2763 float_round_up,
2764 float_round_down
2765 };
2766
2767 target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)
2768 {
2769 target_ulong arg1 = 0;
2770
2771 switch (reg) {
2772 case 0:
2773 arg1 = (int32_t)env->active_fpu.fcr0;
2774 break;
2775 case 1:
2776 /* UFR Support - Read Status FR */
2777 if (env->active_fpu.fcr0 & (1 << FCR0_UFRP)) {
2778 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2779 arg1 = (int32_t)
2780 ((env->CP0_Status & (1 << CP0St_FR)) >> CP0St_FR);
2781 } else {
2782 do_raise_exception(env, EXCP_RI, GETPC());
2783 }
2784 }
2785 break;
2786 case 5:
2787 /* FRE Support - read Config5.FRE bit */
2788 if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
2789 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2790 arg1 = (env->CP0_Config5 >> CP0C5_FRE) & 1;
2791 } else {
2792 helper_raise_exception(env, EXCP_RI);
2793 }
2794 }
2795 break;
2796 case 25:
2797 arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
2798 break;
2799 case 26:
2800 arg1 = env->active_fpu.fcr31 & 0x0003f07c;
2801 break;
2802 case 28:
2803 arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
2804 break;
2805 default:
2806 arg1 = (int32_t)env->active_fpu.fcr31;
2807 break;
2808 }
2809
2810 return arg1;
2811 }
2812
2813 void helper_ctc1(CPUMIPSState *env, target_ulong arg1, uint32_t fs, uint32_t rt)
2814 {
2815 switch (fs) {
2816 case 1:
2817 /* UFR Alias - Reset Status FR */
2818 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2819 return;
2820 }
2821 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2822 env->CP0_Status &= ~(1 << CP0St_FR);
2823 compute_hflags(env);
2824 } else {
2825 do_raise_exception(env, EXCP_RI, GETPC());
2826 }
2827 break;
2828 case 4:
2829 /* UNFR Alias - Set Status FR */
2830 if (!((env->active_fpu.fcr0 & (1 << FCR0_UFRP)) && (rt == 0))) {
2831 return;
2832 }
2833 if (env->CP0_Config5 & (1 << CP0C5_UFR)) {
2834 env->CP0_Status |= (1 << CP0St_FR);
2835 compute_hflags(env);
2836 } else {
2837 do_raise_exception(env, EXCP_RI, GETPC());
2838 }
2839 break;
2840 case 5:
2841 /* FRE Support - clear Config5.FRE bit */
2842 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2843 return;
2844 }
2845 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2846 env->CP0_Config5 &= ~(1 << CP0C5_FRE);
2847 compute_hflags(env);
2848 } else {
2849 helper_raise_exception(env, EXCP_RI);
2850 }
2851 break;
2852 case 6:
2853 /* FRE Support - set Config5.FRE bit */
2854 if (!((env->active_fpu.fcr0 & (1 << FCR0_FREP)) && (rt == 0))) {
2855 return;
2856 }
2857 if (env->CP0_Config5 & (1 << CP0C5_UFE)) {
2858 env->CP0_Config5 |= (1 << CP0C5_FRE);
2859 compute_hflags(env);
2860 } else {
2861 helper_raise_exception(env, EXCP_RI);
2862 }
2863 break;
2864 case 25:
2865 if ((env->insn_flags & ISA_MIPS32R6) || (arg1 & 0xffffff00)) {
2866 return;
2867 }
2868 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
2869 ((arg1 & 0x1) << 23);
2870 break;
2871 case 26:
2872 if (arg1 & 0x007c0000)
2873 return;
2874 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2875 break;
2876 case 28:
2877 if (arg1 & 0x007c0000)
2878 return;
2879 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
2880 ((arg1 & 0x4) << 22);
2881 break;
2882 case 31:
2883 env->active_fpu.fcr31 = (arg1 & env->active_fpu.fcr31_rw_bitmask) |
2884 (env->active_fpu.fcr31 & ~(env->active_fpu.fcr31_rw_bitmask));
2885 break;
2886 default:
2887 if (env->insn_flags & ISA_MIPS32R6) {
2888 do_raise_exception(env, EXCP_RI, GETPC());
2889 }
2890 return;
2891 }
2892 restore_fp_status(env);
2893 set_float_exception_flags(0, &env->active_fpu.fp_status);
2894 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2895 do_raise_exception(env, EXCP_FPE, GETPC());
2896 }
2897
2898 int ieee_ex_to_mips(int xcpt)
2899 {
2900 int ret = 0;
2901 if (xcpt) {
2902 if (xcpt & float_flag_invalid) {
2903 ret |= FP_INVALID;
2904 }
2905 if (xcpt & float_flag_overflow) {
2906 ret |= FP_OVERFLOW;
2907 }
2908 if (xcpt & float_flag_underflow) {
2909 ret |= FP_UNDERFLOW;
2910 }
2911 if (xcpt & float_flag_divbyzero) {
2912 ret |= FP_DIV0;
2913 }
2914 if (xcpt & float_flag_inexact) {
2915 ret |= FP_INEXACT;
2916 }
2917 }
2918 return ret;
2919 }
2920
2921 static inline void update_fcr31(CPUMIPSState *env, uintptr_t pc)
2922 {
2923 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2924
2925 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
2926
2927 if (tmp) {
2928 set_float_exception_flags(0, &env->active_fpu.fp_status);
2929
2930 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp) {
2931 do_raise_exception(env, EXCP_FPE, pc);
2932 } else {
2933 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2934 }
2935 }
2936 }
2937
2938 /* Float support.
2939 Single precition routines have a "s" suffix, double precision a
2940 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
2941 paired single lower "pl", paired single upper "pu". */
2942
2943 /* unary operations, modifying fp status */
2944 uint64_t helper_float_sqrt_d(CPUMIPSState *env, uint64_t fdt0)
2945 {
2946 fdt0 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2947 update_fcr31(env, GETPC());
2948 return fdt0;
2949 }
2950
2951 uint32_t helper_float_sqrt_s(CPUMIPSState *env, uint32_t fst0)
2952 {
2953 fst0 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2954 update_fcr31(env, GETPC());
2955 return fst0;
2956 }
2957
2958 uint64_t helper_float_cvtd_s(CPUMIPSState *env, uint32_t fst0)
2959 {
2960 uint64_t fdt2;
2961
2962 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2963 update_fcr31(env, GETPC());
2964 return fdt2;
2965 }
2966
2967 uint64_t helper_float_cvtd_w(CPUMIPSState *env, uint32_t wt0)
2968 {
2969 uint64_t fdt2;
2970
2971 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2972 update_fcr31(env, GETPC());
2973 return fdt2;
2974 }
2975
2976 uint64_t helper_float_cvtd_l(CPUMIPSState *env, uint64_t dt0)
2977 {
2978 uint64_t fdt2;
2979
2980 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2981 update_fcr31(env, GETPC());
2982 return fdt2;
2983 }
2984
2985 uint64_t helper_float_cvt_l_d(CPUMIPSState *env, uint64_t fdt0)
2986 {
2987 uint64_t dt2;
2988
2989 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2990 if (get_float_exception_flags(&env->active_fpu.fp_status)
2991 & (float_flag_invalid | float_flag_overflow)) {
2992 dt2 = FP_TO_INT64_OVERFLOW;
2993 }
2994 update_fcr31(env, GETPC());
2995 return dt2;
2996 }
2997
2998 uint64_t helper_float_cvt_l_s(CPUMIPSState *env, uint32_t fst0)
2999 {
3000 uint64_t dt2;
3001
3002 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3003 if (get_float_exception_flags(&env->active_fpu.fp_status)
3004 & (float_flag_invalid | float_flag_overflow)) {
3005 dt2 = FP_TO_INT64_OVERFLOW;
3006 }
3007 update_fcr31(env, GETPC());
3008 return dt2;
3009 }
3010
3011 uint64_t helper_float_cvtps_pw(CPUMIPSState *env, uint64_t dt0)
3012 {
3013 uint32_t fst2;
3014 uint32_t fsth2;
3015
3016 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3017 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
3018 update_fcr31(env, GETPC());
3019 return ((uint64_t)fsth2 << 32) | fst2;
3020 }
3021
3022 uint64_t helper_float_cvtpw_ps(CPUMIPSState *env, uint64_t fdt0)
3023 {
3024 uint32_t wt2;
3025 uint32_t wth2;
3026 int excp, excph;
3027
3028 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3029 excp = get_float_exception_flags(&env->active_fpu.fp_status);
3030 if (excp & (float_flag_overflow | float_flag_invalid)) {
3031 wt2 = FP_TO_INT32_OVERFLOW;
3032 }
3033
3034 set_float_exception_flags(0, &env->active_fpu.fp_status);
3035 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
3036 excph = get_float_exception_flags(&env->active_fpu.fp_status);
3037 if (excph & (float_flag_overflow | float_flag_invalid)) {
3038 wth2 = FP_TO_INT32_OVERFLOW;
3039 }
3040
3041 set_float_exception_flags(excp | excph, &env->active_fpu.fp_status);
3042 update_fcr31(env, GETPC());
3043
3044 return ((uint64_t)wth2 << 32) | wt2;
3045 }
3046
3047 uint32_t helper_float_cvts_d(CPUMIPSState *env, uint64_t fdt0)
3048 {
3049 uint32_t fst2;
3050
3051 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
3052 update_fcr31(env, GETPC());
3053 return fst2;
3054 }
3055
3056 uint32_t helper_float_cvts_w(CPUMIPSState *env, uint32_t wt0)
3057 {
3058 uint32_t fst2;
3059
3060 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
3061 update_fcr31(env, GETPC());
3062 return fst2;
3063 }
3064
3065 uint32_t helper_float_cvts_l(CPUMIPSState *env, uint64_t dt0)
3066 {
3067 uint32_t fst2;
3068
3069 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
3070 update_fcr31(env, GETPC());
3071 return fst2;
3072 }
3073
3074 uint32_t helper_float_cvts_pl(CPUMIPSState *env, uint32_t wt0)
3075 {
3076 uint32_t wt2;
3077
3078 wt2 = wt0;
3079 update_fcr31(env, GETPC());
3080 return wt2;
3081 }
3082
3083 uint32_t helper_float_cvts_pu(CPUMIPSState *env, uint32_t wth0)
3084 {
3085 uint32_t wt2;
3086
3087 wt2 = wth0;
3088 update_fcr31(env, GETPC());
3089 return wt2;
3090 }
3091
3092 uint32_t helper_float_cvt_w_s(CPUMIPSState *env, uint32_t fst0)
3093 {
3094 uint32_t wt2;
3095
3096 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3097 if (get_float_exception_flags(&env->active_fpu.fp_status)
3098 & (float_flag_invalid | float_flag_overflow)) {
3099 wt2 = FP_TO_INT32_OVERFLOW;
3100 }
3101 update_fcr31(env, GETPC());
3102 return wt2;
3103 }
3104
3105 uint32_t helper_float_cvt_w_d(CPUMIPSState *env, uint64_t fdt0)
3106 {
3107 uint32_t wt2;
3108
3109 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3110 if (get_float_exception_flags(&env->active_fpu.fp_status)
3111 & (float_flag_invalid | float_flag_overflow)) {
3112 wt2 = FP_TO_INT32_OVERFLOW;
3113 }
3114 update_fcr31(env, GETPC());
3115 return wt2;
3116 }
3117
3118 uint64_t helper_float_round_l_d(CPUMIPSState *env, uint64_t fdt0)
3119 {
3120 uint64_t dt2;
3121
3122 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
3123 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3124 restore_rounding_mode(env);
3125 if (get_float_exception_flags(&env->active_fpu.fp_status)
3126 & (float_flag_invalid | float_flag_overflow)) {
3127 dt2 = FP_TO_INT64_OVERFLOW;
3128 }
3129 update_fcr31(env, GETPC());
3130 return dt2;
3131 }
3132
3133 uint64_t helper_float_round_l_s(CPUMIPSState *env, uint32_t fst0)
3134 {
3135 uint64_t dt2;
3136
3137 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
3138 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3139 restore_rounding_mode(env);
3140 if (get_float_exception_flags(&env->active_fpu.fp_status)
3141 & (float_flag_invalid | float_flag_overflow)) {
3142 dt2 = FP_TO_INT64_OVERFLOW;
3143 }
3144 update_fcr31(env, GETPC());
3145 return dt2;
3146 }
3147
3148 uint32_t helper_float_round_w_d(CPUMIPSState *env, uint64_t fdt0)
3149 {
3150 uint32_t wt2;
3151
3152 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
3153 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3154 restore_rounding_mode(env);
3155 if (get_float_exception_flags(&env->active_fpu.fp_status)
3156 & (float_flag_invalid | float_flag_overflow)) {
3157 wt2 = FP_TO_INT32_OVERFLOW;
3158 }
3159 update_fcr31(env, GETPC());
3160 return wt2;
3161 }
3162
3163 uint32_t helper_float_round_w_s(CPUMIPSState *env, uint32_t fst0)
3164 {
3165 uint32_t wt2;
3166
3167 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
3168 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3169 restore_rounding_mode(env);
3170 if (get_float_exception_flags(&env->active_fpu.fp_status)
3171 & (float_flag_invalid | float_flag_overflow)) {
3172 wt2 = FP_TO_INT32_OVERFLOW;
3173 }
3174 update_fcr31(env, GETPC());
3175 return wt2;
3176 }
3177
3178 uint64_t helper_float_trunc_l_d(CPUMIPSState *env, uint64_t fdt0)
3179 {
3180 uint64_t dt2;
3181
3182 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
3183 if (get_float_exception_flags(&env->active_fpu.fp_status)
3184 & (float_flag_invalid | float_flag_overflow)) {
3185 dt2 = FP_TO_INT64_OVERFLOW;
3186 }
3187 update_fcr31(env, GETPC());
3188 return dt2;
3189 }
3190
3191 uint64_t helper_float_trunc_l_s(CPUMIPSState *env, uint32_t fst0)
3192 {
3193 uint64_t dt2;
3194
3195 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
3196 if (get_float_exception_flags(&env->active_fpu.fp_status)
3197 & (float_flag_invalid | float_flag_overflow)) {
3198 dt2 = FP_TO_INT64_OVERFLOW;
3199 }
3200 update_fcr31(env, GETPC());
3201 return dt2;
3202 }
3203
3204 uint32_t helper_float_trunc_w_d(CPUMIPSState *env, uint64_t fdt0)
3205 {
3206 uint32_t wt2;
3207
3208 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
3209 if (get_float_exception_flags(&env->active_fpu.fp_status)
3210 & (float_flag_invalid | float_flag_overflow)) {
3211 wt2 = FP_TO_INT32_OVERFLOW;
3212 }
3213 update_fcr31(env, GETPC());
3214 return wt2;
3215 }
3216
3217 uint32_t helper_float_trunc_w_s(CPUMIPSState *env, uint32_t fst0)
3218 {
3219 uint32_t wt2;
3220
3221 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
3222 if (get_float_exception_flags(&env->active_fpu.fp_status)
3223 & (float_flag_invalid | float_flag_overflow)) {
3224 wt2 = FP_TO_INT32_OVERFLOW;
3225 }
3226 update_fcr31(env, GETPC());
3227 return wt2;
3228 }
3229
3230 uint64_t helper_float_ceil_l_d(CPUMIPSState *env, uint64_t fdt0)
3231 {
3232 uint64_t dt2;
3233
3234 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3235 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3236 restore_rounding_mode(env);
3237 if (get_float_exception_flags(&env->active_fpu.fp_status)
3238 & (float_flag_invalid | float_flag_overflow)) {
3239 dt2 = FP_TO_INT64_OVERFLOW;
3240 }
3241 update_fcr31(env, GETPC());
3242 return dt2;
3243 }
3244
3245 uint64_t helper_float_ceil_l_s(CPUMIPSState *env, uint32_t fst0)
3246 {
3247 uint64_t dt2;
3248
3249 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3250 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3251 restore_rounding_mode(env);
3252 if (get_float_exception_flags(&env->active_fpu.fp_status)
3253 & (float_flag_invalid | float_flag_overflow)) {
3254 dt2 = FP_TO_INT64_OVERFLOW;
3255 }
3256 update_fcr31(env, GETPC());
3257 return dt2;
3258 }
3259
3260 uint32_t helper_float_ceil_w_d(CPUMIPSState *env, uint64_t fdt0)
3261 {
3262 uint32_t wt2;
3263
3264 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3265 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3266 restore_rounding_mode(env);
3267 if (get_float_exception_flags(&env->active_fpu.fp_status)
3268 & (float_flag_invalid | float_flag_overflow)) {
3269 wt2 = FP_TO_INT32_OVERFLOW;
3270 }
3271 update_fcr31(env, GETPC());
3272 return wt2;
3273 }
3274
3275 uint32_t helper_float_ceil_w_s(CPUMIPSState *env, uint32_t fst0)
3276 {
3277 uint32_t wt2;
3278
3279 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3280 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3281 restore_rounding_mode(env);
3282 if (get_float_exception_flags(&env->active_fpu.fp_status)
3283 & (float_flag_invalid | float_flag_overflow)) {
3284 wt2 = FP_TO_INT32_OVERFLOW;
3285 }
3286 update_fcr31(env, GETPC());
3287 return wt2;
3288 }
3289
3290 uint64_t helper_float_floor_l_d(CPUMIPSState *env, uint64_t fdt0)
3291 {
3292 uint64_t dt2;
3293
3294 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3295 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3296 restore_rounding_mode(env);
3297 if (get_float_exception_flags(&env->active_fpu.fp_status)
3298 & (float_flag_invalid | float_flag_overflow)) {
3299 dt2 = FP_TO_INT64_OVERFLOW;
3300 }
3301 update_fcr31(env, GETPC());
3302 return dt2;
3303 }
3304
3305 uint64_t helper_float_floor_l_s(CPUMIPSState *env, uint32_t fst0)
3306 {
3307 uint64_t dt2;
3308
3309 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3310 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3311 restore_rounding_mode(env);
3312 if (get_float_exception_flags(&env->active_fpu.fp_status)
3313 & (float_flag_invalid | float_flag_overflow)) {
3314 dt2 = FP_TO_INT64_OVERFLOW;
3315 }
3316 update_fcr31(env, GETPC());
3317 return dt2;
3318 }
3319
3320 uint32_t helper_float_floor_w_d(CPUMIPSState *env, uint64_t fdt0)
3321 {
3322 uint32_t wt2;
3323
3324 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3325 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3326 restore_rounding_mode(env);
3327 if (get_float_exception_flags(&env->active_fpu.fp_status)
3328 & (float_flag_invalid | float_flag_overflow)) {
3329 wt2 = FP_TO_INT32_OVERFLOW;
3330 }
3331 update_fcr31(env, GETPC());
3332 return wt2;
3333 }
3334
3335 uint32_t helper_float_floor_w_s(CPUMIPSState *env, uint32_t fst0)
3336 {
3337 uint32_t wt2;
3338
3339 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3340 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3341 restore_rounding_mode(env);
3342 if (get_float_exception_flags(&env->active_fpu.fp_status)
3343 & (float_flag_invalid | float_flag_overflow)) {
3344 wt2 = FP_TO_INT32_OVERFLOW;
3345 }
3346 update_fcr31(env, GETPC());
3347 return wt2;
3348 }
3349
3350 uint64_t helper_float_cvt_2008_l_d(CPUMIPSState *env, uint64_t fdt0)
3351 {
3352 uint64_t dt2;
3353
3354 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3355 if (get_float_exception_flags(&env->active_fpu.fp_status)
3356 & float_flag_invalid) {
3357 if (float64_is_any_nan(fdt0)) {
3358 dt2 = 0;
3359 }
3360 }
3361 update_fcr31(env, GETPC());
3362 return dt2;
3363 }
3364
3365 uint64_t helper_float_cvt_2008_l_s(CPUMIPSState *env, uint32_t fst0)
3366 {
3367 uint64_t dt2;
3368
3369 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3370 if (get_float_exception_flags(&env->active_fpu.fp_status)
3371 & float_flag_invalid) {
3372 if (float32_is_any_nan(fst0)) {
3373 dt2 = 0;
3374 }
3375 }
3376 update_fcr31(env, GETPC());
3377 return dt2;
3378 }
3379
3380 uint32_t helper_float_cvt_2008_w_d(CPUMIPSState *env, uint64_t fdt0)
3381 {
3382 uint32_t wt2;
3383
3384 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3385 if (get_float_exception_flags(&env->active_fpu.fp_status)
3386 & float_flag_invalid) {
3387 if (float64_is_any_nan(fdt0)) {
3388 wt2 = 0;
3389 }
3390 }
3391 update_fcr31(env, GETPC());
3392 return wt2;
3393 }
3394
3395 uint32_t helper_float_cvt_2008_w_s(CPUMIPSState *env, uint32_t fst0)
3396 {
3397 uint32_t wt2;
3398
3399 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3400 if (get_float_exception_flags(&env->active_fpu.fp_status)
3401 & float_flag_invalid) {
3402 if (float32_is_any_nan(fst0)) {
3403 wt2 = 0;
3404 }
3405 }
3406 update_fcr31(env, GETPC());
3407 return wt2;
3408 }
3409
3410 uint64_t helper_float_round_2008_l_d(CPUMIPSState *env, uint64_t fdt0)
3411 {
3412 uint64_t dt2;
3413
3414 set_float_rounding_mode(float_round_nearest_even,
3415 &env->active_fpu.fp_status);
3416 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3417 restore_rounding_mode(env);
3418 if (get_float_exception_flags(&env->active_fpu.fp_status)
3419 & float_flag_invalid) {
3420 if (float64_is_any_nan(fdt0)) {
3421 dt2 = 0;
3422 }
3423 }
3424 update_fcr31(env, GETPC());
3425 return dt2;
3426 }
3427
3428 uint64_t helper_float_round_2008_l_s(CPUMIPSState *env, uint32_t fst0)
3429 {
3430 uint64_t dt2;
3431
3432 set_float_rounding_mode(float_round_nearest_even,
3433 &env->active_fpu.fp_status);
3434 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3435 restore_rounding_mode(env);
3436 if (get_float_exception_flags(&env->active_fpu.fp_status)
3437 & float_flag_invalid) {
3438 if (float32_is_any_nan(fst0)) {
3439 dt2 = 0;
3440 }
3441 }
3442 update_fcr31(env, GETPC());
3443 return dt2;
3444 }
3445
3446 uint32_t helper_float_round_2008_w_d(CPUMIPSState *env, uint64_t fdt0)
3447 {
3448 uint32_t wt2;
3449
3450 set_float_rounding_mode(float_round_nearest_even,
3451 &env->active_fpu.fp_status);
3452 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3453 restore_rounding_mode(env);
3454 if (get_float_exception_flags(&env->active_fpu.fp_status)
3455 & float_flag_invalid) {
3456 if (float64_is_any_nan(fdt0)) {
3457 wt2 = 0;
3458 }
3459 }
3460 update_fcr31(env, GETPC());
3461 return wt2;
3462 }
3463
3464 uint32_t helper_float_round_2008_w_s(CPUMIPSState *env, uint32_t fst0)
3465 {
3466 uint32_t wt2;
3467
3468 set_float_rounding_mode(float_round_nearest_even,
3469 &env->active_fpu.fp_status);
3470 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3471 restore_rounding_mode(env);
3472 if (get_float_exception_flags(&env->active_fpu.fp_status)
3473 & float_flag_invalid) {
3474 if (float32_is_any_nan(fst0)) {
3475 wt2 = 0;
3476 }
3477 }
3478 update_fcr31(env, GETPC());
3479 return wt2;
3480 }
3481
3482 uint64_t helper_float_trunc_2008_l_d(CPUMIPSState *env, uint64_t fdt0)
3483 {
3484 uint64_t dt2;
3485
3486 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
3487 if (get_float_exception_flags(&env->active_fpu.fp_status)
3488 & float_flag_invalid) {
3489 if (float64_is_any_nan(fdt0)) {
3490 dt2 = 0;
3491 }
3492 }
3493 update_fcr31(env, GETPC());
3494 return dt2;
3495 }
3496
3497 uint64_t helper_float_trunc_2008_l_s(CPUMIPSState *env, uint32_t fst0)
3498 {
3499 uint64_t dt2;
3500
3501 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
3502 if (get_float_exception_flags(&env->active_fpu.fp_status)
3503 & float_flag_invalid) {
3504 if (float32_is_any_nan(fst0)) {
3505 dt2 = 0;
3506 }
3507 }
3508 update_fcr31(env, GETPC());
3509 return dt2;
3510 }
3511
3512 uint32_t helper_float_trunc_2008_w_d(CPUMIPSState *env, uint64_t fdt0)
3513 {
3514 uint32_t wt2;
3515
3516 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
3517 if (get_float_exception_flags(&env->active_fpu.fp_status)
3518 & float_flag_invalid) {
3519 if (float64_is_any_nan(fdt0)) {
3520 wt2 = 0;
3521 }
3522 }
3523 update_fcr31(env, GETPC());
3524 return wt2;
3525 }
3526
3527 uint32_t helper_float_trunc_2008_w_s(CPUMIPSState *env, uint32_t fst0)
3528 {
3529 uint32_t wt2;
3530
3531 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
3532 if (get_float_exception_flags(&env->active_fpu.fp_status)
3533 & float_flag_invalid) {
3534 if (float32_is_any_nan(fst0)) {
3535 wt2 = 0;
3536 }
3537 }
3538 update_fcr31(env, GETPC());
3539 return wt2;
3540 }
3541
3542 uint64_t helper_float_ceil_2008_l_d(CPUMIPSState *env, uint64_t fdt0)
3543 {
3544 uint64_t dt2;
3545
3546 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3547 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3548 restore_rounding_mode(env);
3549 if (get_float_exception_flags(&env->active_fpu.fp_status)
3550 & float_flag_invalid) {
3551 if (float64_is_any_nan(fdt0)) {
3552 dt2 = 0;
3553 }
3554 }
3555 update_fcr31(env, GETPC());
3556 return dt2;
3557 }
3558
3559 uint64_t helper_float_ceil_2008_l_s(CPUMIPSState *env, uint32_t fst0)
3560 {
3561 uint64_t dt2;
3562
3563 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3564 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3565 restore_rounding_mode(env);
3566 if (get_float_exception_flags(&env->active_fpu.fp_status)
3567 & float_flag_invalid) {
3568 if (float32_is_any_nan(fst0)) {
3569 dt2 = 0;
3570 }
3571 }
3572 update_fcr31(env, GETPC());
3573 return dt2;
3574 }
3575
3576 uint32_t helper_float_ceil_2008_w_d(CPUMIPSState *env, uint64_t fdt0)
3577 {
3578 uint32_t wt2;
3579
3580 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3581 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3582 restore_rounding_mode(env);
3583 if (get_float_exception_flags(&env->active_fpu.fp_status)
3584 & float_flag_invalid) {
3585 if (float64_is_any_nan(fdt0)) {
3586 wt2 = 0;
3587 }
3588 }
3589 update_fcr31(env, GETPC());
3590 return wt2;
3591 }
3592
3593 uint32_t helper_float_ceil_2008_w_s(CPUMIPSState *env, uint32_t fst0)
3594 {
3595 uint32_t wt2;
3596
3597 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
3598 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3599 restore_rounding_mode(env);
3600 if (get_float_exception_flags(&env->active_fpu.fp_status)
3601 & float_flag_invalid) {
3602 if (float32_is_any_nan(fst0)) {
3603 wt2 = 0;
3604 }
3605 }
3606 update_fcr31(env, GETPC());
3607 return wt2;
3608 }
3609
3610 uint64_t helper_float_floor_2008_l_d(CPUMIPSState *env, uint64_t fdt0)
3611 {
3612 uint64_t dt2;
3613
3614 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3615 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
3616 restore_rounding_mode(env);
3617 if (get_float_exception_flags(&env->active_fpu.fp_status)
3618 & float_flag_invalid) {
3619 if (float64_is_any_nan(fdt0)) {
3620 dt2 = 0;
3621 }
3622 }
3623 update_fcr31(env, GETPC());
3624 return dt2;
3625 }
3626
3627 uint64_t helper_float_floor_2008_l_s(CPUMIPSState *env, uint32_t fst0)
3628 {
3629 uint64_t dt2;
3630
3631 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3632 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
3633 restore_rounding_mode(env);
3634 if (get_float_exception_flags(&env->active_fpu.fp_status)
3635 & float_flag_invalid) {
3636 if (float32_is_any_nan(fst0)) {
3637 dt2 = 0;
3638 }
3639 }
3640 update_fcr31(env, GETPC());
3641 return dt2;
3642 }
3643
3644 uint32_t helper_float_floor_2008_w_d(CPUMIPSState *env, uint64_t fdt0)
3645 {
3646 uint32_t wt2;
3647
3648 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3649 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
3650 restore_rounding_mode(env);
3651 if (get_float_exception_flags(&env->active_fpu.fp_status)
3652 & float_flag_invalid) {
3653 if (float64_is_any_nan(fdt0)) {
3654 wt2 = 0;
3655 }
3656 }
3657 update_fcr31(env, GETPC());
3658 return wt2;
3659 }
3660
3661 uint32_t helper_float_floor_2008_w_s(CPUMIPSState *env, uint32_t fst0)
3662 {
3663 uint32_t wt2;
3664
3665 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
3666 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
3667 restore_rounding_mode(env);
3668 if (get_float_exception_flags(&env->active_fpu.fp_status)
3669 & float_flag_invalid) {
3670 if (float32_is_any_nan(fst0)) {
3671 wt2 = 0;
3672 }
3673 }
3674 update_fcr31(env, GETPC());
3675 return wt2;
3676 }
3677
3678 /* unary operations, not modifying fp status */
3679 #define FLOAT_UNOP(name) \
3680 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
3681 { \
3682 return float64_ ## name(fdt0); \
3683 } \
3684 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
3685 { \
3686 return float32_ ## name(fst0); \
3687 } \
3688 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
3689 { \
3690 uint32_t wt0; \
3691 uint32_t wth0; \
3692 \
3693 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
3694 wth0 = float32_ ## name(fdt0 >> 32); \
3695 return ((uint64_t)wth0 << 32) | wt0; \
3696 }
3697 FLOAT_UNOP(abs)
3698 FLOAT_UNOP(chs)
3699 #undef FLOAT_UNOP
3700
3701 /* MIPS specific unary operations */
3702 uint64_t helper_float_recip_d(CPUMIPSState *env, uint64_t fdt0)
3703 {
3704 uint64_t fdt2;
3705
3706 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
3707 update_fcr31(env, GETPC());
3708 return fdt2;
3709 }
3710
3711 uint32_t helper_float_recip_s(CPUMIPSState *env, uint32_t fst0)
3712 {
3713 uint32_t fst2;
3714
3715 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
3716 update_fcr31(env, GETPC());
3717 return fst2;
3718 }
3719
3720 uint64_t helper_float_rsqrt_d(CPUMIPSState *env, uint64_t fdt0)
3721 {
3722 uint64_t fdt2;
3723
3724 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
3725 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
3726 update_fcr31(env, GETPC());
3727 return fdt2;
3728 }
3729
3730 uint32_t helper_float_rsqrt_s(CPUMIPSState *env, uint32_t fst0)
3731 {
3732 uint32_t fst2;
3733
3734 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
3735 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3736 update_fcr31(env, GETPC());
3737 return fst2;
3738 }
3739
3740 uint64_t helper_float_recip1_d(CPUMIPSState *env, uint64_t fdt0)
3741 {
3742 uint64_t fdt2;
3743
3744 fdt2 = float64_div(float64_one, fdt0, &env->active_fpu.fp_status);
3745 update_fcr31(env, GETPC());
3746 return fdt2;
3747 }
3748
3749 uint32_t helper_float_recip1_s(CPUMIPSState *env, uint32_t fst0)
3750 {
3751 uint32_t fst2;
3752
3753 fst2 = float32_div(float32_one, fst0, &env->active_fpu.fp_status);
3754 update_fcr31(env, GETPC());
3755 return fst2;
3756 }
3757
3758 uint64_t helper_float_recip1_ps(CPUMIPSState *env, uint64_t fdt0)
3759 {
3760 uint32_t fst2;
3761 uint32_t fsth2;
3762
3763 fst2 = float32_div(float32_one, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3764 fsth2 = float32_div(float32_one, fdt0 >> 32, &env->active_fpu.fp_status);
3765 update_fcr31(env, GETPC());
3766 return ((uint64_t)fsth2 << 32) | fst2;
3767 }
3768
3769 uint64_t helper_float_rsqrt1_d(CPUMIPSState *env, uint64_t fdt0)
3770 {
3771 uint64_t fdt2;
3772
3773 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
3774 fdt2 = float64_div(float64_one, fdt2, &env->active_fpu.fp_status);
3775 update_fcr31(env, GETPC());
3776 return fdt2;
3777 }
3778
3779 uint32_t helper_float_rsqrt1_s(CPUMIPSState *env, uint32_t fst0)
3780 {
3781 uint32_t fst2;
3782
3783 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
3784 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3785 update_fcr31(env, GETPC());
3786 return fst2;
3787 }
3788
3789 uint64_t helper_float_rsqrt1_ps(CPUMIPSState *env, uint64_t fdt0)
3790 {
3791 uint32_t fst2;
3792 uint32_t fsth2;
3793
3794 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
3795 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
3796 fst2 = float32_div(float32_one, fst2, &env->active_fpu.fp_status);
3797 fsth2 = float32_div(float32_one, fsth2, &env->active_fpu.fp_status);
3798 update_fcr31(env, GETPC());
3799 return ((uint64_t)fsth2 << 32) | fst2;
3800 }
3801
3802 #define FLOAT_RINT(name, bits) \
3803 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3804 uint ## bits ## _t fs) \
3805 { \
3806 uint ## bits ## _t fdret; \
3807 \
3808 fdret = float ## bits ## _round_to_int(fs, &env->active_fpu.fp_status); \
3809 update_fcr31(env, GETPC()); \
3810 return fdret; \
3811 }
3812
3813 FLOAT_RINT(rint_s, 32)
3814 FLOAT_RINT(rint_d, 64)
3815 #undef FLOAT_RINT
3816
3817 #define FLOAT_CLASS_SIGNALING_NAN 0x001
3818 #define FLOAT_CLASS_QUIET_NAN 0x002
3819 #define FLOAT_CLASS_NEGATIVE_INFINITY 0x004
3820 #define FLOAT_CLASS_NEGATIVE_NORMAL 0x008
3821 #define FLOAT_CLASS_NEGATIVE_SUBNORMAL 0x010
3822 #define FLOAT_CLASS_NEGATIVE_ZERO 0x020
3823 #define FLOAT_CLASS_POSITIVE_INFINITY 0x040
3824 #define FLOAT_CLASS_POSITIVE_NORMAL 0x080
3825 #define FLOAT_CLASS_POSITIVE_SUBNORMAL 0x100
3826 #define FLOAT_CLASS_POSITIVE_ZERO 0x200
3827
3828 #define FLOAT_CLASS(name, bits) \
3829 uint ## bits ## _t float_ ## name (uint ## bits ## _t arg, \
3830 float_status *status) \
3831 { \
3832 if (float ## bits ## _is_signaling_nan(arg, status)) { \
3833 return FLOAT_CLASS_SIGNALING_NAN; \
3834 } else if (float ## bits ## _is_quiet_nan(arg, status)) { \
3835 return FLOAT_CLASS_QUIET_NAN; \
3836 } else if (float ## bits ## _is_neg(arg)) { \
3837 if (float ## bits ## _is_infinity(arg)) { \
3838 return FLOAT_CLASS_NEGATIVE_INFINITY; \
3839 } else if (float ## bits ## _is_zero(arg)) { \
3840 return FLOAT_CLASS_NEGATIVE_ZERO; \
3841 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3842 return FLOAT_CLASS_NEGATIVE_SUBNORMAL; \
3843 } else { \
3844 return FLOAT_CLASS_NEGATIVE_NORMAL; \
3845 } \
3846 } else { \
3847 if (float ## bits ## _is_infinity(arg)) { \
3848 return FLOAT_CLASS_POSITIVE_INFINITY; \
3849 } else if (float ## bits ## _is_zero(arg)) { \
3850 return FLOAT_CLASS_POSITIVE_ZERO; \
3851 } else if (float ## bits ## _is_zero_or_denormal(arg)) { \
3852 return FLOAT_CLASS_POSITIVE_SUBNORMAL; \
3853 } else { \
3854 return FLOAT_CLASS_POSITIVE_NORMAL; \
3855 } \
3856 } \
3857 } \
3858 \
3859 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
3860 uint ## bits ## _t arg) \
3861 { \
3862 return float_ ## name(arg, &env->active_fpu.fp_status); \
3863 }
3864
3865 FLOAT_CLASS(class_s, 32)
3866 FLOAT_CLASS(class_d, 64)
3867 #undef FLOAT_CLASS
3868
3869 /* binary operations */
3870 #define FLOAT_BINOP(name) \
3871 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
3872 uint64_t fdt0, uint64_t fdt1) \
3873 { \
3874 uint64_t dt2; \
3875 \
3876 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
3877 update_fcr31(env, GETPC()); \
3878 return dt2; \
3879 } \
3880 \
3881 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
3882 uint32_t fst0, uint32_t fst1) \
3883 { \
3884 uint32_t wt2; \
3885 \
3886 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3887 update_fcr31(env, GETPC()); \
3888 return wt2; \
3889 } \
3890 \
3891 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
3892 uint64_t fdt0, \
3893 uint64_t fdt1) \
3894 { \
3895 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
3896 uint32_t fsth0 = fdt0 >> 32; \
3897 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
3898 uint32_t fsth1 = fdt1 >> 32; \
3899 uint32_t wt2; \
3900 uint32_t wth2; \
3901 \
3902 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
3903 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
3904 update_fcr31(env, GETPC()); \
3905 return ((uint64_t)wth2 << 32) | wt2; \
3906 }
3907
3908 FLOAT_BINOP(add)
3909 FLOAT_BINOP(sub)
3910 FLOAT_BINOP(mul)
3911 FLOAT_BINOP(div)
3912 #undef FLOAT_BINOP
3913
3914 /* MIPS specific binary operations */
3915 uint64_t helper_float_recip2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3916 {
3917 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3918 fdt2 = float64_chs(float64_sub(fdt2, float64_one, &env->active_fpu.fp_status));
3919 update_fcr31(env, GETPC());
3920 return fdt2;
3921 }
3922
3923 uint32_t helper_float_recip2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3924 {
3925 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3926 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3927 update_fcr31(env, GETPC());
3928 return fst2;
3929 }
3930
3931 uint64_t helper_float_recip2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3932 {
3933 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3934 uint32_t fsth0 = fdt0 >> 32;
3935 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3936 uint32_t fsth2 = fdt2 >> 32;
3937
3938 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3939 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3940 fst2 = float32_chs(float32_sub(fst2, float32_one, &env->active_fpu.fp_status));
3941 fsth2 = float32_chs(float32_sub(fsth2, float32_one, &env->active_fpu.fp_status));
3942 update_fcr31(env, GETPC());
3943 return ((uint64_t)fsth2 << 32) | fst2;
3944 }
3945
3946 uint64_t helper_float_rsqrt2_d(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3947 {
3948 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
3949 fdt2 = float64_sub(fdt2, float64_one, &env->active_fpu.fp_status);
3950 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
3951 update_fcr31(env, GETPC());
3952 return fdt2;
3953 }
3954
3955 uint32_t helper_float_rsqrt2_s(CPUMIPSState *env, uint32_t fst0, uint32_t fst2)
3956 {
3957 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3958 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3959 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3960 update_fcr31(env, GETPC());
3961 return fst2;
3962 }
3963
3964 uint64_t helper_float_rsqrt2_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt2)
3965 {
3966 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3967 uint32_t fsth0 = fdt0 >> 32;
3968 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
3969 uint32_t fsth2 = fdt2 >> 32;
3970
3971 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
3972 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
3973 fst2 = float32_sub(fst2, float32_one, &env->active_fpu.fp_status);
3974 fsth2 = float32_sub(fsth2, float32_one, &env->active_fpu.fp_status);
3975 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
3976 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
3977 update_fcr31(env, GETPC());
3978 return ((uint64_t)fsth2 << 32) | fst2;
3979 }
3980
3981 uint64_t helper_float_addr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3982 {
3983 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3984 uint32_t fsth0 = fdt0 >> 32;
3985 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
3986 uint32_t fsth1 = fdt1 >> 32;
3987 uint32_t fst2;
3988 uint32_t fsth2;
3989
3990 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
3991 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
3992 update_fcr31(env, GETPC());
3993 return ((uint64_t)fsth2 << 32) | fst2;
3994 }
3995
3996 uint64_t helper_float_mulr_ps(CPUMIPSState *env, uint64_t fdt0, uint64_t fdt1)
3997 {
3998 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
3999 uint32_t fsth0 = fdt0 >> 32;
4000 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
4001 uint32_t fsth1 = fdt1 >> 32;
4002 uint32_t fst2;
4003 uint32_t fsth2;
4004
4005 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
4006 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
4007 update_fcr31(env, GETPC());
4008 return ((uint64_t)fsth2 << 32) | fst2;
4009 }
4010
4011 #define FLOAT_MINMAX(name, bits, minmaxfunc) \
4012 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
4013 uint ## bits ## _t fs, \
4014 uint ## bits ## _t ft) \
4015 { \
4016 uint ## bits ## _t fdret; \
4017 \
4018 fdret = float ## bits ## _ ## minmaxfunc(fs, ft, \
4019 &env->active_fpu.fp_status); \
4020 update_fcr31(env, GETPC()); \
4021 return fdret; \
4022 }
4023
4024 FLOAT_MINMAX(max_s, 32, maxnum)
4025 FLOAT_MINMAX(max_d, 64, maxnum)
4026 FLOAT_MINMAX(maxa_s, 32, maxnummag)
4027 FLOAT_MINMAX(maxa_d, 64, maxnummag)
4028
4029 FLOAT_MINMAX(min_s, 32, minnum)
4030 FLOAT_MINMAX(min_d, 64, minnum)
4031 FLOAT_MINMAX(mina_s, 32, minnummag)
4032 FLOAT_MINMAX(mina_d, 64, minnummag)
4033 #undef FLOAT_MINMAX
4034
4035 /* ternary operations */
4036 #define UNFUSED_FMA(prefix, a, b, c, flags) \
4037 { \
4038 a = prefix##_mul(a, b, &env->active_fpu.fp_status); \
4039 if ((flags) & float_muladd_negate_c) { \
4040 a = prefix##_sub(a, c, &env->active_fpu.fp_status); \
4041 } else { \
4042 a = prefix##_add(a, c, &env->active_fpu.fp_status); \
4043 } \
4044 if ((flags) & float_muladd_negate_result) { \
4045 a = prefix##_chs(a); \
4046 } \
4047 }
4048
4049 /* FMA based operations */
4050 #define FLOAT_FMA(name, type) \
4051 uint64_t helper_float_ ## name ## _d(CPUMIPSState *env, \
4052 uint64_t fdt0, uint64_t fdt1, \
4053 uint64_t fdt2) \
4054 { \
4055 UNFUSED_FMA(float64, fdt0, fdt1, fdt2, type); \
4056 update_fcr31(env, GETPC()); \
4057 return fdt0; \
4058 } \
4059 \
4060 uint32_t helper_float_ ## name ## _s(CPUMIPSState *env, \
4061 uint32_t fst0, uint32_t fst1, \
4062 uint32_t fst2) \
4063 { \
4064 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
4065 update_fcr31(env, GETPC()); \
4066 return fst0; \
4067 } \
4068 \
4069 uint64_t helper_float_ ## name ## _ps(CPUMIPSState *env, \
4070 uint64_t fdt0, uint64_t fdt1, \
4071 uint64_t fdt2) \
4072 { \
4073 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
4074 uint32_t fsth0 = fdt0 >> 32; \
4075 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
4076 uint32_t fsth1 = fdt1 >> 32; \
4077 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
4078 uint32_t fsth2 = fdt2 >> 32; \
4079 \
4080 UNFUSED_FMA(float32, fst0, fst1, fst2, type); \
4081 UNFUSED_FMA(float32, fsth0, fsth1, fsth2, type); \
4082 update_fcr31(env, GETPC()); \
4083 return ((uint64_t)fsth0 << 32) | fst0; \
4084 }
4085 FLOAT_FMA(madd, 0)
4086 FLOAT_FMA(msub, float_muladd_negate_c)
4087 FLOAT_FMA(nmadd, float_muladd_negate_result)
4088 FLOAT_FMA(nmsub, float_muladd_negate_result | float_muladd_negate_c)
4089 #undef FLOAT_FMA
4090
4091 #define FLOAT_FMADDSUB(name, bits, muladd_arg) \
4092 uint ## bits ## _t helper_float_ ## name (CPUMIPSState *env, \
4093 uint ## bits ## _t fs, \
4094 uint ## bits ## _t ft, \
4095 uint ## bits ## _t fd) \
4096 { \
4097 uint ## bits ## _t fdret; \
4098 \
4099 fdret = float ## bits ## _muladd(fs, ft, fd, muladd_arg, \
4100 &env->active_fpu.fp_status); \
4101 update_fcr31(env, GETPC()); \
4102 return fdret; \
4103 }
4104
4105 FLOAT_FMADDSUB(maddf_s, 32, 0)
4106 FLOAT_FMADDSUB(maddf_d, 64, 0)
4107 FLOAT_FMADDSUB(msubf_s, 32, float_muladd_negate_product)
4108 FLOAT_FMADDSUB(msubf_d, 64, float_muladd_negate_product)
4109 #undef FLOAT_FMADDSUB
4110
4111 /* compare operations */
4112 #define FOP_COND_D(op, cond) \
4113 void helper_cmp_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
4114 uint64_t fdt1, int cc) \
4115 { \
4116 int c; \
4117 c = cond; \
4118 update_fcr31(env, GETPC()); \
4119 if (c) \
4120 SET_FP_COND(cc, env->active_fpu); \
4121 else \
4122 CLEAR_FP_COND(cc, env->active_fpu); \
4123 } \
4124 void helper_cmpabs_d_ ## op(CPUMIPSState *env, uint64_t fdt0, \
4125 uint64_t fdt1, int cc) \
4126 { \
4127 int c; \
4128 fdt0 = float64_abs(fdt0); \
4129 fdt1 = float64_abs(fdt1); \
4130 c = cond; \
4131 update_fcr31(env, GETPC()); \
4132 if (c) \
4133 SET_FP_COND(cc, env->active_fpu); \
4134 else \
4135 CLEAR_FP_COND(cc, env->active_fpu); \
4136 }
4137
4138 /* NOTE: the comma operator will make "cond" to eval to false,
4139 * but float64_unordered_quiet() is still called. */
4140 FOP_COND_D(f, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
4141 FOP_COND_D(un, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status))
4142 FOP_COND_D(eq, float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
4143 FOP_COND_D(ueq, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
4144 FOP_COND_D(olt, float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
4145 FOP_COND_D(ult, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
4146 FOP_COND_D(ole, float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
4147 FOP_COND_D(ule, float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status))
4148 /* NOTE: the comma operator will make "cond" to eval to false,
4149 * but float64_unordered() is still called. */
4150 FOP_COND_D(sf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
4151 FOP_COND_D(ngle,float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status))
4152 FOP_COND_D(seq, float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
4153 FOP_COND_D(ngl, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
4154 FOP_COND_D(lt, float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
4155 FOP_COND_D(nge, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
4156 FOP_COND_D(le, float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
4157 FOP_COND_D(ngt, float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
4158
4159 #define FOP_COND_S(op, cond) \
4160 void helper_cmp_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
4161 uint32_t fst1, int cc) \
4162 { \
4163 int c; \
4164 c = cond; \
4165 update_fcr31(env, GETPC()); \
4166 if (c) \
4167 SET_FP_COND(cc, env->active_fpu); \
4168 else \
4169 CLEAR_FP_COND(cc, env->active_fpu); \
4170 } \
4171 void helper_cmpabs_s_ ## op(CPUMIPSState *env, uint32_t fst0, \
4172 uint32_t fst1, int cc) \
4173 { \
4174 int c; \
4175 fst0 = float32_abs(fst0); \
4176 fst1 = float32_abs(fst1); \
4177 c = cond; \
4178 update_fcr31(env, GETPC()); \
4179 if (c) \
4180 SET_FP_COND(cc, env->active_fpu); \
4181 else \
4182 CLEAR_FP_COND(cc, env->active_fpu); \
4183 }
4184
4185 /* NOTE: the comma operator will make "cond" to eval to false,
4186 * but float32_unordered_quiet() is still called. */
4187 FOP_COND_S(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
4188 FOP_COND_S(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status))
4189 FOP_COND_S(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
4190 FOP_COND_S(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status))
4191 FOP_COND_S(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
4192 FOP_COND_S(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status))
4193 FOP_COND_S(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
4194 FOP_COND_S(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status))
4195 /* NOTE: the comma operator will make "cond" to eval to false,
4196 * but float32_unordered() is still called. */
4197 FOP_COND_S(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
4198 FOP_COND_S(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status))
4199 FOP_COND_S(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status))
4200 FOP_COND_S(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
4201 FOP_COND_S(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status))
4202 FOP_COND_S(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
4203 FOP_COND_S(le, float32_le(fst0, fst1, &env->active_fpu.fp_status))
4204 FOP_COND_S(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
4205
4206 #define FOP_COND_PS(op, condl, condh) \
4207 void helper_cmp_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
4208 uint64_t fdt1, int cc) \
4209 { \
4210 uint32_t fst0, fsth0, fst1, fsth1; \
4211 int ch, cl; \
4212 fst0 = fdt0 & 0XFFFFFFFF; \
4213 fsth0 = fdt0 >> 32; \
4214 fst1 = fdt1 & 0XFFFFFFFF; \
4215 fsth1 = fdt1 >> 32; \
4216 cl = condl; \
4217 ch = condh; \
4218 update_fcr31(env, GETPC()); \
4219 if (cl) \
4220 SET_FP_COND(cc, env->active_fpu); \
4221 else \
4222 CLEAR_FP_COND(cc, env->active_fpu); \
4223 if (ch) \
4224 SET_FP_COND(cc + 1, env->active_fpu); \
4225 else \
4226 CLEAR_FP_COND(cc + 1, env->active_fpu); \
4227 } \
4228 void helper_cmpabs_ps_ ## op(CPUMIPSState *env, uint64_t fdt0, \
4229 uint64_t fdt1, int cc) \
4230 { \
4231 uint32_t fst0, fsth0, fst1, fsth1; \
4232 int ch, cl; \
4233 fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
4234 fsth0 = float32_abs(fdt0 >> 32); \
4235 fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
4236 fsth1 = float32_abs(fdt1 >> 32); \
4237 cl = condl; \
4238 ch = condh; \
4239 update_fcr31(env, GETPC()); \
4240 if (cl) \
4241 SET_FP_COND(cc, env->active_fpu); \
4242 else \
4243 CLEAR_FP_COND(cc, env->active_fpu); \
4244 if (ch) \
4245 SET_FP_COND(cc + 1, env->active_fpu); \
4246 else \
4247 CLEAR_FP_COND(cc + 1, env->active_fpu); \
4248 }
4249
4250 /* NOTE: the comma operator will make "cond" to eval to false,
4251 * but float32_unordered_quiet() is still called. */
4252 FOP_COND_PS(f, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0),
4253 (float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status), 0))
4254 FOP_COND_PS(un, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status),
4255 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status))
4256 FOP_COND_PS(eq, float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
4257 float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
4258 FOP_COND_PS(ueq, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status),
4259 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
4260 FOP_COND_PS(olt, float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
4261 float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
4262 FOP_COND_PS(ult, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status),
4263 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
4264 FOP_COND_PS(ole, float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
4265 float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
4266 FOP_COND_PS(ule, float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status) || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status),
4267 float32_unordered_quiet(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le_quiet(fsth0, fsth1, &env->active_fpu.fp_status))
4268 /* NOTE: the comma operator will make "cond" to eval to false,
4269 * but float32_unordered() is still called. */
4270 FOP_COND_PS(sf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0),
4271 (float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status), 0))
4272 FOP_COND_PS(ngle,float32_unordered(fst1, fst0, &env->active_fpu.fp_status),
4273 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status))
4274 FOP_COND_PS(seq, float32_eq(fst0, fst1, &env->active_fpu.fp_status),
4275 float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
4276 FOP_COND_PS(ngl, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
4277 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
4278 FOP_COND_PS(lt, float32_lt(fst0, fst1, &env->active_fpu.fp_status),
4279 float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
4280 FOP_COND_PS(nge, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
4281 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
4282 FOP_COND_PS(le, float32_le(fst0, fst1, &env->active_fpu.fp_status),
4283 float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
4284 FOP_COND_PS(ngt, float32_unordered(fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
4285 float32_unordered(fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
4286
4287 /* R6 compare operations */
4288 #define FOP_CONDN_D(op, cond) \
4289 uint64_t helper_r6_cmp_d_ ## op(CPUMIPSState * env, uint64_t fdt0, \
4290 uint64_t fdt1) \
4291 { \
4292 uint64_t c; \
4293 c = cond; \
4294 update_fcr31(env, GETPC()); \
4295 if (c) { \
4296 return -1; \
4297 } else { \
4298 return 0; \
4299 } \
4300 }
4301
4302 /* NOTE: the comma operator will make "cond" to eval to false,
4303 * but float64_unordered_quiet() is still called. */
4304 FOP_CONDN_D(af, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status), 0))
4305 FOP_CONDN_D(un, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)))
4306 FOP_CONDN_D(eq, (float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4307 FOP_CONDN_D(ueq, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4308 || float64_eq_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4309 FOP_CONDN_D(lt, (float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4310 FOP_CONDN_D(ult, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4311 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4312 FOP_CONDN_D(le, (float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4313 FOP_CONDN_D(ule, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4314 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4315 /* NOTE: the comma operator will make "cond" to eval to false,
4316 * but float64_unordered() is still called. */
4317 FOP_CONDN_D(saf, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status), 0))
4318 FOP_CONDN_D(sun, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)))
4319 FOP_CONDN_D(seq, (float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
4320 FOP_CONDN_D(sueq, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
4321 || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status)))
4322 FOP_CONDN_D(slt, (float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
4323 FOP_CONDN_D(sult, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
4324 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
4325 FOP_CONDN_D(sle, (float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
4326 FOP_CONDN_D(sule, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
4327 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
4328 FOP_CONDN_D(or, (float64_le_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4329 || float64_le_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4330 FOP_CONDN_D(une, (float64_unordered_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4331 || float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4332 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4333 FOP_CONDN_D(ne, (float64_lt_quiet(fdt1, fdt0, &env->active_fpu.fp_status)
4334 || float64_lt_quiet(fdt0, fdt1, &env->active_fpu.fp_status)))
4335 FOP_CONDN_D(sor, (float64_le(fdt1, fdt0, &env->active_fpu.fp_status)
4336 || float64_le(fdt0, fdt1, &env->active_fpu.fp_status)))
4337 FOP_CONDN_D(sune, (float64_unordered(fdt1, fdt0, &env->active_fpu.fp_status)
4338 || float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
4339 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
4340 FOP_CONDN_D(sne, (float64_lt(fdt1, fdt0, &env->active_fpu.fp_status)
4341 || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status)))
4342
4343 #define FOP_CONDN_S(op, cond) \
4344 uint32_t helper_r6_cmp_s_ ## op(CPUMIPSState * env, uint32_t fst0, \
4345 uint32_t fst1) \
4346 { \
4347 uint64_t c; \
4348 c = cond; \
4349 update_fcr31(env, GETPC()); \
4350 if (c) { \
4351 return -1; \
4352 } else { \
4353 return 0; \
4354 } \
4355 }
4356
4357 /* NOTE: the comma operator will make "cond" to eval to false,
4358 * but float32_unordered_quiet() is still called. */
4359 FOP_CONDN_S(af, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status), 0))
4360 FOP_CONDN_S(un, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)))
4361 FOP_CONDN_S(eq, (float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4362 FOP_CONDN_S(ueq, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
4363 || float32_eq_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4364 FOP_CONDN_S(lt, (float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4365 FOP_CONDN_S(ult, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
4366 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4367 FOP_CONDN_S(le, (float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4368 FOP_CONDN_S(ule, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
4369 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4370 /* NOTE: the comma operator will make "cond" to eval to false,
4371 * but float32_unordered() is still called. */
4372 FOP_CONDN_S(saf, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status), 0))
4373 FOP_CONDN_S(sun, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)))
4374 FOP_CONDN_S(seq, (float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
4375 FOP_CONDN_S(sueq, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
4376 || float32_eq(fst0, fst1, &env->active_fpu.fp_status)))
4377 FOP_CONDN_S(slt, (float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
4378 FOP_CONDN_S(sult, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
4379 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
4380 FOP_CONDN_S(sle, (float32_le(fst0, fst1, &env->active_fpu.fp_status)))
4381 FOP_CONDN_S(sule, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
4382 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
4383 FOP_CONDN_S(or, (float32_le_quiet(fst1, fst0, &env->active_fpu.fp_status)
4384 || float32_le_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4385 FOP_CONDN_S(une, (float32_unordered_quiet(fst1, fst0, &env->active_fpu.fp_status)
4386 || float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
4387 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4388 FOP_CONDN_S(ne, (float32_lt_quiet(fst1, fst0, &env->active_fpu.fp_status)
4389 || float32_lt_quiet(fst0, fst1, &env->active_fpu.fp_status)))
4390 FOP_CONDN_S(sor, (float32_le(fst1, fst0, &env->active_fpu.fp_status)
4391 || float32_le(fst0, fst1, &env->active_fpu.fp_status)))
4392 FOP_CONDN_S(sune, (float32_unordered(fst1, fst0, &env->active_fpu.fp_status)
4393 || float32_lt(fst1, fst0, &env->active_fpu.fp_status)
4394 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
4395 FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status)
4396 || float32_lt(fst0, fst1, &env->active_fpu.fp_status)))
4397
4398 /* MSA */
4399 /* Data format min and max values */
4400 #define DF_BITS(df) (1 << ((df) + 3))
4401
4402 /* Element-by-element access macros */
4403 #define DF_ELEMENTS(df) (MSA_WRLEN / DF_BITS(df))
4404
4405 #if !defined(CONFIG_USER_ONLY)
4406 #define MEMOP_IDX(DF) \
4407 TCGMemOpIdx oi = make_memop_idx(MO_TE | DF | MO_UNALN, \
4408 cpu_mmu_index(env, false));
4409 #else
4410 #define MEMOP_IDX(DF)
4411 #endif
4412
4413 #define MSA_LD_DF(DF, TYPE, LD_INSN, ...) \
4414 void helper_msa_ld_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
4415 target_ulong addr) \
4416 { \
4417 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
4418 wr_t wx; \
4419 int i; \
4420 MEMOP_IDX(DF) \
4421 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
4422 wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__); \
4423 } \
4424 memcpy(pwd, &wx, sizeof(wr_t)); \
4425 }
4426
4427 #if !defined(CONFIG_USER_ONLY)
4428 MSA_LD_DF(DF_BYTE, b, helper_ret_ldub_mmu, oi, GETPC())
4429 MSA_LD_DF(DF_HALF, h, helper_ret_lduw_mmu, oi, GETPC())
4430 MSA_LD_DF(DF_WORD, w, helper_ret_ldul_mmu, oi, GETPC())
4431 MSA_LD_DF(DF_DOUBLE, d, helper_ret_ldq_mmu, oi, GETPC())
4432 #else
4433 MSA_LD_DF(DF_BYTE, b, cpu_ldub_data)
4434 MSA_LD_DF(DF_HALF, h, cpu_lduw_data)
4435 MSA_LD_DF(DF_WORD, w, cpu_ldl_data)
4436 MSA_LD_DF(DF_DOUBLE, d, cpu_ldq_data)
4437 #endif
4438
4439 #define MSA_PAGESPAN(x) \
4440 ((((x) & ~TARGET_PAGE_MASK) + MSA_WRLEN/8 - 1) >= TARGET_PAGE_SIZE)
4441
4442 static inline void ensure_writable_pages(CPUMIPSState *env,
4443 target_ulong addr,
4444 int mmu_idx,
4445 uintptr_t retaddr)
4446 {
4447 #if !defined(CONFIG_USER_ONLY)
4448 target_ulong page_addr;
4449 if (unlikely(MSA_PAGESPAN(addr))) {
4450 /* first page */
4451 probe_write(env, addr, 0, mmu_idx, retaddr);
4452 /* second page */
4453 page_addr = (addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
4454 probe_write(env, page_addr, 0, mmu_idx, retaddr);
4455 }
4456 #endif
4457 }
4458
4459 #define MSA_ST_DF(DF, TYPE, ST_INSN, ...) \
4460 void helper_msa_st_ ## TYPE(CPUMIPSState *env, uint32_t wd, \
4461 target_ulong addr) \
4462 { \
4463 wr_t *pwd = &(env->active_fpu.fpr[wd].wr); \
4464 int mmu_idx = cpu_mmu_index(env, false); \
4465 int i; \
4466 MEMOP_IDX(DF) \
4467 ensure_writable_pages(env, addr, mmu_idx, GETPC()); \
4468 for (i = 0; i < DF_ELEMENTS(DF); i++) { \
4469 ST_INSN(env, addr + (i << DF), pwd->TYPE[i], ##__VA_ARGS__); \
4470 } \
4471 }
4472
4473 #if !defined(CONFIG_USER_ONLY)
4474 MSA_ST_DF(DF_BYTE, b, helper_ret_stb_mmu, oi, GETPC())
4475 MSA_ST_DF(DF_HALF, h, helper_ret_stw_mmu, oi, GETPC())
4476 MSA_ST_DF(DF_WORD, w, helper_ret_stl_mmu, oi, GETPC())
4477 MSA_ST_DF(DF_DOUBLE, d, helper_ret_stq_mmu, oi, GETPC())
4478 #else
4479 MSA_ST_DF(DF_BYTE, b, cpu_stb_data)
4480 MSA_ST_DF(DF_HALF, h, cpu_stw_data)
4481 MSA_ST_DF(DF_WORD, w, cpu_stl_data)
4482 MSA_ST_DF(DF_DOUBLE, d, cpu_stq_data)
4483 #endif
4484
4485 void helper_cache(CPUMIPSState *env, target_ulong addr, uint32_t op)
4486 {
4487 #ifndef CONFIG_USER_ONLY
4488 target_ulong index = addr & 0x1fffffff;
4489 if (op == 9) {
4490 /* Index Store Tag */
4491 memory_region_dispatch_write(env->itc_tag, index, env->CP0_TagLo,
4492 8, MEMTXATTRS_UNSPECIFIED);
4493 } else if (op == 5) {
4494 /* Index Load Tag */
4495 memory_region_dispatch_read(env->itc_tag, index, &env->CP0_TagLo,
4496 8, MEMTXATTRS_UNSPECIFIED);
4497 }
4498 #endif
4499 }