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