2 * Microblaze helper routines.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "host-utils.h"
27 #if !defined(CONFIG_USER_ONLY)
28 #define MMUSUFFIX _mmu
30 #include "softmmu_template.h"
32 #include "softmmu_template.h"
34 #include "softmmu_template.h"
36 #include "softmmu_template.h"
38 /* Try to fill the TLB and return an exception if error. If retaddr is
39 NULL, it means that the function was called in C code (i.e. not
40 from generated code or from helper.c) */
41 /* XXX: fix it to restore all registers */
42 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
49 /* XXX: hack to restore env in all cases, even if not called from
54 ret = cpu_mb_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
57 /* now we have a real cpu fault */
58 pc = (unsigned long)retaddr;
61 /* the PC is inside the translated code. It means that we have
62 a virtual CPU fault */
63 cpu_restore_state(tb, env, pc, NULL);
72 void helper_raise_exception(uint32_t index)
74 env->exception_index = index;
78 void helper_debug(void)
82 qemu_log("PC=%8.8x\n", env->sregs[SR_PC]);
83 for (i = 0; i < 32; i++) {
84 qemu_log("r%2.2d=%8.8x ", i, env->regs[i]);
91 static inline uint32_t compute_carry(uint32_t a, uint32_t b, uint32_t cin)
97 else if ((~0 - a) < (b + cin))
102 uint32_t helper_cmp(uint32_t a, uint32_t b)
107 if ((b & 0x80000000) ^ (a & 0x80000000))
108 t = (t & 0x7fffffff) | (b & 0x80000000);
112 uint32_t helper_cmpu(uint32_t a, uint32_t b)
117 if ((b & 0x80000000) ^ (a & 0x80000000))
118 t = (t & 0x7fffffff) | (a & 0x80000000);
122 uint32_t helper_addkc(uint32_t a, uint32_t b, uint32_t k, uint32_t c)
124 uint32_t d, cf = 0, ncf;
127 cf = env->sregs[SR_MSR] >> 31;
128 assert(cf == 0 || cf == 1);
132 ncf = compute_carry(a, b, cf);
133 assert(ncf == 0 || ncf == 1);
135 env->sregs[SR_MSR] |= MSR_C | MSR_CC;
137 env->sregs[SR_MSR] &= ~(MSR_C | MSR_CC);
139 D(qemu_log("%x = %x + %x cf=%d ncf=%d k=%d c=%d\n",
140 d, a, b, cf, ncf, k, c));
144 uint32_t helper_subkc(uint32_t a, uint32_t b, uint32_t k, uint32_t c)
146 uint32_t d, cf = 1, ncf;
149 cf = env->sregs[SR_MSR] >> 31;
150 assert(cf == 0 || cf == 1);
154 ncf = compute_carry(b, ~a, cf);
155 assert(ncf == 0 || ncf == 1);
157 env->sregs[SR_MSR] |= MSR_C | MSR_CC;
159 env->sregs[SR_MSR] &= ~(MSR_C | MSR_CC);
161 D(qemu_log("%x = %x + %x cf=%d ncf=%d k=%d c=%d\n",
162 d, a, b, cf, ncf, k, c));
166 static inline int div_prepare(uint32_t a, uint32_t b)
169 env->sregs[SR_MSR] |= MSR_DZ;
171 if ((env->sregs[SR_MSR] & MSR_EE)
172 && !(env->pvr.regs[2] & PVR2_DIV_ZERO_EXC_MASK)) {
173 env->sregs[SR_ESR] = ESR_EC_DIVZERO;
174 helper_raise_exception(EXCP_HW_EXCP);
178 env->sregs[SR_MSR] &= ~MSR_DZ;
182 uint32_t helper_divs(uint32_t a, uint32_t b)
184 if (!div_prepare(a, b))
186 return (int32_t)a / (int32_t)b;
189 uint32_t helper_divu(uint32_t a, uint32_t b)
191 if (!div_prepare(a, b))
196 uint32_t helper_pcmpbf(uint32_t a, uint32_t b)
199 uint32_t mask = 0xff000000;
201 for (i = 0; i < 4; i++) {
202 if ((a & mask) == (b & mask))
209 void helper_memalign(uint32_t addr, uint32_t dr, uint32_t wr, uint32_t mask)
212 qemu_log_mask(CPU_LOG_INT,
213 "unaligned access addr=%x mask=%x, wr=%d dr=r%d\n",
215 env->sregs[SR_EAR] = addr;
216 env->sregs[SR_ESR] = ESR_EC_UNALIGNED_DATA | (wr << 10) \
219 env->sregs[SR_ESR] |= 1 << 11;
221 if (!(env->sregs[SR_MSR] & MSR_EE)) {
224 helper_raise_exception(EXCP_HW_EXCP);
228 #if !defined(CONFIG_USER_ONLY)
229 /* Writes/reads to the MMU's special regs end up here. */
230 uint32_t helper_mmu_read(uint32_t rn)
232 return mmu_read(env, rn);
235 void helper_mmu_write(uint32_t rn, uint32_t v)
237 mmu_write(env, rn, v);
241 void do_unassigned_access(a_target_phys_addr addr, int is_write, int is_exec,
242 int is_asi, int size)
245 /* XXX: hack to restore env in all cases, even if not called from
248 env = cpu_single_env;
249 qemu_log_mask(CPU_LOG_INT, "Unassigned " TARGET_FMT_plx " wr=%d exe=%d\n",
250 addr, is_write, is_exec);
251 if (!(env->sregs[SR_MSR] & MSR_EE)) {
255 env->sregs[SR_EAR] = addr;
257 if ((env->pvr.regs[2] & PVR2_IOPB_BUS_EXC_MASK)) {
258 env->sregs[SR_ESR] = ESR_EC_INSN_BUS;
259 helper_raise_exception(EXCP_HW_EXCP);
262 if ((env->pvr.regs[2] & PVR2_DOPB_BUS_EXC_MASK)) {
263 env->sregs[SR_ESR] = ESR_EC_DATA_BUS;
264 helper_raise_exception(EXCP_HW_EXCP);