Skip to content

Commit d7c0497

Browse files
authored
Added start in mips16 mode support, and unit test for it - on dev branch (#2089)
Co-authored-by: ZakDanger <[email protected]>
1 parent 7f6dcc7 commit d7c0497

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

qemu/target/mips/unicorn.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ MIPSCPU *cpu_mips_init(struct uc_struct *uc);
1919

2020
static void mips_set_pc(struct uc_struct *uc, uint64_t address)
2121
{
22-
((CPUMIPSState *)uc->cpu->env_ptr)->active_tc.PC = address;
22+
((CPUMIPSState *)uc->cpu->env_ptr)->active_tc.PC = address & ~(uint64_t )1ULL;
23+
if (address & 1) {
24+
((CPUMIPSState *)uc->cpu->env_ptr)->hflags |= MIPS_HFLAG_M16;
25+
} else {
26+
((CPUMIPSState *)uc->cpu->env_ptr)->hflags &= ~(MIPS_HFLAG_M16);
27+
}
2328
}
2429

2530
static uint64_t mips_get_pc(struct uc_struct *uc)
2631
{
27-
return ((CPUMIPSState *)uc->cpu->env_ptr)->active_tc.PC;
32+
return ((CPUMIPSState *)uc->cpu->env_ptr)->active_tc.PC |
33+
!!(((CPUMIPSState *)uc->cpu->env_ptr)->hflags & (MIPS_HFLAG_M16));
2834
}
2935

3036
static void mips_release(void *ctx)
@@ -128,7 +134,12 @@ uc_err reg_write(void *_env, int mode, unsigned int regid, const void *value,
128134
break;
129135
case UC_MIPS_REG_PC:
130136
CHECK_REG_TYPE(mipsreg_t);
131-
env->active_tc.PC = *(mipsreg_t *)value;
137+
env->active_tc.PC = *(mipsreg_t *)value & ~1ULL;
138+
if ((*(uint32_t *)value & 1)) {
139+
env->hflags |= MIPS_HFLAG_M16;
140+
} else {
141+
env->hflags &= ~(MIPS_HFLAG_M16);
142+
}
132143
*setpc = 1;
133144
break;
134145
case UC_MIPS_REG_CP0_CONFIG3:

tests/unit/test_mips.c

+26-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void test_mips_el_ori(void)
1717
char code[] = "\x56\x34\x21\x34"; // ori $at, $at, 0x3456;
1818
int r_r1 = 0x6789;
1919

20-
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_32 | UC_MODE_LITTLE_ENDIAN, code,
20+
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_MIPS32 | UC_MODE_LITTLE_ENDIAN, code,
2121
sizeof(code) - 1);
2222
OK(uc_reg_write(uc, UC_MIPS_REG_1, &r_r1));
2323

@@ -36,7 +36,7 @@ static void test_mips_eb_ori(void)
3636
char code[] = "\x34\x21\x34\x56"; // ori $at, $at, 0x3456;
3737
int r_r1 = 0x6789;
3838

39-
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_32 | UC_MODE_BIG_ENDIAN, code,
39+
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_MIPS32 | UC_MODE_BIG_ENDIAN, code,
4040
sizeof(code) - 1);
4141
OK(uc_reg_write(uc, UC_MIPS_REG_1, &r_r1));
4242

@@ -56,7 +56,7 @@ static void test_mips_stop_at_branch(void)
5656
"\x02\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00"; // j 0x8; nop;
5757
int r_pc = 0x0;
5858

59-
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_32 | UC_MODE_LITTLE_ENDIAN, code,
59+
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_MIPS32 | UC_MODE_LITTLE_ENDIAN, code,
6060
sizeof(code) - 1);
6161

6262
// Execute one instruction with branch delay slot.
@@ -78,7 +78,7 @@ static void test_mips_stop_at_delay_slot(void)
7878
"\x02\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00"; // j 0x8; nop;
7979
int r_pc = 0x0;
8080

81-
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_32 | UC_MODE_LITTLE_ENDIAN, code,
81+
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_MIPS32 | UC_MODE_LITTLE_ENDIAN, code,
8282
sizeof(code) - 1);
8383

8484
// Stop at the delay slot by design.
@@ -99,7 +99,7 @@ static void test_mips_lwx_exception_issue_1314(void)
9999
char code[] = "\x0a\xc8\x79\x7e"; // lwx $t9, $t9($s3)
100100
int reg;
101101

102-
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_32 | UC_MODE_LITTLE_ENDIAN, code,
102+
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_MIPS32 | UC_MODE_LITTLE_ENDIAN, code,
103103
sizeof(code) - 1);
104104
OK(uc_mem_map(uc, 0x10000, 0x4000, UC_PROT_ALL));
105105

@@ -126,10 +126,31 @@ static void test_mips_lwx_exception_issue_1314(void)
126126
OK(uc_close(uc));
127127
}
128128

129+
static void test_mips_mips16(void)
130+
{
131+
uc_engine *uc;
132+
char code[] = "\xC4\x6B\x49\xE3"; // li $v1, 0xC4; addu $v0, $v1, $v0
133+
int r_v0 = 0x6789;
134+
int mips16_lowbit = 1;
135+
136+
uc_common_setup(&uc, UC_ARCH_MIPS, UC_MODE_MIPS32 | UC_MODE_LITTLE_ENDIAN, code,
137+
sizeof(code) - 1);
138+
OK(uc_reg_write(uc, UC_MIPS_REG_V0, &r_v0));
139+
140+
OK(uc_emu_start(uc, code_start | mips16_lowbit, code_start + sizeof(code) - 1, 0, 0));
141+
142+
OK(uc_reg_read(uc, UC_MIPS_REG_V0, &r_v0));
143+
144+
TEST_CHECK(r_v0 == 0x684D);
145+
146+
OK(uc_close(uc));
147+
}
148+
129149
TEST_LIST = {
130150
{"test_mips_stop_at_branch", test_mips_stop_at_branch},
131151
{"test_mips_stop_at_delay_slot", test_mips_stop_at_delay_slot},
132152
{"test_mips_el_ori", test_mips_el_ori},
133153
{"test_mips_eb_ori", test_mips_eb_ori},
134154
{"test_mips_lwx_exception_issue_1314", test_mips_lwx_exception_issue_1314},
155+
{"test_mips_mips16", test_mips_mips16},
135156
{NULL, NULL}};

0 commit comments

Comments
 (0)