]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/op_helper.c
Merge remote-tracking branch 'remotes/juanquintela/tags/pull-migration-pull-request...
[mirror_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/units.h"
21 #include "qemu/log.h"
22 #include "qemu/main-loop.h"
23 #include "cpu.h"
24 #include "exec/helper-proto.h"
25 #include "internals.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
28
29 #define SIGNBIT (uint32_t)0x80000000
30 #define SIGNBIT64 ((uint64_t)1 << 63)
31
32 static CPUState *do_raise_exception(CPUARMState *env, uint32_t excp,
33 uint32_t syndrome, uint32_t target_el)
34 {
35 CPUState *cs = env_cpu(env);
36
37 if (target_el == 1 && (arm_hcr_el2_eff(env) & HCR_TGE)) {
38 /*
39 * Redirect NS EL1 exceptions to NS EL2. These are reported with
40 * their original syndrome register value, with the exception of
41 * SIMD/FP access traps, which are reported as uncategorized
42 * (see DDI0478C.a D1.10.4)
43 */
44 target_el = 2;
45 if (syn_get_ec(syndrome) == EC_ADVSIMDFPACCESSTRAP) {
46 syndrome = syn_uncategorized();
47 }
48 }
49
50 assert(!excp_is_internal(excp));
51 cs->exception_index = excp;
52 env->exception.syndrome = syndrome;
53 env->exception.target_el = target_el;
54
55 return cs;
56 }
57
58 void raise_exception(CPUARMState *env, uint32_t excp,
59 uint32_t syndrome, uint32_t target_el)
60 {
61 CPUState *cs = do_raise_exception(env, excp, syndrome, target_el);
62 cpu_loop_exit(cs);
63 }
64
65 void raise_exception_ra(CPUARMState *env, uint32_t excp, uint32_t syndrome,
66 uint32_t target_el, uintptr_t ra)
67 {
68 CPUState *cs = do_raise_exception(env, excp, syndrome, target_el);
69 cpu_loop_exit_restore(cs, ra);
70 }
71
72 uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def, void *vn,
73 uint32_t maxindex)
74 {
75 uint32_t val, shift;
76 uint64_t *table = vn;
77
78 val = 0;
79 for (shift = 0; shift < 32; shift += 8) {
80 uint32_t index = (ireg >> shift) & 0xff;
81 if (index < maxindex) {
82 uint32_t tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
83 val |= tmp << shift;
84 } else {
85 val |= def & (0xff << shift);
86 }
87 }
88 return val;
89 }
90
91 void HELPER(v8m_stackcheck)(CPUARMState *env, uint32_t newvalue)
92 {
93 /*
94 * Perform the v8M stack limit check for SP updates from translated code,
95 * raising an exception if the limit is breached.
96 */
97 if (newvalue < v7m_sp_limit(env)) {
98 CPUState *cs = env_cpu(env);
99
100 /*
101 * Stack limit exceptions are a rare case, so rather than syncing
102 * PC/condbits before the call, we use cpu_restore_state() to
103 * get them right before raising the exception.
104 */
105 cpu_restore_state(cs, GETPC(), true);
106 raise_exception(env, EXCP_STKOF, 0, 1);
107 }
108 }
109
110 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
111 {
112 uint32_t res = a + b;
113 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
114 env->QF = 1;
115 return res;
116 }
117
118 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
119 {
120 uint32_t res = a + b;
121 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
122 env->QF = 1;
123 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
124 }
125 return res;
126 }
127
128 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
129 {
130 uint32_t res = a - b;
131 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
132 env->QF = 1;
133 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
134 }
135 return res;
136 }
137
138 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
139 {
140 uint32_t res = a + b;
141 if (res < a) {
142 env->QF = 1;
143 res = ~0;
144 }
145 return res;
146 }
147
148 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
149 {
150 uint32_t res = a - b;
151 if (res > a) {
152 env->QF = 1;
153 res = 0;
154 }
155 return res;
156 }
157
158 /* Signed saturation. */
159 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
160 {
161 int32_t top;
162 uint32_t mask;
163
164 top = val >> shift;
165 mask = (1u << shift) - 1;
166 if (top > 0) {
167 env->QF = 1;
168 return mask;
169 } else if (top < -1) {
170 env->QF = 1;
171 return ~mask;
172 }
173 return val;
174 }
175
176 /* Unsigned saturation. */
177 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
178 {
179 uint32_t max;
180
181 max = (1u << shift) - 1;
182 if (val < 0) {
183 env->QF = 1;
184 return 0;
185 } else if (val > max) {
186 env->QF = 1;
187 return max;
188 }
189 return val;
190 }
191
192 /* Signed saturate. */
193 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
194 {
195 return do_ssat(env, x, shift);
196 }
197
198 /* Dual halfword signed saturate. */
199 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
200 {
201 uint32_t res;
202
203 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
204 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
205 return res;
206 }
207
208 /* Unsigned saturate. */
209 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
210 {
211 return do_usat(env, x, shift);
212 }
213
214 /* Dual halfword unsigned saturate. */
215 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
216 {
217 uint32_t res;
218
219 res = (uint16_t)do_usat(env, (int16_t)x, shift);
220 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
221 return res;
222 }
223
224 void HELPER(setend)(CPUARMState *env)
225 {
226 env->uncached_cpsr ^= CPSR_E;
227 arm_rebuild_hflags(env);
228 }
229
230 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
231 * The function returns the target EL (1-3) if the instruction is to be trapped;
232 * otherwise it returns 0 indicating it is not trapped.
233 */
234 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
235 {
236 int cur_el = arm_current_el(env);
237 uint64_t mask;
238
239 if (arm_feature(env, ARM_FEATURE_M)) {
240 /* M profile cores can never trap WFI/WFE. */
241 return 0;
242 }
243
244 /* If we are currently in EL0 then we need to check if SCTLR is set up for
245 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
246 */
247 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
248 int target_el;
249
250 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
251 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
252 /* Secure EL0 and Secure PL1 is at EL3 */
253 target_el = 3;
254 } else {
255 target_el = 1;
256 }
257
258 if (!(env->cp15.sctlr_el[target_el] & mask)) {
259 return target_el;
260 }
261 }
262
263 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
264 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
265 * bits will be zero indicating no trap.
266 */
267 if (cur_el < 2) {
268 mask = is_wfe ? HCR_TWE : HCR_TWI;
269 if (arm_hcr_el2_eff(env) & mask) {
270 return 2;
271 }
272 }
273
274 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
275 if (cur_el < 3) {
276 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
277 if (env->cp15.scr_el3 & mask) {
278 return 3;
279 }
280 }
281
282 return 0;
283 }
284
285 void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
286 {
287 CPUState *cs = env_cpu(env);
288 int target_el = check_wfx_trap(env, false);
289
290 if (cpu_has_work(cs)) {
291 /* Don't bother to go into our "low power state" if
292 * we would just wake up immediately.
293 */
294 return;
295 }
296
297 if (target_el) {
298 if (env->aarch64) {
299 env->pc -= insn_len;
300 } else {
301 env->regs[15] -= insn_len;
302 }
303
304 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2),
305 target_el);
306 }
307
308 cs->exception_index = EXCP_HLT;
309 cs->halted = 1;
310 cpu_loop_exit(cs);
311 }
312
313 void HELPER(wfe)(CPUARMState *env)
314 {
315 /* This is a hint instruction that is semantically different
316 * from YIELD even though we currently implement it identically.
317 * Don't actually halt the CPU, just yield back to top
318 * level loop. This is not going into a "low power state"
319 * (ie halting until some event occurs), so we never take
320 * a configurable trap to a different exception level.
321 */
322 HELPER(yield)(env);
323 }
324
325 void HELPER(yield)(CPUARMState *env)
326 {
327 CPUState *cs = env_cpu(env);
328
329 /* This is a non-trappable hint instruction that generally indicates
330 * that the guest is currently busy-looping. Yield control back to the
331 * top level loop so that a more deserving VCPU has a chance to run.
332 */
333 cs->exception_index = EXCP_YIELD;
334 cpu_loop_exit(cs);
335 }
336
337 /* Raise an internal-to-QEMU exception. This is limited to only
338 * those EXCP values which are special cases for QEMU to interrupt
339 * execution and not to be used for exceptions which are passed to
340 * the guest (those must all have syndrome information and thus should
341 * use exception_with_syndrome).
342 */
343 void HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
344 {
345 CPUState *cs = env_cpu(env);
346
347 assert(excp_is_internal(excp));
348 cs->exception_index = excp;
349 cpu_loop_exit(cs);
350 }
351
352 /* Raise an exception with the specified syndrome register value */
353 void HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
354 uint32_t syndrome, uint32_t target_el)
355 {
356 raise_exception(env, excp, syndrome, target_el);
357 }
358
359 /* Raise an EXCP_BKPT with the specified syndrome register value,
360 * targeting the correct exception level for debug exceptions.
361 */
362 void HELPER(exception_bkpt_insn)(CPUARMState *env, uint32_t syndrome)
363 {
364 int debug_el = arm_debug_target_el(env);
365 int cur_el = arm_current_el(env);
366
367 /* FSR will only be used if the debug target EL is AArch32. */
368 env->exception.fsr = arm_debug_exception_fsr(env);
369 /* FAR is UNKNOWN: clear vaddress to avoid potentially exposing
370 * values to the guest that it shouldn't be able to see at its
371 * exception/security level.
372 */
373 env->exception.vaddress = 0;
374 /*
375 * Other kinds of architectural debug exception are ignored if
376 * they target an exception level below the current one (in QEMU
377 * this is checked by arm_generate_debug_exceptions()). Breakpoint
378 * instructions are special because they always generate an exception
379 * to somewhere: if they can't go to the configured debug exception
380 * level they are taken to the current exception level.
381 */
382 if (debug_el < cur_el) {
383 debug_el = cur_el;
384 }
385 raise_exception(env, EXCP_BKPT, syndrome, debug_el);
386 }
387
388 uint32_t HELPER(cpsr_read)(CPUARMState *env)
389 {
390 /*
391 * We store the ARMv8 PSTATE.SS bit in env->uncached_cpsr.
392 * This is convenient for populating SPSR_ELx, but must be
393 * hidden from aarch32 mode, where it is not visible.
394 *
395 * TODO: ARMv8.4-DIT -- need to move SS somewhere else.
396 */
397 return cpsr_read(env) & ~(CPSR_EXEC | PSTATE_SS);
398 }
399
400 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
401 {
402 cpsr_write(env, val, mask, CPSRWriteByInstr);
403 /* TODO: Not all cpsr bits are relevant to hflags. */
404 arm_rebuild_hflags(env);
405 }
406
407 /* Write the CPSR for a 32-bit exception return */
408 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
409 {
410 uint32_t mask;
411
412 qemu_mutex_lock_iothread();
413 arm_call_pre_el_change_hook(env_archcpu(env));
414 qemu_mutex_unlock_iothread();
415
416 mask = aarch32_cpsr_valid_mask(env->features, &env_archcpu(env)->isar);
417 cpsr_write(env, val, mask, CPSRWriteExceptionReturn);
418
419 /* Generated code has already stored the new PC value, but
420 * without masking out its low bits, because which bits need
421 * masking depends on whether we're returning to Thumb or ARM
422 * state. Do the masking now.
423 */
424 env->regs[15] &= (env->thumb ? ~1 : ~3);
425 arm_rebuild_hflags(env);
426
427 qemu_mutex_lock_iothread();
428 arm_call_el_change_hook(env_archcpu(env));
429 qemu_mutex_unlock_iothread();
430 }
431
432 /* Access to user mode registers from privileged modes. */
433 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
434 {
435 uint32_t val;
436
437 if (regno == 13) {
438 val = env->banked_r13[BANK_USRSYS];
439 } else if (regno == 14) {
440 val = env->banked_r14[BANK_USRSYS];
441 } else if (regno >= 8
442 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
443 val = env->usr_regs[regno - 8];
444 } else {
445 val = env->regs[regno];
446 }
447 return val;
448 }
449
450 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
451 {
452 if (regno == 13) {
453 env->banked_r13[BANK_USRSYS] = val;
454 } else if (regno == 14) {
455 env->banked_r14[BANK_USRSYS] = val;
456 } else if (regno >= 8
457 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
458 env->usr_regs[regno - 8] = val;
459 } else {
460 env->regs[regno] = val;
461 }
462 }
463
464 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
465 {
466 if ((env->uncached_cpsr & CPSR_M) == mode) {
467 env->regs[13] = val;
468 } else {
469 env->banked_r13[bank_number(mode)] = val;
470 }
471 }
472
473 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
474 {
475 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
476 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
477 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
478 */
479 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
480 exception_target_el(env));
481 }
482
483 if ((env->uncached_cpsr & CPSR_M) == mode) {
484 return env->regs[13];
485 } else {
486 return env->banked_r13[bank_number(mode)];
487 }
488 }
489
490 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
491 uint32_t regno)
492 {
493 /* Raise an exception if the requested access is one of the UNPREDICTABLE
494 * cases; otherwise return. This broadly corresponds to the pseudocode
495 * BankedRegisterAccessValid() and SPSRAccessValid(),
496 * except that we have already handled some cases at translate time.
497 */
498 int curmode = env->uncached_cpsr & CPSR_M;
499
500 if (regno == 17) {
501 /* ELR_Hyp: a special case because access from tgtmode is OK */
502 if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
503 goto undef;
504 }
505 return;
506 }
507
508 if (curmode == tgtmode) {
509 goto undef;
510 }
511
512 if (tgtmode == ARM_CPU_MODE_USR) {
513 switch (regno) {
514 case 8 ... 12:
515 if (curmode != ARM_CPU_MODE_FIQ) {
516 goto undef;
517 }
518 break;
519 case 13:
520 if (curmode == ARM_CPU_MODE_SYS) {
521 goto undef;
522 }
523 break;
524 case 14:
525 if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
526 goto undef;
527 }
528 break;
529 default:
530 break;
531 }
532 }
533
534 if (tgtmode == ARM_CPU_MODE_HYP) {
535 /* SPSR_Hyp, r13_hyp: accessible from Monitor mode only */
536 if (curmode != ARM_CPU_MODE_MON) {
537 goto undef;
538 }
539 }
540
541 return;
542
543 undef:
544 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
545 exception_target_el(env));
546 }
547
548 void HELPER(msr_banked)(CPUARMState *env, uint32_t value, uint32_t tgtmode,
549 uint32_t regno)
550 {
551 msr_mrs_banked_exc_checks(env, tgtmode, regno);
552
553 switch (regno) {
554 case 16: /* SPSRs */
555 env->banked_spsr[bank_number(tgtmode)] = value;
556 break;
557 case 17: /* ELR_Hyp */
558 env->elr_el[2] = value;
559 break;
560 case 13:
561 env->banked_r13[bank_number(tgtmode)] = value;
562 break;
563 case 14:
564 env->banked_r14[r14_bank_number(tgtmode)] = value;
565 break;
566 case 8 ... 12:
567 switch (tgtmode) {
568 case ARM_CPU_MODE_USR:
569 env->usr_regs[regno - 8] = value;
570 break;
571 case ARM_CPU_MODE_FIQ:
572 env->fiq_regs[regno - 8] = value;
573 break;
574 default:
575 g_assert_not_reached();
576 }
577 break;
578 default:
579 g_assert_not_reached();
580 }
581 }
582
583 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
584 {
585 msr_mrs_banked_exc_checks(env, tgtmode, regno);
586
587 switch (regno) {
588 case 16: /* SPSRs */
589 return env->banked_spsr[bank_number(tgtmode)];
590 case 17: /* ELR_Hyp */
591 return env->elr_el[2];
592 case 13:
593 return env->banked_r13[bank_number(tgtmode)];
594 case 14:
595 return env->banked_r14[r14_bank_number(tgtmode)];
596 case 8 ... 12:
597 switch (tgtmode) {
598 case ARM_CPU_MODE_USR:
599 return env->usr_regs[regno - 8];
600 case ARM_CPU_MODE_FIQ:
601 return env->fiq_regs[regno - 8];
602 default:
603 g_assert_not_reached();
604 }
605 default:
606 g_assert_not_reached();
607 }
608 }
609
610 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome,
611 uint32_t isread)
612 {
613 const ARMCPRegInfo *ri = rip;
614 int target_el;
615
616 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
617 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
618 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
619 }
620
621 /*
622 * Check for an EL2 trap due to HSTR_EL2. We expect EL0 accesses
623 * to sysregs non accessible at EL0 to have UNDEF-ed already.
624 */
625 if (!is_a64(env) && arm_current_el(env) < 2 && ri->cp == 15 &&
626 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
627 uint32_t mask = 1 << ri->crn;
628
629 if (ri->type & ARM_CP_64BIT) {
630 mask = 1 << ri->crm;
631 }
632
633 /* T4 and T14 are RES0 */
634 mask &= ~((1 << 4) | (1 << 14));
635
636 if (env->cp15.hstr_el2 & mask) {
637 target_el = 2;
638 goto exept;
639 }
640 }
641
642 if (!ri->accessfn) {
643 return;
644 }
645
646 switch (ri->accessfn(env, ri, isread)) {
647 case CP_ACCESS_OK:
648 return;
649 case CP_ACCESS_TRAP:
650 target_el = exception_target_el(env);
651 break;
652 case CP_ACCESS_TRAP_EL2:
653 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
654 * a bug in the access function.
655 */
656 assert(!arm_is_secure(env) && arm_current_el(env) != 3);
657 target_el = 2;
658 break;
659 case CP_ACCESS_TRAP_EL3:
660 target_el = 3;
661 break;
662 case CP_ACCESS_TRAP_UNCATEGORIZED:
663 target_el = exception_target_el(env);
664 syndrome = syn_uncategorized();
665 break;
666 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
667 target_el = 2;
668 syndrome = syn_uncategorized();
669 break;
670 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
671 target_el = 3;
672 syndrome = syn_uncategorized();
673 break;
674 case CP_ACCESS_TRAP_FP_EL2:
675 target_el = 2;
676 /* Since we are an implementation that takes exceptions on a trapped
677 * conditional insn only if the insn has passed its condition code
678 * check, we take the IMPDEF choice to always report CV=1 COND=0xe
679 * (which is also the required value for AArch64 traps).
680 */
681 syndrome = syn_fp_access_trap(1, 0xe, false);
682 break;
683 case CP_ACCESS_TRAP_FP_EL3:
684 target_el = 3;
685 syndrome = syn_fp_access_trap(1, 0xe, false);
686 break;
687 default:
688 g_assert_not_reached();
689 }
690
691 exept:
692 raise_exception(env, EXCP_UDEF, syndrome, target_el);
693 }
694
695 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
696 {
697 const ARMCPRegInfo *ri = rip;
698
699 if (ri->type & ARM_CP_IO) {
700 qemu_mutex_lock_iothread();
701 ri->writefn(env, ri, value);
702 qemu_mutex_unlock_iothread();
703 } else {
704 ri->writefn(env, ri, value);
705 }
706 }
707
708 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
709 {
710 const ARMCPRegInfo *ri = rip;
711 uint32_t res;
712
713 if (ri->type & ARM_CP_IO) {
714 qemu_mutex_lock_iothread();
715 res = ri->readfn(env, ri);
716 qemu_mutex_unlock_iothread();
717 } else {
718 res = ri->readfn(env, ri);
719 }
720
721 return res;
722 }
723
724 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
725 {
726 const ARMCPRegInfo *ri = rip;
727
728 if (ri->type & ARM_CP_IO) {
729 qemu_mutex_lock_iothread();
730 ri->writefn(env, ri, value);
731 qemu_mutex_unlock_iothread();
732 } else {
733 ri->writefn(env, ri, value);
734 }
735 }
736
737 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
738 {
739 const ARMCPRegInfo *ri = rip;
740 uint64_t res;
741
742 if (ri->type & ARM_CP_IO) {
743 qemu_mutex_lock_iothread();
744 res = ri->readfn(env, ri);
745 qemu_mutex_unlock_iothread();
746 } else {
747 res = ri->readfn(env, ri);
748 }
749
750 return res;
751 }
752
753 void HELPER(pre_hvc)(CPUARMState *env)
754 {
755 ARMCPU *cpu = env_archcpu(env);
756 int cur_el = arm_current_el(env);
757 /* FIXME: Use actual secure state. */
758 bool secure = false;
759 bool undef;
760
761 if (arm_is_psci_call(cpu, EXCP_HVC)) {
762 /* If PSCI is enabled and this looks like a valid PSCI call then
763 * that overrides the architecturally mandated HVC behaviour.
764 */
765 return;
766 }
767
768 if (!arm_feature(env, ARM_FEATURE_EL2)) {
769 /* If EL2 doesn't exist, HVC always UNDEFs */
770 undef = true;
771 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
772 /* EL3.HCE has priority over EL2.HCD. */
773 undef = !(env->cp15.scr_el3 & SCR_HCE);
774 } else {
775 undef = env->cp15.hcr_el2 & HCR_HCD;
776 }
777
778 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
779 * For ARMv8/AArch64, HVC is allowed in EL3.
780 * Note that we've already trapped HVC from EL0 at translation
781 * time.
782 */
783 if (secure && (!is_a64(env) || cur_el == 1)) {
784 undef = true;
785 }
786
787 if (undef) {
788 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
789 exception_target_el(env));
790 }
791 }
792
793 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
794 {
795 ARMCPU *cpu = env_archcpu(env);
796 int cur_el = arm_current_el(env);
797 bool secure = arm_is_secure(env);
798 bool smd_flag = env->cp15.scr_el3 & SCR_SMD;
799
800 /*
801 * SMC behaviour is summarized in the following table.
802 * This helper handles the "Trap to EL2" and "Undef insn" cases.
803 * The "Trap to EL3" and "PSCI call" cases are handled in the exception
804 * helper.
805 *
806 * -> ARM_FEATURE_EL3 and !SMD
807 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
808 *
809 * Conduit SMC, valid call Trap to EL2 PSCI Call
810 * Conduit SMC, inval call Trap to EL2 Trap to EL3
811 * Conduit not SMC Trap to EL2 Trap to EL3
812 *
813 *
814 * -> ARM_FEATURE_EL3 and SMD
815 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
816 *
817 * Conduit SMC, valid call Trap to EL2 PSCI Call
818 * Conduit SMC, inval call Trap to EL2 Undef insn
819 * Conduit not SMC Trap to EL2 Undef insn
820 *
821 *
822 * -> !ARM_FEATURE_EL3
823 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
824 *
825 * Conduit SMC, valid call Trap to EL2 PSCI Call
826 * Conduit SMC, inval call Trap to EL2 Undef insn
827 * Conduit not SMC Undef insn Undef insn
828 */
829
830 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
831 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
832 * extensions, SMD only applies to NS state.
833 * On ARMv7 without the Virtualization extensions, the SMD bit
834 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
835 * so we need not special case this here.
836 */
837 bool smd = arm_feature(env, ARM_FEATURE_AARCH64) ? smd_flag
838 : smd_flag && !secure;
839
840 if (!arm_feature(env, ARM_FEATURE_EL3) &&
841 cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
842 /* If we have no EL3 then SMC always UNDEFs and can't be
843 * trapped to EL2. PSCI-via-SMC is a sort of ersatz EL3
844 * firmware within QEMU, and we want an EL2 guest to be able
845 * to forbid its EL1 from making PSCI calls into QEMU's
846 * "firmware" via HCR.TSC, so for these purposes treat
847 * PSCI-via-SMC as implying an EL3.
848 * This handles the very last line of the previous table.
849 */
850 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
851 exception_target_el(env));
852 }
853
854 if (cur_el == 1 && (arm_hcr_el2_eff(env) & HCR_TSC)) {
855 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
856 * We also want an EL2 guest to be able to forbid its EL1 from
857 * making PSCI calls into QEMU's "firmware" via HCR.TSC.
858 * This handles all the "Trap to EL2" cases of the previous table.
859 */
860 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
861 }
862
863 /* Catch the two remaining "Undef insn" cases of the previous table:
864 * - PSCI conduit is SMC but we don't have a valid PCSI call,
865 * - We don't have EL3 or SMD is set.
866 */
867 if (!arm_is_psci_call(cpu, EXCP_SMC) &&
868 (smd || !arm_feature(env, ARM_FEATURE_EL3))) {
869 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
870 exception_target_el(env));
871 }
872 }
873
874 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
875 The only way to do that in TCG is a conditional branch, which clobbers
876 all our temporaries. For now implement these as helper functions. */
877
878 /* Similarly for variable shift instructions. */
879
880 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
881 {
882 int shift = i & 0xff;
883 if (shift >= 32) {
884 if (shift == 32)
885 env->CF = x & 1;
886 else
887 env->CF = 0;
888 return 0;
889 } else if (shift != 0) {
890 env->CF = (x >> (32 - shift)) & 1;
891 return x << shift;
892 }
893 return x;
894 }
895
896 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
897 {
898 int shift = i & 0xff;
899 if (shift >= 32) {
900 if (shift == 32)
901 env->CF = (x >> 31) & 1;
902 else
903 env->CF = 0;
904 return 0;
905 } else if (shift != 0) {
906 env->CF = (x >> (shift - 1)) & 1;
907 return x >> shift;
908 }
909 return x;
910 }
911
912 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
913 {
914 int shift = i & 0xff;
915 if (shift >= 32) {
916 env->CF = (x >> 31) & 1;
917 return (int32_t)x >> 31;
918 } else if (shift != 0) {
919 env->CF = (x >> (shift - 1)) & 1;
920 return (int32_t)x >> shift;
921 }
922 return x;
923 }
924
925 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
926 {
927 int shift1, shift;
928 shift1 = i & 0xff;
929 shift = shift1 & 0x1f;
930 if (shift == 0) {
931 if (shift1 != 0)
932 env->CF = (x >> 31) & 1;
933 return x;
934 } else {
935 env->CF = (x >> (shift - 1)) & 1;
936 return ((uint32_t)x >> shift) | (x << (32 - shift));
937 }
938 }
939
940 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
941 {
942 /*
943 * Implement DC ZVA, which zeroes a fixed-length block of memory.
944 * Note that we do not implement the (architecturally mandated)
945 * alignment fault for attempts to use this on Device memory
946 * (which matches the usual QEMU behaviour of not implementing either
947 * alignment faults or any memory attribute handling).
948 */
949
950 ARMCPU *cpu = env_archcpu(env);
951 uint64_t blocklen = 4 << cpu->dcz_blocksize;
952 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
953
954 #ifndef CONFIG_USER_ONLY
955 {
956 /*
957 * Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
958 * the block size so we might have to do more than one TLB lookup.
959 * We know that in fact for any v8 CPU the page size is at least 4K
960 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
961 * 1K as an artefact of legacy v5 subpage support being present in the
962 * same QEMU executable. So in practice the hostaddr[] array has
963 * two entries, given the current setting of TARGET_PAGE_BITS_MIN.
964 */
965 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
966 void *hostaddr[DIV_ROUND_UP(2 * KiB, 1 << TARGET_PAGE_BITS_MIN)];
967 int try, i;
968 unsigned mmu_idx = cpu_mmu_index(env, false);
969 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
970
971 assert(maxidx <= ARRAY_SIZE(hostaddr));
972
973 for (try = 0; try < 2; try++) {
974
975 for (i = 0; i < maxidx; i++) {
976 hostaddr[i] = tlb_vaddr_to_host(env,
977 vaddr + TARGET_PAGE_SIZE * i,
978 1, mmu_idx);
979 if (!hostaddr[i]) {
980 break;
981 }
982 }
983 if (i == maxidx) {
984 /*
985 * If it's all in the TLB it's fair game for just writing to;
986 * we know we don't need to update dirty status, etc.
987 */
988 for (i = 0; i < maxidx - 1; i++) {
989 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
990 }
991 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
992 return;
993 }
994 /*
995 * OK, try a store and see if we can populate the tlb. This
996 * might cause an exception if the memory isn't writable,
997 * in which case we will longjmp out of here. We must for
998 * this purpose use the actual register value passed to us
999 * so that we get the fault address right.
1000 */
1001 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
1002 /* Now we can populate the other TLB entries, if any */
1003 for (i = 0; i < maxidx; i++) {
1004 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
1005 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
1006 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
1007 }
1008 }
1009 }
1010
1011 /*
1012 * Slow path (probably attempt to do this to an I/O device or
1013 * similar, or clearing of a block of code we have translations
1014 * cached for). Just do a series of byte writes as the architecture
1015 * demands. It's not worth trying to use a cpu_physical_memory_map(),
1016 * memset(), unmap() sequence here because:
1017 * + we'd need to account for the blocksize being larger than a page
1018 * + the direct-RAM access case is almost always going to be dealt
1019 * with in the fastpath code above, so there's no speed benefit
1020 * + we would have to deal with the map returning NULL because the
1021 * bounce buffer was in use
1022 */
1023 for (i = 0; i < blocklen; i++) {
1024 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
1025 }
1026 }
1027 #else
1028 memset(g2h(vaddr), 0, blocklen);
1029 #endif
1030 }