ipc.c 11 KB

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