capability.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Interfaces for capabilities.
  18. */
  19. #ifndef KERN_CAP_H
  20. #define KERN_CAP_H
  21. #include <assert.h>
  22. #include <stdint.h>
  23. #include <kern/hlist.h>
  24. #include <kern/init.h>
  25. #include <kern/ipc.h>
  26. #include <kern/list.h>
  27. #include <kern/pqueue.h>
  28. #include <kern/slist.h>
  29. #include <kern/spinlock.h>
  30. #include <kern/sref.h>
  31. struct task;
  32. struct thread;
  33. enum
  34. {
  35. CAP_TYPE_CHANNEL,
  36. CAP_TYPE_FLOW,
  37. CAP_TYPE_TASK,
  38. CAP_TYPE_THREAD,
  39. CAP_TYPE_KERNEL,
  40. CAP_TYPE_MAX,
  41. };
  42. // Size of an alert message, in bytes.
  43. #define CAP_ALERT_SIZE 16
  44. #define CAP_ALERT_NONBLOCK 0x01 // Don't block when sending an alert.
  45. // Alert types.
  46. enum
  47. {
  48. CAP_ALERT_USER,
  49. CAP_ALERT_INTR,
  50. CAP_ALERT_THREAD_DIED,
  51. CAP_ALERT_TASK_DIED,
  52. CAP_ALERT_CHAN_CLOSED,
  53. };
  54. // Kernel-sent alert.
  55. struct cap_kern_alert
  56. {
  57. int type;
  58. union
  59. {
  60. struct
  61. {
  62. uint32_t irq;
  63. uint32_t count;
  64. } intr;
  65. int thread_id;
  66. int task_id;
  67. int any_id;
  68. uintptr_t tag;
  69. };
  70. };
  71. static_assert (sizeof (struct cap_kern_alert) <= CAP_ALERT_SIZE,
  72. "struct cap_kern_alert is too big");
  73. static_assert (OFFSETOF (struct cap_kern_alert, intr.irq) ==
  74. OFFSETOF (struct cap_kern_alert, thread_id) &&
  75. OFFSETOF (struct cap_kern_alert, thread_id) ==
  76. OFFSETOF (struct cap_kern_alert, task_id),
  77. "invalid layout for cap_kern_alert");
  78. struct cap_base
  79. {
  80. unsigned char type;
  81. unsigned int flags:24;
  82. struct sref_counter sref;
  83. };
  84. enum
  85. {
  86. CAP_KERNEL_MEMORY, // Allows mapping physical memory.
  87. CAP_KERNEL_DEVICE, // Allows registering interrupts.
  88. CAP_KERNEL_MAX,
  89. };
  90. struct cap_thread_info
  91. {
  92. struct futex_td *futex_td;
  93. void *thread_ptr;
  94. };
  95. #define CAPABILITY struct cap_base base
  96. #define CAP_FLOW_HANDLE_INTR 0x01 // Flow can handle interrupts.
  97. #define CAP_FLOW_EXT_PAGER 0x02 // Flow is an external pager.
  98. struct cap_flow
  99. {
  100. CAPABILITY;
  101. struct list waiters;
  102. struct list receivers;
  103. struct slist ports;
  104. struct hlist alloc_alerts;
  105. struct pqueue pending_alerts;
  106. uintptr_t tag;
  107. uintptr_t entry;
  108. #if CONFIG_MAX_CPUS > 1
  109. char pad[CPU_L1_SIZE];
  110. #endif
  111. struct spinlock lock;
  112. };
  113. struct cap_channel
  114. {
  115. CAPABILITY;
  116. struct cap_flow *flow;
  117. uintptr_t tag;
  118. };
  119. struct cap_task
  120. {
  121. CAPABILITY;
  122. struct task *task;
  123. };
  124. struct cap_thread
  125. {
  126. CAPABILITY;
  127. struct thread *thread;
  128. };
  129. struct cap_kernel
  130. {
  131. CAPABILITY;
  132. int kind;
  133. };
  134. // Triplet of iterators.
  135. struct cap_iters
  136. {
  137. struct ipc_iov_iter iov;
  138. struct ipc_cap_iter cap;
  139. struct ipc_vme_iter vme;
  140. };
  141. struct bulletin;
  142. // Cast a capability to the base type.
  143. #define CAP_BASE(x) ((struct cap_base *)(x))
  144. #define CAP(x) \
  145. _Generic (x, \
  146. struct cap_kernel * : CAP_BASE (x), \
  147. struct cap_thread * : CAP_BASE (x), \
  148. struct cap_task * : CAP_BASE (x), \
  149. struct cap_channel *: CAP_BASE (x), \
  150. struct cap_flow * : CAP_BASE (x), \
  151. default: (x))
  152. // Acquire or release a reference on a capability.
  153. static inline void
  154. cap_base_acq (struct cap_base *cap)
  155. {
  156. sref_counter_inc (&cap->sref);
  157. }
  158. static inline void
  159. cap_base_rel (struct cap_base *cap)
  160. {
  161. sref_counter_dec (&cap->sref);
  162. }
  163. #define cap_base_acq(cap) (cap_base_acq) (CAP (cap))
  164. #define cap_base_rel(cap) (cap_base_rel) (CAP (cap))
  165. /*
  166. * Intern a capability within the local space. Returns the new capability
  167. * index, or a negated errno value on error.
  168. */
  169. int cap_intern (struct cap_base *cap, int flags);
  170. #define cap_intern(cap, flags) (cap_intern) (CAP (cap), (flags))
  171. // Get the capability's type.
  172. #define cap_type(cap) (((const struct cap_base *)(x))->type)
  173. // Create a flow.
  174. int cap_flow_create (struct cap_flow **outp, uint32_t flags,
  175. uintptr_t tag, uintptr_t entry);
  176. // Create a channel for a flow.
  177. int cap_channel_create (struct cap_channel **outp, struct cap_flow *flow,
  178. uintptr_t tag);
  179. // Create a capability representing a task.
  180. int cap_task_create (struct cap_task **outp, struct task *task);
  181. // Create a capability representing a thread.
  182. int cap_thread_create (struct cap_thread **outp, struct thread *thread);
  183. // Get and set a capability's tag (Used for channels and flows).
  184. int cap_get_tag (const struct cap_base *cap, uintptr_t *tagp);
  185. int cap_set_tag (struct cap_base *cap, uintptr_t tag);
  186. #define cap_get_tag(cap, tagp) (cap_get_tag) (CAP (cap), (tagp))
  187. #define cap_set_tag(cap, tag) (cap_set_tag) (CAP (cap), (tag))
  188. // Link a channel to a flow.
  189. int cap_channel_link (struct cap_channel *channel, struct cap_flow *flow);
  190. // Hook a channel to a remote flow in a task.
  191. int cap_flow_hook (struct cap_channel **outp, struct task *task, int cap_idx);
  192. // Send and receive iterator triplets to a capability.
  193. ssize_t cap_send_iters (struct cap_base *cap, struct cap_iters *in_it,
  194. struct cap_iters *out_it, struct ipc_msg_data *data);
  195. // Reply to the current message with an iterator triplet or error value.
  196. ssize_t cap_reply_iters (struct cap_iters *it, int rv);
  197. // Pull an iterator triplet from the current message.
  198. ssize_t cap_pull_iters (struct cap_iters *it, struct ipc_msg_data *data);
  199. // Push an iterator triplet to the current message.
  200. ssize_t cap_push_iters (struct cap_iters *it, struct ipc_msg_data *data);
  201. // Receive an alert from a flow.
  202. int cap_recv_alert (struct cap_flow *flow, void *buf,
  203. uint32_t flags, struct ipc_msg_data *mdata);
  204. // Send an alert to a flow.
  205. int cap_send_alert (struct cap_base *cap, const void *buf,
  206. uint32_t flags, uint32_t prio);
  207. #define cap_send_alert(cap, buf, flags, prio) \
  208. (cap_send_alert) (CAP (cap), buf, flags, prio)
  209. // Add and remove a port to/from a flow.
  210. int cap_flow_add_port (struct cap_flow *flow, void *stack, size_t size,
  211. struct ipc_msg *msg, struct ipc_msg_data *mdata,
  212. struct cap_thread_info *info);
  213. int cap_flow_rem_port (struct cap_flow *flow, uintptr_t stack);
  214. // Register a flow for interrupt handling.
  215. int cap_intr_register (struct cap_flow *flow, uint32_t irq);
  216. // Unregister a flow for interrupt handling.
  217. int cap_intr_unregister (struct cap_flow *flow, uint32_t irq);
  218. // Register a thread on a flow to notify on its death.
  219. int cap_thread_register (struct cap_flow *flow, struct thread *thread);
  220. // Register a task on a flow to notify on its death.
  221. int cap_task_register (struct cap_flow *flow, struct task *task);
  222. // Unregister a thread.
  223. int cap_thread_unregister (struct cap_flow *flow, struct thread *thread);
  224. // Unregister a task.
  225. int cap_task_unregister (struct cap_flow *flow, struct task *task);
  226. // Traverse a list of dead notifications.
  227. void cap_notify_dead (struct bulletin *bulletin);
  228. #define cap_iters_init_impl(it, buf, size, iov_init) \
  229. do \
  230. { \
  231. iov_init (&(it)->iov, (void *)(buf), size); \
  232. ipc_cap_iter_init (&(it)->cap, 0, 0); \
  233. ipc_vme_iter_init (&(it)->vme, 0, 0); \
  234. } \
  235. while (0)
  236. #define cap_iters_init_buf(it, buf, size) \
  237. cap_iters_init_impl (it, buf, size, ipc_iov_iter_init_buf)
  238. #define cap_iters_init_iov(it, iovs, nr_iovs) \
  239. cap_iters_init_impl (it, iovs, nr_iovs, ipc_iov_iter_init)
  240. #define cap_iters_init_msg(it, msg) \
  241. do \
  242. { \
  243. ipc_iov_iter_init (&(it)->iov, (msg)->iovs, (msg)->iov_cnt); \
  244. ipc_cap_iter_init (&(it)->cap, (msg)->caps, (msg)->cap_cnt); \
  245. ipc_vme_iter_init (&(it)->vme, (msg)->vmes, (msg)->vme_cnt); \
  246. } \
  247. while (0)
  248. // Send raw bytes to a capability and receive the reply.
  249. static inline ssize_t
  250. cap_send_bytes (struct cap_base *cap, const void *src, size_t src_size,
  251. void *dst, size_t dst_size)
  252. {
  253. struct cap_iters in, out;
  254. cap_iters_init_buf (&in, src, src_size);
  255. cap_iters_init_buf (&out, dst, dst_size);
  256. return (cap_send_iters (cap, &in, &out, NULL));
  257. }
  258. #define cap_send_bytes(cap, src, src_size, dst, dst_size) \
  259. (cap_send_bytes) (CAP (cap), (src), (src_size), (dst), (dst_size))
  260. // Send bytes in iovecs and receive the reply.
  261. static inline ssize_t
  262. cap_send_iov (struct cap_base *cap, const struct iovec *src, uint32_t nr_src,
  263. struct iovec *dst, uint32_t nr_dst)
  264. {
  265. struct cap_iters in, out;
  266. cap_iters_init_iov (&in, src, nr_src);
  267. cap_iters_init_iov (&out, dst, nr_dst);
  268. return (cap_send_iters (cap, &in, &out, NULL));
  269. }
  270. #define cap_send_iov(cap, src, nr_src, dst, nr_dst) \
  271. (cap_send_iov) (CAP (cap), (src), (nr_src), (dst), (nr_dst))
  272. // Send and receive full messages and also the metadata.
  273. static inline ssize_t
  274. cap_send_msg (struct cap_base *cap, const struct ipc_msg *src,
  275. struct ipc_msg *dst, struct ipc_msg_data *data)
  276. {
  277. struct cap_iters in, out;
  278. cap_iters_init_msg (&in, src);
  279. cap_iters_init_msg (&out, dst);
  280. return (cap_send_iters (cap, &in, &out, data));
  281. }
  282. #define cap_send_msg(cap, src, dst, data) \
  283. (cap_send_msg) (CAP (cap), (src), (dst), (data))
  284. // Reply to the current message with raw bytes or an error.
  285. static inline int
  286. cap_reply_bytes (const void *src, size_t bytes, int err)
  287. {
  288. struct cap_iters it;
  289. cap_iters_init_buf (&it, src, bytes);
  290. return (cap_reply_iters (&it, err));
  291. }
  292. // Reply to the current message with bytes in iovecs or an error.
  293. static inline int
  294. cap_reply_iov (const struct iovec *iov, uint32_t nr_iov, int err)
  295. {
  296. struct cap_iters it;
  297. cap_iters_init_iov (&it, iov, nr_iov);
  298. return (cap_reply_iters (&it, err));
  299. }
  300. // Reply to the current message with a full IPC message or an error.
  301. static inline int
  302. cap_reply_msg (const struct ipc_msg *msg, int err)
  303. {
  304. struct cap_iters it;
  305. cap_iters_init_msg (&it, msg);
  306. return (cap_reply_iters (&it, err));
  307. }
  308. // Pull raw bytes from the current message.
  309. static inline ssize_t
  310. cap_pull_bytes (void *dst, size_t bytes, struct ipc_msg_data *mdata)
  311. {
  312. struct cap_iters it;
  313. cap_iters_init_buf (&it, dst, bytes);
  314. return (cap_pull_iters (&it, mdata));
  315. }
  316. // Pull iovecs from the current message.
  317. static inline ssize_t
  318. cap_pull_iov (struct iovec *iovs, uint32_t nr_iovs, struct ipc_msg_data *mdata)
  319. {
  320. struct cap_iters it;
  321. cap_iters_init_iov (&it, iovs, nr_iovs);
  322. return (cap_pull_iters (&it, mdata));
  323. }
  324. // Pull an IPC message from the current message.
  325. static inline ssize_t
  326. cap_pull_msg (struct ipc_msg *msg, struct ipc_msg_data *mdata)
  327. {
  328. struct cap_iters it;
  329. cap_iters_init_msg (&it, msg);
  330. return (cap_pull_iters (&it, mdata));
  331. }
  332. // Push raw bytes into the current message.
  333. static inline ssize_t
  334. cap_push_bytes (const void *src, size_t bytes,
  335. struct ipc_msg_data *mdata)
  336. {
  337. struct cap_iters it;
  338. cap_iters_init_buf (&it, src, bytes);
  339. return (cap_push_iters (&it, mdata));
  340. }
  341. // Push iovecs into the current message.
  342. static inline ssize_t
  343. cap_push_iov (const struct iovec *iovs, uint32_t nr_iovs,
  344. struct ipc_msg_data *mdata)
  345. {
  346. struct cap_iters it;
  347. cap_iters_init_iov (&it, iovs, nr_iovs);
  348. return (cap_push_iters (&it, mdata));
  349. }
  350. // Push an IPC message to the current message.
  351. static inline ssize_t
  352. cap_push_msg (const struct ipc_msg *msg, struct ipc_msg_data *mdata)
  353. {
  354. struct cap_iters it;
  355. cap_iters_init_msg (&it, msg);
  356. return (cap_push_iters (&it, mdata));
  357. }
  358. /*
  359. * This init operation provides :
  360. * - capabilities fully operational.
  361. */
  362. INIT_OP_DECLARE (cap_setup);
  363. #endif