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/>.
24 #include "qemu/host-utils.h"
28 #if !defined(CONFIG_USER_ONLY)
29 #include "exec/softmmu_exec.h"
31 #define MMUSUFFIX _mmu
33 #include "exec/softmmu_template.h"
35 #include "exec/softmmu_template.h"
37 #include "exec/softmmu_template.h"
39 #include "exec/softmmu_template.h"
41 /* Try to fill the TLB and return an exception if error. If retaddr is
42 * NULL, it means that the function was called in C code (i.e. not
43 * from generated code or from helper.c)
45 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
50 ret
= mb_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
53 /* now we have a real cpu fault */
54 cpu_restore_state(cs
, retaddr
);
61 void helper_put(uint32_t id
, uint32_t ctrl
, uint32_t data
)
63 int test
= ctrl
& STREAM_TEST
;
64 int atomic
= ctrl
& STREAM_ATOMIC
;
65 int control
= ctrl
& STREAM_CONTROL
;
66 int nonblock
= ctrl
& STREAM_NONBLOCK
;
67 int exception
= ctrl
& STREAM_EXCEPTION
;
69 qemu_log("Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
78 uint32_t helper_get(uint32_t id
, uint32_t ctrl
)
80 int test
= ctrl
& STREAM_TEST
;
81 int atomic
= ctrl
& STREAM_ATOMIC
;
82 int control
= ctrl
& STREAM_CONTROL
;
83 int nonblock
= ctrl
& STREAM_NONBLOCK
;
84 int exception
= ctrl
& STREAM_EXCEPTION
;
86 qemu_log("Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
93 return 0xdead0000 | id
;
96 void helper_raise_exception(CPUMBState
*env
, uint32_t index
)
98 CPUState
*cs
= CPU(mb_env_get_cpu(env
));
100 cs
->exception_index
= index
;
104 void helper_debug(CPUMBState
*env
)
108 qemu_log("PC=%8.8x\n", env
->sregs
[SR_PC
]);
109 qemu_log("rmsr=%x resr=%x rear=%x debug[%x] imm=%x iflags=%x\n",
110 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
], env
->sregs
[SR_EAR
],
111 env
->debug
, env
->imm
, env
->iflags
);
112 qemu_log("btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
113 env
->btaken
, env
->btarget
,
114 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
115 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel",
116 (env
->sregs
[SR_MSR
] & MSR_EIP
),
117 (env
->sregs
[SR_MSR
] & MSR_IE
));
118 for (i
= 0; i
< 32; i
++) {
119 qemu_log("r%2.2d=%8.8x ", i
, env
->regs
[i
]);
120 if ((i
+ 1) % 4 == 0)
126 static inline uint32_t compute_carry(uint32_t a
, uint32_t b
, uint32_t cin
)
130 if ((b
== ~0) && cin
)
132 else if ((~0 - a
) < (b
+ cin
))
137 uint32_t helper_cmp(uint32_t a
, uint32_t b
)
142 if ((b
& 0x80000000) ^ (a
& 0x80000000))
143 t
= (t
& 0x7fffffff) | (b
& 0x80000000);
147 uint32_t helper_cmpu(uint32_t a
, uint32_t b
)
152 if ((b
& 0x80000000) ^ (a
& 0x80000000))
153 t
= (t
& 0x7fffffff) | (a
& 0x80000000);
157 uint32_t helper_clz(uint32_t t0
)
162 uint32_t helper_carry(uint32_t a
, uint32_t b
, uint32_t cf
)
165 ncf
= compute_carry(a
, b
, cf
);
169 static inline int div_prepare(CPUMBState
*env
, uint32_t a
, uint32_t b
)
172 env
->sregs
[SR_MSR
] |= MSR_DZ
;
174 if ((env
->sregs
[SR_MSR
] & MSR_EE
)
175 && !(env
->pvr
.regs
[2] & PVR2_DIV_ZERO_EXC_MASK
)) {
176 env
->sregs
[SR_ESR
] = ESR_EC_DIVZERO
;
177 helper_raise_exception(env
, EXCP_HW_EXCP
);
181 env
->sregs
[SR_MSR
] &= ~MSR_DZ
;
185 uint32_t helper_divs(CPUMBState
*env
, uint32_t a
, uint32_t b
)
187 if (!div_prepare(env
, a
, b
)) {
190 return (int32_t)a
/ (int32_t)b
;
193 uint32_t helper_divu(CPUMBState
*env
, uint32_t a
, uint32_t b
)
195 if (!div_prepare(env
, a
, b
)) {
201 /* raise FPU exception. */
202 static void raise_fpu_exception(CPUMBState
*env
)
204 env
->sregs
[SR_ESR
] = ESR_EC_FPU
;
205 helper_raise_exception(env
, EXCP_HW_EXCP
);
208 static void update_fpu_flags(CPUMBState
*env
, int flags
)
212 if (flags
& float_flag_invalid
) {
213 env
->sregs
[SR_FSR
] |= FSR_IO
;
216 if (flags
& float_flag_divbyzero
) {
217 env
->sregs
[SR_FSR
] |= FSR_DZ
;
220 if (flags
& float_flag_overflow
) {
221 env
->sregs
[SR_FSR
] |= FSR_OF
;
224 if (flags
& float_flag_underflow
) {
225 env
->sregs
[SR_FSR
] |= FSR_UF
;
229 && (env
->pvr
.regs
[2] & PVR2_FPU_EXC_MASK
)
230 && (env
->sregs
[SR_MSR
] & MSR_EE
)) {
231 raise_fpu_exception(env
);
235 uint32_t helper_fadd(CPUMBState
*env
, uint32_t a
, uint32_t b
)
237 CPU_FloatU fd
, fa
, fb
;
240 set_float_exception_flags(0, &env
->fp_status
);
243 fd
.f
= float32_add(fa
.f
, fb
.f
, &env
->fp_status
);
245 flags
= get_float_exception_flags(&env
->fp_status
);
246 update_fpu_flags(env
, flags
);
250 uint32_t helper_frsub(CPUMBState
*env
, uint32_t a
, uint32_t b
)
252 CPU_FloatU fd
, fa
, fb
;
255 set_float_exception_flags(0, &env
->fp_status
);
258 fd
.f
= float32_sub(fb
.f
, fa
.f
, &env
->fp_status
);
259 flags
= get_float_exception_flags(&env
->fp_status
);
260 update_fpu_flags(env
, flags
);
264 uint32_t helper_fmul(CPUMBState
*env
, uint32_t a
, uint32_t b
)
266 CPU_FloatU fd
, fa
, fb
;
269 set_float_exception_flags(0, &env
->fp_status
);
272 fd
.f
= float32_mul(fa
.f
, fb
.f
, &env
->fp_status
);
273 flags
= get_float_exception_flags(&env
->fp_status
);
274 update_fpu_flags(env
, flags
);
279 uint32_t helper_fdiv(CPUMBState
*env
, uint32_t a
, uint32_t b
)
281 CPU_FloatU fd
, fa
, fb
;
284 set_float_exception_flags(0, &env
->fp_status
);
287 fd
.f
= float32_div(fb
.f
, fa
.f
, &env
->fp_status
);
288 flags
= get_float_exception_flags(&env
->fp_status
);
289 update_fpu_flags(env
, flags
);
294 uint32_t helper_fcmp_un(CPUMBState
*env
, uint32_t a
, uint32_t b
)
302 if (float32_is_signaling_nan(fa
.f
) || float32_is_signaling_nan(fb
.f
)) {
303 update_fpu_flags(env
, float_flag_invalid
);
307 if (float32_is_quiet_nan(fa
.f
) || float32_is_quiet_nan(fb
.f
)) {
314 uint32_t helper_fcmp_lt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
320 set_float_exception_flags(0, &env
->fp_status
);
323 r
= float32_lt(fb
.f
, fa
.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_eq(CPUMBState
*env
, uint32_t a
, uint32_t b
)
336 set_float_exception_flags(0, &env
->fp_status
);
339 r
= float32_eq_quiet(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
);
346 uint32_t helper_fcmp_le(CPUMBState
*env
, uint32_t a
, uint32_t b
)
354 set_float_exception_flags(0, &env
->fp_status
);
355 r
= float32_le(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
);
363 uint32_t helper_fcmp_gt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
370 set_float_exception_flags(0, &env
->fp_status
);
371 r
= float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
372 flags
= get_float_exception_flags(&env
->fp_status
);
373 update_fpu_flags(env
, flags
& float_flag_invalid
);
377 uint32_t helper_fcmp_ne(CPUMBState
*env
, uint32_t a
, uint32_t b
)
384 set_float_exception_flags(0, &env
->fp_status
);
385 r
= !float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
386 flags
= get_float_exception_flags(&env
->fp_status
);
387 update_fpu_flags(env
, flags
& float_flag_invalid
);
392 uint32_t helper_fcmp_ge(CPUMBState
*env
, uint32_t a
, uint32_t b
)
399 set_float_exception_flags(0, &env
->fp_status
);
400 r
= !float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
401 flags
= get_float_exception_flags(&env
->fp_status
);
402 update_fpu_flags(env
, flags
& float_flag_invalid
);
407 uint32_t helper_flt(CPUMBState
*env
, uint32_t a
)
412 fd
.f
= int32_to_float32(fa
.l
, &env
->fp_status
);
416 uint32_t helper_fint(CPUMBState
*env
, uint32_t a
)
422 set_float_exception_flags(0, &env
->fp_status
);
424 r
= float32_to_int32(fa
.f
, &env
->fp_status
);
425 flags
= get_float_exception_flags(&env
->fp_status
);
426 update_fpu_flags(env
, flags
);
431 uint32_t helper_fsqrt(CPUMBState
*env
, uint32_t a
)
436 set_float_exception_flags(0, &env
->fp_status
);
438 fd
.l
= float32_sqrt(fa
.f
, &env
->fp_status
);
439 flags
= get_float_exception_flags(&env
->fp_status
);
440 update_fpu_flags(env
, flags
);
445 uint32_t helper_pcmpbf(uint32_t a
, uint32_t b
)
448 uint32_t mask
= 0xff000000;
450 for (i
= 0; i
< 4; i
++) {
451 if ((a
& mask
) == (b
& mask
))
458 void helper_memalign(CPUMBState
*env
, uint32_t addr
, uint32_t dr
, uint32_t wr
,
462 qemu_log_mask(CPU_LOG_INT
,
463 "unaligned access addr=%x mask=%x, wr=%d dr=r%d\n",
465 env
->sregs
[SR_EAR
] = addr
;
466 env
->sregs
[SR_ESR
] = ESR_EC_UNALIGNED_DATA
| (wr
<< 10) \
469 env
->sregs
[SR_ESR
] |= 1 << 11;
471 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
474 helper_raise_exception(env
, EXCP_HW_EXCP
);
478 void helper_stackprot(CPUMBState
*env
, uint32_t addr
)
480 if (addr
< env
->slr
|| addr
> env
->shr
) {
481 qemu_log("Stack protector violation at %x %x %x\n",
482 addr
, env
->slr
, env
->shr
);
483 env
->sregs
[SR_EAR
] = addr
;
484 env
->sregs
[SR_ESR
] = ESR_EC_STACKPROT
;
485 helper_raise_exception(env
, EXCP_HW_EXCP
);
489 #if !defined(CONFIG_USER_ONLY)
490 /* Writes/reads to the MMU's special regs end up here. */
491 uint32_t helper_mmu_read(CPUMBState
*env
, uint32_t rn
)
493 return mmu_read(env
, rn
);
496 void helper_mmu_write(CPUMBState
*env
, uint32_t rn
, uint32_t v
)
498 mmu_write(env
, rn
, v
);
501 void mb_cpu_unassigned_access(CPUState
*cs
, hwaddr addr
,
502 bool is_write
, bool is_exec
, int is_asi
,
508 qemu_log_mask(CPU_LOG_INT
, "Unassigned " TARGET_FMT_plx
" wr=%d exe=%d\n",
509 addr
, is_write
? 1 : 0, is_exec
? 1 : 0);
513 cpu
= MICROBLAZE_CPU(cs
);
515 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
519 env
->sregs
[SR_EAR
] = addr
;
521 if ((env
->pvr
.regs
[2] & PVR2_IOPB_BUS_EXC_MASK
)) {
522 env
->sregs
[SR_ESR
] = ESR_EC_INSN_BUS
;
523 helper_raise_exception(env
, EXCP_HW_EXCP
);
526 if ((env
->pvr
.regs
[2] & PVR2_DOPB_BUS_EXC_MASK
)) {
527 env
->sregs
[SR_ESR
] = ESR_EC_DATA_BUS
;
528 helper_raise_exception(env
, EXCP_HW_EXCP
);