shim_tls.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef _SHIM_TLS_H_
  2. #define _SHIM_TLS_H_
  3. #include <atomic.h>
  4. #define SHIM_TLS_CANARY 0xdeadbeef
  5. struct shim_regs {
  6. unsigned long orig_rax;
  7. unsigned long rsp;
  8. unsigned long r15;
  9. unsigned long r14;
  10. unsigned long r13;
  11. unsigned long r12;
  12. unsigned long r11;
  13. unsigned long r10;
  14. unsigned long r9;
  15. unsigned long r8;
  16. unsigned long rcx;
  17. unsigned long rdx;
  18. unsigned long rsi;
  19. unsigned long rdi;
  20. unsigned long rbx;
  21. unsigned long rbp;
  22. unsigned long rflags;
  23. unsigned long rip;
  24. };
  25. struct shim_context {
  26. struct shim_regs * regs;
  27. struct shim_context * next;
  28. uint64_t enter_time;
  29. struct atomic_int preempt;
  30. };
  31. struct debug_buf;
  32. typedef struct shim_tcb shim_tcb_t;
  33. struct shim_tcb {
  34. uint64_t canary;
  35. shim_tcb_t * self;
  36. struct shim_thread * tp;
  37. struct shim_context context;
  38. unsigned int tid;
  39. int pal_errno;
  40. struct debug_buf * debug_buf;
  41. /* This record is for testing the memory of user inputs.
  42. * If a segfault occurs with the range [start, end],
  43. * the code addr is set to cont_addr to alert the caller. */
  44. struct {
  45. void * start, * end;
  46. void * cont_addr;
  47. bool has_fault;
  48. } test_range;
  49. };
  50. void init_tcb (shim_tcb_t * tcb);
  51. static inline shim_tcb_t * shim_get_tcb(void)
  52. {
  53. /* TODO: optimize to use single movq %gs:<offset> */
  54. PAL_TCB * tcb = pal_get_tcb();
  55. return (shim_tcb_t*)tcb->libos_tcb;
  56. }
  57. static inline bool shim_tls_check_canary(void)
  58. {
  59. /* TODO: optimize to use single movq %gs:<offset> */
  60. shim_tcb_t * shim_tcb = shim_get_tcb();
  61. return shim_tcb->canary == SHIM_TLS_CANARY;
  62. }
  63. #endif /* _SHIM_H_ */