1 /* 2 * ARM Nested Vectored Interrupt Controller 3 * 4 * Copyright (c) 2006-2007 CodeSourcery. 5 * Written by Paul Brook 6 * 7 * This code is licensed under the GPL. 8 * 9 * The ARMv7M System controller is fairly tightly tied in with the 10 * NVIC. Much of that is also implemented here. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qapi/error.h" 15 #include "qemu-common.h" 16 #include "cpu.h" 17 #include "hw/sysbus.h" 18 #include "qemu/timer.h" 19 #include "hw/arm/arm.h" 20 #include "hw/intc/armv7m_nvic.h" 21 #include "target/arm/cpu.h" 22 #include "exec/exec-all.h" 23 #include "qemu/log.h" 24 #include "trace.h" 25 26 /* IRQ number counting: 27 * 28 * the num-irq property counts the number of external IRQ lines 29 * 30 * NVICState::num_irq counts the total number of exceptions 31 * (external IRQs, the 15 internal exceptions including reset, 32 * and one for the unused exception number 0). 33 * 34 * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines. 35 * 36 * NVIC_MAX_VECTORS is the highest permitted number of exceptions. 37 * 38 * Iterating through all exceptions should typically be done with 39 * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0. 40 * 41 * The external qemu_irq lines are the NVIC's external IRQ lines, 42 * so line 0 is exception 16. 43 * 44 * In the terminology of the architecture manual, "interrupts" are 45 * a subcategory of exception referring to the external interrupts 46 * (which are exception numbers NVIC_FIRST_IRQ and upward). 47 * For historical reasons QEMU tends to use "interrupt" and 48 * "exception" more or less interchangeably. 49 */ 50 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS 51 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ) 52 53 /* Effective running priority of the CPU when no exception is active 54 * (higher than the highest possible priority value) 55 */ 56 #define NVIC_NOEXC_PRIO 0x100 57 /* Maximum priority of non-secure exceptions when AIRCR.PRIS is set */ 58 #define NVIC_NS_PRIO_LIMIT 0x80 59 60 static const uint8_t nvic_id[] = { 61 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 62 }; 63 64 static int nvic_pending_prio(NVICState *s) 65 { 66 /* return the group priority of the current pending interrupt, 67 * or NVIC_NOEXC_PRIO if no interrupt is pending 68 */ 69 return s->vectpending_prio; 70 } 71 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). 77 * 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. 85 */ 86 static bool nvic_rettobase(NVICState *s) 87 { 88 int irq, nhand = 0; 89 bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY); 90 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; 98 } 99 } 100 } 101 102 return 1; 103 } 104 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 108 */ 109 static bool nvic_isrpending(NVICState *s) 110 { 111 int irq; 112 113 /* We can shortcut if the highest priority pending interrupt 114 * happens to be external or if there is nothing pending. 115 */ 116 if (s->vectpending > NVIC_FIRST_IRQ) { 117 return true; 118 } 119 if (s->vectpending == 0) { 120 return false; 121 } 122 123 for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) { 124 if (s->vectors[irq].pending) { 125 return true; 126 } 127 } 128 return false; 129 } 130 131 static bool exc_is_banked(int exc) 132 { 133 /* Return true if this is one of the limited set of exceptions which 134 * are banked (and thus have state in sec_vectors[]) 135 */ 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; 142 } 143 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. 147 */ 148 static inline uint32_t nvic_gprio_mask(NVICState *s, bool secure) 149 { 150 return ~0U << (s->prigroup[secure] + 1); 151 } 152 153 static bool exc_targets_secure(NVICState *s, int exc) 154 { 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; 158 } 159 160 if (exc >= NVIC_FIRST_IRQ) { 161 return !s->itns[exc]; 162 } 163 164 /* Function shouldn't be called for banked exceptions. */ 165 assert(!exc_is_banked(exc)); 166 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. 181 */ 182 return true; 183 } 184 } 185 186 static int exc_group_prio(NVICState *s, int rawprio, bool targets_secure) 187 { 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. 191 */ 192 if (rawprio < 0) { 193 return rawprio; 194 } 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 198 */ 199 if (!targets_secure && 200 (s->cpu->env.v7m.aircr & R_V7M_AIRCR_PRIS_MASK)) { 201 rawprio = (rawprio >> 1) + NVIC_NS_PRIO_LIMIT; 202 } 203 return rawprio; 204 } 205 206 /* Recompute vectpending and exception_prio for a CPU which implements 207 * the Security extension 208 */ 209 static void nvic_recompute_state_secure(NVICState *s) 210 { 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; 216 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. 225 */ 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; 231 232 if (bank == M_REG_S) { 233 if (!exc_is_banked(i)) { 234 continue; 235 } 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); 241 } 242 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); 248 } 249 if (vec->active && prio < active_prio) { 250 active_prio = prio; 251 } 252 } 253 } 254 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; 259 260 trace_nvic_recompute_state_secure(s->vectpending, 261 s->vectpending_is_s_banked, 262 s->vectpending_prio, 263 s->exception_prio); 264 } 265 266 /* Recompute vectpending and exception_prio */ 267 static void nvic_recompute_state(NVICState *s) 268 { 269 int i; 270 int pend_prio = NVIC_NOEXC_PRIO; 271 int active_prio = NVIC_NOEXC_PRIO; 272 int pend_irq = 0; 273 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. 280 */ 281 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 282 nvic_recompute_state_secure(s); 283 return; 284 } 285 286 for (i = 1; i < s->num_irq; i++) { 287 VecInfo *vec = &s->vectors[i]; 288 289 if (vec->enabled && vec->pending && vec->prio < pend_prio) { 290 pend_prio = vec->prio; 291 pend_irq = i; 292 } 293 if (vec->active && vec->prio < active_prio) { 294 active_prio = vec->prio; 295 } 296 } 297 298 if (active_prio > 0) { 299 active_prio &= nvic_gprio_mask(s, false); 300 } 301 302 if (pend_prio > 0) { 303 pend_prio &= nvic_gprio_mask(s, false); 304 } 305 306 s->vectpending = pend_irq; 307 s->vectpending_prio = pend_prio; 308 s->exception_prio = active_prio; 309 310 trace_nvic_recompute_state(s->vectpending, 311 s->vectpending_prio, 312 s->exception_prio); 313 } 314 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. 318 */ 319 static inline int nvic_exec_prio(NVICState *s) 320 { 321 CPUARMState *env = &s->cpu->env; 322 int running = NVIC_NOEXC_PRIO; 323 324 if (env->v7m.basepri[M_REG_NS] > 0) { 325 running = exc_group_prio(s, env->v7m.basepri[M_REG_NS], M_REG_NS); 326 } 327 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; 332 } 333 } 334 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; 339 } 340 } else { 341 running = 0; 342 } 343 } 344 345 if (env->v7m.primask[M_REG_S]) { 346 running = 0; 347 } 348 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; 356 } 357 } else { 358 running = 0; 359 } 360 } 361 } 362 363 if (env->v7m.faultmask[M_REG_S]) { 364 running = (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) ? -3 : -1; 365 } 366 367 /* consider priority of active handler */ 368 return MIN(running, s->exception_prio); 369 } 370 371 bool armv7m_nvic_neg_prio_requested(void *opaque, bool secure) 372 { 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(). 380 */ 381 NVICState *s = opaque; 382 383 if (s->cpu->env.v7m.faultmask[secure]) { 384 return true; 385 } 386 387 if (secure ? s->sec_vectors[ARMV7M_EXCP_HARD].active : 388 s->vectors[ARMV7M_EXCP_HARD].active) { 389 return true; 390 } 391 392 if (s->vectors[ARMV7M_EXCP_NMI].active && 393 exc_targets_secure(s, ARMV7M_EXCP_NMI) == secure) { 394 return true; 395 } 396 397 return false; 398 } 399 400 bool armv7m_nvic_can_take_pending_exception(void *opaque) 401 { 402 NVICState *s = opaque; 403 404 return nvic_exec_prio(s) > nvic_pending_prio(s); 405 } 406 407 int armv7m_nvic_raw_execution_priority(void *opaque) 408 { 409 NVICState *s = opaque; 410 411 return s->exception_prio; 412 } 413 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). 417 */ 418 static void set_prio(NVICState *s, unsigned irq, bool secure, uint8_t prio) 419 { 420 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */ 421 assert(irq < s->num_irq); 422 423 if (secure) { 424 assert(exc_is_banked(irq)); 425 s->sec_vectors[irq].prio = prio; 426 } else { 427 s->vectors[irq].prio = prio; 428 } 429 430 trace_nvic_set_prio(irq, secure, prio); 431 } 432 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). 436 */ 437 static int get_prio(NVICState *s, unsigned irq, bool secure) 438 { 439 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */ 440 assert(irq < s->num_irq); 441 442 if (secure) { 443 assert(exc_is_banked(irq)); 444 return s->sec_vectors[irq].prio; 445 } else { 446 return s->vectors[irq].prio; 447 } 448 } 449 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 454 */ 455 static void nvic_irq_update(NVICState *s) 456 { 457 int lvl; 458 int pend_prio; 459 460 nvic_recompute_state(s); 461 pend_prio = nvic_pending_prio(s); 462 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. 468 */ 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); 472 } 473 474 /** 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. 481 * 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. 485 */ 486 static void armv7m_nvic_clear_pending(void *opaque, int irq, bool secure) 487 { 488 NVICState *s = (NVICState *)opaque; 489 VecInfo *vec; 490 491 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 492 493 if (secure) { 494 assert(exc_is_banked(irq)); 495 vec = &s->sec_vectors[irq]; 496 } else { 497 vec = &s->vectors[irq]; 498 } 499 trace_nvic_clear_pending(irq, secure, vec->enabled, vec->prio); 500 if (vec->pending) { 501 vec->pending = 0; 502 nvic_irq_update(s); 503 } 504 } 505 506 static void do_armv7m_nvic_set_pending(void *opaque, int irq, bool secure, 507 bool derived) 508 { 509 /* Pend an exception, including possibly escalating it to HardFault. 510 * 511 * This function handles both "normal" pending of interrupts and 512 * exceptions, and also derived exceptions (ones which occur as 513 * a result of trying to take some other exception). 514 * 515 * If derived == true, the caller guarantees that we are part way through 516 * trying to take an exception (but have not yet called 517 * armv7m_nvic_acknowledge_irq() to make it active), and so: 518 * - s->vectpending is the "original exception" we were trying to take 519 * - irq is the "derived exception" 520 * - nvic_exec_prio(s) gives the priority before exception entry 521 * Here we handle the prioritization logic which the pseudocode puts 522 * in the DerivedLateArrival() function. 523 */ 524 525 NVICState *s = (NVICState *)opaque; 526 bool banked = exc_is_banked(irq); 527 VecInfo *vec; 528 529 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 530 assert(!secure || banked); 531 532 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq]; 533 534 trace_nvic_set_pending(irq, secure, derived, vec->enabled, vec->prio); 535 536 if (derived) { 537 /* Derived exceptions are always synchronous. */ 538 assert(irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV); 539 540 if (irq == ARMV7M_EXCP_DEBUG && 541 exc_group_prio(s, vec->prio, secure) >= nvic_exec_prio(s)) { 542 /* DebugMonitorFault, but its priority is lower than the 543 * preempted exception priority: just ignore it. 544 */ 545 return; 546 } 547 548 if (irq == ARMV7M_EXCP_HARD && vec->prio >= s->vectpending_prio) { 549 /* If this is a terminal exception (one which means we cannot 550 * take the original exception, like a failure to read its 551 * vector table entry), then we must take the derived exception. 552 * If the derived exception can't take priority over the 553 * original exception, then we go into Lockup. 554 * 555 * For QEMU, we rely on the fact that a derived exception is 556 * terminal if and only if it's reported to us as HardFault, 557 * which saves having to have an extra argument is_terminal 558 * that we'd only use in one place. 559 */ 560 cpu_abort(&s->cpu->parent_obj, 561 "Lockup: can't take terminal derived exception " 562 "(original exception priority %d)\n", 563 s->vectpending_prio); 564 } 565 /* We now continue with the same code as for a normal pending 566 * exception, which will cause us to pend the derived exception. 567 * We'll then take either the original or the derived exception 568 * based on which is higher priority by the usual mechanism 569 * for selecting the highest priority pending interrupt. 570 */ 571 } 572 573 if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) { 574 /* If a synchronous exception is pending then it may be 575 * escalated to HardFault if: 576 * * it is equal or lower priority to current execution 577 * * it is disabled 578 * (ie we need to take it immediately but we can't do so). 579 * Asynchronous exceptions (and interrupts) simply remain pending. 580 * 581 * For QEMU, we don't have any imprecise (asynchronous) faults, 582 * so we can assume that PREFETCH_ABORT and DATA_ABORT are always 583 * synchronous. 584 * Debug exceptions are awkward because only Debug exceptions 585 * resulting from the BKPT instruction should be escalated, 586 * but we don't currently implement any Debug exceptions other 587 * than those that result from BKPT, so we treat all debug exceptions 588 * as needing escalation. 589 * 590 * This all means we can identify whether to escalate based only on 591 * the exception number and don't (yet) need the caller to explicitly 592 * tell us whether this exception is synchronous or not. 593 */ 594 int running = nvic_exec_prio(s); 595 bool escalate = false; 596 597 if (exc_group_prio(s, vec->prio, secure) >= running) { 598 trace_nvic_escalate_prio(irq, vec->prio, running); 599 escalate = true; 600 } else if (!vec->enabled) { 601 trace_nvic_escalate_disabled(irq); 602 escalate = true; 603 } 604 605 if (escalate) { 606 607 /* We need to escalate this exception to a synchronous HardFault. 608 * If BFHFNMINS is set then we escalate to the banked HF for 609 * the target security state of the original exception; otherwise 610 * we take a Secure HardFault. 611 */ 612 irq = ARMV7M_EXCP_HARD; 613 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) && 614 (secure || 615 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) { 616 vec = &s->sec_vectors[irq]; 617 } else { 618 vec = &s->vectors[irq]; 619 } 620 if (running <= vec->prio) { 621 /* We want to escalate to HardFault but we can't take the 622 * synchronous HardFault at this point either. This is a 623 * Lockup condition due to a guest bug. We don't model 624 * Lockup, so report via cpu_abort() instead. 625 */ 626 cpu_abort(&s->cpu->parent_obj, 627 "Lockup: can't escalate %d to HardFault " 628 "(current priority %d)\n", irq, running); 629 } 630 631 /* HF may be banked but there is only one shared HFSR */ 632 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK; 633 } 634 } 635 636 if (!vec->pending) { 637 vec->pending = 1; 638 nvic_irq_update(s); 639 } 640 } 641 642 void armv7m_nvic_set_pending(void *opaque, int irq, bool secure) 643 { 644 do_armv7m_nvic_set_pending(opaque, irq, secure, false); 645 } 646 647 void armv7m_nvic_set_pending_derived(void *opaque, int irq, bool secure) 648 { 649 do_armv7m_nvic_set_pending(opaque, irq, secure, true); 650 } 651 652 /* Make pending IRQ active. */ 653 void armv7m_nvic_acknowledge_irq(void *opaque) 654 { 655 NVICState *s = (NVICState *)opaque; 656 CPUARMState *env = &s->cpu->env; 657 const int pending = s->vectpending; 658 const int running = nvic_exec_prio(s); 659 VecInfo *vec; 660 661 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq); 662 663 if (s->vectpending_is_s_banked) { 664 vec = &s->sec_vectors[pending]; 665 } else { 666 vec = &s->vectors[pending]; 667 } 668 669 assert(vec->enabled); 670 assert(vec->pending); 671 672 assert(s->vectpending_prio < running); 673 674 trace_nvic_acknowledge_irq(pending, s->vectpending_prio); 675 676 vec->active = 1; 677 vec->pending = 0; 678 679 write_v7m_exception(env, s->vectpending); 680 681 nvic_irq_update(s); 682 } 683 684 void armv7m_nvic_get_pending_irq_info(void *opaque, 685 int *pirq, bool *ptargets_secure) 686 { 687 NVICState *s = (NVICState *)opaque; 688 const int pending = s->vectpending; 689 bool targets_secure; 690 691 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq); 692 693 if (s->vectpending_is_s_banked) { 694 targets_secure = true; 695 } else { 696 targets_secure = !exc_is_banked(pending) && 697 exc_targets_secure(s, pending); 698 } 699 700 trace_nvic_get_pending_irq_info(pending, targets_secure); 701 702 *ptargets_secure = targets_secure; 703 *pirq = pending; 704 } 705 706 int armv7m_nvic_complete_irq(void *opaque, int irq, bool secure) 707 { 708 NVICState *s = (NVICState *)opaque; 709 VecInfo *vec; 710 int ret; 711 712 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 713 714 if (secure && exc_is_banked(irq)) { 715 vec = &s->sec_vectors[irq]; 716 } else { 717 vec = &s->vectors[irq]; 718 } 719 720 trace_nvic_complete_irq(irq, secure); 721 722 if (!vec->active) { 723 /* Tell the caller this was an illegal exception return */ 724 return -1; 725 } 726 727 ret = nvic_rettobase(s); 728 729 vec->active = 0; 730 if (vec->level) { 731 /* Re-pend the exception if it's still held high; only 732 * happens for extenal IRQs 733 */ 734 assert(irq >= NVIC_FIRST_IRQ); 735 vec->pending = 1; 736 } 737 738 nvic_irq_update(s); 739 740 return ret; 741 } 742 743 /* callback when external interrupt line is changed */ 744 static void set_irq_level(void *opaque, int n, int level) 745 { 746 NVICState *s = opaque; 747 VecInfo *vec; 748 749 n += NVIC_FIRST_IRQ; 750 751 assert(n >= NVIC_FIRST_IRQ && n < s->num_irq); 752 753 trace_nvic_set_irq_level(n, level); 754 755 /* The pending status of an external interrupt is 756 * latched on rising edge and exception handler return. 757 * 758 * Pulsing the IRQ will always run the handler 759 * once, and the handler will re-run until the 760 * level is low when the handler completes. 761 */ 762 vec = &s->vectors[n]; 763 if (level != vec->level) { 764 vec->level = level; 765 if (level) { 766 armv7m_nvic_set_pending(s, n, false); 767 } 768 } 769 } 770 771 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs) 772 { 773 ARMCPU *cpu = s->cpu; 774 uint32_t val; 775 776 switch (offset) { 777 case 4: /* Interrupt Control Type. */ 778 return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1; 779 case 0xc: /* CPPWR */ 780 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 781 goto bad_offset; 782 } 783 /* We make the IMPDEF choice that nothing can ever go into a 784 * non-retentive power state, which allows us to RAZ/WI this. 785 */ 786 return 0; 787 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */ 788 { 789 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ; 790 int i; 791 792 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 793 goto bad_offset; 794 } 795 if (!attrs.secure) { 796 return 0; 797 } 798 val = 0; 799 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) { 800 if (s->itns[startvec + i]) { 801 val |= (1 << i); 802 } 803 } 804 return val; 805 } 806 case 0xd00: /* CPUID Base. */ 807 return cpu->midr; 808 case 0xd04: /* Interrupt Control State (ICSR) */ 809 /* VECTACTIVE */ 810 val = cpu->env.v7m.exception; 811 /* VECTPENDING */ 812 val |= (s->vectpending & 0xff) << 12; 813 /* ISRPENDING - set if any external IRQ is pending */ 814 if (nvic_isrpending(s)) { 815 val |= (1 << 22); 816 } 817 /* RETTOBASE - set if only one handler is active */ 818 if (nvic_rettobase(s)) { 819 val |= (1 << 11); 820 } 821 if (attrs.secure) { 822 /* PENDSTSET */ 823 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) { 824 val |= (1 << 26); 825 } 826 /* PENDSVSET */ 827 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) { 828 val |= (1 << 28); 829 } 830 } else { 831 /* PENDSTSET */ 832 if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) { 833 val |= (1 << 26); 834 } 835 /* PENDSVSET */ 836 if (s->vectors[ARMV7M_EXCP_PENDSV].pending) { 837 val |= (1 << 28); 838 } 839 } 840 /* NMIPENDSET */ 841 if ((attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) 842 && s->vectors[ARMV7M_EXCP_NMI].pending) { 843 val |= (1 << 31); 844 } 845 /* ISRPREEMPT: RES0 when halting debug not implemented */ 846 /* STTNS: RES0 for the Main Extension */ 847 return val; 848 case 0xd08: /* Vector Table Offset. */ 849 return cpu->env.v7m.vecbase[attrs.secure]; 850 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */ 851 val = 0xfa050000 | (s->prigroup[attrs.secure] << 8); 852 if (attrs.secure) { 853 /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */ 854 val |= cpu->env.v7m.aircr; 855 } else { 856 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 857 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If 858 * security isn't supported then BFHFNMINS is RAO (and 859 * the bit in env.v7m.aircr is always set). 860 */ 861 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK; 862 } 863 } 864 return val; 865 case 0xd10: /* System Control. */ 866 /* TODO: Implement SLEEPONEXIT. */ 867 return 0; 868 case 0xd14: /* Configuration Control. */ 869 /* The BFHFNMIGN bit is the only non-banked bit; we 870 * keep it in the non-secure copy of the register. 871 */ 872 val = cpu->env.v7m.ccr[attrs.secure]; 873 val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK; 874 return val; 875 case 0xd24: /* System Handler Control and State (SHCSR) */ 876 val = 0; 877 if (attrs.secure) { 878 if (s->sec_vectors[ARMV7M_EXCP_MEM].active) { 879 val |= (1 << 0); 880 } 881 if (s->sec_vectors[ARMV7M_EXCP_HARD].active) { 882 val |= (1 << 2); 883 } 884 if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) { 885 val |= (1 << 3); 886 } 887 if (s->sec_vectors[ARMV7M_EXCP_SVC].active) { 888 val |= (1 << 7); 889 } 890 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) { 891 val |= (1 << 10); 892 } 893 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) { 894 val |= (1 << 11); 895 } 896 if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) { 897 val |= (1 << 12); 898 } 899 if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) { 900 val |= (1 << 13); 901 } 902 if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) { 903 val |= (1 << 15); 904 } 905 if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) { 906 val |= (1 << 16); 907 } 908 if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) { 909 val |= (1 << 18); 910 } 911 if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) { 912 val |= (1 << 21); 913 } 914 /* SecureFault is not banked but is always RAZ/WI to NS */ 915 if (s->vectors[ARMV7M_EXCP_SECURE].active) { 916 val |= (1 << 4); 917 } 918 if (s->vectors[ARMV7M_EXCP_SECURE].enabled) { 919 val |= (1 << 19); 920 } 921 if (s->vectors[ARMV7M_EXCP_SECURE].pending) { 922 val |= (1 << 20); 923 } 924 } else { 925 if (s->vectors[ARMV7M_EXCP_MEM].active) { 926 val |= (1 << 0); 927 } 928 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 929 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */ 930 if (s->vectors[ARMV7M_EXCP_HARD].active) { 931 val |= (1 << 2); 932 } 933 if (s->vectors[ARMV7M_EXCP_HARD].pending) { 934 val |= (1 << 21); 935 } 936 } 937 if (s->vectors[ARMV7M_EXCP_USAGE].active) { 938 val |= (1 << 3); 939 } 940 if (s->vectors[ARMV7M_EXCP_SVC].active) { 941 val |= (1 << 7); 942 } 943 if (s->vectors[ARMV7M_EXCP_PENDSV].active) { 944 val |= (1 << 10); 945 } 946 if (s->vectors[ARMV7M_EXCP_SYSTICK].active) { 947 val |= (1 << 11); 948 } 949 if (s->vectors[ARMV7M_EXCP_USAGE].pending) { 950 val |= (1 << 12); 951 } 952 if (s->vectors[ARMV7M_EXCP_MEM].pending) { 953 val |= (1 << 13); 954 } 955 if (s->vectors[ARMV7M_EXCP_SVC].pending) { 956 val |= (1 << 15); 957 } 958 if (s->vectors[ARMV7M_EXCP_MEM].enabled) { 959 val |= (1 << 16); 960 } 961 if (s->vectors[ARMV7M_EXCP_USAGE].enabled) { 962 val |= (1 << 18); 963 } 964 } 965 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 966 if (s->vectors[ARMV7M_EXCP_BUS].active) { 967 val |= (1 << 1); 968 } 969 if (s->vectors[ARMV7M_EXCP_BUS].pending) { 970 val |= (1 << 14); 971 } 972 if (s->vectors[ARMV7M_EXCP_BUS].enabled) { 973 val |= (1 << 17); 974 } 975 if (arm_feature(&cpu->env, ARM_FEATURE_V8) && 976 s->vectors[ARMV7M_EXCP_NMI].active) { 977 /* NMIACT is not present in v7M */ 978 val |= (1 << 5); 979 } 980 } 981 982 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */ 983 if (s->vectors[ARMV7M_EXCP_DEBUG].active) { 984 val |= (1 << 8); 985 } 986 return val; 987 case 0xd2c: /* Hard Fault Status. */ 988 return cpu->env.v7m.hfsr; 989 case 0xd30: /* Debug Fault Status. */ 990 return cpu->env.v7m.dfsr; 991 case 0xd34: /* MMFAR MemManage Fault Address */ 992 return cpu->env.v7m.mmfar[attrs.secure]; 993 case 0xd38: /* Bus Fault Address. */ 994 return cpu->env.v7m.bfar; 995 case 0xd3c: /* Aux Fault Status. */ 996 /* TODO: Implement fault status registers. */ 997 qemu_log_mask(LOG_UNIMP, 998 "Aux Fault status registers unimplemented\n"); 999 return 0; 1000 case 0xd40: /* PFR0. */ 1001 return cpu->id_pfr0; 1002 case 0xd44: /* PFR1. */ 1003 return cpu->id_pfr1; 1004 case 0xd48: /* DFR0. */ 1005 return cpu->id_dfr0; 1006 case 0xd4c: /* AFR0. */ 1007 return cpu->id_afr0; 1008 case 0xd50: /* MMFR0. */ 1009 return cpu->id_mmfr0; 1010 case 0xd54: /* MMFR1. */ 1011 return cpu->id_mmfr1; 1012 case 0xd58: /* MMFR2. */ 1013 return cpu->id_mmfr2; 1014 case 0xd5c: /* MMFR3. */ 1015 return cpu->id_mmfr3; 1016 case 0xd60: /* ISAR0. */ 1017 return cpu->id_isar0; 1018 case 0xd64: /* ISAR1. */ 1019 return cpu->id_isar1; 1020 case 0xd68: /* ISAR2. */ 1021 return cpu->id_isar2; 1022 case 0xd6c: /* ISAR3. */ 1023 return cpu->id_isar3; 1024 case 0xd70: /* ISAR4. */ 1025 return cpu->id_isar4; 1026 case 0xd74: /* ISAR5. */ 1027 return cpu->id_isar5; 1028 /* TODO: Implement debug registers. */ 1029 case 0xd90: /* MPU_TYPE */ 1030 /* Unified MPU; if the MPU is not present this value is zero */ 1031 return cpu->pmsav7_dregion << 8; 1032 break; 1033 case 0xd94: /* MPU_CTRL */ 1034 return cpu->env.v7m.mpu_ctrl[attrs.secure]; 1035 case 0xd98: /* MPU_RNR */ 1036 return cpu->env.pmsav7.rnr[attrs.secure]; 1037 case 0xd9c: /* MPU_RBAR */ 1038 case 0xda4: /* MPU_RBAR_A1 */ 1039 case 0xdac: /* MPU_RBAR_A2 */ 1040 case 0xdb4: /* MPU_RBAR_A3 */ 1041 { 1042 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1043 1044 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1045 /* PMSAv8M handling of the aliases is different from v7M: 1046 * aliases A1, A2, A3 override the low two bits of the region 1047 * number in MPU_RNR, and there is no 'region' field in the 1048 * RBAR register. 1049 */ 1050 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1051 if (aliasno) { 1052 region = deposit32(region, 0, 2, aliasno); 1053 } 1054 if (region >= cpu->pmsav7_dregion) { 1055 return 0; 1056 } 1057 return cpu->env.pmsav8.rbar[attrs.secure][region]; 1058 } 1059 1060 if (region >= cpu->pmsav7_dregion) { 1061 return 0; 1062 } 1063 return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf); 1064 } 1065 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */ 1066 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */ 1067 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */ 1068 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */ 1069 { 1070 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1071 1072 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1073 /* PMSAv8M handling of the aliases is different from v7M: 1074 * aliases A1, A2, A3 override the low two bits of the region 1075 * number in MPU_RNR. 1076 */ 1077 int aliasno = (offset - 0xda0) / 8; /* 0..3 */ 1078 if (aliasno) { 1079 region = deposit32(region, 0, 2, aliasno); 1080 } 1081 if (region >= cpu->pmsav7_dregion) { 1082 return 0; 1083 } 1084 return cpu->env.pmsav8.rlar[attrs.secure][region]; 1085 } 1086 1087 if (region >= cpu->pmsav7_dregion) { 1088 return 0; 1089 } 1090 return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) | 1091 (cpu->env.pmsav7.drsr[region] & 0xffff); 1092 } 1093 case 0xdc0: /* MPU_MAIR0 */ 1094 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1095 goto bad_offset; 1096 } 1097 return cpu->env.pmsav8.mair0[attrs.secure]; 1098 case 0xdc4: /* MPU_MAIR1 */ 1099 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1100 goto bad_offset; 1101 } 1102 return cpu->env.pmsav8.mair1[attrs.secure]; 1103 case 0xdd0: /* SAU_CTRL */ 1104 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1105 goto bad_offset; 1106 } 1107 if (!attrs.secure) { 1108 return 0; 1109 } 1110 return cpu->env.sau.ctrl; 1111 case 0xdd4: /* SAU_TYPE */ 1112 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1113 goto bad_offset; 1114 } 1115 if (!attrs.secure) { 1116 return 0; 1117 } 1118 return cpu->sau_sregion; 1119 case 0xdd8: /* SAU_RNR */ 1120 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1121 goto bad_offset; 1122 } 1123 if (!attrs.secure) { 1124 return 0; 1125 } 1126 return cpu->env.sau.rnr; 1127 case 0xddc: /* SAU_RBAR */ 1128 { 1129 int region = cpu->env.sau.rnr; 1130 1131 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1132 goto bad_offset; 1133 } 1134 if (!attrs.secure) { 1135 return 0; 1136 } 1137 if (region >= cpu->sau_sregion) { 1138 return 0; 1139 } 1140 return cpu->env.sau.rbar[region]; 1141 } 1142 case 0xde0: /* SAU_RLAR */ 1143 { 1144 int region = cpu->env.sau.rnr; 1145 1146 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1147 goto bad_offset; 1148 } 1149 if (!attrs.secure) { 1150 return 0; 1151 } 1152 if (region >= cpu->sau_sregion) { 1153 return 0; 1154 } 1155 return cpu->env.sau.rlar[region]; 1156 } 1157 case 0xde4: /* SFSR */ 1158 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1159 goto bad_offset; 1160 } 1161 if (!attrs.secure) { 1162 return 0; 1163 } 1164 return cpu->env.v7m.sfsr; 1165 case 0xde8: /* SFAR */ 1166 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1167 goto bad_offset; 1168 } 1169 if (!attrs.secure) { 1170 return 0; 1171 } 1172 return cpu->env.v7m.sfar; 1173 default: 1174 bad_offset: 1175 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset); 1176 return 0; 1177 } 1178 } 1179 1180 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value, 1181 MemTxAttrs attrs) 1182 { 1183 ARMCPU *cpu = s->cpu; 1184 1185 switch (offset) { 1186 case 0xc: /* CPPWR */ 1187 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1188 goto bad_offset; 1189 } 1190 /* Make the IMPDEF choice to RAZ/WI this. */ 1191 break; 1192 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */ 1193 { 1194 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ; 1195 int i; 1196 1197 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1198 goto bad_offset; 1199 } 1200 if (!attrs.secure) { 1201 break; 1202 } 1203 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) { 1204 s->itns[startvec + i] = (value >> i) & 1; 1205 } 1206 nvic_irq_update(s); 1207 break; 1208 } 1209 case 0xd04: /* Interrupt Control State (ICSR) */ 1210 if (attrs.secure || cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1211 if (value & (1 << 31)) { 1212 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false); 1213 } else if (value & (1 << 30) && 1214 arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1215 /* PENDNMICLR didn't exist in v7M */ 1216 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false); 1217 } 1218 } 1219 if (value & (1 << 28)) { 1220 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure); 1221 } else if (value & (1 << 27)) { 1222 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure); 1223 } 1224 if (value & (1 << 26)) { 1225 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure); 1226 } else if (value & (1 << 25)) { 1227 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure); 1228 } 1229 break; 1230 case 0xd08: /* Vector Table Offset. */ 1231 cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80; 1232 break; 1233 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */ 1234 if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) { 1235 if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) { 1236 if (attrs.secure || 1237 !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) { 1238 qemu_irq_pulse(s->sysresetreq); 1239 } 1240 } 1241 if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) { 1242 qemu_log_mask(LOG_GUEST_ERROR, 1243 "Setting VECTCLRACTIVE when not in DEBUG mode " 1244 "is UNPREDICTABLE\n"); 1245 } 1246 if (value & R_V7M_AIRCR_VECTRESET_MASK) { 1247 /* NB: this bit is RES0 in v8M */ 1248 qemu_log_mask(LOG_GUEST_ERROR, 1249 "Setting VECTRESET when not in DEBUG mode " 1250 "is UNPREDICTABLE\n"); 1251 } 1252 s->prigroup[attrs.secure] = extract32(value, 1253 R_V7M_AIRCR_PRIGROUP_SHIFT, 1254 R_V7M_AIRCR_PRIGROUP_LENGTH); 1255 if (attrs.secure) { 1256 /* These bits are only writable by secure */ 1257 cpu->env.v7m.aircr = value & 1258 (R_V7M_AIRCR_SYSRESETREQS_MASK | 1259 R_V7M_AIRCR_BFHFNMINS_MASK | 1260 R_V7M_AIRCR_PRIS_MASK); 1261 /* BFHFNMINS changes the priority of Secure HardFault, and 1262 * allows a pending Non-secure HardFault to preempt (which 1263 * we implement by marking it enabled). 1264 */ 1265 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1266 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3; 1267 s->vectors[ARMV7M_EXCP_HARD].enabled = 1; 1268 } else { 1269 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1; 1270 s->vectors[ARMV7M_EXCP_HARD].enabled = 0; 1271 } 1272 } 1273 nvic_irq_update(s); 1274 } 1275 break; 1276 case 0xd10: /* System Control. */ 1277 /* TODO: Implement control registers. */ 1278 qemu_log_mask(LOG_UNIMP, "NVIC: SCR unimplemented\n"); 1279 break; 1280 case 0xd14: /* Configuration Control. */ 1281 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */ 1282 value &= (R_V7M_CCR_STKALIGN_MASK | 1283 R_V7M_CCR_BFHFNMIGN_MASK | 1284 R_V7M_CCR_DIV_0_TRP_MASK | 1285 R_V7M_CCR_UNALIGN_TRP_MASK | 1286 R_V7M_CCR_USERSETMPEND_MASK | 1287 R_V7M_CCR_NONBASETHRDENA_MASK); 1288 1289 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1290 /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */ 1291 value |= R_V7M_CCR_NONBASETHRDENA_MASK 1292 | R_V7M_CCR_STKALIGN_MASK; 1293 } 1294 if (attrs.secure) { 1295 /* the BFHFNMIGN bit is not banked; keep that in the NS copy */ 1296 cpu->env.v7m.ccr[M_REG_NS] = 1297 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK) 1298 | (value & R_V7M_CCR_BFHFNMIGN_MASK); 1299 value &= ~R_V7M_CCR_BFHFNMIGN_MASK; 1300 } 1301 1302 cpu->env.v7m.ccr[attrs.secure] = value; 1303 break; 1304 case 0xd24: /* System Handler Control and State (SHCSR) */ 1305 if (attrs.secure) { 1306 s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; 1307 /* Secure HardFault active bit cannot be written */ 1308 s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; 1309 s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; 1310 s->sec_vectors[ARMV7M_EXCP_PENDSV].active = 1311 (value & (1 << 10)) != 0; 1312 s->sec_vectors[ARMV7M_EXCP_SYSTICK].active = 1313 (value & (1 << 11)) != 0; 1314 s->sec_vectors[ARMV7M_EXCP_USAGE].pending = 1315 (value & (1 << 12)) != 0; 1316 s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; 1317 s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; 1318 s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; 1319 s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; 1320 s->sec_vectors[ARMV7M_EXCP_USAGE].enabled = 1321 (value & (1 << 18)) != 0; 1322 s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0; 1323 /* SecureFault not banked, but RAZ/WI to NS */ 1324 s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0; 1325 s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0; 1326 s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0; 1327 } else { 1328 s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; 1329 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1330 /* HARDFAULTPENDED is not present in v7M */ 1331 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0; 1332 } 1333 s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; 1334 s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; 1335 s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0; 1336 s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0; 1337 s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0; 1338 s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; 1339 s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; 1340 s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; 1341 s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0; 1342 } 1343 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1344 s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0; 1345 s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0; 1346 s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; 1347 } 1348 /* NMIACT can only be written if the write is of a zero, with 1349 * BFHFNMINS 1, and by the CPU in secure state via the NS alias. 1350 */ 1351 if (!attrs.secure && cpu->env.v7m.secure && 1352 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 1353 (value & (1 << 5)) == 0) { 1354 s->vectors[ARMV7M_EXCP_NMI].active = 0; 1355 } 1356 /* HARDFAULTACT can only be written if the write is of a zero 1357 * to the non-secure HardFault state by the CPU in secure state. 1358 * The only case where we can be targeting the non-secure HF state 1359 * when in secure state is if this is a write via the NS alias 1360 * and BFHFNMINS is 1. 1361 */ 1362 if (!attrs.secure && cpu->env.v7m.secure && 1363 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 1364 (value & (1 << 2)) == 0) { 1365 s->vectors[ARMV7M_EXCP_HARD].active = 0; 1366 } 1367 1368 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */ 1369 s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0; 1370 nvic_irq_update(s); 1371 break; 1372 case 0xd2c: /* Hard Fault Status. */ 1373 cpu->env.v7m.hfsr &= ~value; /* W1C */ 1374 break; 1375 case 0xd30: /* Debug Fault Status. */ 1376 cpu->env.v7m.dfsr &= ~value; /* W1C */ 1377 break; 1378 case 0xd34: /* Mem Manage Address. */ 1379 cpu->env.v7m.mmfar[attrs.secure] = value; 1380 return; 1381 case 0xd38: /* Bus Fault Address. */ 1382 cpu->env.v7m.bfar = value; 1383 return; 1384 case 0xd3c: /* Aux Fault Status. */ 1385 qemu_log_mask(LOG_UNIMP, 1386 "NVIC: Aux fault status registers unimplemented\n"); 1387 break; 1388 case 0xd90: /* MPU_TYPE */ 1389 return; /* RO */ 1390 case 0xd94: /* MPU_CTRL */ 1391 if ((value & 1392 (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK)) 1393 == R_V7M_MPU_CTRL_HFNMIENA_MASK) { 1394 qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is " 1395 "UNPREDICTABLE\n"); 1396 } 1397 cpu->env.v7m.mpu_ctrl[attrs.secure] 1398 = value & (R_V7M_MPU_CTRL_ENABLE_MASK | 1399 R_V7M_MPU_CTRL_HFNMIENA_MASK | 1400 R_V7M_MPU_CTRL_PRIVDEFENA_MASK); 1401 tlb_flush(CPU(cpu)); 1402 break; 1403 case 0xd98: /* MPU_RNR */ 1404 if (value >= cpu->pmsav7_dregion) { 1405 qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %" 1406 PRIu32 "/%" PRIu32 "\n", 1407 value, cpu->pmsav7_dregion); 1408 } else { 1409 cpu->env.pmsav7.rnr[attrs.secure] = value; 1410 } 1411 break; 1412 case 0xd9c: /* MPU_RBAR */ 1413 case 0xda4: /* MPU_RBAR_A1 */ 1414 case 0xdac: /* MPU_RBAR_A2 */ 1415 case 0xdb4: /* MPU_RBAR_A3 */ 1416 { 1417 int region; 1418 1419 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1420 /* PMSAv8M handling of the aliases is different from v7M: 1421 * aliases A1, A2, A3 override the low two bits of the region 1422 * number in MPU_RNR, and there is no 'region' field in the 1423 * RBAR register. 1424 */ 1425 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1426 1427 region = cpu->env.pmsav7.rnr[attrs.secure]; 1428 if (aliasno) { 1429 region = deposit32(region, 0, 2, aliasno); 1430 } 1431 if (region >= cpu->pmsav7_dregion) { 1432 return; 1433 } 1434 cpu->env.pmsav8.rbar[attrs.secure][region] = value; 1435 tlb_flush(CPU(cpu)); 1436 return; 1437 } 1438 1439 if (value & (1 << 4)) { 1440 /* VALID bit means use the region number specified in this 1441 * value and also update MPU_RNR.REGION with that value. 1442 */ 1443 region = extract32(value, 0, 4); 1444 if (region >= cpu->pmsav7_dregion) { 1445 qemu_log_mask(LOG_GUEST_ERROR, 1446 "MPU region out of range %u/%" PRIu32 "\n", 1447 region, cpu->pmsav7_dregion); 1448 return; 1449 } 1450 cpu->env.pmsav7.rnr[attrs.secure] = region; 1451 } else { 1452 region = cpu->env.pmsav7.rnr[attrs.secure]; 1453 } 1454 1455 if (region >= cpu->pmsav7_dregion) { 1456 return; 1457 } 1458 1459 cpu->env.pmsav7.drbar[region] = value & ~0x1f; 1460 tlb_flush(CPU(cpu)); 1461 break; 1462 } 1463 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */ 1464 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */ 1465 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */ 1466 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */ 1467 { 1468 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1469 1470 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1471 /* PMSAv8M handling of the aliases is different from v7M: 1472 * aliases A1, A2, A3 override the low two bits of the region 1473 * number in MPU_RNR. 1474 */ 1475 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1476 1477 region = cpu->env.pmsav7.rnr[attrs.secure]; 1478 if (aliasno) { 1479 region = deposit32(region, 0, 2, aliasno); 1480 } 1481 if (region >= cpu->pmsav7_dregion) { 1482 return; 1483 } 1484 cpu->env.pmsav8.rlar[attrs.secure][region] = value; 1485 tlb_flush(CPU(cpu)); 1486 return; 1487 } 1488 1489 if (region >= cpu->pmsav7_dregion) { 1490 return; 1491 } 1492 1493 cpu->env.pmsav7.drsr[region] = value & 0xff3f; 1494 cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f; 1495 tlb_flush(CPU(cpu)); 1496 break; 1497 } 1498 case 0xdc0: /* MPU_MAIR0 */ 1499 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1500 goto bad_offset; 1501 } 1502 if (cpu->pmsav7_dregion) { 1503 /* Register is RES0 if no MPU regions are implemented */ 1504 cpu->env.pmsav8.mair0[attrs.secure] = value; 1505 } 1506 /* We don't need to do anything else because memory attributes 1507 * only affect cacheability, and we don't implement caching. 1508 */ 1509 break; 1510 case 0xdc4: /* MPU_MAIR1 */ 1511 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1512 goto bad_offset; 1513 } 1514 if (cpu->pmsav7_dregion) { 1515 /* Register is RES0 if no MPU regions are implemented */ 1516 cpu->env.pmsav8.mair1[attrs.secure] = value; 1517 } 1518 /* We don't need to do anything else because memory attributes 1519 * only affect cacheability, and we don't implement caching. 1520 */ 1521 break; 1522 case 0xdd0: /* SAU_CTRL */ 1523 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1524 goto bad_offset; 1525 } 1526 if (!attrs.secure) { 1527 return; 1528 } 1529 cpu->env.sau.ctrl = value & 3; 1530 break; 1531 case 0xdd4: /* SAU_TYPE */ 1532 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1533 goto bad_offset; 1534 } 1535 break; 1536 case 0xdd8: /* SAU_RNR */ 1537 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1538 goto bad_offset; 1539 } 1540 if (!attrs.secure) { 1541 return; 1542 } 1543 if (value >= cpu->sau_sregion) { 1544 qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %" 1545 PRIu32 "/%" PRIu32 "\n", 1546 value, cpu->sau_sregion); 1547 } else { 1548 cpu->env.sau.rnr = value; 1549 } 1550 break; 1551 case 0xddc: /* SAU_RBAR */ 1552 { 1553 int region = cpu->env.sau.rnr; 1554 1555 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1556 goto bad_offset; 1557 } 1558 if (!attrs.secure) { 1559 return; 1560 } 1561 if (region >= cpu->sau_sregion) { 1562 return; 1563 } 1564 cpu->env.sau.rbar[region] = value & ~0x1f; 1565 tlb_flush(CPU(cpu)); 1566 break; 1567 } 1568 case 0xde0: /* SAU_RLAR */ 1569 { 1570 int region = cpu->env.sau.rnr; 1571 1572 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1573 goto bad_offset; 1574 } 1575 if (!attrs.secure) { 1576 return; 1577 } 1578 if (region >= cpu->sau_sregion) { 1579 return; 1580 } 1581 cpu->env.sau.rlar[region] = value & ~0x1c; 1582 tlb_flush(CPU(cpu)); 1583 break; 1584 } 1585 case 0xde4: /* SFSR */ 1586 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1587 goto bad_offset; 1588 } 1589 if (!attrs.secure) { 1590 return; 1591 } 1592 cpu->env.v7m.sfsr &= ~value; /* W1C */ 1593 break; 1594 case 0xde8: /* SFAR */ 1595 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1596 goto bad_offset; 1597 } 1598 if (!attrs.secure) { 1599 return; 1600 } 1601 cpu->env.v7m.sfsr = value; 1602 break; 1603 case 0xf00: /* Software Triggered Interrupt Register */ 1604 { 1605 int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ; 1606 if (excnum < s->num_irq) { 1607 armv7m_nvic_set_pending(s, excnum, false); 1608 } 1609 break; 1610 } 1611 case 0xf50: /* ICIALLU */ 1612 case 0xf58: /* ICIMVAU */ 1613 case 0xf5c: /* DCIMVAC */ 1614 case 0xf60: /* DCISW */ 1615 case 0xf64: /* DCCMVAU */ 1616 case 0xf68: /* DCCMVAC */ 1617 case 0xf6c: /* DCCSW */ 1618 case 0xf70: /* DCCIMVAC */ 1619 case 0xf74: /* DCCISW */ 1620 case 0xf78: /* BPIALL */ 1621 /* Cache and branch predictor maintenance: for QEMU these always NOP */ 1622 break; 1623 default: 1624 bad_offset: 1625 qemu_log_mask(LOG_GUEST_ERROR, 1626 "NVIC: Bad write offset 0x%x\n", offset); 1627 } 1628 } 1629 1630 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs) 1631 { 1632 /* Return true if unprivileged access to this register is permitted. */ 1633 switch (offset) { 1634 case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */ 1635 /* For access via STIR_NS it is the NS CCR.USERSETMPEND that 1636 * controls access even though the CPU is in Secure state (I_QDKX). 1637 */ 1638 return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK; 1639 default: 1640 /* All other user accesses cause a BusFault unconditionally */ 1641 return false; 1642 } 1643 } 1644 1645 static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs) 1646 { 1647 /* Behaviour for the SHPR register field for this exception: 1648 * return M_REG_NS to use the nonsecure vector (including for 1649 * non-banked exceptions), M_REG_S for the secure version of 1650 * a banked exception, and -1 if this field should RAZ/WI. 1651 */ 1652 switch (exc) { 1653 case ARMV7M_EXCP_MEM: 1654 case ARMV7M_EXCP_USAGE: 1655 case ARMV7M_EXCP_SVC: 1656 case ARMV7M_EXCP_PENDSV: 1657 case ARMV7M_EXCP_SYSTICK: 1658 /* Banked exceptions */ 1659 return attrs.secure; 1660 case ARMV7M_EXCP_BUS: 1661 /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */ 1662 if (!attrs.secure && 1663 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1664 return -1; 1665 } 1666 return M_REG_NS; 1667 case ARMV7M_EXCP_SECURE: 1668 /* Not banked, RAZ/WI from nonsecure */ 1669 if (!attrs.secure) { 1670 return -1; 1671 } 1672 return M_REG_NS; 1673 case ARMV7M_EXCP_DEBUG: 1674 /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */ 1675 return M_REG_NS; 1676 case 8 ... 10: 1677 case 13: 1678 /* RES0 */ 1679 return -1; 1680 default: 1681 /* Not reachable due to decode of SHPR register addresses */ 1682 g_assert_not_reached(); 1683 } 1684 } 1685 1686 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr, 1687 uint64_t *data, unsigned size, 1688 MemTxAttrs attrs) 1689 { 1690 NVICState *s = (NVICState *)opaque; 1691 uint32_t offset = addr; 1692 unsigned i, startvec, end; 1693 uint32_t val; 1694 1695 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) { 1696 /* Generate BusFault for unprivileged accesses */ 1697 return MEMTX_ERROR; 1698 } 1699 1700 switch (offset) { 1701 /* reads of set and clear both return the status */ 1702 case 0x100 ... 0x13f: /* NVIC Set enable */ 1703 offset += 0x80; 1704 /* fall through */ 1705 case 0x180 ... 0x1bf: /* NVIC Clear enable */ 1706 val = 0; 1707 startvec = offset - 0x180 + NVIC_FIRST_IRQ; /* vector # */ 1708 1709 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1710 if (s->vectors[startvec + i].enabled && 1711 (attrs.secure || s->itns[startvec + i])) { 1712 val |= (1 << i); 1713 } 1714 } 1715 break; 1716 case 0x200 ... 0x23f: /* NVIC Set pend */ 1717 offset += 0x80; 1718 /* fall through */ 1719 case 0x280 ... 0x2bf: /* NVIC Clear pend */ 1720 val = 0; 1721 startvec = offset - 0x280 + NVIC_FIRST_IRQ; /* vector # */ 1722 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1723 if (s->vectors[startvec + i].pending && 1724 (attrs.secure || s->itns[startvec + i])) { 1725 val |= (1 << i); 1726 } 1727 } 1728 break; 1729 case 0x300 ... 0x33f: /* NVIC Active */ 1730 val = 0; 1731 startvec = offset - 0x300 + NVIC_FIRST_IRQ; /* vector # */ 1732 1733 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1734 if (s->vectors[startvec + i].active && 1735 (attrs.secure || s->itns[startvec + i])) { 1736 val |= (1 << i); 1737 } 1738 } 1739 break; 1740 case 0x400 ... 0x5ef: /* NVIC Priority */ 1741 val = 0; 1742 startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */ 1743 1744 for (i = 0; i < size && startvec + i < s->num_irq; i++) { 1745 if (attrs.secure || s->itns[startvec + i]) { 1746 val |= s->vectors[startvec + i].prio << (8 * i); 1747 } 1748 } 1749 break; 1750 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */ 1751 val = 0; 1752 for (i = 0; i < size; i++) { 1753 unsigned hdlidx = (offset - 0xd14) + i; 1754 int sbank = shpr_bank(s, hdlidx, attrs); 1755 1756 if (sbank < 0) { 1757 continue; 1758 } 1759 val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank)); 1760 } 1761 break; 1762 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */ 1763 /* The BFSR bits [15:8] are shared between security states 1764 * and we store them in the NS copy 1765 */ 1766 val = s->cpu->env.v7m.cfsr[attrs.secure]; 1767 val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK; 1768 val = extract32(val, (offset - 0xd28) * 8, size * 8); 1769 break; 1770 case 0xfe0 ... 0xfff: /* ID. */ 1771 if (offset & 3) { 1772 val = 0; 1773 } else { 1774 val = nvic_id[(offset - 0xfe0) >> 2]; 1775 } 1776 break; 1777 default: 1778 if (size == 4) { 1779 val = nvic_readl(s, offset, attrs); 1780 } else { 1781 qemu_log_mask(LOG_GUEST_ERROR, 1782 "NVIC: Bad read of size %d at offset 0x%x\n", 1783 size, offset); 1784 val = 0; 1785 } 1786 } 1787 1788 trace_nvic_sysreg_read(addr, val, size); 1789 *data = val; 1790 return MEMTX_OK; 1791 } 1792 1793 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr, 1794 uint64_t value, unsigned size, 1795 MemTxAttrs attrs) 1796 { 1797 NVICState *s = (NVICState *)opaque; 1798 uint32_t offset = addr; 1799 unsigned i, startvec, end; 1800 unsigned setval = 0; 1801 1802 trace_nvic_sysreg_write(addr, value, size); 1803 1804 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) { 1805 /* Generate BusFault for unprivileged accesses */ 1806 return MEMTX_ERROR; 1807 } 1808 1809 switch (offset) { 1810 case 0x100 ... 0x13f: /* NVIC Set enable */ 1811 offset += 0x80; 1812 setval = 1; 1813 /* fall through */ 1814 case 0x180 ... 0x1bf: /* NVIC Clear enable */ 1815 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; 1816 1817 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1818 if (value & (1 << i) && 1819 (attrs.secure || s->itns[startvec + i])) { 1820 s->vectors[startvec + i].enabled = setval; 1821 } 1822 } 1823 nvic_irq_update(s); 1824 return MEMTX_OK; 1825 case 0x200 ... 0x23f: /* NVIC Set pend */ 1826 /* the special logic in armv7m_nvic_set_pending() 1827 * is not needed since IRQs are never escalated 1828 */ 1829 offset += 0x80; 1830 setval = 1; 1831 /* fall through */ 1832 case 0x280 ... 0x2bf: /* NVIC Clear pend */ 1833 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */ 1834 1835 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1836 if (value & (1 << i) && 1837 (attrs.secure || s->itns[startvec + i])) { 1838 s->vectors[startvec + i].pending = setval; 1839 } 1840 } 1841 nvic_irq_update(s); 1842 return MEMTX_OK; 1843 case 0x300 ... 0x33f: /* NVIC Active */ 1844 return MEMTX_OK; /* R/O */ 1845 case 0x400 ... 0x5ef: /* NVIC Priority */ 1846 startvec = 8 * (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */ 1847 1848 for (i = 0; i < size && startvec + i < s->num_irq; i++) { 1849 if (attrs.secure || s->itns[startvec + i]) { 1850 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff); 1851 } 1852 } 1853 nvic_irq_update(s); 1854 return MEMTX_OK; 1855 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */ 1856 for (i = 0; i < size; i++) { 1857 unsigned hdlidx = (offset - 0xd14) + i; 1858 int newprio = extract32(value, i * 8, 8); 1859 int sbank = shpr_bank(s, hdlidx, attrs); 1860 1861 if (sbank < 0) { 1862 continue; 1863 } 1864 set_prio(s, hdlidx, sbank, newprio); 1865 } 1866 nvic_irq_update(s); 1867 return MEMTX_OK; 1868 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */ 1869 /* All bits are W1C, so construct 32 bit value with 0s in 1870 * the parts not written by the access size 1871 */ 1872 value <<= ((offset - 0xd28) * 8); 1873 1874 s->cpu->env.v7m.cfsr[attrs.secure] &= ~value; 1875 if (attrs.secure) { 1876 /* The BFSR bits [15:8] are shared between security states 1877 * and we store them in the NS copy. 1878 */ 1879 s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK); 1880 } 1881 return MEMTX_OK; 1882 } 1883 if (size == 4) { 1884 nvic_writel(s, offset, value, attrs); 1885 return MEMTX_OK; 1886 } 1887 qemu_log_mask(LOG_GUEST_ERROR, 1888 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset); 1889 /* This is UNPREDICTABLE; treat as RAZ/WI */ 1890 return MEMTX_OK; 1891 } 1892 1893 static const MemoryRegionOps nvic_sysreg_ops = { 1894 .read_with_attrs = nvic_sysreg_read, 1895 .write_with_attrs = nvic_sysreg_write, 1896 .endianness = DEVICE_NATIVE_ENDIAN, 1897 }; 1898 1899 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr, 1900 uint64_t value, unsigned size, 1901 MemTxAttrs attrs) 1902 { 1903 MemoryRegion *mr = opaque; 1904 1905 if (attrs.secure) { 1906 /* S accesses to the alias act like NS accesses to the real region */ 1907 attrs.secure = 0; 1908 return memory_region_dispatch_write(mr, addr, value, size, attrs); 1909 } else { 1910 /* NS attrs are RAZ/WI for privileged, and BusFault for user */ 1911 if (attrs.user) { 1912 return MEMTX_ERROR; 1913 } 1914 return MEMTX_OK; 1915 } 1916 } 1917 1918 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr, 1919 uint64_t *data, unsigned size, 1920 MemTxAttrs attrs) 1921 { 1922 MemoryRegion *mr = opaque; 1923 1924 if (attrs.secure) { 1925 /* S accesses to the alias act like NS accesses to the real region */ 1926 attrs.secure = 0; 1927 return memory_region_dispatch_read(mr, addr, data, size, attrs); 1928 } else { 1929 /* NS attrs are RAZ/WI for privileged, and BusFault for user */ 1930 if (attrs.user) { 1931 return MEMTX_ERROR; 1932 } 1933 *data = 0; 1934 return MEMTX_OK; 1935 } 1936 } 1937 1938 static const MemoryRegionOps nvic_sysreg_ns_ops = { 1939 .read_with_attrs = nvic_sysreg_ns_read, 1940 .write_with_attrs = nvic_sysreg_ns_write, 1941 .endianness = DEVICE_NATIVE_ENDIAN, 1942 }; 1943 1944 static MemTxResult nvic_systick_write(void *opaque, hwaddr addr, 1945 uint64_t value, unsigned size, 1946 MemTxAttrs attrs) 1947 { 1948 NVICState *s = opaque; 1949 MemoryRegion *mr; 1950 1951 /* Direct the access to the correct systick */ 1952 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0); 1953 return memory_region_dispatch_write(mr, addr, value, size, attrs); 1954 } 1955 1956 static MemTxResult nvic_systick_read(void *opaque, hwaddr addr, 1957 uint64_t *data, unsigned size, 1958 MemTxAttrs attrs) 1959 { 1960 NVICState *s = opaque; 1961 MemoryRegion *mr; 1962 1963 /* Direct the access to the correct systick */ 1964 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0); 1965 return memory_region_dispatch_read(mr, addr, data, size, attrs); 1966 } 1967 1968 static const MemoryRegionOps nvic_systick_ops = { 1969 .read_with_attrs = nvic_systick_read, 1970 .write_with_attrs = nvic_systick_write, 1971 .endianness = DEVICE_NATIVE_ENDIAN, 1972 }; 1973 1974 static int nvic_post_load(void *opaque, int version_id) 1975 { 1976 NVICState *s = opaque; 1977 unsigned i; 1978 int resetprio; 1979 1980 /* Check for out of range priority settings */ 1981 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3; 1982 1983 if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio || 1984 s->vectors[ARMV7M_EXCP_NMI].prio != -2 || 1985 s->vectors[ARMV7M_EXCP_HARD].prio != -1) { 1986 return 1; 1987 } 1988 for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) { 1989 if (s->vectors[i].prio & ~0xff) { 1990 return 1; 1991 } 1992 } 1993 1994 nvic_recompute_state(s); 1995 1996 return 0; 1997 } 1998 1999 static const VMStateDescription vmstate_VecInfo = { 2000 .name = "armv7m_nvic_info", 2001 .version_id = 1, 2002 .minimum_version_id = 1, 2003 .fields = (VMStateField[]) { 2004 VMSTATE_INT16(prio, VecInfo), 2005 VMSTATE_UINT8(enabled, VecInfo), 2006 VMSTATE_UINT8(pending, VecInfo), 2007 VMSTATE_UINT8(active, VecInfo), 2008 VMSTATE_UINT8(level, VecInfo), 2009 VMSTATE_END_OF_LIST() 2010 } 2011 }; 2012 2013 static bool nvic_security_needed(void *opaque) 2014 { 2015 NVICState *s = opaque; 2016 2017 return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY); 2018 } 2019 2020 static int nvic_security_post_load(void *opaque, int version_id) 2021 { 2022 NVICState *s = opaque; 2023 int i; 2024 2025 /* Check for out of range priority settings */ 2026 if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1 2027 && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) { 2028 /* We can't cross-check against AIRCR.BFHFNMINS as we don't know 2029 * if the CPU state has been migrated yet; a mismatch won't 2030 * cause the emulation to blow up, though. 2031 */ 2032 return 1; 2033 } 2034 for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) { 2035 if (s->sec_vectors[i].prio & ~0xff) { 2036 return 1; 2037 } 2038 } 2039 return 0; 2040 } 2041 2042 static const VMStateDescription vmstate_nvic_security = { 2043 .name = "nvic/m-security", 2044 .version_id = 1, 2045 .minimum_version_id = 1, 2046 .needed = nvic_security_needed, 2047 .post_load = &nvic_security_post_load, 2048 .fields = (VMStateField[]) { 2049 VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1, 2050 vmstate_VecInfo, VecInfo), 2051 VMSTATE_UINT32(prigroup[M_REG_S], NVICState), 2052 VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS), 2053 VMSTATE_END_OF_LIST() 2054 } 2055 }; 2056 2057 static const VMStateDescription vmstate_nvic = { 2058 .name = "armv7m_nvic", 2059 .version_id = 4, 2060 .minimum_version_id = 4, 2061 .post_load = &nvic_post_load, 2062 .fields = (VMStateField[]) { 2063 VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1, 2064 vmstate_VecInfo, VecInfo), 2065 VMSTATE_UINT32(prigroup[M_REG_NS], NVICState), 2066 VMSTATE_END_OF_LIST() 2067 }, 2068 .subsections = (const VMStateDescription*[]) { 2069 &vmstate_nvic_security, 2070 NULL 2071 } 2072 }; 2073 2074 static Property props_nvic[] = { 2075 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */ 2076 DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64), 2077 DEFINE_PROP_END_OF_LIST() 2078 }; 2079 2080 static void armv7m_nvic_reset(DeviceState *dev) 2081 { 2082 int resetprio; 2083 NVICState *s = NVIC(dev); 2084 2085 memset(s->vectors, 0, sizeof(s->vectors)); 2086 memset(s->sec_vectors, 0, sizeof(s->sec_vectors)); 2087 s->prigroup[M_REG_NS] = 0; 2088 s->prigroup[M_REG_S] = 0; 2089 2090 s->vectors[ARMV7M_EXCP_NMI].enabled = 1; 2091 /* MEM, BUS, and USAGE are enabled through 2092 * the System Handler Control register 2093 */ 2094 s->vectors[ARMV7M_EXCP_SVC].enabled = 1; 2095 s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1; 2096 s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1; 2097 s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; 2098 2099 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3; 2100 s->vectors[ARMV7M_EXCP_RESET].prio = resetprio; 2101 s->vectors[ARMV7M_EXCP_NMI].prio = -2; 2102 s->vectors[ARMV7M_EXCP_HARD].prio = -1; 2103 2104 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2105 s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1; 2106 s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1; 2107 s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1; 2108 s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; 2109 2110 /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */ 2111 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1; 2112 /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */ 2113 s->vectors[ARMV7M_EXCP_HARD].enabled = 0; 2114 } else { 2115 s->vectors[ARMV7M_EXCP_HARD].enabled = 1; 2116 } 2117 2118 /* Strictly speaking the reset handler should be enabled. 2119 * However, we don't simulate soft resets through the NVIC, 2120 * and the reset vector should never be pended. 2121 * So we leave it disabled to catch logic errors. 2122 */ 2123 2124 s->exception_prio = NVIC_NOEXC_PRIO; 2125 s->vectpending = 0; 2126 s->vectpending_is_s_banked = false; 2127 s->vectpending_prio = NVIC_NOEXC_PRIO; 2128 2129 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2130 memset(s->itns, 0, sizeof(s->itns)); 2131 } else { 2132 /* This state is constant and not guest accessible in a non-security 2133 * NVIC; we set the bits to true to avoid having to do a feature 2134 * bit check in the NVIC enable/pend/etc register accessors. 2135 */ 2136 int i; 2137 2138 for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) { 2139 s->itns[i] = true; 2140 } 2141 } 2142 } 2143 2144 static void nvic_systick_trigger(void *opaque, int n, int level) 2145 { 2146 NVICState *s = opaque; 2147 2148 if (level) { 2149 /* SysTick just asked us to pend its exception. 2150 * (This is different from an external interrupt line's 2151 * behaviour.) 2152 * n == 0 : NonSecure systick 2153 * n == 1 : Secure systick 2154 */ 2155 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n); 2156 } 2157 } 2158 2159 static void armv7m_nvic_realize(DeviceState *dev, Error **errp) 2160 { 2161 NVICState *s = NVIC(dev); 2162 Error *err = NULL; 2163 int regionlen; 2164 2165 s->cpu = ARM_CPU(qemu_get_cpu(0)); 2166 assert(s->cpu); 2167 2168 if (s->num_irq > NVIC_MAX_IRQ) { 2169 error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq); 2170 return; 2171 } 2172 2173 qdev_init_gpio_in(dev, set_irq_level, s->num_irq); 2174 2175 /* include space for internal exception vectors */ 2176 s->num_irq += NVIC_FIRST_IRQ; 2177 2178 object_property_set_bool(OBJECT(&s->systick[M_REG_NS]), true, 2179 "realized", &err); 2180 if (err != NULL) { 2181 error_propagate(errp, err); 2182 return; 2183 } 2184 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0, 2185 qdev_get_gpio_in_named(dev, "systick-trigger", 2186 M_REG_NS)); 2187 2188 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2189 /* We couldn't init the secure systick device in instance_init 2190 * as we didn't know then if the CPU had the security extensions; 2191 * so we have to do it here. 2192 */ 2193 object_initialize(&s->systick[M_REG_S], sizeof(s->systick[M_REG_S]), 2194 TYPE_SYSTICK); 2195 qdev_set_parent_bus(DEVICE(&s->systick[M_REG_S]), sysbus_get_default()); 2196 2197 object_property_set_bool(OBJECT(&s->systick[M_REG_S]), true, 2198 "realized", &err); 2199 if (err != NULL) { 2200 error_propagate(errp, err); 2201 return; 2202 } 2203 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0, 2204 qdev_get_gpio_in_named(dev, "systick-trigger", 2205 M_REG_S)); 2206 } 2207 2208 /* The NVIC and System Control Space (SCS) starts at 0xe000e000 2209 * and looks like this: 2210 * 0x004 - ICTR 2211 * 0x010 - 0xff - systick 2212 * 0x100..0x7ec - NVIC 2213 * 0x7f0..0xcff - Reserved 2214 * 0xd00..0xd3c - SCS registers 2215 * 0xd40..0xeff - Reserved or Not implemented 2216 * 0xf00 - STIR 2217 * 2218 * Some registers within this space are banked between security states. 2219 * In v8M there is a second range 0xe002e000..0xe002efff which is the 2220 * NonSecure alias SCS; secure accesses to this behave like NS accesses 2221 * to the main SCS range, and non-secure accesses (including when 2222 * the security extension is not implemented) are RAZ/WI. 2223 * Note that both the main SCS range and the alias range are defined 2224 * to be exempt from memory attribution (R_BLJT) and so the memory 2225 * transaction attribute always matches the current CPU security 2226 * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops 2227 * wrappers we change attrs.secure to indicate the NS access; so 2228 * generally code determining which banked register to use should 2229 * use attrs.secure; code determining actual behaviour of the system 2230 * should use env->v7m.secure. 2231 */ 2232 regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000; 2233 memory_region_init(&s->container, OBJECT(s), "nvic", regionlen); 2234 /* The system register region goes at the bottom of the priority 2235 * stack as it covers the whole page. 2236 */ 2237 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s, 2238 "nvic_sysregs", 0x1000); 2239 memory_region_add_subregion(&s->container, 0, &s->sysregmem); 2240 2241 memory_region_init_io(&s->systickmem, OBJECT(s), 2242 &nvic_systick_ops, s, 2243 "nvic_systick", 0xe0); 2244 2245 memory_region_add_subregion_overlap(&s->container, 0x10, 2246 &s->systickmem, 1); 2247 2248 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) { 2249 memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s), 2250 &nvic_sysreg_ns_ops, &s->sysregmem, 2251 "nvic_sysregs_ns", 0x1000); 2252 memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem); 2253 memory_region_init_io(&s->systick_ns_mem, OBJECT(s), 2254 &nvic_sysreg_ns_ops, &s->systickmem, 2255 "nvic_systick_ns", 0xe0); 2256 memory_region_add_subregion_overlap(&s->container, 0x20010, 2257 &s->systick_ns_mem, 1); 2258 } 2259 2260 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container); 2261 } 2262 2263 static void armv7m_nvic_instance_init(Object *obj) 2264 { 2265 /* We have a different default value for the num-irq property 2266 * than our superclass. This function runs after qdev init 2267 * has set the defaults from the Property array and before 2268 * any user-specified property setting, so just modify the 2269 * value in the GICState struct. 2270 */ 2271 DeviceState *dev = DEVICE(obj); 2272 NVICState *nvic = NVIC(obj); 2273 SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 2274 2275 object_initialize(&nvic->systick[M_REG_NS], 2276 sizeof(nvic->systick[M_REG_NS]), TYPE_SYSTICK); 2277 qdev_set_parent_bus(DEVICE(&nvic->systick[M_REG_NS]), sysbus_get_default()); 2278 /* We can't initialize the secure systick here, as we don't know 2279 * yet if we need it. 2280 */ 2281 2282 sysbus_init_irq(sbd, &nvic->excpout); 2283 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1); 2284 qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger", 2285 M_REG_NUM_BANKS); 2286 } 2287 2288 static void armv7m_nvic_class_init(ObjectClass *klass, void *data) 2289 { 2290 DeviceClass *dc = DEVICE_CLASS(klass); 2291 2292 dc->vmsd = &vmstate_nvic; 2293 dc->props = props_nvic; 2294 dc->reset = armv7m_nvic_reset; 2295 dc->realize = armv7m_nvic_realize; 2296 } 2297 2298 static const TypeInfo armv7m_nvic_info = { 2299 .name = TYPE_NVIC, 2300 .parent = TYPE_SYS_BUS_DEVICE, 2301 .instance_init = armv7m_nvic_instance_init, 2302 .instance_size = sizeof(NVICState), 2303 .class_init = armv7m_nvic_class_init, 2304 .class_size = sizeof(SysBusDeviceClass), 2305 }; 2306 2307 static void armv7m_nvic_register_types(void) 2308 { 2309 type_register_static(&armv7m_nvic_info); 2310 } 2311 2312 type_init(armv7m_nvic_register_types) 2313