arch.rs 683 B

123456789101112131415161718192021222324
  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. {
  8. #[cfg(target_arch = "x86_64")]
  9. {
  10. let base: *mut #ty;
  11. ::core::arch::asm!(
  12. "mov %gs:0, {address}",
  13. "add ${percpu_pointer}, {address}",
  14. percpu_pointer = sym #percpu,
  15. address = out(reg) base,
  16. options(att_syntax)
  17. );
  18. base
  19. }
  20. }
  21. }
  22. .into()
  23. }