diff --git a/lib.rs b/lib.rs index da193a0..87a9b52 100644 --- a/lib.rs +++ b/lib.rs @@ -133,6 +133,13 @@ pub fn cpu_id() -> usize { CPU_ID.load(Ordering::Relaxed) } +static CPU_COUNT : AtomicUsize = ATOMIC_USIZE_INIT; + +#[inline(always)] +pub fn cpu_count() -> usize { + CPU_COUNT.load(Ordering::Relaxed) +} + pub extern fn userspace_init() { assert_eq!(syscall::chdir(b"initfs:bin"), Ok(0)); @@ -146,13 +153,14 @@ pub extern fn userspace_init() { } #[no_mangle] -pub extern fn kmain() { +pub extern fn kmain(cpus: usize) { CPU_ID.store(0, Ordering::SeqCst); + CPU_COUNT.store(cpus, Ordering::SeqCst); context::init(); let pid = syscall::getpid(); - println!("BSP: {:?}", pid); + println!("BSP: {:?} {}", pid, cpus); match context::contexts_mut().spawn(userspace_init) { Ok(context_lock) => { diff --git a/syscall/process.rs b/syscall/process.rs index 5d35af6..39dd822 100644 --- a/syscall/process.rs +++ b/syscall/process.rs @@ -346,7 +346,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result { context.grants = grants; } else { // Copy percpu mapping - { + for cpu_id in 0..::cpu_count() { extern { /// The starting byte of the thread data segment static mut __tdata_start: u8; @@ -356,7 +356,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result { let size = unsafe { & __tbss_end as *const _ as usize - & __tdata_start as *const _ as usize }; - let start = arch::KERNEL_PERCPU_OFFSET + arch::KERNEL_PERCPU_SIZE * ::cpu_id(); + let start = arch::KERNEL_PERCPU_OFFSET + arch::KERNEL_PERCPU_SIZE * cpu_id; let end = start + size; let start_page = Page::containing_address(VirtualAddress::new(start));