License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2008-10-22 22:26:29 -07:00
|
|
|
#ifndef _ASM_X86_ALTERNATIVE_H
|
|
|
|
#define _ASM_X86_ALTERNATIVE_H
|
2008-01-30 13:30:30 +01:00
|
|
|
|
|
|
|
#include <linux/types.h>
|
2009-04-28 11:13:46 -04:00
|
|
|
#include <linux/stringify.h>
|
2024-11-28 10:38:57 +01:00
|
|
|
#include <linux/objtool.h>
|
2008-01-30 13:30:30 +01:00
|
|
|
#include <asm/asm.h>
|
2025-04-23 09:57:31 +02:00
|
|
|
#include <asm/bug.h>
|
2008-01-30 13:30:30 +01:00
|
|
|
|
2022-12-22 12:42:24 +01:00
|
|
|
#define ALT_FLAGS_SHIFT 16
|
|
|
|
|
2023-04-18 08:42:28 +02:00
|
|
|
#define ALT_FLAG_NOT (1 << 0)
|
2022-12-22 12:42:24 +01:00
|
|
|
#define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
|
2023-12-10 07:21:36 +01:00
|
|
|
#define ALT_FLAG_DIRECT_CALL (1 << 1)
|
|
|
|
#define ALT_DIRECT_CALL(feature) ((ALT_FLAG_DIRECT_CALL << ALT_FLAGS_SHIFT) | (feature))
|
|
|
|
#define ALT_CALL_ALWAYS ALT_DIRECT_CALL(X86_FEATURE_ALWAYS)
|
2021-03-11 15:23:10 +01:00
|
|
|
|
2025-03-19 11:30:57 +01:00
|
|
|
#ifndef __ASSEMBLER__
|
2021-03-11 15:23:06 +01:00
|
|
|
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
/*
|
|
|
|
* Alternative inline assembly for SMP.
|
|
|
|
*
|
|
|
|
* The LOCK_PREFIX macro defined here replaces the LOCK and
|
|
|
|
* LOCK_PREFIX macros used everywhere in the source tree.
|
|
|
|
*
|
|
|
|
* SMP alternatives use the same data structures as the other
|
|
|
|
* alternatives and the X86_FEATURE_UP flag to indicate the case of a
|
|
|
|
* UP system running a SMP kernel. The existing apply_alternatives()
|
|
|
|
* works fine for patching a SMP kernel for UP.
|
|
|
|
*
|
|
|
|
* The SMP alternative tables can be kept after boot and contain both
|
|
|
|
* UP and SMP versions of the instructions to allow switching back to
|
|
|
|
* SMP at runtime, when hotplugging in a new CPU, which is especially
|
|
|
|
* useful in virtualized environments.
|
|
|
|
*
|
|
|
|
* The very common lock prefix is handled as special case in a
|
|
|
|
* separate table which is a pure address list without replacement ptr
|
|
|
|
* and size information. That keeps the table sizes small.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
2018-12-19 11:20:57 +01:00
|
|
|
#define LOCK_PREFIX_HERE \
|
|
|
|
".pushsection .smp_locks,\"a\"\n" \
|
|
|
|
".balign 4\n" \
|
|
|
|
".long 671f - .\n" /* offset */ \
|
|
|
|
".popsection\n" \
|
|
|
|
"671:"
|
|
|
|
|
2025-02-28 09:51:15 +01:00
|
|
|
#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock "
|
2018-12-19 11:20:57 +01:00
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
#else /* ! CONFIG_SMP */
|
2010-04-29 16:03:57 -07:00
|
|
|
#define LOCK_PREFIX_HERE ""
|
2008-01-30 13:30:30 +01:00
|
|
|
#define LOCK_PREFIX ""
|
|
|
|
#endif
|
|
|
|
|
2022-12-22 12:42:24 +01:00
|
|
|
/*
|
|
|
|
* The patching flags are part of the upper bits of the @ft_flags parameter when
|
|
|
|
* specifying them. The split is currently like this:
|
|
|
|
*
|
|
|
|
* [31... flags ...16][15... CPUID feature bit ...0]
|
|
|
|
*
|
|
|
|
* but since this is all hidden in the macros argument being split, those fields can be
|
|
|
|
* extended in the future to fit in a u64 or however the need arises.
|
|
|
|
*/
|
2008-01-30 13:30:30 +01:00
|
|
|
struct alt_instr {
|
2011-07-13 09:24:10 -04:00
|
|
|
s32 instr_offset; /* original instruction */
|
|
|
|
s32 repl_offset; /* offset to replacement instruction */
|
2022-12-22 12:42:24 +01:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
u32 cpuid: 16; /* CPUID bit set for replacement */
|
|
|
|
u32 flags: 16; /* patching control flags */
|
|
|
|
};
|
|
|
|
u32 ft_flags;
|
|
|
|
};
|
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
u8 instrlen; /* length of original instruction */
|
2014-12-27 10:41:52 +01:00
|
|
|
u8 replacementlen; /* length of new instruction */
|
|
|
|
} __packed;
|
2008-01-30 13:30:30 +01:00
|
|
|
|
2023-12-10 07:21:37 +01:00
|
|
|
extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
|
|
|
|
|
2025-04-11 07:41:00 +02:00
|
|
|
extern s32 __retpoline_sites[], __retpoline_sites_end[];
|
|
|
|
extern s32 __return_sites[], __return_sites_end[];
|
|
|
|
extern s32 __cfi_sites[], __cfi_sites_end[];
|
|
|
|
extern s32 __ibt_endbr_seal[], __ibt_endbr_seal_end[];
|
|
|
|
extern s32 __smp_locks[], __smp_locks_end[];
|
|
|
|
|
2015-04-30 09:09:26 +02:00
|
|
|
/*
|
|
|
|
* Debug flag that can be tested to see whether alternative
|
|
|
|
* instructions were patched in already:
|
|
|
|
*/
|
|
|
|
extern int alternatives_patched;
|
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
extern void alternative_instructions(void);
|
2025-01-26 09:47:31 +02:00
|
|
|
extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
|
|
|
|
extern void apply_retpolines(s32 *start, s32 *end);
|
|
|
|
extern void apply_returns(s32 *start, s32 *end);
|
|
|
|
extern void apply_seal_endbr(s32 *start, s32 *end);
|
2022-10-27 11:28:14 +02:00
|
|
|
extern void apply_fineibt(s32 *start_retpoline, s32 *end_retpoine,
|
2025-01-26 09:47:31 +02:00
|
|
|
s32 *start_cfi, s32 *end_cfi);
|
|
|
|
|
|
|
|
struct module;
|
2022-09-15 13:11:23 +02:00
|
|
|
|
|
|
|
struct callthunk_sites {
|
|
|
|
s32 *call_start, *call_end;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_CALL_THUNKS
|
|
|
|
extern void callthunks_patch_builtin_calls(void);
|
2022-09-15 13:11:24 +02:00
|
|
|
extern void callthunks_patch_module_calls(struct callthunk_sites *sites,
|
|
|
|
struct module *mod);
|
2022-09-15 13:11:31 +02:00
|
|
|
extern void *callthunks_translate_call_dest(void *dest);
|
2024-04-01 20:55:30 +02:00
|
|
|
extern int x86_call_depth_emit_accounting(u8 **pprog, void *func, void *ip);
|
2022-09-15 13:11:23 +02:00
|
|
|
#else
|
|
|
|
static __always_inline void callthunks_patch_builtin_calls(void) {}
|
2022-09-15 13:11:24 +02:00
|
|
|
static __always_inline void
|
|
|
|
callthunks_patch_module_calls(struct callthunk_sites *sites,
|
|
|
|
struct module *mod) {}
|
2022-09-15 13:11:31 +02:00
|
|
|
static __always_inline void *callthunks_translate_call_dest(void *dest)
|
|
|
|
{
|
|
|
|
return dest;
|
|
|
|
}
|
2022-09-15 13:11:34 +02:00
|
|
|
static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
|
2024-04-01 20:55:30 +02:00
|
|
|
void *func, void *ip)
|
2022-09-15 13:11:34 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-15 13:11:23 +02:00
|
|
|
#endif
|
2008-01-30 13:30:30 +01:00
|
|
|
|
2024-10-14 10:05:48 -07:00
|
|
|
#ifdef CONFIG_MITIGATION_ITS
|
|
|
|
extern void its_init_mod(struct module *mod);
|
|
|
|
extern void its_fini_mod(struct module *mod);
|
|
|
|
extern void its_free_mod(struct module *mod);
|
2025-04-23 09:57:31 +02:00
|
|
|
extern u8 *its_static_thunk(int reg);
|
2024-10-14 10:05:48 -07:00
|
|
|
#else /* CONFIG_MITIGATION_ITS */
|
|
|
|
static inline void its_init_mod(struct module *mod) { }
|
|
|
|
static inline void its_fini_mod(struct module *mod) { }
|
|
|
|
static inline void its_free_mod(struct module *mod) { }
|
2025-04-23 09:57:31 +02:00
|
|
|
static inline u8 *its_static_thunk(int reg)
|
|
|
|
{
|
|
|
|
WARN_ONCE(1, "ITS not compiled in");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-10-14 10:05:48 -07:00
|
|
|
#endif
|
|
|
|
|
2024-06-21 21:17:21 -07:00
|
|
|
#if defined(CONFIG_MITIGATION_RETHUNK) && defined(CONFIG_OBJTOOL)
|
|
|
|
extern bool cpu_wants_rethunk(void);
|
|
|
|
extern bool cpu_wants_rethunk_at(void *addr);
|
|
|
|
#else
|
|
|
|
static __always_inline bool cpu_wants_rethunk(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
static __always_inline bool cpu_wants_rethunk_at(void *addr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
extern void alternatives_smp_module_add(struct module *mod, char *name,
|
|
|
|
void *locks, void *locks_end,
|
|
|
|
void *text, void *text_end);
|
|
|
|
extern void alternatives_smp_module_del(struct module *mod);
|
2012-08-06 17:29:49 +09:30
|
|
|
extern void alternatives_enable_smp(void);
|
2010-02-02 16:49:11 -05:00
|
|
|
extern int alternatives_text_reserved(void *start, void *end);
|
2010-11-23 16:11:40 -08:00
|
|
|
extern bool skip_smp_alternatives;
|
2008-01-30 13:30:30 +01:00
|
|
|
#else
|
|
|
|
static inline void alternatives_smp_module_add(struct module *mod, char *name,
|
2008-03-23 01:01:37 -07:00
|
|
|
void *locks, void *locks_end,
|
|
|
|
void *text, void *text_end) {}
|
2008-01-30 13:30:30 +01:00
|
|
|
static inline void alternatives_smp_module_del(struct module *mod) {}
|
2012-08-06 17:29:49 +09:30
|
|
|
static inline void alternatives_enable_smp(void) {}
|
2010-02-02 16:49:11 -05:00
|
|
|
static inline int alternatives_text_reserved(void *start, void *end)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-30 13:30:30 +01:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
2023-12-10 07:21:36 +01:00
|
|
|
#define ALT_CALL_INSTR "call BUG_func"
|
|
|
|
|
2024-06-07 13:17:01 +02:00
|
|
|
#define alt_slen "772b-771b"
|
|
|
|
#define alt_total_slen "773b-771b"
|
|
|
|
#define alt_rlen "775f-774f"
|
2012-05-24 18:19:45 -07:00
|
|
|
|
2024-06-07 13:17:01 +02:00
|
|
|
#define OLDINSTR(oldinstr) \
|
|
|
|
"# ALT: oldinstr\n" \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
"771:\n\t" oldinstr "\n772:\n" \
|
2024-06-07 13:17:01 +02:00
|
|
|
"# ALT: padding\n" \
|
|
|
|
".skip -(((" alt_rlen ")-(" alt_slen ")) > 0) * " \
|
|
|
|
"((" alt_rlen ")-(" alt_slen ")),0x90\n" \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
"773:\n"
|
|
|
|
|
2024-06-07 13:17:01 +02:00
|
|
|
#define ALTINSTR_ENTRY(ft_flags) \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
".pushsection .altinstructions,\"a\"\n" \
|
|
|
|
" .long 771b - .\n" /* label */ \
|
|
|
|
" .long 774f - .\n" /* new instruction */ \
|
|
|
|
" .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \
|
2024-06-07 13:17:01 +02:00
|
|
|
" .byte " alt_total_slen "\n" /* source len */ \
|
|
|
|
" .byte " alt_rlen "\n" /* replacement len */ \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
".popsection\n"
|
|
|
|
|
2024-06-07 13:17:01 +02:00
|
|
|
#define ALTINSTR_REPLACEMENT(newinstr) /* replacement */ \
|
|
|
|
".pushsection .altinstr_replacement, \"ax\"\n" \
|
|
|
|
"# ALT: replacement\n" \
|
|
|
|
"774:\n\t" newinstr "\n775:\n" \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
".popsection\n"
|
|
|
|
|
2009-04-28 11:13:46 -04:00
|
|
|
/* alternative assembly primitive: */
|
2022-12-22 12:42:24 +01:00
|
|
|
#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \
|
2024-06-07 13:17:01 +02:00
|
|
|
OLDINSTR(oldinstr) \
|
|
|
|
ALTINSTR_ENTRY(ft_flags) \
|
|
|
|
ALTINSTR_REPLACEMENT(newinstr)
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
|
2022-12-22 12:42:24 +01:00
|
|
|
#define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
|
2024-06-07 13:17:01 +02:00
|
|
|
ALTERNATIVE(ALTERNATIVE(oldinstr, newinstr1, ft_flags1), newinstr2, ft_flags2)
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
|
2021-03-11 15:23:11 +01:00
|
|
|
/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
|
2022-12-22 12:42:24 +01:00
|
|
|
#define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
|
2024-06-07 13:17:01 +02:00
|
|
|
ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, newinstr_yes, ft_flags)
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
|
2024-06-07 13:16:57 +02:00
|
|
|
#define ALTERNATIVE_3(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, \
|
|
|
|
newinstr3, ft_flags3) \
|
2024-06-07 13:17:01 +02:00
|
|
|
ALTERNATIVE(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2), \
|
2024-06-07 13:16:57 +02:00
|
|
|
newinstr3, ft_flags3)
|
2024-06-07 13:17:01 +02:00
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
/*
|
|
|
|
* Alternative instructions for different CPU types or capabilities.
|
|
|
|
*
|
|
|
|
* This allows to use optimized instructions even on generic binary
|
|
|
|
* kernels.
|
|
|
|
*
|
|
|
|
* length of oldinstr must be longer or equal the length of newinstr
|
|
|
|
* It can be padded with nops as needed.
|
|
|
|
*
|
|
|
|
* For non barrier like inlines please define new variants
|
|
|
|
* without volatile and memory clobber.
|
|
|
|
*/
|
2022-12-22 12:42:24 +01:00
|
|
|
#define alternative(oldinstr, newinstr, ft_flags) \
|
2024-06-07 13:17:01 +02:00
|
|
|
asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory")
|
2008-01-30 13:30:30 +01:00
|
|
|
|
2022-12-22 12:42:24 +01:00
|
|
|
#define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
|
2024-06-07 13:17:01 +02:00
|
|
|
asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
|
2008-01-30 13:30:30 +01:00
|
|
|
/*
|
|
|
|
* Alternative inline assembly with input.
|
|
|
|
*
|
2018-12-03 10:47:34 +01:00
|
|
|
* Peculiarities:
|
2008-01-30 13:30:30 +01:00
|
|
|
* No memory clobber here.
|
|
|
|
* Argument numbers start with 1.
|
2009-04-28 11:13:46 -04:00
|
|
|
* Leaving an unused argument 0 to keep API compatibility.
|
2008-01-30 13:30:30 +01:00
|
|
|
*/
|
2022-12-22 12:42:24 +01:00
|
|
|
#define alternative_input(oldinstr, newinstr, ft_flags, input...) \
|
2024-06-07 13:17:01 +02:00
|
|
|
asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \
|
2009-04-28 11:13:46 -04:00
|
|
|
: : "i" (0), ## input)
|
2008-01-30 13:30:30 +01:00
|
|
|
|
|
|
|
/* Like alternative_input, but with a single output argument */
|
2022-12-22 12:42:24 +01:00
|
|
|
#define alternative_io(oldinstr, newinstr, ft_flags, output, input...) \
|
2024-06-07 13:17:01 +02:00
|
|
|
asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
: output : "i" (0), ## input)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like alternative_io, but for replacing a direct call with another one.
|
|
|
|
*
|
|
|
|
* Use the %c operand modifier which is the generic way to print a bare
|
|
|
|
* constant expression with all syntax-specific punctuation omitted. %P
|
|
|
|
* is the x86-specific variant which can handle constants too, for
|
|
|
|
* historical reasons, but it should be used primarily for PIC
|
|
|
|
* references: i.e., if used for a function, it would add the PLT
|
|
|
|
* suffix.
|
|
|
|
*/
|
2025-03-02 17:21:01 -08:00
|
|
|
#define alternative_call(oldfunc, newfunc, ft_flags, output, input, clobbers...) \
|
x86/alternatives, kvm: Fix a couple of CALLs without a frame pointer
objtool complains:
arch/x86/kvm/kvm.o: warning: objtool: .altinstr_replacement+0xc5: call without frame pointer save/setup
vmlinux.o: warning: objtool: .altinstr_replacement+0x2eb: call without frame pointer save/setup
Make sure %rSP is an output operand to the respective asm() statements.
The test_cc() hunk and ALT_OUTPUT_SP() courtesy of peterz. Also from him
add some helpful debugging info to the documentation.
Now on to the explanations:
tl;dr: The alternatives macros are pretty fragile.
If I do ALT_OUTPUT_SP(output) in order to be able to package in a %rsp
reference for objtool so that a stack frame gets properly generated, the
inline asm input operand with positional argument 0 in clear_page():
"0" (page)
gets "renumbered" due to the added
: "+r" (current_stack_pointer), "=D" (page)
and then gcc says:
./arch/x86/include/asm/page_64.h:53:9: error: inconsistent operand constraints in an ‘asm’
The fix is to use an explicit "D" constraint which points to a singleton
register class (gcc terminology) which ends up doing what is expected
here: the page pointer - input and output - should be in the same %rdi
register.
Other register classes have more than one register in them - example:
"r" and "=r" or "A":
‘A’
The ‘a’ and ‘d’ registers. This class is used for
instructions that return double word results in the ‘ax:dx’
register pair. Single word values will be allocated either in
‘ax’ or ‘dx’.
so using "D" and "=D" just works in this particular case.
And yes, one would say, sure, why don't you do "+D" but then:
: "+r" (current_stack_pointer), "+D" (page)
: [old] "i" (clear_page_orig), [new1] "i" (clear_page_rep), [new2] "i" (clear_page_erms),
: "cc", "memory", "rax", "rcx")
now find the Waldo^Wcomma which throws a wrench into all this.
Because that silly macro has an "input..." consume-all last macro arg
and in it, one is supposed to supply input *and* clobbers, leading to
silly syntax snafus.
Yap, they need to be cleaned up, one fine day...
Closes: https://lore.kernel.org/oe-kbuild-all/202406141648.jO9qNGLa-lkp@intel.com/
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Sean Christopherson <seanjc@google.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240625112056.GDZnqoGDXgYuWBDUwu@fat_crate.local
2024-06-18 21:57:27 +02:00
|
|
|
asm_inline volatile(ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \
|
|
|
|
: ALT_OUTPUT_SP(output) \
|
2025-03-02 17:21:01 -08:00
|
|
|
: [old] "i" (oldfunc), [new] "i" (newfunc) \
|
|
|
|
COMMA(input) \
|
|
|
|
: clobbers)
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
|
2012-05-24 18:19:45 -07:00
|
|
|
/*
|
|
|
|
* Like alternative_call, but there are two features and respective functions.
|
|
|
|
* If CPU has feature2, function2 is used.
|
|
|
|
* Otherwise, if CPU has feature1, function1 is used.
|
|
|
|
* Otherwise, old function is used.
|
|
|
|
*/
|
2024-06-07 13:16:55 +02:00
|
|
|
#define alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2, \
|
2025-03-02 17:21:01 -08:00
|
|
|
output, input, clobbers...) \
|
2024-06-07 13:17:01 +02:00
|
|
|
asm_inline volatile(ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1, \
|
2024-06-07 13:16:55 +02:00
|
|
|
"call %c[new2]", ft_flags2) \
|
x86/alternatives, kvm: Fix a couple of CALLs without a frame pointer
objtool complains:
arch/x86/kvm/kvm.o: warning: objtool: .altinstr_replacement+0xc5: call without frame pointer save/setup
vmlinux.o: warning: objtool: .altinstr_replacement+0x2eb: call without frame pointer save/setup
Make sure %rSP is an output operand to the respective asm() statements.
The test_cc() hunk and ALT_OUTPUT_SP() courtesy of peterz. Also from him
add some helpful debugging info to the documentation.
Now on to the explanations:
tl;dr: The alternatives macros are pretty fragile.
If I do ALT_OUTPUT_SP(output) in order to be able to package in a %rsp
reference for objtool so that a stack frame gets properly generated, the
inline asm input operand with positional argument 0 in clear_page():
"0" (page)
gets "renumbered" due to the added
: "+r" (current_stack_pointer), "=D" (page)
and then gcc says:
./arch/x86/include/asm/page_64.h:53:9: error: inconsistent operand constraints in an ‘asm’
The fix is to use an explicit "D" constraint which points to a singleton
register class (gcc terminology) which ends up doing what is expected
here: the page pointer - input and output - should be in the same %rdi
register.
Other register classes have more than one register in them - example:
"r" and "=r" or "A":
‘A’
The ‘a’ and ‘d’ registers. This class is used for
instructions that return double word results in the ‘ax:dx’
register pair. Single word values will be allocated either in
‘ax’ or ‘dx’.
so using "D" and "=D" just works in this particular case.
And yes, one would say, sure, why don't you do "+D" but then:
: "+r" (current_stack_pointer), "+D" (page)
: [old] "i" (clear_page_orig), [new1] "i" (clear_page_rep), [new2] "i" (clear_page_erms),
: "cc", "memory", "rax", "rcx")
now find the Waldo^Wcomma which throws a wrench into all this.
Because that silly macro has an "input..." consume-all last macro arg
and in it, one is supposed to supply input *and* clobbers, leading to
silly syntax snafus.
Yap, they need to be cleaned up, one fine day...
Closes: https://lore.kernel.org/oe-kbuild-all/202406141648.jO9qNGLa-lkp@intel.com/
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Sean Christopherson <seanjc@google.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240625112056.GDZnqoGDXgYuWBDUwu@fat_crate.local
2024-06-18 21:57:27 +02:00
|
|
|
: ALT_OUTPUT_SP(output) \
|
2024-06-07 13:16:55 +02:00
|
|
|
: [old] "i" (oldfunc), [new1] "i" (newfunc1), \
|
2025-03-02 17:21:01 -08:00
|
|
|
[new2] "i" (newfunc2) \
|
|
|
|
COMMA(input) \
|
|
|
|
: clobbers)
|
2012-01-20 16:21:41 +00:00
|
|
|
|
x86/alternatives, kvm: Fix a couple of CALLs without a frame pointer
objtool complains:
arch/x86/kvm/kvm.o: warning: objtool: .altinstr_replacement+0xc5: call without frame pointer save/setup
vmlinux.o: warning: objtool: .altinstr_replacement+0x2eb: call without frame pointer save/setup
Make sure %rSP is an output operand to the respective asm() statements.
The test_cc() hunk and ALT_OUTPUT_SP() courtesy of peterz. Also from him
add some helpful debugging info to the documentation.
Now on to the explanations:
tl;dr: The alternatives macros are pretty fragile.
If I do ALT_OUTPUT_SP(output) in order to be able to package in a %rsp
reference for objtool so that a stack frame gets properly generated, the
inline asm input operand with positional argument 0 in clear_page():
"0" (page)
gets "renumbered" due to the added
: "+r" (current_stack_pointer), "=D" (page)
and then gcc says:
./arch/x86/include/asm/page_64.h:53:9: error: inconsistent operand constraints in an ‘asm’
The fix is to use an explicit "D" constraint which points to a singleton
register class (gcc terminology) which ends up doing what is expected
here: the page pointer - input and output - should be in the same %rdi
register.
Other register classes have more than one register in them - example:
"r" and "=r" or "A":
‘A’
The ‘a’ and ‘d’ registers. This class is used for
instructions that return double word results in the ‘ax:dx’
register pair. Single word values will be allocated either in
‘ax’ or ‘dx’.
so using "D" and "=D" just works in this particular case.
And yes, one would say, sure, why don't you do "+D" but then:
: "+r" (current_stack_pointer), "+D" (page)
: [old] "i" (clear_page_orig), [new1] "i" (clear_page_rep), [new2] "i" (clear_page_erms),
: "cc", "memory", "rax", "rcx")
now find the Waldo^Wcomma which throws a wrench into all this.
Because that silly macro has an "input..." consume-all last macro arg
and in it, one is supposed to supply input *and* clobbers, leading to
silly syntax snafus.
Yap, they need to be cleaned up, one fine day...
Closes: https://lore.kernel.org/oe-kbuild-all/202406141648.jO9qNGLa-lkp@intel.com/
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Sean Christopherson <seanjc@google.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240625112056.GDZnqoGDXgYuWBDUwu@fat_crate.local
2024-06-18 21:57:27 +02:00
|
|
|
#define ALT_OUTPUT_SP(...) ASM_CALL_CONSTRAINT, ## __VA_ARGS__
|
|
|
|
|
2023-11-29 14:33:29 +01:00
|
|
|
/* Macro for creating assembler functions avoiding any C magic. */
|
|
|
|
#define DEFINE_ASM_FUNC(func, instr, sec) \
|
|
|
|
asm (".pushsection " #sec ", \"ax\"\n" \
|
|
|
|
".global " #func "\n\t" \
|
|
|
|
".type " #func ", @function\n\t" \
|
|
|
|
ASM_FUNC_ALIGN "\n" \
|
|
|
|
#func ":\n\t" \
|
|
|
|
ASM_ENDBR \
|
|
|
|
instr "\n\t" \
|
|
|
|
ASM_RET \
|
|
|
|
".size " #func ", . - " #func "\n\t" \
|
|
|
|
".popsection")
|
|
|
|
|
|
|
|
void BUG_func(void);
|
|
|
|
void nop_func(void);
|
|
|
|
|
2025-03-19 11:30:57 +01:00
|
|
|
#else /* __ASSEMBLER__ */
|
2021-03-11 15:23:06 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
.macro LOCK_PREFIX
|
|
|
|
672: lock
|
|
|
|
.pushsection .smp_locks,"a"
|
|
|
|
.balign 4
|
|
|
|
.long 672b - .
|
|
|
|
.popsection
|
|
|
|
.endm
|
|
|
|
#else
|
|
|
|
.macro LOCK_PREFIX
|
|
|
|
.endm
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue one struct alt_instr descriptor entry (need to put it into
|
|
|
|
* the section .altinstructions, see below). This entry contains
|
|
|
|
* enough information for the alternatives patching code to patch an
|
|
|
|
* instruction. See apply_alternatives().
|
|
|
|
*/
|
2022-12-22 12:42:24 +01:00
|
|
|
.macro altinstr_entry orig alt ft_flags orig_len alt_len
|
2021-03-11 15:23:06 +01:00
|
|
|
.long \orig - .
|
|
|
|
.long \alt - .
|
2022-12-22 12:42:24 +01:00
|
|
|
.4byte \ft_flags
|
2021-03-11 15:23:06 +01:00
|
|
|
.byte \orig_len
|
|
|
|
.byte \alt_len
|
|
|
|
.endm
|
|
|
|
|
2023-12-10 07:21:36 +01:00
|
|
|
.macro ALT_CALL_INSTR
|
|
|
|
call BUG_func
|
|
|
|
.endm
|
|
|
|
|
2021-03-11 15:23:06 +01:00
|
|
|
/*
|
|
|
|
* Define an alternative between two instructions. If @feature is
|
|
|
|
* present, early code in apply_alternatives() replaces @oldinstr with
|
|
|
|
* @newinstr. ".skip" directive takes care of proper instruction padding
|
|
|
|
* in case @newinstr is longer than @oldinstr.
|
|
|
|
*/
|
2024-06-07 13:17:01 +02:00
|
|
|
#define __ALTERNATIVE(oldinst, newinst, flag) \
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
740: \
|
|
|
|
oldinst ; \
|
|
|
|
741: \
|
|
|
|
.skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;\
|
|
|
|
742: \
|
|
|
|
.pushsection .altinstructions,"a" ; \
|
|
|
|
altinstr_entry 740b,743f,flag,742b-740b,744f-743f ; \
|
|
|
|
.popsection ; \
|
|
|
|
.pushsection .altinstr_replacement,"ax" ; \
|
|
|
|
743: \
|
|
|
|
newinst ; \
|
|
|
|
744: \
|
|
|
|
.popsection ;
|
|
|
|
|
2024-06-07 13:16:58 +02:00
|
|
|
.macro ALTERNATIVE oldinstr, newinstr, ft_flags
|
2024-06-07 13:17:01 +02:00
|
|
|
__ALTERNATIVE(\oldinstr, \newinstr, \ft_flags)
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
.endm
|
|
|
|
|
2021-03-11 15:23:06 +01:00
|
|
|
/*
|
|
|
|
* Same as ALTERNATIVE macro above but for two alternatives. If CPU
|
|
|
|
* has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
|
|
|
|
* @feature2, it replaces @oldinstr with @feature2.
|
|
|
|
*/
|
2022-12-22 12:42:24 +01:00
|
|
|
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
|
2024-06-07 13:17:01 +02:00
|
|
|
__ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1),
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
\newinstr2, \ft_flags2)
|
|
|
|
.endm
|
|
|
|
|
2022-12-22 12:42:24 +01:00
|
|
|
.macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
|
2024-06-07 13:17:01 +02:00
|
|
|
__ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2),
|
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n()
implementations, use a nested alternative expression.
The only difference between:
ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
and
ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
newinst2, flag2)
is that the outer alternative can add additional padding when the inner
alternative is the shorter one, which then results in
alt_instr::instrlen being inconsistent.
However, this is easily remedied since the alt_instr entries will be
consecutive and it is trivial to compute the max(alt_instr::instrlen) at
runtime while patching.
Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion
(and manual layout), looks like this:
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
740:
740: \oldinstr ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr1 ;
744: .popsection ; ;
741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;
742: .pushsection .altinstructions,"a" ;
altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ;
.popsection ;
.pushsection .altinstr_replacement,"ax" ;
743: \newinstr2 ;
744: .popsection ;
.endm
The only label that is ambiguous is 740, however they all reference the
same spot, so that doesn't matter.
NOTE: obviously only @oldinstr may be an alternative; making @newinstr
an alternative would mean patching .altinstr_replacement which very
likely isn't what is intended, also the labels will be confused in that
case.
[ bp: Debug an issue where it would match the wrong two insns and
and consider them nested due to the same signed offsets in the
.alternative section and use instr_va() to compare the full virtual
addresses instead.
- Use new labels to denote that the new, nested
alternatives are being used when staring at preprocessed output.
- Use the %c constraint everywhere instead of %P and document the
difference for future reference. ]
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
2024-06-07 13:16:49 +02:00
|
|
|
\newinstr3, \ft_flags3)
|
|
|
|
.endm
|
|
|
|
|
2021-03-11 15:23:11 +01:00
|
|
|
/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
|
2022-12-22 12:42:24 +01:00
|
|
|
#define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
|
2021-03-11 15:23:11 +01:00
|
|
|
ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
|
2022-12-22 12:42:24 +01:00
|
|
|
newinstr_yes, ft_flags
|
2021-03-11 15:23:11 +01:00
|
|
|
|
2025-03-19 11:30:57 +01:00
|
|
|
#endif /* __ASSEMBLER__ */
|
2016-04-26 12:23:25 -07:00
|
|
|
|
2008-10-22 22:26:29 -07:00
|
|
|
#endif /* _ASM_X86_ALTERNATIVE_H */
|