Skip to content

Commit 3c88ee1

Browse files
mhiramatrostedt
authored andcommitted
x86: ptrace: Add function argument access API
Add regs_get_argument() which returns N th argument of the function call. Note that this chooses most probably assignment, in some case it can be incorrect (e.g. passing data structure or floating point etc.) This is expected to be called from kprobes or ftrace with regs where the top of stack is the return address. Link: http://lkml.kernel.org/r/152465885737.26224.2822487520472783854.stgit@devbox Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 40b53b7 commit 3c88ee1

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

arch/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ config HAVE_RSEQ
290290
This symbol should be selected by an architecture if it
291291
supports an implementation of restartable sequences.
292292

293+
config HAVE_FUNCTION_ARG_ACCESS_API
294+
bool
295+
help
296+
This symbol should be selected by an architecure if it supports
297+
the API needed to access function arguments from pt_regs,
298+
declared in asm/ptrace.h
299+
293300
config HAVE_CLK
294301
bool
295302
help

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ config X86
184184
select HAVE_RCU_TABLE_INVALIDATE if HAVE_RCU_TABLE_FREE
185185
select HAVE_REGS_AND_STACK_ACCESS_API
186186
select HAVE_RELIABLE_STACKTRACE if X86_64 && (UNWINDER_FRAME_POINTER || UNWINDER_ORC) && STACK_VALIDATION
187+
select HAVE_FUNCTION_ARG_ACCESS_API
187188
select HAVE_STACKPROTECTOR if CC_HAS_SANE_STACKPROTECTOR
188189
select HAVE_STACK_VALIDATION if X86_64
189190
select HAVE_RSEQ

arch/x86/include/asm/ptrace.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,44 @@ static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
256256
return 0;
257257
}
258258

259+
/**
260+
* regs_get_kernel_argument() - get Nth function argument in kernel
261+
* @regs: pt_regs of that context
262+
* @n: function argument number (start from 0)
263+
*
264+
* regs_get_argument() returns @n th argument of the function call.
265+
* Note that this chooses most probably assignment, in some case
266+
* it can be incorrect.
267+
* This is expected to be called from kprobes or ftrace with regs
268+
* where the top of stack is the return address.
269+
*/
270+
static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
271+
unsigned int n)
272+
{
273+
static const unsigned int argument_offs[] = {
274+
#ifdef __i386__
275+
offsetof(struct pt_regs, ax),
276+
offsetof(struct pt_regs, cx),
277+
offsetof(struct pt_regs, dx),
278+
#define NR_REG_ARGUMENTS 3
279+
#else
280+
offsetof(struct pt_regs, di),
281+
offsetof(struct pt_regs, si),
282+
offsetof(struct pt_regs, dx),
283+
offsetof(struct pt_regs, cx),
284+
offsetof(struct pt_regs, r8),
285+
offsetof(struct pt_regs, r9),
286+
#define NR_REG_ARGUMENTS 6
287+
#endif
288+
};
289+
290+
if (n >= NR_REG_ARGUMENTS) {
291+
n -= NR_REG_ARGUMENTS - 1;
292+
return regs_get_kernel_stack_nth(regs, n);
293+
} else
294+
return regs_get_register(regs, argument_offs[n]);
295+
}
296+
259297
#define arch_has_single_step() (1)
260298
#ifdef CONFIG_X86_DEBUGCTLMSR
261299
#define arch_has_block_step() (1)

0 commit comments

Comments
 (0)