-
Notifications
You must be signed in to change notification settings - Fork 182
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
Support x86_64-unknown-linux-none
#424
Comments
I think #401 is relevant here. We need a way of doing raw syscalls for this. Ideally, it would be handled by the |
I think if we want to support this target, we should just write the inline assembly to invoke the I agre with @sunfishcode that supporting a general mechanism for invoking any syscall on any Linux CPU architecture is very complicated (vDSO, some syscalls may or may not return, But the situation for const NR_GETRANDOM: u32 = 318;
pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>], flags: u32) -> isize {
unsafe {
let ret;
asm!(
"syscall",
inlateout("rax") NR_GETRANDOM as isize => ret,
in("rdi") buf.as_mut_ptr(),
in("rsi") buf.len(),
in("rdx") flags,
lateout("rcx") _,
lateout("r11") _,
options(nostack, preserves_flags)
);
ret
}
} |
@josephlr We probably can do it for other target arches as well (on x86 we can just use |
In the PR, @morr0ne said that x86_64-unknown-linux-none won't have libc, so it's likely that many crates will use rustix for I/O on If it were just getrandom it may seem ok to hard-code a little bit of |
The *-linux-none targets heavily rely on something like rustix to work; at the moment its basically guaranteed that rustix will already be in the dependency tree.
The x86_64-unknown-linux-none target is just the first of other *-linux-none targets. I plan to implement at the very least aarch64 and riscv64gc targets. Using asm here would mean having to implement such syscall manually for each potential target. |
This is why I would strongly prefer to have raw syscalls in the If raw syscalls will not be added to |
@josephlr wrote:
I agree with this suggestion, and this is what I'm planning to do in ring for all getrandom shouldn't add rustix as a dependency for this. At the most, we could have a feature flag to use rustix instead of the inline assembly support. I suspect there are reason why some rustix users would prefer this, e.g. if they are using rustix to port to a non-Linux OS. But there's a separate issue for rustix already, to track this. If getrandom is going to have the inline assembly, it should use the inline assembly for all |
I would rather not have getrandom depend on |
I don't think it's likely. The crate is a largely automatic conversion of the Linux headers and has almost no issues in its tracker despite being used actively (mostly through I think that maintaining inline assembly for syscalls and syscall numbers (rant: I wish Linux had stable syscall numbers across targets...) for various targets should be outside of the |
What are the practical problems with using rustix for just *-linux-none, and leaving the rest of getrandom as-is? |
@josephlr wrote:
That would support the target when the FYI: In ring, for
At least for me (ring), I don't want to (in some sense, can't) have divergent codepaths for the |
I'm not familiar with ring. What are the practical considerations around using different codepaths for the
If we only used rustix for |
I will reverse the question: what are the practical problems with exposing raw syscall functions in |
I am using |
When adding the target I specifically did not want to add a minimum version because I felt it was too soon considering I was the only user of the target. Seeing there is interest in adding support for the *-linux-none target(s) in getrandom then it is reasonable to bump the minimum kernel version to support the required syscall. |
Regarding the rustix/linux-raw-sys argument I believe we are missing some in-between crate. I think a proper solution would be to have a "linux-syscall" crate that depends on linux-raw-sys and provides access to linux syscalls; rustix could then depend on both this new crate and libc to provide a unified api like it does today. Projects like getrandom that cannot or do not want to depend on rustix can depend directly on the new "linux-syscall" crate to talk to the kernel. |
This is why I created sunfishcode/linux-raw-sys#116.
I don't think it makes much sense to introduce a separate crate. You wrote yourself that this hypothetical crate would need to depend on Drawing parallels with the classic |
The linux-raw-sys crate cannot provide a syscall function because such a function does not exist. Currently the api is generated from the kernel userspace api in the same way *-sys crates are generated from C headers. The kernel does not provide any function to call syscalls but just the appropriate definition to create a wrapper manually. Such a wrapper would be outside the scope of a *-sys crate |
I disagree. For most practical intents and purposes syscalls are functions, they just have a "weird" ABI, which can not be properly described in C headers (well, I guess technically you could do it? but historically people don't do it for various reasons). The items defined in the Linux headers are intended to by used with syscalls, so, arguably, it makes sense to have them in one crate. That said, I am fine with having a separate syscall crate when compared to the current status quo. I guess such crate could be relatively stable, while |
Perhaps I didn't explain myself properly. What I mean to say is that syscalls are not something we can automatically generate but need to be manually implemented. linux-raw-sys is fully generated code.
That is somewhat the idea however we really need @sunfishcode input on this |
I think it would be possible for the
However, I definitely recognize that such auto-generation is quite complex, compared to what Zooming out, I think it would be fine for this crate to depend on Until that point, having our own syscall wrapper seems like the least bad option. It will work with our without rustix, and we can always move to |
I agree with this. |
One question is, how will we test this? My understanding is that I propose the following:
The implication here is we'd need to remove all the libc usage from the Ideally, we'd change Cargo.toml so that libc isn't a dependency for
(The use_file implementation uses |
That testing strategy sounds good to me. I think that provided we don't explicitly check for
The main other uses of
The ideal approach might be to have the main looping functionality of |
My opinion is that we should add new |
Does anybody want to take a stab at the inline assembly for invoking the syscall? |
It looks like it doesn't vary by architecture; it's always 4; https://github.com/torvalds/linux/blob/2df0193e62cf887f373995fb8a91068562784adc/include/uapi/asm-generic/errno-base.h#L8. Though it would be good to find a definitive promise of that.
We could change the signature of - sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
+ sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> Result<usize, Error> where any short read, including now usize::try_from(res).map_err(|_| res.unsigned_abs()) Regardless, I think it is manageable. |
Why would we want to keep the current code path for using |
nvm. Giving it a go now. |
OK, there is now a sketch of this, including building for |
Personally, I would be fine with unconditionally using raw syscalls for calling |
Out of curiosity, I was trying to figure out if it would work to only use either
Looking at the supported Android/Linux targets:
Given this, it's probably not possible to remove use of |
The target was added in rust-lang/rust#125023 and is available in the latest Nightly builds. Use
cargo +nightly build --target=x86_64-unknown-linux-none -Zbuild-std
.The
libc
crate does NOT support this target, solibc::syscall
andlibc::getrandom
cannot be used on this target.I am planning to support this target in ring soon.
The text was updated successfully, but these errors were encountered: