pal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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.h
  17. *
  18. * This file contains definition of PAL host ABI.
  19. */
  20. #ifndef PAL_H
  21. #define PAL_H
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <stddef.h>
  25. #ifdef __x86_64__
  26. typedef unsigned long PAL_NUM;
  27. #endif
  28. typedef const char * PAL_STR;
  29. typedef const void * PAL_PTR;
  30. typedef void * PAL_BUF;
  31. typedef unsigned int PAL_FLG;
  32. typedef unsigned int PAL_IDX;
  33. typedef char PAL_CHR;
  34. typedef bool PAL_BOL;
  35. typedef struct {
  36. #ifdef __x86_64__
  37. uint64_t r8, r9, r10, r11, r12, r13, r14, r15, rdi, rsi, rbp, rbx, rdx, rax,
  38. rcx, rsp, rip, efl, csgsfs, err, trapno, oldmask, cr2;
  39. #endif
  40. } PAL_CONTEXT;
  41. #define PAL_TRUE true
  42. #define PAL_FALSE false
  43. /********** PAL TYPE DEFINITIONS **********/
  44. enum {
  45. pal_type_none = 0,
  46. pal_type_file,
  47. pal_type_pipe,
  48. pal_type_pipesrv,
  49. pal_type_pipecli,
  50. pal_type_pipeprv,
  51. pal_type_dev,
  52. pal_type_dir,
  53. pal_type_tcp,
  54. pal_type_tcpsrv,
  55. pal_type_udp,
  56. pal_type_udpsrv,
  57. pal_type_process,
  58. pal_type_mcast,
  59. pal_type_thread,
  60. pal_type_semaphore,
  61. pal_type_event,
  62. pal_type_gipc,
  63. PAL_HANDLE_TYPE_BOUND,
  64. };
  65. #ifdef IN_PAL
  66. struct atomic_int {
  67. volatile int counter;
  68. } __attribute__((aligned(sizeof(int))));
  69. typedef struct atomic_int PAL_REF;
  70. typedef struct {
  71. PAL_IDX type;
  72. PAL_REF ref;
  73. PAL_FLG flags;
  74. } PAL_HDR;
  75. # include "pal_host.h"
  76. # define SET_HANDLE_TYPE(handle, t) \
  77. do { \
  78. (handle)->__in.type = pal_type_##t; \
  79. (handle)->__in.ref.counter = 0; \
  80. (handle)->__in.flags = 0; \
  81. } while (0)
  82. # define IS_HANDLE_TYPE(handle, t) \
  83. ((handle)->__in.type == pal_type_##t)
  84. #else
  85. typedef union pal_handle
  86. {
  87. struct {
  88. PAL_IDX type;
  89. } __in;
  90. } * PAL_HANDLE;
  91. #endif /* !IN_PAL */
  92. /* PAL identifier poison value */
  93. #define PAL_IDX_POISON ((PAL_IDX) -1)
  94. #define PAL_GET_TYPE(h) ((h)->__in.type)
  95. #define PAL_CHECK_TYPE(h, t) (__PAL_GET_TYPE(h) == pal_type_##t)
  96. typedef struct { PAL_PTR start, end; } PAL_PTR_RANGE;
  97. /********** PAL APIs **********/
  98. typedef struct {
  99. /* An identifier of current picoprocess */
  100. PAL_NUM process_id;
  101. PAL_NUM host_id;
  102. /***** Handles and executables *****/
  103. /* program manifest */
  104. PAL_HANDLE manifest_handle;
  105. /* executable name */
  106. PAL_STR executable;
  107. /* handle of parent process */
  108. PAL_HANDLE parent_process;
  109. /* handle of first thread */
  110. PAL_HANDLE first_thread;
  111. /* debug stream */
  112. PAL_HANDLE debug_stream;
  113. /* broadcast RPC stream */
  114. PAL_HANDLE broadcast_stream;
  115. /***** Memory layout ******/
  116. /* The range of user address */
  117. PAL_PTR_RANGE user_address;
  118. /* address where executable is loaded */
  119. PAL_PTR_RANGE executable_range;
  120. /***** Host information *****/
  121. /* host page size / allocation alignment */
  122. PAL_NUM pagesize, alloc_align;
  123. /* CPU information (only required ones) */
  124. struct {
  125. PAL_NUM cpu_num;
  126. PAL_STR cpu_vendor;
  127. PAL_STR cpu_brand;
  128. PAL_NUM cpu_family;
  129. PAL_NUM cpu_model;
  130. PAL_NUM cpu_stepping;
  131. PAL_STR cpu_flags;
  132. } cpu_info;
  133. /* Memory information (only required ones) */
  134. struct {
  135. PAL_NUM mem_total;
  136. } mem_info;
  137. /* Purely for profiling */
  138. PAL_NUM startup_time;
  139. PAL_NUM host_specific_startup_time;
  140. PAL_NUM relocation_time;
  141. PAL_NUM linking_time;
  142. PAL_NUM manifest_loading_time;
  143. PAL_NUM allocation_time;
  144. PAL_NUM tail_startup_time;
  145. PAL_NUM child_creation_time;
  146. } PAL_CONTROL;
  147. #define pal_control (*pal_control_addr())
  148. PAL_CONTROL * pal_control_addr (void);
  149. /* The ABI includes three calls to allocate, free, and modify the
  150. * permission bits on page-base virtual memory. Permissions in-
  151. * clude read, write, execute, and guard. Memory regions can be
  152. * unallocated, reserved, or backed by committed memory
  153. */
  154. /* Memory Allocation Flags */
  155. #define PAL_ALLOC_32BIT 0x0001 /* Only give out 32-bit addresses */
  156. #define PAL_ALLOC_RESERVE 0x0002 /* Only reserve the memory */
  157. /* Memory Protection Flags */
  158. #define PAL_PROT_NONE 0x0 /* 0x0 Page can not be accessed. */
  159. #define PAL_PROT_READ 0x1 /* 0x1 Page can be read. */
  160. #define PAL_PROT_WRITE 0x2 /* 0x2 Page can be written. */
  161. #define PAL_PROT_EXEC 0x4 /* 0x4 Page can be executed. */
  162. #define PAL_PROT_WRITECOPY 0x8 /* 0x8 Copy on write */
  163. PAL_PTR
  164. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  165. PAL_FLG prot);
  166. void
  167. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size);
  168. PAL_BOL
  169. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot);
  170. /* The ABI includes one call to create a child process and one call to
  171. * terminate the running process. A child process does not inherit
  172. * any objects or memory from its parent process and the parent
  173. * process may not modify the execution of its children. A parent can
  174. * wait for a child to exit using its handle. Parent and child may
  175. * communicate through I/O streams provided by the parent to the
  176. * child at creation
  177. */
  178. #define PAL_PROCESS_MASK 0x0
  179. PAL_HANDLE
  180. DkProcessCreate (PAL_STR uri, PAL_FLG flags, PAL_STR * args);
  181. void
  182. DkProcessExit (PAL_NUM exitCode);
  183. #define PAL_SANDBOX_PIPE 0x1
  184. PAL_BOL
  185. DkProcessSandboxCreate (PAL_STR manifest, PAL_FLG flags);
  186. /* The stream ABI includes nine calls to open, read, write, map, unmap,
  187. * truncate, flush, delete and wait for I/O streams and three calls to
  188. * access metadata about an I/O stream. The ABI purposefully does not
  189. * provide an ioctl call. Supported URI schemes include file:, pipe:,
  190. * http:, https:, tcp:, udp:, pipe.srv:, http.srv, tcp.srv:, and udp.srv:.
  191. * The latter four schemes are used to open inbound I/O streams for
  192. * server applications.
  193. */
  194. /* DkStreamOpen
  195. * access_mode: WRONLY or RDONLY or RDWR
  196. * share_flags: permission for the created file
  197. * create_flags: the creation options for the file
  198. * options: other options
  199. */
  200. /* Stream Access Flags */
  201. #define PAL_ACCESS_RDONLY 00
  202. #define PAL_ACCESS_WRONLY 01
  203. #define PAL_ACCESS_RDWR 02
  204. #define PAL_ACCESS_APPEND 04
  205. #define PAL_ACCESS_MASK 07
  206. /* Stream Sharing Flags */
  207. #define PAL_SHARE_GLOBAL_X 01
  208. #define PAL_SHARE_GLOBAL_W 02
  209. #define PAL_SHARE_GLOBAL_R 04
  210. #define PAL_SHARE_GROUP_X 010
  211. #define PAL_SHARE_GROUP_W 020
  212. #define PAL_SHARE_GROUP_R 040
  213. #define PAL_SHARE_OWNER_X 0100
  214. #define PAL_SHARE_OWNER_W 0200
  215. #define PAL_SHARE_OWNER_R 0400
  216. #define PAL_SHARE_MASK 0777
  217. /* Stream Create Flags */
  218. #define PAL_CREAT_TRY 0100 /* 0100 Create file if file not
  219. exist (O_CREAT) */
  220. #define PAL_CREAT_ALWAYS 0200 /* 0300 Create file and fail if file
  221. already exist (O_CREAT|O_EXCL) */
  222. #define PAL_CREAT_MASK 0300
  223. /* Stream Option Flags */
  224. #define PAL_OPTION_NONBLOCK 04000
  225. #define PAL_OPTION_MASK 04000
  226. PAL_HANDLE
  227. DkStreamOpen (PAL_STR uri, PAL_FLG access, PAL_FLG share_flags,
  228. PAL_FLG create, PAL_FLG options);
  229. PAL_HANDLE
  230. DkStreamWaitForClient (PAL_HANDLE handle);
  231. PAL_NUM
  232. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  233. PAL_BUF buffer, PAL_BUF source, PAL_NUM size);
  234. PAL_NUM
  235. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  236. PAL_PTR buffer, PAL_STR dest);
  237. #define PAL_DELETE_RD 01
  238. #define PAL_DELETE_WR 02
  239. void
  240. DkStreamDelete (PAL_HANDLE handle, PAL_FLG access);
  241. PAL_PTR
  242. DkStreamMap (PAL_HANDLE handle, PAL_PTR address, PAL_FLG prot,
  243. PAL_NUM offset, PAL_NUM size);
  244. void
  245. DkStreamUnmap (PAL_PTR addr, PAL_NUM size);
  246. PAL_NUM
  247. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length);
  248. PAL_BOL
  249. DkStreamFlush (PAL_HANDLE handle);
  250. PAL_BOL
  251. DkSendHandle (PAL_HANDLE handle, PAL_HANDLE cargo);
  252. PAL_HANDLE
  253. DkReceiveHandle (PAL_HANDLE handle);
  254. /* stream attribute structure */
  255. typedef struct {
  256. PAL_IDX handle_type;
  257. PAL_BOL disconnected;
  258. PAL_BOL nonblocking;
  259. PAL_BOL readable, writeable, runnable;
  260. PAL_FLG share_flags;
  261. PAL_NUM pending_size;
  262. union {
  263. struct {
  264. PAL_NUM linger;
  265. PAL_NUM receivebuf, sendbuf;
  266. PAL_NUM receivetimeout, sendtimeout;
  267. PAL_BOL tcp_cork;
  268. PAL_BOL tcp_keepalive;
  269. PAL_BOL tcp_nodelay;
  270. } socket;
  271. };
  272. } PAL_STREAM_ATTR;
  273. PAL_BOL
  274. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr);
  275. PAL_BOL
  276. DkStreamAttributesQuerybyHandle (PAL_HANDLE handle,
  277. PAL_STREAM_ATTR * attr);
  278. PAL_BOL
  279. DkStreamAttributesSetbyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  280. PAL_NUM
  281. DkStreamGetName (PAL_HANDLE handle, PAL_BUF buffer, PAL_NUM size);
  282. PAL_BOL
  283. DkStreamChangeName (PAL_HANDLE handle, PAL_STR uri);
  284. /* The ABI supports multithreading through five calls to create,
  285. * sleep, yield the scheduler quantum for, resume execution of, and
  286. * terminate threads, as well as seven calls to create, signal, and
  287. * block on synchronization objects
  288. */
  289. #define PAL_THREAD_MASK 0
  290. PAL_HANDLE
  291. DkThreadCreate (PAL_PTR addr, PAL_PTR param, PAL_FLG flags);
  292. // assuming duration to be in microseconds
  293. PAL_NUM
  294. DkThreadDelayExecution (PAL_NUM duration);
  295. void
  296. DkThreadYieldExecution (void);
  297. void
  298. DkThreadExit (void);
  299. PAL_BOL
  300. DkThreadResume (PAL_HANDLE thread);
  301. /* Exception Handling */
  302. /* Div-by-zero */
  303. #define PAL_EVENT_DIVZERO 1
  304. /* segmentation fault, protection fault, bus fault */
  305. #define PAL_EVENT_MEMFAULT 2
  306. /* illegal instructions */
  307. #define PAL_EVENT_ILLEGAL 3
  308. /* terminated by external program */
  309. #define PAL_EVENT_QUIT 4
  310. /* suspended by external program */
  311. #define PAL_EVENT_SUSPEND 5
  312. /* continued by external program */
  313. #define PAL_EVENT_RESUME 6
  314. /* failure within PAL calls */
  315. #define PAL_EVENT_FAILURE 7
  316. #define PAL_EVENT_NUM_BOUND 8
  317. #define PAL_EVENT_PRIVATE 0x0001 /* upcall specific to thread */
  318. #define PAL_EVENT_RESET 0x0002 /* reset the event upcall */
  319. typedef void (*PAL_EVENT_HANDLER) (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT *);
  320. PAL_BOL
  321. DkSetExceptionHandler (PAL_EVENT_HANDLER handler, PAL_NUM event,
  322. PAL_FLG flags);
  323. void DkExceptionReturn (PAL_PTR event);
  324. /* parameter: keeping int threadHandle for now (to be in sync with the paper).
  325. * We may want to replace it with a PAL_HANDLE. Ideally, either use PAL_HANDLE
  326. * or threadHandle.
  327. */
  328. PAL_HANDLE
  329. DkSemaphoreCreate (PAL_NUM initialCount, PAL_NUM maxCount);
  330. /* DkSemaphoreDestroy deprecated, replaced by DkObjectClose */
  331. /* TSAI: I preserve this API because DkObjectsWaitAny can't acquire multiple
  332. * counts of a semaphore. Acquiring multiple counts is required for
  333. * implementing a read-write-lock. To make this API complementary to
  334. * DkObjectsWaitAny, I added a 'timeout' to its arguments */
  335. /* DkSemaphoreAcquire deprecated */
  336. void
  337. DkSemaphoreRelease (PAL_HANDLE semaphoreHandle, PAL_NUM count);
  338. /* DkSemaphoreGetCurrentCount deprecated */
  339. PAL_HANDLE
  340. DkNotificationEventCreate (PAL_BOL initialState);
  341. PAL_HANDLE
  342. DkSynchronizationEventCreate (PAL_BOL initialState);
  343. /* DkEventDestroy deprecated, replaced by DkObjectClose */
  344. void
  345. DkEventSet (PAL_HANDLE eventHandle);
  346. /* DkEventWait deprecated, replaced by DkObjectsWaitAny */
  347. void
  348. DkEventClear (PAL_HANDLE eventHandle);
  349. #define NO_TIMEOUT ((PAL_NUM) -1)
  350. /* assuming timeout to be in microseconds */
  351. PAL_HANDLE
  352. DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE * handleArray, PAL_NUM timeout);
  353. void DkObjectReference (PAL_HANDLE objectHandle);
  354. void DkObjectClose (PAL_HANDLE objectHandle);
  355. /* the ABI includes seven assorted calls to get wall clock
  356. * time, generate cryptographically-strong random bits, flush por-
  357. * tions of instruction caches, increment and decrement the reference
  358. * counts on objects shared between threads, and to coordinate
  359. * threads with the security monitor during process serialization
  360. */
  361. /* assuming the time to be in microseconds */
  362. PAL_NUM
  363. DkSystemTimeQuery (void);
  364. PAL_NUM
  365. DkRandomBitsRead (PAL_BUF buffer, PAL_NUM size);
  366. PAL_BOL
  367. DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size);
  368. #define PAL_SEGMENT_FS 0x1
  369. #define PAL_SEGMENT_GS 0x2
  370. PAL_PTR DkSegmentRegister (PAL_FLG reg, PAL_PTR addr);
  371. PAL_HANDLE
  372. DkCreatePhysicalMemoryChannel (PAL_NUM * key);
  373. PAL_NUM
  374. DkPhysicalMemoryCommit (PAL_HANDLE channel, PAL_NUM entries, PAL_PTR * addrs,
  375. PAL_NUM * sizes, PAL_FLG flags);
  376. PAL_NUM
  377. DkPhysicalMemoryMap (PAL_HANDLE channel, PAL_NUM entries, PAL_PTR * addrs,
  378. PAL_NUM * sizes, PAL_FLG * prots);
  379. PAL_NUM DkMemoryAvailableQuota (void);
  380. PAL_BOL
  381. DkCpuIdRetrieve (PAL_NUM level, PAL_NUM values[4]);
  382. #endif /* PAL_H */