xsave_gnu.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _XSAVE_GNU_H_
  32. #define _XSAVE_GNU_H_
  33. #include "se_types.h"
  34. #ifdef __x86_64__
  35. # define ASM_FXSAVE "rex64/fxsave"
  36. # define ASM_FXRSTR "rex64/fxrstor"
  37. # define ASM_XSAVE ".byte 0x48,0x0f,0xae,0x21"
  38. # define ASM_XRSTR ".byte 0x48,0x0f,0xae,0x2f"
  39. #else
  40. # define ASM_FXSAVE "fxsave"
  41. # define ASM_FXRSTR "fxrstor"
  42. # define ASM_XSAVE ".byte 0x0f,0xae,0x21"
  43. # define ASM_XRSTR ".byte 0x0f,0xae,0x2f"
  44. #endif
  45. static inline void do_fwait(void)
  46. {
  47. asm volatile("fwait");
  48. }
  49. static inline void do_fxsave(void *buffer)
  50. {
  51. asm volatile(ASM_FXSAVE" (%0)" : : "r"(buffer) : "memory");
  52. }
  53. static inline void do_fxrstor(const void *buffer)
  54. {
  55. asm volatile(ASM_FXRSTR" (%0)" : : "r"(buffer));
  56. }
  57. static inline void do_xsave(void *buffer)
  58. {
  59. asm volatile(ASM_XSAVE
  60. :
  61. : "D" (buffer), "a" (-1), "d" (-1)
  62. : "memory");
  63. }
  64. static inline void _do_xrstor(const void *buffer, uint64_t mask)
  65. {
  66. uint32_t lmask = (uint32_t)mask;
  67. uint32_t hmask = (uint32_t)(mask >> 32);
  68. asm volatile(ASM_XRSTR
  69. :
  70. : "D" (buffer), "a" (lmask), "d" (hmask));
  71. }
  72. static inline void do_xrstor(const void *buffer)
  73. {
  74. _do_xrstor(buffer, 0xffffffffffffffffULL);
  75. }
  76. static inline void do_vzeroupper()
  77. {
  78. asm volatile("vzeroupper");
  79. }
  80. #endif