ipc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright (c) 2022 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. #include <kern/capability.h>
  18. #include <kern/cspace.h>
  19. #include <kern/ipc.h>
  20. #include <kern/task.h>
  21. #include <kern/thread.h>
  22. #include <kern/unwind.h>
  23. #include <kern/user.h>
  24. #include <machine/cpu.h>
  25. #include <vm/map.h>
  26. #include <vm/page.h>
  27. struct ipc_data
  28. {
  29. cpu_flags_t cpu_flags;
  30. uint32_t flags;
  31. int prot;
  32. struct pmap_window wstore;
  33. struct pmap_window *window;
  34. struct vm_page *page;
  35. };
  36. static void
  37. ipc_data_init (struct ipc_data *data, uint32_t flags)
  38. {
  39. data->flags = flags;
  40. data->prot = (flags & IPC_COPY_FROM) ? VM_PROT_READ : VM_PROT_RDWR;
  41. data->window = NULL;
  42. data->page = NULL;
  43. }
  44. static void
  45. ipc_data_intr_save (struct ipc_data *data)
  46. {
  47. cpu_intr_save (&data->cpu_flags);
  48. }
  49. static void
  50. ipc_data_intr_restore (struct ipc_data *data)
  51. {
  52. cpu_intr_restore (data->cpu_flags);
  53. }
  54. static void
  55. ipc_data_page_ref (struct ipc_data *data, phys_addr_t pa)
  56. {
  57. assert (!data->page);
  58. struct vm_page *page = vm_page_lookup (pa);
  59. assert (page);
  60. vm_page_ref (page);
  61. data->page = page;
  62. }
  63. static void
  64. ipc_data_page_unref (struct ipc_data *data)
  65. {
  66. assert (data->page);
  67. vm_page_unref (data->page);
  68. data->page = NULL;
  69. }
  70. static void
  71. ipc_data_win_get (struct ipc_data *data)
  72. {
  73. data->window = pmap_window_load (0, &data->wstore);
  74. }
  75. static void
  76. ipc_data_win_map (struct ipc_data *data, phys_addr_t pa)
  77. {
  78. pmap_window_set (data->window, pa);
  79. }
  80. static void
  81. ipc_data_win_put (struct ipc_data *data)
  82. {
  83. pmap_window_put (data->window);
  84. data->window = NULL;
  85. }
  86. static void
  87. ipc_data_fini (void *arg)
  88. {
  89. struct ipc_data *data = arg;
  90. if (data->window)
  91. ipc_data_win_put (data);
  92. if (data->page)
  93. vm_page_unref (data->page);
  94. }
  95. static int
  96. ipc_iov_iter_refill (struct task *task, struct ipc_iov_iter *it)
  97. {
  98. uint32_t cnt = MIN (it->end - it->cur, IPC_IOV_ITER_CACHE_SIZE),
  99. off = IPC_IOV_ITER_CACHE_SIZE - cnt,
  100. bsize = cnt * sizeof (struct iovec);
  101. ssize_t ret = ipc_bcopy (task, it->begin + it->cur, bsize,
  102. &it->cache[off], bsize,
  103. IPC_COPY_FROM | IPC_CHECK_REMOTE);
  104. if (ret < 0 || (ret % sizeof (struct iovec)) != 0)
  105. return (-EFAULT);
  106. it->cur += cnt;
  107. it->cache_idx = off;
  108. return (0);
  109. }
  110. static int
  111. ipc_map_errno (int err)
  112. {
  113. switch (err)
  114. {
  115. case EACCES:
  116. return (-EPERM);
  117. case EFAULT:
  118. return (-ENXIO);
  119. default:
  120. return (-err);
  121. }
  122. }
  123. /*
  124. * Get the physical address associated to a remote virtual address, faulting
  125. * in the necessary pages in case they aren't resident already. This function
  126. * disables interrupts but doesn't restore them when done.
  127. */
  128. static int
  129. ipc_map_addr (struct vm_map *map, const void *addr,
  130. struct ipc_data *data, phys_addr_t *pap)
  131. {
  132. ipc_data_intr_save (data);
  133. int error = pmap_extract_check (map->pmap, (uintptr_t)addr,
  134. data->prot & VM_PROT_WRITE, pap);
  135. if (error)
  136. { // Need to page in the destination address.
  137. error = vm_map_fault (map, (uintptr_t)addr, data->prot);
  138. if (error)
  139. {
  140. ipc_data_intr_restore (data);
  141. return (ipc_map_errno (error));
  142. }
  143. /*
  144. * Since we're running with interrupts disabled, and the address
  145. * has been paged in, this call cannot fail.
  146. */
  147. error = pmap_extract (map->pmap, (uintptr_t)addr, pap);
  148. assert (! error);
  149. }
  150. return (0);
  151. }
  152. static ssize_t
  153. ipc_bcopyv_impl (struct vm_map *r_map, const struct iovec *r_v,
  154. const struct iovec *l_v, struct ipc_data *data)
  155. {
  156. size_t page_off = (uintptr_t)r_v->iov_base % PAGE_SIZE,
  157. ret = MIN (PAGE_SIZE - page_off, MIN (r_v->iov_len, l_v->iov_len));
  158. phys_addr_t pa;
  159. int error = ipc_map_addr (r_map, r_v->iov_base, data, &pa);
  160. if (error)
  161. return (error);
  162. ipc_data_win_get (data);
  163. ipc_data_page_ref (data, pa);
  164. ipc_data_win_map (data, pa);
  165. ipc_data_intr_restore (data);
  166. void *va = (char *)pmap_window_va (data->window) + page_off;
  167. if (data->flags & IPC_COPY_TO)
  168. memcpy (va, l_v->iov_base, ret);
  169. else
  170. memcpy ((void *)l_v->iov_base, va, ret);
  171. ipc_data_win_put (data);
  172. ipc_data_page_unref (data);
  173. return ((ssize_t)ret);
  174. }
  175. struct iovec*
  176. ipc_iov_iter_usrnext (struct ipc_iov_iter *it, ssize_t *errp)
  177. {
  178. while (1)
  179. {
  180. if (it->head.iov_len)
  181. return (&it->head);
  182. else if (it->cur >= it->end)
  183. return (NULL);
  184. _Auto iov = it->begin + it->cur;
  185. if (errp && !user_check_range (iov->iov_base, iov->iov_len))
  186. {
  187. *errp = -EFAULT;
  188. return (NULL);
  189. }
  190. it->head = *iov;
  191. ++it->cur;
  192. }
  193. }
  194. static struct iovec*
  195. ipc_iov_iter_next_remote (struct ipc_iov_iter *it,
  196. struct task *task, ssize_t *outp)
  197. { // Same as above, only for a remote iterator.
  198. while (1)
  199. {
  200. if (it->head.iov_len)
  201. return (&it->head);
  202. else if (it->cache_idx < IPC_IOV_ITER_CACHE_SIZE)
  203. it->head = it->cache[it->cache_idx++];
  204. else if (it->cur >= it->end)
  205. return (NULL);
  206. else
  207. {
  208. int error = ipc_iov_iter_refill (task, it);
  209. if (error)
  210. {
  211. *outp = error;
  212. return (NULL);
  213. }
  214. }
  215. }
  216. }
  217. ssize_t
  218. ipc_iov_iter_copy (struct task *r_task, struct ipc_iov_iter *r_it,
  219. struct ipc_iov_iter *l_it, uint32_t flags)
  220. {
  221. struct ipc_data data;
  222. ipc_data_init (&data, flags);
  223. struct unw_fixup fixup;
  224. int error = unw_fixup_save (&fixup);
  225. if (unlikely (error))
  226. {
  227. ipc_data_fini (&data);
  228. return (-error);
  229. }
  230. ssize_t ret = 0, *l_err = (flags & IPC_CHECK_LOCAL) ? &ret : NULL;
  231. while (1)
  232. {
  233. struct iovec *lv = ipc_iov_iter_usrnext (l_it, l_err);
  234. if (! lv)
  235. return (ret);
  236. struct iovec *rv = ipc_iov_iter_next_remote (r_it, r_task, &ret);
  237. if (! rv)
  238. return (ret);
  239. ssize_t tmp = ipc_bcopyv_impl (r_task->map, rv, lv, &data);
  240. if (tmp < 0)
  241. return (tmp);
  242. else if (unlikely ((ret += tmp) < 0))
  243. return (-EOVERFLOW);
  244. iovec_adv (lv, tmp);
  245. iovec_adv (rv, tmp);
  246. }
  247. }
  248. ssize_t
  249. ipc_bcopy (struct task *r_task, void *r_ptr, size_t r_size,
  250. void *l_ptr, size_t l_size, uint32_t flags)
  251. {
  252. if (((flags & IPC_CHECK_REMOTE) &&
  253. !user_check_range (r_ptr, r_size)) ||
  254. ((flags & IPC_CHECK_LOCAL) &&
  255. !user_check_range (l_ptr, l_size)))
  256. return (-EFAULT);
  257. struct ipc_data data;
  258. struct unw_fixup fixup;
  259. int error = unw_fixup_save (&fixup);
  260. if (unlikely (error))
  261. {
  262. ipc_data_fini (&data);
  263. return (-error);
  264. }
  265. ipc_data_init (&data, flags);
  266. struct iovec r_v = IOVEC (r_ptr, r_size), l_v = IOVEC (l_ptr, l_size);
  267. for (ssize_t ret = 0 ; ; )
  268. {
  269. if (!r_v.iov_len || !l_v.iov_len)
  270. return (ret);
  271. ssize_t tmp = ipc_bcopyv_impl (r_task->map, &r_v, &l_v, &data);
  272. if (tmp < 0)
  273. return (tmp);
  274. else if (unlikely ((ret += tmp) < 0))
  275. return (-EOVERFLOW);
  276. iovec_adv (&r_v, tmp);
  277. iovec_adv (&l_v, tmp);
  278. }
  279. }
  280. static void
  281. ipc_cspace_guard_fini (struct adaptive_lock **lockp)
  282. {
  283. if (*lockp)
  284. adaptive_lock_release (*lockp);
  285. }
  286. static int
  287. ipc_cap_iter_cleanup (struct cspace *sp, struct ipc_cap_iter *it,
  288. uint32_t idx, int error)
  289. {
  290. for (; it->cur != idx; --it->cur)
  291. cspace_rem_locked (sp, it->begin[it->cur - 1].cap);
  292. return (error);
  293. }
  294. static int
  295. ipc_cap_copy_one (struct cspace *in_cs, struct cspace *out_cs,
  296. struct ipc_cap_iter *in_it, struct ipc_cap_iter *out_it)
  297. {
  298. int mark, capx = in_it->begin[in_it->cur].cap;
  299. _Auto cap = cspace_get_all (in_cs, capx, &mark);
  300. if (unlikely (! cap))
  301. return (-EBADF);
  302. _Auto outp = out_it->begin + out_it->cur;
  303. capx = cspace_add_free_locked (out_cs, cap, outp->flags & ~CSPACE_MASK);
  304. cap_base_rel (cap);
  305. if (unlikely (capx < 0))
  306. return (capx);
  307. else if (mark && cap_type (cap) == CAP_TYPE_CHANNEL &&
  308. cap_channel_mark_shared (cap))
  309. cap_base_rel (cap);
  310. outp->cap = capx;
  311. ++in_it->cur;
  312. ++out_it->cur;
  313. return (0);
  314. }
  315. static int
  316. ipc_cap_copy_impl (struct task *r_task, struct ipc_cap_iter *r_it,
  317. struct ipc_cap_iter *l_it, uint32_t flags)
  318. {
  319. struct ipc_cap_iter *in_it, *out_it;
  320. struct cspace *in_cs, *out_cs;
  321. if (flags & IPC_COPY_FROM)
  322. {
  323. in_it = r_it, out_it = l_it;
  324. in_cs = &r_task->caps, out_cs = cspace_self ();
  325. }
  326. else
  327. {
  328. in_it = l_it, out_it = r_it;
  329. in_cs = cspace_self (), out_cs = &r_task->caps;
  330. }
  331. uint32_t prev = out_it->cur;
  332. int rv = 0;
  333. struct adaptive_lock *lock CLEANUP (ipc_cspace_guard_fini) = &out_cs->lock;
  334. ADAPTIVE_LOCK_GUARD (&in_cs->lock);
  335. if (likely (in_cs != out_cs))
  336. adaptive_lock_acquire (lock);
  337. else
  338. lock = NULL;
  339. for (; ipc_cap_iter_size (in_it) && ipc_cap_iter_size (out_it); ++rv)
  340. {
  341. int tmp = ipc_cap_copy_one (in_cs, out_cs, in_it, out_it);
  342. if (unlikely (tmp != 0))
  343. return (ipc_cap_iter_cleanup (out_cs, out_it, prev, tmp));
  344. }
  345. return (rv);
  346. }
  347. static void*
  348. ipc_buffer_alloc (void *array, int iter_len, int room, int size)
  349. {
  350. if (iter_len <= room)
  351. return (array);
  352. _Auto page = vm_page_alloc (vm_page_order (size), VM_PAGE_SEL_DIRECTMAP,
  353. VM_PAGE_KERNEL, VM_PAGE_SLEEP);
  354. return (page ? vm_page_direct_ptr (page) : NULL);
  355. }
  356. static void
  357. ipc_buffer_free (void *array, void *ptr, int size)
  358. {
  359. if (array != ptr)
  360. vm_page_free (vm_page_lookup (vm_page_direct_pa ((uintptr_t)ptr)),
  361. vm_page_order (size), VM_PAGE_SLEEP);
  362. }
  363. #define IPC_ITER_LOOP(type, fn) \
  364. struct ipc_msg_##type tmp[16], *ptr; \
  365. int len = ipc_##type##_iter_size (r_it), size = len * sizeof (*ptr); \
  366. \
  367. ptr = ipc_buffer_alloc (tmp, len, (int)ARRAY_SIZE (tmp), size); \
  368. if (! ptr) \
  369. return (-ENOMEM); \
  370. \
  371. int rv = ipc_bcopy (r_task, r_it->begin + r_it->cur, size, ptr, size, \
  372. IPC_COPY_FROM | (flags & IPC_CHECK_REMOTE)); \
  373. \
  374. if (unlikely (rv < 0)) \
  375. { \
  376. ipc_buffer_free (tmp, ptr, size); \
  377. return (rv); \
  378. } \
  379. \
  380. struct ipc_##type##_iter aux = \
  381. { \
  382. .begin = ptr, \
  383. .cur = 0, \
  384. .end = r_it->end - r_it->cur \
  385. }; \
  386. \
  387. struct unw_fixup fx; \
  388. rv = unw_fixup_save (&fx); \
  389. rv = rv == 0 ? fn (r_task, &aux, l_it, flags) : -rv; \
  390. if (rv >= 0) \
  391. { \
  392. len = rv * sizeof (*ptr); \
  393. if (ipc_bcopy (r_task, r_it->begin + r_it->cur, len, \
  394. ptr, len, IPC_COPY_TO | IPC_CHECK_REMOTE) > 0) \
  395. r_it->cur += aux.cur; \
  396. } \
  397. \
  398. ipc_buffer_free (tmp, ptr, size); \
  399. return (rv)
  400. int
  401. ipc_cap_iter_copy (struct task *r_task, struct ipc_cap_iter *r_it,
  402. struct ipc_cap_iter *l_it, uint32_t flags)
  403. {
  404. if ((flags & IPC_CHECK_LOCAL) &&
  405. !user_check_range (l_it->begin, l_it->end * sizeof (*l_it->begin)))
  406. return (-EFAULT);
  407. IPC_ITER_LOOP (cap, ipc_cap_copy_impl);
  408. }
  409. int
  410. ipc_vme_iter_copy (struct task *r_task, struct ipc_vme_iter *r_it,
  411. struct ipc_vme_iter *l_it, uint32_t flags)
  412. {
  413. if ((flags & IPC_CHECK_LOCAL) &&
  414. !user_check_range (l_it->begin, l_it->end * sizeof (*l_it->begin)))
  415. return (-EFAULT);
  416. #define ipc_vme_copy_impl(task, r_it, l_it, flg) \
  417. vm_map_iter_copy ((task)->map, (r_it), (l_it), (flg))
  418. IPC_ITER_LOOP (vme, ipc_vme_copy_impl);
  419. #undef ipc_vme_copy_impl
  420. }