Skip to content

Add a map_addr method for transforming a register address#80

Open
jnkr-ifx wants to merge 1 commit intoInfineon:mainfrom
jnkr-ifx:map-addr
Open

Add a map_addr method for transforming a register address#80
jnkr-ifx wants to merge 1 commit intoInfineon:mainfrom
jnkr-ifx:map-addr

Conversation

@jnkr-ifx
Copy link
Contributor

@jnkr-ifx jnkr-ifx commented Feb 2, 2026

On many processors implementing TrustZone, bit 28 of the address space is used to indicate whether a memory access is secure or non-secure, meaning drivers often need to bitwise-OR a security bit against the register address.

In order to support this with svd2pac, this commit adds a map_addr function to registers and clusters allowing a driver to override the register's address:

/// Creates a new register by mapping the addresss of this register. This is useful on platforms where
/// some bits of a transaction address are used to indicate flags (such as a TrustZone security attribute).
///
/// # Safety
/// The address must be non-null, as well as valid and properly aligned for the read and write operations
/// performed on the resulting register instance.
#[must_use]
unsafe fn map_addr(&self, f: impl FnOnce(usize) -> usize) -> &Self;

The function is implemented as a trait method for two reasons:

  • To avoid naming collisions in case a cluster has a register named map_addr.
  • So that drivers can build safe abstractions on top of map_addr, like:
/// Helper trait for applying a security attribute to a register address.
pub trait WithSecurity {
    /// Applies a security attribute to the address referenced by `self`.
    fn with_security(self, security: impl Security) -> Self;
}

impl<T: pac::MapAddr> WithSecurity for &T {
    fn with_security(self, security: impl Security) -> Self {
        unsafe { self.map_addr(|addr| security.map_addr(addr)) }
    }
}

The function signature intentionally mirrors std's map_addr function on pointer types.

On TrustZone devices, bit 28 of the address space is used to indicate
whether a memory access is secure or non-secure, meaning drivers often
need to bitwise-OR a security bit against the register address.

In order to support this with svd2pac, this commit adds a `map_addr`
function to registers and clusters allowing a driver to override the
register's address.

The function is implemented as a trait method for two reasons:

- To avoid naming collisions in case a cluster has a register named
  `map_addr`.
- So that drivers can build safe abstractions on top of `map_addr`, like:

```rust
/// Helper trait for applying a security attribute to a register address.
pub trait WithSecurity {
    /// Applies a security attribute to the address referenced by `self`.
    fn with_security(self, security: impl Security) -> Self;
}

impl<T: pac::MapAddr> WithSecurity for &T {
    fn with_security(self, security: impl Security) -> Self {
        unsafe { self.map_addr(|addr| security.map_addr(addr)) }
    }
}
```

The function signature intentionally mirrors [std's `map_addr`][map_addr]
function on pointer types.

[map_addr]: https://doc.rust-lang.org/std/primitive.pointer.html#method.map_addr-1
@pellico pellico added the enhancement New feature or request label Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants