1*f19fbd5eSMartin Schwidefsky // SPDX-License-Identifier: GPL-2.0 2*f19fbd5eSMartin Schwidefsky #include <linux/module.h> 3*f19fbd5eSMartin Schwidefsky #include <asm/nospec-branch.h> 4*f19fbd5eSMartin Schwidefsky 5*f19fbd5eSMartin Schwidefsky int nospec_call_disable = IS_ENABLED(EXPOLINE_OFF); 6*f19fbd5eSMartin Schwidefsky int nospec_return_disable = !IS_ENABLED(EXPOLINE_FULL); 7*f19fbd5eSMartin Schwidefsky 8*f19fbd5eSMartin Schwidefsky static int __init nospectre_v2_setup_early(char *str) 9*f19fbd5eSMartin Schwidefsky { 10*f19fbd5eSMartin Schwidefsky nospec_call_disable = 1; 11*f19fbd5eSMartin Schwidefsky nospec_return_disable = 1; 12*f19fbd5eSMartin Schwidefsky return 0; 13*f19fbd5eSMartin Schwidefsky } 14*f19fbd5eSMartin Schwidefsky early_param("nospectre_v2", nospectre_v2_setup_early); 15*f19fbd5eSMartin Schwidefsky 16*f19fbd5eSMartin Schwidefsky static int __init spectre_v2_setup_early(char *str) 17*f19fbd5eSMartin Schwidefsky { 18*f19fbd5eSMartin Schwidefsky if (str && !strncmp(str, "on", 2)) { 19*f19fbd5eSMartin Schwidefsky nospec_call_disable = 0; 20*f19fbd5eSMartin Schwidefsky nospec_return_disable = 0; 21*f19fbd5eSMartin Schwidefsky } 22*f19fbd5eSMartin Schwidefsky if (str && !strncmp(str, "off", 3)) { 23*f19fbd5eSMartin Schwidefsky nospec_call_disable = 1; 24*f19fbd5eSMartin Schwidefsky nospec_return_disable = 1; 25*f19fbd5eSMartin Schwidefsky } 26*f19fbd5eSMartin Schwidefsky if (str && !strncmp(str, "auto", 4)) { 27*f19fbd5eSMartin Schwidefsky nospec_call_disable = 0; 28*f19fbd5eSMartin Schwidefsky nospec_return_disable = 1; 29*f19fbd5eSMartin Schwidefsky } 30*f19fbd5eSMartin Schwidefsky return 0; 31*f19fbd5eSMartin Schwidefsky } 32*f19fbd5eSMartin Schwidefsky early_param("spectre_v2", spectre_v2_setup_early); 33*f19fbd5eSMartin Schwidefsky 34*f19fbd5eSMartin Schwidefsky static void __init_or_module __nospec_revert(s32 *start, s32 *end) 35*f19fbd5eSMartin Schwidefsky { 36*f19fbd5eSMartin Schwidefsky enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type; 37*f19fbd5eSMartin Schwidefsky u8 *instr, *thunk, *br; 38*f19fbd5eSMartin Schwidefsky u8 insnbuf[6]; 39*f19fbd5eSMartin Schwidefsky s32 *epo; 40*f19fbd5eSMartin Schwidefsky 41*f19fbd5eSMartin Schwidefsky /* Second part of the instruction replace is always a nop */ 42*f19fbd5eSMartin Schwidefsky memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x00, 0x00 }, 4); 43*f19fbd5eSMartin Schwidefsky for (epo = start; epo < end; epo++) { 44*f19fbd5eSMartin Schwidefsky instr = (u8 *) epo + *epo; 45*f19fbd5eSMartin Schwidefsky if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04) 46*f19fbd5eSMartin Schwidefsky type = BRCL_EXPOLINE; /* brcl instruction */ 47*f19fbd5eSMartin Schwidefsky else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05) 48*f19fbd5eSMartin Schwidefsky type = BRASL_EXPOLINE; /* brasl instruction */ 49*f19fbd5eSMartin Schwidefsky else 50*f19fbd5eSMartin Schwidefsky continue; 51*f19fbd5eSMartin Schwidefsky thunk = instr + (*(int *)(instr + 2)) * 2; 52*f19fbd5eSMartin Schwidefsky if (thunk[0] == 0xc6 && thunk[1] == 0x00) 53*f19fbd5eSMartin Schwidefsky /* exrl %r0,<target-br> */ 54*f19fbd5eSMartin Schwidefsky br = thunk + (*(int *)(thunk + 2)) * 2; 55*f19fbd5eSMartin Schwidefsky else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 && 56*f19fbd5eSMartin Schwidefsky thunk[6] == 0x44 && thunk[7] == 0x00 && 57*f19fbd5eSMartin Schwidefsky (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 && 58*f19fbd5eSMartin Schwidefsky (thunk[1] & 0xf0) == (thunk[8] & 0xf0)) 59*f19fbd5eSMartin Schwidefsky /* larl %rx,<target br> + ex %r0,0(%rx) */ 60*f19fbd5eSMartin Schwidefsky br = thunk + (*(int *)(thunk + 2)) * 2; 61*f19fbd5eSMartin Schwidefsky else 62*f19fbd5eSMartin Schwidefsky continue; 63*f19fbd5eSMartin Schwidefsky if (br[0] != 0x07 || (br[1] & 0xf0) != 0xf0) 64*f19fbd5eSMartin Schwidefsky continue; 65*f19fbd5eSMartin Schwidefsky switch (type) { 66*f19fbd5eSMartin Schwidefsky case BRCL_EXPOLINE: 67*f19fbd5eSMartin Schwidefsky /* brcl to thunk, replace with br + nop */ 68*f19fbd5eSMartin Schwidefsky insnbuf[0] = br[0]; 69*f19fbd5eSMartin Schwidefsky insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f); 70*f19fbd5eSMartin Schwidefsky break; 71*f19fbd5eSMartin Schwidefsky case BRASL_EXPOLINE: 72*f19fbd5eSMartin Schwidefsky /* brasl to thunk, replace with basr + nop */ 73*f19fbd5eSMartin Schwidefsky insnbuf[0] = 0x0d; 74*f19fbd5eSMartin Schwidefsky insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f); 75*f19fbd5eSMartin Schwidefsky break; 76*f19fbd5eSMartin Schwidefsky } 77*f19fbd5eSMartin Schwidefsky 78*f19fbd5eSMartin Schwidefsky s390_kernel_write(instr, insnbuf, 6); 79*f19fbd5eSMartin Schwidefsky } 80*f19fbd5eSMartin Schwidefsky } 81*f19fbd5eSMartin Schwidefsky 82*f19fbd5eSMartin Schwidefsky void __init_or_module nospec_call_revert(s32 *start, s32 *end) 83*f19fbd5eSMartin Schwidefsky { 84*f19fbd5eSMartin Schwidefsky if (nospec_call_disable) 85*f19fbd5eSMartin Schwidefsky __nospec_revert(start, end); 86*f19fbd5eSMartin Schwidefsky } 87*f19fbd5eSMartin Schwidefsky 88*f19fbd5eSMartin Schwidefsky void __init_or_module nospec_return_revert(s32 *start, s32 *end) 89*f19fbd5eSMartin Schwidefsky { 90*f19fbd5eSMartin Schwidefsky if (nospec_return_disable) 91*f19fbd5eSMartin Schwidefsky __nospec_revert(start, end); 92*f19fbd5eSMartin Schwidefsky } 93*f19fbd5eSMartin Schwidefsky 94*f19fbd5eSMartin Schwidefsky extern s32 __nospec_call_start[], __nospec_call_end[]; 95*f19fbd5eSMartin Schwidefsky extern s32 __nospec_return_start[], __nospec_return_end[]; 96*f19fbd5eSMartin Schwidefsky void __init nospec_init_branches(void) 97*f19fbd5eSMartin Schwidefsky { 98*f19fbd5eSMartin Schwidefsky nospec_call_revert(__nospec_call_start, __nospec_call_end); 99*f19fbd5eSMartin Schwidefsky nospec_return_revert(__nospec_return_start, __nospec_return_end); 100*f19fbd5eSMartin Schwidefsky } 101