Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't see a way for MIPS to change the current ISA in unicorn. #2086

Closed
ZakDanger opened this issue Jan 18, 2025 · 4 comments
Closed

I can't see a way for MIPS to change the current ISA in unicorn. #2086

ZakDanger opened this issue Jan 18, 2025 · 4 comments

Comments

@ZakDanger
Copy link
Contributor

MIPS supports various 16bit or variable sized instructions formats in addition to the normal 32bit instructions.

This is pretty much the same deal as ARM supporting THUMB instructions.
For MIPS the lowest address bit of PC is even used for change between modes, just like between ARM and THUMB.

I cannot however find any way to perform this change when using unicorn with MIPS.
Perhaps I am missing something?

In "qemu/target/mips/cpu.c" lin 28 you can see how setting the PC is used to set the M16 (mips16 bit mode) flag.

static void mips_cpu_set_pc(CPUState *cs, vaddr value)
{
    MIPSCPU *cpu = MIPS_CPU(cs);
    CPUMIPSState *env = &cpu->env;

    env->active_tc.PC = value & ~(target_ulong)1;
    if (value & 1) {
        env->hflags |= MIPS_HFLAG_M16;
    } else {
        env->hflags &= ~(MIPS_HFLAG_M16);
    }
}

However the code in "qemu/target/mips/unicorn.c" only does this:

static void mips_set_pc(struct uc_struct *uc, uint64_t address)
{
    ((CPUMIPSState *)uc->cpu->env_ptr)->active_tc.PC = address;
}

I propose to change it to:

static void mips_set_pc(struct uc_struct *uc, uint64_t address)
{
    ((CPUMIPSState *)uc->cpu->env_ptr)->active_tc.PC = address & ~(uint64_t )1ULL;
    if (address & 1) {
        ((CPUMIPSState *)uc->cpu->env_ptr)->hflags |= MIPS_HFLAG_M16;
    } else {
        ((CPUMIPSState *)uc->cpu->env_ptr)->hflags &= ~(MIPS_HFLAG_M16);
    }
}

This would make it similar to unicorns "set pc" function for arm which is:

static void arm_set_pc(struct uc_struct *uc, uint64_t address)
{
    ((CPUARMState *)uc->cpu->env_ptr)->pc = address;
    ((CPUARMState *)uc->cpu->env_ptr)->regs[15] = address & ~1;
    ((CPUARMState *)uc->cpu->env_ptr)->thumb = address & 1;
}
@wtdcode
Copy link
Member

wtdcode commented Jan 18, 2025

Ah yeah, good catch. Would you like to contribute? It would be also much appreciated if you could also attach a unit test to tests/unit/test_mips.c

@ZakDanger
Copy link
Contributor Author

Added PR for this, but perhaps I need to do it on dev branch?

@wtdcode
Copy link
Member

wtdcode commented Jan 18, 2025

Added PR for this, but perhaps I need to do it on dev branch?

Yes, you shall PR to dev branch.

@wtdcode
Copy link
Member

wtdcode commented Jan 19, 2025

Closing, thanks for your contributions!

@wtdcode wtdcode closed this as completed Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants