xref: /openbmc/qemu/hw/intc/armv7m_nvic.c (revision e1be0a576ba4836e772d717fcc8d3c79e560179b)
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 "qemu-common.h"
16 #include "cpu.h"
17 #include "hw/sysbus.h"
18 #include "qemu/timer.h"
19 #include "hw/arm/arm.h"
20 #include "hw/intc/armv7m_nvic.h"
21 #include "target/arm/cpu.h"
22 #include "exec/exec-all.h"
23 #include "qemu/log.h"
24 #include "trace.h"
25 
26 /* IRQ number counting:
27  *
28  * the num-irq property counts the number of external IRQ lines
29  *
30  * NVICState::num_irq counts the total number of exceptions
31  * (external IRQs, the 15 internal exceptions including reset,
32  * and one for the unused exception number 0).
33  *
34  * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines.
35  *
36  * NVIC_MAX_VECTORS is the highest permitted number of exceptions.
37  *
38  * Iterating through all exceptions should typically be done with
39  * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0.
40  *
41  * The external qemu_irq lines are the NVIC's external IRQ lines,
42  * so line 0 is exception 16.
43  *
44  * In the terminology of the architecture manual, "interrupts" are
45  * a subcategory of exception referring to the external interrupts
46  * (which are exception numbers NVIC_FIRST_IRQ and upward).
47  * For historical reasons QEMU tends to use "interrupt" and
48  * "exception" more or less interchangeably.
49  */
50 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS
51 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ)
52 
53 /* Effective running priority of the CPU when no exception is active
54  * (higher than the highest possible priority value)
55  */
56 #define NVIC_NOEXC_PRIO 0x100
57 
58 static const uint8_t nvic_id[] = {
59     0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
60 };
61 
62 static int nvic_pending_prio(NVICState *s)
63 {
64     /* return the group priority of the current pending interrupt,
65      * or NVIC_NOEXC_PRIO if no interrupt is pending
66      */
67     return s->vectpending_prio;
68 }
69 
70 /* Return the value of the ISCR RETTOBASE bit:
71  * 1 if there is exactly one active exception
72  * 0 if there is more than one active exception
73  * UNKNOWN if there are no active exceptions (we choose 1,
74  * which matches the choice Cortex-M3 is documented as making).
75  *
76  * NB: some versions of the documentation talk about this
77  * counting "active exceptions other than the one shown by IPSR";
78  * this is only different in the obscure corner case where guest
79  * code has manually deactivated an exception and is about
80  * to fail an exception-return integrity check. The definition
81  * above is the one from the v8M ARM ARM and is also in line
82  * with the behaviour documented for the Cortex-M3.
83  */
84 static bool nvic_rettobase(NVICState *s)
85 {
86     int irq, nhand = 0;
87     bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
88 
89     for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) {
90         if (s->vectors[irq].active ||
91             (check_sec && irq < NVIC_INTERNAL_VECTORS &&
92              s->sec_vectors[irq].active)) {
93             nhand++;
94             if (nhand == 2) {
95                 return 0;
96             }
97         }
98     }
99 
100     return 1;
101 }
102 
103 /* Return the value of the ISCR ISRPENDING bit:
104  * 1 if an external interrupt is pending
105  * 0 if no external interrupt is pending
106  */
107 static bool nvic_isrpending(NVICState *s)
108 {
109     int irq;
110 
111     /* We can shortcut if the highest priority pending interrupt
112      * happens to be external or if there is nothing pending.
113      */
114     if (s->vectpending > NVIC_FIRST_IRQ) {
115         return true;
116     }
117     if (s->vectpending == 0) {
118         return false;
119     }
120 
121     for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) {
122         if (s->vectors[irq].pending) {
123             return true;
124         }
125     }
126     return false;
127 }
128 
129 /* Return a mask word which clears the subpriority bits from
130  * a priority value for an M-profile exception, leaving only
131  * the group priority.
132  */
133 static inline uint32_t nvic_gprio_mask(NVICState *s)
134 {
135     return ~0U << (s->prigroup[M_REG_NS] + 1);
136 }
137 
138 /* Recompute vectpending and exception_prio */
139 static void nvic_recompute_state(NVICState *s)
140 {
141     int i;
142     int pend_prio = NVIC_NOEXC_PRIO;
143     int active_prio = NVIC_NOEXC_PRIO;
144     int pend_irq = 0;
145 
146     for (i = 1; i < s->num_irq; i++) {
147         VecInfo *vec = &s->vectors[i];
148 
149         if (vec->enabled && vec->pending && vec->prio < pend_prio) {
150             pend_prio = vec->prio;
151             pend_irq = i;
152         }
153         if (vec->active && vec->prio < active_prio) {
154             active_prio = vec->prio;
155         }
156     }
157 
158     if (active_prio > 0) {
159         active_prio &= nvic_gprio_mask(s);
160     }
161 
162     if (pend_prio > 0) {
163         pend_prio &= nvic_gprio_mask(s);
164     }
165 
166     s->vectpending = pend_irq;
167     s->vectpending_prio = pend_prio;
168     s->exception_prio = active_prio;
169 
170     trace_nvic_recompute_state(s->vectpending,
171                                s->vectpending_prio,
172                                s->exception_prio);
173 }
174 
175 /* Return the current execution priority of the CPU
176  * (equivalent to the pseudocode ExecutionPriority function).
177  * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
178  */
179 static inline int nvic_exec_prio(NVICState *s)
180 {
181     CPUARMState *env = &s->cpu->env;
182     int running;
183 
184     if (env->v7m.faultmask[env->v7m.secure]) {
185         running = -1;
186     } else if (env->v7m.primask[env->v7m.secure]) {
187         running = 0;
188     } else if (env->v7m.basepri[env->v7m.secure] > 0) {
189         running = env->v7m.basepri[env->v7m.secure] & nvic_gprio_mask(s);
190     } else {
191         running = NVIC_NOEXC_PRIO; /* lower than any possible priority */
192     }
193     /* consider priority of active handler */
194     return MIN(running, s->exception_prio);
195 }
196 
197 bool armv7m_nvic_can_take_pending_exception(void *opaque)
198 {
199     NVICState *s = opaque;
200 
201     return nvic_exec_prio(s) > nvic_pending_prio(s);
202 }
203 
204 int armv7m_nvic_raw_execution_priority(void *opaque)
205 {
206     NVICState *s = opaque;
207 
208     return s->exception_prio;
209 }
210 
211 /* caller must call nvic_irq_update() after this */
212 static void set_prio(NVICState *s, unsigned irq, uint8_t prio)
213 {
214     assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
215     assert(irq < s->num_irq);
216 
217     s->vectors[irq].prio = prio;
218 
219     trace_nvic_set_prio(irq, prio);
220 }
221 
222 /* Recompute state and assert irq line accordingly.
223  * Must be called after changes to:
224  *  vec->active, vec->enabled, vec->pending or vec->prio for any vector
225  *  prigroup
226  */
227 static void nvic_irq_update(NVICState *s)
228 {
229     int lvl;
230     int pend_prio;
231 
232     nvic_recompute_state(s);
233     pend_prio = nvic_pending_prio(s);
234 
235     /* Raise NVIC output if this IRQ would be taken, except that we
236      * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
237      * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
238      * to those CPU registers don't cause us to recalculate the NVIC
239      * pending info.
240      */
241     lvl = (pend_prio < s->exception_prio);
242     trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl);
243     qemu_set_irq(s->excpout, lvl);
244 }
245 
246 static void armv7m_nvic_clear_pending(void *opaque, int irq)
247 {
248     NVICState *s = (NVICState *)opaque;
249     VecInfo *vec;
250 
251     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
252 
253     vec = &s->vectors[irq];
254     trace_nvic_clear_pending(irq, vec->enabled, vec->prio);
255     if (vec->pending) {
256         vec->pending = 0;
257         nvic_irq_update(s);
258     }
259 }
260 
261 void armv7m_nvic_set_pending(void *opaque, int irq)
262 {
263     NVICState *s = (NVICState *)opaque;
264     VecInfo *vec;
265 
266     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
267 
268     vec = &s->vectors[irq];
269     trace_nvic_set_pending(irq, vec->enabled, vec->prio);
270 
271 
272     if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) {
273         /* If a synchronous exception is pending then it may be
274          * escalated to HardFault if:
275          *  * it is equal or lower priority to current execution
276          *  * it is disabled
277          * (ie we need to take it immediately but we can't do so).
278          * Asynchronous exceptions (and interrupts) simply remain pending.
279          *
280          * For QEMU, we don't have any imprecise (asynchronous) faults,
281          * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
282          * synchronous.
283          * Debug exceptions are awkward because only Debug exceptions
284          * resulting from the BKPT instruction should be escalated,
285          * but we don't currently implement any Debug exceptions other
286          * than those that result from BKPT, so we treat all debug exceptions
287          * as needing escalation.
288          *
289          * This all means we can identify whether to escalate based only on
290          * the exception number and don't (yet) need the caller to explicitly
291          * tell us whether this exception is synchronous or not.
292          */
293         int running = nvic_exec_prio(s);
294         bool escalate = false;
295 
296         if (vec->prio >= running) {
297             trace_nvic_escalate_prio(irq, vec->prio, running);
298             escalate = true;
299         } else if (!vec->enabled) {
300             trace_nvic_escalate_disabled(irq);
301             escalate = true;
302         }
303 
304         if (escalate) {
305             if (running < 0) {
306                 /* We want to escalate to HardFault but we can't take a
307                  * synchronous HardFault at this point either. This is a
308                  * Lockup condition due to a guest bug. We don't model
309                  * Lockup, so report via cpu_abort() instead.
310                  */
311                 cpu_abort(&s->cpu->parent_obj,
312                           "Lockup: can't escalate %d to HardFault "
313                           "(current priority %d)\n", irq, running);
314             }
315 
316             /* We can do the escalation, so we take HardFault instead */
317             irq = ARMV7M_EXCP_HARD;
318             vec = &s->vectors[irq];
319             s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
320         }
321     }
322 
323     if (!vec->pending) {
324         vec->pending = 1;
325         nvic_irq_update(s);
326     }
327 }
328 
329 /* Make pending IRQ active.  */
330 void armv7m_nvic_acknowledge_irq(void *opaque)
331 {
332     NVICState *s = (NVICState *)opaque;
333     CPUARMState *env = &s->cpu->env;
334     const int pending = s->vectpending;
335     const int running = nvic_exec_prio(s);
336     VecInfo *vec;
337 
338     assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
339 
340     vec = &s->vectors[pending];
341 
342     assert(vec->enabled);
343     assert(vec->pending);
344 
345     assert(s->vectpending_prio < running);
346 
347     trace_nvic_acknowledge_irq(pending, s->vectpending_prio);
348 
349     vec->active = 1;
350     vec->pending = 0;
351 
352     env->v7m.exception = s->vectpending;
353 
354     nvic_irq_update(s);
355 }
356 
357 int armv7m_nvic_complete_irq(void *opaque, int irq)
358 {
359     NVICState *s = (NVICState *)opaque;
360     VecInfo *vec;
361     int ret;
362 
363     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
364 
365     vec = &s->vectors[irq];
366 
367     trace_nvic_complete_irq(irq);
368 
369     if (!vec->active) {
370         /* Tell the caller this was an illegal exception return */
371         return -1;
372     }
373 
374     ret = nvic_rettobase(s);
375 
376     vec->active = 0;
377     if (vec->level) {
378         /* Re-pend the exception if it's still held high; only
379          * happens for extenal IRQs
380          */
381         assert(irq >= NVIC_FIRST_IRQ);
382         vec->pending = 1;
383     }
384 
385     nvic_irq_update(s);
386 
387     return ret;
388 }
389 
390 /* callback when external interrupt line is changed */
391 static void set_irq_level(void *opaque, int n, int level)
392 {
393     NVICState *s = opaque;
394     VecInfo *vec;
395 
396     n += NVIC_FIRST_IRQ;
397 
398     assert(n >= NVIC_FIRST_IRQ && n < s->num_irq);
399 
400     trace_nvic_set_irq_level(n, level);
401 
402     /* The pending status of an external interrupt is
403      * latched on rising edge and exception handler return.
404      *
405      * Pulsing the IRQ will always run the handler
406      * once, and the handler will re-run until the
407      * level is low when the handler completes.
408      */
409     vec = &s->vectors[n];
410     if (level != vec->level) {
411         vec->level = level;
412         if (level) {
413             armv7m_nvic_set_pending(s, n);
414         }
415     }
416 }
417 
418 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
419 {
420     ARMCPU *cpu = s->cpu;
421     uint32_t val;
422 
423     switch (offset) {
424     case 4: /* Interrupt Control Type.  */
425         return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1;
426     case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
427     {
428         int startvec = 32 * (offset - 0x380) + NVIC_FIRST_IRQ;
429         int i;
430 
431         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
432             goto bad_offset;
433         }
434         if (!attrs.secure) {
435             return 0;
436         }
437         val = 0;
438         for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
439             if (s->itns[startvec + i]) {
440                 val |= (1 << i);
441             }
442         }
443         return val;
444     }
445     case 0xd00: /* CPUID Base.  */
446         return cpu->midr;
447     case 0xd04: /* Interrupt Control State.  */
448         /* VECTACTIVE */
449         val = cpu->env.v7m.exception;
450         /* VECTPENDING */
451         val |= (s->vectpending & 0xff) << 12;
452         /* ISRPENDING - set if any external IRQ is pending */
453         if (nvic_isrpending(s)) {
454             val |= (1 << 22);
455         }
456         /* RETTOBASE - set if only one handler is active */
457         if (nvic_rettobase(s)) {
458             val |= (1 << 11);
459         }
460         /* PENDSTSET */
461         if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) {
462             val |= (1 << 26);
463         }
464         /* PENDSVSET */
465         if (s->vectors[ARMV7M_EXCP_PENDSV].pending) {
466             val |= (1 << 28);
467         }
468         /* NMIPENDSET */
469         if (s->vectors[ARMV7M_EXCP_NMI].pending) {
470             val |= (1 << 31);
471         }
472         /* ISRPREEMPT not implemented */
473         return val;
474     case 0xd08: /* Vector Table Offset.  */
475         return cpu->env.v7m.vecbase[attrs.secure];
476     case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
477         val = 0xfa050000 | (s->prigroup[attrs.secure] << 8);
478         if (attrs.secure) {
479             /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */
480             val |= cpu->env.v7m.aircr;
481         } else {
482             if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
483                 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If
484                  * security isn't supported then BFHFNMINS is RAO (and
485                  * the bit in env.v7m.aircr is always set).
486                  */
487                 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK;
488             }
489         }
490         return val;
491     case 0xd10: /* System Control.  */
492         /* TODO: Implement SLEEPONEXIT.  */
493         return 0;
494     case 0xd14: /* Configuration Control.  */
495         /* The BFHFNMIGN bit is the only non-banked bit; we
496          * keep it in the non-secure copy of the register.
497          */
498         val = cpu->env.v7m.ccr[attrs.secure];
499         val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
500         return val;
501     case 0xd24: /* System Handler Status.  */
502         val = 0;
503         if (s->vectors[ARMV7M_EXCP_MEM].active) {
504             val |= (1 << 0);
505         }
506         if (s->vectors[ARMV7M_EXCP_BUS].active) {
507             val |= (1 << 1);
508         }
509         if (s->vectors[ARMV7M_EXCP_USAGE].active) {
510             val |= (1 << 3);
511         }
512         if (s->vectors[ARMV7M_EXCP_SVC].active) {
513             val |= (1 << 7);
514         }
515         if (s->vectors[ARMV7M_EXCP_DEBUG].active) {
516             val |= (1 << 8);
517         }
518         if (s->vectors[ARMV7M_EXCP_PENDSV].active) {
519             val |= (1 << 10);
520         }
521         if (s->vectors[ARMV7M_EXCP_SYSTICK].active) {
522             val |= (1 << 11);
523         }
524         if (s->vectors[ARMV7M_EXCP_USAGE].pending) {
525             val |= (1 << 12);
526         }
527         if (s->vectors[ARMV7M_EXCP_MEM].pending) {
528             val |= (1 << 13);
529         }
530         if (s->vectors[ARMV7M_EXCP_BUS].pending) {
531             val |= (1 << 14);
532         }
533         if (s->vectors[ARMV7M_EXCP_SVC].pending) {
534             val |= (1 << 15);
535         }
536         if (s->vectors[ARMV7M_EXCP_MEM].enabled) {
537             val |= (1 << 16);
538         }
539         if (s->vectors[ARMV7M_EXCP_BUS].enabled) {
540             val |= (1 << 17);
541         }
542         if (s->vectors[ARMV7M_EXCP_USAGE].enabled) {
543             val |= (1 << 18);
544         }
545         return val;
546     case 0xd28: /* Configurable Fault Status.  */
547         /* The BFSR bits [15:8] are shared between security states
548          * and we store them in the NS copy
549          */
550         val = cpu->env.v7m.cfsr[attrs.secure];
551         val |= cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK;
552         return val;
553     case 0xd2c: /* Hard Fault Status.  */
554         return cpu->env.v7m.hfsr;
555     case 0xd30: /* Debug Fault Status.  */
556         return cpu->env.v7m.dfsr;
557     case 0xd34: /* MMFAR MemManage Fault Address */
558         return cpu->env.v7m.mmfar[attrs.secure];
559     case 0xd38: /* Bus Fault Address.  */
560         return cpu->env.v7m.bfar;
561     case 0xd3c: /* Aux Fault Status.  */
562         /* TODO: Implement fault status registers.  */
563         qemu_log_mask(LOG_UNIMP,
564                       "Aux Fault status registers unimplemented\n");
565         return 0;
566     case 0xd40: /* PFR0.  */
567         return 0x00000030;
568     case 0xd44: /* PRF1.  */
569         return 0x00000200;
570     case 0xd48: /* DFR0.  */
571         return 0x00100000;
572     case 0xd4c: /* AFR0.  */
573         return 0x00000000;
574     case 0xd50: /* MMFR0.  */
575         return 0x00000030;
576     case 0xd54: /* MMFR1.  */
577         return 0x00000000;
578     case 0xd58: /* MMFR2.  */
579         return 0x00000000;
580     case 0xd5c: /* MMFR3.  */
581         return 0x00000000;
582     case 0xd60: /* ISAR0.  */
583         return 0x01141110;
584     case 0xd64: /* ISAR1.  */
585         return 0x02111000;
586     case 0xd68: /* ISAR2.  */
587         return 0x21112231;
588     case 0xd6c: /* ISAR3.  */
589         return 0x01111110;
590     case 0xd70: /* ISAR4.  */
591         return 0x01310102;
592     /* TODO: Implement debug registers.  */
593     case 0xd90: /* MPU_TYPE */
594         /* Unified MPU; if the MPU is not present this value is zero */
595         return cpu->pmsav7_dregion << 8;
596         break;
597     case 0xd94: /* MPU_CTRL */
598         return cpu->env.v7m.mpu_ctrl[attrs.secure];
599     case 0xd98: /* MPU_RNR */
600         return cpu->env.pmsav7.rnr[attrs.secure];
601     case 0xd9c: /* MPU_RBAR */
602     case 0xda4: /* MPU_RBAR_A1 */
603     case 0xdac: /* MPU_RBAR_A2 */
604     case 0xdb4: /* MPU_RBAR_A3 */
605     {
606         int region = cpu->env.pmsav7.rnr[attrs.secure];
607 
608         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
609             /* PMSAv8M handling of the aliases is different from v7M:
610              * aliases A1, A2, A3 override the low two bits of the region
611              * number in MPU_RNR, and there is no 'region' field in the
612              * RBAR register.
613              */
614             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
615             if (aliasno) {
616                 region = deposit32(region, 0, 2, aliasno);
617             }
618             if (region >= cpu->pmsav7_dregion) {
619                 return 0;
620             }
621             return cpu->env.pmsav8.rbar[attrs.secure][region];
622         }
623 
624         if (region >= cpu->pmsav7_dregion) {
625             return 0;
626         }
627         return (cpu->env.pmsav7.drbar[region] & 0x1f) | (region & 0xf);
628     }
629     case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
630     case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
631     case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
632     case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
633     {
634         int region = cpu->env.pmsav7.rnr[attrs.secure];
635 
636         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
637             /* PMSAv8M handling of the aliases is different from v7M:
638              * aliases A1, A2, A3 override the low two bits of the region
639              * number in MPU_RNR.
640              */
641             int aliasno = (offset - 0xda0) / 8; /* 0..3 */
642             if (aliasno) {
643                 region = deposit32(region, 0, 2, aliasno);
644             }
645             if (region >= cpu->pmsav7_dregion) {
646                 return 0;
647             }
648             return cpu->env.pmsav8.rlar[attrs.secure][region];
649         }
650 
651         if (region >= cpu->pmsav7_dregion) {
652             return 0;
653         }
654         return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) |
655             (cpu->env.pmsav7.drsr[region] & 0xffff);
656     }
657     case 0xdc0: /* MPU_MAIR0 */
658         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
659             goto bad_offset;
660         }
661         return cpu->env.pmsav8.mair0[attrs.secure];
662     case 0xdc4: /* MPU_MAIR1 */
663         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
664             goto bad_offset;
665         }
666         return cpu->env.pmsav8.mair1[attrs.secure];
667     default:
668     bad_offset:
669         qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
670         return 0;
671     }
672 }
673 
674 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value,
675                         MemTxAttrs attrs)
676 {
677     ARMCPU *cpu = s->cpu;
678 
679     switch (offset) {
680     case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
681     {
682         int startvec = 32 * (offset - 0x380) + NVIC_FIRST_IRQ;
683         int i;
684 
685         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
686             goto bad_offset;
687         }
688         if (!attrs.secure) {
689             break;
690         }
691         for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
692             s->itns[startvec + i] = (value >> i) & 1;
693         }
694         nvic_irq_update(s);
695         break;
696     }
697     case 0xd04: /* Interrupt Control State.  */
698         if (value & (1 << 31)) {
699             armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI);
700         }
701         if (value & (1 << 28)) {
702             armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV);
703         } else if (value & (1 << 27)) {
704             armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV);
705         }
706         if (value & (1 << 26)) {
707             armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
708         } else if (value & (1 << 25)) {
709             armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK);
710         }
711         break;
712     case 0xd08: /* Vector Table Offset.  */
713         cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80;
714         break;
715     case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
716         if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) {
717             if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) {
718                 if (attrs.secure ||
719                     !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) {
720                     qemu_irq_pulse(s->sysresetreq);
721                 }
722             }
723             if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) {
724                 qemu_log_mask(LOG_GUEST_ERROR,
725                               "Setting VECTCLRACTIVE when not in DEBUG mode "
726                               "is UNPREDICTABLE\n");
727             }
728             if (value & R_V7M_AIRCR_VECTRESET_MASK) {
729                 /* NB: this bit is RES0 in v8M */
730                 qemu_log_mask(LOG_GUEST_ERROR,
731                               "Setting VECTRESET when not in DEBUG mode "
732                               "is UNPREDICTABLE\n");
733             }
734             s->prigroup[attrs.secure] = extract32(value,
735                                                   R_V7M_AIRCR_PRIGROUP_SHIFT,
736                                                   R_V7M_AIRCR_PRIGROUP_LENGTH);
737             if (attrs.secure) {
738                 /* These bits are only writable by secure */
739                 cpu->env.v7m.aircr = value &
740                     (R_V7M_AIRCR_SYSRESETREQS_MASK |
741                      R_V7M_AIRCR_BFHFNMINS_MASK |
742                      R_V7M_AIRCR_PRIS_MASK);
743             }
744             nvic_irq_update(s);
745         }
746         break;
747     case 0xd10: /* System Control.  */
748         /* TODO: Implement control registers.  */
749         qemu_log_mask(LOG_UNIMP, "NVIC: SCR unimplemented\n");
750         break;
751     case 0xd14: /* Configuration Control.  */
752         /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
753         value &= (R_V7M_CCR_STKALIGN_MASK |
754                   R_V7M_CCR_BFHFNMIGN_MASK |
755                   R_V7M_CCR_DIV_0_TRP_MASK |
756                   R_V7M_CCR_UNALIGN_TRP_MASK |
757                   R_V7M_CCR_USERSETMPEND_MASK |
758                   R_V7M_CCR_NONBASETHRDENA_MASK);
759 
760         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
761             /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */
762             value |= R_V7M_CCR_NONBASETHRDENA_MASK
763                 | R_V7M_CCR_STKALIGN_MASK;
764         }
765         if (attrs.secure) {
766             /* the BFHFNMIGN bit is not banked; keep that in the NS copy */
767             cpu->env.v7m.ccr[M_REG_NS] =
768                 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK)
769                 | (value & R_V7M_CCR_BFHFNMIGN_MASK);
770             value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
771         }
772 
773         cpu->env.v7m.ccr[attrs.secure] = value;
774         break;
775     case 0xd24: /* System Handler Control.  */
776         s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
777         s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0;
778         s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
779         s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
780         s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0;
781         s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0;
782         s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0;
783         s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0;
784         s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
785         s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0;
786         s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
787         s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
788         s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
789         s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
790         nvic_irq_update(s);
791         break;
792     case 0xd28: /* Configurable Fault Status.  */
793         cpu->env.v7m.cfsr[attrs.secure] &= ~value; /* W1C */
794         if (attrs.secure) {
795             /* The BFSR bits [15:8] are shared between security states
796              * and we store them in the NS copy.
797              */
798             cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK);
799         }
800         break;
801     case 0xd2c: /* Hard Fault Status.  */
802         cpu->env.v7m.hfsr &= ~value; /* W1C */
803         break;
804     case 0xd30: /* Debug Fault Status.  */
805         cpu->env.v7m.dfsr &= ~value; /* W1C */
806         break;
807     case 0xd34: /* Mem Manage Address.  */
808         cpu->env.v7m.mmfar[attrs.secure] = value;
809         return;
810     case 0xd38: /* Bus Fault Address.  */
811         cpu->env.v7m.bfar = value;
812         return;
813     case 0xd3c: /* Aux Fault Status.  */
814         qemu_log_mask(LOG_UNIMP,
815                       "NVIC: Aux fault status registers unimplemented\n");
816         break;
817     case 0xd90: /* MPU_TYPE */
818         return; /* RO */
819     case 0xd94: /* MPU_CTRL */
820         if ((value &
821              (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK))
822             == R_V7M_MPU_CTRL_HFNMIENA_MASK) {
823             qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is "
824                           "UNPREDICTABLE\n");
825         }
826         cpu->env.v7m.mpu_ctrl[attrs.secure]
827             = value & (R_V7M_MPU_CTRL_ENABLE_MASK |
828                        R_V7M_MPU_CTRL_HFNMIENA_MASK |
829                        R_V7M_MPU_CTRL_PRIVDEFENA_MASK);
830         tlb_flush(CPU(cpu));
831         break;
832     case 0xd98: /* MPU_RNR */
833         if (value >= cpu->pmsav7_dregion) {
834             qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %"
835                           PRIu32 "/%" PRIu32 "\n",
836                           value, cpu->pmsav7_dregion);
837         } else {
838             cpu->env.pmsav7.rnr[attrs.secure] = value;
839         }
840         break;
841     case 0xd9c: /* MPU_RBAR */
842     case 0xda4: /* MPU_RBAR_A1 */
843     case 0xdac: /* MPU_RBAR_A2 */
844     case 0xdb4: /* MPU_RBAR_A3 */
845     {
846         int region;
847 
848         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
849             /* PMSAv8M handling of the aliases is different from v7M:
850              * aliases A1, A2, A3 override the low two bits of the region
851              * number in MPU_RNR, and there is no 'region' field in the
852              * RBAR register.
853              */
854             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
855 
856             region = cpu->env.pmsav7.rnr[attrs.secure];
857             if (aliasno) {
858                 region = deposit32(region, 0, 2, aliasno);
859             }
860             if (region >= cpu->pmsav7_dregion) {
861                 return;
862             }
863             cpu->env.pmsav8.rbar[attrs.secure][region] = value;
864             tlb_flush(CPU(cpu));
865             return;
866         }
867 
868         if (value & (1 << 4)) {
869             /* VALID bit means use the region number specified in this
870              * value and also update MPU_RNR.REGION with that value.
871              */
872             region = extract32(value, 0, 4);
873             if (region >= cpu->pmsav7_dregion) {
874                 qemu_log_mask(LOG_GUEST_ERROR,
875                               "MPU region out of range %u/%" PRIu32 "\n",
876                               region, cpu->pmsav7_dregion);
877                 return;
878             }
879             cpu->env.pmsav7.rnr[attrs.secure] = region;
880         } else {
881             region = cpu->env.pmsav7.rnr[attrs.secure];
882         }
883 
884         if (region >= cpu->pmsav7_dregion) {
885             return;
886         }
887 
888         cpu->env.pmsav7.drbar[region] = value & ~0x1f;
889         tlb_flush(CPU(cpu));
890         break;
891     }
892     case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
893     case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
894     case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
895     case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
896     {
897         int region = cpu->env.pmsav7.rnr[attrs.secure];
898 
899         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
900             /* PMSAv8M handling of the aliases is different from v7M:
901              * aliases A1, A2, A3 override the low two bits of the region
902              * number in MPU_RNR.
903              */
904             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
905 
906             region = cpu->env.pmsav7.rnr[attrs.secure];
907             if (aliasno) {
908                 region = deposit32(region, 0, 2, aliasno);
909             }
910             if (region >= cpu->pmsav7_dregion) {
911                 return;
912             }
913             cpu->env.pmsav8.rlar[attrs.secure][region] = value;
914             tlb_flush(CPU(cpu));
915             return;
916         }
917 
918         if (region >= cpu->pmsav7_dregion) {
919             return;
920         }
921 
922         cpu->env.pmsav7.drsr[region] = value & 0xff3f;
923         cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f;
924         tlb_flush(CPU(cpu));
925         break;
926     }
927     case 0xdc0: /* MPU_MAIR0 */
928         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
929             goto bad_offset;
930         }
931         if (cpu->pmsav7_dregion) {
932             /* Register is RES0 if no MPU regions are implemented */
933             cpu->env.pmsav8.mair0[attrs.secure] = value;
934         }
935         /* We don't need to do anything else because memory attributes
936          * only affect cacheability, and we don't implement caching.
937          */
938         break;
939     case 0xdc4: /* MPU_MAIR1 */
940         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
941             goto bad_offset;
942         }
943         if (cpu->pmsav7_dregion) {
944             /* Register is RES0 if no MPU regions are implemented */
945             cpu->env.pmsav8.mair1[attrs.secure] = value;
946         }
947         /* We don't need to do anything else because memory attributes
948          * only affect cacheability, and we don't implement caching.
949          */
950         break;
951     case 0xf00: /* Software Triggered Interrupt Register */
952     {
953         int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ;
954         if (excnum < s->num_irq) {
955             armv7m_nvic_set_pending(s, excnum);
956         }
957         break;
958     }
959     default:
960     bad_offset:
961         qemu_log_mask(LOG_GUEST_ERROR,
962                       "NVIC: Bad write offset 0x%x\n", offset);
963     }
964 }
965 
966 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs)
967 {
968     /* Return true if unprivileged access to this register is permitted. */
969     switch (offset) {
970     case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */
971         /* For access via STIR_NS it is the NS CCR.USERSETMPEND that
972          * controls access even though the CPU is in Secure state (I_QDKX).
973          */
974         return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK;
975     default:
976         /* All other user accesses cause a BusFault unconditionally */
977         return false;
978     }
979 }
980 
981 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr,
982                                     uint64_t *data, unsigned size,
983                                     MemTxAttrs attrs)
984 {
985     NVICState *s = (NVICState *)opaque;
986     uint32_t offset = addr;
987     unsigned i, startvec, end;
988     uint32_t val;
989 
990     if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
991         /* Generate BusFault for unprivileged accesses */
992         return MEMTX_ERROR;
993     }
994 
995     switch (offset) {
996     /* reads of set and clear both return the status */
997     case 0x100 ... 0x13f: /* NVIC Set enable */
998         offset += 0x80;
999         /* fall through */
1000     case 0x180 ... 0x1bf: /* NVIC Clear enable */
1001         val = 0;
1002         startvec = offset - 0x180 + NVIC_FIRST_IRQ; /* vector # */
1003 
1004         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1005             if (s->vectors[startvec + i].enabled &&
1006                 (attrs.secure || s->itns[startvec + i])) {
1007                 val |= (1 << i);
1008             }
1009         }
1010         break;
1011     case 0x200 ... 0x23f: /* NVIC Set pend */
1012         offset += 0x80;
1013         /* fall through */
1014     case 0x280 ... 0x2bf: /* NVIC Clear pend */
1015         val = 0;
1016         startvec = offset - 0x280 + NVIC_FIRST_IRQ; /* vector # */
1017         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1018             if (s->vectors[startvec + i].pending &&
1019                 (attrs.secure || s->itns[startvec + i])) {
1020                 val |= (1 << i);
1021             }
1022         }
1023         break;
1024     case 0x300 ... 0x33f: /* NVIC Active */
1025         val = 0;
1026         startvec = offset - 0x300 + NVIC_FIRST_IRQ; /* vector # */
1027 
1028         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1029             if (s->vectors[startvec + i].active &&
1030                 (attrs.secure || s->itns[startvec + i])) {
1031                 val |= (1 << i);
1032             }
1033         }
1034         break;
1035     case 0x400 ... 0x5ef: /* NVIC Priority */
1036         val = 0;
1037         startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */
1038 
1039         for (i = 0; i < size && startvec + i < s->num_irq; i++) {
1040             if (attrs.secure || s->itns[startvec + i]) {
1041                 val |= s->vectors[startvec + i].prio << (8 * i);
1042             }
1043         }
1044         break;
1045     case 0xd18 ... 0xd23: /* System Handler Priority.  */
1046         val = 0;
1047         for (i = 0; i < size; i++) {
1048             val |= s->vectors[(offset - 0xd14) + i].prio << (i * 8);
1049         }
1050         break;
1051     case 0xfe0 ... 0xfff: /* ID.  */
1052         if (offset & 3) {
1053             val = 0;
1054         } else {
1055             val = nvic_id[(offset - 0xfe0) >> 2];
1056         }
1057         break;
1058     default:
1059         if (size == 4) {
1060             val = nvic_readl(s, offset, attrs);
1061         } else {
1062             qemu_log_mask(LOG_GUEST_ERROR,
1063                           "NVIC: Bad read of size %d at offset 0x%x\n",
1064                           size, offset);
1065             val = 0;
1066         }
1067     }
1068 
1069     trace_nvic_sysreg_read(addr, val, size);
1070     *data = val;
1071     return MEMTX_OK;
1072 }
1073 
1074 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr,
1075                                      uint64_t value, unsigned size,
1076                                      MemTxAttrs attrs)
1077 {
1078     NVICState *s = (NVICState *)opaque;
1079     uint32_t offset = addr;
1080     unsigned i, startvec, end;
1081     unsigned setval = 0;
1082 
1083     trace_nvic_sysreg_write(addr, value, size);
1084 
1085     if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
1086         /* Generate BusFault for unprivileged accesses */
1087         return MEMTX_ERROR;
1088     }
1089 
1090     switch (offset) {
1091     case 0x100 ... 0x13f: /* NVIC Set enable */
1092         offset += 0x80;
1093         setval = 1;
1094         /* fall through */
1095     case 0x180 ... 0x1bf: /* NVIC Clear enable */
1096         startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ;
1097 
1098         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1099             if (value & (1 << i) &&
1100                 (attrs.secure || s->itns[startvec + i])) {
1101                 s->vectors[startvec + i].enabled = setval;
1102             }
1103         }
1104         nvic_irq_update(s);
1105         return MEMTX_OK;
1106     case 0x200 ... 0x23f: /* NVIC Set pend */
1107         /* the special logic in armv7m_nvic_set_pending()
1108          * is not needed since IRQs are never escalated
1109          */
1110         offset += 0x80;
1111         setval = 1;
1112         /* fall through */
1113     case 0x280 ... 0x2bf: /* NVIC Clear pend */
1114         startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
1115 
1116         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1117             if (value & (1 << i) &&
1118                 (attrs.secure || s->itns[startvec + i])) {
1119                 s->vectors[startvec + i].pending = setval;
1120             }
1121         }
1122         nvic_irq_update(s);
1123         return MEMTX_OK;
1124     case 0x300 ... 0x33f: /* NVIC Active */
1125         return MEMTX_OK; /* R/O */
1126     case 0x400 ... 0x5ef: /* NVIC Priority */
1127         startvec = 8 * (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */
1128 
1129         for (i = 0; i < size && startvec + i < s->num_irq; i++) {
1130             if (attrs.secure || s->itns[startvec + i]) {
1131                 set_prio(s, startvec + i, (value >> (i * 8)) & 0xff);
1132             }
1133         }
1134         nvic_irq_update(s);
1135         return MEMTX_OK;
1136     case 0xd18 ... 0xd23: /* System Handler Priority.  */
1137         for (i = 0; i < size; i++) {
1138             unsigned hdlidx = (offset - 0xd14) + i;
1139             set_prio(s, hdlidx, (value >> (i * 8)) & 0xff);
1140         }
1141         nvic_irq_update(s);
1142         return MEMTX_OK;
1143     }
1144     if (size == 4) {
1145         nvic_writel(s, offset, value, attrs);
1146         return MEMTX_OK;
1147     }
1148     qemu_log_mask(LOG_GUEST_ERROR,
1149                   "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
1150     /* This is UNPREDICTABLE; treat as RAZ/WI */
1151     return MEMTX_OK;
1152 }
1153 
1154 static const MemoryRegionOps nvic_sysreg_ops = {
1155     .read_with_attrs = nvic_sysreg_read,
1156     .write_with_attrs = nvic_sysreg_write,
1157     .endianness = DEVICE_NATIVE_ENDIAN,
1158 };
1159 
1160 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr,
1161                                         uint64_t value, unsigned size,
1162                                         MemTxAttrs attrs)
1163 {
1164     if (attrs.secure) {
1165         /* S accesses to the alias act like NS accesses to the real region */
1166         attrs.secure = 0;
1167         return nvic_sysreg_write(opaque, addr, value, size, attrs);
1168     } else {
1169         /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1170         if (attrs.user) {
1171             return MEMTX_ERROR;
1172         }
1173         return MEMTX_OK;
1174     }
1175 }
1176 
1177 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr,
1178                                        uint64_t *data, unsigned size,
1179                                        MemTxAttrs attrs)
1180 {
1181     if (attrs.secure) {
1182         /* S accesses to the alias act like NS accesses to the real region */
1183         attrs.secure = 0;
1184         return nvic_sysreg_read(opaque, addr, data, size, attrs);
1185     } else {
1186         /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1187         if (attrs.user) {
1188             return MEMTX_ERROR;
1189         }
1190         *data = 0;
1191         return MEMTX_OK;
1192     }
1193 }
1194 
1195 static const MemoryRegionOps nvic_sysreg_ns_ops = {
1196     .read_with_attrs = nvic_sysreg_ns_read,
1197     .write_with_attrs = nvic_sysreg_ns_write,
1198     .endianness = DEVICE_NATIVE_ENDIAN,
1199 };
1200 
1201 static int nvic_post_load(void *opaque, int version_id)
1202 {
1203     NVICState *s = opaque;
1204     unsigned i;
1205 
1206     /* Check for out of range priority settings */
1207     if (s->vectors[ARMV7M_EXCP_RESET].prio != -3 ||
1208         s->vectors[ARMV7M_EXCP_NMI].prio != -2 ||
1209         s->vectors[ARMV7M_EXCP_HARD].prio != -1) {
1210         return 1;
1211     }
1212     for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) {
1213         if (s->vectors[i].prio & ~0xff) {
1214             return 1;
1215         }
1216     }
1217 
1218     nvic_recompute_state(s);
1219 
1220     return 0;
1221 }
1222 
1223 static const VMStateDescription vmstate_VecInfo = {
1224     .name = "armv7m_nvic_info",
1225     .version_id = 1,
1226     .minimum_version_id = 1,
1227     .fields = (VMStateField[]) {
1228         VMSTATE_INT16(prio, VecInfo),
1229         VMSTATE_UINT8(enabled, VecInfo),
1230         VMSTATE_UINT8(pending, VecInfo),
1231         VMSTATE_UINT8(active, VecInfo),
1232         VMSTATE_UINT8(level, VecInfo),
1233         VMSTATE_END_OF_LIST()
1234     }
1235 };
1236 
1237 static bool nvic_security_needed(void *opaque)
1238 {
1239     NVICState *s = opaque;
1240 
1241     return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
1242 }
1243 
1244 static int nvic_security_post_load(void *opaque, int version_id)
1245 {
1246     NVICState *s = opaque;
1247     int i;
1248 
1249     /* Check for out of range priority settings */
1250     if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1) {
1251         return 1;
1252     }
1253     for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) {
1254         if (s->sec_vectors[i].prio & ~0xff) {
1255             return 1;
1256         }
1257     }
1258     return 0;
1259 }
1260 
1261 static const VMStateDescription vmstate_nvic_security = {
1262     .name = "nvic/m-security",
1263     .version_id = 1,
1264     .minimum_version_id = 1,
1265     .needed = nvic_security_needed,
1266     .post_load = &nvic_security_post_load,
1267     .fields = (VMStateField[]) {
1268         VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1,
1269                              vmstate_VecInfo, VecInfo),
1270         VMSTATE_UINT32(prigroup[M_REG_S], NVICState),
1271         VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS),
1272         VMSTATE_END_OF_LIST()
1273     }
1274 };
1275 
1276 static const VMStateDescription vmstate_nvic = {
1277     .name = "armv7m_nvic",
1278     .version_id = 4,
1279     .minimum_version_id = 4,
1280     .post_load = &nvic_post_load,
1281     .fields = (VMStateField[]) {
1282         VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1,
1283                              vmstate_VecInfo, VecInfo),
1284         VMSTATE_UINT32(prigroup[M_REG_NS], NVICState),
1285         VMSTATE_END_OF_LIST()
1286     },
1287     .subsections = (const VMStateDescription*[]) {
1288         &vmstate_nvic_security,
1289         NULL
1290     }
1291 };
1292 
1293 static Property props_nvic[] = {
1294     /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
1295     DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64),
1296     DEFINE_PROP_END_OF_LIST()
1297 };
1298 
1299 static void armv7m_nvic_reset(DeviceState *dev)
1300 {
1301     NVICState *s = NVIC(dev);
1302 
1303     s->vectors[ARMV7M_EXCP_NMI].enabled = 1;
1304     s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
1305     /* MEM, BUS, and USAGE are enabled through
1306      * the System Handler Control register
1307      */
1308     s->vectors[ARMV7M_EXCP_SVC].enabled = 1;
1309     s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1;
1310     s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
1311     s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
1312 
1313     s->vectors[ARMV7M_EXCP_RESET].prio = -3;
1314     s->vectors[ARMV7M_EXCP_NMI].prio = -2;
1315     s->vectors[ARMV7M_EXCP_HARD].prio = -1;
1316 
1317     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
1318         s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1;
1319         s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1;
1320         s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
1321         s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
1322 
1323         /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */
1324         s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
1325     }
1326 
1327     /* Strictly speaking the reset handler should be enabled.
1328      * However, we don't simulate soft resets through the NVIC,
1329      * and the reset vector should never be pended.
1330      * So we leave it disabled to catch logic errors.
1331      */
1332 
1333     s->exception_prio = NVIC_NOEXC_PRIO;
1334     s->vectpending = 0;
1335     s->vectpending_is_s_banked = false;
1336     s->vectpending_prio = NVIC_NOEXC_PRIO;
1337 
1338     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
1339         memset(s->itns, 0, sizeof(s->itns));
1340     } else {
1341         /* This state is constant and not guest accessible in a non-security
1342          * NVIC; we set the bits to true to avoid having to do a feature
1343          * bit check in the NVIC enable/pend/etc register accessors.
1344          */
1345         int i;
1346 
1347         for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) {
1348             s->itns[i] = true;
1349         }
1350     }
1351 }
1352 
1353 static void nvic_systick_trigger(void *opaque, int n, int level)
1354 {
1355     NVICState *s = opaque;
1356 
1357     if (level) {
1358         /* SysTick just asked us to pend its exception.
1359          * (This is different from an external interrupt line's
1360          * behaviour.)
1361          */
1362         armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
1363     }
1364 }
1365 
1366 static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
1367 {
1368     NVICState *s = NVIC(dev);
1369     SysBusDevice *systick_sbd;
1370     Error *err = NULL;
1371     int regionlen;
1372 
1373     s->cpu = ARM_CPU(qemu_get_cpu(0));
1374     assert(s->cpu);
1375 
1376     if (s->num_irq > NVIC_MAX_IRQ) {
1377         error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);
1378         return;
1379     }
1380 
1381     qdev_init_gpio_in(dev, set_irq_level, s->num_irq);
1382 
1383     /* include space for internal exception vectors */
1384     s->num_irq += NVIC_FIRST_IRQ;
1385 
1386     object_property_set_bool(OBJECT(&s->systick), true, "realized", &err);
1387     if (err != NULL) {
1388         error_propagate(errp, err);
1389         return;
1390     }
1391     systick_sbd = SYS_BUS_DEVICE(&s->systick);
1392     sysbus_connect_irq(systick_sbd, 0,
1393                        qdev_get_gpio_in_named(dev, "systick-trigger", 0));
1394 
1395     /* The NVIC and System Control Space (SCS) starts at 0xe000e000
1396      * and looks like this:
1397      *  0x004 - ICTR
1398      *  0x010 - 0xff - systick
1399      *  0x100..0x7ec - NVIC
1400      *  0x7f0..0xcff - Reserved
1401      *  0xd00..0xd3c - SCS registers
1402      *  0xd40..0xeff - Reserved or Not implemented
1403      *  0xf00 - STIR
1404      *
1405      * Some registers within this space are banked between security states.
1406      * In v8M there is a second range 0xe002e000..0xe002efff which is the
1407      * NonSecure alias SCS; secure accesses to this behave like NS accesses
1408      * to the main SCS range, and non-secure accesses (including when
1409      * the security extension is not implemented) are RAZ/WI.
1410      * Note that both the main SCS range and the alias range are defined
1411      * to be exempt from memory attribution (R_BLJT) and so the memory
1412      * transaction attribute always matches the current CPU security
1413      * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops
1414      * wrappers we change attrs.secure to indicate the NS access; so
1415      * generally code determining which banked register to use should
1416      * use attrs.secure; code determining actual behaviour of the system
1417      * should use env->v7m.secure.
1418      */
1419     regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000;
1420     memory_region_init(&s->container, OBJECT(s), "nvic", regionlen);
1421     /* The system register region goes at the bottom of the priority
1422      * stack as it covers the whole page.
1423      */
1424     memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
1425                           "nvic_sysregs", 0x1000);
1426     memory_region_add_subregion(&s->container, 0, &s->sysregmem);
1427     memory_region_add_subregion_overlap(&s->container, 0x10,
1428                                         sysbus_mmio_get_region(systick_sbd, 0),
1429                                         1);
1430 
1431     if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
1432         memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
1433                               &nvic_sysreg_ns_ops, s,
1434                               "nvic_sysregs_ns", 0x1000);
1435         memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem);
1436     }
1437 
1438     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
1439 }
1440 
1441 static void armv7m_nvic_instance_init(Object *obj)
1442 {
1443     /* We have a different default value for the num-irq property
1444      * than our superclass. This function runs after qdev init
1445      * has set the defaults from the Property array and before
1446      * any user-specified property setting, so just modify the
1447      * value in the GICState struct.
1448      */
1449     DeviceState *dev = DEVICE(obj);
1450     NVICState *nvic = NVIC(obj);
1451     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1452 
1453     object_initialize(&nvic->systick, sizeof(nvic->systick), TYPE_SYSTICK);
1454     qdev_set_parent_bus(DEVICE(&nvic->systick), sysbus_get_default());
1455 
1456     sysbus_init_irq(sbd, &nvic->excpout);
1457     qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
1458     qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger", 1);
1459 }
1460 
1461 static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
1462 {
1463     DeviceClass *dc = DEVICE_CLASS(klass);
1464 
1465     dc->vmsd  = &vmstate_nvic;
1466     dc->props = props_nvic;
1467     dc->reset = armv7m_nvic_reset;
1468     dc->realize = armv7m_nvic_realize;
1469 }
1470 
1471 static const TypeInfo armv7m_nvic_info = {
1472     .name          = TYPE_NVIC,
1473     .parent        = TYPE_SYS_BUS_DEVICE,
1474     .instance_init = armv7m_nvic_instance_init,
1475     .instance_size = sizeof(NVICState),
1476     .class_init    = armv7m_nvic_class_init,
1477     .class_size    = sizeof(SysBusDeviceClass),
1478 };
1479 
1480 static void armv7m_nvic_register_types(void)
1481 {
1482     type_register_static(&armv7m_nvic_info);
1483 }
1484 
1485 type_init(armv7m_nvic_register_types)
1486