shim_tls.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <stddef.h>
  63. void init_tcb (shim_tcb_t * tcb);
  64. static inline shim_tcb_t * shim_get_tls(void)
  65. {
  66. PAL_TCB * tcb = pal_get_tcb();
  67. return (shim_tcb_t*)tcb->libos_tcb;
  68. }
  69. static inline bool shim_tls_check_canary(void)
  70. {
  71. /* TODO: optimize to use single movq %gs:<offset> */
  72. shim_tcb_t * shim_tcb = shim_get_tcb();
  73. return shim_tcb->canary == SHIM_TLS_CANARY;
  74. }
  75. #endif /* IN_SHIM */
  76. #endif /* _SHIM_H_ */