xref: /openbmc/qemu/hw/intc/armv7m_nvic.c (revision 7caad65756c0afaf4b238b068ab61481eb68a1dc)
1 /*
2  * ARM Nested Vectored Interrupt Controller
3  *
4  * Copyright (c) 2006-2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GPL.
8  *
9  * The ARMv7M System controller is fairly tightly tied in with the
10  * NVIC.  Much of that is also implemented here.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "hw/sysbus.h"
16 #include "migration/vmstate.h"
17 #include "qemu/timer.h"
18 #include "hw/intc/armv7m_nvic.h"
19 #include "hw/irq.h"
20 #include "hw/qdev-properties.h"
21 #include "sysemu/runstate.h"
22 #include "target/arm/cpu.h"
23 #include "exec/exec-all.h"
24 #include "exec/memop.h"
25 #include "qemu/log.h"
26 #include "qemu/module.h"
27 #include "trace.h"
28 
29 /* IRQ number counting:
30  *
31  * the num-irq property counts the number of external IRQ lines
32  *
33  * NVICState::num_irq counts the total number of exceptions
34  * (external IRQs, the 15 internal exceptions including reset,
35  * and one for the unused exception number 0).
36  *
37  * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines.
38  *
39  * NVIC_MAX_VECTORS is the highest permitted number of exceptions.
40  *
41  * Iterating through all exceptions should typically be done with
42  * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0.
43  *
44  * The external qemu_irq lines are the NVIC's external IRQ lines,
45  * so line 0 is exception 16.
46  *
47  * In the terminology of the architecture manual, "interrupts" are
48  * a subcategory of exception referring to the external interrupts
49  * (which are exception numbers NVIC_FIRST_IRQ and upward).
50  * For historical reasons QEMU tends to use "interrupt" and
51  * "exception" more or less interchangeably.
52  */
53 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS
54 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ)
55 
56 /* Effective running priority of the CPU when no exception is active
57  * (higher than the highest possible priority value)
58  */
59 #define NVIC_NOEXC_PRIO 0x100
60 /* Maximum priority of non-secure exceptions when AIRCR.PRIS is set */
61 #define NVIC_NS_PRIO_LIMIT 0x80
62 
63 static const uint8_t nvic_id[] = {
64     0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
65 };
66 
67 static void signal_sysresetreq(NVICState *s)
68 {
69     if (qemu_irq_is_connected(s->sysresetreq)) {
70         qemu_irq_pulse(s->sysresetreq);
71     } else {
72         /*
73          * Default behaviour if the SoC doesn't need to wire up
74          * SYSRESETREQ (eg to a system reset controller of some kind):
75          * perform a system reset via the usual QEMU API.
76          */
77         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
78     }
79 }
80 
81 static int nvic_pending_prio(NVICState *s)
82 {
83     /* return the group priority of the current pending interrupt,
84      * or NVIC_NOEXC_PRIO if no interrupt is pending
85      */
86     return s->vectpending_prio;
87 }
88 
89 /* Return the value of the ISCR RETTOBASE bit:
90  * 1 if there is exactly one active exception
91  * 0 if there is more than one active exception
92  * UNKNOWN if there are no active exceptions (we choose 1,
93  * which matches the choice Cortex-M3 is documented as making).
94  *
95  * NB: some versions of the documentation talk about this
96  * counting "active exceptions other than the one shown by IPSR";
97  * this is only different in the obscure corner case where guest
98  * code has manually deactivated an exception and is about
99  * to fail an exception-return integrity check. The definition
100  * above is the one from the v8M ARM ARM and is also in line
101  * with the behaviour documented for the Cortex-M3.
102  */
103 static bool nvic_rettobase(NVICState *s)
104 {
105     int irq, nhand = 0;
106     bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
107 
108     for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) {
109         if (s->vectors[irq].active ||
110             (check_sec && irq < NVIC_INTERNAL_VECTORS &&
111              s->sec_vectors[irq].active)) {
112             nhand++;
113             if (nhand == 2) {
114                 return 0;
115             }
116         }
117     }
118 
119     return 1;
120 }
121 
122 /* Return the value of the ISCR ISRPENDING bit:
123  * 1 if an external interrupt is pending
124  * 0 if no external interrupt is pending
125  */
126 static bool nvic_isrpending(NVICState *s)
127 {
128     int irq;
129 
130     /*
131      * We can shortcut if the highest priority pending interrupt
132      * happens to be external; if not we need to check the whole
133      * vectors[] array.
134      */
135     if (s->vectpending > NVIC_FIRST_IRQ) {
136         return true;
137     }
138 
139     for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) {
140         if (s->vectors[irq].pending) {
141             return true;
142         }
143     }
144     return false;
145 }
146 
147 static bool exc_is_banked(int exc)
148 {
149     /* Return true if this is one of the limited set of exceptions which
150      * are banked (and thus have state in sec_vectors[])
151      */
152     return exc == ARMV7M_EXCP_HARD ||
153         exc == ARMV7M_EXCP_MEM ||
154         exc == ARMV7M_EXCP_USAGE ||
155         exc == ARMV7M_EXCP_SVC ||
156         exc == ARMV7M_EXCP_PENDSV ||
157         exc == ARMV7M_EXCP_SYSTICK;
158 }
159 
160 /* Return a mask word which clears the subpriority bits from
161  * a priority value for an M-profile exception, leaving only
162  * the group priority.
163  */
164 static inline uint32_t nvic_gprio_mask(NVICState *s, bool secure)
165 {
166     return ~0U << (s->prigroup[secure] + 1);
167 }
168 
169 static bool exc_targets_secure(NVICState *s, int exc)
170 {
171     /* Return true if this non-banked exception targets Secure state. */
172     if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
173         return false;
174     }
175 
176     if (exc >= NVIC_FIRST_IRQ) {
177         return !s->itns[exc];
178     }
179 
180     /* Function shouldn't be called for banked exceptions. */
181     assert(!exc_is_banked(exc));
182 
183     switch (exc) {
184     case ARMV7M_EXCP_NMI:
185     case ARMV7M_EXCP_BUS:
186         return !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
187     case ARMV7M_EXCP_SECURE:
188         return true;
189     case ARMV7M_EXCP_DEBUG:
190         /* TODO: controlled by DEMCR.SDME, which we don't yet implement */
191         return false;
192     default:
193         /* reset, and reserved (unused) low exception numbers.
194          * We'll get called by code that loops through all the exception
195          * numbers, but it doesn't matter what we return here as these
196          * non-existent exceptions will never be pended or active.
197          */
198         return true;
199     }
200 }
201 
202 static int exc_group_prio(NVICState *s, int rawprio, bool targets_secure)
203 {
204     /* Return the group priority for this exception, given its raw
205      * (group-and-subgroup) priority value and whether it is targeting
206      * secure state or not.
207      */
208     if (rawprio < 0) {
209         return rawprio;
210     }
211     rawprio &= nvic_gprio_mask(s, targets_secure);
212     /* AIRCR.PRIS causes us to squash all NS priorities into the
213      * lower half of the total range
214      */
215     if (!targets_secure &&
216         (s->cpu->env.v7m.aircr & R_V7M_AIRCR_PRIS_MASK)) {
217         rawprio = (rawprio >> 1) + NVIC_NS_PRIO_LIMIT;
218     }
219     return rawprio;
220 }
221 
222 /* Recompute vectpending and exception_prio for a CPU which implements
223  * the Security extension
224  */
225 static void nvic_recompute_state_secure(NVICState *s)
226 {
227     int i, bank;
228     int pend_prio = NVIC_NOEXC_PRIO;
229     int active_prio = NVIC_NOEXC_PRIO;
230     int pend_irq = 0;
231     bool pending_is_s_banked = false;
232     int pend_subprio = 0;
233 
234     /* R_CQRV: precedence is by:
235      *  - lowest group priority; if both the same then
236      *  - lowest subpriority; if both the same then
237      *  - lowest exception number; if both the same (ie banked) then
238      *  - secure exception takes precedence
239      * Compare pseudocode RawExecutionPriority.
240      * Annoyingly, now we have two prigroup values (for S and NS)
241      * we can't do the loop comparison on raw priority values.
242      */
243     for (i = 1; i < s->num_irq; i++) {
244         for (bank = M_REG_S; bank >= M_REG_NS; bank--) {
245             VecInfo *vec;
246             int prio, subprio;
247             bool targets_secure;
248 
249             if (bank == M_REG_S) {
250                 if (!exc_is_banked(i)) {
251                     continue;
252                 }
253                 vec = &s->sec_vectors[i];
254                 targets_secure = true;
255             } else {
256                 vec = &s->vectors[i];
257                 targets_secure = !exc_is_banked(i) && exc_targets_secure(s, i);
258             }
259 
260             prio = exc_group_prio(s, vec->prio, targets_secure);
261             subprio = vec->prio & ~nvic_gprio_mask(s, targets_secure);
262             if (vec->enabled && vec->pending &&
263                 ((prio < pend_prio) ||
264                  (prio == pend_prio && prio >= 0 && subprio < pend_subprio))) {
265                 pend_prio = prio;
266                 pend_subprio = subprio;
267                 pend_irq = i;
268                 pending_is_s_banked = (bank == M_REG_S);
269             }
270             if (vec->active && prio < active_prio) {
271                 active_prio = prio;
272             }
273         }
274     }
275 
276     s->vectpending_is_s_banked = pending_is_s_banked;
277     s->vectpending = pend_irq;
278     s->vectpending_prio = pend_prio;
279     s->exception_prio = active_prio;
280 
281     trace_nvic_recompute_state_secure(s->vectpending,
282                                       s->vectpending_is_s_banked,
283                                       s->vectpending_prio,
284                                       s->exception_prio);
285 }
286 
287 /* Recompute vectpending and exception_prio */
288 static void nvic_recompute_state(NVICState *s)
289 {
290     int i;
291     int pend_prio = NVIC_NOEXC_PRIO;
292     int active_prio = NVIC_NOEXC_PRIO;
293     int pend_irq = 0;
294 
295     /* In theory we could write one function that handled both
296      * the "security extension present" and "not present"; however
297      * the security related changes significantly complicate the
298      * recomputation just by themselves and mixing both cases together
299      * would be even worse, so we retain a separate non-secure-only
300      * version for CPUs which don't implement the security extension.
301      */
302     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
303         nvic_recompute_state_secure(s);
304         return;
305     }
306 
307     for (i = 1; i < s->num_irq; i++) {
308         VecInfo *vec = &s->vectors[i];
309 
310         if (vec->enabled && vec->pending && vec->prio < pend_prio) {
311             pend_prio = vec->prio;
312             pend_irq = i;
313         }
314         if (vec->active && vec->prio < active_prio) {
315             active_prio = vec->prio;
316         }
317     }
318 
319     if (active_prio > 0) {
320         active_prio &= nvic_gprio_mask(s, false);
321     }
322 
323     if (pend_prio > 0) {
324         pend_prio &= nvic_gprio_mask(s, false);
325     }
326 
327     s->vectpending = pend_irq;
328     s->vectpending_prio = pend_prio;
329     s->exception_prio = active_prio;
330 
331     trace_nvic_recompute_state(s->vectpending,
332                                s->vectpending_prio,
333                                s->exception_prio);
334 }
335 
336 /* Return the current execution priority of the CPU
337  * (equivalent to the pseudocode ExecutionPriority function).
338  * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
339  */
340 static inline int nvic_exec_prio(NVICState *s)
341 {
342     CPUARMState *env = &s->cpu->env;
343     int running = NVIC_NOEXC_PRIO;
344 
345     if (env->v7m.basepri[M_REG_NS] > 0) {
346         running = exc_group_prio(s, env->v7m.basepri[M_REG_NS], M_REG_NS);
347     }
348 
349     if (env->v7m.basepri[M_REG_S] > 0) {
350         int basepri = exc_group_prio(s, env->v7m.basepri[M_REG_S], M_REG_S);
351         if (running > basepri) {
352             running = basepri;
353         }
354     }
355 
356     if (env->v7m.primask[M_REG_NS]) {
357         if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
358             if (running > NVIC_NS_PRIO_LIMIT) {
359                 running = NVIC_NS_PRIO_LIMIT;
360             }
361         } else {
362             running = 0;
363         }
364     }
365 
366     if (env->v7m.primask[M_REG_S]) {
367         running = 0;
368     }
369 
370     if (env->v7m.faultmask[M_REG_NS]) {
371         if (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
372             running = -1;
373         } else {
374             if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
375                 if (running > NVIC_NS_PRIO_LIMIT) {
376                     running = NVIC_NS_PRIO_LIMIT;
377                 }
378             } else {
379                 running = 0;
380             }
381         }
382     }
383 
384     if (env->v7m.faultmask[M_REG_S]) {
385         running = (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) ? -3 : -1;
386     }
387 
388     /* consider priority of active handler */
389     return MIN(running, s->exception_prio);
390 }
391 
392 bool armv7m_nvic_neg_prio_requested(void *opaque, bool secure)
393 {
394     /* Return true if the requested execution priority is negative
395      * for the specified security state, ie that security state
396      * has an active NMI or HardFault or has set its FAULTMASK.
397      * Note that this is not the same as whether the execution
398      * priority is actually negative (for instance AIRCR.PRIS may
399      * mean we don't allow FAULTMASK_NS to actually make the execution
400      * priority negative). Compare pseudocode IsReqExcPriNeg().
401      */
402     NVICState *s = opaque;
403 
404     if (s->cpu->env.v7m.faultmask[secure]) {
405         return true;
406     }
407 
408     if (secure ? s->sec_vectors[ARMV7M_EXCP_HARD].active :
409         s->vectors[ARMV7M_EXCP_HARD].active) {
410         return true;
411     }
412 
413     if (s->vectors[ARMV7M_EXCP_NMI].active &&
414         exc_targets_secure(s, ARMV7M_EXCP_NMI) == secure) {
415         return true;
416     }
417 
418     return false;
419 }
420 
421 bool armv7m_nvic_can_take_pending_exception(void *opaque)
422 {
423     NVICState *s = opaque;
424 
425     return nvic_exec_prio(s) > nvic_pending_prio(s);
426 }
427 
428 int armv7m_nvic_raw_execution_priority(void *opaque)
429 {
430     NVICState *s = opaque;
431 
432     return s->exception_prio;
433 }
434 
435 /* caller must call nvic_irq_update() after this.
436  * secure indicates the bank to use for banked exceptions (we assert if
437  * we are passed secure=true for a non-banked exception).
438  */
439 static void set_prio(NVICState *s, unsigned irq, bool secure, uint8_t prio)
440 {
441     assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
442     assert(irq < s->num_irq);
443 
444     prio &= MAKE_64BIT_MASK(8 - s->num_prio_bits, s->num_prio_bits);
445 
446     if (secure) {
447         assert(exc_is_banked(irq));
448         s->sec_vectors[irq].prio = prio;
449     } else {
450         s->vectors[irq].prio = prio;
451     }
452 
453     trace_nvic_set_prio(irq, secure, prio);
454 }
455 
456 /* Return the current raw priority register value.
457  * secure indicates the bank to use for banked exceptions (we assert if
458  * we are passed secure=true for a non-banked exception).
459  */
460 static int get_prio(NVICState *s, unsigned irq, bool secure)
461 {
462     assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
463     assert(irq < s->num_irq);
464 
465     if (secure) {
466         assert(exc_is_banked(irq));
467         return s->sec_vectors[irq].prio;
468     } else {
469         return s->vectors[irq].prio;
470     }
471 }
472 
473 /* Recompute state and assert irq line accordingly.
474  * Must be called after changes to:
475  *  vec->active, vec->enabled, vec->pending or vec->prio for any vector
476  *  prigroup
477  */
478 static void nvic_irq_update(NVICState *s)
479 {
480     int lvl;
481     int pend_prio;
482 
483     nvic_recompute_state(s);
484     pend_prio = nvic_pending_prio(s);
485 
486     /* Raise NVIC output if this IRQ would be taken, except that we
487      * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
488      * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
489      * to those CPU registers don't cause us to recalculate the NVIC
490      * pending info.
491      */
492     lvl = (pend_prio < s->exception_prio);
493     trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl);
494     qemu_set_irq(s->excpout, lvl);
495 }
496 
497 /**
498  * armv7m_nvic_clear_pending: mark the specified exception as not pending
499  * @opaque: the NVIC
500  * @irq: the exception number to mark as not pending
501  * @secure: false for non-banked exceptions or for the nonsecure
502  * version of a banked exception, true for the secure version of a banked
503  * exception.
504  *
505  * Marks the specified exception as not pending. Note that we will assert()
506  * if @secure is true and @irq does not specify one of the fixed set
507  * of architecturally banked exceptions.
508  */
509 static void armv7m_nvic_clear_pending(void *opaque, int irq, bool secure)
510 {
511     NVICState *s = (NVICState *)opaque;
512     VecInfo *vec;
513 
514     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
515 
516     if (secure) {
517         assert(exc_is_banked(irq));
518         vec = &s->sec_vectors[irq];
519     } else {
520         vec = &s->vectors[irq];
521     }
522     trace_nvic_clear_pending(irq, secure, vec->enabled, vec->prio);
523     if (vec->pending) {
524         vec->pending = 0;
525         nvic_irq_update(s);
526     }
527 }
528 
529 static void do_armv7m_nvic_set_pending(void *opaque, int irq, bool secure,
530                                        bool derived)
531 {
532     /* Pend an exception, including possibly escalating it to HardFault.
533      *
534      * This function handles both "normal" pending of interrupts and
535      * exceptions, and also derived exceptions (ones which occur as
536      * a result of trying to take some other exception).
537      *
538      * If derived == true, the caller guarantees that we are part way through
539      * trying to take an exception (but have not yet called
540      * armv7m_nvic_acknowledge_irq() to make it active), and so:
541      *  - s->vectpending is the "original exception" we were trying to take
542      *  - irq is the "derived exception"
543      *  - nvic_exec_prio(s) gives the priority before exception entry
544      * Here we handle the prioritization logic which the pseudocode puts
545      * in the DerivedLateArrival() function.
546      */
547 
548     NVICState *s = (NVICState *)opaque;
549     bool banked = exc_is_banked(irq);
550     VecInfo *vec;
551     bool targets_secure;
552 
553     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
554     assert(!secure || banked);
555 
556     vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
557 
558     targets_secure = banked ? secure : exc_targets_secure(s, irq);
559 
560     trace_nvic_set_pending(irq, secure, targets_secure,
561                            derived, vec->enabled, vec->prio);
562 
563     if (derived) {
564         /* Derived exceptions are always synchronous. */
565         assert(irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV);
566 
567         if (irq == ARMV7M_EXCP_DEBUG &&
568             exc_group_prio(s, vec->prio, secure) >= nvic_exec_prio(s)) {
569             /* DebugMonitorFault, but its priority is lower than the
570              * preempted exception priority: just ignore it.
571              */
572             return;
573         }
574 
575         if (irq == ARMV7M_EXCP_HARD && vec->prio >= s->vectpending_prio) {
576             /* If this is a terminal exception (one which means we cannot
577              * take the original exception, like a failure to read its
578              * vector table entry), then we must take the derived exception.
579              * If the derived exception can't take priority over the
580              * original exception, then we go into Lockup.
581              *
582              * For QEMU, we rely on the fact that a derived exception is
583              * terminal if and only if it's reported to us as HardFault,
584              * which saves having to have an extra argument is_terminal
585              * that we'd only use in one place.
586              */
587             cpu_abort(&s->cpu->parent_obj,
588                       "Lockup: can't take terminal derived exception "
589                       "(original exception priority %d)\n",
590                       s->vectpending_prio);
591         }
592         /* We now continue with the same code as for a normal pending
593          * exception, which will cause us to pend the derived exception.
594          * We'll then take either the original or the derived exception
595          * based on which is higher priority by the usual mechanism
596          * for selecting the highest priority pending interrupt.
597          */
598     }
599 
600     if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) {
601         /* If a synchronous exception is pending then it may be
602          * escalated to HardFault if:
603          *  * it is equal or lower priority to current execution
604          *  * it is disabled
605          * (ie we need to take it immediately but we can't do so).
606          * Asynchronous exceptions (and interrupts) simply remain pending.
607          *
608          * For QEMU, we don't have any imprecise (asynchronous) faults,
609          * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
610          * synchronous.
611          * Debug exceptions are awkward because only Debug exceptions
612          * resulting from the BKPT instruction should be escalated,
613          * but we don't currently implement any Debug exceptions other
614          * than those that result from BKPT, so we treat all debug exceptions
615          * as needing escalation.
616          *
617          * This all means we can identify whether to escalate based only on
618          * the exception number and don't (yet) need the caller to explicitly
619          * tell us whether this exception is synchronous or not.
620          */
621         int running = nvic_exec_prio(s);
622         bool escalate = false;
623 
624         if (exc_group_prio(s, vec->prio, secure) >= running) {
625             trace_nvic_escalate_prio(irq, vec->prio, running);
626             escalate = true;
627         } else if (!vec->enabled) {
628             trace_nvic_escalate_disabled(irq);
629             escalate = true;
630         }
631 
632         if (escalate) {
633 
634             /* We need to escalate this exception to a synchronous HardFault.
635              * If BFHFNMINS is set then we escalate to the banked HF for
636              * the target security state of the original exception; otherwise
637              * we take a Secure HardFault.
638              */
639             irq = ARMV7M_EXCP_HARD;
640             if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
641                 (targets_secure ||
642                  !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) {
643                 vec = &s->sec_vectors[irq];
644             } else {
645                 vec = &s->vectors[irq];
646             }
647             if (running <= vec->prio) {
648                 /* We want to escalate to HardFault but we can't take the
649                  * synchronous HardFault at this point either. This is a
650                  * Lockup condition due to a guest bug. We don't model
651                  * Lockup, so report via cpu_abort() instead.
652                  */
653                 cpu_abort(&s->cpu->parent_obj,
654                           "Lockup: can't escalate %d to HardFault "
655                           "(current priority %d)\n", irq, running);
656             }
657 
658             /* HF may be banked but there is only one shared HFSR */
659             s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
660         }
661     }
662 
663     if (!vec->pending) {
664         vec->pending = 1;
665         nvic_irq_update(s);
666     }
667 }
668 
669 void armv7m_nvic_set_pending(void *opaque, int irq, bool secure)
670 {
671     do_armv7m_nvic_set_pending(opaque, irq, secure, false);
672 }
673 
674 void armv7m_nvic_set_pending_derived(void *opaque, int irq, bool secure)
675 {
676     do_armv7m_nvic_set_pending(opaque, irq, secure, true);
677 }
678 
679 void armv7m_nvic_set_pending_lazyfp(void *opaque, int irq, bool secure)
680 {
681     /*
682      * Pend an exception during lazy FP stacking. This differs
683      * from the usual exception pending because the logic for
684      * whether we should escalate depends on the saved context
685      * in the FPCCR register, not on the current state of the CPU/NVIC.
686      */
687     NVICState *s = (NVICState *)opaque;
688     bool banked = exc_is_banked(irq);
689     VecInfo *vec;
690     bool targets_secure;
691     bool escalate = false;
692     /*
693      * We will only look at bits in fpccr if this is a banked exception
694      * (in which case 'secure' tells us whether it is the S or NS version).
695      * All the bits for the non-banked exceptions are in fpccr_s.
696      */
697     uint32_t fpccr_s = s->cpu->env.v7m.fpccr[M_REG_S];
698     uint32_t fpccr = s->cpu->env.v7m.fpccr[secure];
699 
700     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
701     assert(!secure || banked);
702 
703     vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
704 
705     targets_secure = banked ? secure : exc_targets_secure(s, irq);
706 
707     switch (irq) {
708     case ARMV7M_EXCP_DEBUG:
709         if (!(fpccr_s & R_V7M_FPCCR_MONRDY_MASK)) {
710             /* Ignore DebugMonitor exception */
711             return;
712         }
713         break;
714     case ARMV7M_EXCP_MEM:
715         escalate = !(fpccr & R_V7M_FPCCR_MMRDY_MASK);
716         break;
717     case ARMV7M_EXCP_USAGE:
718         escalate = !(fpccr & R_V7M_FPCCR_UFRDY_MASK);
719         break;
720     case ARMV7M_EXCP_BUS:
721         escalate = !(fpccr_s & R_V7M_FPCCR_BFRDY_MASK);
722         break;
723     case ARMV7M_EXCP_SECURE:
724         escalate = !(fpccr_s & R_V7M_FPCCR_SFRDY_MASK);
725         break;
726     default:
727         g_assert_not_reached();
728     }
729 
730     if (escalate) {
731         /*
732          * Escalate to HardFault: faults that initially targeted Secure
733          * continue to do so, even if HF normally targets NonSecure.
734          */
735         irq = ARMV7M_EXCP_HARD;
736         if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
737             (targets_secure ||
738              !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) {
739             vec = &s->sec_vectors[irq];
740         } else {
741             vec = &s->vectors[irq];
742         }
743     }
744 
745     if (!vec->enabled ||
746         nvic_exec_prio(s) <= exc_group_prio(s, vec->prio, secure)) {
747         if (!(fpccr_s & R_V7M_FPCCR_HFRDY_MASK)) {
748             /*
749              * We want to escalate to HardFault but the context the
750              * FP state belongs to prevents the exception pre-empting.
751              */
752             cpu_abort(&s->cpu->parent_obj,
753                       "Lockup: can't escalate to HardFault during "
754                       "lazy FP register stacking\n");
755         }
756     }
757 
758     if (escalate) {
759         s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
760     }
761     if (!vec->pending) {
762         vec->pending = 1;
763         /*
764          * We do not call nvic_irq_update(), because we know our caller
765          * is going to handle causing us to take the exception by
766          * raising EXCP_LAZYFP, so raising the IRQ line would be
767          * pointless extra work. We just need to recompute the
768          * priorities so that armv7m_nvic_can_take_pending_exception()
769          * returns the right answer.
770          */
771         nvic_recompute_state(s);
772     }
773 }
774 
775 /* Make pending IRQ active.  */
776 void armv7m_nvic_acknowledge_irq(void *opaque)
777 {
778     NVICState *s = (NVICState *)opaque;
779     CPUARMState *env = &s->cpu->env;
780     const int pending = s->vectpending;
781     const int running = nvic_exec_prio(s);
782     VecInfo *vec;
783 
784     assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
785 
786     if (s->vectpending_is_s_banked) {
787         vec = &s->sec_vectors[pending];
788     } else {
789         vec = &s->vectors[pending];
790     }
791 
792     assert(vec->enabled);
793     assert(vec->pending);
794 
795     assert(s->vectpending_prio < running);
796 
797     trace_nvic_acknowledge_irq(pending, s->vectpending_prio);
798 
799     vec->active = 1;
800     vec->pending = 0;
801 
802     write_v7m_exception(env, s->vectpending);
803 
804     nvic_irq_update(s);
805 }
806 
807 void armv7m_nvic_get_pending_irq_info(void *opaque,
808                                       int *pirq, bool *ptargets_secure)
809 {
810     NVICState *s = (NVICState *)opaque;
811     const int pending = s->vectpending;
812     bool targets_secure;
813 
814     assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
815 
816     if (s->vectpending_is_s_banked) {
817         targets_secure = true;
818     } else {
819         targets_secure = !exc_is_banked(pending) &&
820             exc_targets_secure(s, pending);
821     }
822 
823     trace_nvic_get_pending_irq_info(pending, targets_secure);
824 
825     *ptargets_secure = targets_secure;
826     *pirq = pending;
827 }
828 
829 int armv7m_nvic_complete_irq(void *opaque, int irq, bool secure)
830 {
831     NVICState *s = (NVICState *)opaque;
832     VecInfo *vec = NULL;
833     int ret = 0;
834 
835     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
836 
837     trace_nvic_complete_irq(irq, secure);
838 
839     if (secure && exc_is_banked(irq)) {
840         vec = &s->sec_vectors[irq];
841     } else {
842         vec = &s->vectors[irq];
843     }
844 
845     /*
846      * Identify illegal exception return cases. We can't immediately
847      * return at this point because we still need to deactivate
848      * (either this exception or NMI/HardFault) first.
849      */
850     if (!exc_is_banked(irq) && exc_targets_secure(s, irq) != secure) {
851         /*
852          * Return from a configurable exception targeting the opposite
853          * security state from the one we're trying to complete it for.
854          * Clear vec because it's not really the VecInfo for this
855          * (irq, secstate) so we mustn't deactivate it.
856          */
857         ret = -1;
858         vec = NULL;
859     } else if (!vec->active) {
860         /* Return from an inactive interrupt */
861         ret = -1;
862     } else {
863         /* Legal return, we will return the RETTOBASE bit value to the caller */
864         ret = nvic_rettobase(s);
865     }
866 
867     /*
868      * For negative priorities, v8M will forcibly deactivate the appropriate
869      * NMI or HardFault regardless of what interrupt we're being asked to
870      * deactivate (compare the DeActivate() pseudocode). This is a guard
871      * against software returning from NMI or HardFault with a corrupted
872      * IPSR and leaving the CPU in a negative-priority state.
873      * v7M does not do this, but simply deactivates the requested interrupt.
874      */
875     if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
876         switch (armv7m_nvic_raw_execution_priority(s)) {
877         case -1:
878             if (s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
879                 vec = &s->vectors[ARMV7M_EXCP_HARD];
880             } else {
881                 vec = &s->sec_vectors[ARMV7M_EXCP_HARD];
882             }
883             break;
884         case -2:
885             vec = &s->vectors[ARMV7M_EXCP_NMI];
886             break;
887         case -3:
888             vec = &s->sec_vectors[ARMV7M_EXCP_HARD];
889             break;
890         default:
891             break;
892         }
893     }
894 
895     if (!vec) {
896         return ret;
897     }
898 
899     vec->active = 0;
900     if (vec->level) {
901         /* Re-pend the exception if it's still held high; only
902          * happens for extenal IRQs
903          */
904         assert(irq >= NVIC_FIRST_IRQ);
905         vec->pending = 1;
906     }
907 
908     nvic_irq_update(s);
909 
910     return ret;
911 }
912 
913 bool armv7m_nvic_get_ready_status(void *opaque, int irq, bool secure)
914 {
915     /*
916      * Return whether an exception is "ready", i.e. it is enabled and is
917      * configured at a priority which would allow it to interrupt the
918      * current execution priority.
919      *
920      * irq and secure have the same semantics as for armv7m_nvic_set_pending():
921      * for non-banked exceptions secure is always false; for banked exceptions
922      * it indicates which of the exceptions is required.
923      */
924     NVICState *s = (NVICState *)opaque;
925     bool banked = exc_is_banked(irq);
926     VecInfo *vec;
927     int running = nvic_exec_prio(s);
928 
929     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
930     assert(!secure || banked);
931 
932     /*
933      * HardFault is an odd special case: we always check against -1,
934      * even if we're secure and HardFault has priority -3; we never
935      * need to check for enabled state.
936      */
937     if (irq == ARMV7M_EXCP_HARD) {
938         return running > -1;
939     }
940 
941     vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
942 
943     return vec->enabled &&
944         exc_group_prio(s, vec->prio, secure) < running;
945 }
946 
947 /* callback when external interrupt line is changed */
948 static void set_irq_level(void *opaque, int n, int level)
949 {
950     NVICState *s = opaque;
951     VecInfo *vec;
952 
953     n += NVIC_FIRST_IRQ;
954 
955     assert(n >= NVIC_FIRST_IRQ && n < s->num_irq);
956 
957     trace_nvic_set_irq_level(n, level);
958 
959     /* The pending status of an external interrupt is
960      * latched on rising edge and exception handler return.
961      *
962      * Pulsing the IRQ will always run the handler
963      * once, and the handler will re-run until the
964      * level is low when the handler completes.
965      */
966     vec = &s->vectors[n];
967     if (level != vec->level) {
968         vec->level = level;
969         if (level) {
970             armv7m_nvic_set_pending(s, n, false);
971         }
972     }
973 }
974 
975 /* callback when external NMI line is changed */
976 static void nvic_nmi_trigger(void *opaque, int n, int level)
977 {
978     NVICState *s = opaque;
979 
980     trace_nvic_set_nmi_level(level);
981 
982     /*
983      * The architecture doesn't specify whether NMI should share
984      * the normal-interrupt behaviour of being resampled on
985      * exception handler return. We choose not to, so just
986      * set NMI pending here and don't track the current level.
987      */
988     if (level) {
989         armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false);
990     }
991 }
992 
993 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
994 {
995     ARMCPU *cpu = s->cpu;
996     uint32_t val;
997 
998     switch (offset) {
999     case 4: /* Interrupt Control Type.  */
1000         if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1001             goto bad_offset;
1002         }
1003         return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1;
1004     case 0xc: /* CPPWR */
1005         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1006             goto bad_offset;
1007         }
1008         /* We make the IMPDEF choice that nothing can ever go into a
1009          * non-retentive power state, which allows us to RAZ/WI this.
1010          */
1011         return 0;
1012     case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
1013     {
1014         int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
1015         int i;
1016 
1017         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1018             goto bad_offset;
1019         }
1020         if (!attrs.secure) {
1021             return 0;
1022         }
1023         val = 0;
1024         for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
1025             if (s->itns[startvec + i]) {
1026                 val |= (1 << i);
1027             }
1028         }
1029         return val;
1030     }
1031     case 0xcfc:
1032         if (!arm_feature(&cpu->env, ARM_FEATURE_V8_1M)) {
1033             goto bad_offset;
1034         }
1035         return cpu->revidr;
1036     case 0xd00: /* CPUID Base.  */
1037         return cpu->midr;
1038     case 0xd04: /* Interrupt Control State (ICSR) */
1039         /* VECTACTIVE */
1040         val = cpu->env.v7m.exception;
1041         /* VECTPENDING */
1042         val |= (s->vectpending & 0x1ff) << 12;
1043         /* ISRPENDING - set if any external IRQ is pending */
1044         if (nvic_isrpending(s)) {
1045             val |= (1 << 22);
1046         }
1047         /* RETTOBASE - set if only one handler is active */
1048         if (nvic_rettobase(s)) {
1049             val |= (1 << 11);
1050         }
1051         if (attrs.secure) {
1052             /* PENDSTSET */
1053             if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) {
1054                 val |= (1 << 26);
1055             }
1056             /* PENDSVSET */
1057             if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) {
1058                 val |= (1 << 28);
1059             }
1060         } else {
1061             /* PENDSTSET */
1062             if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) {
1063                 val |= (1 << 26);
1064             }
1065             /* PENDSVSET */
1066             if (s->vectors[ARMV7M_EXCP_PENDSV].pending) {
1067                 val |= (1 << 28);
1068             }
1069         }
1070         /* NMIPENDSET */
1071         if ((attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))
1072             && s->vectors[ARMV7M_EXCP_NMI].pending) {
1073             val |= (1 << 31);
1074         }
1075         /* ISRPREEMPT: RES0 when halting debug not implemented */
1076         /* STTNS: RES0 for the Main Extension */
1077         return val;
1078     case 0xd08: /* Vector Table Offset.  */
1079         return cpu->env.v7m.vecbase[attrs.secure];
1080     case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
1081         val = 0xfa050000 | (s->prigroup[attrs.secure] << 8);
1082         if (attrs.secure) {
1083             /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */
1084             val |= cpu->env.v7m.aircr;
1085         } else {
1086             if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1087                 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If
1088                  * security isn't supported then BFHFNMINS is RAO (and
1089                  * the bit in env.v7m.aircr is always set).
1090                  */
1091                 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK;
1092             }
1093         }
1094         return val;
1095     case 0xd10: /* System Control.  */
1096         if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1097             goto bad_offset;
1098         }
1099         return cpu->env.v7m.scr[attrs.secure];
1100     case 0xd14: /* Configuration Control.  */
1101         /*
1102          * Non-banked bits: BFHFNMIGN (stored in the NS copy of the register)
1103          * and TRD (stored in the S copy of the register)
1104          */
1105         val = cpu->env.v7m.ccr[attrs.secure];
1106         val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
1107         /* BFHFNMIGN is RAZ/WI from NS if AIRCR.BFHFNMINS is 0 */
1108         if (!attrs.secure) {
1109             if (!(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1110                 val &= ~R_V7M_CCR_BFHFNMIGN_MASK;
1111             }
1112         }
1113         return val;
1114     case 0xd24: /* System Handler Control and State (SHCSR) */
1115         if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1116             goto bad_offset;
1117         }
1118         val = 0;
1119         if (attrs.secure) {
1120             if (s->sec_vectors[ARMV7M_EXCP_MEM].active) {
1121                 val |= (1 << 0);
1122             }
1123             if (s->sec_vectors[ARMV7M_EXCP_HARD].active) {
1124                 val |= (1 << 2);
1125             }
1126             if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) {
1127                 val |= (1 << 3);
1128             }
1129             if (s->sec_vectors[ARMV7M_EXCP_SVC].active) {
1130                 val |= (1 << 7);
1131             }
1132             if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) {
1133                 val |= (1 << 10);
1134             }
1135             if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) {
1136                 val |= (1 << 11);
1137             }
1138             if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) {
1139                 val |= (1 << 12);
1140             }
1141             if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) {
1142                 val |= (1 << 13);
1143             }
1144             if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) {
1145                 val |= (1 << 15);
1146             }
1147             if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) {
1148                 val |= (1 << 16);
1149             }
1150             if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) {
1151                 val |= (1 << 18);
1152             }
1153             if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) {
1154                 val |= (1 << 21);
1155             }
1156             /* SecureFault is not banked but is always RAZ/WI to NS */
1157             if (s->vectors[ARMV7M_EXCP_SECURE].active) {
1158                 val |= (1 << 4);
1159             }
1160             if (s->vectors[ARMV7M_EXCP_SECURE].enabled) {
1161                 val |= (1 << 19);
1162             }
1163             if (s->vectors[ARMV7M_EXCP_SECURE].pending) {
1164                 val |= (1 << 20);
1165             }
1166         } else {
1167             if (s->vectors[ARMV7M_EXCP_MEM].active) {
1168                 val |= (1 << 0);
1169             }
1170             if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1171                 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */
1172                 if (s->vectors[ARMV7M_EXCP_HARD].active) {
1173                     val |= (1 << 2);
1174                 }
1175                 if (s->vectors[ARMV7M_EXCP_HARD].pending) {
1176                     val |= (1 << 21);
1177                 }
1178             }
1179             if (s->vectors[ARMV7M_EXCP_USAGE].active) {
1180                 val |= (1 << 3);
1181             }
1182             if (s->vectors[ARMV7M_EXCP_SVC].active) {
1183                 val |= (1 << 7);
1184             }
1185             if (s->vectors[ARMV7M_EXCP_PENDSV].active) {
1186                 val |= (1 << 10);
1187             }
1188             if (s->vectors[ARMV7M_EXCP_SYSTICK].active) {
1189                 val |= (1 << 11);
1190             }
1191             if (s->vectors[ARMV7M_EXCP_USAGE].pending) {
1192                 val |= (1 << 12);
1193             }
1194             if (s->vectors[ARMV7M_EXCP_MEM].pending) {
1195                 val |= (1 << 13);
1196             }
1197             if (s->vectors[ARMV7M_EXCP_SVC].pending) {
1198                 val |= (1 << 15);
1199             }
1200             if (s->vectors[ARMV7M_EXCP_MEM].enabled) {
1201                 val |= (1 << 16);
1202             }
1203             if (s->vectors[ARMV7M_EXCP_USAGE].enabled) {
1204                 val |= (1 << 18);
1205             }
1206         }
1207         if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1208             if (s->vectors[ARMV7M_EXCP_BUS].active) {
1209                 val |= (1 << 1);
1210             }
1211             if (s->vectors[ARMV7M_EXCP_BUS].pending) {
1212                 val |= (1 << 14);
1213             }
1214             if (s->vectors[ARMV7M_EXCP_BUS].enabled) {
1215                 val |= (1 << 17);
1216             }
1217             if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
1218                 s->vectors[ARMV7M_EXCP_NMI].active) {
1219                 /* NMIACT is not present in v7M */
1220                 val |= (1 << 5);
1221             }
1222         }
1223 
1224         /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
1225         if (s->vectors[ARMV7M_EXCP_DEBUG].active) {
1226             val |= (1 << 8);
1227         }
1228         return val;
1229     case 0xd2c: /* Hard Fault Status.  */
1230         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1231             goto bad_offset;
1232         }
1233         return cpu->env.v7m.hfsr;
1234     case 0xd30: /* Debug Fault Status.  */
1235         return cpu->env.v7m.dfsr;
1236     case 0xd34: /* MMFAR MemManage Fault Address */
1237         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1238             goto bad_offset;
1239         }
1240         return cpu->env.v7m.mmfar[attrs.secure];
1241     case 0xd38: /* Bus Fault Address.  */
1242         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1243             goto bad_offset;
1244         }
1245         if (!attrs.secure &&
1246             !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1247             return 0;
1248         }
1249         return cpu->env.v7m.bfar;
1250     case 0xd3c: /* Aux Fault Status.  */
1251         /* TODO: Implement fault status registers.  */
1252         qemu_log_mask(LOG_UNIMP,
1253                       "Aux Fault status registers unimplemented\n");
1254         return 0;
1255     case 0xd40: /* PFR0.  */
1256         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1257             goto bad_offset;
1258         }
1259         return cpu->isar.id_pfr0;
1260     case 0xd44: /* PFR1.  */
1261         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1262             goto bad_offset;
1263         }
1264         return cpu->isar.id_pfr1;
1265     case 0xd48: /* DFR0.  */
1266         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1267             goto bad_offset;
1268         }
1269         return cpu->isar.id_dfr0;
1270     case 0xd4c: /* AFR0.  */
1271         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1272             goto bad_offset;
1273         }
1274         return cpu->id_afr0;
1275     case 0xd50: /* MMFR0.  */
1276         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1277             goto bad_offset;
1278         }
1279         return cpu->isar.id_mmfr0;
1280     case 0xd54: /* MMFR1.  */
1281         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1282             goto bad_offset;
1283         }
1284         return cpu->isar.id_mmfr1;
1285     case 0xd58: /* MMFR2.  */
1286         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1287             goto bad_offset;
1288         }
1289         return cpu->isar.id_mmfr2;
1290     case 0xd5c: /* MMFR3.  */
1291         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1292             goto bad_offset;
1293         }
1294         return cpu->isar.id_mmfr3;
1295     case 0xd60: /* ISAR0.  */
1296         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1297             goto bad_offset;
1298         }
1299         return cpu->isar.id_isar0;
1300     case 0xd64: /* ISAR1.  */
1301         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1302             goto bad_offset;
1303         }
1304         return cpu->isar.id_isar1;
1305     case 0xd68: /* ISAR2.  */
1306         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1307             goto bad_offset;
1308         }
1309         return cpu->isar.id_isar2;
1310     case 0xd6c: /* ISAR3.  */
1311         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1312             goto bad_offset;
1313         }
1314         return cpu->isar.id_isar3;
1315     case 0xd70: /* ISAR4.  */
1316         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1317             goto bad_offset;
1318         }
1319         return cpu->isar.id_isar4;
1320     case 0xd74: /* ISAR5.  */
1321         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1322             goto bad_offset;
1323         }
1324         return cpu->isar.id_isar5;
1325     case 0xd78: /* CLIDR */
1326         return cpu->clidr;
1327     case 0xd7c: /* CTR */
1328         return cpu->ctr;
1329     case 0xd80: /* CSSIDR */
1330     {
1331         int idx = cpu->env.v7m.csselr[attrs.secure] & R_V7M_CSSELR_INDEX_MASK;
1332         return cpu->ccsidr[idx];
1333     }
1334     case 0xd84: /* CSSELR */
1335         return cpu->env.v7m.csselr[attrs.secure];
1336     case 0xd88: /* CPACR */
1337         if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1338             return 0;
1339         }
1340         return cpu->env.v7m.cpacr[attrs.secure];
1341     case 0xd8c: /* NSACR */
1342         if (!attrs.secure || !cpu_isar_feature(aa32_vfp_simd, cpu)) {
1343             return 0;
1344         }
1345         return cpu->env.v7m.nsacr;
1346     /* TODO: Implement debug registers.  */
1347     case 0xd90: /* MPU_TYPE */
1348         /* Unified MPU; if the MPU is not present this value is zero */
1349         return cpu->pmsav7_dregion << 8;
1350     case 0xd94: /* MPU_CTRL */
1351         return cpu->env.v7m.mpu_ctrl[attrs.secure];
1352     case 0xd98: /* MPU_RNR */
1353         return cpu->env.pmsav7.rnr[attrs.secure];
1354     case 0xd9c: /* MPU_RBAR */
1355     case 0xda4: /* MPU_RBAR_A1 */
1356     case 0xdac: /* MPU_RBAR_A2 */
1357     case 0xdb4: /* MPU_RBAR_A3 */
1358     {
1359         int region = cpu->env.pmsav7.rnr[attrs.secure];
1360 
1361         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1362             /* PMSAv8M handling of the aliases is different from v7M:
1363              * aliases A1, A2, A3 override the low two bits of the region
1364              * number in MPU_RNR, and there is no 'region' field in the
1365              * RBAR register.
1366              */
1367             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1368             if (aliasno) {
1369                 region = deposit32(region, 0, 2, aliasno);
1370             }
1371             if (region >= cpu->pmsav7_dregion) {
1372                 return 0;
1373             }
1374             return cpu->env.pmsav8.rbar[attrs.secure][region];
1375         }
1376 
1377         if (region >= cpu->pmsav7_dregion) {
1378             return 0;
1379         }
1380         return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf);
1381     }
1382     case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1383     case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1384     case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1385     case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
1386     {
1387         int region = cpu->env.pmsav7.rnr[attrs.secure];
1388 
1389         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1390             /* PMSAv8M handling of the aliases is different from v7M:
1391              * aliases A1, A2, A3 override the low two bits of the region
1392              * number in MPU_RNR.
1393              */
1394             int aliasno = (offset - 0xda0) / 8; /* 0..3 */
1395             if (aliasno) {
1396                 region = deposit32(region, 0, 2, aliasno);
1397             }
1398             if (region >= cpu->pmsav7_dregion) {
1399                 return 0;
1400             }
1401             return cpu->env.pmsav8.rlar[attrs.secure][region];
1402         }
1403 
1404         if (region >= cpu->pmsav7_dregion) {
1405             return 0;
1406         }
1407         return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) |
1408             (cpu->env.pmsav7.drsr[region] & 0xffff);
1409     }
1410     case 0xdc0: /* MPU_MAIR0 */
1411         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1412             goto bad_offset;
1413         }
1414         return cpu->env.pmsav8.mair0[attrs.secure];
1415     case 0xdc4: /* MPU_MAIR1 */
1416         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1417             goto bad_offset;
1418         }
1419         return cpu->env.pmsav8.mair1[attrs.secure];
1420     case 0xdd0: /* SAU_CTRL */
1421         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1422             goto bad_offset;
1423         }
1424         if (!attrs.secure) {
1425             return 0;
1426         }
1427         return cpu->env.sau.ctrl;
1428     case 0xdd4: /* SAU_TYPE */
1429         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1430             goto bad_offset;
1431         }
1432         if (!attrs.secure) {
1433             return 0;
1434         }
1435         return cpu->sau_sregion;
1436     case 0xdd8: /* SAU_RNR */
1437         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1438             goto bad_offset;
1439         }
1440         if (!attrs.secure) {
1441             return 0;
1442         }
1443         return cpu->env.sau.rnr;
1444     case 0xddc: /* SAU_RBAR */
1445     {
1446         int region = cpu->env.sau.rnr;
1447 
1448         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1449             goto bad_offset;
1450         }
1451         if (!attrs.secure) {
1452             return 0;
1453         }
1454         if (region >= cpu->sau_sregion) {
1455             return 0;
1456         }
1457         return cpu->env.sau.rbar[region];
1458     }
1459     case 0xde0: /* SAU_RLAR */
1460     {
1461         int region = cpu->env.sau.rnr;
1462 
1463         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1464             goto bad_offset;
1465         }
1466         if (!attrs.secure) {
1467             return 0;
1468         }
1469         if (region >= cpu->sau_sregion) {
1470             return 0;
1471         }
1472         return cpu->env.sau.rlar[region];
1473     }
1474     case 0xde4: /* SFSR */
1475         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1476             goto bad_offset;
1477         }
1478         if (!attrs.secure) {
1479             return 0;
1480         }
1481         return cpu->env.v7m.sfsr;
1482     case 0xde8: /* SFAR */
1483         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1484             goto bad_offset;
1485         }
1486         if (!attrs.secure) {
1487             return 0;
1488         }
1489         return cpu->env.v7m.sfar;
1490     case 0xf04: /* RFSR */
1491         if (!cpu_isar_feature(aa32_ras, cpu)) {
1492             goto bad_offset;
1493         }
1494         /* We provide minimal-RAS only: RFSR is RAZ/WI */
1495         return 0;
1496     case 0xf34: /* FPCCR */
1497         if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1498             return 0;
1499         }
1500         if (attrs.secure) {
1501             return cpu->env.v7m.fpccr[M_REG_S];
1502         } else {
1503             /*
1504              * NS can read LSPEN, CLRONRET and MONRDY. It can read
1505              * BFRDY and HFRDY if AIRCR.BFHFNMINS != 0;
1506              * other non-banked bits RAZ.
1507              * TODO: MONRDY should RAZ/WI if DEMCR.SDME is set.
1508              */
1509             uint32_t value = cpu->env.v7m.fpccr[M_REG_S];
1510             uint32_t mask = R_V7M_FPCCR_LSPEN_MASK |
1511                 R_V7M_FPCCR_CLRONRET_MASK |
1512                 R_V7M_FPCCR_MONRDY_MASK;
1513 
1514             if (s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1515                 mask |= R_V7M_FPCCR_BFRDY_MASK | R_V7M_FPCCR_HFRDY_MASK;
1516             }
1517 
1518             value &= mask;
1519 
1520             value |= cpu->env.v7m.fpccr[M_REG_NS];
1521             return value;
1522         }
1523     case 0xf38: /* FPCAR */
1524         if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1525             return 0;
1526         }
1527         return cpu->env.v7m.fpcar[attrs.secure];
1528     case 0xf3c: /* FPDSCR */
1529         if (!cpu_isar_feature(aa32_vfp_simd, cpu)) {
1530             return 0;
1531         }
1532         return cpu->env.v7m.fpdscr[attrs.secure];
1533     case 0xf40: /* MVFR0 */
1534         return cpu->isar.mvfr0;
1535     case 0xf44: /* MVFR1 */
1536         return cpu->isar.mvfr1;
1537     case 0xf48: /* MVFR2 */
1538         return cpu->isar.mvfr2;
1539     default:
1540     bad_offset:
1541         qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
1542         return 0;
1543     }
1544 }
1545 
1546 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value,
1547                         MemTxAttrs attrs)
1548 {
1549     ARMCPU *cpu = s->cpu;
1550 
1551     switch (offset) {
1552     case 0xc: /* CPPWR */
1553         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1554             goto bad_offset;
1555         }
1556         /* Make the IMPDEF choice to RAZ/WI this. */
1557         break;
1558     case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
1559     {
1560         int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
1561         int i;
1562 
1563         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1564             goto bad_offset;
1565         }
1566         if (!attrs.secure) {
1567             break;
1568         }
1569         for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
1570             s->itns[startvec + i] = (value >> i) & 1;
1571         }
1572         nvic_irq_update(s);
1573         break;
1574     }
1575     case 0xd04: /* Interrupt Control State (ICSR) */
1576         if (attrs.secure || cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1577             if (value & (1 << 31)) {
1578                 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false);
1579             } else if (value & (1 << 30) &&
1580                        arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1581                 /* PENDNMICLR didn't exist in v7M */
1582                 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false);
1583             }
1584         }
1585         if (value & (1 << 28)) {
1586             armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
1587         } else if (value & (1 << 27)) {
1588             armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
1589         }
1590         if (value & (1 << 26)) {
1591             armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
1592         } else if (value & (1 << 25)) {
1593             armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
1594         }
1595         break;
1596     case 0xd08: /* Vector Table Offset.  */
1597         cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80;
1598         break;
1599     case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
1600         if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) {
1601             if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) {
1602                 if (attrs.secure ||
1603                     !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) {
1604                     signal_sysresetreq(s);
1605                 }
1606             }
1607             if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) {
1608                 qemu_log_mask(LOG_GUEST_ERROR,
1609                               "Setting VECTCLRACTIVE when not in DEBUG mode "
1610                               "is UNPREDICTABLE\n");
1611             }
1612             if (value & R_V7M_AIRCR_VECTRESET_MASK) {
1613                 /* NB: this bit is RES0 in v8M */
1614                 qemu_log_mask(LOG_GUEST_ERROR,
1615                               "Setting VECTRESET when not in DEBUG mode "
1616                               "is UNPREDICTABLE\n");
1617             }
1618             if (arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1619                 s->prigroup[attrs.secure] =
1620                     extract32(value,
1621                               R_V7M_AIRCR_PRIGROUP_SHIFT,
1622                               R_V7M_AIRCR_PRIGROUP_LENGTH);
1623             }
1624             /* AIRCR.IESB is RAZ/WI because we implement only minimal RAS */
1625             if (attrs.secure) {
1626                 /* These bits are only writable by secure */
1627                 cpu->env.v7m.aircr = value &
1628                     (R_V7M_AIRCR_SYSRESETREQS_MASK |
1629                      R_V7M_AIRCR_BFHFNMINS_MASK |
1630                      R_V7M_AIRCR_PRIS_MASK);
1631                 /* BFHFNMINS changes the priority of Secure HardFault, and
1632                  * allows a pending Non-secure HardFault to preempt (which
1633                  * we implement by marking it enabled).
1634                  */
1635                 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1636                     s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3;
1637                     s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
1638                 } else {
1639                     s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
1640                     s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
1641                 }
1642             }
1643             nvic_irq_update(s);
1644         }
1645         break;
1646     case 0xd10: /* System Control.  */
1647         if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1648             goto bad_offset;
1649         }
1650         /* We don't implement deep-sleep so these bits are RAZ/WI.
1651          * The other bits in the register are banked.
1652          * QEMU's implementation ignores SEVONPEND and SLEEPONEXIT, which
1653          * is architecturally permitted.
1654          */
1655         value &= ~(R_V7M_SCR_SLEEPDEEP_MASK | R_V7M_SCR_SLEEPDEEPS_MASK);
1656         cpu->env.v7m.scr[attrs.secure] = value;
1657         break;
1658     case 0xd14: /* Configuration Control.  */
1659     {
1660         uint32_t mask;
1661 
1662         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1663             goto bad_offset;
1664         }
1665 
1666         /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
1667         mask = R_V7M_CCR_STKALIGN_MASK |
1668             R_V7M_CCR_BFHFNMIGN_MASK |
1669             R_V7M_CCR_DIV_0_TRP_MASK |
1670             R_V7M_CCR_UNALIGN_TRP_MASK |
1671             R_V7M_CCR_USERSETMPEND_MASK |
1672             R_V7M_CCR_NONBASETHRDENA_MASK;
1673         if (arm_feature(&cpu->env, ARM_FEATURE_V8_1M) && attrs.secure) {
1674             /* TRD is always RAZ/WI from NS */
1675             mask |= R_V7M_CCR_TRD_MASK;
1676         }
1677         value &= mask;
1678 
1679         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1680             /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */
1681             value |= R_V7M_CCR_NONBASETHRDENA_MASK
1682                 | R_V7M_CCR_STKALIGN_MASK;
1683         }
1684         if (attrs.secure) {
1685             /* the BFHFNMIGN bit is not banked; keep that in the NS copy */
1686             cpu->env.v7m.ccr[M_REG_NS] =
1687                 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK)
1688                 | (value & R_V7M_CCR_BFHFNMIGN_MASK);
1689             value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
1690         } else {
1691             /*
1692              * BFHFNMIGN is RAZ/WI from NS if AIRCR.BFHFNMINS is 0, so
1693              * preserve the state currently in the NS element of the array
1694              */
1695             if (!(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1696                 value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
1697                 value |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
1698             }
1699         }
1700 
1701         cpu->env.v7m.ccr[attrs.secure] = value;
1702         break;
1703     }
1704     case 0xd24: /* System Handler Control and State (SHCSR) */
1705         if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1706             goto bad_offset;
1707         }
1708         if (attrs.secure) {
1709             s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1710             /* Secure HardFault active bit cannot be written */
1711             s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1712             s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1713             s->sec_vectors[ARMV7M_EXCP_PENDSV].active =
1714                 (value & (1 << 10)) != 0;
1715             s->sec_vectors[ARMV7M_EXCP_SYSTICK].active =
1716                 (value & (1 << 11)) != 0;
1717             s->sec_vectors[ARMV7M_EXCP_USAGE].pending =
1718                 (value & (1 << 12)) != 0;
1719             s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1720             s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1721             s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1722             s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1723             s->sec_vectors[ARMV7M_EXCP_USAGE].enabled =
1724                 (value & (1 << 18)) != 0;
1725             s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1726             /* SecureFault not banked, but RAZ/WI to NS */
1727             s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0;
1728             s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0;
1729             s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0;
1730         } else {
1731             s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1732             if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1733                 /* HARDFAULTPENDED is not present in v7M */
1734                 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1735             }
1736             s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1737             s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1738             s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0;
1739             s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0;
1740             s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0;
1741             s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1742             s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1743             s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1744             s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
1745         }
1746         if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1747             s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0;
1748             s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0;
1749             s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1750         }
1751         /* NMIACT can only be written if the write is of a zero, with
1752          * BFHFNMINS 1, and by the CPU in secure state via the NS alias.
1753          */
1754         if (!attrs.secure && cpu->env.v7m.secure &&
1755             (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1756             (value & (1 << 5)) == 0) {
1757             s->vectors[ARMV7M_EXCP_NMI].active = 0;
1758         }
1759         /* HARDFAULTACT can only be written if the write is of a zero
1760          * to the non-secure HardFault state by the CPU in secure state.
1761          * The only case where we can be targeting the non-secure HF state
1762          * when in secure state is if this is a write via the NS alias
1763          * and BFHFNMINS is 1.
1764          */
1765         if (!attrs.secure && cpu->env.v7m.secure &&
1766             (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1767             (value & (1 << 2)) == 0) {
1768             s->vectors[ARMV7M_EXCP_HARD].active = 0;
1769         }
1770 
1771         /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
1772         s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0;
1773         nvic_irq_update(s);
1774         break;
1775     case 0xd2c: /* Hard Fault Status.  */
1776         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1777             goto bad_offset;
1778         }
1779         cpu->env.v7m.hfsr &= ~value; /* W1C */
1780         break;
1781     case 0xd30: /* Debug Fault Status.  */
1782         cpu->env.v7m.dfsr &= ~value; /* W1C */
1783         break;
1784     case 0xd34: /* Mem Manage Address.  */
1785         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1786             goto bad_offset;
1787         }
1788         cpu->env.v7m.mmfar[attrs.secure] = value;
1789         return;
1790     case 0xd38: /* Bus Fault Address.  */
1791         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
1792             goto bad_offset;
1793         }
1794         if (!attrs.secure &&
1795             !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1796             return;
1797         }
1798         cpu->env.v7m.bfar = value;
1799         return;
1800     case 0xd3c: /* Aux Fault Status.  */
1801         qemu_log_mask(LOG_UNIMP,
1802                       "NVIC: Aux fault status registers unimplemented\n");
1803         break;
1804     case 0xd84: /* CSSELR */
1805         if (!arm_v7m_csselr_razwi(cpu)) {
1806             cpu->env.v7m.csselr[attrs.secure] = value & R_V7M_CSSELR_INDEX_MASK;
1807         }
1808         break;
1809     case 0xd88: /* CPACR */
1810         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1811             /* We implement only the Floating Point extension's CP10/CP11 */
1812             cpu->env.v7m.cpacr[attrs.secure] = value & (0xf << 20);
1813         }
1814         break;
1815     case 0xd8c: /* NSACR */
1816         if (attrs.secure && cpu_isar_feature(aa32_vfp_simd, cpu)) {
1817             /* We implement only the Floating Point extension's CP10/CP11 */
1818             cpu->env.v7m.nsacr = value & (3 << 10);
1819         }
1820         break;
1821     case 0xd90: /* MPU_TYPE */
1822         return; /* RO */
1823     case 0xd94: /* MPU_CTRL */
1824         if ((value &
1825              (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK))
1826             == R_V7M_MPU_CTRL_HFNMIENA_MASK) {
1827             qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is "
1828                           "UNPREDICTABLE\n");
1829         }
1830         cpu->env.v7m.mpu_ctrl[attrs.secure]
1831             = value & (R_V7M_MPU_CTRL_ENABLE_MASK |
1832                        R_V7M_MPU_CTRL_HFNMIENA_MASK |
1833                        R_V7M_MPU_CTRL_PRIVDEFENA_MASK);
1834         tlb_flush(CPU(cpu));
1835         break;
1836     case 0xd98: /* MPU_RNR */
1837         if (value >= cpu->pmsav7_dregion) {
1838             qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %"
1839                           PRIu32 "/%" PRIu32 "\n",
1840                           value, cpu->pmsav7_dregion);
1841         } else {
1842             cpu->env.pmsav7.rnr[attrs.secure] = value;
1843         }
1844         break;
1845     case 0xd9c: /* MPU_RBAR */
1846     case 0xda4: /* MPU_RBAR_A1 */
1847     case 0xdac: /* MPU_RBAR_A2 */
1848     case 0xdb4: /* MPU_RBAR_A3 */
1849     {
1850         int region;
1851 
1852         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1853             /* PMSAv8M handling of the aliases is different from v7M:
1854              * aliases A1, A2, A3 override the low two bits of the region
1855              * number in MPU_RNR, and there is no 'region' field in the
1856              * RBAR register.
1857              */
1858             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1859 
1860             region = cpu->env.pmsav7.rnr[attrs.secure];
1861             if (aliasno) {
1862                 region = deposit32(region, 0, 2, aliasno);
1863             }
1864             if (region >= cpu->pmsav7_dregion) {
1865                 return;
1866             }
1867             cpu->env.pmsav8.rbar[attrs.secure][region] = value;
1868             tlb_flush(CPU(cpu));
1869             return;
1870         }
1871 
1872         if (value & (1 << 4)) {
1873             /* VALID bit means use the region number specified in this
1874              * value and also update MPU_RNR.REGION with that value.
1875              */
1876             region = extract32(value, 0, 4);
1877             if (region >= cpu->pmsav7_dregion) {
1878                 qemu_log_mask(LOG_GUEST_ERROR,
1879                               "MPU region out of range %u/%" PRIu32 "\n",
1880                               region, cpu->pmsav7_dregion);
1881                 return;
1882             }
1883             cpu->env.pmsav7.rnr[attrs.secure] = region;
1884         } else {
1885             region = cpu->env.pmsav7.rnr[attrs.secure];
1886         }
1887 
1888         if (region >= cpu->pmsav7_dregion) {
1889             return;
1890         }
1891 
1892         cpu->env.pmsav7.drbar[region] = value & ~0x1f;
1893         tlb_flush(CPU(cpu));
1894         break;
1895     }
1896     case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1897     case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1898     case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1899     case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
1900     {
1901         int region = cpu->env.pmsav7.rnr[attrs.secure];
1902 
1903         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1904             /* PMSAv8M handling of the aliases is different from v7M:
1905              * aliases A1, A2, A3 override the low two bits of the region
1906              * number in MPU_RNR.
1907              */
1908             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1909 
1910             region = cpu->env.pmsav7.rnr[attrs.secure];
1911             if (aliasno) {
1912                 region = deposit32(region, 0, 2, aliasno);
1913             }
1914             if (region >= cpu->pmsav7_dregion) {
1915                 return;
1916             }
1917             cpu->env.pmsav8.rlar[attrs.secure][region] = value;
1918             tlb_flush(CPU(cpu));
1919             return;
1920         }
1921 
1922         if (region >= cpu->pmsav7_dregion) {
1923             return;
1924         }
1925 
1926         cpu->env.pmsav7.drsr[region] = value & 0xff3f;
1927         cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f;
1928         tlb_flush(CPU(cpu));
1929         break;
1930     }
1931     case 0xdc0: /* MPU_MAIR0 */
1932         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1933             goto bad_offset;
1934         }
1935         if (cpu->pmsav7_dregion) {
1936             /* Register is RES0 if no MPU regions are implemented */
1937             cpu->env.pmsav8.mair0[attrs.secure] = value;
1938         }
1939         /* We don't need to do anything else because memory attributes
1940          * only affect cacheability, and we don't implement caching.
1941          */
1942         break;
1943     case 0xdc4: /* MPU_MAIR1 */
1944         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1945             goto bad_offset;
1946         }
1947         if (cpu->pmsav7_dregion) {
1948             /* Register is RES0 if no MPU regions are implemented */
1949             cpu->env.pmsav8.mair1[attrs.secure] = value;
1950         }
1951         /* We don't need to do anything else because memory attributes
1952          * only affect cacheability, and we don't implement caching.
1953          */
1954         break;
1955     case 0xdd0: /* SAU_CTRL */
1956         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1957             goto bad_offset;
1958         }
1959         if (!attrs.secure) {
1960             return;
1961         }
1962         cpu->env.sau.ctrl = value & 3;
1963         break;
1964     case 0xdd4: /* SAU_TYPE */
1965         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1966             goto bad_offset;
1967         }
1968         break;
1969     case 0xdd8: /* SAU_RNR */
1970         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1971             goto bad_offset;
1972         }
1973         if (!attrs.secure) {
1974             return;
1975         }
1976         if (value >= cpu->sau_sregion) {
1977             qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %"
1978                           PRIu32 "/%" PRIu32 "\n",
1979                           value, cpu->sau_sregion);
1980         } else {
1981             cpu->env.sau.rnr = value;
1982         }
1983         break;
1984     case 0xddc: /* SAU_RBAR */
1985     {
1986         int region = cpu->env.sau.rnr;
1987 
1988         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1989             goto bad_offset;
1990         }
1991         if (!attrs.secure) {
1992             return;
1993         }
1994         if (region >= cpu->sau_sregion) {
1995             return;
1996         }
1997         cpu->env.sau.rbar[region] = value & ~0x1f;
1998         tlb_flush(CPU(cpu));
1999         break;
2000     }
2001     case 0xde0: /* SAU_RLAR */
2002     {
2003         int region = cpu->env.sau.rnr;
2004 
2005         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
2006             goto bad_offset;
2007         }
2008         if (!attrs.secure) {
2009             return;
2010         }
2011         if (region >= cpu->sau_sregion) {
2012             return;
2013         }
2014         cpu->env.sau.rlar[region] = value & ~0x1c;
2015         tlb_flush(CPU(cpu));
2016         break;
2017     }
2018     case 0xde4: /* SFSR */
2019         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
2020             goto bad_offset;
2021         }
2022         if (!attrs.secure) {
2023             return;
2024         }
2025         cpu->env.v7m.sfsr &= ~value; /* W1C */
2026         break;
2027     case 0xde8: /* SFAR */
2028         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
2029             goto bad_offset;
2030         }
2031         if (!attrs.secure) {
2032             return;
2033         }
2034         cpu->env.v7m.sfsr = value;
2035         break;
2036     case 0xf00: /* Software Triggered Interrupt Register */
2037     {
2038         int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ;
2039 
2040         if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) {
2041             goto bad_offset;
2042         }
2043 
2044         if (excnum < s->num_irq) {
2045             armv7m_nvic_set_pending(s, excnum, false);
2046         }
2047         break;
2048     }
2049     case 0xf04: /* RFSR */
2050         if (!cpu_isar_feature(aa32_ras, cpu)) {
2051             goto bad_offset;
2052         }
2053         /* We provide minimal-RAS only: RFSR is RAZ/WI */
2054         break;
2055     case 0xf34: /* FPCCR */
2056         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
2057             /* Not all bits here are banked. */
2058             uint32_t fpccr_s;
2059 
2060             if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
2061                 /* Don't allow setting of bits not present in v7M */
2062                 value &= (R_V7M_FPCCR_LSPACT_MASK |
2063                           R_V7M_FPCCR_USER_MASK |
2064                           R_V7M_FPCCR_THREAD_MASK |
2065                           R_V7M_FPCCR_HFRDY_MASK |
2066                           R_V7M_FPCCR_MMRDY_MASK |
2067                           R_V7M_FPCCR_BFRDY_MASK |
2068                           R_V7M_FPCCR_MONRDY_MASK |
2069                           R_V7M_FPCCR_LSPEN_MASK |
2070                           R_V7M_FPCCR_ASPEN_MASK);
2071             }
2072             value &= ~R_V7M_FPCCR_RES0_MASK;
2073 
2074             if (!attrs.secure) {
2075                 /* Some non-banked bits are configurably writable by NS */
2076                 fpccr_s = cpu->env.v7m.fpccr[M_REG_S];
2077                 if (!(fpccr_s & R_V7M_FPCCR_LSPENS_MASK)) {
2078                     uint32_t lspen = FIELD_EX32(value, V7M_FPCCR, LSPEN);
2079                     fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, LSPEN, lspen);
2080                 }
2081                 if (!(fpccr_s & R_V7M_FPCCR_CLRONRETS_MASK)) {
2082                     uint32_t cor = FIELD_EX32(value, V7M_FPCCR, CLRONRET);
2083                     fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, CLRONRET, cor);
2084                 }
2085                 if ((s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2086                     uint32_t hfrdy = FIELD_EX32(value, V7M_FPCCR, HFRDY);
2087                     uint32_t bfrdy = FIELD_EX32(value, V7M_FPCCR, BFRDY);
2088                     fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, HFRDY, hfrdy);
2089                     fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, BFRDY, bfrdy);
2090                 }
2091                 /* TODO MONRDY should RAZ/WI if DEMCR.SDME is set */
2092                 {
2093                     uint32_t monrdy = FIELD_EX32(value, V7M_FPCCR, MONRDY);
2094                     fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, MONRDY, monrdy);
2095                 }
2096 
2097                 /*
2098                  * All other non-banked bits are RAZ/WI from NS; write
2099                  * just the banked bits to fpccr[M_REG_NS].
2100                  */
2101                 value &= R_V7M_FPCCR_BANKED_MASK;
2102                 cpu->env.v7m.fpccr[M_REG_NS] = value;
2103             } else {
2104                 fpccr_s = value;
2105             }
2106             cpu->env.v7m.fpccr[M_REG_S] = fpccr_s;
2107         }
2108         break;
2109     case 0xf38: /* FPCAR */
2110         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
2111             value &= ~7;
2112             cpu->env.v7m.fpcar[attrs.secure] = value;
2113         }
2114         break;
2115     case 0xf3c: /* FPDSCR */
2116         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
2117             uint32_t mask = FPCR_AHP | FPCR_DN | FPCR_FZ | FPCR_RMODE_MASK;
2118             if (cpu_isar_feature(any_fp16, cpu)) {
2119                 mask |= FPCR_FZ16;
2120             }
2121             value &= mask;
2122             if (cpu_isar_feature(aa32_lob, cpu)) {
2123                 value |= 4 << FPCR_LTPSIZE_SHIFT;
2124             }
2125             cpu->env.v7m.fpdscr[attrs.secure] = value;
2126         }
2127         break;
2128     case 0xf50: /* ICIALLU */
2129     case 0xf58: /* ICIMVAU */
2130     case 0xf5c: /* DCIMVAC */
2131     case 0xf60: /* DCISW */
2132     case 0xf64: /* DCCMVAU */
2133     case 0xf68: /* DCCMVAC */
2134     case 0xf6c: /* DCCSW */
2135     case 0xf70: /* DCCIMVAC */
2136     case 0xf74: /* DCCISW */
2137     case 0xf78: /* BPIALL */
2138         /* Cache and branch predictor maintenance: for QEMU these always NOP */
2139         break;
2140     default:
2141     bad_offset:
2142         qemu_log_mask(LOG_GUEST_ERROR,
2143                       "NVIC: Bad write offset 0x%x\n", offset);
2144     }
2145 }
2146 
2147 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs)
2148 {
2149     /* Return true if unprivileged access to this register is permitted. */
2150     switch (offset) {
2151     case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */
2152         /* For access via STIR_NS it is the NS CCR.USERSETMPEND that
2153          * controls access even though the CPU is in Secure state (I_QDKX).
2154          */
2155         return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK;
2156     default:
2157         /* All other user accesses cause a BusFault unconditionally */
2158         return false;
2159     }
2160 }
2161 
2162 static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs)
2163 {
2164     /* Behaviour for the SHPR register field for this exception:
2165      * return M_REG_NS to use the nonsecure vector (including for
2166      * non-banked exceptions), M_REG_S for the secure version of
2167      * a banked exception, and -1 if this field should RAZ/WI.
2168      */
2169     switch (exc) {
2170     case ARMV7M_EXCP_MEM:
2171     case ARMV7M_EXCP_USAGE:
2172     case ARMV7M_EXCP_SVC:
2173     case ARMV7M_EXCP_PENDSV:
2174     case ARMV7M_EXCP_SYSTICK:
2175         /* Banked exceptions */
2176         return attrs.secure;
2177     case ARMV7M_EXCP_BUS:
2178         /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */
2179         if (!attrs.secure &&
2180             !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2181             return -1;
2182         }
2183         return M_REG_NS;
2184     case ARMV7M_EXCP_SECURE:
2185         /* Not banked, RAZ/WI from nonsecure */
2186         if (!attrs.secure) {
2187             return -1;
2188         }
2189         return M_REG_NS;
2190     case ARMV7M_EXCP_DEBUG:
2191         /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */
2192         return M_REG_NS;
2193     case 8 ... 10:
2194     case 13:
2195         /* RES0 */
2196         return -1;
2197     default:
2198         /* Not reachable due to decode of SHPR register addresses */
2199         g_assert_not_reached();
2200     }
2201 }
2202 
2203 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr,
2204                                     uint64_t *data, unsigned size,
2205                                     MemTxAttrs attrs)
2206 {
2207     NVICState *s = (NVICState *)opaque;
2208     uint32_t offset = addr;
2209     unsigned i, startvec, end;
2210     uint32_t val;
2211 
2212     if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
2213         /* Generate BusFault for unprivileged accesses */
2214         return MEMTX_ERROR;
2215     }
2216 
2217     switch (offset) {
2218     /* reads of set and clear both return the status */
2219     case 0x100 ... 0x13f: /* NVIC Set enable */
2220         offset += 0x80;
2221         /* fall through */
2222     case 0x180 ... 0x1bf: /* NVIC Clear enable */
2223         val = 0;
2224         startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; /* vector # */
2225 
2226         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2227             if (s->vectors[startvec + i].enabled &&
2228                 (attrs.secure || s->itns[startvec + i])) {
2229                 val |= (1 << i);
2230             }
2231         }
2232         break;
2233     case 0x200 ... 0x23f: /* NVIC Set pend */
2234         offset += 0x80;
2235         /* fall through */
2236     case 0x280 ... 0x2bf: /* NVIC Clear pend */
2237         val = 0;
2238         startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
2239         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2240             if (s->vectors[startvec + i].pending &&
2241                 (attrs.secure || s->itns[startvec + i])) {
2242                 val |= (1 << i);
2243             }
2244         }
2245         break;
2246     case 0x300 ... 0x33f: /* NVIC Active */
2247         val = 0;
2248 
2249         if (!arm_feature(&s->cpu->env, ARM_FEATURE_V7)) {
2250             break;
2251         }
2252 
2253         startvec = 8 * (offset - 0x300) + NVIC_FIRST_IRQ; /* vector # */
2254 
2255         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2256             if (s->vectors[startvec + i].active &&
2257                 (attrs.secure || s->itns[startvec + i])) {
2258                 val |= (1 << i);
2259             }
2260         }
2261         break;
2262     case 0x400 ... 0x5ef: /* NVIC Priority */
2263         val = 0;
2264         startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */
2265 
2266         for (i = 0; i < size && startvec + i < s->num_irq; i++) {
2267             if (attrs.secure || s->itns[startvec + i]) {
2268                 val |= s->vectors[startvec + i].prio << (8 * i);
2269             }
2270         }
2271         break;
2272     case 0xd18 ... 0xd1b: /* System Handler Priority (SHPR1) */
2273         if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2274             val = 0;
2275             break;
2276         }
2277         /* fall through */
2278     case 0xd1c ... 0xd23: /* System Handler Priority (SHPR2, SHPR3) */
2279         val = 0;
2280         for (i = 0; i < size; i++) {
2281             unsigned hdlidx = (offset - 0xd14) + i;
2282             int sbank = shpr_bank(s, hdlidx, attrs);
2283 
2284             if (sbank < 0) {
2285                 continue;
2286             }
2287             val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank));
2288         }
2289         break;
2290     case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
2291         if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2292             val = 0;
2293             break;
2294         };
2295         /*
2296          * The BFSR bits [15:8] are shared between security states
2297          * and we store them in the NS copy. They are RAZ/WI for
2298          * NS code if AIRCR.BFHFNMINS is 0.
2299          */
2300         val = s->cpu->env.v7m.cfsr[attrs.secure];
2301         if (!attrs.secure &&
2302             !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2303             val &= ~R_V7M_CFSR_BFSR_MASK;
2304         } else {
2305             val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK;
2306         }
2307         val = extract32(val, (offset - 0xd28) * 8, size * 8);
2308         break;
2309     case 0xfe0 ... 0xfff: /* ID.  */
2310         if (offset & 3) {
2311             val = 0;
2312         } else {
2313             val = nvic_id[(offset - 0xfe0) >> 2];
2314         }
2315         break;
2316     default:
2317         if (size == 4) {
2318             val = nvic_readl(s, offset, attrs);
2319         } else {
2320             qemu_log_mask(LOG_GUEST_ERROR,
2321                           "NVIC: Bad read of size %d at offset 0x%x\n",
2322                           size, offset);
2323             val = 0;
2324         }
2325     }
2326 
2327     trace_nvic_sysreg_read(addr, val, size);
2328     *data = val;
2329     return MEMTX_OK;
2330 }
2331 
2332 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr,
2333                                      uint64_t value, unsigned size,
2334                                      MemTxAttrs attrs)
2335 {
2336     NVICState *s = (NVICState *)opaque;
2337     uint32_t offset = addr;
2338     unsigned i, startvec, end;
2339     unsigned setval = 0;
2340 
2341     trace_nvic_sysreg_write(addr, value, size);
2342 
2343     if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
2344         /* Generate BusFault for unprivileged accesses */
2345         return MEMTX_ERROR;
2346     }
2347 
2348     switch (offset) {
2349     case 0x100 ... 0x13f: /* NVIC Set enable */
2350         offset += 0x80;
2351         setval = 1;
2352         /* fall through */
2353     case 0x180 ... 0x1bf: /* NVIC Clear enable */
2354         startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ;
2355 
2356         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2357             if (value & (1 << i) &&
2358                 (attrs.secure || s->itns[startvec + i])) {
2359                 s->vectors[startvec + i].enabled = setval;
2360             }
2361         }
2362         nvic_irq_update(s);
2363         goto exit_ok;
2364     case 0x200 ... 0x23f: /* NVIC Set pend */
2365         /* the special logic in armv7m_nvic_set_pending()
2366          * is not needed since IRQs are never escalated
2367          */
2368         offset += 0x80;
2369         setval = 1;
2370         /* fall through */
2371     case 0x280 ... 0x2bf: /* NVIC Clear pend */
2372         startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
2373 
2374         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
2375             if (value & (1 << i) &&
2376                 (attrs.secure || s->itns[startvec + i])) {
2377                 s->vectors[startvec + i].pending = setval;
2378             }
2379         }
2380         nvic_irq_update(s);
2381         goto exit_ok;
2382     case 0x300 ... 0x33f: /* NVIC Active */
2383         goto exit_ok; /* R/O */
2384     case 0x400 ... 0x5ef: /* NVIC Priority */
2385         startvec = (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */
2386 
2387         for (i = 0; i < size && startvec + i < s->num_irq; i++) {
2388             if (attrs.secure || s->itns[startvec + i]) {
2389                 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff);
2390             }
2391         }
2392         nvic_irq_update(s);
2393         goto exit_ok;
2394     case 0xd18 ... 0xd1b: /* System Handler Priority (SHPR1) */
2395         if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2396             goto exit_ok;
2397         }
2398         /* fall through */
2399     case 0xd1c ... 0xd23: /* System Handler Priority (SHPR2, SHPR3) */
2400         for (i = 0; i < size; i++) {
2401             unsigned hdlidx = (offset - 0xd14) + i;
2402             int newprio = extract32(value, i * 8, 8);
2403             int sbank = shpr_bank(s, hdlidx, attrs);
2404 
2405             if (sbank < 0) {
2406                 continue;
2407             }
2408             set_prio(s, hdlidx, sbank, newprio);
2409         }
2410         nvic_irq_update(s);
2411         goto exit_ok;
2412     case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
2413         if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) {
2414             goto exit_ok;
2415         }
2416         /* All bits are W1C, so construct 32 bit value with 0s in
2417          * the parts not written by the access size
2418          */
2419         value <<= ((offset - 0xd28) * 8);
2420 
2421         if (!attrs.secure &&
2422             !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
2423             /* BFSR bits are RAZ/WI for NS if BFHFNMINS is set */
2424             value &= ~R_V7M_CFSR_BFSR_MASK;
2425         }
2426 
2427         s->cpu->env.v7m.cfsr[attrs.secure] &= ~value;
2428         if (attrs.secure) {
2429             /* The BFSR bits [15:8] are shared between security states
2430              * and we store them in the NS copy.
2431              */
2432             s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK);
2433         }
2434         goto exit_ok;
2435     }
2436     if (size == 4) {
2437         nvic_writel(s, offset, value, attrs);
2438         goto exit_ok;
2439     }
2440     qemu_log_mask(LOG_GUEST_ERROR,
2441                   "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
2442     /* This is UNPREDICTABLE; treat as RAZ/WI */
2443 
2444  exit_ok:
2445     /* Ensure any changes made are reflected in the cached hflags.  */
2446     arm_rebuild_hflags(&s->cpu->env);
2447     return MEMTX_OK;
2448 }
2449 
2450 static const MemoryRegionOps nvic_sysreg_ops = {
2451     .read_with_attrs = nvic_sysreg_read,
2452     .write_with_attrs = nvic_sysreg_write,
2453     .endianness = DEVICE_NATIVE_ENDIAN,
2454 };
2455 
2456 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr,
2457                                         uint64_t value, unsigned size,
2458                                         MemTxAttrs attrs)
2459 {
2460     MemoryRegion *mr = opaque;
2461 
2462     if (attrs.secure) {
2463         /* S accesses to the alias act like NS accesses to the real region */
2464         attrs.secure = 0;
2465         return memory_region_dispatch_write(mr, addr, value,
2466                                             size_memop(size) | MO_TE, attrs);
2467     } else {
2468         /* NS attrs are RAZ/WI for privileged, and BusFault for user */
2469         if (attrs.user) {
2470             return MEMTX_ERROR;
2471         }
2472         return MEMTX_OK;
2473     }
2474 }
2475 
2476 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr,
2477                                        uint64_t *data, unsigned size,
2478                                        MemTxAttrs attrs)
2479 {
2480     MemoryRegion *mr = opaque;
2481 
2482     if (attrs.secure) {
2483         /* S accesses to the alias act like NS accesses to the real region */
2484         attrs.secure = 0;
2485         return memory_region_dispatch_read(mr, addr, data,
2486                                            size_memop(size) | MO_TE, attrs);
2487     } else {
2488         /* NS attrs are RAZ/WI for privileged, and BusFault for user */
2489         if (attrs.user) {
2490             return MEMTX_ERROR;
2491         }
2492         *data = 0;
2493         return MEMTX_OK;
2494     }
2495 }
2496 
2497 static const MemoryRegionOps nvic_sysreg_ns_ops = {
2498     .read_with_attrs = nvic_sysreg_ns_read,
2499     .write_with_attrs = nvic_sysreg_ns_write,
2500     .endianness = DEVICE_NATIVE_ENDIAN,
2501 };
2502 
2503 static MemTxResult nvic_systick_write(void *opaque, hwaddr addr,
2504                                       uint64_t value, unsigned size,
2505                                       MemTxAttrs attrs)
2506 {
2507     NVICState *s = opaque;
2508     MemoryRegion *mr;
2509 
2510     /* Direct the access to the correct systick */
2511     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
2512     return memory_region_dispatch_write(mr, addr, value,
2513                                         size_memop(size) | MO_TE, attrs);
2514 }
2515 
2516 static MemTxResult nvic_systick_read(void *opaque, hwaddr addr,
2517                                      uint64_t *data, unsigned size,
2518                                      MemTxAttrs attrs)
2519 {
2520     NVICState *s = opaque;
2521     MemoryRegion *mr;
2522 
2523     /* Direct the access to the correct systick */
2524     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
2525     return memory_region_dispatch_read(mr, addr, data, size_memop(size) | MO_TE,
2526                                        attrs);
2527 }
2528 
2529 static const MemoryRegionOps nvic_systick_ops = {
2530     .read_with_attrs = nvic_systick_read,
2531     .write_with_attrs = nvic_systick_write,
2532     .endianness = DEVICE_NATIVE_ENDIAN,
2533 };
2534 
2535 
2536 static MemTxResult ras_read(void *opaque, hwaddr addr,
2537                             uint64_t *data, unsigned size,
2538                             MemTxAttrs attrs)
2539 {
2540     if (attrs.user) {
2541         return MEMTX_ERROR;
2542     }
2543 
2544     switch (addr) {
2545     case 0xe10: /* ERRIIDR */
2546         /* architect field = Arm; product/variant/revision 0 */
2547         *data = 0x43b;
2548         break;
2549     case 0xfc8: /* ERRDEVID */
2550         /* Minimal RAS: we implement 0 error record indexes */
2551         *data = 0;
2552         break;
2553     default:
2554         qemu_log_mask(LOG_UNIMP, "Read RAS register offset 0x%x\n",
2555                       (uint32_t)addr);
2556         *data = 0;
2557         break;
2558     }
2559     return MEMTX_OK;
2560 }
2561 
2562 static MemTxResult ras_write(void *opaque, hwaddr addr,
2563                              uint64_t value, unsigned size,
2564                              MemTxAttrs attrs)
2565 {
2566     if (attrs.user) {
2567         return MEMTX_ERROR;
2568     }
2569 
2570     switch (addr) {
2571     default:
2572         qemu_log_mask(LOG_UNIMP, "Write to RAS register offset 0x%x\n",
2573                       (uint32_t)addr);
2574         break;
2575     }
2576     return MEMTX_OK;
2577 }
2578 
2579 static const MemoryRegionOps ras_ops = {
2580     .read_with_attrs = ras_read,
2581     .write_with_attrs = ras_write,
2582     .endianness = DEVICE_NATIVE_ENDIAN,
2583 };
2584 
2585 /*
2586  * Unassigned portions of the PPB space are RAZ/WI for privileged
2587  * accesses, and fault for non-privileged accesses.
2588  */
2589 static MemTxResult ppb_default_read(void *opaque, hwaddr addr,
2590                                     uint64_t *data, unsigned size,
2591                                     MemTxAttrs attrs)
2592 {
2593     qemu_log_mask(LOG_UNIMP, "Read of unassigned area of PPB: offset 0x%x\n",
2594                   (uint32_t)addr);
2595     if (attrs.user) {
2596         return MEMTX_ERROR;
2597     }
2598     *data = 0;
2599     return MEMTX_OK;
2600 }
2601 
2602 static MemTxResult ppb_default_write(void *opaque, hwaddr addr,
2603                                      uint64_t value, unsigned size,
2604                                      MemTxAttrs attrs)
2605 {
2606     qemu_log_mask(LOG_UNIMP, "Write of unassigned area of PPB: offset 0x%x\n",
2607                   (uint32_t)addr);
2608     if (attrs.user) {
2609         return MEMTX_ERROR;
2610     }
2611     return MEMTX_OK;
2612 }
2613 
2614 static const MemoryRegionOps ppb_default_ops = {
2615     .read_with_attrs = ppb_default_read,
2616     .write_with_attrs = ppb_default_write,
2617     .endianness = DEVICE_NATIVE_ENDIAN,
2618     .valid.min_access_size = 1,
2619     .valid.max_access_size = 8,
2620 };
2621 
2622 static int nvic_post_load(void *opaque, int version_id)
2623 {
2624     NVICState *s = opaque;
2625     unsigned i;
2626     int resetprio;
2627 
2628     /* Check for out of range priority settings */
2629     resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
2630 
2631     if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio ||
2632         s->vectors[ARMV7M_EXCP_NMI].prio != -2 ||
2633         s->vectors[ARMV7M_EXCP_HARD].prio != -1) {
2634         return 1;
2635     }
2636     for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) {
2637         if (s->vectors[i].prio & ~0xff) {
2638             return 1;
2639         }
2640     }
2641 
2642     nvic_recompute_state(s);
2643 
2644     return 0;
2645 }
2646 
2647 static const VMStateDescription vmstate_VecInfo = {
2648     .name = "armv7m_nvic_info",
2649     .version_id = 1,
2650     .minimum_version_id = 1,
2651     .fields = (VMStateField[]) {
2652         VMSTATE_INT16(prio, VecInfo),
2653         VMSTATE_UINT8(enabled, VecInfo),
2654         VMSTATE_UINT8(pending, VecInfo),
2655         VMSTATE_UINT8(active, VecInfo),
2656         VMSTATE_UINT8(level, VecInfo),
2657         VMSTATE_END_OF_LIST()
2658     }
2659 };
2660 
2661 static bool nvic_security_needed(void *opaque)
2662 {
2663     NVICState *s = opaque;
2664 
2665     return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
2666 }
2667 
2668 static int nvic_security_post_load(void *opaque, int version_id)
2669 {
2670     NVICState *s = opaque;
2671     int i;
2672 
2673     /* Check for out of range priority settings */
2674     if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1
2675         && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) {
2676         /* We can't cross-check against AIRCR.BFHFNMINS as we don't know
2677          * if the CPU state has been migrated yet; a mismatch won't
2678          * cause the emulation to blow up, though.
2679          */
2680         return 1;
2681     }
2682     for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) {
2683         if (s->sec_vectors[i].prio & ~0xff) {
2684             return 1;
2685         }
2686     }
2687     return 0;
2688 }
2689 
2690 static const VMStateDescription vmstate_nvic_security = {
2691     .name = "armv7m_nvic/m-security",
2692     .version_id = 1,
2693     .minimum_version_id = 1,
2694     .needed = nvic_security_needed,
2695     .post_load = &nvic_security_post_load,
2696     .fields = (VMStateField[]) {
2697         VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1,
2698                              vmstate_VecInfo, VecInfo),
2699         VMSTATE_UINT32(prigroup[M_REG_S], NVICState),
2700         VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS),
2701         VMSTATE_END_OF_LIST()
2702     }
2703 };
2704 
2705 static const VMStateDescription vmstate_nvic = {
2706     .name = "armv7m_nvic",
2707     .version_id = 4,
2708     .minimum_version_id = 4,
2709     .post_load = &nvic_post_load,
2710     .fields = (VMStateField[]) {
2711         VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1,
2712                              vmstate_VecInfo, VecInfo),
2713         VMSTATE_UINT32(prigroup[M_REG_NS], NVICState),
2714         VMSTATE_END_OF_LIST()
2715     },
2716     .subsections = (const VMStateDescription*[]) {
2717         &vmstate_nvic_security,
2718         NULL
2719     }
2720 };
2721 
2722 static Property props_nvic[] = {
2723     /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
2724     DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64),
2725     DEFINE_PROP_END_OF_LIST()
2726 };
2727 
2728 static void armv7m_nvic_reset(DeviceState *dev)
2729 {
2730     int resetprio;
2731     NVICState *s = NVIC(dev);
2732 
2733     memset(s->vectors, 0, sizeof(s->vectors));
2734     memset(s->sec_vectors, 0, sizeof(s->sec_vectors));
2735     s->prigroup[M_REG_NS] = 0;
2736     s->prigroup[M_REG_S] = 0;
2737 
2738     s->vectors[ARMV7M_EXCP_NMI].enabled = 1;
2739     /* MEM, BUS, and USAGE are enabled through
2740      * the System Handler Control register
2741      */
2742     s->vectors[ARMV7M_EXCP_SVC].enabled = 1;
2743     s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2744     s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2745 
2746     /* DebugMonitor is enabled via DEMCR.MON_EN */
2747     s->vectors[ARMV7M_EXCP_DEBUG].enabled = 0;
2748 
2749     resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
2750     s->vectors[ARMV7M_EXCP_RESET].prio = resetprio;
2751     s->vectors[ARMV7M_EXCP_NMI].prio = -2;
2752     s->vectors[ARMV7M_EXCP_HARD].prio = -1;
2753 
2754     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2755         s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1;
2756         s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1;
2757         s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2758         s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2759 
2760         /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */
2761         s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
2762         /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */
2763         s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
2764     } else {
2765         s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
2766     }
2767 
2768     /* Strictly speaking the reset handler should be enabled.
2769      * However, we don't simulate soft resets through the NVIC,
2770      * and the reset vector should never be pended.
2771      * So we leave it disabled to catch logic errors.
2772      */
2773 
2774     s->exception_prio = NVIC_NOEXC_PRIO;
2775     s->vectpending = 0;
2776     s->vectpending_is_s_banked = false;
2777     s->vectpending_prio = NVIC_NOEXC_PRIO;
2778 
2779     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2780         memset(s->itns, 0, sizeof(s->itns));
2781     } else {
2782         /* This state is constant and not guest accessible in a non-security
2783          * NVIC; we set the bits to true to avoid having to do a feature
2784          * bit check in the NVIC enable/pend/etc register accessors.
2785          */
2786         int i;
2787 
2788         for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) {
2789             s->itns[i] = true;
2790         }
2791     }
2792 
2793     /*
2794      * We updated state that affects the CPU's MMUidx and thus its hflags;
2795      * and we can't guarantee that we run before the CPU reset function.
2796      */
2797     arm_rebuild_hflags(&s->cpu->env);
2798 }
2799 
2800 static void nvic_systick_trigger(void *opaque, int n, int level)
2801 {
2802     NVICState *s = opaque;
2803 
2804     if (level) {
2805         /* SysTick just asked us to pend its exception.
2806          * (This is different from an external interrupt line's
2807          * behaviour.)
2808          * n == 0 : NonSecure systick
2809          * n == 1 : Secure systick
2810          */
2811         armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n);
2812     }
2813 }
2814 
2815 static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
2816 {
2817     NVICState *s = NVIC(dev);
2818 
2819     /* The armv7m container object will have set our CPU pointer */
2820     if (!s->cpu || !arm_feature(&s->cpu->env, ARM_FEATURE_M)) {
2821         error_setg(errp, "The NVIC can only be used with a Cortex-M CPU");
2822         return;
2823     }
2824 
2825     if (s->num_irq > NVIC_MAX_IRQ) {
2826         error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);
2827         return;
2828     }
2829 
2830     qdev_init_gpio_in(dev, set_irq_level, s->num_irq);
2831 
2832     /* include space for internal exception vectors */
2833     s->num_irq += NVIC_FIRST_IRQ;
2834 
2835     s->num_prio_bits = arm_feature(&s->cpu->env, ARM_FEATURE_V7) ? 8 : 2;
2836 
2837     if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), errp)) {
2838         return;
2839     }
2840     sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0,
2841                        qdev_get_gpio_in_named(dev, "systick-trigger",
2842                                               M_REG_NS));
2843 
2844     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2845         /* We couldn't init the secure systick device in instance_init
2846          * as we didn't know then if the CPU had the security extensions;
2847          * so we have to do it here.
2848          */
2849         object_initialize_child(OBJECT(dev), "systick-reg-s",
2850                                 &s->systick[M_REG_S], TYPE_SYSTICK);
2851 
2852         if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_S]), errp)) {
2853             return;
2854         }
2855         sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0,
2856                            qdev_get_gpio_in_named(dev, "systick-trigger",
2857                                                   M_REG_S));
2858     }
2859 
2860     /*
2861      * This device provides a single sysbus memory region which
2862      * represents the whole of the "System PPB" space. This is the
2863      * range from 0xe0000000 to 0xe00fffff and includes the NVIC,
2864      * the System Control Space (system registers), the systick timer,
2865      * and for CPUs with the Security extension an NS banked version
2866      * of all of these.
2867      *
2868      * The default behaviour for unimplemented registers/ranges
2869      * (for instance the Data Watchpoint and Trace unit at 0xe0001000)
2870      * is to RAZ/WI for privileged access and BusFault for non-privileged
2871      * access.
2872      *
2873      * The NVIC and System Control Space (SCS) starts at 0xe000e000
2874      * and looks like this:
2875      *  0x004 - ICTR
2876      *  0x010 - 0xff - systick
2877      *  0x100..0x7ec - NVIC
2878      *  0x7f0..0xcff - Reserved
2879      *  0xd00..0xd3c - SCS registers
2880      *  0xd40..0xeff - Reserved or Not implemented
2881      *  0xf00 - STIR
2882      *
2883      * Some registers within this space are banked between security states.
2884      * In v8M there is a second range 0xe002e000..0xe002efff which is the
2885      * NonSecure alias SCS; secure accesses to this behave like NS accesses
2886      * to the main SCS range, and non-secure accesses (including when
2887      * the security extension is not implemented) are RAZ/WI.
2888      * Note that both the main SCS range and the alias range are defined
2889      * to be exempt from memory attribution (R_BLJT) and so the memory
2890      * transaction attribute always matches the current CPU security
2891      * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops
2892      * wrappers we change attrs.secure to indicate the NS access; so
2893      * generally code determining which banked register to use should
2894      * use attrs.secure; code determining actual behaviour of the system
2895      * should use env->v7m.secure.
2896      *
2897      * The container covers the whole PPB space. Within it the priority
2898      * of overlapping regions is:
2899      *  - default region (for RAZ/WI and BusFault) : -1
2900      *  - system register regions : 0
2901      *  - systick : 1
2902      * This is because the systick device is a small block of registers
2903      * in the middle of the other system control registers.
2904      */
2905     memory_region_init(&s->container, OBJECT(s), "nvic", 0x100000);
2906     memory_region_init_io(&s->defaultmem, OBJECT(s), &ppb_default_ops, s,
2907                           "nvic-default", 0x100000);
2908     memory_region_add_subregion_overlap(&s->container, 0, &s->defaultmem, -1);
2909     memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
2910                           "nvic_sysregs", 0x1000);
2911     memory_region_add_subregion(&s->container, 0xe000, &s->sysregmem);
2912 
2913     memory_region_init_io(&s->systickmem, OBJECT(s),
2914                           &nvic_systick_ops, s,
2915                           "nvic_systick", 0xe0);
2916 
2917     memory_region_add_subregion_overlap(&s->container, 0xe010,
2918                                         &s->systickmem, 1);
2919 
2920     if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
2921         memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
2922                               &nvic_sysreg_ns_ops, &s->sysregmem,
2923                               "nvic_sysregs_ns", 0x1000);
2924         memory_region_add_subregion(&s->container, 0x2e000, &s->sysreg_ns_mem);
2925         memory_region_init_io(&s->systick_ns_mem, OBJECT(s),
2926                               &nvic_sysreg_ns_ops, &s->systickmem,
2927                               "nvic_systick_ns", 0xe0);
2928         memory_region_add_subregion_overlap(&s->container, 0x2e010,
2929                                             &s->systick_ns_mem, 1);
2930     }
2931 
2932     if (cpu_isar_feature(aa32_ras, s->cpu)) {
2933         memory_region_init_io(&s->ras_mem, OBJECT(s),
2934                               &ras_ops, s, "nvic_ras", 0x1000);
2935         memory_region_add_subregion(&s->container, 0x5000, &s->ras_mem);
2936     }
2937 
2938     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
2939 }
2940 
2941 static void armv7m_nvic_instance_init(Object *obj)
2942 {
2943     DeviceState *dev = DEVICE(obj);
2944     NVICState *nvic = NVIC(obj);
2945     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
2946 
2947     object_initialize_child(obj, "systick-reg-ns", &nvic->systick[M_REG_NS],
2948                             TYPE_SYSTICK);
2949     /* We can't initialize the secure systick here, as we don't know
2950      * yet if we need it.
2951      */
2952 
2953     sysbus_init_irq(sbd, &nvic->excpout);
2954     qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
2955     qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger",
2956                             M_REG_NUM_BANKS);
2957     qdev_init_gpio_in_named(dev, nvic_nmi_trigger, "NMI", 1);
2958 }
2959 
2960 static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
2961 {
2962     DeviceClass *dc = DEVICE_CLASS(klass);
2963 
2964     dc->vmsd  = &vmstate_nvic;
2965     device_class_set_props(dc, props_nvic);
2966     dc->reset = armv7m_nvic_reset;
2967     dc->realize = armv7m_nvic_realize;
2968 }
2969 
2970 static const TypeInfo armv7m_nvic_info = {
2971     .name          = TYPE_NVIC,
2972     .parent        = TYPE_SYS_BUS_DEVICE,
2973     .instance_init = armv7m_nvic_instance_init,
2974     .instance_size = sizeof(NVICState),
2975     .class_init    = armv7m_nvic_class_init,
2976     .class_size    = sizeof(SysBusDeviceClass),
2977 };
2978 
2979 static void armv7m_nvic_register_types(void)
2980 {
2981     type_register_static(&armv7m_nvic_info);
2982 }
2983 
2984 type_init(armv7m_nvic_register_types)
2985