]> Git Repo - qemu.git/blob - target/arm/op_helper.c
target/arm: Honour MDCR_EL2.TDE when routing exceptions due to BKPT/BRK
[qemu.git] / target / arm / op_helper.c
1 /*
2  *  ARM helper routines
3  *
4  *  Copyright (c) 2005-2007 CodeSourcery, LLC
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include "qemu/osdep.h"
20 #include "qemu/log.h"
21 #include "qemu/main-loop.h"
22 #include "cpu.h"
23 #include "exec/helper-proto.h"
24 #include "internals.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27
28 #define SIGNBIT (uint32_t)0x80000000
29 #define SIGNBIT64 ((uint64_t)1 << 63)
30
31 static void raise_exception(CPUARMState *env, uint32_t excp,
32                             uint32_t syndrome, uint32_t target_el)
33 {
34     CPUState *cs = CPU(arm_env_get_cpu(env));
35
36     assert(!excp_is_internal(excp));
37     cs->exception_index = excp;
38     env->exception.syndrome = syndrome;
39     env->exception.target_el = target_el;
40     cpu_loop_exit(cs);
41 }
42
43 static int exception_target_el(CPUARMState *env)
44 {
45     int target_el = MAX(1, arm_current_el(env));
46
47     /* No such thing as secure EL1 if EL3 is aarch32, so update the target EL
48      * to EL3 in this case.
49      */
50     if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
51         target_el = 3;
52     }
53
54     return target_el;
55 }
56
57 uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def, void *vn,
58                           uint32_t maxindex)
59 {
60     uint32_t val, shift;
61     uint64_t *table = vn;
62
63     val = 0;
64     for (shift = 0; shift < 32; shift += 8) {
65         uint32_t index = (ireg >> shift) & 0xff;
66         if (index < maxindex) {
67             uint32_t tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
68             val |= tmp << shift;
69         } else {
70             val |= def & (0xff << shift);
71         }
72     }
73     return val;
74 }
75
76 #if !defined(CONFIG_USER_ONLY)
77
78 static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
79                                             unsigned int target_el,
80                                             bool same_el, bool ea,
81                                             bool s1ptw, bool is_write,
82                                             int fsc)
83 {
84     uint32_t syn;
85
86     /* ISV is only set for data aborts routed to EL2 and
87      * never for stage-1 page table walks faulting on stage 2.
88      *
89      * Furthermore, ISV is only set for certain kinds of load/stores.
90      * If the template syndrome does not have ISV set, we should leave
91      * it cleared.
92      *
93      * See ARMv8 specs, D7-1974:
94      * ISS encoding for an exception from a Data Abort, the
95      * ISV field.
96      */
97     if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
98         syn = syn_data_abort_no_iss(same_el,
99                                     ea, 0, s1ptw, is_write, fsc);
100     } else {
101         /* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
102          * syndrome created at translation time.
103          * Now we create the runtime syndrome with the remaining fields.
104          */
105         syn = syn_data_abort_with_iss(same_el,
106                                       0, 0, 0, 0, 0,
107                                       ea, 0, s1ptw, is_write, fsc,
108                                       false);
109         /* Merge the runtime syndrome with the template syndrome.  */
110         syn |= template_syn;
111     }
112     return syn;
113 }
114
115 static void deliver_fault(ARMCPU *cpu, vaddr addr, MMUAccessType access_type,
116                           int mmu_idx, ARMMMUFaultInfo *fi)
117 {
118     CPUARMState *env = &cpu->env;
119     int target_el;
120     bool same_el;
121     uint32_t syn, exc, fsr, fsc;
122     ARMMMUIdx arm_mmu_idx = core_to_arm_mmu_idx(env, mmu_idx);
123
124     target_el = exception_target_el(env);
125     if (fi->stage2) {
126         target_el = 2;
127         env->cp15.hpfar_el2 = extract64(fi->s2addr, 12, 47) << 4;
128     }
129     same_el = (arm_current_el(env) == target_el);
130
131     if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
132         arm_s1_regime_using_lpae_format(env, arm_mmu_idx)) {
133         /* LPAE format fault status register : bottom 6 bits are
134          * status code in the same form as needed for syndrome
135          */
136         fsr = arm_fi_to_lfsc(fi);
137         fsc = extract32(fsr, 0, 6);
138     } else {
139         fsr = arm_fi_to_sfsc(fi);
140         /* Short format FSR : this fault will never actually be reported
141          * to an EL that uses a syndrome register. Use a (currently)
142          * reserved FSR code in case the constructed syndrome does leak
143          * into the guest somehow.
144          */
145         fsc = 0x3f;
146     }
147
148     if (access_type == MMU_INST_FETCH) {
149         syn = syn_insn_abort(same_el, fi->ea, fi->s1ptw, fsc);
150         exc = EXCP_PREFETCH_ABORT;
151     } else {
152         syn = merge_syn_data_abort(env->exception.syndrome, target_el,
153                                    same_el, fi->ea, fi->s1ptw,
154                                    access_type == MMU_DATA_STORE,
155                                    fsc);
156         if (access_type == MMU_DATA_STORE
157             && arm_feature(env, ARM_FEATURE_V6)) {
158             fsr |= (1 << 11);
159         }
160         exc = EXCP_DATA_ABORT;
161     }
162
163     env->exception.vaddress = addr;
164     env->exception.fsr = fsr;
165     raise_exception(env, exc, syn, target_el);
166 }
167
168 /* try to fill the TLB and return an exception if error. If retaddr is
169  * NULL, it means that the function was called in C code (i.e. not
170  * from generated code or from helper.c)
171  */
172 void tlb_fill(CPUState *cs, target_ulong addr, int size,
173               MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
174 {
175     bool ret;
176     ARMMMUFaultInfo fi = {};
177
178     ret = arm_tlb_fill(cs, addr, access_type, mmu_idx, &fi);
179     if (unlikely(ret)) {
180         ARMCPU *cpu = ARM_CPU(cs);
181
182         /* now we have a real cpu fault */
183         cpu_restore_state(cs, retaddr);
184
185         deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
186     }
187 }
188
189 /* Raise a data fault alignment exception for the specified virtual address */
190 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
191                                  MMUAccessType access_type,
192                                  int mmu_idx, uintptr_t retaddr)
193 {
194     ARMCPU *cpu = ARM_CPU(cs);
195     ARMMMUFaultInfo fi = {};
196
197     /* now we have a real cpu fault */
198     cpu_restore_state(cs, retaddr);
199
200     fi.type = ARMFault_Alignment;
201     deliver_fault(cpu, vaddr, access_type, mmu_idx, &fi);
202 }
203
204 /* arm_cpu_do_transaction_failed: handle a memory system error response
205  * (eg "no device/memory present at address") by raising an external abort
206  * exception
207  */
208 void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
209                                    vaddr addr, unsigned size,
210                                    MMUAccessType access_type,
211                                    int mmu_idx, MemTxAttrs attrs,
212                                    MemTxResult response, uintptr_t retaddr)
213 {
214     ARMCPU *cpu = ARM_CPU(cs);
215     ARMMMUFaultInfo fi = {};
216
217     /* now we have a real cpu fault */
218     cpu_restore_state(cs, retaddr);
219
220     fi.ea = arm_extabort_type(response);
221     fi.type = ARMFault_SyncExternal;
222     deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
223 }
224
225 #endif /* !defined(CONFIG_USER_ONLY) */
226
227 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
228 {
229     uint32_t res = a + b;
230     if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
231         env->QF = 1;
232     return res;
233 }
234
235 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
236 {
237     uint32_t res = a + b;
238     if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
239         env->QF = 1;
240         res = ~(((int32_t)a >> 31) ^ SIGNBIT);
241     }
242     return res;
243 }
244
245 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
246 {
247     uint32_t res = a - b;
248     if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
249         env->QF = 1;
250         res = ~(((int32_t)a >> 31) ^ SIGNBIT);
251     }
252     return res;
253 }
254
255 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
256 {
257     uint32_t res;
258     if (val >= 0x40000000) {
259         res = ~SIGNBIT;
260         env->QF = 1;
261     } else if (val <= (int32_t)0xc0000000) {
262         res = SIGNBIT;
263         env->QF = 1;
264     } else {
265         res = val << 1;
266     }
267     return res;
268 }
269
270 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
271 {
272     uint32_t res = a + b;
273     if (res < a) {
274         env->QF = 1;
275         res = ~0;
276     }
277     return res;
278 }
279
280 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
281 {
282     uint32_t res = a - b;
283     if (res > a) {
284         env->QF = 1;
285         res = 0;
286     }
287     return res;
288 }
289
290 /* Signed saturation.  */
291 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
292 {
293     int32_t top;
294     uint32_t mask;
295
296     top = val >> shift;
297     mask = (1u << shift) - 1;
298     if (top > 0) {
299         env->QF = 1;
300         return mask;
301     } else if (top < -1) {
302         env->QF = 1;
303         return ~mask;
304     }
305     return val;
306 }
307
308 /* Unsigned saturation.  */
309 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
310 {
311     uint32_t max;
312
313     max = (1u << shift) - 1;
314     if (val < 0) {
315         env->QF = 1;
316         return 0;
317     } else if (val > max) {
318         env->QF = 1;
319         return max;
320     }
321     return val;
322 }
323
324 /* Signed saturate.  */
325 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
326 {
327     return do_ssat(env, x, shift);
328 }
329
330 /* Dual halfword signed saturate.  */
331 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
332 {
333     uint32_t res;
334
335     res = (uint16_t)do_ssat(env, (int16_t)x, shift);
336     res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
337     return res;
338 }
339
340 /* Unsigned saturate.  */
341 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
342 {
343     return do_usat(env, x, shift);
344 }
345
346 /* Dual halfword unsigned saturate.  */
347 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
348 {
349     uint32_t res;
350
351     res = (uint16_t)do_usat(env, (int16_t)x, shift);
352     res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
353     return res;
354 }
355
356 void HELPER(setend)(CPUARMState *env)
357 {
358     env->uncached_cpsr ^= CPSR_E;
359 }
360
361 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
362  * The function returns the target EL (1-3) if the instruction is to be trapped;
363  * otherwise it returns 0 indicating it is not trapped.
364  */
365 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
366 {
367     int cur_el = arm_current_el(env);
368     uint64_t mask;
369
370     if (arm_feature(env, ARM_FEATURE_M)) {
371         /* M profile cores can never trap WFI/WFE. */
372         return 0;
373     }
374
375     /* If we are currently in EL0 then we need to check if SCTLR is set up for
376      * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
377      */
378     if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
379         int target_el;
380
381         mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
382         if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
383             /* Secure EL0 and Secure PL1 is at EL3 */
384             target_el = 3;
385         } else {
386             target_el = 1;
387         }
388
389         if (!(env->cp15.sctlr_el[target_el] & mask)) {
390             return target_el;
391         }
392     }
393
394     /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
395      * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
396      * bits will be zero indicating no trap.
397      */
398     if (cur_el < 2 && !arm_is_secure(env)) {
399         mask = (is_wfe) ? HCR_TWE : HCR_TWI;
400         if (env->cp15.hcr_el2 & mask) {
401             return 2;
402         }
403     }
404
405     /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
406     if (cur_el < 3) {
407         mask = (is_wfe) ? SCR_TWE : SCR_TWI;
408         if (env->cp15.scr_el3 & mask) {
409             return 3;
410         }
411     }
412
413     return 0;
414 }
415
416 void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
417 {
418     CPUState *cs = CPU(arm_env_get_cpu(env));
419     int target_el = check_wfx_trap(env, false);
420
421     if (cpu_has_work(cs)) {
422         /* Don't bother to go into our "low power state" if
423          * we would just wake up immediately.
424          */
425         return;
426     }
427
428     if (target_el) {
429         env->pc -= insn_len;
430         raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2),
431                         target_el);
432     }
433
434     cs->exception_index = EXCP_HLT;
435     cs->halted = 1;
436     cpu_loop_exit(cs);
437 }
438
439 void HELPER(wfe)(CPUARMState *env)
440 {
441     /* This is a hint instruction that is semantically different
442      * from YIELD even though we currently implement it identically.
443      * Don't actually halt the CPU, just yield back to top
444      * level loop. This is not going into a "low power state"
445      * (ie halting until some event occurs), so we never take
446      * a configurable trap to a different exception level.
447      */
448     HELPER(yield)(env);
449 }
450
451 void HELPER(yield)(CPUARMState *env)
452 {
453     ARMCPU *cpu = arm_env_get_cpu(env);
454     CPUState *cs = CPU(cpu);
455
456     /* This is a non-trappable hint instruction that generally indicates
457      * that the guest is currently busy-looping. Yield control back to the
458      * top level loop so that a more deserving VCPU has a chance to run.
459      */
460     cs->exception_index = EXCP_YIELD;
461     cpu_loop_exit(cs);
462 }
463
464 /* Raise an internal-to-QEMU exception. This is limited to only
465  * those EXCP values which are special cases for QEMU to interrupt
466  * execution and not to be used for exceptions which are passed to
467  * the guest (those must all have syndrome information and thus should
468  * use exception_with_syndrome).
469  */
470 void HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
471 {
472     CPUState *cs = CPU(arm_env_get_cpu(env));
473
474     assert(excp_is_internal(excp));
475     cs->exception_index = excp;
476     cpu_loop_exit(cs);
477 }
478
479 /* Raise an exception with the specified syndrome register value */
480 void HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
481                                      uint32_t syndrome, uint32_t target_el)
482 {
483     raise_exception(env, excp, syndrome, target_el);
484 }
485
486 /* Raise an EXCP_BKPT with the specified syndrome register value,
487  * targeting the correct exception level for debug exceptions.
488  */
489 void HELPER(exception_bkpt_insn)(CPUARMState *env, uint32_t syndrome)
490 {
491     raise_exception(env, EXCP_BKPT, syndrome, arm_debug_target_el(env));
492 }
493
494 uint32_t HELPER(cpsr_read)(CPUARMState *env)
495 {
496     return cpsr_read(env) & ~(CPSR_EXEC | CPSR_RESERVED);
497 }
498
499 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
500 {
501     cpsr_write(env, val, mask, CPSRWriteByInstr);
502 }
503
504 /* Write the CPSR for a 32-bit exception return */
505 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
506 {
507     cpsr_write(env, val, CPSR_ERET_MASK, CPSRWriteExceptionReturn);
508
509     /* Generated code has already stored the new PC value, but
510      * without masking out its low bits, because which bits need
511      * masking depends on whether we're returning to Thumb or ARM
512      * state. Do the masking now.
513      */
514     env->regs[15] &= (env->thumb ? ~1 : ~3);
515
516     qemu_mutex_lock_iothread();
517     arm_call_el_change_hook(arm_env_get_cpu(env));
518     qemu_mutex_unlock_iothread();
519 }
520
521 /* Access to user mode registers from privileged modes.  */
522 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
523 {
524     uint32_t val;
525
526     if (regno == 13) {
527         val = env->banked_r13[BANK_USRSYS];
528     } else if (regno == 14) {
529         val = env->banked_r14[BANK_USRSYS];
530     } else if (regno >= 8
531                && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
532         val = env->usr_regs[regno - 8];
533     } else {
534         val = env->regs[regno];
535     }
536     return val;
537 }
538
539 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
540 {
541     if (regno == 13) {
542         env->banked_r13[BANK_USRSYS] = val;
543     } else if (regno == 14) {
544         env->banked_r14[BANK_USRSYS] = val;
545     } else if (regno >= 8
546                && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
547         env->usr_regs[regno - 8] = val;
548     } else {
549         env->regs[regno] = val;
550     }
551 }
552
553 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
554 {
555     if ((env->uncached_cpsr & CPSR_M) == mode) {
556         env->regs[13] = val;
557     } else {
558         env->banked_r13[bank_number(mode)] = val;
559     }
560 }
561
562 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
563 {
564     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
565         /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
566          * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
567          */
568         raise_exception(env, EXCP_UDEF, syn_uncategorized(),
569                         exception_target_el(env));
570     }
571
572     if ((env->uncached_cpsr & CPSR_M) == mode) {
573         return env->regs[13];
574     } else {
575         return env->banked_r13[bank_number(mode)];
576     }
577 }
578
579 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
580                                       uint32_t regno)
581 {
582     /* Raise an exception if the requested access is one of the UNPREDICTABLE
583      * cases; otherwise return. This broadly corresponds to the pseudocode
584      * BankedRegisterAccessValid() and SPSRAccessValid(),
585      * except that we have already handled some cases at translate time.
586      */
587     int curmode = env->uncached_cpsr & CPSR_M;
588
589     if (curmode == tgtmode) {
590         goto undef;
591     }
592
593     if (tgtmode == ARM_CPU_MODE_USR) {
594         switch (regno) {
595         case 8 ... 12:
596             if (curmode != ARM_CPU_MODE_FIQ) {
597                 goto undef;
598             }
599             break;
600         case 13:
601             if (curmode == ARM_CPU_MODE_SYS) {
602                 goto undef;
603             }
604             break;
605         case 14:
606             if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
607                 goto undef;
608             }
609             break;
610         default:
611             break;
612         }
613     }
614
615     if (tgtmode == ARM_CPU_MODE_HYP) {
616         switch (regno) {
617         case 17: /* ELR_Hyp */
618             if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
619                 goto undef;
620             }
621             break;
622         default:
623             if (curmode != ARM_CPU_MODE_MON) {
624                 goto undef;
625             }
626             break;
627         }
628     }
629
630     return;
631
632 undef:
633     raise_exception(env, EXCP_UDEF, syn_uncategorized(),
634                     exception_target_el(env));
635 }
636
637 void HELPER(msr_banked)(CPUARMState *env, uint32_t value, uint32_t tgtmode,
638                         uint32_t regno)
639 {
640     msr_mrs_banked_exc_checks(env, tgtmode, regno);
641
642     switch (regno) {
643     case 16: /* SPSRs */
644         env->banked_spsr[bank_number(tgtmode)] = value;
645         break;
646     case 17: /* ELR_Hyp */
647         env->elr_el[2] = value;
648         break;
649     case 13:
650         env->banked_r13[bank_number(tgtmode)] = value;
651         break;
652     case 14:
653         env->banked_r14[bank_number(tgtmode)] = value;
654         break;
655     case 8 ... 12:
656         switch (tgtmode) {
657         case ARM_CPU_MODE_USR:
658             env->usr_regs[regno - 8] = value;
659             break;
660         case ARM_CPU_MODE_FIQ:
661             env->fiq_regs[regno - 8] = value;
662             break;
663         default:
664             g_assert_not_reached();
665         }
666         break;
667     default:
668         g_assert_not_reached();
669     }
670 }
671
672 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
673 {
674     msr_mrs_banked_exc_checks(env, tgtmode, regno);
675
676     switch (regno) {
677     case 16: /* SPSRs */
678         return env->banked_spsr[bank_number(tgtmode)];
679     case 17: /* ELR_Hyp */
680         return env->elr_el[2];
681     case 13:
682         return env->banked_r13[bank_number(tgtmode)];
683     case 14:
684         return env->banked_r14[bank_number(tgtmode)];
685     case 8 ... 12:
686         switch (tgtmode) {
687         case ARM_CPU_MODE_USR:
688             return env->usr_regs[regno - 8];
689         case ARM_CPU_MODE_FIQ:
690             return env->fiq_regs[regno - 8];
691         default:
692             g_assert_not_reached();
693         }
694     default:
695         g_assert_not_reached();
696     }
697 }
698
699 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome,
700                                  uint32_t isread)
701 {
702     const ARMCPRegInfo *ri = rip;
703     int target_el;
704
705     if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
706         && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
707         raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
708     }
709
710     if (!ri->accessfn) {
711         return;
712     }
713
714     switch (ri->accessfn(env, ri, isread)) {
715     case CP_ACCESS_OK:
716         return;
717     case CP_ACCESS_TRAP:
718         target_el = exception_target_el(env);
719         break;
720     case CP_ACCESS_TRAP_EL2:
721         /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
722          * a bug in the access function.
723          */
724         assert(!arm_is_secure(env) && arm_current_el(env) != 3);
725         target_el = 2;
726         break;
727     case CP_ACCESS_TRAP_EL3:
728         target_el = 3;
729         break;
730     case CP_ACCESS_TRAP_UNCATEGORIZED:
731         target_el = exception_target_el(env);
732         syndrome = syn_uncategorized();
733         break;
734     case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
735         target_el = 2;
736         syndrome = syn_uncategorized();
737         break;
738     case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
739         target_el = 3;
740         syndrome = syn_uncategorized();
741         break;
742     case CP_ACCESS_TRAP_FP_EL2:
743         target_el = 2;
744         /* Since we are an implementation that takes exceptions on a trapped
745          * conditional insn only if the insn has passed its condition code
746          * check, we take the IMPDEF choice to always report CV=1 COND=0xe
747          * (which is also the required value for AArch64 traps).
748          */
749         syndrome = syn_fp_access_trap(1, 0xe, false);
750         break;
751     case CP_ACCESS_TRAP_FP_EL3:
752         target_el = 3;
753         syndrome = syn_fp_access_trap(1, 0xe, false);
754         break;
755     default:
756         g_assert_not_reached();
757     }
758
759     raise_exception(env, EXCP_UDEF, syndrome, target_el);
760 }
761
762 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
763 {
764     const ARMCPRegInfo *ri = rip;
765
766     if (ri->type & ARM_CP_IO) {
767         qemu_mutex_lock_iothread();
768         ri->writefn(env, ri, value);
769         qemu_mutex_unlock_iothread();
770     } else {
771         ri->writefn(env, ri, value);
772     }
773 }
774
775 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
776 {
777     const ARMCPRegInfo *ri = rip;
778     uint32_t res;
779
780     if (ri->type & ARM_CP_IO) {
781         qemu_mutex_lock_iothread();
782         res = ri->readfn(env, ri);
783         qemu_mutex_unlock_iothread();
784     } else {
785         res = ri->readfn(env, ri);
786     }
787
788     return res;
789 }
790
791 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
792 {
793     const ARMCPRegInfo *ri = rip;
794
795     if (ri->type & ARM_CP_IO) {
796         qemu_mutex_lock_iothread();
797         ri->writefn(env, ri, value);
798         qemu_mutex_unlock_iothread();
799     } else {
800         ri->writefn(env, ri, value);
801     }
802 }
803
804 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
805 {
806     const ARMCPRegInfo *ri = rip;
807     uint64_t res;
808
809     if (ri->type & ARM_CP_IO) {
810         qemu_mutex_lock_iothread();
811         res = ri->readfn(env, ri);
812         qemu_mutex_unlock_iothread();
813     } else {
814         res = ri->readfn(env, ri);
815     }
816
817     return res;
818 }
819
820 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
821 {
822     /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
823      * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
824      * to catch that case at translate time.
825      */
826     if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
827         uint32_t syndrome = syn_aa64_sysregtrap(0, extract32(op, 0, 3),
828                                                 extract32(op, 3, 3), 4,
829                                                 imm, 0x1f, 0);
830         raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
831     }
832
833     switch (op) {
834     case 0x05: /* SPSel */
835         update_spsel(env, imm);
836         break;
837     case 0x1e: /* DAIFSet */
838         env->daif |= (imm << 6) & PSTATE_DAIF;
839         break;
840     case 0x1f: /* DAIFClear */
841         env->daif &= ~((imm << 6) & PSTATE_DAIF);
842         break;
843     default:
844         g_assert_not_reached();
845     }
846 }
847
848 void HELPER(clear_pstate_ss)(CPUARMState *env)
849 {
850     env->pstate &= ~PSTATE_SS;
851 }
852
853 void HELPER(pre_hvc)(CPUARMState *env)
854 {
855     ARMCPU *cpu = arm_env_get_cpu(env);
856     int cur_el = arm_current_el(env);
857     /* FIXME: Use actual secure state.  */
858     bool secure = false;
859     bool undef;
860
861     if (arm_is_psci_call(cpu, EXCP_HVC)) {
862         /* If PSCI is enabled and this looks like a valid PSCI call then
863          * that overrides the architecturally mandated HVC behaviour.
864          */
865         return;
866     }
867
868     if (!arm_feature(env, ARM_FEATURE_EL2)) {
869         /* If EL2 doesn't exist, HVC always UNDEFs */
870         undef = true;
871     } else if (arm_feature(env, ARM_FEATURE_EL3)) {
872         /* EL3.HCE has priority over EL2.HCD. */
873         undef = !(env->cp15.scr_el3 & SCR_HCE);
874     } else {
875         undef = env->cp15.hcr_el2 & HCR_HCD;
876     }
877
878     /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
879      * For ARMv8/AArch64, HVC is allowed in EL3.
880      * Note that we've already trapped HVC from EL0 at translation
881      * time.
882      */
883     if (secure && (!is_a64(env) || cur_el == 1)) {
884         undef = true;
885     }
886
887     if (undef) {
888         raise_exception(env, EXCP_UDEF, syn_uncategorized(),
889                         exception_target_el(env));
890     }
891 }
892
893 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
894 {
895     ARMCPU *cpu = arm_env_get_cpu(env);
896     int cur_el = arm_current_el(env);
897     bool secure = arm_is_secure(env);
898     bool smd = env->cp15.scr_el3 & SCR_SMD;
899     /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
900      * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
901      *  extensions, SMD only applies to NS state.
902      * On ARMv7 without the Virtualization extensions, the SMD bit
903      * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
904      * so we need not special case this here.
905      */
906     bool undef = arm_feature(env, ARM_FEATURE_AARCH64) ? smd : smd && !secure;
907
908     if (!arm_feature(env, ARM_FEATURE_EL3) &&
909         cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
910         /* If we have no EL3 then SMC always UNDEFs and can't be
911          * trapped to EL2. PSCI-via-SMC is a sort of ersatz EL3
912          * firmware within QEMU, and we want an EL2 guest to be able
913          * to forbid its EL1 from making PSCI calls into QEMU's
914          * "firmware" via HCR.TSC, so for these purposes treat
915          * PSCI-via-SMC as implying an EL3.
916          */
917         undef = true;
918     } else if (!secure && cur_el == 1 && (env->cp15.hcr_el2 & HCR_TSC)) {
919         /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
920          * We also want an EL2 guest to be able to forbid its EL1 from
921          * making PSCI calls into QEMU's "firmware" via HCR.TSC.
922          */
923         raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
924     }
925
926     /* If PSCI is enabled and this looks like a valid PSCI call then
927      * suppress the UNDEF -- we'll catch the SMC exception and
928      * implement the PSCI call behaviour there.
929      */
930     if (undef && !arm_is_psci_call(cpu, EXCP_SMC)) {
931         raise_exception(env, EXCP_UDEF, syn_uncategorized(),
932                         exception_target_el(env));
933     }
934 }
935
936 static int el_from_spsr(uint32_t spsr)
937 {
938     /* Return the exception level that this SPSR is requesting a return to,
939      * or -1 if it is invalid (an illegal return)
940      */
941     if (spsr & PSTATE_nRW) {
942         switch (spsr & CPSR_M) {
943         case ARM_CPU_MODE_USR:
944             return 0;
945         case ARM_CPU_MODE_HYP:
946             return 2;
947         case ARM_CPU_MODE_FIQ:
948         case ARM_CPU_MODE_IRQ:
949         case ARM_CPU_MODE_SVC:
950         case ARM_CPU_MODE_ABT:
951         case ARM_CPU_MODE_UND:
952         case ARM_CPU_MODE_SYS:
953             return 1;
954         case ARM_CPU_MODE_MON:
955             /* Returning to Mon from AArch64 is never possible,
956              * so this is an illegal return.
957              */
958         default:
959             return -1;
960         }
961     } else {
962         if (extract32(spsr, 1, 1)) {
963             /* Return with reserved M[1] bit set */
964             return -1;
965         }
966         if (extract32(spsr, 0, 4) == 1) {
967             /* return to EL0 with M[0] bit set */
968             return -1;
969         }
970         return extract32(spsr, 2, 2);
971     }
972 }
973
974 void HELPER(exception_return)(CPUARMState *env)
975 {
976     int cur_el = arm_current_el(env);
977     unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
978     uint32_t spsr = env->banked_spsr[spsr_idx];
979     int new_el;
980     bool return_to_aa64 = (spsr & PSTATE_nRW) == 0;
981
982     aarch64_save_sp(env, cur_el);
983
984     arm_clear_exclusive(env);
985
986     /* We must squash the PSTATE.SS bit to zero unless both of the
987      * following hold:
988      *  1. debug exceptions are currently disabled
989      *  2. singlestep will be active in the EL we return to
990      * We check 1 here and 2 after we've done the pstate/cpsr write() to
991      * transition to the EL we're going to.
992      */
993     if (arm_generate_debug_exceptions(env)) {
994         spsr &= ~PSTATE_SS;
995     }
996
997     new_el = el_from_spsr(spsr);
998     if (new_el == -1) {
999         goto illegal_return;
1000     }
1001     if (new_el > cur_el
1002         || (new_el == 2 && !arm_feature(env, ARM_FEATURE_EL2))) {
1003         /* Disallow return to an EL which is unimplemented or higher
1004          * than the current one.
1005          */
1006         goto illegal_return;
1007     }
1008
1009     if (new_el != 0 && arm_el_is_aa64(env, new_el) != return_to_aa64) {
1010         /* Return to an EL which is configured for a different register width */
1011         goto illegal_return;
1012     }
1013
1014     if (new_el == 2 && arm_is_secure_below_el3(env)) {
1015         /* Return to the non-existent secure-EL2 */
1016         goto illegal_return;
1017     }
1018
1019     if (new_el == 1 && (env->cp15.hcr_el2 & HCR_TGE)
1020         && !arm_is_secure_below_el3(env)) {
1021         goto illegal_return;
1022     }
1023
1024     if (!return_to_aa64) {
1025         env->aarch64 = 0;
1026         /* We do a raw CPSR write because aarch64_sync_64_to_32()
1027          * will sort the register banks out for us, and we've already
1028          * caught all the bad-mode cases in el_from_spsr().
1029          */
1030         cpsr_write(env, spsr, ~0, CPSRWriteRaw);
1031         if (!arm_singlestep_active(env)) {
1032             env->uncached_cpsr &= ~PSTATE_SS;
1033         }
1034         aarch64_sync_64_to_32(env);
1035
1036         if (spsr & CPSR_T) {
1037             env->regs[15] = env->elr_el[cur_el] & ~0x1;
1038         } else {
1039             env->regs[15] = env->elr_el[cur_el] & ~0x3;
1040         }
1041         qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1042                       "AArch32 EL%d PC 0x%" PRIx32 "\n",
1043                       cur_el, new_el, env->regs[15]);
1044     } else {
1045         env->aarch64 = 1;
1046         pstate_write(env, spsr);
1047         if (!arm_singlestep_active(env)) {
1048             env->pstate &= ~PSTATE_SS;
1049         }
1050         aarch64_restore_sp(env, new_el);
1051         env->pc = env->elr_el[cur_el];
1052         qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1053                       "AArch64 EL%d PC 0x%" PRIx64 "\n",
1054                       cur_el, new_el, env->pc);
1055     }
1056
1057     qemu_mutex_lock_iothread();
1058     arm_call_el_change_hook(arm_env_get_cpu(env));
1059     qemu_mutex_unlock_iothread();
1060
1061     return;
1062
1063 illegal_return:
1064     /* Illegal return events of various kinds have architecturally
1065      * mandated behaviour:
1066      * restore NZCV and DAIF from SPSR_ELx
1067      * set PSTATE.IL
1068      * restore PC from ELR_ELx
1069      * no change to exception level, execution state or stack pointer
1070      */
1071     env->pstate |= PSTATE_IL;
1072     env->pc = env->elr_el[cur_el];
1073     spsr &= PSTATE_NZCV | PSTATE_DAIF;
1074     spsr |= pstate_read(env) & ~(PSTATE_NZCV | PSTATE_DAIF);
1075     pstate_write(env, spsr);
1076     if (!arm_singlestep_active(env)) {
1077         env->pstate &= ~PSTATE_SS;
1078     }
1079     qemu_log_mask(LOG_GUEST_ERROR, "Illegal exception return at EL%d: "
1080                   "resuming execution at 0x%" PRIx64 "\n", cur_el, env->pc);
1081 }
1082
1083 /* Return true if the linked breakpoint entry lbn passes its checks */
1084 static bool linked_bp_matches(ARMCPU *cpu, int lbn)
1085 {
1086     CPUARMState *env = &cpu->env;
1087     uint64_t bcr = env->cp15.dbgbcr[lbn];
1088     int brps = extract32(cpu->dbgdidr, 24, 4);
1089     int ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
1090     int bt;
1091     uint32_t contextidr;
1092
1093     /* Links to unimplemented or non-context aware breakpoints are
1094      * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
1095      * as if linked to an UNKNOWN context-aware breakpoint (in which
1096      * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
1097      * We choose the former.
1098      */
1099     if (lbn > brps || lbn < (brps - ctx_cmps)) {
1100         return false;
1101     }
1102
1103     bcr = env->cp15.dbgbcr[lbn];
1104
1105     if (extract64(bcr, 0, 1) == 0) {
1106         /* Linked breakpoint disabled : generate no events */
1107         return false;
1108     }
1109
1110     bt = extract64(bcr, 20, 4);
1111
1112     /* We match the whole register even if this is AArch32 using the
1113      * short descriptor format (in which case it holds both PROCID and ASID),
1114      * since we don't implement the optional v7 context ID masking.
1115      */
1116     contextidr = extract64(env->cp15.contextidr_el[1], 0, 32);
1117
1118     switch (bt) {
1119     case 3: /* linked context ID match */
1120         if (arm_current_el(env) > 1) {
1121             /* Context matches never fire in EL2 or (AArch64) EL3 */
1122             return false;
1123         }
1124         return (contextidr == extract64(env->cp15.dbgbvr[lbn], 0, 32));
1125     case 5: /* linked address mismatch (reserved in AArch64) */
1126     case 9: /* linked VMID match (reserved if no EL2) */
1127     case 11: /* linked context ID and VMID match (reserved if no EL2) */
1128     default:
1129         /* Links to Unlinked context breakpoints must generate no
1130          * events; we choose to do the same for reserved values too.
1131          */
1132         return false;
1133     }
1134
1135     return false;
1136 }
1137
1138 static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
1139 {
1140     CPUARMState *env = &cpu->env;
1141     uint64_t cr;
1142     int pac, hmc, ssc, wt, lbn;
1143     /* Note that for watchpoints the check is against the CPU security
1144      * state, not the S/NS attribute on the offending data access.
1145      */
1146     bool is_secure = arm_is_secure(env);
1147     int access_el = arm_current_el(env);
1148
1149     if (is_wp) {
1150         CPUWatchpoint *wp = env->cpu_watchpoint[n];
1151
1152         if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
1153             return false;
1154         }
1155         cr = env->cp15.dbgwcr[n];
1156         if (wp->hitattrs.user) {
1157             /* The LDRT/STRT/LDT/STT "unprivileged access" instructions should
1158              * match watchpoints as if they were accesses done at EL0, even if
1159              * the CPU is at EL1 or higher.
1160              */
1161             access_el = 0;
1162         }
1163     } else {
1164         uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1165
1166         if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
1167             return false;
1168         }
1169         cr = env->cp15.dbgbcr[n];
1170     }
1171     /* The WATCHPOINT_HIT flag guarantees us that the watchpoint is
1172      * enabled and that the address and access type match; for breakpoints
1173      * we know the address matched; check the remaining fields, including
1174      * linked breakpoints. We rely on WCR and BCR having the same layout
1175      * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
1176      * Note that some combinations of {PAC, HMC, SSC} are reserved and
1177      * must act either like some valid combination or as if the watchpoint
1178      * were disabled. We choose the former, and use this together with
1179      * the fact that EL3 must always be Secure and EL2 must always be
1180      * Non-Secure to simplify the code slightly compared to the full
1181      * table in the ARM ARM.
1182      */
1183     pac = extract64(cr, 1, 2);
1184     hmc = extract64(cr, 13, 1);
1185     ssc = extract64(cr, 14, 2);
1186
1187     switch (ssc) {
1188     case 0:
1189         break;
1190     case 1:
1191     case 3:
1192         if (is_secure) {
1193             return false;
1194         }
1195         break;
1196     case 2:
1197         if (!is_secure) {
1198             return false;
1199         }
1200         break;
1201     }
1202
1203     switch (access_el) {
1204     case 3:
1205     case 2:
1206         if (!hmc) {
1207             return false;
1208         }
1209         break;
1210     case 1:
1211         if (extract32(pac, 0, 1) == 0) {
1212             return false;
1213         }
1214         break;
1215     case 0:
1216         if (extract32(pac, 1, 1) == 0) {
1217             return false;
1218         }
1219         break;
1220     default:
1221         g_assert_not_reached();
1222     }
1223
1224     wt = extract64(cr, 20, 1);
1225     lbn = extract64(cr, 16, 4);
1226
1227     if (wt && !linked_bp_matches(cpu, lbn)) {
1228         return false;
1229     }
1230
1231     return true;
1232 }
1233
1234 static bool check_watchpoints(ARMCPU *cpu)
1235 {
1236     CPUARMState *env = &cpu->env;
1237     int n;
1238
1239     /* If watchpoints are disabled globally or we can't take debug
1240      * exceptions here then watchpoint firings are ignored.
1241      */
1242     if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1243         || !arm_generate_debug_exceptions(env)) {
1244         return false;
1245     }
1246
1247     for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
1248         if (bp_wp_matches(cpu, n, true)) {
1249             return true;
1250         }
1251     }
1252     return false;
1253 }
1254
1255 static bool check_breakpoints(ARMCPU *cpu)
1256 {
1257     CPUARMState *env = &cpu->env;
1258     int n;
1259
1260     /* If breakpoints are disabled globally or we can't take debug
1261      * exceptions here then breakpoint firings are ignored.
1262      */
1263     if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1264         || !arm_generate_debug_exceptions(env)) {
1265         return false;
1266     }
1267
1268     for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
1269         if (bp_wp_matches(cpu, n, false)) {
1270             return true;
1271         }
1272     }
1273     return false;
1274 }
1275
1276 void HELPER(check_breakpoints)(CPUARMState *env)
1277 {
1278     ARMCPU *cpu = arm_env_get_cpu(env);
1279
1280     if (check_breakpoints(cpu)) {
1281         HELPER(exception_internal(env, EXCP_DEBUG));
1282     }
1283 }
1284
1285 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
1286 {
1287     /* Called by core code when a CPU watchpoint fires; need to check if this
1288      * is also an architectural watchpoint match.
1289      */
1290     ARMCPU *cpu = ARM_CPU(cs);
1291
1292     return check_watchpoints(cpu);
1293 }
1294
1295 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len)
1296 {
1297     ARMCPU *cpu = ARM_CPU(cs);
1298     CPUARMState *env = &cpu->env;
1299
1300     /* In BE32 system mode, target memory is stored byteswapped (on a
1301      * little-endian host system), and by the time we reach here (via an
1302      * opcode helper) the addresses of subword accesses have been adjusted
1303      * to account for that, which means that watchpoints will not match.
1304      * Undo the adjustment here.
1305      */
1306     if (arm_sctlr_b(env)) {
1307         if (len == 1) {
1308             addr ^= 3;
1309         } else if (len == 2) {
1310             addr ^= 2;
1311         }
1312     }
1313
1314     return addr;
1315 }
1316
1317 void arm_debug_excp_handler(CPUState *cs)
1318 {
1319     /* Called by core code when a watchpoint or breakpoint fires;
1320      * need to check which one and raise the appropriate exception.
1321      */
1322     ARMCPU *cpu = ARM_CPU(cs);
1323     CPUARMState *env = &cpu->env;
1324     CPUWatchpoint *wp_hit = cs->watchpoint_hit;
1325
1326     if (wp_hit) {
1327         if (wp_hit->flags & BP_CPU) {
1328             bool wnr = (wp_hit->flags & BP_WATCHPOINT_HIT_WRITE) != 0;
1329             bool same_el = arm_debug_target_el(env) == arm_current_el(env);
1330
1331             cs->watchpoint_hit = NULL;
1332
1333             if (extended_addresses_enabled(env)) {
1334                 env->exception.fsr = (1 << 9) | 0x22;
1335             } else {
1336                 env->exception.fsr = 0x2;
1337             }
1338             env->exception.vaddress = wp_hit->hitaddr;
1339             raise_exception(env, EXCP_DATA_ABORT,
1340                     syn_watchpoint(same_el, 0, wnr),
1341                     arm_debug_target_el(env));
1342         }
1343     } else {
1344         uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1345         bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
1346
1347         /* (1) GDB breakpoints should be handled first.
1348          * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
1349          * since singlestep is also done by generating a debug internal
1350          * exception.
1351          */
1352         if (cpu_breakpoint_test(cs, pc, BP_GDB)
1353             || !cpu_breakpoint_test(cs, pc, BP_CPU)) {
1354             return;
1355         }
1356
1357         if (extended_addresses_enabled(env)) {
1358             env->exception.fsr = (1 << 9) | 0x22;
1359         } else {
1360             env->exception.fsr = 0x2;
1361         }
1362         /* FAR is UNKNOWN, so doesn't need setting */
1363         raise_exception(env, EXCP_PREFETCH_ABORT,
1364                         syn_breakpoint(same_el),
1365                         arm_debug_target_el(env));
1366     }
1367 }
1368
1369 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
1370    The only way to do that in TCG is a conditional branch, which clobbers
1371    all our temporaries.  For now implement these as helper functions.  */
1372
1373 /* Similarly for variable shift instructions.  */
1374
1375 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1376 {
1377     int shift = i & 0xff;
1378     if (shift >= 32) {
1379         if (shift == 32)
1380             env->CF = x & 1;
1381         else
1382             env->CF = 0;
1383         return 0;
1384     } else if (shift != 0) {
1385         env->CF = (x >> (32 - shift)) & 1;
1386         return x << shift;
1387     }
1388     return x;
1389 }
1390
1391 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1392 {
1393     int shift = i & 0xff;
1394     if (shift >= 32) {
1395         if (shift == 32)
1396             env->CF = (x >> 31) & 1;
1397         else
1398             env->CF = 0;
1399         return 0;
1400     } else if (shift != 0) {
1401         env->CF = (x >> (shift - 1)) & 1;
1402         return x >> shift;
1403     }
1404     return x;
1405 }
1406
1407 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1408 {
1409     int shift = i & 0xff;
1410     if (shift >= 32) {
1411         env->CF = (x >> 31) & 1;
1412         return (int32_t)x >> 31;
1413     } else if (shift != 0) {
1414         env->CF = (x >> (shift - 1)) & 1;
1415         return (int32_t)x >> shift;
1416     }
1417     return x;
1418 }
1419
1420 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1421 {
1422     int shift1, shift;
1423     shift1 = i & 0xff;
1424     shift = shift1 & 0x1f;
1425     if (shift == 0) {
1426         if (shift1 != 0)
1427             env->CF = (x >> 31) & 1;
1428         return x;
1429     } else {
1430         env->CF = (x >> (shift - 1)) & 1;
1431         return ((uint32_t)x >> shift) | (x << (32 - shift));
1432     }
1433 }
This page took 0.100531 seconds and 4 git commands to generate.