diff --git a/src/cpu/jitv2/comp.rs b/src/cpu/jitv2/comp.rs index 49114b884..8284ca026 100644 --- a/src/cpu/jitv2/comp.rs +++ b/src/cpu/jitv2/comp.rs @@ -258,7 +258,7 @@ fn prepare_multi_entry_compile( // Publish no code for this generation and deny every offset, so // arrivals interpret the page without asking again until the // release moves the generation (`step_jit`'s denylist gate). - if page.publish(&[0; crate::cpu::jitv2::BITMAP_WORDS], crate::cpu::jitv2::NO_CODE as *const (), gen, 0, 0, req.compiled_for_fr1) { + if page.publish(&[0; crate::cpu::jitv2::BITMAP_WORDS], std::ptr::null(), gen, 0, 0, req.compiled_for_fr1) { page.denylist_all(); } page.mark_prepare_bounced(); diff --git a/src/cpu/jitv2/equiv_test.rs b/src/cpu/jitv2/equiv_test.rs index e338c45e0..d70309b39 100644 --- a/src/cpu/jitv2/equiv_test.rs +++ b/src/cpu/jitv2/equiv_test.rs @@ -4221,7 +4221,7 @@ mod tests { let page = unsafe { &*exec.pcp }; let mut entries = [0u64; crate::cpu::jitv2::BITMAP_WORDS]; entries[0] = 1; - assert!(page.publish(&entries, crate::cpu::jitv2::NO_CODE as *const (), page.current_gen(), 0, 0, false)); + assert!(page.publish(&entries, std::ptr::null(), page.current_gen(), 0, 0, false)); assert!(page.is_runnable(0) && !page.has_code()); exec.core.pc = pc; exec.core.gpr[1] = 0; diff --git a/src/cpu/jitv2/jitv2.rs b/src/cpu/jitv2/jitv2.rs index 98776dc1b..48bd05c21 100644 --- a/src/cpu/jitv2/jitv2.rs +++ b/src/cpu/jitv2/jitv2.rs @@ -1031,6 +1031,9 @@ pub struct PhysicalCodePage { /// published yet. Never null. Validity is owned by `compiled`'s bits together with `entry_gen` /// matching `current_gen()` — see `is_runnable`. func: std::sync::atomic::AtomicPtr<()>, + /// Whether `func` is compiled code. Not read off `func`: a function has no + /// single address across codegen units. Written under `publish_lock`. + holds_code: std::sync::atomic::AtomicBool, /// The FR mode `func` was compiled for. Distinct from the pin (`fr1`), /// which is the mode the *next* compile targets: the two disagree while a /// page used under both modes waits for its recompile. `publish` needs @@ -1573,6 +1576,7 @@ impl PhysicalCodePage { denied: new_bitmap_all_set(), compiled: new_bitmap(), func: std::sync::atomic::AtomicPtr::new(NO_CODE as *mut ()), + holds_code: std::sync::atomic::AtomicBool::new(false), func_fr1: std::sync::atomic::AtomicBool::new(false), tenancy: AtomicU32::new(0), referenced: std::sync::atomic::AtomicBool::new(false), @@ -1626,6 +1630,7 @@ impl PhysicalCodePage { // page permanently denylisted everywhere). for word in self.denied.iter() { word.store(u64::MAX, Ordering::Relaxed); } self.func.store(NO_CODE as *mut (), Ordering::Relaxed); + self.holds_code.store(false, Ordering::Relaxed); self.func_fr1.store(false, Ordering::Relaxed); self.code_epoch.store(0, Ordering::Relaxed); // The snapshot describes the `func` just dropped — see @@ -2009,9 +2014,10 @@ impl PhysicalCodePage { } /// Whether `func` is real code rather than the `jit_no_code` stub. + /// Advisory outside `publish_lock`: a publish can change it right after the read. #[inline] pub fn has_code(&self) -> bool { - self.func.load(Ordering::Relaxed) != NO_CODE as *mut () + self.holds_code.load(Ordering::Relaxed) } /// The page's compiled function, or `jit_no_code` if unpublished. Callers must @@ -2380,8 +2386,9 @@ impl PhysicalCodePage { /// all-allowed (§13.6): a page-gen bump means the bytes are provably /// different from whatever got denied last time. /// - /// For code that lives in no rotating arena (the stub, a test's - /// function); compiled code goes through [`Self::publish_from`]. + /// A null `func` publishes no code: the page keeps `jit_no_code`. For code + /// in no rotating arena (a test's function); compiled code goes through + /// [`Self::publish_from`]. pub fn publish( &self, new_entries: &[u64; BITMAP_WORDS], @@ -2441,10 +2448,12 @@ impl PhysicalCodePage { // because no concurrent reader trusts it without first Acquire- // loading `entry_gen` (via `is_runnable`) and observing it equal // to current_gen(); `publish_lock` excludes any concurrent writer of - // these same three fields. - self.func.store(func as *mut (), Ordering::Release); + // these same four fields. + let real = !func.is_null(); + self.func.store(if real { func as *mut () } else { NO_CODE as *mut () }, Ordering::Release); + self.holds_code.store(real, Ordering::Relaxed); self.func_fr1.store(fr1, Ordering::Relaxed); - self.code_epoch.store(if func == NO_CODE as *const () { 0 } else { arena.epoch() }, Ordering::Relaxed); + self.code_epoch.store(if real { arena.epoch() } else { 0 }, Ordering::Relaxed); self.compiles_since_flush.fetch_add(1, Ordering::Relaxed); self.instr_count.store(instr_count as u32, Ordering::Relaxed); self.code_size.store(code_size, Ordering::Relaxed); @@ -2574,6 +2583,7 @@ impl PhysicalCodePage { } for word in self.compiled.iter() { word.store(0, Ordering::Release); } self.func.store(NO_CODE as *mut (), Ordering::Release); + self.holds_code.store(false, Ordering::Relaxed); self.func_fr1.store(false, Ordering::Relaxed); self.code_epoch.store(0, Ordering::Relaxed); self.instr_count.store(0, Ordering::Relaxed); @@ -4798,7 +4808,7 @@ mod tests { assert!(page.retire_code(a.epoch())); assert!(!page.is_runnable(0) && !page.is_runnable(2) && !page.has_code()); - assert_eq!(page.func(), NO_CODE as *const (), "never null: a CPU may be about to call it"); + assert!(func_is_stub(&page), "never null: a CPU may be about to call it"); assert!(!page.is_requested(7), "entry requests go with the code"); assert!(!page.has_compile_snapshot(), "nothing may re-validate the function that went"); assert!(!page.retire_code(a.epoch()), "nothing left to take"); @@ -4812,6 +4822,12 @@ mod tests { assert_eq!(page.code_epoch(), b.epoch()); } + /// The page's `func` is callable and sends the CPU to the interpreter. + fn func_is_stub(page: &PhysicalCodePage) -> bool { + let stub: JitFn = unsafe { std::mem::transmute(page.func()) }; + unsafe { stub(std::ptr::null_mut()) == crate::cpu::mips_exec::EXEC_FALLBACK } + } + fn claimed_page(fr1: bool) -> Box { let mut page = Box::new(PhysicalCodePage::new(UNCLAIMED_PFN, std::ptr::null())); page.claim(CodeKey::flat(0x1234 << 12), std::ptr::null(), fr1); @@ -6226,7 +6242,7 @@ mod tests { assert_eq!(jit.page_at(at(0, 2), &dev, false), Some(slot)); let page = &jit.pages[slot as usize]; assert!(!page.has_code() && !page.is_runnable(0) && !page.is_requested(5) && !page.is_denylisted(9)); - assert_eq!(page.func(), NO_CODE as *const ()); + assert!(func_is_stub(&page)); } #[test]