arch.rs 607 B

123456789101112131415161718192021
  1. use proc_macro2::TokenStream;
  2. use quote::quote;
  3. use syn::{Ident, Type};
  4. /// Get the base address for percpu variables of the current thread.
  5. pub fn get_percpu_pointer(percpu: &Ident, ty: &Type) -> TokenStream {
  6. quote! {
  7. #[cfg(target_arch = "x86_64")] {
  8. let base: *mut #ty;
  9. ::core::arch::asm!(
  10. "mov %gs:0, {address}",
  11. "add ${percpu_pointer}, {address}",
  12. percpu_pointer = sym #percpu,
  13. address = out(reg) base,
  14. options(att_syntax)
  15. );
  16. base
  17. }
  18. }
  19. .into()
  20. }