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

Implement RegId for Mips and MSP430 #38

Merged
merged 7 commits into from Jan 9, 2021

Conversation

starfleetcadet75
Copy link
Contributor

This PR addresses the missing arch implementations (MIPS32/64, MSP430, and PPC) for RegId mentioned in issue #29. It also adds the MIPS DSP register struct.

@daniel5151 daniel5151 self-requested a review January 8, 2021 23:16
Copy link
Owner

@daniel5151 daniel5151 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR!

There are a couple issues that need to be fixed before we can merge, but the core definitions seem fine.

I do have one big question - while I'm fairly confident that you've tested the Mips RegId (given that you contributed the initial Mips arch to gdbstub), have you actually tested the msp430 or Power PC definitions?

While it'd certainly be nice to close #29, the reason I opened the issue in the first place + implemented this somewhat janky-looking RegIdImpl approach was because I didn't feel comfortable adding definitions that haven't been tested-working in at least one project.

I've taken the same approach with every other architecture-specific piece of functionality in gdbstub. Case in point: the only arch I've actually added myself was armv4t - every other arch has been a community contribution!

While I appreciate the effort, if the msp430 and Power PC RegIds haven't actually been tested, then I'd want to omit them from this PR, and focus on just the Mips RegIds. I don't mind leaving #29 open until someone with more msp430 / Power PC experience comes along to fill in the gaps, since the issue doesn't really affect the broader codebase / end user experience all that much.

src/arch/mips/reg/id.rs Outdated Show resolved Hide resolved
@@ -22,6 +22,8 @@ pub struct MipsCoreRegs<U> {
pub cp0: MipsCp0Regs<U>,
/// FPU registers
pub fpu: MipsFpuRegs<U>,
/// DSP registers
pub dsp: MipsDspRegs<U>,
Copy link
Owner

@daniel5151 daniel5151 Jan 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, darn, adding this field will be a semver breaking change :'(

Unfortunately, this is a pretty glaring problem I've run into before, and is tracked under #12. In this case, since the DPS registers are optional, there really aught to be some way to specifying whether or not the DSP registers should be used + toggling the corresponding target.xml feature when active (see https://sourceware.org/gdb/current/onlinedocs/gdb/MIPS-Features.html#MIPS-Features)

At some point I really should sit down and hammer out a proper fix for #12, though I don't know when that'll be exactly. As such, the following workaround will have to suffice:

You'll need to create a new regs struct + associated Arch impls:

struct MipsCoreRegsWithDps<U> {
    core: MipsCoreRegs<U>, 
    dsp: MipsDspRegs<U>
}

enum MipsWithDsp {}

impl Arch for MipsWithDsp {
    type Usize = u32;
    type Registers = reg::MipsCoreRegsWithDps<u32>;
    type RegId = RegIdImpl;
    type RegId = reg::id::MipsRegId;

    fn target_description_xml() -> Option<&'static str> {
        Some(r#"<target version="1.0"><architecture>mips</architecture><feature name="org.gnu.gdb.mips.dsp"></feature></target>"#)
    }
}

// similarly for Mips64WithDsp

Feel free to pick a different/better suffix than WithDps.


So yeah, not pretty, but that's just how the code is structured right now unfortunately ☹️

That said, these changes would not require a semver breaking change, as they would only add new types without adding any old ones. As such, I'd be able to publish gdbstub 0.4.3 with these definitions included, which I'm sure you'd like 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(leaving this comment unresolved so that I can easily find it in the future, if I ever need to reference it again)

src/arch/mips/mod.rs Outdated Show resolved Hide resolved
src/arch/msp430/mod.rs Outdated Show resolved Hide resolved
src/arch/ppc/mod.rs Outdated Show resolved Hide resolved
src/arch/msp430/reg/id.rs Outdated Show resolved Hide resolved
src/arch/msp430/reg/id.rs Show resolved Hide resolved
src/arch/ppc/reg/id.rs Outdated Show resolved Hide resolved
Copy link
Owner

@daniel5151 daniel5151 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, good stuff!

Just a few more nits, but aside from that this looks pretty much good to merge.

Given that you've removed the PowerPC work, could you update the PR title?

src/arch/mips/mod.rs Outdated Show resolved Hide resolved
src/arch/mips/reg/mips.rs Outdated Show resolved Hide resolved
Comment on lines -102 to +140
// ensure bytes.chunks_exact(ptrsize) won't panic
if bytes.len() % ptrsize != 0 {
// Ensure bytes contains enough data for all 72 registers
if bytes.len() < ptrsize * 72 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two checks aren't equivalent.

For example, if bytes.len() == ptrsize * 72 + 1, then chunks_exact will panic.

You can leave this condition in, but do not remove the other one.

// Ensure bytes contains enough data for all 79 registers of target-width
// and the dspctl register which is always 4 bytes
let ptrsize = core::mem::size_of::<U>();
if bytes.len() < (ptrsize * 79) + 4 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment - this check is not sufficient to ensure that chunks_exact won't panic.

You can leave it in, but you still need the other check as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue I'm seeing with if bytes.len() % ptrsize != 0 { is when U is a u64 there will be 79 registers of size 64 and one register of size 32 (dspctl), thus this check will fail. It looks like chunks_exact actually doesn't panic when there are extra elements, it should still split the bytes up until the end with a possible remainder of 4 bytes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, yep, you're right.

Well, in any case, I decided to see if the optimizer was smart enough to get rid of any panics, and yep, sure enough it seems to have done the trick:

https://godbolt.org/z/8zsKf9

So yeah, this check can be left as-is.

src/arch/mips/reg/mod.rs Outdated Show resolved Hide resolved
src/arch/mips/mod.rs Outdated Show resolved Hide resolved
src/arch/mips/mod.rs Outdated Show resolved Hide resolved
@starfleetcadet75
Copy link
Contributor Author

I've separated the MIPS DSP struct from the core registers, which made me realize interestingly enough that dspctl is the only MIPS register that doesn't change size when going from 32 to 64 bit. I have tested the Mips defs with my emulator, however I don't have anything to test PPC with so I went ahead and pulled the PPC stuff out for now. I do have some experience with msp430, unfortunately it looks like GDB doesn't have a nice XML description for it so I linked what seems to be the closest thing in the gdb source.

@starfleetcadet75 starfleetcadet75 changed the title Implement RegId for Mips, MSP430, and PPC Implement RegId for Mips and MSP430 Jan 9, 2021
@daniel5151 daniel5151 self-requested a review January 9, 2021 23:22
Copy link
Owner

@daniel5151 daniel5151 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Thanks for the contribution!

I'll go ahead and publish gdbstub 0.4.3 with these changes shortly.

@daniel5151 daniel5151 merged commit 13b48ff into daniel5151:master Jan 9, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants