1ca9f4942SBharata B Rao // SPDX-License-Identifier: GPL-2.0
2ca9f4942SBharata B Rao /*
3ca9f4942SBharata B Rao * Secure pages management: Migration of pages between normal and secure
4ca9f4942SBharata B Rao * memory of KVM guests.
5ca9f4942SBharata B Rao *
6ca9f4942SBharata B Rao * Copyright 2018 Bharata B Rao, IBM Corp. <bharata@linux.ibm.com>
7ca9f4942SBharata B Rao */
8ca9f4942SBharata B Rao
9ca9f4942SBharata B Rao /*
10ca9f4942SBharata B Rao * A pseries guest can be run as secure guest on Ultravisor-enabled
11ca9f4942SBharata B Rao * POWER platforms. On such platforms, this driver will be used to manage
12ca9f4942SBharata B Rao * the movement of guest pages between the normal memory managed by
13ca9f4942SBharata B Rao * hypervisor (HV) and secure memory managed by Ultravisor (UV).
14ca9f4942SBharata B Rao *
15ca9f4942SBharata B Rao * The page-in or page-out requests from UV will come to HV as hcalls and
16ca9f4942SBharata B Rao * HV will call back into UV via ultracalls to satisfy these page requests.
17ca9f4942SBharata B Rao *
18ca9f4942SBharata B Rao * Private ZONE_DEVICE memory equal to the amount of secure memory
19ca9f4942SBharata B Rao * available in the platform for running secure guests is hotplugged.
20ca9f4942SBharata B Rao * Whenever a page belonging to the guest becomes secure, a page from this
21ca9f4942SBharata B Rao * private device memory is used to represent and track that secure page
2260f0a643SBharata B Rao * on the HV side. Some pages (like virtio buffers, VPA pages etc) are
2360f0a643SBharata B Rao * shared between UV and HV. However such pages aren't represented by
2460f0a643SBharata B Rao * device private memory and mappings to shared memory exist in both
2560f0a643SBharata B Rao * UV and HV page tables.
26ca9f4942SBharata B Rao */
27ca9f4942SBharata B Rao
28ca9f4942SBharata B Rao /*
29ca9f4942SBharata B Rao * Notes on locking
30ca9f4942SBharata B Rao *
31ca9f4942SBharata B Rao * kvm->arch.uvmem_lock is a per-guest lock that prevents concurrent
32ca9f4942SBharata B Rao * page-in and page-out requests for the same GPA. Concurrent accesses
33ca9f4942SBharata B Rao * can either come via UV (guest vCPUs requesting for same page)
34ca9f4942SBharata B Rao * or when HV and guest simultaneously access the same page.
35ca9f4942SBharata B Rao * This mutex serializes the migration of page from HV(normal) to
36ca9f4942SBharata B Rao * UV(secure) and vice versa. So the serialization points are around
37ca9f4942SBharata B Rao * migrate_vma routines and page-in/out routines.
38ca9f4942SBharata B Rao *
39ca9f4942SBharata B Rao * Per-guest mutex comes with a cost though. Mainly it serializes the
40ca9f4942SBharata B Rao * fault path as page-out can occur when HV faults on accessing secure
41ca9f4942SBharata B Rao * guest pages. Currently UV issues page-in requests for all the guest
42ca9f4942SBharata B Rao * PFNs one at a time during early boot (UV_ESM uvcall), so this is
43ca9f4942SBharata B Rao * not a cause for concern. Also currently the number of page-outs caused
44ca9f4942SBharata B Rao * by HV touching secure pages is very very low. If an when UV supports
45ca9f4942SBharata B Rao * overcommitting, then we might see concurrent guest driven page-outs.
46ca9f4942SBharata B Rao *
47ca9f4942SBharata B Rao * Locking order
48ca9f4942SBharata B Rao *
49ca9f4942SBharata B Rao * 1. kvm->srcu - Protects KVM memslots
50c1e8d7c6SMichel Lespinasse * 2. kvm->mm->mmap_lock - find_vma, migrate_vma_pages and helpers, ksm_madvise
51ca9f4942SBharata B Rao * 3. kvm->arch.uvmem_lock - protects read/writes to uvmem slots thus acting
52ca9f4942SBharata B Rao * as sync-points for page-in/out
53ca9f4942SBharata B Rao */
54ca9f4942SBharata B Rao
55ca9f4942SBharata B Rao /*
56ca9f4942SBharata B Rao * Notes on page size
57ca9f4942SBharata B Rao *
58ca9f4942SBharata B Rao * Currently UV uses 2MB mappings internally, but will issue H_SVM_PAGE_IN
59ca9f4942SBharata B Rao * and H_SVM_PAGE_OUT hcalls in PAGE_SIZE(64K) granularity. HV tracks
60ca9f4942SBharata B Rao * secure GPAs at 64K page size and maintains one device PFN for each
61ca9f4942SBharata B Rao * 64K secure GPA. UV_PAGE_IN and UV_PAGE_OUT calls by HV are also issued
62ca9f4942SBharata B Rao * for 64K page at a time.
63ca9f4942SBharata B Rao *
64ca9f4942SBharata B Rao * HV faulting on secure pages: When HV touches any secure page, it
65ca9f4942SBharata B Rao * faults and issues a UV_PAGE_OUT request with 64K page size. Currently
66ca9f4942SBharata B Rao * UV splits and remaps the 2MB page if necessary and copies out the
67ca9f4942SBharata B Rao * required 64K page contents.
68ca9f4942SBharata B Rao *
6960f0a643SBharata B Rao * Shared pages: Whenever guest shares a secure page, UV will split and
7060f0a643SBharata B Rao * remap the 2MB page if required and issue H_SVM_PAGE_IN with 64K page size.
7160f0a643SBharata B Rao *
72008e359cSBharata B Rao * HV invalidating a page: When a regular page belonging to secure
73008e359cSBharata B Rao * guest gets unmapped, HV informs UV with UV_PAGE_INVAL of 64K
74008e359cSBharata B Rao * page size. Using 64K page size is correct here because any non-secure
75008e359cSBharata B Rao * page will essentially be of 64K page size. Splitting by UV during sharing
76008e359cSBharata B Rao * and page-out ensures this.
77008e359cSBharata B Rao *
78008e359cSBharata B Rao * Page fault handling: When HV handles page fault of a page belonging
79008e359cSBharata B Rao * to secure guest, it sends that to UV with a 64K UV_PAGE_IN request.
80008e359cSBharata B Rao * Using 64K size is correct here too as UV would have split the 2MB page
81008e359cSBharata B Rao * into 64k mappings and would have done page-outs earlier.
82008e359cSBharata B Rao *
83ca9f4942SBharata B Rao * In summary, the current secure pages handling code in HV assumes
84ca9f4942SBharata B Rao * 64K page size and in fact fails any page-in/page-out requests of
85ca9f4942SBharata B Rao * non-64K size upfront. If and when UV starts supporting multiple
86ca9f4942SBharata B Rao * page-sizes, we need to break this assumption.
87ca9f4942SBharata B Rao */
88ca9f4942SBharata B Rao
89ca9f4942SBharata B Rao #include <linux/pagemap.h>
90ca9f4942SBharata B Rao #include <linux/migrate.h>
91ca9f4942SBharata B Rao #include <linux/kvm_host.h>
92ca9f4942SBharata B Rao #include <linux/ksm.h>
9313a9a5d1SMarc Zyngier #include <linux/of.h>
94dc90f084SChristoph Hellwig #include <linux/memremap.h>
95ca9f4942SBharata B Rao #include <asm/ultravisor.h>
96ca9f4942SBharata B Rao #include <asm/mman.h>
97ca9f4942SBharata B Rao #include <asm/kvm_ppc.h>
98dfaa973aSRam Pai #include <asm/kvm_book3s_uvmem.h>
99ca9f4942SBharata B Rao
100ca9f4942SBharata B Rao static struct dev_pagemap kvmppc_uvmem_pgmap;
101ca9f4942SBharata B Rao static unsigned long *kvmppc_uvmem_bitmap;
102ca9f4942SBharata B Rao static DEFINE_SPINLOCK(kvmppc_uvmem_bitmap_lock);
103ca9f4942SBharata B Rao
104651a6310SRam Pai /*
105651a6310SRam Pai * States of a GFN
106651a6310SRam Pai * ---------------
107651a6310SRam Pai * The GFN can be in one of the following states.
108651a6310SRam Pai *
109651a6310SRam Pai * (a) Secure - The GFN is secure. The GFN is associated with
110651a6310SRam Pai * a Secure VM, the contents of the GFN is not accessible
111651a6310SRam Pai * to the Hypervisor. This GFN can be backed by a secure-PFN,
112651a6310SRam Pai * or can be backed by a normal-PFN with contents encrypted.
113651a6310SRam Pai * The former is true when the GFN is paged-in into the
114651a6310SRam Pai * ultravisor. The latter is true when the GFN is paged-out
115651a6310SRam Pai * of the ultravisor.
116651a6310SRam Pai *
117651a6310SRam Pai * (b) Shared - The GFN is shared. The GFN is associated with a
118651a6310SRam Pai * a secure VM. The contents of the GFN is accessible to
119651a6310SRam Pai * Hypervisor. This GFN is backed by a normal-PFN and its
120651a6310SRam Pai * content is un-encrypted.
121651a6310SRam Pai *
122651a6310SRam Pai * (c) Normal - The GFN is a normal. The GFN is associated with
1231fd02f66SJulia Lawall * a normal VM. The contents of the GFN is accessible to
124651a6310SRam Pai * the Hypervisor. Its content is never encrypted.
125651a6310SRam Pai *
126651a6310SRam Pai * States of a VM.
127651a6310SRam Pai * ---------------
128651a6310SRam Pai *
129651a6310SRam Pai * Normal VM: A VM whose contents are always accessible to
130651a6310SRam Pai * the hypervisor. All its GFNs are normal-GFNs.
131651a6310SRam Pai *
132651a6310SRam Pai * Secure VM: A VM whose contents are not accessible to the
133651a6310SRam Pai * hypervisor without the VM's consent. Its GFNs are
134651a6310SRam Pai * either Shared-GFN or Secure-GFNs.
135651a6310SRam Pai *
136651a6310SRam Pai * Transient VM: A Normal VM that is transitioning to secure VM.
137651a6310SRam Pai * The transition starts on successful return of
138651a6310SRam Pai * H_SVM_INIT_START, and ends on successful return
139651a6310SRam Pai * of H_SVM_INIT_DONE. This transient VM, can have GFNs
140651a6310SRam Pai * in any of the three states; i.e Secure-GFN, Shared-GFN,
141651a6310SRam Pai * and Normal-GFN. The VM never executes in this state
142651a6310SRam Pai * in supervisor-mode.
143651a6310SRam Pai *
144651a6310SRam Pai * Memory slot State.
145651a6310SRam Pai * -----------------------------
146651a6310SRam Pai * The state of a memory slot mirrors the state of the
147651a6310SRam Pai * VM the memory slot is associated with.
148651a6310SRam Pai *
149651a6310SRam Pai * VM State transition.
150651a6310SRam Pai * --------------------
151651a6310SRam Pai *
152651a6310SRam Pai * A VM always starts in Normal Mode.
153651a6310SRam Pai *
154651a6310SRam Pai * H_SVM_INIT_START moves the VM into transient state. During this
155651a6310SRam Pai * time the Ultravisor may request some of its GFNs to be shared or
156651a6310SRam Pai * secured. So its GFNs can be in one of the three GFN states.
157651a6310SRam Pai *
158651a6310SRam Pai * H_SVM_INIT_DONE moves the VM entirely from transient state to
159651a6310SRam Pai * secure-state. At this point any left-over normal-GFNs are
160651a6310SRam Pai * transitioned to Secure-GFN.
161651a6310SRam Pai *
162651a6310SRam Pai * H_SVM_INIT_ABORT moves the transient VM back to normal VM.
163651a6310SRam Pai * All its GFNs are moved to Normal-GFNs.
164651a6310SRam Pai *
165651a6310SRam Pai * UV_TERMINATE transitions the secure-VM back to normal-VM. All
166651a6310SRam Pai * the secure-GFN and shared-GFNs are tranistioned to normal-GFN
167651a6310SRam Pai * Note: The contents of the normal-GFN is undefined at this point.
168651a6310SRam Pai *
169651a6310SRam Pai * GFN state implementation:
170651a6310SRam Pai * -------------------------
171651a6310SRam Pai *
172651a6310SRam Pai * Secure GFN is associated with a secure-PFN; also called uvmem_pfn,
173651a6310SRam Pai * when the GFN is paged-in. Its pfn[] has KVMPPC_GFN_UVMEM_PFN flag
174651a6310SRam Pai * set, and contains the value of the secure-PFN.
175651a6310SRam Pai * It is associated with a normal-PFN; also called mem_pfn, when
176651a6310SRam Pai * the GFN is pagedout. Its pfn[] has KVMPPC_GFN_MEM_PFN flag set.
177651a6310SRam Pai * The value of the normal-PFN is not tracked.
178651a6310SRam Pai *
179651a6310SRam Pai * Shared GFN is associated with a normal-PFN. Its pfn[] has
180651a6310SRam Pai * KVMPPC_UVMEM_SHARED_PFN flag set. The value of the normal-PFN
181651a6310SRam Pai * is not tracked.
182651a6310SRam Pai *
183651a6310SRam Pai * Normal GFN is associated with normal-PFN. Its pfn[] has
184651a6310SRam Pai * no flag set. The value of the normal-PFN is not tracked.
185651a6310SRam Pai *
186651a6310SRam Pai * Life cycle of a GFN
187651a6310SRam Pai * --------------------
188651a6310SRam Pai *
189651a6310SRam Pai * --------------------------------------------------------------
190651a6310SRam Pai * | | Share | Unshare | SVM |H_SVM_INIT_DONE|
191651a6310SRam Pai * | |operation |operation | abort/ | |
192651a6310SRam Pai * | | | | terminate | |
193651a6310SRam Pai * -------------------------------------------------------------
194651a6310SRam Pai * | | | | | |
195651a6310SRam Pai * | Secure | Shared | Secure |Normal |Secure |
196651a6310SRam Pai * | | | | | |
197651a6310SRam Pai * | Shared | Shared | Secure |Normal |Shared |
198651a6310SRam Pai * | | | | | |
199651a6310SRam Pai * | Normal | Shared | Secure |Normal |Secure |
200651a6310SRam Pai * --------------------------------------------------------------
201651a6310SRam Pai *
202651a6310SRam Pai * Life cycle of a VM
203651a6310SRam Pai * --------------------
204651a6310SRam Pai *
205651a6310SRam Pai * --------------------------------------------------------------------
206651a6310SRam Pai * | | start | H_SVM_ |H_SVM_ |H_SVM_ |UV_SVM_ |
207651a6310SRam Pai * | | VM |INIT_START|INIT_DONE|INIT_ABORT |TERMINATE |
208651a6310SRam Pai * | | | | | | |
209651a6310SRam Pai * --------- ----------------------------------------------------------
210651a6310SRam Pai * | | | | | | |
211651a6310SRam Pai * | Normal | Normal | Transient|Error |Error |Normal |
212651a6310SRam Pai * | | | | | | |
213651a6310SRam Pai * | Secure | Error | Error |Error |Error |Normal |
214651a6310SRam Pai * | | | | | | |
215651a6310SRam Pai * |Transient| N/A | Error |Secure |Normal |Normal |
216651a6310SRam Pai * --------------------------------------------------------------------
217651a6310SRam Pai */
218651a6310SRam Pai
219651a6310SRam Pai #define KVMPPC_GFN_UVMEM_PFN (1UL << 63)
220651a6310SRam Pai #define KVMPPC_GFN_MEM_PFN (1UL << 62)
221651a6310SRam Pai #define KVMPPC_GFN_SHARED (1UL << 61)
222651a6310SRam Pai #define KVMPPC_GFN_SECURE (KVMPPC_GFN_UVMEM_PFN | KVMPPC_GFN_MEM_PFN)
223651a6310SRam Pai #define KVMPPC_GFN_FLAG_MASK (KVMPPC_GFN_SECURE | KVMPPC_GFN_SHARED)
224651a6310SRam Pai #define KVMPPC_GFN_PFN_MASK (~KVMPPC_GFN_FLAG_MASK)
225ca9f4942SBharata B Rao
226ca9f4942SBharata B Rao struct kvmppc_uvmem_slot {
227ca9f4942SBharata B Rao struct list_head list;
228ca9f4942SBharata B Rao unsigned long nr_pfns;
229ca9f4942SBharata B Rao unsigned long base_pfn;
230ca9f4942SBharata B Rao unsigned long *pfns;
231ca9f4942SBharata B Rao };
232ca9f4942SBharata B Rao struct kvmppc_uvmem_page_pvt {
233ca9f4942SBharata B Rao struct kvm *kvm;
234ca9f4942SBharata B Rao unsigned long gpa;
23560f0a643SBharata B Rao bool skip_page_out;
236651a6310SRam Pai bool remove_gfn;
237ca9f4942SBharata B Rao };
238ca9f4942SBharata B Rao
kvmppc_uvmem_available(void)2399a5788c6SPaul Mackerras bool kvmppc_uvmem_available(void)
2409a5788c6SPaul Mackerras {
2419a5788c6SPaul Mackerras /*
2429a5788c6SPaul Mackerras * If kvmppc_uvmem_bitmap != NULL, then there is an ultravisor
2439a5788c6SPaul Mackerras * and our data structures have been initialized successfully.
2449a5788c6SPaul Mackerras */
2459a5788c6SPaul Mackerras return !!kvmppc_uvmem_bitmap;
2469a5788c6SPaul Mackerras }
2479a5788c6SPaul Mackerras
kvmppc_uvmem_slot_init(struct kvm * kvm,const struct kvm_memory_slot * slot)248ca9f4942SBharata B Rao int kvmppc_uvmem_slot_init(struct kvm *kvm, const struct kvm_memory_slot *slot)
249ca9f4942SBharata B Rao {
250ca9f4942SBharata B Rao struct kvmppc_uvmem_slot *p;
251ca9f4942SBharata B Rao
252ca9f4942SBharata B Rao p = kzalloc(sizeof(*p), GFP_KERNEL);
253ca9f4942SBharata B Rao if (!p)
254ca9f4942SBharata B Rao return -ENOMEM;
25537b2a651SPaolo Bonzini p->pfns = vcalloc(slot->npages, sizeof(*p->pfns));
256ca9f4942SBharata B Rao if (!p->pfns) {
257ca9f4942SBharata B Rao kfree(p);
258ca9f4942SBharata B Rao return -ENOMEM;
259ca9f4942SBharata B Rao }
260ca9f4942SBharata B Rao p->nr_pfns = slot->npages;
261ca9f4942SBharata B Rao p->base_pfn = slot->base_gfn;
262ca9f4942SBharata B Rao
263ca9f4942SBharata B Rao mutex_lock(&kvm->arch.uvmem_lock);
264ca9f4942SBharata B Rao list_add(&p->list, &kvm->arch.uvmem_pfns);
265ca9f4942SBharata B Rao mutex_unlock(&kvm->arch.uvmem_lock);
266ca9f4942SBharata B Rao
267ca9f4942SBharata B Rao return 0;
268ca9f4942SBharata B Rao }
269ca9f4942SBharata B Rao
270ca9f4942SBharata B Rao /*
271ca9f4942SBharata B Rao * All device PFNs are already released by the time we come here.
272ca9f4942SBharata B Rao */
kvmppc_uvmem_slot_free(struct kvm * kvm,const struct kvm_memory_slot * slot)273ca9f4942SBharata B Rao void kvmppc_uvmem_slot_free(struct kvm *kvm, const struct kvm_memory_slot *slot)
274ca9f4942SBharata B Rao {
275ca9f4942SBharata B Rao struct kvmppc_uvmem_slot *p, *next;
276ca9f4942SBharata B Rao
277ca9f4942SBharata B Rao mutex_lock(&kvm->arch.uvmem_lock);
278ca9f4942SBharata B Rao list_for_each_entry_safe(p, next, &kvm->arch.uvmem_pfns, list) {
279ca9f4942SBharata B Rao if (p->base_pfn == slot->base_gfn) {
280ca9f4942SBharata B Rao vfree(p->pfns);
281ca9f4942SBharata B Rao list_del(&p->list);
282ca9f4942SBharata B Rao kfree(p);
283ca9f4942SBharata B Rao break;
284ca9f4942SBharata B Rao }
285ca9f4942SBharata B Rao }
286ca9f4942SBharata B Rao mutex_unlock(&kvm->arch.uvmem_lock);
287ca9f4942SBharata B Rao }
288ca9f4942SBharata B Rao
kvmppc_mark_gfn(unsigned long gfn,struct kvm * kvm,unsigned long flag,unsigned long uvmem_pfn)289651a6310SRam Pai static void kvmppc_mark_gfn(unsigned long gfn, struct kvm *kvm,
290651a6310SRam Pai unsigned long flag, unsigned long uvmem_pfn)
291ca9f4942SBharata B Rao {
292ca9f4942SBharata B Rao struct kvmppc_uvmem_slot *p;
293ca9f4942SBharata B Rao
294ca9f4942SBharata B Rao list_for_each_entry(p, &kvm->arch.uvmem_pfns, list) {
295ca9f4942SBharata B Rao if (gfn >= p->base_pfn && gfn < p->base_pfn + p->nr_pfns) {
296ca9f4942SBharata B Rao unsigned long index = gfn - p->base_pfn;
297ca9f4942SBharata B Rao
298651a6310SRam Pai if (flag == KVMPPC_GFN_UVMEM_PFN)
299651a6310SRam Pai p->pfns[index] = uvmem_pfn | flag;
300651a6310SRam Pai else
301651a6310SRam Pai p->pfns[index] = flag;
302ca9f4942SBharata B Rao return;
303ca9f4942SBharata B Rao }
304ca9f4942SBharata B Rao }
305ca9f4942SBharata B Rao }
306ca9f4942SBharata B Rao
307651a6310SRam Pai /* mark the GFN as secure-GFN associated with @uvmem pfn device-PFN. */
kvmppc_gfn_secure_uvmem_pfn(unsigned long gfn,unsigned long uvmem_pfn,struct kvm * kvm)308651a6310SRam Pai static void kvmppc_gfn_secure_uvmem_pfn(unsigned long gfn,
309651a6310SRam Pai unsigned long uvmem_pfn, struct kvm *kvm)
310ca9f4942SBharata B Rao {
311651a6310SRam Pai kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_UVMEM_PFN, uvmem_pfn);
312ca9f4942SBharata B Rao }
313ca9f4942SBharata B Rao
314651a6310SRam Pai /* mark the GFN as secure-GFN associated with a memory-PFN. */
kvmppc_gfn_secure_mem_pfn(unsigned long gfn,struct kvm * kvm)315651a6310SRam Pai static void kvmppc_gfn_secure_mem_pfn(unsigned long gfn, struct kvm *kvm)
316651a6310SRam Pai {
317651a6310SRam Pai kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_MEM_PFN, 0);
318651a6310SRam Pai }
319651a6310SRam Pai
320651a6310SRam Pai /* mark the GFN as a shared GFN. */
kvmppc_gfn_shared(unsigned long gfn,struct kvm * kvm)321651a6310SRam Pai static void kvmppc_gfn_shared(unsigned long gfn, struct kvm *kvm)
322651a6310SRam Pai {
323651a6310SRam Pai kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_SHARED, 0);
324651a6310SRam Pai }
325651a6310SRam Pai
326651a6310SRam Pai /* mark the GFN as a non-existent GFN. */
kvmppc_gfn_remove(unsigned long gfn,struct kvm * kvm)327651a6310SRam Pai static void kvmppc_gfn_remove(unsigned long gfn, struct kvm *kvm)
328651a6310SRam Pai {
329651a6310SRam Pai kvmppc_mark_gfn(gfn, kvm, 0, 0);
330651a6310SRam Pai }
331651a6310SRam Pai
332651a6310SRam Pai /* return true, if the GFN is a secure-GFN backed by a secure-PFN */
kvmppc_gfn_is_uvmem_pfn(unsigned long gfn,struct kvm * kvm,unsigned long * uvmem_pfn)333ca9f4942SBharata B Rao static bool kvmppc_gfn_is_uvmem_pfn(unsigned long gfn, struct kvm *kvm,
334ca9f4942SBharata B Rao unsigned long *uvmem_pfn)
335ca9f4942SBharata B Rao {
336ca9f4942SBharata B Rao struct kvmppc_uvmem_slot *p;
337ca9f4942SBharata B Rao
338ca9f4942SBharata B Rao list_for_each_entry(p, &kvm->arch.uvmem_pfns, list) {
339ca9f4942SBharata B Rao if (gfn >= p->base_pfn && gfn < p->base_pfn + p->nr_pfns) {
340ca9f4942SBharata B Rao unsigned long index = gfn - p->base_pfn;
341ca9f4942SBharata B Rao
342651a6310SRam Pai if (p->pfns[index] & KVMPPC_GFN_UVMEM_PFN) {
343ca9f4942SBharata B Rao if (uvmem_pfn)
344ca9f4942SBharata B Rao *uvmem_pfn = p->pfns[index] &
345651a6310SRam Pai KVMPPC_GFN_PFN_MASK;
346ca9f4942SBharata B Rao return true;
347ca9f4942SBharata B Rao } else
348ca9f4942SBharata B Rao return false;
349ca9f4942SBharata B Rao }
350ca9f4942SBharata B Rao }
351ca9f4942SBharata B Rao return false;
352ca9f4942SBharata B Rao }
353ca9f4942SBharata B Rao
354dfaa973aSRam Pai /*
355dfaa973aSRam Pai * starting from *gfn search for the next available GFN that is not yet
356dfaa973aSRam Pai * transitioned to a secure GFN. return the value of that GFN in *gfn. If a
357dfaa973aSRam Pai * GFN is found, return true, else return false
358dfaa973aSRam Pai *
359dfaa973aSRam Pai * Must be called with kvm->arch.uvmem_lock held.
360dfaa973aSRam Pai */
kvmppc_next_nontransitioned_gfn(const struct kvm_memory_slot * memslot,struct kvm * kvm,unsigned long * gfn)361dfaa973aSRam Pai static bool kvmppc_next_nontransitioned_gfn(const struct kvm_memory_slot *memslot,
362dfaa973aSRam Pai struct kvm *kvm, unsigned long *gfn)
363dfaa973aSRam Pai {
364300981abSXiaomeng Tong struct kvmppc_uvmem_slot *p = NULL, *iter;
365dfaa973aSRam Pai bool ret = false;
366dfaa973aSRam Pai unsigned long i;
367dfaa973aSRam Pai
368300981abSXiaomeng Tong list_for_each_entry(iter, &kvm->arch.uvmem_pfns, list)
369300981abSXiaomeng Tong if (*gfn >= iter->base_pfn && *gfn < iter->base_pfn + iter->nr_pfns) {
370300981abSXiaomeng Tong p = iter;
371dfaa973aSRam Pai break;
372300981abSXiaomeng Tong }
373dfaa973aSRam Pai if (!p)
374dfaa973aSRam Pai return ret;
375dfaa973aSRam Pai /*
376dfaa973aSRam Pai * The code below assumes, one to one correspondence between
377dfaa973aSRam Pai * kvmppc_uvmem_slot and memslot.
378dfaa973aSRam Pai */
379dfaa973aSRam Pai for (i = *gfn; i < p->base_pfn + p->nr_pfns; i++) {
380dfaa973aSRam Pai unsigned long index = i - p->base_pfn;
381dfaa973aSRam Pai
382dfaa973aSRam Pai if (!(p->pfns[index] & KVMPPC_GFN_FLAG_MASK)) {
383dfaa973aSRam Pai *gfn = i;
384dfaa973aSRam Pai ret = true;
385dfaa973aSRam Pai break;
386dfaa973aSRam Pai }
387dfaa973aSRam Pai }
388dfaa973aSRam Pai return ret;
389dfaa973aSRam Pai }
390dfaa973aSRam Pai
kvmppc_memslot_page_merge(struct kvm * kvm,const struct kvm_memory_slot * memslot,bool merge)3912027a24aSRam Pai static int kvmppc_memslot_page_merge(struct kvm *kvm,
3922027a24aSRam Pai const struct kvm_memory_slot *memslot, bool merge)
3932027a24aSRam Pai {
3942027a24aSRam Pai unsigned long gfn = memslot->base_gfn;
3952027a24aSRam Pai unsigned long end, start = gfn_to_hva(kvm, gfn);
396ff126c0eSSuren Baghdasaryan unsigned long vm_flags;
3972027a24aSRam Pai int ret = 0;
3982027a24aSRam Pai struct vm_area_struct *vma;
3992027a24aSRam Pai int merge_flag = (merge) ? MADV_MERGEABLE : MADV_UNMERGEABLE;
4002027a24aSRam Pai
4012027a24aSRam Pai if (kvm_is_error_hva(start))
4022027a24aSRam Pai return H_STATE;
4032027a24aSRam Pai
4042027a24aSRam Pai end = start + (memslot->npages << PAGE_SHIFT);
4052027a24aSRam Pai
4062027a24aSRam Pai mmap_write_lock(kvm->mm);
4072027a24aSRam Pai do {
4082027a24aSRam Pai vma = find_vma_intersection(kvm->mm, start, end);
4092027a24aSRam Pai if (!vma) {
4102027a24aSRam Pai ret = H_STATE;
4112027a24aSRam Pai break;
4122027a24aSRam Pai }
413*60081bf1SSuren Baghdasaryan vma_start_write(vma);
414ff126c0eSSuren Baghdasaryan /* Copy vm_flags to avoid partial modifications in ksm_madvise */
415ff126c0eSSuren Baghdasaryan vm_flags = vma->vm_flags;
4162027a24aSRam Pai ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
417ff126c0eSSuren Baghdasaryan merge_flag, &vm_flags);
4182027a24aSRam Pai if (ret) {
4192027a24aSRam Pai ret = H_STATE;
4202027a24aSRam Pai break;
4212027a24aSRam Pai }
422ff126c0eSSuren Baghdasaryan vm_flags_reset(vma, vm_flags);
4232027a24aSRam Pai start = vma->vm_end;
4242027a24aSRam Pai } while (end > vma->vm_end);
4252027a24aSRam Pai
4262027a24aSRam Pai mmap_write_unlock(kvm->mm);
4272027a24aSRam Pai return ret;
4282027a24aSRam Pai }
4292027a24aSRam Pai
__kvmppc_uvmem_memslot_delete(struct kvm * kvm,const struct kvm_memory_slot * memslot)430a2ce7200SLaurent Dufour static void __kvmppc_uvmem_memslot_delete(struct kvm *kvm,
4312027a24aSRam Pai const struct kvm_memory_slot *memslot)
4322027a24aSRam Pai {
4332027a24aSRam Pai uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
4342027a24aSRam Pai kvmppc_uvmem_slot_free(kvm, memslot);
4352027a24aSRam Pai kvmppc_memslot_page_merge(kvm, memslot, true);
4362027a24aSRam Pai }
4372027a24aSRam Pai
__kvmppc_uvmem_memslot_create(struct kvm * kvm,const struct kvm_memory_slot * memslot)438a2ce7200SLaurent Dufour static int __kvmppc_uvmem_memslot_create(struct kvm *kvm,
4392027a24aSRam Pai const struct kvm_memory_slot *memslot)
4402027a24aSRam Pai {
4412027a24aSRam Pai int ret = H_PARAMETER;
4422027a24aSRam Pai
4432027a24aSRam Pai if (kvmppc_memslot_page_merge(kvm, memslot, false))
4442027a24aSRam Pai return ret;
4452027a24aSRam Pai
4462027a24aSRam Pai if (kvmppc_uvmem_slot_init(kvm, memslot))
4472027a24aSRam Pai goto out1;
4482027a24aSRam Pai
4492027a24aSRam Pai ret = uv_register_mem_slot(kvm->arch.lpid,
4502027a24aSRam Pai memslot->base_gfn << PAGE_SHIFT,
4512027a24aSRam Pai memslot->npages * PAGE_SIZE,
4522027a24aSRam Pai 0, memslot->id);
4532027a24aSRam Pai if (ret < 0) {
4542027a24aSRam Pai ret = H_PARAMETER;
4552027a24aSRam Pai goto out;
4562027a24aSRam Pai }
4572027a24aSRam Pai return 0;
4582027a24aSRam Pai out:
4592027a24aSRam Pai kvmppc_uvmem_slot_free(kvm, memslot);
4602027a24aSRam Pai out1:
4612027a24aSRam Pai kvmppc_memslot_page_merge(kvm, memslot, true);
4622027a24aSRam Pai return ret;
4632027a24aSRam Pai }
4642027a24aSRam Pai
kvmppc_h_svm_init_start(struct kvm * kvm)465ca9f4942SBharata B Rao unsigned long kvmppc_h_svm_init_start(struct kvm *kvm)
466ca9f4942SBharata B Rao {
467ca9f4942SBharata B Rao struct kvm_memslots *slots;
4682027a24aSRam Pai struct kvm_memory_slot *memslot, *m;
469ca9f4942SBharata B Rao int ret = H_SUCCESS;
470a54d8066SMaciej S. Szmigiero int srcu_idx, bkt;
471ca9f4942SBharata B Rao
472377f02d4SLaurent Dufour kvm->arch.secure_guest = KVMPPC_SECURE_INIT_START;
473377f02d4SLaurent Dufour
474ca9f4942SBharata B Rao if (!kvmppc_uvmem_bitmap)
475ca9f4942SBharata B Rao return H_UNSUPPORTED;
476ca9f4942SBharata B Rao
477ca9f4942SBharata B Rao /* Only radix guests can be secure guests */
478ca9f4942SBharata B Rao if (!kvm_is_radix(kvm))
479ca9f4942SBharata B Rao return H_UNSUPPORTED;
480ca9f4942SBharata B Rao
4819a5788c6SPaul Mackerras /* NAK the transition to secure if not enabled */
4829a5788c6SPaul Mackerras if (!kvm->arch.svm_enabled)
4839a5788c6SPaul Mackerras return H_AUTHORITY;
4849a5788c6SPaul Mackerras
485ca9f4942SBharata B Rao srcu_idx = srcu_read_lock(&kvm->srcu);
4862027a24aSRam Pai
4872027a24aSRam Pai /* register the memslot */
488ca9f4942SBharata B Rao slots = kvm_memslots(kvm);
489a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(memslot, bkt, slots) {
490a2ce7200SLaurent Dufour ret = __kvmppc_uvmem_memslot_create(kvm, memslot);
4912027a24aSRam Pai if (ret)
4922027a24aSRam Pai break;
493ca9f4942SBharata B Rao }
4942027a24aSRam Pai
4952027a24aSRam Pai if (ret) {
4962027a24aSRam Pai slots = kvm_memslots(kvm);
497a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(m, bkt, slots) {
4982027a24aSRam Pai if (m == memslot)
4992027a24aSRam Pai break;
500a2ce7200SLaurent Dufour __kvmppc_uvmem_memslot_delete(kvm, memslot);
501ca9f4942SBharata B Rao }
502ca9f4942SBharata B Rao }
5032027a24aSRam Pai
504ca9f4942SBharata B Rao srcu_read_unlock(&kvm->srcu, srcu_idx);
505ca9f4942SBharata B Rao return ret;
506ca9f4942SBharata B Rao }
507ca9f4942SBharata B Rao
508ca9f4942SBharata B Rao /*
509f1b87ea8SLaurent Dufour * Provision a new page on HV side and copy over the contents
510f1b87ea8SLaurent Dufour * from secure memory using UV_PAGE_OUT uvcall.
511f1b87ea8SLaurent Dufour * Caller must held kvm->arch.uvmem_lock.
512f1b87ea8SLaurent Dufour */
__kvmppc_svm_page_out(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long page_shift,struct kvm * kvm,unsigned long gpa,struct page * fault_page)513f1b87ea8SLaurent Dufour static int __kvmppc_svm_page_out(struct vm_area_struct *vma,
514f1b87ea8SLaurent Dufour unsigned long start,
515f1b87ea8SLaurent Dufour unsigned long end, unsigned long page_shift,
51616ce101dSAlistair Popple struct kvm *kvm, unsigned long gpa, struct page *fault_page)
517ca9f4942SBharata B Rao {
518f1b87ea8SLaurent Dufour unsigned long src_pfn, dst_pfn = 0;
51916ce101dSAlistair Popple struct migrate_vma mig = { 0 };
520f1b87ea8SLaurent Dufour struct page *dpage, *spage;
521f1b87ea8SLaurent Dufour struct kvmppc_uvmem_page_pvt *pvt;
522f1b87ea8SLaurent Dufour unsigned long pfn;
523f1b87ea8SLaurent Dufour int ret = U_SUCCESS;
524ca9f4942SBharata B Rao
525f1b87ea8SLaurent Dufour memset(&mig, 0, sizeof(mig));
526f1b87ea8SLaurent Dufour mig.vma = vma;
527f1b87ea8SLaurent Dufour mig.start = start;
528f1b87ea8SLaurent Dufour mig.end = end;
529f1b87ea8SLaurent Dufour mig.src = &src_pfn;
530f1b87ea8SLaurent Dufour mig.dst = &dst_pfn;
5313ff03278SPaolo Bonzini mig.pgmap_owner = &kvmppc_uvmem_pgmap;
5323ff03278SPaolo Bonzini mig.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
53316ce101dSAlistair Popple mig.fault_page = fault_page;
534f1b87ea8SLaurent Dufour
535f1b87ea8SLaurent Dufour /* The requested page is already paged-out, nothing to do */
536f1b87ea8SLaurent Dufour if (!kvmppc_gfn_is_uvmem_pfn(gpa >> page_shift, kvm, NULL))
537f1b87ea8SLaurent Dufour return ret;
538f1b87ea8SLaurent Dufour
539f1b87ea8SLaurent Dufour ret = migrate_vma_setup(&mig);
540f1b87ea8SLaurent Dufour if (ret)
541f1b87ea8SLaurent Dufour return -1;
542f1b87ea8SLaurent Dufour
543f1b87ea8SLaurent Dufour spage = migrate_pfn_to_page(*mig.src);
544f1b87ea8SLaurent Dufour if (!spage || !(*mig.src & MIGRATE_PFN_MIGRATE))
545f1b87ea8SLaurent Dufour goto out_finalize;
546f1b87ea8SLaurent Dufour
547f1b87ea8SLaurent Dufour if (!is_zone_device_page(spage))
548f1b87ea8SLaurent Dufour goto out_finalize;
549f1b87ea8SLaurent Dufour
550f1b87ea8SLaurent Dufour dpage = alloc_page_vma(GFP_HIGHUSER, vma, start);
551f1b87ea8SLaurent Dufour if (!dpage) {
552f1b87ea8SLaurent Dufour ret = -1;
553f1b87ea8SLaurent Dufour goto out_finalize;
554f1b87ea8SLaurent Dufour }
555f1b87ea8SLaurent Dufour
556f1b87ea8SLaurent Dufour lock_page(dpage);
557f1b87ea8SLaurent Dufour pvt = spage->zone_device_data;
558f1b87ea8SLaurent Dufour pfn = page_to_pfn(dpage);
559f1b87ea8SLaurent Dufour
560f1b87ea8SLaurent Dufour /*
561f1b87ea8SLaurent Dufour * This function is used in two cases:
562f1b87ea8SLaurent Dufour * - When HV touches a secure page, for which we do UV_PAGE_OUT
563f1b87ea8SLaurent Dufour * - When a secure page is converted to shared page, we *get*
564f1b87ea8SLaurent Dufour * the page to essentially unmap the device page. In this
565f1b87ea8SLaurent Dufour * case we skip page-out.
566f1b87ea8SLaurent Dufour */
567f1b87ea8SLaurent Dufour if (!pvt->skip_page_out)
568f1b87ea8SLaurent Dufour ret = uv_page_out(kvm->arch.lpid, pfn << page_shift,
569f1b87ea8SLaurent Dufour gpa, 0, page_shift);
570f1b87ea8SLaurent Dufour
571f1b87ea8SLaurent Dufour if (ret == U_SUCCESS)
572ab09243aSAlistair Popple *mig.dst = migrate_pfn(pfn);
573f1b87ea8SLaurent Dufour else {
574f1b87ea8SLaurent Dufour unlock_page(dpage);
575f1b87ea8SLaurent Dufour __free_page(dpage);
576f1b87ea8SLaurent Dufour goto out_finalize;
577f1b87ea8SLaurent Dufour }
578f1b87ea8SLaurent Dufour
579f1b87ea8SLaurent Dufour migrate_vma_pages(&mig);
580f1b87ea8SLaurent Dufour
581f1b87ea8SLaurent Dufour out_finalize:
582f1b87ea8SLaurent Dufour migrate_vma_finalize(&mig);
583f1b87ea8SLaurent Dufour return ret;
584f1b87ea8SLaurent Dufour }
585f1b87ea8SLaurent Dufour
kvmppc_svm_page_out(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long page_shift,struct kvm * kvm,unsigned long gpa,struct page * fault_page)586f1b87ea8SLaurent Dufour static inline int kvmppc_svm_page_out(struct vm_area_struct *vma,
587f1b87ea8SLaurent Dufour unsigned long start, unsigned long end,
588f1b87ea8SLaurent Dufour unsigned long page_shift,
58916ce101dSAlistair Popple struct kvm *kvm, unsigned long gpa,
59016ce101dSAlistair Popple struct page *fault_page)
591f1b87ea8SLaurent Dufour {
592f1b87ea8SLaurent Dufour int ret;
593f1b87ea8SLaurent Dufour
594f1b87ea8SLaurent Dufour mutex_lock(&kvm->arch.uvmem_lock);
59516ce101dSAlistair Popple ret = __kvmppc_svm_page_out(vma, start, end, page_shift, kvm, gpa,
59616ce101dSAlistair Popple fault_page);
597f1b87ea8SLaurent Dufour mutex_unlock(&kvm->arch.uvmem_lock);
598f1b87ea8SLaurent Dufour
599f1b87ea8SLaurent Dufour return ret;
600ca9f4942SBharata B Rao }
601ca9f4942SBharata B Rao
602ca9f4942SBharata B Rao /*
603c3262257SBharata B Rao * Drop device pages that we maintain for the secure guest
604c3262257SBharata B Rao *
605c3262257SBharata B Rao * We first mark the pages to be skipped from UV_PAGE_OUT when there
606c3262257SBharata B Rao * is HV side fault on these pages. Next we *get* these pages, forcing
607c3262257SBharata B Rao * fault on them, do fault time migration to replace the device PTEs in
608c3262257SBharata B Rao * QEMU page table with normal PTEs from newly allocated pages.
609c3262257SBharata B Rao */
kvmppc_uvmem_drop_pages(const struct kvm_memory_slot * slot,struct kvm * kvm,bool skip_page_out)61081ab595dSLaurent Dufour void kvmppc_uvmem_drop_pages(const struct kvm_memory_slot *slot,
611ce477a7aSSukadev Bhattiprolu struct kvm *kvm, bool skip_page_out)
612c3262257SBharata B Rao {
613c3262257SBharata B Rao int i;
614c3262257SBharata B Rao struct kvmppc_uvmem_page_pvt *pvt;
615c3262257SBharata B Rao struct page *uvmem_page;
61681ab595dSLaurent Dufour struct vm_area_struct *vma = NULL;
61781ab595dSLaurent Dufour unsigned long uvmem_pfn, gfn;
61881ab595dSLaurent Dufour unsigned long addr;
619c3262257SBharata B Rao
62081ab595dSLaurent Dufour mmap_read_lock(kvm->mm);
62181ab595dSLaurent Dufour
62281ab595dSLaurent Dufour addr = slot->userspace_addr;
62381ab595dSLaurent Dufour
62481ab595dSLaurent Dufour gfn = slot->base_gfn;
62581ab595dSLaurent Dufour for (i = slot->npages; i; --i, ++gfn, addr += PAGE_SIZE) {
62681ab595dSLaurent Dufour
62781ab595dSLaurent Dufour /* Fetch the VMA if addr is not in the latest fetched one */
62881ab595dSLaurent Dufour if (!vma || addr >= vma->vm_end) {
62927a14d28SLiam Howlett vma = vma_lookup(kvm->mm, addr);
63081ab595dSLaurent Dufour if (!vma) {
63181ab595dSLaurent Dufour pr_err("Can't find VMA for gfn:0x%lx\n", gfn);
63281ab595dSLaurent Dufour break;
63381ab595dSLaurent Dufour }
634c3262257SBharata B Rao }
635c3262257SBharata B Rao
63681ab595dSLaurent Dufour mutex_lock(&kvm->arch.uvmem_lock);
63781ab595dSLaurent Dufour
63881ab595dSLaurent Dufour if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
639c3262257SBharata B Rao uvmem_page = pfn_to_page(uvmem_pfn);
640c3262257SBharata B Rao pvt = uvmem_page->zone_device_data;
641ce477a7aSSukadev Bhattiprolu pvt->skip_page_out = skip_page_out;
642651a6310SRam Pai pvt->remove_gfn = true;
643c3262257SBharata B Rao
64481ab595dSLaurent Dufour if (__kvmppc_svm_page_out(vma, addr, addr + PAGE_SIZE,
64516ce101dSAlistair Popple PAGE_SHIFT, kvm, pvt->gpa, NULL))
64681ab595dSLaurent Dufour pr_err("Can't page out gpa:0x%lx addr:0x%lx\n",
64781ab595dSLaurent Dufour pvt->gpa, addr);
64881ab595dSLaurent Dufour } else {
64981ab595dSLaurent Dufour /* Remove the shared flag if any */
65081ab595dSLaurent Dufour kvmppc_gfn_remove(gfn, kvm);
651c3262257SBharata B Rao }
65281ab595dSLaurent Dufour
65381ab595dSLaurent Dufour mutex_unlock(&kvm->arch.uvmem_lock);
65481ab595dSLaurent Dufour }
65581ab595dSLaurent Dufour
65681ab595dSLaurent Dufour mmap_read_unlock(kvm->mm);
657c3262257SBharata B Rao }
658c3262257SBharata B Rao
kvmppc_h_svm_init_abort(struct kvm * kvm)6593a43970dSSukadev Bhattiprolu unsigned long kvmppc_h_svm_init_abort(struct kvm *kvm)
6603a43970dSSukadev Bhattiprolu {
661a54d8066SMaciej S. Szmigiero int srcu_idx, bkt;
6623a43970dSSukadev Bhattiprolu struct kvm_memory_slot *memslot;
6633a43970dSSukadev Bhattiprolu
6643a43970dSSukadev Bhattiprolu /*
6653a43970dSSukadev Bhattiprolu * Expect to be called only after INIT_START and before INIT_DONE.
6663a43970dSSukadev Bhattiprolu * If INIT_DONE was completed, use normal VM termination sequence.
6673a43970dSSukadev Bhattiprolu */
6683a43970dSSukadev Bhattiprolu if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
6693a43970dSSukadev Bhattiprolu return H_UNSUPPORTED;
6703a43970dSSukadev Bhattiprolu
6713a43970dSSukadev Bhattiprolu if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
6723a43970dSSukadev Bhattiprolu return H_STATE;
6733a43970dSSukadev Bhattiprolu
6743a43970dSSukadev Bhattiprolu srcu_idx = srcu_read_lock(&kvm->srcu);
6753a43970dSSukadev Bhattiprolu
676a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
6773a43970dSSukadev Bhattiprolu kvmppc_uvmem_drop_pages(memslot, kvm, false);
6783a43970dSSukadev Bhattiprolu
6793a43970dSSukadev Bhattiprolu srcu_read_unlock(&kvm->srcu, srcu_idx);
6803a43970dSSukadev Bhattiprolu
6813a43970dSSukadev Bhattiprolu kvm->arch.secure_guest = 0;
6823a43970dSSukadev Bhattiprolu uv_svm_terminate(kvm->arch.lpid);
6833a43970dSSukadev Bhattiprolu
6843a43970dSSukadev Bhattiprolu return H_PARAMETER;
6853a43970dSSukadev Bhattiprolu }
6863a43970dSSukadev Bhattiprolu
687c3262257SBharata B Rao /*
688ca9f4942SBharata B Rao * Get a free device PFN from the pool
689ca9f4942SBharata B Rao *
690ca9f4942SBharata B Rao * Called when a normal page is moved to secure memory (UV_PAGE_IN). Device
691ca9f4942SBharata B Rao * PFN will be used to keep track of the secure page on HV side.
692ca9f4942SBharata B Rao *
693ca9f4942SBharata B Rao * Called with kvm->arch.uvmem_lock held
694ca9f4942SBharata B Rao */
kvmppc_uvmem_get_page(unsigned long gpa,struct kvm * kvm)695ca9f4942SBharata B Rao static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm)
696ca9f4942SBharata B Rao {
697ca9f4942SBharata B Rao struct page *dpage = NULL;
698ca9f4942SBharata B Rao unsigned long bit, uvmem_pfn;
699ca9f4942SBharata B Rao struct kvmppc_uvmem_page_pvt *pvt;
700ca9f4942SBharata B Rao unsigned long pfn_last, pfn_first;
701ca9f4942SBharata B Rao
702a4574f63SDan Williams pfn_first = kvmppc_uvmem_pgmap.range.start >> PAGE_SHIFT;
703ca9f4942SBharata B Rao pfn_last = pfn_first +
704a4574f63SDan Williams (range_len(&kvmppc_uvmem_pgmap.range) >> PAGE_SHIFT);
705ca9f4942SBharata B Rao
706ca9f4942SBharata B Rao spin_lock(&kvmppc_uvmem_bitmap_lock);
707ca9f4942SBharata B Rao bit = find_first_zero_bit(kvmppc_uvmem_bitmap,
708ca9f4942SBharata B Rao pfn_last - pfn_first);
709ca9f4942SBharata B Rao if (bit >= (pfn_last - pfn_first))
710ca9f4942SBharata B Rao goto out;
711ca9f4942SBharata B Rao bitmap_set(kvmppc_uvmem_bitmap, bit, 1);
712ca9f4942SBharata B Rao spin_unlock(&kvmppc_uvmem_bitmap_lock);
713ca9f4942SBharata B Rao
714ca9f4942SBharata B Rao pvt = kzalloc(sizeof(*pvt), GFP_KERNEL);
715ca9f4942SBharata B Rao if (!pvt)
716ca9f4942SBharata B Rao goto out_clear;
717ca9f4942SBharata B Rao
718ca9f4942SBharata B Rao uvmem_pfn = bit + pfn_first;
719651a6310SRam Pai kvmppc_gfn_secure_uvmem_pfn(gpa >> PAGE_SHIFT, uvmem_pfn, kvm);
720ca9f4942SBharata B Rao
721ca9f4942SBharata B Rao pvt->gpa = gpa;
722ca9f4942SBharata B Rao pvt->kvm = kvm;
723ca9f4942SBharata B Rao
724ca9f4942SBharata B Rao dpage = pfn_to_page(uvmem_pfn);
725ca9f4942SBharata B Rao dpage->zone_device_data = pvt;
726ef233450SAlistair Popple zone_device_page_init(dpage);
727ca9f4942SBharata B Rao return dpage;
728ca9f4942SBharata B Rao out_clear:
729ca9f4942SBharata B Rao spin_lock(&kvmppc_uvmem_bitmap_lock);
730ca9f4942SBharata B Rao bitmap_clear(kvmppc_uvmem_bitmap, bit, 1);
731ca9f4942SBharata B Rao out:
732ca9f4942SBharata B Rao spin_unlock(&kvmppc_uvmem_bitmap_lock);
733ca9f4942SBharata B Rao return NULL;
734ca9f4942SBharata B Rao }
735ca9f4942SBharata B Rao
736ca9f4942SBharata B Rao /*
737dfaa973aSRam Pai * Alloc a PFN from private device memory pool. If @pagein is true,
738dfaa973aSRam Pai * copy page from normal memory to secure memory using UV_PAGE_IN uvcall.
739ca9f4942SBharata B Rao */
kvmppc_svm_page_in(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long gpa,struct kvm * kvm,unsigned long page_shift,bool pagein)740dfaa973aSRam Pai static int kvmppc_svm_page_in(struct vm_area_struct *vma,
741dfaa973aSRam Pai unsigned long start,
742ca9f4942SBharata B Rao unsigned long end, unsigned long gpa, struct kvm *kvm,
743dfaa973aSRam Pai unsigned long page_shift,
744dfaa973aSRam Pai bool pagein)
745ca9f4942SBharata B Rao {
746ca9f4942SBharata B Rao unsigned long src_pfn, dst_pfn = 0;
74716ce101dSAlistair Popple struct migrate_vma mig = { 0 };
748ca9f4942SBharata B Rao struct page *spage;
749ca9f4942SBharata B Rao unsigned long pfn;
750ca9f4942SBharata B Rao struct page *dpage;
751ca9f4942SBharata B Rao int ret = 0;
752ca9f4942SBharata B Rao
753ca9f4942SBharata B Rao memset(&mig, 0, sizeof(mig));
754ca9f4942SBharata B Rao mig.vma = vma;
755ca9f4942SBharata B Rao mig.start = start;
756ca9f4942SBharata B Rao mig.end = end;
757ca9f4942SBharata B Rao mig.src = &src_pfn;
758ca9f4942SBharata B Rao mig.dst = &dst_pfn;
7595143192cSRalph Campbell mig.flags = MIGRATE_VMA_SELECT_SYSTEM;
760ca9f4942SBharata B Rao
761ca9f4942SBharata B Rao ret = migrate_vma_setup(&mig);
762ca9f4942SBharata B Rao if (ret)
763ca9f4942SBharata B Rao return ret;
764ca9f4942SBharata B Rao
765ca9f4942SBharata B Rao if (!(*mig.src & MIGRATE_PFN_MIGRATE)) {
766ca9f4942SBharata B Rao ret = -1;
767ca9f4942SBharata B Rao goto out_finalize;
768ca9f4942SBharata B Rao }
769ca9f4942SBharata B Rao
770ca9f4942SBharata B Rao dpage = kvmppc_uvmem_get_page(gpa, kvm);
771ca9f4942SBharata B Rao if (!dpage) {
772ca9f4942SBharata B Rao ret = -1;
773ca9f4942SBharata B Rao goto out_finalize;
774ca9f4942SBharata B Rao }
775ca9f4942SBharata B Rao
776dfaa973aSRam Pai if (pagein) {
777ca9f4942SBharata B Rao pfn = *mig.src >> MIGRATE_PFN_SHIFT;
778ca9f4942SBharata B Rao spage = migrate_pfn_to_page(*mig.src);
779dfaa973aSRam Pai if (spage) {
780dfaa973aSRam Pai ret = uv_page_in(kvm->arch.lpid, pfn << page_shift,
781dfaa973aSRam Pai gpa, 0, page_shift);
782dfaa973aSRam Pai if (ret)
783dfaa973aSRam Pai goto out_finalize;
784dfaa973aSRam Pai }
785dfaa973aSRam Pai }
786ca9f4942SBharata B Rao
787ab09243aSAlistair Popple *mig.dst = migrate_pfn(page_to_pfn(dpage));
788ca9f4942SBharata B Rao migrate_vma_pages(&mig);
789ca9f4942SBharata B Rao out_finalize:
790ca9f4942SBharata B Rao migrate_vma_finalize(&mig);
791ca9f4942SBharata B Rao return ret;
792ca9f4942SBharata B Rao }
793ca9f4942SBharata B Rao
kvmppc_uv_migrate_mem_slot(struct kvm * kvm,const struct kvm_memory_slot * memslot)794dfaa973aSRam Pai static int kvmppc_uv_migrate_mem_slot(struct kvm *kvm,
795dfaa973aSRam Pai const struct kvm_memory_slot *memslot)
796dfaa973aSRam Pai {
797dfaa973aSRam Pai unsigned long gfn = memslot->base_gfn;
798dfaa973aSRam Pai struct vm_area_struct *vma;
799dfaa973aSRam Pai unsigned long start, end;
800dfaa973aSRam Pai int ret = 0;
801dfaa973aSRam Pai
802dfaa973aSRam Pai mmap_read_lock(kvm->mm);
803dfaa973aSRam Pai mutex_lock(&kvm->arch.uvmem_lock);
804dfaa973aSRam Pai while (kvmppc_next_nontransitioned_gfn(memslot, kvm, &gfn)) {
805dfaa973aSRam Pai ret = H_STATE;
806dfaa973aSRam Pai start = gfn_to_hva(kvm, gfn);
807dfaa973aSRam Pai if (kvm_is_error_hva(start))
808dfaa973aSRam Pai break;
809dfaa973aSRam Pai
810dfaa973aSRam Pai end = start + (1UL << PAGE_SHIFT);
811dfaa973aSRam Pai vma = find_vma_intersection(kvm->mm, start, end);
812dfaa973aSRam Pai if (!vma || vma->vm_start > start || vma->vm_end < end)
813dfaa973aSRam Pai break;
814dfaa973aSRam Pai
815dfaa973aSRam Pai ret = kvmppc_svm_page_in(vma, start, end,
816dfaa973aSRam Pai (gfn << PAGE_SHIFT), kvm, PAGE_SHIFT, false);
817dfaa973aSRam Pai if (ret) {
818dfaa973aSRam Pai ret = H_STATE;
819dfaa973aSRam Pai break;
820dfaa973aSRam Pai }
821dfaa973aSRam Pai
822dfaa973aSRam Pai /* relinquish the cpu if needed */
823dfaa973aSRam Pai cond_resched();
824dfaa973aSRam Pai }
825dfaa973aSRam Pai mutex_unlock(&kvm->arch.uvmem_lock);
826dfaa973aSRam Pai mmap_read_unlock(kvm->mm);
827dfaa973aSRam Pai return ret;
828dfaa973aSRam Pai }
829dfaa973aSRam Pai
kvmppc_h_svm_init_done(struct kvm * kvm)830dfaa973aSRam Pai unsigned long kvmppc_h_svm_init_done(struct kvm *kvm)
831dfaa973aSRam Pai {
832dfaa973aSRam Pai struct kvm_memslots *slots;
833dfaa973aSRam Pai struct kvm_memory_slot *memslot;
834a54d8066SMaciej S. Szmigiero int srcu_idx, bkt;
835dfaa973aSRam Pai long ret = H_SUCCESS;
836dfaa973aSRam Pai
837dfaa973aSRam Pai if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
838dfaa973aSRam Pai return H_UNSUPPORTED;
839dfaa973aSRam Pai
840dfaa973aSRam Pai /* migrate any unmoved normal pfn to device pfns*/
841dfaa973aSRam Pai srcu_idx = srcu_read_lock(&kvm->srcu);
842dfaa973aSRam Pai slots = kvm_memslots(kvm);
843a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(memslot, bkt, slots) {
844dfaa973aSRam Pai ret = kvmppc_uv_migrate_mem_slot(kvm, memslot);
845dfaa973aSRam Pai if (ret) {
846dfaa973aSRam Pai /*
847dfaa973aSRam Pai * The pages will remain transitioned.
848dfaa973aSRam Pai * Its the callers responsibility to
849dfaa973aSRam Pai * terminate the VM, which will undo
850dfaa973aSRam Pai * all state of the VM. Till then
851dfaa973aSRam Pai * this VM is in a erroneous state.
852dfaa973aSRam Pai * Its KVMPPC_SECURE_INIT_DONE will
853dfaa973aSRam Pai * remain unset.
854dfaa973aSRam Pai */
855dfaa973aSRam Pai ret = H_STATE;
856dfaa973aSRam Pai goto out;
857dfaa973aSRam Pai }
858dfaa973aSRam Pai }
859dfaa973aSRam Pai
860dfaa973aSRam Pai kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_DONE;
861dfaa973aSRam Pai pr_info("LPID %d went secure\n", kvm->arch.lpid);
862dfaa973aSRam Pai
863dfaa973aSRam Pai out:
864dfaa973aSRam Pai srcu_read_unlock(&kvm->srcu, srcu_idx);
865dfaa973aSRam Pai return ret;
866dfaa973aSRam Pai }
867dfaa973aSRam Pai
868ca9f4942SBharata B Rao /*
86960f0a643SBharata B Rao * Shares the page with HV, thus making it a normal page.
87060f0a643SBharata B Rao *
87160f0a643SBharata B Rao * - If the page is already secure, then provision a new page and share
87260f0a643SBharata B Rao * - If the page is a normal page, share the existing page
87360f0a643SBharata B Rao *
87460f0a643SBharata B Rao * In the former case, uses dev_pagemap_ops.migrate_to_ram handler
87560f0a643SBharata B Rao * to unmap the device page from QEMU's page tables.
87660f0a643SBharata B Rao */
kvmppc_share_page(struct kvm * kvm,unsigned long gpa,unsigned long page_shift)87748908a38SRam Pai static unsigned long kvmppc_share_page(struct kvm *kvm, unsigned long gpa,
87848908a38SRam Pai unsigned long page_shift)
87960f0a643SBharata B Rao {
88060f0a643SBharata B Rao
88160f0a643SBharata B Rao int ret = H_PARAMETER;
88260f0a643SBharata B Rao struct page *uvmem_page;
88360f0a643SBharata B Rao struct kvmppc_uvmem_page_pvt *pvt;
88460f0a643SBharata B Rao unsigned long pfn;
88560f0a643SBharata B Rao unsigned long gfn = gpa >> page_shift;
88660f0a643SBharata B Rao int srcu_idx;
88760f0a643SBharata B Rao unsigned long uvmem_pfn;
88860f0a643SBharata B Rao
88960f0a643SBharata B Rao srcu_idx = srcu_read_lock(&kvm->srcu);
89060f0a643SBharata B Rao mutex_lock(&kvm->arch.uvmem_lock);
89160f0a643SBharata B Rao if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
89260f0a643SBharata B Rao uvmem_page = pfn_to_page(uvmem_pfn);
89360f0a643SBharata B Rao pvt = uvmem_page->zone_device_data;
89460f0a643SBharata B Rao pvt->skip_page_out = true;
895651a6310SRam Pai /*
896651a6310SRam Pai * do not drop the GFN. It is a valid GFN
897651a6310SRam Pai * that is transitioned to a shared GFN.
898651a6310SRam Pai */
899651a6310SRam Pai pvt->remove_gfn = false;
90060f0a643SBharata B Rao }
90160f0a643SBharata B Rao
90260f0a643SBharata B Rao retry:
90360f0a643SBharata B Rao mutex_unlock(&kvm->arch.uvmem_lock);
90460f0a643SBharata B Rao pfn = gfn_to_pfn(kvm, gfn);
90560f0a643SBharata B Rao if (is_error_noslot_pfn(pfn))
90660f0a643SBharata B Rao goto out;
90760f0a643SBharata B Rao
90860f0a643SBharata B Rao mutex_lock(&kvm->arch.uvmem_lock);
90960f0a643SBharata B Rao if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
91060f0a643SBharata B Rao uvmem_page = pfn_to_page(uvmem_pfn);
91160f0a643SBharata B Rao pvt = uvmem_page->zone_device_data;
91260f0a643SBharata B Rao pvt->skip_page_out = true;
913651a6310SRam Pai pvt->remove_gfn = false; /* it continues to be a valid GFN */
91460f0a643SBharata B Rao kvm_release_pfn_clean(pfn);
91560f0a643SBharata B Rao goto retry;
91660f0a643SBharata B Rao }
91760f0a643SBharata B Rao
918651a6310SRam Pai if (!uv_page_in(kvm->arch.lpid, pfn << page_shift, gpa, 0,
919651a6310SRam Pai page_shift)) {
920651a6310SRam Pai kvmppc_gfn_shared(gfn, kvm);
92160f0a643SBharata B Rao ret = H_SUCCESS;
922651a6310SRam Pai }
92360f0a643SBharata B Rao kvm_release_pfn_clean(pfn);
92460f0a643SBharata B Rao mutex_unlock(&kvm->arch.uvmem_lock);
92560f0a643SBharata B Rao out:
92660f0a643SBharata B Rao srcu_read_unlock(&kvm->srcu, srcu_idx);
92760f0a643SBharata B Rao return ret;
92860f0a643SBharata B Rao }
92960f0a643SBharata B Rao
93060f0a643SBharata B Rao /*
931ca9f4942SBharata B Rao * H_SVM_PAGE_IN: Move page from normal memory to secure memory.
93260f0a643SBharata B Rao *
93360f0a643SBharata B Rao * H_PAGE_IN_SHARED flag makes the page shared which means that the same
93460f0a643SBharata B Rao * memory in is visible from both UV and HV.
935ca9f4942SBharata B Rao */
kvmppc_h_svm_page_in(struct kvm * kvm,unsigned long gpa,unsigned long flags,unsigned long page_shift)93648908a38SRam Pai unsigned long kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa,
93748908a38SRam Pai unsigned long flags,
93848908a38SRam Pai unsigned long page_shift)
939ca9f4942SBharata B Rao {
940ca9f4942SBharata B Rao unsigned long start, end;
941ca9f4942SBharata B Rao struct vm_area_struct *vma;
942ca9f4942SBharata B Rao int srcu_idx;
943ca9f4942SBharata B Rao unsigned long gfn = gpa >> page_shift;
944ca9f4942SBharata B Rao int ret;
945ca9f4942SBharata B Rao
946ca9f4942SBharata B Rao if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
947ca9f4942SBharata B Rao return H_UNSUPPORTED;
948ca9f4942SBharata B Rao
949ca9f4942SBharata B Rao if (page_shift != PAGE_SHIFT)
950ca9f4942SBharata B Rao return H_P3;
951ca9f4942SBharata B Rao
95260f0a643SBharata B Rao if (flags & ~H_PAGE_IN_SHARED)
953ca9f4942SBharata B Rao return H_P2;
954ca9f4942SBharata B Rao
95560f0a643SBharata B Rao if (flags & H_PAGE_IN_SHARED)
95660f0a643SBharata B Rao return kvmppc_share_page(kvm, gpa, page_shift);
95760f0a643SBharata B Rao
958ca9f4942SBharata B Rao ret = H_PARAMETER;
959ca9f4942SBharata B Rao srcu_idx = srcu_read_lock(&kvm->srcu);
9602027a24aSRam Pai mmap_read_lock(kvm->mm);
961ca9f4942SBharata B Rao
962ca9f4942SBharata B Rao start = gfn_to_hva(kvm, gfn);
963ca9f4942SBharata B Rao if (kvm_is_error_hva(start))
964ca9f4942SBharata B Rao goto out;
965ca9f4942SBharata B Rao
966ca9f4942SBharata B Rao mutex_lock(&kvm->arch.uvmem_lock);
967ca9f4942SBharata B Rao /* Fail the page-in request of an already paged-in page */
968ca9f4942SBharata B Rao if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL))
969ca9f4942SBharata B Rao goto out_unlock;
970ca9f4942SBharata B Rao
971ca9f4942SBharata B Rao end = start + (1UL << page_shift);
972ca9f4942SBharata B Rao vma = find_vma_intersection(kvm->mm, start, end);
973ca9f4942SBharata B Rao if (!vma || vma->vm_start > start || vma->vm_end < end)
974ca9f4942SBharata B Rao goto out_unlock;
975ca9f4942SBharata B Rao
976dfaa973aSRam Pai if (kvmppc_svm_page_in(vma, start, end, gpa, kvm, page_shift,
977dfaa973aSRam Pai true))
978dfaa973aSRam Pai goto out_unlock;
979dfaa973aSRam Pai
980ca9f4942SBharata B Rao ret = H_SUCCESS;
981651a6310SRam Pai
982ca9f4942SBharata B Rao out_unlock:
983ca9f4942SBharata B Rao mutex_unlock(&kvm->arch.uvmem_lock);
984ca9f4942SBharata B Rao out:
985d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm);
986ca9f4942SBharata B Rao srcu_read_unlock(&kvm->srcu, srcu_idx);
987ca9f4942SBharata B Rao return ret;
988ca9f4942SBharata B Rao }
989ca9f4942SBharata B Rao
990ca9f4942SBharata B Rao
991ca9f4942SBharata B Rao /*
992ca9f4942SBharata B Rao * Fault handler callback that gets called when HV touches any page that
993ca9f4942SBharata B Rao * has been moved to secure memory, we ask UV to give back the page by
994ca9f4942SBharata B Rao * issuing UV_PAGE_OUT uvcall.
995ca9f4942SBharata B Rao *
996ca9f4942SBharata B Rao * This eventually results in dropping of device PFN and the newly
997ca9f4942SBharata B Rao * provisioned page/PFN gets populated in QEMU page tables.
998ca9f4942SBharata B Rao */
kvmppc_uvmem_migrate_to_ram(struct vm_fault * vmf)999ca9f4942SBharata B Rao static vm_fault_t kvmppc_uvmem_migrate_to_ram(struct vm_fault *vmf)
1000ca9f4942SBharata B Rao {
1001ca9f4942SBharata B Rao struct kvmppc_uvmem_page_pvt *pvt = vmf->page->zone_device_data;
1002ca9f4942SBharata B Rao
1003ca9f4942SBharata B Rao if (kvmppc_svm_page_out(vmf->vma, vmf->address,
1004ca9f4942SBharata B Rao vmf->address + PAGE_SIZE, PAGE_SHIFT,
100516ce101dSAlistair Popple pvt->kvm, pvt->gpa, vmf->page))
1006ca9f4942SBharata B Rao return VM_FAULT_SIGBUS;
1007ca9f4942SBharata B Rao else
1008ca9f4942SBharata B Rao return 0;
1009ca9f4942SBharata B Rao }
1010ca9f4942SBharata B Rao
1011ca9f4942SBharata B Rao /*
1012ca9f4942SBharata B Rao * Release the device PFN back to the pool
1013ca9f4942SBharata B Rao *
1014651a6310SRam Pai * Gets called when secure GFN tranistions from a secure-PFN
1015651a6310SRam Pai * to a normal PFN during H_SVM_PAGE_OUT.
1016ca9f4942SBharata B Rao * Gets called with kvm->arch.uvmem_lock held.
1017ca9f4942SBharata B Rao */
kvmppc_uvmem_page_free(struct page * page)1018ca9f4942SBharata B Rao static void kvmppc_uvmem_page_free(struct page *page)
1019ca9f4942SBharata B Rao {
1020ca9f4942SBharata B Rao unsigned long pfn = page_to_pfn(page) -
1021a4574f63SDan Williams (kvmppc_uvmem_pgmap.range.start >> PAGE_SHIFT);
1022ca9f4942SBharata B Rao struct kvmppc_uvmem_page_pvt *pvt;
1023ca9f4942SBharata B Rao
1024ca9f4942SBharata B Rao spin_lock(&kvmppc_uvmem_bitmap_lock);
1025ca9f4942SBharata B Rao bitmap_clear(kvmppc_uvmem_bitmap, pfn, 1);
1026ca9f4942SBharata B Rao spin_unlock(&kvmppc_uvmem_bitmap_lock);
1027ca9f4942SBharata B Rao
1028ca9f4942SBharata B Rao pvt = page->zone_device_data;
1029ca9f4942SBharata B Rao page->zone_device_data = NULL;
1030651a6310SRam Pai if (pvt->remove_gfn)
1031651a6310SRam Pai kvmppc_gfn_remove(pvt->gpa >> PAGE_SHIFT, pvt->kvm);
1032651a6310SRam Pai else
1033651a6310SRam Pai kvmppc_gfn_secure_mem_pfn(pvt->gpa >> PAGE_SHIFT, pvt->kvm);
1034ca9f4942SBharata B Rao kfree(pvt);
1035ca9f4942SBharata B Rao }
1036ca9f4942SBharata B Rao
1037ca9f4942SBharata B Rao static const struct dev_pagemap_ops kvmppc_uvmem_ops = {
1038ca9f4942SBharata B Rao .page_free = kvmppc_uvmem_page_free,
1039ca9f4942SBharata B Rao .migrate_to_ram = kvmppc_uvmem_migrate_to_ram,
1040ca9f4942SBharata B Rao };
1041ca9f4942SBharata B Rao
1042ca9f4942SBharata B Rao /*
1043ca9f4942SBharata B Rao * H_SVM_PAGE_OUT: Move page from secure memory to normal memory.
1044ca9f4942SBharata B Rao */
1045ca9f4942SBharata B Rao unsigned long
kvmppc_h_svm_page_out(struct kvm * kvm,unsigned long gpa,unsigned long flags,unsigned long page_shift)1046ca9f4942SBharata B Rao kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa,
1047ca9f4942SBharata B Rao unsigned long flags, unsigned long page_shift)
1048ca9f4942SBharata B Rao {
1049ca9f4942SBharata B Rao unsigned long gfn = gpa >> page_shift;
1050ca9f4942SBharata B Rao unsigned long start, end;
1051ca9f4942SBharata B Rao struct vm_area_struct *vma;
1052ca9f4942SBharata B Rao int srcu_idx;
1053ca9f4942SBharata B Rao int ret;
1054ca9f4942SBharata B Rao
1055ca9f4942SBharata B Rao if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
1056ca9f4942SBharata B Rao return H_UNSUPPORTED;
1057ca9f4942SBharata B Rao
1058ca9f4942SBharata B Rao if (page_shift != PAGE_SHIFT)
1059ca9f4942SBharata B Rao return H_P3;
1060ca9f4942SBharata B Rao
1061ca9f4942SBharata B Rao if (flags)
1062ca9f4942SBharata B Rao return H_P2;
1063ca9f4942SBharata B Rao
1064ca9f4942SBharata B Rao ret = H_PARAMETER;
1065ca9f4942SBharata B Rao srcu_idx = srcu_read_lock(&kvm->srcu);
1066d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm);
1067ca9f4942SBharata B Rao start = gfn_to_hva(kvm, gfn);
1068ca9f4942SBharata B Rao if (kvm_is_error_hva(start))
1069ca9f4942SBharata B Rao goto out;
1070ca9f4942SBharata B Rao
1071ca9f4942SBharata B Rao end = start + (1UL << page_shift);
1072ca9f4942SBharata B Rao vma = find_vma_intersection(kvm->mm, start, end);
1073ca9f4942SBharata B Rao if (!vma || vma->vm_start > start || vma->vm_end < end)
1074ca9f4942SBharata B Rao goto out;
1075ca9f4942SBharata B Rao
107616ce101dSAlistair Popple if (!kvmppc_svm_page_out(vma, start, end, page_shift, kvm, gpa, NULL))
1077ca9f4942SBharata B Rao ret = H_SUCCESS;
1078ca9f4942SBharata B Rao out:
1079d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm);
1080ca9f4942SBharata B Rao srcu_read_unlock(&kvm->srcu, srcu_idx);
1081ca9f4942SBharata B Rao return ret;
1082ca9f4942SBharata B Rao }
1083ca9f4942SBharata B Rao
kvmppc_send_page_to_uv(struct kvm * kvm,unsigned long gfn)1084008e359cSBharata B Rao int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gfn)
1085008e359cSBharata B Rao {
1086008e359cSBharata B Rao unsigned long pfn;
1087008e359cSBharata B Rao int ret = U_SUCCESS;
1088008e359cSBharata B Rao
1089008e359cSBharata B Rao pfn = gfn_to_pfn(kvm, gfn);
1090008e359cSBharata B Rao if (is_error_noslot_pfn(pfn))
1091008e359cSBharata B Rao return -EFAULT;
1092008e359cSBharata B Rao
1093008e359cSBharata B Rao mutex_lock(&kvm->arch.uvmem_lock);
1094008e359cSBharata B Rao if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL))
1095008e359cSBharata B Rao goto out;
1096008e359cSBharata B Rao
1097008e359cSBharata B Rao ret = uv_page_in(kvm->arch.lpid, pfn << PAGE_SHIFT, gfn << PAGE_SHIFT,
1098008e359cSBharata B Rao 0, PAGE_SHIFT);
1099008e359cSBharata B Rao out:
1100008e359cSBharata B Rao kvm_release_pfn_clean(pfn);
1101008e359cSBharata B Rao mutex_unlock(&kvm->arch.uvmem_lock);
1102008e359cSBharata B Rao return (ret == U_SUCCESS) ? RESUME_GUEST : -EFAULT;
1103008e359cSBharata B Rao }
1104008e359cSBharata B Rao
kvmppc_uvmem_memslot_create(struct kvm * kvm,const struct kvm_memory_slot * new)1105a2ce7200SLaurent Dufour int kvmppc_uvmem_memslot_create(struct kvm *kvm, const struct kvm_memory_slot *new)
1106a2ce7200SLaurent Dufour {
1107a2ce7200SLaurent Dufour int ret = __kvmppc_uvmem_memslot_create(kvm, new);
1108a2ce7200SLaurent Dufour
1109a2ce7200SLaurent Dufour if (!ret)
1110a2ce7200SLaurent Dufour ret = kvmppc_uv_migrate_mem_slot(kvm, new);
1111a2ce7200SLaurent Dufour
1112a2ce7200SLaurent Dufour return ret;
1113a2ce7200SLaurent Dufour }
1114a2ce7200SLaurent Dufour
kvmppc_uvmem_memslot_delete(struct kvm * kvm,const struct kvm_memory_slot * old)1115a2ce7200SLaurent Dufour void kvmppc_uvmem_memslot_delete(struct kvm *kvm, const struct kvm_memory_slot *old)
1116a2ce7200SLaurent Dufour {
1117a2ce7200SLaurent Dufour __kvmppc_uvmem_memslot_delete(kvm, old);
1118a2ce7200SLaurent Dufour }
1119a2ce7200SLaurent Dufour
kvmppc_get_secmem_size(void)1120ca9f4942SBharata B Rao static u64 kvmppc_get_secmem_size(void)
1121ca9f4942SBharata B Rao {
1122ca9f4942SBharata B Rao struct device_node *np;
1123ca9f4942SBharata B Rao int i, len;
1124ca9f4942SBharata B Rao const __be32 *prop;
1125ca9f4942SBharata B Rao u64 size = 0;
1126ca9f4942SBharata B Rao
1127512721d2SLaurent Dufour /*
1128512721d2SLaurent Dufour * First try the new ibm,secure-memory nodes which supersede the
1129512721d2SLaurent Dufour * secure-memory-ranges property.
1130512721d2SLaurent Dufour * If we found some, no need to read the deprecated ones.
1131512721d2SLaurent Dufour */
1132512721d2SLaurent Dufour for_each_compatible_node(np, NULL, "ibm,secure-memory") {
1133512721d2SLaurent Dufour prop = of_get_property(np, "reg", &len);
1134512721d2SLaurent Dufour if (!prop)
1135512721d2SLaurent Dufour continue;
1136512721d2SLaurent Dufour size += of_read_number(prop + 2, 2);
1137512721d2SLaurent Dufour }
1138512721d2SLaurent Dufour if (size)
1139512721d2SLaurent Dufour return size;
1140512721d2SLaurent Dufour
1141ca9f4942SBharata B Rao np = of_find_compatible_node(NULL, NULL, "ibm,uv-firmware");
1142ca9f4942SBharata B Rao if (!np)
1143ca9f4942SBharata B Rao goto out;
1144ca9f4942SBharata B Rao
1145ca9f4942SBharata B Rao prop = of_get_property(np, "secure-memory-ranges", &len);
1146ca9f4942SBharata B Rao if (!prop)
1147ca9f4942SBharata B Rao goto out_put;
1148ca9f4942SBharata B Rao
1149ca9f4942SBharata B Rao for (i = 0; i < len / (sizeof(*prop) * 4); i++)
1150ca9f4942SBharata B Rao size += of_read_number(prop + (i * 4) + 2, 2);
1151ca9f4942SBharata B Rao
1152ca9f4942SBharata B Rao out_put:
1153ca9f4942SBharata B Rao of_node_put(np);
1154ca9f4942SBharata B Rao out:
1155ca9f4942SBharata B Rao return size;
1156ca9f4942SBharata B Rao }
1157ca9f4942SBharata B Rao
kvmppc_uvmem_init(void)1158ca9f4942SBharata B Rao int kvmppc_uvmem_init(void)
1159ca9f4942SBharata B Rao {
1160ca9f4942SBharata B Rao int ret = 0;
1161ca9f4942SBharata B Rao unsigned long size;
1162ca9f4942SBharata B Rao struct resource *res;
1163ca9f4942SBharata B Rao void *addr;
1164ca9f4942SBharata B Rao unsigned long pfn_last, pfn_first;
1165ca9f4942SBharata B Rao
1166ca9f4942SBharata B Rao size = kvmppc_get_secmem_size();
1167ca9f4942SBharata B Rao if (!size) {
1168ca9f4942SBharata B Rao /*
1169ca9f4942SBharata B Rao * Don't fail the initialization of kvm-hv module if
1170ca9f4942SBharata B Rao * the platform doesn't export ibm,uv-firmware node.
1171ca9f4942SBharata B Rao * Let normal guests run on such PEF-disabled platform.
1172ca9f4942SBharata B Rao */
1173ca9f4942SBharata B Rao pr_info("KVMPPC-UVMEM: No support for secure guests\n");
1174ca9f4942SBharata B Rao goto out;
1175ca9f4942SBharata B Rao }
1176ca9f4942SBharata B Rao
1177ca9f4942SBharata B Rao res = request_free_mem_region(&iomem_resource, size, "kvmppc_uvmem");
1178ca9f4942SBharata B Rao if (IS_ERR(res)) {
1179ca9f4942SBharata B Rao ret = PTR_ERR(res);
1180ca9f4942SBharata B Rao goto out;
1181ca9f4942SBharata B Rao }
1182ca9f4942SBharata B Rao
1183ca9f4942SBharata B Rao kvmppc_uvmem_pgmap.type = MEMORY_DEVICE_PRIVATE;
1184a4574f63SDan Williams kvmppc_uvmem_pgmap.range.start = res->start;
1185a4574f63SDan Williams kvmppc_uvmem_pgmap.range.end = res->end;
1186b7b3c01bSDan Williams kvmppc_uvmem_pgmap.nr_range = 1;
1187ca9f4942SBharata B Rao kvmppc_uvmem_pgmap.ops = &kvmppc_uvmem_ops;
1188f894ddd5SChristoph Hellwig /* just one global instance: */
1189f894ddd5SChristoph Hellwig kvmppc_uvmem_pgmap.owner = &kvmppc_uvmem_pgmap;
1190ca9f4942SBharata B Rao addr = memremap_pages(&kvmppc_uvmem_pgmap, NUMA_NO_NODE);
1191ca9f4942SBharata B Rao if (IS_ERR(addr)) {
1192ca9f4942SBharata B Rao ret = PTR_ERR(addr);
1193ca9f4942SBharata B Rao goto out_free_region;
1194ca9f4942SBharata B Rao }
1195ca9f4942SBharata B Rao
1196ca9f4942SBharata B Rao pfn_first = res->start >> PAGE_SHIFT;
1197ca9f4942SBharata B Rao pfn_last = pfn_first + (resource_size(res) >> PAGE_SHIFT);
1198a96b2075SChristophe JAILLET kvmppc_uvmem_bitmap = bitmap_zalloc(pfn_last - pfn_first, GFP_KERNEL);
1199ca9f4942SBharata B Rao if (!kvmppc_uvmem_bitmap) {
1200ca9f4942SBharata B Rao ret = -ENOMEM;
1201ca9f4942SBharata B Rao goto out_unmap;
1202ca9f4942SBharata B Rao }
1203ca9f4942SBharata B Rao
1204ca9f4942SBharata B Rao pr_info("KVMPPC-UVMEM: Secure Memory size 0x%lx\n", size);
1205ca9f4942SBharata B Rao return ret;
1206ca9f4942SBharata B Rao out_unmap:
1207ca9f4942SBharata B Rao memunmap_pages(&kvmppc_uvmem_pgmap);
1208ca9f4942SBharata B Rao out_free_region:
1209ca9f4942SBharata B Rao release_mem_region(res->start, size);
1210ca9f4942SBharata B Rao out:
1211ca9f4942SBharata B Rao return ret;
1212ca9f4942SBharata B Rao }
1213ca9f4942SBharata B Rao
kvmppc_uvmem_free(void)1214ca9f4942SBharata B Rao void kvmppc_uvmem_free(void)
1215ca9f4942SBharata B Rao {
12169bee484bSFabiano Rosas if (!kvmppc_uvmem_bitmap)
12179bee484bSFabiano Rosas return;
12189bee484bSFabiano Rosas
1219ca9f4942SBharata B Rao memunmap_pages(&kvmppc_uvmem_pgmap);
1220a4574f63SDan Williams release_mem_region(kvmppc_uvmem_pgmap.range.start,
1221a4574f63SDan Williams range_len(&kvmppc_uvmem_pgmap.range));
1222a96b2075SChristophe JAILLET bitmap_free(kvmppc_uvmem_bitmap);
1223ca9f4942SBharata B Rao }
1224