pal.h 14 KB

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