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 "host-utils.h"
28 #if !defined(CONFIG_USER_ONLY)
29 #include "softmmu_exec.h"
31 #define MMUSUFFIX _mmu
33 #include "softmmu_template.h"
35 #include "softmmu_template.h"
37 #include "softmmu_template.h"
39 #include "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) */
44 void tlb_fill(CPUMBState
*env
, target_ulong addr
, int is_write
, int mmu_idx
,
50 ret
= cpu_mb_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
53 /* now we have a real cpu fault */
54 tb
= tb_find_pc(retaddr
);
56 /* the PC is inside the translated code. It means that we have
57 a virtual CPU fault */
58 cpu_restore_state(tb
, env
, retaddr
);
66 void helper_put(uint32_t id
, uint32_t ctrl
, uint32_t data
)
68 int test
= ctrl
& STREAM_TEST
;
69 int atomic
= ctrl
& STREAM_ATOMIC
;
70 int control
= ctrl
& STREAM_CONTROL
;
71 int nonblock
= ctrl
& STREAM_NONBLOCK
;
72 int exception
= ctrl
& STREAM_EXCEPTION
;
74 qemu_log("Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
83 uint32_t helper_get(uint32_t id
, uint32_t ctrl
)
85 int test
= ctrl
& STREAM_TEST
;
86 int atomic
= ctrl
& STREAM_ATOMIC
;
87 int control
= ctrl
& STREAM_CONTROL
;
88 int nonblock
= ctrl
& STREAM_NONBLOCK
;
89 int exception
= ctrl
& STREAM_EXCEPTION
;
91 qemu_log("Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
98 return 0xdead0000 | id
;
101 void helper_raise_exception(CPUMBState
*env
, uint32_t index
)
103 env
->exception_index
= index
;
107 void helper_debug(CPUMBState
*env
)
111 qemu_log("PC=%8.8x\n", env
->sregs
[SR_PC
]);
112 qemu_log("rmsr=%x resr=%x rear=%x debug[%x] imm=%x iflags=%x\n",
113 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
], env
->sregs
[SR_EAR
],
114 env
->debug
, env
->imm
, env
->iflags
);
115 qemu_log("btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
116 env
->btaken
, env
->btarget
,
117 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
118 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel",
119 (env
->sregs
[SR_MSR
] & MSR_EIP
),
120 (env
->sregs
[SR_MSR
] & MSR_IE
));
121 for (i
= 0; i
< 32; i
++) {
122 qemu_log("r%2.2d=%8.8x ", i
, env
->regs
[i
]);
123 if ((i
+ 1) % 4 == 0)
129 static inline uint32_t compute_carry(uint32_t a
, uint32_t b
, uint32_t cin
)
133 if ((b
== ~0) && cin
)
135 else if ((~0 - a
) < (b
+ cin
))
140 uint32_t helper_cmp(uint32_t a
, uint32_t b
)
145 if ((b
& 0x80000000) ^ (a
& 0x80000000))
146 t
= (t
& 0x7fffffff) | (b
& 0x80000000);
150 uint32_t helper_cmpu(uint32_t a
, uint32_t b
)
155 if ((b
& 0x80000000) ^ (a
& 0x80000000))
156 t
= (t
& 0x7fffffff) | (a
& 0x80000000);
160 uint32_t helper_clz(uint32_t t0
)
165 uint32_t helper_carry(uint32_t a
, uint32_t b
, uint32_t cf
)
168 ncf
= compute_carry(a
, b
, cf
);
172 static inline int div_prepare(CPUMBState
*env
, uint32_t a
, uint32_t b
)
175 env
->sregs
[SR_MSR
] |= MSR_DZ
;
177 if ((env
->sregs
[SR_MSR
] & MSR_EE
)
178 && !(env
->pvr
.regs
[2] & PVR2_DIV_ZERO_EXC_MASK
)) {
179 env
->sregs
[SR_ESR
] = ESR_EC_DIVZERO
;
180 helper_raise_exception(env
, EXCP_HW_EXCP
);
184 env
->sregs
[SR_MSR
] &= ~MSR_DZ
;
188 uint32_t helper_divs(CPUMBState
*env
, uint32_t a
, uint32_t b
)
190 if (!div_prepare(env
, a
, b
)) {
193 return (int32_t)a
/ (int32_t)b
;
196 uint32_t helper_divu(CPUMBState
*env
, uint32_t a
, uint32_t b
)
198 if (!div_prepare(env
, a
, b
)) {
204 /* raise FPU exception. */
205 static void raise_fpu_exception(CPUMBState
*env
)
207 env
->sregs
[SR_ESR
] = ESR_EC_FPU
;
208 helper_raise_exception(env
, EXCP_HW_EXCP
);
211 static void update_fpu_flags(CPUMBState
*env
, int flags
)
215 if (flags
& float_flag_invalid
) {
216 env
->sregs
[SR_FSR
] |= FSR_IO
;
219 if (flags
& float_flag_divbyzero
) {
220 env
->sregs
[SR_FSR
] |= FSR_DZ
;
223 if (flags
& float_flag_overflow
) {
224 env
->sregs
[SR_FSR
] |= FSR_OF
;
227 if (flags
& float_flag_underflow
) {
228 env
->sregs
[SR_FSR
] |= FSR_UF
;
232 && (env
->pvr
.regs
[2] & PVR2_FPU_EXC_MASK
)
233 && (env
->sregs
[SR_MSR
] & MSR_EE
)) {
234 raise_fpu_exception(env
);
238 uint32_t helper_fadd(CPUMBState
*env
, uint32_t a
, uint32_t b
)
240 CPU_FloatU fd
, fa
, fb
;
243 set_float_exception_flags(0, &env
->fp_status
);
246 fd
.f
= float32_add(fa
.f
, fb
.f
, &env
->fp_status
);
248 flags
= get_float_exception_flags(&env
->fp_status
);
249 update_fpu_flags(env
, flags
);
253 uint32_t helper_frsub(CPUMBState
*env
, uint32_t a
, uint32_t b
)
255 CPU_FloatU fd
, fa
, fb
;
258 set_float_exception_flags(0, &env
->fp_status
);
261 fd
.f
= float32_sub(fb
.f
, fa
.f
, &env
->fp_status
);
262 flags
= get_float_exception_flags(&env
->fp_status
);
263 update_fpu_flags(env
, flags
);
267 uint32_t helper_fmul(CPUMBState
*env
, uint32_t a
, uint32_t b
)
269 CPU_FloatU fd
, fa
, fb
;
272 set_float_exception_flags(0, &env
->fp_status
);
275 fd
.f
= float32_mul(fa
.f
, fb
.f
, &env
->fp_status
);
276 flags
= get_float_exception_flags(&env
->fp_status
);
277 update_fpu_flags(env
, flags
);
282 uint32_t helper_fdiv(CPUMBState
*env
, uint32_t a
, uint32_t b
)
284 CPU_FloatU fd
, fa
, fb
;
287 set_float_exception_flags(0, &env
->fp_status
);
290 fd
.f
= float32_div(fb
.f
, fa
.f
, &env
->fp_status
);
291 flags
= get_float_exception_flags(&env
->fp_status
);
292 update_fpu_flags(env
, flags
);
297 uint32_t helper_fcmp_un(CPUMBState
*env
, uint32_t a
, uint32_t b
)
305 if (float32_is_signaling_nan(fa
.f
) || float32_is_signaling_nan(fb
.f
)) {
306 update_fpu_flags(env
, float_flag_invalid
);
310 if (float32_is_quiet_nan(fa
.f
) || float32_is_quiet_nan(fb
.f
)) {
317 uint32_t helper_fcmp_lt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
323 set_float_exception_flags(0, &env
->fp_status
);
326 r
= float32_lt(fb
.f
, fa
.f
, &env
->fp_status
);
327 flags
= get_float_exception_flags(&env
->fp_status
);
328 update_fpu_flags(env
, flags
& float_flag_invalid
);
333 uint32_t helper_fcmp_eq(CPUMBState
*env
, uint32_t a
, uint32_t b
)
339 set_float_exception_flags(0, &env
->fp_status
);
342 r
= float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
343 flags
= get_float_exception_flags(&env
->fp_status
);
344 update_fpu_flags(env
, flags
& float_flag_invalid
);
349 uint32_t helper_fcmp_le(CPUMBState
*env
, uint32_t a
, uint32_t b
)
357 set_float_exception_flags(0, &env
->fp_status
);
358 r
= float32_le(fa
.f
, fb
.f
, &env
->fp_status
);
359 flags
= get_float_exception_flags(&env
->fp_status
);
360 update_fpu_flags(env
, flags
& float_flag_invalid
);
366 uint32_t helper_fcmp_gt(CPUMBState
*env
, uint32_t a
, uint32_t b
)
373 set_float_exception_flags(0, &env
->fp_status
);
374 r
= float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
375 flags
= get_float_exception_flags(&env
->fp_status
);
376 update_fpu_flags(env
, flags
& float_flag_invalid
);
380 uint32_t helper_fcmp_ne(CPUMBState
*env
, uint32_t a
, uint32_t b
)
387 set_float_exception_flags(0, &env
->fp_status
);
388 r
= !float32_eq_quiet(fa
.f
, fb
.f
, &env
->fp_status
);
389 flags
= get_float_exception_flags(&env
->fp_status
);
390 update_fpu_flags(env
, flags
& float_flag_invalid
);
395 uint32_t helper_fcmp_ge(CPUMBState
*env
, uint32_t a
, uint32_t b
)
402 set_float_exception_flags(0, &env
->fp_status
);
403 r
= !float32_lt(fa
.f
, fb
.f
, &env
->fp_status
);
404 flags
= get_float_exception_flags(&env
->fp_status
);
405 update_fpu_flags(env
, flags
& float_flag_invalid
);
410 uint32_t helper_flt(CPUMBState
*env
, uint32_t a
)
415 fd
.f
= int32_to_float32(fa
.l
, &env
->fp_status
);
419 uint32_t helper_fint(CPUMBState
*env
, uint32_t a
)
425 set_float_exception_flags(0, &env
->fp_status
);
427 r
= float32_to_int32(fa
.f
, &env
->fp_status
);
428 flags
= get_float_exception_flags(&env
->fp_status
);
429 update_fpu_flags(env
, flags
);
434 uint32_t helper_fsqrt(CPUMBState
*env
, uint32_t a
)
439 set_float_exception_flags(0, &env
->fp_status
);
441 fd
.l
= float32_sqrt(fa
.f
, &env
->fp_status
);
442 flags
= get_float_exception_flags(&env
->fp_status
);
443 update_fpu_flags(env
, flags
);
448 uint32_t helper_pcmpbf(uint32_t a
, uint32_t b
)
451 uint32_t mask
= 0xff000000;
453 for (i
= 0; i
< 4; i
++) {
454 if ((a
& mask
) == (b
& mask
))
461 void helper_memalign(CPUMBState
*env
, uint32_t addr
, uint32_t dr
, uint32_t wr
,
465 qemu_log_mask(CPU_LOG_INT
,
466 "unaligned access addr=%x mask=%x, wr=%d dr=r%d\n",
468 env
->sregs
[SR_EAR
] = addr
;
469 env
->sregs
[SR_ESR
] = ESR_EC_UNALIGNED_DATA
| (wr
<< 10) \
472 env
->sregs
[SR_ESR
] |= 1 << 11;
474 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
477 helper_raise_exception(env
, EXCP_HW_EXCP
);
481 void helper_stackprot(CPUMBState
*env
, uint32_t addr
)
483 if (addr
< env
->slr
|| addr
> env
->shr
) {
484 qemu_log("Stack protector violation at %x %x %x\n",
485 addr
, env
->slr
, env
->shr
);
486 env
->sregs
[SR_EAR
] = addr
;
487 env
->sregs
[SR_ESR
] = ESR_EC_STACKPROT
;
488 helper_raise_exception(env
, EXCP_HW_EXCP
);
492 #if !defined(CONFIG_USER_ONLY)
493 /* Writes/reads to the MMU's special regs end up here. */
494 uint32_t helper_mmu_read(CPUMBState
*env
, uint32_t rn
)
496 return mmu_read(env
, rn
);
499 void helper_mmu_write(CPUMBState
*env
, uint32_t rn
, uint32_t v
)
501 mmu_write(env
, rn
, v
);
504 void cpu_unassigned_access(CPUMBState
*env
, hwaddr addr
,
505 int is_write
, int is_exec
, int is_asi
, int size
)
507 qemu_log_mask(CPU_LOG_INT
, "Unassigned " TARGET_FMT_plx
" wr=%d exe=%d\n",
508 addr
, is_write
, is_exec
);
509 if (!(env
->sregs
[SR_MSR
] & MSR_EE
)) {
513 env
->sregs
[SR_EAR
] = addr
;
515 if ((env
->pvr
.regs
[2] & PVR2_IOPB_BUS_EXC_MASK
)) {
516 env
->sregs
[SR_ESR
] = ESR_EC_INSN_BUS
;
517 helper_raise_exception(env
, EXCP_HW_EXCP
);
520 if ((env
->pvr
.regs
[2] & PVR2_DOPB_BUS_EXC_MASK
)) {
521 env
->sregs
[SR_ESR
] = ESR_EC_DATA_BUS
;
522 helper_raise_exception(env
, EXCP_HW_EXCP
);