Skip to contentSkip to conversation

TCG S/390 backend

PATCH In review Ulrich Hecht 3 participants 5 messages May 5, 2009 – May 12, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 TCG S/390 backend

Hi!

Here's the S/390 host support again. Run Windows CE on your mainframe, be the envy of other major financial institutions and government agencies. Changes since last time:

- implemented 64-bit operations
- don't abuse R15 (stack pointer)
- proper zero-extension before calling load/store helpers
- implemented i32 divu/remu and RRR multiplication ops

Now i386, AMD64, ARM, SH4, and m68k system emulators work. MIPS works at least as well as on an AMD64 host, i.e. the test image from qemu.org just sits there without doing anything. The SPARC test image fails with a memory access exception in the target system, but works on an AMD64 host, so there probably still is a bug somewhere in the backend.

---
 Makefile.target       |    9 +
 configure             |   10 +-
 dyngen-exec.h         |    2 +-
 linux-user/syscall.c  |    2 +-
 s390-dis.c            |    4 +-
 s390x.ld              |  194 +++++++++
 tcg/s390/tcg-target.c | 1117 +++++++++++++++++++++++++++++++++++++++++++++++++
 tcg/s390/tcg-target.h |   76 ++++
 8 files changed, 1409 insertions(+), 5 deletions(-)
 create mode 100644 s390x.ld
 create mode 100644 tcg/s390/tcg-target.c
 create mode 100644 tcg/s390/tcg-target.h
Makefile.target +9 -0
index f735105..2fa2e30 100644
@@ -142,6 +142,9 @@ CPPFLAGS+=-I$(SRC_PATH)/tcg -I$(SRC_PATH)/tcg/$(ARCH)
 ifeq ($(ARCH),sparc64)
 CPPFLAGS+=-I$(SRC_PATH)/tcg/sparc
 endif
+ifeq ($(ARCH),s390x)
+CPPFLAGS+=-I$(SRC_PATH)/tcg/s390
+endif
 ifdef CONFIG_SOFTFLOAT
 LIBOBJS+=fpu/softfloat.o
 else
@@ -268,6 +271,9 @@ endif
 ifeq ($(ARCH),s390)
 LDFLAGS+=-Wl,-T,$(SRC_PATH)/$(ARCH).ld
 endif
+ifeq ($(ARCH),s390x)
+LDFLAGS+=-Wl,-T,$(SRC_PATH)/$(ARCH).ld
+endif
 
 ifeq ($(ARCH),sparc)
 # -static is used to avoid g1/g3 usage by the dynamic linker	
@@ -434,6 +440,9 @@ endif
 ifeq ($(ARCH),s390)
 LDFLAGS+=-Wl,-T,$(SRC_PATH)/$(ARCH).ld
 endif
+ifeq ($(ARCH),s390x)
+LDFLAGS+=-Wl,-T,$(SRC_PATH)/$(ARCH).ld
+endif
 
 ifeq ($(ARCH),sparc)
 # -static is used to avoid g1/g3 usage by the dynamic linker
configure +9 -1
index 82fb60a..3183b02 100755
@@ -139,9 +139,12 @@ case "$cpu" in
   ppc64)
     cpu="ppc64"
   ;;
-  s390*)
+  s390)
     cpu="s390"
   ;;
+  s390x)
+    cpu="s390x"
+  ;;
   sparc|sun4[cdmuv])
     cpu="sparc"
   ;;
@@ -773,6 +776,7 @@ hostlongbits="32"
 if test "$cpu" = "x86_64" \
      -o "$cpu" = "alpha" \
      -o "$cpu" = "ia64" \
+     -o "$cpu" = "s390x" \
      -o "$cpu" = "sparc64" \
      -o "$cpu" = "ppc64"; then
     hostlongbits="64"
@@ -1449,6 +1453,10 @@ case "$cpu" in
     echo "ARCH=s390" >> $config_mak
     echo "#define HOST_S390 1" >> $config_h
   ;;
+  s390x)
+    echo "ARCH=s390x" >> $config_mak
+    echo "#define HOST_S390X 1" >> $config_h
+  ;;
   sparc)
     echo "ARCH=sparc" >> $config_mak
     echo "#define HOST_SPARC 1" >> $config_h
dyngen-exec.h +1 -1
index c9841df..8341411 100644
@@ -120,7 +120,7 @@ extern int printf(const char *, ...);
 
 /* The return address may point to the start of the next instruction.
    Subtracting one gets us the call instruction itself.  */
-#if defined(__s390__)
+#if defined(__s390__) && !defined(__s390x__)
 # define GETPC() ((void*)(((unsigned long)__builtin_return_address(0) & 0x7fffffffUL) - 1))
 #elif defined(__arm__)
 /* Thumb return addresses have the low bit set, so we need to subtract two.
linux-user/syscall.c +1 -1
index 0bc9902..64c495b 100644
@@ -188,7 +188,7 @@ static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,	\
 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
 
-#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
+#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || defined(__s390x__)
 #define __NR__llseek __NR_lseek
 #endif
 
s390-dis.c +2 -2
index fa24792..293d6d0 100644
@@ -195,10 +195,10 @@ init_disasm (struct disassemble_info *info)
 //  switch (info->mach)
 //    {
 //    case bfd_mach_s390_31:
-      current_arch_mask = 1 << S390_OPCODE_ESA;
+//      current_arch_mask = 1 << S390_OPCODE_ESA;
 //      break;
 //    case bfd_mach_s390_64:
-//      current_arch_mask = 1 << S390_OPCODE_ZARCH;
+      current_arch_mask = 1 << S390_OPCODE_ZARCH;
 //      break;
 //    default:
 //      abort ();
s390x.ld new file +194 -0

194 lines not on this page · raw message

tcg/s390/tcg-target.c new file +1117 -0

1,117 lines not on this page · raw message

tcg/s390/tcg-target.h new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
1.6.2.1
#1 lore raw
Quoted 6 lines · Ulrich Hecht

Hi!

Here's the S/390 host support again. Run Windows CE on your mainframe, be the envy of other major financial institutions and government agencies. Changes since last time:

Quoted 7 lines

- implemented 64-bit operations
- don't abuse R15 (stack pointer)
- proper zero-extension before calling load/store helpers
- implemented i32 divu/remu and RRR multiplication ops

Now i386, AMD64, ARM, SH4, and m68k system emulators work. MIPS works at least as well as on an AMD64 host, i.e. the test image from qemu.org just

Should work with '-M mips'.

Quoted 3 lines

sits there without doing anything. The SPARC test image fails with a memory access exception in the target system, but works on an AMD64 host, so there probably still is a bug somewhere in the backend.

Tried to compare -d in_asm' outputs of S390/AMD64 when using deterministic execution (-icount N')?

[..snip..]

Signature · 1 line
mailto:[email protected]
#2 lore raw
Quoted 6 lines · malc
Quoted 3 lines · Ulrich Hecht

Now i386, AMD64, ARM, SH4, and m68k system emulators work. MIPS works at least as well as on an AMD64 host, i.e. the test image from qemu.org just

Should work with '-M mips'.

It complains about a missing "mips_bios.bin" then, which isn't in pc-bios.

Quoted 6 lines
Quoted 3 lines

sits there without doing anything. The SPARC test image fails with a memory access exception in the target system, but works on an AMD64 host, so there probably still is a bug somewhere in the backend.

Tried to compare -d in_asm' outputs of S390/AMD64 when using deterministic execution (-icount N')?

I found the problem: TCG does not zero- or sign-extend arguments to function calls. Unlike, for instance, AMD64, 32-bit arithmetic ops on S/390 leave the upper 32 bits of the destination register untouched, so there could be any kind of garbage there. If a helper taking a 64-bit argument is called with the result of such an operation, stuff breaks. IMO, TCG should do the extension in tcg_reg_alloc_call(), but that seems to be impossible because it has no information on the types of the input arguments the callee expects. Working around this in the backend is hardly possible as it does not have the information either.

I have the suspicion that even on other 64-bit hosts, this only works by accident (if it does at all): Even if the 32-bit arithmetic op does sign extension, what if the function argument is a pointer? Depending on the address, the function may get a value with a lot of F's in front...

Any ideas?

CU
Uli

Signature · 1 line
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
#3 lore raw
Quoted 5 lines

I found the problem: TCG does not zero- or sign-extend arguments to function calls. Unlike, for instance, AMD64, 32-bit arithmetic ops on S/390 leave the upper 32 bits of the destination register untouched, so there could be any kind of garbage there. If a helper taking a 64-bit argument is called with the result of such an operation, stuff breaks.

You mean an helper that takes a 32-bit value, which happens to be passed in a 64-bit register? This is a tricky one because as you say, TCG has no concept of signed or unsigned values.

I guess we could add some flag bits to the function call for this. def-helper.h/tcg_gen_callN already has something similar for identifying 64-bit arguments.

Paul

#4 lore raw
In reply to Ulrich Hecht
Quoted 10 lines · Ulrich Hecht
Quoted 6 lines · malc
Quoted 3 lines · Ulrich Hecht

Now i386, AMD64, ARM, SH4, and m68k system emulators work. MIPS works at least as well as on an AMD64 host, i.e. the test image from qemu.org just

Should work with '-M mips'.

It complains about a missing "mips_bios.bin" then, which isn't in pc-bios.

Harmless. To verify I just built fresh QEMU checkout and run things like this:

/path/to/freshly/built/qemu/mips-softmmu/qemu-system-mips \
-kernel vmlinux-2.6.18-3-qemu \
-initrd initrd.gz \
-append console=ttyS0 \
-monitor stdio \
-m 128 \
-M mips

Got:
qemu: Warning, could not load MIPS bios '/usr/local/share/qemu/mips_bios.bin'

Then pressing ctrl-alt-2 brought me straight to ttyS0 where Linux was printing it's boot messages.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

sits there without doing anything. The SPARC test image fails with a memory access exception in the target system, but works on an AMD64 host, so there probably still is a bug somewhere in the backend.

Tried to compare -d in_asm' outputs of S390/AMD64 when using deterministic execution (-icount N')?

Quoted 9 lines

I found the problem: TCG does not zero- or sign-extend arguments to function calls. Unlike, for instance, AMD64, 32-bit arithmetic ops on S/390 leave the upper 32 bits of the destination register untouched, so there could be any kind of garbage there. If a helper taking a 64-bit argument is called with the result of such an operation, stuff breaks. IMO, TCG should do the extension in tcg_reg_alloc_call(), but that seems to be impossible because it has no information on the types of the input arguments the callee expects. Working around this in the backend is hardly possible as it does not have the information either.

If the target performs 32bit arithmetics on 64bit arguments then all bets are off - the target that does that is broken. FWIW on PPC64 logical and simple arithmetic ops do not have 32-bit forms so that higher order bits (lower order according to IBM for which many people are eternally grateful) contain "garbage". Shifts/muls/divs have separate 32/64bit forms though and, at least, for shifts those lower/higher order bits will be masked.

Quoted 5 lines

I have the suspicion that even on other 64-bit hosts, this only works by accident (if it does at all): Even if the 32-bit arithmetic op does sign extension, what if the function argument is a pointer? Depending on the address, the function may get a value with a lot of F's in front...

If 32-bit arithmetic op is performed on a 64-bit pointer target translation is broken.

Quoted 2 lines

Any ideas?

As an experiment you can try to manually zero/sign extend results of all _i32 ops, but methinks the real problem should be identified.

Signature · 1 line
mailto:[email protected]