ipc.c 11 KB

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