Skip to content

Add methods for MMIO reads and writes #1

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

Merged
merged 25 commits into from
Mar 12, 2025
Merged

Add methods for MMIO reads and writes #1

merged 25 commits into from
Mar 12, 2025

Conversation

qwandor
Copy link
Collaborator

@qwandor qwandor commented Mar 5, 2025

This adds a bunch of different methods and macros to allow safe MMIO reads and writes of individual fields. In particular:

  • get and split methods on UniqueMmioPointer<[T]> and UniqueMmioPointer<[T; _]> respectively, to go from a pointer to a slice or field to pointers to the individual elements.
  • A field! macro to go from a pointer to a struct to a pointer to an individual field. (And field_shared! to do the same for SharedMmioPointer.) If [RFC] field projections v2 rust-lang/rfcs#3735 gets stabilised then we could implement that instead of needing a macro.
  • OwnedMmioPointer is renamed to UniqueMmioPointer, and a new type SharedMmioPointer is added. SharedMmioPointer is to UniqueMmioPointer as &T is to &mut T. A SharedMmioPointer can be created from a UniqueMmioPointer and can be freely cloned, but fewer operations are possible on it. UniqueMmioPointer also derefs to SharedMmioPointer.
  • A variety of wrapper types for fields, indicating whether they can safely be read (with or without side-effects) or written. It is important to distinguish whether reading an MMIO register may have side-effects (e.g. clearing an interrupt status, or popping from a queue) or not, because a read that has side-effects should be treated like a write and only be allowed from a unique pointer (passed via &mut) whereas a read without side-effects can safely be done via a shared pointer (passed via '&'), e.g. simultaneously from multiple threads.
  • For pointers to fields with the appropriate wrapper type, safe read and write methods to perform MMIO reads and writes. On most platforms this is implemented via read_volatile and write_volatile on the underlying pointer, but on aarch64 inline assembly is used instead to work around Volatile reads and writes on aarch64 sometimes generate instructions not suitable for MMIO in protected VMs rust-lang/rust#131894. To do this safely we require that the underlying type implement the relevant traits from zerocopy.

qwandor added 22 commits March 10, 2025 03:17
volatile_read and volatile_write doesn't work properly in a protected
VM.
Any fields not marked with such a wrapper can only be read or written
via unsafe methods.
These may be useful for testing, as someone may want to derive these
traits for a struct containing a mixture of read-only and write-only
fields.
This may be needed both for tests to construct fakes, and for unsafely
reading or writing fields directly.
This has methods which don't require uniqueness, and so may be cloned.
UniqueMmioPointer derefs to SharedMmioPointer.
These may safely be read from a SharedMmioPointer rather than a
UniqueMmioPointer.
This removes the bound on T implementing the corresponding trait.
@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@qwandor qwandor merged commit 15bc075 into main Mar 12, 2025
6 checks passed
@qwandor qwandor deleted the zerocopy branch March 12, 2025 09:33
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.

1 participant