shim_tls.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. bool has_fault;
  59. } test_range;
  60. };
  61. #ifdef IN_SHIM
  62. /*
  63. * This struct must match the one defined in glibc/nptl/sysdeps/x86_64/tls.h
  64. * The first 10 members(from tcb to __unused1) are used by Glibc-internal,
  65. * they are NOT used by Graphene.
  66. * But Graphene needs to preserve the correct offset of shim_tcb so we have to
  67. * duplicate these 10 fields from the original Glibc struct.
  68. */
  69. struct __libc_tcb_t;
  70. typedef struct __libc_tcb_t __libc_tcb_t;
  71. struct __libc_tcb_t
  72. {
  73. __libc_tcb_t * tcb;
  74. void * dtv, * self;
  75. int mthreads, gscope;
  76. uintptr_t sysinfo, sg, pg;
  77. unsigned long int vgetcpu_cache[2];
  78. int __unused1;
  79. shim_tcb_t shim_tcb;
  80. };
  81. #include <stddef.h>
  82. void init_tcb (shim_tcb_t * tcb);
  83. static inline bool shim_tls_check_canary(void)
  84. {
  85. uint64_t __canary;
  86. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__canary)
  87. : "i" (offsetof(__libc_tcb_t, shim_tcb.canary)));
  88. return __canary == SHIM_TLS_CANARY;
  89. }
  90. static inline shim_tcb_t * shim_get_tls(void)
  91. {
  92. shim_tcb_t *__self;
  93. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  94. : "i" (offsetof(__libc_tcb_t, shim_tcb.self)));
  95. return __self;
  96. }
  97. static inline __libc_tcb_t * shim_libc_tcb(void)
  98. {
  99. __libc_tcb_t *__self;
  100. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  101. : "i" (offsetof(__libc_tcb_t, tcb)));
  102. return __self;
  103. }
  104. #endif /* IN_SHIM */
  105. #endif /* _SHIM_H_ */