vm_mgr.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "gh_vm_mgr: " fmt
  6. #include <linux/anon_inodes.h>
  7. #include <linux/compat.h>
  8. #include <linux/file.h>
  9. #include <linux/gunyah_rsc_mgr.h>
  10. #include <linux/gunyah_vm_mgr.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/xarray.h>
  15. #include <uapi/linux/gunyah.h>
  16. #include "vm_mgr.h"
  17. static void gh_vm_free(struct work_struct *work);
  18. static DEFINE_XARRAY(gh_vm_functions);
  19. static void gh_vm_put_function(struct gh_vm_function *fn)
  20. {
  21. module_put(fn->mod);
  22. }
  23. static struct gh_vm_function *gh_vm_get_function(u32 type)
  24. {
  25. struct gh_vm_function *fn;
  26. fn = xa_load(&gh_vm_functions, type);
  27. if (!fn) {
  28. request_module("ghfunc:%d", type);
  29. fn = xa_load(&gh_vm_functions, type);
  30. }
  31. if (!fn || !try_module_get(fn->mod))
  32. fn = ERR_PTR(-ENOENT);
  33. return fn;
  34. }
  35. static void gh_vm_remove_function_instance(struct gh_vm_function_instance *inst)
  36. __must_hold(&inst->ghvm->fn_lock)
  37. {
  38. inst->fn->unbind(inst);
  39. list_del(&inst->vm_list);
  40. gh_vm_put_function(inst->fn);
  41. kfree(inst->argp);
  42. kfree(inst);
  43. }
  44. static void gh_vm_remove_functions(struct gh_vm *ghvm)
  45. {
  46. struct gh_vm_function_instance *inst, *iiter;
  47. mutex_lock(&ghvm->fn_lock);
  48. list_for_each_entry_safe(inst, iiter, &ghvm->functions, vm_list) {
  49. gh_vm_remove_function_instance(inst);
  50. }
  51. mutex_unlock(&ghvm->fn_lock);
  52. }
  53. static long gh_vm_add_function_instance(struct gh_vm *ghvm, struct gh_fn_desc *f)
  54. {
  55. struct gh_vm_function_instance *inst;
  56. void __user *argp;
  57. long r = 0;
  58. if (f->arg_size > GH_FN_MAX_ARG_SIZE) {
  59. dev_err_ratelimited(ghvm->parent, "%s: arg_size > %d\n",
  60. __func__, GH_FN_MAX_ARG_SIZE);
  61. return -EINVAL;
  62. }
  63. inst = kzalloc(sizeof(*inst), GFP_KERNEL);
  64. if (!inst)
  65. return -ENOMEM;
  66. inst->arg_size = f->arg_size;
  67. if (inst->arg_size) {
  68. inst->argp = kzalloc(inst->arg_size, GFP_KERNEL);
  69. if (!inst->argp) {
  70. r = -ENOMEM;
  71. goto free;
  72. }
  73. argp = u64_to_user_ptr(f->arg);
  74. if (copy_from_user(inst->argp, argp, f->arg_size)) {
  75. r = -EFAULT;
  76. goto free_arg;
  77. }
  78. }
  79. inst->fn = gh_vm_get_function(f->type);
  80. if (IS_ERR(inst->fn)) {
  81. r = PTR_ERR(inst->fn);
  82. goto free_arg;
  83. }
  84. inst->ghvm = ghvm;
  85. inst->rm = ghvm->rm;
  86. mutex_lock(&ghvm->fn_lock);
  87. r = inst->fn->bind(inst);
  88. if (r < 0) {
  89. mutex_unlock(&ghvm->fn_lock);
  90. gh_vm_put_function(inst->fn);
  91. goto free_arg;
  92. }
  93. list_add(&inst->vm_list, &ghvm->functions);
  94. mutex_unlock(&ghvm->fn_lock);
  95. return r;
  96. free_arg:
  97. kfree(inst->argp);
  98. free:
  99. kfree(inst);
  100. return r;
  101. }
  102. static long gh_vm_rm_function_instance(struct gh_vm *ghvm, struct gh_fn_desc *f)
  103. {
  104. struct gh_vm_function_instance *inst, *iter;
  105. void __user *user_argp;
  106. void *argp;
  107. long r = 0;
  108. r = mutex_lock_interruptible(&ghvm->fn_lock);
  109. if (r)
  110. return r;
  111. if (f->arg_size) {
  112. argp = kzalloc(f->arg_size, GFP_KERNEL);
  113. if (!argp) {
  114. r = -ENOMEM;
  115. goto out;
  116. }
  117. user_argp = u64_to_user_ptr(f->arg);
  118. if (copy_from_user(argp, user_argp, f->arg_size)) {
  119. r = -EFAULT;
  120. kfree(argp);
  121. goto out;
  122. }
  123. r = -ENOENT;
  124. list_for_each_entry_safe(inst, iter, &ghvm->functions, vm_list) {
  125. if (inst->fn->type == f->type &&
  126. inst->fn->compare(inst, argp, f->arg_size)) {
  127. gh_vm_remove_function_instance(inst);
  128. r = 0;
  129. }
  130. }
  131. kfree(argp);
  132. }
  133. out:
  134. mutex_unlock(&ghvm->fn_lock);
  135. return r;
  136. }
  137. int gh_vm_function_register(struct gh_vm_function *fn)
  138. {
  139. if (!fn->bind || !fn->unbind)
  140. return -EINVAL;
  141. return xa_err(xa_store(&gh_vm_functions, fn->type, fn, GFP_KERNEL));
  142. }
  143. EXPORT_SYMBOL_GPL(gh_vm_function_register);
  144. void gh_vm_function_unregister(struct gh_vm_function *fn)
  145. {
  146. /* Expecting unregister to only come when unloading a module */
  147. WARN_ON(fn->mod && module_refcount(fn->mod));
  148. xa_erase(&gh_vm_functions, fn->type);
  149. }
  150. EXPORT_SYMBOL_GPL(gh_vm_function_unregister);
  151. int gh_vm_add_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ticket *ticket)
  152. {
  153. struct gh_vm_resource_ticket *iter;
  154. struct gh_resource *ghrsc, *rsc_iter;
  155. int ret = 0;
  156. mutex_lock(&ghvm->resources_lock);
  157. list_for_each_entry(iter, &ghvm->resource_tickets, vm_list) {
  158. if (iter->resource_type == ticket->resource_type && iter->label == ticket->label) {
  159. ret = -EEXIST;
  160. goto out;
  161. }
  162. }
  163. if (!try_module_get(ticket->owner)) {
  164. ret = -ENODEV;
  165. goto out;
  166. }
  167. list_add(&ticket->vm_list, &ghvm->resource_tickets);
  168. INIT_LIST_HEAD(&ticket->resources);
  169. list_for_each_entry_safe(ghrsc, rsc_iter, &ghvm->resources, list) {
  170. if (ghrsc->type == ticket->resource_type && ghrsc->rm_label == ticket->label) {
  171. if (ticket->populate(ticket, ghrsc))
  172. list_move(&ghrsc->list, &ticket->resources);
  173. }
  174. }
  175. out:
  176. mutex_unlock(&ghvm->resources_lock);
  177. return ret;
  178. }
  179. EXPORT_SYMBOL_GPL(gh_vm_add_resource_ticket);
  180. void gh_vm_remove_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ticket *ticket)
  181. {
  182. struct gh_resource *ghrsc, *iter;
  183. mutex_lock(&ghvm->resources_lock);
  184. list_for_each_entry_safe(ghrsc, iter, &ticket->resources, list) {
  185. ticket->unpopulate(ticket, ghrsc);
  186. list_move(&ghrsc->list, &ghvm->resources);
  187. }
  188. module_put(ticket->owner);
  189. list_del(&ticket->vm_list);
  190. mutex_unlock(&ghvm->resources_lock);
  191. }
  192. EXPORT_SYMBOL_GPL(gh_vm_remove_resource_ticket);
  193. static void gh_vm_add_resource(struct gh_vm *ghvm, struct gh_resource *ghrsc)
  194. {
  195. struct gh_vm_resource_ticket *ticket;
  196. mutex_lock(&ghvm->resources_lock);
  197. list_for_each_entry(ticket, &ghvm->resource_tickets, vm_list) {
  198. if (ghrsc->type == ticket->resource_type && ghrsc->rm_label == ticket->label) {
  199. if (ticket->populate(ticket, ghrsc))
  200. list_add(&ghrsc->list, &ticket->resources);
  201. else
  202. list_add(&ghrsc->list, &ghvm->resources);
  203. /* unconditonal -- we prevent multiple identical
  204. * resource tickets so there will not be some other
  205. * ticket elsewhere in the list if populate() failed.
  206. */
  207. goto found;
  208. }
  209. }
  210. list_add(&ghrsc->list, &ghvm->resources);
  211. found:
  212. mutex_unlock(&ghvm->resources_lock);
  213. }
  214. static void gh_vm_clean_resources(struct gh_vm *ghvm)
  215. {
  216. struct gh_vm_resource_ticket *ticket, *titer;
  217. struct gh_resource *ghrsc, *riter;
  218. mutex_lock(&ghvm->resources_lock);
  219. if (!list_empty(&ghvm->resource_tickets)) {
  220. dev_warn(ghvm->parent, "Dangling resource tickets:\n");
  221. list_for_each_entry_safe(ticket, titer, &ghvm->resource_tickets, vm_list) {
  222. dev_warn(ghvm->parent, " %pS\n", ticket->populate);
  223. gh_vm_remove_resource_ticket(ghvm, ticket);
  224. }
  225. }
  226. list_for_each_entry_safe(ghrsc, riter, &ghvm->resources, list) {
  227. gh_rm_free_resource(ghrsc);
  228. }
  229. mutex_unlock(&ghvm->resources_lock);
  230. }
  231. static int _gh_vm_io_handler_compare(const struct rb_node *node, const struct rb_node *parent)
  232. {
  233. struct gh_vm_io_handler *n = container_of(node, struct gh_vm_io_handler, node);
  234. struct gh_vm_io_handler *p = container_of(parent, struct gh_vm_io_handler, node);
  235. if (n->addr < p->addr)
  236. return -1;
  237. if (n->addr > p->addr)
  238. return 1;
  239. if ((n->len && !p->len) || (!n->len && p->len))
  240. return 0;
  241. if (n->len < p->len)
  242. return -1;
  243. if (n->len > p->len)
  244. return 1;
  245. /* one of the io handlers doesn't have datamatch and the other does.
  246. * For purposes of comparison, that makes them identical since the
  247. * one that doesn't have datamatch will cover the same handler that
  248. * does.
  249. */
  250. if (n->datamatch != p->datamatch)
  251. return 0;
  252. if (n->data < p->data)
  253. return -1;
  254. if (n->data > p->data)
  255. return 1;
  256. return 0;
  257. }
  258. static int gh_vm_io_handler_compare(struct rb_node *node, const struct rb_node *parent)
  259. {
  260. return _gh_vm_io_handler_compare(node, parent);
  261. }
  262. static int gh_vm_io_handler_find(const void *key, const struct rb_node *node)
  263. {
  264. const struct gh_vm_io_handler *k = key;
  265. return _gh_vm_io_handler_compare(&k->node, node);
  266. }
  267. static struct gh_vm_io_handler *gh_vm_mgr_find_io_hdlr(struct gh_vm *ghvm, u64 addr,
  268. u64 len, u64 data)
  269. {
  270. struct gh_vm_io_handler key = {
  271. .addr = addr,
  272. .len = len,
  273. .datamatch = true,
  274. .data = data,
  275. };
  276. struct rb_node *node;
  277. node = rb_find(&key, &ghvm->mmio_handler_root, gh_vm_io_handler_find);
  278. if (!node)
  279. return NULL;
  280. return container_of(node, struct gh_vm_io_handler, node);
  281. }
  282. int gh_vm_mmio_write(struct gh_vm *ghvm, u64 addr, u32 len, u64 data)
  283. {
  284. struct gh_vm_io_handler *io_hdlr = NULL;
  285. int ret;
  286. down_read(&ghvm->mmio_handler_lock);
  287. io_hdlr = gh_vm_mgr_find_io_hdlr(ghvm, addr, len, data);
  288. if (!io_hdlr || !io_hdlr->ops || !io_hdlr->ops->write) {
  289. ret = -ENODEV;
  290. goto out;
  291. }
  292. ret = io_hdlr->ops->write(io_hdlr, addr, len, data);
  293. out:
  294. up_read(&ghvm->mmio_handler_lock);
  295. return ret;
  296. }
  297. EXPORT_SYMBOL_GPL(gh_vm_mmio_write);
  298. int gh_vm_add_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_hdlr)
  299. {
  300. struct rb_node *found;
  301. if (io_hdlr->datamatch && (!io_hdlr->len || io_hdlr->len > sizeof(io_hdlr->data)))
  302. return -EINVAL;
  303. down_write(&ghvm->mmio_handler_lock);
  304. found = rb_find_add(&io_hdlr->node, &ghvm->mmio_handler_root, gh_vm_io_handler_compare);
  305. up_write(&ghvm->mmio_handler_lock);
  306. return found ? -EEXIST : 0;
  307. }
  308. EXPORT_SYMBOL_GPL(gh_vm_add_io_handler);
  309. void gh_vm_remove_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_hdlr)
  310. {
  311. down_write(&ghvm->mmio_handler_lock);
  312. rb_erase(&io_hdlr->node, &ghvm->mmio_handler_root);
  313. up_write(&ghvm->mmio_handler_lock);
  314. }
  315. EXPORT_SYMBOL_GPL(gh_vm_remove_io_handler);
  316. static int gh_vm_rm_notification_status(struct gh_vm *ghvm, void *data)
  317. {
  318. struct gh_rm_vm_status_payload *payload = data;
  319. if (le16_to_cpu(payload->vmid) != ghvm->vmid)
  320. return NOTIFY_OK;
  321. /* All other state transitions are synchronous to a corresponding RM call */
  322. if (payload->vm_status == GH_RM_VM_STATUS_RESET) {
  323. down_write(&ghvm->status_lock);
  324. ghvm->vm_status = payload->vm_status;
  325. up_write(&ghvm->status_lock);
  326. wake_up(&ghvm->vm_status_wait);
  327. }
  328. return NOTIFY_DONE;
  329. }
  330. static int gh_vm_rm_notification_exited(struct gh_vm *ghvm, void *data)
  331. {
  332. struct gh_rm_vm_exited_payload *payload = data;
  333. if (le16_to_cpu(payload->vmid) != ghvm->vmid)
  334. return NOTIFY_OK;
  335. down_write(&ghvm->status_lock);
  336. ghvm->vm_status = GH_RM_VM_STATUS_EXITED;
  337. ghvm->exit_info.type = le16_to_cpu(payload->exit_type);
  338. ghvm->exit_info.reason_size = le32_to_cpu(payload->exit_reason_size);
  339. memcpy(&ghvm->exit_info.reason, payload->exit_reason,
  340. min(GH_VM_MAX_EXIT_REASON_SIZE, ghvm->exit_info.reason_size));
  341. up_write(&ghvm->status_lock);
  342. wake_up(&ghvm->vm_status_wait);
  343. return NOTIFY_DONE;
  344. }
  345. static int gh_vm_rm_notification(struct notifier_block *nb, unsigned long action, void *data)
  346. {
  347. struct gh_vm *ghvm = container_of(nb, struct gh_vm, nb);
  348. switch (action) {
  349. case GH_RM_NOTIFICATION_VM_STATUS:
  350. return gh_vm_rm_notification_status(ghvm, data);
  351. case GH_RM_NOTIFICATION_VM_EXITED:
  352. return gh_vm_rm_notification_exited(ghvm, data);
  353. default:
  354. return NOTIFY_OK;
  355. }
  356. }
  357. static void gh_vm_stop(struct gh_vm *ghvm)
  358. {
  359. int ret;
  360. down_write(&ghvm->status_lock);
  361. if (ghvm->vm_status == GH_RM_VM_STATUS_RUNNING) {
  362. ret = gh_rm_vm_stop(ghvm->rm, ghvm->vmid);
  363. if (ret)
  364. dev_warn(ghvm->parent, "Failed to stop VM: %d\n", ret);
  365. }
  366. up_write(&ghvm->status_lock);
  367. wait_event(ghvm->vm_status_wait, ghvm->vm_status == GH_RM_VM_STATUS_EXITED);
  368. }
  369. static __must_check struct gh_vm *gh_vm_alloc(struct gh_rm *rm)
  370. {
  371. struct gh_vm *ghvm;
  372. ghvm = kzalloc(sizeof(*ghvm), GFP_KERNEL);
  373. if (!ghvm)
  374. return ERR_PTR(-ENOMEM);
  375. ghvm->parent = gh_rm_get(rm);
  376. ghvm->vmid = GH_VMID_INVAL;
  377. ghvm->rm = rm;
  378. mmgrab(current->mm);
  379. ghvm->mm = current->mm;
  380. mutex_init(&ghvm->mm_lock);
  381. INIT_LIST_HEAD(&ghvm->memory_mappings);
  382. init_rwsem(&ghvm->status_lock);
  383. init_waitqueue_head(&ghvm->vm_status_wait);
  384. INIT_WORK(&ghvm->free_work, gh_vm_free);
  385. kref_init(&ghvm->kref);
  386. mutex_init(&ghvm->resources_lock);
  387. INIT_LIST_HEAD(&ghvm->resources);
  388. INIT_LIST_HEAD(&ghvm->resource_tickets);
  389. init_rwsem(&ghvm->mmio_handler_lock);
  390. ghvm->mmio_handler_root = RB_ROOT;
  391. INIT_LIST_HEAD(&ghvm->functions);
  392. ghvm->vm_status = GH_RM_VM_STATUS_NO_STATE;
  393. return ghvm;
  394. }
  395. static int gh_vm_start(struct gh_vm *ghvm)
  396. {
  397. struct gh_vm_mem *mapping;
  398. struct gh_rm_hyp_resources *resources;
  399. struct gh_resource *ghrsc;
  400. u64 dtb_offset;
  401. u32 mem_handle;
  402. int ret, i, n;
  403. down_write(&ghvm->status_lock);
  404. if (ghvm->vm_status != GH_RM_VM_STATUS_NO_STATE) {
  405. up_write(&ghvm->status_lock);
  406. return 0;
  407. }
  408. ghvm->nb.notifier_call = gh_vm_rm_notification;
  409. ret = gh_rm_notifier_register(ghvm->rm, &ghvm->nb);
  410. if (ret)
  411. goto err;
  412. ret = gh_rm_alloc_vmid(ghvm->rm, 0);
  413. if (ret < 0) {
  414. gh_rm_notifier_unregister(ghvm->rm, &ghvm->nb);
  415. goto err;
  416. }
  417. ghvm->vmid = ret;
  418. ghvm->vm_status = GH_RM_VM_STATUS_LOAD;
  419. mutex_lock(&ghvm->mm_lock);
  420. list_for_each_entry(mapping, &ghvm->memory_mappings, list) {
  421. mapping->parcel.acl_entries[0].vmid = cpu_to_le16(ghvm->vmid);
  422. switch (mapping->share_type) {
  423. case VM_MEM_LEND:
  424. ret = gh_rm_mem_lend(ghvm->rm, &mapping->parcel);
  425. break;
  426. case VM_MEM_SHARE:
  427. ret = gh_rm_mem_share(ghvm->rm, &mapping->parcel);
  428. break;
  429. }
  430. if (ret) {
  431. dev_warn(ghvm->parent, "Failed to %s parcel %d: %d\n",
  432. mapping->share_type == VM_MEM_LEND ? "lend" : "share",
  433. mapping->parcel.label, ret);
  434. mutex_unlock(&ghvm->mm_lock);
  435. goto err;
  436. }
  437. }
  438. mutex_unlock(&ghvm->mm_lock);
  439. mapping = gh_vm_mem_find_by_addr(ghvm, ghvm->dtb_config.guest_phys_addr,
  440. ghvm->dtb_config.size);
  441. if (!mapping) {
  442. dev_warn(ghvm->parent, "Failed to find the memory_handle for DTB\n");
  443. ret = -EINVAL;
  444. goto err;
  445. }
  446. mem_handle = mapping->parcel.mem_handle;
  447. dtb_offset = ghvm->dtb_config.guest_phys_addr - mapping->guest_phys_addr;
  448. ret = gh_rm_vm_configure(ghvm->rm, ghvm->vmid, ghvm->auth, mem_handle,
  449. 0, 0, dtb_offset, ghvm->dtb_config.size);
  450. if (ret) {
  451. dev_warn(ghvm->parent, "Failed to configure VM: %d\n", ret);
  452. goto err;
  453. }
  454. if (ghvm->auth == GH_RM_VM_AUTH_QCOM_ANDROID_PVM) {
  455. mapping = gh_vm_mem_find_by_addr(ghvm, ghvm->fw_config.guest_phys_addr,
  456. ghvm->fw_config.size);
  457. if (!mapping) {
  458. pr_warn("Failed to find the memory handle for pVM firmware\n");
  459. ret = -EINVAL;
  460. goto err;
  461. }
  462. ret = gh_rm_vm_set_firmware_mem(ghvm->rm, ghvm->vmid, &mapping->parcel,
  463. ghvm->fw_config.guest_phys_addr - mapping->guest_phys_addr,
  464. ghvm->fw_config.size);
  465. if (ret) {
  466. pr_warn("Failed to configure pVM firmware\n");
  467. goto err;
  468. }
  469. }
  470. ret = gh_rm_vm_init(ghvm->rm, ghvm->vmid);
  471. if (ret) {
  472. ghvm->vm_status = GH_RM_VM_STATUS_INIT_FAILED;
  473. dev_warn(ghvm->parent, "Failed to initialize VM: %d\n", ret);
  474. goto err;
  475. }
  476. ghvm->vm_status = GH_RM_VM_STATUS_READY;
  477. ret = gh_rm_get_hyp_resources(ghvm->rm, ghvm->vmid, &resources);
  478. if (ret) {
  479. dev_warn(ghvm->parent, "Failed to get hypervisor resources for VM: %d\n", ret);
  480. goto err;
  481. }
  482. for (i = 0, n = le32_to_cpu(resources->n_entries); i < n; i++) {
  483. ghrsc = gh_rm_alloc_resource(ghvm->rm, &resources->entries[i]);
  484. if (!ghrsc) {
  485. ret = -ENOMEM;
  486. goto err;
  487. }
  488. gh_vm_add_resource(ghvm, ghrsc);
  489. }
  490. ret = gh_rm_vm_start(ghvm->rm, ghvm->vmid);
  491. if (ret) {
  492. dev_warn(ghvm->parent, "Failed to start VM: %d\n", ret);
  493. goto err;
  494. }
  495. ghvm->vm_status = GH_RM_VM_STATUS_RUNNING;
  496. up_write(&ghvm->status_lock);
  497. return ret;
  498. err:
  499. /* gh_vm_free will handle releasing resources and reclaiming memory */
  500. up_write(&ghvm->status_lock);
  501. return ret;
  502. }
  503. static int gh_vm_ensure_started(struct gh_vm *ghvm)
  504. {
  505. int ret;
  506. ret = down_read_interruptible(&ghvm->status_lock);
  507. if (ret)
  508. return ret;
  509. /* Unlikely because VM is typically started */
  510. if (unlikely(ghvm->vm_status == GH_RM_VM_STATUS_NO_STATE)) {
  511. up_read(&ghvm->status_lock);
  512. ret = gh_vm_start(ghvm);
  513. if (ret)
  514. return ret;
  515. /** gh_vm_start() is guaranteed to bring status out of
  516. * GH_RM_VM_STATUS_LOAD, thus infinitely recursive call is not
  517. * possible
  518. */
  519. return gh_vm_ensure_started(ghvm);
  520. }
  521. /* Unlikely because VM is typically running */
  522. if (unlikely(ghvm->vm_status != GH_RM_VM_STATUS_RUNNING))
  523. ret = -ENODEV;
  524. up_read(&ghvm->status_lock);
  525. return ret;
  526. }
  527. static long gh_vm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  528. {
  529. struct gh_vm *ghvm = filp->private_data;
  530. void __user *argp = (void __user *)arg;
  531. long r;
  532. bool lend = false;
  533. switch (cmd) {
  534. case GH_VM_ANDROID_LEND_USER_MEM:
  535. lend = true;
  536. fallthrough;
  537. case GH_VM_SET_USER_MEM_REGION: {
  538. struct gh_userspace_memory_region region;
  539. /* only allow owner task to add memory */
  540. if (ghvm->mm != current->mm)
  541. return -EPERM;
  542. if (copy_from_user(&region, argp, sizeof(region)))
  543. return -EFAULT;
  544. /* All other flag bits are reserved for future use */
  545. if (region.flags & ~(GH_MEM_ALLOW_READ | GH_MEM_ALLOW_WRITE | GH_MEM_ALLOW_EXEC))
  546. return -EINVAL;
  547. r = gh_vm_mem_alloc(ghvm, &region, lend);
  548. break;
  549. }
  550. case GH_VM_SET_DTB_CONFIG: {
  551. struct gh_vm_dtb_config dtb_config;
  552. if (copy_from_user(&dtb_config, argp, sizeof(dtb_config)))
  553. return -EFAULT;
  554. if (overflows_type(dtb_config.guest_phys_addr + dtb_config.size, u64))
  555. return -EOVERFLOW;
  556. ghvm->dtb_config = dtb_config;
  557. r = 0;
  558. break;
  559. }
  560. case GH_VM_ANDROID_SET_FW_CONFIG: {
  561. r = -EFAULT;
  562. if (copy_from_user(&ghvm->fw_config, argp, sizeof(ghvm->fw_config)))
  563. break;
  564. ghvm->auth = GH_RM_VM_AUTH_QCOM_ANDROID_PVM;
  565. r = 0;
  566. break;
  567. }
  568. case GH_VM_START: {
  569. r = gh_vm_ensure_started(ghvm);
  570. break;
  571. }
  572. case GH_VM_ADD_FUNCTION: {
  573. struct gh_fn_desc f;
  574. if (copy_from_user(&f, argp, sizeof(f)))
  575. return -EFAULT;
  576. r = gh_vm_add_function_instance(ghvm, &f);
  577. break;
  578. }
  579. case GH_VM_REMOVE_FUNCTION: {
  580. struct gh_fn_desc f;
  581. if (copy_from_user(&f, argp, sizeof(f)))
  582. return -EFAULT;
  583. r = gh_vm_rm_function_instance(ghvm, &f);
  584. break;
  585. }
  586. default:
  587. r = -ENOTTY;
  588. break;
  589. }
  590. return r;
  591. }
  592. static void gh_vm_free(struct work_struct *work)
  593. {
  594. struct gh_vm *ghvm = container_of(work, struct gh_vm, free_work);
  595. int ret;
  596. if (ghvm->vm_status == GH_RM_VM_STATUS_RUNNING)
  597. gh_vm_stop(ghvm);
  598. gh_vm_remove_functions(ghvm);
  599. gh_vm_clean_resources(ghvm);
  600. if (ghvm->vm_status != GH_RM_VM_STATUS_NO_STATE &&
  601. ghvm->vm_status != GH_RM_VM_STATUS_LOAD &&
  602. ghvm->vm_status != GH_RM_VM_STATUS_RESET) {
  603. ret = gh_rm_vm_reset(ghvm->rm, ghvm->vmid);
  604. if (ret)
  605. dev_err(ghvm->parent, "Failed to reset the vm: %d\n", ret);
  606. wait_event(ghvm->vm_status_wait, ghvm->vm_status == GH_RM_VM_STATUS_RESET);
  607. }
  608. gh_vm_mem_reclaim(ghvm);
  609. if (ghvm->vm_status > GH_RM_VM_STATUS_NO_STATE) {
  610. gh_rm_notifier_unregister(ghvm->rm, &ghvm->nb);
  611. ret = gh_rm_dealloc_vmid(ghvm->rm, ghvm->vmid);
  612. if (ret)
  613. dev_warn(ghvm->parent, "Failed to deallocate vmid: %d\n", ret);
  614. }
  615. gh_rm_put(ghvm->rm);
  616. mmdrop(ghvm->mm);
  617. kfree(ghvm);
  618. }
  619. int __must_check gh_vm_get(struct gh_vm *ghvm)
  620. {
  621. return kref_get_unless_zero(&ghvm->kref);
  622. }
  623. EXPORT_SYMBOL_GPL(gh_vm_get);
  624. static void _gh_vm_put(struct kref *kref)
  625. {
  626. struct gh_vm *ghvm = container_of(kref, struct gh_vm, kref);
  627. /* VM will be reset and make RM calls which can interruptible sleep.
  628. * Defer to a work so this thread can receive signal.
  629. */
  630. schedule_work(&ghvm->free_work);
  631. }
  632. void gh_vm_put(struct gh_vm *ghvm)
  633. {
  634. kref_put(&ghvm->kref, _gh_vm_put);
  635. }
  636. EXPORT_SYMBOL_GPL(gh_vm_put);
  637. static int gh_vm_release(struct inode *inode, struct file *filp)
  638. {
  639. struct gh_vm *ghvm = filp->private_data;
  640. gh_vm_put(ghvm);
  641. return 0;
  642. }
  643. static const struct file_operations gh_vm_fops = {
  644. .owner = THIS_MODULE,
  645. .unlocked_ioctl = gh_vm_ioctl,
  646. .compat_ioctl = compat_ptr_ioctl,
  647. .release = gh_vm_release,
  648. .llseek = noop_llseek,
  649. };
  650. static long gh_dev_ioctl_create_vm(struct gh_rm *rm, unsigned long arg)
  651. {
  652. struct gh_vm *ghvm;
  653. struct file *file;
  654. int fd, err;
  655. /* arg reserved for future use. */
  656. if (arg)
  657. return -EINVAL;
  658. ghvm = gh_vm_alloc(rm);
  659. if (IS_ERR(ghvm))
  660. return PTR_ERR(ghvm);
  661. fd = get_unused_fd_flags(O_CLOEXEC);
  662. if (fd < 0) {
  663. err = fd;
  664. goto err_destroy_vm;
  665. }
  666. file = anon_inode_getfile("gunyah-vm", &gh_vm_fops, ghvm, O_RDWR);
  667. if (IS_ERR(file)) {
  668. err = PTR_ERR(file);
  669. goto err_put_fd;
  670. }
  671. fd_install(fd, file);
  672. return fd;
  673. err_put_fd:
  674. put_unused_fd(fd);
  675. err_destroy_vm:
  676. gh_vm_put(ghvm);
  677. return err;
  678. }
  679. long gh_dev_vm_mgr_ioctl(struct gh_rm *rm, unsigned int cmd, unsigned long arg)
  680. {
  681. switch (cmd) {
  682. case GH_CREATE_VM:
  683. return gh_dev_ioctl_create_vm(rm, arg);
  684. default:
  685. return -ENOTTY;
  686. }
  687. }