pal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * pal.h
  15. *
  16. * This file contains definition of PAL host ABI.
  17. */
  18. #ifndef PAL_H
  19. #define PAL_H
  20. #include <stdbool.h>
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #include <stdnoreturn.h>
  24. typedef uint64_t PAL_NUM;
  25. typedef const char * PAL_STR;
  26. typedef void * PAL_PTR;
  27. typedef uint32_t PAL_FLG;
  28. typedef uint32_t PAL_IDX;
  29. typedef bool PAL_BOL;
  30. #ifdef IN_PAL
  31. #include <atomic.h>
  32. typedef struct atomic_int PAL_REF;
  33. typedef struct {
  34. PAL_IDX type;
  35. PAL_FLG flags;
  36. } PAL_HDR;
  37. # include "pal_host.h"
  38. # ifndef HANDLE_HDR
  39. # define HANDLE_HDR(handle) (&((handle)->hdr))
  40. # endif
  41. # ifndef TRACE_HEAP
  42. # define TRACE_HEAP(handle) do {} while (0)
  43. # endif
  44. # ifndef UNTRACE_HEAP
  45. # define UNTRACE_HEAP(handle) do {} while (0)
  46. # endif
  47. static inline void init_handle_hdr(PAL_HDR *hdr, int pal_type) {
  48. hdr->type = pal_type;
  49. hdr->flags = 0;
  50. }
  51. # define SET_HANDLE_TYPE(handle, t) \
  52. init_handle_hdr(HANDLE_HDR(handle), pal_type_##t)
  53. # define IS_HANDLE_TYPE(handle, t) \
  54. (HANDLE_HDR(handle)->type == pal_type_##t)
  55. #else
  56. typedef union pal_handle
  57. {
  58. struct {
  59. PAL_IDX type;
  60. /* the PAL-level reference counting is deprecated */
  61. } hdr;
  62. } * PAL_HANDLE;
  63. # ifndef HANDLE_HDR
  64. # define HANDLE_HDR(handle) (&((handle)->hdr))
  65. # endif
  66. #endif /* !IN_PAL */
  67. /* TODO: introduce configuration system in long term and
  68. make SHIM_TCB_USE_GS easily configurable without source code
  69. modification */
  70. //#define SHIM_TCB_USE_GS 1
  71. #undef SHIM_TCB_USE_GS
  72. #if defined(IN_PAL) || defined(SHIM_TCB_USE_GS)
  73. #define PAL_LIBOS_TCB_SIZE 256
  74. typedef struct pal_tcb {
  75. struct pal_tcb * self;
  76. /* uint64_t for alignment */
  77. #ifdef SHIM_TCB_USE_GS
  78. uint64_t libos_tcb[(PAL_LIBOS_TCB_SIZE + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
  79. #endif
  80. /* data private to PAL implementation follows this struct. */
  81. } PAL_TCB;
  82. static inline PAL_TCB * pal_get_tcb (void)
  83. {
  84. PAL_TCB * tcb;
  85. __asm__ ("movq %%gs:%c1,%q0"
  86. : "=r" (tcb)
  87. : "i" (offsetof(struct pal_tcb, self)));
  88. return tcb;
  89. }
  90. #endif
  91. typedef struct {
  92. #ifdef __x86_64__
  93. PAL_NUM r8, r9, r10, r11, r12, r13, r14, r15;
  94. PAL_NUM rdi, rsi, rbp, rbx, rdx, rax, rcx;
  95. PAL_NUM rsp, rip;
  96. PAL_NUM efl, csgsfs, err, trapno, oldmask, cr2;
  97. #else
  98. # error "Unsupported architecture"
  99. #endif
  100. } PAL_CONTEXT;
  101. #define PAL_TRUE true
  102. #define PAL_FALSE false
  103. /********** PAL TYPE DEFINITIONS **********/
  104. enum {
  105. pal_type_none = 0,
  106. pal_type_file,
  107. pal_type_pipe,
  108. pal_type_pipesrv,
  109. pal_type_pipecli,
  110. pal_type_pipeprv,
  111. pal_type_dev,
  112. pal_type_dir,
  113. pal_type_tcp,
  114. pal_type_tcpsrv,
  115. pal_type_udp,
  116. pal_type_udpsrv,
  117. pal_type_process,
  118. pal_type_mcast,
  119. pal_type_thread,
  120. pal_type_mutex,
  121. pal_type_event,
  122. pal_type_gipc,
  123. PAL_HANDLE_TYPE_BOUND,
  124. };
  125. /* PAL identifier poison value */
  126. #define PAL_IDX_POISON ((PAL_IDX) -1)
  127. #define PAL_GET_TYPE(h) (HANDLE_HDR(h)->type)
  128. #define PAL_CHECK_TYPE(h, t) (PAL_GET_TYPE(h) == pal_type_##t)
  129. typedef struct { PAL_PTR start, end; } PAL_PTR_RANGE;
  130. typedef struct {
  131. PAL_NUM cpu_num;
  132. PAL_STR cpu_vendor;
  133. PAL_STR cpu_brand;
  134. PAL_NUM cpu_family;
  135. PAL_NUM cpu_model;
  136. PAL_NUM cpu_stepping;
  137. PAL_STR cpu_flags;
  138. } PAL_CPU_INFO;
  139. typedef struct {
  140. PAL_NUM mem_total;
  141. } PAL_MEM_INFO;
  142. /********** PAL APIs **********/
  143. typedef struct {
  144. PAL_STR host_type;
  145. /* An identifier of current picoprocess */
  146. PAL_NUM process_id;
  147. PAL_NUM host_id;
  148. /***** Handles and executables *****/
  149. /* program manifest */
  150. PAL_HANDLE manifest_handle;
  151. /* executable name */
  152. PAL_STR executable;
  153. /* handle of parent process */
  154. PAL_HANDLE parent_process;
  155. /* handle of first thread */
  156. PAL_HANDLE first_thread;
  157. /* debug stream */
  158. PAL_HANDLE debug_stream;
  159. /* broadcast RPC stream */
  160. PAL_HANDLE broadcast_stream;
  161. /***** Memory layout ******/
  162. /* The range of user address */
  163. PAL_PTR_RANGE user_address;
  164. /* address where executable is loaded */
  165. PAL_PTR_RANGE executable_range;
  166. /* manifest preloaded here */
  167. PAL_PTR_RANGE manifest_preload;
  168. /***** Host information *****/
  169. /* host page size / allocation alignment */
  170. PAL_NUM pagesize, alloc_align;
  171. /* CPU information (only required ones) */
  172. PAL_CPU_INFO cpu_info;
  173. /* Memory information (only required ones) */
  174. PAL_MEM_INFO mem_info;
  175. /* Purely for profiling */
  176. PAL_NUM startup_time;
  177. PAL_NUM host_specific_startup_time;
  178. PAL_NUM relocation_time;
  179. PAL_NUM linking_time;
  180. PAL_NUM manifest_loading_time;
  181. PAL_NUM allocation_time;
  182. PAL_NUM tail_startup_time;
  183. PAL_NUM child_creation_time;
  184. } PAL_CONTROL;
  185. #define pal_control (*pal_control_addr())
  186. PAL_CONTROL * pal_control_addr (void);
  187. /* The ABI includes three calls to allocate, free, and modify the
  188. * permission bits on page-base virtual memory. Permissions in-
  189. * clude read, write, execute, and guard. Memory regions can be
  190. * unallocated, reserved, or backed by committed memory
  191. */
  192. /* Memory Allocation Flags */
  193. #define PAL_ALLOC_RESERVE 0x0001 /* Only reserve the memory */
  194. #ifdef IN_PAL
  195. #define PAL_ALLOC_INTERNAL 0x8000
  196. #endif
  197. /* Memory Protection Flags */
  198. #define PAL_PROT_NONE 0x0 /* 0x0 Page can not be accessed. */
  199. #define PAL_PROT_READ 0x1 /* 0x1 Page can be read. */
  200. #define PAL_PROT_WRITE 0x2 /* 0x2 Page can be written. */
  201. #define PAL_PROT_EXEC 0x4 /* 0x4 Page can be executed. */
  202. #define PAL_PROT_WRITECOPY 0x8 /* 0x8 Copy on write */
  203. #define PAL_PROT_MASK 0xF
  204. // If addr != NULL, then the returned region is always exactly at addr.
  205. PAL_PTR
  206. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  207. PAL_FLG prot);
  208. void
  209. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size);
  210. PAL_BOL
  211. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot);
  212. /* The ABI includes one call to create a child process and one call to
  213. * terminate the running process. A child process does not inherit
  214. * any objects or memory from its parent process and the parent
  215. * process may not modify the execution of its children. A parent can
  216. * wait for a child to exit using its handle. Parent and child may
  217. * communicate through I/O streams provided by the parent to the
  218. * child at creation
  219. */
  220. #define PAL_PROCESS_MASK 0x0
  221. PAL_HANDLE
  222. DkProcessCreate (PAL_STR uri, PAL_STR * args);
  223. noreturn void
  224. DkProcessExit (PAL_NUM exitCode);
  225. /* The stream ABI includes nine calls to open, read, write, map, unmap,
  226. * truncate, flush, delete and wait for I/O streams and three calls to
  227. * access metadata about an I/O stream. The ABI purposefully does not
  228. * provide an ioctl call. Supported URI schemes include file:, pipe:,
  229. * http:, https:, tcp:, udp:, pipe.srv:, http.srv, tcp.srv:, and udp.srv:.
  230. * The latter four schemes are used to open inbound I/O streams for
  231. * server applications.
  232. */
  233. /* DkStreamOpen
  234. * access_mode: WRONLY or RDONLY or RDWR
  235. * share_flags: permission for the created file
  236. * create_flags: the creation options for the file
  237. * options: other options
  238. */
  239. /* Stream Access Flags */
  240. #define PAL_ACCESS_RDONLY 00
  241. #define PAL_ACCESS_WRONLY 01
  242. #define PAL_ACCESS_RDWR 02
  243. #define PAL_ACCESS_APPEND 04
  244. #define PAL_ACCESS_MASK 07
  245. /* Stream Sharing Flags */
  246. #define PAL_SHARE_GLOBAL_X 01
  247. #define PAL_SHARE_GLOBAL_W 02
  248. #define PAL_SHARE_GLOBAL_R 04
  249. #define PAL_SHARE_GROUP_X 010
  250. #define PAL_SHARE_GROUP_W 020
  251. #define PAL_SHARE_GROUP_R 040
  252. #define PAL_SHARE_OWNER_X 0100
  253. #define PAL_SHARE_OWNER_W 0200
  254. #define PAL_SHARE_OWNER_R 0400
  255. #define PAL_SHARE_MASK 0777
  256. /* Stream Create Flags */
  257. #define PAL_CREATE_TRY 0100 /* 0100 Create file if file not
  258. exist (O_CREAT) */
  259. #define PAL_CREATE_ALWAYS 0200 /* 0300 Create file and fail if file
  260. already exist (O_CREAT|O_EXCL) */
  261. #define PAL_CREATE_MASK 0300
  262. /* Stream Option Flags */
  263. #define PAL_OPTION_NONBLOCK 04000
  264. #define PAL_OPTION_MASK 04000
  265. #define WITHIN_MASK(val, mask) (((val)|(mask)) == (mask))
  266. PAL_HANDLE
  267. DkStreamOpen (PAL_STR uri, PAL_FLG access, PAL_FLG share_flags,
  268. PAL_FLG create, PAL_FLG options);
  269. PAL_HANDLE
  270. DkStreamWaitForClient (PAL_HANDLE handle);
  271. PAL_NUM
  272. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  273. PAL_PTR buffer, PAL_PTR source, PAL_NUM size);
  274. PAL_NUM
  275. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  276. PAL_PTR buffer, PAL_STR dest);
  277. #define PAL_DELETE_RD 01
  278. #define PAL_DELETE_WR 02
  279. void
  280. DkStreamDelete (PAL_HANDLE handle, PAL_FLG access);
  281. PAL_PTR
  282. DkStreamMap (PAL_HANDLE handle, PAL_PTR address, PAL_FLG prot,
  283. PAL_NUM offset, PAL_NUM size);
  284. void
  285. DkStreamUnmap (PAL_PTR addr, PAL_NUM size);
  286. /* Sets the length of the file referenced by handle to length. Returns the 0
  287. * on success, a _positive_ errno on failure.
  288. */
  289. PAL_NUM
  290. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length);
  291. PAL_BOL
  292. DkStreamFlush (PAL_HANDLE handle);
  293. PAL_BOL
  294. DkSendHandle (PAL_HANDLE handle, PAL_HANDLE cargo);
  295. PAL_HANDLE
  296. DkReceiveHandle (PAL_HANDLE handle);
  297. /* stream attribute structure */
  298. typedef struct {
  299. PAL_IDX handle_type;
  300. PAL_BOL disconnected;
  301. PAL_BOL nonblocking;
  302. PAL_BOL readable, writable, runnable;
  303. PAL_FLG share_flags;
  304. PAL_NUM pending_size;
  305. union {
  306. struct {
  307. PAL_NUM linger;
  308. PAL_NUM receivebuf, sendbuf;
  309. PAL_NUM receivetimeout, sendtimeout;
  310. PAL_BOL tcp_cork;
  311. PAL_BOL tcp_keepalive;
  312. PAL_BOL tcp_nodelay;
  313. } socket;
  314. };
  315. } PAL_STREAM_ATTR;
  316. PAL_BOL
  317. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr);
  318. PAL_BOL
  319. DkStreamAttributesQueryByHandle (PAL_HANDLE handle,
  320. PAL_STREAM_ATTR * attr);
  321. PAL_BOL
  322. DkStreamAttributesSetByHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  323. PAL_NUM
  324. DkStreamGetName (PAL_HANDLE handle, PAL_PTR buffer, PAL_NUM size);
  325. PAL_BOL
  326. DkStreamChangeName (PAL_HANDLE handle, PAL_STR uri);
  327. /* The ABI supports multithreading through five calls to create,
  328. * sleep, yield the scheduler quantum for, resume execution of, and
  329. * terminate threads, as well as seven calls to create, signal, and
  330. * block on synchronization objects
  331. */
  332. #define PAL_THREAD_MASK 0
  333. PAL_HANDLE
  334. DkThreadCreate (PAL_PTR addr, PAL_PTR param);
  335. // assuming duration to be in microseconds
  336. PAL_NUM
  337. DkThreadDelayExecution (PAL_NUM duration);
  338. void
  339. DkThreadYieldExecution (void);
  340. noreturn void
  341. DkThreadExit (void);
  342. PAL_BOL
  343. DkThreadResume (PAL_HANDLE thread);
  344. /* Exception Handling */
  345. /* arithmetic error (div-by-zero, floating point exception, etc.) */
  346. #define PAL_EVENT_ARITHMETIC_ERROR 1
  347. /* segmentation fault, protection fault, bus fault */
  348. #define PAL_EVENT_MEMFAULT 2
  349. /* illegal instructions */
  350. #define PAL_EVENT_ILLEGAL 3
  351. /* terminated by external program */
  352. #define PAL_EVENT_QUIT 4
  353. /* suspended by external program */
  354. #define PAL_EVENT_SUSPEND 5
  355. /* continued by external program */
  356. #define PAL_EVENT_RESUME 6
  357. /* failure within PAL calls */
  358. #define PAL_EVENT_FAILURE 7
  359. #define PAL_EVENT_NUM_BOUND 8
  360. #define PAL_EVENT_PRIVATE 0x0001 /* upcall specific to thread */
  361. #define PAL_EVENT_RESET 0x0002 /* reset the event upcall */
  362. typedef void (*PAL_EVENT_HANDLER) (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT *);
  363. PAL_BOL
  364. DkSetExceptionHandler (PAL_EVENT_HANDLER handler, PAL_NUM event);
  365. void DkExceptionReturn (PAL_PTR event);
  366. /* parameter: keeping int threadHandle for now (to be in sync with the paper).
  367. * We may want to replace it with a PAL_HANDLE. Ideally, either use PAL_HANDLE
  368. * or threadHandle.
  369. */
  370. /* Create a Mutex.
  371. * initialCount of 0 is totally unlocked; an initialCount of 1
  372. * is initialized to locked. */
  373. PAL_HANDLE
  374. DkMutexCreate (PAL_NUM initialCount);
  375. /* Destroy a mutex using DkObjectClose */
  376. void
  377. DkMutexRelease (PAL_HANDLE mutexHandle);
  378. PAL_HANDLE
  379. DkNotificationEventCreate (PAL_BOL initialState);
  380. PAL_HANDLE
  381. DkSynchronizationEventCreate (PAL_BOL initialState);
  382. /* DkEventDestroy deprecated, replaced by DkObjectClose */
  383. void
  384. DkEventSet (PAL_HANDLE eventHandle);
  385. /* DkEventWait deprecated, replaced by DkObjectsWaitAny */
  386. void
  387. DkEventClear (PAL_HANDLE eventHandle);
  388. #define NO_TIMEOUT ((PAL_NUM)-1)
  389. /* assuming timeout to be in microseconds
  390. * NO_TIMEOUT means no timeout, as the name implies.
  391. */
  392. /* Returns: NULL if the call times out, the ready handle on success */
  393. PAL_HANDLE
  394. DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE * handleArray, PAL_NUM timeout);
  395. /* Deprecate DkObjectReference */
  396. void DkObjectClose (PAL_HANDLE objectHandle);
  397. /* the ABI includes seven assorted calls to get wall clock
  398. * time, generate cryptographically-strong random bits, flush por-
  399. * tions of instruction caches, increment and decrement the reference
  400. * counts on objects shared between threads, and to coordinate
  401. * threads with the security monitor during process serialization
  402. */
  403. /* assuming the time to be in microseconds */
  404. PAL_NUM
  405. DkSystemTimeQuery (void);
  406. /*
  407. * Cryptographically secure random.
  408. * 0 on success, negative on failure.
  409. */
  410. PAL_NUM
  411. DkRandomBitsRead (PAL_PTR buffer, PAL_NUM size);
  412. PAL_BOL
  413. DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size);
  414. #define PAL_SEGMENT_FS 0x1
  415. #define PAL_SEGMENT_GS 0x2
  416. PAL_PTR DkSegmentRegister (PAL_FLG reg, PAL_PTR addr);
  417. PAL_HANDLE
  418. DkCreatePhysicalMemoryChannel (PAL_NUM * key);
  419. PAL_NUM
  420. DkPhysicalMemoryCommit (PAL_HANDLE channel, PAL_NUM entries, PAL_PTR * addrs, PAL_NUM * sizes);
  421. PAL_NUM
  422. DkPhysicalMemoryMap (PAL_HANDLE channel, PAL_NUM entries, PAL_PTR * addrs,
  423. PAL_NUM * sizes, PAL_FLG * prots);
  424. PAL_NUM DkMemoryAvailableQuota (void);
  425. #define PAL_CPUID_WORD_EAX 0
  426. #define PAL_CPUID_WORD_EBX 1
  427. #define PAL_CPUID_WORD_ECX 2
  428. #define PAL_CPUID_WORD_EDX 3
  429. #define PAL_CPUID_WORD_NUM 4
  430. PAL_BOL
  431. DkCpuIdRetrieve (PAL_IDX leaf, PAL_IDX subleaf, PAL_IDX values[4]);
  432. #ifdef __GNUC__
  433. # define symbol_version_default(real, name, version) \
  434. __asm__ (".symver " #real "," #name "@@" #version "\n")
  435. #else
  436. # define symbol_version_default(real, name, version)
  437. #endif
  438. #endif /* PAL_H */