book3s_hv_uvmem.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Secure pages management: Migration of pages between normal and secure
  4. * memory of KVM guests.
  5. *
  6. * Copyright 2018 Bharata B Rao, IBM Corp. <[email protected]>
  7. */
  8. /*
  9. * A pseries guest can be run as secure guest on Ultravisor-enabled
  10. * POWER platforms. On such platforms, this driver will be used to manage
  11. * the movement of guest pages between the normal memory managed by
  12. * hypervisor (HV) and secure memory managed by Ultravisor (UV).
  13. *
  14. * The page-in or page-out requests from UV will come to HV as hcalls and
  15. * HV will call back into UV via ultracalls to satisfy these page requests.
  16. *
  17. * Private ZONE_DEVICE memory equal to the amount of secure memory
  18. * available in the platform for running secure guests is hotplugged.
  19. * Whenever a page belonging to the guest becomes secure, a page from this
  20. * private device memory is used to represent and track that secure page
  21. * on the HV side. Some pages (like virtio buffers, VPA pages etc) are
  22. * shared between UV and HV. However such pages aren't represented by
  23. * device private memory and mappings to shared memory exist in both
  24. * UV and HV page tables.
  25. */
  26. /*
  27. * Notes on locking
  28. *
  29. * kvm->arch.uvmem_lock is a per-guest lock that prevents concurrent
  30. * page-in and page-out requests for the same GPA. Concurrent accesses
  31. * can either come via UV (guest vCPUs requesting for same page)
  32. * or when HV and guest simultaneously access the same page.
  33. * This mutex serializes the migration of page from HV(normal) to
  34. * UV(secure) and vice versa. So the serialization points are around
  35. * migrate_vma routines and page-in/out routines.
  36. *
  37. * Per-guest mutex comes with a cost though. Mainly it serializes the
  38. * fault path as page-out can occur when HV faults on accessing secure
  39. * guest pages. Currently UV issues page-in requests for all the guest
  40. * PFNs one at a time during early boot (UV_ESM uvcall), so this is
  41. * not a cause for concern. Also currently the number of page-outs caused
  42. * by HV touching secure pages is very very low. If an when UV supports
  43. * overcommitting, then we might see concurrent guest driven page-outs.
  44. *
  45. * Locking order
  46. *
  47. * 1. kvm->srcu - Protects KVM memslots
  48. * 2. kvm->mm->mmap_lock - find_vma, migrate_vma_pages and helpers, ksm_madvise
  49. * 3. kvm->arch.uvmem_lock - protects read/writes to uvmem slots thus acting
  50. * as sync-points for page-in/out
  51. */
  52. /*
  53. * Notes on page size
  54. *
  55. * Currently UV uses 2MB mappings internally, but will issue H_SVM_PAGE_IN
  56. * and H_SVM_PAGE_OUT hcalls in PAGE_SIZE(64K) granularity. HV tracks
  57. * secure GPAs at 64K page size and maintains one device PFN for each
  58. * 64K secure GPA. UV_PAGE_IN and UV_PAGE_OUT calls by HV are also issued
  59. * for 64K page at a time.
  60. *
  61. * HV faulting on secure pages: When HV touches any secure page, it
  62. * faults and issues a UV_PAGE_OUT request with 64K page size. Currently
  63. * UV splits and remaps the 2MB page if necessary and copies out the
  64. * required 64K page contents.
  65. *
  66. * Shared pages: Whenever guest shares a secure page, UV will split and
  67. * remap the 2MB page if required and issue H_SVM_PAGE_IN with 64K page size.
  68. *
  69. * HV invalidating a page: When a regular page belonging to secure
  70. * guest gets unmapped, HV informs UV with UV_PAGE_INVAL of 64K
  71. * page size. Using 64K page size is correct here because any non-secure
  72. * page will essentially be of 64K page size. Splitting by UV during sharing
  73. * and page-out ensures this.
  74. *
  75. * Page fault handling: When HV handles page fault of a page belonging
  76. * to secure guest, it sends that to UV with a 64K UV_PAGE_IN request.
  77. * Using 64K size is correct here too as UV would have split the 2MB page
  78. * into 64k mappings and would have done page-outs earlier.
  79. *
  80. * In summary, the current secure pages handling code in HV assumes
  81. * 64K page size and in fact fails any page-in/page-out requests of
  82. * non-64K size upfront. If and when UV starts supporting multiple
  83. * page-sizes, we need to break this assumption.
  84. */
  85. #include <linux/pagemap.h>
  86. #include <linux/migrate.h>
  87. #include <linux/kvm_host.h>
  88. #include <linux/ksm.h>
  89. #include <linux/of.h>
  90. #include <linux/memremap.h>
  91. #include <asm/ultravisor.h>
  92. #include <asm/mman.h>
  93. #include <asm/kvm_ppc.h>
  94. #include <asm/kvm_book3s_uvmem.h>
  95. static struct dev_pagemap kvmppc_uvmem_pgmap;
  96. static unsigned long *kvmppc_uvmem_bitmap;
  97. static DEFINE_SPINLOCK(kvmppc_uvmem_bitmap_lock);
  98. /*
  99. * States of a GFN
  100. * ---------------
  101. * The GFN can be in one of the following states.
  102. *
  103. * (a) Secure - The GFN is secure. The GFN is associated with
  104. * a Secure VM, the contents of the GFN is not accessible
  105. * to the Hypervisor. This GFN can be backed by a secure-PFN,
  106. * or can be backed by a normal-PFN with contents encrypted.
  107. * The former is true when the GFN is paged-in into the
  108. * ultravisor. The latter is true when the GFN is paged-out
  109. * of the ultravisor.
  110. *
  111. * (b) Shared - The GFN is shared. The GFN is associated with a
  112. * a secure VM. The contents of the GFN is accessible to
  113. * Hypervisor. This GFN is backed by a normal-PFN and its
  114. * content is un-encrypted.
  115. *
  116. * (c) Normal - The GFN is a normal. The GFN is associated with
  117. * a normal VM. The contents of the GFN is accessible to
  118. * the Hypervisor. Its content is never encrypted.
  119. *
  120. * States of a VM.
  121. * ---------------
  122. *
  123. * Normal VM: A VM whose contents are always accessible to
  124. * the hypervisor. All its GFNs are normal-GFNs.
  125. *
  126. * Secure VM: A VM whose contents are not accessible to the
  127. * hypervisor without the VM's consent. Its GFNs are
  128. * either Shared-GFN or Secure-GFNs.
  129. *
  130. * Transient VM: A Normal VM that is transitioning to secure VM.
  131. * The transition starts on successful return of
  132. * H_SVM_INIT_START, and ends on successful return
  133. * of H_SVM_INIT_DONE. This transient VM, can have GFNs
  134. * in any of the three states; i.e Secure-GFN, Shared-GFN,
  135. * and Normal-GFN. The VM never executes in this state
  136. * in supervisor-mode.
  137. *
  138. * Memory slot State.
  139. * -----------------------------
  140. * The state of a memory slot mirrors the state of the
  141. * VM the memory slot is associated with.
  142. *
  143. * VM State transition.
  144. * --------------------
  145. *
  146. * A VM always starts in Normal Mode.
  147. *
  148. * H_SVM_INIT_START moves the VM into transient state. During this
  149. * time the Ultravisor may request some of its GFNs to be shared or
  150. * secured. So its GFNs can be in one of the three GFN states.
  151. *
  152. * H_SVM_INIT_DONE moves the VM entirely from transient state to
  153. * secure-state. At this point any left-over normal-GFNs are
  154. * transitioned to Secure-GFN.
  155. *
  156. * H_SVM_INIT_ABORT moves the transient VM back to normal VM.
  157. * All its GFNs are moved to Normal-GFNs.
  158. *
  159. * UV_TERMINATE transitions the secure-VM back to normal-VM. All
  160. * the secure-GFN and shared-GFNs are tranistioned to normal-GFN
  161. * Note: The contents of the normal-GFN is undefined at this point.
  162. *
  163. * GFN state implementation:
  164. * -------------------------
  165. *
  166. * Secure GFN is associated with a secure-PFN; also called uvmem_pfn,
  167. * when the GFN is paged-in. Its pfn[] has KVMPPC_GFN_UVMEM_PFN flag
  168. * set, and contains the value of the secure-PFN.
  169. * It is associated with a normal-PFN; also called mem_pfn, when
  170. * the GFN is pagedout. Its pfn[] has KVMPPC_GFN_MEM_PFN flag set.
  171. * The value of the normal-PFN is not tracked.
  172. *
  173. * Shared GFN is associated with a normal-PFN. Its pfn[] has
  174. * KVMPPC_UVMEM_SHARED_PFN flag set. The value of the normal-PFN
  175. * is not tracked.
  176. *
  177. * Normal GFN is associated with normal-PFN. Its pfn[] has
  178. * no flag set. The value of the normal-PFN is not tracked.
  179. *
  180. * Life cycle of a GFN
  181. * --------------------
  182. *
  183. * --------------------------------------------------------------
  184. * | | Share | Unshare | SVM |H_SVM_INIT_DONE|
  185. * | |operation |operation | abort/ | |
  186. * | | | | terminate | |
  187. * -------------------------------------------------------------
  188. * | | | | | |
  189. * | Secure | Shared | Secure |Normal |Secure |
  190. * | | | | | |
  191. * | Shared | Shared | Secure |Normal |Shared |
  192. * | | | | | |
  193. * | Normal | Shared | Secure |Normal |Secure |
  194. * --------------------------------------------------------------
  195. *
  196. * Life cycle of a VM
  197. * --------------------
  198. *
  199. * --------------------------------------------------------------------
  200. * | | start | H_SVM_ |H_SVM_ |H_SVM_ |UV_SVM_ |
  201. * | | VM |INIT_START|INIT_DONE|INIT_ABORT |TERMINATE |
  202. * | | | | | | |
  203. * --------- ----------------------------------------------------------
  204. * | | | | | | |
  205. * | Normal | Normal | Transient|Error |Error |Normal |
  206. * | | | | | | |
  207. * | Secure | Error | Error |Error |Error |Normal |
  208. * | | | | | | |
  209. * |Transient| N/A | Error |Secure |Normal |Normal |
  210. * --------------------------------------------------------------------
  211. */
  212. #define KVMPPC_GFN_UVMEM_PFN (1UL << 63)
  213. #define KVMPPC_GFN_MEM_PFN (1UL << 62)
  214. #define KVMPPC_GFN_SHARED (1UL << 61)
  215. #define KVMPPC_GFN_SECURE (KVMPPC_GFN_UVMEM_PFN | KVMPPC_GFN_MEM_PFN)
  216. #define KVMPPC_GFN_FLAG_MASK (KVMPPC_GFN_SECURE | KVMPPC_GFN_SHARED)
  217. #define KVMPPC_GFN_PFN_MASK (~KVMPPC_GFN_FLAG_MASK)
  218. struct kvmppc_uvmem_slot {
  219. struct list_head list;
  220. unsigned long nr_pfns;
  221. unsigned long base_pfn;
  222. unsigned long *pfns;
  223. };
  224. struct kvmppc_uvmem_page_pvt {
  225. struct kvm *kvm;
  226. unsigned long gpa;
  227. bool skip_page_out;
  228. bool remove_gfn;
  229. };
  230. bool kvmppc_uvmem_available(void)
  231. {
  232. /*
  233. * If kvmppc_uvmem_bitmap != NULL, then there is an ultravisor
  234. * and our data structures have been initialized successfully.
  235. */
  236. return !!kvmppc_uvmem_bitmap;
  237. }
  238. int kvmppc_uvmem_slot_init(struct kvm *kvm, const struct kvm_memory_slot *slot)
  239. {
  240. struct kvmppc_uvmem_slot *p;
  241. p = kzalloc(sizeof(*p), GFP_KERNEL);
  242. if (!p)
  243. return -ENOMEM;
  244. p->pfns = vcalloc(slot->npages, sizeof(*p->pfns));
  245. if (!p->pfns) {
  246. kfree(p);
  247. return -ENOMEM;
  248. }
  249. p->nr_pfns = slot->npages;
  250. p->base_pfn = slot->base_gfn;
  251. mutex_lock(&kvm->arch.uvmem_lock);
  252. list_add(&p->list, &kvm->arch.uvmem_pfns);
  253. mutex_unlock(&kvm->arch.uvmem_lock);
  254. return 0;
  255. }
  256. /*
  257. * All device PFNs are already released by the time we come here.
  258. */
  259. void kvmppc_uvmem_slot_free(struct kvm *kvm, const struct kvm_memory_slot *slot)
  260. {
  261. struct kvmppc_uvmem_slot *p, *next;
  262. mutex_lock(&kvm->arch.uvmem_lock);
  263. list_for_each_entry_safe(p, next, &kvm->arch.uvmem_pfns, list) {
  264. if (p->base_pfn == slot->base_gfn) {
  265. vfree(p->pfns);
  266. list_del(&p->list);
  267. kfree(p);
  268. break;
  269. }
  270. }
  271. mutex_unlock(&kvm->arch.uvmem_lock);
  272. }
  273. static void kvmppc_mark_gfn(unsigned long gfn, struct kvm *kvm,
  274. unsigned long flag, unsigned long uvmem_pfn)
  275. {
  276. struct kvmppc_uvmem_slot *p;
  277. list_for_each_entry(p, &kvm->arch.uvmem_pfns, list) {
  278. if (gfn >= p->base_pfn && gfn < p->base_pfn + p->nr_pfns) {
  279. unsigned long index = gfn - p->base_pfn;
  280. if (flag == KVMPPC_GFN_UVMEM_PFN)
  281. p->pfns[index] = uvmem_pfn | flag;
  282. else
  283. p->pfns[index] = flag;
  284. return;
  285. }
  286. }
  287. }
  288. /* mark the GFN as secure-GFN associated with @uvmem pfn device-PFN. */
  289. static void kvmppc_gfn_secure_uvmem_pfn(unsigned long gfn,
  290. unsigned long uvmem_pfn, struct kvm *kvm)
  291. {
  292. kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_UVMEM_PFN, uvmem_pfn);
  293. }
  294. /* mark the GFN as secure-GFN associated with a memory-PFN. */
  295. static void kvmppc_gfn_secure_mem_pfn(unsigned long gfn, struct kvm *kvm)
  296. {
  297. kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_MEM_PFN, 0);
  298. }
  299. /* mark the GFN as a shared GFN. */
  300. static void kvmppc_gfn_shared(unsigned long gfn, struct kvm *kvm)
  301. {
  302. kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_SHARED, 0);
  303. }
  304. /* mark the GFN as a non-existent GFN. */
  305. static void kvmppc_gfn_remove(unsigned long gfn, struct kvm *kvm)
  306. {
  307. kvmppc_mark_gfn(gfn, kvm, 0, 0);
  308. }
  309. /* return true, if the GFN is a secure-GFN backed by a secure-PFN */
  310. static bool kvmppc_gfn_is_uvmem_pfn(unsigned long gfn, struct kvm *kvm,
  311. unsigned long *uvmem_pfn)
  312. {
  313. struct kvmppc_uvmem_slot *p;
  314. list_for_each_entry(p, &kvm->arch.uvmem_pfns, list) {
  315. if (gfn >= p->base_pfn && gfn < p->base_pfn + p->nr_pfns) {
  316. unsigned long index = gfn - p->base_pfn;
  317. if (p->pfns[index] & KVMPPC_GFN_UVMEM_PFN) {
  318. if (uvmem_pfn)
  319. *uvmem_pfn = p->pfns[index] &
  320. KVMPPC_GFN_PFN_MASK;
  321. return true;
  322. } else
  323. return false;
  324. }
  325. }
  326. return false;
  327. }
  328. /*
  329. * starting from *gfn search for the next available GFN that is not yet
  330. * transitioned to a secure GFN. return the value of that GFN in *gfn. If a
  331. * GFN is found, return true, else return false
  332. *
  333. * Must be called with kvm->arch.uvmem_lock held.
  334. */
  335. static bool kvmppc_next_nontransitioned_gfn(const struct kvm_memory_slot *memslot,
  336. struct kvm *kvm, unsigned long *gfn)
  337. {
  338. struct kvmppc_uvmem_slot *p = NULL, *iter;
  339. bool ret = false;
  340. unsigned long i;
  341. list_for_each_entry(iter, &kvm->arch.uvmem_pfns, list)
  342. if (*gfn >= iter->base_pfn && *gfn < iter->base_pfn + iter->nr_pfns) {
  343. p = iter;
  344. break;
  345. }
  346. if (!p)
  347. return ret;
  348. /*
  349. * The code below assumes, one to one correspondence between
  350. * kvmppc_uvmem_slot and memslot.
  351. */
  352. for (i = *gfn; i < p->base_pfn + p->nr_pfns; i++) {
  353. unsigned long index = i - p->base_pfn;
  354. if (!(p->pfns[index] & KVMPPC_GFN_FLAG_MASK)) {
  355. *gfn = i;
  356. ret = true;
  357. break;
  358. }
  359. }
  360. return ret;
  361. }
  362. static int kvmppc_memslot_page_merge(struct kvm *kvm,
  363. const struct kvm_memory_slot *memslot, bool merge)
  364. {
  365. unsigned long gfn = memslot->base_gfn;
  366. unsigned long end, start = gfn_to_hva(kvm, gfn);
  367. unsigned long vm_flags;
  368. int ret = 0;
  369. struct vm_area_struct *vma;
  370. int merge_flag = (merge) ? MADV_MERGEABLE : MADV_UNMERGEABLE;
  371. if (kvm_is_error_hva(start))
  372. return H_STATE;
  373. end = start + (memslot->npages << PAGE_SHIFT);
  374. mmap_write_lock(kvm->mm);
  375. do {
  376. vma = find_vma_intersection(kvm->mm, start, end);
  377. if (!vma) {
  378. ret = H_STATE;
  379. break;
  380. }
  381. vma_start_write(vma);
  382. /* Copy vm_flags to avoid partial modifications in ksm_madvise */
  383. vm_flags = vma->vm_flags;
  384. ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
  385. merge_flag, &vm_flags);
  386. if (ret) {
  387. ret = H_STATE;
  388. break;
  389. }
  390. vm_flags_reset(vma, vm_flags);
  391. start = vma->vm_end;
  392. } while (end > vma->vm_end);
  393. mmap_write_unlock(kvm->mm);
  394. return ret;
  395. }
  396. static void __kvmppc_uvmem_memslot_delete(struct kvm *kvm,
  397. const struct kvm_memory_slot *memslot)
  398. {
  399. uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
  400. kvmppc_uvmem_slot_free(kvm, memslot);
  401. kvmppc_memslot_page_merge(kvm, memslot, true);
  402. }
  403. static int __kvmppc_uvmem_memslot_create(struct kvm *kvm,
  404. const struct kvm_memory_slot *memslot)
  405. {
  406. int ret = H_PARAMETER;
  407. if (kvmppc_memslot_page_merge(kvm, memslot, false))
  408. return ret;
  409. if (kvmppc_uvmem_slot_init(kvm, memslot))
  410. goto out1;
  411. ret = uv_register_mem_slot(kvm->arch.lpid,
  412. memslot->base_gfn << PAGE_SHIFT,
  413. memslot->npages * PAGE_SIZE,
  414. 0, memslot->id);
  415. if (ret < 0) {
  416. ret = H_PARAMETER;
  417. goto out;
  418. }
  419. return 0;
  420. out:
  421. kvmppc_uvmem_slot_free(kvm, memslot);
  422. out1:
  423. kvmppc_memslot_page_merge(kvm, memslot, true);
  424. return ret;
  425. }
  426. unsigned long kvmppc_h_svm_init_start(struct kvm *kvm)
  427. {
  428. struct kvm_memslots *slots;
  429. struct kvm_memory_slot *memslot, *m;
  430. int ret = H_SUCCESS;
  431. int srcu_idx, bkt;
  432. kvm->arch.secure_guest = KVMPPC_SECURE_INIT_START;
  433. if (!kvmppc_uvmem_bitmap)
  434. return H_UNSUPPORTED;
  435. /* Only radix guests can be secure guests */
  436. if (!kvm_is_radix(kvm))
  437. return H_UNSUPPORTED;
  438. /* NAK the transition to secure if not enabled */
  439. if (!kvm->arch.svm_enabled)
  440. return H_AUTHORITY;
  441. srcu_idx = srcu_read_lock(&kvm->srcu);
  442. /* register the memslot */
  443. slots = kvm_memslots(kvm);
  444. kvm_for_each_memslot(memslot, bkt, slots) {
  445. ret = __kvmppc_uvmem_memslot_create(kvm, memslot);
  446. if (ret)
  447. break;
  448. }
  449. if (ret) {
  450. slots = kvm_memslots(kvm);
  451. kvm_for_each_memslot(m, bkt, slots) {
  452. if (m == memslot)
  453. break;
  454. __kvmppc_uvmem_memslot_delete(kvm, memslot);
  455. }
  456. }
  457. srcu_read_unlock(&kvm->srcu, srcu_idx);
  458. return ret;
  459. }
  460. /*
  461. * Provision a new page on HV side and copy over the contents
  462. * from secure memory using UV_PAGE_OUT uvcall.
  463. * Caller must held kvm->arch.uvmem_lock.
  464. */
  465. static int __kvmppc_svm_page_out(struct vm_area_struct *vma,
  466. unsigned long start,
  467. unsigned long end, unsigned long page_shift,
  468. struct kvm *kvm, unsigned long gpa, struct page *fault_page)
  469. {
  470. unsigned long src_pfn, dst_pfn = 0;
  471. struct migrate_vma mig = { 0 };
  472. struct page *dpage, *spage;
  473. struct kvmppc_uvmem_page_pvt *pvt;
  474. unsigned long pfn;
  475. int ret = U_SUCCESS;
  476. memset(&mig, 0, sizeof(mig));
  477. mig.vma = vma;
  478. mig.start = start;
  479. mig.end = end;
  480. mig.src = &src_pfn;
  481. mig.dst = &dst_pfn;
  482. mig.pgmap_owner = &kvmppc_uvmem_pgmap;
  483. mig.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
  484. mig.fault_page = fault_page;
  485. /* The requested page is already paged-out, nothing to do */
  486. if (!kvmppc_gfn_is_uvmem_pfn(gpa >> page_shift, kvm, NULL))
  487. return ret;
  488. ret = migrate_vma_setup(&mig);
  489. if (ret)
  490. return -1;
  491. spage = migrate_pfn_to_page(*mig.src);
  492. if (!spage || !(*mig.src & MIGRATE_PFN_MIGRATE))
  493. goto out_finalize;
  494. if (!is_zone_device_page(spage))
  495. goto out_finalize;
  496. dpage = alloc_page_vma(GFP_HIGHUSER, vma, start);
  497. if (!dpage) {
  498. ret = -1;
  499. goto out_finalize;
  500. }
  501. lock_page(dpage);
  502. pvt = spage->zone_device_data;
  503. pfn = page_to_pfn(dpage);
  504. /*
  505. * This function is used in two cases:
  506. * - When HV touches a secure page, for which we do UV_PAGE_OUT
  507. * - When a secure page is converted to shared page, we *get*
  508. * the page to essentially unmap the device page. In this
  509. * case we skip page-out.
  510. */
  511. if (!pvt->skip_page_out)
  512. ret = uv_page_out(kvm->arch.lpid, pfn << page_shift,
  513. gpa, 0, page_shift);
  514. if (ret == U_SUCCESS)
  515. *mig.dst = migrate_pfn(pfn);
  516. else {
  517. unlock_page(dpage);
  518. __free_page(dpage);
  519. goto out_finalize;
  520. }
  521. migrate_vma_pages(&mig);
  522. out_finalize:
  523. migrate_vma_finalize(&mig);
  524. return ret;
  525. }
  526. static inline int kvmppc_svm_page_out(struct vm_area_struct *vma,
  527. unsigned long start, unsigned long end,
  528. unsigned long page_shift,
  529. struct kvm *kvm, unsigned long gpa,
  530. struct page *fault_page)
  531. {
  532. int ret;
  533. mutex_lock(&kvm->arch.uvmem_lock);
  534. ret = __kvmppc_svm_page_out(vma, start, end, page_shift, kvm, gpa,
  535. fault_page);
  536. mutex_unlock(&kvm->arch.uvmem_lock);
  537. return ret;
  538. }
  539. /*
  540. * Drop device pages that we maintain for the secure guest
  541. *
  542. * We first mark the pages to be skipped from UV_PAGE_OUT when there
  543. * is HV side fault on these pages. Next we *get* these pages, forcing
  544. * fault on them, do fault time migration to replace the device PTEs in
  545. * QEMU page table with normal PTEs from newly allocated pages.
  546. */
  547. void kvmppc_uvmem_drop_pages(const struct kvm_memory_slot *slot,
  548. struct kvm *kvm, bool skip_page_out)
  549. {
  550. int i;
  551. struct kvmppc_uvmem_page_pvt *pvt;
  552. struct page *uvmem_page;
  553. struct vm_area_struct *vma = NULL;
  554. unsigned long uvmem_pfn, gfn;
  555. unsigned long addr;
  556. mmap_read_lock(kvm->mm);
  557. addr = slot->userspace_addr;
  558. gfn = slot->base_gfn;
  559. for (i = slot->npages; i; --i, ++gfn, addr += PAGE_SIZE) {
  560. /* Fetch the VMA if addr is not in the latest fetched one */
  561. if (!vma || addr >= vma->vm_end) {
  562. vma = vma_lookup(kvm->mm, addr);
  563. if (!vma) {
  564. pr_err("Can't find VMA for gfn:0x%lx\n", gfn);
  565. break;
  566. }
  567. }
  568. mutex_lock(&kvm->arch.uvmem_lock);
  569. if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
  570. uvmem_page = pfn_to_page(uvmem_pfn);
  571. pvt = uvmem_page->zone_device_data;
  572. pvt->skip_page_out = skip_page_out;
  573. pvt->remove_gfn = true;
  574. if (__kvmppc_svm_page_out(vma, addr, addr + PAGE_SIZE,
  575. PAGE_SHIFT, kvm, pvt->gpa, NULL))
  576. pr_err("Can't page out gpa:0x%lx addr:0x%lx\n",
  577. pvt->gpa, addr);
  578. } else {
  579. /* Remove the shared flag if any */
  580. kvmppc_gfn_remove(gfn, kvm);
  581. }
  582. mutex_unlock(&kvm->arch.uvmem_lock);
  583. }
  584. mmap_read_unlock(kvm->mm);
  585. }
  586. unsigned long kvmppc_h_svm_init_abort(struct kvm *kvm)
  587. {
  588. int srcu_idx, bkt;
  589. struct kvm_memory_slot *memslot;
  590. /*
  591. * Expect to be called only after INIT_START and before INIT_DONE.
  592. * If INIT_DONE was completed, use normal VM termination sequence.
  593. */
  594. if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
  595. return H_UNSUPPORTED;
  596. if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
  597. return H_STATE;
  598. srcu_idx = srcu_read_lock(&kvm->srcu);
  599. kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
  600. kvmppc_uvmem_drop_pages(memslot, kvm, false);
  601. srcu_read_unlock(&kvm->srcu, srcu_idx);
  602. kvm->arch.secure_guest = 0;
  603. uv_svm_terminate(kvm->arch.lpid);
  604. return H_PARAMETER;
  605. }
  606. /*
  607. * Get a free device PFN from the pool
  608. *
  609. * Called when a normal page is moved to secure memory (UV_PAGE_IN). Device
  610. * PFN will be used to keep track of the secure page on HV side.
  611. *
  612. * Called with kvm->arch.uvmem_lock held
  613. */
  614. static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm)
  615. {
  616. struct page *dpage = NULL;
  617. unsigned long bit, uvmem_pfn;
  618. struct kvmppc_uvmem_page_pvt *pvt;
  619. unsigned long pfn_last, pfn_first;
  620. pfn_first = kvmppc_uvmem_pgmap.range.start >> PAGE_SHIFT;
  621. pfn_last = pfn_first +
  622. (range_len(&kvmppc_uvmem_pgmap.range) >> PAGE_SHIFT);
  623. spin_lock(&kvmppc_uvmem_bitmap_lock);
  624. bit = find_first_zero_bit(kvmppc_uvmem_bitmap,
  625. pfn_last - pfn_first);
  626. if (bit >= (pfn_last - pfn_first))
  627. goto out;
  628. bitmap_set(kvmppc_uvmem_bitmap, bit, 1);
  629. spin_unlock(&kvmppc_uvmem_bitmap_lock);
  630. pvt = kzalloc(sizeof(*pvt), GFP_KERNEL);
  631. if (!pvt)
  632. goto out_clear;
  633. uvmem_pfn = bit + pfn_first;
  634. kvmppc_gfn_secure_uvmem_pfn(gpa >> PAGE_SHIFT, uvmem_pfn, kvm);
  635. pvt->gpa = gpa;
  636. pvt->kvm = kvm;
  637. dpage = pfn_to_page(uvmem_pfn);
  638. dpage->zone_device_data = pvt;
  639. zone_device_page_init(dpage);
  640. return dpage;
  641. out_clear:
  642. spin_lock(&kvmppc_uvmem_bitmap_lock);
  643. bitmap_clear(kvmppc_uvmem_bitmap, bit, 1);
  644. out:
  645. spin_unlock(&kvmppc_uvmem_bitmap_lock);
  646. return NULL;
  647. }
  648. /*
  649. * Alloc a PFN from private device memory pool. If @pagein is true,
  650. * copy page from normal memory to secure memory using UV_PAGE_IN uvcall.
  651. */
  652. static int kvmppc_svm_page_in(struct vm_area_struct *vma,
  653. unsigned long start,
  654. unsigned long end, unsigned long gpa, struct kvm *kvm,
  655. unsigned long page_shift,
  656. bool pagein)
  657. {
  658. unsigned long src_pfn, dst_pfn = 0;
  659. struct migrate_vma mig = { 0 };
  660. struct page *spage;
  661. unsigned long pfn;
  662. struct page *dpage;
  663. int ret = 0;
  664. memset(&mig, 0, sizeof(mig));
  665. mig.vma = vma;
  666. mig.start = start;
  667. mig.end = end;
  668. mig.src = &src_pfn;
  669. mig.dst = &dst_pfn;
  670. mig.flags = MIGRATE_VMA_SELECT_SYSTEM;
  671. ret = migrate_vma_setup(&mig);
  672. if (ret)
  673. return ret;
  674. if (!(*mig.src & MIGRATE_PFN_MIGRATE)) {
  675. ret = -1;
  676. goto out_finalize;
  677. }
  678. dpage = kvmppc_uvmem_get_page(gpa, kvm);
  679. if (!dpage) {
  680. ret = -1;
  681. goto out_finalize;
  682. }
  683. if (pagein) {
  684. pfn = *mig.src >> MIGRATE_PFN_SHIFT;
  685. spage = migrate_pfn_to_page(*mig.src);
  686. if (spage) {
  687. ret = uv_page_in(kvm->arch.lpid, pfn << page_shift,
  688. gpa, 0, page_shift);
  689. if (ret)
  690. goto out_finalize;
  691. }
  692. }
  693. *mig.dst = migrate_pfn(page_to_pfn(dpage));
  694. migrate_vma_pages(&mig);
  695. out_finalize:
  696. migrate_vma_finalize(&mig);
  697. return ret;
  698. }
  699. static int kvmppc_uv_migrate_mem_slot(struct kvm *kvm,
  700. const struct kvm_memory_slot *memslot)
  701. {
  702. unsigned long gfn = memslot->base_gfn;
  703. struct vm_area_struct *vma;
  704. unsigned long start, end;
  705. int ret = 0;
  706. mmap_read_lock(kvm->mm);
  707. mutex_lock(&kvm->arch.uvmem_lock);
  708. while (kvmppc_next_nontransitioned_gfn(memslot, kvm, &gfn)) {
  709. ret = H_STATE;
  710. start = gfn_to_hva(kvm, gfn);
  711. if (kvm_is_error_hva(start))
  712. break;
  713. end = start + (1UL << PAGE_SHIFT);
  714. vma = find_vma_intersection(kvm->mm, start, end);
  715. if (!vma || vma->vm_start > start || vma->vm_end < end)
  716. break;
  717. ret = kvmppc_svm_page_in(vma, start, end,
  718. (gfn << PAGE_SHIFT), kvm, PAGE_SHIFT, false);
  719. if (ret) {
  720. ret = H_STATE;
  721. break;
  722. }
  723. /* relinquish the cpu if needed */
  724. cond_resched();
  725. }
  726. mutex_unlock(&kvm->arch.uvmem_lock);
  727. mmap_read_unlock(kvm->mm);
  728. return ret;
  729. }
  730. unsigned long kvmppc_h_svm_init_done(struct kvm *kvm)
  731. {
  732. struct kvm_memslots *slots;
  733. struct kvm_memory_slot *memslot;
  734. int srcu_idx, bkt;
  735. long ret = H_SUCCESS;
  736. if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
  737. return H_UNSUPPORTED;
  738. /* migrate any unmoved normal pfn to device pfns*/
  739. srcu_idx = srcu_read_lock(&kvm->srcu);
  740. slots = kvm_memslots(kvm);
  741. kvm_for_each_memslot(memslot, bkt, slots) {
  742. ret = kvmppc_uv_migrate_mem_slot(kvm, memslot);
  743. if (ret) {
  744. /*
  745. * The pages will remain transitioned.
  746. * Its the callers responsibility to
  747. * terminate the VM, which will undo
  748. * all state of the VM. Till then
  749. * this VM is in a erroneous state.
  750. * Its KVMPPC_SECURE_INIT_DONE will
  751. * remain unset.
  752. */
  753. ret = H_STATE;
  754. goto out;
  755. }
  756. }
  757. kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_DONE;
  758. pr_info("LPID %d went secure\n", kvm->arch.lpid);
  759. out:
  760. srcu_read_unlock(&kvm->srcu, srcu_idx);
  761. return ret;
  762. }
  763. /*
  764. * Shares the page with HV, thus making it a normal page.
  765. *
  766. * - If the page is already secure, then provision a new page and share
  767. * - If the page is a normal page, share the existing page
  768. *
  769. * In the former case, uses dev_pagemap_ops.migrate_to_ram handler
  770. * to unmap the device page from QEMU's page tables.
  771. */
  772. static unsigned long kvmppc_share_page(struct kvm *kvm, unsigned long gpa,
  773. unsigned long page_shift)
  774. {
  775. int ret = H_PARAMETER;
  776. struct page *uvmem_page;
  777. struct kvmppc_uvmem_page_pvt *pvt;
  778. unsigned long pfn;
  779. unsigned long gfn = gpa >> page_shift;
  780. int srcu_idx;
  781. unsigned long uvmem_pfn;
  782. srcu_idx = srcu_read_lock(&kvm->srcu);
  783. mutex_lock(&kvm->arch.uvmem_lock);
  784. if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
  785. uvmem_page = pfn_to_page(uvmem_pfn);
  786. pvt = uvmem_page->zone_device_data;
  787. pvt->skip_page_out = true;
  788. /*
  789. * do not drop the GFN. It is a valid GFN
  790. * that is transitioned to a shared GFN.
  791. */
  792. pvt->remove_gfn = false;
  793. }
  794. retry:
  795. mutex_unlock(&kvm->arch.uvmem_lock);
  796. pfn = gfn_to_pfn(kvm, gfn);
  797. if (is_error_noslot_pfn(pfn))
  798. goto out;
  799. mutex_lock(&kvm->arch.uvmem_lock);
  800. if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
  801. uvmem_page = pfn_to_page(uvmem_pfn);
  802. pvt = uvmem_page->zone_device_data;
  803. pvt->skip_page_out = true;
  804. pvt->remove_gfn = false; /* it continues to be a valid GFN */
  805. kvm_release_pfn_clean(pfn);
  806. goto retry;
  807. }
  808. if (!uv_page_in(kvm->arch.lpid, pfn << page_shift, gpa, 0,
  809. page_shift)) {
  810. kvmppc_gfn_shared(gfn, kvm);
  811. ret = H_SUCCESS;
  812. }
  813. kvm_release_pfn_clean(pfn);
  814. mutex_unlock(&kvm->arch.uvmem_lock);
  815. out:
  816. srcu_read_unlock(&kvm->srcu, srcu_idx);
  817. return ret;
  818. }
  819. /*
  820. * H_SVM_PAGE_IN: Move page from normal memory to secure memory.
  821. *
  822. * H_PAGE_IN_SHARED flag makes the page shared which means that the same
  823. * memory in is visible from both UV and HV.
  824. */
  825. unsigned long kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa,
  826. unsigned long flags,
  827. unsigned long page_shift)
  828. {
  829. unsigned long start, end;
  830. struct vm_area_struct *vma;
  831. int srcu_idx;
  832. unsigned long gfn = gpa >> page_shift;
  833. int ret;
  834. if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
  835. return H_UNSUPPORTED;
  836. if (page_shift != PAGE_SHIFT)
  837. return H_P3;
  838. if (flags & ~H_PAGE_IN_SHARED)
  839. return H_P2;
  840. if (flags & H_PAGE_IN_SHARED)
  841. return kvmppc_share_page(kvm, gpa, page_shift);
  842. ret = H_PARAMETER;
  843. srcu_idx = srcu_read_lock(&kvm->srcu);
  844. mmap_read_lock(kvm->mm);
  845. start = gfn_to_hva(kvm, gfn);
  846. if (kvm_is_error_hva(start))
  847. goto out;
  848. mutex_lock(&kvm->arch.uvmem_lock);
  849. /* Fail the page-in request of an already paged-in page */
  850. if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL))
  851. goto out_unlock;
  852. end = start + (1UL << page_shift);
  853. vma = find_vma_intersection(kvm->mm, start, end);
  854. if (!vma || vma->vm_start > start || vma->vm_end < end)
  855. goto out_unlock;
  856. if (kvmppc_svm_page_in(vma, start, end, gpa, kvm, page_shift,
  857. true))
  858. goto out_unlock;
  859. ret = H_SUCCESS;
  860. out_unlock:
  861. mutex_unlock(&kvm->arch.uvmem_lock);
  862. out:
  863. mmap_read_unlock(kvm->mm);
  864. srcu_read_unlock(&kvm->srcu, srcu_idx);
  865. return ret;
  866. }
  867. /*
  868. * Fault handler callback that gets called when HV touches any page that
  869. * has been moved to secure memory, we ask UV to give back the page by
  870. * issuing UV_PAGE_OUT uvcall.
  871. *
  872. * This eventually results in dropping of device PFN and the newly
  873. * provisioned page/PFN gets populated in QEMU page tables.
  874. */
  875. static vm_fault_t kvmppc_uvmem_migrate_to_ram(struct vm_fault *vmf)
  876. {
  877. struct kvmppc_uvmem_page_pvt *pvt = vmf->page->zone_device_data;
  878. if (kvmppc_svm_page_out(vmf->vma, vmf->address,
  879. vmf->address + PAGE_SIZE, PAGE_SHIFT,
  880. pvt->kvm, pvt->gpa, vmf->page))
  881. return VM_FAULT_SIGBUS;
  882. else
  883. return 0;
  884. }
  885. /*
  886. * Release the device PFN back to the pool
  887. *
  888. * Gets called when secure GFN tranistions from a secure-PFN
  889. * to a normal PFN during H_SVM_PAGE_OUT.
  890. * Gets called with kvm->arch.uvmem_lock held.
  891. */
  892. static void kvmppc_uvmem_page_free(struct page *page)
  893. {
  894. unsigned long pfn = page_to_pfn(page) -
  895. (kvmppc_uvmem_pgmap.range.start >> PAGE_SHIFT);
  896. struct kvmppc_uvmem_page_pvt *pvt;
  897. spin_lock(&kvmppc_uvmem_bitmap_lock);
  898. bitmap_clear(kvmppc_uvmem_bitmap, pfn, 1);
  899. spin_unlock(&kvmppc_uvmem_bitmap_lock);
  900. pvt = page->zone_device_data;
  901. page->zone_device_data = NULL;
  902. if (pvt->remove_gfn)
  903. kvmppc_gfn_remove(pvt->gpa >> PAGE_SHIFT, pvt->kvm);
  904. else
  905. kvmppc_gfn_secure_mem_pfn(pvt->gpa >> PAGE_SHIFT, pvt->kvm);
  906. kfree(pvt);
  907. }
  908. static const struct dev_pagemap_ops kvmppc_uvmem_ops = {
  909. .page_free = kvmppc_uvmem_page_free,
  910. .migrate_to_ram = kvmppc_uvmem_migrate_to_ram,
  911. };
  912. /*
  913. * H_SVM_PAGE_OUT: Move page from secure memory to normal memory.
  914. */
  915. unsigned long
  916. kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa,
  917. unsigned long flags, unsigned long page_shift)
  918. {
  919. unsigned long gfn = gpa >> page_shift;
  920. unsigned long start, end;
  921. struct vm_area_struct *vma;
  922. int srcu_idx;
  923. int ret;
  924. if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
  925. return H_UNSUPPORTED;
  926. if (page_shift != PAGE_SHIFT)
  927. return H_P3;
  928. if (flags)
  929. return H_P2;
  930. ret = H_PARAMETER;
  931. srcu_idx = srcu_read_lock(&kvm->srcu);
  932. mmap_read_lock(kvm->mm);
  933. start = gfn_to_hva(kvm, gfn);
  934. if (kvm_is_error_hva(start))
  935. goto out;
  936. end = start + (1UL << page_shift);
  937. vma = find_vma_intersection(kvm->mm, start, end);
  938. if (!vma || vma->vm_start > start || vma->vm_end < end)
  939. goto out;
  940. if (!kvmppc_svm_page_out(vma, start, end, page_shift, kvm, gpa, NULL))
  941. ret = H_SUCCESS;
  942. out:
  943. mmap_read_unlock(kvm->mm);
  944. srcu_read_unlock(&kvm->srcu, srcu_idx);
  945. return ret;
  946. }
  947. int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gfn)
  948. {
  949. unsigned long pfn;
  950. int ret = U_SUCCESS;
  951. pfn = gfn_to_pfn(kvm, gfn);
  952. if (is_error_noslot_pfn(pfn))
  953. return -EFAULT;
  954. mutex_lock(&kvm->arch.uvmem_lock);
  955. if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL))
  956. goto out;
  957. ret = uv_page_in(kvm->arch.lpid, pfn << PAGE_SHIFT, gfn << PAGE_SHIFT,
  958. 0, PAGE_SHIFT);
  959. out:
  960. kvm_release_pfn_clean(pfn);
  961. mutex_unlock(&kvm->arch.uvmem_lock);
  962. return (ret == U_SUCCESS) ? RESUME_GUEST : -EFAULT;
  963. }
  964. int kvmppc_uvmem_memslot_create(struct kvm *kvm, const struct kvm_memory_slot *new)
  965. {
  966. int ret = __kvmppc_uvmem_memslot_create(kvm, new);
  967. if (!ret)
  968. ret = kvmppc_uv_migrate_mem_slot(kvm, new);
  969. return ret;
  970. }
  971. void kvmppc_uvmem_memslot_delete(struct kvm *kvm, const struct kvm_memory_slot *old)
  972. {
  973. __kvmppc_uvmem_memslot_delete(kvm, old);
  974. }
  975. static u64 kvmppc_get_secmem_size(void)
  976. {
  977. struct device_node *np;
  978. int i, len;
  979. const __be32 *prop;
  980. u64 size = 0;
  981. /*
  982. * First try the new ibm,secure-memory nodes which supersede the
  983. * secure-memory-ranges property.
  984. * If we found some, no need to read the deprecated ones.
  985. */
  986. for_each_compatible_node(np, NULL, "ibm,secure-memory") {
  987. prop = of_get_property(np, "reg", &len);
  988. if (!prop)
  989. continue;
  990. size += of_read_number(prop + 2, 2);
  991. }
  992. if (size)
  993. return size;
  994. np = of_find_compatible_node(NULL, NULL, "ibm,uv-firmware");
  995. if (!np)
  996. goto out;
  997. prop = of_get_property(np, "secure-memory-ranges", &len);
  998. if (!prop)
  999. goto out_put;
  1000. for (i = 0; i < len / (sizeof(*prop) * 4); i++)
  1001. size += of_read_number(prop + (i * 4) + 2, 2);
  1002. out_put:
  1003. of_node_put(np);
  1004. out:
  1005. return size;
  1006. }
  1007. int kvmppc_uvmem_init(void)
  1008. {
  1009. int ret = 0;
  1010. unsigned long size;
  1011. struct resource *res;
  1012. void *addr;
  1013. unsigned long pfn_last, pfn_first;
  1014. size = kvmppc_get_secmem_size();
  1015. if (!size) {
  1016. /*
  1017. * Don't fail the initialization of kvm-hv module if
  1018. * the platform doesn't export ibm,uv-firmware node.
  1019. * Let normal guests run on such PEF-disabled platform.
  1020. */
  1021. pr_info("KVMPPC-UVMEM: No support for secure guests\n");
  1022. goto out;
  1023. }
  1024. res = request_free_mem_region(&iomem_resource, size, "kvmppc_uvmem");
  1025. if (IS_ERR(res)) {
  1026. ret = PTR_ERR(res);
  1027. goto out;
  1028. }
  1029. kvmppc_uvmem_pgmap.type = MEMORY_DEVICE_PRIVATE;
  1030. kvmppc_uvmem_pgmap.range.start = res->start;
  1031. kvmppc_uvmem_pgmap.range.end = res->end;
  1032. kvmppc_uvmem_pgmap.nr_range = 1;
  1033. kvmppc_uvmem_pgmap.ops = &kvmppc_uvmem_ops;
  1034. /* just one global instance: */
  1035. kvmppc_uvmem_pgmap.owner = &kvmppc_uvmem_pgmap;
  1036. addr = memremap_pages(&kvmppc_uvmem_pgmap, NUMA_NO_NODE);
  1037. if (IS_ERR(addr)) {
  1038. ret = PTR_ERR(addr);
  1039. goto out_free_region;
  1040. }
  1041. pfn_first = res->start >> PAGE_SHIFT;
  1042. pfn_last = pfn_first + (resource_size(res) >> PAGE_SHIFT);
  1043. kvmppc_uvmem_bitmap = kcalloc(BITS_TO_LONGS(pfn_last - pfn_first),
  1044. sizeof(unsigned long), GFP_KERNEL);
  1045. if (!kvmppc_uvmem_bitmap) {
  1046. ret = -ENOMEM;
  1047. goto out_unmap;
  1048. }
  1049. pr_info("KVMPPC-UVMEM: Secure Memory size 0x%lx\n", size);
  1050. return ret;
  1051. out_unmap:
  1052. memunmap_pages(&kvmppc_uvmem_pgmap);
  1053. out_free_region:
  1054. release_mem_region(res->start, size);
  1055. out:
  1056. return ret;
  1057. }
  1058. void kvmppc_uvmem_free(void)
  1059. {
  1060. if (!kvmppc_uvmem_bitmap)
  1061. return;
  1062. memunmap_pages(&kvmppc_uvmem_pgmap);
  1063. release_mem_region(kvmppc_uvmem_pgmap.range.start,
  1064. range_len(&kvmppc_uvmem_pgmap.range));
  1065. kfree(kvmppc_uvmem_bitmap);
  1066. }