mirror of
https://github.com/torvalds/linux.git
synced 2026-03-08 03:24:45 +01:00
- Reorganize the alternatives patching mechanism to patch a single
location only once instead of multiple times as it was the case with the two or three alternative options macros -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEzv7L6UO9uDPlPSfHEsHwGGHeVUoFAmmKXicACgkQEsHwGGHe VUrh5A//ZyxZV+AeDK2EaIEFRRRIP9i+DvRV4VEvoKpFLrWiX9y6yRlktYP/3FxK y7bX50HcSoqS8+AZdHTyQyELKpWzykixZAJQcRAFBHmUZucDn/rQLm+F7PK3gDBa RqYowTNOx9lAS9Z9fNlZ2CzpCcj1yacJu9nHlioGSKriJYmNjoaOi6NGBJudenJ4 UUj+ICcOA1qoMMxN3jWgtcY5LGnSo+nM1ssN504UHVSiuzhDjkO/W9kosHI5sdpW QP9J+hnnRbkStcfxSVmVAq9qDQQx0tqx579kiaw6+w0Jw+/GPmt4/pMt7nPKd+rZ +0bywg02vc+/4KRmTJylY7bzSKMn0ynBKPVU5ZJRcaB9v7cgWkw8GA+wTkZDnNt9 ZVs8E7KM1m/DASk7fsn5HIc+oOJG4XcUHLchCyD8QsVk/dJTXnK9LO/cctU8tuot HDUbO8Zr4OFAqLV7BEda+OsfRQnZxIlOa/wxv1kK5YRgNAG+WDQn3fbTOJZrwc68 vXKIffT/7e5ILX/T6MoIGcF7PzCYED8041f5u3Aw2diZ9Ocf7DWslye3exBE0mLE bY216JJ9+dJD+RP8ZZDliB1NyCcScShBe1JvXn3odgxhWHcqKPHrP5wJF86nzE/H vnAVT1U6ECohs7QPOmvgNwLIcWFcuuBvGAVlF85FYbu5NT8cSFY= =JJc3 -----END PGP SIGNATURE----- Merge tag 'x86_alternatives_for_v7.0_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip Pull x86 alternatives update from Borislav Petkov: - Reorganize the alternatives patching mechanism to patch a single location only once instead of multiple times as it was the case with the two or three alternative options macros * tag 'x86_alternatives_for_v7.0_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/alternative: Patch a single alternative location only once x86/alternative: Use helper functions for patching alternatives
This commit is contained in:
commit
d1953aa3bc
1 changed files with 91 additions and 56 deletions
|
|
@ -586,6 +586,87 @@ static inline u8 * instr_va(struct alt_instr *i)
|
|||
return (u8 *)&i->instr_offset + i->instr_offset;
|
||||
}
|
||||
|
||||
struct patch_site {
|
||||
u8 *instr;
|
||||
struct alt_instr *alt;
|
||||
u8 buff[MAX_PATCH_LEN];
|
||||
u8 len;
|
||||
};
|
||||
|
||||
static struct alt_instr * __init_or_module analyze_patch_site(struct patch_site *ps,
|
||||
struct alt_instr *start,
|
||||
struct alt_instr *end)
|
||||
{
|
||||
struct alt_instr *alt = start;
|
||||
|
||||
ps->instr = instr_va(start);
|
||||
|
||||
/*
|
||||
* In case of nested ALTERNATIVE()s the outer alternative might add
|
||||
* more padding. To ensure consistent patching find the max padding for
|
||||
* all alt_instr entries for this site (nested alternatives result in
|
||||
* consecutive entries).
|
||||
* Find the last alt_instr eligible for patching at the site.
|
||||
*/
|
||||
for (; alt < end && instr_va(alt) == ps->instr; alt++) {
|
||||
ps->len = max(ps->len, alt->instrlen);
|
||||
|
||||
BUG_ON(alt->cpuid >= (NCAPINTS + NBUGINTS) * 32);
|
||||
/*
|
||||
* Patch if either:
|
||||
* - feature is present
|
||||
* - feature not present but ALT_FLAG_NOT is set to mean,
|
||||
* patch if feature is *NOT* present.
|
||||
*/
|
||||
if (!boot_cpu_has(alt->cpuid) != !(alt->flags & ALT_FLAG_NOT))
|
||||
ps->alt = alt;
|
||||
}
|
||||
|
||||
BUG_ON(ps->len > sizeof(ps->buff));
|
||||
|
||||
return alt;
|
||||
}
|
||||
|
||||
static void __init_or_module prep_patch_site(struct patch_site *ps)
|
||||
{
|
||||
struct alt_instr *alt = ps->alt;
|
||||
u8 buff_sz;
|
||||
u8 *repl;
|
||||
|
||||
if (!alt) {
|
||||
/* Nothing to patch, use original instruction. */
|
||||
memcpy(ps->buff, ps->instr, ps->len);
|
||||
return;
|
||||
}
|
||||
|
||||
repl = (u8 *)&alt->repl_offset + alt->repl_offset;
|
||||
DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
|
||||
alt->cpuid >> 5, alt->cpuid & 0x1f,
|
||||
ps->instr, ps->instr, ps->len,
|
||||
repl, alt->replacementlen, alt->flags);
|
||||
|
||||
memcpy(ps->buff, repl, alt->replacementlen);
|
||||
buff_sz = alt->replacementlen;
|
||||
|
||||
if (alt->flags & ALT_FLAG_DIRECT_CALL)
|
||||
buff_sz = alt_replace_call(ps->instr, ps->buff, alt);
|
||||
|
||||
for (; buff_sz < ps->len; buff_sz++)
|
||||
ps->buff[buff_sz] = 0x90;
|
||||
|
||||
__apply_relocation(ps->buff, ps->instr, ps->len, repl, alt->replacementlen);
|
||||
|
||||
DUMP_BYTES(ALT, ps->instr, ps->len, "%px: old_insn: ", ps->instr);
|
||||
DUMP_BYTES(ALT, repl, alt->replacementlen, "%px: rpl_insn: ", repl);
|
||||
DUMP_BYTES(ALT, ps->buff, ps->len, "%px: final_insn: ", ps->instr);
|
||||
}
|
||||
|
||||
static void __init_or_module patch_site(struct patch_site *ps)
|
||||
{
|
||||
optimize_nops(ps->instr, ps->buff, ps->len);
|
||||
text_poke_early(ps->instr, ps->buff, ps->len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Replace instructions with better alternatives for this CPU type. This runs
|
||||
* before SMP is initialized to avoid SMP problems with self modifying code.
|
||||
|
|
@ -599,9 +680,7 @@ static inline u8 * instr_va(struct alt_instr *i)
|
|||
void __init_or_module noinline apply_alternatives(struct alt_instr *start,
|
||||
struct alt_instr *end)
|
||||
{
|
||||
u8 insn_buff[MAX_PATCH_LEN];
|
||||
u8 *instr, *replacement;
|
||||
struct alt_instr *a, *b;
|
||||
struct alt_instr *a;
|
||||
|
||||
DPRINTK(ALT, "alt table %px, -> %px", start, end);
|
||||
|
||||
|
|
@ -624,60 +703,16 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
|
|||
* So be careful if you want to change the scan order to any other
|
||||
* order.
|
||||
*/
|
||||
for (a = start; a < end; a++) {
|
||||
unsigned int insn_buff_sz = 0;
|
||||
a = start;
|
||||
while (a < end) {
|
||||
struct patch_site ps = {
|
||||
.alt = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
/*
|
||||
* In case of nested ALTERNATIVE()s the outer alternative might
|
||||
* add more padding. To ensure consistent patching find the max
|
||||
* padding for all alt_instr entries for this site (nested
|
||||
* alternatives result in consecutive entries).
|
||||
*/
|
||||
for (b = a+1; b < end && instr_va(b) == instr_va(a); b++) {
|
||||
u8 len = max(a->instrlen, b->instrlen);
|
||||
a->instrlen = b->instrlen = len;
|
||||
}
|
||||
|
||||
instr = instr_va(a);
|
||||
replacement = (u8 *)&a->repl_offset + a->repl_offset;
|
||||
BUG_ON(a->instrlen > sizeof(insn_buff));
|
||||
BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
|
||||
|
||||
/*
|
||||
* Patch if either:
|
||||
* - feature is present
|
||||
* - feature not present but ALT_FLAG_NOT is set to mean,
|
||||
* patch if feature is *NOT* present.
|
||||
*/
|
||||
if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
|
||||
memcpy(insn_buff, instr, a->instrlen);
|
||||
optimize_nops(instr, insn_buff, a->instrlen);
|
||||
text_poke_early(instr, insn_buff, a->instrlen);
|
||||
continue;
|
||||
}
|
||||
|
||||
DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
|
||||
a->cpuid >> 5,
|
||||
a->cpuid & 0x1f,
|
||||
instr, instr, a->instrlen,
|
||||
replacement, a->replacementlen, a->flags);
|
||||
|
||||
memcpy(insn_buff, replacement, a->replacementlen);
|
||||
insn_buff_sz = a->replacementlen;
|
||||
|
||||
if (a->flags & ALT_FLAG_DIRECT_CALL)
|
||||
insn_buff_sz = alt_replace_call(instr, insn_buff, a);
|
||||
|
||||
for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
|
||||
insn_buff[insn_buff_sz] = 0x90;
|
||||
|
||||
text_poke_apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen);
|
||||
|
||||
DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr);
|
||||
DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement);
|
||||
DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr);
|
||||
|
||||
text_poke_early(instr, insn_buff, insn_buff_sz);
|
||||
a = analyze_patch_site(&ps, a, end);
|
||||
prep_patch_site(&ps);
|
||||
patch_site(&ps);
|
||||
}
|
||||
|
||||
kasan_enable_current();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue