Skip to contentSkip to conversation

ARM aarch64 TCG target

PATCH Superseded Claudio Fontana 2 participants 6 messages May 28, 2013 – May 29, 2013

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover ARM aarch64 TCG target

This series implements preliminary support for the ARM aarch64 TCG target.

Limitations of this initial implementation (TODOs) include:

* missing TLB lookup in qemu_ld/st [C helpers always called]. An incremental patch, which requires this series, is coming up from colleague Jani Kokkonen to implement this.
* most optional opcodes are not implemented yet (only rotation done).
* CONFIG_SOFTMMU only

Tested running on a x86-64 physical machine running Foundation v8, running a linux 3.8.0-rc6+ minimal host system based on linaro v8 image 201301271620 for user space.

Tested guests: arm v5 test image, i386 FreeDOS test image, i386 linux test image, all from qemu-devel testing page. Also tested on x86-64/linux built with buildroot, and on arm v7/linux built with buildroot as well.

Changes in v2:

* for icache flushing, removed placeholder for old gcc
* aligned defines values in the elf aarch64 relocations
* added comment in the elf aarch64 relocations
* use X16 and X17 as well, they should be safe to use
* defined TCG_REG_TMP to TCG_REG_X8
* fix relocs and gotos to be more robust during retranslation
* removed declarations and assignments on same line
* added braces in 'if's even when unnecessary
* added comment about COND_NV behaving like COND_AL in aarch64
* added comment about no-extend field
* remove trampoline for the conditional branches, add CONDBR19
* set MAX_CODE_GEN_BUFFER_SIZE for aarch64, matching JUMP26
* improved left rotations, by using one less instruction
* for setcond_i32/i64 use CSET instead of CSEL
* implement andi and subi for working with the stack
* do not rely on temp_buf for tcg_set_frame: use stack
* remove unused constrained ARM constant
* redefine enums with same value to one-another
* fix setting of available regs (set all 32 bits)
* moved configure patch to after the tcg target in the series
* added low level operations useful in preparation of tlb lookup

Changes in v3:
* removed low level operations introduced in v2, will be in separate series
* honor 'addend' in patch_reloc, although it's always 0
* replace use of 'int' with 'TCGReg' when registers are expected
* merge movi32 and movi64 into movi_aux
* use 32bit version of the instructions when possible, to save energy/cycles
* do not clobber a passed register for INDEX_op_rotl_i32/i64
* removed hard coded SP and FP in stack functions, make them params
* zero-extend addr_reg for 32bit guests in qemu_ld/st
* make use of deposit32 (bitops) in reloc_pc26 and reloc_pc19
* never use multiple cases per line in switches even when empty
* less pessimistic range checks for instructions
* other formatting fixes that fell through the cracks in v2

Claudio Fontana (3):
include/elf.h: add aarch64 ELF machine and relocs tcg/aarch64: implement new TCG target for aarch64
configure: permit compilation on arm aarch64

 configure                |    8 +
 include/elf.h            |  129 ++++++
 include/exec/exec-all.h  |    5 +-
 tcg/aarch64/tcg-target.c | 1159 ++++++++++++++++++++++++++++++++++++++++++++++
 tcg/aarch64/tcg-target.h |   99 ++++
 translate-all.c          |    2 +
 6 files changed, 1401 insertions(+), 1 deletion(-)
 create mode 100644 tcg/aarch64/tcg-target.c
 create mode 100644 tcg/aarch64/tcg-target.h
Signature · 1 line
1.8.1
#1 lore raw

PATCH v3 1/3 include/elf.h: add aarch64 ELF machine and relocs

we will use the 26bit relative relocs in the aarch64 tcg target.

---
 include/elf.h | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
include/elf.h +129 -0
index a21ea53..cf0d3e2 100644
@@ -129,6 +129,8 @@ typedef int64_t  Elf64_Sxword;
 
 #define EM_XTENSA   94      /* Tensilica Xtensa */
 
+#define EM_AARCH64  183
+
 /* This is the info that is needed to parse the dynamic section of the file */
 #define DT_NULL		0
 #define DT_NEEDED	1
@@ -616,6 +618,133 @@ typedef struct {
 /* Keep this the last entry.  */
 #define R_ARM_NUM		256
 
+/* ARM Aarch64 relocation types */
+#define R_AARCH64_NONE                256 /* also accepts R_ARM_NONE (0) */
+/* static data relocations */
+#define R_AARCH64_ABS64               257
+#define R_AARCH64_ABS32               258
+#define R_AARCH64_ABS16               259
+#define R_AARCH64_PREL64              260
+#define R_AARCH64_PREL32              261
+#define R_AARCH64_PREL16              262
+/* static aarch64 group relocations */
+/* group relocs to create unsigned data value or address inline */
+#define R_AARCH64_MOVW_UABS_G0        263
+#define R_AARCH64_MOVW_UABS_G0_NC     264
+#define R_AARCH64_MOVW_UABS_G1        265
+#define R_AARCH64_MOVW_UABS_G1_NC     266
+#define R_AARCH64_MOVW_UABS_G2        267
+#define R_AARCH64_MOVW_UABS_G2_NC     268
+#define R_AARCH64_MOVW_UABS_G3        269
+/* group relocs to create signed data or offset value inline */
+#define R_AARCH64_MOVW_SABS_G0        270
+#define R_AARCH64_MOVW_SABS_G1        271
+#define R_AARCH64_MOVW_SABS_G2        272
+/* relocs to generate 19, 21, and 33 bit PC-relative addresses */
+#define R_AARCH64_LD_PREL_LO19        273
+#define R_AARCH64_ADR_PREL_LO21       274
+#define R_AARCH64_ADR_PREL_PG_HI21    275
+#define R_AARCH64_ADR_PREL_PG_HI21_NC 276
+#define R_AARCH64_ADD_ABS_LO12_NC     277
+#define R_AARCH64_LDST8_ABS_LO12_NC   278
+#define R_AARCH64_LDST16_ABS_LO12_NC  284
+#define R_AARCH64_LDST32_ABS_LO12_NC  285
+#define R_AARCH64_LDST64_ABS_LO12_NC  286
+#define R_AARCH64_LDST128_ABS_LO12_NC 299
+/* relocs for control-flow - all offsets as multiple of 4 */
+#define R_AARCH64_TSTBR14             279
+#define R_AARCH64_CONDBR19            280
+#define R_AARCH64_JUMP26              282
+#define R_AARCH64_CALL26              283
+/* group relocs to create pc-relative offset inline */
+#define R_AARCH64_MOVW_PREL_G0        287
+#define R_AARCH64_MOVW_PREL_G0_NC     288
+#define R_AARCH64_MOVW_PREL_G1        289
+#define R_AARCH64_MOVW_PREL_G1_NC     290
+#define R_AARCH64_MOVW_PREL_G2        291
+#define R_AARCH64_MOVW_PREL_G2_NC     292
+#define R_AARCH64_MOVW_PREL_G3        293
+/* group relocs to create a GOT-relative offset inline */
+#define R_AARCH64_MOVW_GOTOFF_G0      300
+#define R_AARCH64_MOVW_GOTOFF_G0_NC   301
+#define R_AARCH64_MOVW_GOTOFF_G1      302
+#define R_AARCH64_MOVW_GOTOFF_G1_NC   303
+#define R_AARCH64_MOVW_GOTOFF_G2      304
+#define R_AARCH64_MOVW_GOTOFF_G2_NC   305
+#define R_AARCH64_MOVW_GOTOFF_G3      306
+/* GOT-relative data relocs */
+#define R_AARCH64_GOTREL64            307
+#define R_AARCH64_GOTREL32            308
+/* GOT-relative instr relocs */
+#define R_AARCH64_GOT_LD_PREL19       309
+#define R_AARCH64_LD64_GOTOFF_LO15    310
+#define R_AARCH64_ADR_GOT_PAGE        311
+#define R_AARCH64_LD64_GOT_LO12_NC    312
+#define R_AARCH64_LD64_GOTPAGE_LO15   313
+/* General Dynamic TLS relocations */
+#define R_AARCH64_TLSGD_ADR_PREL21            512
+#define R_AARCH64_TLSGD_ADR_PAGE21            513
+#define R_AARCH64_TLSGD_ADD_LO12_NC           514
+#define R_AARCH64_TLSGD_MOVW_G1               515
+#define R_AARCH64_TLSGD_MOVW_G0_NC            516
+/* Local Dynamic TLS relocations */
+#define R_AARCH64_TLSLD_ADR_PREL21            517
+#define R_AARCH64_TLSLD_ADR_PAGE21            518
+#define R_AARCH64_TLSLD_ADD_LO12_NC           519
+#define R_AARCH64_TLSLD_MOVW_G1               520
+#define R_AARCH64_TLSLD_MOVW_G0_NC            521
+#define R_AARCH64_TLSLD_LD_PREL19             522
+#define R_AARCH64_TLSLD_MOVW_DTPREL_G2        523
+#define R_AARCH64_TLSLD_MOVW_DTPREL_G1        524
+#define R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC     525
+#define R_AARCH64_TLSLD_MOVW_DTPREL_G0        526
+#define R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC     527
+#define R_AARCH64_TLSLD_ADD_DTPREL_HI12       528
+#define R_AARCH64_TLSLD_ADD_DTPREL_LO12       529
+#define R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC    530
+#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12     531
+#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC  532
+#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12    533
+#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC 534
+#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12    535
+#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC 536
+#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12    537
+#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC 538
+/* initial exec TLS relocations */
+#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G1      539
+#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC   540
+#define R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21   541
+#define R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC 542
+#define R_AARCH64_TLSIE_LD_GOTTPREL_PREL19    543
+/* local exec TLS relocations */
+#define R_AARCH64_TLSLE_MOVW_TPREL_G2         544
+#define R_AARCH64_TLSLE_MOVW_TPREL_G1         545
+#define R_AARCH64_TLSLE_MOVW_TPREL_G1_NC      546
+#define R_AARCH64_TLSLE_MOVW_TPREL_G0         547
+#define R_AARCH64_TLSLE_MOVW_TPREL_G0_NC      548
+#define R_AARCH64_TLSLE_ADD_TPREL_HI12        549
+#define R_AARCH64_TLSLE_ADD_TPREL_LO12        550
+#define R_AARCH64_TLSLE_ADD_TPREL_LO12_NC     551
+#define R_AARCH64_TLSLE_LDST8_TPREL_LO12      552
+#define R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC   553
+#define R_AARCH64_TLSLE_LDST16_TPREL_LO12     554
+#define R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC  555
+#define R_AARCH64_TLSLE_LDST32_TPREL_LO12     556
+#define R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC  557
+#define R_AARCH64_TLSLE_LDST64_TPREL_LO12     558
+#define R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC  559
+/* Dynamic Relocations */
+#define R_AARCH64_COPY         1024
+#define R_AARCH64_GLOB_DAT     1025
+#define R_AARCH64_JUMP_SLOT    1026
+#define R_AARCH64_RELATIVE     1027
+#define R_AARCH64_TLS_DTPREL64 1028
+#define R_AARCH64_TLS_DTPMOD64 1029
+#define R_AARCH64_TLS_TPREL64  1030
+#define R_AARCH64_TLS_DTPREL32 1031
+#define R_AARCH64_TLS_DTPMOD32 1032
+#define R_AARCH64_TLS_TPREL32  1033
+
 /* s390 relocations defined by the ABIs */
 #define R_390_NONE		0	/* No reloc.  */
 #define R_390_8			1	/* Direct 8 bit.  */
Signature · 1 line
1.8.1
#2 lore raw

PATCH v3 2/3 tcg/aarch64: implement new TCG target for aarch64

In reply to Claudio Fontana

add preliminary support for TCG target aarch64.

---
 include/exec/exec-all.h  |    5 +-
 tcg/aarch64/tcg-target.c | 1159 ++++++++++++++++++++++++++++++++++++++++++++++
 tcg/aarch64/tcg-target.h |   99 ++++
 translate-all.c          |    2 +
 4 files changed, 1264 insertions(+), 1 deletion(-)
 create mode 100644 tcg/aarch64/tcg-target.c
 create mode 100644 tcg/aarch64/tcg-target.h
include/exec/exec-all.h +4 -1
index 6362074..5c31863 100644
@@ -128,7 +128,7 @@ static inline void tlb_flush(CPUArchState *env, int flush_global)
 
 #if defined(__arm__) || defined(_ARCH_PPC) \
     || defined(__x86_64__) || defined(__i386__) \
-    || defined(__sparc__) \
+    || defined(__sparc__) || defined(__aarch64__) \
     || defined(CONFIG_TCG_INTERPRETER)
 #define USE_DIRECT_JUMP
 #endif
@@ -230,6 +230,9 @@ static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
     *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
     /* no need to flush icache explicitly */
 }
+#elif defined(__aarch64__)
+void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
+#define tb_set_jmp_target1 aarch64_tb_set_jmp_target
 #elif defined(__arm__)
 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
tcg/aarch64/tcg-target.c new file +1159 -0

1,159 lines not on this page · raw message

tcg/aarch64/tcg-target.h new file +99 -0

99 lines not on this page · raw message

translate-all.c +2 -0
index 211be31..da6edae 100644
@@ -460,6 +460,8 @@ static inline PageDesc *page_find(tb_page_addr_t index)
 # define MAX_CODE_GEN_BUFFER_SIZE  (2ul * 1024 * 1024 * 1024)
 #elif defined(__sparc__)
 # define MAX_CODE_GEN_BUFFER_SIZE  (2ul * 1024 * 1024 * 1024)
+#elif defined(__aarch64__)
+# define MAX_CODE_GEN_BUFFER_SIZE  (128ul * 1024 * 1024)
 #elif defined(__arm__)
 # define MAX_CODE_GEN_BUFFER_SIZE  (16u * 1024 * 1024)
 #elif defined(__s390x__)
Signature · 1 line
1.8.1
#3 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] tcg/aarch64: implement new TCG target for aarch64

Quoted 8 lines · Claudio Fontana
tcg/aarch64/tcg-target.c new file +8 -0
+static inline void tcg_out_movi_aux(TCGContext *s,
+                                    TCGReg rd, uint64_t value)
+{
+    uint32_t half, base, movk = 0, shift = 0;
+
+    /* construct halfwords of the immediate with MOVZ/MOVK with LSL */
+    /* using MOVZ 0x52800000 | extended reg.. */
+    base = (value > 0xffffffff) ? 0xd2800000 : 0x52800000;

tcg/aarch64/tcg-target.c line 326

Quoted 10 lines
tcg/aarch64/tcg-target.c new file +10 -0
+
+    do {
+        int skip_zeros = ctz64(value) & (63 & -16);
+        value >>= skip_zeros;
+        shift += skip_zeros << 17;
+        half = value & 0xffff;
+        tcg_out32(s, base | movk | shift | half << 5 | rd);
+        movk = 0x20000000; /* morph next MOVZs into MOVKs */
+        value >>= 16;
+        shift += 16 << 17;

This is way more confusing than it needs to be. I don't think you should modify VALUE by shifting at all. If you don't do that then you don't need to make SHIFT loop carried, since we compute its exact correct value every time with the ctz.

Was the only bug in the code that I pasted the lack of the shift-by-17 when encoding SHIFT into the tcg_out32?

tcg/aarch64/tcg-target.c line 338

Quoted 9 lines
tcg/aarch64/tcg-target.c new file +9 -0
+static inline void tcg_out_movi(TCGContext *s, TCGType type,
+                                TCGReg rd, tcg_target_long value)
+{
+    if (type == TCG_TYPE_I64) {
+        tcg_out_movi_aux(s, rd, value);
+    } else {
+        tcg_out_movi_aux(s, rd, value & 0xffffffff);
+    }
+}

Any reason you're splitting out tcg_out_movi_aux to a separate function?

tcg/aarch64/tcg-target.c line 1078

Quoted 4 lines
tcg/aarch64/tcg-target.c new file +4 -0
+    tcg_regset_clear(s->reserved_regs);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_X18); /* platform register */

Reserve the frame pointer.

r~

#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] tcg/aarch64: implement new TCG target for aarch64

Quoted 25 lines · Richard Henderson
Quoted 8 lines · Claudio Fontana
tcg/aarch64/tcg-target.c new file +8 -0
+static inline void tcg_out_movi_aux(TCGContext *s,
+                                    TCGReg rd, uint64_t value)
+{
+    uint32_t half, base, movk = 0, shift = 0;
+
+    /* construct halfwords of the immediate with MOVZ/MOVK with LSL */
+    /* using MOVZ 0x52800000 | extended reg.. */
+    base = (value > 0xffffffff) ? 0xd2800000 : 0x52800000;

tcg/aarch64/tcg-target.c line 326

Quoted 10 lines
tcg/aarch64/tcg-target.c new file +10 -0
+
+    do {
+        int skip_zeros = ctz64(value) & (63 & -16);
+        value >>= skip_zeros;
+        shift += skip_zeros << 17;
+        half = value & 0xffff;
+        tcg_out32(s, base | movk | shift | half << 5 | rd);
+        movk = 0x20000000; /* morph next MOVZs into MOVKs */
+        value >>= 16;
+        shift += 16 << 17;

This is way more confusing than it needs to be. I don't think you should modify VALUE by shifting at all. If you don't do that then you don't need to make SHIFT loop carried, since we compute its exact correct value every time with the ctz.

Quoted 2 lines

Was the only bug in the code that I pasted the lack of the shift-by-17 when encoding SHIFT into the tcg_out32?

yes, you only forgot to encode the shift in the tcg_out32, the variation above was an attempt to make it easier to understand. I agree that the approach that avoids changing value in the right shift is more concise, I'll go back to that, adding a comment about how the function works.

Quoted 11 lines

tcg/aarch64/tcg-target.c line 338

Quoted 9 lines
tcg/aarch64/tcg-target.c new file +9 -0
+static inline void tcg_out_movi(TCGContext *s, TCGType type,
+                                TCGReg rd, tcg_target_long value)
+{
+    if (type == TCG_TYPE_I64) {
+        tcg_out_movi_aux(s, rd, value);
+    } else {
+        tcg_out_movi_aux(s, rd, value & 0xffffffff);
+    }
+}

Any reason you're splitting out tcg_out_movi_aux to a separate function?

tcg_out_movi is an interface with tcg, and as such the prototype is fixed. I'd rather work with a value that is unsigned, because of the right shift. Having a separate _aux function does that without the need for adding another local variable and another operation to understand in the function with the actual algorithm.

Quoted 7 lines

tcg/aarch64/tcg-target.c line 1078

Quoted 5 lines
tcg/aarch64/tcg-target.c new file +4 -0
+    tcg_regset_clear(s->reserved_regs);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_X18); /* platform register */

Reserve the frame pointer.

Ok.

Quoted 1 line

r~

Claudio

#5 lore raw

PATCH v3 3/3 configure: permit compilation on arm aarch64

In reply to Claudio Fontana

support compiling on aarch64.

---
 configure | 8 ++++++++
 1 file changed, 8 insertions(+)
configure +8 -0
index eb74510..f021bdd 100755
@@ -385,6 +385,8 @@ elif check_define __s390__ ; then
   fi
 elif check_define __arm__ ; then
   cpu="arm"
+elif check_define __aarch64__ ; then
+  cpu="aarch64"
 elif check_define __hppa__ ; then
   cpu="hppa"
 else
@@ -407,6 +409,9 @@ case "$cpu" in
   armv*b|armv*l|arm)
     cpu="arm"
   ;;
+  aarch64)
+    cpu="aarch64"
+  ;;
   hppa|parisc|parisc64)
     cpu="hppa"
   ;;
@@ -4127,6 +4132,9 @@ if test "$linux" = "yes" ; then
   s390x)
     linux_arch=s390
     ;;
+  aarch64)
+    linux_arch=arm64
+    ;;
   *)
     # For most CPUs the kernel architecture name and QEMU CPU name match.
     linux_arch="$cpu"
Signature · 1 line
1.8.1