From: marc.zyngier@arm.com (Marc Zyngier)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v3 05/19] arm64: alternatives: Add dynamic patching feature
Date: Tue, 19 Dec 2017 13:32:40 +0000 [thread overview]
Message-ID: <a5911baa-9b7f-a04b-7819-536dfade6da9@arm.com> (raw)
In-Reply-To: <20171219130437.nna4hwxhbbnawbq2@capper-debian.cambridge.arm.com>
Hi Steve,
On 19/12/17 13:04, Steve Capper wrote:
> Hi Marc,
>
> On Mon, Dec 18, 2017 at 05:39:12PM +0000, Marc Zyngier wrote:
>> We've so far relied on a patching infrastructure that only gave us
>> a single alternative, without any way to finely control what gets
>> patched. For a single feature, this is an all or nothing thing.
>>
>> It would be interesting to have a more fine grained way of patching
>> the kernel though, where we could dynamically tune the code that gets
>> injected.
>>
>> In order to achive this, let's introduce a new form of alternative
>> that is associated with a callback. This callback gets the instruction
>> sequence number and the old instruction as a parameter, and returns
>> the new instruction. This callback is always called, as the patching
>> decision is now done at runtime (not patching is equivalent to returning
>> the same instruction).
>>
>> Patching with a callback is declared with the new ALTERNATIVE_CB
>> and alternative_cb directives:
>>
>> asm volatile(ALTERNATIVE_CB("mov %0, #0\n", callback)
>> : "r" (v));
>> or
>> alternative_cb callback
>> mov x0, #0
>> alternative_cb_end
>>
>> where callback is the C function computing the alternative.
>>
>> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
>> ---
>> arch/arm64/include/asm/alternative.h | 36 ++++++++++++++++++++++++++----
>> arch/arm64/include/asm/alternative_types.h | 3 +++
>> arch/arm64/kernel/alternative.c | 21 +++++++++++++----
>> 3 files changed, 52 insertions(+), 8 deletions(-)
>>
>
> [...]
>
>> diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c
>> index 6dd0a3a3e5c9..cd299af96c95 100644
>> --- a/arch/arm64/kernel/alternative.c
>> +++ b/arch/arm64/kernel/alternative.c
>> @@ -110,25 +110,38 @@ static void __apply_alternatives(void *alt_region, bool use_linear_alias)
>> struct alt_instr *alt;
>> struct alt_region *region = alt_region;
>> __le32 *origptr, *replptr, *updptr;
>> + alternative_cb_t alt_cb;
>>
>> for (alt = region->begin; alt < region->end; alt++) {
>> u32 insn;
>> int i, nr_inst;
>>
>> - if (!cpus_have_cap(alt->cpufeature))
>> + /* Use ARM64_NCAPS as an unconditional patch */
>> + if (alt->cpufeature < ARM64_NCAPS &&
>> + !cpus_have_cap(alt->cpufeature))
>> continue;
>>
>> - BUG_ON(alt->alt_len != alt->orig_len);
>> + if (alt->cpufeature == ARM64_NCAPS)
>> + BUG_ON(alt->alt_len != 0);
>> + else
>> + BUG_ON(alt->alt_len != alt->orig_len);
>>
>> pr_info_once("patching kernel code\n");
>>
>> origptr = ALT_ORIG_PTR(alt);
>> replptr = ALT_REPL_PTR(alt);
>> + alt_cb = ALT_REPL_PTR(alt);
>> updptr = use_linear_alias ? lm_alias(origptr) : origptr;
>> - nr_inst = alt->alt_len / sizeof(insn);
>> + nr_inst = alt->orig_len / sizeof(insn);
>>
>> for (i = 0; i < nr_inst; i++) {
>> - insn = get_alt_insn(alt, origptr + i, replptr + i);
>> + if (alt->cpufeature == ARM64_NCAPS) {
>> + insn = le32_to_cpu(updptr[i]);
>> + insn = alt_cb(alt, i, insn);
>> + } else {
>> + insn = get_alt_insn(alt, origptr + i,
>> + replptr + i);
>> + }
>> updptr[i] = cpu_to_le32(insn);
>> }
>
> Is it possible to call the callback only once per entry (rather than
> once per instruction)? That would allow one to retain some more
> execution state in the callback, which may be handy if things get more
> elaborate.
Yeah, it was something that Catalin suggested too. I guess the only
thing that really annoys me about that is that we'd let the callback do
the write to the kernel text, which I find a bit... meh.
But overall I agree that it would be more useful, and make the loop a
bit less ugly.
I'll work something out for the next round!
Thanks,
M.
--
Jazz is not dead. It just smells funny...
next prev parent reply other threads:[~2017-12-19 13:32 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-18 17:39 [PATCH v3 00/19] KVM/arm64: Randomise EL2 mappings Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 01/19] arm64: asm-offsets: Avoid clashing DMA definitions Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 02/19] arm64: asm-offsets: Remove unused definitions Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 03/19] arm64: asm-offsets: Remove potential circular dependency Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 04/19] arm64: alternatives: Enforce alignment of struct alt_instr Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 05/19] arm64: alternatives: Add dynamic patching feature Marc Zyngier
2017-12-19 13:04 ` Steve Capper
2017-12-19 13:32 ` Marc Zyngier [this message]
2017-12-18 17:39 ` [PATCH v3 06/19] arm64: insn: Add N immediate encoding Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 07/19] arm64: insn: Add encoder for bitwise operations using literals Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 08/19] arm64: KVM: Dynamically patch the kernel/hyp VA mask Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 09/19] arm64: cpufeatures: Drop the ARM64_HYP_OFFSET_LOW feature flag Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 10/19] KVM: arm/arm64: Do not use kern_hyp_va() with kvm_vgic_global_state Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 11/19] KVM: arm/arm64: Demote HYP VA range display to being a debug feature Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 12/19] KVM: arm/arm64: Move ioremap calls to create_hyp_io_mappings Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 13/19] KVM: arm/arm64: Keep GICv2 HYP VAs in kvm_vgic_global_state Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 14/19] KVM: arm/arm64: Move HYP IO VAs to the "idmap" range Marc Zyngier
2017-12-20 13:16 ` Steve Capper
2017-12-26 11:03 ` Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 15/19] arm64; insn: Add encoder for the EXTR instruction Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 16/19] arm64: insn: Allow ADD/SUB (immediate) with LSL #12 Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 17/19] arm64: KVM: Dynamically compute the HYP VA mask Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 18/19] arm64: KVM: Introduce EL2 VA randomisation Marc Zyngier
2017-12-18 17:39 ` [PATCH v3 19/19] arm64: Update the KVM memory map documentation Marc Zyngier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a5911baa-9b7f-a04b-7819-536dfade6da9@arm.com \
--to=marc.zyngier@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox