Skip to content

Commit 84bc444

Browse files
author
Simon Dardis
committed
Merging r325653 with test fixups:
------------------------------------------------------------------------ r325653 | sdardis | 2018-02-21 00:06:53 +0000 (Wed, 21 Feb 2018) | 31 lines [mips] Spectre variant two mitigation for MIPSR2 This patch provides mitigation for CVE-2017-5715, Spectre variant two, which affects the P5600 and P6600. It implements the LLVM part of -mindirect-jump=hazard. It is _not_ enabled by default for the P5600. The migitation strategy suggested by MIPS for these processors is to use hazard barrier instructions. 'jalr.hb' and 'jr.hb' are hazard barrier variants of the 'jalr' and 'jr' instructions respectively. These instructions impede the execution of instruction stream until architecturally defined hazards (changes to the instruction stream, privileged registers which may affect execution) are cleared. These instructions in MIPS' designs are not speculated past. These instructions are used with the attribute +use-indirect-jump-hazard when branching indirectly and for indirect function calls. These instructions are defined by the MIPS32R2 ISA, so this mitigation method is not compatible with processors which implement an earlier revision of the MIPS ISA. Performance benchmarking of this option with -fpic and lld using -z hazardplt shows a difference of overall 10%~ time increase for the LLVM testsuite. Certain benchmarks such as methcall show a substantially larger increase in time due to their nature. Reviewers: atanasyan, zoran.jovanovic Differential Revision: https://reviews.llvm.org/D43486 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@329798 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 417182e commit 84bc444

22 files changed

+1421
-38
lines changed

lib/Target/Mips/AsmParser/MipsAsmParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5136,6 +5136,7 @@ unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
51365136
// It also applies for registers Rt and Rs of microMIPSr6 jalrc.hb instruction
51375137
// and registers Rd and Base for microMIPS lwp instruction
51385138
case Mips::JALR_HB:
5139+
case Mips::JALR_HB64:
51395140
case Mips::JALRC_HB_MMR6:
51405141
case Mips::JALRC_MMR6:
51415142
if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg())

lib/Target/Mips/MicroMips32r6InstrInfo.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,12 @@ let AddedComplexity = 41 in {
18861886

18871887
def TAILCALL_MMR6 : TailCall<BC_MMR6, brtarget26_mm>, ISA_MICROMIPS32R6;
18881888

1889+
def TAILCALLREG_MMR6 : TailCallReg<JRC16_MM, GPR32Opnd>, ISA_MICROMIPS32R6;
1890+
1891+
def PseudoIndirectBranch_MMR6 : PseudoIndirectBranchBase<JRC16_MMR6,
1892+
GPR32Opnd>,
1893+
ISA_MICROMIPS32R6;
1894+
18891895
def : MipsPat<(MipsTailCall (iPTR tglobaladdr:$dst)),
18901896
(TAILCALL_MMR6 tglobaladdr:$dst)>, ISA_MICROMIPS32R6;
18911897

lib/Target/Mips/MicroMipsInstrInfo.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,12 @@ let DecoderNamespace = "MicroMips", Predicates = [InMicroMips] in {
10031003

10041004
def TAILCALL_MM : TailCall<J_MM, jmptarget_mm>, ISA_MIPS1_NOT_32R6_64R6;
10051005

1006+
def TAILCALLREG_MM : TailCallReg<JRC16_MM, GPR32Opnd>,
1007+
ISA_MICROMIPS32_NOT_MIPS32R6;
1008+
1009+
def PseudoIndirectBranch_MM : PseudoIndirectBranchBase<JR_MM, GPR32Opnd>,
1010+
ISA_MICROMIPS32_NOT_MIPS32R6;
1011+
10061012
let DecoderNamespace = "MicroMips" in {
10071013
def RDHWR_MM : MMRel, R6MMR6Rel, ReadHardware<GPR32Opnd, HWRegsOpnd>,
10081014
RDHWR_FM_MM, ISA_MICROMIPS32_NOT_MIPS32R6;

lib/Target/Mips/Mips.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ def FeatureMT : SubtargetFeature<"mt", "HasMT", "true", "Mips MT ASE">;
193193
def FeatureLongCalls : SubtargetFeature<"long-calls", "UseLongCalls", "true",
194194
"Disable use of the jal instruction">;
195195

196+
def FeatureUseIndirectJumpsHazard : SubtargetFeature<"use-indirect-jump-hazard",
197+
"UseIndirectJumpsHazard",
198+
"true", "Use indirect jump"
199+
" guards to prevent certain speculation based attacks">;
196200
//===----------------------------------------------------------------------===//
197201
// Mips processors supported.
198202
//===----------------------------------------------------------------------===//

lib/Target/Mips/Mips32r6InstrInfo.td

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,3 +1036,42 @@ def : MipsPat<(select i32:$cond, immz, i32:$f),
10361036
(SELEQZ i32:$f, i32:$cond)>,
10371037
ISA_MIPS32R6;
10381038
}
1039+
1040+
// Pseudo instructions
1041+
let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, hasDelaySlot = 1,
1042+
hasExtraSrcRegAllocReq = 1, isCTI = 1, Defs = [AT] in {
1043+
class TailCallRegR6<Instruction JumpInst, Register RT, RegisterOperand RO> :
1044+
PseudoSE<(outs), (ins RO:$rs), [(MipsTailCall RO:$rs)], II_JR>,
1045+
PseudoInstExpansion<(JumpInst RT:$rt, RO:$rs)>;
1046+
}
1047+
1048+
class PseudoIndirectBranchBaseR6<Instruction JumpInst, Register RT,
1049+
RegisterOperand RO> :
1050+
MipsPseudo<(outs), (ins RO:$rs), [(brind RO:$rs)],
1051+
II_IndirectBranchPseudo>,
1052+
PseudoInstExpansion<(JumpInst RT:$rt, RO:$rs)> {
1053+
let isTerminator=1;
1054+
let isBarrier=1;
1055+
let hasDelaySlot = 1;
1056+
let isBranch = 1;
1057+
let isIndirectBranch = 1;
1058+
bit isCTI = 1;
1059+
}
1060+
1061+
1062+
let AdditionalPredicates = [NotInMips16Mode, NotInMicroMips,
1063+
NoIndirectJumpGuards] in {
1064+
def TAILCALLR6REG : TailCallRegR6<JALR, ZERO, GPR32Opnd>, ISA_MIPS32R6;
1065+
def PseudoIndirectBranchR6 : PseudoIndirectBranchBaseR6<JALR, ZERO,
1066+
GPR32Opnd>,
1067+
ISA_MIPS32R6;
1068+
}
1069+
1070+
let AdditionalPredicates = [NotInMips16Mode, NotInMicroMips,
1071+
UseIndirectJumpsHazard] in {
1072+
def TAILCALLHBR6REG : TailCallReg<JR_HB_R6, GPR32Opnd>, ISA_MIPS32R6;
1073+
def PseudoIndrectHazardBranchR6 : PseudoIndirectBranchBase<JR_HB_R6,
1074+
GPR32Opnd>,
1075+
ISA_MIPS32R6;
1076+
}
1077+

lib/Target/Mips/Mips64InstrInfo.td

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,32 @@ let isCodeGenOnly = 1 in {
240240
def BGTZ64 : CBranchZero<"bgtz", brtarget, setgt, GPR64Opnd>, BGEZ_FM<7, 0>;
241241
def BLEZ64 : CBranchZero<"blez", brtarget, setle, GPR64Opnd>, BGEZ_FM<6, 0>;
242242
def BLTZ64 : CBranchZero<"bltz", brtarget, setlt, GPR64Opnd>, BGEZ_FM<1, 0>;
243-
def JALR64Pseudo : JumpLinkRegPseudo<GPR64Opnd, JALR, RA, GPR32Opnd>;
243+
let AdditionalPredicates = [NoIndirectJumpGuards] in
244+
def JALR64Pseudo : JumpLinkRegPseudo<GPR64Opnd, JALR, RA, GPR32Opnd>;
244245
}
246+
let AdditionalPredicates = [NotInMicroMips],
247+
DecoderNamespace = "Mips64" in {
248+
def JR_HB64 : JR_HB_DESC<GPR64Opnd>, JR_HB_ENC, ISA_MIPS32_NOT_32R6_64R6;
249+
def JALR_HB64 : JALR_HB_DESC<GPR64Opnd>, JALR_HB_ENC, ISA_MIPS32R2;
250+
}
251+
def PseudoReturn64 : PseudoReturnBase<GPR64Opnd>;
245252

246-
def TAILCALLREG64 : TailCallReg<GPR64Opnd>;
253+
let AdditionalPredicates = [NotInMips16Mode, NotInMicroMips,
254+
NoIndirectJumpGuards] in {
255+
def TAILCALLREG64 : TailCallReg<JR64, GPR64Opnd>, ISA_MIPS3_NOT_32R6_64R6,
256+
PTR_64;
257+
def PseudoIndirectBranch64 : PseudoIndirectBranchBase<JR64, GPR64Opnd>,
258+
ISA_MIPS3_NOT_32R6_64R6;
259+
}
247260

248-
def PseudoReturn64 : PseudoReturnBase<GPR64Opnd>;
249-
def PseudoIndirectBranch64 : PseudoIndirectBranchBase<GPR64Opnd>;
261+
let AdditionalPredicates = [NotInMips16Mode, NotInMicroMips,
262+
UseIndirectJumpsHazard] in {
263+
def TAILCALLREGHB64 : TailCallReg<JR_HB64, GPR64Opnd>,
264+
ISA_MIPS32R2_NOT_32R6_64R6, PTR_64;
265+
def PseudoIndirectHazardBranch64 : PseudoIndirectBranchBase<JR_HB64,
266+
GPR64Opnd>,
267+
ISA_MIPS32R2_NOT_32R6_64R6;
268+
}
250269

251270
/// Multiply and Divide Instructions.
252271
let AdditionalPredicates = [NotInMicroMips] in {
@@ -536,6 +555,10 @@ def DMTC2 : MTC3OP<"dmtc2", COP2Opnd, GPR64Opnd, II_DMTC2>, MFC3OP_FM<0x12, 5>,
536555
ISA_MIPS3;
537556
}
538557

558+
559+
let AdditionalPredicates = [UseIndirectJumpsHazard] in
560+
def JALRHB64Pseudo : JumpLinkRegPseudo<GPR64Opnd, JALR_HB64, RA_64>;
561+
539562
//===----------------------------------------------------------------------===//
540563
// Arbitrary patterns that map to one or more instructions
541564
//===----------------------------------------------------------------------===//
@@ -843,7 +866,8 @@ let AdditionalPredicates = [NotInMicroMips] in {
843866
def : MipsInstAlias<"dext $rt, $rs, $pos, $size",
844867
(DEXTU GPR64Opnd:$rt, GPR64Opnd:$rs, uimm5_plus32:$pos,
845868
uimm5_plus1:$size), 0>, ISA_MIPS64R2;
846-
869+
def : MipsInstAlias<"jalr.hb $rs", (JALR_HB64 RA_64, GPR64Opnd:$rs), 1>,
870+
ISA_MIPS64;
847871
// Two operand (implicit 0 selector) versions:
848872
def : MipsInstAlias<"dmtc0 $rt, $rd",
849873
(DMTC0 COP0Opnd:$rd, GPR64Opnd:$rt, 0), 0>;

lib/Target/Mips/Mips64r6InstrInfo.td

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ class JIC64_DESC : JMP_IDX_COMPACT_DESC_BASE<"jic", jmpoffset16, GPR64Opnd,
104104

105105
class LL64_R6_DESC : LL_R6_DESC_BASE<"ll", GPR32Opnd, mem_simm9, II_LL>;
106106
class SC64_R6_DESC : SC_R6_DESC_BASE<"sc", GPR32Opnd, II_SC>;
107+
108+
class JR_HB64_R6_DESC : JR_HB_DESC_BASE<"jr.hb", GPR64Opnd> {
109+
bit isBranch = 1;
110+
bit isIndirectBranch = 1;
111+
bit hasDelaySlot = 1;
112+
bit isTerminator=1;
113+
bit isBarrier=1;
114+
bit isCTI = 1;
115+
InstrItinClass Itinerary = II_JR_HB;
116+
}
107117
//===----------------------------------------------------------------------===//
108118
//
109119
// Instruction Definitions
@@ -136,6 +146,7 @@ def SCD_R6 : SCD_R6_ENC, SCD_R6_DESC, ISA_MIPS32R6;
136146
let DecoderNamespace = "Mips32r6_64r6_GP64" in {
137147
def SELEQZ64 : SELEQZ_ENC, SELEQZ64_DESC, ISA_MIPS32R6, GPR_64;
138148
def SELNEZ64 : SELNEZ_ENC, SELNEZ64_DESC, ISA_MIPS32R6, GPR_64;
149+
def JR_HB64_R6 : JR_HB_R6_ENC, JR_HB64_R6_DESC, ISA_MIPS32R6;
139150
}
140151
let AdditionalPredicates = [NotInMicroMips],
141152
DecoderNamespace = "Mips32r6_64r6_PTR64" in {
@@ -277,3 +288,22 @@ def : MipsPat<(select (i32 (setne i32:$cond, immz)), immz, i64:$f),
277288
def : MipsPat<(select (i32 (seteq i32:$cond, immz)), immz, i64:$f),
278289
(SELNEZ64 i64:$f, (SLL64_32 i32:$cond))>,
279290
ISA_MIPS64R6;
291+
292+
// Pseudo instructions
293+
294+
let AdditionalPredicates = [NotInMips16Mode, NotInMicroMips,
295+
NoIndirectJumpGuards] in {
296+
def TAILCALL64R6REG : TailCallRegR6<JALR64, ZERO_64, GPR64Opnd>, ISA_MIPS64R6;
297+
def PseudoIndirectBranch64R6 : PseudoIndirectBranchBaseR6<JALR64, ZERO_64,
298+
GPR64Opnd>,
299+
ISA_MIPS64R6;
300+
}
301+
302+
let AdditionalPredicates = [NotInMips16Mode, NotInMicroMips,
303+
UseIndirectJumpsHazard] in {
304+
def TAILCALLHB64R6REG : TailCallReg<JR_HB64_R6, GPR64Opnd>,
305+
ISA_MIPS64R6;
306+
def PseudoIndrectHazardBranch64R6 : PseudoIndirectBranchBase<JR_HB64_R6,
307+
GPR64Opnd>,
308+
ISA_MIPS64R6;
309+
}

lib/Target/Mips/MipsDSPInstrFormats.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class DSPInst<string opstr = "">
5353

5454
class PseudoDSP<dag outs, dag ins, list<dag> pattern,
5555
InstrItinClass itin = IIPseudo>
56-
: MipsPseudo<outs, ins, pattern, itin>, PredicateControl {
56+
: MipsPseudo<outs, ins, pattern, itin> {
5757
let InsnPredicates = [HasDSP];
5858
}
5959

lib/Target/Mips/MipsInstrFormats.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ class InstSE<dag outs, dag ins, string asmstr, list<dag> pattern,
128128
// Mips Pseudo Instructions Format
129129
class MipsPseudo<dag outs, dag ins, list<dag> pattern,
130130
InstrItinClass itin = IIPseudo> :
131-
MipsInst<outs, ins, "", pattern, itin, Pseudo> {
131+
MipsInst<outs, ins, "", pattern, itin, Pseudo>, PredicateControl {
132132
let isCodeGenOnly = 1;
133133
let isPseudo = 1;
134134
}
135135

136136
// Mips32/64 Pseudo Instruction Format
137137
class PseudoSE<dag outs, dag ins, list<dag> pattern,
138138
InstrItinClass itin = IIPseudo> :
139-
MipsPseudo<outs, ins, pattern, itin>, PredicateControl {
139+
MipsPseudo<outs, ins, pattern, itin> {
140140
let EncodingPredicates = [HasStdEnc];
141141
}
142142

lib/Target/Mips/MipsInstrInfo.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ unsigned MipsInstrInfo::getEquivalentCompactForm(
298298
case Mips::JR:
299299
case Mips::PseudoReturn:
300300
case Mips::PseudoIndirectBranch:
301-
case Mips::TAILCALLREG:
302301
canUseShortMicroMipsCTI = true;
303302
break;
304303
}
@@ -377,18 +376,18 @@ unsigned MipsInstrInfo::getEquivalentCompactForm(
377376
// For MIPSR6, the instruction 'jic' can be used for these cases. Some
378377
// tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
379378
case Mips::JR:
379+
case Mips::PseudoIndirectBranchR6:
380380
case Mips::PseudoReturn:
381-
case Mips::PseudoIndirectBranch:
382-
case Mips::TAILCALLREG:
381+
case Mips::TAILCALLR6REG:
383382
if (canUseShortMicroMipsCTI)
384383
return Mips::JRC16_MM;
385384
return Mips::JIC;
386385
case Mips::JALRPseudo:
387386
return Mips::JIALC;
388387
case Mips::JR64:
388+
case Mips::PseudoIndirectBranch64R6:
389389
case Mips::PseudoReturn64:
390-
case Mips::PseudoIndirectBranch64:
391-
case Mips::TAILCALLREG64:
390+
case Mips::TAILCALL64R6REG:
392391
return Mips::JIC64;
393392
case Mips::JALR64Pseudo:
394393
return Mips::JIALC64;
@@ -617,6 +616,18 @@ bool MipsInstrInfo::verifyInstruction(const MachineInstr &MI,
617616
return verifyInsExtInstruction(MI, ErrInfo, 0, 32, 32, 64, 32, 64);
618617
case Mips::DEXTU:
619618
return verifyInsExtInstruction(MI, ErrInfo, 32, 64, 0, 32, 32, 64);
619+
case Mips::TAILCALLREG:
620+
case Mips::PseudoIndirectBranch:
621+
case Mips::JR:
622+
case Mips::JR64:
623+
case Mips::JALR:
624+
case Mips::JALR64:
625+
case Mips::JALRPseudo:
626+
if (!Subtarget.useIndirectJumpsHazard())
627+
return true;
628+
629+
ErrInfo = "invalid instruction when using jump guards!";
630+
return false;
620631
default:
621632
return true;
622633
}

0 commit comments

Comments
 (0)