pal_internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_internal.h
  17. *
  18. * This file contains definition of functions, variables and data structures
  19. * for internal uses.
  20. */
  21. #ifndef PAL_INTERNAL_H
  22. #define PAL_INTERNAL_H
  23. #include "pal_defs.h"
  24. #include "pal.h"
  25. #ifndef IN_PAL
  26. # error "pal_internal.h can only be included in PAL"
  27. #endif
  28. #define PAL_FILE(name) XSTRINGIFY(PAL_DIR) "/" name
  29. /* handle_ops is the operators provided for each handler type. They are
  30. mostly used by Stream-related PAL calls, but can also be used by
  31. some others in special ways. */
  32. struct handle_ops {
  33. /* 'getrealpath' return the real path that represent the handle */
  34. const char * (*getrealpath) (PAL_HANDLE handle);
  35. /* 'getname' is used by DkStreamGetName. It's different from
  36. 'getrealpath' */
  37. int (*getname) (PAL_HANDLE handle, char * buffer, int count);
  38. /* 'open' is used by DkStreamOpen. 'handle' is a preallocated handle,
  39. 'type' will be a normalized prefix, 'uri' is the remaining string
  40. of uri */
  41. int (*open) (PAL_HANDLE * handle, const char * type, const char * uri,
  42. int access, int share, int create, int options);
  43. /* 'read' and 'write' is used by DkStreamRead and DkStreamWrite, so
  44. they have exactly same prototype as them. */
  45. int (*read) (PAL_HANDLE handle, int offset, int count, void * buffer);
  46. int (*write) (PAL_HANDLE handle, int offset, int count,
  47. const void * buffer);
  48. /* 'readbyaddr' and 'writebyaddr' are the same as read and write,
  49. but with extra field to specify address */
  50. int (*readbyaddr) (PAL_HANDLE handle, int offset, int count, void * buffer,
  51. char * addr, int addrlen);
  52. int (*writebyaddr) (PAL_HANDLE handle, int offset, int count,
  53. const void * buffer, const char * addr, int addrlen);
  54. /* 'close' and 'delete' is used by DkObjectClose and DkStreamDelete,
  55. 'close' will close the stream, while 'delete' actually destroy
  56. the stream, such as deleting a file or shutting down a socket */
  57. int (*close) (PAL_HANDLE handle);
  58. int (*delete) (PAL_HANDLE handle, int access);
  59. /* 'map' and 'unmap' will map or unmap the handle into memory space,
  60. it's not necessary mapped by mmap, so unmap also needs 'handle'
  61. to deal with special cases */
  62. int (*map) (PAL_HANDLE handle, void ** address, int prot, int offset,
  63. int size);
  64. /* 'setlength' is used by DkStreamFlush. It truncate the stream
  65. to certain size. */
  66. int (*setlength) (PAL_HANDLE handle, int length);
  67. /* 'flush' is used by DkStreamFlush. It syncs the stream to the device */
  68. int (*flush) (PAL_HANDLE handle);
  69. /* 'waitforclient' is used by DkStreamWaitforClient. It accepts an
  70. connection */
  71. int (*waitforclient) (PAL_HANDLE server, PAL_HANDLE *client);
  72. /* 'attrquery' is used by DkStreamAttributesQuery. It queries the
  73. attributes of a stream */
  74. int (*attrquery) (const char * type, const char * uri,
  75. PAL_STREAM_ATTR * attr);
  76. /* 'attrquerybyhdl' is used by DkStreamAttributesQuerybyHandle. It queries
  77. the attributes of a stream handle */
  78. int (*attrquerybyhdl) (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  79. /* 'attrsetbyhdl' is used by DkStreamAttributesSetbyHandle. It queries
  80. the attributes of a stream handle */
  81. int (*attrsetbyhdl) (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  82. /* 'wait' is used for synchronous wait */
  83. int (*wait) (PAL_HANDLE handle, int time);
  84. /* 'rename' is used to change name of a stream, or reset its share
  85. option */
  86. int (*rename) (PAL_HANDLE handle, const char * type, const char * uri);
  87. };
  88. extern const struct handle_ops * pal_handle_ops [];
  89. static inline const struct handle_ops * HANDLE_OPS (PAL_HANDLE handle)
  90. {
  91. int _type = PAL_GET_TYPE(handle);
  92. if (_type <= 0 || _type >= PAL_HANDLE_TYPE_BOUND)
  93. return NULL;
  94. return pal_handle_ops[_type];
  95. }
  96. /* interger hash functions defined inline. The algorithm we used here
  97. is based on Robert Jenkins developed in 96', the algorithm has two
  98. version, 32-bit one and 64-bit one. */
  99. static inline unsigned int hash32 (unsigned int key)
  100. {
  101. key = ~key + (key << 15);
  102. key = key ^ (key >> 12);
  103. key = key + (key << 2);
  104. key = key ^ (key >> 4);
  105. key = (key + (key << 3)) + (key << 11);
  106. key = key ^ (key >> 16);
  107. return key;
  108. }
  109. static inline unsigned int hash64 (unsigned long key)
  110. {
  111. key = (~key) + (key << 21);
  112. key = key ^ (key >> 24);
  113. key = (key + (key << 3)) + (key << 8);
  114. key = key ^ (key >> 14);
  115. key = (key + (key << 2)) + (key << 4);
  116. key = key ^ (key >> 28);
  117. key = key + (key << 31);
  118. return key;
  119. }
  120. /* We allow dynamic size handle allocation. Here is some macro to help
  121. deciding the actual size of the handle */
  122. extern PAL_HANDLE _h;
  123. #define HANDLE_SIZE(type) (sizeof(_h->type))
  124. #define UNKNOWN_HANDLE(handle) \
  125. (PAL_GET_TYPE(handle) == 0 || PAL_GET_TYPE(handle) >= PAL_HANDLE_TYPE_BOUND)
  126. static inline int handle_size (PAL_HANDLE handle)
  127. {
  128. static int handle_sizes[PAL_HANDLE_TYPE_BOUND]
  129. = { 0,
  130. [pal_type_file] = sizeof(handle->file),
  131. [pal_type_pipe] = sizeof(handle->pipe),
  132. [pal_type_pipesrv] = sizeof(handle->pipe),
  133. [pal_type_pipecli] = sizeof(handle->pipe),
  134. [pal_type_pipeprv] = sizeof(handle->pipeprv),
  135. [pal_type_dev] = sizeof(handle->dev),
  136. [pal_type_dir] = sizeof(handle->dir),
  137. [pal_type_tcp] = sizeof(handle->sock),
  138. [pal_type_tcpsrv] = sizeof(handle->sock),
  139. [pal_type_udp] = sizeof(handle->sock),
  140. [pal_type_udpsrv] = sizeof(handle->sock),
  141. [pal_type_process] = sizeof(handle->process),
  142. [pal_type_mcast] = sizeof(handle->mcast),
  143. [pal_type_thread] = sizeof(handle->thread),
  144. [pal_type_semaphore] = sizeof(handle->semaphore),
  145. [pal_type_event] = sizeof(handle->event),
  146. [pal_type_gipc] = sizeof(handle->gipc),
  147. };
  148. if (UNKNOWN_HANDLE(handle))
  149. return 0;
  150. else
  151. return handle_sizes[PAL_GET_TYPE(handle)];
  152. }
  153. #ifndef ENTER_PAL_CALL
  154. # define ENTER_PAL_CALL(name)
  155. #endif
  156. #ifndef LEAVE_PAL_CALL
  157. # define LEAVE_PAL_CALL()
  158. #endif
  159. #ifndef LEAVE_PAL_CALL_RETURN
  160. # define LEAVE_PAL_CALL_RETURN(retval) do { return (retval); } while (0)
  161. #endif
  162. /* failure notify. The rountine is called whenever a PAL call return
  163. error code. As the current design of PAL does not return error
  164. code directly, we rely on DkAsynchronousEventUpcall to handle
  165. PAL call error. If the user does not set up a upcall, the error
  166. code will be ignored. Ignoring PAL error code can be a possible
  167. optimization for SHIM. */
  168. void notify_failure (unsigned long error);
  169. /* all pal config value */
  170. extern struct pal_internal_state {
  171. PAL_NUM instance_id;
  172. PAL_HANDLE parent_process;
  173. const char * manifest;
  174. PAL_HANDLE manifest_handle;
  175. const char * exec;
  176. PAL_HANDLE exec_handle;
  177. PAL_HANDLE log_stream;
  178. enum {
  179. LOG_FILE = 0x01,
  180. LOG_PIPE = 0x02,
  181. LOG_SOCKET = 0x04,
  182. LOG_GENERIC_TYPES = 0x08,
  183. } log_types;
  184. struct config_store * root_config;
  185. unsigned long pagesize;
  186. unsigned long alloc_align, alloc_shift, alloc_mask;
  187. PAL_HANDLE console;
  188. const char * syscall_sym_name;
  189. void * syscall_sym_addr;
  190. unsigned long start_time;
  191. #if PROFILING == 1
  192. unsigned long relocation_time;
  193. unsigned long linking_time;
  194. unsigned long manifest_loading_time;
  195. unsigned long slab_time;
  196. unsigned long tail_startup_time;
  197. unsigned long process_create_time;
  198. #endif
  199. } pal_state;
  200. #ifdef __GNUC__
  201. #define BREAK() \
  202. do { \
  203. asm volatile ("int $3"); \
  204. } while (0)
  205. #else
  206. #define BREAK()
  207. #endif
  208. extern PAL_CONTROL __pal_control;
  209. #define ALLOC_ALIGNDOWN(addr) \
  210. (((unsigned long) (addr)) & pal_state.alloc_mask)
  211. #define ALLOC_ALIGNUP(addr) \
  212. ((((unsigned long) (addr)) + pal_state.alloc_shift) & pal_state.alloc_mask)
  213. #define ALLOC_ALIGNED(addr) \
  214. ((unsigned long) (addr) == \
  215. (((unsigned long) (addr)) & pal_state.alloc_mask))
  216. /* Main initialization function */
  217. void pal_main (
  218. PAL_NUM instance_id, /* current instance id */
  219. PAL_HANDLE manifest_handle, /* manifest handle if opened */
  220. PAL_HANDLE exec_handle, /* executable handle if opened */
  221. PAL_PTR exec_loaded_addr, /* executable addr if loaded */
  222. PAL_HANDLE parent_process, /* parent process if it's a child */
  223. PAL_HANDLE first_thread, /* first thread handle */
  224. PAL_STR * arguments, /* application arguments */
  225. PAL_STR * environments /* environment variables */
  226. );
  227. /* For initialization */
  228. unsigned long _DkGetPagesize (void);
  229. unsigned long _DkGetAllocationAlignment (void);
  230. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end);
  231. bool _DkCheckMemoryMappable (const void * addr, int size);
  232. PAL_NUM _DkGetProcessId (void);
  233. PAL_NUM _DkGetHostId (void);
  234. unsigned long _DkMemoryQuota (void);
  235. unsigned long _DkMemoryAvailableQuota (void);
  236. void _DkGetCPUInfo (PAL_CPU_INFO * info);
  237. /* Internal DK calls, in case any of the internal routines needs to use them */
  238. /* DkStream calls */
  239. int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
  240. int access, int share, int create, int options);
  241. int _DkStreamDelete (PAL_HANDLE handle, int access);
  242. int _DkStreamRead (PAL_HANDLE handle, int offset, int count, void * buf,
  243. char * addr, int addrlen);
  244. int _DkStreamWrite (PAL_HANDLE handle, int offset, int count,
  245. const void * buf, const char * addr, int addrlen);
  246. int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr);
  247. int _DkStreamAttributesQuerybyHandle (PAL_HANDLE hdl, PAL_STREAM_ATTR * attr);
  248. int _DkStreamMap (PAL_HANDLE handle, void ** addr, int prot, int offset,
  249. int size);
  250. int _DkStreamUnmap (void * addr, int size);
  251. int _DkStreamSetLength (PAL_HANDLE handle, int length);
  252. int _DkStreamFlush (PAL_HANDLE handle);
  253. int _DkStreamGetName (PAL_HANDLE handle, char * buf, int size);
  254. const char * _DkStreamRealpath (PAL_HANDLE hdl);
  255. int _DkStreamFile (PAL_HANDLE hdl, PAL_HANDLE * file);
  256. int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo);
  257. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo);
  258. PAL_HANDLE _DkBroadcastStreamOpen (void);
  259. /* DkProcess and DkThread calls */
  260. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  261. const void * parem, int flags);
  262. void _DkThreadExit (void);
  263. int _DkThreadDelayExecution (unsigned long * duration);
  264. void _DkThreadYieldExecution (void);
  265. int _DkThreadResume (PAL_HANDLE threadHandle);
  266. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri,
  267. int flags, const char ** args);
  268. void _DkProcessExit (int exitCode);
  269. int _DkProcessSandboxCreate (const char * manifest, int flags);
  270. /* DkSemaphore calls */
  271. int _DkSemaphoreCreate (PAL_HANDLE handle, int initialCount, int maxCount);
  272. void _DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle);
  273. int _DkSemaphoreAcquire (PAL_HANDLE sem, int count);
  274. int _DkSemaphoreAcquireTimeout (PAL_HANDLE sem, int count, int timeout);
  275. void _DkSemaphoreRelease (PAL_HANDLE sem, int count);
  276. int _DkSemaphoreGetCurrentCount (PAL_HANDLE sem);
  277. /* DkEvent calls */
  278. int _DkEventCreate (PAL_HANDLE * event, bool initialState,
  279. bool isnotification);
  280. void _DkEventDestroy (PAL_HANDLE handle);
  281. int _DkEventSet (PAL_HANDLE event, int wakeup);
  282. int _DkEventWaitTimeout (PAL_HANDLE event, int timeout);
  283. int _DkEventWait (PAL_HANDLE event);
  284. int _DkEventClear (PAL_HANDLE event);
  285. /* DkVirtualMemory calls */
  286. int _DkVirtualMemoryAlloc (void ** paddr, int size, int alloc_type, int prot);
  287. int _DkVirtualMemoryFree (void * addr, int size);
  288. int _DkVirtualMemoryProtect (void * addr, int size, int prot);
  289. /* DkObject calls */
  290. int _DkObjectReference (PAL_HANDLE objectHandle);
  291. int _DkObjectClose (PAL_HANDLE objectHandle);
  292. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, int timeout,
  293. PAL_HANDLE * polled);
  294. /* DkException calls & structures */
  295. typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
  296. int (*_DkExceptionHandlers[PAL_EVENT_NUM_BOUND]) (int, PAL_UPCALL, int);
  297. void _DkRaiseFailure (int error);
  298. void _DkExceptionReturn (void * event);
  299. /* other DK calls */
  300. int _DkInternalLock (PAL_LOCK * mut);
  301. int _DkInternalUnlock (PAL_LOCK * mut);
  302. unsigned long _DkSystemTimeQuery (void);
  303. int _DkFastRandomBitsRead (void * buffer, int size);
  304. int _DkRandomBitsRead (void * buffer, int size);
  305. int _DkSegmentRegisterSet (int reg, const void * addr);
  306. int _DkSegmentRegisterGet (int reg, void ** addr);
  307. int _DkInstructionCacheFlush (const void * addr, int size);
  308. int _DkCreatePhysicalMemoryChannel (PAL_HANDLE * handle, unsigned long * key);
  309. int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries,
  310. PAL_PTR * addrs, PAL_NUM * sizes, int flags);
  311. int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries,
  312. PAL_PTR * addrs, PAL_NUM * sizes, PAL_FLG * prots);
  313. int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf, unsigned int values[4]);
  314. unsigned long _DkHandleCompatibilityException (unsigned long syscallno,
  315. unsigned long args[6]);
  316. #define init_fail(exitcode, reason) \
  317. do { \
  318. printf("PAL failed at " __FILE__ ":%s:%u (exitcode = %u, reason=%s)\n", \
  319. __FUNCTION__, (unsigned int)__LINE__, \
  320. (unsigned int) (exitcode), (const char *) (reason)); \
  321. _DkProcessExit(exitcode); \
  322. } while (0)
  323. /* function and definition for loading binaries */
  324. enum object_type { OBJECT_RTLD, OBJECT_EXEC, OBJECT_PRELOAD, OBJECT_EXTERNAL };
  325. int check_elf_object (PAL_HANDLE handle);
  326. int load_elf_object (const char * uri, enum object_type type);
  327. int load_elf_object_by_handle (PAL_HANDLE handle, enum object_type type);
  328. int add_elf_object(void * addr, PAL_HANDLE handle, int type);
  329. #ifndef NO_INTERNAL_ALLOC
  330. void init_slab_mgr (int alignment);
  331. void * malloc (int size);
  332. void * remalloc (const void * mem, int size);
  333. void * calloc (int nmem, int size);
  334. void free (void * mem);
  335. #endif
  336. #ifdef __GNUC__
  337. # define __attribute_hidden __attribute__ ((visibility ("hidden")))
  338. # define __attribute_always_inline __attribute__((always_inline))
  339. # define __attribute_unused __attribute__((unused))
  340. # define __attribute_noinline __attribute__((noinline))
  341. #else
  342. # define __attribute_hidden
  343. # define __attribute_always_inline
  344. # define __attribute_unused
  345. # define __attribute_noinline
  346. #endif
  347. #define alias_str(name) #name
  348. #ifdef __GNUC__
  349. # define extern_alias(name) \
  350. extern __typeof(name) pal_##name __attribute ((alias (alias_str(name))))
  351. #else
  352. # define extern_alias(name)
  353. #endif
  354. void _DkPrintConsole (const void * buf, int size);
  355. int printf (const char *fmt, ...);
  356. void write_log (int nstrs, ...);
  357. static inline void log_stream (const char * uri)
  358. {
  359. if (!uri || !pal_state.log_stream)
  360. return;
  361. bool logging = false;
  362. if ((pal_state.log_types & LOG_FILE) &&
  363. uri[0] == 'f' && uri[1] == 'i' && uri[2] == 'l' && uri[3] == 'e')
  364. logging = true;
  365. if ((pal_state.log_types & LOG_PIPE) &&
  366. uri[0] == 'p' && uri[1] == 'i' && uri[2] == 'p' && uri[3] == 'e')
  367. logging = true;
  368. if ((pal_state.log_types & LOG_SOCKET) &&
  369. uri[0] == 't' && uri[1] == 'c' && uri[2] == 'p')
  370. logging = true;
  371. if ((pal_state.log_types & LOG_SOCKET) &&
  372. uri[0] == 'u' && uri[1] == 'd' && uri[2] == 'p')
  373. logging = true;
  374. if (logging)
  375. write_log(2, uri, "\n");
  376. }
  377. #endif