Skip to contentSkip to conversation

MIPS(EL) host support

PATCH In review Aurelien Jarno 2 participants 8 messages Oct 22, 2009 – Oct 23, 2009

lore
Share & tools
lore
#6 lore raw

PATCH 4/5 tcg: increase TCG_MAX_OP_SIZE to 192

In reply to Aurelien Jarno

This is needed on a MIPS host and a 64-bit cross-endian target.

---
 exec-all.h |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
exec-all.h +3 -3
index dd134a9..820b59e 100644
@@ -41,10 +41,10 @@ typedef struct TranslationBlock TranslationBlock;
 #define OPC_MAX_SIZE (OPC_BUF_SIZE - MAX_OP_PER_INSTR)
 
 /* Maximum size a TCG op can expand to.  This is complicated because a
-   single op may require several host instructions and regirster reloads.
-   For now take a wild guess at 128 bytes, which should allow at least
+   single op may require several host instructions and register reloads.
+   For now take a wild guess at 192 bytes, which should allow at least
    a couple of fixup instructions per argument.  */
-#define TCG_MAX_OP_SIZE 128
+#define TCG_MAX_OP_SIZE 192
 
 #define OPPARAM_BUF_SIZE (OPC_BUF_SIZE * MAX_OPC_PARAM)
 
Signature · 1 line
1.6.1.3
#0 lore raw

cover MIPS(EL) host support

This patch series add host MIPS(EL) support to QEMU. It is based on the excellent work from Arnaud Patard [1].

It supports both little and big-endian hosts, and is currently limited to the o32 abi. It has been mainly developped and tested in system mode on a MIPSEL host, but I have verified that both MIPS and MIPSEL hosts can boot Debian Installer amd64, arm, i386, mips, mipsel, ppc and sparc, and can also boot an SH4 kernel. On the user side, I have verified that basic support (aka starting libc.so.6) works, but there is probably some more work to do on this side.

I have mainly focused on getting host support fully working, and haven't written "aggressively" optimised code. There is still space for speed improvement, for example by filling the delay slots or using MIPS R2 instructions (if available) for byte swapping, rotations or bit field insertion/extraction (useful for the memory load/store).

[1] http://git.rtp-net.org/?p=qemu.git;a=summary

Arnaud Patard (2):
linux-user: remove hardcoded value of _NSIG in signal.c cpu-all.h: fix cpu_get_real_ticks on mips host

Aurelien Jarno (3):
tcg: initial mips support
tcg: increase TCG_MAX_OP_SIZE to 192
tcg/mips: use direct jumps

 configure             |    7 +-
 cpu-all.h             |   27 +-
 exec-all.h            |   19 +-
 exec.c                |    7 +
 linux-user/signal.c   |   12 +-
 tcg/mips/tcg-target.c | 1464 +++++++++++++++++++++++++++++++++++++++++++++++++
 tcg/mips/tcg-target.h |  102 ++++
 7 files changed, 1618 insertions(+), 20 deletions(-)
 create mode 100644 tcg/mips/tcg-target.c
 create mode 100644 tcg/mips/tcg-target.h
#7 lore raw

PATCH 5/5 tcg/mips: use direct jumps

Use direct jumps on MIPS hosts. The jump address has to be written atomically, so a R_MIPS_26 needs to be used. It means the code generation buffer should not be bigger than 256MB, and has to be aligned on a 256MB boundary.

---
 exec-all.h            |   13 ++++++++++++-
 exec.c                |    7 +++++++
 tcg/mips/tcg-target.c |    3 ++-
 tcg/mips/tcg-target.h |    5 -----
 4 files changed, 21 insertions(+), 7 deletions(-)
exec-all.h +12 -1
index 820b59e..21d6c91 100644
@@ -114,7 +114,7 @@ static inline int tlb_set_page(CPUState *env1, target_ulong vaddr,
 #define CODE_GEN_AVG_BLOCK_SIZE 64
 #endif
 
-#if defined(_ARCH_PPC) || defined(__x86_64__) || defined(__arm__) || defined(__i386__)
+#if defined(_ARCH_PPC) || defined(__x86_64__) || defined(__arm__) || defined(__i386__) || defined(__mips__)
 #define USE_DIRECT_JUMP
 #endif
 
@@ -222,6 +222,17 @@ static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr
     __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
 #endif
 }
+#elif defined(__mips__)
+static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
+{
+    *(uint32_t *)jmp_addr = 0x08000000 | ((addr >> 2) & 0x03ffffff);
+#if QEMU_GNUC_PREREQ(4, 1)
+    void __clear_cache(char *beg, char *end);
+    __clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
+#else
+# error __clear_cache not available
+#endif
+}
 #endif
 
 static inline void tb_set_jmp_target(TranslationBlock *tb,
exec.c +7 -0
index 076d26b..1d3f48c 100644
@@ -454,6 +454,13 @@ static void code_gen_alloc(unsigned long tb_size)
         start = (void *) 0x01000000UL;
         if (code_gen_buffer_size > 16 * 1024 * 1024)
             code_gen_buffer_size = 16 * 1024 * 1024;
+#elif defined(__mips__)
+        /* Map the buffer aligned on a 256M boundary, so we can use direct
+           calls and branches (R_MIPS_26 relocation) */
+        flags |= MAP_FIXED;
+        start = (void *) 0x30000000UL;
+        if (code_gen_buffer_size > 256 * 1024 * 1024)
+            code_gen_buffer_size = 256 * 1024 * 1024;
 #endif
         code_gen_buffer = mmap(start, code_gen_buffer_size,
                                PROT_WRITE | PROT_READ | PROT_EXEC,
tcg/mips/tcg-target.c +2 -1
index 03fdcbf..d445169 100644
@@ -1042,7 +1042,8 @@ static inline void tcg_out_op(TCGContext *s, int opc,
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
             /* direct jump method */
-            tcg_abort();
+            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
+            s->code_ptr += 4;
         } else {
             /* indirect jump method */
             tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_AT, (tcg_target_long)(s->tb_next + args[0]));
tcg/mips/tcg-target.h +0 -5
index ba91623..3d36100 100644
@@ -98,10 +98,5 @@ enum {
 
 static inline void flush_icache_range(unsigned long start, unsigned long stop)
 {
-#if QEMU_GNUC_PREREQ(4, 1)
-    void __clear_cache(char *beg, char *end);
     __clear_cache((char *) start, (char *) stop);
-#else
-# error __clear_cache not available
-#endif
 }
Signature · 1 line
1.6.1.3
#1 lore raw

PATCH 1/5 linux-user: remove hardcoded value of _NSIG in signal.c

In reply to Aurelien Jarno

From: Arnaud Patard <[email protected]>

In a bunch of places, 64 is used as value of _NSIG but it's wrong at least on MIPS were _NSIG is 128.

---
 linux-user/signal.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
linux-user/signal.c +6 -6
index 2df17aa..6620ce3 100644
@@ -44,7 +44,7 @@ static struct target_sigaction sigact_table[TARGET_NSIG];
 static void host_signal_handler(int host_signum, siginfo_t *info,
                                 void *puc);
 
-static uint8_t host_to_target_signal_table[65] = {
+static uint8_t host_to_target_signal_table[_NSIG+1] = {
     [SIGHUP] = TARGET_SIGHUP,
     [SIGINT] = TARGET_SIGINT,
     [SIGQUIT] = TARGET_SIGQUIT,
@@ -87,7 +87,7 @@ static uint8_t host_to_target_signal_table[65] = {
     [__SIGRTMIN] = __SIGRTMAX,
     [__SIGRTMAX] = __SIGRTMIN,
 };
-static uint8_t target_to_host_signal_table[65];
+static uint8_t target_to_host_signal_table[_NSIG+1];
 
 static inline int on_sig_stack(unsigned long sp)
 {
@@ -103,14 +103,14 @@ static inline int sas_ss_flags(unsigned long sp)
 
 int host_to_target_signal(int sig)
 {
-    if (sig > 64)
+    if (sig > _NSIG)
         return sig;
     return host_to_target_signal_table[sig];
 }
 
 int target_to_host_signal(int sig)
 {
-    if (sig > 64)
+    if (sig > _NSIG)
         return sig;
     return target_to_host_signal_table[sig];
 }
@@ -311,11 +311,11 @@ void signal_init(void)
     int host_sig;
 
     /* generate signal conversion tables */
-    for(i = 1; i <= 64; i++) {
+    for(i = 1; i <= _NSIG; i++) {
         if (host_to_target_signal_table[i] == 0)
             host_to_target_signal_table[i] = i;
     }
-    for(i = 1; i <= 64; i++) {
+    for(i = 1; i <= _NSIG; i++) {
         j = host_to_target_signal_table[i];
         target_to_host_signal_table[j] = i;
     }
Signature · 1 line
1.6.1.3
#4 lore raw

PATCH 2/5 cpu-all.h: fix cpu_get_real_ticks on mips host

In reply to Aurelien Jarno

From: Arnaud Patard <[email protected]>

Fix cpu_get_real_ticks:
- check should be done on __mips and not __mips_isa_rev
- linux kernels >= 2.6.25 are emulating the 2 needed rdhwr functions so it's safe to use rdhwr.

This is better than what's currently in but it doesn't mean it works nicely Some tests needs to be done imho

---
 cpu-all.h |   27 ++++++++++++++++++---------
 1 files changed, 18 insertions(+), 9 deletions(-)
cpu-all.h +18 -9
index ebe8bfb..e214374 100644
@@ -1017,24 +1017,33 @@ static inline int64_t cpu_get_real_ticks (void)
 #endif
 }
 
-#elif defined(__mips__)
+#elif (defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__)
+/*
+ * binutils wants to use rdhwr only on mips32r2
+ * but as linux kernel emulate it, it's fine
+ * to use it.
+ *
+ */
+#define MIPS_RDHWR(rd, value) {                 \
+    __asm__ __volatile__ (                      \
+                          ".set   push\n\t"     \
+                          ".set mips32r2\n\t"   \
+                          "rdhwr  %0, "rd"\n\t" \
+                          ".set   pop"          \
+                          : "=r" (value));      \
+}
 
 static inline int64_t cpu_get_real_ticks(void)
 {
-#if defined(__mips_isa_rev) && __mips_isa_rev >= 2
+/* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
     uint32_t count;
     static uint32_t cyc_per_count = 0;
 
     if (!cyc_per_count)
-        __asm__ __volatile__("rdhwr %0, $3" : "=r" (cyc_per_count));
+        MIPS_RDHWR("$3", cyc_per_count);
 
-    __asm__ __volatile__("rdhwr %1, $2" : "=r" (count));
+    MIPS_RDHWR("$2", count);
     return (int64_t)(count * cyc_per_count);
-#else
-    /* FIXME */
-    static int64_t ticks = 0;
-    return ticks++;
-#endif
 }
 
 #else
Signature · 1 line
1.6.1.3
#5 lore raw

PATCH 3/5 tcg: initial mips support

In reply to Aurelien Jarno

Based on a patch from Arnaud Patard (Rtp) <[email protected]>

A few words about design choices:
* Two registers, at and t0, are reserved for TCG internal use. They are useful for bswap and 64-bit ops.
* Most ops supports a constant argument with value 0, which is actually mapped to the zero register.
* While the at register is available for constant loading, ops only support a limited range of constants. TCG does a better job doing the register allocation and constant loading by itself. There are plenty of registers available anyway.

---
 configure             |    7 +-
 tcg/mips/tcg-target.c | 1463 +++++++++++++++++++++++++++++++++++++++++++++++++
 tcg/mips/tcg-target.h |  107 ++++
 3 files changed, 1576 insertions(+), 1 deletions(-)
 create mode 100644 tcg/mips/tcg-target.c
 create mode 100644 tcg/mips/tcg-target.h
configure +6 -1
index 43d87c5..e70b7ce 100755
@@ -133,13 +133,15 @@ elif check_define _ARCH_PPC ; then
   else
     cpu="ppc"
   fi
+elif check_define __mips__ ; then
+  cpu="mips"
 else
   cpu=`uname -m`
 fi
 
 target_list=""
 case "$cpu" in
-  alpha|cris|ia64|m68k|microblaze|mips|mips64|ppc|ppc64|sparc64)
+  alpha|cris|ia64|m68k|microblaze|ppc|ppc64|sparc64)
     cpu="$cpu"
   ;;
   i386|i486|i586|i686|i86pc|BePC)
@@ -157,6 +159,9 @@ case "$cpu" in
   parisc|parisc64)
     cpu="hppa"
   ;;
+  mips*)
+    cpu="mips"
+  ;;
   s390*)
     cpu="s390"
   ;;
tcg/mips/tcg-target.c new file +1463 -0

1,463 lines not on this page · raw message

tcg/mips/tcg-target.h new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
1.6.1.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] linux-user: remove hardcoded value of _NSIG in signal.c

In reply to Aurelien Jarno
Quoted 55 lines · Aurelien Jarno

From: Arnaud Patard <[email protected]>

In a bunch of places, 64 is used as value of _NSIG but it's wrong at least on MIPS were _NSIG is 128.

---
 linux-user/signal.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
linux-user/signal.c +4 -4
index 2df17aa..6620ce3 100644
@@ -44,7 +44,7 @@ static struct target_sigaction sigact_table[TARGET_NSIG];
 static void host_signal_handler(int host_signum, siginfo_t *info,
                                 void *puc);
 
-static uint8_t host_to_target_signal_table[65] = {
+static uint8_t host_to_target_signal_table[_NSIG+1] = {
     [SIGHUP] = TARGET_SIGHUP,
     [SIGINT] = TARGET_SIGINT,
     [SIGQUIT] = TARGET_SIGQUIT,
@@ -87,7 +87,7 @@ static uint8_t host_to_target_signal_table[65] = {
     [__SIGRTMIN] = __SIGRTMAX,
     [__SIGRTMAX] = __SIGRTMIN,
 };
-static uint8_t target_to_host_signal_table[65];
+static uint8_t target_to_host_signal_table[_NSIG+1];
 
 static inline int on_sig_stack(unsigned long sp)
 {
@@ -103,14 +103,14 @@ static inline int sas_ss_flags(unsigned long sp)
 
 int host_to_target_signal(int sig)
 {
-    if (sig > 64)
+    if (sig > _NSIG)
         return sig;
     return host_to_target_signal_table[sig];
 }
 
 int target_to_host_signal(int sig)
 {
-    if (sig > 64)
+    if (sig > _NSIG)
         return sig;
     return target_to_host_signal_table[sig];
 }
@@ -311,11 +311,11 @@ void signal_init(void)
     int host_sig;
 
     /* generate signal conversion tables */

linux-user/signal.c void signal_init(void) · line 321

Quoted 10 lines
linux-user/signal.c +2 -2
-    for(i = 1; i <= 64; i++) {
+    for(i = 1; i <= _NSIG; i++) {
         if (host_to_target_signal_table[i] == 0)
             host_to_target_signal_table[i] = i;
     }
-    for(i = 1; i <= 64; i++) {
+    for(i = 1; i <= _NSIG; i++) {
         j = host_to_target_signal_table[i];
         target_to_host_signal_table[j] = i;
     }

At least in my linux installation, _NSIG is 65 (x86) or 128 (mips), so the patch above should replace 64 by (_NSIG - 1) and 65 by _NSIG.

But perhaps it does no harm if the code allocates an unused signal at the end.

For my tests with mips host and tci, I used
static uint8_t target_to_host_signal_table[_NSIG]

Regards,
Stefan

#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] linux-user: remove hardcoded value of _NSIG in signal.c

Quoted 70 lines · Stefan Weil
Quoted 55 lines · Aurelien Jarno

From: Arnaud Patard <[email protected]>

In a bunch of places, 64 is used as value of _NSIG but it's wrong at least on MIPS were _NSIG is 128.

---
 linux-user/signal.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
linux-user/signal.c +4 -4
index 2df17aa..6620ce3 100644
@@ -44,7 +44,7 @@ static struct target_sigaction sigact_table[TARGET_NSIG];
 static void host_signal_handler(int host_signum, siginfo_t *info,
                                 void *puc);
 
-static uint8_t host_to_target_signal_table[65] = {
+static uint8_t host_to_target_signal_table[_NSIG+1] = {
     [SIGHUP] = TARGET_SIGHUP,
     [SIGINT] = TARGET_SIGINT,
     [SIGQUIT] = TARGET_SIGQUIT,
@@ -87,7 +87,7 @@ static uint8_t host_to_target_signal_table[65] = {
     [__SIGRTMIN] = __SIGRTMAX,
     [__SIGRTMAX] = __SIGRTMIN,
 };
-static uint8_t target_to_host_signal_table[65];
+static uint8_t target_to_host_signal_table[_NSIG+1];
 
 static inline int on_sig_stack(unsigned long sp)
 {
@@ -103,14 +103,14 @@ static inline int sas_ss_flags(unsigned long sp)
 
 int host_to_target_signal(int sig)
 {
-    if (sig > 64)
+    if (sig > _NSIG)
         return sig;
     return host_to_target_signal_table[sig];
 }
 
 int target_to_host_signal(int sig)
 {
-    if (sig > 64)
+    if (sig > _NSIG)
         return sig;
     return target_to_host_signal_table[sig];
 }
@@ -311,11 +311,11 @@ void signal_init(void)
     int host_sig;
 
     /* generate signal conversion tables */

linux-user/signal.c void signal_init(void) · line 321

Quoted 10 lines
linux-user/signal.c +2 -2
-    for(i = 1; i <= 64; i++) {
+    for(i = 1; i <= _NSIG; i++) {
         if (host_to_target_signal_table[i] == 0)
             host_to_target_signal_table[i] = i;
     }
-    for(i = 1; i <= 64; i++) {
+    for(i = 1; i <= _NSIG; i++) {
         j = host_to_target_signal_table[i];
         target_to_host_signal_table[j] = i;
     }

At least in my linux installation, _NSIG is 65 (x86) or 128 (mips), so the patch above should replace 64 by (_NSIG - 1) and 65 by _NSIG.

Quoted 8 lines

But perhaps it does no harm if the code allocates an unused signal at the end.

For my tests with mips host and tci, I used
static uint8_t target_to_host_signal_table[_NSIG]

From /usr/include/bits/signum.h:

#define _NSIG 65 /* Biggest signal number + 1

I confirm that all use of _NSIG of them should be decremented by one.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net