capability.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 (__builtin_offsetof (struct cap_kern_alert, intr.irq) ==
  74. __builtin_offsetof (struct cap_kern_alert, thread_id) &&
  75. __builtin_offsetof (struct cap_kern_alert, thread_id) ==
  76. __builtin_offsetof (struct cap_kern_alert, task_id),
  77. "invalid layout for cap_kern_alert");
  78. struct cap_base
  79. {
  80. unsigned int type;
  81. struct sref_counter sref;
  82. };
  83. enum
  84. {
  85. CAP_KERNEL_MEMORY, // Allows mapping physical memory.
  86. CAP_KERNEL_DEVICE, // Allows registering interrupts.
  87. CAP_KERNEL_MAX,
  88. };
  89. struct cap_thread_info
  90. {
  91. struct futex_td *futex_td;
  92. void *thread_ptr;
  93. };
  94. #define CAPABILITY struct cap_base base
  95. struct cap_flow
  96. {
  97. CAPABILITY;
  98. struct list waiters;
  99. struct list receivers;
  100. struct slist ports;
  101. struct hlist alloc_alerts;
  102. struct pqueue pending_alerts;
  103. uintptr_t tag;
  104. uintptr_t entry;
  105. uint32_t flags;
  106. #if CONFIG_MAX_CPUS > 1
  107. char pad[CPU_L1_SIZE];
  108. #endif
  109. struct spinlock lock;
  110. };
  111. struct cap_channel
  112. {
  113. CAPABILITY;
  114. struct cap_flow *flow;
  115. uintptr_t tag;
  116. };
  117. struct cap_task
  118. {
  119. CAPABILITY;
  120. struct task *task;
  121. };
  122. struct cap_thread
  123. {
  124. CAPABILITY;
  125. struct thread *thread;
  126. };
  127. struct cap_kernel
  128. {
  129. CAPABILITY;
  130. int kind;
  131. };
  132. // Triplet of iterators.
  133. struct cap_iters
  134. {
  135. struct ipc_iov_iter iov;
  136. struct ipc_cap_iter cap;
  137. struct ipc_vme_iter vme;
  138. };
  139. struct bulletin;
  140. // Cast a capability to the base type.
  141. #define CAP_BASE(x) ((struct cap_base *)(x))
  142. #define CAP(x) \
  143. _Generic (x, \
  144. struct cap_kernel * : CAP_BASE (x), \
  145. struct cap_thread * : CAP_BASE (x), \
  146. struct cap_task * : CAP_BASE (x), \
  147. struct cap_channel *: CAP_BASE (x), \
  148. struct cap_flow * : CAP_BASE (x), \
  149. default: (x))
  150. // Acquire or release a reference on a capability.
  151. static inline void
  152. cap_base_acq (struct cap_base *cap)
  153. {
  154. sref_counter_inc (&cap->sref);
  155. }
  156. static inline void
  157. cap_base_rel (struct cap_base *cap)
  158. {
  159. sref_counter_dec (&cap->sref);
  160. }
  161. #define cap_base_acq(cap) (cap_base_acq) (CAP (cap))
  162. #define cap_base_rel(cap) (cap_base_rel) (CAP (cap))
  163. /*
  164. * Intern a capability within the local space. Returns the new capability
  165. * index, or a negated errno value on error.
  166. */
  167. int cap_intern (struct cap_base *cap, int flags);
  168. #define cap_intern(cap, flags) (cap_intern) (CAP (cap), (flags))
  169. // Get the capability's type.
  170. #define cap_type(cap) (((const struct cap_base *)(x))->type)
  171. // Create a flow.
  172. int cap_flow_create (struct cap_flow **outp, uint32_t flags,
  173. uintptr_t tag, uintptr_t entry);
  174. // Create a channel for a flow.
  175. int cap_channel_create (struct cap_channel **outp, struct cap_flow *flow,
  176. uintptr_t tag);
  177. // Create a capability representing a task.
  178. int cap_task_create (struct cap_task **outp, struct task *task);
  179. // Create a capability representing a thread.
  180. int cap_thread_create (struct cap_thread **outp, struct thread *thread);
  181. // Get and set a capability's tag (Used for channels and flows).
  182. int cap_get_tag (const struct cap_base *cap, uintptr_t *tagp);
  183. int cap_set_tag (struct cap_base *cap, uintptr_t tag);
  184. #define cap_get_tag(cap, tagp) (cap_get_tag) (CAP (cap), (tagp))
  185. #define cap_set_tag(cap, tag) (cap_set_tag) (CAP (cap), (tag))
  186. // Link a channel to a flow.
  187. int cap_channel_link (struct cap_channel *channel, struct cap_flow *flow);
  188. // Hook a channel to a remote flow in a task.
  189. int cap_flow_hook (struct cap_channel **outp, struct task *task, int cap_idx);
  190. // Send and receive iterator triplets to a capability.
  191. ssize_t cap_send_iters (struct cap_base *cap, struct cap_iters *in_it,
  192. struct cap_iters *out_it, struct ipc_msg_data *data);
  193. // Reply to the current message with an iterator triplet or error value.
  194. ssize_t cap_reply_iters (struct cap_iters *it, int rv);
  195. // Pull an iterator triplet from the current message.
  196. ssize_t cap_pull_iters (struct cap_iters *it, struct ipc_msg_data *data);
  197. // Push an iterator triplet to the current message.
  198. ssize_t cap_push_iters (struct cap_iters *it, struct ipc_msg_data *data);
  199. // Receive an alert from a flow.
  200. int cap_recv_alert (struct cap_flow *flow, void *buf,
  201. uint32_t flags, struct ipc_msg_data *mdata);
  202. // Send an alert to a flow.
  203. int cap_send_alert (struct cap_base *cap, const void *buf,
  204. uint32_t flags, uint32_t prio);
  205. #define cap_send_alert(cap, buf, flags, prio) \
  206. (cap_send_alert) (CAP (cap), buf, flags, prio)
  207. // Add and remove a port to/from a flow.
  208. int cap_flow_add_port (struct cap_flow *flow, void *stack, size_t size,
  209. struct ipc_msg *msg, struct ipc_msg_data *mdata,
  210. struct cap_thread_info *info);
  211. int cap_flow_rem_port (struct cap_flow *flow, uintptr_t stack);
  212. // Register a flow for interrupt handling.
  213. int cap_intr_register (struct cap_flow *flow, uint32_t irq);
  214. // Unregister a flow for interrupt handling.
  215. int cap_intr_unregister (struct cap_flow *flow, uint32_t irq);
  216. // Register a thread on a flow to notify on its death.
  217. int cap_thread_register (struct cap_flow *flow, struct thread *thread);
  218. // Register a task on a flow to notify on its death.
  219. int cap_task_register (struct cap_flow *flow, struct task *task);
  220. // Unregister a thread.
  221. int cap_thread_unregister (struct cap_flow *flow, struct thread *thread);
  222. // Unregister a task.
  223. int cap_task_unregister (struct cap_flow *flow, struct task *task);
  224. // Traverse a list of dead notifications.
  225. void cap_notify_dead (struct bulletin *bulletin);
  226. #define cap_iters_init_impl(it, buf, size, iov_init) \
  227. do \
  228. { \
  229. iov_init (&(it)->iov, (void *)(buf), size); \
  230. ipc_cap_iter_init (&(it)->cap, 0, 0); \
  231. ipc_vme_iter_init (&(it)->vme, 0, 0); \
  232. } \
  233. while (0)
  234. #define cap_iters_init_buf(it, buf, size) \
  235. cap_iters_init_impl (it, buf, size, ipc_iov_iter_init_buf)
  236. #define cap_iters_init_iov(it, iovs, nr_iovs) \
  237. cap_iters_init_impl (it, iovs, nr_iovs, ipc_iov_iter_init)
  238. #define cap_iters_init_msg(it, msg) \
  239. do \
  240. { \
  241. ipc_iov_iter_init (&(it)->iov, (msg)->iovs, (msg)->iov_cnt); \
  242. ipc_cap_iter_init (&(it)->cap, (msg)->caps, (msg)->cap_cnt); \
  243. ipc_vme_iter_init (&(it)->vme, (msg)->vmes, (msg)->vme_cnt); \
  244. } \
  245. while (0)
  246. // Send raw bytes to a capability and receive the reply.
  247. static inline ssize_t
  248. cap_send_bytes (struct cap_base *cap, const void *src, size_t src_size,
  249. void *dst, size_t dst_size)
  250. {
  251. struct cap_iters in, out;
  252. cap_iters_init_buf (&in, src, src_size);
  253. cap_iters_init_buf (&out, dst, dst_size);
  254. return (cap_send_iters (cap, &in, &out, NULL));
  255. }
  256. #define cap_send_bytes(cap, src, src_size, dst, dst_size) \
  257. (cap_send_bytes) (CAP (cap), (src), (src_size), (dst), (dst_size))
  258. // Send bytes in iovecs and receive the reply.
  259. static inline ssize_t
  260. cap_send_iov (struct cap_base *cap, const struct iovec *src, uint32_t nr_src,
  261. struct iovec *dst, uint32_t nr_dst)
  262. {
  263. struct cap_iters in, out;
  264. cap_iters_init_iov (&in, src, nr_src);
  265. cap_iters_init_iov (&out, dst, nr_dst);
  266. return (cap_send_iters (cap, &in, &out, NULL));
  267. }
  268. #define cap_send_iov(cap, src, nr_src, dst, nr_dst) \
  269. (cap_send_iov) (CAP (cap), (src), (nr_src), (dst), (nr_dst))
  270. // Send and receive full messages and also the metadata.
  271. static inline ssize_t
  272. cap_send_msg (struct cap_base *cap, const struct ipc_msg *src,
  273. struct ipc_msg *dst, struct ipc_msg_data *data)
  274. {
  275. struct cap_iters in, out;
  276. cap_iters_init_msg (&in, src);
  277. cap_iters_init_msg (&out, dst);
  278. return (cap_send_iters (cap, &in, &out, data));
  279. }
  280. #define cap_send_msg(cap, src, dst, data) \
  281. (cap_send_msg) (CAP (cap), (src), (dst), (data))
  282. // Reply to the current message with raw bytes or an error.
  283. static inline int
  284. cap_reply_bytes (const void *src, size_t bytes, int err)
  285. {
  286. struct cap_iters it;
  287. cap_iters_init_buf (&it, src, bytes);
  288. return (cap_reply_iters (&it, err));
  289. }
  290. // Reply to the current message with bytes in iovecs or an error.
  291. static inline int
  292. cap_reply_iov (const struct iovec *iov, uint32_t nr_iov, int err)
  293. {
  294. struct cap_iters it;
  295. cap_iters_init_iov (&it, iov, nr_iov);
  296. return (cap_reply_iters (&it, err));
  297. }
  298. // Reply to the current message with a full IPC message or an error.
  299. static inline int
  300. cap_reply_msg (const struct ipc_msg *msg, int err)
  301. {
  302. struct cap_iters it;
  303. cap_iters_init_msg (&it, msg);
  304. return (cap_reply_iters (&it, err));
  305. }
  306. // Pull raw bytes from the current message.
  307. static inline ssize_t
  308. cap_pull_bytes (void *dst, size_t bytes, struct ipc_msg_data *mdata)
  309. {
  310. struct cap_iters it;
  311. cap_iters_init_buf (&it, dst, bytes);
  312. return (cap_pull_iters (&it, mdata));
  313. }
  314. // Pull iovecs from the current message.
  315. static inline ssize_t
  316. cap_pull_iov (struct iovec *iovs, uint32_t nr_iovs, struct ipc_msg_data *mdata)
  317. {
  318. struct cap_iters it;
  319. cap_iters_init_iov (&it, iovs, nr_iovs);
  320. return (cap_pull_iters (&it, mdata));
  321. }
  322. // Pull an IPC message from the current message.
  323. static inline ssize_t
  324. cap_pull_msg (struct ipc_msg *msg, struct ipc_msg_data *mdata)
  325. {
  326. struct cap_iters it;
  327. cap_iters_init_msg (&it, msg);
  328. return (cap_pull_iters (&it, mdata));
  329. }
  330. // Push raw bytes into the current message.
  331. static inline ssize_t
  332. cap_push_bytes (const void *src, size_t bytes,
  333. struct ipc_msg_data *mdata)
  334. {
  335. struct cap_iters it;
  336. cap_iters_init_buf (&it, src, bytes);
  337. return (cap_push_iters (&it, mdata));
  338. }
  339. // Push iovecs into the current message.
  340. static inline ssize_t
  341. cap_push_iov (const struct iovec *iovs, uint32_t nr_iovs,
  342. struct ipc_msg_data *mdata)
  343. {
  344. struct cap_iters it;
  345. cap_iters_init_iov (&it, iovs, nr_iovs);
  346. return (cap_push_iters (&it, mdata));
  347. }
  348. // Push an IPC message to the current message.
  349. static inline ssize_t
  350. cap_push_msg (const struct ipc_msg *msg, struct ipc_msg_data *mdata)
  351. {
  352. struct cap_iters it;
  353. cap_iters_init_msg (&it, msg);
  354. return (cap_push_iters (&it, mdata));
  355. }
  356. /*
  357. * This init operation provides :
  358. * - capabilities fully operational.
  359. */
  360. INIT_OP_DECLARE (cap_setup);
  361. #endif