From: Peter Zijlstra <peterz@infradead.org>
To: Borislav Petkov <bp@alien8.de>
Cc: x86@kernel.org, Masami Hiramatsu <mhiramat@kernel.org>,
"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
Andrew Cooper <Andrew.Cooper3@citrix.com>,
Josh Poimboeuf <jpoimboe@redhat.com>,
linux-kernel@vger.kernel.org
Subject: Re: [RFC] x86/alternative: Support relocations in alternatives
Date: Fri, 3 Feb 2023 16:12:06 +0100 [thread overview]
Message-ID: <Y90kRtZA0d8nsRRj@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <Y90ilxBFMAQZ/dRg@hirez.programming.kicks-ass.net>
On Fri, Feb 03, 2023 at 04:04:55PM +0100, Peter Zijlstra wrote:
> > > + switch (insn.opcode.bytes[0]) {
> > > + case 0x0f:
> > > + if (insn.opcode.bytes[1] < 0x80 ||
> > > + insn.opcode.bytes[1] > 0x8f)
> > > + break;
> > > +
> > > + fallthrough; /* Jcc.d32 */
> > > + case 0x70 ... 0x7f: /* Jcc.d8 */
> > > + case JMP8_INSN_OPCODE:
> > > + case JMP32_INSN_OPCODE:
> > > + case CALL_INSN_OPCODE:
> > > + u8 *target = src + i + insn.length + insn.immediate.value;
> > > + if (target < src || target > src + src_len) {
> > > + apply_reloc(insn.immediate.nbytes,
> > > + instr + i + insn_offset_immediate(&insn),
> > > + src - dest);
> > > + }
> >
> > Uff, it took me a while to parse this. So this can be simpler. The basic
> > math is:
> >
> > new_offset = next_rip - target_address;
> >
> > where
> > next_rip = instr + insn.length;
> >
> > and I admit that my function was a bit clumsy but I think yours can be
> > made simpler while keeping it cleaner.
>
> I'm not sure what you're saying here... so let me walk through the whole
> thing (specifically the immediate case, since that's what you quote). So
> what we need is:
>
> src_imm = target - src_next_ip (1)
>
> what we want is:
>
> dst_imm = target - dst_next_ip (2)
>
> so what we do is rework (1) as an expression for target like:
>
> target = src_imm + src_next_ip (1a)
>
> and substitute in (2) to get:
>
> dst_imm = (src_imm + src_next_ip) - dst_next_ip (3)
>
> Now, since the instruction stream is 'identical' at src and dest
> we can state that:
>
> src_next_ip = src + ip_offset
> dst_next_ip = dst + ip_offset (4)
>
> Substitute (4) in (3) and observe ip_offset being cancelled out to
> obtain:
>
> dst_imm = src_imm + (src + ip_offset) - (dst + ip_offset)
> = src_imm + src - dst + ip_offset - ip_offset
> = src_imm + src - dst (5)
>
> The very thing I did.
>
> IOW, we can correct the displacement without caring about which actual
> instruction in the stream we're correcting since the relative offset is
> given by 'src - dst'. IOW, we don't give a crap about insn.length in
> this case ;-)
Specifically, in the above case ip_offset would be given by:
ip_offset = i + insn.length
where i is the offset in the buffer to the current instruction and
insn.length is well, the length of the current instruction ;-)
next prev parent reply other threads:[~2023-02-03 15:17 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-01 14:10 Peter Zijlstra
2023-02-01 17:02 ` Kirill A. Shutemov
2023-02-03 13:13 ` Borislav Petkov
2023-02-03 15:04 ` Peter Zijlstra
2023-02-03 15:12 ` Peter Zijlstra [this message]
2023-02-03 16:04 ` Borislav Petkov
2023-02-03 16:46 ` Borislav Petkov
2023-02-03 23:25 ` Borislav Petkov
2023-02-06 14:29 ` Borislav Petkov
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=Y90kRtZA0d8nsRRj@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=Andrew.Cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=jpoimboe@redhat.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=x86@kernel.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
all inboxes | Powered by JetHome®