capability.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 lpads;
  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. int cap_flow_add_port (struct cap_flow *flow, void *stack, size_t size,
  208. struct ipc_msg *msg, struct ipc_msg_data *mdata,
  209. struct cap_thread_info *info);
  210. int cap_flow_rem_port (struct cap_flow *flow, uintptr_t stack);
  211. // Register a flow for interrupt handling.
  212. int cap_intr_register (struct cap_flow *flow, uint32_t irq);
  213. // Unregister a flow for interrupt handling.
  214. int cap_intr_unregister (struct cap_flow *flow, uint32_t irq);
  215. // Register a thread on a flow to notify on its death.
  216. int cap_thread_register (struct cap_flow *flow, struct thread *thread);
  217. // Register a task on a flow to notify on its death.
  218. int cap_task_register (struct cap_flow *flow, struct task *task);
  219. // Unregister a thread.
  220. int cap_thread_unregister (struct cap_flow *flow, struct thread *thread);
  221. // Unregister a task.
  222. int cap_task_unregister (struct cap_flow *flow, struct task *task);
  223. // Traverse a list of dead notifications.
  224. void cap_notify_dead (struct bulletin *bulletin);
  225. #define cap_iters_init_impl(it, buf, size, iov_init) \
  226. do \
  227. { \
  228. iov_init (&(it)->iov, (void *)(buf), size); \
  229. ipc_cap_iter_init (&(it)->cap, 0, 0); \
  230. ipc_vme_iter_init (&(it)->vme, 0, 0); \
  231. } \
  232. while (0)
  233. #define cap_iters_init_buf(it, buf, size) \
  234. cap_iters_init_impl (it, buf, size, ipc_iov_iter_init_buf)
  235. #define cap_iters_init_iov(it, iovs, nr_iovs) \
  236. cap_iters_init_impl (it, iovs, nr_iovs, ipc_iov_iter_init)
  237. #define cap_iters_init_msg(it, msg) \
  238. do \
  239. { \
  240. ipc_iov_iter_init (&(it)->iov, (msg)->iovs, (msg)->iov_cnt); \
  241. ipc_cap_iter_init (&(it)->cap, (msg)->caps, (msg)->cap_cnt); \
  242. ipc_vme_iter_init (&(it)->vme, (msg)->vmes, (msg)->vme_cnt); \
  243. } \
  244. while (0)
  245. // Send raw bytes to a capability and receive the reply.
  246. static inline ssize_t
  247. cap_send_bytes (struct cap_base *cap, const void *src, size_t src_size,
  248. void *dst, size_t dst_size)
  249. {
  250. struct cap_iters in, out;
  251. cap_iters_init_buf (&in, src, src_size);
  252. cap_iters_init_buf (&out, dst, dst_size);
  253. return (cap_send_iters (cap, &in, &out, NULL));
  254. }
  255. #define cap_send_bytes(cap, src, src_size, dst, dst_size) \
  256. (cap_send_bytes) (CAP (cap), (src), (src_size), (dst), (dst_size))
  257. // Send bytes in iovecs and receive the reply.
  258. static inline ssize_t
  259. cap_send_iov (struct cap_base *cap, const struct iovec *src, uint32_t nr_src,
  260. struct iovec *dst, uint32_t nr_dst)
  261. {
  262. struct cap_iters in, out;
  263. cap_iters_init_iov (&in, src, nr_src);
  264. cap_iters_init_iov (&out, dst, nr_dst);
  265. return (cap_send_iters (cap, &in, &out, NULL));
  266. }
  267. #define cap_send_iov(cap, src, nr_src, dst, nr_dst) \
  268. (cap_send_iov) (CAP (cap), (src), (nr_src), (dst), (nr_dst))
  269. // Send and receive full messages and also the metadata.
  270. static inline ssize_t
  271. cap_send_msg (struct cap_base *cap, const struct ipc_msg *src,
  272. struct ipc_msg *dst, struct ipc_msg_data *data)
  273. {
  274. struct cap_iters in, out;
  275. cap_iters_init_msg (&in, src);
  276. cap_iters_init_msg (&out, dst);
  277. return (cap_send_iters (cap, &in, &out, data));
  278. }
  279. #define cap_send_msg(cap, src, dst, data) \
  280. (cap_send_msg) (CAP (cap), (src), (dst), (data))
  281. // Reply to the current message with raw bytes or an error.
  282. static inline int
  283. cap_reply_bytes (const void *src, size_t bytes, int err)
  284. {
  285. struct cap_iters it;
  286. cap_iters_init_buf (&it, src, bytes);
  287. return (cap_reply_iters (&it, err));
  288. }
  289. // Reply to the current message with bytes in iovecs or an error.
  290. static inline int
  291. cap_reply_iov (const struct iovec *iov, uint32_t nr_iov, int err)
  292. {
  293. struct cap_iters it;
  294. cap_iters_init_iov (&it, iov, nr_iov);
  295. return (cap_reply_iters (&it, err));
  296. }
  297. // Reply to the current message with a full IPC message or an error.
  298. static inline int
  299. cap_reply_msg (const struct ipc_msg *msg, int err)
  300. {
  301. struct cap_iters it;
  302. cap_iters_init_msg (&it, msg);
  303. return (cap_reply_iters (&it, err));
  304. }
  305. // Pull raw bytes from the current message.
  306. static inline ssize_t
  307. cap_pull_bytes (void *dst, size_t bytes, struct ipc_msg_data *mdata)
  308. {
  309. struct cap_iters it;
  310. cap_iters_init_buf (&it, dst, bytes);
  311. return (cap_pull_iters (&it, mdata));
  312. }
  313. // Pull iovecs from the current message.
  314. static inline ssize_t
  315. cap_pull_iov (struct iovec *iovs, uint32_t nr_iovs, struct ipc_msg_data *mdata)
  316. {
  317. struct cap_iters it;
  318. cap_iters_init_iov (&it, iovs, nr_iovs);
  319. return (cap_pull_iters (&it, mdata));
  320. }
  321. // Pull an IPC message from the current message.
  322. static inline ssize_t
  323. cap_pull_msg (struct ipc_msg *msg, struct ipc_msg_data *mdata)
  324. {
  325. struct cap_iters it;
  326. cap_iters_init_msg (&it, msg);
  327. return (cap_pull_iters (&it, mdata));
  328. }
  329. // Push raw bytes into the current message.
  330. static inline ssize_t
  331. cap_push_bytes (const void *src, size_t bytes,
  332. struct ipc_msg_data *mdata)
  333. {
  334. struct cap_iters it;
  335. cap_iters_init_buf (&it, src, bytes);
  336. return (cap_push_iters (&it, mdata));
  337. }
  338. // Push iovecs into the current message.
  339. static inline ssize_t
  340. cap_push_iov (const struct iovec *iovs, uint32_t nr_iovs,
  341. struct ipc_msg_data *mdata)
  342. {
  343. struct cap_iters it;
  344. cap_iters_init_iov (&it, iovs, nr_iovs);
  345. return (cap_push_iters (&it, mdata));
  346. }
  347. // Push an IPC message to the current message.
  348. static inline ssize_t
  349. cap_push_msg (const struct ipc_msg *msg, struct ipc_msg_data *mdata)
  350. {
  351. struct cap_iters it;
  352. cap_iters_init_msg (&it, msg);
  353. return (cap_push_iters (&it, mdata));
  354. }
  355. /*
  356. * This init operation provides :
  357. * - capabilities fully operational.
  358. */
  359. INIT_OP_DECLARE (cap_setup);
  360. #endif