blob: 199423be8056c8767d153b960b0f26201be4bb10 [file] [log] [blame]
Alice Wang81399f52023-05-26 14:23:43 +00001// Copyright 2023, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Wrappers of assembly calls.
16
Michał Mazurek626347c2024-11-29 13:08:27 +010017pub mod dbm;
Michał Mazurek747c08d2024-11-29 13:26:22 +010018pub mod exceptions;
Michał Mazurek3a9ecbf2024-11-29 13:09:12 +010019pub mod hvc;
Michał Mazurekf08509a2025-01-24 11:39:24 +000020pub mod layout;
Michał Mazurek6cd065f2024-11-29 13:07:11 +010021pub mod linker;
Michał Mazurek5b294692024-11-29 13:05:46 +010022pub mod page_table;
Bartłomiej Grzesik86f108d2024-11-29 15:48:00 +010023pub mod platform;
24
Alice Wang81399f52023-05-26 14:23:43 +000025/// Reads a value from a system register.
26#[macro_export]
27macro_rules! read_sysreg {
28 ($sysreg:literal) => {{
29 let mut r: usize;
Alice Wang81399f52023-05-26 14:23:43 +000030 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000031 // SAFETY: Reading a system register does not affect memory.
Alice Wang81399f52023-05-26 14:23:43 +000032 unsafe {
33 core::arch::asm!(
34 concat!("mrs {}, ", $sysreg),
35 out(reg) r,
36 options(nomem, nostack, preserves_flags),
37 )
38 }
39 r
40 }};
41}
42
43/// Writes a value to a system register.
44///
45/// # Safety
46///
47/// Callers must ensure that side effects of updating the system register are properly handled.
48#[macro_export]
49macro_rules! write_sysreg {
50 ($sysreg:literal, $val:expr) => {{
51 let value: usize = $val;
52 core::arch::asm!(
53 concat!("msr ", $sysreg, ", {}"),
54 in(reg) value,
55 options(nomem, nostack, preserves_flags),
56 )
57 }};
58}
59
60/// Executes an instruction synchronization barrier.
61#[macro_export]
62macro_rules! isb {
63 () => {{
Alice Wang81399f52023-05-26 14:23:43 +000064 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000065 // SAFETY: memory barriers do not affect Rust's memory model.
Alice Wang81399f52023-05-26 14:23:43 +000066 unsafe {
67 core::arch::asm!("isb", options(nomem, nostack, preserves_flags));
68 }
69 }};
70}
71
72/// Executes a data synchronization barrier.
73#[macro_export]
74macro_rules! dsb {
75 ($option:literal) => {{
Alice Wang81399f52023-05-26 14:23:43 +000076 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000077 // SAFETY: memory barriers do not affect Rust's memory model.
Alice Wang81399f52023-05-26 14:23:43 +000078 unsafe {
79 core::arch::asm!(concat!("dsb ", $option), options(nomem, nostack, preserves_flags));
80 }
81 }};
82}
83
Pierre-Clément Tosi8ab7c372024-10-30 20:46:04 +000084/// Executes a data cache operation.
85#[macro_export]
86macro_rules! dc {
87 ($option:literal, $addr:expr) => {{
88 let addr: usize = $addr;
89 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
90 // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects.
91 unsafe {
92 core::arch::asm!(
93 concat!("dc ", $option, ", {x}"),
94 x = in(reg) addr,
95 options(nomem, nostack, preserves_flags),
96 );
97 }
98 }};
99}
100
Alice Wang81399f52023-05-26 14:23:43 +0000101/// Invalidates cached leaf PTE entries by virtual address.
102#[macro_export]
103macro_rules! tlbi {
104 ($option:literal, $asid:expr, $addr:expr) => {{
105 let asid: usize = $asid;
106 let addr: usize = $addr;
Alice Wang81399f52023-05-26 14:23:43 +0000107 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +0000108 // SAFETY: Invalidating the TLB doesn't affect Rust. When the address matches a
109 // block entry larger than the page size, all translations for the block are invalidated.
Alice Wang81399f52023-05-26 14:23:43 +0000110 unsafe {
111 core::arch::asm!(
112 concat!("tlbi ", $option, ", {x}"),
113 x = in(reg) (asid << 48) | (addr >> 12),
114 options(nomem, nostack, preserves_flags)
115 );
116 }
117 }};
118}
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100119
Pierre-Clément Tosi043dfb72024-10-30 21:17:10 +0000120/// STRB intrinsics.
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100121///
122/// See https://github.com/rust-lang/rust/issues/131894
123///
124/// # Safety
125///
126/// `dst` must be valid for writes.
Pierre-Clément Tosi043dfb72024-10-30 21:17:10 +0000127#[inline]
128pub unsafe fn strb(dst: *mut u8, src: u8) {
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100129 // SAFETY: strb only modifies *dst, which must be valid for writes.
130 unsafe {
131 core::arch::asm!(
132 "strb {value:w}, [{ptr}]",
133 value = in(reg) src,
134 ptr = in(reg) dst,
135 options(preserves_flags),
136 );
137 }
138}
Pierre-Clément Tosi8ab7c372024-10-30 20:46:04 +0000139
140/// Reads the number of words in the smallest cache line of all the data caches and unified caches.
141#[inline]
142pub fn min_dcache_line_size() -> usize {
143 const DMINLINE_SHIFT: usize = 16;
144 const DMINLINE_MASK: usize = 0xf;
145 let ctr_el0 = read_sysreg!("ctr_el0");
146
147 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
148 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
149
150 1 << dminline
151}