shim_tls.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef _SHIM_TLS_H_
  2. #define _SHIM_TLS_H_
  3. #ifdef IN_SHIM
  4. #include <atomic.h>
  5. #else /* !IN_SHIM */
  6. /* workaround to make glibc build
  7. * the following structure must match to the one defined in pal/lib/atomic.h
  8. */
  9. #ifdef __x86_64__
  10. struct atomic_int {
  11. volatile int64_t counter;
  12. };
  13. #endif
  14. #endif /* IN_SHIM */
  15. #define SHIM_TLS_CANARY 0xdeadbeef
  16. struct shim_regs {
  17. unsigned long orig_rax;
  18. unsigned long rsp;
  19. unsigned long r15;
  20. unsigned long r14;
  21. unsigned long r13;
  22. unsigned long r12;
  23. unsigned long r11;
  24. unsigned long r10;
  25. unsigned long r9;
  26. unsigned long r8;
  27. unsigned long rcx;
  28. unsigned long rdx;
  29. unsigned long rsi;
  30. unsigned long rdi;
  31. unsigned long rbx;
  32. unsigned long rbp;
  33. unsigned long rflags;
  34. unsigned long rip;
  35. };
  36. struct shim_context {
  37. struct shim_regs * regs;
  38. struct shim_context * next;
  39. uint64_t enter_time;
  40. struct atomic_int preempt;
  41. };
  42. struct debug_buf;
  43. typedef struct shim_tcb shim_tcb_t;
  44. struct shim_tcb {
  45. uint64_t canary;
  46. shim_tcb_t * self;
  47. struct shim_thread * tp;
  48. struct shim_context context;
  49. unsigned int tid;
  50. int pal_errno;
  51. struct debug_buf * debug_buf;
  52. /* This record is for testing the memory of user inputs.
  53. * If a segfault occurs with the range [start, end],
  54. * the code addr is set to cont_addr to alert the caller. */
  55. struct {
  56. void * start, * end;
  57. void * cont_addr;
  58. } test_range;
  59. };
  60. #ifdef IN_SHIM
  61. /*
  62. * This struct must match the one defined in glibc/nptl/sysdeps/x86_64/tls.h
  63. * The first 10 members(from tcb to __unused1) are used by Glibc-internal,
  64. * they are NOT used by Graphene.
  65. * But Graphene needs to preserve the correct offset of shim_tcb so we have to
  66. * duplicate these 10 fields from the original Glibc struct.
  67. */
  68. struct __libc_tcb_t;
  69. typedef struct __libc_tcb_t __libc_tcb_t;
  70. struct __libc_tcb_t
  71. {
  72. __libc_tcb_t * tcb;
  73. void * dtv, * self;
  74. int mthreads, gscope;
  75. uintptr_t sysinfo, sg, pg;
  76. unsigned long int vgetcpu_cache[2];
  77. int __unused1;
  78. shim_tcb_t shim_tcb;
  79. };
  80. #include <stddef.h>
  81. void init_tcb (shim_tcb_t * tcb);
  82. static inline bool shim_tls_check_canary(void)
  83. {
  84. uint64_t __canary;
  85. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__canary)
  86. : "i" (offsetof(__libc_tcb_t, shim_tcb.canary)));
  87. return __canary == SHIM_TLS_CANARY;
  88. }
  89. static inline shim_tcb_t * shim_get_tls(void)
  90. {
  91. shim_tcb_t *__self;
  92. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  93. : "i" (offsetof(__libc_tcb_t, shim_tcb.self)));
  94. return __self;
  95. }
  96. static inline __libc_tcb_t * shim_libc_tcb(void)
  97. {
  98. __libc_tcb_t *__self;
  99. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  100. : "i" (offsetof(__libc_tcb_t, tcb)));
  101. return __self;
  102. }
  103. #endif /* IN_SHIM */
  104. #endif /* _SHIM_H_ */