pcie/aer: fix inject aer error command
[qemu.git] / target-sh4 / op_helper.c
blob163858f5605fe04f0a8f36ff543195cab60aef3d
1 /*
2 * SH4 emulation
4 * Copyright (c) 2005 Samuel Tardieu
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.
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.
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/>.
19 #include <assert.h>
20 #include <stdlib.h>
21 #include "cpu.h"
22 #include "dyngen-exec.h"
23 #include "helper.h"
25 static void cpu_restore_state_from_retaddr(void *retaddr)
27 TranslationBlock *tb;
28 unsigned long pc;
30 if (retaddr) {
31 pc = (unsigned long) retaddr;
32 tb = tb_find_pc(pc);
33 if (tb) {
34 /* the PC is inside the translated code. It means that we have
35 a virtual CPU fault */
36 cpu_restore_state(tb, env, pc);
41 #ifndef CONFIG_USER_ONLY
42 #include "softmmu_exec.h"
44 #define MMUSUFFIX _mmu
46 #define SHIFT 0
47 #include "softmmu_template.h"
49 #define SHIFT 1
50 #include "softmmu_template.h"
52 #define SHIFT 2
53 #include "softmmu_template.h"
55 #define SHIFT 3
56 #include "softmmu_template.h"
58 void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
60 CPUState *saved_env;
61 int ret;
63 /* XXX: hack to restore env in all cases, even if not called from
64 generated code */
65 saved_env = env;
66 env = cpu_single_env;
67 ret = cpu_sh4_handle_mmu_fault(env, addr, is_write, mmu_idx);
68 if (ret) {
69 /* now we have a real cpu fault */
70 cpu_restore_state_from_retaddr(retaddr);
71 cpu_loop_exit(env);
73 env = saved_env;
76 #endif
78 void helper_ldtlb(void)
80 #ifdef CONFIG_USER_ONLY
81 /* XXXXX */
82 cpu_abort(env, "Unhandled ldtlb");
83 #else
84 cpu_load_tlb(env);
85 #endif
88 static inline void raise_exception(int index, void *retaddr)
90 env->exception_index = index;
91 cpu_restore_state_from_retaddr(retaddr);
92 cpu_loop_exit(env);
95 void helper_raise_illegal_instruction(void)
97 raise_exception(0x180, GETPC());
100 void helper_raise_slot_illegal_instruction(void)
102 raise_exception(0x1a0, GETPC());
105 void helper_raise_fpu_disable(void)
107 raise_exception(0x800, GETPC());
110 void helper_raise_slot_fpu_disable(void)
112 raise_exception(0x820, GETPC());
115 void helper_debug(void)
117 env->exception_index = EXCP_DEBUG;
118 cpu_loop_exit(env);
121 void helper_sleep(uint32_t next_pc)
123 env->halted = 1;
124 env->in_sleep = 1;
125 env->exception_index = EXCP_HLT;
126 env->pc = next_pc;
127 cpu_loop_exit(env);
130 void helper_trapa(uint32_t tra)
132 env->tra = tra << 2;
133 raise_exception(0x160, GETPC());
136 void helper_movcal(uint32_t address, uint32_t value)
138 if (cpu_sh4_is_cached (env, address))
140 memory_content *r = malloc (sizeof(memory_content));
141 r->address = address;
142 r->value = value;
143 r->next = NULL;
145 *(env->movcal_backup_tail) = r;
146 env->movcal_backup_tail = &(r->next);
150 void helper_discard_movcal_backup(void)
152 memory_content *current = env->movcal_backup;
154 while(current)
156 memory_content *next = current->next;
157 free (current);
158 env->movcal_backup = current = next;
159 if (current == NULL)
160 env->movcal_backup_tail = &(env->movcal_backup);
164 void helper_ocbi(uint32_t address)
166 memory_content **current = &(env->movcal_backup);
167 while (*current)
169 uint32_t a = (*current)->address;
170 if ((a & ~0x1F) == (address & ~0x1F))
172 memory_content *next = (*current)->next;
173 stl(a, (*current)->value);
175 if (next == NULL)
177 env->movcal_backup_tail = current;
180 free (*current);
181 *current = next;
182 break;
187 uint32_t helper_addc(uint32_t arg0, uint32_t arg1)
189 uint32_t tmp0, tmp1;
191 tmp1 = arg0 + arg1;
192 tmp0 = arg1;
193 arg1 = tmp1 + (env->sr & 1);
194 if (tmp0 > tmp1)
195 env->sr |= SR_T;
196 else
197 env->sr &= ~SR_T;
198 if (tmp1 > arg1)
199 env->sr |= SR_T;
200 return arg1;
203 uint32_t helper_addv(uint32_t arg0, uint32_t arg1)
205 uint32_t dest, src, ans;
207 if ((int32_t) arg1 >= 0)
208 dest = 0;
209 else
210 dest = 1;
211 if ((int32_t) arg0 >= 0)
212 src = 0;
213 else
214 src = 1;
215 src += dest;
216 arg1 += arg0;
217 if ((int32_t) arg1 >= 0)
218 ans = 0;
219 else
220 ans = 1;
221 ans += dest;
222 if (src == 0 || src == 2) {
223 if (ans == 1)
224 env->sr |= SR_T;
225 else
226 env->sr &= ~SR_T;
227 } else
228 env->sr &= ~SR_T;
229 return arg1;
232 #define T (env->sr & SR_T)
233 #define Q (env->sr & SR_Q ? 1 : 0)
234 #define M (env->sr & SR_M ? 1 : 0)
235 #define SETT env->sr |= SR_T
236 #define CLRT env->sr &= ~SR_T
237 #define SETQ env->sr |= SR_Q
238 #define CLRQ env->sr &= ~SR_Q
239 #define SETM env->sr |= SR_M
240 #define CLRM env->sr &= ~SR_M
242 uint32_t helper_div1(uint32_t arg0, uint32_t arg1)
244 uint32_t tmp0, tmp2;
245 uint8_t old_q, tmp1 = 0xff;
247 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
248 old_q = Q;
249 if ((0x80000000 & arg1) != 0)
250 SETQ;
251 else
252 CLRQ;
253 tmp2 = arg0;
254 arg1 <<= 1;
255 arg1 |= T;
256 switch (old_q) {
257 case 0:
258 switch (M) {
259 case 0:
260 tmp0 = arg1;
261 arg1 -= tmp2;
262 tmp1 = arg1 > tmp0;
263 switch (Q) {
264 case 0:
265 if (tmp1)
266 SETQ;
267 else
268 CLRQ;
269 break;
270 case 1:
271 if (tmp1 == 0)
272 SETQ;
273 else
274 CLRQ;
275 break;
277 break;
278 case 1:
279 tmp0 = arg1;
280 arg1 += tmp2;
281 tmp1 = arg1 < tmp0;
282 switch (Q) {
283 case 0:
284 if (tmp1 == 0)
285 SETQ;
286 else
287 CLRQ;
288 break;
289 case 1:
290 if (tmp1)
291 SETQ;
292 else
293 CLRQ;
294 break;
296 break;
298 break;
299 case 1:
300 switch (M) {
301 case 0:
302 tmp0 = arg1;
303 arg1 += tmp2;
304 tmp1 = arg1 < tmp0;
305 switch (Q) {
306 case 0:
307 if (tmp1)
308 SETQ;
309 else
310 CLRQ;
311 break;
312 case 1:
313 if (tmp1 == 0)
314 SETQ;
315 else
316 CLRQ;
317 break;
319 break;
320 case 1:
321 tmp0 = arg1;
322 arg1 -= tmp2;
323 tmp1 = arg1 > tmp0;
324 switch (Q) {
325 case 0:
326 if (tmp1 == 0)
327 SETQ;
328 else
329 CLRQ;
330 break;
331 case 1:
332 if (tmp1)
333 SETQ;
334 else
335 CLRQ;
336 break;
338 break;
340 break;
342 if (Q == M)
343 SETT;
344 else
345 CLRT;
346 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
347 return arg1;
350 void helper_macl(uint32_t arg0, uint32_t arg1)
352 int64_t res;
354 res = ((uint64_t) env->mach << 32) | env->macl;
355 res += (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1;
356 env->mach = (res >> 32) & 0xffffffff;
357 env->macl = res & 0xffffffff;
358 if (env->sr & SR_S) {
359 if (res < 0)
360 env->mach |= 0xffff0000;
361 else
362 env->mach &= 0x00007fff;
366 void helper_macw(uint32_t arg0, uint32_t arg1)
368 int64_t res;
370 res = ((uint64_t) env->mach << 32) | env->macl;
371 res += (int64_t) (int16_t) arg0 *(int64_t) (int16_t) arg1;
372 env->mach = (res >> 32) & 0xffffffff;
373 env->macl = res & 0xffffffff;
374 if (env->sr & SR_S) {
375 if (res < -0x80000000) {
376 env->mach = 1;
377 env->macl = 0x80000000;
378 } else if (res > 0x000000007fffffff) {
379 env->mach = 1;
380 env->macl = 0x7fffffff;
385 uint32_t helper_subc(uint32_t arg0, uint32_t arg1)
387 uint32_t tmp0, tmp1;
389 tmp1 = arg1 - arg0;
390 tmp0 = arg1;
391 arg1 = tmp1 - (env->sr & SR_T);
392 if (tmp0 < tmp1)
393 env->sr |= SR_T;
394 else
395 env->sr &= ~SR_T;
396 if (tmp1 < arg1)
397 env->sr |= SR_T;
398 return arg1;
401 uint32_t helper_subv(uint32_t arg0, uint32_t arg1)
403 int32_t dest, src, ans;
405 if ((int32_t) arg1 >= 0)
406 dest = 0;
407 else
408 dest = 1;
409 if ((int32_t) arg0 >= 0)
410 src = 0;
411 else
412 src = 1;
413 src += dest;
414 arg1 -= arg0;
415 if ((int32_t) arg1 >= 0)
416 ans = 0;
417 else
418 ans = 1;
419 ans += dest;
420 if (src == 1) {
421 if (ans == 1)
422 env->sr |= SR_T;
423 else
424 env->sr &= ~SR_T;
425 } else
426 env->sr &= ~SR_T;
427 return arg1;
430 static inline void set_t(void)
432 env->sr |= SR_T;
435 static inline void clr_t(void)
437 env->sr &= ~SR_T;
440 void helper_ld_fpscr(uint32_t val)
442 env->fpscr = val & FPSCR_MASK;
443 if ((val & FPSCR_RM_MASK) == FPSCR_RM_ZERO) {
444 set_float_rounding_mode(float_round_to_zero, &env->fp_status);
445 } else {
446 set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
448 set_flush_to_zero((val & FPSCR_DN) != 0, &env->fp_status);
451 static void update_fpscr(void *retaddr)
453 int xcpt, cause, enable;
455 xcpt = get_float_exception_flags(&env->fp_status);
457 /* Clear the flag entries */
458 env->fpscr &= ~FPSCR_FLAG_MASK;
460 if (unlikely(xcpt)) {
461 if (xcpt & float_flag_invalid) {
462 env->fpscr |= FPSCR_FLAG_V;
464 if (xcpt & float_flag_divbyzero) {
465 env->fpscr |= FPSCR_FLAG_Z;
467 if (xcpt & float_flag_overflow) {
468 env->fpscr |= FPSCR_FLAG_O;
470 if (xcpt & float_flag_underflow) {
471 env->fpscr |= FPSCR_FLAG_U;
473 if (xcpt & float_flag_inexact) {
474 env->fpscr |= FPSCR_FLAG_I;
477 /* Accumulate in cause entries */
478 env->fpscr |= (env->fpscr & FPSCR_FLAG_MASK)
479 << (FPSCR_CAUSE_SHIFT - FPSCR_FLAG_SHIFT);
481 /* Generate an exception if enabled */
482 cause = (env->fpscr & FPSCR_CAUSE_MASK) >> FPSCR_CAUSE_SHIFT;
483 enable = (env->fpscr & FPSCR_ENABLE_MASK) >> FPSCR_ENABLE_SHIFT;
484 if (cause & enable) {
485 cpu_restore_state_from_retaddr(retaddr);
486 env->exception_index = 0x120;
487 cpu_loop_exit(env);
492 float32 helper_fabs_FT(float32 t0)
494 return float32_abs(t0);
497 float64 helper_fabs_DT(float64 t0)
499 return float64_abs(t0);
502 float32 helper_fadd_FT(float32 t0, float32 t1)
504 set_float_exception_flags(0, &env->fp_status);
505 t0 = float32_add(t0, t1, &env->fp_status);
506 update_fpscr(GETPC());
507 return t0;
510 float64 helper_fadd_DT(float64 t0, float64 t1)
512 set_float_exception_flags(0, &env->fp_status);
513 t0 = float64_add(t0, t1, &env->fp_status);
514 update_fpscr(GETPC());
515 return t0;
518 void helper_fcmp_eq_FT(float32 t0, float32 t1)
520 int relation;
522 set_float_exception_flags(0, &env->fp_status);
523 relation = float32_compare(t0, t1, &env->fp_status);
524 if (unlikely(relation == float_relation_unordered)) {
525 update_fpscr(GETPC());
526 } else if (relation == float_relation_equal) {
527 set_t();
528 } else {
529 clr_t();
533 void helper_fcmp_eq_DT(float64 t0, float64 t1)
535 int relation;
537 set_float_exception_flags(0, &env->fp_status);
538 relation = float64_compare(t0, t1, &env->fp_status);
539 if (unlikely(relation == float_relation_unordered)) {
540 update_fpscr(GETPC());
541 } else if (relation == float_relation_equal) {
542 set_t();
543 } else {
544 clr_t();
548 void helper_fcmp_gt_FT(float32 t0, float32 t1)
550 int relation;
552 set_float_exception_flags(0, &env->fp_status);
553 relation = float32_compare(t0, t1, &env->fp_status);
554 if (unlikely(relation == float_relation_unordered)) {
555 update_fpscr(GETPC());
556 } else if (relation == float_relation_greater) {
557 set_t();
558 } else {
559 clr_t();
563 void helper_fcmp_gt_DT(float64 t0, float64 t1)
565 int relation;
567 set_float_exception_flags(0, &env->fp_status);
568 relation = float64_compare(t0, t1, &env->fp_status);
569 if (unlikely(relation == float_relation_unordered)) {
570 update_fpscr(GETPC());
571 } else if (relation == float_relation_greater) {
572 set_t();
573 } else {
574 clr_t();
578 float64 helper_fcnvsd_FT_DT(float32 t0)
580 float64 ret;
581 set_float_exception_flags(0, &env->fp_status);
582 ret = float32_to_float64(t0, &env->fp_status);
583 update_fpscr(GETPC());
584 return ret;
587 float32 helper_fcnvds_DT_FT(float64 t0)
589 float32 ret;
590 set_float_exception_flags(0, &env->fp_status);
591 ret = float64_to_float32(t0, &env->fp_status);
592 update_fpscr(GETPC());
593 return ret;
596 float32 helper_fdiv_FT(float32 t0, float32 t1)
598 set_float_exception_flags(0, &env->fp_status);
599 t0 = float32_div(t0, t1, &env->fp_status);
600 update_fpscr(GETPC());
601 return t0;
604 float64 helper_fdiv_DT(float64 t0, float64 t1)
606 set_float_exception_flags(0, &env->fp_status);
607 t0 = float64_div(t0, t1, &env->fp_status);
608 update_fpscr(GETPC());
609 return t0;
612 float32 helper_float_FT(uint32_t t0)
614 float32 ret;
615 set_float_exception_flags(0, &env->fp_status);
616 ret = int32_to_float32(t0, &env->fp_status);
617 update_fpscr(GETPC());
618 return ret;
621 float64 helper_float_DT(uint32_t t0)
623 float64 ret;
624 set_float_exception_flags(0, &env->fp_status);
625 ret = int32_to_float64(t0, &env->fp_status);
626 update_fpscr(GETPC());
627 return ret;
630 float32 helper_fmac_FT(float32 t0, float32 t1, float32 t2)
632 set_float_exception_flags(0, &env->fp_status);
633 t0 = float32_mul(t0, t1, &env->fp_status);
634 t0 = float32_add(t0, t2, &env->fp_status);
635 update_fpscr(GETPC());
636 return t0;
639 float32 helper_fmul_FT(float32 t0, float32 t1)
641 set_float_exception_flags(0, &env->fp_status);
642 t0 = float32_mul(t0, t1, &env->fp_status);
643 update_fpscr(GETPC());
644 return t0;
647 float64 helper_fmul_DT(float64 t0, float64 t1)
649 set_float_exception_flags(0, &env->fp_status);
650 t0 = float64_mul(t0, t1, &env->fp_status);
651 update_fpscr(GETPC());
652 return t0;
655 float32 helper_fneg_T(float32 t0)
657 return float32_chs(t0);
660 float32 helper_fsqrt_FT(float32 t0)
662 set_float_exception_flags(0, &env->fp_status);
663 t0 = float32_sqrt(t0, &env->fp_status);
664 update_fpscr(GETPC());
665 return t0;
668 float64 helper_fsqrt_DT(float64 t0)
670 set_float_exception_flags(0, &env->fp_status);
671 t0 = float64_sqrt(t0, &env->fp_status);
672 update_fpscr(GETPC());
673 return t0;
676 float32 helper_fsub_FT(float32 t0, float32 t1)
678 set_float_exception_flags(0, &env->fp_status);
679 t0 = float32_sub(t0, t1, &env->fp_status);
680 update_fpscr(GETPC());
681 return t0;
684 float64 helper_fsub_DT(float64 t0, float64 t1)
686 set_float_exception_flags(0, &env->fp_status);
687 t0 = float64_sub(t0, t1, &env->fp_status);
688 update_fpscr(GETPC());
689 return t0;
692 uint32_t helper_ftrc_FT(float32 t0)
694 uint32_t ret;
695 set_float_exception_flags(0, &env->fp_status);
696 ret = float32_to_int32_round_to_zero(t0, &env->fp_status);
697 update_fpscr(GETPC());
698 return ret;
701 uint32_t helper_ftrc_DT(float64 t0)
703 uint32_t ret;
704 set_float_exception_flags(0, &env->fp_status);
705 ret = float64_to_int32_round_to_zero(t0, &env->fp_status);
706 update_fpscr(GETPC());
707 return ret;
710 void helper_fipr(uint32_t m, uint32_t n)
712 int bank, i;
713 float32 r, p;
715 bank = (env->sr & FPSCR_FR) ? 16 : 0;
716 r = float32_zero;
717 set_float_exception_flags(0, &env->fp_status);
719 for (i = 0 ; i < 4 ; i++) {
720 p = float32_mul(env->fregs[bank + m + i],
721 env->fregs[bank + n + i],
722 &env->fp_status);
723 r = float32_add(r, p, &env->fp_status);
725 update_fpscr(GETPC());
727 env->fregs[bank + n + 3] = r;
730 void helper_ftrv(uint32_t n)
732 int bank_matrix, bank_vector;
733 int i, j;
734 float32 r[4];
735 float32 p;
737 bank_matrix = (env->sr & FPSCR_FR) ? 0 : 16;
738 bank_vector = (env->sr & FPSCR_FR) ? 16 : 0;
739 set_float_exception_flags(0, &env->fp_status);
740 for (i = 0 ; i < 4 ; i++) {
741 r[i] = float32_zero;
742 for (j = 0 ; j < 4 ; j++) {
743 p = float32_mul(env->fregs[bank_matrix + 4 * j + i],
744 env->fregs[bank_vector + j],
745 &env->fp_status);
746 r[i] = float32_add(r[i], p, &env->fp_status);
749 update_fpscr(GETPC());
751 for (i = 0 ; i < 4 ; i++) {
752 env->fregs[bank_vector + i] = r[i];