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 #if !defined(CONFIG_USER_ONLY)
33 /* Try to fill the TLB and return an exception if error. If retaddr is
34 * NULL, it means that the function was called in C code (i.e. not
35 * from generated code or from helper.c)
37 void tlb_fill(CPUState
*cs
, target_ulong addr
, int size
,
38 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
42 ret
= mb_cpu_handle_mmu_fault(cs
, addr
, size
, access_type
, mmu_idx
);
44 /* now we have a real cpu fault */
45 cpu_loop_exit_restore(cs
, retaddr
);
50 void helper_put(uint32_t id
, uint32_t ctrl
, uint32_t data
)
52 int test
= ctrl
& STREAM_TEST
;
53 int atomic
= ctrl
& STREAM_ATOMIC
;
54 int control
= ctrl
& STREAM_CONTROL
;
55 int nonblock
= ctrl
& STREAM_NONBLOCK
;
56 int exception
= ctrl
& STREAM_EXCEPTION
;
58 qemu_log_mask(LOG_UNIMP
, "Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
67 uint32_t helper_get(uint32_t id
, uint32_t ctrl
)
69 int test
= ctrl
& STREAM_TEST
;
70 int atomic
= ctrl
& STREAM_ATOMIC
;
71 int control
= ctrl
& STREAM_CONTROL
;
72 int nonblock
= ctrl
& STREAM_NONBLOCK
;
73 int exception
= ctrl
& STREAM_EXCEPTION
;
75 qemu_log_mask(LOG_UNIMP
, "Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
82 return 0xdead0000 | id
;
85 void helper_raise_exception(CPUMBState
*env
, uint32_t index
)
87 CPUState
*cs
= CPU(mb_env_get_cpu(env
));
89 cs
->exception_index
= index
;
93 void helper_debug(CPUMBState
*env
)
97 qemu_log("PC=%8.8x\n", env
->sregs
[SR_PC
]);
98 qemu_log("rmsr=%x resr=%x rear=%x debug[%x] imm=%x iflags=%x\n",
99 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
], env
->sregs
[SR_EAR
],
100 env
->debug
, env
->imm
, env
->iflags
);
101 qemu_log("btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
102 env
->btaken
, env
->btarget
,
103 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
104 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel",
105 (env
->sregs
[SR_MSR
] & MSR_EIP
),
106 (env
->sregs
[SR_MSR
] & MSR_IE
));
107 for (i
= 0; i
< 32; i
++) {
108 qemu_log("r%2.2d=%8.8x ", i
, env
->regs
[i
]);
109 if ((i
+ 1) % 4 == 0)
115 static inline uint32_t compute_carry(uint32_t a
, uint32_t b
, uint32_t cin
)
119 if ((b
== ~0) && cin
)
121 else if ((~0 - a
) < (b
+ cin
))
126 uint32_t helper_cmp(uint32_t a
, uint32_t b
)
131 if ((b
& 0x80000000) ^ (a
& 0x80000000))
132 t
= (t
& 0x7fffffff) | (b
& 0x80000000);
136 uint32_t helper_cmpu(uint32_t a
, uint32_t b
)
141 if ((b
& 0x80000000) ^ (a
& 0x80000000))
142 t
= (t
& 0x7fffffff) | (a
& 0x80000000);
146 uint32_t helper_carry(uint32_t a
, uint32_t b
, uint32_t cf
)
148 return compute_carry(a
, b
, cf
);
151 static inline int div_prepare(CPUMBState
*env
, uint32_t a
, uint32_t b
)
154 env
->sregs
[SR_MSR
] |= MSR_DZ
;
156 if ((env
->sregs
[SR_MSR
] & MSR_EE
)
157 && !(env
->pvr
.regs
[2] & PVR2_DIV_ZERO_EXC_MASK
)) {
158 env
->sregs
[SR_ESR
] = ESR_EC_DIVZERO
;
159 helper_raise_exception(env
, EXCP_HW_EXCP
);
163 env
->sregs
[SR_MSR
] &= ~MSR_DZ
;
167 uint32_t helper_divs(CPUMBState
*env
, uint32_t a
, uint32_t b
)
169 if (!div_prepare(env
, a
, b
)) {
172 return (int32_t)a
/ (int32_t)b
;
175 uint32_t helper_divu(CPUMBState
*env
, uint32_t a
, uint32_t b
)
177 if (!div_prepare(env
, a
, b
)) {
183 /* raise FPU exception. */
184 static void raise_fpu_exception(CPUMBState
*env
)
186 env
->sregs
[SR_ESR
] = ESR_EC_FPU
;
187 helper_raise_exception(env
, EXCP_HW_EXCP
);
190 static void update_fpu_flags(CPUMBState
*env
, int flags
)
194 if (flags
& float_flag_invalid
) {
195 env
->sregs
[SR_FSR
] |= FSR_IO
;
198 if (flags
& float_flag_divbyzero
) {
199 env
->sregs
[SR_FSR
] |= FSR_DZ
;
202 if (flags
& float_flag_overflow
) {
203 env
->sregs
[SR_FSR
] |= FSR_OF
;
206 if (flags
& float_flag_underflow
) {
207 env
->sregs
[SR_FSR
] |= FSR_UF
;
211 && (env
->pvr
.regs
[2] & PVR2_FPU_EXC_MASK
)
212 && (env
->sregs
[SR_MSR
] & MSR_EE
)) {
213 raise_fpu_exception(env
);
217 uint32_t helper_fadd(CPUMBState
*env
, uint32_t a
, uint32_t b
)
219 CPU_FloatU fd
, fa
, fb
;
222 set_float_exception_flags(0, &env
->fp_status
);
225 fd
.f
= float32_add(fa
.f
, fb
.f
, &env
->fp_status
);
227 flags
= get_float_exception_flags(&env
->fp_status
);
228 update_fpu_flags(env
, flags
);
232 uint32_t helper_frsub(CPUMBState
*env
, uint32_t a
, uint32_t b
)
234 CPU_FloatU fd
, fa
, fb
;
237 set_float_exception_flags(0, &env
->fp_status
);
240 fd
.f
= float32_sub(fb
.f
, fa
.f
, &env
->fp_status
);
241 flags
= get_float_exception_flags(&env
->fp_status
);
242 update_fpu_flags(env
, flags
);
246 uint32_t helper_fmul(CPUMBState
*env
, uint32_t a
, uint32_t b
)
248 CPU_FloatU fd
, fa
, fb
;
251 set_float_exception_flags(0, &env
->fp_status
);
254 fd
.f
= float32_mul(fa
.f
, fb
.f
, &env
->fp_status
);
255 flags
= get_float_exception_flags(&env
->fp_status
);
256 update_fpu_flags(env
, flags
);
261 uint32_t helper_fdiv(CPUMBState
*env
, uint32_t a
, uint32_t b
)
263 CPU_FloatU fd
, fa
, fb
;
266 set_float_exception_flags(0, &env
->fp_status
);
269 fd
.f
= float32_div(fb
.f
, fa
.f
, &env
->fp_status
);
270 flags
= get_float_exception_flags(&env
->fp_status
);
271 update_fpu_flags(env
, flags
);
276 uint32_t helper_fcmp_un(CPUMBState
*env
, uint32_t a
, uint32_t b
)
284 if (float32_is_signaling_nan(fa
.f
, &env
->fp_status
) ||
285 float32_is_signaling_nan(fb
.f
, &env
->fp_status
)) {
286 update_fpu_flags(env
, float_flag_invalid
);
290 if (float32_is_quiet_nan(fa
.f
, &env
->fp_status
) ||
291 float32_is_quiet_nan(fb
.f
, &env
->fp_status
)) {
298 uint32_t helper_fcmp_lt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
304 set_float_exception_flags(0, &env
->fp_status
);
307 r
= float32_lt(fb
.f
, fa
.f
, &env
->fp_status
);
308 flags
= get_float_exception_flags(&env
->fp_status
);
309 update_fpu_flags(env
, flags
& float_flag_invalid
);
314 uint32_t helper_fcmp_eq(CPUMBState
*env
, uint32_t a
, uint32_t b
)
320 set_float_exception_flags(0, &env
->fp_status
);
323 r
= float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
324 flags
= get_float_exception_flags(&env
->fp_status
);
325 update_fpu_flags(env
, flags
& float_flag_invalid
);
330 uint32_t helper_fcmp_le(CPUMBState
*env
, uint32_t a
, uint32_t b
)
338 set_float_exception_flags(0, &env
->fp_status
);
339 r
= float32_le(fa
.f
, fb
.f
, &env
->fp_status
);
340 flags
= get_float_exception_flags(&env
->fp_status
);
341 update_fpu_flags(env
, flags
& float_flag_invalid
);
347 uint32_t helper_fcmp_gt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
354 set_float_exception_flags(0, &env
->fp_status
);
355 r
= float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
356 flags
= get_float_exception_flags(&env
->fp_status
);
357 update_fpu_flags(env
, flags
& float_flag_invalid
);
361 uint32_t helper_fcmp_ne(CPUMBState
*env
, uint32_t a
, uint32_t b
)
368 set_float_exception_flags(0, &env
->fp_status
);
369 r
= !float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
370 flags
= get_float_exception_flags(&env
->fp_status
);
371 update_fpu_flags(env
, flags
& float_flag_invalid
);
376 uint32_t helper_fcmp_ge(CPUMBState
*env
, uint32_t a
, uint32_t b
)
383 set_float_exception_flags(0, &env
->fp_status
);
384 r
= !float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
385 flags
= get_float_exception_flags(&env
->fp_status
);
386 update_fpu_flags(env
, flags
& float_flag_invalid
);
391 uint32_t helper_flt(CPUMBState
*env
, uint32_t a
)
396 fd
.f
= int32_to_float32(fa
.l
, &env
->fp_status
);
400 uint32_t helper_fint(CPUMBState
*env
, uint32_t a
)
406 set_float_exception_flags(0, &env
->fp_status
);
408 r
= float32_to_int32(fa
.f
, &env
->fp_status
);
409 flags
= get_float_exception_flags(&env
->fp_status
);
410 update_fpu_flags(env
, flags
);
415 uint32_t helper_fsqrt(CPUMBState
*env
, uint32_t a
)
420 set_float_exception_flags(0, &env
->fp_status
);
422 fd
.l
= float32_sqrt(fa
.f
, &env
->fp_status
);
423 flags
= get_float_exception_flags(&env
->fp_status
);
424 update_fpu_flags(env
, flags
);
429 uint32_t helper_pcmpbf(uint32_t a
, uint32_t b
)
432 uint32_t mask
= 0xff000000;
434 for (i
= 0; i
< 4; i
++) {
435 if ((a
& mask
) == (b
& mask
))
442 void helper_memalign(CPUMBState
*env
, uint32_t addr
, uint32_t dr
, uint32_t wr
,
446 qemu_log_mask(CPU_LOG_INT
,
447 "unaligned access addr=%x mask=%x, wr=%d dr=r%d\n",
449 env
->sregs
[SR_EAR
] = addr
;
450 env
->sregs
[SR_ESR
] = ESR_EC_UNALIGNED_DATA
| (wr
<< 10) \
453 env
->sregs
[SR_ESR
] |= 1 << 11;
455 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
458 helper_raise_exception(env
, EXCP_HW_EXCP
);
462 void helper_stackprot(CPUMBState
*env
, uint32_t addr
)
464 if (addr
< env
->slr
|| addr
> env
->shr
) {
465 qemu_log_mask(CPU_LOG_INT
, "Stack protector violation at %x %x %x\n",
466 addr
, env
->slr
, env
->shr
);
467 env
->sregs
[SR_EAR
] = addr
;
468 env
->sregs
[SR_ESR
] = ESR_EC_STACKPROT
;
469 helper_raise_exception(env
, EXCP_HW_EXCP
);
473 #if !defined(CONFIG_USER_ONLY)
474 /* Writes/reads to the MMU's special regs end up here. */
475 uint32_t helper_mmu_read(CPUMBState
*env
, uint32_t rn
)
477 return mmu_read(env
, rn
);
480 void helper_mmu_write(CPUMBState
*env
, uint32_t rn
, uint32_t v
)
482 mmu_write(env
, rn
, v
);
485 void mb_cpu_unassigned_access(CPUState
*cs
, hwaddr addr
,
486 bool is_write
, bool is_exec
, int is_asi
,
492 qemu_log_mask(CPU_LOG_INT
, "Unassigned " TARGET_FMT_plx
" wr=%d exe=%d\n",
493 addr
, is_write
? 1 : 0, is_exec
? 1 : 0);
497 cpu
= MICROBLAZE_CPU(cs
);
499 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
503 env
->sregs
[SR_EAR
] = addr
;
505 if ((env
->pvr
.regs
[2] & PVR2_IOPB_BUS_EXC_MASK
)) {
506 env
->sregs
[SR_ESR
] = ESR_EC_INSN_BUS
;
507 helper_raise_exception(env
, EXCP_HW_EXCP
);
510 if ((env
->pvr
.regs
[2] & PVR2_DOPB_BUS_EXC_MASK
)) {
511 env
->sregs
[SR_ESR
] = ESR_EC_DATA_BUS
;
512 helper_raise_exception(env
, EXCP_HW_EXCP
);