public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
       [not found] <20090701010838.32547.62843.stgit@localhost.localdomain>
@ 2009-07-01  1:09 ` Masami Hiramatsu
  2009-07-01  1:09   ` Masami Hiramatsu
                     ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Masami Hiramatsu @ 2009-07-01  1:09 UTC (permalink / raw)
  To: lkml
  Cc: systemtap, kvm, DLE, Masami Hiramatsu, Christoph Hellwig,
	Steven Rostedt, Ananth N Mavinakayanahalli, Ingo Molnar,
	Frederic Weisbecker, Roland McGrath, Srikar Dronamraju,
	linux-arch

Add following APIs for accessing registers and stack entries from pt_regs.

- regs_query_register_offset(const char *name)
   Query the offset of "name" register.

- regs_query_register_name(unsigned offset)
   Query the name of register by its offset.

- regs_get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
   Check the address is in the kernel stack.

- regs_get_kernel_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the kernel stack. (N >= 0)

- regs_get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

Changes from v9:
 -Fix a typo in a comment.

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Cc: linux-arch@vger.kernel.org
---

 arch/x86/include/asm/ptrace.h |  122 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |   73 +++++++++++++++++++++++++
 2 files changed, 195 insertions(+), 0 deletions(-)

diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index 0f0d908..d5e3b3b 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@
 
 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif
 
 #ifndef __ASSEMBLY__
@@ -216,6 +217,127 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }
 
+/* Query offset/name of register from its name/offset */
+extern int regs_query_register_offset(const char *name);
+extern const char *regs_query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
+
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:	pt_regs from which register value is gotten.
+ * @offset:	offset number of the register.
+ *
+ * regs_get_register returns the value of a register whose offset from @regs
+ * is @offset. The @offset is the offset of the register in struct pt_regs.
+ * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+					      unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/**
+ * regs_within_kernel_stack() - check the address in the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @addr:	address which is checked.
+ *
+ * regs_within_kenel_stack() checks @addr is within the kernel stack page(s).
+ * If @addr is within the kernel stack, it returns true. If not, returns false.
+ */
+static inline int regs_within_kernel_stack(struct pt_regs *regs,
+					   unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/**
+ * regs_get_kernel_stack_nth() - get Nth entry of the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @n:		stack entry number.
+ *
+ * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
+ * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
+ * this returns 0.
+ */
+static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
+						      unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
+	addr += n;
+	if (regs_within_kernel_stack(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/**
+ * regs_get_argument_nth() - get Nth argument at function call
+ * @regs:	pt_regs which contains registers at function entry.
+ * @n:		argument number.
+ *
+ * regs_get_argument_nth() returns @n th argument of a function call.
+ * Since usually the kernel stack will be changed right after function entry,
+ * you must use this at function entry. If the @n th entry is NOT in the
+ * kernel stack or pt_regs, this returns 0.
+ */
+#ifdef CONFIG_X86_32
+#define NR_REGPARMS 3
+static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
+						  unsigned n)
+{
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0:
+			return regs->ax;
+		case 1:
+			return regs->dx;
+		case 2:
+			return regs->cx;
+		}
+		return 0;
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+#else /* CONFIG_X86_64 */
+#define NR_REGPARMS 6
+static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
+						  unsigned n)
+{
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0:
+			return regs->di;
+		case 1:
+			return regs->si;
+		case 2:
+			return regs->dx;
+		case 3:
+			return regs->cx;
+		case 4:
+			return regs->r8;
+		case 5:
+			return regs->r9;
+		}
+		return 0;
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+#endif
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index b457f78..2944d3a 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -49,6 +49,79 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };
 
+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static const struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_NAME(ss),
+	REG_OFFSET_END,
+};
+
+/**
+ * regs_query_register_offset() - query register offset from its name
+ * @name:	the name of a register
+ *
+ * regs_query_register_offset() returns the offset of a register in struct
+ * pt_regs from its name. If the name is invalid, this returns -EINVAL;
+ */
+int regs_query_register_offset(const char *name)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return roff->offset;
+	return -EINVAL;
+}
+
+/**
+ * regs_query_register_name() - query register name from its offset
+ * @offset:	the offset of a register in struct pt_regs.
+ *
+ * regs_query_register_name() returns the name of a register from its
+ * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
+ */
+const char *regs_query_register_name(unsigned offset)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.


-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-01  1:09 ` [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs Masami Hiramatsu
@ 2009-07-01  1:09   ` Masami Hiramatsu
  2009-07-06  1:42   ` Frederic Weisbecker
  2009-07-06 14:34   ` Andi Kleen
  2 siblings, 0 replies; 11+ messages in thread
From: Masami Hiramatsu @ 2009-07-01  1:09 UTC (permalink / raw)
  To: Ingo Molnar, Steven Rostedt, lkml
  Cc: systemtap, kvm, DLE, Masami Hiramatsu, Christoph Hellwig,
	Ananth N Mavinakayanahalli, Frederic Weisbecker, Roland McGrath,
	Srikar Dronamraju, linux-arch

Add following APIs for accessing registers and stack entries from pt_regs.

- regs_query_register_offset(const char *name)
   Query the offset of "name" register.

- regs_query_register_name(unsigned offset)
   Query the name of register by its offset.

- regs_get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
   Check the address is in the kernel stack.

- regs_get_kernel_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the kernel stack. (N >= 0)

- regs_get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

Changes from v9:
 -Fix a typo in a comment.

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Cc: linux-arch@vger.kernel.org
---

 arch/x86/include/asm/ptrace.h |  122 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |   73 +++++++++++++++++++++++++
 2 files changed, 195 insertions(+), 0 deletions(-)

diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index 0f0d908..d5e3b3b 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@
 
 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif
 
 #ifndef __ASSEMBLY__
@@ -216,6 +217,127 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }
 
+/* Query offset/name of register from its name/offset */
+extern int regs_query_register_offset(const char *name);
+extern const char *regs_query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
+
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:	pt_regs from which register value is gotten.
+ * @offset:	offset number of the register.
+ *
+ * regs_get_register returns the value of a register whose offset from @regs
+ * is @offset. The @offset is the offset of the register in struct pt_regs.
+ * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+					      unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/**
+ * regs_within_kernel_stack() - check the address in the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @addr:	address which is checked.
+ *
+ * regs_within_kenel_stack() checks @addr is within the kernel stack page(s).
+ * If @addr is within the kernel stack, it returns true. If not, returns false.
+ */
+static inline int regs_within_kernel_stack(struct pt_regs *regs,
+					   unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/**
+ * regs_get_kernel_stack_nth() - get Nth entry of the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @n:		stack entry number.
+ *
+ * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
+ * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
+ * this returns 0.
+ */
+static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
+						      unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
+	addr += n;
+	if (regs_within_kernel_stack(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/**
+ * regs_get_argument_nth() - get Nth argument at function call
+ * @regs:	pt_regs which contains registers at function entry.
+ * @n:		argument number.
+ *
+ * regs_get_argument_nth() returns @n th argument of a function call.
+ * Since usually the kernel stack will be changed right after function entry,
+ * you must use this at function entry. If the @n th entry is NOT in the
+ * kernel stack or pt_regs, this returns 0.
+ */
+#ifdef CONFIG_X86_32
+#define NR_REGPARMS 3
+static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
+						  unsigned n)
+{
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0:
+			return regs->ax;
+		case 1:
+			return regs->dx;
+		case 2:
+			return regs->cx;
+		}
+		return 0;
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+#else /* CONFIG_X86_64 */
+#define NR_REGPARMS 6
+static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
+						  unsigned n)
+{
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0:
+			return regs->di;
+		case 1:
+			return regs->si;
+		case 2:
+			return regs->dx;
+		case 3:
+			return regs->cx;
+		case 4:
+			return regs->r8;
+		case 5:
+			return regs->r9;
+		}
+		return 0;
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+#endif
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index b457f78..2944d3a 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -49,6 +49,79 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };
 
+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static const struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_NAME(ss),
+	REG_OFFSET_END,
+};
+
+/**
+ * regs_query_register_offset() - query register offset from its name
+ * @name:	the name of a register
+ *
+ * regs_query_register_offset() returns the offset of a register in struct
+ * pt_regs from its name. If the name is invalid, this returns -EINVAL;
+ */
+int regs_query_register_offset(const char *name)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return roff->offset;
+	return -EINVAL;
+}
+
+/**
+ * regs_query_register_name() - query register name from its offset
+ * @offset:	the offset of a register in struct pt_regs.
+ *
+ * regs_query_register_name() returns the name of a register from its
+ * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
+ */
+const char *regs_query_register_name(unsigned offset)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.


-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-01  1:09 ` [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs Masami Hiramatsu
  2009-07-01  1:09   ` Masami Hiramatsu
@ 2009-07-06  1:42   ` Frederic Weisbecker
  2009-07-06  1:42     ` Frederic Weisbecker
  2009-07-06 14:34   ` Andi Kleen
  2 siblings, 1 reply; 11+ messages in thread
From: Frederic Weisbecker @ 2009-07-06  1:42 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm, DLE,
	Christoph Hellwig, Ananth N Mavinakayanahalli, Roland McGrath,
	Srikar Dronamraju, linux-arch

On Tue, Jun 30, 2009 at 09:09:11PM -0400, Masami Hiramatsu wrote:
> Add following APIs for accessing registers and stack entries from pt_regs.
> 
> - regs_query_register_offset(const char *name)
>    Query the offset of "name" register.
> 
> - regs_query_register_name(unsigned offset)
>    Query the name of register by its offset.
> 
> - regs_get_register(struct pt_regs *regs, unsigned offset)
>    Get the value of a register by its offset.
> 
> - regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
>    Check the address is in the kernel stack.
> 
> - regs_get_kernel_stack_nth(struct pt_regs *reg, unsigned nth)
>    Get Nth entry of the kernel stack. (N >= 0)
> 
> - regs_get_argument_nth(struct pt_regs *reg, unsigned nth)
>    Get Nth argument at function call. (N >= 0)
> 
> Changes from v9:
>  -Fix a typo in a comment.
> 
> Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
> Cc: Christoph Hellwig <hch@infradead.org>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
> Cc: Ingo Molnar <mingo@elte.hu>
> Cc: Frederic Weisbecker <fweisbec@gmail.com>
> Cc: Roland McGrath <roland@redhat.com>
> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
> Cc: linux-arch@vger.kernel.org



Looks good!

Reviewed-by: Frederic Weisbecker <fweisbec@gmail.com>

Frederic.



> ---
> 
>  arch/x86/include/asm/ptrace.h |  122 +++++++++++++++++++++++++++++++++++++++++
>  arch/x86/kernel/ptrace.c      |   73 +++++++++++++++++++++++++
>  2 files changed, 195 insertions(+), 0 deletions(-)
> 
> diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
> index 0f0d908..d5e3b3b 100644
> --- a/arch/x86/include/asm/ptrace.h
> +++ b/arch/x86/include/asm/ptrace.h
> @@ -7,6 +7,7 @@
>  
>  #ifdef __KERNEL__
>  #include <asm/segment.h>
> +#include <asm/page_types.h>
>  #endif
>  
>  #ifndef __ASSEMBLY__
> @@ -216,6 +217,127 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
>  	return regs->sp;
>  }
>  
> +/* Query offset/name of register from its name/offset */
> +extern int regs_query_register_offset(const char *name);
> +extern const char *regs_query_register_name(unsigned offset);
> +#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
> +
> +/**
> + * regs_get_register() - get register value from its offset
> + * @regs:	pt_regs from which register value is gotten.
> + * @offset:	offset number of the register.
> + *
> + * regs_get_register returns the value of a register whose offset from @regs
> + * is @offset. The @offset is the offset of the register in struct pt_regs.
> + * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
> + */
> +static inline unsigned long regs_get_register(struct pt_regs *regs,
> +					      unsigned offset)
> +{
> +	if (unlikely(offset > MAX_REG_OFFSET))
> +		return 0;
> +	return *(unsigned long *)((unsigned long)regs + offset);
> +}
> +
> +/**
> + * regs_within_kernel_stack() - check the address in the stack
> + * @regs:	pt_regs which contains kernel stack pointer.
> + * @addr:	address which is checked.
> + *
> + * regs_within_kenel_stack() checks @addr is within the kernel stack page(s).
> + * If @addr is within the kernel stack, it returns true. If not, returns false.
> + */
> +static inline int regs_within_kernel_stack(struct pt_regs *regs,
> +					   unsigned long addr)
> +{
> +	return ((addr & ~(THREAD_SIZE - 1))  ==
> +		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
> +}
> +
> +/**
> + * regs_get_kernel_stack_nth() - get Nth entry of the stack
> + * @regs:	pt_regs which contains kernel stack pointer.
> + * @n:		stack entry number.
> + *
> + * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
> + * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
> + * this returns 0.
> + */
> +static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
> +						      unsigned n)
> +{
> +	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
> +	addr += n;
> +	if (regs_within_kernel_stack(regs, (unsigned long)addr))
> +		return *addr;
> +	else
> +		return 0;
> +}
> +
> +/**
> + * regs_get_argument_nth() - get Nth argument at function call
> + * @regs:	pt_regs which contains registers at function entry.
> + * @n:		argument number.
> + *
> + * regs_get_argument_nth() returns @n th argument of a function call.
> + * Since usually the kernel stack will be changed right after function entry,
> + * you must use this at function entry. If the @n th entry is NOT in the
> + * kernel stack or pt_regs, this returns 0.
> + */
> +#ifdef CONFIG_X86_32
> +#define NR_REGPARMS 3
> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
> +						  unsigned n)
> +{
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->ax;
> +		case 1:
> +			return regs->dx;
> +		case 2:
> +			return regs->cx;
> +		}
> +		return 0;
> +	} else {
> +		/*
> +		 * The typical case: arg n is on the stack.
> +		 * (Note: stack[0] = return address, so skip it)
> +		 */
> +		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
> +	}
> +}
> +#else /* CONFIG_X86_64 */
> +#define NR_REGPARMS 6
> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
> +						  unsigned n)
> +{
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->di;
> +		case 1:
> +			return regs->si;
> +		case 2:
> +			return regs->dx;
> +		case 3:
> +			return regs->cx;
> +		case 4:
> +			return regs->r8;
> +		case 5:
> +			return regs->r9;
> +		}
> +		return 0;
> +	} else {
> +		/*
> +		 * The typical case: arg n is on the stack.
> +		 * (Note: stack[0] = return address, so skip it)
> +		 */
> +		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
> +	}
> +}
> +#endif
> +
>  /*
>   * These are defined as per linux/ptrace.h, which see.
>   */
> diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
> index b457f78..2944d3a 100644
> --- a/arch/x86/kernel/ptrace.c
> +++ b/arch/x86/kernel/ptrace.c
> @@ -49,6 +49,79 @@ enum x86_regset {
>  	REGSET_IOPERM32,
>  };
>  
> +struct pt_regs_offset {
> +	const char *name;
> +	int offset;
> +};
> +
> +#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
> +#define REG_OFFSET_END {.name = NULL, .offset = 0}
> +
> +static const struct pt_regs_offset regoffset_table[] = {
> +#ifdef CONFIG_X86_64
> +	REG_OFFSET_NAME(r15),
> +	REG_OFFSET_NAME(r14),
> +	REG_OFFSET_NAME(r13),
> +	REG_OFFSET_NAME(r12),
> +	REG_OFFSET_NAME(r11),
> +	REG_OFFSET_NAME(r10),
> +	REG_OFFSET_NAME(r9),
> +	REG_OFFSET_NAME(r8),
> +#endif
> +	REG_OFFSET_NAME(bx),
> +	REG_OFFSET_NAME(cx),
> +	REG_OFFSET_NAME(dx),
> +	REG_OFFSET_NAME(si),
> +	REG_OFFSET_NAME(di),
> +	REG_OFFSET_NAME(bp),
> +	REG_OFFSET_NAME(ax),
> +#ifdef CONFIG_X86_32
> +	REG_OFFSET_NAME(ds),
> +	REG_OFFSET_NAME(es),
> +	REG_OFFSET_NAME(fs),
> +	REG_OFFSET_NAME(gs),
> +#endif
> +	REG_OFFSET_NAME(orig_ax),
> +	REG_OFFSET_NAME(ip),
> +	REG_OFFSET_NAME(cs),
> +	REG_OFFSET_NAME(flags),
> +	REG_OFFSET_NAME(sp),
> +	REG_OFFSET_NAME(ss),
> +	REG_OFFSET_END,
> +};
> +
> +/**
> + * regs_query_register_offset() - query register offset from its name
> + * @name:	the name of a register
> + *
> + * regs_query_register_offset() returns the offset of a register in struct
> + * pt_regs from its name. If the name is invalid, this returns -EINVAL;
> + */
> +int regs_query_register_offset(const char *name)
> +{
> +	const struct pt_regs_offset *roff;
> +	for (roff = regoffset_table; roff->name != NULL; roff++)
> +		if (!strcmp(roff->name, name))
> +			return roff->offset;
> +	return -EINVAL;
> +}
> +
> +/**
> + * regs_query_register_name() - query register name from its offset
> + * @offset:	the offset of a register in struct pt_regs.
> + *
> + * regs_query_register_name() returns the name of a register from its
> + * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
> + */
> +const char *regs_query_register_name(unsigned offset)
> +{
> +	const struct pt_regs_offset *roff;
> +	for (roff = regoffset_table; roff->name != NULL; roff++)
> +		if (roff->offset == offset)
> +			return roff->name;
> +	return NULL;
> +}
> +
>  /*
>   * does not yet catch signals sent when the child dies.
>   * in exit.c or in signal.c.
> 
> 
> -- 
> Masami Hiramatsu
> 
> Software Engineer
> Hitachi Computer Products (America), Inc.
> Software Solutions Division
> 
> e-mail: mhiramat@redhat.com


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-06  1:42   ` Frederic Weisbecker
@ 2009-07-06  1:42     ` Frederic Weisbecker
  0 siblings, 0 replies; 11+ messages in thread
From: Frederic Weisbecker @ 2009-07-06  1:42 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm, DLE,
	Christoph Hellwig, Ananth N Mavinakayanahalli, Roland McGrath,
	Srikar Dronamraju, linux-arch

On Tue, Jun 30, 2009 at 09:09:11PM -0400, Masami Hiramatsu wrote:
> Add following APIs for accessing registers and stack entries from pt_regs.
> 
> - regs_query_register_offset(const char *name)
>    Query the offset of "name" register.
> 
> - regs_query_register_name(unsigned offset)
>    Query the name of register by its offset.
> 
> - regs_get_register(struct pt_regs *regs, unsigned offset)
>    Get the value of a register by its offset.
> 
> - regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
>    Check the address is in the kernel stack.
> 
> - regs_get_kernel_stack_nth(struct pt_regs *reg, unsigned nth)
>    Get Nth entry of the kernel stack. (N >= 0)
> 
> - regs_get_argument_nth(struct pt_regs *reg, unsigned nth)
>    Get Nth argument at function call. (N >= 0)
> 
> Changes from v9:
>  -Fix a typo in a comment.
> 
> Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
> Cc: Christoph Hellwig <hch@infradead.org>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
> Cc: Ingo Molnar <mingo@elte.hu>
> Cc: Frederic Weisbecker <fweisbec@gmail.com>
> Cc: Roland McGrath <roland@redhat.com>
> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
> Cc: linux-arch@vger.kernel.org



Looks good!

Reviewed-by: Frederic Weisbecker <fweisbec@gmail.com>

Frederic.



> ---
> 
>  arch/x86/include/asm/ptrace.h |  122 +++++++++++++++++++++++++++++++++++++++++
>  arch/x86/kernel/ptrace.c      |   73 +++++++++++++++++++++++++
>  2 files changed, 195 insertions(+), 0 deletions(-)
> 
> diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
> index 0f0d908..d5e3b3b 100644
> --- a/arch/x86/include/asm/ptrace.h
> +++ b/arch/x86/include/asm/ptrace.h
> @@ -7,6 +7,7 @@
>  
>  #ifdef __KERNEL__
>  #include <asm/segment.h>
> +#include <asm/page_types.h>
>  #endif
>  
>  #ifndef __ASSEMBLY__
> @@ -216,6 +217,127 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
>  	return regs->sp;
>  }
>  
> +/* Query offset/name of register from its name/offset */
> +extern int regs_query_register_offset(const char *name);
> +extern const char *regs_query_register_name(unsigned offset);
> +#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
> +
> +/**
> + * regs_get_register() - get register value from its offset
> + * @regs:	pt_regs from which register value is gotten.
> + * @offset:	offset number of the register.
> + *
> + * regs_get_register returns the value of a register whose offset from @regs
> + * is @offset. The @offset is the offset of the register in struct pt_regs.
> + * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
> + */
> +static inline unsigned long regs_get_register(struct pt_regs *regs,
> +					      unsigned offset)
> +{
> +	if (unlikely(offset > MAX_REG_OFFSET))
> +		return 0;
> +	return *(unsigned long *)((unsigned long)regs + offset);
> +}
> +
> +/**
> + * regs_within_kernel_stack() - check the address in the stack
> + * @regs:	pt_regs which contains kernel stack pointer.
> + * @addr:	address which is checked.
> + *
> + * regs_within_kenel_stack() checks @addr is within the kernel stack page(s).
> + * If @addr is within the kernel stack, it returns true. If not, returns false.
> + */
> +static inline int regs_within_kernel_stack(struct pt_regs *regs,
> +					   unsigned long addr)
> +{
> +	return ((addr & ~(THREAD_SIZE - 1))  ==
> +		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
> +}
> +
> +/**
> + * regs_get_kernel_stack_nth() - get Nth entry of the stack
> + * @regs:	pt_regs which contains kernel stack pointer.
> + * @n:		stack entry number.
> + *
> + * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
> + * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
> + * this returns 0.
> + */
> +static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
> +						      unsigned n)
> +{
> +	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
> +	addr += n;
> +	if (regs_within_kernel_stack(regs, (unsigned long)addr))
> +		return *addr;
> +	else
> +		return 0;
> +}
> +
> +/**
> + * regs_get_argument_nth() - get Nth argument at function call
> + * @regs:	pt_regs which contains registers at function entry.
> + * @n:		argument number.
> + *
> + * regs_get_argument_nth() returns @n th argument of a function call.
> + * Since usually the kernel stack will be changed right after function entry,
> + * you must use this at function entry. If the @n th entry is NOT in the
> + * kernel stack or pt_regs, this returns 0.
> + */
> +#ifdef CONFIG_X86_32
> +#define NR_REGPARMS 3
> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
> +						  unsigned n)
> +{
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->ax;
> +		case 1:
> +			return regs->dx;
> +		case 2:
> +			return regs->cx;
> +		}
> +		return 0;
> +	} else {
> +		/*
> +		 * The typical case: arg n is on the stack.
> +		 * (Note: stack[0] = return address, so skip it)
> +		 */
> +		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
> +	}
> +}
> +#else /* CONFIG_X86_64 */
> +#define NR_REGPARMS 6
> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
> +						  unsigned n)
> +{
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->di;
> +		case 1:
> +			return regs->si;
> +		case 2:
> +			return regs->dx;
> +		case 3:
> +			return regs->cx;
> +		case 4:
> +			return regs->r8;
> +		case 5:
> +			return regs->r9;
> +		}
> +		return 0;
> +	} else {
> +		/*
> +		 * The typical case: arg n is on the stack.
> +		 * (Note: stack[0] = return address, so skip it)
> +		 */
> +		return regs_get_kernel_stack_nth(regs, 1 + n - NR_REGPARMS);
> +	}
> +}
> +#endif
> +
>  /*
>   * These are defined as per linux/ptrace.h, which see.
>   */
> diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
> index b457f78..2944d3a 100644
> --- a/arch/x86/kernel/ptrace.c
> +++ b/arch/x86/kernel/ptrace.c
> @@ -49,6 +49,79 @@ enum x86_regset {
>  	REGSET_IOPERM32,
>  };
>  
> +struct pt_regs_offset {
> +	const char *name;
> +	int offset;
> +};
> +
> +#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
> +#define REG_OFFSET_END {.name = NULL, .offset = 0}
> +
> +static const struct pt_regs_offset regoffset_table[] = {
> +#ifdef CONFIG_X86_64
> +	REG_OFFSET_NAME(r15),
> +	REG_OFFSET_NAME(r14),
> +	REG_OFFSET_NAME(r13),
> +	REG_OFFSET_NAME(r12),
> +	REG_OFFSET_NAME(r11),
> +	REG_OFFSET_NAME(r10),
> +	REG_OFFSET_NAME(r9),
> +	REG_OFFSET_NAME(r8),
> +#endif
> +	REG_OFFSET_NAME(bx),
> +	REG_OFFSET_NAME(cx),
> +	REG_OFFSET_NAME(dx),
> +	REG_OFFSET_NAME(si),
> +	REG_OFFSET_NAME(di),
> +	REG_OFFSET_NAME(bp),
> +	REG_OFFSET_NAME(ax),
> +#ifdef CONFIG_X86_32
> +	REG_OFFSET_NAME(ds),
> +	REG_OFFSET_NAME(es),
> +	REG_OFFSET_NAME(fs),
> +	REG_OFFSET_NAME(gs),
> +#endif
> +	REG_OFFSET_NAME(orig_ax),
> +	REG_OFFSET_NAME(ip),
> +	REG_OFFSET_NAME(cs),
> +	REG_OFFSET_NAME(flags),
> +	REG_OFFSET_NAME(sp),
> +	REG_OFFSET_NAME(ss),
> +	REG_OFFSET_END,
> +};
> +
> +/**
> + * regs_query_register_offset() - query register offset from its name
> + * @name:	the name of a register
> + *
> + * regs_query_register_offset() returns the offset of a register in struct
> + * pt_regs from its name. If the name is invalid, this returns -EINVAL;
> + */
> +int regs_query_register_offset(const char *name)
> +{
> +	const struct pt_regs_offset *roff;
> +	for (roff = regoffset_table; roff->name != NULL; roff++)
> +		if (!strcmp(roff->name, name))
> +			return roff->offset;
> +	return -EINVAL;
> +}
> +
> +/**
> + * regs_query_register_name() - query register name from its offset
> + * @offset:	the offset of a register in struct pt_regs.
> + *
> + * regs_query_register_name() returns the name of a register from its
> + * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
> + */
> +const char *regs_query_register_name(unsigned offset)
> +{
> +	const struct pt_regs_offset *roff;
> +	for (roff = regoffset_table; roff->name != NULL; roff++)
> +		if (roff->offset == offset)
> +			return roff->name;
> +	return NULL;
> +}
> +
>  /*
>   * does not yet catch signals sent when the child dies.
>   * in exit.c or in signal.c.
> 
> 
> -- 
> Masami Hiramatsu
> 
> Software Engineer
> Hitachi Computer Products (America), Inc.
> Software Solutions Division
> 
> e-mail: mhiramat@redhat.com


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-01  1:09 ` [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs Masami Hiramatsu
  2009-07-01  1:09   ` Masami Hiramatsu
  2009-07-06  1:42   ` Frederic Weisbecker
@ 2009-07-06 14:34   ` Andi Kleen
  2009-07-06 14:34     ` Andi Kleen
  2009-07-06 19:28     ` Masami Hiramatsu
  2 siblings, 2 replies; 11+ messages in thread
From: Andi Kleen @ 2009-07-06 14:34 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm, DLE,
	Christoph Hellwig, Ananth N Mavinakayanahalli,
	Frederic Weisbecker, Roland McGrath, Srikar Dronamraju,
	linux-arch

Masami Hiramatsu <mhiramat@redhat.com> writes:

> Add following APIs for accessing registers and stack entries from pt_regs.

You forgot to state who calls these functions/why are they added?
Who only has strings for registers?

I can see the point of having a function for nth argument though,
that's useful.

> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
> +						  unsigned n)
> +{
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->ax;
> +		case 1:
> +			return regs->dx;
> +		case 2:
> +			return regs->cx;


[....]

That could be done shorter with a offsetof table.

> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->di;
> +		case 1:
> +			return regs->si;
> +		case 2:
> +			return regs->dx;
> +		case 3:
> +			return regs->cx;
> +		case 4:
> +			return regs->r8;
> +		case 5:
> +			return regs->r9;

and that too.


-Andi
-- 
ak@linux.intel.com -- Speaking for myself only.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-06 14:34   ` Andi Kleen
@ 2009-07-06 14:34     ` Andi Kleen
  2009-07-06 19:28     ` Masami Hiramatsu
  1 sibling, 0 replies; 11+ messages in thread
From: Andi Kleen @ 2009-07-06 14:34 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm, DLE,
	Christoph Hellwig, Ananth N Mavinakayanahalli,
	Frederic Weisbecker, Roland McGrath, Srikar Dronamraju,
	linux-arch

Masami Hiramatsu <mhiramat@redhat.com> writes:

> Add following APIs for accessing registers and stack entries from pt_regs.

You forgot to state who calls these functions/why are they added?
Who only has strings for registers?

I can see the point of having a function for nth argument though,
that's useful.

> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
> +						  unsigned n)
> +{
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->ax;
> +		case 1:
> +			return regs->dx;
> +		case 2:
> +			return regs->cx;


[....]

That could be done shorter with a offsetof table.

> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0:
> +			return regs->di;
> +		case 1:
> +			return regs->si;
> +		case 2:
> +			return regs->dx;
> +		case 3:
> +			return regs->cx;
> +		case 4:
> +			return regs->r8;
> +		case 5:
> +			return regs->r9;

and that too.


-Andi
-- 
ak@linux.intel.com -- Speaking for myself only.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access  APIs
  2009-07-06 14:34   ` Andi Kleen
  2009-07-06 14:34     ` Andi Kleen
@ 2009-07-06 19:28     ` Masami Hiramatsu
  2009-07-06 19:28       ` Masami Hiramatsu
  2009-07-06 20:06       ` Andi Kleen
  1 sibling, 2 replies; 11+ messages in thread
From: Masami Hiramatsu @ 2009-07-06 19:28 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm, DLE,
	Christoph Hellwig, Ananth N Mavinakayanahalli,
	Frederic Weisbecker, Roland McGrath, Srikar Dronamraju,
	linux-arch

Andi Kleen wrote:
> Masami Hiramatsu <mhiramat@redhat.com> writes:
> 
>> Add following APIs for accessing registers and stack entries from pt_regs.
> 
> You forgot to state who calls these functions/why are they added?
> Who only has strings for registers?

Oh, yes. This patch is needed for kprobes based event tracer on ftrace.
Some other debugging tools might be able to use it.

> I can see the point of having a function for nth argument though,
> that's useful.
> 
>> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
>> +						  unsigned n)
>> +{
>> +	if (n < NR_REGPARMS) {
>> +		switch (n) {
>> +		case 0:
>> +			return regs->ax;
>> +		case 1:
>> +			return regs->dx;
>> +		case 2:
>> +			return regs->cx;
> 
> 
> [....]
> 
> That could be done shorter with a offsetof table.
> 
>> +	if (n < NR_REGPARMS) {
>> +		switch (n) {
>> +		case 0:
>> +			return regs->di;
>> +		case 1:
>> +			return regs->si;
>> +		case 2:
>> +			return regs->dx;
>> +		case 3:
>> +			return regs->cx;
>> +		case 4:
>> +			return regs->r8;
>> +		case 5:
>> +			return regs->r9;
> 
> and that too.

I'm not so sure about your idea.
Would you mean below code?

int offs_table[NR_REGPARMS] = {
	[0] = offsetof(struct pt_regs, di),
	...
};
if (n < NR_REGPARMS)
	return *((unsigned long *)regs + offs_table[n]);


Thank you,

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-06 19:28     ` Masami Hiramatsu
@ 2009-07-06 19:28       ` Masami Hiramatsu
  2009-07-06 20:06       ` Andi Kleen
  1 sibling, 0 replies; 11+ messages in thread
From: Masami Hiramatsu @ 2009-07-06 19:28 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm, DLE,
	Christoph Hellwig, Ananth N Mavinakayanahalli,
	Frederic Weisbecker, Roland McGrath, Srikar Dronamraju,
	linux-arch

Andi Kleen wrote:
> Masami Hiramatsu <mhiramat@redhat.com> writes:
> 
>> Add following APIs for accessing registers and stack entries from pt_regs.
> 
> You forgot to state who calls these functions/why are they added?
> Who only has strings for registers?

Oh, yes. This patch is needed for kprobes based event tracer on ftrace.
Some other debugging tools might be able to use it.

> I can see the point of having a function for nth argument though,
> that's useful.
> 
>> +static inline unsigned long regs_get_argument_nth(struct pt_regs *regs,
>> +						  unsigned n)
>> +{
>> +	if (n < NR_REGPARMS) {
>> +		switch (n) {
>> +		case 0:
>> +			return regs->ax;
>> +		case 1:
>> +			return regs->dx;
>> +		case 2:
>> +			return regs->cx;
> 
> 
> [....]
> 
> That could be done shorter with a offsetof table.
> 
>> +	if (n < NR_REGPARMS) {
>> +		switch (n) {
>> +		case 0:
>> +			return regs->di;
>> +		case 1:
>> +			return regs->si;
>> +		case 2:
>> +			return regs->dx;
>> +		case 3:
>> +			return regs->cx;
>> +		case 4:
>> +			return regs->r8;
>> +		case 5:
>> +			return regs->r9;
> 
> and that too.

I'm not so sure about your idea.
Would you mean below code?

int offs_table[NR_REGPARMS] = {
	[0] = offsetof(struct pt_regs, di),
	...
};
if (n < NR_REGPARMS)
	return *((unsigned long *)regs + offs_table[n]);


Thank you,

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-06 19:28     ` Masami Hiramatsu
  2009-07-06 19:28       ` Masami Hiramatsu
@ 2009-07-06 20:06       ` Andi Kleen
  2009-07-07  0:07         ` Masami Hiramatsu
  1 sibling, 1 reply; 11+ messages in thread
From: Andi Kleen @ 2009-07-06 20:06 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Andi Kleen, Ingo Molnar, Steven Rostedt, lkml, systemtap, kvm,
	DLE, Christoph Hellwig, Ananth N Mavinakayanahalli,
	Frederic Weisbecker, Roland McGrath, Srikar Dronamraju,
	linux-arch

On Mon, Jul 06, 2009 at 03:28:02PM -0400, Masami Hiramatsu wrote:
> I'm not so sure about your idea.
> Would you mean below code?
> 
> int offs_table[NR_REGPARMS] = {

not REGPARMS of course

> 	[0] = offsetof(struct pt_regs, di),
> 	...
> };
> if (n < NR_REGPARMS)
> 	return *((unsigned long *)regs + offs_table[n]);

Yes.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access  APIs
  2009-07-06 20:06       ` Andi Kleen
@ 2009-07-07  0:07         ` Masami Hiramatsu
  2009-07-07  0:07           ` Masami Hiramatsu
  0 siblings, 1 reply; 11+ messages in thread
From: Masami Hiramatsu @ 2009-07-07  0:07 UTC (permalink / raw)
  To: Andi Kleen, Ingo Molnar
  Cc: Steven Rostedt, lkml, systemtap, kvm, DLE, Christoph Hellwig,
	Ananth N Mavinakayanahalli, Frederic Weisbecker, Roland McGrath,
	Srikar Dronamraju, linux-arch

Andi Kleen wrote:
> On Mon, Jul 06, 2009 at 03:28:02PM -0400, Masami Hiramatsu wrote:
>> I'm not so sure about your idea.
>> Would you mean below code?
>>
>> int offs_table[NR_REGPARMS] = {
> 
> not REGPARMS of course
> 
>> 	[0] = offsetof(struct pt_regs, di),
>> 	...
>> };
>> if (n < NR_REGPARMS)
>> 	return *((unsigned long *)regs + offs_table[n]);
> 
> Yes.

OK, here, I updated my patch.

Thank you,


x86: add pt_regs register and stack access APIs

From: Masami Hiramatsu <mhiramat@redhat.com>

Add following APIs for accessing registers and stack entries from pt_regs.
These APIs are required by kprobes-based event tracer on ftrace.
Some other debugging tools might be able to use it too.

- regs_query_register_offset(const char *name)
   Query the offset of "name" register.

- regs_query_register_name(unsigned offset)
   Query the name of register by its offset.

- regs_get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
   Check the address is in the kernel stack.

- regs_get_kernel_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the kernel stack. (N >= 0)

- regs_get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

Changes from v10:
 - Use an offsetof table in regs_get_argument_nth().

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Cc: linux-arch@vger.kernel.org
---

 arch/x86/include/asm/ptrace.h |   61 ++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |  112 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+), 0 deletions(-)


diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index 0f0d908..a9b7e2d 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@

 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif

 #ifndef __ASSEMBLY__
@@ -216,6 +217,66 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }

+/* Query offset/name of register from its name/offset */
+extern int regs_query_register_offset(const char *name);
+extern const char *regs_query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
+
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:	pt_regs from which register value is gotten.
+ * @offset:	offset number of the register.
+ *
+ * regs_get_register returns the value of a register whose offset from @regs
+ * is @offset. The @offset is the offset of the register in struct pt_regs.
+ * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+					      unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/**
+ * regs_within_kernel_stack() - check the address in the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @addr:	address which is checked.
+ *
+ * regs_within_kenel_stack() checks @addr is within the kernel stack page(s).
+ * If @addr is within the kernel stack, it returns true. If not, returns false.
+ */
+static inline int regs_within_kernel_stack(struct pt_regs *regs,
+					   unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/**
+ * regs_get_kernel_stack_nth() - get Nth entry of the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @n:		stack entry number.
+ *
+ * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
+ * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
+ * this returns 0.
+ */
+static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
+						      unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
+	addr += n;
+	if (regs_within_kernel_stack(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/* Get Nth argument at function call */
+extern unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned n);
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index cabdabc..4f9b513 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -49,6 +49,118 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };

+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static const struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_NAME(ss),
+	REG_OFFSET_END,
+};
+
+/**
+ * regs_query_register_offset() - query register offset from its name
+ * @name:	the name of a register
+ *
+ * regs_query_register_offset() returns the offset of a register in struct
+ * pt_regs from its name. If the name is invalid, this returns -EINVAL;
+ */
+int regs_query_register_offset(const char *name)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return roff->offset;
+	return -EINVAL;
+}
+
+/**
+ * regs_query_register_name() - query register name from its offset
+ * @offset:	the offset of a register in struct pt_regs.
+ *
+ * regs_query_register_name() returns the name of a register from its
+ * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
+ */
+const char *regs_query_register_name(unsigned offset)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
+static const int arg_offs_table[] = {
+#ifdef CONFIG_X86_32
+	[0] = offsetof(struct pt_regs, ax),
+	[1] = offsetof(struct pt_regs, dx),
+	[2] = offsetof(struct pt_regs, cx)
+#else /* CONFIG_X86_64 */
+	[0] = offsetof(struct pt_regs, di),
+	[1] = offsetof(struct pt_regs, si),
+	[2] = offsetof(struct pt_regs, dx),
+	[3] = offsetof(struct pt_regs, cx),
+	[4] = offsetof(struct pt_regs, r8),
+	[5] = offsetof(struct pt_regs, r9)
+#endif
+};
+
+/**
+ * regs_get_argument_nth() - get Nth argument at function call
+ * @regs:	pt_regs which contains registers at function entry.
+ * @n:		argument number.
+ *
+ * regs_get_argument_nth() returns @n th argument of a function call.
+ * Since usually the kernel stack will be changed right after function entry,
+ * you must use this at function entry. If the @n th entry is NOT in the
+ * kernel stack or pt_regs, this returns 0.
+ */
+unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned n)
+{
+	if (n < ARRAY_SIZE(arg_offs_table))
+		return *((unsigned long *)regs + arg_offs_table[n]);
+	else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		n -= ARRAY_SIZE(arg_offs_table);
+		return regs_get_kernel_stack_nth(regs, 1 + n);
+	}
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.


-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs
  2009-07-07  0:07         ` Masami Hiramatsu
@ 2009-07-07  0:07           ` Masami Hiramatsu
  0 siblings, 0 replies; 11+ messages in thread
From: Masami Hiramatsu @ 2009-07-07  0:07 UTC (permalink / raw)
  To: Andi Kleen, Ingo Molnar
  Cc: Steven Rostedt, lkml, systemtap, kvm, DLE, Christoph Hellwig,
	Ananth N Mavinakayanahalli, Frederic Weisbecker, Roland McGrath,
	Srikar Dronamraju, linux-arch

Andi Kleen wrote:
> On Mon, Jul 06, 2009 at 03:28:02PM -0400, Masami Hiramatsu wrote:
>> I'm not so sure about your idea.
>> Would you mean below code?
>>
>> int offs_table[NR_REGPARMS] = {
> 
> not REGPARMS of course
> 
>> 	[0] = offsetof(struct pt_regs, di),
>> 	...
>> };
>> if (n < NR_REGPARMS)
>> 	return *((unsigned long *)regs + offs_table[n]);
> 
> Yes.

OK, here, I updated my patch.

Thank you,


x86: add pt_regs register and stack access APIs

From: Masami Hiramatsu <mhiramat@redhat.com>

Add following APIs for accessing registers and stack entries from pt_regs.
These APIs are required by kprobes-based event tracer on ftrace.
Some other debugging tools might be able to use it too.

- regs_query_register_offset(const char *name)
   Query the offset of "name" register.

- regs_query_register_name(unsigned offset)
   Query the name of register by its offset.

- regs_get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
   Check the address is in the kernel stack.

- regs_get_kernel_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the kernel stack. (N >= 0)

- regs_get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

Changes from v10:
 - Use an offsetof table in regs_get_argument_nth().

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Cc: linux-arch@vger.kernel.org
---

 arch/x86/include/asm/ptrace.h |   61 ++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |  112 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+), 0 deletions(-)


diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index 0f0d908..a9b7e2d 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@

 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif

 #ifndef __ASSEMBLY__
@@ -216,6 +217,66 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }

+/* Query offset/name of register from its name/offset */
+extern int regs_query_register_offset(const char *name);
+extern const char *regs_query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
+
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:	pt_regs from which register value is gotten.
+ * @offset:	offset number of the register.
+ *
+ * regs_get_register returns the value of a register whose offset from @regs
+ * is @offset. The @offset is the offset of the register in struct pt_regs.
+ * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+					      unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/**
+ * regs_within_kernel_stack() - check the address in the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @addr:	address which is checked.
+ *
+ * regs_within_kenel_stack() checks @addr is within the kernel stack page(s).
+ * If @addr is within the kernel stack, it returns true. If not, returns false.
+ */
+static inline int regs_within_kernel_stack(struct pt_regs *regs,
+					   unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/**
+ * regs_get_kernel_stack_nth() - get Nth entry of the stack
+ * @regs:	pt_regs which contains kernel stack pointer.
+ * @n:		stack entry number.
+ *
+ * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
+ * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
+ * this returns 0.
+ */
+static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
+						      unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
+	addr += n;
+	if (regs_within_kernel_stack(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/* Get Nth argument at function call */
+extern unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned n);
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index cabdabc..4f9b513 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -49,6 +49,118 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };

+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static const struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_NAME(ss),
+	REG_OFFSET_END,
+};
+
+/**
+ * regs_query_register_offset() - query register offset from its name
+ * @name:	the name of a register
+ *
+ * regs_query_register_offset() returns the offset of a register in struct
+ * pt_regs from its name. If the name is invalid, this returns -EINVAL;
+ */
+int regs_query_register_offset(const char *name)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return roff->offset;
+	return -EINVAL;
+}
+
+/**
+ * regs_query_register_name() - query register name from its offset
+ * @offset:	the offset of a register in struct pt_regs.
+ *
+ * regs_query_register_name() returns the name of a register from its
+ * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
+ */
+const char *regs_query_register_name(unsigned offset)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
+static const int arg_offs_table[] = {
+#ifdef CONFIG_X86_32
+	[0] = offsetof(struct pt_regs, ax),
+	[1] = offsetof(struct pt_regs, dx),
+	[2] = offsetof(struct pt_regs, cx)
+#else /* CONFIG_X86_64 */
+	[0] = offsetof(struct pt_regs, di),
+	[1] = offsetof(struct pt_regs, si),
+	[2] = offsetof(struct pt_regs, dx),
+	[3] = offsetof(struct pt_regs, cx),
+	[4] = offsetof(struct pt_regs, r8),
+	[5] = offsetof(struct pt_regs, r9)
+#endif
+};
+
+/**
+ * regs_get_argument_nth() - get Nth argument at function call
+ * @regs:	pt_regs which contains registers at function entry.
+ * @n:		argument number.
+ *
+ * regs_get_argument_nth() returns @n th argument of a function call.
+ * Since usually the kernel stack will be changed right after function entry,
+ * you must use this at function entry. If the @n th entry is NOT in the
+ * kernel stack or pt_regs, this returns 0.
+ */
+unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned n)
+{
+	if (n < ARRAY_SIZE(arg_offs_table))
+		return *((unsigned long *)regs + arg_offs_table[n]);
+	else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		n -= ARRAY_SIZE(arg_offs_table);
+		return regs_get_kernel_stack_nth(regs, 1 + n);
+	}
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.


-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-07-07  0:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20090701010838.32547.62843.stgit@localhost.localdomain>
2009-07-01  1:09 ` [PATCH -tip -v10 5/7] x86: add pt_regs register and stack access APIs Masami Hiramatsu
2009-07-01  1:09   ` Masami Hiramatsu
2009-07-06  1:42   ` Frederic Weisbecker
2009-07-06  1:42     ` Frederic Weisbecker
2009-07-06 14:34   ` Andi Kleen
2009-07-06 14:34     ` Andi Kleen
2009-07-06 19:28     ` Masami Hiramatsu
2009-07-06 19:28       ` Masami Hiramatsu
2009-07-06 20:06       ` Andi Kleen
2009-07-07  0:07         ` Masami Hiramatsu
2009-07-07  0:07           ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox