daifflags.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2017 ARM Ltd.
  4. */
  5. #ifndef __ASM_DAIFFLAGS_H
  6. #define __ASM_DAIFFLAGS_H
  7. #include <linux/irqflags.h>
  8. #include <asm/arch_gicv3.h>
  9. #include <asm/barrier.h>
  10. #include <asm/cpufeature.h>
  11. #include <asm/ptrace.h>
  12. #define DAIF_PROCCTX 0
  13. #define DAIF_PROCCTX_NOIRQ (PSR_I_BIT | PSR_F_BIT)
  14. #define DAIF_ERRCTX (PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
  15. #define DAIF_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
  16. /* mask/save/unmask/restore all exceptions, including interrupts. */
  17. static inline void local_daif_mask(void)
  18. {
  19. WARN_ON(system_has_prio_mask_debugging() &&
  20. (read_sysreg_s(SYS_ICC_PMR_EL1) == (GIC_PRIO_IRQOFF |
  21. GIC_PRIO_PSR_I_SET)));
  22. asm volatile(
  23. "msr daifset, #0xf // local_daif_mask\n"
  24. :
  25. :
  26. : "memory");
  27. /* Don't really care for a dsb here, we don't intend to enable IRQs */
  28. if (system_uses_irq_prio_masking())
  29. gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
  30. trace_hardirqs_off();
  31. }
  32. static inline unsigned long local_daif_save_flags(void)
  33. {
  34. unsigned long flags;
  35. flags = read_sysreg(daif);
  36. if (system_uses_irq_prio_masking()) {
  37. /* If IRQs are masked with PMR, reflect it in the flags */
  38. if (read_sysreg_s(SYS_ICC_PMR_EL1) != GIC_PRIO_IRQON)
  39. flags |= PSR_I_BIT | PSR_F_BIT;
  40. }
  41. return flags;
  42. }
  43. static inline unsigned long local_daif_save(void)
  44. {
  45. unsigned long flags;
  46. flags = local_daif_save_flags();
  47. local_daif_mask();
  48. return flags;
  49. }
  50. static inline void local_daif_restore(unsigned long flags)
  51. {
  52. bool irq_disabled = flags & PSR_I_BIT;
  53. WARN_ON(system_has_prio_mask_debugging() &&
  54. (read_sysreg(daif) & (PSR_I_BIT | PSR_F_BIT)) != (PSR_I_BIT | PSR_F_BIT));
  55. if (!irq_disabled) {
  56. trace_hardirqs_on();
  57. if (system_uses_irq_prio_masking()) {
  58. gic_write_pmr(GIC_PRIO_IRQON);
  59. pmr_sync();
  60. }
  61. } else if (system_uses_irq_prio_masking()) {
  62. u64 pmr;
  63. if (!(flags & PSR_A_BIT)) {
  64. /*
  65. * If interrupts are disabled but we can take
  66. * asynchronous errors, we can take NMIs
  67. */
  68. flags &= ~(PSR_I_BIT | PSR_F_BIT);
  69. pmr = GIC_PRIO_IRQOFF;
  70. } else {
  71. pmr = GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET;
  72. }
  73. /*
  74. * There has been concern that the write to daif
  75. * might be reordered before this write to PMR.
  76. * From the ARM ARM DDI 0487D.a, section D1.7.1
  77. * "Accessing PSTATE fields":
  78. * Writes to the PSTATE fields have side-effects on
  79. * various aspects of the PE operation. All of these
  80. * side-effects are guaranteed:
  81. * - Not to be visible to earlier instructions in
  82. * the execution stream.
  83. * - To be visible to later instructions in the
  84. * execution stream
  85. *
  86. * Also, writes to PMR are self-synchronizing, so no
  87. * interrupts with a lower priority than PMR is signaled
  88. * to the PE after the write.
  89. *
  90. * So we don't need additional synchronization here.
  91. */
  92. gic_write_pmr(pmr);
  93. }
  94. write_sysreg(flags, daif);
  95. if (irq_disabled)
  96. trace_hardirqs_off();
  97. }
  98. /*
  99. * Called by synchronous exception handlers to restore the DAIF bits that were
  100. * modified by taking an exception.
  101. */
  102. static inline void local_daif_inherit(struct pt_regs *regs)
  103. {
  104. unsigned long flags = regs->pstate & DAIF_MASK;
  105. if (interrupts_enabled(regs))
  106. trace_hardirqs_on();
  107. if (system_uses_irq_prio_masking())
  108. gic_write_pmr(regs->pmr_save);
  109. /*
  110. * We can't use local_daif_restore(regs->pstate) here as
  111. * system_has_prio_mask_debugging() won't restore the I bit if it can
  112. * use the pmr instead.
  113. */
  114. write_sysreg(flags, daif);
  115. }
  116. #endif