qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] win64: perform correct setjmp calls
@ 2015-02-09  7:55 Pavel Dovgalyuk
  2015-02-09  8:04 ` Stefan Weil
  0 siblings, 1 reply; 5+ messages in thread
From: Pavel Dovgalyuk @ 2015-02-09  7:55 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, zealot351, maria.klimushenkova, pavel.dovgaluk, sw

On w64, setjmp is implemented by _setjmp which needs a second parameter.
This parameter should be NULL to allow using longjump from generated code.
This patch replaces all usages of setjmp.h with new header files which
replaces setjmp with _setjmp function on win64 platform.

Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
---
 coroutine-sigaltstack.c   |    2 +-
 coroutine-ucontext.c      |    2 +-
 disas/i386.c              |    2 +-
 disas/m68k.c              |    2 +-
 include/qom/cpu.h         |    2 +-
 include/sysemu/os-win32.h |   17 -----------------
 include/sysemu/setjmp.h   |   35 +++++++++++++++++++++++++++++++++++
 util/oslib-posix.c        |    2 +-
 8 files changed, 41 insertions(+), 23 deletions(-)
 create mode 100755 include/sysemu/setjmp.h

diff --git a/coroutine-sigaltstack.c b/coroutine-sigaltstack.c
index 63519ff..c890496 100644
--- a/coroutine-sigaltstack.c
+++ b/coroutine-sigaltstack.c
@@ -26,7 +26,7 @@
 #undef _FORTIFY_SOURCE
 #endif
 #include <stdlib.h>
-#include <setjmp.h>
+#include "sysemu/setjmp.h"
 #include <stdint.h>
 #include <pthread.h>
 #include <signal.h>
diff --git a/coroutine-ucontext.c b/coroutine-ucontext.c
index 259fcb4..12e2826 100644
--- a/coroutine-ucontext.c
+++ b/coroutine-ucontext.c
@@ -23,7 +23,7 @@
 #undef _FORTIFY_SOURCE
 #endif
 #include <stdlib.h>
-#include <setjmp.h>
+#include "sysemu/setjmp.h"
 #include <stdint.h>
 #include <ucontext.h>
 #include "qemu-common.h"
diff --git a/disas/i386.c b/disas/i386.c
index 00ceca9..fdb1ec6 100644
--- a/disas/i386.c
+++ b/disas/i386.c
@@ -153,7 +153,7 @@
 /* opcodes/i386-dis.c r1.126 */
 #include "qemu-common.h"
 
-#include <setjmp.h>
+#include "sysemu/setjmp.h"
 
 static int fetch_data2(struct disassemble_info *, bfd_byte *);
 static int fetch_data(struct disassemble_info *, bfd_byte *);
diff --git a/disas/m68k.c b/disas/m68k.c
index cc0db96..312351e 100644
--- a/disas/m68k.c
+++ b/disas/m68k.c
@@ -616,7 +616,7 @@ static const char *const reg_half_names[] =
 /* Maximum length of an instruction.  */
 #define MAXLEN 22
 
-#include <setjmp.h>
+#include "sysemu/setjmp.h"
 
 struct private
 {
diff --git a/include/qom/cpu.h b/include/qom/cpu.h
index 2098f1c..16dc2f7 100644
--- a/include/qom/cpu.h
+++ b/include/qom/cpu.h
@@ -21,7 +21,7 @@
 #define QEMU_CPU_H
 
 #include <signal.h>
-#include <setjmp.h>
+#include "sysemu/setjmp.h"
 #include "hw/qdev-core.h"
 #include "exec/hwaddr.h"
 #include "qemu/queue.h"
diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index af3fbc4..0387313 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -55,23 +55,6 @@
 # define EWOULDBLOCK  WSAEWOULDBLOCK
 #endif
 
-#if defined(_WIN64)
-/* On w64, setjmp is implemented by _setjmp which needs a second parameter.
- * If this parameter is NULL, longjump does no stack unwinding.
- * That is what we need for QEMU. Passing the value of register rsp (default)
- * lets longjmp try a stack unwinding which will crash with generated code. */
-# undef setjmp
-# define setjmp(env) _setjmp(env, NULL)
-#endif
-/* QEMU uses sigsetjmp()/siglongjmp() as the portable way to specify
- * "longjmp and don't touch the signal masks". Since we know that the
- * savemask parameter will always be zero we can safely define these
- * in terms of setjmp/longjmp on Win32.
- */
-#define sigjmp_buf jmp_buf
-#define sigsetjmp(env, savemask) setjmp(env)
-#define siglongjmp(env, val) longjmp(env, val)
-
 /* Declaration of ffs() is missing in MinGW's strings.h. */
 int ffs(int i);
 
diff --git a/include/sysemu/setjmp.h b/include/sysemu/setjmp.h
new file mode 100755
index 0000000..ffd804a
--- /dev/null
+++ b/include/sysemu/setjmp.h
@@ -0,0 +1,35 @@
+/*
+ * setjmp.h
+ *
+ * Copyright (c) 2015 Institute for System Programming
+ *                    of the Russian Academy of Sciences.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#ifndef QEMU_SETJMP_H
+#define QEMU_SETJMP_H
+
+#include <setjmp.h>
+
+#if defined(_WIN32)
+#if defined(_WIN64)
+/* On w64, setjmp is implemented by _setjmp which needs a second parameter.
+ * If this parameter is NULL, longjump does no stack unwinding.
+ * That is what we need for QEMU. Passing the value of register rsp (default)
+ * lets longjmp try a stack unwinding which will crash with generated code. */
+# undef setjmp
+# define setjmp(env) _setjmp(env, NULL)
+#endif
+/* QEMU uses sigsetjmp()/siglongjmp() as the portable way to specify
+ * "longjmp and don't touch the signal masks". Since we know that the
+ * savemask parameter will always be zero we can safely define these
+ * in terms of setjmp/longjmp on Win32.
+ */
+#define sigjmp_buf jmp_buf
+#define sigsetjmp(env, savemask) setjmp(env)
+#define siglongjmp(env, val) longjmp(env, val)
+#endif
+
+#endif
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 16fcec2..23b7af8 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -59,7 +59,7 @@ extern int daemon(int, int);
 #include "qemu/sockets.h"
 #include <sys/mman.h>
 #include <libgen.h>
-#include <setjmp.h>
+#include "sysemu/setjmp.h"
 #include <sys/signal.h>
 
 #ifdef CONFIG_LINUX

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

* Re: [Qemu-devel] [PATCH] win64: perform correct setjmp calls
  2015-02-09  7:55 [Qemu-devel] [PATCH] win64: perform correct setjmp calls Pavel Dovgalyuk
@ 2015-02-09  8:04 ` Stefan Weil
  2015-02-09  8:07   ` Pavel Dovgaluk
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Weil @ 2015-02-09  8:04 UTC (permalink / raw)
  To: Pavel Dovgalyuk, qemu-devel; +Cc: pbonzini, zealot351, maria.klimushenkova

Am 09.02.2015 um 08:55 schrieb Pavel Dovgalyuk:
> On w64, setjmp is implemented by _setjmp which needs a second parameter.
> This parameter should be NULL to allow using longjump from generated code.
> This patch replaces all usages of setjmp.h with new header files which
> replaces setjmp with _setjmp function on win64 platform.
>
> Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
> ---
>

Please have a look at include/sysemu/os-win32.h. I think that your patch 
is not needed because the current code already uses _setjmp.

Regards
Stefan

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

* Re: [Qemu-devel] [PATCH] win64: perform correct setjmp calls
  2015-02-09  8:04 ` Stefan Weil
@ 2015-02-09  8:07   ` Pavel Dovgaluk
  2015-02-09 19:47     ` Stefan Weil
  0 siblings, 1 reply; 5+ messages in thread
From: Pavel Dovgaluk @ 2015-02-09  8:07 UTC (permalink / raw)
  To: 'Stefan Weil', qemu-devel
  Cc: pbonzini, zealot351, maria.klimushenkova

> From: Stefan Weil [mailto:sw@weilnetz.de]
> Am 09.02.2015 um 08:55 schrieb Pavel Dovgalyuk:
> > On w64, setjmp is implemented by _setjmp which needs a second parameter.
> > This parameter should be NULL to allow using longjump from generated code.
> > This patch replaces all usages of setjmp.h with new header files which
> > replaces setjmp with _setjmp function on win64 platform.
> >
> > Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
> 
> Please have a look at include/sysemu/os-win32.h. I think that your patch
> is not needed because the current code already uses _setjmp.

Right, but some of the files (e.g. include/qom/cpu.h) include setjmp.h directly.
Then we have the following for compiling cpu-exec.c:

cpu-exec.c:
...
os-win32.h
...
setjmp.h
...

In this situation cpu-exec will call incorrect setjmp function.

Pavel Dovgalyuk

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

* Re: [Qemu-devel] [PATCH] win64: perform correct setjmp calls
  2015-02-09  8:07   ` Pavel Dovgaluk
@ 2015-02-09 19:47     ` Stefan Weil
  2015-02-10  5:50       ` Pavel Dovgaluk
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Weil @ 2015-02-09 19:47 UTC (permalink / raw)
  To: Pavel Dovgaluk, qemu-devel; +Cc: pbonzini, zealot351, maria.klimushenkova

Am 09.02.2015 um 09:07 schrieb Pavel Dovgaluk:
>> From: Stefan Weil [mailto:sw@weilnetz.de]
>> Am 09.02.2015 um 08:55 schrieb Pavel Dovgalyuk:
>>> On w64, setjmp is implemented by _setjmp which needs a second parameter.
>>> This parameter should be NULL to allow using longjump from generated code.
>>> This patch replaces all usages of setjmp.h with new header files which
>>> replaces setjmp with _setjmp function on win64 platform.
>>>
>>> Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
>> Please have a look at include/sysemu/os-win32.h. I think that your patch
>> is not needed because the current code already uses _setjmp.
> Right, but some of the files (e.g. include/qom/cpu.h) include setjmp.h directly.
> Then we have the following for compiling cpu-exec.c:
>
> cpu-exec.c:
> ...
> os-win32.h
> ...
> setjmp.h
> ...
>
> In this situation cpu-exec will call incorrect setjmp function.
>
> Pavel Dovgalyuk


It won't call the wrong setjmp function, at least not in my tests. 
cpu-exec.c gets the setjmp declaration from os-win32.h. Without it, QEMU 
would be unusable because it would crash very soon during the emulation.

Do you see problems caused by a wrong setjmp with latest QEMU? If yes: 
which build environment do you use (host, compiler, version of MinGW*)?

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

* Re: [Qemu-devel] [PATCH] win64: perform correct setjmp calls
  2015-02-09 19:47     ` Stefan Weil
@ 2015-02-10  5:50       ` Pavel Dovgaluk
  0 siblings, 0 replies; 5+ messages in thread
From: Pavel Dovgaluk @ 2015-02-10  5:50 UTC (permalink / raw)
  To: 'Stefan Weil', qemu-devel
  Cc: pbonzini, zealot351, maria.klimushenkova

> From: Stefan Weil [mailto:sw@weilnetz.de]
> Am 09.02.2015 um 09:07 schrieb Pavel Dovgaluk:
> >> From: Stefan Weil [mailto:sw@weilnetz.de]
> >> Am 09.02.2015 um 08:55 schrieb Pavel Dovgalyuk:
> >>> On w64, setjmp is implemented by _setjmp which needs a second parameter.
> >>> This parameter should be NULL to allow using longjump from generated code.
> >>> This patch replaces all usages of setjmp.h with new header files which
> >>> replaces setjmp with _setjmp function on win64 platform.
> >>>
> >>> Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
> >> Please have a look at include/sysemu/os-win32.h. I think that your patch
> >> is not needed because the current code already uses _setjmp.
> > Right, but some of the files (e.g. include/qom/cpu.h) include setjmp.h directly.
> > Then we have the following for compiling cpu-exec.c:
> >
> > cpu-exec.c:
> > ...
> > os-win32.h
> > ...
> > setjmp.h
> > ...
> >
> > In this situation cpu-exec will call incorrect setjmp function.
> >
> > Pavel Dovgalyuk
> 
> 
> It won't call the wrong setjmp function, at least not in my tests.
> cpu-exec.c gets the setjmp declaration from os-win32.h. Without it, QEMU
> would be unusable because it would crash very soon during the emulation.
> 
> Do you see problems caused by a wrong setjmp with latest QEMU? If yes:
> which build environment do you use (host, compiler, version of MinGW*)?

Yes, I've got the problems. To verify the correctness of the call I disassembled
cpu-exec.o and found that setjmp is called with second parameter not equal to zero.

I'm using Cygwin environment and packages from here: http://win-builds.org/
My host OS is Windows 7x64 and the compiler is x86_64-w64-mingw32-gcc.exe (GCC) 4.8.3

Pavel Dovgalyuk

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

end of thread, other threads:[~2015-02-10  5:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-09  7:55 [Qemu-devel] [PATCH] win64: perform correct setjmp calls Pavel Dovgalyuk
2015-02-09  8:04 ` Stefan Weil
2015-02-09  8:07   ` Pavel Dovgaluk
2015-02-09 19:47     ` Stefan Weil
2015-02-10  5:50       ` Pavel Dovgaluk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).