shim_tls.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #ifndef _SHIM_TLS_H_
  4. #define _SHIM_TLS_H_
  5. #ifdef __ASSEMBLER__
  6. #define SHIM_TLS_CANARY $xdeadbeef
  7. #if defined(__x86_64__)
  8. # define SHIM_TCB_OFFSET 80
  9. #else
  10. # define SHIM_TCB_OFFSET 44
  11. #endif
  12. #else /* !__ASSEMBLER__ */
  13. #define SHIM_TLS_CANARY 0xdeadbeef
  14. struct lock_record {
  15. enum { NO_LOCK, SEM_LOCK, READ_LOCK, WRITE_LOCK } type;
  16. void * lock;
  17. const char * filename;
  18. int lineno;
  19. };
  20. #define NUM_LOCK_RECORD 32
  21. #define NUM_LOCK_RECORD_MASK (NUM_LOCK_RECORD - 1)
  22. struct shim_regs {
  23. unsigned long r15;
  24. unsigned long r14;
  25. unsigned long r13;
  26. unsigned long r12;
  27. unsigned long r11;
  28. unsigned long r10;
  29. unsigned long r9;
  30. unsigned long r8;
  31. unsigned long rcx;
  32. unsigned long rdx;
  33. unsigned long rsi;
  34. unsigned long rdi;
  35. unsigned long rbx;
  36. unsigned long rbp;
  37. };
  38. struct shim_context {
  39. unsigned long syscall_nr;
  40. void * sp;
  41. void * ret_ip;
  42. struct shim_regs * regs;
  43. struct shim_context * next;
  44. unsigned long enter_time;
  45. unsigned long preempt;
  46. };
  47. #ifdef IN_SHIM
  48. #include <shim_defs.h>
  49. #define SIGNAL_DELAYED (0x80000000UL)
  50. #endif /* IN_SHIM */
  51. typedef struct {
  52. uint64_t canary;
  53. void * self;
  54. void * tp;
  55. struct shim_context context;
  56. unsigned int tid;
  57. int pal_errno;
  58. void * debug_buf;
  59. int last_lock;
  60. struct lock_record held_locks[NUM_LOCK_RECORD];
  61. /* This record is for testing the memory of user inputs.
  62. * If a segfault occurs with the range [start, end],
  63. * the code addr is set to cont_addr to alert the caller. */
  64. struct {
  65. void * start, * end;
  66. void * cont_addr;
  67. } test_range;
  68. } shim_tcb_t;
  69. #ifdef IN_SHIM
  70. typedef struct
  71. {
  72. void * tcb, * dtv, * self;
  73. int mthreads, gscope;
  74. uintptr_t sysinfo, sg, pg;
  75. unsigned long int vgetcpu_cache[2];
  76. int __unused1;
  77. shim_tcb_t shim_tcb;
  78. } __libc_tcb_t;
  79. #include <stddef.h>
  80. #define SHIM_TLS_CHECK_CANARY() \
  81. ({ uint64_t __canary; \
  82. asm ("movq %%fs:%c1,%q0" : "=r" (__canary) \
  83. : "i" (offsetof(__libc_tcb_t, shim_tcb.canary))); \
  84. __canary == SHIM_TLS_CANARY; })
  85. #define SHIM_GET_TLS() \
  86. ({ shim_tcb_t *__self; \
  87. asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
  88. : "i" (offsetof(__libc_tcb_t, shim_tcb.self))); \
  89. __self; })
  90. #define GET_LIBC_TCB() \
  91. ({ void *__self; \
  92. asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
  93. : "i" (offsetof(__libc_tcb_t, tcb))); \
  94. __self; })
  95. #endif /* IN_SHIM */
  96. #endif /* !__ASSEMBLER__ */
  97. #endif /* _SHIM_H_ */