2 * Microblaze helper routines.
4 * Copyright (c) 2009 Edgar E. Iglesias <edgar.iglesias@gmail.com>.
5 * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "exec/helper-proto.h"
24 #include "qemu/host-utils.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "fpu/softfloat.h"
31 void helper_put(uint32_t id
, uint32_t ctrl
, uint32_t data
)
33 int test
= ctrl
& STREAM_TEST
;
34 int atomic
= ctrl
& STREAM_ATOMIC
;
35 int control
= ctrl
& STREAM_CONTROL
;
36 int nonblock
= ctrl
& STREAM_NONBLOCK
;
37 int exception
= ctrl
& STREAM_EXCEPTION
;
39 qemu_log_mask(LOG_UNIMP
, "Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
48 uint32_t helper_get(uint32_t id
, uint32_t ctrl
)
50 int test
= ctrl
& STREAM_TEST
;
51 int atomic
= ctrl
& STREAM_ATOMIC
;
52 int control
= ctrl
& STREAM_CONTROL
;
53 int nonblock
= ctrl
& STREAM_NONBLOCK
;
54 int exception
= ctrl
& STREAM_EXCEPTION
;
56 qemu_log_mask(LOG_UNIMP
, "Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
63 return 0xdead0000 | id
;
66 void helper_raise_exception(CPUMBState
*env
, uint32_t index
)
68 CPUState
*cs
= env_cpu(env
);
70 cs
->exception_index
= index
;
74 void helper_debug(CPUMBState
*env
)
78 qemu_log("PC=%" PRIx64
"\n", env
->sregs
[SR_PC
]);
79 qemu_log("rmsr=%" PRIx64
" resr=%" PRIx64
" rear=%" PRIx64
" "
80 "debug[%x] imm=%x iflags=%x\n",
81 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
], env
->sregs
[SR_EAR
],
82 env
->debug
, env
->imm
, env
->iflags
);
83 qemu_log("btaken=%d btarget=%" PRIx64
" mode=%s(saved=%s) eip=%d ie=%d\n",
84 env
->btaken
, env
->btarget
,
85 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
86 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel",
87 (bool)(env
->sregs
[SR_MSR
] & MSR_EIP
),
88 (bool)(env
->sregs
[SR_MSR
] & MSR_IE
));
89 for (i
= 0; i
< 32; i
++) {
90 qemu_log("r%2.2d=%8.8x ", i
, env
->regs
[i
]);
97 static inline uint32_t compute_carry(uint32_t a
, uint32_t b
, uint32_t cin
)
101 if ((b
== ~0) && cin
)
103 else if ((~0 - a
) < (b
+ cin
))
108 uint32_t helper_cmp(uint32_t a
, uint32_t b
)
113 if ((b
& 0x80000000) ^ (a
& 0x80000000))
114 t
= (t
& 0x7fffffff) | (b
& 0x80000000);
118 uint32_t helper_cmpu(uint32_t a
, uint32_t b
)
123 if ((b
& 0x80000000) ^ (a
& 0x80000000))
124 t
= (t
& 0x7fffffff) | (a
& 0x80000000);
128 uint32_t helper_carry(uint32_t a
, uint32_t b
, uint32_t cf
)
130 return compute_carry(a
, b
, cf
);
133 static inline int div_prepare(CPUMBState
*env
, uint32_t a
, uint32_t b
)
136 env
->sregs
[SR_MSR
] |= MSR_DZ
;
138 if ((env
->sregs
[SR_MSR
] & MSR_EE
)
139 && !(env
->pvr
.regs
[2] & PVR2_DIV_ZERO_EXC_MASK
)) {
140 env
->sregs
[SR_ESR
] = ESR_EC_DIVZERO
;
141 helper_raise_exception(env
, EXCP_HW_EXCP
);
145 env
->sregs
[SR_MSR
] &= ~MSR_DZ
;
149 uint32_t helper_divs(CPUMBState
*env
, uint32_t a
, uint32_t b
)
151 if (!div_prepare(env
, a
, b
)) {
154 return (int32_t)a
/ (int32_t)b
;
157 uint32_t helper_divu(CPUMBState
*env
, uint32_t a
, uint32_t b
)
159 if (!div_prepare(env
, a
, b
)) {
165 /* raise FPU exception. */
166 static void raise_fpu_exception(CPUMBState
*env
)
168 env
->sregs
[SR_ESR
] = ESR_EC_FPU
;
169 helper_raise_exception(env
, EXCP_HW_EXCP
);
172 static void update_fpu_flags(CPUMBState
*env
, int flags
)
176 if (flags
& float_flag_invalid
) {
177 env
->sregs
[SR_FSR
] |= FSR_IO
;
180 if (flags
& float_flag_divbyzero
) {
181 env
->sregs
[SR_FSR
] |= FSR_DZ
;
184 if (flags
& float_flag_overflow
) {
185 env
->sregs
[SR_FSR
] |= FSR_OF
;
188 if (flags
& float_flag_underflow
) {
189 env
->sregs
[SR_FSR
] |= FSR_UF
;
193 && (env
->pvr
.regs
[2] & PVR2_FPU_EXC_MASK
)
194 && (env
->sregs
[SR_MSR
] & MSR_EE
)) {
195 raise_fpu_exception(env
);
199 uint32_t helper_fadd(CPUMBState
*env
, uint32_t a
, uint32_t b
)
201 CPU_FloatU fd
, fa
, fb
;
204 set_float_exception_flags(0, &env
->fp_status
);
207 fd
.f
= float32_add(fa
.f
, fb
.f
, &env
->fp_status
);
209 flags
= get_float_exception_flags(&env
->fp_status
);
210 update_fpu_flags(env
, flags
);
214 uint32_t helper_frsub(CPUMBState
*env
, uint32_t a
, uint32_t b
)
216 CPU_FloatU fd
, fa
, fb
;
219 set_float_exception_flags(0, &env
->fp_status
);
222 fd
.f
= float32_sub(fb
.f
, fa
.f
, &env
->fp_status
);
223 flags
= get_float_exception_flags(&env
->fp_status
);
224 update_fpu_flags(env
, flags
);
228 uint32_t helper_fmul(CPUMBState
*env
, uint32_t a
, uint32_t b
)
230 CPU_FloatU fd
, fa
, fb
;
233 set_float_exception_flags(0, &env
->fp_status
);
236 fd
.f
= float32_mul(fa
.f
, fb
.f
, &env
->fp_status
);
237 flags
= get_float_exception_flags(&env
->fp_status
);
238 update_fpu_flags(env
, flags
);
243 uint32_t helper_fdiv(CPUMBState
*env
, uint32_t a
, uint32_t b
)
245 CPU_FloatU fd
, fa
, fb
;
248 set_float_exception_flags(0, &env
->fp_status
);
251 fd
.f
= float32_div(fb
.f
, fa
.f
, &env
->fp_status
);
252 flags
= get_float_exception_flags(&env
->fp_status
);
253 update_fpu_flags(env
, flags
);
258 uint32_t helper_fcmp_un(CPUMBState
*env
, uint32_t a
, uint32_t b
)
266 if (float32_is_signaling_nan(fa
.f
, &env
->fp_status
) ||
267 float32_is_signaling_nan(fb
.f
, &env
->fp_status
)) {
268 update_fpu_flags(env
, float_flag_invalid
);
272 if (float32_is_quiet_nan(fa
.f
, &env
->fp_status
) ||
273 float32_is_quiet_nan(fb
.f
, &env
->fp_status
)) {
280 uint32_t helper_fcmp_lt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
286 set_float_exception_flags(0, &env
->fp_status
);
289 r
= float32_lt(fb
.f
, fa
.f
, &env
->fp_status
);
290 flags
= get_float_exception_flags(&env
->fp_status
);
291 update_fpu_flags(env
, flags
& float_flag_invalid
);
296 uint32_t helper_fcmp_eq(CPUMBState
*env
, uint32_t a
, uint32_t b
)
302 set_float_exception_flags(0, &env
->fp_status
);
305 r
= float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
306 flags
= get_float_exception_flags(&env
->fp_status
);
307 update_fpu_flags(env
, flags
& float_flag_invalid
);
312 uint32_t helper_fcmp_le(CPUMBState
*env
, uint32_t a
, uint32_t b
)
320 set_float_exception_flags(0, &env
->fp_status
);
321 r
= float32_le(fa
.f
, fb
.f
, &env
->fp_status
);
322 flags
= get_float_exception_flags(&env
->fp_status
);
323 update_fpu_flags(env
, flags
& float_flag_invalid
);
329 uint32_t helper_fcmp_gt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
336 set_float_exception_flags(0, &env
->fp_status
);
337 r
= float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
338 flags
= get_float_exception_flags(&env
->fp_status
);
339 update_fpu_flags(env
, flags
& float_flag_invalid
);
343 uint32_t helper_fcmp_ne(CPUMBState
*env
, uint32_t a
, uint32_t b
)
350 set_float_exception_flags(0, &env
->fp_status
);
351 r
= !float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
352 flags
= get_float_exception_flags(&env
->fp_status
);
353 update_fpu_flags(env
, flags
& float_flag_invalid
);
358 uint32_t helper_fcmp_ge(CPUMBState
*env
, uint32_t a
, uint32_t b
)
365 set_float_exception_flags(0, &env
->fp_status
);
366 r
= !float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
367 flags
= get_float_exception_flags(&env
->fp_status
);
368 update_fpu_flags(env
, flags
& float_flag_invalid
);
373 uint32_t helper_flt(CPUMBState
*env
, uint32_t a
)
378 fd
.f
= int32_to_float32(fa
.l
, &env
->fp_status
);
382 uint32_t helper_fint(CPUMBState
*env
, uint32_t a
)
388 set_float_exception_flags(0, &env
->fp_status
);
390 r
= float32_to_int32(fa
.f
, &env
->fp_status
);
391 flags
= get_float_exception_flags(&env
->fp_status
);
392 update_fpu_flags(env
, flags
);
397 uint32_t helper_fsqrt(CPUMBState
*env
, uint32_t a
)
402 set_float_exception_flags(0, &env
->fp_status
);
404 fd
.l
= float32_sqrt(fa
.f
, &env
->fp_status
);
405 flags
= get_float_exception_flags(&env
->fp_status
);
406 update_fpu_flags(env
, flags
);
411 uint32_t helper_pcmpbf(uint32_t a
, uint32_t b
)
414 uint32_t mask
= 0xff000000;
416 for (i
= 0; i
< 4; i
++) {
417 if ((a
& mask
) == (b
& mask
))
424 void helper_memalign(CPUMBState
*env
, target_ulong addr
,
425 uint32_t dr
, uint32_t wr
,
429 qemu_log_mask(CPU_LOG_INT
,
430 "unaligned access addr=" TARGET_FMT_lx
431 " mask=%x, wr=%d dr=r%d\n",
433 env
->sregs
[SR_EAR
] = addr
;
434 env
->sregs
[SR_ESR
] = ESR_EC_UNALIGNED_DATA
| (wr
<< 10) \
437 env
->sregs
[SR_ESR
] |= 1 << 11;
439 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
442 helper_raise_exception(env
, EXCP_HW_EXCP
);
446 void helper_stackprot(CPUMBState
*env
, target_ulong addr
)
448 if (addr
< env
->slr
|| addr
> env
->shr
) {
449 qemu_log_mask(CPU_LOG_INT
, "Stack protector violation at "
450 TARGET_FMT_lx
" %x %x\n",
451 addr
, env
->slr
, env
->shr
);
452 env
->sregs
[SR_EAR
] = addr
;
453 env
->sregs
[SR_ESR
] = ESR_EC_STACKPROT
;
454 helper_raise_exception(env
, EXCP_HW_EXCP
);
458 #if !defined(CONFIG_USER_ONLY)
459 /* Writes/reads to the MMU's special regs end up here. */
460 uint32_t helper_mmu_read(CPUMBState
*env
, uint32_t ext
, uint32_t rn
)
462 return mmu_read(env
, ext
, rn
);
465 void helper_mmu_write(CPUMBState
*env
, uint32_t ext
, uint32_t rn
, uint32_t v
)
467 mmu_write(env
, ext
, rn
, v
);
470 void mb_cpu_transaction_failed(CPUState
*cs
, hwaddr physaddr
, vaddr addr
,
471 unsigned size
, MMUAccessType access_type
,
472 int mmu_idx
, MemTxAttrs attrs
,
473 MemTxResult response
, uintptr_t retaddr
)
477 qemu_log_mask(CPU_LOG_INT
, "Transaction failed: vaddr 0x%" VADDR_PRIx
478 " physaddr 0x" TARGET_FMT_plx
" size %d access type %s\n",
479 addr
, physaddr
, size
,
480 access_type
== MMU_INST_FETCH
? "INST_FETCH" :
481 (access_type
== MMU_DATA_LOAD
? "DATA_LOAD" : "DATA_STORE"));
482 cpu
= MICROBLAZE_CPU(cs
);
485 cpu_restore_state(cs
, retaddr
, true);
486 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
490 env
->sregs
[SR_EAR
] = addr
;
491 if (access_type
== MMU_INST_FETCH
) {
492 if ((env
->pvr
.regs
[2] & PVR2_IOPB_BUS_EXC_MASK
)) {
493 env
->sregs
[SR_ESR
] = ESR_EC_INSN_BUS
;
494 helper_raise_exception(env
, EXCP_HW_EXCP
);
497 if ((env
->pvr
.regs
[2] & PVR2_DOPB_BUS_EXC_MASK
)) {
498 env
->sregs
[SR_ESR
] = ESR_EC_DATA_BUS
;
499 helper_raise_exception(env
, EXCP_HW_EXCP
);