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