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 "cpu.h" 16 #include "hw/sysbus.h" 17 #include "migration/vmstate.h" 18 #include "qemu/timer.h" 19 #include "hw/intc/armv7m_nvic.h" 20 #include "hw/irq.h" 21 #include "hw/qdev-properties.h" 22 #include "sysemu/runstate.h" 23 #include "target/arm/cpu.h" 24 #include "exec/exec-all.h" 25 #include "exec/memop.h" 26 #include "qemu/log.h" 27 #include "qemu/module.h" 28 #include "trace.h" 29 30 /* IRQ number counting: 31 * 32 * the num-irq property counts the number of external IRQ lines 33 * 34 * NVICState::num_irq counts the total number of exceptions 35 * (external IRQs, the 15 internal exceptions including reset, 36 * and one for the unused exception number 0). 37 * 38 * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines. 39 * 40 * NVIC_MAX_VECTORS is the highest permitted number of exceptions. 41 * 42 * Iterating through all exceptions should typically be done with 43 * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0. 44 * 45 * The external qemu_irq lines are the NVIC's external IRQ lines, 46 * so line 0 is exception 16. 47 * 48 * In the terminology of the architecture manual, "interrupts" are 49 * a subcategory of exception referring to the external interrupts 50 * (which are exception numbers NVIC_FIRST_IRQ and upward). 51 * For historical reasons QEMU tends to use "interrupt" and 52 * "exception" more or less interchangeably. 53 */ 54 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS 55 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ) 56 57 /* Effective running priority of the CPU when no exception is active 58 * (higher than the highest possible priority value) 59 */ 60 #define NVIC_NOEXC_PRIO 0x100 61 /* Maximum priority of non-secure exceptions when AIRCR.PRIS is set */ 62 #define NVIC_NS_PRIO_LIMIT 0x80 63 64 static const uint8_t nvic_id[] = { 65 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 66 }; 67 68 static void signal_sysresetreq(NVICState *s) 69 { 70 if (qemu_irq_is_connected(s->sysresetreq)) { 71 qemu_irq_pulse(s->sysresetreq); 72 } else { 73 /* 74 * Default behaviour if the SoC doesn't need to wire up 75 * SYSRESETREQ (eg to a system reset controller of some kind): 76 * perform a system reset via the usual QEMU API. 77 */ 78 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 79 } 80 } 81 82 static int nvic_pending_prio(NVICState *s) 83 { 84 /* return the group priority of the current pending interrupt, 85 * or NVIC_NOEXC_PRIO if no interrupt is pending 86 */ 87 return s->vectpending_prio; 88 } 89 90 /* Return the value of the ISCR RETTOBASE bit: 91 * 1 if there is exactly one active exception 92 * 0 if there is more than one active exception 93 * UNKNOWN if there are no active exceptions (we choose 1, 94 * which matches the choice Cortex-M3 is documented as making). 95 * 96 * NB: some versions of the documentation talk about this 97 * counting "active exceptions other than the one shown by IPSR"; 98 * this is only different in the obscure corner case where guest 99 * code has manually deactivated an exception and is about 100 * to fail an exception-return integrity check. The definition 101 * above is the one from the v8M ARM ARM and is also in line 102 * with the behaviour documented for the Cortex-M3. 103 */ 104 static bool nvic_rettobase(NVICState *s) 105 { 106 int irq, nhand = 0; 107 bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY); 108 109 for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) { 110 if (s->vectors[irq].active || 111 (check_sec && irq < NVIC_INTERNAL_VECTORS && 112 s->sec_vectors[irq].active)) { 113 nhand++; 114 if (nhand == 2) { 115 return 0; 116 } 117 } 118 } 119 120 return 1; 121 } 122 123 /* Return the value of the ISCR ISRPENDING bit: 124 * 1 if an external interrupt is pending 125 * 0 if no external interrupt is pending 126 */ 127 static bool nvic_isrpending(NVICState *s) 128 { 129 int irq; 130 131 /* We can shortcut if the highest priority pending interrupt 132 * happens to be external or if there is nothing pending. 133 */ 134 if (s->vectpending > NVIC_FIRST_IRQ) { 135 return true; 136 } 137 if (s->vectpending == 0) { 138 return false; 139 } 140 141 for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) { 142 if (s->vectors[irq].pending) { 143 return true; 144 } 145 } 146 return false; 147 } 148 149 static bool exc_is_banked(int exc) 150 { 151 /* Return true if this is one of the limited set of exceptions which 152 * are banked (and thus have state in sec_vectors[]) 153 */ 154 return exc == ARMV7M_EXCP_HARD || 155 exc == ARMV7M_EXCP_MEM || 156 exc == ARMV7M_EXCP_USAGE || 157 exc == ARMV7M_EXCP_SVC || 158 exc == ARMV7M_EXCP_PENDSV || 159 exc == ARMV7M_EXCP_SYSTICK; 160 } 161 162 /* Return a mask word which clears the subpriority bits from 163 * a priority value for an M-profile exception, leaving only 164 * the group priority. 165 */ 166 static inline uint32_t nvic_gprio_mask(NVICState *s, bool secure) 167 { 168 return ~0U << (s->prigroup[secure] + 1); 169 } 170 171 static bool exc_targets_secure(NVICState *s, int exc) 172 { 173 /* Return true if this non-banked exception targets Secure state. */ 174 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 175 return false; 176 } 177 178 if (exc >= NVIC_FIRST_IRQ) { 179 return !s->itns[exc]; 180 } 181 182 /* Function shouldn't be called for banked exceptions. */ 183 assert(!exc_is_banked(exc)); 184 185 switch (exc) { 186 case ARMV7M_EXCP_NMI: 187 case ARMV7M_EXCP_BUS: 188 return !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK); 189 case ARMV7M_EXCP_SECURE: 190 return true; 191 case ARMV7M_EXCP_DEBUG: 192 /* TODO: controlled by DEMCR.SDME, which we don't yet implement */ 193 return false; 194 default: 195 /* reset, and reserved (unused) low exception numbers. 196 * We'll get called by code that loops through all the exception 197 * numbers, but it doesn't matter what we return here as these 198 * non-existent exceptions will never be pended or active. 199 */ 200 return true; 201 } 202 } 203 204 static int exc_group_prio(NVICState *s, int rawprio, bool targets_secure) 205 { 206 /* Return the group priority for this exception, given its raw 207 * (group-and-subgroup) priority value and whether it is targeting 208 * secure state or not. 209 */ 210 if (rawprio < 0) { 211 return rawprio; 212 } 213 rawprio &= nvic_gprio_mask(s, targets_secure); 214 /* AIRCR.PRIS causes us to squash all NS priorities into the 215 * lower half of the total range 216 */ 217 if (!targets_secure && 218 (s->cpu->env.v7m.aircr & R_V7M_AIRCR_PRIS_MASK)) { 219 rawprio = (rawprio >> 1) + NVIC_NS_PRIO_LIMIT; 220 } 221 return rawprio; 222 } 223 224 /* Recompute vectpending and exception_prio for a CPU which implements 225 * the Security extension 226 */ 227 static void nvic_recompute_state_secure(NVICState *s) 228 { 229 int i, bank; 230 int pend_prio = NVIC_NOEXC_PRIO; 231 int active_prio = NVIC_NOEXC_PRIO; 232 int pend_irq = 0; 233 bool pending_is_s_banked = false; 234 int pend_subprio = 0; 235 236 /* R_CQRV: precedence is by: 237 * - lowest group priority; if both the same then 238 * - lowest subpriority; if both the same then 239 * - lowest exception number; if both the same (ie banked) then 240 * - secure exception takes precedence 241 * Compare pseudocode RawExecutionPriority. 242 * Annoyingly, now we have two prigroup values (for S and NS) 243 * we can't do the loop comparison on raw priority values. 244 */ 245 for (i = 1; i < s->num_irq; i++) { 246 for (bank = M_REG_S; bank >= M_REG_NS; bank--) { 247 VecInfo *vec; 248 int prio, subprio; 249 bool targets_secure; 250 251 if (bank == M_REG_S) { 252 if (!exc_is_banked(i)) { 253 continue; 254 } 255 vec = &s->sec_vectors[i]; 256 targets_secure = true; 257 } else { 258 vec = &s->vectors[i]; 259 targets_secure = !exc_is_banked(i) && exc_targets_secure(s, i); 260 } 261 262 prio = exc_group_prio(s, vec->prio, targets_secure); 263 subprio = vec->prio & ~nvic_gprio_mask(s, targets_secure); 264 if (vec->enabled && vec->pending && 265 ((prio < pend_prio) || 266 (prio == pend_prio && prio >= 0 && subprio < pend_subprio))) { 267 pend_prio = prio; 268 pend_subprio = subprio; 269 pend_irq = i; 270 pending_is_s_banked = (bank == M_REG_S); 271 } 272 if (vec->active && prio < active_prio) { 273 active_prio = prio; 274 } 275 } 276 } 277 278 s->vectpending_is_s_banked = pending_is_s_banked; 279 s->vectpending = pend_irq; 280 s->vectpending_prio = pend_prio; 281 s->exception_prio = active_prio; 282 283 trace_nvic_recompute_state_secure(s->vectpending, 284 s->vectpending_is_s_banked, 285 s->vectpending_prio, 286 s->exception_prio); 287 } 288 289 /* Recompute vectpending and exception_prio */ 290 static void nvic_recompute_state(NVICState *s) 291 { 292 int i; 293 int pend_prio = NVIC_NOEXC_PRIO; 294 int active_prio = NVIC_NOEXC_PRIO; 295 int pend_irq = 0; 296 297 /* In theory we could write one function that handled both 298 * the "security extension present" and "not present"; however 299 * the security related changes significantly complicate the 300 * recomputation just by themselves and mixing both cases together 301 * would be even worse, so we retain a separate non-secure-only 302 * version for CPUs which don't implement the security extension. 303 */ 304 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 305 nvic_recompute_state_secure(s); 306 return; 307 } 308 309 for (i = 1; i < s->num_irq; i++) { 310 VecInfo *vec = &s->vectors[i]; 311 312 if (vec->enabled && vec->pending && vec->prio < pend_prio) { 313 pend_prio = vec->prio; 314 pend_irq = i; 315 } 316 if (vec->active && vec->prio < active_prio) { 317 active_prio = vec->prio; 318 } 319 } 320 321 if (active_prio > 0) { 322 active_prio &= nvic_gprio_mask(s, false); 323 } 324 325 if (pend_prio > 0) { 326 pend_prio &= nvic_gprio_mask(s, false); 327 } 328 329 s->vectpending = pend_irq; 330 s->vectpending_prio = pend_prio; 331 s->exception_prio = active_prio; 332 333 trace_nvic_recompute_state(s->vectpending, 334 s->vectpending_prio, 335 s->exception_prio); 336 } 337 338 /* Return the current execution priority of the CPU 339 * (equivalent to the pseudocode ExecutionPriority function). 340 * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO. 341 */ 342 static inline int nvic_exec_prio(NVICState *s) 343 { 344 CPUARMState *env = &s->cpu->env; 345 int running = NVIC_NOEXC_PRIO; 346 347 if (env->v7m.basepri[M_REG_NS] > 0) { 348 running = exc_group_prio(s, env->v7m.basepri[M_REG_NS], M_REG_NS); 349 } 350 351 if (env->v7m.basepri[M_REG_S] > 0) { 352 int basepri = exc_group_prio(s, env->v7m.basepri[M_REG_S], M_REG_S); 353 if (running > basepri) { 354 running = basepri; 355 } 356 } 357 358 if (env->v7m.primask[M_REG_NS]) { 359 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) { 360 if (running > NVIC_NS_PRIO_LIMIT) { 361 running = NVIC_NS_PRIO_LIMIT; 362 } 363 } else { 364 running = 0; 365 } 366 } 367 368 if (env->v7m.primask[M_REG_S]) { 369 running = 0; 370 } 371 372 if (env->v7m.faultmask[M_REG_NS]) { 373 if (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 374 running = -1; 375 } else { 376 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) { 377 if (running > NVIC_NS_PRIO_LIMIT) { 378 running = NVIC_NS_PRIO_LIMIT; 379 } 380 } else { 381 running = 0; 382 } 383 } 384 } 385 386 if (env->v7m.faultmask[M_REG_S]) { 387 running = (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) ? -3 : -1; 388 } 389 390 /* consider priority of active handler */ 391 return MIN(running, s->exception_prio); 392 } 393 394 bool armv7m_nvic_neg_prio_requested(void *opaque, bool secure) 395 { 396 /* Return true if the requested execution priority is negative 397 * for the specified security state, ie that security state 398 * has an active NMI or HardFault or has set its FAULTMASK. 399 * Note that this is not the same as whether the execution 400 * priority is actually negative (for instance AIRCR.PRIS may 401 * mean we don't allow FAULTMASK_NS to actually make the execution 402 * priority negative). Compare pseudocode IsReqExcPriNeg(). 403 */ 404 NVICState *s = opaque; 405 406 if (s->cpu->env.v7m.faultmask[secure]) { 407 return true; 408 } 409 410 if (secure ? s->sec_vectors[ARMV7M_EXCP_HARD].active : 411 s->vectors[ARMV7M_EXCP_HARD].active) { 412 return true; 413 } 414 415 if (s->vectors[ARMV7M_EXCP_NMI].active && 416 exc_targets_secure(s, ARMV7M_EXCP_NMI) == secure) { 417 return true; 418 } 419 420 return false; 421 } 422 423 bool armv7m_nvic_can_take_pending_exception(void *opaque) 424 { 425 NVICState *s = opaque; 426 427 return nvic_exec_prio(s) > nvic_pending_prio(s); 428 } 429 430 int armv7m_nvic_raw_execution_priority(void *opaque) 431 { 432 NVICState *s = opaque; 433 434 return s->exception_prio; 435 } 436 437 /* caller must call nvic_irq_update() after this. 438 * secure indicates the bank to use for banked exceptions (we assert if 439 * we are passed secure=true for a non-banked exception). 440 */ 441 static void set_prio(NVICState *s, unsigned irq, bool secure, uint8_t prio) 442 { 443 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */ 444 assert(irq < s->num_irq); 445 446 prio &= MAKE_64BIT_MASK(8 - s->num_prio_bits, s->num_prio_bits); 447 448 if (secure) { 449 assert(exc_is_banked(irq)); 450 s->sec_vectors[irq].prio = prio; 451 } else { 452 s->vectors[irq].prio = prio; 453 } 454 455 trace_nvic_set_prio(irq, secure, prio); 456 } 457 458 /* Return the current raw priority register value. 459 * secure indicates the bank to use for banked exceptions (we assert if 460 * we are passed secure=true for a non-banked exception). 461 */ 462 static int get_prio(NVICState *s, unsigned irq, bool secure) 463 { 464 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */ 465 assert(irq < s->num_irq); 466 467 if (secure) { 468 assert(exc_is_banked(irq)); 469 return s->sec_vectors[irq].prio; 470 } else { 471 return s->vectors[irq].prio; 472 } 473 } 474 475 /* Recompute state and assert irq line accordingly. 476 * Must be called after changes to: 477 * vec->active, vec->enabled, vec->pending or vec->prio for any vector 478 * prigroup 479 */ 480 static void nvic_irq_update(NVICState *s) 481 { 482 int lvl; 483 int pend_prio; 484 485 nvic_recompute_state(s); 486 pend_prio = nvic_pending_prio(s); 487 488 /* Raise NVIC output if this IRQ would be taken, except that we 489 * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which 490 * will be checked for in arm_v7m_cpu_exec_interrupt()); changes 491 * to those CPU registers don't cause us to recalculate the NVIC 492 * pending info. 493 */ 494 lvl = (pend_prio < s->exception_prio); 495 trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl); 496 qemu_set_irq(s->excpout, lvl); 497 } 498 499 /** 500 * armv7m_nvic_clear_pending: mark the specified exception as not pending 501 * @opaque: the NVIC 502 * @irq: the exception number to mark as not pending 503 * @secure: false for non-banked exceptions or for the nonsecure 504 * version of a banked exception, true for the secure version of a banked 505 * exception. 506 * 507 * Marks the specified exception as not pending. Note that we will assert() 508 * if @secure is true and @irq does not specify one of the fixed set 509 * of architecturally banked exceptions. 510 */ 511 static void armv7m_nvic_clear_pending(void *opaque, int irq, bool secure) 512 { 513 NVICState *s = (NVICState *)opaque; 514 VecInfo *vec; 515 516 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 517 518 if (secure) { 519 assert(exc_is_banked(irq)); 520 vec = &s->sec_vectors[irq]; 521 } else { 522 vec = &s->vectors[irq]; 523 } 524 trace_nvic_clear_pending(irq, secure, vec->enabled, vec->prio); 525 if (vec->pending) { 526 vec->pending = 0; 527 nvic_irq_update(s); 528 } 529 } 530 531 static void do_armv7m_nvic_set_pending(void *opaque, int irq, bool secure, 532 bool derived) 533 { 534 /* Pend an exception, including possibly escalating it to HardFault. 535 * 536 * This function handles both "normal" pending of interrupts and 537 * exceptions, and also derived exceptions (ones which occur as 538 * a result of trying to take some other exception). 539 * 540 * If derived == true, the caller guarantees that we are part way through 541 * trying to take an exception (but have not yet called 542 * armv7m_nvic_acknowledge_irq() to make it active), and so: 543 * - s->vectpending is the "original exception" we were trying to take 544 * - irq is the "derived exception" 545 * - nvic_exec_prio(s) gives the priority before exception entry 546 * Here we handle the prioritization logic which the pseudocode puts 547 * in the DerivedLateArrival() function. 548 */ 549 550 NVICState *s = (NVICState *)opaque; 551 bool banked = exc_is_banked(irq); 552 VecInfo *vec; 553 bool targets_secure; 554 555 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 556 assert(!secure || banked); 557 558 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq]; 559 560 targets_secure = banked ? secure : exc_targets_secure(s, irq); 561 562 trace_nvic_set_pending(irq, secure, targets_secure, 563 derived, vec->enabled, vec->prio); 564 565 if (derived) { 566 /* Derived exceptions are always synchronous. */ 567 assert(irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV); 568 569 if (irq == ARMV7M_EXCP_DEBUG && 570 exc_group_prio(s, vec->prio, secure) >= nvic_exec_prio(s)) { 571 /* DebugMonitorFault, but its priority is lower than the 572 * preempted exception priority: just ignore it. 573 */ 574 return; 575 } 576 577 if (irq == ARMV7M_EXCP_HARD && vec->prio >= s->vectpending_prio) { 578 /* If this is a terminal exception (one which means we cannot 579 * take the original exception, like a failure to read its 580 * vector table entry), then we must take the derived exception. 581 * If the derived exception can't take priority over the 582 * original exception, then we go into Lockup. 583 * 584 * For QEMU, we rely on the fact that a derived exception is 585 * terminal if and only if it's reported to us as HardFault, 586 * which saves having to have an extra argument is_terminal 587 * that we'd only use in one place. 588 */ 589 cpu_abort(&s->cpu->parent_obj, 590 "Lockup: can't take terminal derived exception " 591 "(original exception priority %d)\n", 592 s->vectpending_prio); 593 } 594 /* We now continue with the same code as for a normal pending 595 * exception, which will cause us to pend the derived exception. 596 * We'll then take either the original or the derived exception 597 * based on which is higher priority by the usual mechanism 598 * for selecting the highest priority pending interrupt. 599 */ 600 } 601 602 if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) { 603 /* If a synchronous exception is pending then it may be 604 * escalated to HardFault if: 605 * * it is equal or lower priority to current execution 606 * * it is disabled 607 * (ie we need to take it immediately but we can't do so). 608 * Asynchronous exceptions (and interrupts) simply remain pending. 609 * 610 * For QEMU, we don't have any imprecise (asynchronous) faults, 611 * so we can assume that PREFETCH_ABORT and DATA_ABORT are always 612 * synchronous. 613 * Debug exceptions are awkward because only Debug exceptions 614 * resulting from the BKPT instruction should be escalated, 615 * but we don't currently implement any Debug exceptions other 616 * than those that result from BKPT, so we treat all debug exceptions 617 * as needing escalation. 618 * 619 * This all means we can identify whether to escalate based only on 620 * the exception number and don't (yet) need the caller to explicitly 621 * tell us whether this exception is synchronous or not. 622 */ 623 int running = nvic_exec_prio(s); 624 bool escalate = false; 625 626 if (exc_group_prio(s, vec->prio, secure) >= running) { 627 trace_nvic_escalate_prio(irq, vec->prio, running); 628 escalate = true; 629 } else if (!vec->enabled) { 630 trace_nvic_escalate_disabled(irq); 631 escalate = true; 632 } 633 634 if (escalate) { 635 636 /* We need to escalate this exception to a synchronous HardFault. 637 * If BFHFNMINS is set then we escalate to the banked HF for 638 * the target security state of the original exception; otherwise 639 * we take a Secure HardFault. 640 */ 641 irq = ARMV7M_EXCP_HARD; 642 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) && 643 (targets_secure || 644 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) { 645 vec = &s->sec_vectors[irq]; 646 } else { 647 vec = &s->vectors[irq]; 648 } 649 if (running <= vec->prio) { 650 /* We want to escalate to HardFault but we can't take the 651 * synchronous HardFault at this point either. This is a 652 * Lockup condition due to a guest bug. We don't model 653 * Lockup, so report via cpu_abort() instead. 654 */ 655 cpu_abort(&s->cpu->parent_obj, 656 "Lockup: can't escalate %d to HardFault " 657 "(current priority %d)\n", irq, running); 658 } 659 660 /* HF may be banked but there is only one shared HFSR */ 661 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK; 662 } 663 } 664 665 if (!vec->pending) { 666 vec->pending = 1; 667 nvic_irq_update(s); 668 } 669 } 670 671 void armv7m_nvic_set_pending(void *opaque, int irq, bool secure) 672 { 673 do_armv7m_nvic_set_pending(opaque, irq, secure, false); 674 } 675 676 void armv7m_nvic_set_pending_derived(void *opaque, int irq, bool secure) 677 { 678 do_armv7m_nvic_set_pending(opaque, irq, secure, true); 679 } 680 681 void armv7m_nvic_set_pending_lazyfp(void *opaque, int irq, bool secure) 682 { 683 /* 684 * Pend an exception during lazy FP stacking. This differs 685 * from the usual exception pending because the logic for 686 * whether we should escalate depends on the saved context 687 * in the FPCCR register, not on the current state of the CPU/NVIC. 688 */ 689 NVICState *s = (NVICState *)opaque; 690 bool banked = exc_is_banked(irq); 691 VecInfo *vec; 692 bool targets_secure; 693 bool escalate = false; 694 /* 695 * We will only look at bits in fpccr if this is a banked exception 696 * (in which case 'secure' tells us whether it is the S or NS version). 697 * All the bits for the non-banked exceptions are in fpccr_s. 698 */ 699 uint32_t fpccr_s = s->cpu->env.v7m.fpccr[M_REG_S]; 700 uint32_t fpccr = s->cpu->env.v7m.fpccr[secure]; 701 702 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 703 assert(!secure || banked); 704 705 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq]; 706 707 targets_secure = banked ? secure : exc_targets_secure(s, irq); 708 709 switch (irq) { 710 case ARMV7M_EXCP_DEBUG: 711 if (!(fpccr_s & R_V7M_FPCCR_MONRDY_MASK)) { 712 /* Ignore DebugMonitor exception */ 713 return; 714 } 715 break; 716 case ARMV7M_EXCP_MEM: 717 escalate = !(fpccr & R_V7M_FPCCR_MMRDY_MASK); 718 break; 719 case ARMV7M_EXCP_USAGE: 720 escalate = !(fpccr & R_V7M_FPCCR_UFRDY_MASK); 721 break; 722 case ARMV7M_EXCP_BUS: 723 escalate = !(fpccr_s & R_V7M_FPCCR_BFRDY_MASK); 724 break; 725 case ARMV7M_EXCP_SECURE: 726 escalate = !(fpccr_s & R_V7M_FPCCR_SFRDY_MASK); 727 break; 728 default: 729 g_assert_not_reached(); 730 } 731 732 if (escalate) { 733 /* 734 * Escalate to HardFault: faults that initially targeted Secure 735 * continue to do so, even if HF normally targets NonSecure. 736 */ 737 irq = ARMV7M_EXCP_HARD; 738 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) && 739 (targets_secure || 740 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) { 741 vec = &s->sec_vectors[irq]; 742 } else { 743 vec = &s->vectors[irq]; 744 } 745 } 746 747 if (!vec->enabled || 748 nvic_exec_prio(s) <= exc_group_prio(s, vec->prio, secure)) { 749 if (!(fpccr_s & R_V7M_FPCCR_HFRDY_MASK)) { 750 /* 751 * We want to escalate to HardFault but the context the 752 * FP state belongs to prevents the exception pre-empting. 753 */ 754 cpu_abort(&s->cpu->parent_obj, 755 "Lockup: can't escalate to HardFault during " 756 "lazy FP register stacking\n"); 757 } 758 } 759 760 if (escalate) { 761 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK; 762 } 763 if (!vec->pending) { 764 vec->pending = 1; 765 /* 766 * We do not call nvic_irq_update(), because we know our caller 767 * is going to handle causing us to take the exception by 768 * raising EXCP_LAZYFP, so raising the IRQ line would be 769 * pointless extra work. We just need to recompute the 770 * priorities so that armv7m_nvic_can_take_pending_exception() 771 * returns the right answer. 772 */ 773 nvic_recompute_state(s); 774 } 775 } 776 777 /* Make pending IRQ active. */ 778 void armv7m_nvic_acknowledge_irq(void *opaque) 779 { 780 NVICState *s = (NVICState *)opaque; 781 CPUARMState *env = &s->cpu->env; 782 const int pending = s->vectpending; 783 const int running = nvic_exec_prio(s); 784 VecInfo *vec; 785 786 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq); 787 788 if (s->vectpending_is_s_banked) { 789 vec = &s->sec_vectors[pending]; 790 } else { 791 vec = &s->vectors[pending]; 792 } 793 794 assert(vec->enabled); 795 assert(vec->pending); 796 797 assert(s->vectpending_prio < running); 798 799 trace_nvic_acknowledge_irq(pending, s->vectpending_prio); 800 801 vec->active = 1; 802 vec->pending = 0; 803 804 write_v7m_exception(env, s->vectpending); 805 806 nvic_irq_update(s); 807 } 808 809 void armv7m_nvic_get_pending_irq_info(void *opaque, 810 int *pirq, bool *ptargets_secure) 811 { 812 NVICState *s = (NVICState *)opaque; 813 const int pending = s->vectpending; 814 bool targets_secure; 815 816 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq); 817 818 if (s->vectpending_is_s_banked) { 819 targets_secure = true; 820 } else { 821 targets_secure = !exc_is_banked(pending) && 822 exc_targets_secure(s, pending); 823 } 824 825 trace_nvic_get_pending_irq_info(pending, targets_secure); 826 827 *ptargets_secure = targets_secure; 828 *pirq = pending; 829 } 830 831 int armv7m_nvic_complete_irq(void *opaque, int irq, bool secure) 832 { 833 NVICState *s = (NVICState *)opaque; 834 VecInfo *vec = NULL; 835 int ret; 836 837 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 838 839 /* 840 * For negative priorities, v8M will forcibly deactivate the appropriate 841 * NMI or HardFault regardless of what interrupt we're being asked to 842 * deactivate (compare the DeActivate() pseudocode). This is a guard 843 * against software returning from NMI or HardFault with a corrupted 844 * IPSR and leaving the CPU in a negative-priority state. 845 * v7M does not do this, but simply deactivates the requested interrupt. 846 */ 847 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) { 848 switch (armv7m_nvic_raw_execution_priority(s)) { 849 case -1: 850 if (s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 851 vec = &s->vectors[ARMV7M_EXCP_HARD]; 852 } else { 853 vec = &s->sec_vectors[ARMV7M_EXCP_HARD]; 854 } 855 break; 856 case -2: 857 vec = &s->vectors[ARMV7M_EXCP_NMI]; 858 break; 859 case -3: 860 vec = &s->sec_vectors[ARMV7M_EXCP_HARD]; 861 break; 862 default: 863 break; 864 } 865 } 866 867 if (!vec) { 868 if (secure && exc_is_banked(irq)) { 869 vec = &s->sec_vectors[irq]; 870 } else { 871 vec = &s->vectors[irq]; 872 } 873 } 874 875 trace_nvic_complete_irq(irq, secure); 876 877 if (!vec->active) { 878 /* Tell the caller this was an illegal exception return */ 879 return -1; 880 } 881 882 /* 883 * If this is a configurable exception and it is currently 884 * targeting the opposite security state from the one we're trying 885 * to complete it for, this counts as an illegal exception return. 886 * We still need to deactivate whatever vector the logic above has 887 * selected, though, as it might not be the same as the one for the 888 * requested exception number. 889 */ 890 if (!exc_is_banked(irq) && exc_targets_secure(s, irq) != secure) { 891 ret = -1; 892 } else { 893 ret = nvic_rettobase(s); 894 } 895 896 vec->active = 0; 897 if (vec->level) { 898 /* Re-pend the exception if it's still held high; only 899 * happens for extenal IRQs 900 */ 901 assert(irq >= NVIC_FIRST_IRQ); 902 vec->pending = 1; 903 } 904 905 nvic_irq_update(s); 906 907 return ret; 908 } 909 910 bool armv7m_nvic_get_ready_status(void *opaque, int irq, bool secure) 911 { 912 /* 913 * Return whether an exception is "ready", i.e. it is enabled and is 914 * configured at a priority which would allow it to interrupt the 915 * current execution priority. 916 * 917 * irq and secure have the same semantics as for armv7m_nvic_set_pending(): 918 * for non-banked exceptions secure is always false; for banked exceptions 919 * it indicates which of the exceptions is required. 920 */ 921 NVICState *s = (NVICState *)opaque; 922 bool banked = exc_is_banked(irq); 923 VecInfo *vec; 924 int running = nvic_exec_prio(s); 925 926 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); 927 assert(!secure || banked); 928 929 /* 930 * HardFault is an odd special case: we always check against -1, 931 * even if we're secure and HardFault has priority -3; we never 932 * need to check for enabled state. 933 */ 934 if (irq == ARMV7M_EXCP_HARD) { 935 return running > -1; 936 } 937 938 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq]; 939 940 return vec->enabled && 941 exc_group_prio(s, vec->prio, secure) < running; 942 } 943 944 /* callback when external interrupt line is changed */ 945 static void set_irq_level(void *opaque, int n, int level) 946 { 947 NVICState *s = opaque; 948 VecInfo *vec; 949 950 n += NVIC_FIRST_IRQ; 951 952 assert(n >= NVIC_FIRST_IRQ && n < s->num_irq); 953 954 trace_nvic_set_irq_level(n, level); 955 956 /* The pending status of an external interrupt is 957 * latched on rising edge and exception handler return. 958 * 959 * Pulsing the IRQ will always run the handler 960 * once, and the handler will re-run until the 961 * level is low when the handler completes. 962 */ 963 vec = &s->vectors[n]; 964 if (level != vec->level) { 965 vec->level = level; 966 if (level) { 967 armv7m_nvic_set_pending(s, n, false); 968 } 969 } 970 } 971 972 /* callback when external NMI line is changed */ 973 static void nvic_nmi_trigger(void *opaque, int n, int level) 974 { 975 NVICState *s = opaque; 976 977 trace_nvic_set_nmi_level(level); 978 979 /* 980 * The architecture doesn't specify whether NMI should share 981 * the normal-interrupt behaviour of being resampled on 982 * exception handler return. We choose not to, so just 983 * set NMI pending here and don't track the current level. 984 */ 985 if (level) { 986 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false); 987 } 988 } 989 990 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs) 991 { 992 ARMCPU *cpu = s->cpu; 993 uint32_t val; 994 995 switch (offset) { 996 case 4: /* Interrupt Control Type. */ 997 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) { 998 goto bad_offset; 999 } 1000 return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1; 1001 case 0xc: /* CPPWR */ 1002 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1003 goto bad_offset; 1004 } 1005 /* We make the IMPDEF choice that nothing can ever go into a 1006 * non-retentive power state, which allows us to RAZ/WI this. 1007 */ 1008 return 0; 1009 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */ 1010 { 1011 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ; 1012 int i; 1013 1014 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1015 goto bad_offset; 1016 } 1017 if (!attrs.secure) { 1018 return 0; 1019 } 1020 val = 0; 1021 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) { 1022 if (s->itns[startvec + i]) { 1023 val |= (1 << i); 1024 } 1025 } 1026 return val; 1027 } 1028 case 0xcfc: 1029 if (!arm_feature(&cpu->env, ARM_FEATURE_V8_1M)) { 1030 goto bad_offset; 1031 } 1032 return cpu->revidr; 1033 case 0xd00: /* CPUID Base. */ 1034 return cpu->midr; 1035 case 0xd04: /* Interrupt Control State (ICSR) */ 1036 /* VECTACTIVE */ 1037 val = cpu->env.v7m.exception; 1038 /* VECTPENDING */ 1039 val |= (s->vectpending & 0xff) << 12; 1040 /* ISRPENDING - set if any external IRQ is pending */ 1041 if (nvic_isrpending(s)) { 1042 val |= (1 << 22); 1043 } 1044 /* RETTOBASE - set if only one handler is active */ 1045 if (nvic_rettobase(s)) { 1046 val |= (1 << 11); 1047 } 1048 if (attrs.secure) { 1049 /* PENDSTSET */ 1050 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) { 1051 val |= (1 << 26); 1052 } 1053 /* PENDSVSET */ 1054 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) { 1055 val |= (1 << 28); 1056 } 1057 } else { 1058 /* PENDSTSET */ 1059 if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) { 1060 val |= (1 << 26); 1061 } 1062 /* PENDSVSET */ 1063 if (s->vectors[ARMV7M_EXCP_PENDSV].pending) { 1064 val |= (1 << 28); 1065 } 1066 } 1067 /* NMIPENDSET */ 1068 if ((attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) 1069 && s->vectors[ARMV7M_EXCP_NMI].pending) { 1070 val |= (1 << 31); 1071 } 1072 /* ISRPREEMPT: RES0 when halting debug not implemented */ 1073 /* STTNS: RES0 for the Main Extension */ 1074 return val; 1075 case 0xd08: /* Vector Table Offset. */ 1076 return cpu->env.v7m.vecbase[attrs.secure]; 1077 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */ 1078 val = 0xfa050000 | (s->prigroup[attrs.secure] << 8); 1079 if (attrs.secure) { 1080 /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */ 1081 val |= cpu->env.v7m.aircr; 1082 } else { 1083 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1084 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If 1085 * security isn't supported then BFHFNMINS is RAO (and 1086 * the bit in env.v7m.aircr is always set). 1087 */ 1088 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK; 1089 } 1090 } 1091 return val; 1092 case 0xd10: /* System Control. */ 1093 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) { 1094 goto bad_offset; 1095 } 1096 return cpu->env.v7m.scr[attrs.secure]; 1097 case 0xd14: /* Configuration Control. */ 1098 /* 1099 * Non-banked bits: BFHFNMIGN (stored in the NS copy of the register) 1100 * and TRD (stored in the S copy of the register) 1101 */ 1102 val = cpu->env.v7m.ccr[attrs.secure]; 1103 val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK; 1104 return val; 1105 case 0xd24: /* System Handler Control and State (SHCSR) */ 1106 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) { 1107 goto bad_offset; 1108 } 1109 val = 0; 1110 if (attrs.secure) { 1111 if (s->sec_vectors[ARMV7M_EXCP_MEM].active) { 1112 val |= (1 << 0); 1113 } 1114 if (s->sec_vectors[ARMV7M_EXCP_HARD].active) { 1115 val |= (1 << 2); 1116 } 1117 if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) { 1118 val |= (1 << 3); 1119 } 1120 if (s->sec_vectors[ARMV7M_EXCP_SVC].active) { 1121 val |= (1 << 7); 1122 } 1123 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) { 1124 val |= (1 << 10); 1125 } 1126 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) { 1127 val |= (1 << 11); 1128 } 1129 if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) { 1130 val |= (1 << 12); 1131 } 1132 if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) { 1133 val |= (1 << 13); 1134 } 1135 if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) { 1136 val |= (1 << 15); 1137 } 1138 if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) { 1139 val |= (1 << 16); 1140 } 1141 if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) { 1142 val |= (1 << 18); 1143 } 1144 if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) { 1145 val |= (1 << 21); 1146 } 1147 /* SecureFault is not banked but is always RAZ/WI to NS */ 1148 if (s->vectors[ARMV7M_EXCP_SECURE].active) { 1149 val |= (1 << 4); 1150 } 1151 if (s->vectors[ARMV7M_EXCP_SECURE].enabled) { 1152 val |= (1 << 19); 1153 } 1154 if (s->vectors[ARMV7M_EXCP_SECURE].pending) { 1155 val |= (1 << 20); 1156 } 1157 } else { 1158 if (s->vectors[ARMV7M_EXCP_MEM].active) { 1159 val |= (1 << 0); 1160 } 1161 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1162 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */ 1163 if (s->vectors[ARMV7M_EXCP_HARD].active) { 1164 val |= (1 << 2); 1165 } 1166 if (s->vectors[ARMV7M_EXCP_HARD].pending) { 1167 val |= (1 << 21); 1168 } 1169 } 1170 if (s->vectors[ARMV7M_EXCP_USAGE].active) { 1171 val |= (1 << 3); 1172 } 1173 if (s->vectors[ARMV7M_EXCP_SVC].active) { 1174 val |= (1 << 7); 1175 } 1176 if (s->vectors[ARMV7M_EXCP_PENDSV].active) { 1177 val |= (1 << 10); 1178 } 1179 if (s->vectors[ARMV7M_EXCP_SYSTICK].active) { 1180 val |= (1 << 11); 1181 } 1182 if (s->vectors[ARMV7M_EXCP_USAGE].pending) { 1183 val |= (1 << 12); 1184 } 1185 if (s->vectors[ARMV7M_EXCP_MEM].pending) { 1186 val |= (1 << 13); 1187 } 1188 if (s->vectors[ARMV7M_EXCP_SVC].pending) { 1189 val |= (1 << 15); 1190 } 1191 if (s->vectors[ARMV7M_EXCP_MEM].enabled) { 1192 val |= (1 << 16); 1193 } 1194 if (s->vectors[ARMV7M_EXCP_USAGE].enabled) { 1195 val |= (1 << 18); 1196 } 1197 } 1198 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1199 if (s->vectors[ARMV7M_EXCP_BUS].active) { 1200 val |= (1 << 1); 1201 } 1202 if (s->vectors[ARMV7M_EXCP_BUS].pending) { 1203 val |= (1 << 14); 1204 } 1205 if (s->vectors[ARMV7M_EXCP_BUS].enabled) { 1206 val |= (1 << 17); 1207 } 1208 if (arm_feature(&cpu->env, ARM_FEATURE_V8) && 1209 s->vectors[ARMV7M_EXCP_NMI].active) { 1210 /* NMIACT is not present in v7M */ 1211 val |= (1 << 5); 1212 } 1213 } 1214 1215 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */ 1216 if (s->vectors[ARMV7M_EXCP_DEBUG].active) { 1217 val |= (1 << 8); 1218 } 1219 return val; 1220 case 0xd2c: /* Hard Fault Status. */ 1221 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1222 goto bad_offset; 1223 } 1224 return cpu->env.v7m.hfsr; 1225 case 0xd30: /* Debug Fault Status. */ 1226 return cpu->env.v7m.dfsr; 1227 case 0xd34: /* MMFAR MemManage Fault Address */ 1228 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1229 goto bad_offset; 1230 } 1231 return cpu->env.v7m.mmfar[attrs.secure]; 1232 case 0xd38: /* Bus Fault Address. */ 1233 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1234 goto bad_offset; 1235 } 1236 if (!attrs.secure && 1237 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1238 return 0; 1239 } 1240 return cpu->env.v7m.bfar; 1241 case 0xd3c: /* Aux Fault Status. */ 1242 /* TODO: Implement fault status registers. */ 1243 qemu_log_mask(LOG_UNIMP, 1244 "Aux Fault status registers unimplemented\n"); 1245 return 0; 1246 case 0xd40: /* PFR0. */ 1247 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1248 goto bad_offset; 1249 } 1250 return cpu->isar.id_pfr0; 1251 case 0xd44: /* PFR1. */ 1252 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1253 goto bad_offset; 1254 } 1255 return cpu->isar.id_pfr1; 1256 case 0xd48: /* DFR0. */ 1257 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1258 goto bad_offset; 1259 } 1260 return cpu->isar.id_dfr0; 1261 case 0xd4c: /* AFR0. */ 1262 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1263 goto bad_offset; 1264 } 1265 return cpu->id_afr0; 1266 case 0xd50: /* MMFR0. */ 1267 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1268 goto bad_offset; 1269 } 1270 return cpu->isar.id_mmfr0; 1271 case 0xd54: /* MMFR1. */ 1272 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1273 goto bad_offset; 1274 } 1275 return cpu->isar.id_mmfr1; 1276 case 0xd58: /* MMFR2. */ 1277 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1278 goto bad_offset; 1279 } 1280 return cpu->isar.id_mmfr2; 1281 case 0xd5c: /* MMFR3. */ 1282 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1283 goto bad_offset; 1284 } 1285 return cpu->isar.id_mmfr3; 1286 case 0xd60: /* ISAR0. */ 1287 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1288 goto bad_offset; 1289 } 1290 return cpu->isar.id_isar0; 1291 case 0xd64: /* ISAR1. */ 1292 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1293 goto bad_offset; 1294 } 1295 return cpu->isar.id_isar1; 1296 case 0xd68: /* ISAR2. */ 1297 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1298 goto bad_offset; 1299 } 1300 return cpu->isar.id_isar2; 1301 case 0xd6c: /* ISAR3. */ 1302 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1303 goto bad_offset; 1304 } 1305 return cpu->isar.id_isar3; 1306 case 0xd70: /* ISAR4. */ 1307 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1308 goto bad_offset; 1309 } 1310 return cpu->isar.id_isar4; 1311 case 0xd74: /* ISAR5. */ 1312 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1313 goto bad_offset; 1314 } 1315 return cpu->isar.id_isar5; 1316 case 0xd78: /* CLIDR */ 1317 return cpu->clidr; 1318 case 0xd7c: /* CTR */ 1319 return cpu->ctr; 1320 case 0xd80: /* CSSIDR */ 1321 { 1322 int idx = cpu->env.v7m.csselr[attrs.secure] & R_V7M_CSSELR_INDEX_MASK; 1323 return cpu->ccsidr[idx]; 1324 } 1325 case 0xd84: /* CSSELR */ 1326 return cpu->env.v7m.csselr[attrs.secure]; 1327 case 0xd88: /* CPACR */ 1328 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) { 1329 return 0; 1330 } 1331 return cpu->env.v7m.cpacr[attrs.secure]; 1332 case 0xd8c: /* NSACR */ 1333 if (!attrs.secure || !cpu_isar_feature(aa32_vfp_simd, cpu)) { 1334 return 0; 1335 } 1336 return cpu->env.v7m.nsacr; 1337 /* TODO: Implement debug registers. */ 1338 case 0xd90: /* MPU_TYPE */ 1339 /* Unified MPU; if the MPU is not present this value is zero */ 1340 return cpu->pmsav7_dregion << 8; 1341 case 0xd94: /* MPU_CTRL */ 1342 return cpu->env.v7m.mpu_ctrl[attrs.secure]; 1343 case 0xd98: /* MPU_RNR */ 1344 return cpu->env.pmsav7.rnr[attrs.secure]; 1345 case 0xd9c: /* MPU_RBAR */ 1346 case 0xda4: /* MPU_RBAR_A1 */ 1347 case 0xdac: /* MPU_RBAR_A2 */ 1348 case 0xdb4: /* MPU_RBAR_A3 */ 1349 { 1350 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1351 1352 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1353 /* PMSAv8M handling of the aliases is different from v7M: 1354 * aliases A1, A2, A3 override the low two bits of the region 1355 * number in MPU_RNR, and there is no 'region' field in the 1356 * RBAR register. 1357 */ 1358 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1359 if (aliasno) { 1360 region = deposit32(region, 0, 2, aliasno); 1361 } 1362 if (region >= cpu->pmsav7_dregion) { 1363 return 0; 1364 } 1365 return cpu->env.pmsav8.rbar[attrs.secure][region]; 1366 } 1367 1368 if (region >= cpu->pmsav7_dregion) { 1369 return 0; 1370 } 1371 return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf); 1372 } 1373 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */ 1374 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */ 1375 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */ 1376 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */ 1377 { 1378 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1379 1380 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1381 /* PMSAv8M handling of the aliases is different from v7M: 1382 * aliases A1, A2, A3 override the low two bits of the region 1383 * number in MPU_RNR. 1384 */ 1385 int aliasno = (offset - 0xda0) / 8; /* 0..3 */ 1386 if (aliasno) { 1387 region = deposit32(region, 0, 2, aliasno); 1388 } 1389 if (region >= cpu->pmsav7_dregion) { 1390 return 0; 1391 } 1392 return cpu->env.pmsav8.rlar[attrs.secure][region]; 1393 } 1394 1395 if (region >= cpu->pmsav7_dregion) { 1396 return 0; 1397 } 1398 return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) | 1399 (cpu->env.pmsav7.drsr[region] & 0xffff); 1400 } 1401 case 0xdc0: /* MPU_MAIR0 */ 1402 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1403 goto bad_offset; 1404 } 1405 return cpu->env.pmsav8.mair0[attrs.secure]; 1406 case 0xdc4: /* MPU_MAIR1 */ 1407 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1408 goto bad_offset; 1409 } 1410 return cpu->env.pmsav8.mair1[attrs.secure]; 1411 case 0xdd0: /* SAU_CTRL */ 1412 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1413 goto bad_offset; 1414 } 1415 if (!attrs.secure) { 1416 return 0; 1417 } 1418 return cpu->env.sau.ctrl; 1419 case 0xdd4: /* SAU_TYPE */ 1420 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1421 goto bad_offset; 1422 } 1423 if (!attrs.secure) { 1424 return 0; 1425 } 1426 return cpu->sau_sregion; 1427 case 0xdd8: /* SAU_RNR */ 1428 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1429 goto bad_offset; 1430 } 1431 if (!attrs.secure) { 1432 return 0; 1433 } 1434 return cpu->env.sau.rnr; 1435 case 0xddc: /* SAU_RBAR */ 1436 { 1437 int region = cpu->env.sau.rnr; 1438 1439 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1440 goto bad_offset; 1441 } 1442 if (!attrs.secure) { 1443 return 0; 1444 } 1445 if (region >= cpu->sau_sregion) { 1446 return 0; 1447 } 1448 return cpu->env.sau.rbar[region]; 1449 } 1450 case 0xde0: /* SAU_RLAR */ 1451 { 1452 int region = cpu->env.sau.rnr; 1453 1454 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1455 goto bad_offset; 1456 } 1457 if (!attrs.secure) { 1458 return 0; 1459 } 1460 if (region >= cpu->sau_sregion) { 1461 return 0; 1462 } 1463 return cpu->env.sau.rlar[region]; 1464 } 1465 case 0xde4: /* SFSR */ 1466 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1467 goto bad_offset; 1468 } 1469 if (!attrs.secure) { 1470 return 0; 1471 } 1472 return cpu->env.v7m.sfsr; 1473 case 0xde8: /* SFAR */ 1474 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1475 goto bad_offset; 1476 } 1477 if (!attrs.secure) { 1478 return 0; 1479 } 1480 return cpu->env.v7m.sfar; 1481 case 0xf34: /* FPCCR */ 1482 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) { 1483 return 0; 1484 } 1485 if (attrs.secure) { 1486 return cpu->env.v7m.fpccr[M_REG_S]; 1487 } else { 1488 /* 1489 * NS can read LSPEN, CLRONRET and MONRDY. It can read 1490 * BFRDY and HFRDY if AIRCR.BFHFNMINS != 0; 1491 * other non-banked bits RAZ. 1492 * TODO: MONRDY should RAZ/WI if DEMCR.SDME is set. 1493 */ 1494 uint32_t value = cpu->env.v7m.fpccr[M_REG_S]; 1495 uint32_t mask = R_V7M_FPCCR_LSPEN_MASK | 1496 R_V7M_FPCCR_CLRONRET_MASK | 1497 R_V7M_FPCCR_MONRDY_MASK; 1498 1499 if (s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1500 mask |= R_V7M_FPCCR_BFRDY_MASK | R_V7M_FPCCR_HFRDY_MASK; 1501 } 1502 1503 value &= mask; 1504 1505 value |= cpu->env.v7m.fpccr[M_REG_NS]; 1506 return value; 1507 } 1508 case 0xf38: /* FPCAR */ 1509 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) { 1510 return 0; 1511 } 1512 return cpu->env.v7m.fpcar[attrs.secure]; 1513 case 0xf3c: /* FPDSCR */ 1514 if (!cpu_isar_feature(aa32_vfp_simd, cpu)) { 1515 return 0; 1516 } 1517 return cpu->env.v7m.fpdscr[attrs.secure]; 1518 case 0xf40: /* MVFR0 */ 1519 return cpu->isar.mvfr0; 1520 case 0xf44: /* MVFR1 */ 1521 return cpu->isar.mvfr1; 1522 case 0xf48: /* MVFR2 */ 1523 return cpu->isar.mvfr2; 1524 default: 1525 bad_offset: 1526 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset); 1527 return 0; 1528 } 1529 } 1530 1531 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value, 1532 MemTxAttrs attrs) 1533 { 1534 ARMCPU *cpu = s->cpu; 1535 1536 switch (offset) { 1537 case 0xc: /* CPPWR */ 1538 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1539 goto bad_offset; 1540 } 1541 /* Make the IMPDEF choice to RAZ/WI this. */ 1542 break; 1543 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */ 1544 { 1545 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ; 1546 int i; 1547 1548 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1549 goto bad_offset; 1550 } 1551 if (!attrs.secure) { 1552 break; 1553 } 1554 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) { 1555 s->itns[startvec + i] = (value >> i) & 1; 1556 } 1557 nvic_irq_update(s); 1558 break; 1559 } 1560 case 0xd04: /* Interrupt Control State (ICSR) */ 1561 if (attrs.secure || cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1562 if (value & (1 << 31)) { 1563 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false); 1564 } else if (value & (1 << 30) && 1565 arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1566 /* PENDNMICLR didn't exist in v7M */ 1567 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false); 1568 } 1569 } 1570 if (value & (1 << 28)) { 1571 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure); 1572 } else if (value & (1 << 27)) { 1573 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure); 1574 } 1575 if (value & (1 << 26)) { 1576 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure); 1577 } else if (value & (1 << 25)) { 1578 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure); 1579 } 1580 break; 1581 case 0xd08: /* Vector Table Offset. */ 1582 cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80; 1583 break; 1584 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */ 1585 if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) { 1586 if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) { 1587 if (attrs.secure || 1588 !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) { 1589 signal_sysresetreq(s); 1590 } 1591 } 1592 if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) { 1593 qemu_log_mask(LOG_GUEST_ERROR, 1594 "Setting VECTCLRACTIVE when not in DEBUG mode " 1595 "is UNPREDICTABLE\n"); 1596 } 1597 if (value & R_V7M_AIRCR_VECTRESET_MASK) { 1598 /* NB: this bit is RES0 in v8M */ 1599 qemu_log_mask(LOG_GUEST_ERROR, 1600 "Setting VECTRESET when not in DEBUG mode " 1601 "is UNPREDICTABLE\n"); 1602 } 1603 if (arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1604 s->prigroup[attrs.secure] = 1605 extract32(value, 1606 R_V7M_AIRCR_PRIGROUP_SHIFT, 1607 R_V7M_AIRCR_PRIGROUP_LENGTH); 1608 } 1609 if (attrs.secure) { 1610 /* These bits are only writable by secure */ 1611 cpu->env.v7m.aircr = value & 1612 (R_V7M_AIRCR_SYSRESETREQS_MASK | 1613 R_V7M_AIRCR_BFHFNMINS_MASK | 1614 R_V7M_AIRCR_PRIS_MASK); 1615 /* BFHFNMINS changes the priority of Secure HardFault, and 1616 * allows a pending Non-secure HardFault to preempt (which 1617 * we implement by marking it enabled). 1618 */ 1619 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) { 1620 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3; 1621 s->vectors[ARMV7M_EXCP_HARD].enabled = 1; 1622 } else { 1623 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1; 1624 s->vectors[ARMV7M_EXCP_HARD].enabled = 0; 1625 } 1626 } 1627 nvic_irq_update(s); 1628 } 1629 break; 1630 case 0xd10: /* System Control. */ 1631 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) { 1632 goto bad_offset; 1633 } 1634 /* We don't implement deep-sleep so these bits are RAZ/WI. 1635 * The other bits in the register are banked. 1636 * QEMU's implementation ignores SEVONPEND and SLEEPONEXIT, which 1637 * is architecturally permitted. 1638 */ 1639 value &= ~(R_V7M_SCR_SLEEPDEEP_MASK | R_V7M_SCR_SLEEPDEEPS_MASK); 1640 cpu->env.v7m.scr[attrs.secure] = value; 1641 break; 1642 case 0xd14: /* Configuration Control. */ 1643 { 1644 uint32_t mask; 1645 1646 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1647 goto bad_offset; 1648 } 1649 1650 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */ 1651 mask = R_V7M_CCR_STKALIGN_MASK | 1652 R_V7M_CCR_BFHFNMIGN_MASK | 1653 R_V7M_CCR_DIV_0_TRP_MASK | 1654 R_V7M_CCR_UNALIGN_TRP_MASK | 1655 R_V7M_CCR_USERSETMPEND_MASK | 1656 R_V7M_CCR_NONBASETHRDENA_MASK; 1657 if (arm_feature(&cpu->env, ARM_FEATURE_V8_1M) && attrs.secure) { 1658 /* TRD is always RAZ/WI from NS */ 1659 mask |= R_V7M_CCR_TRD_MASK; 1660 } 1661 value &= mask; 1662 1663 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1664 /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */ 1665 value |= R_V7M_CCR_NONBASETHRDENA_MASK 1666 | R_V7M_CCR_STKALIGN_MASK; 1667 } 1668 if (attrs.secure) { 1669 /* the BFHFNMIGN bit is not banked; keep that in the NS copy */ 1670 cpu->env.v7m.ccr[M_REG_NS] = 1671 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK) 1672 | (value & R_V7M_CCR_BFHFNMIGN_MASK); 1673 value &= ~R_V7M_CCR_BFHFNMIGN_MASK; 1674 } 1675 1676 cpu->env.v7m.ccr[attrs.secure] = value; 1677 break; 1678 } 1679 case 0xd24: /* System Handler Control and State (SHCSR) */ 1680 if (!arm_feature(&cpu->env, ARM_FEATURE_V7)) { 1681 goto bad_offset; 1682 } 1683 if (attrs.secure) { 1684 s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; 1685 /* Secure HardFault active bit cannot be written */ 1686 s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; 1687 s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; 1688 s->sec_vectors[ARMV7M_EXCP_PENDSV].active = 1689 (value & (1 << 10)) != 0; 1690 s->sec_vectors[ARMV7M_EXCP_SYSTICK].active = 1691 (value & (1 << 11)) != 0; 1692 s->sec_vectors[ARMV7M_EXCP_USAGE].pending = 1693 (value & (1 << 12)) != 0; 1694 s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; 1695 s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; 1696 s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; 1697 s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; 1698 s->sec_vectors[ARMV7M_EXCP_USAGE].enabled = 1699 (value & (1 << 18)) != 0; 1700 s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0; 1701 /* SecureFault not banked, but RAZ/WI to NS */ 1702 s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0; 1703 s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0; 1704 s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0; 1705 } else { 1706 s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; 1707 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1708 /* HARDFAULTPENDED is not present in v7M */ 1709 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0; 1710 } 1711 s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; 1712 s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; 1713 s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0; 1714 s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0; 1715 s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0; 1716 s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; 1717 s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; 1718 s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; 1719 s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0; 1720 } 1721 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1722 s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0; 1723 s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0; 1724 s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; 1725 } 1726 /* NMIACT can only be written if the write is of a zero, with 1727 * BFHFNMINS 1, and by the CPU in secure state via the NS alias. 1728 */ 1729 if (!attrs.secure && cpu->env.v7m.secure && 1730 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 1731 (value & (1 << 5)) == 0) { 1732 s->vectors[ARMV7M_EXCP_NMI].active = 0; 1733 } 1734 /* HARDFAULTACT can only be written if the write is of a zero 1735 * to the non-secure HardFault state by the CPU in secure state. 1736 * The only case where we can be targeting the non-secure HF state 1737 * when in secure state is if this is a write via the NS alias 1738 * and BFHFNMINS is 1. 1739 */ 1740 if (!attrs.secure && cpu->env.v7m.secure && 1741 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) && 1742 (value & (1 << 2)) == 0) { 1743 s->vectors[ARMV7M_EXCP_HARD].active = 0; 1744 } 1745 1746 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */ 1747 s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0; 1748 nvic_irq_update(s); 1749 break; 1750 case 0xd2c: /* Hard Fault Status. */ 1751 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1752 goto bad_offset; 1753 } 1754 cpu->env.v7m.hfsr &= ~value; /* W1C */ 1755 break; 1756 case 0xd30: /* Debug Fault Status. */ 1757 cpu->env.v7m.dfsr &= ~value; /* W1C */ 1758 break; 1759 case 0xd34: /* Mem Manage Address. */ 1760 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1761 goto bad_offset; 1762 } 1763 cpu->env.v7m.mmfar[attrs.secure] = value; 1764 return; 1765 case 0xd38: /* Bus Fault Address. */ 1766 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 1767 goto bad_offset; 1768 } 1769 if (!attrs.secure && 1770 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 1771 return; 1772 } 1773 cpu->env.v7m.bfar = value; 1774 return; 1775 case 0xd3c: /* Aux Fault Status. */ 1776 qemu_log_mask(LOG_UNIMP, 1777 "NVIC: Aux fault status registers unimplemented\n"); 1778 break; 1779 case 0xd84: /* CSSELR */ 1780 if (!arm_v7m_csselr_razwi(cpu)) { 1781 cpu->env.v7m.csselr[attrs.secure] = value & R_V7M_CSSELR_INDEX_MASK; 1782 } 1783 break; 1784 case 0xd88: /* CPACR */ 1785 if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 1786 /* We implement only the Floating Point extension's CP10/CP11 */ 1787 cpu->env.v7m.cpacr[attrs.secure] = value & (0xf << 20); 1788 } 1789 break; 1790 case 0xd8c: /* NSACR */ 1791 if (attrs.secure && cpu_isar_feature(aa32_vfp_simd, cpu)) { 1792 /* We implement only the Floating Point extension's CP10/CP11 */ 1793 cpu->env.v7m.nsacr = value & (3 << 10); 1794 } 1795 break; 1796 case 0xd90: /* MPU_TYPE */ 1797 return; /* RO */ 1798 case 0xd94: /* MPU_CTRL */ 1799 if ((value & 1800 (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK)) 1801 == R_V7M_MPU_CTRL_HFNMIENA_MASK) { 1802 qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is " 1803 "UNPREDICTABLE\n"); 1804 } 1805 cpu->env.v7m.mpu_ctrl[attrs.secure] 1806 = value & (R_V7M_MPU_CTRL_ENABLE_MASK | 1807 R_V7M_MPU_CTRL_HFNMIENA_MASK | 1808 R_V7M_MPU_CTRL_PRIVDEFENA_MASK); 1809 tlb_flush(CPU(cpu)); 1810 break; 1811 case 0xd98: /* MPU_RNR */ 1812 if (value >= cpu->pmsav7_dregion) { 1813 qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %" 1814 PRIu32 "/%" PRIu32 "\n", 1815 value, cpu->pmsav7_dregion); 1816 } else { 1817 cpu->env.pmsav7.rnr[attrs.secure] = value; 1818 } 1819 break; 1820 case 0xd9c: /* MPU_RBAR */ 1821 case 0xda4: /* MPU_RBAR_A1 */ 1822 case 0xdac: /* MPU_RBAR_A2 */ 1823 case 0xdb4: /* MPU_RBAR_A3 */ 1824 { 1825 int region; 1826 1827 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1828 /* PMSAv8M handling of the aliases is different from v7M: 1829 * aliases A1, A2, A3 override the low two bits of the region 1830 * number in MPU_RNR, and there is no 'region' field in the 1831 * RBAR register. 1832 */ 1833 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1834 1835 region = cpu->env.pmsav7.rnr[attrs.secure]; 1836 if (aliasno) { 1837 region = deposit32(region, 0, 2, aliasno); 1838 } 1839 if (region >= cpu->pmsav7_dregion) { 1840 return; 1841 } 1842 cpu->env.pmsav8.rbar[attrs.secure][region] = value; 1843 tlb_flush(CPU(cpu)); 1844 return; 1845 } 1846 1847 if (value & (1 << 4)) { 1848 /* VALID bit means use the region number specified in this 1849 * value and also update MPU_RNR.REGION with that value. 1850 */ 1851 region = extract32(value, 0, 4); 1852 if (region >= cpu->pmsav7_dregion) { 1853 qemu_log_mask(LOG_GUEST_ERROR, 1854 "MPU region out of range %u/%" PRIu32 "\n", 1855 region, cpu->pmsav7_dregion); 1856 return; 1857 } 1858 cpu->env.pmsav7.rnr[attrs.secure] = region; 1859 } else { 1860 region = cpu->env.pmsav7.rnr[attrs.secure]; 1861 } 1862 1863 if (region >= cpu->pmsav7_dregion) { 1864 return; 1865 } 1866 1867 cpu->env.pmsav7.drbar[region] = value & ~0x1f; 1868 tlb_flush(CPU(cpu)); 1869 break; 1870 } 1871 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */ 1872 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */ 1873 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */ 1874 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */ 1875 { 1876 int region = cpu->env.pmsav7.rnr[attrs.secure]; 1877 1878 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1879 /* PMSAv8M handling of the aliases is different from v7M: 1880 * aliases A1, A2, A3 override the low two bits of the region 1881 * number in MPU_RNR. 1882 */ 1883 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */ 1884 1885 region = cpu->env.pmsav7.rnr[attrs.secure]; 1886 if (aliasno) { 1887 region = deposit32(region, 0, 2, aliasno); 1888 } 1889 if (region >= cpu->pmsav7_dregion) { 1890 return; 1891 } 1892 cpu->env.pmsav8.rlar[attrs.secure][region] = value; 1893 tlb_flush(CPU(cpu)); 1894 return; 1895 } 1896 1897 if (region >= cpu->pmsav7_dregion) { 1898 return; 1899 } 1900 1901 cpu->env.pmsav7.drsr[region] = value & 0xff3f; 1902 cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f; 1903 tlb_flush(CPU(cpu)); 1904 break; 1905 } 1906 case 0xdc0: /* MPU_MAIR0 */ 1907 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1908 goto bad_offset; 1909 } 1910 if (cpu->pmsav7_dregion) { 1911 /* Register is RES0 if no MPU regions are implemented */ 1912 cpu->env.pmsav8.mair0[attrs.secure] = value; 1913 } 1914 /* We don't need to do anything else because memory attributes 1915 * only affect cacheability, and we don't implement caching. 1916 */ 1917 break; 1918 case 0xdc4: /* MPU_MAIR1 */ 1919 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1920 goto bad_offset; 1921 } 1922 if (cpu->pmsav7_dregion) { 1923 /* Register is RES0 if no MPU regions are implemented */ 1924 cpu->env.pmsav8.mair1[attrs.secure] = value; 1925 } 1926 /* We don't need to do anything else because memory attributes 1927 * only affect cacheability, and we don't implement caching. 1928 */ 1929 break; 1930 case 0xdd0: /* SAU_CTRL */ 1931 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1932 goto bad_offset; 1933 } 1934 if (!attrs.secure) { 1935 return; 1936 } 1937 cpu->env.sau.ctrl = value & 3; 1938 break; 1939 case 0xdd4: /* SAU_TYPE */ 1940 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1941 goto bad_offset; 1942 } 1943 break; 1944 case 0xdd8: /* SAU_RNR */ 1945 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1946 goto bad_offset; 1947 } 1948 if (!attrs.secure) { 1949 return; 1950 } 1951 if (value >= cpu->sau_sregion) { 1952 qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %" 1953 PRIu32 "/%" PRIu32 "\n", 1954 value, cpu->sau_sregion); 1955 } else { 1956 cpu->env.sau.rnr = value; 1957 } 1958 break; 1959 case 0xddc: /* SAU_RBAR */ 1960 { 1961 int region = cpu->env.sau.rnr; 1962 1963 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1964 goto bad_offset; 1965 } 1966 if (!attrs.secure) { 1967 return; 1968 } 1969 if (region >= cpu->sau_sregion) { 1970 return; 1971 } 1972 cpu->env.sau.rbar[region] = value & ~0x1f; 1973 tlb_flush(CPU(cpu)); 1974 break; 1975 } 1976 case 0xde0: /* SAU_RLAR */ 1977 { 1978 int region = cpu->env.sau.rnr; 1979 1980 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1981 goto bad_offset; 1982 } 1983 if (!attrs.secure) { 1984 return; 1985 } 1986 if (region >= cpu->sau_sregion) { 1987 return; 1988 } 1989 cpu->env.sau.rlar[region] = value & ~0x1c; 1990 tlb_flush(CPU(cpu)); 1991 break; 1992 } 1993 case 0xde4: /* SFSR */ 1994 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1995 goto bad_offset; 1996 } 1997 if (!attrs.secure) { 1998 return; 1999 } 2000 cpu->env.v7m.sfsr &= ~value; /* W1C */ 2001 break; 2002 case 0xde8: /* SFAR */ 2003 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 2004 goto bad_offset; 2005 } 2006 if (!attrs.secure) { 2007 return; 2008 } 2009 cpu->env.v7m.sfsr = value; 2010 break; 2011 case 0xf00: /* Software Triggered Interrupt Register */ 2012 { 2013 int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ; 2014 2015 if (!arm_feature(&cpu->env, ARM_FEATURE_M_MAIN)) { 2016 goto bad_offset; 2017 } 2018 2019 if (excnum < s->num_irq) { 2020 armv7m_nvic_set_pending(s, excnum, false); 2021 } 2022 break; 2023 } 2024 case 0xf34: /* FPCCR */ 2025 if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 2026 /* Not all bits here are banked. */ 2027 uint32_t fpccr_s; 2028 2029 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) { 2030 /* Don't allow setting of bits not present in v7M */ 2031 value &= (R_V7M_FPCCR_LSPACT_MASK | 2032 R_V7M_FPCCR_USER_MASK | 2033 R_V7M_FPCCR_THREAD_MASK | 2034 R_V7M_FPCCR_HFRDY_MASK | 2035 R_V7M_FPCCR_MMRDY_MASK | 2036 R_V7M_FPCCR_BFRDY_MASK | 2037 R_V7M_FPCCR_MONRDY_MASK | 2038 R_V7M_FPCCR_LSPEN_MASK | 2039 R_V7M_FPCCR_ASPEN_MASK); 2040 } 2041 value &= ~R_V7M_FPCCR_RES0_MASK; 2042 2043 if (!attrs.secure) { 2044 /* Some non-banked bits are configurably writable by NS */ 2045 fpccr_s = cpu->env.v7m.fpccr[M_REG_S]; 2046 if (!(fpccr_s & R_V7M_FPCCR_LSPENS_MASK)) { 2047 uint32_t lspen = FIELD_EX32(value, V7M_FPCCR, LSPEN); 2048 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, LSPEN, lspen); 2049 } 2050 if (!(fpccr_s & R_V7M_FPCCR_CLRONRETS_MASK)) { 2051 uint32_t cor = FIELD_EX32(value, V7M_FPCCR, CLRONRET); 2052 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, CLRONRET, cor); 2053 } 2054 if ((s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 2055 uint32_t hfrdy = FIELD_EX32(value, V7M_FPCCR, HFRDY); 2056 uint32_t bfrdy = FIELD_EX32(value, V7M_FPCCR, BFRDY); 2057 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, HFRDY, hfrdy); 2058 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, BFRDY, bfrdy); 2059 } 2060 /* TODO MONRDY should RAZ/WI if DEMCR.SDME is set */ 2061 { 2062 uint32_t monrdy = FIELD_EX32(value, V7M_FPCCR, MONRDY); 2063 fpccr_s = FIELD_DP32(fpccr_s, V7M_FPCCR, MONRDY, monrdy); 2064 } 2065 2066 /* 2067 * All other non-banked bits are RAZ/WI from NS; write 2068 * just the banked bits to fpccr[M_REG_NS]. 2069 */ 2070 value &= R_V7M_FPCCR_BANKED_MASK; 2071 cpu->env.v7m.fpccr[M_REG_NS] = value; 2072 } else { 2073 fpccr_s = value; 2074 } 2075 cpu->env.v7m.fpccr[M_REG_S] = fpccr_s; 2076 } 2077 break; 2078 case 0xf38: /* FPCAR */ 2079 if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 2080 value &= ~7; 2081 cpu->env.v7m.fpcar[attrs.secure] = value; 2082 } 2083 break; 2084 case 0xf3c: /* FPDSCR */ 2085 if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 2086 uint32_t mask = FPCR_AHP | FPCR_DN | FPCR_FZ | FPCR_RMODE_MASK; 2087 if (cpu_isar_feature(any_fp16, cpu)) { 2088 mask |= FPCR_FZ16; 2089 } 2090 value &= mask; 2091 if (cpu_isar_feature(aa32_lob, cpu)) { 2092 value |= 4 << FPCR_LTPSIZE_SHIFT; 2093 } 2094 cpu->env.v7m.fpdscr[attrs.secure] = value; 2095 } 2096 break; 2097 case 0xf50: /* ICIALLU */ 2098 case 0xf58: /* ICIMVAU */ 2099 case 0xf5c: /* DCIMVAC */ 2100 case 0xf60: /* DCISW */ 2101 case 0xf64: /* DCCMVAU */ 2102 case 0xf68: /* DCCMVAC */ 2103 case 0xf6c: /* DCCSW */ 2104 case 0xf70: /* DCCIMVAC */ 2105 case 0xf74: /* DCCISW */ 2106 case 0xf78: /* BPIALL */ 2107 /* Cache and branch predictor maintenance: for QEMU these always NOP */ 2108 break; 2109 default: 2110 bad_offset: 2111 qemu_log_mask(LOG_GUEST_ERROR, 2112 "NVIC: Bad write offset 0x%x\n", offset); 2113 } 2114 } 2115 2116 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs) 2117 { 2118 /* Return true if unprivileged access to this register is permitted. */ 2119 switch (offset) { 2120 case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */ 2121 /* For access via STIR_NS it is the NS CCR.USERSETMPEND that 2122 * controls access even though the CPU is in Secure state (I_QDKX). 2123 */ 2124 return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK; 2125 default: 2126 /* All other user accesses cause a BusFault unconditionally */ 2127 return false; 2128 } 2129 } 2130 2131 static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs) 2132 { 2133 /* Behaviour for the SHPR register field for this exception: 2134 * return M_REG_NS to use the nonsecure vector (including for 2135 * non-banked exceptions), M_REG_S for the secure version of 2136 * a banked exception, and -1 if this field should RAZ/WI. 2137 */ 2138 switch (exc) { 2139 case ARMV7M_EXCP_MEM: 2140 case ARMV7M_EXCP_USAGE: 2141 case ARMV7M_EXCP_SVC: 2142 case ARMV7M_EXCP_PENDSV: 2143 case ARMV7M_EXCP_SYSTICK: 2144 /* Banked exceptions */ 2145 return attrs.secure; 2146 case ARMV7M_EXCP_BUS: 2147 /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */ 2148 if (!attrs.secure && 2149 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 2150 return -1; 2151 } 2152 return M_REG_NS; 2153 case ARMV7M_EXCP_SECURE: 2154 /* Not banked, RAZ/WI from nonsecure */ 2155 if (!attrs.secure) { 2156 return -1; 2157 } 2158 return M_REG_NS; 2159 case ARMV7M_EXCP_DEBUG: 2160 /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */ 2161 return M_REG_NS; 2162 case 8 ... 10: 2163 case 13: 2164 /* RES0 */ 2165 return -1; 2166 default: 2167 /* Not reachable due to decode of SHPR register addresses */ 2168 g_assert_not_reached(); 2169 } 2170 } 2171 2172 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr, 2173 uint64_t *data, unsigned size, 2174 MemTxAttrs attrs) 2175 { 2176 NVICState *s = (NVICState *)opaque; 2177 uint32_t offset = addr; 2178 unsigned i, startvec, end; 2179 uint32_t val; 2180 2181 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) { 2182 /* Generate BusFault for unprivileged accesses */ 2183 return MEMTX_ERROR; 2184 } 2185 2186 switch (offset) { 2187 /* reads of set and clear both return the status */ 2188 case 0x100 ... 0x13f: /* NVIC Set enable */ 2189 offset += 0x80; 2190 /* fall through */ 2191 case 0x180 ... 0x1bf: /* NVIC Clear enable */ 2192 val = 0; 2193 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; /* vector # */ 2194 2195 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 2196 if (s->vectors[startvec + i].enabled && 2197 (attrs.secure || s->itns[startvec + i])) { 2198 val |= (1 << i); 2199 } 2200 } 2201 break; 2202 case 0x200 ... 0x23f: /* NVIC Set pend */ 2203 offset += 0x80; 2204 /* fall through */ 2205 case 0x280 ... 0x2bf: /* NVIC Clear pend */ 2206 val = 0; 2207 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */ 2208 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 2209 if (s->vectors[startvec + i].pending && 2210 (attrs.secure || s->itns[startvec + i])) { 2211 val |= (1 << i); 2212 } 2213 } 2214 break; 2215 case 0x300 ... 0x33f: /* NVIC Active */ 2216 val = 0; 2217 2218 if (!arm_feature(&s->cpu->env, ARM_FEATURE_V7)) { 2219 break; 2220 } 2221 2222 startvec = 8 * (offset - 0x300) + NVIC_FIRST_IRQ; /* vector # */ 2223 2224 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 2225 if (s->vectors[startvec + i].active && 2226 (attrs.secure || s->itns[startvec + i])) { 2227 val |= (1 << i); 2228 } 2229 } 2230 break; 2231 case 0x400 ... 0x5ef: /* NVIC Priority */ 2232 val = 0; 2233 startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */ 2234 2235 for (i = 0; i < size && startvec + i < s->num_irq; i++) { 2236 if (attrs.secure || s->itns[startvec + i]) { 2237 val |= s->vectors[startvec + i].prio << (8 * i); 2238 } 2239 } 2240 break; 2241 case 0xd18 ... 0xd1b: /* System Handler Priority (SHPR1) */ 2242 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) { 2243 val = 0; 2244 break; 2245 } 2246 /* fall through */ 2247 case 0xd1c ... 0xd23: /* System Handler Priority (SHPR2, SHPR3) */ 2248 val = 0; 2249 for (i = 0; i < size; i++) { 2250 unsigned hdlidx = (offset - 0xd14) + i; 2251 int sbank = shpr_bank(s, hdlidx, attrs); 2252 2253 if (sbank < 0) { 2254 continue; 2255 } 2256 val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank)); 2257 } 2258 break; 2259 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */ 2260 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) { 2261 val = 0; 2262 break; 2263 }; 2264 /* 2265 * The BFSR bits [15:8] are shared between security states 2266 * and we store them in the NS copy. They are RAZ/WI for 2267 * NS code if AIRCR.BFHFNMINS is 0. 2268 */ 2269 val = s->cpu->env.v7m.cfsr[attrs.secure]; 2270 if (!attrs.secure && 2271 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 2272 val &= ~R_V7M_CFSR_BFSR_MASK; 2273 } else { 2274 val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK; 2275 } 2276 val = extract32(val, (offset - 0xd28) * 8, size * 8); 2277 break; 2278 case 0xfe0 ... 0xfff: /* ID. */ 2279 if (offset & 3) { 2280 val = 0; 2281 } else { 2282 val = nvic_id[(offset - 0xfe0) >> 2]; 2283 } 2284 break; 2285 default: 2286 if (size == 4) { 2287 val = nvic_readl(s, offset, attrs); 2288 } else { 2289 qemu_log_mask(LOG_GUEST_ERROR, 2290 "NVIC: Bad read of size %d at offset 0x%x\n", 2291 size, offset); 2292 val = 0; 2293 } 2294 } 2295 2296 trace_nvic_sysreg_read(addr, val, size); 2297 *data = val; 2298 return MEMTX_OK; 2299 } 2300 2301 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr, 2302 uint64_t value, unsigned size, 2303 MemTxAttrs attrs) 2304 { 2305 NVICState *s = (NVICState *)opaque; 2306 uint32_t offset = addr; 2307 unsigned i, startvec, end; 2308 unsigned setval = 0; 2309 2310 trace_nvic_sysreg_write(addr, value, size); 2311 2312 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) { 2313 /* Generate BusFault for unprivileged accesses */ 2314 return MEMTX_ERROR; 2315 } 2316 2317 switch (offset) { 2318 case 0x100 ... 0x13f: /* NVIC Set enable */ 2319 offset += 0x80; 2320 setval = 1; 2321 /* fall through */ 2322 case 0x180 ... 0x1bf: /* NVIC Clear enable */ 2323 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; 2324 2325 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 2326 if (value & (1 << i) && 2327 (attrs.secure || s->itns[startvec + i])) { 2328 s->vectors[startvec + i].enabled = setval; 2329 } 2330 } 2331 nvic_irq_update(s); 2332 goto exit_ok; 2333 case 0x200 ... 0x23f: /* NVIC Set pend */ 2334 /* the special logic in armv7m_nvic_set_pending() 2335 * is not needed since IRQs are never escalated 2336 */ 2337 offset += 0x80; 2338 setval = 1; 2339 /* fall through */ 2340 case 0x280 ... 0x2bf: /* NVIC Clear pend */ 2341 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */ 2342 2343 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { 2344 if (value & (1 << i) && 2345 (attrs.secure || s->itns[startvec + i])) { 2346 s->vectors[startvec + i].pending = setval; 2347 } 2348 } 2349 nvic_irq_update(s); 2350 goto exit_ok; 2351 case 0x300 ... 0x33f: /* NVIC Active */ 2352 goto exit_ok; /* R/O */ 2353 case 0x400 ... 0x5ef: /* NVIC Priority */ 2354 startvec = (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */ 2355 2356 for (i = 0; i < size && startvec + i < s->num_irq; i++) { 2357 if (attrs.secure || s->itns[startvec + i]) { 2358 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff); 2359 } 2360 } 2361 nvic_irq_update(s); 2362 goto exit_ok; 2363 case 0xd18 ... 0xd1b: /* System Handler Priority (SHPR1) */ 2364 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) { 2365 goto exit_ok; 2366 } 2367 /* fall through */ 2368 case 0xd1c ... 0xd23: /* System Handler Priority (SHPR2, SHPR3) */ 2369 for (i = 0; i < size; i++) { 2370 unsigned hdlidx = (offset - 0xd14) + i; 2371 int newprio = extract32(value, i * 8, 8); 2372 int sbank = shpr_bank(s, hdlidx, attrs); 2373 2374 if (sbank < 0) { 2375 continue; 2376 } 2377 set_prio(s, hdlidx, sbank, newprio); 2378 } 2379 nvic_irq_update(s); 2380 goto exit_ok; 2381 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */ 2382 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_MAIN)) { 2383 goto exit_ok; 2384 } 2385 /* All bits are W1C, so construct 32 bit value with 0s in 2386 * the parts not written by the access size 2387 */ 2388 value <<= ((offset - 0xd28) * 8); 2389 2390 if (!attrs.secure && 2391 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) { 2392 /* BFSR bits are RAZ/WI for NS if BFHFNMINS is set */ 2393 value &= ~R_V7M_CFSR_BFSR_MASK; 2394 } 2395 2396 s->cpu->env.v7m.cfsr[attrs.secure] &= ~value; 2397 if (attrs.secure) { 2398 /* The BFSR bits [15:8] are shared between security states 2399 * and we store them in the NS copy. 2400 */ 2401 s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK); 2402 } 2403 goto exit_ok; 2404 } 2405 if (size == 4) { 2406 nvic_writel(s, offset, value, attrs); 2407 goto exit_ok; 2408 } 2409 qemu_log_mask(LOG_GUEST_ERROR, 2410 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset); 2411 /* This is UNPREDICTABLE; treat as RAZ/WI */ 2412 2413 exit_ok: 2414 /* Ensure any changes made are reflected in the cached hflags. */ 2415 arm_rebuild_hflags(&s->cpu->env); 2416 return MEMTX_OK; 2417 } 2418 2419 static const MemoryRegionOps nvic_sysreg_ops = { 2420 .read_with_attrs = nvic_sysreg_read, 2421 .write_with_attrs = nvic_sysreg_write, 2422 .endianness = DEVICE_NATIVE_ENDIAN, 2423 }; 2424 2425 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr, 2426 uint64_t value, unsigned size, 2427 MemTxAttrs attrs) 2428 { 2429 MemoryRegion *mr = opaque; 2430 2431 if (attrs.secure) { 2432 /* S accesses to the alias act like NS accesses to the real region */ 2433 attrs.secure = 0; 2434 return memory_region_dispatch_write(mr, addr, value, 2435 size_memop(size) | MO_TE, attrs); 2436 } else { 2437 /* NS attrs are RAZ/WI for privileged, and BusFault for user */ 2438 if (attrs.user) { 2439 return MEMTX_ERROR; 2440 } 2441 return MEMTX_OK; 2442 } 2443 } 2444 2445 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr, 2446 uint64_t *data, unsigned size, 2447 MemTxAttrs attrs) 2448 { 2449 MemoryRegion *mr = opaque; 2450 2451 if (attrs.secure) { 2452 /* S accesses to the alias act like NS accesses to the real region */ 2453 attrs.secure = 0; 2454 return memory_region_dispatch_read(mr, addr, data, 2455 size_memop(size) | MO_TE, attrs); 2456 } else { 2457 /* NS attrs are RAZ/WI for privileged, and BusFault for user */ 2458 if (attrs.user) { 2459 return MEMTX_ERROR; 2460 } 2461 *data = 0; 2462 return MEMTX_OK; 2463 } 2464 } 2465 2466 static const MemoryRegionOps nvic_sysreg_ns_ops = { 2467 .read_with_attrs = nvic_sysreg_ns_read, 2468 .write_with_attrs = nvic_sysreg_ns_write, 2469 .endianness = DEVICE_NATIVE_ENDIAN, 2470 }; 2471 2472 static MemTxResult nvic_systick_write(void *opaque, hwaddr addr, 2473 uint64_t value, unsigned size, 2474 MemTxAttrs attrs) 2475 { 2476 NVICState *s = opaque; 2477 MemoryRegion *mr; 2478 2479 /* Direct the access to the correct systick */ 2480 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0); 2481 return memory_region_dispatch_write(mr, addr, value, 2482 size_memop(size) | MO_TE, attrs); 2483 } 2484 2485 static MemTxResult nvic_systick_read(void *opaque, hwaddr addr, 2486 uint64_t *data, unsigned size, 2487 MemTxAttrs attrs) 2488 { 2489 NVICState *s = opaque; 2490 MemoryRegion *mr; 2491 2492 /* Direct the access to the correct systick */ 2493 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0); 2494 return memory_region_dispatch_read(mr, addr, data, size_memop(size) | MO_TE, 2495 attrs); 2496 } 2497 2498 static const MemoryRegionOps nvic_systick_ops = { 2499 .read_with_attrs = nvic_systick_read, 2500 .write_with_attrs = nvic_systick_write, 2501 .endianness = DEVICE_NATIVE_ENDIAN, 2502 }; 2503 2504 /* 2505 * Unassigned portions of the PPB space are RAZ/WI for privileged 2506 * accesses, and fault for non-privileged accesses. 2507 */ 2508 static MemTxResult ppb_default_read(void *opaque, hwaddr addr, 2509 uint64_t *data, unsigned size, 2510 MemTxAttrs attrs) 2511 { 2512 qemu_log_mask(LOG_UNIMP, "Read of unassigned area of PPB: offset 0x%x\n", 2513 (uint32_t)addr); 2514 if (attrs.user) { 2515 return MEMTX_ERROR; 2516 } 2517 *data = 0; 2518 return MEMTX_OK; 2519 } 2520 2521 static MemTxResult ppb_default_write(void *opaque, hwaddr addr, 2522 uint64_t value, unsigned size, 2523 MemTxAttrs attrs) 2524 { 2525 qemu_log_mask(LOG_UNIMP, "Write of unassigned area of PPB: offset 0x%x\n", 2526 (uint32_t)addr); 2527 if (attrs.user) { 2528 return MEMTX_ERROR; 2529 } 2530 return MEMTX_OK; 2531 } 2532 2533 static const MemoryRegionOps ppb_default_ops = { 2534 .read_with_attrs = ppb_default_read, 2535 .write_with_attrs = ppb_default_write, 2536 .endianness = DEVICE_NATIVE_ENDIAN, 2537 .valid.min_access_size = 1, 2538 .valid.max_access_size = 8, 2539 }; 2540 2541 static int nvic_post_load(void *opaque, int version_id) 2542 { 2543 NVICState *s = opaque; 2544 unsigned i; 2545 int resetprio; 2546 2547 /* Check for out of range priority settings */ 2548 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3; 2549 2550 if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio || 2551 s->vectors[ARMV7M_EXCP_NMI].prio != -2 || 2552 s->vectors[ARMV7M_EXCP_HARD].prio != -1) { 2553 return 1; 2554 } 2555 for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) { 2556 if (s->vectors[i].prio & ~0xff) { 2557 return 1; 2558 } 2559 } 2560 2561 nvic_recompute_state(s); 2562 2563 return 0; 2564 } 2565 2566 static const VMStateDescription vmstate_VecInfo = { 2567 .name = "armv7m_nvic_info", 2568 .version_id = 1, 2569 .minimum_version_id = 1, 2570 .fields = (VMStateField[]) { 2571 VMSTATE_INT16(prio, VecInfo), 2572 VMSTATE_UINT8(enabled, VecInfo), 2573 VMSTATE_UINT8(pending, VecInfo), 2574 VMSTATE_UINT8(active, VecInfo), 2575 VMSTATE_UINT8(level, VecInfo), 2576 VMSTATE_END_OF_LIST() 2577 } 2578 }; 2579 2580 static bool nvic_security_needed(void *opaque) 2581 { 2582 NVICState *s = opaque; 2583 2584 return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY); 2585 } 2586 2587 static int nvic_security_post_load(void *opaque, int version_id) 2588 { 2589 NVICState *s = opaque; 2590 int i; 2591 2592 /* Check for out of range priority settings */ 2593 if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1 2594 && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) { 2595 /* We can't cross-check against AIRCR.BFHFNMINS as we don't know 2596 * if the CPU state has been migrated yet; a mismatch won't 2597 * cause the emulation to blow up, though. 2598 */ 2599 return 1; 2600 } 2601 for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) { 2602 if (s->sec_vectors[i].prio & ~0xff) { 2603 return 1; 2604 } 2605 } 2606 return 0; 2607 } 2608 2609 static const VMStateDescription vmstate_nvic_security = { 2610 .name = "armv7m_nvic/m-security", 2611 .version_id = 1, 2612 .minimum_version_id = 1, 2613 .needed = nvic_security_needed, 2614 .post_load = &nvic_security_post_load, 2615 .fields = (VMStateField[]) { 2616 VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1, 2617 vmstate_VecInfo, VecInfo), 2618 VMSTATE_UINT32(prigroup[M_REG_S], NVICState), 2619 VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS), 2620 VMSTATE_END_OF_LIST() 2621 } 2622 }; 2623 2624 static const VMStateDescription vmstate_nvic = { 2625 .name = "armv7m_nvic", 2626 .version_id = 4, 2627 .minimum_version_id = 4, 2628 .post_load = &nvic_post_load, 2629 .fields = (VMStateField[]) { 2630 VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1, 2631 vmstate_VecInfo, VecInfo), 2632 VMSTATE_UINT32(prigroup[M_REG_NS], NVICState), 2633 VMSTATE_END_OF_LIST() 2634 }, 2635 .subsections = (const VMStateDescription*[]) { 2636 &vmstate_nvic_security, 2637 NULL 2638 } 2639 }; 2640 2641 static Property props_nvic[] = { 2642 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */ 2643 DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64), 2644 DEFINE_PROP_END_OF_LIST() 2645 }; 2646 2647 static void armv7m_nvic_reset(DeviceState *dev) 2648 { 2649 int resetprio; 2650 NVICState *s = NVIC(dev); 2651 2652 memset(s->vectors, 0, sizeof(s->vectors)); 2653 memset(s->sec_vectors, 0, sizeof(s->sec_vectors)); 2654 s->prigroup[M_REG_NS] = 0; 2655 s->prigroup[M_REG_S] = 0; 2656 2657 s->vectors[ARMV7M_EXCP_NMI].enabled = 1; 2658 /* MEM, BUS, and USAGE are enabled through 2659 * the System Handler Control register 2660 */ 2661 s->vectors[ARMV7M_EXCP_SVC].enabled = 1; 2662 s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1; 2663 s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; 2664 2665 /* DebugMonitor is enabled via DEMCR.MON_EN */ 2666 s->vectors[ARMV7M_EXCP_DEBUG].enabled = 0; 2667 2668 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3; 2669 s->vectors[ARMV7M_EXCP_RESET].prio = resetprio; 2670 s->vectors[ARMV7M_EXCP_NMI].prio = -2; 2671 s->vectors[ARMV7M_EXCP_HARD].prio = -1; 2672 2673 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2674 s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1; 2675 s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1; 2676 s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1; 2677 s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; 2678 2679 /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */ 2680 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1; 2681 /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */ 2682 s->vectors[ARMV7M_EXCP_HARD].enabled = 0; 2683 } else { 2684 s->vectors[ARMV7M_EXCP_HARD].enabled = 1; 2685 } 2686 2687 /* Strictly speaking the reset handler should be enabled. 2688 * However, we don't simulate soft resets through the NVIC, 2689 * and the reset vector should never be pended. 2690 * So we leave it disabled to catch logic errors. 2691 */ 2692 2693 s->exception_prio = NVIC_NOEXC_PRIO; 2694 s->vectpending = 0; 2695 s->vectpending_is_s_banked = false; 2696 s->vectpending_prio = NVIC_NOEXC_PRIO; 2697 2698 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2699 memset(s->itns, 0, sizeof(s->itns)); 2700 } else { 2701 /* This state is constant and not guest accessible in a non-security 2702 * NVIC; we set the bits to true to avoid having to do a feature 2703 * bit check in the NVIC enable/pend/etc register accessors. 2704 */ 2705 int i; 2706 2707 for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) { 2708 s->itns[i] = true; 2709 } 2710 } 2711 2712 /* 2713 * We updated state that affects the CPU's MMUidx and thus its hflags; 2714 * and we can't guarantee that we run before the CPU reset function. 2715 */ 2716 arm_rebuild_hflags(&s->cpu->env); 2717 } 2718 2719 static void nvic_systick_trigger(void *opaque, int n, int level) 2720 { 2721 NVICState *s = opaque; 2722 2723 if (level) { 2724 /* SysTick just asked us to pend its exception. 2725 * (This is different from an external interrupt line's 2726 * behaviour.) 2727 * n == 0 : NonSecure systick 2728 * n == 1 : Secure systick 2729 */ 2730 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n); 2731 } 2732 } 2733 2734 static void armv7m_nvic_realize(DeviceState *dev, Error **errp) 2735 { 2736 NVICState *s = NVIC(dev); 2737 2738 /* The armv7m container object will have set our CPU pointer */ 2739 if (!s->cpu || !arm_feature(&s->cpu->env, ARM_FEATURE_M)) { 2740 error_setg(errp, "The NVIC can only be used with a Cortex-M CPU"); 2741 return; 2742 } 2743 2744 if (s->num_irq > NVIC_MAX_IRQ) { 2745 error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq); 2746 return; 2747 } 2748 2749 qdev_init_gpio_in(dev, set_irq_level, s->num_irq); 2750 2751 /* include space for internal exception vectors */ 2752 s->num_irq += NVIC_FIRST_IRQ; 2753 2754 s->num_prio_bits = arm_feature(&s->cpu->env, ARM_FEATURE_V7) ? 8 : 2; 2755 2756 if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), errp)) { 2757 return; 2758 } 2759 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0, 2760 qdev_get_gpio_in_named(dev, "systick-trigger", 2761 M_REG_NS)); 2762 2763 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) { 2764 /* We couldn't init the secure systick device in instance_init 2765 * as we didn't know then if the CPU had the security extensions; 2766 * so we have to do it here. 2767 */ 2768 object_initialize_child(OBJECT(dev), "systick-reg-s", 2769 &s->systick[M_REG_S], TYPE_SYSTICK); 2770 2771 if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_S]), errp)) { 2772 return; 2773 } 2774 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0, 2775 qdev_get_gpio_in_named(dev, "systick-trigger", 2776 M_REG_S)); 2777 } 2778 2779 /* 2780 * This device provides a single sysbus memory region which 2781 * represents the whole of the "System PPB" space. This is the 2782 * range from 0xe0000000 to 0xe00fffff and includes the NVIC, 2783 * the System Control Space (system registers), the systick timer, 2784 * and for CPUs with the Security extension an NS banked version 2785 * of all of these. 2786 * 2787 * The default behaviour for unimplemented registers/ranges 2788 * (for instance the Data Watchpoint and Trace unit at 0xe0001000) 2789 * is to RAZ/WI for privileged access and BusFault for non-privileged 2790 * access. 2791 * 2792 * The NVIC and System Control Space (SCS) starts at 0xe000e000 2793 * and looks like this: 2794 * 0x004 - ICTR 2795 * 0x010 - 0xff - systick 2796 * 0x100..0x7ec - NVIC 2797 * 0x7f0..0xcff - Reserved 2798 * 0xd00..0xd3c - SCS registers 2799 * 0xd40..0xeff - Reserved or Not implemented 2800 * 0xf00 - STIR 2801 * 2802 * Some registers within this space are banked between security states. 2803 * In v8M there is a second range 0xe002e000..0xe002efff which is the 2804 * NonSecure alias SCS; secure accesses to this behave like NS accesses 2805 * to the main SCS range, and non-secure accesses (including when 2806 * the security extension is not implemented) are RAZ/WI. 2807 * Note that both the main SCS range and the alias range are defined 2808 * to be exempt from memory attribution (R_BLJT) and so the memory 2809 * transaction attribute always matches the current CPU security 2810 * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops 2811 * wrappers we change attrs.secure to indicate the NS access; so 2812 * generally code determining which banked register to use should 2813 * use attrs.secure; code determining actual behaviour of the system 2814 * should use env->v7m.secure. 2815 * 2816 * The container covers the whole PPB space. Within it the priority 2817 * of overlapping regions is: 2818 * - default region (for RAZ/WI and BusFault) : -1 2819 * - system register regions : 0 2820 * - systick : 1 2821 * This is because the systick device is a small block of registers 2822 * in the middle of the other system control registers. 2823 */ 2824 memory_region_init(&s->container, OBJECT(s), "nvic", 0x100000); 2825 memory_region_init_io(&s->defaultmem, OBJECT(s), &ppb_default_ops, s, 2826 "nvic-default", 0x100000); 2827 memory_region_add_subregion_overlap(&s->container, 0, &s->defaultmem, -1); 2828 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s, 2829 "nvic_sysregs", 0x1000); 2830 memory_region_add_subregion(&s->container, 0xe000, &s->sysregmem); 2831 2832 memory_region_init_io(&s->systickmem, OBJECT(s), 2833 &nvic_systick_ops, s, 2834 "nvic_systick", 0xe0); 2835 2836 memory_region_add_subregion_overlap(&s->container, 0xe010, 2837 &s->systickmem, 1); 2838 2839 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) { 2840 memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s), 2841 &nvic_sysreg_ns_ops, &s->sysregmem, 2842 "nvic_sysregs_ns", 0x1000); 2843 memory_region_add_subregion(&s->container, 0x2e000, &s->sysreg_ns_mem); 2844 memory_region_init_io(&s->systick_ns_mem, OBJECT(s), 2845 &nvic_sysreg_ns_ops, &s->systickmem, 2846 "nvic_systick_ns", 0xe0); 2847 memory_region_add_subregion_overlap(&s->container, 0x2e010, 2848 &s->systick_ns_mem, 1); 2849 } 2850 2851 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container); 2852 } 2853 2854 static void armv7m_nvic_instance_init(Object *obj) 2855 { 2856 /* We have a different default value for the num-irq property 2857 * than our superclass. This function runs after qdev init 2858 * has set the defaults from the Property array and before 2859 * any user-specified property setting, so just modify the 2860 * value in the GICState struct. 2861 */ 2862 DeviceState *dev = DEVICE(obj); 2863 NVICState *nvic = NVIC(obj); 2864 SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 2865 2866 object_initialize_child(obj, "systick-reg-ns", &nvic->systick[M_REG_NS], 2867 TYPE_SYSTICK); 2868 /* We can't initialize the secure systick here, as we don't know 2869 * yet if we need it. 2870 */ 2871 2872 sysbus_init_irq(sbd, &nvic->excpout); 2873 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1); 2874 qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger", 2875 M_REG_NUM_BANKS); 2876 qdev_init_gpio_in_named(dev, nvic_nmi_trigger, "NMI", 1); 2877 } 2878 2879 static void armv7m_nvic_class_init(ObjectClass *klass, void *data) 2880 { 2881 DeviceClass *dc = DEVICE_CLASS(klass); 2882 2883 dc->vmsd = &vmstate_nvic; 2884 device_class_set_props(dc, props_nvic); 2885 dc->reset = armv7m_nvic_reset; 2886 dc->realize = armv7m_nvic_realize; 2887 } 2888 2889 static const TypeInfo armv7m_nvic_info = { 2890 .name = TYPE_NVIC, 2891 .parent = TYPE_SYS_BUS_DEVICE, 2892 .instance_init = armv7m_nvic_instance_init, 2893 .instance_size = sizeof(NVICState), 2894 .class_init = armv7m_nvic_class_init, 2895 .class_size = sizeof(SysBusDeviceClass), 2896 }; 2897 2898 static void armv7m_nvic_register_types(void) 2899 { 2900 type_register_static(&armv7m_nvic_info); 2901 } 2902 2903 type_init(armv7m_nvic_register_types) 2904