shim_tls.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 r15;
  17. unsigned long r14;
  18. unsigned long r13;
  19. unsigned long r12;
  20. unsigned long r11;
  21. unsigned long r10;
  22. unsigned long r9;
  23. unsigned long r8;
  24. unsigned long rcx;
  25. unsigned long rdx;
  26. unsigned long rsi;
  27. unsigned long rdi;
  28. unsigned long rbx;
  29. unsigned long rbp;
  30. unsigned long rflags;
  31. };
  32. struct shim_context {
  33. unsigned long syscall_nr;
  34. void * sp;
  35. void * ret_ip;
  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. typedef struct
  65. {
  66. void * tcb, * dtv, * self;
  67. int mthreads, gscope;
  68. uintptr_t sysinfo, sg, pg;
  69. unsigned long int vgetcpu_cache[2];
  70. int __unused1;
  71. shim_tcb_t shim_tcb;
  72. } __libc_tcb_t;
  73. #include <stddef.h>
  74. void init_tcb (shim_tcb_t * tcb);
  75. static inline bool shim_tls_check_canary(void)
  76. {
  77. uint64_t __canary;
  78. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__canary)
  79. : "i" (offsetof(__libc_tcb_t, shim_tcb.canary)));
  80. return __canary == SHIM_TLS_CANARY;
  81. }
  82. static inline shim_tcb_t * shim_get_tls(void)
  83. {
  84. shim_tcb_t *__self;
  85. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  86. : "i" (offsetof(__libc_tcb_t, shim_tcb.self)));
  87. return __self;
  88. }
  89. static inline __libc_tcb_t * shim_libc_tcb(void)
  90. {
  91. __libc_tcb_t *__self;
  92. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  93. : "i" (offsetof(__libc_tcb_t, tcb)));
  94. return __self;
  95. }
  96. #endif /* IN_SHIM */
  97. #endif /* !__ASSEMBLER__ */
  98. #endif /* _SHIM_H_ */