pal_host.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * pal_host.h
  15. *
  16. * This file contains definition of PAL host ABI.
  17. */
  18. #ifndef PAL_HOST_H
  19. #define PAL_HOST_H
  20. #ifndef IN_PAL
  21. # error "cannot be included outside PAL"
  22. #endif
  23. #include <atomic.h>
  24. #include <spinlock.h>
  25. typedef spinlock_t PAL_LOCK;
  26. #define LOCK_INIT INIT_SPINLOCK_UNLOCKED
  27. #define _DkInternalLock spinlock_lock
  28. #define _DkInternalUnlock spinlock_unlock
  29. void * malloc_untrusted (int size);
  30. void free_untrusted (void * mem);
  31. #include <list.h>
  32. /* Simpler mutex design: a single variable that tracks whether the mutex
  33. * is locked (just waste a 64 bit word for now). State is 1 (locked) or
  34. * 0 (unlocked).
  35. *
  36. * Keep a count of how many threads are waiting on the mutex.
  37. *
  38. * If DEBUG_MUTEX is defined, mutex_handle will record the owner of
  39. * mutex locking. */
  40. struct mutex_handle {
  41. volatile int64_t * locked;
  42. struct atomic_int nwaiters;
  43. #ifdef DEBUG_MUTEX
  44. int owner;
  45. #endif
  46. };
  47. /* Initializer of Mutexes */
  48. #define MUTEX_HANDLE_INIT { .u = 0 }
  49. #define INIT_MUTEX_HANDLE(m) do { (m)->u = 0; } while (0)
  50. DEFINE_LIST(pal_handle_thread);
  51. struct pal_handle_thread {
  52. PAL_HDR reserved;
  53. PAL_IDX tid;
  54. PAL_PTR tcs;
  55. LIST_TYPE(pal_handle_thread) list;
  56. void * param;
  57. };
  58. /* RPC streams are encrypted with 256-bit AES keys */
  59. typedef uint8_t PAL_SESSION_KEY[32];
  60. typedef struct pal_handle
  61. {
  62. /*
  63. * Here we define the internal structure of PAL_HANDLE.
  64. * user has no access to the content inside these handles.
  65. */
  66. PAL_HDR hdr;
  67. union {
  68. struct {
  69. PAL_IDX fds[MAX_FDS];
  70. } generic;
  71. struct {
  72. PAL_IDX fd;
  73. PAL_STR realpath;
  74. PAL_NUM total;
  75. PAL_NUM offset;
  76. /* below fields are used only for trusted files */
  77. PAL_PTR stubs; /* contains hashes of file chunks */
  78. PAL_PTR umem; /* valid only when stubs != NULL */
  79. } file;
  80. struct {
  81. PAL_IDX fd;
  82. PAL_NUM pipeid;
  83. PAL_BOL nonblocking;
  84. } pipe;
  85. struct {
  86. PAL_IDX fds[MAX_FDS];
  87. PAL_BOL nonblocking;
  88. } pipeprv;
  89. struct {
  90. PAL_IDX fd;
  91. /* TODO: add other flags in future, if needed (e.g., semaphore) */
  92. PAL_BOL nonblocking;
  93. } eventfd;
  94. struct {
  95. PAL_IDX fd_in, fd_out;
  96. PAL_IDX dev_type;
  97. PAL_BOL destroy;
  98. PAL_STR realpath;
  99. } dev;
  100. struct {
  101. PAL_IDX fd;
  102. PAL_STR realpath;
  103. PAL_PTR buf;
  104. PAL_PTR ptr;
  105. PAL_PTR end;
  106. PAL_BOL endofstream;
  107. } dir;
  108. struct {
  109. PAL_IDX fd;
  110. PAL_PTR bind;
  111. PAL_PTR conn;
  112. PAL_BOL nonblocking;
  113. PAL_NUM linger;
  114. PAL_NUM receivebuf;
  115. PAL_NUM sendbuf;
  116. PAL_NUM receivetimeout;
  117. PAL_NUM sendtimeout;
  118. PAL_BOL tcp_cork;
  119. PAL_BOL tcp_keepalive;
  120. PAL_BOL tcp_nodelay;
  121. } sock;
  122. struct {
  123. PAL_IDX stream;
  124. PAL_IDX cargo;
  125. PAL_IDX pid;
  126. PAL_BOL nonblocking;
  127. PAL_SESSION_KEY session_key;
  128. } process;
  129. struct pal_handle_thread thread;
  130. struct {
  131. struct atomic_int nwaiters;
  132. PAL_NUM max_value;
  133. union {
  134. struct mutex_handle mut;
  135. } mutex;
  136. struct {
  137. struct atomic_int * signaled;
  138. struct atomic_int nwaiters;
  139. PAL_BOL isnotification;
  140. } event;
  141. };
  142. };
  143. } * PAL_HANDLE;
  144. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  145. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  146. #define ERROR(n) (1 << (MAX_FDS*2 + (n)))
  147. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  148. struct arch_frame {
  149. #ifdef __x86_64__
  150. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  151. #else
  152. # error "unsupported architecture"
  153. #endif
  154. };
  155. #ifdef __x86_64__
  156. # define store_register(reg, var) \
  157. __asm__ volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  158. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  159. # define arch_store_frame(f) \
  160. store_register_in_frame(rsp, f) \
  161. store_register_in_frame(rbp, f) \
  162. store_register_in_frame(rbx, f) \
  163. store_register_in_frame(rsi, f) \
  164. store_register_in_frame(rdi, f) \
  165. store_register_in_frame(r12, f) \
  166. store_register_in_frame(r13, f) \
  167. store_register_in_frame(r14, f) \
  168. store_register_in_frame(r15, f)
  169. # define restore_register(reg, var, clobber...) \
  170. __asm__ volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  171. # define restore_register_in_frame(reg, f) \
  172. restore_register(reg, (f)->reg, \
  173. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  174. # define arch_restore_frame(f) \
  175. restore_register_in_frame(r15, f) \
  176. restore_register_in_frame(r14, f) \
  177. restore_register_in_frame(r13, f) \
  178. restore_register_in_frame(r12, f) \
  179. restore_register_in_frame(rdi, f) \
  180. restore_register_in_frame(rsi, f) \
  181. restore_register_in_frame(rbx, f) \
  182. restore_register_in_frame(rbp, f) \
  183. restore_register_in_frame(rsp, f)
  184. #else /* __x86_64__ */
  185. # error "unsupported architecture"
  186. #endif
  187. #define PAL_FRAME_IDENTIFIER 0xdeaddeadbeefbeef
  188. struct pal_frame {
  189. volatile uint64_t identifier;
  190. void * func;
  191. const char * funcname;
  192. struct arch_frame arch;
  193. };
  194. /* DEP 12/25/17: This frame storage thing is important to mark volatile.
  195. * The compiler should not optimize out any of these changes, and
  196. * because some accesses can happen during an exception, these are not
  197. * visible to the compiler in an otherwise stack-local variable (so the
  198. * compiler will try to optimize out these assignments.
  199. */
  200. static inline
  201. void __store_frame (volatile struct pal_frame * frame,
  202. void * func, const char * funcname)
  203. {
  204. arch_store_frame(&frame->arch)
  205. frame->func = func;
  206. frame->funcname = funcname;
  207. __asm__ volatile ("nop" ::: "memory");
  208. frame->identifier = PAL_FRAME_IDENTIFIER;
  209. }
  210. #define ENTER_PAL_CALL(name) \
  211. struct pal_frame frame; \
  212. __store_frame(&frame, &(name), #name)
  213. static inline
  214. void __clear_frame (volatile struct pal_frame * frame)
  215. {
  216. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  217. __asm__ volatile ("nop" ::: "memory");
  218. frame->identifier = 0;
  219. }
  220. }
  221. #define LEAVE_PAL_CALL() \
  222. do { \
  223. __clear_frame(&frame); \
  224. } while (0)
  225. #define LEAVE_PAL_CALL_RETURN(retval) \
  226. do { \
  227. __clear_frame(&frame); \
  228. return (retval); \
  229. } while (0)
  230. #endif /* PAL_HOST_H */