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.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/host-utils.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
28 #include "fpu/softfloat.h"
30 void helper_put(uint32_t id
, uint32_t ctrl
, uint32_t data
)
32 int test
= ctrl
& STREAM_TEST
;
33 int atomic
= ctrl
& STREAM_ATOMIC
;
34 int control
= ctrl
& STREAM_CONTROL
;
35 int nonblock
= ctrl
& STREAM_NONBLOCK
;
36 int exception
= ctrl
& STREAM_EXCEPTION
;
38 qemu_log_mask(LOG_UNIMP
, "Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
47 uint32_t helper_get(uint32_t id
, uint32_t ctrl
)
49 int test
= ctrl
& STREAM_TEST
;
50 int atomic
= ctrl
& STREAM_ATOMIC
;
51 int control
= ctrl
& STREAM_CONTROL
;
52 int nonblock
= ctrl
& STREAM_NONBLOCK
;
53 int exception
= ctrl
& STREAM_EXCEPTION
;
55 qemu_log_mask(LOG_UNIMP
, "Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
62 return 0xdead0000 | id
;
65 void helper_raise_exception(CPUMBState
*env
, uint32_t index
)
67 CPUState
*cs
= env_cpu(env
);
69 cs
->exception_index
= index
;
73 static bool check_divz(CPUMBState
*env
, uint32_t a
, uint32_t b
, uintptr_t ra
)
75 if (unlikely(b
== 0)) {
78 if ((env
->msr
& MSR_EE
) &&
79 env_archcpu(env
)->cfg
.div_zero_exception
) {
80 CPUState
*cs
= env_cpu(env
);
82 env
->esr
= ESR_EC_DIVZERO
;
83 cs
->exception_index
= EXCP_HW_EXCP
;
84 cpu_loop_exit_restore(cs
, ra
);
91 uint32_t helper_divs(CPUMBState
*env
, uint32_t a
, uint32_t b
)
93 if (!check_divz(env
, a
, b
, GETPC())) {
96 return (int32_t)a
/ (int32_t)b
;
99 uint32_t helper_divu(CPUMBState
*env
, uint32_t a
, uint32_t b
)
101 if (!check_divz(env
, a
, b
, GETPC())) {
107 /* raise FPU exception. */
108 static void raise_fpu_exception(CPUMBState
*env
, uintptr_t ra
)
110 CPUState
*cs
= env_cpu(env
);
112 env
->esr
= ESR_EC_FPU
;
113 cs
->exception_index
= EXCP_HW_EXCP
;
114 cpu_loop_exit_restore(cs
, ra
);
117 static void update_fpu_flags(CPUMBState
*env
, int flags
, uintptr_t ra
)
121 if (flags
& float_flag_invalid
) {
125 if (flags
& float_flag_divbyzero
) {
129 if (flags
& float_flag_overflow
) {
133 if (flags
& float_flag_underflow
) {
138 && (env_archcpu(env
)->cfg
.pvr_regs
[2] & PVR2_FPU_EXC_MASK
)
139 && (env
->msr
& MSR_EE
)) {
140 raise_fpu_exception(env
, ra
);
144 uint32_t helper_fadd(CPUMBState
*env
, uint32_t a
, uint32_t b
)
146 CPU_FloatU fd
, fa
, fb
;
149 set_float_exception_flags(0, &env
->fp_status
);
152 fd
.f
= float32_add(fa
.f
, fb
.f
, &env
->fp_status
);
154 flags
= get_float_exception_flags(&env
->fp_status
);
155 update_fpu_flags(env
, flags
, GETPC());
159 uint32_t helper_frsub(CPUMBState
*env
, uint32_t a
, uint32_t b
)
161 CPU_FloatU fd
, fa
, fb
;
164 set_float_exception_flags(0, &env
->fp_status
);
167 fd
.f
= float32_sub(fb
.f
, fa
.f
, &env
->fp_status
);
168 flags
= get_float_exception_flags(&env
->fp_status
);
169 update_fpu_flags(env
, flags
, GETPC());
173 uint32_t helper_fmul(CPUMBState
*env
, uint32_t a
, uint32_t b
)
175 CPU_FloatU fd
, fa
, fb
;
178 set_float_exception_flags(0, &env
->fp_status
);
181 fd
.f
= float32_mul(fa
.f
, fb
.f
, &env
->fp_status
);
182 flags
= get_float_exception_flags(&env
->fp_status
);
183 update_fpu_flags(env
, flags
, GETPC());
188 uint32_t helper_fdiv(CPUMBState
*env
, uint32_t a
, uint32_t b
)
190 CPU_FloatU fd
, fa
, fb
;
193 set_float_exception_flags(0, &env
->fp_status
);
196 fd
.f
= float32_div(fb
.f
, fa
.f
, &env
->fp_status
);
197 flags
= get_float_exception_flags(&env
->fp_status
);
198 update_fpu_flags(env
, flags
, GETPC());
203 uint32_t helper_fcmp_un(CPUMBState
*env
, uint32_t a
, uint32_t b
)
211 if (float32_is_signaling_nan(fa
.f
, &env
->fp_status
) ||
212 float32_is_signaling_nan(fb
.f
, &env
->fp_status
)) {
213 update_fpu_flags(env
, float_flag_invalid
, GETPC());
217 if (float32_is_quiet_nan(fa
.f
, &env
->fp_status
) ||
218 float32_is_quiet_nan(fb
.f
, &env
->fp_status
)) {
225 uint32_t helper_fcmp_lt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
231 set_float_exception_flags(0, &env
->fp_status
);
234 r
= float32_lt(fb
.f
, fa
.f
, &env
->fp_status
);
235 flags
= get_float_exception_flags(&env
->fp_status
);
236 update_fpu_flags(env
, flags
& float_flag_invalid
, GETPC());
241 uint32_t helper_fcmp_eq(CPUMBState
*env
, uint32_t a
, uint32_t b
)
247 set_float_exception_flags(0, &env
->fp_status
);
250 r
= float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
251 flags
= get_float_exception_flags(&env
->fp_status
);
252 update_fpu_flags(env
, flags
& float_flag_invalid
, GETPC());
257 uint32_t helper_fcmp_le(CPUMBState
*env
, uint32_t a
, uint32_t b
)
265 set_float_exception_flags(0, &env
->fp_status
);
266 r
= float32_le(fa
.f
, fb
.f
, &env
->fp_status
);
267 flags
= get_float_exception_flags(&env
->fp_status
);
268 update_fpu_flags(env
, flags
& float_flag_invalid
, GETPC());
274 uint32_t helper_fcmp_gt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
281 set_float_exception_flags(0, &env
->fp_status
);
282 r
= float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
283 flags
= get_float_exception_flags(&env
->fp_status
);
284 update_fpu_flags(env
, flags
& float_flag_invalid
, GETPC());
288 uint32_t helper_fcmp_ne(CPUMBState
*env
, uint32_t a
, uint32_t b
)
295 set_float_exception_flags(0, &env
->fp_status
);
296 r
= !float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
297 flags
= get_float_exception_flags(&env
->fp_status
);
298 update_fpu_flags(env
, flags
& float_flag_invalid
, GETPC());
303 uint32_t helper_fcmp_ge(CPUMBState
*env
, uint32_t a
, uint32_t b
)
310 set_float_exception_flags(0, &env
->fp_status
);
311 r
= !float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
312 flags
= get_float_exception_flags(&env
->fp_status
);
313 update_fpu_flags(env
, flags
& float_flag_invalid
, GETPC());
318 uint32_t helper_flt(CPUMBState
*env
, uint32_t a
)
323 fd
.f
= int32_to_float32(fa
.l
, &env
->fp_status
);
327 uint32_t helper_fint(CPUMBState
*env
, uint32_t a
)
333 set_float_exception_flags(0, &env
->fp_status
);
335 r
= float32_to_int32(fa
.f
, &env
->fp_status
);
336 flags
= get_float_exception_flags(&env
->fp_status
);
337 update_fpu_flags(env
, flags
, GETPC());
342 uint32_t helper_fsqrt(CPUMBState
*env
, uint32_t a
)
347 set_float_exception_flags(0, &env
->fp_status
);
349 fd
.l
= float32_sqrt(fa
.f
, &env
->fp_status
);
350 flags
= get_float_exception_flags(&env
->fp_status
);
351 update_fpu_flags(env
, flags
, GETPC());
356 uint32_t helper_pcmpbf(uint32_t a
, uint32_t b
)
359 uint32_t mask
= 0xff000000;
361 for (i
= 0; i
< 4; i
++) {
362 if ((a
& mask
) == (b
& mask
))
369 void helper_stackprot(CPUMBState
*env
, target_ulong addr
)
371 if (addr
< env
->slr
|| addr
> env
->shr
) {
372 CPUState
*cs
= env_cpu(env
);
374 qemu_log_mask(CPU_LOG_INT
, "Stack protector violation at "
375 TARGET_FMT_lx
" %x %x\n",
376 addr
, env
->slr
, env
->shr
);
379 env
->esr
= ESR_EC_STACKPROT
;
380 cs
->exception_index
= EXCP_HW_EXCP
;
381 cpu_loop_exit_restore(cs
, GETPC());
385 #if !defined(CONFIG_USER_ONLY)
386 /* Writes/reads to the MMU's special regs end up here. */
387 uint32_t helper_mmu_read(CPUMBState
*env
, uint32_t ext
, uint32_t rn
)
389 return mmu_read(env
, ext
, rn
);
392 void helper_mmu_write(CPUMBState
*env
, uint32_t ext
, uint32_t rn
, uint32_t v
)
394 mmu_write(env
, ext
, rn
, v
);
397 void mb_cpu_transaction_failed(CPUState
*cs
, hwaddr physaddr
, vaddr addr
,
398 unsigned size
, MMUAccessType access_type
,
399 int mmu_idx
, MemTxAttrs attrs
,
400 MemTxResult response
, uintptr_t retaddr
)
402 MicroBlazeCPU
*cpu
= MICROBLAZE_CPU(cs
);
403 CPUMBState
*env
= &cpu
->env
;
405 qemu_log_mask(CPU_LOG_INT
, "Transaction failed: vaddr 0x%" VADDR_PRIx
406 " physaddr 0x" HWADDR_FMT_plx
" size %d access type %s\n",
407 addr
, physaddr
, size
,
408 access_type
== MMU_INST_FETCH
? "INST_FETCH" :
409 (access_type
== MMU_DATA_LOAD
? "DATA_LOAD" : "DATA_STORE"));
411 if (!(env
->msr
& MSR_EE
)) {
415 if (access_type
== MMU_INST_FETCH
) {
416 if (!cpu
->cfg
.iopb_bus_exception
) {
419 env
->esr
= ESR_EC_INSN_BUS
;
421 if (!cpu
->cfg
.dopb_bus_exception
) {
424 env
->esr
= ESR_EC_DATA_BUS
;
428 cs
->exception_index
= EXCP_HW_EXCP
;
429 cpu_loop_exit_restore(cs
, retaddr
);