ASM_wrapper.cpp 998 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Types.h>
  7. #include <Kernel/Arch/x86/ASM_wrapper.h>
  8. #include <Kernel/Arch/x86/Processor.h>
  9. #include <Kernel/Sections.h>
  10. namespace Kernel {
  11. #define XCR_XFEATURE_ENABLED_MASK 0
  12. UNMAP_AFTER_INIT u64 read_xcr0()
  13. {
  14. u32 eax, edx;
  15. asm volatile("xgetbv"
  16. : "=a"(eax), "=d"(edx)
  17. : "c"(XCR_XFEATURE_ENABLED_MASK));
  18. return eax + ((u64)edx << 32);
  19. }
  20. UNMAP_AFTER_INIT void write_xcr0(u64 value)
  21. {
  22. u32 eax = value;
  23. u32 edx = value >> 32;
  24. asm volatile("xsetbv" ::"a"(eax), "d"(edx), "c"(XCR_XFEATURE_ENABLED_MASK));
  25. }
  26. void stac()
  27. {
  28. if (!Processor::current().has_feature(CPUFeature::SMAP))
  29. return;
  30. asm volatile("stac" ::
  31. : "cc");
  32. }
  33. void clac()
  34. {
  35. if (!Processor::current().has_feature(CPUFeature::SMAP))
  36. return;
  37. asm volatile("clac" ::
  38. : "cc");
  39. }
  40. }