Commit c43a43e4 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Ingo Molnar

x86/alternatives: Teach text_poke_bp() to emulate RET

Future patches will need to poke a RET instruction, provide the
infrastructure required for this.
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
Reviewed-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Link: https://lore.kernel.org/r/20200818135804.982214828@infradead.org
parent f03c4129
...@@ -53,6 +53,9 @@ extern void text_poke_finish(void); ...@@ -53,6 +53,9 @@ extern void text_poke_finish(void);
#define INT3_INSN_SIZE 1 #define INT3_INSN_SIZE 1
#define INT3_INSN_OPCODE 0xCC #define INT3_INSN_OPCODE 0xCC
#define RET_INSN_SIZE 1
#define RET_INSN_OPCODE 0xC3
#define CALL_INSN_SIZE 5 #define CALL_INSN_SIZE 5
#define CALL_INSN_OPCODE 0xE8 #define CALL_INSN_OPCODE 0xE8
...@@ -73,6 +76,7 @@ static __always_inline int text_opcode_size(u8 opcode) ...@@ -73,6 +76,7 @@ static __always_inline int text_opcode_size(u8 opcode)
switch(opcode) { switch(opcode) {
__CASE(INT3); __CASE(INT3);
__CASE(RET);
__CASE(CALL); __CASE(CALL);
__CASE(JMP32); __CASE(JMP32);
__CASE(JMP8); __CASE(JMP8);
...@@ -140,12 +144,27 @@ void int3_emulate_push(struct pt_regs *regs, unsigned long val) ...@@ -140,12 +144,27 @@ void int3_emulate_push(struct pt_regs *regs, unsigned long val)
*(unsigned long *)regs->sp = val; *(unsigned long *)regs->sp = val;
} }
static __always_inline
unsigned long int3_emulate_pop(struct pt_regs *regs)
{
unsigned long val = *(unsigned long *)regs->sp;
regs->sp += sizeof(unsigned long);
return val;
}
static __always_inline static __always_inline
void int3_emulate_call(struct pt_regs *regs, unsigned long func) void int3_emulate_call(struct pt_regs *regs, unsigned long func)
{ {
int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE); int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE);
int3_emulate_jmp(regs, func); int3_emulate_jmp(regs, func);
} }
static __always_inline
void int3_emulate_ret(struct pt_regs *regs)
{
unsigned long ip = int3_emulate_pop(regs);
int3_emulate_jmp(regs, ip);
}
#endif /* !CONFIG_UML_X86 */ #endif /* !CONFIG_UML_X86 */
#endif /* _ASM_X86_TEXT_PATCHING_H */ #endif /* _ASM_X86_TEXT_PATCHING_H */
...@@ -1103,6 +1103,10 @@ noinstr int poke_int3_handler(struct pt_regs *regs) ...@@ -1103,6 +1103,10 @@ noinstr int poke_int3_handler(struct pt_regs *regs)
*/ */
goto out_put; goto out_put;
case RET_INSN_OPCODE:
int3_emulate_ret(regs);
break;
case CALL_INSN_OPCODE: case CALL_INSN_OPCODE:
int3_emulate_call(regs, (long)ip + tp->rel32); int3_emulate_call(regs, (long)ip + tp->rel32);
break; break;
...@@ -1277,6 +1281,7 @@ static void text_poke_loc_init(struct text_poke_loc *tp, void *addr, ...@@ -1277,6 +1281,7 @@ static void text_poke_loc_init(struct text_poke_loc *tp, void *addr,
switch (tp->opcode) { switch (tp->opcode) {
case INT3_INSN_OPCODE: case INT3_INSN_OPCODE:
case RET_INSN_OPCODE:
break; break;
case CALL_INSN_OPCODE: case CALL_INSN_OPCODE:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment