|
| 1 | +//! cpu_local provides functionality to access a different copy of the same |
| 2 | +//! structure on each CPU. This is used to track data like the CPU ID and |
| 3 | +//! the currently-executing thread. |
| 4 | +
|
| 5 | +// For now, we take a simple but slightly inefficient approach, where we |
| 6 | +// allocate a copy of the CpuData struct in the CPU-local address space |
| 7 | +// and store a pointer to it in the GS base. To access the data, we use |
| 8 | +// a wrapper function to retrieve the pointer from the GS base, casting |
| 9 | +// it to the right type, then access the data as usual. |
| 10 | +// |
| 11 | +// This is less efficient than using offsets from the GS base directly |
| 12 | +// in assembly, as described here[1], but it's much simpler to implement. |
| 13 | +// If rust-osdev/x86_64#257 is merged, that will probably be used to |
| 14 | +// replace this module. |
| 15 | +// |
| 16 | +// [1]: https://github.com/rust-osdev/x86_64/pull/257#issuecomment-849514649 |
| 17 | + |
| 18 | +use crate::memory::{kernel_pml4, pmm, VirtAddrRange, CPU_LOCAL}; |
| 19 | +use crate::multitasking::thread::Thread; |
| 20 | +use alloc::sync::Arc; |
| 21 | +use core::mem::size_of; |
| 22 | +use core::sync::atomic::{AtomicU64, Ordering}; |
| 23 | +use x86_64::registers::model_specific::GsBase; |
| 24 | +use x86_64::structures::paging::{ |
| 25 | + FrameAllocator, Mapper, Page, PageSize, PageTableFlags, Size4KiB, |
| 26 | +}; |
| 27 | + |
| 28 | +/// init prepares the current CPU's local |
| 29 | +/// data using the given CPU ID and stack |
| 30 | +/// space. |
| 31 | +/// |
| 32 | +pub fn init(cpu_id: CpuId, stack_space: &VirtAddrRange) { |
| 33 | + if cpu_id.0 != 0 { |
| 34 | + unimplemented!("additional CPUs not implemented: no idle stack space"); |
| 35 | + } |
| 36 | + |
| 37 | + // Next, work out where we will store our CpuId |
| 38 | + // data. We align up to page size to make paging |
| 39 | + // easier. |
| 40 | + let size = align_up(size_of::<CpuId>(), Size4KiB::SIZE as usize) as u64; |
| 41 | + let start = CPU_LOCAL.start() + cpu_id.as_u64() * size; |
| 42 | + let end = start + size; |
| 43 | + |
| 44 | + // The page addresses should already be aligned, |
| 45 | + // so we shouldn't get any panics here. |
| 46 | + let start_page = Page::from_start_address(start).expect("bad start address"); |
| 47 | + let end_page = Page::from_start_address(end).expect("bad end address"); |
| 48 | + |
| 49 | + // Map our per-CPU address space. |
| 50 | + let mut mapper = unsafe { kernel_pml4() }; |
| 51 | + let mut frame_allocator = pmm::ALLOCATOR.lock(); |
| 52 | + for page in Page::range(start_page, end_page) { |
| 53 | + let frame = frame_allocator |
| 54 | + .allocate_frame() |
| 55 | + .expect("failed to allocate for per-CPU data"); |
| 56 | + |
| 57 | + let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE | PageTableFlags::NO_EXECUTE; |
| 58 | + unsafe { |
| 59 | + mapper |
| 60 | + .map_to(page, frame, flags, &mut *frame_allocator) |
| 61 | + .expect("failed to map per-CPU data") |
| 62 | + .flush() |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + // Store the pointer to the CpuData in the GS base. |
| 67 | + GsBase::write(start); |
| 68 | + |
| 69 | + // Create our idle thread. |
| 70 | + let idle = Thread::new_idle_thread(stack_space); |
| 71 | + |
| 72 | + // Initialise the CpuData from a pointer at the |
| 73 | + // start of the address space. |
| 74 | + let cpu_data = start.as_mut_ptr() as *mut CpuData; |
| 75 | + unsafe { |
| 76 | + cpu_data.write(CpuData { |
| 77 | + id: cpu_id, |
| 78 | + idle_thread: idle.clone(), |
| 79 | + current_thread: idle, |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Helper functions to expose the CPU data. |
| 85 | + |
| 86 | +/// cpu_data is our helper function to get |
| 87 | +/// the pointer to the CPU data from the |
| 88 | +/// GS register. |
| 89 | +/// |
| 90 | +unsafe fn cpu_data() -> &'static mut CpuData { |
| 91 | + let ptr = GsBase::read(); |
| 92 | + |
| 93 | + &mut *(ptr.as_mut_ptr() as *mut CpuData) |
| 94 | +} |
| 95 | + |
| 96 | +/// cpu_id returns this CPU's unique ID. |
| 97 | +/// |
| 98 | +pub fn cpu_id() -> CpuId { |
| 99 | + unsafe { cpu_data() }.id |
| 100 | +} |
| 101 | + |
| 102 | +/// idle_thread returns this CPU's idle thread. |
| 103 | +/// |
| 104 | +pub fn idle_thread() -> Arc<Thread> { |
| 105 | + unsafe { cpu_data() }.idle_thread.clone() |
| 106 | +} |
| 107 | + |
| 108 | +/// current_thread returns the currently executing thread. |
| 109 | +/// |
| 110 | +pub fn current_thread() -> Arc<Thread> { |
| 111 | + unsafe { cpu_data() }.current_thread.clone() |
| 112 | +} |
| 113 | + |
| 114 | +/// set_current_thread overwrites the currently executing |
| 115 | +/// thread. |
| 116 | +/// |
| 117 | +pub fn set_current_thread(thread: Arc<Thread>) { |
| 118 | + unsafe { cpu_data() }.current_thread = thread; |
| 119 | +} |
| 120 | + |
| 121 | +/// align_up aligns the given address upwards to alignment align. |
| 122 | +/// |
| 123 | +/// Requires that align is a power of two. |
| 124 | +/// |
| 125 | +fn align_up(addr: usize, align: usize) -> usize { |
| 126 | + (addr + align - 1) & !(align - 1) |
| 127 | +} |
| 128 | + |
| 129 | +/// CpuId uniquely identifies a CPU core. |
| 130 | +/// |
| 131 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 132 | +pub struct CpuId(u64); |
| 133 | + |
| 134 | +impl CpuId { |
| 135 | + /// new allocates and returns the next available |
| 136 | + /// CpuId. |
| 137 | + /// |
| 138 | + pub fn new() -> Self { |
| 139 | + static NEXT_CPU_ID: AtomicU64 = AtomicU64::new(0); |
| 140 | + CpuId(NEXT_CPU_ID.fetch_add(1, Ordering::Relaxed)) |
| 141 | + } |
| 142 | + |
| 143 | + pub const fn as_u64(&self) -> u64 { |
| 144 | + self.0 |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// CpuData contains the data specific to an individual CPU core. |
| 149 | +// |
| 150 | +struct CpuData { |
| 151 | + id: CpuId, |
| 152 | + idle_thread: Arc<Thread>, |
| 153 | + current_thread: Arc<Thread>, |
| 154 | +} |
0 commit comments