shim_handle.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_handle.h
  15. *
  16. * Definitions of types and functions for file/handle bookkeeping.
  17. */
  18. #ifndef _SHIM_HANDLE_H_
  19. #define _SHIM_HANDLE_H_
  20. #include <asm/fcntl.h>
  21. #include <asm/resource.h>
  22. #include <atomic.h> // TODO: migrate to stdatomic.h
  23. #include <linux/in.h>
  24. #include <linux/in6.h>
  25. #include <linux/shm.h>
  26. #include <linux/un.h>
  27. #include <list.h>
  28. #include <pal.h>
  29. #include <shim_defs.h>
  30. #include <shim_sysv.h>
  31. #include <shim_types.h>
  32. #include <stdalign.h>
  33. /* start definition of shim handle */
  34. enum shim_handle_type {
  35. TYPE_FILE,
  36. TYPE_DEV,
  37. TYPE_PIPE,
  38. TYPE_SOCK,
  39. TYPE_DIR,
  40. TYPE_SHM,
  41. TYPE_SEM,
  42. TYPE_MSG,
  43. TYPE_FUTEX,
  44. TYPE_STR,
  45. TYPE_EPOLL,
  46. TYPE_EVENTFD
  47. };
  48. struct shim_handle;
  49. struct shim_thread;
  50. struct shim_vma;
  51. enum shim_file_type {
  52. FILE_UNKNOWN,
  53. FILE_REGULAR,
  54. FILE_DIR,
  55. FILE_DEV,
  56. FILE_TTY,
  57. };
  58. struct shim_file_data {
  59. struct shim_lock lock;
  60. struct atomic_int version;
  61. bool queried;
  62. enum shim_file_type type;
  63. mode_t mode;
  64. struct atomic_int size;
  65. struct shim_qstr host_uri;
  66. unsigned long atime;
  67. unsigned long mtime;
  68. unsigned long ctime;
  69. unsigned long nlink;
  70. };
  71. struct shim_file_handle {
  72. unsigned int version;
  73. struct shim_file_data* data;
  74. enum shim_file_type type;
  75. off_t size;
  76. off_t marker;
  77. enum { FILEBUF_MAP, FILEBUF_NONE } buf_type;
  78. size_t mapsize;
  79. off_t mapoffset;
  80. void* mapbuf;
  81. };
  82. #define FILE_HANDLE_DATA(hdl) ((hdl)->info.file.data)
  83. #define FILE_DENTRY_DATA(dent) ((struct shim_file_data*)(dent)->data)
  84. struct shim_dev_ops {
  85. /* open: provide a filename relative to the mount point and flags,
  86. modify the shim handle */
  87. int (*open)(struct shim_handle* hdl, const char* name, int flags);
  88. /* close: clean up the file state inside the handle */
  89. int (*close)(struct shim_handle* hdl);
  90. /* read: the content from the file opened as handle */
  91. ssize_t (*read)(struct shim_handle* hdl, void* buf, size_t count);
  92. /* write: the content from the file opened as handle */
  93. ssize_t (*write)(struct shim_handle* hdl, const void* buf, size_t count);
  94. /* flush: flush out user buffer */
  95. int (*flush)(struct shim_handle* hdl);
  96. /* seek: the content from the file opened as handle */
  97. off_t (*seek)(struct shim_handle* hdl, off_t offset, int wence);
  98. int (*truncate)(struct shim_handle* hdl, uint64_t len);
  99. int (*mode)(const char* name, mode_t* mode);
  100. /* stat, hstat: get status of the file */
  101. int (*stat)(const char* name, struct stat* buf);
  102. int (*hstat)(struct shim_handle* hdl, struct stat* buf);
  103. };
  104. struct shim_dev_handle {
  105. struct shim_dev_ops dev_ops;
  106. };
  107. struct shim_pipe_handle {
  108. #if USE_SIMPLE_PIPE == 1
  109. struct shim_handle* pair;
  110. #else
  111. IDTYPE pipeid;
  112. #endif
  113. };
  114. #define SOCK_STREAM 1
  115. #define SOCK_DGRAM 2
  116. #define SOCK_NONBLOCK 04000
  117. #define SOCK_CLOEXEC 02000000
  118. #define SOL_TCP 6
  119. #define PF_LOCAL 1
  120. #define PF_UNIX PF_LOCAL
  121. #define PF_FILE PF_LOCAL
  122. #define PF_INET 2
  123. #define PF_INET6 10
  124. #define AF_UNIX PF_UNIX
  125. #define AF_INET PF_INET
  126. #define AF_INET6 PF_INET6
  127. #define SOCK_URI_SIZE 108
  128. enum shim_sock_state {
  129. SOCK_CREATED,
  130. SOCK_BOUND,
  131. SOCK_CONNECTED,
  132. SOCK_BOUNDCONNECTED,
  133. SOCK_LISTENED,
  134. SOCK_ACCEPTED,
  135. SOCK_SHUTDOWN,
  136. };
  137. struct shim_unix_data {
  138. unsigned int pipeid;
  139. };
  140. struct shim_sock_handle {
  141. int domain;
  142. int sock_type;
  143. int protocol;
  144. int error;
  145. enum shim_sock_state sock_state;
  146. union shim_sock_addr {
  147. // INET addr
  148. struct {
  149. struct addr_inet {
  150. unsigned short port;
  151. unsigned short ext_port;
  152. union {
  153. struct in_addr v4;
  154. struct in6_addr v6;
  155. } addr;
  156. } bind, conn;
  157. } in;
  158. // UNIX addr
  159. struct addr_unix {
  160. struct shim_dentry* dentry;
  161. unsigned int pipeid;
  162. struct shim_unix_data* data;
  163. } un;
  164. } addr;
  165. struct shim_sock_option {
  166. struct shim_sock_option* next;
  167. int level;
  168. int optname;
  169. int optlen;
  170. char optval[];
  171. }* pending_options;
  172. struct shim_peek_buffer {
  173. size_t size; /* total size (capacity) of buffer `buf` */
  174. size_t start; /* beginning of buffered but yet unread data in `buf` */
  175. size_t end; /* end of buffered but yet unread data in `buf` */
  176. char uri[SOCK_URI_SIZE]; /* cached URI for recvfrom(udp_socket) case */
  177. char buf[]; /* peek buffer of size `size` */
  178. }* peek_buffer;
  179. };
  180. struct shim_dirent {
  181. struct shim_dirent* next;
  182. unsigned long ino; /* Inode number */
  183. unsigned char type;
  184. char name[]; /* File name (null-terminated) */
  185. };
  186. #define SHIM_DIRENT_SIZE offsetof(struct shim_dirent, name)
  187. #define SHIM_DIRENT_ALIGNMENT alignof(struct shim_dirent)
  188. /* Size of struct shim_dirent instance together with alignment,
  189. * which might be different depending on the length of the name field */
  190. #define SHIM_DIRENT_ALIGNED_SIZE(len) ALIGN_UP(SHIM_DIRENT_SIZE + (len), SHIM_DIRENT_ALIGNMENT)
  191. struct shim_dir_handle {
  192. int offset;
  193. struct shim_dentry* dotdot;
  194. struct shim_dentry* dot;
  195. struct shim_dentry** buf;
  196. struct shim_dentry** ptr;
  197. };
  198. struct shim_shm_handle {
  199. /* XXX: need to implement */
  200. void* __reserved;
  201. };
  202. struct msg_type;
  203. struct msg_item;
  204. struct msg_client;
  205. #define MAX_SYSV_CLIENTS 32
  206. DEFINE_LIST(shim_msg_handle);
  207. struct shim_msg_handle {
  208. unsigned long msqkey; /* msg queue key from user */
  209. IDTYPE msqid; /* msg queue identifier */
  210. bool owned; /* owned by current process */
  211. struct shim_ipc_info* owner;
  212. LEASETYPE lease;
  213. int perm; /* access permissions */
  214. bool deleted; /* marking the queue deleted */
  215. int nmsgs; /* number of msgs */
  216. int currentsize; /* current size in bytes */
  217. struct msg_qobj* queue;
  218. int queuesize;
  219. int queueused;
  220. struct msg_qobj* freed;
  221. PAL_HANDLE event; /* event for waiting */
  222. int ntypes;
  223. int maxtypes;
  224. struct msg_type* types;
  225. struct sysv_score scores[MAX_SYSV_CLIENTS];
  226. LIST_TYPE(shim_msg_handle) list;
  227. LIST_TYPE(shim_msg_handle) key_hlist;
  228. LIST_TYPE(shim_msg_handle) qid_hlist;
  229. };
  230. struct sem_objs;
  231. DEFINE_LIST(shim_sem_handle);
  232. struct shim_sem_handle {
  233. unsigned long semkey;
  234. IDTYPE semid;
  235. bool owned;
  236. struct shim_ipc_info* owner;
  237. LEASETYPE lease;
  238. int perm;
  239. bool deleted;
  240. PAL_HANDLE event;
  241. int nsems;
  242. struct sem_obj* sems;
  243. int nreqs;
  244. struct sysv_score scores[MAX_SYSV_CLIENTS];
  245. LISTP_TYPE(sem_ops) migrated;
  246. LIST_TYPE(shim_sem_handle) list;
  247. LIST_TYPE(shim_sem_handle) key_hlist;
  248. LIST_TYPE(shim_sem_handle) sid_hlist;
  249. };
  250. struct shim_str_data {
  251. REFTYPE ref_count;
  252. char* str;
  253. off_t len;
  254. size_t buf_size;
  255. bool dirty;
  256. int (*update)(struct shim_handle* hdl);
  257. int (*modify)(struct shim_handle* hdl);
  258. };
  259. struct shim_str_handle {
  260. struct shim_str_data* data; /* inode is stored in dentry, too.
  261. store pointer here for efficiency */
  262. char* ptr;
  263. };
  264. DEFINE_LIST(shim_epoll_item);
  265. DEFINE_LISTP(shim_epoll_item);
  266. struct shim_epoll_handle {
  267. int maxfds;
  268. int waiter_cnt;
  269. int pal_cnt;
  270. PAL_HANDLE* pal_handles;
  271. AEVENTTYPE event;
  272. LISTP_TYPE(shim_epoll_item) fds;
  273. };
  274. struct shim_mount;
  275. struct shim_qstr;
  276. struct shim_dentry;
  277. /* The epolls list links to the back field of the shim_epoll_item structure
  278. */
  279. struct shim_handle {
  280. enum shim_handle_type type;
  281. REFTYPE ref_count;
  282. char fs_type[8];
  283. struct shim_mount* fs;
  284. struct shim_qstr path;
  285. struct shim_dentry* dentry;
  286. /* If this handle is registered for any epoll handle, this list contains
  287. * a shim_epoll_item object in correspondence with the epoll handle. */
  288. LISTP_TYPE(shim_epoll_item) epolls;
  289. struct shim_qstr uri; /* URI representing this handle, it is not
  290. * necessary to be set. */
  291. PAL_HANDLE pal_handle;
  292. union {
  293. struct shim_file_handle file;
  294. struct shim_dev_handle dev;
  295. struct shim_pipe_handle pipe;
  296. struct shim_sock_handle sock;
  297. struct shim_shm_handle shm;
  298. struct shim_msg_handle msg;
  299. struct shim_sem_handle sem;
  300. struct shim_str_handle str;
  301. struct shim_epoll_handle epoll;
  302. } info;
  303. struct shim_dir_handle dir_info;
  304. int flags;
  305. int acc_mode;
  306. IDTYPE owner;
  307. struct shim_lock lock;
  308. };
  309. /* allocating / manage handle */
  310. struct shim_handle* get_new_handle(void);
  311. void flush_handle(struct shim_handle* hdl);
  312. void get_handle(struct shim_handle* hdl);
  313. void put_handle(struct shim_handle* hdl);
  314. /* file descriptor table */
  315. struct shim_fd_handle {
  316. FDTYPE vfd; /* virtual file descriptor */
  317. int flags; /* file descriptor flags, only FD_CLOEXEC */
  318. struct shim_handle* handle;
  319. };
  320. struct shim_handle_map {
  321. /* the top of created file descriptors */
  322. FDTYPE fd_size;
  323. FDTYPE fd_top;
  324. /* refrence count and lock */
  325. REFTYPE ref_count;
  326. struct shim_lock lock;
  327. /* An array of file descriptor belong to this mapping */
  328. struct shim_fd_handle** map;
  329. };
  330. /* allocating file descriptors */
  331. #define FD_NULL ((FDTYPE)-1)
  332. #define HANDLE_ALLOCATED(fd_handle) ((fd_handle) && (fd_handle)->vfd != FD_NULL)
  333. struct shim_handle* __get_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map);
  334. struct shim_handle* get_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map);
  335. /*!
  336. * \brief Assign new fd to a handle.
  337. *
  338. * \param hdl A handle to be mapped to the new fd.
  339. * \param flags Flags assigned to new shim_fd_handle.
  340. * \param handle_map Handle map to be used. If NULL is passed, current thread's handle map is used.
  341. *
  342. * Creates mapping for the given handle to a new file descriptor which is then returned.
  343. * Uses the lowest, non-negative available number for the new fd.
  344. */
  345. int set_new_fd_handle(struct shim_handle* hdl, int flags, struct shim_handle_map* map);
  346. int set_new_fd_handle_by_fd(FDTYPE fd, struct shim_handle* hdl, int flags,
  347. struct shim_handle_map* map);
  348. struct shim_handle* __detach_fd_handle(struct shim_fd_handle* fd, int* flags,
  349. struct shim_handle_map* map);
  350. struct shim_handle* detach_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map);
  351. /* manage handle mapping */
  352. int dup_handle_map(struct shim_handle_map** new_map, struct shim_handle_map* old_map);
  353. int flush_handle_map(struct shim_handle_map* map);
  354. void get_handle_map(struct shim_handle_map* map);
  355. void put_handle_map(struct shim_handle_map* map);
  356. int walk_handle_map(int (*callback)(struct shim_fd_handle*, struct shim_handle_map*),
  357. struct shim_handle_map* map);
  358. int init_handle(void);
  359. int init_important_handles(void);
  360. off_t get_file_size(struct shim_handle* file);
  361. int do_handle_read(struct shim_handle* hdl, void* buf, int count);
  362. int do_handle_write(struct shim_handle* hdl, const void* buf, int count);
  363. #endif /* _SHIM_HANDLE_H_ */