shim_tls.h 3.3 KB

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