Skip to content

Conversation

Tropix126
Copy link
Contributor

@Tropix126 Tropix126 commented Aug 28, 2025

This PR adds standard library support for the VEX V5 Brain (armv7a-vex-v5 target). It is more-or-less an updated version of the library-side work done in #131530.

This was a joint effort between me, @lewisfm, @max-niederman, @Gavin-Niederman and several other members of the vexide project.

Background

VEXos is a fairly unconventional operating system, with user code running in a restricted enviornment with regards to I/O capabilities and whatnot. As such, several OS-dependent APIs are unsupported or have partial support (such as std::net, std::process, and most of std::thread). A more comprehensive list of what does or doesn't work is outlined in the updated target documentation. Despite these limitations, we believe that libstd support on this target still has value to users, especially given the popular use of this hardware for educational purposes. For some previous discussion on this matter, see this comment.

SDK Linkage

VEXos doesn't really ship with an official libc or POSIX-style platform API (and though it does port newlib, these are stubbed on top of the underlying SDK). Instead, VEX provides their own SDK for calling platform APIs. Their official SDK is kept proprietary (with public headers), though open-source implementations exist. Following the precedent of the armv6k-nintendo-3ds team's work in #95897, we've opted not to directly link libstd to any SDK with the expectation that users will provide their own - either through vex-sdk-build (which will download the official SDK from VEX), vex-sdk-jumptable (a compatible, open-source reimplementation), or potentially through linking the PROS kernel. The vex-sdk crate used in the VEXos PAL provides libc-style FFI bindings for any compatible system library, so any of these options should work fine. A functional demo project using vex-sdk-build can be found here.

Future Work

This PR implements virtually everything we are currently able to implement given the current capabilities of the platform. The exception to this is file directory enumeration, though the implementation of that is sufficiently gross enough to drive us away from supporting this officially.

Additionally, I have a working branch implementing the panic_unwind runtime for this target, which is something that would be nice to see in the future, though given the volume of compiler changes i've deemed it out-of-scope for this PR.

@rustbot
Copy link
Collaborator

rustbot commented Aug 28, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-tidy Area: The tidy tool S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 28, 2025

Some changes occurred in src/doc/rustc/src/platform-support

cc @Noratrieb

These commits modify the library/Cargo.lock file. Unintentional changes to library/Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

These commits modify compiler targets.
(See the Target Tier Policy.)

The list of allowed third-party dependencies may have been modified! You must ensure that any new dependencies have compatible licenses before merging.

cc @davidtwco, @wesleywiser

@tgross35
Copy link
Contributor

I think the limitations here seem fine, and from a quick skim nothing jumps out to me as problematic. Nominating to make sure the team doesn't have any concerns.

@rustbot label +I-libs-nominated

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Aug 28, 2025
@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 30, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 30, 2025
@Tropix126
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 30, 2025
@tgross35
Copy link
Contributor

Thanks for the updates here, I'll take a more thorough look through after the libs meeting (next Wednesday).

@tgross35 tgross35 added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 30, 2025
@bors
Copy link
Collaborator

bors commented Sep 3, 2025

☔ The latest upstream changes (presumably #146160) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2025

Some changes occurred in src/tools/cargo

cc @ehuss

@rustbot

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Sep 20, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment on lines +19 to +29
while let Some((out_byte, new_buf)) = buf.split_first_mut() {
buf = new_buf;

let byte = unsafe { vex_sdk::vexSerialReadChar(STDIO_CHANNEL) };
if byte < 0 {
break;
}

*out_byte = byte as u8;
count += 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be simplified as for out_byte in buf rather than using the split API and reassigning.

File { size: u64 },
}

pub struct ReadDir(!);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could reexport from unsupported, and just move your comment from fn readdir to the reexport for now.

Copy link
Contributor Author

@Tropix126 Tropix126 Sep 23, 2025

Choose a reason for hiding this comment

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

I can't do that for reasons similar to the FilePermissions situation:

  • ReadDir must impl Iterator<Item = io::Result<DirEntry>>.
  • DirEntry must return FileAttr/FileType, which do have proper implementations in this case (though not for directories).

I tried this a while back and ran into this issue; I'm pretty sure the current code re-exports everything it possibly can from unsupported at the moment. Maybe I could have DirEntry just store ! rather than an actual path and just return that here? Not sure.

Comment on lines +519 to +526
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
use crate::fs::File;

let mut reader = File::open(from)?;
let mut writer = File::create(to)?;

io::copy(&mut reader, &mut writer)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an implementation in common that does this, but also verifies it's a file and sets permissions. Would that work here?

pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {

Copy link
Contributor Author

@Tropix126 Tropix126 Sep 23, 2025

Choose a reason for hiding this comment

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

VEXos will return unsupported for writer.set_permissions(perm)?; (we can't support changing file permissions after a file has been opened), meaning the copy function in common will always fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Should the file vs. dir check be included, or will the reads handle failing if from is a directory?

Comment on lines +528 to +535
fn map_fresult(fresult: vex_sdk::FRESULT) -> io::Result<()> {
// VEX uses a derivative of FatFs (Xilinx's xilffs library) for filesystem operations.
match fresult {
vex_sdk::FRESULT::FR_OK => Ok(()),
vex_sdk::FRESULT::FR_DISK_ERR => Err(io::Error::new(
io::ErrorKind::Uncategorized,
"internal function reported an unrecoverable hard error",
)),
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use io::const_error! for errors known ahead of time if possible, saves the allocation. Also applies quite a few other places in this module.

Comment on lines +241 to +257
if *create_new {
if vex_sdk::vexFileStatus(path.as_ptr()) != 0 {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
"File exists",
));
}
} else if !*create {
if vex_sdk::vexFileStatus(path.as_ptr()) == 0 {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"No such file or directory",
));
}
}

vex_sdk::vexFileOpenWrite(path.as_ptr())
Copy link
Contributor

Choose a reason for hiding this comment

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

The status checks aren't required for the correctness of vexFileOpenWrite are they? It's a convenience, but we probably shouldn't be doing this check: it's an extra syscall that the user may not need, plus a bit of TOCTU. If VEX doesn't have a more detailed error from the file APIs (or errno), it would be better to return a generic error and let the user do this check themselves (via fs::metadata) if they need more details.

Copy link
Contributor Author

@Tropix126 Tropix126 Sep 23, 2025

Choose a reason for hiding this comment

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

The checks are required for correctness here. The create/create_new flags are kind of "smoke and mirrors" in this implementation. Since we don't have anything like an oflag system or global errno, ensuring that files already exist or are created with create/create_new is verified by libstd rather than the OS itself. If those status checks weren't there, we could violate the contract requiring create_new to disallow existing files of the same name or !create requiring a file to already be there.

As for the overhead of vexFileStatus - I think that it should be negligable here, since it would just be a simple lookup rather than a full syscall since most of the filesystem is implemented in "userspace" (though I'm cautious to use that word here, since the distinction is blurry on this target).

};

#[derive(Debug)]
struct FileDesc(*mut vex_sdk::FIL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a real pointer to something that the OS will dereference? Or is it more like a Unix file descriptor (int) / Windows handle where the OS does some validity checking and lookup? Would be worth a comment.

Copy link
Contributor Author

@Tropix126 Tropix126 Sep 23, 2025

Choose a reason for hiding this comment

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

This is a real pointer - there is no unix-like file descriptor table exposed to us. I believe it points to a FatFs file object structure under the hood. I'll add a comment, though the SDK intentionally keeps this type opaque (FIL is just c_void), so actually dereferencing it from libstd or treating it as anything other than opaque would be a bad idea since there's no stability guarantees regarding size/layout of FIL in firmware.

Comment on lines +54 to +55
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FilePermissions {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly curious, why is an empty struct used here rather than unsupported?

Copy link
Contributor Author

@Tropix126 Tropix126 Sep 23, 2025

Choose a reason for hiding this comment

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

In the sys api, File::perm must infallibly return an instance of FilePermissions. This is also why I can't re-export this type from unsupported, because it stores ! due to the fact that FileAttr is verified to be never-initialized as well (and therefore it can get away with just returning ! rather than the real type).

Comment on lines +345 to +347
let len = buf.len() as _;
let buf_ptr = buf.as_mut_ptr();
let read = unsafe { vex_sdk::vexFileRead(buf_ptr.cast(), 1, len, self.fd.0) };
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let len = buf.len() as _;
let buf_ptr = buf.as_mut_ptr();
let read = unsafe { vex_sdk::vexFileRead(buf_ptr.cast(), 1, len, self.fd.0) };
let len = buf.len() as u32;
let buf_ptr = buf.as_mut_ptr();
let read = unsafe { vex_sdk::vexFileRead(buf_ptr.cast::<c_char>(), 1, len, self.fd.0) };

Just explicit for a bit more readability

Comment on lines +370 to +373
let len = buf.len();
let buf_ptr = buf.as_ptr();
let written =
unsafe { vex_sdk::vexFileWrite(buf_ptr.cast_mut().cast(), 1, len as _, self.fd.0) };
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto

Suggested change
let len = buf.len();
let buf_ptr = buf.as_ptr();
let written =
unsafe { vex_sdk::vexFileWrite(buf_ptr.cast_mut().cast(), 1, len as _, self.fd.0) };
let len = buf.len() as u32;
let buf_ptr = buf.as_ptr();
let written =
unsafe { vex_sdk::vexFileWrite(buf_ptr.cast_mut().cast::<c_char>(), 1, len, self.fd.0) };

Comment on lines +436 to +442
// `vexFileSeek` does not support seeking with negative offset, meaning
// we have to calculate the offset from the end of the file ourselves.
map_fresult(vex_sdk::vexFileSeek(
self.fd.0,
try_convert_offset(self.file_attr()?.size() as i64 + offset)?,
SEEK_SET,
))?
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to seek to the end, tell, then seek to an offset from there? I'm speculating about the inner workings here but if vexFileSize reads the size from disk, it may not be the same as the size as seen from the open (potentially unflushed) buffer.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tidy Area: The tidy tool S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants