shim_handle.h 11 KB

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