]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.19.46/x86_64-allow-breakpoints-to-emulate-call-instructions.patch
Linux 5.0.19
[thirdparty/kernel/stable-queue.git] / releases / 4.19.46 / x86_64-allow-breakpoints-to-emulate-call-instructions.patch
1 From 4b33dadf37666c0860b88f9e52a16d07bf6d0b03 Mon Sep 17 00:00:00 2001
2 From: Peter Zijlstra <peterz@infradead.org>
3 Date: Wed, 1 May 2019 15:11:17 +0200
4 Subject: x86_64: Allow breakpoints to emulate call instructions
5
6 From: Peter Zijlstra <peterz@infradead.org>
7
8 commit 4b33dadf37666c0860b88f9e52a16d07bf6d0b03 upstream.
9
10 In order to allow breakpoints to emulate call instructions, they need to push
11 the return address onto the stack. The x86_64 int3 handler adds a small gap
12 to allow the stack to grow some. Use this gap to add the return address to
13 be able to emulate a call instruction at the breakpoint location.
14
15 These helper functions are added:
16
17 int3_emulate_jmp(): changes the location of the regs->ip to return there.
18
19 (The next two are only for x86_64)
20 int3_emulate_push(): to push the address onto the gap in the stack
21 int3_emulate_call(): push the return address and change regs->ip
22
23 Cc: Andy Lutomirski <luto@kernel.org>
24 Cc: Nicolai Stange <nstange@suse.de>
25 Cc: Thomas Gleixner <tglx@linutronix.de>
26 Cc: Ingo Molnar <mingo@redhat.com>
27 Cc: Borislav Petkov <bp@alien8.de>
28 Cc: "H. Peter Anvin" <hpa@zytor.com>
29 Cc: the arch/x86 maintainers <x86@kernel.org>
30 Cc: Josh Poimboeuf <jpoimboe@redhat.com>
31 Cc: Jiri Kosina <jikos@kernel.org>
32 Cc: Miroslav Benes <mbenes@suse.cz>
33 Cc: Petr Mladek <pmladek@suse.com>
34 Cc: Joe Lawrence <joe.lawrence@redhat.com>
35 Cc: Shuah Khan <shuah@kernel.org>
36 Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
37 Cc: Tim Chen <tim.c.chen@linux.intel.com>
38 Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
39 Cc: Mimi Zohar <zohar@linux.ibm.com>
40 Cc: Juergen Gross <jgross@suse.com>
41 Cc: Nick Desaulniers <ndesaulniers@google.com>
42 Cc: Nayna Jain <nayna@linux.ibm.com>
43 Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
44 Cc: Joerg Roedel <jroedel@suse.de>
45 Cc: "open list:KERNEL SELFTEST FRAMEWORK" <linux-kselftest@vger.kernel.org>
46 Cc: stable@vger.kernel.org
47 Fixes: b700e7f03df5 ("livepatch: kernel: add support for live patching")
48 Tested-by: Nicolai Stange <nstange@suse.de>
49 Reviewed-by: Nicolai Stange <nstange@suse.de>
50 Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
51 Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
52 [ Modified to only work for x86_64 and added comment to int3_emulate_push() ]
53 Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
54 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
55
56 ---
57 arch/x86/include/asm/text-patching.h | 28 ++++++++++++++++++++++++++++
58 1 file changed, 28 insertions(+)
59
60 --- a/arch/x86/include/asm/text-patching.h
61 +++ b/arch/x86/include/asm/text-patching.h
62 @@ -39,4 +39,32 @@ extern int poke_int3_handler(struct pt_r
63 extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
64 extern int after_bootmem;
65
66 +static inline void int3_emulate_jmp(struct pt_regs *regs, unsigned long ip)
67 +{
68 + regs->ip = ip;
69 +}
70 +
71 +#define INT3_INSN_SIZE 1
72 +#define CALL_INSN_SIZE 5
73 +
74 +#ifdef CONFIG_X86_64
75 +static inline void int3_emulate_push(struct pt_regs *regs, unsigned long val)
76 +{
77 + /*
78 + * The int3 handler in entry_64.S adds a gap between the
79 + * stack where the break point happened, and the saving of
80 + * pt_regs. We can extend the original stack because of
81 + * this gap. See the idtentry macro's create_gap option.
82 + */
83 + regs->sp -= sizeof(unsigned long);
84 + *(unsigned long *)regs->sp = val;
85 +}
86 +
87 +static inline void int3_emulate_call(struct pt_regs *regs, unsigned long func)
88 +{
89 + int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE);
90 + int3_emulate_jmp(regs, func);
91 +}
92 +#endif
93 +
94 #endif /* _ASM_X86_TEXT_PATCHING_H */