pal_host.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * pal_host.h
  17. *
  18. * This file contains definition of PAL host ABI.
  19. */
  20. #ifndef PAL_HOST_H
  21. #define PAL_HOST_H
  22. #ifndef IN_PAL
  23. # error "cannot be included outside PAL"
  24. #endif
  25. #include <atomic.h>
  26. /* Spinlocking */
  27. typedef struct spinlock {
  28. struct atomic_int value;
  29. } PAL_LOCK;
  30. int _DkSpinLock (struct spinlock * lock);
  31. int _DkSpinUnlock (struct spinlock * lock);
  32. #define LOCK_INIT { .value = { 0 } }
  33. #define _DkInternalLock _DkSpinLock
  34. #define _DkInternalUnlock _DkSpinUnlock
  35. void * malloc_untrusted (int size);
  36. void free_untrusted (void * mem);
  37. #include <list.h>
  38. /* internal Mutex design, the structure has to align at integer boundary
  39. because it is required by futex call. If DEBUG_MUTEX is defined,
  40. mutex_handle will record the owner of mutex locking. */
  41. struct mutex_handle {
  42. union {
  43. unsigned int u;
  44. struct {
  45. unsigned char locked;
  46. unsigned char contended;
  47. } b;
  48. };
  49. };
  50. /* Initializer of Mutexes */
  51. #define MUTEX_HANDLE_INIT { .u = 0 }
  52. #define INIT_MUTEX_HANDLE(m) do { m->u = 0; } while (0)
  53. DEFINE_LIST(pal_handle_thread);
  54. struct pal_handle_thread {
  55. PAL_HDR reserved;
  56. PAL_IDX tid;
  57. PAL_PTR tcs;
  58. LIST_TYPE(pal_handle_thread) list;
  59. void * param;
  60. };
  61. typedef union pal_handle
  62. {
  63. /* TSAI: Here we define the internal types of PAL_HANDLE
  64. * in PAL design, user has not to access the content inside the
  65. * handle, also there is no need to allocate the internal
  66. * handles, so we hide the type name of these handles on purpose.
  67. */
  68. struct {
  69. PAL_IDX type;
  70. PAL_REF ref;
  71. PAL_FLG flags;
  72. PAL_IDX fds[];
  73. } hdr;
  74. struct {
  75. PAL_HDR reserved;
  76. PAL_IDX fd;
  77. PAL_BOL append;
  78. PAL_BOL pass;
  79. PAL_STR realpath;
  80. PAL_NUM total;
  81. PAL_PTR stubs;
  82. } file;
  83. struct {
  84. PAL_HDR reserved;
  85. PAL_IDX fd;
  86. PAL_NUM pipeid;
  87. PAL_BOL nonblocking;
  88. } pipe;
  89. struct {
  90. PAL_HDR reserved;
  91. PAL_IDX fds[2];
  92. PAL_BOL nonblocking;
  93. } pipeprv;
  94. struct {
  95. PAL_HDR reserved;
  96. PAL_IDX fd_in, fd_out;
  97. PAL_IDX dev_type;
  98. PAL_BOL destroy;
  99. PAL_STR realpath;
  100. } dev;
  101. struct {
  102. PAL_HDR reserved;
  103. PAL_IDX fd;
  104. PAL_STR realpath;
  105. PAL_PTR buf;
  106. PAL_PTR ptr;
  107. PAL_PTR end;
  108. PAL_BOL endofstream;
  109. } dir;
  110. struct {
  111. PAL_HDR reserved;
  112. PAL_IDX fd;
  113. } gipc;
  114. struct {
  115. PAL_HDR reserved;
  116. PAL_IDX fd;
  117. PAL_PTR bind;
  118. PAL_PTR conn;
  119. PAL_BOL nonblocking;
  120. PAL_NUM linger;
  121. PAL_NUM receivebuf;
  122. PAL_NUM sendbuf;
  123. PAL_NUM receivetimeout;
  124. PAL_NUM sendtimeout;
  125. PAL_BOL tcp_cork;
  126. PAL_BOL tcp_keepalive;
  127. PAL_BOL tcp_nodelay;
  128. } sock;
  129. struct {
  130. PAL_HDR reserved;
  131. PAL_IDX stream_in, stream_out;
  132. PAL_IDX cargo;
  133. PAL_IDX pid;
  134. PAL_BOL nonblocking;
  135. } process;
  136. struct {
  137. PAL_HDR reserved;
  138. PAL_IDX cli;
  139. PAL_IDX srv;
  140. PAL_IDX port;
  141. PAL_BOL nonblocking;
  142. } mcast;
  143. struct pal_handle_thread thread;
  144. struct {
  145. PAL_HDR reserved;
  146. struct atomic_int nwaiters;
  147. PAL_NUM max_value;
  148. union {
  149. struct mutex_handle mut;
  150. struct atomic_int i;
  151. } value;
  152. } semaphore;
  153. struct {
  154. PAL_HDR reserved;
  155. struct atomic_int signaled;
  156. struct atomic_int nwaiters;
  157. PAL_BOL isnotification;
  158. } event;
  159. } * PAL_HANDLE;
  160. #define RFD(n) (00001 << (n))
  161. #define WFD(n) (00010 << (n))
  162. #define WRITEABLE(n) (00100 << (n))
  163. #define ERROR(n) (01000 << (n))
  164. #define MAX_FDS (3)
  165. #define HAS_FDS (00077)
  166. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  167. struct arch_frame {
  168. #ifdef __x86_64__
  169. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  170. #else
  171. # error "unsupported architecture"
  172. #endif
  173. };
  174. #ifdef __x86_64__
  175. # define store_register(reg, var) \
  176. asm volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  177. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  178. # define arch_store_frame(f) \
  179. store_register_in_frame(rsp, f) \
  180. store_register_in_frame(rbp, f) \
  181. store_register_in_frame(rbx, f) \
  182. store_register_in_frame(rsi, f) \
  183. store_register_in_frame(rdi, f) \
  184. store_register_in_frame(r12, f) \
  185. store_register_in_frame(r13, f) \
  186. store_register_in_frame(r14, f) \
  187. store_register_in_frame(r15, f)
  188. # define restore_register(reg, var, clobber...) \
  189. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  190. # define restore_register_in_frame(reg, f) \
  191. restore_register(reg, (f)->reg, \
  192. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  193. # define arch_restore_frame(f) \
  194. restore_register_in_frame(r15, f) \
  195. restore_register_in_frame(r14, f) \
  196. restore_register_in_frame(r13, f) \
  197. restore_register_in_frame(r12, f) \
  198. restore_register_in_frame(rdi, f) \
  199. restore_register_in_frame(rsi, f) \
  200. restore_register_in_frame(rbx, f) \
  201. restore_register_in_frame(rbp, f) \
  202. restore_register_in_frame(rsp, f)
  203. #else /* __x86_64__ */
  204. # error "unsupported architecture"
  205. #endif
  206. #define PAL_FRAME_IDENTIFIER (0xdeaddeadbeefbeef)
  207. struct pal_frame {
  208. volatile uint64_t identifier;
  209. void * func;
  210. const char * funcname;
  211. struct arch_frame arch;
  212. };
  213. static inline
  214. void __store_frame (struct pal_frame * frame,
  215. void * func, const char * funcname)
  216. {
  217. arch_store_frame(&frame->arch)
  218. frame->func = func;
  219. frame->funcname = funcname;
  220. asm volatile ("nop" ::: "memory");
  221. frame->identifier = PAL_FRAME_IDENTIFIER;
  222. }
  223. #define ENTER_PAL_CALL(name) \
  224. struct pal_frame frame; \
  225. __store_frame(&frame, &(name), #name)
  226. static inline
  227. void __clear_frame (struct pal_frame * frame)
  228. {
  229. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  230. asm volatile ("nop" ::: "memory");
  231. frame->identifier = 0;
  232. }
  233. }
  234. #define LEAVE_PAL_CALL() \
  235. do { \
  236. __clear_frame(&frame); \
  237. } while (0)
  238. #define LEAVE_PAL_CALL_RETURN(retval) \
  239. do { \
  240. __clear_frame(&frame); \
  241. return (retval); \
  242. } while (0)
  243. #endif /* PAL_HOST_H */