Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
| 4 | //! Module for converting addresses between kernel virtual address space to physical address space |
| 5 | |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 6 | use core::ops::Range; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 7 | |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 8 | use alloc::sync::Arc; |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 9 | use spin::Mutex; |
| 10 | |
| 11 | use super::{ |
| 12 | page_pool::{Page, PagePool}, |
| 13 | MemoryAccessRights, Xlat, XlatError, |
| 14 | }; |
| 15 | |
| 16 | static mut KERNEL_SPACE_INSTANCE: Option<KernelSpace> = None; |
| 17 | |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 18 | #[derive(Clone)] |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 19 | pub struct KernelSpace { |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 20 | xlat: Arc<Mutex<Xlat>>, |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /// Kernel space memory mapping |
| 24 | /// |
| 25 | /// This object handles the translation tables of the kernel address space. The main goal is to |
| 26 | /// limit the kernel's access to the memory and only map the ranges which are necessary for |
| 27 | /// operation. |
| 28 | /// The current implementation uses identity mapping into the upper virtual address range, e.g. |
| 29 | /// PA = 0x0000_0001_2345_0000 -> VA = 0xffff_fff1_2345_0000. |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 30 | impl KernelSpace { |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 31 | pub const PAGE_SIZE: usize = Page::SIZE; |
| 32 | |
| 33 | /// Creates the kernel memory mapping instance. This should be called from the main core's init |
| 34 | /// code. |
| 35 | /// # Arguments |
| 36 | /// * page_pool: Page pool for allocation kernel translation tables |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 37 | pub fn new(page_pool: PagePool) -> Self { |
| 38 | Self { |
| 39 | xlat: Arc::new(Mutex::new(Xlat::new( |
| 40 | page_pool, |
| 41 | 0x0000_0000..0x10_0000_0000, |
| 42 | ))), |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Maps the code (RX) and data (RW) segments of the SPMC itself. |
| 47 | /// # Arguments |
| 48 | /// * code_range: (start, end) addresses of the code segment |
| 49 | /// * data_range: (start, end) addresses of the data segment |
| 50 | /// # Return value |
| 51 | /// * The result of the operation |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 52 | pub fn init( |
| 53 | &self, |
| 54 | code_range: Range<usize>, |
| 55 | data_range: Range<usize>, |
| 56 | ) -> Result<(), XlatError> { |
| 57 | let mut xlat = self.xlat.lock(); |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 58 | |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 59 | xlat.map_physical_address_range( |
| 60 | Some(code_range.start), |
| 61 | code_range.start, |
| 62 | code_range.len(), |
| 63 | MemoryAccessRights::RX | MemoryAccessRights::GLOBAL, |
| 64 | )?; |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 65 | |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 66 | xlat.map_physical_address_range( |
| 67 | Some(data_range.start), |
| 68 | data_range.start, |
| 69 | data_range.len(), |
| 70 | MemoryAccessRights::RW | MemoryAccessRights::GLOBAL, |
| 71 | )?; |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 72 | |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 73 | Ok(()) |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// Map memory range into the kernel address space |
| 77 | /// # Arguments |
| 78 | /// * pa: Physical address of the memory |
| 79 | /// * length: Length of the range in bytes |
| 80 | /// * access_right: Memory access rights |
| 81 | /// # Return value |
| 82 | /// * Virtual address of the mapped memory or error |
| 83 | pub fn map_memory( |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 84 | &self, |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 85 | pa: usize, |
| 86 | length: usize, |
| 87 | access_rights: MemoryAccessRights, |
| 88 | ) -> Result<usize, XlatError> { |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 89 | let lower_va = self.xlat.lock().map_physical_address_range( |
| 90 | Some(pa), |
| 91 | pa, |
| 92 | length, |
| 93 | access_rights | MemoryAccessRights::GLOBAL, |
| 94 | )?; |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 95 | |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 96 | Ok(Self::pa_to_kernel(lower_va as u64) as usize) |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// Unmap memory range from the kernel address space |
| 100 | /// # Arguments |
| 101 | /// * va: Virtual address of the memory |
| 102 | /// * length: Length of the range in bytes |
| 103 | /// # Return value |
| 104 | /// The result of the operation |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 105 | pub fn unmap_memory(&self, va: usize, length: usize) -> Result<(), XlatError> { |
| 106 | self.xlat |
| 107 | .lock() |
| 108 | .unmap_virtual_address_range(Self::kernel_to_pa(va as u64) as usize, length) |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /// Activate kernel address space mapping |
Imre Kis | 9a7440e | 2024-04-18 15:44:45 +0200 | [diff] [blame^] | 112 | pub fn activate(&self) { |
| 113 | self.xlat.lock().activate(0, super::TTBR::TTBR1_EL1); |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /// Rounds a value down to a kernel space page boundary |
| 117 | pub const fn round_down_to_page_size(size: usize) -> usize { |
| 118 | size & !(Self::PAGE_SIZE - 1) |
| 119 | } |
| 120 | |
| 121 | /// Rounds a value up to a kernel space page boundary |
| 122 | pub const fn round_up_to_page_size(size: usize) -> usize { |
| 123 | (size + Self::PAGE_SIZE - 1) & !(Self::PAGE_SIZE - 1) |
| 124 | } |
| 125 | |
Imre Kis | 49c08e3 | 2024-04-19 14:16:39 +0200 | [diff] [blame] | 126 | /// Returns the offset to the preceding page aligned address |
| 127 | pub const fn offset_in_page(address: usize) -> usize { |
| 128 | address & (Self::PAGE_SIZE - 1) |
| 129 | } |
| 130 | |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 131 | /// Kernel virtual address to physical address |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 132 | #[cfg(not(test))] |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 133 | pub const fn kernel_to_pa(kernel_address: u64) -> u64 { |
| 134 | kernel_address & 0x0000_000f_ffff_ffff |
| 135 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 136 | /// Physical address to kernel virtual address |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 137 | #[cfg(not(test))] |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 138 | pub const fn pa_to_kernel(pa: u64) -> u64 { |
| 139 | // TODO: make this consts assert_eq!(pa & 0xffff_fff0_0000_0000, 0); |
| 140 | pa | 0xffff_fff0_0000_0000 |
| 141 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 142 | |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 143 | // Do not use any mapping in test build |
| 144 | #[cfg(test)] |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 145 | pub const fn kernel_to_pa(kernel_address: u64) -> u64 { |
| 146 | kernel_address |
| 147 | } |
| 148 | |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 149 | #[cfg(test)] |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 150 | pub const fn pa_to_kernel(pa: u64) -> u64 { |
| 151 | pa |
| 152 | } |
| 153 | } |