pal_host.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <linux_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. typedef union pal_handle
  54. {
  55. /* TSAI: Here we define the internal types of PAL_HANDLE
  56. * in PAL design, user has not to access the content inside the
  57. * handle, also there is no need to allocate the internal
  58. * handles, so we hide the type name of these handles on purpose.
  59. */
  60. struct {
  61. PAL_IDX type;
  62. PAL_REF ref;
  63. PAL_FLG flags;
  64. PAL_IDX fds[];
  65. } hdr;
  66. struct {
  67. PAL_HDR reserved;
  68. PAL_IDX fd;
  69. PAL_BOL append;
  70. PAL_BOL pass;
  71. PAL_STR realpath;
  72. PAL_NUM total;
  73. PAL_PTR stubs;
  74. } file;
  75. struct {
  76. PAL_HDR reserved;
  77. PAL_IDX fd;
  78. PAL_NUM pipeid;
  79. PAL_BOL nonblocking;
  80. } pipe;
  81. struct {
  82. PAL_HDR reserved;
  83. PAL_IDX fds[2];
  84. PAL_BOL nonblocking;
  85. } pipeprv;
  86. struct {
  87. PAL_HDR reserved;
  88. PAL_IDX fd_in, fd_out;
  89. PAL_IDX dev_type;
  90. PAL_BOL destroy;
  91. PAL_STR realpath;
  92. } dev;
  93. struct {
  94. PAL_HDR reserved;
  95. PAL_IDX fd;
  96. PAL_STR realpath;
  97. PAL_PTR buf;
  98. PAL_PTR ptr;
  99. PAL_PTR end;
  100. PAL_BOL endofstream;
  101. } dir;
  102. struct {
  103. PAL_HDR reserved;
  104. PAL_IDX fd;
  105. } gipc;
  106. struct {
  107. PAL_HDR reserved;
  108. PAL_IDX fd;
  109. PAL_PTR bind;
  110. PAL_PTR conn;
  111. PAL_BOL nonblocking;
  112. PAL_NUM linger;
  113. PAL_NUM receivebuf;
  114. PAL_NUM sendbuf;
  115. PAL_NUM receivetimeout;
  116. PAL_NUM sendtimeout;
  117. PAL_BOL tcp_cork;
  118. PAL_BOL tcp_keepalive;
  119. PAL_BOL tcp_nodelay;
  120. } sock;
  121. struct {
  122. PAL_HDR reserved;
  123. PAL_IDX stream_in, stream_out;
  124. PAL_IDX cargo;
  125. PAL_IDX pid;
  126. PAL_BOL nonblocking;
  127. } process;
  128. struct {
  129. PAL_HDR reserved;
  130. PAL_IDX cli;
  131. PAL_IDX srv;
  132. PAL_IDX port;
  133. PAL_BOL nonblocking;
  134. } mcast;
  135. struct {
  136. PAL_HDR reserved;
  137. PAL_IDX tid;
  138. struct hlist_node hlist;
  139. } thread;
  140. struct {
  141. PAL_HDR reserved;
  142. struct atomic_int nwaiters;
  143. PAL_NUM max_value;
  144. union {
  145. struct mutex_handle mut;
  146. struct atomic_int i;
  147. } value;
  148. } semaphore;
  149. struct {
  150. PAL_HDR reserved;
  151. struct atomic_int signaled;
  152. struct atomic_int nwaiters;
  153. PAL_BOL isnotification;
  154. } event;
  155. } * PAL_HANDLE;
  156. #define RFD(n) (00001 << (n))
  157. #define WFD(n) (00010 << (n))
  158. #define WRITEABLE(n) (00100 << (n))
  159. #define ERROR(n) (01000 << (n))
  160. #define MAX_FDS (3)
  161. #define HAS_FDS (00077)
  162. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  163. struct arch_frame {
  164. #ifdef __x86_64__
  165. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  166. #else
  167. # error "unsupported architecture"
  168. #endif
  169. };
  170. #ifdef __x86_64__
  171. # define store_register(reg, var) \
  172. asm volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  173. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  174. # define arch_store_frame(f) \
  175. store_register_in_frame(rsp, f) \
  176. store_register_in_frame(rbp, f) \
  177. store_register_in_frame(rbx, f) \
  178. store_register_in_frame(rsi, f) \
  179. store_register_in_frame(rdi, f) \
  180. store_register_in_frame(r12, f) \
  181. store_register_in_frame(r13, f) \
  182. store_register_in_frame(r14, f) \
  183. store_register_in_frame(r15, f)
  184. # define restore_register(reg, var, clobber...) \
  185. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  186. # define restore_register_in_frame(reg, f) \
  187. restore_register(reg, (f)->reg, \
  188. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  189. # define arch_restore_frame(f) \
  190. restore_register_in_frame(r15, f) \
  191. restore_register_in_frame(r14, f) \
  192. restore_register_in_frame(r13, f) \
  193. restore_register_in_frame(r12, f) \
  194. restore_register_in_frame(rdi, f) \
  195. restore_register_in_frame(rsi, f) \
  196. restore_register_in_frame(rbx, f) \
  197. restore_register_in_frame(rbp, f) \
  198. restore_register_in_frame(rsp, f)
  199. #else /* __x86_64__ */
  200. # error "unsupported architecture"
  201. #endif
  202. #define PAL_FRAME_IDENTIFIER (0xdeaddeadbeefbeef)
  203. struct pal_frame {
  204. volatile uint64_t identifier;
  205. void * func;
  206. const char * funcname;
  207. struct arch_frame arch;
  208. };
  209. static inline
  210. void __store_frame (struct pal_frame * frame,
  211. void * func, const char * funcname)
  212. {
  213. arch_store_frame(&frame->arch)
  214. frame->func = func;
  215. frame->funcname = funcname;
  216. asm volatile ("nop" ::: "memory");
  217. frame->identifier = PAL_FRAME_IDENTIFIER;
  218. }
  219. #define ENTER_PAL_CALL(name) \
  220. struct pal_frame frame; \
  221. __store_frame(&frame, &(name), #name)
  222. static inline
  223. void __clear_frame (struct pal_frame * frame)
  224. {
  225. if (frame->identifier == PAL_FRAME_IDENTIFIER) {
  226. asm volatile ("nop" ::: "memory");
  227. frame->identifier = 0;
  228. }
  229. }
  230. #define LEAVE_PAL_CALL() \
  231. do { \
  232. __clear_frame(&frame); \
  233. } while (0)
  234. #define LEAVE_PAL_CALL_RETURN(retval) \
  235. do { \
  236. __clear_frame(&frame); \
  237. return (retval); \
  238. } while (0)
  239. #endif /* PAL_HOST_H */