|
| 1 | +// Copyright (C) 2021-2023 The Aero Project Developers. |
| 2 | +// |
| 3 | +// This file is part of The Aero Project. |
| 4 | +// |
| 5 | +// Aero is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Aero is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Aero. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +use core::alloc::Layout; |
| 19 | +use core::ops::{Deref, DerefMut}; |
| 20 | + |
| 21 | +use crate::mem::paging::VirtAddr; |
| 22 | +use crate::utils::LinkerSymbol; |
| 23 | + |
| 24 | +use super::io; |
| 25 | + |
| 26 | +extern "C" { |
| 27 | + static __cpu_local_start: LinkerSymbol<u8>; |
| 28 | + static __cpu_local_end: LinkerSymbol<u8>; |
| 29 | +} |
| 30 | + |
| 31 | +#[repr(C)] |
| 32 | +pub struct CpuLocal<T>(T); |
| 33 | + |
| 34 | +impl<T> CpuLocal<T> { |
| 35 | + pub const fn new(val: T) -> Self { |
| 36 | + Self(val) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn addr(&self) -> VirtAddr { |
| 40 | + let val: u64; |
| 41 | + |
| 42 | + unsafe { |
| 43 | + // gs:[0] -> SELF_PTR |
| 44 | + asm!( |
| 45 | + "mov {}, qword ptr gs:[0]", |
| 46 | + lateout(reg) val, |
| 47 | + options(nostack, preserves_flags, pure, readonly), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + let self_addr = VirtAddr::new(self as *const _ as u64); |
| 52 | + let section_addr = VirtAddr::new(unsafe { &__cpu_local_start as *const _ as u64 }); |
| 53 | + |
| 54 | + let offset = self_addr - section_addr; |
| 55 | + VirtAddr::new(val) + offset |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<T> Deref for CpuLocal<T> { |
| 60 | + type Target = T; |
| 61 | + |
| 62 | + fn deref(&self) -> &Self::Target { |
| 63 | + unsafe { &*self.addr().as_ptr() } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<T> DerefMut for CpuLocal<T> { |
| 68 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 69 | + unsafe { &mut *self.addr().as_mut_ptr() } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// The GS register holds a pointer to CPU-local data at a fixed offset of 0. While this approach |
| 74 | +/// requires an additional memory lookup for accessing the data, it enables better code optimization |
| 75 | +/// since the subsequent access is just a normal memory access. Considering the size of the |
| 76 | +/// CPU-local data, this optimization is beneficial. |
| 77 | +#[cpu_local(subsection = "self_ptr")] |
| 78 | +static SELF_PTR: u64 = 0; |
| 79 | + |
| 80 | +#[cpu_local] |
| 81 | +static mut CPUID: usize = 0; |
| 82 | + |
| 83 | +pub fn init(cpu_id: usize) { |
| 84 | + unsafe { |
| 85 | + let start = VirtAddr::new(&__cpu_local_start as *const _ as u64); |
| 86 | + let end = VirtAddr::new(&__cpu_local_end as *const _ as u64); |
| 87 | + let size = end - start; |
| 88 | + |
| 89 | + let layout = Layout::from_size_align_unchecked(size as _, 64); |
| 90 | + let data = alloc::alloc::alloc_zeroed(layout); |
| 91 | + |
| 92 | + core::ptr::copy_nonoverlapping::<u8>(start.as_ptr(), data, size as usize); |
| 93 | + *data.cast::<u64>() = data as u64; |
| 94 | + |
| 95 | + io::wrmsr(io::IA32_GS_BASE, data as u64); |
| 96 | + *CPUID = cpu_id; |
| 97 | + } |
| 98 | +} |
0 commit comments