Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Try to align the native code to the next 8 byte boundary #899

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

Closed
wants to merge 2 commits into from

Conversation

filmor
Copy link
Member

@filmor filmor commented Jun 26, 2019

What does this implement/fix? Explain your changes.

Aligns the ARM code to the next 8 byte boundary.

Does this close any currently open issues?

Maybe #898

NativeCodePage = mapper.MapWriteable(codeLength + 7);

// Round to 8 byte boundary
var start = new IntPtr(((NativeCodePage.ToInt64() + 7) >> 3) << 3);
Copy link
Member

Choose a reason for hiding this comment

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

>> 3) << 3) can be replaced with & ~0x7L
IMHO, even better to write a generic align function (and unit test it)

Marshal.Copy(NativeCode.Active.Code, 0, NativeCodePage, codeLength);

// Request 7 bytes more than required such that we can align to an 8 byte boundary on ARM
NativeCodePage = mapper.MapWriteable(codeLength + 7);
Copy link
Contributor

Choose a reason for hiding this comment

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

mmap will return minimum a full page, and pages are typically 4096-byte-aligned (certainly more than 8-byte aligned). Add some logging to make sure, but I don't expect your changes are actually doing anything.

After the bx lr you might need a nop? I don't know this ISA myself, but it's not uncommon in architectures with branch prediction (i.e. basically all of them) that it'll unconditionally run an opcode right after a branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://community.arm.com/developer/ip-products/processors/f/cortex-m-forum/7154/alignment-in-arm

Looks like you need 16-byte-aligned if it's 64-bit ARM. So your Return1 is not properly aligned.

Copy link
Member Author

Choose a reason for hiding this comment

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

The older RPis are 32bit, though, so this (8 byte alignment) can't be the current issue.

@benoithudson
Copy link
Contributor

You mentioned compiling on the raspberry pi was slow. You can compile the C# dll on any platform -- as long as you set the #defines properly, the generated code is compatible. It's just the platform you run on that has to match your #defines. We generally build on windows for all three platforms (windows/mac/linux), I've done it on mac as well.

@filmor filmor closed this Aug 2, 2019
@filmor filmor deleted the align-native-code branch January 9, 2023 09:44
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.

3 participants