blob: f888ccf1cbade16d4e80a95eebc6c50f471041ab [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ł Mazurek3a9ecbf2024-11-29 13:09:12 +010018pub mod hvc;
Michał Mazurekf08509a2025-01-24 11:39:24 +000019pub mod layout;
Michał Mazurek6cd065f2024-11-29 13:07:11 +010020pub mod linker;
Michał Mazurek5b294692024-11-29 13:05:46 +010021pub mod page_table;
Bartłomiej Grzesik86f108d2024-11-29 15:48:00 +010022pub mod platform;
23
Alice Wang81399f52023-05-26 14:23:43 +000024/// Reads a value from a system register.
25#[macro_export]
26macro_rules! read_sysreg {
27 ($sysreg:literal) => {{
28 let mut r: usize;
Alice Wang81399f52023-05-26 14:23:43 +000029 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000030 // SAFETY: Reading a system register does not affect memory.
Alice Wang81399f52023-05-26 14:23:43 +000031 unsafe {
32 core::arch::asm!(
33 concat!("mrs {}, ", $sysreg),
34 out(reg) r,
35 options(nomem, nostack, preserves_flags),
36 )
37 }
38 r
39 }};
40}
41
42/// Writes a value to a system register.
43///
44/// # Safety
45///
46/// Callers must ensure that side effects of updating the system register are properly handled.
47#[macro_export]
48macro_rules! write_sysreg {
49 ($sysreg:literal, $val:expr) => {{
50 let value: usize = $val;
51 core::arch::asm!(
52 concat!("msr ", $sysreg, ", {}"),
53 in(reg) value,
54 options(nomem, nostack, preserves_flags),
55 )
56 }};
57}
58
59/// Executes an instruction synchronization barrier.
60#[macro_export]
61macro_rules! isb {
62 () => {{
Alice Wang81399f52023-05-26 14:23:43 +000063 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000064 // SAFETY: memory barriers do not affect Rust's memory model.
Alice Wang81399f52023-05-26 14:23:43 +000065 unsafe {
66 core::arch::asm!("isb", options(nomem, nostack, preserves_flags));
67 }
68 }};
69}
70
71/// Executes a data synchronization barrier.
72#[macro_export]
73macro_rules! dsb {
74 ($option:literal) => {{
Alice Wang81399f52023-05-26 14:23:43 +000075 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +000076 // SAFETY: memory barriers do not affect Rust's memory model.
Alice Wang81399f52023-05-26 14:23:43 +000077 unsafe {
78 core::arch::asm!(concat!("dsb ", $option), options(nomem, nostack, preserves_flags));
79 }
80 }};
81}
82
Pierre-Clément Tosi8ab7c372024-10-30 20:46:04 +000083/// Executes a data cache operation.
84#[macro_export]
85macro_rules! dc {
86 ($option:literal, $addr:expr) => {{
87 let addr: usize = $addr;
88 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
89 // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects.
90 unsafe {
91 core::arch::asm!(
92 concat!("dc ", $option, ", {x}"),
93 x = in(reg) addr,
94 options(nomem, nostack, preserves_flags),
95 );
96 }
97 }};
98}
99
Alice Wang81399f52023-05-26 14:23:43 +0000100/// Invalidates cached leaf PTE entries by virtual address.
101#[macro_export]
102macro_rules! tlbi {
103 ($option:literal, $asid:expr, $addr:expr) => {{
104 let asid: usize = $asid;
105 let addr: usize = $addr;
Alice Wang81399f52023-05-26 14:23:43 +0000106 #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
Andrew Walbranc06e7342023-07-05 14:00:51 +0000107 // SAFETY: Invalidating the TLB doesn't affect Rust. When the address matches a
108 // block entry larger than the page size, all translations for the block are invalidated.
Alice Wang81399f52023-05-26 14:23:43 +0000109 unsafe {
110 core::arch::asm!(
111 concat!("tlbi ", $option, ", {x}"),
112 x = in(reg) (asid << 48) | (addr >> 12),
113 options(nomem, nostack, preserves_flags)
114 );
115 }
116 }};
117}
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100118
Pierre-Clément Tosi043dfb72024-10-30 21:17:10 +0000119/// STRB intrinsics.
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100120///
121/// See https://github.com/rust-lang/rust/issues/131894
122///
123/// # Safety
124///
125/// `dst` must be valid for writes.
Pierre-Clément Tosi043dfb72024-10-30 21:17:10 +0000126#[inline]
127pub unsafe fn strb(dst: *mut u8, src: u8) {
Pierre-Clément Tosi4ec3a932024-10-08 18:10:25 +0100128 // SAFETY: strb only modifies *dst, which must be valid for writes.
129 unsafe {
130 core::arch::asm!(
131 "strb {value:w}, [{ptr}]",
132 value = in(reg) src,
133 ptr = in(reg) dst,
134 options(preserves_flags),
135 );
136 }
137}
Pierre-Clément Tosi8ab7c372024-10-30 20:46:04 +0000138
139/// Reads the number of words in the smallest cache line of all the data caches and unified caches.
140#[inline]
141pub fn min_dcache_line_size() -> usize {
142 const DMINLINE_SHIFT: usize = 16;
143 const DMINLINE_MASK: usize = 0xf;
144 let ctr_el0 = read_sysreg!("ctr_el0");
145
146 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
147 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
148
149 1 << dminline
150}