spapr: Simplify error handling in spapr_cpu_core_realize()
[qemu/ar7.git] / hw / intc / armv7m_nvic.c
blob42b1ad59e65d6e934333b58bc945df00a6836297
1 /*
2 * ARM Nested Vectored Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "cpu.h"
16 #include "hw/sysbus.h"
17 #include "migration/vmstate.h"
18 #include "qemu/timer.h"
19 #include "hw/intc/armv7m_nvic.h"
20 #include "hw/irq.h"
21 #include "hw/qdev-properties.h"
22 #include "sysemu/runstate.h"
23 #include "target/arm/cpu.h"
24 #include "exec/exec-all.h"
25 #include "exec/memop.h"
26 #include "qemu/log.h"
27 #include "qemu/module.h"
28 #include "trace.h"
30 /* IRQ number counting:
32 * the num-irq property counts the number of external IRQ lines
34 * NVICState::num_irq counts the total number of exceptions
35 * (external IRQs, the 15 internal exceptions including reset,
36 * and one for the unused exception number 0).
38 * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines.
40 * NVIC_MAX_VECTORS is the highest permitted number of exceptions.
42 * Iterating through all exceptions should typically be done with
43 * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0.
45 * The external qemu_irq lines are the NVIC's external IRQ lines,
46 * so line 0 is exception 16.
48 * In the terminology of the architecture manual, "interrupts" are
49 * a subcategory of exception referring to the external interrupts
50 * (which are exception numbers NVIC_FIRST_IRQ and upward).
51 * For historical reasons QEMU tends to use "interrupt" and
52 * "exception" more or less interchangeably.
54 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS
55 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ)
57 /* Effective running priority of the CPU when no exception is active
58 * (higher than the highest possible priority value)
60 #define NVIC_NOEXC_PRIO 0x100
61 /* Maximum priority of non-secure exceptions when AIRCR.PRIS is set */
62 #define NVIC_NS_PRIO_LIMIT 0x80
64 static const uint8_t nvic_id[] = {
65 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
68 static void signal_sysresetreq(NVICState *s)
70 if (qemu_irq_is_connected(s->sysresetreq)) {
71 qemu_irq_pulse(s->sysresetreq);
72 } else {
74 * Default behaviour if the SoC doesn't need to wire up
75 * SYSRESETREQ (eg to a system reset controller of some kind):
76 * perform a system reset via the usual QEMU API.
78 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
82 static int nvic_pending_prio(NVICState *s)
84 /* return the group priority of the current pending interrupt,
85 * or NVIC_NOEXC_PRIO if no interrupt is pending
87 return s->vectpending_prio;
90 /* Return the value of the ISCR RETTOBASE bit:
91 * 1 if there is exactly one active exception
92 * 0 if there is more than one active exception
93 * UNKNOWN if there are no active exceptions (we choose 1,
94 * which matches the choice Cortex-M3 is documented as making).
96 * NB: some versions of the documentation talk about this
97 * counting "active exceptions other than the one shown by IPSR";
98 * this is only different in the obscure corner case where guest
99 * code has manually deactivated an exception and is about
100 * to fail an exception-return integrity check. The definition
101 * above is the one from the v8M ARM ARM and is also in line
102 * with the behaviour documented for the Cortex-M3.
104 static bool nvic_rettobase(NVICState *s)
106 int irq, nhand = 0;
107 bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
109 for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) {
110 if (s->vectors[irq].active ||
111 (check_sec && irq < NVIC_INTERNAL_VECTORS &&
112 s->sec_vectors[irq].active)) {
113 nhand++;
114 if (nhand == 2) {
115 return 0;
120 return 1;
123 /* Return the value of the ISCR ISRPENDING bit:
124 * 1 if an external interrupt is pending
125 * 0 if no external interrupt is pending
127 static bool nvic_isrpending(NVICState *s)
129 int irq;
131 /* We can shortcut if the highest priority pending interrupt
132 * happens to be external or if there is nothing pending.
134 if (s->vectpending > NVIC_FIRST_IRQ) {
135 return true;
137 if (s->vectpending == 0) {
138 return false;
141 for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) {
142 if (s->vectors[irq].pending) {
143 return true;
146 return false;
149 static bool exc_is_banked(int exc)
151 /* Return true if this is one of the limited set of exceptions which
152 * are banked (and thus have state in sec_vectors[])
154 return exc == ARMV7M_EXCP_HARD ||
155 exc == ARMV7M_EXCP_MEM ||
156 exc == ARMV7M_EXCP_USAGE ||
157 exc == ARMV7M_EXCP_SVC ||
158 exc == ARMV7M_EXCP_PENDSV ||
159 exc == ARMV7M_EXCP_SYSTICK;
162 /* Return a mask word which clears the subpriority bits from
163 * a priority value for an M-profile exception, leaving only
164 * the group priority.
166 static inline uint32_t nvic_gprio_mask(NVICState *s, bool secure)
168 return ~0U << (s->prigroup[secure] + 1);
171 static bool exc_targets_secure(NVICState *s, int exc)
173 /* Return true if this non-banked exception targets Secure state. */
174 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
175 return false;
178 if (exc >= NVIC_FIRST_IRQ) {
179 return !s->itns[exc];
182 /* Function shouldn't be called for banked exceptions. */
183 assert(!exc_is_banked(exc));
185 switch (exc) {
186 case ARMV7M_EXCP_NMI:
187 case ARMV7M_EXCP_BUS:
188 return !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
189 case ARMV7M_EXCP_SECURE:
190 return true;
191 case ARMV7M_EXCP_DEBUG:
192 /* TODO: controlled by DEMCR.SDME, which we don't yet implement */
193 return false;
194 default:
195 /* reset, and reserved (unused) low exception numbers.
196 * We'll get called by code that loops through all the exception
197 * numbers, but it doesn't matter what we return here as these
198 * non-existent exceptions will never be pended or active.
200 return true;
204 static int exc_group_prio(NVICState *s, int rawprio, bool targets_secure)
206 /* Return the group priority for this exception, given its raw
207 * (group-and-subgroup) priority value and whether it is targeting
208 * secure state or not.
210 if (rawprio < 0) {
211 return rawprio;
213 rawprio &= nvic_gprio_mask(s, targets_secure);
214 /* AIRCR.PRIS causes us to squash all NS priorities into the
215 * lower half of the total range
217 if (!targets_secure &&
218 (s->cpu->env.v7m.aircr & R_V7M_AIRCR_PRIS_MASK)) {
219 rawprio = (rawprio >> 1) + NVIC_NS_PRIO_LIMIT;
221 return rawprio;
224 /* Recompute vectpending and exception_prio for a CPU which implements
225 * the Security extension
227 static void nvic_recompute_state_secure(NVICState *s)
229 int i, bank;
230 int pend_prio = NVIC_NOEXC_PRIO;
231 int active_prio = NVIC_NOEXC_PRIO;
232 int pend_irq = 0;
233 bool pending_is_s_banked = false;
234 int pend_subprio = 0;
236 /* R_CQRV: precedence is by:
237 * - lowest group priority; if both the same then
238 * - lowest subpriority; if both the same then
239 * - lowest exception number; if both the same (ie banked) then
240 * - secure exception takes precedence
241 * Compare pseudocode RawExecutionPriority.
242 * Annoyingly, now we have two prigroup values (for S and NS)
243 * we can't do the loop comparison on raw priority values.
245 for (i = 1; i < s->num_irq; i++) {
246 for (bank = M_REG_S; bank >= M_REG_NS; bank--) {
247 VecInfo *vec;
248 int prio, subprio;
249 bool targets_secure;
251 if (bank == M_REG_S) {
252 if (!exc_is_banked(i)) {
253 continue;
255 vec = &s->sec_vectors[i];
256 targets_secure = true;
257 } else {
258 vec = &s->vectors[i];
259 targets_secure = !exc_is_banked(i) && exc_targets_secure(s, i);
262 prio = exc_group_prio(s, vec->prio, targets_secure);
263 subprio = vec->prio & ~nvic_gprio_mask(s, targets_secure);
264 if (vec->enabled && vec->pending &&
265 ((prio < pend_prio) ||
266 (prio == pend_prio && prio >= 0 && subprio < pend_subprio))) {
267 pend_prio = prio;
268 pend_subprio = subprio;
269 pend_irq = i;
270 pending_is_s_banked = (bank == M_REG_S);
272 if (vec->active && prio < active_prio) {
273 active_prio = prio;
278 s->vectpending_is_s_banked = pending_is_s_banked;
279 s->vectpending = pend_irq;
280 s->vectpending_prio = pend_prio;
281 s->exception_prio = active_prio;
283 trace_nvic_recompute_state_secure(s->vectpending,
284 s->vectpending_is_s_banked,
285 s->vectpending_prio,
286 s->exception_prio);
289 /* Recompute vectpending and exception_prio */
290 static void nvic_recompute_state(NVICState *s)
292 int i;
293 int pend_prio = NVIC_NOEXC_PRIO;
294 int active_prio = NVIC_NOEXC_PRIO;
295 int pend_irq = 0;
297 /* In theory we could write one function that handled both
298 * the "security extension present" and "not present"; however
299 * the security related changes significantly complicate the
300 * recomputation just by themselves and mixing both cases together
301 * would be even worse, so we retain a separate non-secure-only
302 * version for CPUs which don't implement the security extension.
304 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
305 nvic_recompute_state_secure(s);
306 return;
309 for (i = 1; i < s->num_irq; i++) {
310 VecInfo *vec = &s->vectors[i];
312 if (vec->enabled && vec->pending && vec->prio < pend_prio) {
313 pend_prio = vec->prio;
314 pend_irq = i;
316 if (vec->active && vec->prio < active_prio) {
317 active_prio = vec->prio;
321 if (active_prio > 0) {
322 active_prio &= nvic_gprio_mask(s, false);
325 if (pend_prio > 0) {
326 pend_prio &= nvic_gprio_mask(s, false);
329 s->vectpending = pend_irq;
330 s->vectpending_prio = pend_prio;
331 s->exception_prio = active_prio;
333 trace_nvic_recompute_state(s->vectpending,
334 s->vectpending_prio,
335 s->exception_prio);
338 /* Return the current execution priority of the CPU
339 * (equivalent to the pseudocode ExecutionPriority function).
340 * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
342 static inline int nvic_exec_prio(NVICState *s)
344 CPUARMState *env = &s->cpu->env;
345 int running = NVIC_NOEXC_PRIO;
347 if (env->v7m.basepri[M_REG_NS] > 0) {
348 running = exc_group_prio(s, env->v7m.basepri[M_REG_NS], M_REG_NS);
351 if (env->v7m.basepri[M_REG_S] > 0) {
352 int basepri = exc_group_prio(s, env->v7m.basepri[M_REG_S], M_REG_S);
353 if (running > basepri) {
354 running = basepri;
358 if (env->v7m.primask[M_REG_NS]) {
359 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
360 if (running > NVIC_NS_PRIO_LIMIT) {
361 running = NVIC_NS_PRIO_LIMIT;
363 } else {
364 running = 0;
368 if (env->v7m.primask[M_REG_S]) {
369 running = 0;
372 if (env->v7m.faultmask[M_REG_NS]) {
373 if (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
374 running = -1;
375 } else {
376 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
377 if (running > NVIC_NS_PRIO_LIMIT) {
378 running = NVIC_NS_PRIO_LIMIT;
380 } else {
381 running = 0;
386 if (env->v7m.faultmask[M_REG_S]) {
387 running = (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) ? -3 : -1;
390 /* consider priority of active handler */
391 return MIN(running, s->exception_prio);
394 bool armv7m_nvic_neg_prio_requested(void *opaque, bool secure)
396 /* Return true if the requested execution priority is negative
397 * for the specified security state, ie that security state
398 * has an active NMI or HardFault or has set its FAULTMASK.
399 * Note that this is not the same as whether the execution
400 * priority is actually negative (for instance AIRCR.PRIS may
401 * mean we don't allow FAULTMASK_NS to actually make the execution
402 * priority negative). Compare pseudocode IsReqExcPriNeg().
404 NVICState *s = opaque;
406 if (s->cpu->env.v7m.faultmask[secure]) {
407 return true;
410 if (secure ? s->sec_vectors[ARMV7M_EXCP_HARD].active :
411 s->vectors[ARMV7M_EXCP_HARD].active) {
412 return true;
415 if (s->vectors[ARMV7M_EXCP_NMI].active &&
416 exc_targets_secure(s, ARMV7M_EXCP_NMI) == secure) {
417 return true;
420 return false;
423 bool armv7m_nvic_can_take_pending_exception(void *opaque)
425 NVICState *s = opaque;
427 return nvic_exec_prio(s) > nvic_pending_prio(s);
430 int armv7m_nvic_raw_execution_priority(void *opaque)
432 NVICState *s = opaque;
434 return s->exception_prio;
437 /* caller must call nvic_irq_update() after this.
438 * secure indicates the bank to use for banked exceptions (we assert if
439 * we are passed secure=true for a non-banked exception).
441 static void set_prio(NVICState *s, unsigned irq, bool secure, uint8_t prio)
443 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
444 assert(irq < s->num_irq);
446 prio &= MAKE_64BIT_MASK(8 - s->num_prio_bits, s->num_prio_bits);
448 if (secure) {
449 assert(exc_is_banked(irq));
450 s->sec_vectors[irq].prio = prio;
451 } else {
452 s->vectors[irq].prio = prio;
455 trace_nvic_set_prio(irq, secure, prio);
458 /* Return the current raw priority register value.
459 * secure indicates the bank to use for banked exceptions (we assert if
460 * we are passed secure=true for a non-banked exception).
462 static int get_prio(NVICState *s, unsigned irq, bool secure)
464 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
465 assert(irq < s->num_irq);
467 if (secure) {
468 assert(exc_is_banked(irq));
469 return s->sec_vectors[irq].prio;
470 } else {
471 return s->vectors[irq].prio;
475 /* Recompute state and assert irq line accordingly.
476 * Must be called after changes to:
477 * vec->active, vec->enabled, vec->pending or vec->prio for any vector
478 * prigroup
480 static void nvic_irq_update(NVICState *s)
482 int lvl;
483 int pend_prio;
485 nvic_recompute_state(s);
486 pend_prio = nvic_pending_prio(s);
488 /* Raise NVIC output if this IRQ would be taken, except that we
489 * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
490 * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
491 * to those CPU registers don't cause us to recalculate the NVIC
492 * pending info.
494 lvl = (pend_prio < s->exception_prio);
495 trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl);
496 qemu_set_irq(s->excpout, lvl);
500 * armv7m_nvic_clear_pending: mark the specified exception as not pending
501 * @opaque: the NVIC
502 * @irq: the exception number to mark as not pending
503 * @secure: false for non-banked exceptions or for the nonsecure
504 * version of a banked exception, true for the secure version of a banked
505 * exception.
507 * Marks the specified exception as not pending. Note that we will assert()
508 * if @secure is true and @irq does not specify one of the fixed set
509 * of architecturally banked exceptions.
511 static void armv7m_nvic_clear_pending(void *opaque, int irq, bool secure)
513 NVICState *s = (NVICState *)opaque;
514 VecInfo *vec;
516 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
518 if (secure) {
519 assert(exc_is_banked(irq));
520 vec = &s->sec_vectors[irq];
521 } else {
522 vec = &s->vectors[irq];
524 trace_nvic_clear_pending(irq, secure, vec->enabled, vec->prio);
525 if (vec->pending) {
526 vec->pending = 0;
527 nvic_irq_update(s);
531 static void do_armv7m_nvic_set_pending(void *opaque, int irq, bool secure,
532 bool derived)
534 /* Pend an exception, including possibly escalating it to HardFault.
536 * This function handles both "normal" pending of interrupts and
537 * exceptions, and also derived exceptions (ones which occur as
538 * a result of trying to take some other exception).
540 * If derived == true, the caller guarantees that we are part way through
541 * trying to take an exception (but have not yet called
542 * armv7m_nvic_acknowledge_irq() to make it active), and so:
543 * - s->vectpending is the "original exception" we were trying to take
544 * - irq is the "derived exception"
545 * - nvic_exec_prio(s) gives the priority before exception entry
546 * Here we handle the prioritization logic which the pseudocode puts
547 * in the DerivedLateArrival() function.
550 NVICState *s = (NVICState *)opaque;
551 bool banked = exc_is_banked(irq);
552 VecInfo *vec;
553 bool targets_secure;
555 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
556 assert(!secure || banked);
558 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
560 targets_secure = banked ? secure : exc_targets_secure(s, irq);
562 trace_nvic_set_pending(irq, secure, targets_secure,
563 derived, vec->enabled, vec->prio);
565 if (derived) {
566 /* Derived exceptions are always synchronous. */
567 assert(irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV);
569 if (irq == ARMV7M_EXCP_DEBUG &&
570 exc_group_prio(s, vec->prio, secure) >= nvic_exec_prio(s)) {
571 /* DebugMonitorFault, but its priority is lower than the
572 * preempted exception priority: just ignore it.
574 return;
577 if (irq == ARMV7M_EXCP_HARD && vec->prio >= s->vectpending_prio) {
578 /* If this is a terminal exception (one which means we cannot
579 * take the original exception, like a failure to read its
580 * vector table entry), then we must take the derived exception.
581 * If the derived exception can't take priority over the
582 * original exception, then we go into Lockup.
584 * For QEMU, we rely on the fact that a derived exception is
585 * terminal if and only if it's reported to us as HardFault,
586 * which saves having to have an extra argument is_terminal
587 * that we'd only use in one place.
589 cpu_abort(&s->cpu->parent_obj,
590 "Lockup: can't take terminal derived exception "
591 "(original exception priority %d)\n",
592 s->vectpending_prio);
594 /* We now continue with the same code as for a normal pending
595 * exception, which will cause us to pend the derived exception.
596 * We'll then take either the original or the derived exception
597 * based on which is higher priority by the usual mechanism
598 * for selecting the highest priority pending interrupt.
602 if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) {
603 /* If a synchronous exception is pending then it may be
604 * escalated to HardFault if:
605 * * it is equal or lower priority to current execution
606 * * it is disabled
607 * (ie we need to take it immediately but we can't do so).
608 * Asynchronous exceptions (and interrupts) simply remain pending.
610 * For QEMU, we don't have any imprecise (asynchronous) faults,
611 * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
612 * synchronous.
613 * Debug exceptions are awkward because only Debug exceptions
614 * resulting from the BKPT instruction should be escalated,
615 * but we don't currently implement any Debug exceptions other
616 * than those that result from BKPT, so we treat all debug exceptions
617 * as needing escalation.
619 * This all means we can identify whether to escalate based only on
620 * the exception number and don't (yet) need the caller to explicitly
621 * tell us whether this exception is synchronous or not.
623 int running = nvic_exec_prio(s);
624 bool escalate = false;
626 if (exc_group_prio(s, vec->prio, secure) >= running) {
627 trace_nvic_escalate_prio(irq, vec->prio, running);
628 escalate = true;
629 } else if (!vec->enabled) {
630 trace_nvic_escalate_disabled(irq);
631 escalate = true;
634 if (escalate) {
636 /* We need to escalate this exception to a synchronous HardFault.
637 * If BFHFNMINS is set then we escalate to the banked HF for
638 * the target security state of the original exception; otherwise
639 * we take a Secure HardFault.
641 irq = ARMV7M_EXCP_HARD;
642 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
643 (targets_secure ||
644 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) {
645 vec = &s->sec_vectors[irq];
646 } else {
647 vec = &s->vectors[irq];
649 if (running <= vec->prio) {
650 /* We want to escalate to HardFault but we can't take the
651 * synchronous HardFault at this point either. This is a
652 * Lockup condition due to a guest bug. We don't model
653 * Lockup, so report via cpu_abort() instead.
655 cpu_abort(&s->cpu->parent_obj,
656 "Lockup: can't escalate %d to HardFault "
657 "(current priority %d)\n", irq, running);
660 /* HF may be banked but there is only one shared HFSR */
661 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
665 if (!vec->pending) {
666 vec->pending = 1;
667 nvic_irq_update(s);
671 void armv7m_nvic_set_pending(void *opaque, int irq, bool secure)
673 do_armv7m_nvic_set_pending(opaque, irq, secure, false);
676 void armv7m_nvic_set_pending_derived(void *opaque, int irq, bool secure)
678 do_armv7m_nvic_set_pending(opaque, irq, secure, true);
681 void armv7m_nvic_set_pending_lazyfp(void *opaque, int irq, bool secure)
684 * Pend an exception during lazy FP stacking. This differs
685 * from the usual exception pending because the logic for
686 * whether we should escalate depends on the saved context
687 * in the FPCCR register, not on the current state of the CPU/NVIC.
689 NVICState *s = (NVICState *)opaque;
690 bool banked = exc_is_banked(irq);
691 VecInfo *vec;
692 bool targets_secure;
693 bool escalate = false;
695 * We will only look at bits in fpccr if this is a banked exception
696 * (in which case 'secure' tells us whether it is the S or NS version).
697 * All the bits for the non-banked exceptions are in fpccr_s.
699 uint32_t fpccr_s = s->cpu->env.v7m.fpccr[M_REG_S];
700 uint32_t fpccr = s->cpu->env.v7m.fpccr[secure];
702 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
703 assert(!secure || banked);
705 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
707 targets_secure = banked ? secure : exc_targets_secure(s, irq);
709 switch (irq) {
710 case ARMV7M_EXCP_DEBUG:
711 if (!(fpccr_s & R_V7M_FPCCR_MONRDY_MASK)) {
712 /* Ignore DebugMonitor exception */
713 return;
715 break;
716 case ARMV7M_EXCP_MEM:
717 escalate = !(fpccr & R_V7M_FPCCR_MMRDY_MASK);
718 break;
719 case ARMV7M_EXCP_USAGE:
720 escalate = !(fpccr & R_V7M_FPCCR_UFRDY_MASK);
721 break;
722 case ARMV7M_EXCP_BUS:
723 escalate = !(fpccr_s & R_V7M_FPCCR_BFRDY_MASK);
724 break;
725 case ARMV7M_EXCP_SECURE:
726 escalate = !(fpccr_s & R_V7M_FPCCR_SFRDY_MASK);
727 break;
728 default:
729 g_assert_not_reached();
732 if (escalate) {
734 * Escalate to HardFault: faults that initially targeted Secure
735 * continue to do so, even if HF normally targets NonSecure.
737 irq = ARMV7M_EXCP_HARD;
738 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
739 (targets_secure ||
740 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) {
741 vec = &s->sec_vectors[irq];
742 } else {
743 vec = &s->vectors[irq];
747 if (!vec->enabled ||
748 nvic_exec_prio(s) <= exc_group_prio(s, vec->prio, secure)) {
749 if (!(fpccr_s & R_V7M_FPCCR_HFRDY_MASK)) {
751 * We want to escalate to HardFault but the context the
752 * FP state belongs to prevents the exception pre-empting.
754 cpu_abort(&s->cpu->parent_obj,
755 "Lockup: can't escalate to HardFault during "
756 "lazy FP register stacking\n");
760 if (escalate) {
761 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
763 if (!vec->pending) {
764 vec->pending = 1;
766 * We do not call nvic_irq_update(), because we know our caller
767 * is going to handle causing us to take the exception by
768 * raising EXCP_LAZYFP, so raising the IRQ line would be
769 * pointless extra work. We just need to recompute the
770 * priorities so that armv7m_nvic_can_take_pending_exception()
771 * returns the right answer.
773 nvic_recompute_state(s);
777 /* Make pending IRQ active. */
778 void armv7m_nvic_acknowledge_irq(void *opaque)
780 NVICState *s = (NVICState *)opaque;
781 CPUARMState *env = &s->cpu->env;
782 const int pending = s->vectpending;
783 const int running = nvic_exec_prio(s);
784 VecInfo *vec;
786 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
788 if (s->vectpending_is_s_banked) {
789 vec = &s->sec_vectors[pending];
790 } else {
791 vec = &s->vectors[pending];
794 assert(vec->enabled);
795 assert(vec->pending);
797 assert(s->vectpending_prio < running);
799 trace_nvic_acknowledge_irq(pending, s->vectpending_prio);
801 vec->active = 1;
802 vec->pending = 0;
804 write_v7m_exception(env, s->vectpending);
806 nvic_irq_update(s);
809 void armv7m_nvic_get_pending_irq_info(void *opaque,
810 int *pirq, bool *ptargets_secure)
812 NVICState *s = (NVICState *)opaque;
813 const int pending = s->vectpending;
814 bool targets_secure;
816 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
818 if (s->vectpending_is_s_banked) {
819 targets_secure = true;
820 } else {
821 targets_secure = !exc_is_banked(pending) &&
822 exc_targets_secure(s, pending);
825 trace_nvic_get_pending_irq_info(pending, targets_secure);
827 *ptargets_secure = targets_secure;
828 *pirq = pending;
831 int armv7m_nvic_complete_irq(void *opaque, int irq, bool secure)
833 NVICState *s = (NVICState *)opaque;
834 VecInfo *vec = NULL;
835 int ret;
837 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
840 * For negative priorities, v8M will forcibly deactivate the appropriate
841 * NMI or HardFault regardless of what interrupt we're being asked to
842 * deactivate (compare the DeActivate() pseudocode). This is a guard
843 * against software returning from NMI or HardFault with a corrupted
844 * IPSR and leaving the CPU in a negative-priority state.
845 * v7M does not do this, but simply deactivates the requested interrupt.
847 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
848 switch (armv7m_nvic_raw_execution_priority(s)) {
849 case -1:
850 if (s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
851 vec = &s->vectors[ARMV7M_EXCP_HARD];
852 } else {
853 vec = &s->sec_vectors[ARMV7M_EXCP_HARD];
855 break;
856 case -2:
857 vec = &s->vectors[ARMV7M_EXCP_NMI];
858 break;
859 case -3:
860 vec = &s->sec_vectors[ARMV7M_EXCP_HARD];
861 break;
862 default:
863 break;
867 if (!vec) {
868 if (secure && exc_is_banked(irq)) {
869 vec = &s->sec_vectors[irq];
870 } else {
871 vec = &s->vectors[irq];
875 trace_nvic_complete_irq(irq, secure);
877 if (!vec->active) {
878 /* Tell the caller this was an illegal exception return */
879 return -1;
883 * If this is a configurable exception and it is currently
884 * targeting the opposite security state from the one we're trying
885 * to complete it for, this counts as an illegal exception return.
886 * We still need to deactivate whatever vector the logic above has
887 * selected, though, as it might not be the same as the one for the
888 * requested exception number.
890 if (!exc_is_banked(irq) && exc_targets_secure(s, irq) != secure) {
891 ret = -1;
892 } else {
893 ret = nvic_rettobase(s);
896 vec->active = 0;
897 if (vec->level) {
898 /* Re-pend the exception if it's still held high; only
899 * happens for extenal IRQs
901 assert(irq >= NVIC_FIRST_IRQ);
902 vec->pending = 1;
905 nvic_irq_update(s);
907 return ret;
910 bool armv7m_nvic_get_ready_status(void *opaque, int irq, bool secure)
913 * Return whether an exception is "ready", i.e. it is enabled and is
914 * configured at a priority which would allow it to interrupt the
915 * current execution priority.
917 * irq and secure have the same semantics as for armv7m_nvic_set_pending():
918 * for non-banked exceptions secure is always false; for banked exceptions
919 * it indicates which of the exceptions is required.
921 NVICState *s = (NVICState *)opaque;
922 bool banked = exc_is_banked(irq);
923 VecInfo *vec;
924 int running = nvic_exec_prio(s);
926 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
927 assert(!secure || banked);
930 * HardFault is an odd special case: we always check against -1,
931 * even if we're secure and HardFault has priority -3; we never
932 * need to check for enabled state.
934 if (irq == ARMV7M_EXCP_HARD) {
935 return running > -1;
938 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
940 return vec->enabled &&
941 exc_group_prio(s, vec->prio, secure) < running;
944 /* callback when external interrupt line is changed */
945 static void set_irq_level(void *opaque, int n, int level)
947 NVICState *s = opaque;
948 VecInfo *vec;
950 n += NVIC_FIRST_IRQ;
952 assert(n >= NVIC_FIRST_IRQ && n < s->num_irq);
954 trace_nvic_set_irq_level(n, level);
956 /* The pending status of an external interrupt is
957 * latched on rising edge and exception handler return.
959 * Pulsing the IRQ will always run the handler
960 * once, and the handler will re-run until the
961 * level is low when the handler completes.
963 vec = &s->vectors[n];
964 if (level != vec->level) {
965 vec->level = level;
966 if (level) {
967 armv7m_nvic_set_pending(s, n, false);
972 /* callback when external NMI line is changed */
973 static void nvic_nmi_trigger(void *opaque, int n, int level)
975 NVICState *s = opaque;
977 trace_nvic_set_nmi_level(level);
980 * The architecture doesn't specify whether NMI should share
981 * the normal-interrupt behaviour of being resampled on
982 * exception handler return. We choose not to, so just
983 * set NMI pending here and don't track the current level.
985 if (level) {
986 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false);
990 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
992 ARMCPU *cpu = s->cpu;
993 uint32_t val;
995 switch (offset) {
996 case 4: /* Interrupt Control Type. */
997 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
998 goto bad_offset;
1000 return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1;
1001 case 0xc: /* CPPWR */
1002 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1003 goto bad_offset;
1005 /* We make the IMPDEF choice that nothing can ever go into a
1006 * non-retentive power state, which allows us to RAZ/WI this.
1008 return 0;
1009 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
1011 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
1012 int i;
1014 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1015 goto bad_offset;
1017 if (!attrs.secure) {
1018 return 0;
1020 val = 0;
1021 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
1022 if (s->itns[startvec + i]) {
1023 val |= (1 << i);
1026 return val;
1028 case 0xd00: /* CPUID Base. */
1029 return cpu->midr;
1030 case 0xd04: /* Interrupt Control State (ICSR) */
1031 /* VECTACTIVE */
1032 val = cpu->env.v7m.exception;
1033 /* VECTPENDING */
1034 val |= (s->vectpending & 0xff) << 12;
1035 /* ISRPENDING - set if any external IRQ is pending */
1036 if (nvic_isrpending(s)) {
1037 val |= (1 << 22);
1039 /* RETTOBASE - set if only one handler is active */
1040 if (nvic_rettobase(s)) {
1041 val |= (1 << 11);
1043 if (attrs.secure) {
1044 /* PENDSTSET */
1045 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) {
1046 val |= (1 << 26);
1048 /* PENDSVSET */
1049 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) {
1050 val |= (1 << 28);
1052 } else {
1053 /* PENDSTSET */
1054 if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) {
1055 val |= (1 << 26);
1057 /* PENDSVSET */
1058 if (s->vectors[ARMV7M_EXCP_PENDSV].pending) {
1059 val |= (1 << 28);
1062 /* NMIPENDSET */
1063 if ((attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))
1064 && s->vectors[ARMV7M_EXCP_NMI].pending) {
1065 val |= (1 << 31);
1067 /* ISRPREEMPT: RES0 when halting debug not implemented */
1068 /* STTNS: RES0 for the Main Extension */
1069 return val;
1070 case 0xd08: /* Vector Table Offset. */
1071 return cpu->env.v7m.vecbase[attrs.secure];
1072 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
1073 val = 0xfa050000 | (s->prigroup[attrs.secure] << 8);
1074 if (attrs.secure) {
1075 /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */
1076 val |= cpu->env.v7m.aircr;
1077 } else {
1078 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1079 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If
1080 * security isn't supported then BFHFNMINS is RAO (and
1081 * the bit in env.v7m.aircr is always set).
1083 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK;
1086 return val;
1087 case 0xd10: /* System Control. */
1088 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1089 goto bad_offset;
1091 return cpu->env.v7m.scr[attrs.secure];
1092 case 0xd14: /* Configuration Control. */
1093 /* The BFHFNMIGN bit is the only non-banked bit; we
1094 * keep it in the non-secure copy of the register.
1096 val = cpu->env.v7m.ccr[attrs.secure];
1097 val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
1098 return val;
1099 case 0xd24: /* System Handler Control and State (SHCSR) */
1100 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1101 goto bad_offset;
1103 val = 0;
1104 if (attrs.secure) {
1105 if (s->sec_vectors[ARMV7M_EXCP_MEM].active) {
1106 val |= (1 << 0);
1108 if (s->sec_vectors[ARMV7M_EXCP_HARD].active) {
1109 val |= (1 << 2);
1111 if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) {
1112 val |= (1 << 3);
1114 if (s->sec_vectors[ARMV7M_EXCP_SVC].active) {
1115 val |= (1 << 7);
1117 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) {
1118 val |= (1 << 10);
1120 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) {
1121 val |= (1 << 11);
1123 if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) {
1124 val |= (1 << 12);
1126 if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) {
1127 val |= (1 << 13);
1129 if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) {
1130 val |= (1 << 15);
1132 if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) {
1133 val |= (1 << 16);
1135 if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) {
1136 val |= (1 << 18);
1138 if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) {
1139 val |= (1 << 21);
1141 /* SecureFault is not banked but is always RAZ/WI to NS */
1142 if (s->vectors[ARMV7M_EXCP_SECURE].active) {
1143 val |= (1 << 4);
1145 if (s->vectors[ARMV7M_EXCP_SECURE].enabled) {
1146 val |= (1 << 19);
1148 if (s->vectors[ARMV7M_EXCP_SECURE].pending) {
1149 val |= (1 << 20);
1151 } else {
1152 if (s->vectors[ARMV7M_EXCP_MEM].active) {
1153 val |= (1 << 0);
1155 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1156 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */
1157 if (s->vectors[ARMV7M_EXCP_HARD].active) {
1158 val |= (1 << 2);
1160 if (s->vectors[ARMV7M_EXCP_HARD].pending) {
1161 val |= (1 << 21);
1164 if (s->vectors[ARMV7M_EXCP_USAGE].active) {
1165 val |= (1 << 3);
1167 if (s->vectors[ARMV7M_EXCP_SVC].active) {
1168 val |= (1 << 7);
1170 if (s->vectors[ARMV7M_EXCP_PENDSV].active) {
1171 val |= (1 << 10);
1173 if (s->vectors[ARMV7M_EXCP_SYSTICK].active) {
1174 val |= (1 << 11);
1176 if (s->vectors[ARMV7M_EXCP_USAGE].pending) {
1177 val |= (1 << 12);
1179 if (s->vectors[ARMV7M_EXCP_MEM].pending) {
1180 val |= (1 << 13);
1182 if (s->vectors[ARMV7M_EXCP_SVC].pending) {
1183 val |= (1 << 15);
1185 if (s->vectors[ARMV7M_EXCP_MEM].enabled) {
1186 val |= (1 << 16);
1188 if (s->vectors[ARMV7M_EXCP_USAGE].enabled) {
1189 val |= (1 << 18);
1192 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1193 if (s->vectors[ARMV7M_EXCP_BUS].active) {
1194 val |= (1 << 1);
1196 if (s->vectors[ARMV7M_EXCP_BUS].pending) {
1197 val |= (1 << 14);
1199 if (s->vectors[ARMV7M_EXCP_BUS].enabled) {
1200 val |= (1 << 17);
1202 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
1203 s->vectors[ARMV7M_EXCP_NMI].active) {
1204 /* NMIACT is not present in v7M */
1205 val |= (1 << 5);
1209 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
1210 if (s->vectors[ARMV7M_EXCP_DEBUG].active) {
1211 val |= (1 << 8);
1213 return val;
1214 case 0xd2c: /* Hard Fault Status. */
1215 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1216 goto bad_offset;
1218 return cpu->env.v7m.hfsr;
1219 case 0xd30: /* Debug Fault Status. */
1220 return cpu->env.v7m.dfsr;
1221 case 0xd34: /* MMFAR MemManage Fault Address */
1222 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1223 goto bad_offset;
1225 return cpu->env.v7m.mmfar[attrs.secure];
1226 case 0xd38: /* Bus Fault Address. */
1227 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1228 goto bad_offset;
1230 if (!attrs.secure &&
1231 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1232 return 0;
1234 return cpu->env.v7m.bfar;
1235 case 0xd3c: /* Aux Fault Status. */
1236 /* TODO: Implement fault status registers. */
1237 qemu_log_mask(LOG_UNIMP,
1238 "Aux Fault status registers unimplemented\n");
1239 return 0;
1240 case 0xd40: /* PFR0. */
1241 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1242 goto bad_offset;
1244 return cpu->isar.id_pfr0;
1245 case 0xd44: /* PFR1. */
1246 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1247 goto bad_offset;
1249 return cpu->isar.id_pfr1;
1250 case 0xd48: /* DFR0. */
1251 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1252 goto bad_offset;
1254 return cpu->isar.id_dfr0;
1255 case 0xd4c: /* AFR0. */
1256 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1257 goto bad_offset;
1259 return cpu->id_afr0;
1260 case 0xd50: /* MMFR0. */
1261 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1262 goto bad_offset;
1264 return cpu->isar.id_mmfr0;
1265 case 0xd54: /* MMFR1. */
1266 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1267 goto bad_offset;
1269 return cpu->isar.id_mmfr1;
1270 case 0xd58: /* MMFR2. */
1271 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1272 goto bad_offset;
1274 return cpu->isar.id_mmfr2;
1275 case 0xd5c: /* MMFR3. */
1276 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1277 goto bad_offset;
1279 return cpu->isar.id_mmfr3;
1280 case 0xd60: /* ISAR0. */
1281 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1282 goto bad_offset;
1284 return cpu->isar.id_isar0;
1285 case 0xd64: /* ISAR1. */
1286 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1287 goto bad_offset;
1289 return cpu->isar.id_isar1;
1290 case 0xd68: /* ISAR2. */
1291 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1292 goto bad_offset;
1294 return cpu->isar.id_isar2;
1295 case 0xd6c: /* ISAR3. */
1296 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1297 goto bad_offset;
1299 return cpu->isar.id_isar3;
1300 case 0xd70: /* ISAR4. */
1301 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1302 goto bad_offset;
1304 return cpu->isar.id_isar4;
1305 case 0xd74: /* ISAR5. */
1306 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1307 goto bad_offset;
1309 return cpu->isar.id_isar5;
1310 case 0xd78: /* CLIDR */
1311 return cpu->clidr;
1312 case 0xd7c: /* CTR */
1313 return cpu->ctr;
1314 case 0xd80: /* CSSIDR */
1316 int idx = cpu->env.v7m.csselr[attrs.secure] & R_V7M_CSSELR_INDEX_MASK;
1317 return cpu->ccsidr[idx];
1319 case 0xd84: /* CSSELR */
1320 return cpu->env.v7m.csselr[attrs.secure];
1321 case 0xd88: /* CPACR */
1322 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1323 return 0;
1325 return cpu->env.v7m.cpacr[attrs.secure];
1326 case 0xd8c: /* NSACR */
1327 if (!attrs.secure || !cpu_isar_feature(aa32_vfp_simd, cpu)) {
1328 return 0;
1330 return cpu->env.v7m.nsacr;
1331 /* TODO: Implement debug registers. */
1332 case 0xd90: /* MPU_TYPE */
1333 /* Unified MPU; if the MPU is not present this value is zero */
1334 return cpu->pmsav7_dregion << 8;
1335 case 0xd94: /* MPU_CTRL */
1336 return cpu->env.v7m.mpu_ctrl[attrs.secure];
1337 case 0xd98: /* MPU_RNR */
1338 return cpu->env.pmsav7.rnr[attrs.secure];
1339 case 0xd9c: /* MPU_RBAR */
1340 case 0xda4: /* MPU_RBAR_A1 */
1341 case 0xdac: /* MPU_RBAR_A2 */
1342 case 0xdb4: /* MPU_RBAR_A3 */
1344 int region = cpu->env.pmsav7.rnr[attrs.secure];
1346 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1347 /* PMSAv8M handling of the aliases is different from v7M:
1348 * aliases A1, A2, A3 override the low two bits of the region
1349 * number in MPU_RNR, and there is no 'region' field in the
1350 * RBAR register.
1352 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1353 if (aliasno) {
1354 region = deposit32(region, 0, 2, aliasno);
1356 if (region >= cpu->pmsav7_dregion) {
1357 return 0;
1359 return cpu->env.pmsav8.rbar[attrs.secure][region];
1362 if (region >= cpu->pmsav7_dregion) {
1363 return 0;
1365 return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf);
1367 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1368 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1369 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1370 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
1372 int region = cpu->env.pmsav7.rnr[attrs.secure];
1374 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1375 /* PMSAv8M handling of the aliases is different from v7M:
1376 * aliases A1, A2, A3 override the low two bits of the region
1377 * number in MPU_RNR.
1379 int aliasno = (offset - 0xda0) / 8; /* 0..3 */
1380 if (aliasno) {
1381 region = deposit32(region, 0, 2, aliasno);
1383 if (region >= cpu->pmsav7_dregion) {
1384 return 0;
1386 return cpu->env.pmsav8.rlar[attrs.secure][region];
1389 if (region >= cpu->pmsav7_dregion) {
1390 return 0;
1392 return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) |
1393 (cpu->env.pmsav7.drsr[region] & 0xffff);
1395 case 0xdc0: /* MPU_MAIR0 */
1396 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1397 goto bad_offset;
1399 return cpu->env.pmsav8.mair0[attrs.secure];
1400 case 0xdc4: /* MPU_MAIR1 */
1401 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1402 goto bad_offset;
1404 return cpu->env.pmsav8.mair1[attrs.secure];
1405 case 0xdd0: /* SAU_CTRL */
1406 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1407 goto bad_offset;
1409 if (!attrs.secure) {
1410 return 0;
1412 return cpu->env.sau.ctrl;
1413 case 0xdd4: /* SAU_TYPE */
1414 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1415 goto bad_offset;
1417 if (!attrs.secure) {
1418 return 0;
1420 return cpu->sau_sregion;
1421 case 0xdd8: /* SAU_RNR */
1422 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1423 goto bad_offset;
1425 if (!attrs.secure) {
1426 return 0;
1428 return cpu->env.sau.rnr;
1429 case 0xddc: /* SAU_RBAR */
1431 int region = cpu->env.sau.rnr;
1433 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1434 goto bad_offset;
1436 if (!attrs.secure) {
1437 return 0;
1439 if (region >= cpu->sau_sregion) {
1440 return 0;
1442 return cpu->env.sau.rbar[region];
1444 case 0xde0: /* SAU_RLAR */
1446 int region = cpu->env.sau.rnr;
1448 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1449 goto bad_offset;
1451 if (!attrs.secure) {
1452 return 0;
1454 if (region >= cpu->sau_sregion) {
1455 return 0;
1457 return cpu->env.sau.rlar[region];
1459 case 0xde4: /* SFSR */
1460 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1461 goto bad_offset;
1463 if (!attrs.secure) {
1464 return 0;
1466 return cpu->env.v7m.sfsr;
1467 case 0xde8: /* SFAR */
1468 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1469 goto bad_offset;
1471 if (!attrs.secure) {
1472 return 0;
1474 return cpu->env.v7m.sfar;
1475 case 0xf34: /* FPCCR */
1476 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1477 return 0;
1479 if (attrs.secure) {
1480 return cpu->env.v7m.fpccr[M_REG_S];
1481 } else {
1483 * NS can read LSPEN, CLRONRET and MONRDY. It can read
1484 * BFRDY and HFRDY if AIRCR.BFHFNMINS != 0;
1485 * other non-banked bits RAZ.
1486 * TODO: MONRDY should RAZ/WI if DEMCR.SDME is set.
1488 uint32_t value = cpu->env.v7m.fpccr[M_REG_S];
1489 uint32_t mask = R_V7M_FPCCR_LSPEN_MASK |
1490 R_V7M_FPCCR_CLRONRET_MASK |
1491 R_V7M_FPCCR_MONRDY_MASK;
1493 if (s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1494 mask |= R_V7M_FPCCR_BFRDY_MASK | R_V7M_FPCCR_HFRDY_MASK;
1497 value &= mask;
1499 value |= cpu->env.v7m.fpccr[M_REG_NS];
1500 return value;
1502 case 0xf38: /* FPCAR */
1503 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1504 return 0;
1506 return cpu->env.v7m.fpcar[attrs.secure];
1507 case 0xf3c: /* FPDSCR */
1508 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1509 return 0;
1511 return cpu->env.v7m.fpdscr[attrs.secure];
1512 case 0xf40: /* MVFR0 */
1513 return cpu->isar.mvfr0;
1514 case 0xf44: /* MVFR1 */
1515 return cpu->isar.mvfr1;
1516 case 0xf48: /* MVFR2 */
1517 return cpu->isar.mvfr2;
1518 default:
1519 bad_offset:
1520 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
1521 return 0;
1525 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value,
1526 MemTxAttrs attrs)
1528 ARMCPU *cpu = s->cpu;
1530 switch (offset) {
1531 case 0xc: /* CPPWR */
1532 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1533 goto bad_offset;
1535 /* Make the IMPDEF choice to RAZ/WI this. */
1536 break;
1537 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
1539 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
1540 int i;
1542 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1543 goto bad_offset;
1545 if (!attrs.secure) {
1546 break;
1548 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
1549 s->itns[startvec + i] = (value >> i) & 1;
1551 nvic_irq_update(s);
1552 break;
1554 case 0xd04: /* Interrupt Control State (ICSR) */
1555 if (attrs.secure || cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1556 if (value & (1 << 31)) {
1557 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false);
1558 } else if (value & (1 << 30) &&
1559 arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1560 /* PENDNMICLR didn't exist in v7M */
1561 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false);
1564 if (value & (1 << 28)) {
1565 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
1566 } else if (value & (1 << 27)) {
1567 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
1569 if (value & (1 << 26)) {
1570 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
1571 } else if (value & (1 << 25)) {
1572 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
1574 break;
1575 case 0xd08: /* Vector Table Offset. */
1576 cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80;
1577 break;
1578 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
1579 if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) {
1580 if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) {
1581 if (attrs.secure ||
1582 !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) {
1583 signal_sysresetreq(s);
1586 if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) {
1587 qemu_log_mask(LOG_GUEST_ERROR,
1588 "Setting VECTCLRACTIVE when not in DEBUG mode "
1589 "is UNPREDICTABLE\n");
1591 if (value & R_V7M_AIRCR_VECTRESET_MASK) {
1592 /* NB: this bit is RES0 in v8M */
1593 qemu_log_mask(LOG_GUEST_ERROR,
1594 "Setting VECTRESET when not in DEBUG mode "
1595 "is UNPREDICTABLE\n");
1597 if (arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1598 s->prigroup[attrs.secure] =
1599 extract32(value,
1600 R_V7M_AIRCR_PRIGROUP_SHIFT,
1601 R_V7M_AIRCR_PRIGROUP_LENGTH);
1603 if (attrs.secure) {
1604 /* These bits are only writable by secure */
1605 cpu->env.v7m.aircr = value &
1606 (R_V7M_AIRCR_SYSRESETREQS_MASK |
1607 R_V7M_AIRCR_BFHFNMINS_MASK |
1608 R_V7M_AIRCR_PRIS_MASK);
1609 /* BFHFNMINS changes the priority of Secure HardFault, and
1610 * allows a pending Non-secure HardFault to preempt (which
1611 * we implement by marking it enabled).
1613 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1614 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3;
1615 s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
1616 } else {
1617 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
1618 s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
1621 nvic_irq_update(s);
1623 break;
1624 case 0xd10: /* System Control. */
1625 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1626 goto bad_offset;
1628 /* We don't implement deep-sleep so these bits are RAZ/WI.
1629 * The other bits in the register are banked.
1630 * QEMU's implementation ignores SEVONPEND and SLEEPONEXIT, which
1631 * is architecturally permitted.
1633 value &= ~(R_V7M_SCR_SLEEPDEEP_MASK | R_V7M_SCR_SLEEPDEEPS_MASK);
1634 cpu->env.v7m.scr[attrs.secure] = value;
1635 break;
1636 case 0xd14: /* Configuration Control. */
1637 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1638 goto bad_offset;
1641 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
1642 value &= (R_V7M_CCR_STKALIGN_MASK |
1643 R_V7M_CCR_BFHFNMIGN_MASK |
1644 R_V7M_CCR_DIV_0_TRP_MASK |
1645 R_V7M_CCR_UNALIGN_TRP_MASK |
1646 R_V7M_CCR_USERSETMPEND_MASK |
1647 R_V7M_CCR_NONBASETHRDENA_MASK);
1649 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1650 /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */
1651 value |= R_V7M_CCR_NONBASETHRDENA_MASK
1652 | R_V7M_CCR_STKALIGN_MASK;
1654 if (attrs.secure) {
1655 /* the BFHFNMIGN bit is not banked; keep that in the NS copy */
1656 cpu->env.v7m.ccr[M_REG_NS] =
1657 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK)
1658 | (value & R_V7M_CCR_BFHFNMIGN_MASK);
1659 value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
1662 cpu->env.v7m.ccr[attrs.secure] = value;
1663 break;
1664 case 0xd24: /* System Handler Control and State (SHCSR) */
1665 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1666 goto bad_offset;
1668 if (attrs.secure) {
1669 s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1670 /* Secure HardFault active bit cannot be written */
1671 s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1672 s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1673 s->sec_vectors[ARMV7M_EXCP_PENDSV].active =
1674 (value & (1 << 10)) != 0;
1675 s->sec_vectors[ARMV7M_EXCP_SYSTICK].active =
1676 (value & (1 << 11)) != 0;
1677 s->sec_vectors[ARMV7M_EXCP_USAGE].pending =
1678 (value & (1 << 12)) != 0;
1679 s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1680 s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1681 s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1682 s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1683 s->sec_vectors[ARMV7M_EXCP_USAGE].enabled =
1684 (value & (1 << 18)) != 0;
1685 s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1686 /* SecureFault not banked, but RAZ/WI to NS */
1687 s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0;
1688 s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0;
1689 s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0;
1690 } else {
1691 s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1692 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1693 /* HARDFAULTPENDED is not present in v7M */
1694 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1696 s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1697 s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1698 s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0;
1699 s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0;
1700 s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0;
1701 s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1702 s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1703 s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1704 s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
1706 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1707 s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0;
1708 s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0;
1709 s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1711 /* NMIACT can only be written if the write is of a zero, with
1712 * BFHFNMINS 1, and by the CPU in secure state via the NS alias.
1714 if (!attrs.secure && cpu->env.v7m.secure &&
1715 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1716 (value & (1 << 5)) == 0) {
1717 s->vectors[ARMV7M_EXCP_NMI].active = 0;
1719 /* HARDFAULTACT can only be written if the write is of a zero
1720 * to the non-secure HardFault state by the CPU in secure state.
1721 * The only case where we can be targeting the non-secure HF state
1722 * when in secure state is if this is a write via the NS alias
1723 * and BFHFNMINS is 1.
1725 if (!attrs.secure && cpu->env.v7m.secure &&
1726 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1727 (value & (1 << 2)) == 0) {
1728 s->vectors[ARMV7M_EXCP_HARD].active = 0;
1731 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
1732 s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0;
1733 nvic_irq_update(s);
1734 break;
1735 case 0xd2c: /* Hard Fault Status. */
1736 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1737 goto bad_offset;
1739 cpu->env.v7m.hfsr &= ~value; /* W1C */
1740 break;
1741 case 0xd30: /* Debug Fault Status. */
1742 cpu->env.v7m.dfsr &= ~value; /* W1C */
1743 break;
1744 case 0xd34: /* Mem Manage Address. */
1745 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1746 goto bad_offset;
1748 cpu->env.v7m.mmfar[attrs.secure] = value;
1749 return;
1750 case 0xd38: /* Bus Fault Address. */
1751 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1752 goto bad_offset;
1754 if (!attrs.secure &&
1755 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1756 return;
1758 cpu->env.v7m.bfar = value;
1759 return;
1760 case 0xd3c: /* Aux Fault Status. */
1761 qemu_log_mask(LOG_UNIMP,
1762 "NVIC: Aux fault status registers unimplemented\n");
1763 break;
1764 case 0xd84: /* CSSELR */
1765 if (!arm_v7m_csselr_razwi(cpu)) {
1766 cpu->env.v7m.csselr[attrs.secure] = value & R_V7M_CSSELR_INDEX_MASK;
1768 break;
1769 case 0xd88: /* CPACR */
1770 if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1771 /* We implement only the Floating Point extension's CP10/CP11 */
1772 cpu->env.v7m.cpacr[attrs.secure] = value & (0xf << 20);
1774 break;
1775 case 0xd8c: /* NSACR */
1776 if (attrs.secure && cpu_isar_feature(aa32_vfp_simd, cpu)) {
1777 /* We implement only the Floating Point extension's CP10/CP11 */
1778 cpu->env.v7m.nsacr = value & (3 << 10);
1780 break;
1781 case 0xd90: /* MPU_TYPE */
1782 return; /* RO */
1783 case 0xd94: /* MPU_CTRL */
1784 if ((value &
1785 (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK))
1786 == R_V7M_MPU_CTRL_HFNMIENA_MASK) {
1787 qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is "
1788 "UNPREDICTABLE\n");
1790 cpu->env.v7m.mpu_ctrl[attrs.secure]
1791 = value & (R_V7M_MPU_CTRL_ENABLE_MASK |
1792 R_V7M_MPU_CTRL_HFNMIENA_MASK |
1793 R_V7M_MPU_CTRL_PRIVDEFENA_MASK);
1794 tlb_flush(CPU(cpu));
1795 break;
1796 case 0xd98: /* MPU_RNR */
1797 if (value >= cpu->pmsav7_dregion) {
1798 qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %"
1799 PRIu32 "/%" PRIu32 "\n",
1800 value, cpu->pmsav7_dregion);
1801 } else {
1802 cpu->env.pmsav7.rnr[attrs.secure] = value;
1804 break;
1805 case 0xd9c: /* MPU_RBAR */
1806 case 0xda4: /* MPU_RBAR_A1 */
1807 case 0xdac: /* MPU_RBAR_A2 */
1808 case 0xdb4: /* MPU_RBAR_A3 */
1810 int region;
1812 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1813 /* PMSAv8M handling of the aliases is different from v7M:
1814 * aliases A1, A2, A3 override the low two bits of the region
1815 * number in MPU_RNR, and there is no 'region' field in the
1816 * RBAR register.
1818 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1820 region = cpu->env.pmsav7.rnr[attrs.secure];
1821 if (aliasno) {
1822 region = deposit32(region, 0, 2, aliasno);
1824 if (region >= cpu->pmsav7_dregion) {
1825 return;
1827 cpu->env.pmsav8.rbar[attrs.secure][region] = value;
1828 tlb_flush(CPU(cpu));
1829 return;
1832 if (value & (1 << 4)) {
1833 /* VALID bit means use the region number specified in this
1834 * value and also update MPU_RNR.REGION with that value.
1836 region = extract32(value, 0, 4);
1837 if (region >= cpu->pmsav7_dregion) {
1838 qemu_log_mask(LOG_GUEST_ERROR,
1839 "MPU region out of range %u/%" PRIu32 "\n",
1840 region, cpu->pmsav7_dregion);
1841 return;
1843 cpu->env.pmsav7.rnr[attrs.secure] = region;
1844 } else {
1845 region = cpu->env.pmsav7.rnr[attrs.secure];
1848 if (region >= cpu->pmsav7_dregion) {
1849 return;
1852 cpu->env.pmsav7.drbar[region] = value & ~0x1f;
1853 tlb_flush(CPU(cpu));
1854 break;
1856 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1857 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1858 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1859 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
1861 int region = cpu->env.pmsav7.rnr[attrs.secure];
1863 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1864 /* PMSAv8M handling of the aliases is different from v7M:
1865 * aliases A1, A2, A3 override the low two bits of the region
1866 * number in MPU_RNR.
1868 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1870 region = cpu->env.pmsav7.rnr[attrs.secure];
1871 if (aliasno) {
1872 region = deposit32(region, 0, 2, aliasno);
1874 if (region >= cpu->pmsav7_dregion) {
1875 return;
1877 cpu->env.pmsav8.rlar[attrs.secure][region] = value;
1878 tlb_flush(CPU(cpu));
1879 return;
1882 if (region >= cpu->pmsav7_dregion) {
1883 return;
1886 cpu->env.pmsav7.drsr[region] = value & 0xff3f;
1887 cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f;
1888 tlb_flush(CPU(cpu));
1889 break;
1891 case 0xdc0: /* MPU_MAIR0 */
1892 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1893 goto bad_offset;
1895 if (cpu->pmsav7_dregion) {
1896 /* Register is RES0 if no MPU regions are implemented */
1897 cpu->env.pmsav8.mair0[attrs.secure] = value;
1899 /* We don't need to do anything else because memory attributes
1900 * only affect cacheability, and we don't implement caching.
1902 break;
1903 case 0xdc4: /* MPU_MAIR1 */
1904 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1905 goto bad_offset;
1907 if (cpu->pmsav7_dregion) {
1908 /* Register is RES0 if no MPU regions are implemented */
1909 cpu->env.pmsav8.mair1[attrs.secure] = value;
1911 /* We don't need to do anything else because memory attributes
1912 * only affect cacheability, and we don't implement caching.
1914 break;
1915 case 0xdd0: /* SAU_CTRL */
1916 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1917 goto bad_offset;
1919 if (!attrs.secure) {
1920 return;
1922 cpu->env.sau.ctrl = value & 3;
1923 break;
1924 case 0xdd4: /* SAU_TYPE */
1925 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1926 goto bad_offset;
1928 break;
1929 case 0xdd8: /* SAU_RNR */
1930 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1931 goto bad_offset;
1933 if (!attrs.secure) {
1934 return;
1936 if (value >= cpu->sau_sregion) {
1937 qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %"
1938 PRIu32 "/%" PRIu32 "\n",
1939 value, cpu->sau_sregion);
1940 } else {
1941 cpu->env.sau.rnr = value;
1943 break;
1944 case 0xddc: /* SAU_RBAR */
1946 int region = cpu->env.sau.rnr;
1948 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1949 goto bad_offset;
1951 if (!attrs.secure) {
1952 return;
1954 if (region >= cpu->sau_sregion) {
1955 return;
1957 cpu->env.sau.rbar[region] = value & ~0x1f;
1958 tlb_flush(CPU(cpu));
1959 break;
1961 case 0xde0: /* SAU_RLAR */
1963 int region = cpu->env.sau.rnr;
1965 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1966 goto bad_offset;
1968 if (!attrs.secure) {
1969 return;
1971 if (region >= cpu->sau_sregion) {
1972 return;
1974 cpu->env.sau.rlar[region] = value & ~0x1c;
1975 tlb_flush(CPU(cpu));
1976 break;
1978 case 0xde4: /* SFSR */
1979 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1980 goto bad_offset;
1982 if (!attrs.secure) {
1983 return;
1985 cpu->env.v7m.sfsr &= ~value; /* W1C */
1986 break;
1987 case 0xde8: /* SFAR */
1988 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1989 goto bad_offset;
1991 if (!attrs.secure) {
1992 return;
1994 cpu->env.v7m.sfsr = value;
1995 break;
1996 case 0xf00: /* Software Triggered Interrupt Register */
1998 int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ;
2000 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
2001 goto bad_offset;
2004 if (excnum < s->num_irq) {
2005 armv7m_nvic_set_pending(s, excnum, false);
2007 break;
2009 case 0xf34: /* FPCCR */
2010 if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
2011 /* Not all bits here are banked. */
2012 uint32_t fpccr_s;
2014 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
2015 /* Don't allow setting of bits not present in v7M */
2016 value &= (R_V7M_FPCCR_LSPACT_MASK |
2017 R_V7M_FPCCR_USER_MASK |
2018 R_V7M_FPCCR_THREAD_MASK |
2019 R_V7M_FPCCR_HFRDY_MASK |
2020 R_V7M_FPCCR_MMRDY_MASK |
2021 R_V7M_FPCCR_BFRDY_MASK |
2022 R_V7M_FPCCR_MONRDY_MASK |
2023 R_V7M_FPCCR_LSPEN_MASK |
2024 R_V7M_FPCCR_ASPEN_MASK);
2026 value &= ~R_V7M_FPCCR_RES0_MASK;
2028 if (!attrs.secure) {
2029 /* Some non-banked bits are configurably writable by NS */
2030 fpccr_s = cpu->env.v7m.fpccr[M_REG_S];
2031 if (!(fpccr_s & R_V7M_FPCCR_LSPENS_MASK)) {
2032 uint32_t lspen = FIELD_EX32(value, V7M_FPCCR, LSPEN);
2033 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, LSPEN, lspen);
2035 if (!(fpccr_s & R_V7M_FPCCR_CLRONRETS_MASK)) {
2036 uint32_t cor = FIELD_EX32(value, V7M_FPCCR, CLRONRET);
2037 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, CLRONRET, cor);
2039 if ((s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2040 uint32_t hfrdy = FIELD_EX32(value, V7M_FPCCR, HFRDY);
2041 uint32_t bfrdy = FIELD_EX32(value, V7M_FPCCR, BFRDY);
2042 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, HFRDY, hfrdy);
2043 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, BFRDY, bfrdy);
2045 /* TODO MONRDY should RAZ/WI if DEMCR.SDME is set */
2047 uint32_t monrdy = FIELD_EX32(value, V7M_FPCCR, MONRDY);
2048 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, MONRDY, monrdy);
2052 * All other non-banked bits are RAZ/WI from NS; write
2053 * just the banked bits to fpccr[M_REG_NS].
2055 value &= R_V7M_FPCCR_BANKED_MASK;
2056 cpu->env.v7m.fpccr[M_REG_NS] = value;
2057 } else {
2058 fpccr_s = value;
2060 cpu->env.v7m.fpccr[M_REG_S] = fpccr_s;
2062 break;
2063 case 0xf38: /* FPCAR */
2064 if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
2065 value &= ~7;
2066 cpu->env.v7m.fpcar[attrs.secure] = value;
2068 break;
2069 case 0xf3c: /* FPDSCR */
2070 if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
2071 value &= 0x07c00000;
2072 cpu->env.v7m.fpdscr[attrs.secure] = value;
2074 break;
2075 case 0xf50: /* ICIALLU */
2076 case 0xf58: /* ICIMVAU */
2077 case 0xf5c: /* DCIMVAC */
2078 case 0xf60: /* DCISW */
2079 case 0xf64: /* DCCMVAU */
2080 case 0xf68: /* DCCMVAC */
2081 case 0xf6c: /* DCCSW */
2082 case 0xf70: /* DCCIMVAC */
2083 case 0xf74: /* DCCISW */
2084 case 0xf78: /* BPIALL */
2085 /* Cache and branch predictor maintenance: for QEMU these always NOP */
2086 break;
2087 default:
2088 bad_offset:
2089 qemu_log_mask(LOG_GUEST_ERROR,
2090 "NVIC: Bad write offset 0x%x\n", offset);
2094 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs)
2096 /* Return true if unprivileged access to this register is permitted. */
2097 switch (offset) {
2098 case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */
2099 /* For access via STIR_NS it is the NS CCR.USERSETMPEND that
2100 * controls access even though the CPU is in Secure state (I_QDKX).
2102 return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK;
2103 default:
2104 /* All other user accesses cause a BusFault unconditionally */
2105 return false;
2109 static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs)
2111 /* Behaviour for the SHPR register field for this exception:
2112 * return M_REG_NS to use the nonsecure vector (including for
2113 * non-banked exceptions), M_REG_S for the secure version of
2114 * a banked exception, and -1 if this field should RAZ/WI.
2116 switch (exc) {
2117 case ARMV7M_EXCP_MEM:
2118 case ARMV7M_EXCP_USAGE:
2119 case ARMV7M_EXCP_SVC:
2120 case ARMV7M_EXCP_PENDSV:
2121 case ARMV7M_EXCP_SYSTICK:
2122 /* Banked exceptions */
2123 return attrs.secure;
2124 case ARMV7M_EXCP_BUS:
2125 /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */
2126 if (!attrs.secure &&
2127 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2128 return -1;
2130 return M_REG_NS;
2131 case ARMV7M_EXCP_SECURE:
2132 /* Not banked, RAZ/WI from nonsecure */
2133 if (!attrs.secure) {
2134 return -1;
2136 return M_REG_NS;
2137 case ARMV7M_EXCP_DEBUG:
2138 /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */
2139 return M_REG_NS;
2140 case 8 ... 10:
2141 case 13:
2142 /* RES0 */
2143 return -1;
2144 default:
2145 /* Not reachable due to decode of SHPR register addresses */
2146 g_assert_not_reached();
2150 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr,
2151 uint64_t *data, unsigned size,
2152 MemTxAttrs attrs)
2154 NVICState *s = (NVICState *)opaque;
2155 uint32_t offset = addr;
2156 unsigned i, startvec, end;
2157 uint32_t val;
2159 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
2160 /* Generate BusFault for unprivileged accesses */
2161 return MEMTX_ERROR;
2164 switch (offset) {
2165 /* reads of set and clear both return the status */
2166 case 0x100 ... 0x13f: /* NVIC Set enable */
2167 offset += 0x80;
2168 /* fall through */
2169 case 0x180 ... 0x1bf: /* NVIC Clear enable */
2170 val = 0;
2171 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; /* vector # */
2173 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2174 if (s->vectors[startvec + i].enabled &&
2175 (attrs.secure || s->itns[startvec + i])) {
2176 val |= (1 << i);
2179 break;
2180 case 0x200 ... 0x23f: /* NVIC Set pend */
2181 offset += 0x80;
2182 /* fall through */
2183 case 0x280 ... 0x2bf: /* NVIC Clear pend */
2184 val = 0;
2185 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
2186 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2187 if (s->vectors[startvec + i].pending &&
2188 (attrs.secure || s->itns[startvec + i])) {
2189 val |= (1 << i);
2192 break;
2193 case 0x300 ... 0x33f: /* NVIC Active */
2194 val = 0;
2196 if (!arm_feature(&s->cpu->env, ARM_FEATURE_V7)) {
2197 break;
2200 startvec = 8 * (offset - 0x300) + NVIC_FIRST_IRQ; /* vector # */
2202 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2203 if (s->vectors[startvec + i].active &&
2204 (attrs.secure || s->itns[startvec + i])) {
2205 val |= (1 << i);
2208 break;
2209 case 0x400 ... 0x5ef: /* NVIC Priority */
2210 val = 0;
2211 startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */
2213 for (i = 0; i < size && startvec + i < s->num_irq; i++) {
2214 if (attrs.secure || s->itns[startvec + i]) {
2215 val |= s->vectors[startvec + i].prio << (8 * i);
2218 break;
2219 case 0xd18 ... 0xd1b: /* System Handler Priority (SHPR1) */
2220 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2221 val = 0;
2222 break;
2224 /* fall through */
2225 case 0xd1c ... 0xd23: /* System Handler Priority (SHPR2, SHPR3) */
2226 val = 0;
2227 for (i = 0; i < size; i++) {
2228 unsigned hdlidx = (offset - 0xd14) + i;
2229 int sbank = shpr_bank(s, hdlidx, attrs);
2231 if (sbank < 0) {
2232 continue;
2234 val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank));
2236 break;
2237 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
2238 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2239 val = 0;
2240 break;
2243 * The BFSR bits [15:8] are shared between security states
2244 * and we store them in the NS copy. They are RAZ/WI for
2245 * NS code if AIRCR.BFHFNMINS is 0.
2247 val = s->cpu->env.v7m.cfsr[attrs.secure];
2248 if (!attrs.secure &&
2249 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2250 val &= ~R_V7M_CFSR_BFSR_MASK;
2251 } else {
2252 val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK;
2254 val = extract32(val, (offset - 0xd28) * 8, size * 8);
2255 break;
2256 case 0xfe0 ... 0xfff: /* ID. */
2257 if (offset & 3) {
2258 val = 0;
2259 } else {
2260 val = nvic_id[(offset - 0xfe0) >> 2];
2262 break;
2263 default:
2264 if (size == 4) {
2265 val = nvic_readl(s, offset, attrs);
2266 } else {
2267 qemu_log_mask(LOG_GUEST_ERROR,
2268 "NVIC: Bad read of size %d at offset 0x%x\n",
2269 size, offset);
2270 val = 0;
2274 trace_nvic_sysreg_read(addr, val, size);
2275 *data = val;
2276 return MEMTX_OK;
2279 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr,
2280 uint64_t value, unsigned size,
2281 MemTxAttrs attrs)
2283 NVICState *s = (NVICState *)opaque;
2284 uint32_t offset = addr;
2285 unsigned i, startvec, end;
2286 unsigned setval = 0;
2288 trace_nvic_sysreg_write(addr, value, size);
2290 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
2291 /* Generate BusFault for unprivileged accesses */
2292 return MEMTX_ERROR;
2295 switch (offset) {
2296 case 0x100 ... 0x13f: /* NVIC Set enable */
2297 offset += 0x80;
2298 setval = 1;
2299 /* fall through */
2300 case 0x180 ... 0x1bf: /* NVIC Clear enable */
2301 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ;
2303 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2304 if (value & (1 << i) &&
2305 (attrs.secure || s->itns[startvec + i])) {
2306 s->vectors[startvec + i].enabled = setval;
2309 nvic_irq_update(s);
2310 goto exit_ok;
2311 case 0x200 ... 0x23f: /* NVIC Set pend */
2312 /* the special logic in armv7m_nvic_set_pending()
2313 * is not needed since IRQs are never escalated
2315 offset += 0x80;
2316 setval = 1;
2317 /* fall through */
2318 case 0x280 ... 0x2bf: /* NVIC Clear pend */
2319 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
2321 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2322 if (value & (1 << i) &&
2323 (attrs.secure || s->itns[startvec + i])) {
2324 s->vectors[startvec + i].pending = setval;
2327 nvic_irq_update(s);
2328 goto exit_ok;
2329 case 0x300 ... 0x33f: /* NVIC Active */
2330 goto exit_ok; /* R/O */
2331 case 0x400 ... 0x5ef: /* NVIC Priority */
2332 startvec = (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */
2334 for (i = 0; i < size && startvec + i < s->num_irq; i++) {
2335 if (attrs.secure || s->itns[startvec + i]) {
2336 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff);
2339 nvic_irq_update(s);
2340 goto exit_ok;
2341 case 0xd18 ... 0xd1b: /* System Handler Priority (SHPR1) */
2342 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2343 goto exit_ok;
2345 /* fall through */
2346 case 0xd1c ... 0xd23: /* System Handler Priority (SHPR2, SHPR3) */
2347 for (i = 0; i < size; i++) {
2348 unsigned hdlidx = (offset - 0xd14) + i;
2349 int newprio = extract32(value, i * 8, 8);
2350 int sbank = shpr_bank(s, hdlidx, attrs);
2352 if (sbank < 0) {
2353 continue;
2355 set_prio(s, hdlidx, sbank, newprio);
2357 nvic_irq_update(s);
2358 goto exit_ok;
2359 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
2360 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2361 goto exit_ok;
2363 /* All bits are W1C, so construct 32 bit value with 0s in
2364 * the parts not written by the access size
2366 value <<= ((offset - 0xd28) * 8);
2368 if (!attrs.secure &&
2369 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2370 /* BFSR bits are RAZ/WI for NS if BFHFNMINS is set */
2371 value &= ~R_V7M_CFSR_BFSR_MASK;
2374 s->cpu->env.v7m.cfsr[attrs.secure] &= ~value;
2375 if (attrs.secure) {
2376 /* The BFSR bits [15:8] are shared between security states
2377 * and we store them in the NS copy.
2379 s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK);
2381 goto exit_ok;
2383 if (size == 4) {
2384 nvic_writel(s, offset, value, attrs);
2385 goto exit_ok;
2387 qemu_log_mask(LOG_GUEST_ERROR,
2388 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
2389 /* This is UNPREDICTABLE; treat as RAZ/WI */
2391 exit_ok:
2392 /* Ensure any changes made are reflected in the cached hflags. */
2393 arm_rebuild_hflags(&s->cpu->env);
2394 return MEMTX_OK;
2397 static const MemoryRegionOps nvic_sysreg_ops = {
2398 .read_with_attrs = nvic_sysreg_read,
2399 .write_with_attrs = nvic_sysreg_write,
2400 .endianness = DEVICE_NATIVE_ENDIAN,
2403 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr,
2404 uint64_t value, unsigned size,
2405 MemTxAttrs attrs)
2407 MemoryRegion *mr = opaque;
2409 if (attrs.secure) {
2410 /* S accesses to the alias act like NS accesses to the real region */
2411 attrs.secure = 0;
2412 return memory_region_dispatch_write(mr, addr, value,
2413 size_memop(size) | MO_TE, attrs);
2414 } else {
2415 /* NS attrs are RAZ/WI for privileged, and BusFault for user */
2416 if (attrs.user) {
2417 return MEMTX_ERROR;
2419 return MEMTX_OK;
2423 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr,
2424 uint64_t *data, unsigned size,
2425 MemTxAttrs attrs)
2427 MemoryRegion *mr = opaque;
2429 if (attrs.secure) {
2430 /* S accesses to the alias act like NS accesses to the real region */
2431 attrs.secure = 0;
2432 return memory_region_dispatch_read(mr, addr, data,
2433 size_memop(size) | MO_TE, attrs);
2434 } else {
2435 /* NS attrs are RAZ/WI for privileged, and BusFault for user */
2436 if (attrs.user) {
2437 return MEMTX_ERROR;
2439 *data = 0;
2440 return MEMTX_OK;
2444 static const MemoryRegionOps nvic_sysreg_ns_ops = {
2445 .read_with_attrs = nvic_sysreg_ns_read,
2446 .write_with_attrs = nvic_sysreg_ns_write,
2447 .endianness = DEVICE_NATIVE_ENDIAN,
2450 static MemTxResult nvic_systick_write(void *opaque, hwaddr addr,
2451 uint64_t value, unsigned size,
2452 MemTxAttrs attrs)
2454 NVICState *s = opaque;
2455 MemoryRegion *mr;
2457 /* Direct the access to the correct systick */
2458 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
2459 return memory_region_dispatch_write(mr, addr, value,
2460 size_memop(size) | MO_TE, attrs);
2463 static MemTxResult nvic_systick_read(void *opaque, hwaddr addr,
2464 uint64_t *data, unsigned size,
2465 MemTxAttrs attrs)
2467 NVICState *s = opaque;
2468 MemoryRegion *mr;
2470 /* Direct the access to the correct systick */
2471 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
2472 return memory_region_dispatch_read(mr, addr, data, size_memop(size) | MO_TE,
2473 attrs);
2476 static const MemoryRegionOps nvic_systick_ops = {
2477 .read_with_attrs = nvic_systick_read,
2478 .write_with_attrs = nvic_systick_write,
2479 .endianness = DEVICE_NATIVE_ENDIAN,
2482 static int nvic_post_load(void *opaque, int version_id)
2484 NVICState *s = opaque;
2485 unsigned i;
2486 int resetprio;
2488 /* Check for out of range priority settings */
2489 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
2491 if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio ||
2492 s->vectors[ARMV7M_EXCP_NMI].prio != -2 ||
2493 s->vectors[ARMV7M_EXCP_HARD].prio != -1) {
2494 return 1;
2496 for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) {
2497 if (s->vectors[i].prio & ~0xff) {
2498 return 1;
2502 nvic_recompute_state(s);
2504 return 0;
2507 static const VMStateDescription vmstate_VecInfo = {
2508 .name = "armv7m_nvic_info",
2509 .version_id = 1,
2510 .minimum_version_id = 1,
2511 .fields = (VMStateField[]) {
2512 VMSTATE_INT16(prio, VecInfo),
2513 VMSTATE_UINT8(enabled, VecInfo),
2514 VMSTATE_UINT8(pending, VecInfo),
2515 VMSTATE_UINT8(active, VecInfo),
2516 VMSTATE_UINT8(level, VecInfo),
2517 VMSTATE_END_OF_LIST()
2521 static bool nvic_security_needed(void *opaque)
2523 NVICState *s = opaque;
2525 return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
2528 static int nvic_security_post_load(void *opaque, int version_id)
2530 NVICState *s = opaque;
2531 int i;
2533 /* Check for out of range priority settings */
2534 if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1
2535 && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) {
2536 /* We can't cross-check against AIRCR.BFHFNMINS as we don't know
2537 * if the CPU state has been migrated yet; a mismatch won't
2538 * cause the emulation to blow up, though.
2540 return 1;
2542 for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) {
2543 if (s->sec_vectors[i].prio & ~0xff) {
2544 return 1;
2547 return 0;
2550 static const VMStateDescription vmstate_nvic_security = {
2551 .name = "armv7m_nvic/m-security",
2552 .version_id = 1,
2553 .minimum_version_id = 1,
2554 .needed = nvic_security_needed,
2555 .post_load = &nvic_security_post_load,
2556 .fields = (VMStateField[]) {
2557 VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1,
2558 vmstate_VecInfo, VecInfo),
2559 VMSTATE_UINT32(prigroup[M_REG_S], NVICState),
2560 VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS),
2561 VMSTATE_END_OF_LIST()
2565 static const VMStateDescription vmstate_nvic = {
2566 .name = "armv7m_nvic",
2567 .version_id = 4,
2568 .minimum_version_id = 4,
2569 .post_load = &nvic_post_load,
2570 .fields = (VMStateField[]) {
2571 VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1,
2572 vmstate_VecInfo, VecInfo),
2573 VMSTATE_UINT32(prigroup[M_REG_NS], NVICState),
2574 VMSTATE_END_OF_LIST()
2576 .subsections = (const VMStateDescription*[]) {
2577 &vmstate_nvic_security,
2578 NULL
2582 static Property props_nvic[] = {
2583 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
2584 DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64),
2585 DEFINE_PROP_END_OF_LIST()
2588 static void armv7m_nvic_reset(DeviceState *dev)
2590 int resetprio;
2591 NVICState *s = NVIC(dev);
2593 memset(s->vectors, 0, sizeof(s->vectors));
2594 memset(s->sec_vectors, 0, sizeof(s->sec_vectors));
2595 s->prigroup[M_REG_NS] = 0;
2596 s->prigroup[M_REG_S] = 0;
2598 s->vectors[ARMV7M_EXCP_NMI].enabled = 1;
2599 /* MEM, BUS, and USAGE are enabled through
2600 * the System Handler Control register
2602 s->vectors[ARMV7M_EXCP_SVC].enabled = 1;
2603 s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2604 s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2606 /* DebugMonitor is enabled via DEMCR.MON_EN */
2607 s->vectors[ARMV7M_EXCP_DEBUG].enabled = 0;
2609 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
2610 s->vectors[ARMV7M_EXCP_RESET].prio = resetprio;
2611 s->vectors[ARMV7M_EXCP_NMI].prio = -2;
2612 s->vectors[ARMV7M_EXCP_HARD].prio = -1;
2614 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2615 s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1;
2616 s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1;
2617 s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2618 s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2620 /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */
2621 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
2622 /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */
2623 s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
2624 } else {
2625 s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
2628 /* Strictly speaking the reset handler should be enabled.
2629 * However, we don't simulate soft resets through the NVIC,
2630 * and the reset vector should never be pended.
2631 * So we leave it disabled to catch logic errors.
2634 s->exception_prio = NVIC_NOEXC_PRIO;
2635 s->vectpending = 0;
2636 s->vectpending_is_s_banked = false;
2637 s->vectpending_prio = NVIC_NOEXC_PRIO;
2639 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2640 memset(s->itns, 0, sizeof(s->itns));
2641 } else {
2642 /* This state is constant and not guest accessible in a non-security
2643 * NVIC; we set the bits to true to avoid having to do a feature
2644 * bit check in the NVIC enable/pend/etc register accessors.
2646 int i;
2648 for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) {
2649 s->itns[i] = true;
2654 * We updated state that affects the CPU's MMUidx and thus its hflags;
2655 * and we can't guarantee that we run before the CPU reset function.
2657 arm_rebuild_hflags(&s->cpu->env);
2660 static void nvic_systick_trigger(void *opaque, int n, int level)
2662 NVICState *s = opaque;
2664 if (level) {
2665 /* SysTick just asked us to pend its exception.
2666 * (This is different from an external interrupt line's
2667 * behaviour.)
2668 * n == 0 : NonSecure systick
2669 * n == 1 : Secure systick
2671 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n);
2675 static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
2677 NVICState *s = NVIC(dev);
2678 int regionlen;
2680 /* The armv7m container object will have set our CPU pointer */
2681 if (!s->cpu || !arm_feature(&s->cpu->env, ARM_FEATURE_M)) {
2682 error_setg(errp, "The NVIC can only be used with a Cortex-M CPU");
2683 return;
2686 if (s->num_irq > NVIC_MAX_IRQ) {
2687 error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);
2688 return;
2691 qdev_init_gpio_in(dev, set_irq_level, s->num_irq);
2693 /* include space for internal exception vectors */
2694 s->num_irq += NVIC_FIRST_IRQ;
2696 s->num_prio_bits = arm_feature(&s->cpu->env, ARM_FEATURE_V7) ? 8 : 2;
2698 if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), errp)) {
2699 return;
2701 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0,
2702 qdev_get_gpio_in_named(dev, "systick-trigger",
2703 M_REG_NS));
2705 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2706 /* We couldn't init the secure systick device in instance_init
2707 * as we didn't know then if the CPU had the security extensions;
2708 * so we have to do it here.
2710 object_initialize_child(OBJECT(dev), "systick-reg-s",
2711 &s->systick[M_REG_S], TYPE_SYSTICK);
2713 if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_S]), errp)) {
2714 return;
2716 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0,
2717 qdev_get_gpio_in_named(dev, "systick-trigger",
2718 M_REG_S));
2721 /* The NVIC and System Control Space (SCS) starts at 0xe000e000
2722 * and looks like this:
2723 * 0x004 - ICTR
2724 * 0x010 - 0xff - systick
2725 * 0x100..0x7ec - NVIC
2726 * 0x7f0..0xcff - Reserved
2727 * 0xd00..0xd3c - SCS registers
2728 * 0xd40..0xeff - Reserved or Not implemented
2729 * 0xf00 - STIR
2731 * Some registers within this space are banked between security states.
2732 * In v8M there is a second range 0xe002e000..0xe002efff which is the
2733 * NonSecure alias SCS; secure accesses to this behave like NS accesses
2734 * to the main SCS range, and non-secure accesses (including when
2735 * the security extension is not implemented) are RAZ/WI.
2736 * Note that both the main SCS range and the alias range are defined
2737 * to be exempt from memory attribution (R_BLJT) and so the memory
2738 * transaction attribute always matches the current CPU security
2739 * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops
2740 * wrappers we change attrs.secure to indicate the NS access; so
2741 * generally code determining which banked register to use should
2742 * use attrs.secure; code determining actual behaviour of the system
2743 * should use env->v7m.secure.
2745 regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000;
2746 memory_region_init(&s->container, OBJECT(s), "nvic", regionlen);
2747 /* The system register region goes at the bottom of the priority
2748 * stack as it covers the whole page.
2750 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
2751 "nvic_sysregs", 0x1000);
2752 memory_region_add_subregion(&s->container, 0, &s->sysregmem);
2754 memory_region_init_io(&s->systickmem, OBJECT(s),
2755 &nvic_systick_ops, s,
2756 "nvic_systick", 0xe0);
2758 memory_region_add_subregion_overlap(&s->container, 0x10,
2759 &s->systickmem, 1);
2761 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
2762 memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
2763 &nvic_sysreg_ns_ops, &s->sysregmem,
2764 "nvic_sysregs_ns", 0x1000);
2765 memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem);
2766 memory_region_init_io(&s->systick_ns_mem, OBJECT(s),
2767 &nvic_sysreg_ns_ops, &s->systickmem,
2768 "nvic_systick_ns", 0xe0);
2769 memory_region_add_subregion_overlap(&s->container, 0x20010,
2770 &s->systick_ns_mem, 1);
2773 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
2776 static void armv7m_nvic_instance_init(Object *obj)
2778 /* We have a different default value for the num-irq property
2779 * than our superclass. This function runs after qdev init
2780 * has set the defaults from the Property array and before
2781 * any user-specified property setting, so just modify the
2782 * value in the GICState struct.
2784 DeviceState *dev = DEVICE(obj);
2785 NVICState *nvic = NVIC(obj);
2786 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
2788 object_initialize_child(obj, "systick-reg-ns", &nvic->systick[M_REG_NS],
2789 TYPE_SYSTICK);
2790 /* We can't initialize the secure systick here, as we don't know
2791 * yet if we need it.
2794 sysbus_init_irq(sbd, &nvic->excpout);
2795 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
2796 qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger",
2797 M_REG_NUM_BANKS);
2798 qdev_init_gpio_in_named(dev, nvic_nmi_trigger, "NMI", 1);
2801 static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
2803 DeviceClass *dc = DEVICE_CLASS(klass);
2805 dc->vmsd = &vmstate_nvic;
2806 device_class_set_props(dc, props_nvic);
2807 dc->reset = armv7m_nvic_reset;
2808 dc->realize = armv7m_nvic_realize;
2811 static const TypeInfo armv7m_nvic_info = {
2812 .name = TYPE_NVIC,
2813 .parent = TYPE_SYS_BUS_DEVICE,
2814 .instance_init = armv7m_nvic_instance_init,
2815 .instance_size = sizeof(NVICState),
2816 .class_init = armv7m_nvic_class_init,
2817 .class_size = sizeof(SysBusDeviceClass),
2820 static void armv7m_nvic_register_types(void)
2822 type_register_static(&armv7m_nvic_info);
2825 type_init(armv7m_nvic_register_types)