Interrupt io thread in qemu_set_fd_handler2
[qemu-kvm/fedora.git] / target-cris / op_helper.c
blob7c629c7559574dc62df8e033ec7fda21ff2adee3
1 /*
2 * CRIS helper routines
4 * Copyright (c) 2007 AXIS Communications
5 * Written by Edgar E. Iglesias
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, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <assert.h>
23 #include "exec.h"
24 #include "mmu.h"
26 #define MMUSUFFIX _mmu
27 #ifdef __s390__
28 # define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
29 #else
30 # define GETPC() (__builtin_return_address(0))
31 #endif
33 #define SHIFT 0
34 #include "softmmu_template.h"
36 #define SHIFT 1
37 #include "softmmu_template.h"
39 #define SHIFT 2
40 #include "softmmu_template.h"
42 #define SHIFT 3
43 #include "softmmu_template.h"
45 #define D(x)
47 /* Try to fill the TLB and return an exception if error. If retaddr is
48 NULL, it means that the function was called in C code (i.e. not
49 from generated code or from helper.c) */
50 /* XXX: fix it to restore all registers */
51 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
53 TranslationBlock *tb;
54 CPUState *saved_env;
55 unsigned long pc;
56 int ret;
58 /* XXX: hack to restore env in all cases, even if not called from
59 generated code */
60 saved_env = env;
61 env = cpu_single_env;
63 D(fprintf(logfile, "%s ra=%x acr=%x %x\n", __func__, retaddr,
64 env->regs[R_ACR], saved_env->regs[R_ACR]));
65 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
66 if (__builtin_expect(ret, 0)) {
67 if (retaddr) {
68 /* now we have a real cpu fault */
69 pc = (unsigned long)retaddr;
70 tb = tb_find_pc(pc);
71 if (tb) {
72 /* the PC is inside the translated code. It means that we have
73 a virtual CPU fault */
74 cpu_restore_state(tb, env, pc, NULL);
77 cpu_loop_exit();
79 env = saved_env;
82 void helper_tlb_update(uint32_t T0)
84 #if !defined(CONFIG_USER_ONLY)
85 uint32_t vaddr;
86 uint32_t srs = env->pregs[PR_SRS];
88 if (srs != 1 && srs != 2)
89 return;
91 vaddr = cris_mmu_tlb_latest_update(env, T0);
92 D(printf("flush old_vaddr=%x vaddr=%x T0=%x\n", vaddr,
93 env->sregs[SFR_R_MM_CAUSE] & TARGET_PAGE_MASK, T0));
94 tlb_flush_page(env, vaddr);
95 #endif
98 void helper_tlb_flush(void)
100 tlb_flush(env, 1);
103 void helper_dump(uint32_t a0, uint32_t a1)
105 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
108 void helper_dummy(void)
113 /* Only used for debugging at the moment. */
114 void helper_rfe(void)
116 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
117 env->pregs[PR_ERP], env->pregs[PR_PID],
118 env->pregs[PR_CCS],
119 env->btarget));
122 void helper_store(uint32_t a0)
124 if (env->pregs[PR_CCS] & P_FLAG )
126 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
127 env->pc, a0);
131 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
132 int is_asi)
134 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
135 __func__, addr, is_write, is_exec, is_asi));
138 static void evaluate_flags_writeback(uint32_t flags)
140 int x;
142 /* Extended arithmetics, leave the z flag alone. */
143 env->debug3 = env->pregs[PR_CCS];
145 if (env->cc_x_live)
146 x = env->cc_x;
147 else
148 x = env->pregs[PR_CCS] & X_FLAG;
150 if ((x || env->cc_op == CC_OP_ADDC)
151 && flags & Z_FLAG)
152 env->cc_mask &= ~Z_FLAG;
154 /* all insn clear the x-flag except setf or clrf. */
155 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
156 flags &= env->cc_mask;
157 env->pregs[PR_CCS] |= flags;
158 RETURN();
161 void helper_evaluate_flags_muls(void)
163 uint32_t src;
164 uint32_t dst;
165 uint32_t res;
166 uint32_t flags = 0;
167 /* were gonna have to redo the muls. */
168 int64_t tmp, t0 ,t1;
169 int32_t mof;
170 int dneg;
172 src = env->cc_src;
173 dst = env->cc_dest;
174 res = env->cc_result;
177 /* cast into signed values to make GCC sign extend. */
178 t0 = (int32_t)src;
179 t1 = (int32_t)dst;
180 dneg = ((int32_t)res) < 0;
182 tmp = t0 * t1;
183 mof = tmp >> 32;
184 if (tmp == 0)
185 flags |= Z_FLAG;
186 else if (tmp < 0)
187 flags |= N_FLAG;
188 if ((dneg && mof != -1)
189 || (!dneg && mof != 0))
190 flags |= V_FLAG;
191 evaluate_flags_writeback(flags);
194 void helper_evaluate_flags_mulu(void)
196 uint32_t src;
197 uint32_t dst;
198 uint32_t res;
199 uint32_t flags = 0;
200 /* were gonna have to redo the muls. */
201 uint64_t tmp, t0 ,t1;
202 uint32_t mof;
204 src = env->cc_src;
205 dst = env->cc_dest;
206 res = env->cc_result;
209 /* cast into signed values to make GCC sign extend. */
210 t0 = src;
211 t1 = dst;
213 tmp = t0 * t1;
214 mof = tmp >> 32;
215 if (tmp == 0)
216 flags |= Z_FLAG;
217 else if (tmp >> 63)
218 flags |= N_FLAG;
219 if (mof)
220 flags |= V_FLAG;
222 evaluate_flags_writeback(flags);
225 void helper_evaluate_flags_mcp(void)
227 uint32_t src;
228 uint32_t dst;
229 uint32_t res;
230 uint32_t flags = 0;
232 src = env->cc_src;
233 dst = env->cc_dest;
234 res = env->cc_result;
236 if ((res & 0x80000000L) != 0L)
238 flags |= N_FLAG;
239 if (((src & 0x80000000L) == 0L)
240 && ((dst & 0x80000000L) == 0L))
242 flags |= V_FLAG;
244 else if (((src & 0x80000000L) != 0L) &&
245 ((dst & 0x80000000L) != 0L))
247 flags |= R_FLAG;
250 else
252 if (res == 0L)
253 flags |= Z_FLAG;
254 if (((src & 0x80000000L) != 0L)
255 && ((dst & 0x80000000L) != 0L))
256 flags |= V_FLAG;
257 if ((dst & 0x80000000L) != 0L
258 || (src & 0x80000000L) != 0L)
259 flags |= R_FLAG;
262 evaluate_flags_writeback(flags);
265 void helper_evaluate_flags_alu_4(void)
267 uint32_t src;
268 uint32_t dst;
269 uint32_t res;
270 uint32_t flags = 0;
272 src = env->cc_src;
273 dst = env->cc_dest;
274 res = env->cc_result;
276 if ((res & 0x80000000L) != 0L)
278 flags |= N_FLAG;
279 if (((src & 0x80000000L) == 0L)
280 && ((dst & 0x80000000L) == 0L))
282 flags |= V_FLAG;
284 else if (((src & 0x80000000L) != 0L) &&
285 ((dst & 0x80000000L) != 0L))
287 flags |= C_FLAG;
290 else
292 if (res == 0L)
293 flags |= Z_FLAG;
294 if (((src & 0x80000000L) != 0L)
295 && ((dst & 0x80000000L) != 0L))
296 flags |= V_FLAG;
297 if ((dst & 0x80000000L) != 0L
298 || (src & 0x80000000L) != 0L)
299 flags |= C_FLAG;
302 if (env->cc_op == CC_OP_SUB
303 || env->cc_op == CC_OP_CMP) {
304 flags ^= C_FLAG;
306 evaluate_flags_writeback(flags);
309 void helper_evaluate_flags_move_4 (void)
311 uint32_t src;
312 uint32_t res;
313 uint32_t flags = 0;
315 src = env->cc_src;
316 res = env->cc_result;
318 if ((int32_t)res < 0)
319 flags |= N_FLAG;
320 else if (res == 0L)
321 flags |= Z_FLAG;
323 evaluate_flags_writeback(flags);
325 void helper_evaluate_flags_move_2 (void)
327 uint32_t src;
328 uint32_t flags = 0;
329 uint16_t res;
331 src = env->cc_src;
332 res = env->cc_result;
334 if ((int16_t)res < 0L)
335 flags |= N_FLAG;
336 else if (res == 0)
337 flags |= Z_FLAG;
339 evaluate_flags_writeback(flags);
342 /* TODO: This is expensive. We could split things up and only evaluate part of
343 CCR on a need to know basis. For now, we simply re-evaluate everything. */
344 void helper_evaluate_flags (void)
346 uint32_t src;
347 uint32_t dst;
348 uint32_t res;
349 uint32_t flags = 0;
351 src = env->cc_src;
352 dst = env->cc_dest;
353 res = env->cc_result;
356 /* Now, evaluate the flags. This stuff is based on
357 Per Zander's CRISv10 simulator. */
358 switch (env->cc_size)
360 case 1:
361 if ((res & 0x80L) != 0L)
363 flags |= N_FLAG;
364 if (((src & 0x80L) == 0L)
365 && ((dst & 0x80L) == 0L))
367 flags |= V_FLAG;
369 else if (((src & 0x80L) != 0L)
370 && ((dst & 0x80L) != 0L))
372 flags |= C_FLAG;
375 else
377 if ((res & 0xFFL) == 0L)
379 flags |= Z_FLAG;
381 if (((src & 0x80L) != 0L)
382 && ((dst & 0x80L) != 0L))
384 flags |= V_FLAG;
386 if ((dst & 0x80L) != 0L
387 || (src & 0x80L) != 0L)
389 flags |= C_FLAG;
392 break;
393 case 2:
394 if ((res & 0x8000L) != 0L)
396 flags |= N_FLAG;
397 if (((src & 0x8000L) == 0L)
398 && ((dst & 0x8000L) == 0L))
400 flags |= V_FLAG;
402 else if (((src & 0x8000L) != 0L)
403 && ((dst & 0x8000L) != 0L))
405 flags |= C_FLAG;
408 else
410 if ((res & 0xFFFFL) == 0L)
412 flags |= Z_FLAG;
414 if (((src & 0x8000L) != 0L)
415 && ((dst & 0x8000L) != 0L))
417 flags |= V_FLAG;
419 if ((dst & 0x8000L) != 0L
420 || (src & 0x8000L) != 0L)
422 flags |= C_FLAG;
425 break;
426 case 4:
427 if ((res & 0x80000000L) != 0L)
429 flags |= N_FLAG;
430 if (((src & 0x80000000L) == 0L)
431 && ((dst & 0x80000000L) == 0L))
433 flags |= V_FLAG;
435 else if (((src & 0x80000000L) != 0L) &&
436 ((dst & 0x80000000L) != 0L))
438 flags |= C_FLAG;
441 else
443 if (res == 0L)
444 flags |= Z_FLAG;
445 if (((src & 0x80000000L) != 0L)
446 && ((dst & 0x80000000L) != 0L))
447 flags |= V_FLAG;
448 if ((dst & 0x80000000L) != 0L
449 || (src & 0x80000000L) != 0L)
450 flags |= C_FLAG;
452 break;
453 default:
454 break;
457 if (env->cc_op == CC_OP_SUB
458 || env->cc_op == CC_OP_CMP) {
459 flags ^= C_FLAG;
461 evaluate_flags_writeback(flags);