vmstate: add VMS_MUST_EXIST
[qemu/ar7.git] / target-microblaze / op_helper.c
blobf8fb7f91691887ea4d82eb4b7898ddabe1208085
1 /*
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 <assert.h>
22 #include "cpu.h"
23 #include "helper.h"
24 #include "qemu/host-utils.h"
26 #define D(x)
28 #if !defined(CONFIG_USER_ONLY)
29 #include "exec/softmmu_exec.h"
31 #define MMUSUFFIX _mmu
32 #define SHIFT 0
33 #include "exec/softmmu_template.h"
34 #define SHIFT 1
35 #include "exec/softmmu_template.h"
36 #define SHIFT 2
37 #include "exec/softmmu_template.h"
38 #define SHIFT 3
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,
46 uintptr_t retaddr)
48 int ret;
50 ret = mb_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
51 if (unlikely(ret)) {
52 if (retaddr) {
53 /* now we have a real cpu fault */
54 cpu_restore_state(cs, retaddr);
56 cpu_loop_exit(cs);
59 #endif
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",
70 id, data,
71 test ? "t" : "",
72 nonblock ? "n" : "",
73 exception ? "e" : "",
74 control ? "c" : "",
75 atomic ? "a" : "");
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",
87 id,
88 test ? "t" : "",
89 nonblock ? "n" : "",
90 exception ? "e" : "",
91 control ? "c" : "",
92 atomic ? "a" : "");
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;
101 cpu_loop_exit(cs);
104 void helper_debug(CPUMBState *env)
106 int i;
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)
121 qemu_log("\n");
123 qemu_log("\n\n");
126 static inline uint32_t compute_carry(uint32_t a, uint32_t b, uint32_t cin)
128 uint32_t cout = 0;
130 if ((b == ~0) && cin)
131 cout = 1;
132 else if ((~0 - a) < (b + cin))
133 cout = 1;
134 return cout;
137 uint32_t helper_cmp(uint32_t a, uint32_t b)
139 uint32_t t;
141 t = b + ~a + 1;
142 if ((b & 0x80000000) ^ (a & 0x80000000))
143 t = (t & 0x7fffffff) | (b & 0x80000000);
144 return t;
147 uint32_t helper_cmpu(uint32_t a, uint32_t b)
149 uint32_t t;
151 t = b + ~a + 1;
152 if ((b & 0x80000000) ^ (a & 0x80000000))
153 t = (t & 0x7fffffff) | (a & 0x80000000);
154 return t;
157 uint32_t helper_clz(uint32_t t0)
159 return clz32(t0);
162 uint32_t helper_carry(uint32_t a, uint32_t b, uint32_t cf)
164 uint32_t ncf;
165 ncf = compute_carry(a, b, cf);
166 return ncf;
169 static inline int div_prepare(CPUMBState *env, uint32_t a, uint32_t b)
171 if (b == 0) {
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);
179 return 0;
181 env->sregs[SR_MSR] &= ~MSR_DZ;
182 return 1;
185 uint32_t helper_divs(CPUMBState *env, uint32_t a, uint32_t b)
187 if (!div_prepare(env, a, b)) {
188 return 0;
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)) {
196 return 0;
198 return 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)
210 int raise = 0;
212 if (flags & float_flag_invalid) {
213 env->sregs[SR_FSR] |= FSR_IO;
214 raise = 1;
216 if (flags & float_flag_divbyzero) {
217 env->sregs[SR_FSR] |= FSR_DZ;
218 raise = 1;
220 if (flags & float_flag_overflow) {
221 env->sregs[SR_FSR] |= FSR_OF;
222 raise = 1;
224 if (flags & float_flag_underflow) {
225 env->sregs[SR_FSR] |= FSR_UF;
226 raise = 1;
228 if (raise
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;
238 int flags;
240 set_float_exception_flags(0, &env->fp_status);
241 fa.l = a;
242 fb.l = b;
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);
247 return fd.l;
250 uint32_t helper_frsub(CPUMBState *env, uint32_t a, uint32_t b)
252 CPU_FloatU fd, fa, fb;
253 int flags;
255 set_float_exception_flags(0, &env->fp_status);
256 fa.l = a;
257 fb.l = b;
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);
261 return fd.l;
264 uint32_t helper_fmul(CPUMBState *env, uint32_t a, uint32_t b)
266 CPU_FloatU fd, fa, fb;
267 int flags;
269 set_float_exception_flags(0, &env->fp_status);
270 fa.l = a;
271 fb.l = b;
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);
276 return fd.l;
279 uint32_t helper_fdiv(CPUMBState *env, uint32_t a, uint32_t b)
281 CPU_FloatU fd, fa, fb;
282 int flags;
284 set_float_exception_flags(0, &env->fp_status);
285 fa.l = a;
286 fb.l = b;
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);
291 return fd.l;
294 uint32_t helper_fcmp_un(CPUMBState *env, uint32_t a, uint32_t b)
296 CPU_FloatU fa, fb;
297 uint32_t r = 0;
299 fa.l = a;
300 fb.l = b;
302 if (float32_is_signaling_nan(fa.f) || float32_is_signaling_nan(fb.f)) {
303 update_fpu_flags(env, float_flag_invalid);
304 r = 1;
307 if (float32_is_quiet_nan(fa.f) || float32_is_quiet_nan(fb.f)) {
308 r = 1;
311 return r;
314 uint32_t helper_fcmp_lt(CPUMBState *env, uint32_t a, uint32_t b)
316 CPU_FloatU fa, fb;
317 int r;
318 int flags;
320 set_float_exception_flags(0, &env->fp_status);
321 fa.l = a;
322 fb.l = b;
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);
327 return r;
330 uint32_t helper_fcmp_eq(CPUMBState *env, uint32_t a, uint32_t b)
332 CPU_FloatU fa, fb;
333 int flags;
334 int r;
336 set_float_exception_flags(0, &env->fp_status);
337 fa.l = a;
338 fb.l = b;
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);
343 return r;
346 uint32_t helper_fcmp_le(CPUMBState *env, uint32_t a, uint32_t b)
348 CPU_FloatU fa, fb;
349 int flags;
350 int r;
352 fa.l = a;
353 fb.l = 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);
360 return r;
363 uint32_t helper_fcmp_gt(CPUMBState *env, uint32_t a, uint32_t b)
365 CPU_FloatU fa, fb;
366 int flags, r;
368 fa.l = a;
369 fb.l = 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);
374 return r;
377 uint32_t helper_fcmp_ne(CPUMBState *env, uint32_t a, uint32_t b)
379 CPU_FloatU fa, fb;
380 int flags, r;
382 fa.l = a;
383 fb.l = 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);
389 return r;
392 uint32_t helper_fcmp_ge(CPUMBState *env, uint32_t a, uint32_t b)
394 CPU_FloatU fa, fb;
395 int flags, r;
397 fa.l = a;
398 fb.l = 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);
404 return r;
407 uint32_t helper_flt(CPUMBState *env, uint32_t a)
409 CPU_FloatU fd, fa;
411 fa.l = a;
412 fd.f = int32_to_float32(fa.l, &env->fp_status);
413 return fd.l;
416 uint32_t helper_fint(CPUMBState *env, uint32_t a)
418 CPU_FloatU fa;
419 uint32_t r;
420 int flags;
422 set_float_exception_flags(0, &env->fp_status);
423 fa.l = a;
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);
428 return r;
431 uint32_t helper_fsqrt(CPUMBState *env, uint32_t a)
433 CPU_FloatU fd, fa;
434 int flags;
436 set_float_exception_flags(0, &env->fp_status);
437 fa.l = a;
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);
442 return fd.l;
445 uint32_t helper_pcmpbf(uint32_t a, uint32_t b)
447 unsigned int i;
448 uint32_t mask = 0xff000000;
450 for (i = 0; i < 4; i++) {
451 if ((a & mask) == (b & mask))
452 return i + 1;
453 mask >>= 8;
455 return 0;
458 void helper_memalign(CPUMBState *env, uint32_t addr, uint32_t dr, uint32_t wr,
459 uint32_t mask)
461 if (addr & mask) {
462 qemu_log_mask(CPU_LOG_INT,
463 "unaligned access addr=%x mask=%x, wr=%d dr=r%d\n",
464 addr, mask, wr, dr);
465 env->sregs[SR_EAR] = addr;
466 env->sregs[SR_ESR] = ESR_EC_UNALIGNED_DATA | (wr << 10) \
467 | (dr & 31) << 5;
468 if (mask == 3) {
469 env->sregs[SR_ESR] |= 1 << 11;
471 if (!(env->sregs[SR_MSR] & MSR_EE)) {
472 return;
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,
503 unsigned size)
505 MicroBlazeCPU *cpu;
506 CPUMBState *env;
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);
510 if (cs == NULL) {
511 return;
513 cpu = MICROBLAZE_CPU(cs);
514 env = &cpu->env;
515 if (!(env->sregs[SR_MSR] & MSR_EE)) {
516 return;
519 env->sregs[SR_EAR] = addr;
520 if (is_exec) {
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);
525 } else {
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);
532 #endif