xref: /openbmc/linux/arch/s390/kvm/pv.c (revision cd99b9eb)
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, "protvirt destroy vm fast failed handle %llx rc %x rrc %x",
289 		  kvm_s390_pv_get_handle(kvm), uvcb.header.rc, uvcb.header.rrc);
290 	/* Intended memory leak on "impossible" error */
291 	if (!cc)
292 		kvm_s390_pv_dealloc_vm(kvm);
293 	return cc ? -EIO : 0;
294 }
295 
296 static inline bool is_destroy_fast_available(void)
297 {
298 	return test_bit_inv(BIT_UVC_CMD_DESTROY_SEC_CONF_FAST, uv_info.inst_calls_list);
299 }
300 
301 /**
302  * kvm_s390_pv_set_aside - Set aside a protected VM for later teardown.
303  * @kvm: the VM
304  * @rc: return value for the RC field of the UVCB
305  * @rrc: return value for the RRC field of the UVCB
306  *
307  * Set aside the protected VM for a subsequent teardown. The VM will be able
308  * to continue immediately as a non-secure VM, and the information needed to
309  * properly tear down the protected VM is set aside. If another protected VM
310  * was already set aside without starting its teardown, this function will
311  * fail.
312  * The CPUs of the protected VM need to be destroyed beforehand.
313  *
314  * Context: kvm->lock needs to be held
315  *
316  * Return: 0 in case of success, -EINVAL if another protected VM was already set
317  * aside, -ENOMEM if the system ran out of memory.
318  */
319 int kvm_s390_pv_set_aside(struct kvm *kvm, u16 *rc, u16 *rrc)
320 {
321 	struct pv_vm_to_be_destroyed *priv;
322 	int res = 0;
323 
324 	lockdep_assert_held(&kvm->lock);
325 	/*
326 	 * If another protected VM was already prepared for teardown, refuse.
327 	 * A normal deinitialization has to be performed instead.
328 	 */
329 	if (kvm->arch.pv.set_aside)
330 		return -EINVAL;
331 
332 	/* Guest with segment type ASCE, refuse to destroy asynchronously */
333 	if ((kvm->arch.gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
334 		return -EINVAL;
335 
336 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
337 	if (!priv)
338 		return -ENOMEM;
339 
340 	if (is_destroy_fast_available()) {
341 		res = kvm_s390_pv_deinit_vm_fast(kvm, rc, rrc);
342 	} else {
343 		priv->stor_var = kvm->arch.pv.stor_var;
344 		priv->stor_base = kvm->arch.pv.stor_base;
345 		priv->handle = kvm_s390_pv_get_handle(kvm);
346 		priv->old_gmap_table = (unsigned long)kvm->arch.gmap->table;
347 		WRITE_ONCE(kvm->arch.gmap->guest_handle, 0);
348 		if (s390_replace_asce(kvm->arch.gmap))
349 			res = -ENOMEM;
350 	}
351 
352 	if (res) {
353 		kfree(priv);
354 		return res;
355 	}
356 
357 	kvm_s390_destroy_lower_2g(kvm);
358 	kvm_s390_clear_pv_state(kvm);
359 	kvm->arch.pv.set_aside = priv;
360 
361 	*rc = UVC_RC_EXECUTED;
362 	*rrc = 42;
363 	return 0;
364 }
365 
366 /**
367  * kvm_s390_pv_deinit_vm - Deinitialize the current protected VM
368  * @kvm: the KVM whose protected VM needs to be deinitialized
369  * @rc: the RC code of the UVC
370  * @rrc: the RRC code of the UVC
371  *
372  * Deinitialize the current protected VM. This function will destroy and
373  * cleanup the current protected VM, but it will not cleanup the guest
374  * memory. This function should only be called when the protected VM has
375  * just been created and therefore does not have any guest memory, or when
376  * the caller cleans up the guest memory separately.
377  *
378  * This function should not fail, but if it does, the donated memory must
379  * not be freed.
380  *
381  * Context: kvm->lock needs to be held
382  *
383  * Return: 0 in case of success, otherwise -EIO
384  */
385 int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
386 {
387 	int cc;
388 
389 	cc = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
390 			   UVC_CMD_DESTROY_SEC_CONF, rc, rrc);
391 	WRITE_ONCE(kvm->arch.gmap->guest_handle, 0);
392 	if (!cc) {
393 		atomic_dec(&kvm->mm->context.protected_count);
394 		kvm_s390_pv_dealloc_vm(kvm);
395 	} else {
396 		/* Intended memory leak on "impossible" error */
397 		s390_replace_asce(kvm->arch.gmap);
398 	}
399 	KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY VM: rc %x rrc %x", *rc, *rrc);
400 	WARN_ONCE(cc, "protvirt destroy vm failed rc %x rrc %x", *rc, *rrc);
401 
402 	return cc ? -EIO : 0;
403 }
404 
405 /**
406  * kvm_s390_pv_deinit_cleanup_all - Clean up all protected VMs associated
407  * with a specific KVM.
408  * @kvm: the KVM to be cleaned up
409  * @rc: the RC code of the first failing UVC
410  * @rrc: the RRC code of the first failing UVC
411  *
412  * This function will clean up all protected VMs associated with a KVM.
413  * This includes the active one, the one prepared for deinitialization with
414  * kvm_s390_pv_set_aside, and any still pending in the need_cleanup list.
415  *
416  * Context: kvm->lock needs to be held unless being called from
417  * kvm_arch_destroy_vm.
418  *
419  * Return: 0 if all VMs are successfully cleaned up, otherwise -EIO
420  */
421 int kvm_s390_pv_deinit_cleanup_all(struct kvm *kvm, u16 *rc, u16 *rrc)
422 {
423 	struct pv_vm_to_be_destroyed *cur;
424 	bool need_zap = false;
425 	u16 _rc, _rrc;
426 	int cc = 0;
427 
428 	/*
429 	 * Nothing to do if the counter was already 0. Otherwise make sure
430 	 * the counter does not reach 0 before calling s390_uv_destroy_range.
431 	 */
432 	if (!atomic_inc_not_zero(&kvm->mm->context.protected_count))
433 		return 0;
434 
435 	*rc = 1;
436 	/* If the current VM is protected, destroy it */
437 	if (kvm_s390_pv_get_handle(kvm)) {
438 		cc = kvm_s390_pv_deinit_vm(kvm, rc, rrc);
439 		need_zap = true;
440 	}
441 
442 	/* If a previous protected VM was set aside, put it in the need_cleanup list */
443 	if (kvm->arch.pv.set_aside) {
444 		list_add(kvm->arch.pv.set_aside, &kvm->arch.pv.need_cleanup);
445 		kvm->arch.pv.set_aside = NULL;
446 	}
447 
448 	/* Cleanup all protected VMs in the need_cleanup list */
449 	while (!list_empty(&kvm->arch.pv.need_cleanup)) {
450 		cur = list_first_entry(&kvm->arch.pv.need_cleanup, typeof(*cur), list);
451 		need_zap = true;
452 		if (kvm_s390_pv_dispose_one_leftover(kvm, cur, &_rc, &_rrc)) {
453 			cc = 1;
454 			/*
455 			 * Only return the first error rc and rrc, so make
456 			 * sure it is not overwritten. All destroys will
457 			 * additionally be reported via KVM_UV_EVENT().
458 			 */
459 			if (*rc == UVC_RC_EXECUTED) {
460 				*rc = _rc;
461 				*rrc = _rrc;
462 			}
463 		}
464 		list_del(&cur->list);
465 		kfree(cur);
466 	}
467 
468 	/*
469 	 * If the mm still has a mapping, try to mark all its pages as
470 	 * accessible. The counter should not reach zero before this
471 	 * cleanup has been performed.
472 	 */
473 	if (need_zap && mmget_not_zero(kvm->mm)) {
474 		s390_uv_destroy_range(kvm->mm, 0, TASK_SIZE);
475 		mmput(kvm->mm);
476 	}
477 
478 	/* Now the counter can safely reach 0 */
479 	atomic_dec(&kvm->mm->context.protected_count);
480 	return cc ? -EIO : 0;
481 }
482 
483 /**
484  * kvm_s390_pv_deinit_aside_vm - Teardown a previously set aside protected VM.
485  * @kvm: the VM previously associated with the protected VM
486  * @rc: return value for the RC field of the UVCB
487  * @rrc: return value for the RRC field of the UVCB
488  *
489  * Tear down the protected VM that had been previously prepared for teardown
490  * using kvm_s390_pv_set_aside_vm. Ideally this should be called by
491  * userspace asynchronously from a separate thread.
492  *
493  * Context: kvm->lock must not be held.
494  *
495  * Return: 0 in case of success, -EINVAL if no protected VM had been
496  * prepared for asynchronous teardowm, -EIO in case of other errors.
497  */
498 int kvm_s390_pv_deinit_aside_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
499 {
500 	struct pv_vm_to_be_destroyed *p;
501 	int ret = 0;
502 
503 	lockdep_assert_not_held(&kvm->lock);
504 	mutex_lock(&kvm->lock);
505 	p = kvm->arch.pv.set_aside;
506 	kvm->arch.pv.set_aside = NULL;
507 	mutex_unlock(&kvm->lock);
508 	if (!p)
509 		return -EINVAL;
510 
511 	/* When a fatal signal is received, stop immediately */
512 	if (s390_uv_destroy_range_interruptible(kvm->mm, 0, TASK_SIZE_MAX))
513 		goto done;
514 	if (kvm_s390_pv_dispose_one_leftover(kvm, p, rc, rrc))
515 		ret = -EIO;
516 	kfree(p);
517 	p = NULL;
518 done:
519 	/*
520 	 * p is not NULL if we aborted because of a fatal signal, in which
521 	 * case queue the leftover for later cleanup.
522 	 */
523 	if (p) {
524 		mutex_lock(&kvm->lock);
525 		list_add(&p->list, &kvm->arch.pv.need_cleanup);
526 		mutex_unlock(&kvm->lock);
527 		/* Did not finish, but pretend things went well */
528 		*rc = UVC_RC_EXECUTED;
529 		*rrc = 42;
530 	}
531 	return ret;
532 }
533 
534 static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription,
535 					     struct mm_struct *mm)
536 {
537 	struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier);
538 	u16 dummy;
539 	int r;
540 
541 	/*
542 	 * No locking is needed since this is the last thread of the last user of this
543 	 * struct mm.
544 	 * When the struct kvm gets deinitialized, this notifier is also
545 	 * unregistered. This means that if this notifier runs, then the
546 	 * struct kvm is still valid.
547 	 */
548 	r = kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
549 	if (!r && is_destroy_fast_available() && kvm_s390_pv_get_handle(kvm))
550 		kvm_s390_pv_deinit_vm_fast(kvm, &dummy, &dummy);
551 }
552 
553 static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = {
554 	.release = kvm_s390_pv_mmu_notifier_release,
555 };
556 
557 int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
558 {
559 	struct uv_cb_cgc uvcb = {
560 		.header.cmd = UVC_CMD_CREATE_SEC_CONF,
561 		.header.len = sizeof(uvcb)
562 	};
563 	int cc, ret;
564 	u16 dummy;
565 
566 	ret = kvm_s390_pv_alloc_vm(kvm);
567 	if (ret)
568 		return ret;
569 
570 	/* Inputs */
571 	uvcb.guest_stor_origin = 0; /* MSO is 0 for KVM */
572 	uvcb.guest_stor_len = kvm->arch.pv.guest_len;
573 	uvcb.guest_asce = kvm->arch.gmap->asce;
574 	uvcb.guest_sca = virt_to_phys(kvm->arch.sca);
575 	uvcb.conf_base_stor_origin =
576 		virt_to_phys((void *)kvm->arch.pv.stor_base);
577 	uvcb.conf_virt_stor_origin = (u64)kvm->arch.pv.stor_var;
578 
579 	cc = uv_call_sched(0, (u64)&uvcb);
580 	*rc = uvcb.header.rc;
581 	*rrc = uvcb.header.rrc;
582 	KVM_UV_EVENT(kvm, 3, "PROTVIRT CREATE VM: handle %llx len %llx rc %x rrc %x",
583 		     uvcb.guest_handle, uvcb.guest_stor_len, *rc, *rrc);
584 
585 	/* Outputs */
586 	kvm->arch.pv.handle = uvcb.guest_handle;
587 
588 	atomic_inc(&kvm->mm->context.protected_count);
589 	if (cc) {
590 		if (uvcb.header.rc & UVC_RC_NEED_DESTROY) {
591 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
592 		} else {
593 			atomic_dec(&kvm->mm->context.protected_count);
594 			kvm_s390_pv_dealloc_vm(kvm);
595 		}
596 		return -EIO;
597 	}
598 	kvm->arch.gmap->guest_handle = uvcb.guest_handle;
599 	/* Add the notifier only once. No races because we hold kvm->lock */
600 	if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
601 		kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
602 		mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
603 	}
604 	return 0;
605 }
606 
607 int kvm_s390_pv_set_sec_parms(struct kvm *kvm, void *hdr, u64 length, u16 *rc,
608 			      u16 *rrc)
609 {
610 	struct uv_cb_ssc uvcb = {
611 		.header.cmd = UVC_CMD_SET_SEC_CONF_PARAMS,
612 		.header.len = sizeof(uvcb),
613 		.sec_header_origin = (u64)hdr,
614 		.sec_header_len = length,
615 		.guest_handle = kvm_s390_pv_get_handle(kvm),
616 	};
617 	int cc = uv_call(0, (u64)&uvcb);
618 
619 	*rc = uvcb.header.rc;
620 	*rrc = uvcb.header.rrc;
621 	KVM_UV_EVENT(kvm, 3, "PROTVIRT VM SET PARMS: rc %x rrc %x",
622 		     *rc, *rrc);
623 	return cc ? -EINVAL : 0;
624 }
625 
626 static int unpack_one(struct kvm *kvm, unsigned long addr, u64 tweak,
627 		      u64 offset, u16 *rc, u16 *rrc)
628 {
629 	struct uv_cb_unp uvcb = {
630 		.header.cmd = UVC_CMD_UNPACK_IMG,
631 		.header.len = sizeof(uvcb),
632 		.guest_handle = kvm_s390_pv_get_handle(kvm),
633 		.gaddr = addr,
634 		.tweak[0] = tweak,
635 		.tweak[1] = offset,
636 	};
637 	int ret = gmap_make_secure(kvm->arch.gmap, addr, &uvcb);
638 
639 	*rc = uvcb.header.rc;
640 	*rrc = uvcb.header.rrc;
641 
642 	if (ret && ret != -EAGAIN)
643 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VM UNPACK: failed addr %llx with rc %x rrc %x",
644 			     uvcb.gaddr, *rc, *rrc);
645 	return ret;
646 }
647 
648 int kvm_s390_pv_unpack(struct kvm *kvm, unsigned long addr, unsigned long size,
649 		       unsigned long tweak, u16 *rc, u16 *rrc)
650 {
651 	u64 offset = 0;
652 	int ret = 0;
653 
654 	if (addr & ~PAGE_MASK || !size || size & ~PAGE_MASK)
655 		return -EINVAL;
656 
657 	KVM_UV_EVENT(kvm, 3, "PROTVIRT VM UNPACK: start addr %lx size %lx",
658 		     addr, size);
659 
660 	while (offset < size) {
661 		ret = unpack_one(kvm, addr, tweak, offset, rc, rrc);
662 		if (ret == -EAGAIN) {
663 			cond_resched();
664 			if (fatal_signal_pending(current))
665 				break;
666 			continue;
667 		}
668 		if (ret)
669 			break;
670 		addr += PAGE_SIZE;
671 		offset += PAGE_SIZE;
672 	}
673 	if (!ret)
674 		KVM_UV_EVENT(kvm, 3, "%s", "PROTVIRT VM UNPACK: successful");
675 	return ret;
676 }
677 
678 int kvm_s390_pv_set_cpu_state(struct kvm_vcpu *vcpu, u8 state)
679 {
680 	struct uv_cb_cpu_set_state uvcb = {
681 		.header.cmd	= UVC_CMD_CPU_SET_STATE,
682 		.header.len	= sizeof(uvcb),
683 		.cpu_handle	= kvm_s390_pv_cpu_get_handle(vcpu),
684 		.state		= state,
685 	};
686 	int cc;
687 
688 	cc = uv_call(0, (u64)&uvcb);
689 	KVM_UV_EVENT(vcpu->kvm, 3, "PROTVIRT SET CPU %d STATE %d rc %x rrc %x",
690 		     vcpu->vcpu_id, state, uvcb.header.rc, uvcb.header.rrc);
691 	if (cc)
692 		return -EINVAL;
693 	return 0;
694 }
695 
696 int kvm_s390_pv_dump_cpu(struct kvm_vcpu *vcpu, void *buff, u16 *rc, u16 *rrc)
697 {
698 	struct uv_cb_dump_cpu uvcb = {
699 		.header.cmd = UVC_CMD_DUMP_CPU,
700 		.header.len = sizeof(uvcb),
701 		.cpu_handle = vcpu->arch.pv.handle,
702 		.dump_area_origin = (u64)buff,
703 	};
704 	int cc;
705 
706 	cc = uv_call_sched(0, (u64)&uvcb);
707 	*rc = uvcb.header.rc;
708 	*rrc = uvcb.header.rrc;
709 	return cc;
710 }
711 
712 /* Size of the cache for the storage state dump data. 1MB for now */
713 #define DUMP_BUFF_LEN HPAGE_SIZE
714 
715 /**
716  * kvm_s390_pv_dump_stor_state
717  *
718  * @kvm: pointer to the guest's KVM struct
719  * @buff_user: Userspace pointer where we will write the results to
720  * @gaddr: Starting absolute guest address for which the storage state
721  *	   is requested.
722  * @buff_user_len: Length of the buff_user buffer
723  * @rc: Pointer to where the uvcb return code is stored
724  * @rrc: Pointer to where the uvcb return reason code is stored
725  *
726  * Stores buff_len bytes of tweak component values to buff_user
727  * starting with the 1MB block specified by the absolute guest address
728  * (gaddr). The gaddr pointer will be updated with the last address
729  * for which data was written when returning to userspace. buff_user
730  * might be written to even if an error rc is returned. For instance
731  * if we encounter a fault after writing the first page of data.
732  *
733  * Context: kvm->lock needs to be held
734  *
735  * Return:
736  *  0 on success
737  *  -ENOMEM if allocating the cache fails
738  *  -EINVAL if gaddr is not aligned to 1MB
739  *  -EINVAL if buff_user_len is not aligned to uv_info.conf_dump_storage_state_len
740  *  -EINVAL if the UV call fails, rc and rrc will be set in this case
741  *  -EFAULT if copying the result to buff_user failed
742  */
743 int kvm_s390_pv_dump_stor_state(struct kvm *kvm, void __user *buff_user,
744 				u64 *gaddr, u64 buff_user_len, u16 *rc, u16 *rrc)
745 {
746 	struct uv_cb_dump_stor_state uvcb = {
747 		.header.cmd = UVC_CMD_DUMP_CONF_STOR_STATE,
748 		.header.len = sizeof(uvcb),
749 		.config_handle = kvm->arch.pv.handle,
750 		.gaddr = *gaddr,
751 		.dump_area_origin = 0,
752 	};
753 	const u64 increment_len = uv_info.conf_dump_storage_state_len;
754 	size_t buff_kvm_size;
755 	size_t size_done = 0;
756 	u8 *buff_kvm = NULL;
757 	int cc, ret;
758 
759 	ret = -EINVAL;
760 	/* UV call processes 1MB guest storage chunks at a time */
761 	if (!IS_ALIGNED(*gaddr, HPAGE_SIZE))
762 		goto out;
763 
764 	/*
765 	 * We provide the storage state for 1MB chunks of guest
766 	 * storage. The buffer will need to be aligned to
767 	 * conf_dump_storage_state_len so we don't end on a partial
768 	 * chunk.
769 	 */
770 	if (!buff_user_len ||
771 	    !IS_ALIGNED(buff_user_len, increment_len))
772 		goto out;
773 
774 	/*
775 	 * Allocate a buffer from which we will later copy to the user
776 	 * process. We don't want userspace to dictate our buffer size
777 	 * so we limit it to DUMP_BUFF_LEN.
778 	 */
779 	ret = -ENOMEM;
780 	buff_kvm_size = min_t(u64, buff_user_len, DUMP_BUFF_LEN);
781 	buff_kvm = vzalloc(buff_kvm_size);
782 	if (!buff_kvm)
783 		goto out;
784 
785 	ret = 0;
786 	uvcb.dump_area_origin = (u64)buff_kvm;
787 	/* We will loop until the user buffer is filled or an error occurs */
788 	do {
789 		/* Get 1MB worth of guest storage state data */
790 		cc = uv_call_sched(0, (u64)&uvcb);
791 
792 		/* All or nothing */
793 		if (cc) {
794 			ret = -EINVAL;
795 			break;
796 		}
797 
798 		size_done += increment_len;
799 		uvcb.dump_area_origin += increment_len;
800 		buff_user_len -= increment_len;
801 		uvcb.gaddr += HPAGE_SIZE;
802 
803 		/* KVM Buffer full, time to copy to the process */
804 		if (!buff_user_len || size_done == DUMP_BUFF_LEN) {
805 			if (copy_to_user(buff_user, buff_kvm, size_done)) {
806 				ret = -EFAULT;
807 				break;
808 			}
809 
810 			buff_user += size_done;
811 			size_done = 0;
812 			uvcb.dump_area_origin = (u64)buff_kvm;
813 		}
814 	} while (buff_user_len);
815 
816 	/* Report back where we ended dumping */
817 	*gaddr = uvcb.gaddr;
818 
819 	/* Lets only log errors, we don't want to spam */
820 out:
821 	if (ret)
822 		KVM_UV_EVENT(kvm, 3,
823 			     "PROTVIRT DUMP STORAGE STATE: addr %llx ret %d, uvcb rc %x rrc %x",
824 			     uvcb.gaddr, ret, uvcb.header.rc, uvcb.header.rrc);
825 	*rc = uvcb.header.rc;
826 	*rrc = uvcb.header.rrc;
827 	vfree(buff_kvm);
828 
829 	return ret;
830 }
831 
832 /**
833  * kvm_s390_pv_dump_complete
834  *
835  * @kvm: pointer to the guest's KVM struct
836  * @buff_user: Userspace pointer where we will write the results to
837  * @rc: Pointer to where the uvcb return code is stored
838  * @rrc: Pointer to where the uvcb return reason code is stored
839  *
840  * Completes the dumping operation and writes the completion data to
841  * user space.
842  *
843  * Context: kvm->lock needs to be held
844  *
845  * Return:
846  *  0 on success
847  *  -ENOMEM if allocating the completion buffer fails
848  *  -EINVAL if the UV call fails, rc and rrc will be set in this case
849  *  -EFAULT if copying the result to buff_user failed
850  */
851 int kvm_s390_pv_dump_complete(struct kvm *kvm, void __user *buff_user,
852 			      u16 *rc, u16 *rrc)
853 {
854 	struct uv_cb_dump_complete complete = {
855 		.header.len = sizeof(complete),
856 		.header.cmd = UVC_CMD_DUMP_COMPLETE,
857 		.config_handle = kvm_s390_pv_get_handle(kvm),
858 	};
859 	u64 *compl_data;
860 	int ret;
861 
862 	/* Allocate dump area */
863 	compl_data = vzalloc(uv_info.conf_dump_finalize_len);
864 	if (!compl_data)
865 		return -ENOMEM;
866 	complete.dump_area_origin = (u64)compl_data;
867 
868 	ret = uv_call_sched(0, (u64)&complete);
869 	*rc = complete.header.rc;
870 	*rrc = complete.header.rrc;
871 	KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP COMPLETE: rc %x rrc %x",
872 		     complete.header.rc, complete.header.rrc);
873 
874 	if (!ret) {
875 		/*
876 		 * kvm_s390_pv_dealloc_vm() will also (mem)set
877 		 * this to false on a reboot or other destroy
878 		 * operation for this vm.
879 		 */
880 		kvm->arch.pv.dumping = false;
881 		kvm_s390_vcpu_unblock_all(kvm);
882 		ret = copy_to_user(buff_user, compl_data, uv_info.conf_dump_finalize_len);
883 		if (ret)
884 			ret = -EFAULT;
885 	}
886 	vfree(compl_data);
887 	/* If the UVC returned an error, translate it to -EINVAL */
888 	if (ret > 0)
889 		ret = -EINVAL;
890 	return ret;
891 }
892