88//! invocation of `init_sections` by the BSP.
99
1010use crate :: error:: Result ;
11+ use crate :: lock:: ro_after_init:: RoAfterInit ;
1112use alloc:: vec:: Vec ;
13+ use core:: fmt;
1214
13- static mut AP_PER_CORE_SECTIONS : Option < Vec < u8 > > = None ;
15+ static AP_PER_CORE_SECTIONS : RoAfterInit < Vec < u8 > > =
16+ RoAfterInit :: uninitialized ( ) ;
1417
1518extern "C" {
1619 // The _value_ of the first/last byte of the .per_core section. The
@@ -31,11 +34,9 @@ unsafe fn per_core_address(symbol_addr: *const u8, core: usize) -> *const u8 {
3134 }
3235 let section_len = per_core_section_len ( ) ;
3336 let offset = symbol_addr as u64 - ( & PER_CORE_START as * const _ as u64 ) ;
34- let ap_sections = AP_PER_CORE_SECTIONS
35- . as_ref ( )
36- . expect ( "Per-core sections not initialized" ) ;
3737
38- & ap_sections[ ( section_len * ( core - 1 ) ) + offset as usize ] as * const u8
38+ & AP_PER_CORE_SECTIONS [ ( section_len * ( core - 1 ) ) + offset as usize ]
39+ as * const u8
3940}
4041
4142/// Initialize the per-core sections
@@ -53,35 +54,53 @@ pub unsafe fn init_sections(ncores: usize) -> Result<()> {
5354 ap_sections. extend_from_slice ( per_core_section) ;
5455 }
5556
56- AP_PER_CORE_SECTIONS = Some ( ap_sections) ;
57-
57+ RoAfterInit :: init ( & AP_PER_CORE_SECTIONS , ap_sections) ;
5858 Ok ( ( ) )
5959}
6060
61+ /// The sequential index of a core
62+ #[ derive( Copy , Clone , Debug , Ord , PartialEq , PartialOrd , Eq ) ]
63+ pub struct CoreId {
64+ /// The raw ID as an integer
65+ pub raw : u32 ,
66+ }
67+
68+ impl From < u32 > for CoreId {
69+ fn from ( value : u32 ) -> Self {
70+ CoreId { raw : value }
71+ }
72+ }
73+
74+ impl fmt:: Display for CoreId {
75+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
76+ write ! ( f, "0x{:x}" , self . raw)
77+ }
78+ }
79+
6180/// Get this current core's sequential index
62- pub fn read_core_idx ( ) -> u64 {
81+ pub fn read_core_id ( ) -> CoreId {
6382 unsafe {
6483 let value: u64 ;
6584 llvm_asm ! ( "mov [%fs], %rax"
6685 : "={rax}" ( value)
6786 :: : "volatile" ) ;
68- value >> 3 // Shift away the RPL and TI bits (they will always be 0)
87+ ( ( value >> 3 ) as u32 ) . into ( ) // Shift away the RPL and TI bits (they will always be 0)
6988 }
7089}
7190
7291#[ doc( hidden) ]
7392pub unsafe fn get_pre_core_impl < T > ( t : & T ) -> & T {
7493 core:: mem:: transmute ( per_core_address (
7594 t as * const T as * const u8 ,
76- read_core_idx ( ) as usize ,
95+ read_core_id ( ) . raw as usize ,
7796 ) )
7897}
7998
8099#[ doc( hidden) ]
81100pub unsafe fn get_pre_core_mut_impl < T > ( t : & mut T ) -> & mut T {
82101 core:: mem:: transmute ( per_core_address (
83102 t as * const T as * const u8 ,
84- read_core_idx ( ) as usize ,
103+ read_core_id ( ) . raw as usize ,
85104 ) )
86105}
87106
0 commit comments