shim_utils.h 6.7 KB

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