Skip to content

Commit b91d3e2

Browse files
author
Dil4rd
committed
mips support
1 parent a6758d1 commit b91d3e2

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

linux-user/mips/cpu_loop.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "internal.h"
2626
#include "fpu_helper.h"
2727

28+
/* MIPS_PATCH */
29+
#include "qemuafl/common.h"
30+
2831
# ifdef TARGET_ABI_MIPSO32
2932
# define MIPS_SYSCALL_NUMBER_UNUSED -1
3033
static const int8_t mips_syscall_args[] = {

qemuafl/api.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,16 @@ struct arm64_regs {
137137

138138
};
139139

140+
/* MIPS_PATCH */
141+
#if defined(TARGET_MIPS)
142+
struct mips_regs {
143+
uint32_t r0, at, v0, v1, a0, a1, a2, a3, t0, t1, t2, t3, t4, t5, t6, t7, s0,
144+
s1, s2, s3, s4, s5, s6, s7, t8, t9, k0, k1, gp, sp, fp, ra;
145+
uint32_t HI[4];
146+
uint32_t LO[4];
147+
uint32_t PC;
148+
fpr_t fpr[32];
149+
};
150+
#endif
151+
140152
#endif

qemuafl/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
#define api_regs arm64_regs
5151
#elif defined(TARGET_ARM)
5252
#define api_regs arm_regs
53+
/* MIPS_PATCH */
54+
#elif defined(TARGET_MIPS)
55+
#define api_regs mips_regs
5356
#else
5457
struct generic_api_regs { int v; };
5558
#define api_regs generic_api_regs
@@ -134,7 +137,7 @@ void afl_float_compcov_log_80(target_ulong cur_loc, floatx80 arg1,
134137
abi_ulong afl_get_brk(void);
135138
abi_ulong afl_set_brk(abi_ulong new_brk);
136139

137-
#if defined(TARGET_X86_64) || defined(TARGET_I386) || defined(TARGET_AARCH64) || defined(TARGET_ARM)
140+
#if defined(TARGET_X86_64) || defined(TARGET_I386) || defined(TARGET_AARCH64) || defined(TARGET_ARM) || defined(TARGET_MIPS)
138141
void afl_save_regs(struct api_regs* regs, CPUArchState* env);
139142
void afl_restore_regs(struct api_regs* regs, CPUArchState* env);
140143
#else

qemuafl/qasan-qemu.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ extern __thread struct shadow_stack qasan_shadow_stack;
8383
#define BP_GET(env) ((env)->aarch64 ? (env)->xregs[29] : (env)->regs[11])
8484
#define SP_GET(env) ((env)->aarch64 ? (env)->xregs[31] : (env)->regs[13])
8585

86+
/* MIPS_PATCH */
87+
#elif defined(TARGET_MIPS)
88+
89+
#define PC_GET(env) ((env)->active_tc.PC)
90+
#define BP_GET(env) ((env)->active_tc.gpr[29])
91+
#define SP_GET(env) ((env)->active_tc.gpr[30])
92+
8693
#else
8794
#error "Target not supported by asan-giovese"
8895
#endif

target/mips/translate.c

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@
3939
#include "fpu_helper.h"
4040
#include "translate.h"
4141

42+
/* MIPS_PATCH */
43+
#include "qemuafl/cpu-translate.h"
44+
45+
/* MIPS_PATCH */
46+
#define AFL_QEMU_TARGET_MIPS_SNIPPET \
47+
if (is_persistent) { \
48+
if (ctx->base.pc_next == afl_persistent_addr) { \
49+
gen_helper_afl_persistent_routine(cpu_env); \
50+
\
51+
if (afl_persistent_ret_addr == 0 && !persistent_exits) { \
52+
tcg_gen_movi_tl(cpu_gpr[31], afl_persistent_addr); \
53+
} \
54+
\
55+
if (!persistent_save_gpr) afl_gen_tcg_plain_call(&afl_persistent_loop); \
56+
\
57+
} else if (afl_persistent_ret_addr && \
58+
ctx->base.pc_next == afl_persistent_ret_addr) { \
59+
gen_goto_tb(ctx, 0, afl_persistent_addr); \
60+
} \
61+
}
62+
4263
enum {
4364
/* indirect opcode tables */
4465
OPC_SPECIAL = (0x00 << 26),
@@ -2274,6 +2295,140 @@ static const char * const mxuregnames[] = {
22742295
};
22752296
#endif
22762297

2298+
/* MIPS_PATCH */
2299+
void afl_save_regs(struct api_regs *r, CPUArchState *env) {
2300+
int i = 0;
2301+
int j = 0;
2302+
/* GP registers saving */
2303+
r->r0 = env->active_tc.gpr[0];
2304+
r->at = env->active_tc.gpr[1];
2305+
r->v0 = env->active_tc.gpr[2];
2306+
r->v1 = env->active_tc.gpr[3];
2307+
r->a0 = env->active_tc.gpr[4];
2308+
r->a1 = env->active_tc.gpr[5];
2309+
r->a2 = env->active_tc.gpr[6];
2310+
r->a3 = env->active_tc.gpr[7];
2311+
r->t0 = env->active_tc.gpr[8];
2312+
r->t1 = env->active_tc.gpr[9];
2313+
r->t2 = env->active_tc.gpr[10];
2314+
r->t3 = env->active_tc.gpr[11];
2315+
r->t4 = env->active_tc.gpr[12];
2316+
r->t5 = env->active_tc.gpr[13];
2317+
r->t6 = env->active_tc.gpr[14];
2318+
r->t7 = env->active_tc.gpr[15];
2319+
r->s0 = env->active_tc.gpr[16];
2320+
r->s1 = env->active_tc.gpr[17];
2321+
r->s2 = env->active_tc.gpr[18];
2322+
r->s3 = env->active_tc.gpr[19];
2323+
r->s4 = env->active_tc.gpr[20];
2324+
r->s5 = env->active_tc.gpr[21];
2325+
r->s6 = env->active_tc.gpr[22];
2326+
r->s7 = env->active_tc.gpr[23];
2327+
r->t8 = env->active_tc.gpr[24];
2328+
r->t9 = env->active_tc.gpr[25];
2329+
r->k0 = env->active_tc.gpr[26];
2330+
r->k1 = env->active_tc.gpr[27];
2331+
r->gp = env->active_tc.gpr[28];
2332+
r->sp = env->active_tc.gpr[29];
2333+
r->fp = env->active_tc.gpr[30];
2334+
r->ra = env->active_tc.gpr[31];
2335+
r->PC = env->active_tc.PC;
2336+
for (i = 0; i < 4; i++) {
2337+
r->HI[i] = env->active_tc.HI[i];
2338+
r->LO[i] = env->active_tc.LO[i];
2339+
}
2340+
/* FP registers saving */
2341+
for (i = 0; i < 32; i++) {
2342+
r->fpr[i].fd = env->active_fpu.fpr[i].fd;
2343+
for (j = 0; j < 2; j++) {
2344+
r->fpr[i].fs[j] = env->active_fpu.fpr[i].fs[j];
2345+
}
2346+
r->fpr[i].d = env->active_fpu.fpr[i].d;
2347+
for (j = 0; j < 2; j++) {
2348+
r->fpr[i].w[j] = env->active_fpu.fpr[i].w[j];
2349+
}
2350+
for (j = 0; j < MSA_WRLEN / 8; j++) {
2351+
r->fpr[i].wr.b[j] = env->active_fpu.fpr[i].wr.b[j];
2352+
}
2353+
for (j = 0; j < MSA_WRLEN / 16; j++) {
2354+
r->fpr[i].wr.h[j] = env->active_fpu.fpr[i].wr.h[j];
2355+
}
2356+
for (j = 0; j < MSA_WRLEN / 32; j++) {
2357+
r->fpr[i].wr.w[j] = env->active_fpu.fpr[i].wr.w[j];
2358+
}
2359+
for (j = 0; j < MSA_WRLEN / 64; j++) {
2360+
r->fpr[i].wr.d[j] = env->active_fpu.fpr[i].wr.d[j];
2361+
}
2362+
}
2363+
}
2364+
2365+
/* MIPS_PATCH */
2366+
void afl_restore_regs(struct api_regs *r, CPUArchState *env) {
2367+
int i = 0;
2368+
int j = 0;
2369+
/* GP registers restoring */
2370+
env->active_tc.gpr[0] = r->r0;
2371+
env->active_tc.gpr[1] = r->at;
2372+
env->active_tc.gpr[2] = r->v0;
2373+
env->active_tc.gpr[3] = r->v1;
2374+
env->active_tc.gpr[4] = r->a0;
2375+
env->active_tc.gpr[5] = r->a1;
2376+
env->active_tc.gpr[6] = r->a2;
2377+
env->active_tc.gpr[7] = r->a3;
2378+
env->active_tc.gpr[8] = r->t0;
2379+
env->active_tc.gpr[9] = r->t1;
2380+
env->active_tc.gpr[10] = r->t2;
2381+
env->active_tc.gpr[11] = r->t3;
2382+
env->active_tc.gpr[12] = r->t4;
2383+
env->active_tc.gpr[13] = r->t5;
2384+
env->active_tc.gpr[14] = r->t6;
2385+
env->active_tc.gpr[15] = r->t7;
2386+
env->active_tc.gpr[16] = r->s0;
2387+
env->active_tc.gpr[17] = r->s1;
2388+
env->active_tc.gpr[18] = r->s2;
2389+
env->active_tc.gpr[19] = r->s3;
2390+
env->active_tc.gpr[20] = r->s4;
2391+
env->active_tc.gpr[21] = r->s5;
2392+
env->active_tc.gpr[22] = r->s6;
2393+
env->active_tc.gpr[23] = r->s7;
2394+
env->active_tc.gpr[24] = r->t8;
2395+
env->active_tc.gpr[25] = r->t9;
2396+
env->active_tc.gpr[26] = r->k0;
2397+
env->active_tc.gpr[27] = r->k1;
2398+
env->active_tc.gpr[28] = r->gp;
2399+
env->active_tc.gpr[29] = r->sp;
2400+
env->active_tc.gpr[30] = r->fp;
2401+
env->active_tc.gpr[31] = r->ra;
2402+
env->active_tc.PC = r->PC;
2403+
for (i = 0; i < 4; i++) {
2404+
env->active_tc.HI[i] = r->HI[i];
2405+
env->active_tc.LO[i] = r->LO[i];
2406+
}
2407+
/* FP registers restoring */
2408+
for (i = 0; i < 32; i++) {
2409+
env->active_fpu.fpr[i].fd = r->fpr[i].fd;
2410+
for (j = 0; j < 2; j++) {
2411+
env->active_fpu.fpr[i].fs[j] = r->fpr[i].fs[j];
2412+
}
2413+
env->active_fpu.fpr[i].d = r->fpr[i].d;
2414+
for (j = 0; j < 2; j++) {
2415+
env->active_fpu.fpr[i].w[j] = r->fpr[i].w[j];
2416+
}
2417+
for (j = 0; j < MSA_WRLEN / 8; j++) {
2418+
env->active_fpu.fpr[i].wr.b[j] = r->fpr[i].wr.b[j];
2419+
}
2420+
for (j = 0; j < MSA_WRLEN / 16; j++) {
2421+
env->active_fpu.fpr[i].wr.h[j] = r->fpr[i].wr.h[j];
2422+
}
2423+
for (j = 0; j < MSA_WRLEN / 32; j++) {
2424+
env->active_fpu.fpr[i].wr.w[j] = r->fpr[i].wr.w[j];
2425+
}
2426+
for (j = 0; j < MSA_WRLEN / 64; j++) {
2427+
env->active_fpu.fpr[i].wr.d[j] = r->fpr[i].wr.d[j];
2428+
}
2429+
}
2430+
}
2431+
22772432
/* General purpose registers moves. */
22782433
void gen_load_gpr(TCGv t, int reg)
22792434
{
@@ -29090,6 +29245,9 @@ static void mips_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
2909029245
int insn_bytes;
2909129246
int is_slot;
2909229247

29248+
/* MIPS_PATCH */
29249+
AFL_QEMU_TARGET_MIPS_SNIPPET
29250+
2909329251
is_slot = ctx->hflags & MIPS_HFLAG_BMASK;
2909429252
if (ctx->insn_flags & ISA_NANOMIPS32) {
2909529253
ctx->opcode = translator_lduw(env, ctx->base.pc_next);

0 commit comments

Comments
 (0)