xref: /openbmc/linux/arch/s390/kvm/pv.c (revision b1e42861)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hosting Protected Virtual Machines
4  *
5  * Copyright IBM Corp. 2019, 2020
6  *    Author(s): Janosch Frank <frankja@linux.ibm.com>
7  */
8 #include <linux/kvm.h>
9 #include <linux/kvm_host.h>
10 #include <linux/minmax.h>
11 #include <linux/pagemap.h>
12 #include <linux/sched/signal.h>
13 #include <asm/gmap.h>
14 #include <asm/uv.h>
15 #include <asm/mman.h>
16 #include <linux/pagewalk.h>
17 #include <linux/sched/mm.h>
18 #include <linux/mmu_notifier.h>
19 #include "kvm-s390.h"
20 
21 bool kvm_s390_pv_is_protected(struct kvm *kvm)
22 {
23 	lockdep_assert_held(&kvm->lock);
24 	return !!kvm_s390_pv_get_handle(kvm);
25 }
26 EXPORT_SYMBOL_GPL(kvm_s390_pv_is_protected);
27 
28 bool kvm_s390_pv_cpu_is_protected(struct kvm_vcpu *vcpu)
29 {
30 	lockdep_assert_held(&vcpu->mutex);
31 	return !!kvm_s390_pv_cpu_get_handle(vcpu);
32 }
33 EXPORT_SYMBOL_GPL(kvm_s390_pv_cpu_is_protected);
34 
35 /**
36  * struct pv_vm_to_be_destroyed - Represents a protected VM that needs to
37  * be destroyed
38  *
39  * @list: list head for the list of leftover VMs
40  * @old_gmap_table: the gmap table of the leftover protected VM
41  * @handle: the handle of the leftover protected VM
42  * @stor_var: pointer to the variable storage of the leftover protected VM
43  * @stor_base: address of the base storage of the leftover protected VM
44  *
45  * Represents a protected VM that is still registered with the Ultravisor,
46  * but which does not correspond any longer to an active KVM VM. It should
47  * be destroyed at some point later, either asynchronously or when the
48  * process terminates.
49  */
50 struct pv_vm_to_be_destroyed {
51 	struct list_head list;
52 	unsigned long old_gmap_table;
53 	u64 handle;
54 	void *stor_var;
55 	unsigned long stor_base;
56 };
57 
58 static void kvm_s390_clear_pv_state(struct kvm *kvm)
59 {
60 	kvm->arch.pv.handle = 0;
61 	kvm->arch.pv.guest_len = 0;
62 	kvm->arch.pv.stor_base = 0;
63 	kvm->arch.pv.stor_var = NULL;
64 }
65 
66 int kvm_s390_pv_destroy_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc)
67 {
68 	int cc;
69 
70 	if (!kvm_s390_pv_cpu_get_handle(vcpu))
71 		return 0;
72 
73 	cc = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), UVC_CMD_DESTROY_SEC_CPU, rc, rrc);
74 
75 	KVM_UV_EVENT(vcpu->kvm, 3, "PROTVIRT DESTROY VCPU %d: rc %x rrc %x",
76 		     vcpu->vcpu_id, *rc, *rrc);
77 	WARN_ONCE(cc, "protvirt destroy cpu failed rc %x rrc %x", *rc, *rrc);
78 
79 	/* Intended memory leak for something that should never happen. */
80 	if (!cc)
81 		free_pages(vcpu->arch.pv.stor_base,
82 			   get_order(uv_info.guest_cpu_stor_len));
83 
84 	free_page((unsigned long)sida_addr(vcpu->arch.sie_block));
85 	vcpu->arch.sie_block->pv_handle_cpu = 0;
86 	vcpu->arch.sie_block->pv_handle_config = 0;
87 	memset(&vcpu->arch.pv, 0, sizeof(vcpu->arch.pv));
88 	vcpu->arch.sie_block->sdf = 0;
89 	/*
90 	 * The sidad field (for sdf == 2) is now the gbea field (for sdf == 0).
91 	 * Use the reset value of gbea to avoid leaking the kernel pointer of
92 	 * the just freed sida.
93 	 */
94 	vcpu->arch.sie_block->gbea = 1;
95 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
96 
97 	return cc ? EIO : 0;
98 }
99 
100 int kvm_s390_pv_create_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc)
101 {
102 	struct uv_cb_csc uvcb = {
103 		.header.cmd = UVC_CMD_CREATE_SEC_CPU,
104 		.header.len = sizeof(uvcb),
105 	};
106 	void *sida_addr;
107 	int cc;
108 
109 	if (kvm_s390_pv_cpu_get_handle(vcpu))
110 		return -EINVAL;
111 
112 	vcpu->arch.pv.stor_base = __get_free_pages(GFP_KERNEL_ACCOUNT,
113 						   get_order(uv_info.guest_cpu_stor_len));
114 	if (!vcpu->arch.pv.stor_base)
115 		return -ENOMEM;
116 
117 	/* Input */
118 	uvcb.guest_handle = kvm_s390_pv_get_handle(vcpu->kvm);
119 	uvcb.num = vcpu->arch.sie_block->icpua;
120 	uvcb.state_origin = virt_to_phys(vcpu->arch.sie_block);
121 	uvcb.stor_origin = virt_to_phys((void *)vcpu->arch.pv.stor_base);
122 
123 	/* Alloc Secure Instruction Data Area Designation */
124 	sida_addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
125 	if (!sida_addr) {
126 		free_pages(vcpu->arch.pv.stor_base,
127 			   get_order(uv_info.guest_cpu_stor_len));
128 		return -ENOMEM;
129 	}
130 	vcpu->arch.sie_block->sidad = virt_to_phys(sida_addr);
131 
132 	cc = uv_call(0, (u64)&uvcb);
133 	*rc = uvcb.header.rc;
134 	*rrc = uvcb.header.rrc;
135 	KVM_UV_EVENT(vcpu->kvm, 3,
136 		     "PROTVIRT CREATE VCPU: cpu %d handle %llx rc %x rrc %x",
137 		     vcpu->vcpu_id, uvcb.cpu_handle, uvcb.header.rc,
138 		     uvcb.header.rrc);
139 
140 	if (cc) {
141 		u16 dummy;
142 
143 		kvm_s390_pv_destroy_cpu(vcpu, &dummy, &dummy);
144 		return -EIO;
145 	}
146 
147 	/* Output */
148 	vcpu->arch.pv.handle = uvcb.cpu_handle;
149 	vcpu->arch.sie_block->pv_handle_cpu = uvcb.cpu_handle;
150 	vcpu->arch.sie_block->pv_handle_config = kvm_s390_pv_get_handle(vcpu->kvm);
151 	vcpu->arch.sie_block->sdf = 2;
152 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
153 	return 0;
154 }
155 
156 /* only free resources when the destroy was successful */
157 static void kvm_s390_pv_dealloc_vm(struct kvm *kvm)
158 {
159 	vfree(kvm->arch.pv.stor_var);
160 	free_pages(kvm->arch.pv.stor_base,
161 		   get_order(uv_info.guest_base_stor_len));
162 	kvm_s390_clear_pv_state(kvm);
163 }
164 
165 static int kvm_s390_pv_alloc_vm(struct kvm *kvm)
166 {
167 	unsigned long base = uv_info.guest_base_stor_len;
168 	unsigned long virt = uv_info.guest_virt_var_stor_len;
169 	unsigned long npages = 0, vlen = 0;
170 
171 	kvm->arch.pv.stor_var = NULL;
172 	kvm->arch.pv.stor_base = __get_free_pages(GFP_KERNEL_ACCOUNT, get_order(base));
173 	if (!kvm->arch.pv.stor_base)
174 		return -ENOMEM;
175 
176 	/*
177 	 * Calculate current guest storage for allocation of the
178 	 * variable storage, which is based on the length in MB.
179 	 *
180 	 * Slots are sorted by GFN
181 	 */
182 	mutex_lock(&kvm->slots_lock);
183 	npages = kvm_s390_get_gfn_end(kvm_memslots(kvm));
184 	mutex_unlock(&kvm->slots_lock);
185 
186 	kvm->arch.pv.guest_len = npages * PAGE_SIZE;
187 
188 	/* Allocate variable storage */
189 	vlen = ALIGN(virt * ((npages * PAGE_SIZE) / HPAGE_SIZE), PAGE_SIZE);
190 	vlen += uv_info.guest_virt_base_stor_len;
191 	kvm->arch.pv.stor_var = vzalloc(vlen);
192 	if (!kvm->arch.pv.stor_var)
193 		goto out_err;
194 	return 0;
195 
196 out_err:
197 	kvm_s390_pv_dealloc_vm(kvm);
198 	return -ENOMEM;
199 }
200 
201 /**
202  * kvm_s390_pv_dispose_one_leftover - Clean up one leftover protected VM.
203  * @kvm: the KVM that was associated with this leftover protected VM
204  * @leftover: details about the leftover protected VM that needs a clean up
205  * @rc: the RC code of the Destroy Secure Configuration UVC
206  * @rrc: the RRC code of the Destroy Secure Configuration UVC
207  *
208  * Destroy one leftover protected VM.
209  * On success, kvm->mm->context.protected_count will be decremented atomically
210  * and all other resources used by the VM will be freed.
211  *
212  * Return: 0 in case of success, otherwise 1
213  */
214 static int kvm_s390_pv_dispose_one_leftover(struct kvm *kvm,
215 					    struct pv_vm_to_be_destroyed *leftover,
216 					    u16 *rc, u16 *rrc)
217 {
218 	int cc;
219 
220 	/* It used the destroy-fast UVC, nothing left to do here */
221 	if (!leftover->handle)
222 		goto done_fast;
223 	cc = uv_cmd_nodata(leftover->handle, UVC_CMD_DESTROY_SEC_CONF, rc, rrc);
224 	KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY LEFTOVER VM: rc %x rrc %x", *rc, *rrc);
225 	WARN_ONCE(cc, "protvirt destroy leftover vm failed rc %x rrc %x", *rc, *rrc);
226 	if (cc)
227 		return cc;
228 	/*
229 	 * Intentionally leak unusable memory. If the UVC fails, the memory
230 	 * used for the VM and its metadata is permanently unusable.
231 	 * This can only happen in case of a serious KVM or hardware bug; it
232 	 * is not expected to happen in normal operation.
233 	 */
234 	free_pages(leftover->stor_base, get_order(uv_info.guest_base_stor_len));
235 	free_pages(leftover->old_gmap_table, CRST_ALLOC_ORDER);
236 	vfree(leftover->stor_var);
237 done_fast:
238 	atomic_dec(&kvm->mm->context.protected_count);
239 	return 0;
240 }
241 
242 /**
243  * kvm_s390_destroy_lower_2g - Destroy the first 2GB of protected guest memory.
244  * @kvm: the VM whose memory is to be cleared.
245  *
246  * Destroy the first 2GB of guest memory, to avoid prefix issues after reboot.
247  * The CPUs of the protected VM need to be destroyed beforehand.
248  */
249 static void kvm_s390_destroy_lower_2g(struct kvm *kvm)
250 {
251 	const unsigned long pages_2g = SZ_2G / PAGE_SIZE;
252 	struct kvm_memory_slot *slot;
253 	unsigned long len;
254 	int srcu_idx;
255 
256 	srcu_idx = srcu_read_lock(&kvm->srcu);
257 
258 	/* Take the memslot containing guest absolute address 0 */
259 	slot = gfn_to_memslot(kvm, 0);
260 	/* Clear all slots or parts thereof that are below 2GB */
261 	while (slot && slot->base_gfn < pages_2g) {
262 		len = min_t(u64, slot->npages, pages_2g - slot->base_gfn) * PAGE_SIZE;
263 		s390_uv_destroy_range(kvm->mm, slot->userspace_addr, slot->userspace_addr + len);
264 		/* Take the next memslot */
265 		slot = gfn_to_memslot(kvm, slot->base_gfn + slot->npages);
266 	}
267 
268 	srcu_read_unlock(&kvm->srcu, srcu_idx);
269 }
270 
271 static int kvm_s390_pv_deinit_vm_fast(struct kvm *kvm, u16 *rc, u16 *rrc)
272 {
273 	struct uv_cb_destroy_fast uvcb = {
274 		.header.cmd = UVC_CMD_DESTROY_SEC_CONF_FAST,
275 		.header.len = sizeof(uvcb),
276 		.handle = kvm_s390_pv_get_handle(kvm),
277 	};
278 	int cc;
279 
280 	cc = uv_call_sched(0, (u64)&uvcb);
281 	if (rc)
282 		*rc = uvcb.header.rc;
283 	if (rrc)
284 		*rrc = uvcb.header.rrc;
285 	WRITE_ONCE(kvm->arch.gmap->guest_handle, 0);
286 	KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY VM FAST: rc %x rrc %x",
287 		     uvcb.header.rc, uvcb.header.rrc);
288 	WARN_ONCE(cc && uvcb.header.rc != 0x104,
289 		  "protvirt destroy vm fast failed handle %llx rc %x rrc %x",
290 		  kvm_s390_pv_get_handle(kvm), uvcb.header.rc, uvcb.header.rrc);
291 	/* Intended memory leak on "impossible" error */
292 	if (!cc)
293 		kvm_s390_pv_dealloc_vm(kvm);
294 	return cc ? -EIO : 0;
295 }
296 
297 static inline bool is_destroy_fast_available(void)
298 {
299 	return test_bit_inv(BIT_UVC_CMD_DESTROY_SEC_CONF_FAST, uv_info.inst_calls_list);
300 }
301 
302 /**
303  * kvm_s390_pv_set_aside - Set aside a protected VM for later teardown.
304  * @kvm: the VM
305  * @rc: return value for the RC field of the UVCB
306  * @rrc: return value for the RRC field of the UVCB
307  *
308  * Set aside the protected VM for a subsequent teardown. The VM will be able
309  * to continue immediately as a non-secure VM, and the information needed to
310  * properly tear down the protected VM is set aside. If another protected VM
311  * was already set aside without starting its teardown, this function will
312  * fail.
313  * The CPUs of the protected VM need to be destroyed beforehand.
314  *
315  * Context: kvm->lock needs to be held
316  *
317  * Return: 0 in case of success, -EINVAL if another protected VM was already set
318  * aside, -ENOMEM if the system ran out of memory.
319  */
320 int kvm_s390_pv_set_aside(struct kvm *kvm, u16 *rc, u16 *rrc)
321 {
322 	struct pv_vm_to_be_destroyed *priv;
323 	int res = 0;
324 
325 	lockdep_assert_held(&kvm->lock);
326 	/*
327 	 * If another protected VM was already prepared for teardown, refuse.
328 	 * A normal deinitialization has to be performed instead.
329 	 */
330 	if (kvm->arch.pv.set_aside)
331 		return -EINVAL;
332 
333 	/* Guest with segment type ASCE, refuse to destroy asynchronously */
334 	if ((kvm->arch.gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
335 		return -EINVAL;
336 
337 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
338 	if (!priv)
339 		return -ENOMEM;
340 
341 	if (is_destroy_fast_available()) {
342 		res = kvm_s390_pv_deinit_vm_fast(kvm, rc, rrc);
343 	} else {
344 		priv->stor_var = kvm->arch.pv.stor_var;
345 		priv->stor_base = kvm->arch.pv.stor_base;
346 		priv->handle = kvm_s390_pv_get_handle(kvm);
347 		priv->old_gmap_table = (unsigned long)kvm->arch.gmap->table;
348 		WRITE_ONCE(kvm->arch.gmap->guest_handle, 0);
349 		if (s390_replace_asce(kvm->arch.gmap))
350 			res = -ENOMEM;
351 	}
352 
353 	if (res) {
354 		kfree(priv);
355 		return res;
356 	}
357 
358 	kvm_s390_destroy_lower_2g(kvm);
359 	kvm_s390_clear_pv_state(kvm);
360 	kvm->arch.pv.set_aside = priv;
361 
362 	*rc = UVC_RC_EXECUTED;
363 	*rrc = 42;
364 	return 0;
365 }
366 
367 /**
368  * kvm_s390_pv_deinit_vm - Deinitialize the current protected VM
369  * @kvm: the KVM whose protected VM needs to be deinitialized
370  * @rc: the RC code of the UVC
371  * @rrc: the RRC code of the UVC
372  *
373  * Deinitialize the current protected VM. This function will destroy and
374  * cleanup the current protected VM, but it will not cleanup the guest
375  * memory. This function should only be called when the protected VM has
376  * just been created and therefore does not have any guest memory, or when
377  * the caller cleans up the guest memory separately.
378  *
379  * This function should not fail, but if it does, the donated memory must
380  * not be freed.
381  *
382  * Context: kvm->lock needs to be held
383  *
384  * Return: 0 in case of success, otherwise -EIO
385  */
386 int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
387 {
388 	int cc;
389 
390 	cc = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
391 			   UVC_CMD_DESTROY_SEC_CONF, rc, rrc);
392 	WRITE_ONCE(kvm->arch.gmap->guest_handle, 0);
393 	if (!cc) {
394 		atomic_dec(&kvm->mm->context.protected_count);
395 		kvm_s390_pv_dealloc_vm(kvm);
396 	} else {
397 		/* Intended memory leak on "impossible" error */
398 		s390_replace_asce(kvm->arch.gmap);
399 	}
400 	KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY VM: rc %x rrc %x", *rc, *rrc);
401 	WARN_ONCE(cc, "protvirt destroy vm failed rc %x rrc %x", *rc, *rrc);
402 
403 	return cc ? -EIO : 0;
404 }
405 
406 /**
407  * kvm_s390_pv_deinit_cleanup_all - Clean up all protected VMs associated
408  * with a specific KVM.
409  * @kvm: the KVM to be cleaned up
410  * @rc: the RC code of the first failing UVC
411  * @rrc: the RRC code of the first failing UVC
412  *
413  * This function will clean up all protected VMs associated with a KVM.
414  * This includes the active one, the one prepared for deinitialization with
415  * kvm_s390_pv_set_aside, and any still pending in the need_cleanup list.
416  *
417  * Context: kvm->lock needs to be held unless being called from
418  * kvm_arch_destroy_vm.
419  *
420  * Return: 0 if all VMs are successfully cleaned up, otherwise -EIO
421  */
422 int kvm_s390_pv_deinit_cleanup_all(struct kvm *kvm, u16 *rc, u16 *rrc)
423 {
424 	struct pv_vm_to_be_destroyed *cur;
425 	bool need_zap = false;
426 	u16 _rc, _rrc;
427 	int cc = 0;
428 
429 	/* Make sure the counter does not reach 0 before calling s390_uv_destroy_range */
430 	atomic_inc(&kvm->mm->context.protected_count);
431 
432 	*rc = 1;
433 	/* If the current VM is protected, destroy it */
434 	if (kvm_s390_pv_get_handle(kvm)) {
435 		cc = kvm_s390_pv_deinit_vm(kvm, rc, rrc);
436 		need_zap = true;
437 	}
438 
439 	/* If a previous protected VM was set aside, put it in the need_cleanup list */
440 	if (kvm->arch.pv.set_aside) {
441 		list_add(kvm->arch.pv.set_aside, &kvm->arch.pv.need_cleanup);
442 		kvm->arch.pv.set_aside = NULL;
443 	}
444 
445 	/* Cleanup all protected VMs in the need_cleanup list */
446 	while (!list_empty(&kvm->arch.pv.need_cleanup)) {
447 		cur = list_first_entry(&kvm->arch.pv.need_cleanup, typeof(*cur), list);
448 		need_zap = true;
449 		if (kvm_s390_pv_dispose_one_leftover(kvm, cur, &_rc, &_rrc)) {
450 			cc = 1;
451 			/*
452 			 * Only return the first error rc and rrc, so make
453 			 * sure it is not overwritten. All destroys will
454 			 * additionally be reported via KVM_UV_EVENT().
455 			 */
456 			if (*rc == UVC_RC_EXECUTED) {
457 				*rc = _rc;
458 				*rrc = _rrc;
459 			}
460 		}
461 		list_del(&cur->list);
462 		kfree(cur);
463 	}
464 
465 	/*
466 	 * If the mm still has a mapping, try to mark all its pages as
467 	 * accessible. The counter should not reach zero before this
468 	 * cleanup has been performed.
469 	 */
470 	if (need_zap && mmget_not_zero(kvm->mm)) {
471 		s390_uv_destroy_range(kvm->mm, 0, TASK_SIZE);
472 		mmput(kvm->mm);
473 	}
474 
475 	/* Now the counter can safely reach 0 */
476 	atomic_dec(&kvm->mm->context.protected_count);
477 	return cc ? -EIO : 0;
478 }
479 
480 /**
481  * kvm_s390_pv_deinit_aside_vm - Teardown a previously set aside protected VM.
482  * @kvm: the VM previously associated with the protected VM
483  * @rc: return value for the RC field of the UVCB
484  * @rrc: return value for the RRC field of the UVCB
485  *
486  * Tear down the protected VM that had been previously prepared for teardown
487  * using kvm_s390_pv_set_aside_vm. Ideally this should be called by
488  * userspace asynchronously from a separate thread.
489  *
490  * Context: kvm->lock must not be held.
491  *
492  * Return: 0 in case of success, -EINVAL if no protected VM had been
493  * prepared for asynchronous teardowm, -EIO in case of other errors.
494  */
495 int kvm_s390_pv_deinit_aside_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
496 {
497 	struct pv_vm_to_be_destroyed *p;
498 	int ret = 0;
499 
500 	lockdep_assert_not_held(&kvm->lock);
501 	mutex_lock(&kvm->lock);
502 	p = kvm->arch.pv.set_aside;
503 	kvm->arch.pv.set_aside = NULL;
504 	mutex_unlock(&kvm->lock);
505 	if (!p)
506 		return -EINVAL;
507 
508 	/* When a fatal signal is received, stop immediately */
509 	if (s390_uv_destroy_range_interruptible(kvm->mm, 0, TASK_SIZE_MAX))
510 		goto done;
511 	if (kvm_s390_pv_dispose_one_leftover(kvm, p, rc, rrc))
512 		ret = -EIO;
513 	kfree(p);
514 	p = NULL;
515 done:
516 	/*
517 	 * p is not NULL if we aborted because of a fatal signal, in which
518 	 * case queue the leftover for later cleanup.
519 	 */
520 	if (p) {
521 		mutex_lock(&kvm->lock);
522 		list_add(&p->list, &kvm->arch.pv.need_cleanup);
523 		mutex_unlock(&kvm->lock);
524 		/* Did not finish, but pretend things went well */
525 		*rc = UVC_RC_EXECUTED;
526 		*rrc = 42;
527 	}
528 	return ret;
529 }
530 
531 static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription,
532 					     struct mm_struct *mm)
533 {
534 	struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier);
535 	u16 dummy;
536 	int r;
537 
538 	/*
539 	 * No locking is needed since this is the last thread of the last user of this
540 	 * struct mm.
541 	 * When the struct kvm gets deinitialized, this notifier is also
542 	 * unregistered. This means that if this notifier runs, then the
543 	 * struct kvm is still valid.
544 	 */
545 	r = kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
546 	if (!r && is_destroy_fast_available() && kvm_s390_pv_get_handle(kvm))
547 		kvm_s390_pv_deinit_vm_fast(kvm, &dummy, &dummy);
548 }
549 
550 static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = {
551 	.release = kvm_s390_pv_mmu_notifier_release,
552 };
553 
554 int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
555 {
556 	struct uv_cb_cgc uvcb = {
557 		.header.cmd = UVC_CMD_CREATE_SEC_CONF,
558 		.header.len = sizeof(uvcb)
559 	};
560 	int cc, ret;
561 	u16 dummy;
562 
563 	ret = kvm_s390_pv_alloc_vm(kvm);
564 	if (ret)
565 		return ret;
566 
567 	/* Inputs */
568 	uvcb.guest_stor_origin = 0; /* MSO is 0 for KVM */
569 	uvcb.guest_stor_len = kvm->arch.pv.guest_len;
570 	uvcb.guest_asce = kvm->arch.gmap->asce;
571 	uvcb.guest_sca = virt_to_phys(kvm->arch.sca);
572 	uvcb.conf_base_stor_origin =
573 		virt_to_phys((void *)kvm->arch.pv.stor_base);
574 	uvcb.conf_virt_stor_origin = (u64)kvm->arch.pv.stor_var;
575 
576 	cc = uv_call_sched(0, (u64)&uvcb);
577 	*rc = uvcb.header.rc;
578 	*rrc = uvcb.header.rrc;
579 	KVM_UV_EVENT(kvm, 3, "PROTVIRT CREATE VM: handle %llx len %llx rc %x rrc %x",
580 		     uvcb.guest_handle, uvcb.guest_stor_len, *rc, *rrc);
581 
582 	/* Outputs */
583 	kvm->arch.pv.handle = uvcb.guest_handle;
584 
585 	atomic_inc(&kvm->mm->context.protected_count);
586 	if (cc) {
587 		if (uvcb.header.rc & UVC_RC_NEED_DESTROY) {
588 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
589 		} else {
590 			atomic_dec(&kvm->mm->context.protected_count);
591 			kvm_s390_pv_dealloc_vm(kvm);
592 		}
593 		return -EIO;
594 	}
595 	kvm->arch.gmap->guest_handle = uvcb.guest_handle;
596 	/* Add the notifier only once. No races because we hold kvm->lock */
597 	if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
598 		kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
599 		mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
600 	}
601 	return 0;
602 }
603 
604 int kvm_s390_pv_set_sec_parms(struct kvm *kvm, void *hdr, u64 length, u16 *rc,
605 			      u16 *rrc)
606 {
607 	struct uv_cb_ssc uvcb = {
608 		.header.cmd = UVC_CMD_SET_SEC_CONF_PARAMS,
609 		.header.len = sizeof(uvcb),
610 		.sec_header_origin = (u64)hdr,
611 		.sec_header_len = length,
612 		.guest_handle = kvm_s390_pv_get_handle(kvm),
613 	};
614 	int cc = uv_call(0, (u64)&uvcb);
615 
616 	*rc = uvcb.header.rc;
617 	*rrc = uvcb.header.rrc;
618 	KVM_UV_EVENT(kvm, 3, "PROTVIRT VM SET PARMS: rc %x rrc %x",
619 		     *rc, *rrc);
620 	return cc ? -EINVAL : 0;
621 }
622 
623 static int unpack_one(struct kvm *kvm, unsigned long addr, u64 tweak,
624 		      u64 offset, u16 *rc, u16 *rrc)
625 {
626 	struct uv_cb_unp uvcb = {
627 		.header.cmd = UVC_CMD_UNPACK_IMG,
628 		.header.len = sizeof(uvcb),
629 		.guest_handle = kvm_s390_pv_get_handle(kvm),
630 		.gaddr = addr,
631 		.tweak[0] = tweak,
632 		.tweak[1] = offset,
633 	};
634 	int ret = gmap_make_secure(kvm->arch.gmap, addr, &uvcb);
635 
636 	*rc = uvcb.header.rc;
637 	*rrc = uvcb.header.rrc;
638 
639 	if (ret && ret != -EAGAIN)
640 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VM UNPACK: failed addr %llx with rc %x rrc %x",
641 			     uvcb.gaddr, *rc, *rrc);
642 	return ret;
643 }
644 
645 int kvm_s390_pv_unpack(struct kvm *kvm, unsigned long addr, unsigned long size,
646 		       unsigned long tweak, u16 *rc, u16 *rrc)
647 {
648 	u64 offset = 0;
649 	int ret = 0;
650 
651 	if (addr & ~PAGE_MASK || !size || size & ~PAGE_MASK)
652 		return -EINVAL;
653 
654 	KVM_UV_EVENT(kvm, 3, "PROTVIRT VM UNPACK: start addr %lx size %lx",
655 		     addr, size);
656 
657 	while (offset < size) {
658 		ret = unpack_one(kvm, addr, tweak, offset, rc, rrc);
659 		if (ret == -EAGAIN) {
660 			cond_resched();
661 			if (fatal_signal_pending(current))
662 				break;
663 			continue;
664 		}
665 		if (ret)
666 			break;
667 		addr += PAGE_SIZE;
668 		offset += PAGE_SIZE;
669 	}
670 	if (!ret)
671 		KVM_UV_EVENT(kvm, 3, "%s", "PROTVIRT VM UNPACK: successful");
672 	return ret;
673 }
674 
675 int kvm_s390_pv_set_cpu_state(struct kvm_vcpu *vcpu, u8 state)
676 {
677 	struct uv_cb_cpu_set_state uvcb = {
678 		.header.cmd	= UVC_CMD_CPU_SET_STATE,
679 		.header.len	= sizeof(uvcb),
680 		.cpu_handle	= kvm_s390_pv_cpu_get_handle(vcpu),
681 		.state		= state,
682 	};
683 	int cc;
684 
685 	cc = uv_call(0, (u64)&uvcb);
686 	KVM_UV_EVENT(vcpu->kvm, 3, "PROTVIRT SET CPU %d STATE %d rc %x rrc %x",
687 		     vcpu->vcpu_id, state, uvcb.header.rc, uvcb.header.rrc);
688 	if (cc)
689 		return -EINVAL;
690 	return 0;
691 }
692 
693 int kvm_s390_pv_dump_cpu(struct kvm_vcpu *vcpu, void *buff, u16 *rc, u16 *rrc)
694 {
695 	struct uv_cb_dump_cpu uvcb = {
696 		.header.cmd = UVC_CMD_DUMP_CPU,
697 		.header.len = sizeof(uvcb),
698 		.cpu_handle = vcpu->arch.pv.handle,
699 		.dump_area_origin = (u64)buff,
700 	};
701 	int cc;
702 
703 	cc = uv_call_sched(0, (u64)&uvcb);
704 	*rc = uvcb.header.rc;
705 	*rrc = uvcb.header.rrc;
706 	return cc;
707 }
708 
709 /* Size of the cache for the storage state dump data. 1MB for now */
710 #define DUMP_BUFF_LEN HPAGE_SIZE
711 
712 /**
713  * kvm_s390_pv_dump_stor_state
714  *
715  * @kvm: pointer to the guest's KVM struct
716  * @buff_user: Userspace pointer where we will write the results to
717  * @gaddr: Starting absolute guest address for which the storage state
718  *	   is requested.
719  * @buff_user_len: Length of the buff_user buffer
720  * @rc: Pointer to where the uvcb return code is stored
721  * @rrc: Pointer to where the uvcb return reason code is stored
722  *
723  * Stores buff_len bytes of tweak component values to buff_user
724  * starting with the 1MB block specified by the absolute guest address
725  * (gaddr). The gaddr pointer will be updated with the last address
726  * for which data was written when returning to userspace. buff_user
727  * might be written to even if an error rc is returned. For instance
728  * if we encounter a fault after writing the first page of data.
729  *
730  * Context: kvm->lock needs to be held
731  *
732  * Return:
733  *  0 on success
734  *  -ENOMEM if allocating the cache fails
735  *  -EINVAL if gaddr is not aligned to 1MB
736  *  -EINVAL if buff_user_len is not aligned to uv_info.conf_dump_storage_state_len
737  *  -EINVAL if the UV call fails, rc and rrc will be set in this case
738  *  -EFAULT if copying the result to buff_user failed
739  */
740 int kvm_s390_pv_dump_stor_state(struct kvm *kvm, void __user *buff_user,
741 				u64 *gaddr, u64 buff_user_len, u16 *rc, u16 *rrc)
742 {
743 	struct uv_cb_dump_stor_state uvcb = {
744 		.header.cmd = UVC_CMD_DUMP_CONF_STOR_STATE,
745 		.header.len = sizeof(uvcb),
746 		.config_handle = kvm->arch.pv.handle,
747 		.gaddr = *gaddr,
748 		.dump_area_origin = 0,
749 	};
750 	const u64 increment_len = uv_info.conf_dump_storage_state_len;
751 	size_t buff_kvm_size;
752 	size_t size_done = 0;
753 	u8 *buff_kvm = NULL;
754 	int cc, ret;
755 
756 	ret = -EINVAL;
757 	/* UV call processes 1MB guest storage chunks at a time */
758 	if (!IS_ALIGNED(*gaddr, HPAGE_SIZE))
759 		goto out;
760 
761 	/*
762 	 * We provide the storage state for 1MB chunks of guest
763 	 * storage. The buffer will need to be aligned to
764 	 * conf_dump_storage_state_len so we don't end on a partial
765 	 * chunk.
766 	 */
767 	if (!buff_user_len ||
768 	    !IS_ALIGNED(buff_user_len, increment_len))
769 		goto out;
770 
771 	/*
772 	 * Allocate a buffer from which we will later copy to the user
773 	 * process. We don't want userspace to dictate our buffer size
774 	 * so we limit it to DUMP_BUFF_LEN.
775 	 */
776 	ret = -ENOMEM;
777 	buff_kvm_size = min_t(u64, buff_user_len, DUMP_BUFF_LEN);
778 	buff_kvm = vzalloc(buff_kvm_size);
779 	if (!buff_kvm)
780 		goto out;
781 
782 	ret = 0;
783 	uvcb.dump_area_origin = (u64)buff_kvm;
784 	/* We will loop until the user buffer is filled or an error occurs */
785 	do {
786 		/* Get 1MB worth of guest storage state data */
787 		cc = uv_call_sched(0, (u64)&uvcb);
788 
789 		/* All or nothing */
790 		if (cc) {
791 			ret = -EINVAL;
792 			break;
793 		}
794 
795 		size_done += increment_len;
796 		uvcb.dump_area_origin += increment_len;
797 		buff_user_len -= increment_len;
798 		uvcb.gaddr += HPAGE_SIZE;
799 
800 		/* KVM Buffer full, time to copy to the process */
801 		if (!buff_user_len || size_done == DUMP_BUFF_LEN) {
802 			if (copy_to_user(buff_user, buff_kvm, size_done)) {
803 				ret = -EFAULT;
804 				break;
805 			}
806 
807 			buff_user += size_done;
808 			size_done = 0;
809 			uvcb.dump_area_origin = (u64)buff_kvm;
810 		}
811 	} while (buff_user_len);
812 
813 	/* Report back where we ended dumping */
814 	*gaddr = uvcb.gaddr;
815 
816 	/* Lets only log errors, we don't want to spam */
817 out:
818 	if (ret)
819 		KVM_UV_EVENT(kvm, 3,
820 			     "PROTVIRT DUMP STORAGE STATE: addr %llx ret %d, uvcb rc %x rrc %x",
821 			     uvcb.gaddr, ret, uvcb.header.rc, uvcb.header.rrc);
822 	*rc = uvcb.header.rc;
823 	*rrc = uvcb.header.rrc;
824 	vfree(buff_kvm);
825 
826 	return ret;
827 }
828 
829 /**
830  * kvm_s390_pv_dump_complete
831  *
832  * @kvm: pointer to the guest's KVM struct
833  * @buff_user: Userspace pointer where we will write the results to
834  * @rc: Pointer to where the uvcb return code is stored
835  * @rrc: Pointer to where the uvcb return reason code is stored
836  *
837  * Completes the dumping operation and writes the completion data to
838  * user space.
839  *
840  * Context: kvm->lock needs to be held
841  *
842  * Return:
843  *  0 on success
844  *  -ENOMEM if allocating the completion buffer fails
845  *  -EINVAL if the UV call fails, rc and rrc will be set in this case
846  *  -EFAULT if copying the result to buff_user failed
847  */
848 int kvm_s390_pv_dump_complete(struct kvm *kvm, void __user *buff_user,
849 			      u16 *rc, u16 *rrc)
850 {
851 	struct uv_cb_dump_complete complete = {
852 		.header.len = sizeof(complete),
853 		.header.cmd = UVC_CMD_DUMP_COMPLETE,
854 		.config_handle = kvm_s390_pv_get_handle(kvm),
855 	};
856 	u64 *compl_data;
857 	int ret;
858 
859 	/* Allocate dump area */
860 	compl_data = vzalloc(uv_info.conf_dump_finalize_len);
861 	if (!compl_data)
862 		return -ENOMEM;
863 	complete.dump_area_origin = (u64)compl_data;
864 
865 	ret = uv_call_sched(0, (u64)&complete);
866 	*rc = complete.header.rc;
867 	*rrc = complete.header.rrc;
868 	KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP COMPLETE: rc %x rrc %x",
869 		     complete.header.rc, complete.header.rrc);
870 
871 	if (!ret) {
872 		/*
873 		 * kvm_s390_pv_dealloc_vm() will also (mem)set
874 		 * this to false on a reboot or other destroy
875 		 * operation for this vm.
876 		 */
877 		kvm->arch.pv.dumping = false;
878 		kvm_s390_vcpu_unblock_all(kvm);
879 		ret = copy_to_user(buff_user, compl_data, uv_info.conf_dump_finalize_len);
880 		if (ret)
881 			ret = -EFAULT;
882 	}
883 	vfree(compl_data);
884 	/* If the UVC returned an error, translate it to -EINVAL */
885 	if (ret > 0)
886 		ret = -EINVAL;
887 	return ret;
888 }
889