diff mbox series

[kvm-unit-tests,v3,4/7] s390x: Provide preliminary backtrace support

Message ID 20210222085756.14396-5-frankja@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series s390x: Cleanup exception register save/restore and implement backtrace | expand

Commit Message

Janosch Frank Feb. 22, 2021, 8:57 a.m. UTC
After the stack changes we can finally use -mbackchain and have a
working backtrace.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 lib/s390x/stack.c | 20 ++++++++++++++------
 s390x/Makefile    |  1 +
 s390x/macros.S    |  5 +++++
 3 files changed, 20 insertions(+), 6 deletions(-)

Comments

Thomas Huth March 4, 2021, 12:23 p.m. UTC | #1
On 22/02/2021 09.57, Janosch Frank wrote:
> After the stack changes we can finally use -mbackchain and have a
> working backtrace.
> 
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> ---
>   lib/s390x/stack.c | 20 ++++++++++++++------
>   s390x/Makefile    |  1 +
>   s390x/macros.S    |  5 +++++
>   3 files changed, 20 insertions(+), 6 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>
Claudio Imbrenda March 4, 2021, 1:02 p.m. UTC | #2
On Mon, 22 Feb 2021 03:57:53 -0500
Janosch Frank <frankja@linux.ibm.com> wrote:

> After the stack changes we can finally use -mbackchain and have a
> working backtrace.
> 
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>

Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>

> ---
>  lib/s390x/stack.c | 20 ++++++++++++++------
>  s390x/Makefile    |  1 +
>  s390x/macros.S    |  5 +++++
>  3 files changed, 20 insertions(+), 6 deletions(-)
> 
> diff --git a/lib/s390x/stack.c b/lib/s390x/stack.c
> index 0fcd1afb..4cf80dae 100644
> --- a/lib/s390x/stack.c
> +++ b/lib/s390x/stack.c
> @@ -3,24 +3,32 @@
>   * s390x stack implementation
>   *
>   * Copyright (c) 2017 Red Hat Inc
> + * Copyright 2021 IBM Corp
>   *
>   * Authors:
>   *  Thomas Huth <thuth@redhat.com>
>   *  David Hildenbrand <david@redhat.com>
> + *  Janosch Frank <frankja@de.ibm.com>
>   */
>  #include <libcflat.h>
>  #include <stack.h>
> +#include <asm/arch_def.h>
>  
>  int backtrace_frame(const void *frame, const void **return_addrs,
> int max_depth) {
> -	printf("TODO: Implement backtrace_frame(%p, %p, %d)
> function!\n",
> -	       frame, return_addrs, max_depth);
> -	return 0;
> +	int depth = 0;
> +	struct stack_frame *stack = (struct stack_frame *)frame;
> +
> +	for (depth = 0; stack && depth < max_depth; depth++) {
> +		return_addrs[depth] = (void *)stack->grs[8];
> +		stack = stack->back_chain;
> +	}
> +
> +	return depth;
>  }
>  
>  int backtrace(const void **return_addrs, int max_depth)
>  {
> -	printf("TODO: Implement backtrace(%p, %d) function!\n",
> -	       return_addrs, max_depth);
> -	return 0;
> +	return backtrace_frame(__builtin_frame_address(0),
> +			       return_addrs, max_depth);
>  }
> diff --git a/s390x/Makefile b/s390x/Makefile
> index f3b0fccf..20bb5683 100644
> --- a/s390x/Makefile
> +++ b/s390x/Makefile
> @@ -39,6 +39,7 @@ CFLAGS += -ffreestanding
>  CFLAGS += -I $(SRCDIR)/lib -I $(SRCDIR)/lib/s390x -I lib
>  CFLAGS += -O2
>  CFLAGS += -march=zEC12
> +CFLAGS += -mbackchain
>  CFLAGS += -fno-delete-null-pointer-checks
>  LDFLAGS += -nostdlib -Wl,--build-id=none
>  
> diff --git a/s390x/macros.S b/s390x/macros.S
> index 11f4397a..d4f41ec4 100644
> --- a/s390x/macros.S
> +++ b/s390x/macros.S
> @@ -22,6 +22,11 @@
>  	lgr     %r2, %r15
>  	/* Allocate stack space for called C function, as specified
> in s390 ELF ABI */ slgfi   %r15, 160
> +	/*
> +	 * Save the address of the interrupt stack into the back
> chain
> +	 * of the called function.
> +	 */
> +	stg     %r2, STACK_FRAME_INT_BACKCHAIN(%r15)
>  	brasl	%r14, \c_func
>  	algfi   %r15, 160
>  	RESTORE_REGS_STACK
diff mbox series

Patch

diff --git a/lib/s390x/stack.c b/lib/s390x/stack.c
index 0fcd1afb..4cf80dae 100644
--- a/lib/s390x/stack.c
+++ b/lib/s390x/stack.c
@@ -3,24 +3,32 @@ 
  * s390x stack implementation
  *
  * Copyright (c) 2017 Red Hat Inc
+ * Copyright 2021 IBM Corp
  *
  * Authors:
  *  Thomas Huth <thuth@redhat.com>
  *  David Hildenbrand <david@redhat.com>
+ *  Janosch Frank <frankja@de.ibm.com>
  */
 #include <libcflat.h>
 #include <stack.h>
+#include <asm/arch_def.h>
 
 int backtrace_frame(const void *frame, const void **return_addrs, int max_depth)
 {
-	printf("TODO: Implement backtrace_frame(%p, %p, %d) function!\n",
-	       frame, return_addrs, max_depth);
-	return 0;
+	int depth = 0;
+	struct stack_frame *stack = (struct stack_frame *)frame;
+
+	for (depth = 0; stack && depth < max_depth; depth++) {
+		return_addrs[depth] = (void *)stack->grs[8];
+		stack = stack->back_chain;
+	}
+
+	return depth;
 }
 
 int backtrace(const void **return_addrs, int max_depth)
 {
-	printf("TODO: Implement backtrace(%p, %d) function!\n",
-	       return_addrs, max_depth);
-	return 0;
+	return backtrace_frame(__builtin_frame_address(0),
+			       return_addrs, max_depth);
 }
diff --git a/s390x/Makefile b/s390x/Makefile
index f3b0fccf..20bb5683 100644
--- a/s390x/Makefile
+++ b/s390x/Makefile
@@ -39,6 +39,7 @@  CFLAGS += -ffreestanding
 CFLAGS += -I $(SRCDIR)/lib -I $(SRCDIR)/lib/s390x -I lib
 CFLAGS += -O2
 CFLAGS += -march=zEC12
+CFLAGS += -mbackchain
 CFLAGS += -fno-delete-null-pointer-checks
 LDFLAGS += -nostdlib -Wl,--build-id=none
 
diff --git a/s390x/macros.S b/s390x/macros.S
index 11f4397a..d4f41ec4 100644
--- a/s390x/macros.S
+++ b/s390x/macros.S
@@ -22,6 +22,11 @@ 
 	lgr     %r2, %r15
 	/* Allocate stack space for called C function, as specified in s390 ELF ABI */
 	slgfi   %r15, 160
+	/*
+	 * Save the address of the interrupt stack into the back chain
+	 * of the called function.
+	 */
+	stg     %r2, STACK_FRAME_INT_BACKCHAIN(%r15)
 	brasl	%r14, \c_func
 	algfi   %r15, 160
 	RESTORE_REGS_STACK