diff mbox

[v2,1/2] kvm-unit-tests : Add setjmp/longjmp to libcflat

Message ID 1373987466-2216-2-git-send-email-yzt356@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Arthur Chunqi Li July 16, 2013, 3:11 p.m. UTC
Add setjmp and longjmp functions to libcflat. Now these two functions
are only supported in X86_64 arch.

New files added:
	lib/x86/setjmp64.S
	lib/x86/setjmp64.c

Signed-off-by: Arthur Chunqi Li <yzt356@gmail.com>
---
 config-x86_64.mak  |    2 ++
 lib/x86/setjmp64.S |   27 +++++++++++++++++++++++++++
 lib/x86/setjmp64.h |   15 +++++++++++++++
 3 files changed, 44 insertions(+)
 create mode 100644 lib/x86/setjmp64.S
 create mode 100644 lib/x86/setjmp64.h

Comments

Paolo Bonzini July 16, 2013, 3:19 p.m. UTC | #1
Il 16/07/2013 17:11, Arthur Chunqi Li ha scritto:
> Add setjmp and longjmp functions to libcflat. Now these two functions
> are only supported in X86_64 arch.
> 
> New files added:
> 	lib/x86/setjmp64.S
> 	lib/x86/setjmp64.c
> 
> Signed-off-by: Arthur Chunqi Li <yzt356@gmail.com>
> ---
>  config-x86_64.mak  |    2 ++
>  lib/x86/setjmp64.S |   27 +++++++++++++++++++++++++++
>  lib/x86/setjmp64.h |   15 +++++++++++++++
>  3 files changed, 44 insertions(+)
>  create mode 100644 lib/x86/setjmp64.S
>  create mode 100644 lib/x86/setjmp64.h
> 
> diff --git a/config-x86_64.mak b/config-x86_64.mak
> index 4e525f5..91ffcce 100644
> --- a/config-x86_64.mak
> +++ b/config-x86_64.mak
> @@ -4,6 +4,8 @@ bits = 64
>  ldarch = elf64-x86-64
>  CFLAGS += -D__x86_64__
>  
> +cflatobjs += lib/x86/setjmp64.o
> +
>  tests = $(TEST_DIR)/access.flat $(TEST_DIR)/apic.flat \
>  	  $(TEST_DIR)/emulator.flat $(TEST_DIR)/idt_test.flat \
>  	  $(TEST_DIR)/xsave.flat $(TEST_DIR)/rmap_chain.flat \
> diff --git a/lib/x86/setjmp64.S b/lib/x86/setjmp64.S
> new file mode 100644
> index 0000000..c8ae790
> --- /dev/null
> +++ b/lib/x86/setjmp64.S
> @@ -0,0 +1,27 @@
> +.globl setjmp
> +setjmp:
> +	mov (%rsp), %rsi
> +	mov %rsi, (%rdi)
> +	mov %rsp, 0x8(%rdi)
> +	mov %rbp, 0x10(%rdi)
> +	mov %rbx, 0x18(%rdi)
> +	mov %r12, 0x20(%rdi)
> +	mov %r13, 0x28(%rdi)
> +	mov %r14, 0x30(%rdi)
> +	mov %r15, 0x38(%rdi)
> +	xor %eax, %eax
> +	ret
> +
> +.globl longjmp
> +longjmp:
> +	mov %esi, %eax
> +	mov 0x38(%rdi), %r15
> +	mov 0x30(%rdi), %r14
> +	mov 0x28(%rdi), %r13
> +	mov 0x20(%rdi), %r12
> +	mov 0x18(%rdi), %rbx
> +	mov 0x10(%rdi), %rbp
> +	mov 0x8(%rdi), %rsp
> +	mov (%rdi), %rsi
> +	mov %rsi, (%rsp)
> +	ret

Nice!

> diff --git a/lib/x86/setjmp64.h b/lib/x86/setjmp64.h
> new file mode 100644
> index 0000000..98ff16a
> --- /dev/null
> +++ b/lib/x86/setjmp64.h
> @@ -0,0 +1,15 @@
> +#ifndef LIBCFLAT_SETJMP64_H
> +#define LIBCFLAT_SETJMP64_H
> +
> +#include "libcflat.h"
> +
> +struct jmp_buf {
> +	u64 calling_rip;
> +	u64 rsp, rbp, rbx;
> +	u64 r12, r13, r14, r15;
> +};

Please make it a typedef instead.  In fact, jmp_buf is typically an
array so that longjmp(env, 1) works without the ampersand.  A
"struct-like" declaration can go in setjmp64.S as a comment.

With this change, you can put it in lib/setjmp.h so that implementation
for other arches can be added later.

Otherwise looks good, thanks!

Paolo

> +void longjmp(struct jmp_buf *env, int val);
> +int setjmp(struct jmp_buf *env);
> +
> +#endif
> 

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/config-x86_64.mak b/config-x86_64.mak
index 4e525f5..91ffcce 100644
--- a/config-x86_64.mak
+++ b/config-x86_64.mak
@@ -4,6 +4,8 @@  bits = 64
 ldarch = elf64-x86-64
 CFLAGS += -D__x86_64__
 
+cflatobjs += lib/x86/setjmp64.o
+
 tests = $(TEST_DIR)/access.flat $(TEST_DIR)/apic.flat \
 	  $(TEST_DIR)/emulator.flat $(TEST_DIR)/idt_test.flat \
 	  $(TEST_DIR)/xsave.flat $(TEST_DIR)/rmap_chain.flat \
diff --git a/lib/x86/setjmp64.S b/lib/x86/setjmp64.S
new file mode 100644
index 0000000..c8ae790
--- /dev/null
+++ b/lib/x86/setjmp64.S
@@ -0,0 +1,27 @@ 
+.globl setjmp
+setjmp:
+	mov (%rsp), %rsi
+	mov %rsi, (%rdi)
+	mov %rsp, 0x8(%rdi)
+	mov %rbp, 0x10(%rdi)
+	mov %rbx, 0x18(%rdi)
+	mov %r12, 0x20(%rdi)
+	mov %r13, 0x28(%rdi)
+	mov %r14, 0x30(%rdi)
+	mov %r15, 0x38(%rdi)
+	xor %eax, %eax
+	ret
+
+.globl longjmp
+longjmp:
+	mov %esi, %eax
+	mov 0x38(%rdi), %r15
+	mov 0x30(%rdi), %r14
+	mov 0x28(%rdi), %r13
+	mov 0x20(%rdi), %r12
+	mov 0x18(%rdi), %rbx
+	mov 0x10(%rdi), %rbp
+	mov 0x8(%rdi), %rsp
+	mov (%rdi), %rsi
+	mov %rsi, (%rsp)
+	ret
diff --git a/lib/x86/setjmp64.h b/lib/x86/setjmp64.h
new file mode 100644
index 0000000..98ff16a
--- /dev/null
+++ b/lib/x86/setjmp64.h
@@ -0,0 +1,15 @@ 
+#ifndef LIBCFLAT_SETJMP64_H
+#define LIBCFLAT_SETJMP64_H
+
+#include "libcflat.h"
+
+struct jmp_buf {
+	u64 calling_rip;
+	u64 rsp, rbp, rbx;
+	u64 r12, r13, r14, r15;
+};
+
+void longjmp(struct jmp_buf *env, int val);
+int setjmp(struct jmp_buf *env);
+
+#endif