shim_utils.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_utils.h
  17. */
  18. #ifndef _SHIM_UTILS_H_
  19. #define _SHIM_UTILS_H_
  20. #include <shim_handle.h>
  21. #include <pal.h>
  22. #include <list.h>
  23. #include <api.h>
  24. struct shim_handle;
  25. void sysparser_printf (const char * fmt, ...);
  26. /* string object */
  27. struct shim_str * get_str_obj (void);
  28. int free_str_obj (struct shim_str * str);
  29. int init_str_mgr (void);
  30. /* qstring object */
  31. #define QSTR_INIT { .len = 0, .oflow = NULL }
  32. static inline const char * qstrgetstr (const struct shim_qstr * qstr)
  33. {
  34. return qstr->oflow ? qstr->oflow->str : qstr->name;
  35. }
  36. static inline void qstrfree (struct shim_qstr * qstr)
  37. {
  38. if (qstr->oflow) {
  39. free_str_obj(qstr->oflow);
  40. qstr->oflow = NULL;
  41. }
  42. qstr->name[0] = 0;
  43. qstr->len = 0;
  44. }
  45. static inline char * qstrsetstr (struct shim_qstr * qstr,
  46. const char * str, size_t size)
  47. {
  48. if (!str) {
  49. qstrfree(qstr);
  50. return NULL;
  51. }
  52. if (size >= STR_SIZE)
  53. return NULL;
  54. char * buf = qstr->name;
  55. if (size >= QSTR_SIZE) {
  56. if (!qstr->oflow) {
  57. qstr->oflow = get_str_obj();
  58. if (!qstr->oflow)
  59. return NULL;
  60. }
  61. buf = qstr->oflow->str;
  62. } else {
  63. if (qstr->oflow) {
  64. free_str_obj(qstr->oflow);
  65. qstr->oflow = NULL;
  66. }
  67. }
  68. memcpy(buf, str, size);
  69. buf[size] = 0;
  70. qstr->len = size;
  71. return buf;
  72. }
  73. static inline char * qstrsetstrs (struct shim_qstr * qstr,
  74. int nstrs,
  75. const char ** strs, size_t * sizes)
  76. {
  77. size_t total_size = 0;
  78. for (int i = 0 ; i < nstrs ; i++)
  79. total_size += sizes[i];
  80. if (total_size >= STR_SIZE)
  81. return NULL;
  82. char * buf = qstr->name;
  83. if (total_size >= QSTR_SIZE) {
  84. if (!qstr->oflow) {
  85. // TODO: alloc proper size.
  86. qstr->oflow = get_str_obj();
  87. if (!qstr->oflow)
  88. return NULL;
  89. }
  90. buf = qstr->oflow->str;
  91. }
  92. char * ptr = buf;
  93. qstr->len = 0;
  94. for (int i = 0 ; i < nstrs ; i++) {
  95. int size = sizes[i];
  96. memcpy(ptr, strs[i], size);
  97. ptr[size] = 0;
  98. qstr->len += size;
  99. ptr += size;
  100. }
  101. return buf;
  102. }
  103. static inline int qstrempty (const struct shim_qstr * qstr)
  104. {
  105. return qstr->len == 0;
  106. }
  107. static inline void qstrcopy (struct shim_qstr * to,
  108. const struct shim_qstr * from)
  109. {
  110. qstrsetstr(to, qstrgetstr(from), from->len);
  111. to->hash = from->hash;
  112. }
  113. static inline int qstrcmpstr (const struct shim_qstr * qstr,
  114. const char * str, size_t size)
  115. {
  116. if (qstr->len != size)
  117. return 1;
  118. return memcmp(qstrgetstr(qstr), str, size);
  119. }
  120. //#define SLAB_DEBUG_PRINT
  121. //#define SLAB_DEBUG_TRACE
  122. /* heap allocation functions */
  123. int init_slab (void);
  124. #if defined(SLAB_DEBUG_PRINT) || defined(SLAB_DEBUG_TRACE)
  125. void * __malloc_debug (size_t size, const char * file, int line);
  126. #define malloc(size) __malloc_debug((size), __FILE__, __LINE__)
  127. void __free_debug (void * mem, const char * file, int line);
  128. #define free(mem) __free_debug((mem), __FILE__, __LINE__)
  129. void * __malloc_copy_debug (const void * mem, size_t size,
  130. const char * file, int line);
  131. #define malloc_copy(mem, size) __malloc_copy_debug((mem), (size), __FILE__, __LINE__)
  132. #else
  133. void * malloc (size_t size);
  134. void free (void * mem);
  135. void * malloc_copy (const void * mem, size_t size);
  136. #endif
  137. static_always_inline
  138. char * qstrtostr (struct shim_qstr * qstr, bool on_stack)
  139. {
  140. int len = qstr->len;
  141. char * buf = on_stack ? __alloca(len + 1) : malloc(len + 1);
  142. if (!buf)
  143. return NULL;
  144. memcpy(buf, qstrgetstr(qstr), len);
  145. buf[len] = 0;
  146. return buf;
  147. }
  148. /* typedef a 32 bit type */
  149. # ifndef UINT4
  150. # define UINT4 uint32_t
  151. # endif
  152. /* Data structure for MD5 (Message Digest) computation */
  153. struct shim_md5_ctx {
  154. UINT4 i[2]; /* number of _bits_ handled mod 2^64 */
  155. UINT4 buf[4]; /* scratch buffer */
  156. unsigned char in[64]; /* input buffer */
  157. unsigned char digest[16]; /* actual digest after MD5Final call */
  158. };
  159. void md5_init (struct shim_md5_ctx * mdContext);
  160. void md5_update (struct shim_md5_ctx * mdContext, const void * buf,
  161. size_t len);
  162. void md5_final (struct shim_md5_ctx * mdContext);
  163. /* prompt user for confirmation */
  164. int message_confirm (const char * message, const char * options);
  165. /* ELF binary loading */
  166. int check_elf_object (struct shim_handle * file);
  167. int load_elf_object (struct shim_handle * file, void * addr, size_t mapped);
  168. int load_elf_interp (struct shim_handle * exec);
  169. int free_elf_interp (void);
  170. int execute_elf_object (struct shim_handle * exec,
  171. int * argcp, const char ** argp,
  172. int nauxv, elf_auxv_t * auxp);
  173. int remove_loaded_libraries (void);
  174. /* gdb debugging support */
  175. void remove_r_debug (void * addr);
  176. void append_r_debug (const char * uri, void * addr, void * dyn_addr);
  177. void clean_link_map_list (void);
  178. /* create unique files/pipes */
  179. #define PIPE_URI_SIZE 40
  180. int create_pipe (IDTYPE * pipeid, char * uri, size_t size, PAL_HANDLE * hdl,
  181. struct shim_qstr * qstr);
  182. int create_dir (const char * prefix, char * path, size_t size,
  183. struct shim_handle ** hdl);
  184. int create_file (const char * prefix, char * path, size_t size,
  185. struct shim_handle ** hdl);
  186. int create_handle (const char * prefix, char * path, size_t size,
  187. PAL_HANDLE * hdl, unsigned int * id);
  188. /* Asynchronous event support */
  189. int init_async (void);
  190. int64_t install_async_event (PAL_HANDLE object, unsigned long time,
  191. void (*callback) (IDTYPE caller, void * arg),
  192. void * arg);
  193. int create_async_helper (void);
  194. struct shim_thread * terminate_async_helper (void);
  195. extern struct config_store * root_config;
  196. #endif /* _SHIM_UTILS_H */