Skip to contentSkip to conversation

ARM host (TCG only)

PATCH In review andrzej zaborowski 3 participants 4 messages May 5, 2008 – May 7, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM host (TCG only)

The attached file adds ARM as TCG target (qemu host). It is very basic and I'm sure several things only work by dumb luck and other don't work at all. TCG ops are mostly complete except division. It uses only armv4 instructions. I tested only ARM target and I was able to boot PalmOS, Linux and u-boot. The hosts were Nokia n810 (armv6) and Neo1973 (armv4). There's still a bug somewhere because on linux guest some filesystems won't work for / (Linux will just be unable to find init).

TODO:
* Qemu load and store ops are hacky and don't use the TLB cache. It
doesn't have as much impact as I expected - PalmOS works very smoothly (maybe because it uses DMA for many things) but Linux decompressor is rather slow.

* No division, so even targets fully converted to TCG abort when dividing.

* It should use a constants pool for immediates, like gcc.

* No user mode support.

* Probably unrelated to TCG, the bug causing some cpu loop exit
requests to be lost is still sometimes visible on ARM, causing qemu to get stuck in the exec loop if not using -clock unix and no SDL.

Cheers

Signature · 2 lines
Please do not print this email unless absolutely necessary. Spread
environmental awareness.
cpu-exec.c +1 -1
index 25dcf2b..f74e6a6 100644
@@ -674,7 +674,7 @@ int cpu_exec(CPUState *env1)
                                 "r18", "r19", "r20", "r21", "r22", "r23",
                                 "r24", "r25", "r26", "r27", "r28", "r29",
                                 "r30", "r31");
-#elif defined(__arm__)
+#elif 0 && defined(__arm__)
                 asm volatile ("mov pc, %0\n\t"
                               ".global exec_loop\n\t"
                               "exec_loop:\n\t"
dyngen.c +8 -8
index 369d819..b8578a9 100644
@@ -1342,21 +1342,21 @@ int arm_emit_ldr_info(const char *name, unsigned long start_offset,
                 spare = max_pool - offset;
             if ((offset & 3) !=0)
                 error("%s:%04x: pc offset must be 32 bit aligned",
-                      name, start_offset + p - p_start);
+                      name, (int) (start_offset + p - p_start));
             if (offset < 0)
                 error("%s:%04x: Embedded literal value",
-                      name, start_offset + p - p_start);
+                      name, (int) (start_offset + p - p_start));
             pc_offset = p - p_start + offset + 8;
             if (pc_offset <= (p - p_start) ||
                 pc_offset >= (p_end - p_start))
                 error("%s:%04x: pc offset must point inside the function code",
-                      name, start_offset + p - p_start);
+                      name, (int) (start_offset + p - p_start));
             if (pc_offset < min_offset)
                 min_offset = pc_offset;
             if (outfile) {
                 /* The intruction position */
                 fprintf(outfile, "    arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
-                        p - p_start);
+                        (int) (p - p_start));
                 /* The position of the constant pool data.  */
                 data_index = ((p_end - p_start) - pc_offset) >> 2;
                 fprintf(outfile, "    arm_ldr_ptr->data_ptr = arm_data_ptr - %d;\n",
@@ -1425,7 +1425,7 @@ int arm_emit_ldr_info(const char *name, unsigned long start_offset,
         if (outfile) {
             fprintf(outfile,
                     "    *(uint32_t *)(gen_code_ptr + %d) = 0xe28dd004;\n",
-                    p - p_start);
+                    (int) (p - p_start));
         }
         p += 4;
     } else if ((insn & 0xffff8000) != 0xe89d8000
@@ -2868,7 +2868,7 @@ int gen_file(FILE *outfile, int out_type)
         /* generate big code generation switch */
 
 #ifdef HOST_ARM
-#error broken
+#warning dyngen broken
         /* We need to know the size of all the ops so we can figure out when
            to emit constant pools.  This must be consistent with opc.h.  */
 fprintf(outfile,
@@ -2890,7 +2890,7 @@ fprintf(outfile,
 #endif
 
 #ifdef HOST_ARM
-#error broken
+#warning dyngen broken
 /* Arm is tricky because it uses constant pools for loading immediate values.
    We assume (and require) each function is code followed by a constant pool.
    All the ops are small so this should be ok.  For each op we figure
@@ -2982,7 +2982,7 @@ fprintf(outfile,
 #endif
 
 #ifdef HOST_ARM
-#error broken
+#warning dyngen broken
 /* Generate constant pool if needed */
 fprintf(outfile,
 "            if (gen_code_ptr + arm_opc_size[*opc_ptr] >= arm_pool_ptr) {\n"
exec-all.h +17 -1
index 898cf68..76f4279 100644
@@ -141,7 +141,7 @@ static inline int tlb_set_page(CPUState *env, target_ulong vaddr,
 
 #define CODE_GEN_MAX_BLOCKS    (CODE_GEN_BUFFER_SIZE / CODE_GEN_AVG_BLOCK_SIZE)
 
-#if defined(__powerpc__) || defined(__x86_64__)
+#if defined(__powerpc__) || defined(__x86_64__) || defined(__arm__)
 #define USE_DIRECT_JUMP
 #endif
 #if defined(__i386__) && !defined(_WIN32)
@@ -240,6 +240,22 @@ static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr
     *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
     /* no need to flush icache explicitely */
 }
+#elif defined(__arm__)
+static inline void flush_icache_range(unsigned long start, unsigned long stop)
+{
+    register unsigned long _beg __asm ("a1") = start;
+    register unsigned long _end __asm ("a2") = stop;
+    register unsigned long _flg __asm ("a3") = 0;
+    __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
+}
+
+static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
+{
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
+    *(uint32_t *)jmp_addr |= ((addr - (jmp_addr + 8)) >> 2) & 0xffffff;
+    /* flush icache */
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
 #endif
 
 static inline void tb_set_jmp_target(TranslationBlock *tb,
hw/omap1.c +1 -1
index b439423..21bc27a 100644
@@ -696,7 +696,7 @@ static inline void omap_timer_update(struct omap_mpu_timer_s *timer)
 
     if (timer->enable && timer->st && timer->rate) {
         timer->val = timer->reset_val;	/* Should skip this on clk enable */
-        expires = muldiv64(timer->val << (timer->ptv + 1),
+        expires = muldiv64((uint64_t) timer->val << (timer->ptv + 1),
                         ticks_per_sec, timer->rate);
 
         /* If timer expiry would be sooner than in about 1 ms and
tcg/README +1 -1
index c27c5ab..51709a1 100644
@@ -75,7 +75,7 @@ depending on their declarations).
 * Helpers:
 
 Using the tcg_gen_helper_x_y it is possible to call any function
-taking i32, i64 or pointer types types. Before calling an helper, all
+taking i32, i64 or pointer types. Before calling an helper, all
 globals are stored at their canonical location and it is assumed that
 the function can modify them. In the future, function modifiers will
 be allowed to tell that the helper does not read or write some globals.
tcg/arm/tcg-target.c new file +1217 -0

1,217 lines not on this page · raw message

tcg/arm/tcg-target.h new file +62 -0

62 lines not on this page · raw message

#1 lore raw
Quoted 7 lines · andrzej zaborowski

The attached file adds ARM as TCG target (qemu host). It is very basic and I'm sure several things only work by dumb luck and other don't work at all. TCG ops are mostly complete except division. It uses only armv4 instructions. I tested only ARM target and I was able to boot PalmOS, Linux and u-boot. The hosts were Nokia n810 (armv6) and Neo1973 (armv4). There's still a bug somewhere because on linux guest some filesystems won't work for / (Linux will just be unable to

I'd actually rip out all the dyngen bits. The current arm host support is sufficiently flakey that I don't think it's worth trying to preserve it or pretend it works.

Paul

#2 lore raw
In reply to andrzej zaborowski

Here's a second version with some fixes and less in TODO.

* Now uses the tlb cache, Linux decompressor is about two times faster (caching read addresses had this effect, caching write addresses is not noticeable speed-wise).

* Added user-mode support, tested with a busybox static OABI binary (on EABI host).

* Implemented division ops.

* Calling the generated code now uses the same convention as dyngen used, saving a couple of registers.

* Removes dyngen HOST_ARM code as suggested by Paul. This has the advantage that a non-tcg target will (hopefully) give a readable error message when you try to run it.

* Still no constant pools.

I use USE_DIRECT_JUMP for goto_tb, but it could also work without it - I'm not sure what is the advantage or disadvantage of that.

Signature · 2 lines
Please do not print this email unless absolutely necessary. Spread
environmental awareness.
cpu-exec.c +2 -1
index fea47cb..a7e77a5 100644
@@ -679,7 +679,8 @@ int cpu_exec(CPUState *env1)
                               "exec_loop:\n\t"
                               : /* no outputs */
                               : "r" (gen_func)
-                              : "r1", "r2", "r3", "r8", "r9", "r10", "r12", "r14");
+                              : "r1", "r2", "r3",
+                                "r8", "r9", "r10", "r11", "r12", "r14");
 #elif defined(__ia64)
 		struct fptr {
 			void *ip;
dyngen.c +5 -311

358 lines not on this page · raw message

exec-all.h +17 -1
index d4dea1d..bba2178 100644
@@ -144,7 +144,7 @@ static inline int tlb_set_page(CPUState *env, target_ulong vaddr,
 
 #define CODE_GEN_MAX_BLOCKS    (CODE_GEN_BUFFER_SIZE / CODE_GEN_AVG_BLOCK_SIZE)
 
-#if defined(__powerpc__) || defined(__x86_64__)
+#if defined(__powerpc__) || defined(__x86_64__) || defined(__arm__)
 #define USE_DIRECT_JUMP
 #endif
 #if defined(__i386__) && !defined(_WIN32)
@@ -243,6 +243,22 @@ static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr
     *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
     /* no need to flush icache explicitely */
 }
+#elif defined(__arm__)
+static inline void flush_icache_range(unsigned long start, unsigned long stop)
+{
+    register unsigned long _beg __asm ("a1") = start;
+    register unsigned long _end __asm ("a2") = stop;
+    register unsigned long _flg __asm ("a3") = 0;
+    __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
+}
+
+static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
+{
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
+    *(uint32_t *)jmp_addr |= ((addr - (jmp_addr + 8)) >> 2) & 0xffffff;
+    /* flush icache */
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
 #endif
 
 static inline void tb_set_jmp_target(TranslationBlock *tb,
tcg/README +1 -1
index c27c5ab..51709a1 100644
@@ -75,7 +75,7 @@ depending on their declarations).
 * Helpers:
 
 Using the tcg_gen_helper_x_y it is possible to call any function
-taking i32, i64 or pointer types types. Before calling an helper, all
+taking i32, i64 or pointer types. Before calling an helper, all
 globals are stored at their canonical location and it is assumed that
 the function can modify them. In the future, function modifiers will
 be allowed to tell that the helper does not read or write some globals.
tcg/arm/tcg-target.c new file +1524 -0

1,524 lines not on this page · raw message

tcg/arm/tcg-target.h new file +64 -0

64 lines not on this page · raw message

tcg/tcg-dyngen.c +0 -103

109 lines not on this page · raw message

#3 lore raw
Quoted 14 lines · andrzej zaborowski

Here's a second version with some fixes and less in TODO.

* Now uses the tlb cache, Linux decompressor is about two times faster (caching read addresses had this effect, caching write addresses is not noticeable speed-wise).

* Added user-mode support, tested with a busybox static OABI binary (on EABI host).

* Implemented division ops.

* Calling the generated code now uses the same convention as dyngen used, saving a couple of registers.

Quoted 9 lines

* Removes dyngen HOST_ARM code as suggested by Paul. This has the advantage that a non-tcg target will (hopefully) give a readable error message when you try to run it.

* Still no constant pools.

I use USE_DIRECT_JUMP for goto_tb, but it could also work without it - I'm not sure what is the advantage or disadvantage of that.

Really nice work. I have been able to boot debian installer sparc using qemu-system-sparc on a qemu arm machine running on an x86-64 host.

Not very fast though, so I stopped the installation after a few screens.

Signature · 4 lines
  .''`.  Aurelien Jarno	            | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net