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