pal.h 14 KB

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