Skip to content
This repository was archived by the owner on Feb 3, 2020. It is now read-only.

Commit da3db6a

Browse files
add ARM support for libtcg
Signed-off-by: chaojixx <[email protected]>
1 parent 740de4d commit da3db6a

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ message(STATUS "WITH_GUEST: ${WITH_GUEST}")
7575
if(WITH_GUEST MATCHES "i386")
7676
set(TARGET_LONG_BITS "32")
7777
set(TARGET_INSN_START_EXTRA_WORDS "1")
78+
set(TARGET_I386 "1")
7879
elseif(WITH_GUEST MATCHES "x86_64")
7980
set(TARGET_LONG_BITS "64")
8081
set(TARGET_INSN_START_EXTRA_WORDS "1")
8182
set(TARGET_X86_64 "1")
83+
elseif(WITH_GUEST MATCHES "arm")
84+
set(TARGET_LONG_BITS "32")
85+
set(TARGET_INSN_START_EXTRA_WORDS "2")
86+
set(TARGET_ARM "1")
8287
else()
8388
message(FATAL_ERROR "Incorrect target ${WITH_GUEST}")
8489
endif()

include/tcg/tcg-llvm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ class TCGLLVMTranslator {
140140
// in order to simplify instruction insertion.
141141
llvm::Instruction *m_noop;
142142
llvm::Value *m_eip;
143+
#if defined(TARGET_I386) || defined(TARGET_X86_64)
143144
llvm::Value *m_ccop;
144-
145+
#endif
145146
static unsigned m_eip_last_gep_index;
146147

147148
typedef llvm::DenseMap<std::pair<unsigned, unsigned>, llvm::Instruction *> GepMap;

include/tcg/tcg.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ extern "C" {
4444

4545
#define QEMU_BUILD_BUG_ON(x)
4646

47+
#if defined(TARGET_ARM)
48+
/* The ARM MMU allows 1k pages. */
49+
/* ??? Linux doesn't actually use these, and they're deprecated in recent
50+
architecture revisions. Maybe a configure option to disable them. */
51+
#define TARGET_PAGE_BITS 10
52+
#else
4753
#define TARGET_PAGE_BITS 12
54+
#endif
55+
4856
#define TARGET_PAGE_SIZE (1 << TARGET_PAGE_BITS)
4957
#define TARGET_PAGE_MASK ~(TARGET_PAGE_SIZE - 1)
5058

@@ -120,11 +128,17 @@ typedef struct TranslationBlock TranslationBlock;
120128
/* typedef struct ZMMReg ZMMReg;
121129
122130
typedef union XMMReg XMMReg;*/
123-
131+
#if defined(TARGET_ARM)
132+
#define CPUArchState struct CPUARMState
133+
#define CPUState struct CPUARMState
134+
struct CPUARMState;
135+
typedef struct CPUARMState CPUARMState;
136+
#else
124137
#define CPUArchState struct CPUX86State
125138
#define CPUState struct CPUX86State
126139
struct CPUX86State;
127140
typedef struct CPUX86State CPUX86State;
141+
#endif
128142

129143
extern FILE *logfile;
130144

@@ -849,9 +863,12 @@ struct TCGContext {
849863
uintptr_t env_ptr;
850864
unsigned env_offset_eip;
851865
unsigned env_sizeof_eip;
866+
#ifndef TARGET_ARM
852867
unsigned env_offset_ccop;
853868
unsigned env_sizeof_ccop;
854869
unsigned env_offset_df;
870+
#endif
871+
855872
unsigned env_offset_tlb[3]; // Max 3 mem index
856873

857874
unsigned tlbe_size;

src/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ endif (WITH_SYMBEX_MP)
4141

4242
if (TARGET_X86_64)
4343
set (COMMON_FLAGS "${COMMON_FLAGS} -DTARGET_X86_64")
44-
endif (TARGET_X86_64)
44+
elseif (TARGET_I386)
45+
set (COMMON_FLAGS "${COMMON_FLAGS} -DTARGET_I386")
46+
elseif (TARGET_ARM)
47+
set (COMMON_FLAGS "${COMMON_FLAGS} -DTARGET_ARM")
48+
endif ()
4549

4650
if (TARGET_LONG_BITS)
4751
set (COMMON_FLAGS "${COMMON_FLAGS} -DTARGET_LONG_BITS=${TARGET_LONG_BITS}")

src/tcg-llvm.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ typedef uint32_t target_ulong;
6868
typedef uint64_t target_ulong;
6969
#endif
7070

71+
#if defined(TARGET_I386) || defined(TARGET_X86_64)
7172
// XXX: hack
7273
#define CC_OP_DYNAMIC 0
74+
#endif
7375

7476
extern "C" {
7577
// TODO: get rid of this global var
@@ -101,8 +103,9 @@ TCGLLVMTranslator::TCGLLVMTranslator(const std::string &bitcodeLibraryPath, std:
101103
m_cpuType = NULL;
102104
m_cpuState = NULL;
103105
m_eip = NULL;
106+
#if defined(TARGET_I386) || defined(TARGET_X86_64)
104107
m_ccop = NULL;
105-
108+
#endif
106109
initializeNativeCpuState();
107110
initializeHelpers();
108111
}
@@ -188,8 +191,13 @@ uint64_t TCGLLVMTranslator::toInteger(llvm::Value *v) const {
188191
#ifdef CONFIG_SYMBEX
189192

190193
void TCGLLVMTranslator::initializeNativeCpuState() {
194+
#ifdef TARGET_ARM
195+
m_cpuType = m_module->getTypeByName("struct.CPUARMState");
196+
assert(m_cpuType && "Could not find CPUARMState in LLVM bitcode");
197+
#else
191198
m_cpuType = m_module->getTypeByName("struct.CPUX86State");
192199
assert(m_cpuType && "Could not find CPUX86State in LLVM bitcode");
200+
#endif
193201
}
194202

195203
void TCGLLVMTranslator::initializeHelpers() {
@@ -426,8 +434,9 @@ void TCGLLVMTranslator::loadNativeCpuState(Function *f) {
426434
m_noop = m_builder.Insert(add);
427435

428436
m_eip = generateCpuStatePtr(m_tcgContext->env_offset_eip, m_tcgContext->env_sizeof_eip);
437+
#if defined(TARGET_I386) || defined(TARGET_X86_64)
429438
m_ccop = generateCpuStatePtr(m_tcgContext->env_offset_ccop, m_tcgContext->env_sizeof_ccop);
430-
439+
#endif
431440
if (m_eip_last_gep_index == 0) {
432441
SmallVector<Value *, 3> gepElements;
433442
bool ok = getCpuFieldGepIndexes(m_tcgContext->env_offset_eip, sizeof(target_ulong), gepElements);
@@ -1167,23 +1176,25 @@ Function *TCGLLVMTranslator::generateCode(TCGContext *s, TranslationBlock *tb) {
11671176
case INDEX_op_insn_start: {
11681177
assert(TARGET_INSN_START_WORDS == 2);
11691178
uint64_t curpc = op->args[0] - tb->cs_base;
1179+
#if defined(TARGET_I386) || defined(TARGET_X86_64)
11701180
uint64_t cc_op = op->args[1];
1171-
1181+
#endif
11721182
Value *valueToStore = handleSymbolicPcAssignment(ConstantInt::get(wordType(), curpc));
11731183

11741184
TCGArg args[3];
11751185
args[0] = 0; // Unused
11761186
args[1] = temp_arg(&m_tcgContext->temps[0]);
11771187
args[2] = m_tcgContext->env_offset_eip;
11781188
generateQemuCpuStore(args, m_tcgContext->env_sizeof_eip * 8, valueToStore);
1179-
1189+
#if defined(TARGET_I386) || defined(TARGET_X86_64)
11801190
if (cc_op != CC_OP_DYNAMIC) {
11811191
args[0] = 0; // Unused
11821192
args[1] = temp_arg(&m_tcgContext->temps[0]);
11831193
args[2] = m_tcgContext->env_offset_ccop;
11841194
valueToStore = ConstantInt::get(wordType(m_tcgContext->env_sizeof_ccop * 8), cc_op);
11851195
generateQemuCpuStore(args, m_tcgContext->env_sizeof_ccop * 8, valueToStore);
11861196
}
1197+
#endif
11871198
} break;
11881199
#endif
11891200

0 commit comments

Comments
 (0)