shim_tls.h 3.4 KB

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