tests: virtio-9p: add LOPEN operation test
[qemu/ar7.git] / hw / intc / armv7m_nvic.c
blob8ca6ceeb9b2dd3ec3bd2a1949a04ec0d8c6a8a73
1 /*
2 * ARM Nested Vectored Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "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"
26 /* IRQ number counting:
28 * the num-irq property counts the number of external IRQ lines
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).
34 * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines.
36 * NVIC_MAX_VECTORS is the highest permitted number of exceptions.
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.
41 * The external qemu_irq lines are the NVIC's external IRQ lines,
42 * so line 0 is exception 16.
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.
50 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS
51 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ)
53 /* Effective running priority of the CPU when no exception is active
54 * (higher than the highest possible priority value)
56 #define NVIC_NOEXC_PRIO 0x100
57 /* Maximum priority of non-secure exceptions when AIRCR.PRIS is set */
58 #define NVIC_NS_PRIO_LIMIT 0x80
60 static const uint8_t nvic_id[] = {
61 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
64 static int nvic_pending_prio(NVICState *s)
66 /* return the group priority of the current pending interrupt,
67 * or NVIC_NOEXC_PRIO if no interrupt is pending
69 return s->vectpending_prio;
72 /* Return the value of the ISCR RETTOBASE bit:
73 * 1 if there is exactly one active exception
74 * 0 if there is more than one active exception
75 * UNKNOWN if there are no active exceptions (we choose 1,
76 * which matches the choice Cortex-M3 is documented as making).
78 * NB: some versions of the documentation talk about this
79 * counting "active exceptions other than the one shown by IPSR";
80 * this is only different in the obscure corner case where guest
81 * code has manually deactivated an exception and is about
82 * to fail an exception-return integrity check. The definition
83 * above is the one from the v8M ARM ARM and is also in line
84 * with the behaviour documented for the Cortex-M3.
86 static bool nvic_rettobase(NVICState *s)
88 int irq, nhand = 0;
89 bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
91 for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) {
92 if (s->vectors[irq].active ||
93 (check_sec && irq < NVIC_INTERNAL_VECTORS &&
94 s->sec_vectors[irq].active)) {
95 nhand++;
96 if (nhand == 2) {
97 return 0;
102 return 1;
105 /* Return the value of the ISCR ISRPENDING bit:
106 * 1 if an external interrupt is pending
107 * 0 if no external interrupt is pending
109 static bool nvic_isrpending(NVICState *s)
111 int irq;
113 /* We can shortcut if the highest priority pending interrupt
114 * happens to be external or if there is nothing pending.
116 if (s->vectpending > NVIC_FIRST_IRQ) {
117 return true;
119 if (s->vectpending == 0) {
120 return false;
123 for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) {
124 if (s->vectors[irq].pending) {
125 return true;
128 return false;
131 static bool exc_is_banked(int exc)
133 /* Return true if this is one of the limited set of exceptions which
134 * are banked (and thus have state in sec_vectors[])
136 return exc == ARMV7M_EXCP_HARD ||
137 exc == ARMV7M_EXCP_MEM ||
138 exc == ARMV7M_EXCP_USAGE ||
139 exc == ARMV7M_EXCP_SVC ||
140 exc == ARMV7M_EXCP_PENDSV ||
141 exc == ARMV7M_EXCP_SYSTICK;
144 /* Return a mask word which clears the subpriority bits from
145 * a priority value for an M-profile exception, leaving only
146 * the group priority.
148 static inline uint32_t nvic_gprio_mask(NVICState *s, bool secure)
150 return ~0U << (s->prigroup[secure] + 1);
153 static bool exc_targets_secure(NVICState *s, int exc)
155 /* Return true if this non-banked exception targets Secure state. */
156 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
157 return false;
160 if (exc >= NVIC_FIRST_IRQ) {
161 return !s->itns[exc];
164 /* Function shouldn't be called for banked exceptions. */
165 assert(!exc_is_banked(exc));
167 switch (exc) {
168 case ARMV7M_EXCP_NMI:
169 case ARMV7M_EXCP_BUS:
170 return !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
171 case ARMV7M_EXCP_SECURE:
172 return true;
173 case ARMV7M_EXCP_DEBUG:
174 /* TODO: controlled by DEMCR.SDME, which we don't yet implement */
175 return false;
176 default:
177 /* reset, and reserved (unused) low exception numbers.
178 * We'll get called by code that loops through all the exception
179 * numbers, but it doesn't matter what we return here as these
180 * non-existent exceptions will never be pended or active.
182 return true;
186 static int exc_group_prio(NVICState *s, int rawprio, bool targets_secure)
188 /* Return the group priority for this exception, given its raw
189 * (group-and-subgroup) priority value and whether it is targeting
190 * secure state or not.
192 if (rawprio < 0) {
193 return rawprio;
195 rawprio &= nvic_gprio_mask(s, targets_secure);
196 /* AIRCR.PRIS causes us to squash all NS priorities into the
197 * lower half of the total range
199 if (!targets_secure &&
200 (s->cpu->env.v7m.aircr & R_V7M_AIRCR_PRIS_MASK)) {
201 rawprio = (rawprio >> 1) + NVIC_NS_PRIO_LIMIT;
203 return rawprio;
206 /* Recompute vectpending and exception_prio for a CPU which implements
207 * the Security extension
209 static void nvic_recompute_state_secure(NVICState *s)
211 int i, bank;
212 int pend_prio = NVIC_NOEXC_PRIO;
213 int active_prio = NVIC_NOEXC_PRIO;
214 int pend_irq = 0;
215 bool pending_is_s_banked = false;
217 /* R_CQRV: precedence is by:
218 * - lowest group priority; if both the same then
219 * - lowest subpriority; if both the same then
220 * - lowest exception number; if both the same (ie banked) then
221 * - secure exception takes precedence
222 * Compare pseudocode RawExecutionPriority.
223 * Annoyingly, now we have two prigroup values (for S and NS)
224 * we can't do the loop comparison on raw priority values.
226 for (i = 1; i < s->num_irq; i++) {
227 for (bank = M_REG_S; bank >= M_REG_NS; bank--) {
228 VecInfo *vec;
229 int prio;
230 bool targets_secure;
232 if (bank == M_REG_S) {
233 if (!exc_is_banked(i)) {
234 continue;
236 vec = &s->sec_vectors[i];
237 targets_secure = true;
238 } else {
239 vec = &s->vectors[i];
240 targets_secure = !exc_is_banked(i) && exc_targets_secure(s, i);
243 prio = exc_group_prio(s, vec->prio, targets_secure);
244 if (vec->enabled && vec->pending && prio < pend_prio) {
245 pend_prio = prio;
246 pend_irq = i;
247 pending_is_s_banked = (bank == M_REG_S);
249 if (vec->active && prio < active_prio) {
250 active_prio = prio;
255 s->vectpending_is_s_banked = pending_is_s_banked;
256 s->vectpending = pend_irq;
257 s->vectpending_prio = pend_prio;
258 s->exception_prio = active_prio;
260 trace_nvic_recompute_state_secure(s->vectpending,
261 s->vectpending_is_s_banked,
262 s->vectpending_prio,
263 s->exception_prio);
266 /* Recompute vectpending and exception_prio */
267 static void nvic_recompute_state(NVICState *s)
269 int i;
270 int pend_prio = NVIC_NOEXC_PRIO;
271 int active_prio = NVIC_NOEXC_PRIO;
272 int pend_irq = 0;
274 /* In theory we could write one function that handled both
275 * the "security extension present" and "not present"; however
276 * the security related changes significantly complicate the
277 * recomputation just by themselves and mixing both cases together
278 * would be even worse, so we retain a separate non-secure-only
279 * version for CPUs which don't implement the security extension.
281 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
282 nvic_recompute_state_secure(s);
283 return;
286 for (i = 1; i < s->num_irq; i++) {
287 VecInfo *vec = &s->vectors[i];
289 if (vec->enabled && vec->pending && vec->prio < pend_prio) {
290 pend_prio = vec->prio;
291 pend_irq = i;
293 if (vec->active && vec->prio < active_prio) {
294 active_prio = vec->prio;
298 if (active_prio > 0) {
299 active_prio &= nvic_gprio_mask(s, false);
302 if (pend_prio > 0) {
303 pend_prio &= nvic_gprio_mask(s, false);
306 s->vectpending = pend_irq;
307 s->vectpending_prio = pend_prio;
308 s->exception_prio = active_prio;
310 trace_nvic_recompute_state(s->vectpending,
311 s->vectpending_prio,
312 s->exception_prio);
315 /* Return the current execution priority of the CPU
316 * (equivalent to the pseudocode ExecutionPriority function).
317 * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
319 static inline int nvic_exec_prio(NVICState *s)
321 CPUARMState *env = &s->cpu->env;
322 int running = NVIC_NOEXC_PRIO;
324 if (env->v7m.basepri[M_REG_NS] > 0) {
325 running = exc_group_prio(s, env->v7m.basepri[M_REG_NS], M_REG_NS);
328 if (env->v7m.basepri[M_REG_S] > 0) {
329 int basepri = exc_group_prio(s, env->v7m.basepri[M_REG_S], M_REG_S);
330 if (running > basepri) {
331 running = basepri;
335 if (env->v7m.primask[M_REG_NS]) {
336 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
337 if (running > NVIC_NS_PRIO_LIMIT) {
338 running = NVIC_NS_PRIO_LIMIT;
340 } else {
341 running = 0;
345 if (env->v7m.primask[M_REG_S]) {
346 running = 0;
349 if (env->v7m.faultmask[M_REG_NS]) {
350 if (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
351 running = -1;
352 } else {
353 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
354 if (running > NVIC_NS_PRIO_LIMIT) {
355 running = NVIC_NS_PRIO_LIMIT;
357 } else {
358 running = 0;
363 if (env->v7m.faultmask[M_REG_S]) {
364 running = (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) ? -3 : -1;
367 /* consider priority of active handler */
368 return MIN(running, s->exception_prio);
371 bool armv7m_nvic_neg_prio_requested(void *opaque, bool secure)
373 /* Return true if the requested execution priority is negative
374 * for the specified security state, ie that security state
375 * has an active NMI or HardFault or has set its FAULTMASK.
376 * Note that this is not the same as whether the execution
377 * priority is actually negative (for instance AIRCR.PRIS may
378 * mean we don't allow FAULTMASK_NS to actually make the execution
379 * priority negative). Compare pseudocode IsReqExcPriNeg().
381 NVICState *s = opaque;
383 if (s->cpu->env.v7m.faultmask[secure]) {
384 return true;
387 if (secure ? s->sec_vectors[ARMV7M_EXCP_HARD].active :
388 s->vectors[ARMV7M_EXCP_HARD].active) {
389 return true;
392 if (s->vectors[ARMV7M_EXCP_NMI].active &&
393 exc_targets_secure(s, ARMV7M_EXCP_NMI) == secure) {
394 return true;
397 return false;
400 bool armv7m_nvic_can_take_pending_exception(void *opaque)
402 NVICState *s = opaque;
404 return nvic_exec_prio(s) > nvic_pending_prio(s);
407 int armv7m_nvic_raw_execution_priority(void *opaque)
409 NVICState *s = opaque;
411 return s->exception_prio;
414 /* caller must call nvic_irq_update() after this.
415 * secure indicates the bank to use for banked exceptions (we assert if
416 * we are passed secure=true for a non-banked exception).
418 static void set_prio(NVICState *s, unsigned irq, bool secure, uint8_t prio)
420 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
421 assert(irq < s->num_irq);
423 if (secure) {
424 assert(exc_is_banked(irq));
425 s->sec_vectors[irq].prio = prio;
426 } else {
427 s->vectors[irq].prio = prio;
430 trace_nvic_set_prio(irq, secure, prio);
433 /* Return the current raw priority register value.
434 * secure indicates the bank to use for banked exceptions (we assert if
435 * we are passed secure=true for a non-banked exception).
437 static int get_prio(NVICState *s, unsigned irq, bool secure)
439 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
440 assert(irq < s->num_irq);
442 if (secure) {
443 assert(exc_is_banked(irq));
444 return s->sec_vectors[irq].prio;
445 } else {
446 return s->vectors[irq].prio;
450 /* Recompute state and assert irq line accordingly.
451 * Must be called after changes to:
452 * vec->active, vec->enabled, vec->pending or vec->prio for any vector
453 * prigroup
455 static void nvic_irq_update(NVICState *s)
457 int lvl;
458 int pend_prio;
460 nvic_recompute_state(s);
461 pend_prio = nvic_pending_prio(s);
463 /* Raise NVIC output if this IRQ would be taken, except that we
464 * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
465 * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
466 * to those CPU registers don't cause us to recalculate the NVIC
467 * pending info.
469 lvl = (pend_prio < s->exception_prio);
470 trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl);
471 qemu_set_irq(s->excpout, lvl);
475 * armv7m_nvic_clear_pending: mark the specified exception as not pending
476 * @opaque: the NVIC
477 * @irq: the exception number to mark as not pending
478 * @secure: false for non-banked exceptions or for the nonsecure
479 * version of a banked exception, true for the secure version of a banked
480 * exception.
482 * Marks the specified exception as not pending. Note that we will assert()
483 * if @secure is true and @irq does not specify one of the fixed set
484 * of architecturally banked exceptions.
486 static void armv7m_nvic_clear_pending(void *opaque, int irq, bool secure)
488 NVICState *s = (NVICState *)opaque;
489 VecInfo *vec;
491 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
493 if (secure) {
494 assert(exc_is_banked(irq));
495 vec = &s->sec_vectors[irq];
496 } else {
497 vec = &s->vectors[irq];
499 trace_nvic_clear_pending(irq, secure, vec->enabled, vec->prio);
500 if (vec->pending) {
501 vec->pending = 0;
502 nvic_irq_update(s);
506 void armv7m_nvic_set_pending(void *opaque, int irq, bool secure)
508 NVICState *s = (NVICState *)opaque;
509 bool banked = exc_is_banked(irq);
510 VecInfo *vec;
512 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
513 assert(!secure || banked);
515 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
517 trace_nvic_set_pending(irq, secure, vec->enabled, vec->prio);
519 if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) {
520 /* If a synchronous exception is pending then it may be
521 * escalated to HardFault if:
522 * * it is equal or lower priority to current execution
523 * * it is disabled
524 * (ie we need to take it immediately but we can't do so).
525 * Asynchronous exceptions (and interrupts) simply remain pending.
527 * For QEMU, we don't have any imprecise (asynchronous) faults,
528 * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
529 * synchronous.
530 * Debug exceptions are awkward because only Debug exceptions
531 * resulting from the BKPT instruction should be escalated,
532 * but we don't currently implement any Debug exceptions other
533 * than those that result from BKPT, so we treat all debug exceptions
534 * as needing escalation.
536 * This all means we can identify whether to escalate based only on
537 * the exception number and don't (yet) need the caller to explicitly
538 * tell us whether this exception is synchronous or not.
540 int running = nvic_exec_prio(s);
541 bool escalate = false;
543 if (exc_group_prio(s, vec->prio, secure) >= running) {
544 trace_nvic_escalate_prio(irq, vec->prio, running);
545 escalate = true;
546 } else if (!vec->enabled) {
547 trace_nvic_escalate_disabled(irq);
548 escalate = true;
551 if (escalate) {
553 /* We need to escalate this exception to a synchronous HardFault.
554 * If BFHFNMINS is set then we escalate to the banked HF for
555 * the target security state of the original exception; otherwise
556 * we take a Secure HardFault.
558 irq = ARMV7M_EXCP_HARD;
559 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
560 (secure ||
561 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) {
562 vec = &s->sec_vectors[irq];
563 } else {
564 vec = &s->vectors[irq];
566 if (running <= vec->prio) {
567 /* We want to escalate to HardFault but we can't take the
568 * synchronous HardFault at this point either. This is a
569 * Lockup condition due to a guest bug. We don't model
570 * Lockup, so report via cpu_abort() instead.
572 cpu_abort(&s->cpu->parent_obj,
573 "Lockup: can't escalate %d to HardFault "
574 "(current priority %d)\n", irq, running);
577 /* HF may be banked but there is only one shared HFSR */
578 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
582 if (!vec->pending) {
583 vec->pending = 1;
584 nvic_irq_update(s);
588 /* Make pending IRQ active. */
589 bool armv7m_nvic_acknowledge_irq(void *opaque)
591 NVICState *s = (NVICState *)opaque;
592 CPUARMState *env = &s->cpu->env;
593 const int pending = s->vectpending;
594 const int running = nvic_exec_prio(s);
595 VecInfo *vec;
596 bool targets_secure;
598 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
600 if (s->vectpending_is_s_banked) {
601 vec = &s->sec_vectors[pending];
602 targets_secure = true;
603 } else {
604 vec = &s->vectors[pending];
605 targets_secure = !exc_is_banked(s->vectpending) &&
606 exc_targets_secure(s, s->vectpending);
609 assert(vec->enabled);
610 assert(vec->pending);
612 assert(s->vectpending_prio < running);
614 trace_nvic_acknowledge_irq(pending, s->vectpending_prio, targets_secure);
616 vec->active = 1;
617 vec->pending = 0;
619 write_v7m_exception(env, s->vectpending);
621 nvic_irq_update(s);
623 return targets_secure;
626 int armv7m_nvic_complete_irq(void *opaque, int irq, bool secure)
628 NVICState *s = (NVICState *)opaque;
629 VecInfo *vec;
630 int ret;
632 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
634 if (secure && exc_is_banked(irq)) {
635 vec = &s->sec_vectors[irq];
636 } else {
637 vec = &s->vectors[irq];
640 trace_nvic_complete_irq(irq, secure);
642 if (!vec->active) {
643 /* Tell the caller this was an illegal exception return */
644 return -1;
647 ret = nvic_rettobase(s);
649 vec->active = 0;
650 if (vec->level) {
651 /* Re-pend the exception if it's still held high; only
652 * happens for extenal IRQs
654 assert(irq >= NVIC_FIRST_IRQ);
655 vec->pending = 1;
658 nvic_irq_update(s);
660 return ret;
663 /* callback when external interrupt line is changed */
664 static void set_irq_level(void *opaque, int n, int level)
666 NVICState *s = opaque;
667 VecInfo *vec;
669 n += NVIC_FIRST_IRQ;
671 assert(n >= NVIC_FIRST_IRQ && n < s->num_irq);
673 trace_nvic_set_irq_level(n, level);
675 /* The pending status of an external interrupt is
676 * latched on rising edge and exception handler return.
678 * Pulsing the IRQ will always run the handler
679 * once, and the handler will re-run until the
680 * level is low when the handler completes.
682 vec = &s->vectors[n];
683 if (level != vec->level) {
684 vec->level = level;
685 if (level) {
686 armv7m_nvic_set_pending(s, n, false);
691 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
693 ARMCPU *cpu = s->cpu;
694 uint32_t val;
696 switch (offset) {
697 case 4: /* Interrupt Control Type. */
698 return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1;
699 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
701 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
702 int i;
704 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
705 goto bad_offset;
707 if (!attrs.secure) {
708 return 0;
710 val = 0;
711 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
712 if (s->itns[startvec + i]) {
713 val |= (1 << i);
716 return val;
718 case 0xd00: /* CPUID Base. */
719 return cpu->midr;
720 case 0xd04: /* Interrupt Control State (ICSR) */
721 /* VECTACTIVE */
722 val = cpu->env.v7m.exception;
723 /* VECTPENDING */
724 val |= (s->vectpending & 0xff) << 12;
725 /* ISRPENDING - set if any external IRQ is pending */
726 if (nvic_isrpending(s)) {
727 val |= (1 << 22);
729 /* RETTOBASE - set if only one handler is active */
730 if (nvic_rettobase(s)) {
731 val |= (1 << 11);
733 if (attrs.secure) {
734 /* PENDSTSET */
735 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) {
736 val |= (1 << 26);
738 /* PENDSVSET */
739 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) {
740 val |= (1 << 28);
742 } else {
743 /* PENDSTSET */
744 if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) {
745 val |= (1 << 26);
747 /* PENDSVSET */
748 if (s->vectors[ARMV7M_EXCP_PENDSV].pending) {
749 val |= (1 << 28);
752 /* NMIPENDSET */
753 if ((cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
754 s->vectors[ARMV7M_EXCP_NMI].pending) {
755 val |= (1 << 31);
757 /* ISRPREEMPT: RES0 when halting debug not implemented */
758 /* STTNS: RES0 for the Main Extension */
759 return val;
760 case 0xd08: /* Vector Table Offset. */
761 return cpu->env.v7m.vecbase[attrs.secure];
762 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
763 val = 0xfa050000 | (s->prigroup[attrs.secure] << 8);
764 if (attrs.secure) {
765 /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */
766 val |= cpu->env.v7m.aircr;
767 } else {
768 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
769 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If
770 * security isn't supported then BFHFNMINS is RAO (and
771 * the bit in env.v7m.aircr is always set).
773 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK;
776 return val;
777 case 0xd10: /* System Control. */
778 /* TODO: Implement SLEEPONEXIT. */
779 return 0;
780 case 0xd14: /* Configuration Control. */
781 /* The BFHFNMIGN bit is the only non-banked bit; we
782 * keep it in the non-secure copy of the register.
784 val = cpu->env.v7m.ccr[attrs.secure];
785 val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
786 return val;
787 case 0xd24: /* System Handler Control and State (SHCSR) */
788 val = 0;
789 if (attrs.secure) {
790 if (s->sec_vectors[ARMV7M_EXCP_MEM].active) {
791 val |= (1 << 0);
793 if (s->sec_vectors[ARMV7M_EXCP_HARD].active) {
794 val |= (1 << 2);
796 if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) {
797 val |= (1 << 3);
799 if (s->sec_vectors[ARMV7M_EXCP_SVC].active) {
800 val |= (1 << 7);
802 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) {
803 val |= (1 << 10);
805 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) {
806 val |= (1 << 11);
808 if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) {
809 val |= (1 << 12);
811 if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) {
812 val |= (1 << 13);
814 if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) {
815 val |= (1 << 15);
817 if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) {
818 val |= (1 << 16);
820 if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) {
821 val |= (1 << 18);
823 if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) {
824 val |= (1 << 21);
826 /* SecureFault is not banked but is always RAZ/WI to NS */
827 if (s->vectors[ARMV7M_EXCP_SECURE].active) {
828 val |= (1 << 4);
830 if (s->vectors[ARMV7M_EXCP_SECURE].enabled) {
831 val |= (1 << 19);
833 if (s->vectors[ARMV7M_EXCP_SECURE].pending) {
834 val |= (1 << 20);
836 } else {
837 if (s->vectors[ARMV7M_EXCP_MEM].active) {
838 val |= (1 << 0);
840 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
841 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */
842 if (s->vectors[ARMV7M_EXCP_HARD].active) {
843 val |= (1 << 2);
845 if (s->vectors[ARMV7M_EXCP_HARD].pending) {
846 val |= (1 << 21);
849 if (s->vectors[ARMV7M_EXCP_USAGE].active) {
850 val |= (1 << 3);
852 if (s->vectors[ARMV7M_EXCP_SVC].active) {
853 val |= (1 << 7);
855 if (s->vectors[ARMV7M_EXCP_PENDSV].active) {
856 val |= (1 << 10);
858 if (s->vectors[ARMV7M_EXCP_SYSTICK].active) {
859 val |= (1 << 11);
861 if (s->vectors[ARMV7M_EXCP_USAGE].pending) {
862 val |= (1 << 12);
864 if (s->vectors[ARMV7M_EXCP_MEM].pending) {
865 val |= (1 << 13);
867 if (s->vectors[ARMV7M_EXCP_SVC].pending) {
868 val |= (1 << 15);
870 if (s->vectors[ARMV7M_EXCP_MEM].enabled) {
871 val |= (1 << 16);
873 if (s->vectors[ARMV7M_EXCP_USAGE].enabled) {
874 val |= (1 << 18);
877 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
878 if (s->vectors[ARMV7M_EXCP_BUS].active) {
879 val |= (1 << 1);
881 if (s->vectors[ARMV7M_EXCP_BUS].pending) {
882 val |= (1 << 14);
884 if (s->vectors[ARMV7M_EXCP_BUS].enabled) {
885 val |= (1 << 17);
887 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
888 s->vectors[ARMV7M_EXCP_NMI].active) {
889 /* NMIACT is not present in v7M */
890 val |= (1 << 5);
894 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
895 if (s->vectors[ARMV7M_EXCP_DEBUG].active) {
896 val |= (1 << 8);
898 return val;
899 case 0xd2c: /* Hard Fault Status. */
900 return cpu->env.v7m.hfsr;
901 case 0xd30: /* Debug Fault Status. */
902 return cpu->env.v7m.dfsr;
903 case 0xd34: /* MMFAR MemManage Fault Address */
904 return cpu->env.v7m.mmfar[attrs.secure];
905 case 0xd38: /* Bus Fault Address. */
906 return cpu->env.v7m.bfar;
907 case 0xd3c: /* Aux Fault Status. */
908 /* TODO: Implement fault status registers. */
909 qemu_log_mask(LOG_UNIMP,
910 "Aux Fault status registers unimplemented\n");
911 return 0;
912 case 0xd40: /* PFR0. */
913 return 0x00000030;
914 case 0xd44: /* PRF1. */
915 return 0x00000200;
916 case 0xd48: /* DFR0. */
917 return 0x00100000;
918 case 0xd4c: /* AFR0. */
919 return 0x00000000;
920 case 0xd50: /* MMFR0. */
921 return 0x00000030;
922 case 0xd54: /* MMFR1. */
923 return 0x00000000;
924 case 0xd58: /* MMFR2. */
925 return 0x00000000;
926 case 0xd5c: /* MMFR3. */
927 return 0x00000000;
928 case 0xd60: /* ISAR0. */
929 return 0x01141110;
930 case 0xd64: /* ISAR1. */
931 return 0x02111000;
932 case 0xd68: /* ISAR2. */
933 return 0x21112231;
934 case 0xd6c: /* ISAR3. */
935 return 0x01111110;
936 case 0xd70: /* ISAR4. */
937 return 0x01310102;
938 /* TODO: Implement debug registers. */
939 case 0xd90: /* MPU_TYPE */
940 /* Unified MPU; if the MPU is not present this value is zero */
941 return cpu->pmsav7_dregion << 8;
942 break;
943 case 0xd94: /* MPU_CTRL */
944 return cpu->env.v7m.mpu_ctrl[attrs.secure];
945 case 0xd98: /* MPU_RNR */
946 return cpu->env.pmsav7.rnr[attrs.secure];
947 case 0xd9c: /* MPU_RBAR */
948 case 0xda4: /* MPU_RBAR_A1 */
949 case 0xdac: /* MPU_RBAR_A2 */
950 case 0xdb4: /* MPU_RBAR_A3 */
952 int region = cpu->env.pmsav7.rnr[attrs.secure];
954 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
955 /* PMSAv8M handling of the aliases is different from v7M:
956 * aliases A1, A2, A3 override the low two bits of the region
957 * number in MPU_RNR, and there is no 'region' field in the
958 * RBAR register.
960 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
961 if (aliasno) {
962 region = deposit32(region, 0, 2, aliasno);
964 if (region >= cpu->pmsav7_dregion) {
965 return 0;
967 return cpu->env.pmsav8.rbar[attrs.secure][region];
970 if (region >= cpu->pmsav7_dregion) {
971 return 0;
973 return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf);
975 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
976 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
977 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
978 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
980 int region = cpu->env.pmsav7.rnr[attrs.secure];
982 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
983 /* PMSAv8M handling of the aliases is different from v7M:
984 * aliases A1, A2, A3 override the low two bits of the region
985 * number in MPU_RNR.
987 int aliasno = (offset - 0xda0) / 8; /* 0..3 */
988 if (aliasno) {
989 region = deposit32(region, 0, 2, aliasno);
991 if (region >= cpu->pmsav7_dregion) {
992 return 0;
994 return cpu->env.pmsav8.rlar[attrs.secure][region];
997 if (region >= cpu->pmsav7_dregion) {
998 return 0;
1000 return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) |
1001 (cpu->env.pmsav7.drsr[region] & 0xffff);
1003 case 0xdc0: /* MPU_MAIR0 */
1004 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1005 goto bad_offset;
1007 return cpu->env.pmsav8.mair0[attrs.secure];
1008 case 0xdc4: /* MPU_MAIR1 */
1009 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1010 goto bad_offset;
1012 return cpu->env.pmsav8.mair1[attrs.secure];
1013 case 0xdd0: /* SAU_CTRL */
1014 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1015 goto bad_offset;
1017 if (!attrs.secure) {
1018 return 0;
1020 return cpu->env.sau.ctrl;
1021 case 0xdd4: /* SAU_TYPE */
1022 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1023 goto bad_offset;
1025 if (!attrs.secure) {
1026 return 0;
1028 return cpu->sau_sregion;
1029 case 0xdd8: /* SAU_RNR */
1030 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1031 goto bad_offset;
1033 if (!attrs.secure) {
1034 return 0;
1036 return cpu->env.sau.rnr;
1037 case 0xddc: /* SAU_RBAR */
1039 int region = cpu->env.sau.rnr;
1041 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1042 goto bad_offset;
1044 if (!attrs.secure) {
1045 return 0;
1047 if (region >= cpu->sau_sregion) {
1048 return 0;
1050 return cpu->env.sau.rbar[region];
1052 case 0xde0: /* SAU_RLAR */
1054 int region = cpu->env.sau.rnr;
1056 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1057 goto bad_offset;
1059 if (!attrs.secure) {
1060 return 0;
1062 if (region >= cpu->sau_sregion) {
1063 return 0;
1065 return cpu->env.sau.rlar[region];
1067 case 0xde4: /* SFSR */
1068 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1069 goto bad_offset;
1071 if (!attrs.secure) {
1072 return 0;
1074 return cpu->env.v7m.sfsr;
1075 case 0xde8: /* SFAR */
1076 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1077 goto bad_offset;
1079 if (!attrs.secure) {
1080 return 0;
1082 return cpu->env.v7m.sfar;
1083 default:
1084 bad_offset:
1085 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
1086 return 0;
1090 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value,
1091 MemTxAttrs attrs)
1093 ARMCPU *cpu = s->cpu;
1095 switch (offset) {
1096 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
1098 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
1099 int i;
1101 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1102 goto bad_offset;
1104 if (!attrs.secure) {
1105 break;
1107 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
1108 s->itns[startvec + i] = (value >> i) & 1;
1110 nvic_irq_update(s);
1111 break;
1113 case 0xd04: /* Interrupt Control State (ICSR) */
1114 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1115 if (value & (1 << 31)) {
1116 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false);
1117 } else if (value & (1 << 30) &&
1118 arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1119 /* PENDNMICLR didn't exist in v7M */
1120 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false);
1123 if (value & (1 << 28)) {
1124 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
1125 } else if (value & (1 << 27)) {
1126 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
1128 if (value & (1 << 26)) {
1129 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
1130 } else if (value & (1 << 25)) {
1131 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
1133 break;
1134 case 0xd08: /* Vector Table Offset. */
1135 cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80;
1136 break;
1137 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
1138 if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) {
1139 if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) {
1140 if (attrs.secure ||
1141 !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) {
1142 qemu_irq_pulse(s->sysresetreq);
1145 if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) {
1146 qemu_log_mask(LOG_GUEST_ERROR,
1147 "Setting VECTCLRACTIVE when not in DEBUG mode "
1148 "is UNPREDICTABLE\n");
1150 if (value & R_V7M_AIRCR_VECTRESET_MASK) {
1151 /* NB: this bit is RES0 in v8M */
1152 qemu_log_mask(LOG_GUEST_ERROR,
1153 "Setting VECTRESET when not in DEBUG mode "
1154 "is UNPREDICTABLE\n");
1156 s->prigroup[attrs.secure] = extract32(value,
1157 R_V7M_AIRCR_PRIGROUP_SHIFT,
1158 R_V7M_AIRCR_PRIGROUP_LENGTH);
1159 if (attrs.secure) {
1160 /* These bits are only writable by secure */
1161 cpu->env.v7m.aircr = value &
1162 (R_V7M_AIRCR_SYSRESETREQS_MASK |
1163 R_V7M_AIRCR_BFHFNMINS_MASK |
1164 R_V7M_AIRCR_PRIS_MASK);
1165 /* BFHFNMINS changes the priority of Secure HardFault, and
1166 * allows a pending Non-secure HardFault to preempt (which
1167 * we implement by marking it enabled).
1169 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1170 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3;
1171 s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
1172 } else {
1173 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
1174 s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
1177 nvic_irq_update(s);
1179 break;
1180 case 0xd10: /* System Control. */
1181 /* TODO: Implement control registers. */
1182 qemu_log_mask(LOG_UNIMP, "NVIC: SCR unimplemented\n");
1183 break;
1184 case 0xd14: /* Configuration Control. */
1185 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
1186 value &= (R_V7M_CCR_STKALIGN_MASK |
1187 R_V7M_CCR_BFHFNMIGN_MASK |
1188 R_V7M_CCR_DIV_0_TRP_MASK |
1189 R_V7M_CCR_UNALIGN_TRP_MASK |
1190 R_V7M_CCR_USERSETMPEND_MASK |
1191 R_V7M_CCR_NONBASETHRDENA_MASK);
1193 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1194 /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */
1195 value |= R_V7M_CCR_NONBASETHRDENA_MASK
1196 | R_V7M_CCR_STKALIGN_MASK;
1198 if (attrs.secure) {
1199 /* the BFHFNMIGN bit is not banked; keep that in the NS copy */
1200 cpu->env.v7m.ccr[M_REG_NS] =
1201 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK)
1202 | (value & R_V7M_CCR_BFHFNMIGN_MASK);
1203 value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
1206 cpu->env.v7m.ccr[attrs.secure] = value;
1207 break;
1208 case 0xd24: /* System Handler Control and State (SHCSR) */
1209 if (attrs.secure) {
1210 s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1211 /* Secure HardFault active bit cannot be written */
1212 s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1213 s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1214 s->sec_vectors[ARMV7M_EXCP_PENDSV].active =
1215 (value & (1 << 10)) != 0;
1216 s->sec_vectors[ARMV7M_EXCP_SYSTICK].active =
1217 (value & (1 << 11)) != 0;
1218 s->sec_vectors[ARMV7M_EXCP_USAGE].pending =
1219 (value & (1 << 12)) != 0;
1220 s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1221 s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1222 s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1223 s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1224 s->sec_vectors[ARMV7M_EXCP_USAGE].enabled =
1225 (value & (1 << 18)) != 0;
1226 s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1227 /* SecureFault not banked, but RAZ/WI to NS */
1228 s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0;
1229 s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0;
1230 s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0;
1231 } else {
1232 s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1233 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1234 /* HARDFAULTPENDED is not present in v7M */
1235 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1237 s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1238 s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1239 s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0;
1240 s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0;
1241 s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0;
1242 s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1243 s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1244 s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1245 s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
1247 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1248 s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0;
1249 s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0;
1250 s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1252 /* NMIACT can only be written if the write is of a zero, with
1253 * BFHFNMINS 1, and by the CPU in secure state via the NS alias.
1255 if (!attrs.secure && cpu->env.v7m.secure &&
1256 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1257 (value & (1 << 5)) == 0) {
1258 s->vectors[ARMV7M_EXCP_NMI].active = 0;
1260 /* HARDFAULTACT can only be written if the write is of a zero
1261 * to the non-secure HardFault state by the CPU in secure state.
1262 * The only case where we can be targeting the non-secure HF state
1263 * when in secure state is if this is a write via the NS alias
1264 * and BFHFNMINS is 1.
1266 if (!attrs.secure && cpu->env.v7m.secure &&
1267 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1268 (value & (1 << 2)) == 0) {
1269 s->vectors[ARMV7M_EXCP_HARD].active = 0;
1272 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
1273 s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0;
1274 nvic_irq_update(s);
1275 break;
1276 case 0xd2c: /* Hard Fault Status. */
1277 cpu->env.v7m.hfsr &= ~value; /* W1C */
1278 break;
1279 case 0xd30: /* Debug Fault Status. */
1280 cpu->env.v7m.dfsr &= ~value; /* W1C */
1281 break;
1282 case 0xd34: /* Mem Manage Address. */
1283 cpu->env.v7m.mmfar[attrs.secure] = value;
1284 return;
1285 case 0xd38: /* Bus Fault Address. */
1286 cpu->env.v7m.bfar = value;
1287 return;
1288 case 0xd3c: /* Aux Fault Status. */
1289 qemu_log_mask(LOG_UNIMP,
1290 "NVIC: Aux fault status registers unimplemented\n");
1291 break;
1292 case 0xd90: /* MPU_TYPE */
1293 return; /* RO */
1294 case 0xd94: /* MPU_CTRL */
1295 if ((value &
1296 (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK))
1297 == R_V7M_MPU_CTRL_HFNMIENA_MASK) {
1298 qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is "
1299 "UNPREDICTABLE\n");
1301 cpu->env.v7m.mpu_ctrl[attrs.secure]
1302 = value & (R_V7M_MPU_CTRL_ENABLE_MASK |
1303 R_V7M_MPU_CTRL_HFNMIENA_MASK |
1304 R_V7M_MPU_CTRL_PRIVDEFENA_MASK);
1305 tlb_flush(CPU(cpu));
1306 break;
1307 case 0xd98: /* MPU_RNR */
1308 if (value >= cpu->pmsav7_dregion) {
1309 qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %"
1310 PRIu32 "/%" PRIu32 "\n",
1311 value, cpu->pmsav7_dregion);
1312 } else {
1313 cpu->env.pmsav7.rnr[attrs.secure] = value;
1315 break;
1316 case 0xd9c: /* MPU_RBAR */
1317 case 0xda4: /* MPU_RBAR_A1 */
1318 case 0xdac: /* MPU_RBAR_A2 */
1319 case 0xdb4: /* MPU_RBAR_A3 */
1321 int region;
1323 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1324 /* PMSAv8M handling of the aliases is different from v7M:
1325 * aliases A1, A2, A3 override the low two bits of the region
1326 * number in MPU_RNR, and there is no 'region' field in the
1327 * RBAR register.
1329 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1331 region = cpu->env.pmsav7.rnr[attrs.secure];
1332 if (aliasno) {
1333 region = deposit32(region, 0, 2, aliasno);
1335 if (region >= cpu->pmsav7_dregion) {
1336 return;
1338 cpu->env.pmsav8.rbar[attrs.secure][region] = value;
1339 tlb_flush(CPU(cpu));
1340 return;
1343 if (value & (1 << 4)) {
1344 /* VALID bit means use the region number specified in this
1345 * value and also update MPU_RNR.REGION with that value.
1347 region = extract32(value, 0, 4);
1348 if (region >= cpu->pmsav7_dregion) {
1349 qemu_log_mask(LOG_GUEST_ERROR,
1350 "MPU region out of range %u/%" PRIu32 "\n",
1351 region, cpu->pmsav7_dregion);
1352 return;
1354 cpu->env.pmsav7.rnr[attrs.secure] = region;
1355 } else {
1356 region = cpu->env.pmsav7.rnr[attrs.secure];
1359 if (region >= cpu->pmsav7_dregion) {
1360 return;
1363 cpu->env.pmsav7.drbar[region] = value & ~0x1f;
1364 tlb_flush(CPU(cpu));
1365 break;
1367 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1368 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1369 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1370 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
1372 int region = cpu->env.pmsav7.rnr[attrs.secure];
1374 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1375 /* PMSAv8M handling of the aliases is different from v7M:
1376 * aliases A1, A2, A3 override the low two bits of the region
1377 * number in MPU_RNR.
1379 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1381 region = cpu->env.pmsav7.rnr[attrs.secure];
1382 if (aliasno) {
1383 region = deposit32(region, 0, 2, aliasno);
1385 if (region >= cpu->pmsav7_dregion) {
1386 return;
1388 cpu->env.pmsav8.rlar[attrs.secure][region] = value;
1389 tlb_flush(CPU(cpu));
1390 return;
1393 if (region >= cpu->pmsav7_dregion) {
1394 return;
1397 cpu->env.pmsav7.drsr[region] = value & 0xff3f;
1398 cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f;
1399 tlb_flush(CPU(cpu));
1400 break;
1402 case 0xdc0: /* MPU_MAIR0 */
1403 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1404 goto bad_offset;
1406 if (cpu->pmsav7_dregion) {
1407 /* Register is RES0 if no MPU regions are implemented */
1408 cpu->env.pmsav8.mair0[attrs.secure] = value;
1410 /* We don't need to do anything else because memory attributes
1411 * only affect cacheability, and we don't implement caching.
1413 break;
1414 case 0xdc4: /* MPU_MAIR1 */
1415 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1416 goto bad_offset;
1418 if (cpu->pmsav7_dregion) {
1419 /* Register is RES0 if no MPU regions are implemented */
1420 cpu->env.pmsav8.mair1[attrs.secure] = value;
1422 /* We don't need to do anything else because memory attributes
1423 * only affect cacheability, and we don't implement caching.
1425 break;
1426 case 0xdd0: /* SAU_CTRL */
1427 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1428 goto bad_offset;
1430 if (!attrs.secure) {
1431 return;
1433 cpu->env.sau.ctrl = value & 3;
1434 break;
1435 case 0xdd4: /* SAU_TYPE */
1436 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1437 goto bad_offset;
1439 break;
1440 case 0xdd8: /* SAU_RNR */
1441 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1442 goto bad_offset;
1444 if (!attrs.secure) {
1445 return;
1447 if (value >= cpu->sau_sregion) {
1448 qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %"
1449 PRIu32 "/%" PRIu32 "\n",
1450 value, cpu->sau_sregion);
1451 } else {
1452 cpu->env.sau.rnr = value;
1454 break;
1455 case 0xddc: /* SAU_RBAR */
1457 int region = cpu->env.sau.rnr;
1459 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1460 goto bad_offset;
1462 if (!attrs.secure) {
1463 return;
1465 if (region >= cpu->sau_sregion) {
1466 return;
1468 cpu->env.sau.rbar[region] = value & ~0x1f;
1469 tlb_flush(CPU(cpu));
1470 break;
1472 case 0xde0: /* SAU_RLAR */
1474 int region = cpu->env.sau.rnr;
1476 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1477 goto bad_offset;
1479 if (!attrs.secure) {
1480 return;
1482 if (region >= cpu->sau_sregion) {
1483 return;
1485 cpu->env.sau.rlar[region] = value & ~0x1c;
1486 tlb_flush(CPU(cpu));
1487 break;
1489 case 0xde4: /* SFSR */
1490 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1491 goto bad_offset;
1493 if (!attrs.secure) {
1494 return;
1496 cpu->env.v7m.sfsr &= ~value; /* W1C */
1497 break;
1498 case 0xde8: /* SFAR */
1499 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1500 goto bad_offset;
1502 if (!attrs.secure) {
1503 return;
1505 cpu->env.v7m.sfsr = value;
1506 break;
1507 case 0xf00: /* Software Triggered Interrupt Register */
1509 int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ;
1510 if (excnum < s->num_irq) {
1511 armv7m_nvic_set_pending(s, excnum, false);
1513 break;
1515 default:
1516 bad_offset:
1517 qemu_log_mask(LOG_GUEST_ERROR,
1518 "NVIC: Bad write offset 0x%x\n", offset);
1522 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs)
1524 /* Return true if unprivileged access to this register is permitted. */
1525 switch (offset) {
1526 case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */
1527 /* For access via STIR_NS it is the NS CCR.USERSETMPEND that
1528 * controls access even though the CPU is in Secure state (I_QDKX).
1530 return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK;
1531 default:
1532 /* All other user accesses cause a BusFault unconditionally */
1533 return false;
1537 static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs)
1539 /* Behaviour for the SHPR register field for this exception:
1540 * return M_REG_NS to use the nonsecure vector (including for
1541 * non-banked exceptions), M_REG_S for the secure version of
1542 * a banked exception, and -1 if this field should RAZ/WI.
1544 switch (exc) {
1545 case ARMV7M_EXCP_MEM:
1546 case ARMV7M_EXCP_USAGE:
1547 case ARMV7M_EXCP_SVC:
1548 case ARMV7M_EXCP_PENDSV:
1549 case ARMV7M_EXCP_SYSTICK:
1550 /* Banked exceptions */
1551 return attrs.secure;
1552 case ARMV7M_EXCP_BUS:
1553 /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */
1554 if (!attrs.secure &&
1555 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1556 return -1;
1558 return M_REG_NS;
1559 case ARMV7M_EXCP_SECURE:
1560 /* Not banked, RAZ/WI from nonsecure */
1561 if (!attrs.secure) {
1562 return -1;
1564 return M_REG_NS;
1565 case ARMV7M_EXCP_DEBUG:
1566 /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */
1567 return M_REG_NS;
1568 case 8 ... 10:
1569 case 13:
1570 /* RES0 */
1571 return -1;
1572 default:
1573 /* Not reachable due to decode of SHPR register addresses */
1574 g_assert_not_reached();
1578 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr,
1579 uint64_t *data, unsigned size,
1580 MemTxAttrs attrs)
1582 NVICState *s = (NVICState *)opaque;
1583 uint32_t offset = addr;
1584 unsigned i, startvec, end;
1585 uint32_t val;
1587 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
1588 /* Generate BusFault for unprivileged accesses */
1589 return MEMTX_ERROR;
1592 switch (offset) {
1593 /* reads of set and clear both return the status */
1594 case 0x100 ... 0x13f: /* NVIC Set enable */
1595 offset += 0x80;
1596 /* fall through */
1597 case 0x180 ... 0x1bf: /* NVIC Clear enable */
1598 val = 0;
1599 startvec = offset - 0x180 + NVIC_FIRST_IRQ; /* vector # */
1601 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1602 if (s->vectors[startvec + i].enabled &&
1603 (attrs.secure || s->itns[startvec + i])) {
1604 val |= (1 << i);
1607 break;
1608 case 0x200 ... 0x23f: /* NVIC Set pend */
1609 offset += 0x80;
1610 /* fall through */
1611 case 0x280 ... 0x2bf: /* NVIC Clear pend */
1612 val = 0;
1613 startvec = offset - 0x280 + NVIC_FIRST_IRQ; /* vector # */
1614 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1615 if (s->vectors[startvec + i].pending &&
1616 (attrs.secure || s->itns[startvec + i])) {
1617 val |= (1 << i);
1620 break;
1621 case 0x300 ... 0x33f: /* NVIC Active */
1622 val = 0;
1623 startvec = offset - 0x300 + NVIC_FIRST_IRQ; /* vector # */
1625 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1626 if (s->vectors[startvec + i].active &&
1627 (attrs.secure || s->itns[startvec + i])) {
1628 val |= (1 << i);
1631 break;
1632 case 0x400 ... 0x5ef: /* NVIC Priority */
1633 val = 0;
1634 startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */
1636 for (i = 0; i < size && startvec + i < s->num_irq; i++) {
1637 if (attrs.secure || s->itns[startvec + i]) {
1638 val |= s->vectors[startvec + i].prio << (8 * i);
1641 break;
1642 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */
1643 val = 0;
1644 for (i = 0; i < size; i++) {
1645 unsigned hdlidx = (offset - 0xd14) + i;
1646 int sbank = shpr_bank(s, hdlidx, attrs);
1648 if (sbank < 0) {
1649 continue;
1651 val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank));
1653 break;
1654 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
1655 /* The BFSR bits [15:8] are shared between security states
1656 * and we store them in the NS copy
1658 val = s->cpu->env.v7m.cfsr[attrs.secure];
1659 val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK;
1660 val = extract32(val, (offset - 0xd28) * 8, size * 8);
1661 break;
1662 case 0xfe0 ... 0xfff: /* ID. */
1663 if (offset & 3) {
1664 val = 0;
1665 } else {
1666 val = nvic_id[(offset - 0xfe0) >> 2];
1668 break;
1669 default:
1670 if (size == 4) {
1671 val = nvic_readl(s, offset, attrs);
1672 } else {
1673 qemu_log_mask(LOG_GUEST_ERROR,
1674 "NVIC: Bad read of size %d at offset 0x%x\n",
1675 size, offset);
1676 val = 0;
1680 trace_nvic_sysreg_read(addr, val, size);
1681 *data = val;
1682 return MEMTX_OK;
1685 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr,
1686 uint64_t value, unsigned size,
1687 MemTxAttrs attrs)
1689 NVICState *s = (NVICState *)opaque;
1690 uint32_t offset = addr;
1691 unsigned i, startvec, end;
1692 unsigned setval = 0;
1694 trace_nvic_sysreg_write(addr, value, size);
1696 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
1697 /* Generate BusFault for unprivileged accesses */
1698 return MEMTX_ERROR;
1701 switch (offset) {
1702 case 0x100 ... 0x13f: /* NVIC Set enable */
1703 offset += 0x80;
1704 setval = 1;
1705 /* fall through */
1706 case 0x180 ... 0x1bf: /* NVIC Clear enable */
1707 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ;
1709 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1710 if (value & (1 << i) &&
1711 (attrs.secure || s->itns[startvec + i])) {
1712 s->vectors[startvec + i].enabled = setval;
1715 nvic_irq_update(s);
1716 return MEMTX_OK;
1717 case 0x200 ... 0x23f: /* NVIC Set pend */
1718 /* the special logic in armv7m_nvic_set_pending()
1719 * is not needed since IRQs are never escalated
1721 offset += 0x80;
1722 setval = 1;
1723 /* fall through */
1724 case 0x280 ... 0x2bf: /* NVIC Clear pend */
1725 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
1727 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1728 if (value & (1 << i) &&
1729 (attrs.secure || s->itns[startvec + i])) {
1730 s->vectors[startvec + i].pending = setval;
1733 nvic_irq_update(s);
1734 return MEMTX_OK;
1735 case 0x300 ... 0x33f: /* NVIC Active */
1736 return MEMTX_OK; /* R/O */
1737 case 0x400 ... 0x5ef: /* NVIC Priority */
1738 startvec = 8 * (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */
1740 for (i = 0; i < size && startvec + i < s->num_irq; i++) {
1741 if (attrs.secure || s->itns[startvec + i]) {
1742 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff);
1745 nvic_irq_update(s);
1746 return MEMTX_OK;
1747 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */
1748 for (i = 0; i < size; i++) {
1749 unsigned hdlidx = (offset - 0xd14) + i;
1750 int newprio = extract32(value, i * 8, 8);
1751 int sbank = shpr_bank(s, hdlidx, attrs);
1753 if (sbank < 0) {
1754 continue;
1756 set_prio(s, hdlidx, sbank, newprio);
1758 nvic_irq_update(s);
1759 return MEMTX_OK;
1760 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
1761 /* All bits are W1C, so construct 32 bit value with 0s in
1762 * the parts not written by the access size
1764 value <<= ((offset - 0xd28) * 8);
1766 s->cpu->env.v7m.cfsr[attrs.secure] &= ~value;
1767 if (attrs.secure) {
1768 /* The BFSR bits [15:8] are shared between security states
1769 * and we store them in the NS copy.
1771 s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK);
1773 return MEMTX_OK;
1775 if (size == 4) {
1776 nvic_writel(s, offset, value, attrs);
1777 return MEMTX_OK;
1779 qemu_log_mask(LOG_GUEST_ERROR,
1780 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
1781 /* This is UNPREDICTABLE; treat as RAZ/WI */
1782 return MEMTX_OK;
1785 static const MemoryRegionOps nvic_sysreg_ops = {
1786 .read_with_attrs = nvic_sysreg_read,
1787 .write_with_attrs = nvic_sysreg_write,
1788 .endianness = DEVICE_NATIVE_ENDIAN,
1791 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr,
1792 uint64_t value, unsigned size,
1793 MemTxAttrs attrs)
1795 MemoryRegion *mr = opaque;
1797 if (attrs.secure) {
1798 /* S accesses to the alias act like NS accesses to the real region */
1799 attrs.secure = 0;
1800 return memory_region_dispatch_write(mr, addr, value, size, attrs);
1801 } else {
1802 /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1803 if (attrs.user) {
1804 return MEMTX_ERROR;
1806 return MEMTX_OK;
1810 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr,
1811 uint64_t *data, unsigned size,
1812 MemTxAttrs attrs)
1814 MemoryRegion *mr = opaque;
1816 if (attrs.secure) {
1817 /* S accesses to the alias act like NS accesses to the real region */
1818 attrs.secure = 0;
1819 return memory_region_dispatch_read(mr, addr, data, size, attrs);
1820 } else {
1821 /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1822 if (attrs.user) {
1823 return MEMTX_ERROR;
1825 *data = 0;
1826 return MEMTX_OK;
1830 static const MemoryRegionOps nvic_sysreg_ns_ops = {
1831 .read_with_attrs = nvic_sysreg_ns_read,
1832 .write_with_attrs = nvic_sysreg_ns_write,
1833 .endianness = DEVICE_NATIVE_ENDIAN,
1836 static MemTxResult nvic_systick_write(void *opaque, hwaddr addr,
1837 uint64_t value, unsigned size,
1838 MemTxAttrs attrs)
1840 NVICState *s = opaque;
1841 MemoryRegion *mr;
1843 /* Direct the access to the correct systick */
1844 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
1845 return memory_region_dispatch_write(mr, addr, value, size, attrs);
1848 static MemTxResult nvic_systick_read(void *opaque, hwaddr addr,
1849 uint64_t *data, unsigned size,
1850 MemTxAttrs attrs)
1852 NVICState *s = opaque;
1853 MemoryRegion *mr;
1855 /* Direct the access to the correct systick */
1856 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
1857 return memory_region_dispatch_read(mr, addr, data, size, attrs);
1860 static const MemoryRegionOps nvic_systick_ops = {
1861 .read_with_attrs = nvic_systick_read,
1862 .write_with_attrs = nvic_systick_write,
1863 .endianness = DEVICE_NATIVE_ENDIAN,
1866 static int nvic_post_load(void *opaque, int version_id)
1868 NVICState *s = opaque;
1869 unsigned i;
1870 int resetprio;
1872 /* Check for out of range priority settings */
1873 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
1875 if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio ||
1876 s->vectors[ARMV7M_EXCP_NMI].prio != -2 ||
1877 s->vectors[ARMV7M_EXCP_HARD].prio != -1) {
1878 return 1;
1880 for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) {
1881 if (s->vectors[i].prio & ~0xff) {
1882 return 1;
1886 nvic_recompute_state(s);
1888 return 0;
1891 static const VMStateDescription vmstate_VecInfo = {
1892 .name = "armv7m_nvic_info",
1893 .version_id = 1,
1894 .minimum_version_id = 1,
1895 .fields = (VMStateField[]) {
1896 VMSTATE_INT16(prio, VecInfo),
1897 VMSTATE_UINT8(enabled, VecInfo),
1898 VMSTATE_UINT8(pending, VecInfo),
1899 VMSTATE_UINT8(active, VecInfo),
1900 VMSTATE_UINT8(level, VecInfo),
1901 VMSTATE_END_OF_LIST()
1905 static bool nvic_security_needed(void *opaque)
1907 NVICState *s = opaque;
1909 return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
1912 static int nvic_security_post_load(void *opaque, int version_id)
1914 NVICState *s = opaque;
1915 int i;
1917 /* Check for out of range priority settings */
1918 if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1
1919 && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) {
1920 /* We can't cross-check against AIRCR.BFHFNMINS as we don't know
1921 * if the CPU state has been migrated yet; a mismatch won't
1922 * cause the emulation to blow up, though.
1924 return 1;
1926 for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) {
1927 if (s->sec_vectors[i].prio & ~0xff) {
1928 return 1;
1931 return 0;
1934 static const VMStateDescription vmstate_nvic_security = {
1935 .name = "nvic/m-security",
1936 .version_id = 1,
1937 .minimum_version_id = 1,
1938 .needed = nvic_security_needed,
1939 .post_load = &nvic_security_post_load,
1940 .fields = (VMStateField[]) {
1941 VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1,
1942 vmstate_VecInfo, VecInfo),
1943 VMSTATE_UINT32(prigroup[M_REG_S], NVICState),
1944 VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS),
1945 VMSTATE_END_OF_LIST()
1949 static const VMStateDescription vmstate_nvic = {
1950 .name = "armv7m_nvic",
1951 .version_id = 4,
1952 .minimum_version_id = 4,
1953 .post_load = &nvic_post_load,
1954 .fields = (VMStateField[]) {
1955 VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1,
1956 vmstate_VecInfo, VecInfo),
1957 VMSTATE_UINT32(prigroup[M_REG_NS], NVICState),
1958 VMSTATE_END_OF_LIST()
1960 .subsections = (const VMStateDescription*[]) {
1961 &vmstate_nvic_security,
1962 NULL
1966 static Property props_nvic[] = {
1967 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
1968 DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64),
1969 DEFINE_PROP_END_OF_LIST()
1972 static void armv7m_nvic_reset(DeviceState *dev)
1974 int resetprio;
1975 NVICState *s = NVIC(dev);
1977 memset(s->vectors, 0, sizeof(s->vectors));
1978 memset(s->sec_vectors, 0, sizeof(s->sec_vectors));
1979 s->prigroup[M_REG_NS] = 0;
1980 s->prigroup[M_REG_S] = 0;
1982 s->vectors[ARMV7M_EXCP_NMI].enabled = 1;
1983 /* MEM, BUS, and USAGE are enabled through
1984 * the System Handler Control register
1986 s->vectors[ARMV7M_EXCP_SVC].enabled = 1;
1987 s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1;
1988 s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
1989 s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
1991 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
1992 s->vectors[ARMV7M_EXCP_RESET].prio = resetprio;
1993 s->vectors[ARMV7M_EXCP_NMI].prio = -2;
1994 s->vectors[ARMV7M_EXCP_HARD].prio = -1;
1996 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
1997 s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1;
1998 s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1;
1999 s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2000 s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2002 /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */
2003 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
2004 /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */
2005 s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
2006 } else {
2007 s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
2010 /* Strictly speaking the reset handler should be enabled.
2011 * However, we don't simulate soft resets through the NVIC,
2012 * and the reset vector should never be pended.
2013 * So we leave it disabled to catch logic errors.
2016 s->exception_prio = NVIC_NOEXC_PRIO;
2017 s->vectpending = 0;
2018 s->vectpending_is_s_banked = false;
2019 s->vectpending_prio = NVIC_NOEXC_PRIO;
2021 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2022 memset(s->itns, 0, sizeof(s->itns));
2023 } else {
2024 /* This state is constant and not guest accessible in a non-security
2025 * NVIC; we set the bits to true to avoid having to do a feature
2026 * bit check in the NVIC enable/pend/etc register accessors.
2028 int i;
2030 for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) {
2031 s->itns[i] = true;
2036 static void nvic_systick_trigger(void *opaque, int n, int level)
2038 NVICState *s = opaque;
2040 if (level) {
2041 /* SysTick just asked us to pend its exception.
2042 * (This is different from an external interrupt line's
2043 * behaviour.)
2044 * n == 0 : NonSecure systick
2045 * n == 1 : Secure systick
2047 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n);
2051 static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
2053 NVICState *s = NVIC(dev);
2054 Error *err = NULL;
2055 int regionlen;
2057 s->cpu = ARM_CPU(qemu_get_cpu(0));
2058 assert(s->cpu);
2060 if (s->num_irq > NVIC_MAX_IRQ) {
2061 error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);
2062 return;
2065 qdev_init_gpio_in(dev, set_irq_level, s->num_irq);
2067 /* include space for internal exception vectors */
2068 s->num_irq += NVIC_FIRST_IRQ;
2070 object_property_set_bool(OBJECT(&s->systick[M_REG_NS]), true,
2071 "realized", &err);
2072 if (err != NULL) {
2073 error_propagate(errp, err);
2074 return;
2076 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0,
2077 qdev_get_gpio_in_named(dev, "systick-trigger",
2078 M_REG_NS));
2080 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2081 /* We couldn't init the secure systick device in instance_init
2082 * as we didn't know then if the CPU had the security extensions;
2083 * so we have to do it here.
2085 object_initialize(&s->systick[M_REG_S], sizeof(s->systick[M_REG_S]),
2086 TYPE_SYSTICK);
2087 qdev_set_parent_bus(DEVICE(&s->systick[M_REG_S]), sysbus_get_default());
2089 object_property_set_bool(OBJECT(&s->systick[M_REG_S]), true,
2090 "realized", &err);
2091 if (err != NULL) {
2092 error_propagate(errp, err);
2093 return;
2095 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0,
2096 qdev_get_gpio_in_named(dev, "systick-trigger",
2097 M_REG_S));
2100 /* The NVIC and System Control Space (SCS) starts at 0xe000e000
2101 * and looks like this:
2102 * 0x004 - ICTR
2103 * 0x010 - 0xff - systick
2104 * 0x100..0x7ec - NVIC
2105 * 0x7f0..0xcff - Reserved
2106 * 0xd00..0xd3c - SCS registers
2107 * 0xd40..0xeff - Reserved or Not implemented
2108 * 0xf00 - STIR
2110 * Some registers within this space are banked between security states.
2111 * In v8M there is a second range 0xe002e000..0xe002efff which is the
2112 * NonSecure alias SCS; secure accesses to this behave like NS accesses
2113 * to the main SCS range, and non-secure accesses (including when
2114 * the security extension is not implemented) are RAZ/WI.
2115 * Note that both the main SCS range and the alias range are defined
2116 * to be exempt from memory attribution (R_BLJT) and so the memory
2117 * transaction attribute always matches the current CPU security
2118 * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops
2119 * wrappers we change attrs.secure to indicate the NS access; so
2120 * generally code determining which banked register to use should
2121 * use attrs.secure; code determining actual behaviour of the system
2122 * should use env->v7m.secure.
2124 regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000;
2125 memory_region_init(&s->container, OBJECT(s), "nvic", regionlen);
2126 /* The system register region goes at the bottom of the priority
2127 * stack as it covers the whole page.
2129 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
2130 "nvic_sysregs", 0x1000);
2131 memory_region_add_subregion(&s->container, 0, &s->sysregmem);
2133 memory_region_init_io(&s->systickmem, OBJECT(s),
2134 &nvic_systick_ops, s,
2135 "nvic_systick", 0xe0);
2137 memory_region_add_subregion_overlap(&s->container, 0x10,
2138 &s->systickmem, 1);
2140 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
2141 memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
2142 &nvic_sysreg_ns_ops, &s->sysregmem,
2143 "nvic_sysregs_ns", 0x1000);
2144 memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem);
2145 memory_region_init_io(&s->systick_ns_mem, OBJECT(s),
2146 &nvic_sysreg_ns_ops, &s->systickmem,
2147 "nvic_systick_ns", 0xe0);
2148 memory_region_add_subregion_overlap(&s->container, 0x20010,
2149 &s->systick_ns_mem, 1);
2152 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
2155 static void armv7m_nvic_instance_init(Object *obj)
2157 /* We have a different default value for the num-irq property
2158 * than our superclass. This function runs after qdev init
2159 * has set the defaults from the Property array and before
2160 * any user-specified property setting, so just modify the
2161 * value in the GICState struct.
2163 DeviceState *dev = DEVICE(obj);
2164 NVICState *nvic = NVIC(obj);
2165 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
2167 object_initialize(&nvic->systick[M_REG_NS],
2168 sizeof(nvic->systick[M_REG_NS]), TYPE_SYSTICK);
2169 qdev_set_parent_bus(DEVICE(&nvic->systick[M_REG_NS]), sysbus_get_default());
2170 /* We can't initialize the secure systick here, as we don't know
2171 * yet if we need it.
2174 sysbus_init_irq(sbd, &nvic->excpout);
2175 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
2176 qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger",
2177 M_REG_NUM_BANKS);
2180 static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
2182 DeviceClass *dc = DEVICE_CLASS(klass);
2184 dc->vmsd = &vmstate_nvic;
2185 dc->props = props_nvic;
2186 dc->reset = armv7m_nvic_reset;
2187 dc->realize = armv7m_nvic_realize;
2190 static const TypeInfo armv7m_nvic_info = {
2191 .name = TYPE_NVIC,
2192 .parent = TYPE_SYS_BUS_DEVICE,
2193 .instance_init = armv7m_nvic_instance_init,
2194 .instance_size = sizeof(NVICState),
2195 .class_init = armv7m_nvic_class_init,
2196 .class_size = sizeof(SysBusDeviceClass),
2199 static void armv7m_nvic_register_types(void)
2201 type_register_static(&armv7m_nvic_info);
2204 type_init(armv7m_nvic_register_types)