Re: [PATCH -v5 04/17] x86/alternatives: Add and use text_gen_insn() helper
From: Peter Zijlstra
Date: Tue Nov 12 2019 - 17:25:50 EST
On Tue, Nov 12, 2019 at 12:10:28PM -0500, Steven Rostedt wrote:
> On Mon, 11 Nov 2019 14:12:56 +0100
> Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> > +void *text_gen_insn(u8 opcode, const void *addr, const void *dest)
> > +{
> > + static union text_poke_insn insn; /* text_mutex */
> > + int size = 0;
> > +
> > + lockdep_assert_held(&text_mutex);
> > +
> > + insn.opcode = opcode;
> > +
> > +#define __CASE(insn) \
> > + case insn##_INSN_OPCODE: size = insn##_INSN_SIZE; break
> > +
> > + switch(opcode) {
> > + __CASE(INT3);
> > + __CASE(CALL);
> > + __CASE(JMP32);
> > + __CASE(JMP8);
> > + }
> > +
> > + if (size > 1) {
> > + insn.disp = (long)dest - (long)(addr + size);
> > + if (size == 2)
>
> Could we add a comment here. It took me a little bit to figure out why
> you have this BUG_ON().
:-)
Sure, I'll add something like:
/*
* Ensure that for JMP.d8 the displacement
* actually fits the signed byte.
*/
> > + BUG_ON((insn.disp >> 31) != (insn.disp >> 7));
> > + }
> > +
> > + return &insn.text;
> > +}