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
29 #if !defined(CONFIG_USER_ONLY)
31 #define MMUSUFFIX _mmu
34 #include "softmmu_template.h"
37 #include "softmmu_template.h"
40 #include "softmmu_template.h"
43 #include "softmmu_template.h"
45 /* Try to fill the TLB and return an exception if error. If retaddr is
46 NULL, it means that the function was called in C code (i.e. not
47 from generated code or from helper.c) */
48 /* XXX: fix it to restore all registers */
49 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
56 /* XXX: hack to restore env in all cases, even if not called from
61 D(fprintf(logfile
, "%s pc=%x tpc=%x ra=%x\n", __func__
,
62 env
->pc
, env
->debug1
, retaddr
));
63 ret
= cpu_cris_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
66 /* now we have a real cpu fault */
67 pc
= (unsigned long)retaddr
;
70 /* the PC is inside the translated code. It means that we have
71 a virtual CPU fault */
72 cpu_restore_state(tb
, env
, pc
, NULL
);
74 /* Evaluate flags after retranslation. */
75 helper_top_evaluate_flags();
85 void helper_raise_exception(uint32_t index
)
87 env
->exception_index
= index
;
91 void helper_tlb_flush_pid(uint32_t pid
)
93 #if !defined(CONFIG_USER_ONLY)
95 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
96 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
100 void helper_spc_write(uint32_t new_spc
)
102 #if !defined(CONFIG_USER_ONLY)
103 tlb_flush_page(env
, env
->pregs
[PR_SPC
]);
104 tlb_flush_page(env
, new_spc
);
108 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
110 (fprintf(logfile
, "%s: a0=%x a1=%x\n", __func__
, a0
, a1
));
113 /* Used by the tlb decoder. */
114 #define EXTRACT_FIELD(src, start, end) \
115 (((src) >> start) & ((1 << (end - start + 1)) - 1))
117 void helper_movl_sreg_reg (uint32_t sreg
, uint32_t reg
)
120 srs
= env
->pregs
[PR_SRS
];
122 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
124 #if !defined(CONFIG_USER_ONLY)
125 if (srs
== 1 || srs
== 2) {
127 /* Writes to tlb-hi write to mm_cause as a side
129 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
130 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
132 else if (sreg
== 5) {
139 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
144 /* We've just made a write to tlb_lo. */
145 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
146 /* Writes are done via r_mm_cause. */
147 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
149 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
151 vaddr
<<= TARGET_PAGE_BITS
;
152 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
154 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
155 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
158 "tlb flush vaddr=%x v=%d pc=%x\n",
159 vaddr
, tlb_v
, env
->pc
));
160 tlb_flush_page(env
, vaddr
);
166 void helper_movl_reg_sreg (uint32_t reg
, uint32_t sreg
)
169 env
->pregs
[PR_SRS
] &= 3;
170 srs
= env
->pregs
[PR_SRS
];
172 #if !defined(CONFIG_USER_ONLY)
173 if (srs
== 1 || srs
== 2)
179 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
184 /* Update the mirror regs. */
185 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
186 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
187 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
188 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
191 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
195 static void cris_ccs_rshift(CPUState
*env
)
199 /* Apply the ccs shift. */
200 ccs
= env
->pregs
[PR_CCS
];
201 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
204 /* Enter user mode. */
205 env
->ksp
= env
->regs
[R_SP
];
206 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
209 env
->pregs
[PR_CCS
] = ccs
;
212 void helper_rfe(void)
214 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
216 D(fprintf(logfile
, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
217 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
221 cris_ccs_rshift(env
);
223 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
225 env
->pregs
[PR_CCS
] |= P_FLAG
;
228 void helper_rfn(void)
230 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
232 D(fprintf(logfile
, "rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
233 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
237 cris_ccs_rshift(env
);
239 /* Set the P_FLAG only if the R_FLAG is not set. */
241 env
->pregs
[PR_CCS
] |= P_FLAG
;
243 /* Always set the M flag. */
244 env
->pregs
[PR_CCS
] |= M_FLAG
;
247 void do_unassigned_access(target_phys_addr_t addr
, int is_write
, int is_exec
,
248 int is_asi
, int size
)
250 D(printf("%s addr=%x w=%d ex=%d asi=%d, size=%d\n",
251 __func__
, addr
, is_write
, is_exec
, is_asi
, size
));
254 static void evaluate_flags_writeback(uint32_t flags
)
258 /* Extended arithmetics, leave the z flag alone. */
260 if ((x
|| env
->cc_op
== CC_OP_ADDC
)
262 env
->cc_mask
&= ~Z_FLAG
;
264 /* all insn clear the x-flag except setf or clrf. */
265 env
->pregs
[PR_CCS
] &= ~(env
->cc_mask
| X_FLAG
);
266 flags
&= env
->cc_mask
;
267 env
->pregs
[PR_CCS
] |= flags
;
270 void helper_evaluate_flags_muls(void)
282 res
= env
->cc_result
;
284 dneg
= ((int32_t)res
) < 0;
286 mof
= env
->pregs
[PR_MOF
];
294 if ((dneg
&& mof
!= -1)
295 || (!dneg
&& mof
!= 0))
297 evaluate_flags_writeback(flags
);
300 void helper_evaluate_flags_mulu(void)
311 res
= env
->cc_result
;
313 mof
= env
->pregs
[PR_MOF
];
324 evaluate_flags_writeback(flags
);
327 void helper_evaluate_flags_mcp(void)
336 res
= env
->cc_result
;
338 if ((res
& 0x80000000L
) != 0L)
341 if (((src
& 0x80000000L
) == 0L)
342 && ((dst
& 0x80000000L
) == 0L))
346 else if (((src
& 0x80000000L
) != 0L) &&
347 ((dst
& 0x80000000L
) != 0L))
356 if (((src
& 0x80000000L
) != 0L)
357 && ((dst
& 0x80000000L
) != 0L))
359 if ((dst
& 0x80000000L
) != 0L
360 || (src
& 0x80000000L
) != 0L)
364 evaluate_flags_writeback(flags
);
367 void helper_evaluate_flags_alu_4(void)
377 /* Reconstruct the result. */
387 res
= env
->cc_result
;
391 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
394 if ((res
& 0x80000000L
) != 0L)
397 if (((src
& 0x80000000L
) == 0L)
398 && ((dst
& 0x80000000L
) == 0L))
402 else if (((src
& 0x80000000L
) != 0L) &&
403 ((dst
& 0x80000000L
) != 0L))
412 if (((src
& 0x80000000L
) != 0L)
413 && ((dst
& 0x80000000L
) != 0L))
415 if ((dst
& 0x80000000L
) != 0L
416 || (src
& 0x80000000L
) != 0L)
420 if (env
->cc_op
== CC_OP_SUB
421 || env
->cc_op
== CC_OP_CMP
) {
424 evaluate_flags_writeback(flags
);
427 void helper_evaluate_flags_move_4 (void)
432 res
= env
->cc_result
;
434 if ((int32_t)res
< 0)
439 evaluate_flags_writeback(flags
);
441 void helper_evaluate_flags_move_2 (void)
448 res
= env
->cc_result
;
450 if ((int16_t)res
< 0L)
455 evaluate_flags_writeback(flags
);
458 /* TODO: This is expensive. We could split things up and only evaluate part of
459 CCR on a need to know basis. For now, we simply re-evaluate everything. */
460 void helper_evaluate_flags (void)
469 res
= env
->cc_result
;
471 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
474 /* Now, evaluate the flags. This stuff is based on
475 Per Zander's CRISv10 simulator. */
476 switch (env
->cc_size
)
479 if ((res
& 0x80L
) != 0L)
482 if (((src
& 0x80L
) == 0L)
483 && ((dst
& 0x80L
) == 0L))
487 else if (((src
& 0x80L
) != 0L)
488 && ((dst
& 0x80L
) != 0L))
495 if ((res
& 0xFFL
) == 0L)
499 if (((src
& 0x80L
) != 0L)
500 && ((dst
& 0x80L
) != 0L))
504 if ((dst
& 0x80L
) != 0L
505 || (src
& 0x80L
) != 0L)
512 if ((res
& 0x8000L
) != 0L)
515 if (((src
& 0x8000L
) == 0L)
516 && ((dst
& 0x8000L
) == 0L))
520 else if (((src
& 0x8000L
) != 0L)
521 && ((dst
& 0x8000L
) != 0L))
528 if ((res
& 0xFFFFL
) == 0L)
532 if (((src
& 0x8000L
) != 0L)
533 && ((dst
& 0x8000L
) != 0L))
537 if ((dst
& 0x8000L
) != 0L
538 || (src
& 0x8000L
) != 0L)
545 if ((res
& 0x80000000L
) != 0L)
548 if (((src
& 0x80000000L
) == 0L)
549 && ((dst
& 0x80000000L
) == 0L))
553 else if (((src
& 0x80000000L
) != 0L) &&
554 ((dst
& 0x80000000L
) != 0L))
563 if (((src
& 0x80000000L
) != 0L)
564 && ((dst
& 0x80000000L
) != 0L))
566 if ((dst
& 0x80000000L
) != 0L
567 || (src
& 0x80000000L
) != 0L)
575 if (env
->cc_op
== CC_OP_SUB
576 || env
->cc_op
== CC_OP_CMP
) {
579 evaluate_flags_writeback(flags
);
582 void helper_top_evaluate_flags(void)
587 helper_evaluate_flags_mcp();
590 helper_evaluate_flags_muls();
593 helper_evaluate_flags_mulu();
602 switch (env
->cc_size
)
605 helper_evaluate_flags_move_4();
608 helper_evaluate_flags_move_2();
611 helper_evaluate_flags();
620 switch (env
->cc_size
)
623 helper_evaluate_flags_alu_4();
626 helper_evaluate_flags();