@@ -646,13 +646,26 @@ alternative_else_nop_endif
646646 sub \dst , \dst , PAGE_SIZE
647647 .endm
648648
649- .macro tramp_ventry , vector_start , regsize , kpti
649+
650+ #define BHB_MITIGATION_NONE 0
651+ #define BHB_MITIGATION_LOOP 1
652+ #define BHB_MITIGATION_FW 2
653+
654+ .macro tramp_ventry , vector_start , regsize , kpti , bhb
650655 . align 7
6516561 :
652657 .if \regsize == 64
653658 msr tpidrro_el0 , x30 // Restored in kernel_ventry
654659 .endif
655660
661+ .if \bhb == BHB_MITIGATION_LOOP
662+ / *
663+ * This sequence must appear before the first indirect branch. i.e. the
664+ * ret out of tramp_ventry. It appears here because x30 is free.
665+ * /
666+ __mitigate_spectre_bhb_loop x30
667+ .endif // \bhb == BHB_MITIGATION_LOOP
668+
656669 .if \kpti == 1
657670 / *
658671 * Defend against branch aliasing attacks by pushing a dummy
@@ -680,13 +693,25 @@ alternative_else_nop_endif
680693 ldr x30 , =vectors
681694 .endif // \kpti == 1
682695
696+ .if \bhb == BHB_MITIGATION_FW
697+ / *
698+ * The firmware sequence must appear before the first indirect branch.
699+ * i.e. the ret out of tramp_ventry. But it also needs the stack to be
700+ * mapped to save/restore the registers the SMC clobbers.
701+ * /
702+ __mitigate_spectre_bhb_fw
703+ .endif // \bhb == BHB_MITIGATION_FW
704+
683705 add x30 , x30 , #( 1b - \vector_start + 4 )
684706 ret
685707.org 1b + 128 // Did we overflow the ventry slot?
686708 .endm
687709
688710 .macro tramp_exit , regsize = 64
689711 adr x30 , tramp_vectors
712+ #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
713+ add x30 , x30 , SZ_4K
714+ #endif
690715 msr vbar_el1 , x30
691716 ldr lr , [ sp , #S_LR ]
692717 tramp_unmap_kernel x29
@@ -698,26 +723,32 @@ alternative_else_nop_endif
698723 sb
699724 .endm
700725
701- .macro generate_tramp_vector , kpti
726+ .macro generate_tramp_vector , kpti , bhb
702727.Lvector_start\@:
703728 .space 0x400
704729
705730 .rept 4
706- tramp_ventry .Lvector_start\@ , 64 , \kpti
731+ tramp_ventry .Lvector_start\@ , 64 , \kpti , \bhb
707732 .endr
708733 .rept 4
709- tramp_ventry .Lvector_start\@ , 32 , \kpti
734+ tramp_ventry .Lvector_start\@ , 32 , \kpti , \bhb
710735 .endr
711736 .endm
712737
713738#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
714739/ *
715740 * Exception vectors trampoline.
741+ * The order must match __bp_harden_el1_vectors and the
742+ * arm64_bp_harden_el1_vectors enum.
716743 * /
717744 .pushsection ".entry.tramp.text" , "ax"
718745 . align 11
719746SYM_CODE_START_NOALIGN(tramp_vectors)
720- generate_tramp_vector kpti= 1
747+ #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
748+ generate_tramp_vector kpti= 1 , bhb=BHB_MITIGATION_LOOP
749+ generate_tramp_vector kpti= 1 , bhb=BHB_MITIGATION_FW
750+ #endif / * CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY * /
751+ generate_tramp_vector kpti= 1 , bhb=BHB_MITIGATION_NONE
721752SYM_CODE_END(tramp_vectors)
722753
723754SYM_CODE_START(tramp_exit_native)
@@ -744,7 +775,7 @@ SYM_DATA_END(__entry_tramp_data_start)
744775 * Exception vectors for spectre mitigations on entry from EL1 when
745776 * kpti is not in use.
746777 * /
747- .macro generate_el1_vector
778+ .macro generate_el1_vector , bhb
748779.Lvector_start\@:
749780 kernel_ventry 1 , t , 64 , sync // Synchronous EL1t
750781 kernel_ventry 1 , t , 64 , irq // IRQ EL1t
@@ -757,17 +788,21 @@ SYM_DATA_END(__entry_tramp_data_start)
757788 kernel_ventry 1 , h , 64 , error // Error EL1h
758789
759790 .rept 4
760- tramp_ventry .Lvector_start\@ , 64 , kpti= 0
791+ tramp_ventry .Lvector_start\@ , 64 , 0 , \bhb
761792 .endr
762793 .rept 4
763- tramp_ventry .Lvector_start\@ , 32 , kpti= 0
794+ tramp_ventry .Lvector_start\@ , 32 , 0 , \bhb
764795 .endr
765796 .endm
766797
798+ / * The order must match tramp_vecs and the arm64_bp_harden_el1_vectors enum. * /
767799 .pushsection ".entry.text" , "ax"
768800 . align 11
769801SYM_CODE_START(__bp_harden_el1_vectors)
770- generate_el1_vector
802+ #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
803+ generate_el1_vector bhb=BHB_MITIGATION_LOOP
804+ generate_el1_vector bhb=BHB_MITIGATION_FW
805+ #endif / * CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY * /
771806SYM_CODE_END(__bp_harden_el1_vectors)
772807 .popsection
773808
0 commit comments