blob: 05b556616234aee0a097a7808d0134e6ed2ea001 [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;
Bartłomiej Grzesik3d7fccf2025-01-30 15:10:01 +000024pub mod uart;
Bartłomiej Grzesik86f108d2024-11-29 15:48:00 +010025
Alice Wang81399f52023-05-26 14:23:43 +000026/// Reads a value from a system register.
27#[macro_export]
28macro_rules! read_sysreg {
29 ($sysreg:literal) => {{
30 let mut r: usize;
Alice Wang81399f52023-05-26 14:23:43 +000031 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000032 // SAFETY: Reading a system register does not affect memory.
Alice Wang81399f52023-05-26 14:23:43 +000033 unsafe {
34 core::arch::asm!(
35 concat!("mrs {}, ", $sysreg),
36 out(reg) r,
37 options(nomem, nostack, preserves_flags),
38 )
39 }
40 r
41 }};
42}
43
44/// Writes a value to a system register.
45///
46/// # Safety
47///
48/// Callers must ensure that side effects of updating the system register are properly handled.
49#[macro_export]
50macro_rules! write_sysreg {
51 ($sysreg:literal, $val:expr) => {{
52 let value: usize = $val;
53 core::arch::asm!(
54 concat!("msr ", $sysreg, ", {}"),
55 in(reg) value,
56 options(nomem, nostack, preserves_flags),
57 )
58 }};
59}
60
61/// Executes an instruction synchronization barrier.
62#[macro_export]
63macro_rules! isb {
64 () => {{
Alice Wang81399f52023-05-26 14:23:43 +000065 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000066 // SAFETY: memory barriers do not affect Rust's memory model.
Alice Wang81399f52023-05-26 14:23:43 +000067 unsafe {
68 core::arch::asm!("isb", options(nomem, nostack, preserves_flags));
69 }
70 }};
71}
72
73/// Executes a data synchronization barrier.
74#[macro_export]
75macro_rules! dsb {
76 ($option:literal) => {{
Alice Wang81399f52023-05-26 14:23:43 +000077 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000078 // SAFETY: memory barriers do not affect Rust's memory model.
Alice Wang81399f52023-05-26 14:23:43 +000079 unsafe {
80 core::arch::asm!(concat!("dsb ", $option), options(nomem, nostack, preserves_flags));
81 }
82 }};
83}
84
Pierre-Clément Tosi8ab7c372024-10-30 20:46:04 +000085/// Executes a data cache operation.
86#[macro_export]
87macro_rules! dc {
88 ($option:literal, $addr:expr) => {{
89 let addr: usize = $addr;
90 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
91 // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects.
92 unsafe {
93 core::arch::asm!(
94 concat!("dc ", $option, ", {x}"),
95 x = in(reg) addr,
96 options(nomem, nostack, preserves_flags),
97 );
98 }
99 }};
100}
101
Alice Wang81399f52023-05-26 14:23:43 +0000102/// Invalidates cached leaf PTE entries by virtual address.
103#[macro_export]
104macro_rules! tlbi {
105 ($option:literal, $asid:expr, $addr:expr) => {{
106 let asid: usize = $asid;
107 let addr: usize = $addr;
Alice Wang81399f52023-05-26 14:23:43 +0000108 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +0000109 // SAFETY: Invalidating the TLB doesn't affect Rust. When the address matches a
110 // block entry larger than the page size, all translations for the block are invalidated.
Alice Wang81399f52023-05-26 14:23:43 +0000111 unsafe {
112 core::arch::asm!(
113 concat!("tlbi ", $option, ", {x}"),
114 x = in(reg) (asid << 48) | (addr >> 12),
115 options(nomem, nostack, preserves_flags)
116 );
117 }
118 }};
119}
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100120
Pierre-Clément Tosi043dfb72024-10-30 21:17:10 +0000121/// STRB intrinsics.
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100122///
123/// See https://github.com/rust-lang/rust/issues/131894
124///
125/// # Safety
126///
127/// `dst` must be valid for writes.
Pierre-Clément Tosi043dfb72024-10-30 21:17:10 +0000128#[inline]
129pub unsafe fn strb(dst: *mut u8, src: u8) {
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100130 // SAFETY: strb only modifies *dst, which must be valid for writes.
131 unsafe {
132 core::arch::asm!(
133 "strb {value:w}, [{ptr}]",
134 value = in(reg) src,
135 ptr = in(reg) dst,
136 options(preserves_flags),
137 );
138 }
139}
Pierre-Clément Tosi8ab7c372024-10-30 20:46:04 +0000140
141/// Reads the number of words in the smallest cache line of all the data caches and unified caches.
142#[inline]
143pub fn min_dcache_line_size() -> usize {
144 const DMINLINE_SHIFT: usize = 16;
145 const DMINLINE_MASK: usize = 0xf;
146 let ctr_el0 = read_sysreg!("ctr_el0");
147
148 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
149 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
150
151 1 << dminline
152}