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 0x380 ... 0x3bf: /* NVIC_ITNS<n> */ 780 { 781 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ; 782 int i; 783 784 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 785 goto bad_offset; 786 } 787 if (!attrs.secure) { 788 return 0; 789 } 790 val = 0; 791 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) { 792 if (s->itns[startvec + i]) { 793 val |= (1 << i); 794 } 795 } 796 return val; 797 } 798 case 0xd00: /* CPUID Base. */ 799 return cpu->midr; 800 case 0xd04: /* Interrupt Control State (ICSR) */ 801 /* VECTACTIVE */ 802 val = cpu->env.v7m.exception; 803 /* VECTPENDING */ 804 val |= (s->vectpending & 0xff) << 12; 805 /* ISRPENDING - set if any external IRQ is pending */ 806 if (nvic_isrpending(s)) { 807 val |= (1 << 22); 808 } 809 /* RETTOBASE - set if only one handler is active */ 810 if (nvic_rettobase(s)) { 811 val |= (1 << 11); 812 } 813 if (attrs.secure) { 814 /* PENDSTSET */ 815 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) { 816 val |= (1 << 26); 817 } 818 /* PENDSVSET */ 819 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) { 820 val |= (1 << 28); 821 } 822 } else { 823 /* PENDSTSET */ 824 if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) { 825 val |= (1 << 26); 826 } 827 /* PENDSVSET */ 828 if (s->vectors[ARMV7M_EXCP_PENDSV].pending) { 829 val |= (1 << 28); 830 } 831 } 832 /* NMIPENDSET */ 833 if ((cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 834 s->vectors[ARMV7M_EXCP_NMI].pending) { 835 val |= (1 << 31); 836 } 837 /* ISRPREEMPT: RES0 when halting debug not implemented */ 838 /* STTNS: RES0 for the Main Extension */ 839 return val; 840 case 0xd08: /* Vector Table Offset. */ 841 return cpu->env.v7m.vecbase[attrs.secure]; 842 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */ 843 val = 0xfa050000 | (s->prigroup[attrs.secure] << 8); 844 if (attrs.secure) { 845 /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */ 846 val |= cpu->env.v7m.aircr; 847 } else { 848 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 849 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If 850 * security isn't supported then BFHFNMINS is RAO (and 851 * the bit in env.v7m.aircr is always set). 852 */ 853 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK; 854 } 855 } 856 return val; 857 case 0xd10: /* System Control. */ 858 /* TODO: Implement SLEEPONEXIT. */ 859 return 0; 860 case 0xd14: /* Configuration Control. */ 861 /* The BFHFNMIGN bit is the only non-banked bit; we 862 * keep it in the non-secure copy of the register. 863 */ 864 val = cpu->env.v7m.ccr[attrs.secure]; 865 val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK; 866 return val; 867 case 0xd24: /* System Handler Control and State (SHCSR) */ 868 val = 0; 869 if (attrs.secure) { 870 if (s->sec_vectors[ARMV7M_EXCP_MEM].active) { 871 val |= (1 << 0); 872 } 873 if (s->sec_vectors[ARMV7M_EXCP_HARD].active) { 874 val |= (1 << 2); 875 } 876 if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) { 877 val |= (1 << 3); 878 } 879 if (s->sec_vectors[ARMV7M_EXCP_SVC].active) { 880 val |= (1 << 7); 881 } 882 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) { 883 val |= (1 << 10); 884 } 885 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) { 886 val |= (1 << 11); 887 } 888 if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) { 889 val |= (1 << 12); 890 } 891 if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) { 892 val |= (1 << 13); 893 } 894 if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) { 895 val |= (1 << 15); 896 } 897 if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) { 898 val |= (1 << 16); 899 } 900 if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) { 901 val |= (1 << 18); 902 } 903 if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) { 904 val |= (1 << 21); 905 } 906 /* SecureFault is not banked but is always RAZ/WI to NS */ 907 if (s->vectors[ARMV7M_EXCP_SECURE].active) { 908 val |= (1 << 4); 909 } 910 if (s->vectors[ARMV7M_EXCP_SECURE].enabled) { 911 val |= (1 << 19); 912 } 913 if (s->vectors[ARMV7M_EXCP_SECURE].pending) { 914 val |= (1 << 20); 915 } 916 } else { 917 if (s->vectors[ARMV7M_EXCP_MEM].active) { 918 val |= (1 << 0); 919 } 920 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 921 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */ 922 if (s->vectors[ARMV7M_EXCP_HARD].active) { 923 val |= (1 << 2); 924 } 925 if (s->vectors[ARMV7M_EXCP_HARD].pending) { 926 val |= (1 << 21); 927 } 928 } 929 if (s->vectors[ARMV7M_EXCP_USAGE].active) { 930 val |= (1 << 3); 931 } 932 if (s->vectors[ARMV7M_EXCP_SVC].active) { 933 val |= (1 << 7); 934 } 935 if (s->vectors[ARMV7M_EXCP_PENDSV].active) { 936 val |= (1 << 10); 937 } 938 if (s->vectors[ARMV7M_EXCP_SYSTICK].active) { 939 val |= (1 << 11); 940 } 941 if (s->vectors[ARMV7M_EXCP_USAGE].pending) { 942 val |= (1 << 12); 943 } 944 if (s->vectors[ARMV7M_EXCP_MEM].pending) { 945 val |= (1 << 13); 946 } 947 if (s->vectors[ARMV7M_EXCP_SVC].pending) { 948 val |= (1 << 15); 949 } 950 if (s->vectors[ARMV7M_EXCP_MEM].enabled) { 951 val |= (1 << 16); 952 } 953 if (s->vectors[ARMV7M_EXCP_USAGE].enabled) { 954 val |= (1 << 18); 955 } 956 } 957 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 958 if (s->vectors[ARMV7M_EXCP_BUS].active) { 959 val |= (1 << 1); 960 } 961 if (s->vectors[ARMV7M_EXCP_BUS].pending) { 962 val |= (1 << 14); 963 } 964 if (s->vectors[ARMV7M_EXCP_BUS].enabled) { 965 val |= (1 << 17); 966 } 967 if (arm_feature(&cpu->env, ARM_FEATURE_V8) && 968 s->vectors[ARMV7M_EXCP_NMI].active) { 969 /* NMIACT is not present in v7M */ 970 val |= (1 << 5); 971 } 972 } 973 974 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */ 975 if (s->vectors[ARMV7M_EXCP_DEBUG].active) { 976 val |= (1 << 8); 977 } 978 return val; 979 case 0xd2c: /* Hard Fault Status. */ 980 return cpu->env.v7m.hfsr; 981 case 0xd30: /* Debug Fault Status. */ 982 return cpu->env.v7m.dfsr; 983 case 0xd34: /* MMFAR MemManage Fault Address */ 984 return cpu->env.v7m.mmfar[attrs.secure]; 985 case 0xd38: /* Bus Fault Address. */ 986 return cpu->env.v7m.bfar; 987 case 0xd3c: /* Aux Fault Status. */ 988 /* TODO: Implement fault status registers. */ 989 qemu_log_mask(LOG_UNIMP, 990 "Aux Fault status registers unimplemented\n"); 991 return 0; 992 case 0xd40: /* PFR0. */ 993 return 0x00000030; 994 case 0xd44: /* PRF1. */ 995 return 0x00000200; 996 case 0xd48: /* DFR0. */ 997 return 0x00100000; 998 case 0xd4c: /* AFR0. */ 999 return 0x00000000; 1000 case 0xd50: /* MMFR0. */ 1001 return 0x00000030; 1002 case 0xd54: /* MMFR1. */ 1003 return 0x00000000; 1004 case 0xd58: /* MMFR2. */ 1005 return 0x00000000; 1006 case 0xd5c: /* MMFR3. */ 1007 return 0x00000000; 1008 case 0xd60: /* ISAR0. */ 1009 return 0x01141110; 1010 case 0xd64: /* ISAR1. */ 1011 return 0x02111000; 1012 case 0xd68: /* ISAR2. */ 1013 return 0x21112231; 1014 case 0xd6c: /* ISAR3. */ 1015 return 0x01111110; 1016 case 0xd70: /* ISAR4. */ 1017 return 0x01310102; 1018 /* TODO: Implement debug registers. */ 1019 case 0xd90: /* MPU_TYPE */ 1020 /* Unified MPU; if the MPU is not present this value is zero */ 1021 return cpu->pmsav7_dregion << 8; 1022 break; 1023 case 0xd94: /* MPU_CTRL */ 1024 return cpu->env.v7m.mpu_ctrl[attrs.secure]; 1025 case 0xd98: /* MPU_RNR */ 1026 return cpu->env.pmsav7.rnr[attrs.secure]; 1027 case 0xd9c: /* MPU_RBAR */ 1028 case 0xda4: /* MPU_RBAR_A1 */ 1029 case 0xdac: /* MPU_RBAR_A2 */ 1030 case 0xdb4: /* MPU_RBAR_A3 */ 1031 { 1032 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1033 1034 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1035 /* PMSAv8M handling of the aliases is different from v7M: 1036 * aliases A1, A2, A3 override the low two bits of the region 1037 * number in MPU_RNR, and there is no 'region' field in the 1038 * RBAR register. 1039 */ 1040 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1041 if (aliasno) { 1042 region = deposit32(region, 0, 2, aliasno); 1043 } 1044 if (region >= cpu->pmsav7_dregion) { 1045 return 0; 1046 } 1047 return cpu->env.pmsav8.rbar[attrs.secure][region]; 1048 } 1049 1050 if (region >= cpu->pmsav7_dregion) { 1051 return 0; 1052 } 1053 return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf); 1054 } 1055 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */ 1056 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */ 1057 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */ 1058 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */ 1059 { 1060 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1061 1062 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1063 /* PMSAv8M handling of the aliases is different from v7M: 1064 * aliases A1, A2, A3 override the low two bits of the region 1065 * number in MPU_RNR. 1066 */ 1067 int aliasno = (offset - 0xda0) / 8; /* 0..3 */ 1068 if (aliasno) { 1069 region = deposit32(region, 0, 2, aliasno); 1070 } 1071 if (region >= cpu->pmsav7_dregion) { 1072 return 0; 1073 } 1074 return cpu->env.pmsav8.rlar[attrs.secure][region]; 1075 } 1076 1077 if (region >= cpu->pmsav7_dregion) { 1078 return 0; 1079 } 1080 return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) | 1081 (cpu->env.pmsav7.drsr[region] & 0xffff); 1082 } 1083 case 0xdc0: /* MPU_MAIR0 */ 1084 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1085 goto bad_offset; 1086 } 1087 return cpu->env.pmsav8.mair0[attrs.secure]; 1088 case 0xdc4: /* MPU_MAIR1 */ 1089 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1090 goto bad_offset; 1091 } 1092 return cpu->env.pmsav8.mair1[attrs.secure]; 1093 case 0xdd0: /* SAU_CTRL */ 1094 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1095 goto bad_offset; 1096 } 1097 if (!attrs.secure) { 1098 return 0; 1099 } 1100 return cpu->env.sau.ctrl; 1101 case 0xdd4: /* SAU_TYPE */ 1102 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1103 goto bad_offset; 1104 } 1105 if (!attrs.secure) { 1106 return 0; 1107 } 1108 return cpu->sau_sregion; 1109 case 0xdd8: /* SAU_RNR */ 1110 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1111 goto bad_offset; 1112 } 1113 if (!attrs.secure) { 1114 return 0; 1115 } 1116 return cpu->env.sau.rnr; 1117 case 0xddc: /* SAU_RBAR */ 1118 { 1119 int region = cpu->env.sau.rnr; 1120 1121 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1122 goto bad_offset; 1123 } 1124 if (!attrs.secure) { 1125 return 0; 1126 } 1127 if (region >= cpu->sau_sregion) { 1128 return 0; 1129 } 1130 return cpu->env.sau.rbar[region]; 1131 } 1132 case 0xde0: /* SAU_RLAR */ 1133 { 1134 int region = cpu->env.sau.rnr; 1135 1136 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1137 goto bad_offset; 1138 } 1139 if (!attrs.secure) { 1140 return 0; 1141 } 1142 if (region >= cpu->sau_sregion) { 1143 return 0; 1144 } 1145 return cpu->env.sau.rlar[region]; 1146 } 1147 case 0xde4: /* SFSR */ 1148 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1149 goto bad_offset; 1150 } 1151 if (!attrs.secure) { 1152 return 0; 1153 } 1154 return cpu->env.v7m.sfsr; 1155 case 0xde8: /* SFAR */ 1156 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1157 goto bad_offset; 1158 } 1159 if (!attrs.secure) { 1160 return 0; 1161 } 1162 return cpu->env.v7m.sfar; 1163 default: 1164 bad_offset: 1165 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset); 1166 return 0; 1167 } 1168 } 1169 1170 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value, 1171 MemTxAttrs attrs) 1172 { 1173 ARMCPU *cpu = s->cpu; 1174 1175 switch (offset) { 1176 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */ 1177 { 1178 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ; 1179 int i; 1180 1181 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1182 goto bad_offset; 1183 } 1184 if (!attrs.secure) { 1185 break; 1186 } 1187 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) { 1188 s->itns[startvec + i] = (value >> i) & 1; 1189 } 1190 nvic_irq_update(s); 1191 break; 1192 } 1193 case 0xd04: /* Interrupt Control State (ICSR) */ 1194 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1195 if (value & (1 << 31)) { 1196 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false); 1197 } else if (value & (1 << 30) && 1198 arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1199 /* PENDNMICLR didn't exist in v7M */ 1200 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false); 1201 } 1202 } 1203 if (value & (1 << 28)) { 1204 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure); 1205 } else if (value & (1 << 27)) { 1206 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure); 1207 } 1208 if (value & (1 << 26)) { 1209 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure); 1210 } else if (value & (1 << 25)) { 1211 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure); 1212 } 1213 break; 1214 case 0xd08: /* Vector Table Offset. */ 1215 cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80; 1216 break; 1217 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */ 1218 if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) { 1219 if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) { 1220 if (attrs.secure || 1221 !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) { 1222 qemu_irq_pulse(s->sysresetreq); 1223 } 1224 } 1225 if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) { 1226 qemu_log_mask(LOG_GUEST_ERROR, 1227 "Setting VECTCLRACTIVE when not in DEBUG mode " 1228 "is UNPREDICTABLE\n"); 1229 } 1230 if (value & R_V7M_AIRCR_VECTRESET_MASK) { 1231 /* NB: this bit is RES0 in v8M */ 1232 qemu_log_mask(LOG_GUEST_ERROR, 1233 "Setting VECTRESET when not in DEBUG mode " 1234 "is UNPREDICTABLE\n"); 1235 } 1236 s->prigroup[attrs.secure] = extract32(value, 1237 R_V7M_AIRCR_PRIGROUP_SHIFT, 1238 R_V7M_AIRCR_PRIGROUP_LENGTH); 1239 if (attrs.secure) { 1240 /* These bits are only writable by secure */ 1241 cpu->env.v7m.aircr = value & 1242 (R_V7M_AIRCR_SYSRESETREQS_MASK | 1243 R_V7M_AIRCR_BFHFNMINS_MASK | 1244 R_V7M_AIRCR_PRIS_MASK); 1245 /* BFHFNMINS changes the priority of Secure HardFault, and 1246 * allows a pending Non-secure HardFault to preempt (which 1247 * we implement by marking it enabled). 1248 */ 1249 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1250 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3; 1251 s->vectors[ARMV7M_EXCP_HARD].enabled = 1; 1252 } else { 1253 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1; 1254 s->vectors[ARMV7M_EXCP_HARD].enabled = 0; 1255 } 1256 } 1257 nvic_irq_update(s); 1258 } 1259 break; 1260 case 0xd10: /* System Control. */ 1261 /* TODO: Implement control registers. */ 1262 qemu_log_mask(LOG_UNIMP, "NVIC: SCR unimplemented\n"); 1263 break; 1264 case 0xd14: /* Configuration Control. */ 1265 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */ 1266 value &= (R_V7M_CCR_STKALIGN_MASK | 1267 R_V7M_CCR_BFHFNMIGN_MASK | 1268 R_V7M_CCR_DIV_0_TRP_MASK | 1269 R_V7M_CCR_UNALIGN_TRP_MASK | 1270 R_V7M_CCR_USERSETMPEND_MASK | 1271 R_V7M_CCR_NONBASETHRDENA_MASK); 1272 1273 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1274 /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */ 1275 value |= R_V7M_CCR_NONBASETHRDENA_MASK 1276 | R_V7M_CCR_STKALIGN_MASK; 1277 } 1278 if (attrs.secure) { 1279 /* the BFHFNMIGN bit is not banked; keep that in the NS copy */ 1280 cpu->env.v7m.ccr[M_REG_NS] = 1281 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK) 1282 | (value & R_V7M_CCR_BFHFNMIGN_MASK); 1283 value &= ~R_V7M_CCR_BFHFNMIGN_MASK; 1284 } 1285 1286 cpu->env.v7m.ccr[attrs.secure] = value; 1287 break; 1288 case 0xd24: /* System Handler Control and State (SHCSR) */ 1289 if (attrs.secure) { 1290 s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; 1291 /* Secure HardFault active bit cannot be written */ 1292 s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; 1293 s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; 1294 s->sec_vectors[ARMV7M_EXCP_PENDSV].active = 1295 (value & (1 << 10)) != 0; 1296 s->sec_vectors[ARMV7M_EXCP_SYSTICK].active = 1297 (value & (1 << 11)) != 0; 1298 s->sec_vectors[ARMV7M_EXCP_USAGE].pending = 1299 (value & (1 << 12)) != 0; 1300 s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; 1301 s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; 1302 s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; 1303 s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; 1304 s->sec_vectors[ARMV7M_EXCP_USAGE].enabled = 1305 (value & (1 << 18)) != 0; 1306 s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0; 1307 /* SecureFault not banked, but RAZ/WI to NS */ 1308 s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0; 1309 s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0; 1310 s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0; 1311 } else { 1312 s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; 1313 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1314 /* HARDFAULTPENDED is not present in v7M */ 1315 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0; 1316 } 1317 s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; 1318 s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; 1319 s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0; 1320 s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0; 1321 s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0; 1322 s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; 1323 s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; 1324 s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; 1325 s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0; 1326 } 1327 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1328 s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0; 1329 s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0; 1330 s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; 1331 } 1332 /* NMIACT can only be written if the write is of a zero, with 1333 * BFHFNMINS 1, and by the CPU in secure state via the NS alias. 1334 */ 1335 if (!attrs.secure && cpu->env.v7m.secure && 1336 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 1337 (value & (1 << 5)) == 0) { 1338 s->vectors[ARMV7M_EXCP_NMI].active = 0; 1339 } 1340 /* HARDFAULTACT can only be written if the write is of a zero 1341 * to the non-secure HardFault state by the CPU in secure state. 1342 * The only case where we can be targeting the non-secure HF state 1343 * when in secure state is if this is a write via the NS alias 1344 * and BFHFNMINS is 1. 1345 */ 1346 if (!attrs.secure && cpu->env.v7m.secure && 1347 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 1348 (value & (1 << 2)) == 0) { 1349 s->vectors[ARMV7M_EXCP_HARD].active = 0; 1350 } 1351 1352 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */ 1353 s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0; 1354 nvic_irq_update(s); 1355 break; 1356 case 0xd2c: /* Hard Fault Status. */ 1357 cpu->env.v7m.hfsr &= ~value; /* W1C */ 1358 break; 1359 case 0xd30: /* Debug Fault Status. */ 1360 cpu->env.v7m.dfsr &= ~value; /* W1C */ 1361 break; 1362 case 0xd34: /* Mem Manage Address. */ 1363 cpu->env.v7m.mmfar[attrs.secure] = value; 1364 return; 1365 case 0xd38: /* Bus Fault Address. */ 1366 cpu->env.v7m.bfar = value; 1367 return; 1368 case 0xd3c: /* Aux Fault Status. */ 1369 qemu_log_mask(LOG_UNIMP, 1370 "NVIC: Aux fault status registers unimplemented\n"); 1371 break; 1372 case 0xd90: /* MPU_TYPE */ 1373 return; /* RO */ 1374 case 0xd94: /* MPU_CTRL */ 1375 if ((value & 1376 (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK)) 1377 == R_V7M_MPU_CTRL_HFNMIENA_MASK) { 1378 qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is " 1379 "UNPREDICTABLE\n"); 1380 } 1381 cpu->env.v7m.mpu_ctrl[attrs.secure] 1382 = value & (R_V7M_MPU_CTRL_ENABLE_MASK | 1383 R_V7M_MPU_CTRL_HFNMIENA_MASK | 1384 R_V7M_MPU_CTRL_PRIVDEFENA_MASK); 1385 tlb_flush(CPU(cpu)); 1386 break; 1387 case 0xd98: /* MPU_RNR */ 1388 if (value >= cpu->pmsav7_dregion) { 1389 qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %" 1390 PRIu32 "/%" PRIu32 "\n", 1391 value, cpu->pmsav7_dregion); 1392 } else { 1393 cpu->env.pmsav7.rnr[attrs.secure] = value; 1394 } 1395 break; 1396 case 0xd9c: /* MPU_RBAR */ 1397 case 0xda4: /* MPU_RBAR_A1 */ 1398 case 0xdac: /* MPU_RBAR_A2 */ 1399 case 0xdb4: /* MPU_RBAR_A3 */ 1400 { 1401 int region; 1402 1403 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1404 /* PMSAv8M handling of the aliases is different from v7M: 1405 * aliases A1, A2, A3 override the low two bits of the region 1406 * number in MPU_RNR, and there is no 'region' field in the 1407 * RBAR register. 1408 */ 1409 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1410 1411 region = cpu->env.pmsav7.rnr[attrs.secure]; 1412 if (aliasno) { 1413 region = deposit32(region, 0, 2, aliasno); 1414 } 1415 if (region >= cpu->pmsav7_dregion) { 1416 return; 1417 } 1418 cpu->env.pmsav8.rbar[attrs.secure][region] = value; 1419 tlb_flush(CPU(cpu)); 1420 return; 1421 } 1422 1423 if (value & (1 << 4)) { 1424 /* VALID bit means use the region number specified in this 1425 * value and also update MPU_RNR.REGION with that value. 1426 */ 1427 region = extract32(value, 0, 4); 1428 if (region >= cpu->pmsav7_dregion) { 1429 qemu_log_mask(LOG_GUEST_ERROR, 1430 "MPU region out of range %u/%" PRIu32 "\n", 1431 region, cpu->pmsav7_dregion); 1432 return; 1433 } 1434 cpu->env.pmsav7.rnr[attrs.secure] = region; 1435 } else { 1436 region = cpu->env.pmsav7.rnr[attrs.secure]; 1437 } 1438 1439 if (region >= cpu->pmsav7_dregion) { 1440 return; 1441 } 1442 1443 cpu->env.pmsav7.drbar[region] = value & ~0x1f; 1444 tlb_flush(CPU(cpu)); 1445 break; 1446 } 1447 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */ 1448 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */ 1449 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */ 1450 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */ 1451 { 1452 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1453 1454 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1455 /* PMSAv8M handling of the aliases is different from v7M: 1456 * aliases A1, A2, A3 override the low two bits of the region 1457 * number in MPU_RNR. 1458 */ 1459 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1460 1461 region = cpu->env.pmsav7.rnr[attrs.secure]; 1462 if (aliasno) { 1463 region = deposit32(region, 0, 2, aliasno); 1464 } 1465 if (region >= cpu->pmsav7_dregion) { 1466 return; 1467 } 1468 cpu->env.pmsav8.rlar[attrs.secure][region] = value; 1469 tlb_flush(CPU(cpu)); 1470 return; 1471 } 1472 1473 if (region >= cpu->pmsav7_dregion) { 1474 return; 1475 } 1476 1477 cpu->env.pmsav7.drsr[region] = value & 0xff3f; 1478 cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f; 1479 tlb_flush(CPU(cpu)); 1480 break; 1481 } 1482 case 0xdc0: /* MPU_MAIR0 */ 1483 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1484 goto bad_offset; 1485 } 1486 if (cpu->pmsav7_dregion) { 1487 /* Register is RES0 if no MPU regions are implemented */ 1488 cpu->env.pmsav8.mair0[attrs.secure] = value; 1489 } 1490 /* We don't need to do anything else because memory attributes 1491 * only affect cacheability, and we don't implement caching. 1492 */ 1493 break; 1494 case 0xdc4: /* MPU_MAIR1 */ 1495 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1496 goto bad_offset; 1497 } 1498 if (cpu->pmsav7_dregion) { 1499 /* Register is RES0 if no MPU regions are implemented */ 1500 cpu->env.pmsav8.mair1[attrs.secure] = value; 1501 } 1502 /* We don't need to do anything else because memory attributes 1503 * only affect cacheability, and we don't implement caching. 1504 */ 1505 break; 1506 case 0xdd0: /* SAU_CTRL */ 1507 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1508 goto bad_offset; 1509 } 1510 if (!attrs.secure) { 1511 return; 1512 } 1513 cpu->env.sau.ctrl = value & 3; 1514 break; 1515 case 0xdd4: /* SAU_TYPE */ 1516 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1517 goto bad_offset; 1518 } 1519 break; 1520 case 0xdd8: /* SAU_RNR */ 1521 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1522 goto bad_offset; 1523 } 1524 if (!attrs.secure) { 1525 return; 1526 } 1527 if (value >= cpu->sau_sregion) { 1528 qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %" 1529 PRIu32 "/%" PRIu32 "\n", 1530 value, cpu->sau_sregion); 1531 } else { 1532 cpu->env.sau.rnr = value; 1533 } 1534 break; 1535 case 0xddc: /* SAU_RBAR */ 1536 { 1537 int region = cpu->env.sau.rnr; 1538 1539 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1540 goto bad_offset; 1541 } 1542 if (!attrs.secure) { 1543 return; 1544 } 1545 if (region >= cpu->sau_sregion) { 1546 return; 1547 } 1548 cpu->env.sau.rbar[region] = value & ~0x1f; 1549 tlb_flush(CPU(cpu)); 1550 break; 1551 } 1552 case 0xde0: /* SAU_RLAR */ 1553 { 1554 int region = cpu->env.sau.rnr; 1555 1556 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1557 goto bad_offset; 1558 } 1559 if (!attrs.secure) { 1560 return; 1561 } 1562 if (region >= cpu->sau_sregion) { 1563 return; 1564 } 1565 cpu->env.sau.rlar[region] = value & ~0x1c; 1566 tlb_flush(CPU(cpu)); 1567 break; 1568 } 1569 case 0xde4: /* SFSR */ 1570 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1571 goto bad_offset; 1572 } 1573 if (!attrs.secure) { 1574 return; 1575 } 1576 cpu->env.v7m.sfsr &= ~value; /* W1C */ 1577 break; 1578 case 0xde8: /* SFAR */ 1579 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1580 goto bad_offset; 1581 } 1582 if (!attrs.secure) { 1583 return; 1584 } 1585 cpu->env.v7m.sfsr = value; 1586 break; 1587 case 0xf00: /* Software Triggered Interrupt Register */ 1588 { 1589 int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ; 1590 if (excnum < s->num_irq) { 1591 armv7m_nvic_set_pending(s, excnum, false); 1592 } 1593 break; 1594 } 1595 default: 1596 bad_offset: 1597 qemu_log_mask(LOG_GUEST_ERROR, 1598 "NVIC: Bad write offset 0x%x\n", offset); 1599 } 1600 } 1601 1602 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs) 1603 { 1604 /* Return true if unprivileged access to this register is permitted. */ 1605 switch (offset) { 1606 case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */ 1607 /* For access via STIR_NS it is the NS CCR.USERSETMPEND that 1608 * controls access even though the CPU is in Secure state (I_QDKX). 1609 */ 1610 return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK; 1611 default: 1612 /* All other user accesses cause a BusFault unconditionally */ 1613 return false; 1614 } 1615 } 1616 1617 static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs) 1618 { 1619 /* Behaviour for the SHPR register field for this exception: 1620 * return M_REG_NS to use the nonsecure vector (including for 1621 * non-banked exceptions), M_REG_S for the secure version of 1622 * a banked exception, and -1 if this field should RAZ/WI. 1623 */ 1624 switch (exc) { 1625 case ARMV7M_EXCP_MEM: 1626 case ARMV7M_EXCP_USAGE: 1627 case ARMV7M_EXCP_SVC: 1628 case ARMV7M_EXCP_PENDSV: 1629 case ARMV7M_EXCP_SYSTICK: 1630 /* Banked exceptions */ 1631 return attrs.secure; 1632 case ARMV7M_EXCP_BUS: 1633 /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */ 1634 if (!attrs.secure && 1635 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1636 return -1; 1637 } 1638 return M_REG_NS; 1639 case ARMV7M_EXCP_SECURE: 1640 /* Not banked, RAZ/WI from nonsecure */ 1641 if (!attrs.secure) { 1642 return -1; 1643 } 1644 return M_REG_NS; 1645 case ARMV7M_EXCP_DEBUG: 1646 /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */ 1647 return M_REG_NS; 1648 case 8 ... 10: 1649 case 13: 1650 /* RES0 */ 1651 return -1; 1652 default: 1653 /* Not reachable due to decode of SHPR register addresses */ 1654 g_assert_not_reached(); 1655 } 1656 } 1657 1658 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr, 1659 uint64_t *data, unsigned size, 1660 MemTxAttrs attrs) 1661 { 1662 NVICState *s = (NVICState *)opaque; 1663 uint32_t offset = addr; 1664 unsigned i, startvec, end; 1665 uint32_t val; 1666 1667 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) { 1668 /* Generate BusFault for unprivileged accesses */ 1669 return MEMTX_ERROR; 1670 } 1671 1672 switch (offset) { 1673 /* reads of set and clear both return the status */ 1674 case 0x100 ... 0x13f: /* NVIC Set enable */ 1675 offset += 0x80; 1676 /* fall through */ 1677 case 0x180 ... 0x1bf: /* NVIC Clear enable */ 1678 val = 0; 1679 startvec = offset - 0x180 + NVIC_FIRST_IRQ; /* vector # */ 1680 1681 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1682 if (s->vectors[startvec + i].enabled && 1683 (attrs.secure || s->itns[startvec + i])) { 1684 val |= (1 << i); 1685 } 1686 } 1687 break; 1688 case 0x200 ... 0x23f: /* NVIC Set pend */ 1689 offset += 0x80; 1690 /* fall through */ 1691 case 0x280 ... 0x2bf: /* NVIC Clear pend */ 1692 val = 0; 1693 startvec = offset - 0x280 + NVIC_FIRST_IRQ; /* vector # */ 1694 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1695 if (s->vectors[startvec + i].pending && 1696 (attrs.secure || s->itns[startvec + i])) { 1697 val |= (1 << i); 1698 } 1699 } 1700 break; 1701 case 0x300 ... 0x33f: /* NVIC Active */ 1702 val = 0; 1703 startvec = offset - 0x300 + NVIC_FIRST_IRQ; /* vector # */ 1704 1705 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1706 if (s->vectors[startvec + i].active && 1707 (attrs.secure || s->itns[startvec + i])) { 1708 val |= (1 << i); 1709 } 1710 } 1711 break; 1712 case 0x400 ... 0x5ef: /* NVIC Priority */ 1713 val = 0; 1714 startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */ 1715 1716 for (i = 0; i < size && startvec + i < s->num_irq; i++) { 1717 if (attrs.secure || s->itns[startvec + i]) { 1718 val |= s->vectors[startvec + i].prio << (8 * i); 1719 } 1720 } 1721 break; 1722 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */ 1723 val = 0; 1724 for (i = 0; i < size; i++) { 1725 unsigned hdlidx = (offset - 0xd14) + i; 1726 int sbank = shpr_bank(s, hdlidx, attrs); 1727 1728 if (sbank < 0) { 1729 continue; 1730 } 1731 val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank)); 1732 } 1733 break; 1734 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */ 1735 /* The BFSR bits [15:8] are shared between security states 1736 * and we store them in the NS copy 1737 */ 1738 val = s->cpu->env.v7m.cfsr[attrs.secure]; 1739 val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK; 1740 val = extract32(val, (offset - 0xd28) * 8, size * 8); 1741 break; 1742 case 0xfe0 ... 0xfff: /* ID. */ 1743 if (offset & 3) { 1744 val = 0; 1745 } else { 1746 val = nvic_id[(offset - 0xfe0) >> 2]; 1747 } 1748 break; 1749 default: 1750 if (size == 4) { 1751 val = nvic_readl(s, offset, attrs); 1752 } else { 1753 qemu_log_mask(LOG_GUEST_ERROR, 1754 "NVIC: Bad read of size %d at offset 0x%x\n", 1755 size, offset); 1756 val = 0; 1757 } 1758 } 1759 1760 trace_nvic_sysreg_read(addr, val, size); 1761 *data = val; 1762 return MEMTX_OK; 1763 } 1764 1765 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr, 1766 uint64_t value, unsigned size, 1767 MemTxAttrs attrs) 1768 { 1769 NVICState *s = (NVICState *)opaque; 1770 uint32_t offset = addr; 1771 unsigned i, startvec, end; 1772 unsigned setval = 0; 1773 1774 trace_nvic_sysreg_write(addr, value, size); 1775 1776 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) { 1777 /* Generate BusFault for unprivileged accesses */ 1778 return MEMTX_ERROR; 1779 } 1780 1781 switch (offset) { 1782 case 0x100 ... 0x13f: /* NVIC Set enable */ 1783 offset += 0x80; 1784 setval = 1; 1785 /* fall through */ 1786 case 0x180 ... 0x1bf: /* NVIC Clear enable */ 1787 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; 1788 1789 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1790 if (value & (1 << i) && 1791 (attrs.secure || s->itns[startvec + i])) { 1792 s->vectors[startvec + i].enabled = setval; 1793 } 1794 } 1795 nvic_irq_update(s); 1796 return MEMTX_OK; 1797 case 0x200 ... 0x23f: /* NVIC Set pend */ 1798 /* the special logic in armv7m_nvic_set_pending() 1799 * is not needed since IRQs are never escalated 1800 */ 1801 offset += 0x80; 1802 setval = 1; 1803 /* fall through */ 1804 case 0x280 ... 0x2bf: /* NVIC Clear pend */ 1805 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */ 1806 1807 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 1808 if (value & (1 << i) && 1809 (attrs.secure || s->itns[startvec + i])) { 1810 s->vectors[startvec + i].pending = setval; 1811 } 1812 } 1813 nvic_irq_update(s); 1814 return MEMTX_OK; 1815 case 0x300 ... 0x33f: /* NVIC Active */ 1816 return MEMTX_OK; /* R/O */ 1817 case 0x400 ... 0x5ef: /* NVIC Priority */ 1818 startvec = 8 * (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */ 1819 1820 for (i = 0; i < size && startvec + i < s->num_irq; i++) { 1821 if (attrs.secure || s->itns[startvec + i]) { 1822 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff); 1823 } 1824 } 1825 nvic_irq_update(s); 1826 return MEMTX_OK; 1827 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */ 1828 for (i = 0; i < size; i++) { 1829 unsigned hdlidx = (offset - 0xd14) + i; 1830 int newprio = extract32(value, i * 8, 8); 1831 int sbank = shpr_bank(s, hdlidx, attrs); 1832 1833 if (sbank < 0) { 1834 continue; 1835 } 1836 set_prio(s, hdlidx, sbank, newprio); 1837 } 1838 nvic_irq_update(s); 1839 return MEMTX_OK; 1840 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */ 1841 /* All bits are W1C, so construct 32 bit value with 0s in 1842 * the parts not written by the access size 1843 */ 1844 value <<= ((offset - 0xd28) * 8); 1845 1846 s->cpu->env.v7m.cfsr[attrs.secure] &= ~value; 1847 if (attrs.secure) { 1848 /* The BFSR bits [15:8] are shared between security states 1849 * and we store them in the NS copy. 1850 */ 1851 s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK); 1852 } 1853 return MEMTX_OK; 1854 } 1855 if (size == 4) { 1856 nvic_writel(s, offset, value, attrs); 1857 return MEMTX_OK; 1858 } 1859 qemu_log_mask(LOG_GUEST_ERROR, 1860 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset); 1861 /* This is UNPREDICTABLE; treat as RAZ/WI */ 1862 return MEMTX_OK; 1863 } 1864 1865 static const MemoryRegionOps nvic_sysreg_ops = { 1866 .read_with_attrs = nvic_sysreg_read, 1867 .write_with_attrs = nvic_sysreg_write, 1868 .endianness = DEVICE_NATIVE_ENDIAN, 1869 }; 1870 1871 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr, 1872 uint64_t value, unsigned size, 1873 MemTxAttrs attrs) 1874 { 1875 MemoryRegion *mr = opaque; 1876 1877 if (attrs.secure) { 1878 /* S accesses to the alias act like NS accesses to the real region */ 1879 attrs.secure = 0; 1880 return memory_region_dispatch_write(mr, addr, value, size, attrs); 1881 } else { 1882 /* NS attrs are RAZ/WI for privileged, and BusFault for user */ 1883 if (attrs.user) { 1884 return MEMTX_ERROR; 1885 } 1886 return MEMTX_OK; 1887 } 1888 } 1889 1890 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr, 1891 uint64_t *data, unsigned size, 1892 MemTxAttrs attrs) 1893 { 1894 MemoryRegion *mr = opaque; 1895 1896 if (attrs.secure) { 1897 /* S accesses to the alias act like NS accesses to the real region */ 1898 attrs.secure = 0; 1899 return memory_region_dispatch_read(mr, addr, data, size, attrs); 1900 } else { 1901 /* NS attrs are RAZ/WI for privileged, and BusFault for user */ 1902 if (attrs.user) { 1903 return MEMTX_ERROR; 1904 } 1905 *data = 0; 1906 return MEMTX_OK; 1907 } 1908 } 1909 1910 static const MemoryRegionOps nvic_sysreg_ns_ops = { 1911 .read_with_attrs = nvic_sysreg_ns_read, 1912 .write_with_attrs = nvic_sysreg_ns_write, 1913 .endianness = DEVICE_NATIVE_ENDIAN, 1914 }; 1915 1916 static MemTxResult nvic_systick_write(void *opaque, hwaddr addr, 1917 uint64_t value, unsigned size, 1918 MemTxAttrs attrs) 1919 { 1920 NVICState *s = opaque; 1921 MemoryRegion *mr; 1922 1923 /* Direct the access to the correct systick */ 1924 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0); 1925 return memory_region_dispatch_write(mr, addr, value, size, attrs); 1926 } 1927 1928 static MemTxResult nvic_systick_read(void *opaque, hwaddr addr, 1929 uint64_t *data, unsigned size, 1930 MemTxAttrs attrs) 1931 { 1932 NVICState *s = opaque; 1933 MemoryRegion *mr; 1934 1935 /* Direct the access to the correct systick */ 1936 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0); 1937 return memory_region_dispatch_read(mr, addr, data, size, attrs); 1938 } 1939 1940 static const MemoryRegionOps nvic_systick_ops = { 1941 .read_with_attrs = nvic_systick_read, 1942 .write_with_attrs = nvic_systick_write, 1943 .endianness = DEVICE_NATIVE_ENDIAN, 1944 }; 1945 1946 static int nvic_post_load(void *opaque, int version_id) 1947 { 1948 NVICState *s = opaque; 1949 unsigned i; 1950 int resetprio; 1951 1952 /* Check for out of range priority settings */ 1953 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3; 1954 1955 if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio || 1956 s->vectors[ARMV7M_EXCP_NMI].prio != -2 || 1957 s->vectors[ARMV7M_EXCP_HARD].prio != -1) { 1958 return 1; 1959 } 1960 for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) { 1961 if (s->vectors[i].prio & ~0xff) { 1962 return 1; 1963 } 1964 } 1965 1966 nvic_recompute_state(s); 1967 1968 return 0; 1969 } 1970 1971 static const VMStateDescription vmstate_VecInfo = { 1972 .name = "armv7m_nvic_info", 1973 .version_id = 1, 1974 .minimum_version_id = 1, 1975 .fields = (VMStateField[]) { 1976 VMSTATE_INT16(prio, VecInfo), 1977 VMSTATE_UINT8(enabled, VecInfo), 1978 VMSTATE_UINT8(pending, VecInfo), 1979 VMSTATE_UINT8(active, VecInfo), 1980 VMSTATE_UINT8(level, VecInfo), 1981 VMSTATE_END_OF_LIST() 1982 } 1983 }; 1984 1985 static bool nvic_security_needed(void *opaque) 1986 { 1987 NVICState *s = opaque; 1988 1989 return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY); 1990 } 1991 1992 static int nvic_security_post_load(void *opaque, int version_id) 1993 { 1994 NVICState *s = opaque; 1995 int i; 1996 1997 /* Check for out of range priority settings */ 1998 if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1 1999 && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) { 2000 /* We can't cross-check against AIRCR.BFHFNMINS as we don't know 2001 * if the CPU state has been migrated yet; a mismatch won't 2002 * cause the emulation to blow up, though. 2003 */ 2004 return 1; 2005 } 2006 for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) { 2007 if (s->sec_vectors[i].prio & ~0xff) { 2008 return 1; 2009 } 2010 } 2011 return 0; 2012 } 2013 2014 static const VMStateDescription vmstate_nvic_security = { 2015 .name = "nvic/m-security", 2016 .version_id = 1, 2017 .minimum_version_id = 1, 2018 .needed = nvic_security_needed, 2019 .post_load = &nvic_security_post_load, 2020 .fields = (VMStateField[]) { 2021 VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1, 2022 vmstate_VecInfo, VecInfo), 2023 VMSTATE_UINT32(prigroup[M_REG_S], NVICState), 2024 VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS), 2025 VMSTATE_END_OF_LIST() 2026 } 2027 }; 2028 2029 static const VMStateDescription vmstate_nvic = { 2030 .name = "armv7m_nvic", 2031 .version_id = 4, 2032 .minimum_version_id = 4, 2033 .post_load = &nvic_post_load, 2034 .fields = (VMStateField[]) { 2035 VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1, 2036 vmstate_VecInfo, VecInfo), 2037 VMSTATE_UINT32(prigroup[M_REG_NS], NVICState), 2038 VMSTATE_END_OF_LIST() 2039 }, 2040 .subsections = (const VMStateDescription*[]) { 2041 &vmstate_nvic_security, 2042 NULL 2043 } 2044 }; 2045 2046 static Property props_nvic[] = { 2047 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */ 2048 DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64), 2049 DEFINE_PROP_END_OF_LIST() 2050 }; 2051 2052 static void armv7m_nvic_reset(DeviceState *dev) 2053 { 2054 int resetprio; 2055 NVICState *s = NVIC(dev); 2056 2057 memset(s->vectors, 0, sizeof(s->vectors)); 2058 memset(s->sec_vectors, 0, sizeof(s->sec_vectors)); 2059 s->prigroup[M_REG_NS] = 0; 2060 s->prigroup[M_REG_S] = 0; 2061 2062 s->vectors[ARMV7M_EXCP_NMI].enabled = 1; 2063 /* MEM, BUS, and USAGE are enabled through 2064 * the System Handler Control register 2065 */ 2066 s->vectors[ARMV7M_EXCP_SVC].enabled = 1; 2067 s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1; 2068 s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1; 2069 s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; 2070 2071 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3; 2072 s->vectors[ARMV7M_EXCP_RESET].prio = resetprio; 2073 s->vectors[ARMV7M_EXCP_NMI].prio = -2; 2074 s->vectors[ARMV7M_EXCP_HARD].prio = -1; 2075 2076 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2077 s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1; 2078 s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1; 2079 s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1; 2080 s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; 2081 2082 /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */ 2083 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1; 2084 /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */ 2085 s->vectors[ARMV7M_EXCP_HARD].enabled = 0; 2086 } else { 2087 s->vectors[ARMV7M_EXCP_HARD].enabled = 1; 2088 } 2089 2090 /* Strictly speaking the reset handler should be enabled. 2091 * However, we don't simulate soft resets through the NVIC, 2092 * and the reset vector should never be pended. 2093 * So we leave it disabled to catch logic errors. 2094 */ 2095 2096 s->exception_prio = NVIC_NOEXC_PRIO; 2097 s->vectpending = 0; 2098 s->vectpending_is_s_banked = false; 2099 s->vectpending_prio = NVIC_NOEXC_PRIO; 2100 2101 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2102 memset(s->itns, 0, sizeof(s->itns)); 2103 } else { 2104 /* This state is constant and not guest accessible in a non-security 2105 * NVIC; we set the bits to true to avoid having to do a feature 2106 * bit check in the NVIC enable/pend/etc register accessors. 2107 */ 2108 int i; 2109 2110 for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) { 2111 s->itns[i] = true; 2112 } 2113 } 2114 } 2115 2116 static void nvic_systick_trigger(void *opaque, int n, int level) 2117 { 2118 NVICState *s = opaque; 2119 2120 if (level) { 2121 /* SysTick just asked us to pend its exception. 2122 * (This is different from an external interrupt line's 2123 * behaviour.) 2124 * n == 0 : NonSecure systick 2125 * n == 1 : Secure systick 2126 */ 2127 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n); 2128 } 2129 } 2130 2131 static void armv7m_nvic_realize(DeviceState *dev, Error **errp) 2132 { 2133 NVICState *s = NVIC(dev); 2134 Error *err = NULL; 2135 int regionlen; 2136 2137 s->cpu = ARM_CPU(qemu_get_cpu(0)); 2138 assert(s->cpu); 2139 2140 if (s->num_irq > NVIC_MAX_IRQ) { 2141 error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq); 2142 return; 2143 } 2144 2145 qdev_init_gpio_in(dev, set_irq_level, s->num_irq); 2146 2147 /* include space for internal exception vectors */ 2148 s->num_irq += NVIC_FIRST_IRQ; 2149 2150 object_property_set_bool(OBJECT(&s->systick[M_REG_NS]), true, 2151 "realized", &err); 2152 if (err != NULL) { 2153 error_propagate(errp, err); 2154 return; 2155 } 2156 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0, 2157 qdev_get_gpio_in_named(dev, "systick-trigger", 2158 M_REG_NS)); 2159 2160 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2161 /* We couldn't init the secure systick device in instance_init 2162 * as we didn't know then if the CPU had the security extensions; 2163 * so we have to do it here. 2164 */ 2165 object_initialize(&s->systick[M_REG_S], sizeof(s->systick[M_REG_S]), 2166 TYPE_SYSTICK); 2167 qdev_set_parent_bus(DEVICE(&s->systick[M_REG_S]), sysbus_get_default()); 2168 2169 object_property_set_bool(OBJECT(&s->systick[M_REG_S]), true, 2170 "realized", &err); 2171 if (err != NULL) { 2172 error_propagate(errp, err); 2173 return; 2174 } 2175 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0, 2176 qdev_get_gpio_in_named(dev, "systick-trigger", 2177 M_REG_S)); 2178 } 2179 2180 /* The NVIC and System Control Space (SCS) starts at 0xe000e000 2181 * and looks like this: 2182 * 0x004 - ICTR 2183 * 0x010 - 0xff - systick 2184 * 0x100..0x7ec - NVIC 2185 * 0x7f0..0xcff - Reserved 2186 * 0xd00..0xd3c - SCS registers 2187 * 0xd40..0xeff - Reserved or Not implemented 2188 * 0xf00 - STIR 2189 * 2190 * Some registers within this space are banked between security states. 2191 * In v8M there is a second range 0xe002e000..0xe002efff which is the 2192 * NonSecure alias SCS; secure accesses to this behave like NS accesses 2193 * to the main SCS range, and non-secure accesses (including when 2194 * the security extension is not implemented) are RAZ/WI. 2195 * Note that both the main SCS range and the alias range are defined 2196 * to be exempt from memory attribution (R_BLJT) and so the memory 2197 * transaction attribute always matches the current CPU security 2198 * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops 2199 * wrappers we change attrs.secure to indicate the NS access; so 2200 * generally code determining which banked register to use should 2201 * use attrs.secure; code determining actual behaviour of the system 2202 * should use env->v7m.secure. 2203 */ 2204 regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000; 2205 memory_region_init(&s->container, OBJECT(s), "nvic", regionlen); 2206 /* The system register region goes at the bottom of the priority 2207 * stack as it covers the whole page. 2208 */ 2209 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s, 2210 "nvic_sysregs", 0x1000); 2211 memory_region_add_subregion(&s->container, 0, &s->sysregmem); 2212 2213 memory_region_init_io(&s->systickmem, OBJECT(s), 2214 &nvic_systick_ops, s, 2215 "nvic_systick", 0xe0); 2216 2217 memory_region_add_subregion_overlap(&s->container, 0x10, 2218 &s->systickmem, 1); 2219 2220 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) { 2221 memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s), 2222 &nvic_sysreg_ns_ops, &s->sysregmem, 2223 "nvic_sysregs_ns", 0x1000); 2224 memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem); 2225 memory_region_init_io(&s->systick_ns_mem, OBJECT(s), 2226 &nvic_sysreg_ns_ops, &s->systickmem, 2227 "nvic_systick_ns", 0xe0); 2228 memory_region_add_subregion_overlap(&s->container, 0x20010, 2229 &s->systick_ns_mem, 1); 2230 } 2231 2232 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container); 2233 } 2234 2235 static void armv7m_nvic_instance_init(Object *obj) 2236 { 2237 /* We have a different default value for the num-irq property 2238 * than our superclass. This function runs after qdev init 2239 * has set the defaults from the Property array and before 2240 * any user-specified property setting, so just modify the 2241 * value in the GICState struct. 2242 */ 2243 DeviceState *dev = DEVICE(obj); 2244 NVICState *nvic = NVIC(obj); 2245 SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 2246 2247 object_initialize(&nvic->systick[M_REG_NS], 2248 sizeof(nvic->systick[M_REG_NS]), TYPE_SYSTICK); 2249 qdev_set_parent_bus(DEVICE(&nvic->systick[M_REG_NS]), sysbus_get_default()); 2250 /* We can't initialize the secure systick here, as we don't know 2251 * yet if we need it. 2252 */ 2253 2254 sysbus_init_irq(sbd, &nvic->excpout); 2255 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1); 2256 qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger", 2257 M_REG_NUM_BANKS); 2258 } 2259 2260 static void armv7m_nvic_class_init(ObjectClass *klass, void *data) 2261 { 2262 DeviceClass *dc = DEVICE_CLASS(klass); 2263 2264 dc->vmsd = &vmstate_nvic; 2265 dc->props = props_nvic; 2266 dc->reset = armv7m_nvic_reset; 2267 dc->realize = armv7m_nvic_realize; 2268 } 2269 2270 static const TypeInfo armv7m_nvic_info = { 2271 .name = TYPE_NVIC, 2272 .parent = TYPE_SYS_BUS_DEVICE, 2273 .instance_init = armv7m_nvic_instance_init, 2274 .instance_size = sizeof(NVICState), 2275 .class_init = armv7m_nvic_class_init, 2276 .class_size = sizeof(SysBusDeviceClass), 2277 }; 2278 2279 static void armv7m_nvic_register_types(void) 2280 { 2281 type_register_static(&armv7m_nvic_info); 2282 } 2283 2284 type_init(armv7m_nvic_register_types) 2285