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