Greg Kroah-Hartman | d809aa2 | 2017-11-24 15:00:33 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2 | /* |
Christian Borntraeger | bb64da9 | 2017-11-21 16:02:52 +0100 | [diff] [blame] | 3 | * hosting IBM Z kernel virtual machines (s390x) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4 | * |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 5 | * Copyright IBM Corp. 2008, 2020 |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 8 | * Christian Borntraeger <borntraeger@de.ibm.com> |
| 9 | * Heiko Carstens <heiko.carstens@de.ibm.com> |
Christian Ehrhardt | 628eb9b | 2009-05-25 13:40:51 +0200 | [diff] [blame] | 10 | * Christian Ehrhardt <ehrhardt@de.ibm.com> |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 11 | * Jason J. Herne <jjherne@us.ibm.com> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
Michael Mueller | 7aedd9d | 2018-12-03 10:20:22 +0100 | [diff] [blame] | 14 | #define KMSG_COMPONENT "kvm-s390" |
| 15 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 16 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 17 | #include <linux/compiler.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/fs.h> |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 20 | #include <linux/hrtimer.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/kvm.h> |
| 23 | #include <linux/kvm_host.h> |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 24 | #include <linux/mman.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 25 | #include <linux/module.h> |
Paul Gortmaker | d321796 | 2017-02-09 15:20:25 -0500 | [diff] [blame] | 26 | #include <linux/moduleparam.h> |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 27 | #include <linux/random.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 28 | #include <linux/slab.h> |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 29 | #include <linux/timer.h> |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 31 | #include <linux/bitmap.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 32 | #include <linux/sched/signal.h> |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 33 | #include <linux/string.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 34 | |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 35 | #include <asm/asm-offsets.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 36 | #include <asm/lowcore.h> |
Martin Schwidefsky | fd5ada0 | 2016-05-31 15:06:51 +0200 | [diff] [blame] | 37 | #include <asm/stp.h> |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 38 | #include <asm/pgtable.h> |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 39 | #include <asm/gmap.h> |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 40 | #include <asm/nmi.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 41 | #include <asm/switch_to.h> |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 42 | #include <asm/isc.h> |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 43 | #include <asm/sclp.h> |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 44 | #include <asm/cpacf.h> |
Linus Torvalds | 221bb8a | 2016-08-02 16:11:27 -0400 | [diff] [blame] | 45 | #include <asm/timex.h> |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 46 | #include <asm/ap.h> |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 47 | #include <asm/uv.h> |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 48 | #include "kvm-s390.h" |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 49 | #include "gaccess.h" |
| 50 | |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 51 | #define CREATE_TRACE_POINTS |
| 52 | #include "trace.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 53 | #include "trace-s390.h" |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 54 | |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 55 | #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 56 | #define LOCAL_IRQS 32 |
| 57 | #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ |
| 58 | (KVM_MAX_VCPUS + LOCAL_IRQS)) |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 59 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 60 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
Christian Borntraeger | ccc40c5 | 2018-03-08 12:48:05 +0000 | [diff] [blame] | 61 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 62 | |
| 63 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 64 | { "userspace_handled", VCPU_STAT(exit_userspace) }, |
Christian Borntraeger | 0eaeafa | 2008-05-07 09:22:53 +0200 | [diff] [blame] | 65 | { "exit_null", VCPU_STAT(exit_null) }, |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 66 | { "exit_validity", VCPU_STAT(exit_validity) }, |
| 67 | { "exit_stop_request", VCPU_STAT(exit_stop_request) }, |
| 68 | { "exit_external_request", VCPU_STAT(exit_external_request) }, |
Christian Borntraeger | a5e0acea9 | 2018-02-23 07:57:33 +0000 | [diff] [blame] | 69 | { "exit_io_request", VCPU_STAT(exit_io_request) }, |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 70 | { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 71 | { "exit_instruction", VCPU_STAT(exit_instruction) }, |
Alexander Yarygin | 9ec6de1 | 2016-05-06 16:33:06 +0300 | [diff] [blame] | 72 | { "exit_pei", VCPU_STAT(exit_pei) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 73 | { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, |
| 74 | { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, |
Janosch Frank | a011eeb | 2016-05-09 14:14:01 +0200 | [diff] [blame] | 75 | { "exit_operation_exception", VCPU_STAT(exit_operation_exception) }, |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 76 | { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, |
Paolo Bonzini | 62bea5b | 2015-09-15 18:27:57 +0200 | [diff] [blame] | 77 | { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 78 | { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 79 | { "halt_no_poll_steal", VCPU_STAT(halt_no_poll_steal) }, |
David Hildenbrand | ce2e4f0 | 2014-07-11 10:00:43 +0200 | [diff] [blame] | 80 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 81 | { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 82 | { "instruction_lctl", VCPU_STAT(instruction_lctl) }, |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 83 | { "instruction_stctl", VCPU_STAT(instruction_stctl) }, |
| 84 | { "instruction_stctg", VCPU_STAT(instruction_stctg) }, |
Christian Borntraeger | ccc40c5 | 2018-03-08 12:48:05 +0000 | [diff] [blame] | 85 | { "deliver_ckc", VCPU_STAT(deliver_ckc) }, |
| 86 | { "deliver_cputm", VCPU_STAT(deliver_cputm) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 87 | { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 88 | { "deliver_external_call", VCPU_STAT(deliver_external_call) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 89 | { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, |
Christian Borntraeger | ccc40c5 | 2018-03-08 12:48:05 +0000 | [diff] [blame] | 90 | { "deliver_virtio", VCPU_STAT(deliver_virtio) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 91 | { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, |
| 92 | { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, |
| 93 | { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, |
Christian Borntraeger | ccc40c5 | 2018-03-08 12:48:05 +0000 | [diff] [blame] | 94 | { "deliver_program", VCPU_STAT(deliver_program) }, |
| 95 | { "deliver_io", VCPU_STAT(deliver_io) }, |
QingFeng Hao | 32de074 | 2018-03-02 11:56:47 +0100 | [diff] [blame] | 96 | { "deliver_machine_check", VCPU_STAT(deliver_machine_check) }, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 97 | { "exit_wait_state", VCPU_STAT(exit_wait_state) }, |
Christian Borntraeger | ccc40c5 | 2018-03-08 12:48:05 +0000 | [diff] [blame] | 98 | { "inject_ckc", VCPU_STAT(inject_ckc) }, |
| 99 | { "inject_cputm", VCPU_STAT(inject_cputm) }, |
| 100 | { "inject_external_call", VCPU_STAT(inject_external_call) }, |
| 101 | { "inject_float_mchk", VM_STAT(inject_float_mchk) }, |
| 102 | { "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) }, |
| 103 | { "inject_io", VM_STAT(inject_io) }, |
| 104 | { "inject_mchk", VCPU_STAT(inject_mchk) }, |
| 105 | { "inject_pfault_done", VM_STAT(inject_pfault_done) }, |
| 106 | { "inject_program", VCPU_STAT(inject_program) }, |
| 107 | { "inject_restart", VCPU_STAT(inject_restart) }, |
| 108 | { "inject_service_signal", VM_STAT(inject_service_signal) }, |
| 109 | { "inject_set_prefix", VCPU_STAT(inject_set_prefix) }, |
| 110 | { "inject_stop_signal", VCPU_STAT(inject_stop_signal) }, |
| 111 | { "inject_pfault_init", VCPU_STAT(inject_pfault_init) }, |
| 112 | { "inject_virtio", VM_STAT(inject_virtio) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 113 | { "instruction_epsw", VCPU_STAT(instruction_epsw) }, |
| 114 | { "instruction_gs", VCPU_STAT(instruction_gs) }, |
| 115 | { "instruction_io_other", VCPU_STAT(instruction_io_other) }, |
| 116 | { "instruction_lpsw", VCPU_STAT(instruction_lpsw) }, |
| 117 | { "instruction_lpswe", VCPU_STAT(instruction_lpswe) }, |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 118 | { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 119 | { "instruction_ptff", VCPU_STAT(instruction_ptff) }, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 120 | { "instruction_stidp", VCPU_STAT(instruction_stidp) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 121 | { "instruction_sck", VCPU_STAT(instruction_sck) }, |
| 122 | { "instruction_sckpf", VCPU_STAT(instruction_sckpf) }, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 123 | { "instruction_spx", VCPU_STAT(instruction_spx) }, |
| 124 | { "instruction_stpx", VCPU_STAT(instruction_stpx) }, |
| 125 | { "instruction_stap", VCPU_STAT(instruction_stap) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 126 | { "instruction_iske", VCPU_STAT(instruction_iske) }, |
| 127 | { "instruction_ri", VCPU_STAT(instruction_ri) }, |
| 128 | { "instruction_rrbe", VCPU_STAT(instruction_rrbe) }, |
| 129 | { "instruction_sske", VCPU_STAT(instruction_sske) }, |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 130 | { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 131 | { "instruction_essa", VCPU_STAT(instruction_essa) }, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 132 | { "instruction_stsi", VCPU_STAT(instruction_stsi) }, |
| 133 | { "instruction_stfl", VCPU_STAT(instruction_stfl) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 134 | { "instruction_tb", VCPU_STAT(instruction_tb) }, |
| 135 | { "instruction_tpi", VCPU_STAT(instruction_tpi) }, |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 136 | { "instruction_tprot", VCPU_STAT(instruction_tprot) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 137 | { "instruction_tsch", VCPU_STAT(instruction_tsch) }, |
Janosch Frank | 95ca2cb | 2016-05-23 15:11:58 +0200 | [diff] [blame] | 138 | { "instruction_sthyi", VCPU_STAT(instruction_sthyi) }, |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 139 | { "instruction_sie", VCPU_STAT(instruction_sie) }, |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 140 | { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, |
Cornelia Huck | bd59d3a | 2011-11-17 11:00:42 +0100 | [diff] [blame] | 141 | { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 142 | { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 143 | { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, |
David Hildenbrand | 42cb0c9 | 2014-05-23 12:25:11 +0200 | [diff] [blame] | 144 | { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, |
| 145 | { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 146 | { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, |
David Hildenbrand | 42cb0c9 | 2014-05-23 12:25:11 +0200 | [diff] [blame] | 147 | { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, |
| 148 | { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, |
Eric Farman | cd7b4b6 | 2015-02-12 09:06:34 -0500 | [diff] [blame] | 149 | { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 150 | { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, |
| 151 | { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, |
| 152 | { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, |
David Hildenbrand | 42cb0c9 | 2014-05-23 12:25:11 +0200 | [diff] [blame] | 153 | { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, |
| 154 | { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, |
| 155 | { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, |
Christian Borntraeger | 866c138 | 2018-01-24 12:27:01 +0100 | [diff] [blame] | 156 | { "instruction_diag_10", VCPU_STAT(diagnose_10) }, |
| 157 | { "instruction_diag_44", VCPU_STAT(diagnose_44) }, |
| 158 | { "instruction_diag_9c", VCPU_STAT(diagnose_9c) }, |
Christian Borntraeger | 8474e5c | 2019-02-15 13:47:20 +0100 | [diff] [blame] | 159 | { "diag_9c_ignored", VCPU_STAT(diagnose_9c_ignored) }, |
Christian Borntraeger | 866c138 | 2018-01-24 12:27:01 +0100 | [diff] [blame] | 160 | { "instruction_diag_258", VCPU_STAT(diagnose_258) }, |
| 161 | { "instruction_diag_308", VCPU_STAT(diagnose_308) }, |
| 162 | { "instruction_diag_500", VCPU_STAT(diagnose_500) }, |
Christian Borntraeger | a37cb07 | 2018-01-23 13:28:40 +0100 | [diff] [blame] | 163 | { "instruction_diag_other", VCPU_STAT(diagnose_other) }, |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 164 | { NULL } |
| 165 | }; |
| 166 | |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 167 | struct kvm_s390_tod_clock_ext { |
| 168 | __u8 epoch_idx; |
| 169 | __u64 tod; |
| 170 | __u8 reserved[7]; |
| 171 | } __packed; |
| 172 | |
David Hildenbrand | a411edf | 2016-02-02 15:41:22 +0100 | [diff] [blame] | 173 | /* allow nested virtualization in KVM (if enabled by user space) */ |
| 174 | static int nested; |
| 175 | module_param(nested, int, S_IRUGO); |
| 176 | MODULE_PARM_DESC(nested, "Nested virtualization support"); |
| 177 | |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 178 | /* allow 1m huge page guest backing, if !nested */ |
| 179 | static int hpage; |
| 180 | module_param(hpage, int, 0444); |
| 181 | MODULE_PARM_DESC(hpage, "1m huge page backing support"); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 182 | |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 183 | /* maximum percentage of steal time for polling. >100 is treated like 100 */ |
| 184 | static u8 halt_poll_max_steal = 10; |
| 185 | module_param(halt_poll_max_steal, byte, 0644); |
Wei Yongjun | b41fb52 | 2019-05-04 06:51:45 +0000 | [diff] [blame] | 186 | MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 187 | |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 188 | /* |
| 189 | * For now we handle at most 16 double words as this is what the s390 base |
| 190 | * kernel handles and stores in the prefix page. If we ever need to go beyond |
| 191 | * this, this requires changes to code, but the external uapi can stay. |
| 192 | */ |
| 193 | #define SIZE_INTERNAL 16 |
| 194 | |
| 195 | /* |
| 196 | * Base feature mask that defines default mask for facilities. Consists of the |
| 197 | * defines in FACILITIES_KVM and the non-hypervisor managed bits. |
| 198 | */ |
| 199 | static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; |
| 200 | /* |
| 201 | * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL |
| 202 | * and defines the facilities that can be enabled via a cpu model. |
| 203 | */ |
| 204 | static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; |
| 205 | |
| 206 | static unsigned long kvm_s390_fac_size(void) |
Michael Mueller | 78c4b59 | 2013-07-26 15:04:04 +0200 | [diff] [blame] | 207 | { |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 208 | BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); |
| 209 | BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); |
| 210 | BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > |
| 211 | sizeof(S390_lowcore.stfle_fac_list)); |
| 212 | |
| 213 | return SIZE_INTERNAL; |
Michael Mueller | 78c4b59 | 2013-07-26 15:04:04 +0200 | [diff] [blame] | 214 | } |
| 215 | |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 216 | /* available cpu features supported by kvm */ |
| 217 | static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 218 | /* available subfunctions indicated via query / "test bit" */ |
| 219 | static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 220 | |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 221 | static struct gmap_notifier gmap_notifier; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 222 | static struct gmap_notifier vsie_gmap_notifier; |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 223 | debug_info_t *kvm_s390_dbf; |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 224 | debug_info_t *kvm_s390_dbf_uv; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 225 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 226 | /* Section: not file related */ |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 227 | int kvm_arch_hardware_enable(void) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 228 | { |
| 229 | /* every s390 is virtualization enabled ;-) */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 230 | return 0; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 233 | int kvm_arch_check_processor_compat(void) |
| 234 | { |
| 235 | return 0; |
| 236 | } |
| 237 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 238 | /* forward declarations */ |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 239 | static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, |
| 240 | unsigned long end); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 241 | static int sca_switch_to_extended(struct kvm *kvm); |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 242 | |
David Hildenbrand | 1575767 | 2018-02-07 12:46:45 +0100 | [diff] [blame] | 243 | static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) |
| 244 | { |
| 245 | u8 delta_idx = 0; |
| 246 | |
| 247 | /* |
| 248 | * The TOD jumps by delta, we have to compensate this by adding |
| 249 | * -delta to the epoch. |
| 250 | */ |
| 251 | delta = -delta; |
| 252 | |
| 253 | /* sign-extension - we're adding to signed values below */ |
| 254 | if ((s64)delta < 0) |
| 255 | delta_idx = -1; |
| 256 | |
| 257 | scb->epoch += delta; |
| 258 | if (scb->ecd & ECD_MEF) { |
| 259 | scb->epdx += delta_idx; |
| 260 | if (scb->epoch < delta) |
| 261 | scb->epdx += 1; |
| 262 | } |
| 263 | } |
| 264 | |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 265 | /* |
| 266 | * This callback is executed during stop_machine(). All CPUs are therefore |
| 267 | * temporarily stopped. In order not to change guest behavior, we have to |
| 268 | * disable preemption whenever we touch the epoch of kvm and the VCPUs, |
| 269 | * so a CPU won't be stopped while calculating with the epoch. |
| 270 | */ |
| 271 | static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, |
| 272 | void *v) |
| 273 | { |
| 274 | struct kvm *kvm; |
| 275 | struct kvm_vcpu *vcpu; |
| 276 | int i; |
| 277 | unsigned long long *delta = v; |
| 278 | |
| 279 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 280 | kvm_for_each_vcpu(i, vcpu, kvm) { |
David Hildenbrand | 1575767 | 2018-02-07 12:46:45 +0100 | [diff] [blame] | 281 | kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); |
| 282 | if (i == 0) { |
| 283 | kvm->arch.epoch = vcpu->arch.sie_block->epoch; |
| 284 | kvm->arch.epdx = vcpu->arch.sie_block->epdx; |
| 285 | } |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 286 | if (vcpu->arch.cputm_enabled) |
| 287 | vcpu->arch.cputm_start += *delta; |
David Hildenbrand | 91473b4 | 2015-10-29 10:30:36 +0100 | [diff] [blame] | 288 | if (vcpu->arch.vsie_block) |
David Hildenbrand | 1575767 | 2018-02-07 12:46:45 +0100 | [diff] [blame] | 289 | kvm_clock_sync_scb(vcpu->arch.vsie_block, |
| 290 | *delta); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | return NOTIFY_OK; |
| 294 | } |
| 295 | |
| 296 | static struct notifier_block kvm_clock_notifier = { |
| 297 | .notifier_call = kvm_clock_sync, |
| 298 | }; |
| 299 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 300 | int kvm_arch_hardware_setup(void) |
| 301 | { |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 302 | gmap_notifier.notifier_call = kvm_gmap_notifier; |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 303 | gmap_register_pte_notifier(&gmap_notifier); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 304 | vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; |
| 305 | gmap_register_pte_notifier(&vsie_gmap_notifier); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 306 | atomic_notifier_chain_register(&s390_epoch_delta_notifier, |
| 307 | &kvm_clock_notifier); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | void kvm_arch_hardware_unsetup(void) |
| 312 | { |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 313 | gmap_unregister_pte_notifier(&gmap_notifier); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 314 | gmap_unregister_pte_notifier(&vsie_gmap_notifier); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 315 | atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, |
| 316 | &kvm_clock_notifier); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 317 | } |
| 318 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 319 | static void allow_cpu_feat(unsigned long nr) |
| 320 | { |
| 321 | set_bit_inv(nr, kvm_s390_available_cpu_feat); |
| 322 | } |
| 323 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 324 | static inline int plo_test_bit(unsigned char nr) |
| 325 | { |
| 326 | register unsigned long r0 asm("0") = (unsigned long) nr | 0x100; |
Heiko Carstens | d051ae5 | 2016-12-13 14:25:32 +0100 | [diff] [blame] | 327 | int cc; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 328 | |
| 329 | asm volatile( |
| 330 | /* Parameter registers are ignored for "test bit" */ |
| 331 | " plo 0,0,0,0(0)\n" |
| 332 | " ipm %0\n" |
| 333 | " srl %0,28\n" |
| 334 | : "=d" (cc) |
| 335 | : "d" (r0) |
| 336 | : "cc"); |
| 337 | return cc == 0; |
| 338 | } |
| 339 | |
Heiko Carstens | d0dea73 | 2019-10-02 14:34:37 +0200 | [diff] [blame] | 340 | static __always_inline void __insn32_query(unsigned int opcode, u8 *query) |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 341 | { |
| 342 | register unsigned long r0 asm("0") = 0; /* query function */ |
| 343 | register unsigned long r1 asm("1") = (unsigned long) query; |
| 344 | |
| 345 | asm volatile( |
| 346 | /* Parameter regs are ignored */ |
| 347 | " .insn rrf,%[opc] << 16,2,4,6,0\n" |
Heiko Carstens | b1c41ac | 2019-10-02 14:24:47 +0200 | [diff] [blame] | 348 | : |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 349 | : "d" (r0), "a" (r1), [opc] "i" (opcode) |
Heiko Carstens | b1c41ac | 2019-10-02 14:24:47 +0200 | [diff] [blame] | 350 | : "cc", "memory"); |
Christian Borntraeger | d668139 | 2019-02-20 03:04:07 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 353 | #define INSN_SORTL 0xb938 |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 354 | #define INSN_DFLTCC 0xb939 |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 355 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 356 | static void kvm_s390_cpu_feat_init(void) |
| 357 | { |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 358 | int i; |
| 359 | |
| 360 | for (i = 0; i < 256; ++i) { |
| 361 | if (plo_test_bit(i)) |
| 362 | kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); |
| 363 | } |
| 364 | |
| 365 | if (test_facility(28)) /* TOD-clock steering */ |
Linus Torvalds | 221bb8a | 2016-08-02 16:11:27 -0400 | [diff] [blame] | 366 | ptff(kvm_s390_available_subfunc.ptff, |
| 367 | sizeof(kvm_s390_available_subfunc.ptff), |
| 368 | PTFF_QAF); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 369 | |
| 370 | if (test_facility(17)) { /* MSA */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 371 | __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) |
| 372 | kvm_s390_available_subfunc.kmac); |
| 373 | __cpacf_query(CPACF_KMC, (cpacf_mask_t *) |
| 374 | kvm_s390_available_subfunc.kmc); |
| 375 | __cpacf_query(CPACF_KM, (cpacf_mask_t *) |
| 376 | kvm_s390_available_subfunc.km); |
| 377 | __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) |
| 378 | kvm_s390_available_subfunc.kimd); |
| 379 | __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) |
| 380 | kvm_s390_available_subfunc.klmd); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 381 | } |
| 382 | if (test_facility(76)) /* MSA3 */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 383 | __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) |
| 384 | kvm_s390_available_subfunc.pckmo); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 385 | if (test_facility(77)) { /* MSA4 */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 386 | __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) |
| 387 | kvm_s390_available_subfunc.kmctr); |
| 388 | __cpacf_query(CPACF_KMF, (cpacf_mask_t *) |
| 389 | kvm_s390_available_subfunc.kmf); |
| 390 | __cpacf_query(CPACF_KMO, (cpacf_mask_t *) |
| 391 | kvm_s390_available_subfunc.kmo); |
| 392 | __cpacf_query(CPACF_PCC, (cpacf_mask_t *) |
| 393 | kvm_s390_available_subfunc.pcc); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 394 | } |
| 395 | if (test_facility(57)) /* MSA5 */ |
Harald Freudenberger | 985a9d2 | 2017-02-24 10:11:54 +0100 | [diff] [blame] | 396 | __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 397 | kvm_s390_available_subfunc.ppno); |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 398 | |
Jason J. Herne | e000b8e | 2017-03-20 09:57:42 -0400 | [diff] [blame] | 399 | if (test_facility(146)) /* MSA8 */ |
| 400 | __cpacf_query(CPACF_KMA, (cpacf_mask_t *) |
| 401 | kvm_s390_available_subfunc.kma); |
| 402 | |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 403 | if (test_facility(155)) /* MSA9 */ |
| 404 | __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) |
| 405 | kvm_s390_available_subfunc.kdsa); |
| 406 | |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 407 | if (test_facility(150)) /* SORTL */ |
| 408 | __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); |
| 409 | |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 410 | if (test_facility(151)) /* DFLTCC */ |
| 411 | __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); |
| 412 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 413 | if (MACHINE_HAS_ESOP) |
| 414 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 415 | /* |
| 416 | * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), |
| 417 | * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). |
| 418 | */ |
| 419 | if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || |
David Hildenbrand | a411edf | 2016-02-02 15:41:22 +0100 | [diff] [blame] | 420 | !test_facility(3) || !nested) |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 421 | return; |
| 422 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); |
David Hildenbrand | 19c439b | 2015-11-25 11:02:26 +0100 | [diff] [blame] | 423 | if (sclp.has_64bscao) |
| 424 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); |
David Hildenbrand | 0615a32 | 2015-11-25 09:59:49 +0100 | [diff] [blame] | 425 | if (sclp.has_siif) |
| 426 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); |
David Hildenbrand | 77d18f6 | 2015-11-24 16:32:35 +0100 | [diff] [blame] | 427 | if (sclp.has_gpere) |
| 428 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); |
David Hildenbrand | a1b7b9b | 2015-11-24 16:41:33 +0100 | [diff] [blame] | 429 | if (sclp.has_gsls) |
| 430 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); |
David Hildenbrand | 5630a8e | 2015-11-24 16:53:51 +0100 | [diff] [blame] | 431 | if (sclp.has_ib) |
| 432 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); |
David Hildenbrand | 13ee3f6 | 2015-11-24 16:54:37 +0100 | [diff] [blame] | 433 | if (sclp.has_cei) |
| 434 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); |
David Hildenbrand | 7fd7f39 | 2015-11-24 16:56:23 +0100 | [diff] [blame] | 435 | if (sclp.has_ibs) |
| 436 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 437 | if (sclp.has_kss) |
| 438 | allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); |
David Hildenbrand | 5d3876a | 2016-04-13 17:06:50 +0200 | [diff] [blame] | 439 | /* |
| 440 | * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make |
| 441 | * all skey handling functions read/set the skey from the PGSTE |
| 442 | * instead of the real storage key. |
| 443 | * |
| 444 | * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make |
| 445 | * pages being detected as preserved although they are resident. |
| 446 | * |
| 447 | * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will |
| 448 | * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. |
| 449 | * |
| 450 | * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and |
| 451 | * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be |
| 452 | * correctly shadowed. We can do that for the PGSTE but not for PTE.I. |
| 453 | * |
| 454 | * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We |
| 455 | * cannot easily shadow the SCA because of the ipte lock. |
| 456 | */ |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 459 | int kvm_arch_init(void *opaque) |
| 460 | { |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 461 | int rc = -ENOMEM; |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 462 | |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 463 | kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); |
| 464 | if (!kvm_s390_dbf) |
| 465 | return -ENOMEM; |
| 466 | |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 467 | kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); |
| 468 | if (!kvm_s390_dbf_uv) |
| 469 | goto out; |
| 470 | |
| 471 | if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || |
| 472 | debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 473 | goto out; |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 474 | |
David Hildenbrand | 22be5a13 | 2016-01-21 13:22:54 +0100 | [diff] [blame] | 475 | kvm_s390_cpu_feat_init(); |
| 476 | |
Cornelia Huck | 84877d9 | 2014-09-02 10:27:35 +0100 | [diff] [blame] | 477 | /* Register floating interrupt controller interface. */ |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 478 | rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); |
| 479 | if (rc) { |
Michael Mueller | 8d43d57 | 2018-12-10 11:15:16 +0100 | [diff] [blame] | 480 | pr_err("A FLIC registration call failed with rc=%d\n", rc); |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 481 | goto out; |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 482 | } |
Michael Mueller | b1d1e76 | 2019-01-31 09:52:45 +0100 | [diff] [blame] | 483 | |
| 484 | rc = kvm_s390_gib_init(GAL_ISC); |
| 485 | if (rc) |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 486 | goto out; |
Michael Mueller | b1d1e76 | 2019-01-31 09:52:45 +0100 | [diff] [blame] | 487 | |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 488 | return 0; |
| 489 | |
Janosch Frank | f76f637 | 2019-10-02 03:56:27 -0400 | [diff] [blame] | 490 | out: |
| 491 | kvm_arch_exit(); |
Michael Mueller | 308c3e6 | 2018-11-30 15:32:06 +0100 | [diff] [blame] | 492 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 495 | void kvm_arch_exit(void) |
| 496 | { |
Michael Mueller | 1282c21 | 2019-01-31 09:52:40 +0100 | [diff] [blame] | 497 | kvm_s390_gib_destroy(); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 498 | debug_unregister(kvm_s390_dbf); |
Janosch Frank | 3e6c556 | 2019-10-02 04:46:58 -0400 | [diff] [blame] | 499 | debug_unregister(kvm_s390_dbf_uv); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 502 | /* Section: device related */ |
| 503 | long kvm_arch_dev_ioctl(struct file *filp, |
| 504 | unsigned int ioctl, unsigned long arg) |
| 505 | { |
| 506 | if (ioctl == KVM_S390_ENABLE_SIE) |
| 507 | return s390_enable_sie(); |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 511 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 512 | { |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 513 | int r; |
| 514 | |
Carsten Otte | 2bd0ac4 | 2008-07-25 15:49:13 +0200 | [diff] [blame] | 515 | switch (ext) { |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 516 | case KVM_CAP_S390_PSW: |
Christian Borntraeger | b6cf878 | 2011-09-20 17:07:29 +0200 | [diff] [blame] | 517 | case KVM_CAP_S390_GMAP: |
Christian Borntraeger | 52e16b1 | 2011-11-17 11:00:44 +0100 | [diff] [blame] | 518 | case KVM_CAP_SYNC_MMU: |
Carsten Otte | 1efd0f5 | 2012-01-04 10:25:29 +0100 | [diff] [blame] | 519 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 520 | case KVM_CAP_S390_UCONTROL: |
| 521 | #endif |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 522 | case KVM_CAP_ASYNC_PF: |
Christian Borntraeger | 60b413c | 2012-01-11 11:20:31 +0100 | [diff] [blame] | 523 | case KVM_CAP_SYNC_REGS: |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 524 | case KVM_CAP_ONE_REG: |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 525 | case KVM_CAP_ENABLE_CAP: |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 526 | case KVM_CAP_S390_CSS_SUPPORT: |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 527 | case KVM_CAP_IOEVENTFD: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 528 | case KVM_CAP_DEVICE_CTRL: |
Cornelia Huck | 78599d9 | 2014-07-15 09:54:39 +0200 | [diff] [blame] | 529 | case KVM_CAP_S390_IRQCHIP: |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 530 | case KVM_CAP_VM_ATTRIBUTES: |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 531 | case KVM_CAP_MP_STATE: |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 532 | case KVM_CAP_IMMEDIATE_EXIT: |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 533 | case KVM_CAP_S390_INJECT_IRQ: |
David Hildenbrand | 2444b35 | 2014-10-09 14:10:13 +0200 | [diff] [blame] | 534 | case KVM_CAP_S390_USER_SIGP: |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 535 | case KVM_CAP_S390_USER_STSI: |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 536 | case KVM_CAP_S390_SKEYS: |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 537 | case KVM_CAP_S390_IRQ_STATE: |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 538 | case KVM_CAP_S390_USER_INSTR0: |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 539 | case KVM_CAP_S390_CMMA_MIGRATION: |
Yi Min Zhao | 47a4693 | 2017-03-10 09:29:38 +0100 | [diff] [blame] | 540 | case KVM_CAP_S390_AIS: |
Christian Borntraeger | da9a144 | 2017-11-09 10:00:45 +0100 | [diff] [blame] | 541 | case KVM_CAP_S390_AIS_MIGRATION: |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 542 | case KVM_CAP_S390_VCPU_RESETS: |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 543 | r = 1; |
| 544 | break; |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 545 | case KVM_CAP_S390_HPAGE_1M: |
| 546 | r = 0; |
Janosch Frank | 40ebdb8 | 2018-08-01 11:48:28 +0100 | [diff] [blame] | 547 | if (hpage && !kvm_is_ucontrol(kvm)) |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 548 | r = 1; |
| 549 | break; |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 550 | case KVM_CAP_S390_MEM_OP: |
| 551 | r = MEM_OP_MAX_SIZE; |
| 552 | break; |
Christian Borntraeger | e726b1b | 2012-05-02 10:50:38 +0200 | [diff] [blame] | 553 | case KVM_CAP_NR_VCPUS: |
| 554 | case KVM_CAP_MAX_VCPUS: |
Thomas Huth | a86cb41 | 2019-05-23 18:43:08 +0200 | [diff] [blame] | 555 | case KVM_CAP_MAX_VCPU_ID: |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 556 | r = KVM_S390_BSCA_CPU_SLOTS; |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 557 | if (!kvm_s390_use_sca_entries()) |
| 558 | r = KVM_MAX_VCPUS; |
| 559 | else if (sclp.has_esca && sclp.has_64bscao) |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 560 | r = KVM_S390_ESCA_CPU_SLOTS; |
Christian Borntraeger | e726b1b | 2012-05-02 10:50:38 +0200 | [diff] [blame] | 561 | break; |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 562 | case KVM_CAP_S390_COW: |
Martin Schwidefsky | abf09be | 2012-11-07 13:17:37 +0100 | [diff] [blame] | 563 | r = MACHINE_HAS_ESOP; |
Christian Borntraeger | 1526bf9 | 2012-05-15 14:15:25 +0200 | [diff] [blame] | 564 | break; |
Eric Farman | 68c5575 | 2014-06-09 10:57:26 -0400 | [diff] [blame] | 565 | case KVM_CAP_S390_VECTOR_REGISTERS: |
| 566 | r = MACHINE_HAS_VX; |
| 567 | break; |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 568 | case KVM_CAP_S390_RI: |
| 569 | r = test_facility(64); |
| 570 | break; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 571 | case KVM_CAP_S390_GS: |
| 572 | r = test_facility(133); |
| 573 | break; |
Christian Borntraeger | 35b3fde | 2018-01-17 14:44:34 +0100 | [diff] [blame] | 574 | case KVM_CAP_S390_BPB: |
| 575 | r = test_facility(82); |
| 576 | break; |
Carsten Otte | 2bd0ac4 | 2008-07-25 15:49:13 +0200 | [diff] [blame] | 577 | default: |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 578 | r = 0; |
Carsten Otte | 2bd0ac4 | 2008-07-25 15:49:13 +0200 | [diff] [blame] | 579 | } |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 580 | return r; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 583 | static void kvm_s390_sync_dirty_log(struct kvm *kvm, |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 584 | struct kvm_memory_slot *memslot) |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 585 | { |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 586 | int i; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 587 | gfn_t cur_gfn, last_gfn; |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 588 | unsigned long gaddr, vmaddr; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 589 | struct gmap *gmap = kvm->arch.gmap; |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 590 | DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 591 | |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 592 | /* Loop over all guest segments */ |
| 593 | cur_gfn = memslot->base_gfn; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 594 | last_gfn = memslot->base_gfn + memslot->npages; |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 595 | for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { |
| 596 | gaddr = gfn_to_gpa(cur_gfn); |
| 597 | vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); |
| 598 | if (kvm_is_error_hva(vmaddr)) |
| 599 | continue; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 600 | |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 601 | bitmap_zero(bitmap, _PAGE_ENTRIES); |
| 602 | gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); |
| 603 | for (i = 0; i < _PAGE_ENTRIES; i++) { |
| 604 | if (test_bit(i, bitmap)) |
| 605 | mark_page_dirty(kvm, cur_gfn + i); |
| 606 | } |
| 607 | |
Christian Borntraeger | 1763f8d | 2016-02-03 11:12:34 +0100 | [diff] [blame] | 608 | if (fatal_signal_pending(current)) |
| 609 | return; |
Christian Borntraeger | 70c88a0 | 2016-02-02 15:15:56 +0100 | [diff] [blame] | 610 | cond_resched(); |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 611 | } |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 612 | } |
| 613 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 614 | /* Section: vm related */ |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 615 | static void sca_del_vcpu(struct kvm_vcpu *vcpu); |
| 616 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 617 | /* |
| 618 | * Get (and clear) the dirty memory log for a memory slot. |
| 619 | */ |
| 620 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 621 | struct kvm_dirty_log *log) |
| 622 | { |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 623 | int r; |
| 624 | unsigned long n; |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 625 | struct kvm_memslots *slots; |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 626 | struct kvm_memory_slot *memslot; |
| 627 | int is_dirty = 0; |
| 628 | |
Janosch Frank | e1e8a96 | 2017-02-02 16:39:31 +0100 | [diff] [blame] | 629 | if (kvm_is_ucontrol(kvm)) |
| 630 | return -EINVAL; |
| 631 | |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 632 | mutex_lock(&kvm->slots_lock); |
| 633 | |
| 634 | r = -EINVAL; |
| 635 | if (log->slot >= KVM_USER_MEM_SLOTS) |
| 636 | goto out; |
| 637 | |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 638 | slots = kvm_memslots(kvm); |
| 639 | memslot = id_to_memslot(slots, log->slot); |
Jason J. Herne | 15f36eb | 2012-08-02 10:10:17 -0400 | [diff] [blame] | 640 | r = -ENOENT; |
| 641 | if (!memslot->dirty_bitmap) |
| 642 | goto out; |
| 643 | |
| 644 | kvm_s390_sync_dirty_log(kvm, memslot); |
| 645 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 646 | if (r) |
| 647 | goto out; |
| 648 | |
| 649 | /* Clear the dirty log */ |
| 650 | if (is_dirty) { |
| 651 | n = kvm_dirty_bitmap_bytes(memslot); |
| 652 | memset(memslot->dirty_bitmap, 0, n); |
| 653 | } |
| 654 | r = 0; |
| 655 | out: |
| 656 | mutex_unlock(&kvm->slots_lock); |
| 657 | return r; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 658 | } |
| 659 | |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 660 | static void icpt_operexc_on_all_vcpus(struct kvm *kvm) |
| 661 | { |
| 662 | unsigned int i; |
| 663 | struct kvm_vcpu *vcpu; |
| 664 | |
| 665 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 666 | kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); |
| 667 | } |
| 668 | } |
| 669 | |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 670 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) |
Cornelia Huck | d938dc5 | 2013-10-23 18:26:34 +0200 | [diff] [blame] | 671 | { |
| 672 | int r; |
| 673 | |
| 674 | if (cap->flags) |
| 675 | return -EINVAL; |
| 676 | |
| 677 | switch (cap->cap) { |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 678 | case KVM_CAP_S390_IRQCHIP: |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 679 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 680 | kvm->arch.use_irqchip = 1; |
| 681 | r = 0; |
| 682 | break; |
David Hildenbrand | 2444b35 | 2014-10-09 14:10:13 +0200 | [diff] [blame] | 683 | case KVM_CAP_S390_USER_SIGP: |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 684 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); |
David Hildenbrand | 2444b35 | 2014-10-09 14:10:13 +0200 | [diff] [blame] | 685 | kvm->arch.user_sigp = 1; |
| 686 | r = 0; |
| 687 | break; |
Eric Farman | 68c5575 | 2014-06-09 10:57:26 -0400 | [diff] [blame] | 688 | case KVM_CAP_S390_VECTOR_REGISTERS: |
David Hildenbrand | 5967c17 | 2015-11-06 12:08:48 +0100 | [diff] [blame] | 689 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 690 | if (kvm->created_vcpus) { |
David Hildenbrand | 5967c17 | 2015-11-06 12:08:48 +0100 | [diff] [blame] | 691 | r = -EBUSY; |
| 692 | } else if (MACHINE_HAS_VX) { |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 693 | set_kvm_facility(kvm->arch.model.fac_mask, 129); |
| 694 | set_kvm_facility(kvm->arch.model.fac_list, 129); |
Guenther Hutzl | 2f87d94 | 2016-06-03 14:37:17 +0200 | [diff] [blame] | 695 | if (test_facility(134)) { |
| 696 | set_kvm_facility(kvm->arch.model.fac_mask, 134); |
| 697 | set_kvm_facility(kvm->arch.model.fac_list, 134); |
| 698 | } |
Maxim Samoylov | 53743aa | 2016-02-10 10:31:23 +0100 | [diff] [blame] | 699 | if (test_facility(135)) { |
| 700 | set_kvm_facility(kvm->arch.model.fac_mask, 135); |
| 701 | set_kvm_facility(kvm->arch.model.fac_list, 135); |
| 702 | } |
Christian Borntraeger | 7832e91 | 2018-12-28 09:43:37 +0100 | [diff] [blame] | 703 | if (test_facility(148)) { |
| 704 | set_kvm_facility(kvm->arch.model.fac_mask, 148); |
| 705 | set_kvm_facility(kvm->arch.model.fac_list, 148); |
| 706 | } |
Christian Borntraeger | d5cb6ab | 2018-12-28 09:45:58 +0100 | [diff] [blame] | 707 | if (test_facility(152)) { |
| 708 | set_kvm_facility(kvm->arch.model.fac_mask, 152); |
| 709 | set_kvm_facility(kvm->arch.model.fac_list, 152); |
| 710 | } |
Michael Mueller | 18280d8 | 2015-03-16 16:05:41 +0100 | [diff] [blame] | 711 | r = 0; |
| 712 | } else |
| 713 | r = -EINVAL; |
David Hildenbrand | 5967c17 | 2015-11-06 12:08:48 +0100 | [diff] [blame] | 714 | mutex_unlock(&kvm->lock); |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 715 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", |
| 716 | r ? "(not available)" : "(success)"); |
Eric Farman | 68c5575 | 2014-06-09 10:57:26 -0400 | [diff] [blame] | 717 | break; |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 718 | case KVM_CAP_S390_RI: |
| 719 | r = -EINVAL; |
| 720 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 721 | if (kvm->created_vcpus) { |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 722 | r = -EBUSY; |
| 723 | } else if (test_facility(64)) { |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 724 | set_kvm_facility(kvm->arch.model.fac_mask, 64); |
| 725 | set_kvm_facility(kvm->arch.model.fac_list, 64); |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 726 | r = 0; |
| 727 | } |
| 728 | mutex_unlock(&kvm->lock); |
| 729 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", |
| 730 | r ? "(not available)" : "(success)"); |
| 731 | break; |
Yi Min Zhao | 47a4693 | 2017-03-10 09:29:38 +0100 | [diff] [blame] | 732 | case KVM_CAP_S390_AIS: |
| 733 | mutex_lock(&kvm->lock); |
| 734 | if (kvm->created_vcpus) { |
| 735 | r = -EBUSY; |
| 736 | } else { |
| 737 | set_kvm_facility(kvm->arch.model.fac_mask, 72); |
| 738 | set_kvm_facility(kvm->arch.model.fac_list, 72); |
Yi Min Zhao | 47a4693 | 2017-03-10 09:29:38 +0100 | [diff] [blame] | 739 | r = 0; |
| 740 | } |
| 741 | mutex_unlock(&kvm->lock); |
| 742 | VM_EVENT(kvm, 3, "ENABLE: AIS %s", |
| 743 | r ? "(not available)" : "(success)"); |
| 744 | break; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 745 | case KVM_CAP_S390_GS: |
| 746 | r = -EINVAL; |
| 747 | mutex_lock(&kvm->lock); |
Christian Borntraeger | 241e3ec0 | 2017-11-16 15:12:52 +0100 | [diff] [blame] | 748 | if (kvm->created_vcpus) { |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 749 | r = -EBUSY; |
| 750 | } else if (test_facility(133)) { |
| 751 | set_kvm_facility(kvm->arch.model.fac_mask, 133); |
| 752 | set_kvm_facility(kvm->arch.model.fac_list, 133); |
| 753 | r = 0; |
| 754 | } |
| 755 | mutex_unlock(&kvm->lock); |
| 756 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", |
| 757 | r ? "(not available)" : "(success)"); |
| 758 | break; |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 759 | case KVM_CAP_S390_HPAGE_1M: |
| 760 | mutex_lock(&kvm->lock); |
| 761 | if (kvm->created_vcpus) |
| 762 | r = -EBUSY; |
Janosch Frank | 40ebdb8 | 2018-08-01 11:48:28 +0100 | [diff] [blame] | 763 | else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 764 | r = -EINVAL; |
| 765 | else { |
| 766 | r = 0; |
Janosch Frank | df88f31 | 2018-08-30 16:14:18 +0200 | [diff] [blame] | 767 | down_write(&kvm->mm->mmap_sem); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 768 | kvm->mm->context.allow_gmap_hpage_1m = 1; |
Janosch Frank | df88f31 | 2018-08-30 16:14:18 +0200 | [diff] [blame] | 769 | up_write(&kvm->mm->mmap_sem); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 770 | /* |
| 771 | * We might have to create fake 4k page |
| 772 | * tables. To avoid that the hardware works on |
| 773 | * stale PGSTEs, we emulate these instructions. |
| 774 | */ |
| 775 | kvm->arch.use_skf = 0; |
| 776 | kvm->arch.use_pfmfi = 0; |
| 777 | } |
| 778 | mutex_unlock(&kvm->lock); |
| 779 | VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", |
| 780 | r ? "(not available)" : "(success)"); |
| 781 | break; |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 782 | case KVM_CAP_S390_USER_STSI: |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 783 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 784 | kvm->arch.user_stsi = 1; |
| 785 | r = 0; |
| 786 | break; |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 787 | case KVM_CAP_S390_USER_INSTR0: |
| 788 | VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); |
| 789 | kvm->arch.user_instr0 = 1; |
| 790 | icpt_operexc_on_all_vcpus(kvm); |
| 791 | r = 0; |
| 792 | break; |
Cornelia Huck | d938dc5 | 2013-10-23 18:26:34 +0200 | [diff] [blame] | 793 | default: |
| 794 | r = -EINVAL; |
| 795 | break; |
| 796 | } |
| 797 | return r; |
| 798 | } |
| 799 | |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 800 | static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) |
| 801 | { |
| 802 | int ret; |
| 803 | |
| 804 | switch (attr->attr) { |
| 805 | case KVM_S390_VM_MEM_LIMIT_SIZE: |
| 806 | ret = 0; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 807 | VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 808 | kvm->arch.mem_limit); |
| 809 | if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 810 | ret = -EFAULT; |
| 811 | break; |
| 812 | default: |
| 813 | ret = -ENXIO; |
| 814 | break; |
| 815 | } |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 820 | { |
| 821 | int ret; |
| 822 | unsigned int idx; |
| 823 | switch (attr->attr) { |
| 824 | case KVM_S390_VM_MEM_ENABLE_CMMA: |
David Hildenbrand | f9cbd9b | 2016-03-03 09:48:47 +0100 | [diff] [blame] | 825 | ret = -ENXIO; |
David Hildenbrand | c24cc9c | 2015-11-24 13:53:04 +0100 | [diff] [blame] | 826 | if (!sclp.has_cmma) |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 827 | break; |
| 828 | |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 829 | VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 830 | mutex_lock(&kvm->lock); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 831 | if (kvm->created_vcpus) |
| 832 | ret = -EBUSY; |
| 833 | else if (kvm->mm->context.allow_gmap_hpage_1m) |
| 834 | ret = -EINVAL; |
| 835 | else { |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 836 | kvm->arch.use_cmma = 1; |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 837 | /* Not compatible with cmma. */ |
| 838 | kvm->arch.use_pfmfi = 0; |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 839 | ret = 0; |
| 840 | } |
| 841 | mutex_unlock(&kvm->lock); |
| 842 | break; |
| 843 | case KVM_S390_VM_MEM_CLR_CMMA: |
David Hildenbrand | f9cbd9b | 2016-03-03 09:48:47 +0100 | [diff] [blame] | 844 | ret = -ENXIO; |
| 845 | if (!sclp.has_cmma) |
| 846 | break; |
Dominik Dingel | c348915 | 2015-06-18 13:17:11 +0200 | [diff] [blame] | 847 | ret = -EINVAL; |
| 848 | if (!kvm->arch.use_cmma) |
| 849 | break; |
| 850 | |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 851 | VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 852 | mutex_lock(&kvm->lock); |
| 853 | idx = srcu_read_lock(&kvm->srcu); |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 854 | s390_reset_cmma(kvm->arch.gmap->mm); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 855 | srcu_read_unlock(&kvm->srcu, idx); |
| 856 | mutex_unlock(&kvm->lock); |
| 857 | ret = 0; |
| 858 | break; |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 859 | case KVM_S390_VM_MEM_LIMIT_SIZE: { |
| 860 | unsigned long new_limit; |
| 861 | |
| 862 | if (kvm_is_ucontrol(kvm)) |
| 863 | return -EINVAL; |
| 864 | |
| 865 | if (get_user(new_limit, (u64 __user *)attr->addr)) |
| 866 | return -EFAULT; |
| 867 | |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 868 | if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && |
| 869 | new_limit > kvm->arch.mem_limit) |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 870 | return -E2BIG; |
| 871 | |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 872 | if (!new_limit) |
| 873 | return -EINVAL; |
| 874 | |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 875 | /* gmap_create takes last usable address */ |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 876 | if (new_limit != KVM_S390_NO_MEM_LIMIT) |
| 877 | new_limit -= 1; |
| 878 | |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 879 | ret = -EBUSY; |
| 880 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 881 | if (!kvm->created_vcpus) { |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 882 | /* gmap_create will round the limit up */ |
| 883 | struct gmap *new = gmap_create(current->mm, new_limit); |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 884 | |
| 885 | if (!new) { |
| 886 | ret = -ENOMEM; |
| 887 | } else { |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 888 | gmap_remove(kvm->arch.gmap); |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 889 | new->private = kvm; |
| 890 | kvm->arch.gmap = new; |
| 891 | ret = 0; |
| 892 | } |
| 893 | } |
| 894 | mutex_unlock(&kvm->lock); |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 895 | VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); |
| 896 | VM_EVENT(kvm, 3, "New guest asce: 0x%pK", |
| 897 | (void *) kvm->arch.gmap->asce); |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 898 | break; |
| 899 | } |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 900 | default: |
| 901 | ret = -ENXIO; |
| 902 | break; |
| 903 | } |
| 904 | return ret; |
| 905 | } |
| 906 | |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 907 | static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); |
| 908 | |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 909 | void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 910 | { |
| 911 | struct kvm_vcpu *vcpu; |
| 912 | int i; |
| 913 | |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 914 | kvm_s390_vcpu_block_all(kvm); |
| 915 | |
David Hildenbrand | 3194cdb | 2018-09-25 19:16:17 -0400 | [diff] [blame] | 916 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 917 | kvm_s390_vcpu_crypto_setup(vcpu); |
David Hildenbrand | 3194cdb | 2018-09-25 19:16:17 -0400 | [diff] [blame] | 918 | /* recreate the shadow crycb by leaving the VSIE handler */ |
| 919 | kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); |
| 920 | } |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 921 | |
| 922 | kvm_s390_vcpu_unblock_all(kvm); |
| 923 | } |
| 924 | |
| 925 | static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) |
| 926 | { |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 927 | mutex_lock(&kvm->lock); |
| 928 | switch (attr->attr) { |
| 929 | case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 930 | if (!test_kvm_facility(kvm, 76)) { |
| 931 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 932 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 933 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 934 | get_random_bytes( |
| 935 | kvm->arch.crypto.crycb->aes_wrapping_key_mask, |
| 936 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); |
| 937 | kvm->arch.crypto.aes_kw = 1; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 938 | VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 939 | break; |
| 940 | case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 941 | if (!test_kvm_facility(kvm, 76)) { |
| 942 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 943 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 944 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 945 | get_random_bytes( |
| 946 | kvm->arch.crypto.crycb->dea_wrapping_key_mask, |
| 947 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); |
| 948 | kvm->arch.crypto.dea_kw = 1; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 949 | VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 950 | break; |
| 951 | case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 952 | if (!test_kvm_facility(kvm, 76)) { |
| 953 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 954 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 955 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 956 | kvm->arch.crypto.aes_kw = 0; |
| 957 | memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, |
| 958 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 959 | VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 960 | break; |
| 961 | case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 962 | if (!test_kvm_facility(kvm, 76)) { |
| 963 | mutex_unlock(&kvm->lock); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 964 | return -EINVAL; |
Christian Borntraeger | 8e41bd5 | 2018-10-04 14:42:43 +0200 | [diff] [blame] | 965 | } |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 966 | kvm->arch.crypto.dea_kw = 0; |
| 967 | memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, |
| 968 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 969 | VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 970 | break; |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 971 | case KVM_S390_VM_CRYPTO_ENABLE_APIE: |
| 972 | if (!ap_instructions_available()) { |
| 973 | mutex_unlock(&kvm->lock); |
| 974 | return -EOPNOTSUPP; |
| 975 | } |
| 976 | kvm->arch.crypto.apie = 1; |
| 977 | break; |
| 978 | case KVM_S390_VM_CRYPTO_DISABLE_APIE: |
| 979 | if (!ap_instructions_available()) { |
| 980 | mutex_unlock(&kvm->lock); |
| 981 | return -EOPNOTSUPP; |
| 982 | } |
| 983 | kvm->arch.crypto.apie = 0; |
| 984 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 985 | default: |
| 986 | mutex_unlock(&kvm->lock); |
| 987 | return -ENXIO; |
| 988 | } |
| 989 | |
Tony Krowiak | 20c922f | 2018-04-22 11:37:03 -0400 | [diff] [blame] | 990 | kvm_s390_vcpu_crypto_reset_all(kvm); |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 991 | mutex_unlock(&kvm->lock); |
| 992 | return 0; |
| 993 | } |
| 994 | |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 995 | static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) |
| 996 | { |
| 997 | int cx; |
| 998 | struct kvm_vcpu *vcpu; |
| 999 | |
| 1000 | kvm_for_each_vcpu(cx, vcpu, kvm) |
| 1001 | kvm_s390_sync_request(req, vcpu); |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Must be called with kvm->srcu held to avoid races on memslots, and with |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1006 | * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1007 | */ |
| 1008 | static int kvm_s390_vm_start_migration(struct kvm *kvm) |
| 1009 | { |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1010 | struct kvm_memory_slot *ms; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1011 | struct kvm_memslots *slots; |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1012 | unsigned long ram_pages = 0; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1013 | int slotnr; |
| 1014 | |
| 1015 | /* migration mode already enabled */ |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1016 | if (kvm->arch.migration_mode) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1017 | return 0; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1018 | slots = kvm_memslots(kvm); |
| 1019 | if (!slots || !slots->used_slots) |
| 1020 | return -EINVAL; |
| 1021 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1022 | if (!kvm->arch.use_cmma) { |
| 1023 | kvm->arch.migration_mode = 1; |
| 1024 | return 0; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1025 | } |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1026 | /* mark all the pages in active slots as dirty */ |
| 1027 | for (slotnr = 0; slotnr < slots->used_slots; slotnr++) { |
| 1028 | ms = slots->memslots + slotnr; |
Igor Mammedov | 13a17cc | 2019-09-11 03:52:18 -0400 | [diff] [blame] | 1029 | if (!ms->dirty_bitmap) |
| 1030 | return -EINVAL; |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1031 | /* |
| 1032 | * The second half of the bitmap is only used on x86, |
| 1033 | * and would be wasted otherwise, so we put it to good |
| 1034 | * use here to keep track of the state of the storage |
| 1035 | * attributes. |
| 1036 | */ |
| 1037 | memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); |
| 1038 | ram_pages += ms->npages; |
| 1039 | } |
| 1040 | atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); |
| 1041 | kvm->arch.migration_mode = 1; |
| 1042 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1047 | * Must be called with kvm->slots_lock to avoid races with ourselves and |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1048 | * kvm_s390_vm_start_migration. |
| 1049 | */ |
| 1050 | static int kvm_s390_vm_stop_migration(struct kvm *kvm) |
| 1051 | { |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1052 | /* migration mode already disabled */ |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1053 | if (!kvm->arch.migration_mode) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1054 | return 0; |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1055 | kvm->arch.migration_mode = 0; |
| 1056 | if (kvm->arch.use_cmma) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1057 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | static int kvm_s390_vm_set_migration(struct kvm *kvm, |
| 1062 | struct kvm_device_attr *attr) |
| 1063 | { |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1064 | int res = -ENXIO; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1065 | |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1066 | mutex_lock(&kvm->slots_lock); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1067 | switch (attr->attr) { |
| 1068 | case KVM_S390_VM_MIGRATION_START: |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1069 | res = kvm_s390_vm_start_migration(kvm); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1070 | break; |
| 1071 | case KVM_S390_VM_MIGRATION_STOP: |
| 1072 | res = kvm_s390_vm_stop_migration(kvm); |
| 1073 | break; |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 1077 | mutex_unlock(&kvm->slots_lock); |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1078 | |
| 1079 | return res; |
| 1080 | } |
| 1081 | |
| 1082 | static int kvm_s390_vm_get_migration(struct kvm *kvm, |
| 1083 | struct kvm_device_attr *attr) |
| 1084 | { |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1085 | u64 mig = kvm->arch.migration_mode; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1086 | |
| 1087 | if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) |
| 1088 | return -ENXIO; |
| 1089 | |
| 1090 | if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) |
| 1091 | return -EFAULT; |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1095 | static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1096 | { |
| 1097 | struct kvm_s390_vm_tod_clock gtod; |
| 1098 | |
| 1099 | if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) |
| 1100 | return -EFAULT; |
| 1101 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1102 | if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1103 | return -EINVAL; |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1104 | kvm_s390_set_tod_clock(kvm, >od); |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1105 | |
| 1106 | VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", |
| 1107 | gtod.epoch_idx, gtod.tod); |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1112 | static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1113 | { |
| 1114 | u8 gtod_high; |
| 1115 | |
| 1116 | if (copy_from_user(>od_high, (void __user *)attr->addr, |
| 1117 | sizeof(gtod_high))) |
| 1118 | return -EFAULT; |
| 1119 | |
| 1120 | if (gtod_high != 0) |
| 1121 | return -EINVAL; |
Christian Borntraeger | 58c383c | 2015-10-12 13:27:29 +0200 | [diff] [blame] | 1122 | VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1128 | { |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1129 | struct kvm_s390_vm_tod_clock gtod = { 0 }; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1130 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1131 | if (copy_from_user(>od.tod, (void __user *)attr->addr, |
| 1132 | sizeof(gtod.tod))) |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1133 | return -EFAULT; |
| 1134 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 1135 | kvm_s390_set_tod_clock(kvm, >od); |
| 1136 | VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1141 | { |
| 1142 | int ret; |
| 1143 | |
| 1144 | if (attr->flags) |
| 1145 | return -EINVAL; |
| 1146 | |
| 1147 | switch (attr->attr) { |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1148 | case KVM_S390_VM_TOD_EXT: |
| 1149 | ret = kvm_s390_set_tod_ext(kvm, attr); |
| 1150 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1151 | case KVM_S390_VM_TOD_HIGH: |
| 1152 | ret = kvm_s390_set_tod_high(kvm, attr); |
| 1153 | break; |
| 1154 | case KVM_S390_VM_TOD_LOW: |
| 1155 | ret = kvm_s390_set_tod_low(kvm, attr); |
| 1156 | break; |
| 1157 | default: |
| 1158 | ret = -ENXIO; |
| 1159 | break; |
| 1160 | } |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1164 | static void kvm_s390_get_tod_clock(struct kvm *kvm, |
| 1165 | struct kvm_s390_vm_tod_clock *gtod) |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1166 | { |
| 1167 | struct kvm_s390_tod_clock_ext htod; |
| 1168 | |
| 1169 | preempt_disable(); |
| 1170 | |
| 1171 | get_tod_clock_ext((char *)&htod); |
| 1172 | |
| 1173 | gtod->tod = htod.tod + kvm->arch.epoch; |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1174 | gtod->epoch_idx = 0; |
| 1175 | if (test_kvm_facility(kvm, 139)) { |
| 1176 | gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx; |
| 1177 | if (gtod->tod < htod.tod) |
| 1178 | gtod->epoch_idx += 1; |
| 1179 | } |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1180 | |
| 1181 | preempt_enable(); |
| 1182 | } |
| 1183 | |
| 1184 | static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1185 | { |
| 1186 | struct kvm_s390_vm_tod_clock gtod; |
| 1187 | |
| 1188 | memset(>od, 0, sizeof(gtod)); |
David Hildenbrand | 33d1b27 | 2018-04-27 14:36:13 +0200 | [diff] [blame] | 1189 | kvm_s390_get_tod_clock(kvm, >od); |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1190 | if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) |
| 1191 | return -EFAULT; |
| 1192 | |
| 1193 | VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", |
| 1194 | gtod.epoch_idx, gtod.tod); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1198 | static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1199 | { |
| 1200 | u8 gtod_high = 0; |
| 1201 | |
| 1202 | if (copy_to_user((void __user *)attr->addr, >od_high, |
| 1203 | sizeof(gtod_high))) |
| 1204 | return -EFAULT; |
Christian Borntraeger | 58c383c | 2015-10-12 13:27:29 +0200 | [diff] [blame] | 1205 | VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1211 | { |
David Hildenbrand | 5a3d883 | 2015-09-29 16:27:24 +0200 | [diff] [blame] | 1212 | u64 gtod; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1213 | |
David Hildenbrand | 60417fc | 2015-09-29 16:20:36 +0200 | [diff] [blame] | 1214 | gtod = kvm_s390_get_tod_clock_fast(kvm); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1215 | if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) |
| 1216 | return -EFAULT; |
Christian Borntraeger | 58c383c | 2015-10-12 13:27:29 +0200 | [diff] [blame] | 1217 | VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1223 | { |
| 1224 | int ret; |
| 1225 | |
| 1226 | if (attr->flags) |
| 1227 | return -EINVAL; |
| 1228 | |
| 1229 | switch (attr->attr) { |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 1230 | case KVM_S390_VM_TOD_EXT: |
| 1231 | ret = kvm_s390_get_tod_ext(kvm, attr); |
| 1232 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1233 | case KVM_S390_VM_TOD_HIGH: |
| 1234 | ret = kvm_s390_get_tod_high(kvm, attr); |
| 1235 | break; |
| 1236 | case KVM_S390_VM_TOD_LOW: |
| 1237 | ret = kvm_s390_get_tod_low(kvm, attr); |
| 1238 | break; |
| 1239 | default: |
| 1240 | ret = -ENXIO; |
| 1241 | break; |
| 1242 | } |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1246 | static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1247 | { |
| 1248 | struct kvm_s390_vm_cpu_processor *proc; |
David Hildenbrand | 053dd23 | 2016-04-04 13:59:42 +0200 | [diff] [blame] | 1249 | u16 lowest_ibc, unblocked_ibc; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1250 | int ret = 0; |
| 1251 | |
| 1252 | mutex_lock(&kvm->lock); |
Paolo Bonzini | a03825b | 2016-06-13 14:50:04 +0200 | [diff] [blame] | 1253 | if (kvm->created_vcpus) { |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1254 | ret = -EBUSY; |
| 1255 | goto out; |
| 1256 | } |
| 1257 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 1258 | if (!proc) { |
| 1259 | ret = -ENOMEM; |
| 1260 | goto out; |
| 1261 | } |
| 1262 | if (!copy_from_user(proc, (void __user *)attr->addr, |
| 1263 | sizeof(*proc))) { |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 1264 | kvm->arch.model.cpuid = proc->cpuid; |
David Hildenbrand | 053dd23 | 2016-04-04 13:59:42 +0200 | [diff] [blame] | 1265 | lowest_ibc = sclp.ibc >> 16 & 0xfff; |
| 1266 | unblocked_ibc = sclp.ibc & 0xfff; |
David Hildenbrand | 0487c44 | 2016-06-10 09:22:31 +0200 | [diff] [blame] | 1267 | if (lowest_ibc && proc->ibc) { |
David Hildenbrand | 053dd23 | 2016-04-04 13:59:42 +0200 | [diff] [blame] | 1268 | if (proc->ibc > unblocked_ibc) |
| 1269 | kvm->arch.model.ibc = unblocked_ibc; |
| 1270 | else if (proc->ibc < lowest_ibc) |
| 1271 | kvm->arch.model.ibc = lowest_ibc; |
| 1272 | else |
| 1273 | kvm->arch.model.ibc = proc->ibc; |
| 1274 | } |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 1275 | memcpy(kvm->arch.model.fac_list, proc->fac_list, |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1276 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
Christian Borntraeger | a8c39dd | 2017-01-18 16:01:02 +0100 | [diff] [blame] | 1277 | VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", |
| 1278 | kvm->arch.model.ibc, |
| 1279 | kvm->arch.model.cpuid); |
| 1280 | VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1281 | kvm->arch.model.fac_list[0], |
| 1282 | kvm->arch.model.fac_list[1], |
| 1283 | kvm->arch.model.fac_list[2]); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1284 | } else |
| 1285 | ret = -EFAULT; |
| 1286 | kfree(proc); |
| 1287 | out: |
| 1288 | mutex_unlock(&kvm->lock); |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1292 | static int kvm_s390_set_processor_feat(struct kvm *kvm, |
| 1293 | struct kvm_device_attr *attr) |
| 1294 | { |
| 1295 | struct kvm_s390_vm_cpu_feat data; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1296 | |
| 1297 | if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) |
| 1298 | return -EFAULT; |
| 1299 | if (!bitmap_subset((unsigned long *) data.feat, |
| 1300 | kvm_s390_available_cpu_feat, |
| 1301 | KVM_S390_VM_CPU_FEAT_NR_BITS)) |
| 1302 | return -EINVAL; |
| 1303 | |
| 1304 | mutex_lock(&kvm->lock); |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1305 | if (kvm->created_vcpus) { |
| 1306 | mutex_unlock(&kvm->lock); |
| 1307 | return -EBUSY; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1308 | } |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1309 | bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, |
| 1310 | KVM_S390_VM_CPU_FEAT_NR_BITS); |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1311 | mutex_unlock(&kvm->lock); |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1312 | VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", |
| 1313 | data.feat[0], |
| 1314 | data.feat[1], |
| 1315 | data.feat[2]); |
| 1316 | return 0; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1319 | static int kvm_s390_set_processor_subfunc(struct kvm *kvm, |
| 1320 | struct kvm_device_attr *attr) |
| 1321 | { |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1322 | mutex_lock(&kvm->lock); |
| 1323 | if (kvm->created_vcpus) { |
| 1324 | mutex_unlock(&kvm->lock); |
| 1325 | return -EBUSY; |
| 1326 | } |
| 1327 | |
| 1328 | if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, |
| 1329 | sizeof(struct kvm_s390_vm_cpu_subfunc))) { |
| 1330 | mutex_unlock(&kvm->lock); |
| 1331 | return -EFAULT; |
| 1332 | } |
| 1333 | mutex_unlock(&kvm->lock); |
| 1334 | |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1335 | VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1336 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], |
| 1337 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], |
| 1338 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], |
| 1339 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); |
| 1340 | VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", |
| 1341 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], |
| 1342 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); |
| 1343 | VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", |
| 1344 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], |
| 1345 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); |
| 1346 | VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", |
| 1347 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], |
| 1348 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); |
| 1349 | VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", |
| 1350 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], |
| 1351 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); |
| 1352 | VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", |
| 1353 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], |
| 1354 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); |
| 1355 | VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", |
| 1356 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], |
| 1357 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); |
| 1358 | VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", |
| 1359 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], |
| 1360 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); |
| 1361 | VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", |
| 1362 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], |
| 1363 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); |
| 1364 | VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", |
| 1365 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], |
| 1366 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); |
| 1367 | VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", |
| 1368 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], |
| 1369 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); |
| 1370 | VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", |
| 1371 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], |
| 1372 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); |
| 1373 | VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", |
| 1374 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], |
| 1375 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); |
| 1376 | VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", |
| 1377 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], |
| 1378 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 1379 | VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", |
| 1380 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], |
| 1381 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 1382 | VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1383 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], |
| 1384 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], |
| 1385 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], |
| 1386 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 1387 | VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1388 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], |
| 1389 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], |
| 1390 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], |
| 1391 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1392 | |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1393 | return 0; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1396 | static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1397 | { |
| 1398 | int ret = -ENXIO; |
| 1399 | |
| 1400 | switch (attr->attr) { |
| 1401 | case KVM_S390_VM_CPU_PROCESSOR: |
| 1402 | ret = kvm_s390_set_processor(kvm, attr); |
| 1403 | break; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1404 | case KVM_S390_VM_CPU_PROCESSOR_FEAT: |
| 1405 | ret = kvm_s390_set_processor_feat(kvm, attr); |
| 1406 | break; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1407 | case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: |
| 1408 | ret = kvm_s390_set_processor_subfunc(kvm, attr); |
| 1409 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1410 | } |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
| 1414 | static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1415 | { |
| 1416 | struct kvm_s390_vm_cpu_processor *proc; |
| 1417 | int ret = 0; |
| 1418 | |
| 1419 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 1420 | if (!proc) { |
| 1421 | ret = -ENOMEM; |
| 1422 | goto out; |
| 1423 | } |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 1424 | proc->cpuid = kvm->arch.model.cpuid; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1425 | proc->ibc = kvm->arch.model.ibc; |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 1426 | memcpy(&proc->fac_list, kvm->arch.model.fac_list, |
| 1427 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
Christian Borntraeger | a8c39dd | 2017-01-18 16:01:02 +0100 | [diff] [blame] | 1428 | VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", |
| 1429 | kvm->arch.model.ibc, |
| 1430 | kvm->arch.model.cpuid); |
| 1431 | VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1432 | kvm->arch.model.fac_list[0], |
| 1433 | kvm->arch.model.fac_list[1], |
| 1434 | kvm->arch.model.fac_list[2]); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1435 | if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) |
| 1436 | ret = -EFAULT; |
| 1437 | kfree(proc); |
| 1438 | out: |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
| 1442 | static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1443 | { |
| 1444 | struct kvm_s390_vm_cpu_machine *mach; |
| 1445 | int ret = 0; |
| 1446 | |
| 1447 | mach = kzalloc(sizeof(*mach), GFP_KERNEL); |
| 1448 | if (!mach) { |
| 1449 | ret = -ENOMEM; |
| 1450 | goto out; |
| 1451 | } |
| 1452 | get_cpu_id((struct cpuid *) &mach->cpuid); |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 1453 | mach->ibc = sclp.ibc; |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 1454 | memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, |
Michael Mueller | 981467c | 2015-02-24 13:51:04 +0100 | [diff] [blame] | 1455 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1456 | memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, |
Christian Borntraeger | 0447819 | 2017-01-12 16:25:15 +0100 | [diff] [blame] | 1457 | sizeof(S390_lowcore.stfle_fac_list)); |
Christian Borntraeger | a8c39dd | 2017-01-18 16:01:02 +0100 | [diff] [blame] | 1458 | VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", |
| 1459 | kvm->arch.model.ibc, |
| 1460 | kvm->arch.model.cpuid); |
| 1461 | VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1462 | mach->fac_mask[0], |
| 1463 | mach->fac_mask[1], |
| 1464 | mach->fac_mask[2]); |
| 1465 | VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", |
| 1466 | mach->fac_list[0], |
| 1467 | mach->fac_list[1], |
| 1468 | mach->fac_list[2]); |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1469 | if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) |
| 1470 | ret = -EFAULT; |
| 1471 | kfree(mach); |
| 1472 | out: |
| 1473 | return ret; |
| 1474 | } |
| 1475 | |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1476 | static int kvm_s390_get_processor_feat(struct kvm *kvm, |
| 1477 | struct kvm_device_attr *attr) |
| 1478 | { |
| 1479 | struct kvm_s390_vm_cpu_feat data; |
| 1480 | |
| 1481 | bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, |
| 1482 | KVM_S390_VM_CPU_FEAT_NR_BITS); |
| 1483 | if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) |
| 1484 | return -EFAULT; |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1485 | VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", |
| 1486 | data.feat[0], |
| 1487 | data.feat[1], |
| 1488 | data.feat[2]); |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | static int kvm_s390_get_machine_feat(struct kvm *kvm, |
| 1493 | struct kvm_device_attr *attr) |
| 1494 | { |
| 1495 | struct kvm_s390_vm_cpu_feat data; |
| 1496 | |
| 1497 | bitmap_copy((unsigned long *) data.feat, |
| 1498 | kvm_s390_available_cpu_feat, |
| 1499 | KVM_S390_VM_CPU_FEAT_NR_BITS); |
| 1500 | if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) |
| 1501 | return -EFAULT; |
Christian Borntraeger | 2f8311c | 2017-11-16 12:30:15 +0100 | [diff] [blame] | 1502 | VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", |
| 1503 | data.feat[0], |
| 1504 | data.feat[1], |
| 1505 | data.feat[2]); |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1506 | return 0; |
| 1507 | } |
| 1508 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1509 | static int kvm_s390_get_processor_subfunc(struct kvm *kvm, |
| 1510 | struct kvm_device_attr *attr) |
| 1511 | { |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1512 | if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, |
| 1513 | sizeof(struct kvm_s390_vm_cpu_subfunc))) |
| 1514 | return -EFAULT; |
| 1515 | |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1516 | VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1517 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], |
| 1518 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], |
| 1519 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], |
| 1520 | ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); |
| 1521 | VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", |
| 1522 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], |
| 1523 | ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); |
| 1524 | VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", |
| 1525 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], |
| 1526 | ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); |
| 1527 | VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", |
| 1528 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], |
| 1529 | ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); |
| 1530 | VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", |
| 1531 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], |
| 1532 | ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); |
| 1533 | VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", |
| 1534 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], |
| 1535 | ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); |
| 1536 | VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", |
| 1537 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], |
| 1538 | ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); |
| 1539 | VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", |
| 1540 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], |
| 1541 | ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); |
| 1542 | VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", |
| 1543 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], |
| 1544 | ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); |
| 1545 | VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", |
| 1546 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], |
| 1547 | ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); |
| 1548 | VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", |
| 1549 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], |
| 1550 | ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); |
| 1551 | VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", |
| 1552 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], |
| 1553 | ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); |
| 1554 | VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", |
| 1555 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], |
| 1556 | ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); |
| 1557 | VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", |
| 1558 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], |
| 1559 | ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 1560 | VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", |
| 1561 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], |
| 1562 | ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 1563 | VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1564 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], |
| 1565 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], |
| 1566 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], |
| 1567 | ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 1568 | VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1569 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], |
| 1570 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], |
| 1571 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], |
| 1572 | ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1573 | |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1574 | return 0; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | static int kvm_s390_get_machine_subfunc(struct kvm *kvm, |
| 1578 | struct kvm_device_attr *attr) |
| 1579 | { |
| 1580 | if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, |
| 1581 | sizeof(struct kvm_s390_vm_cpu_subfunc))) |
| 1582 | return -EFAULT; |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1583 | |
| 1584 | VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1585 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], |
| 1586 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], |
| 1587 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], |
| 1588 | ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); |
| 1589 | VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", |
| 1590 | ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], |
| 1591 | ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); |
| 1592 | VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", |
| 1593 | ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], |
| 1594 | ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); |
| 1595 | VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", |
| 1596 | ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], |
| 1597 | ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); |
| 1598 | VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", |
| 1599 | ((unsigned long *) &kvm_s390_available_subfunc.km)[0], |
| 1600 | ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); |
| 1601 | VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", |
| 1602 | ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], |
| 1603 | ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); |
| 1604 | VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", |
| 1605 | ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], |
| 1606 | ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); |
| 1607 | VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", |
| 1608 | ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], |
| 1609 | ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); |
| 1610 | VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", |
| 1611 | ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], |
| 1612 | ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); |
| 1613 | VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", |
| 1614 | ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], |
| 1615 | ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); |
| 1616 | VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", |
| 1617 | ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], |
| 1618 | ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); |
| 1619 | VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", |
| 1620 | ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], |
| 1621 | ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); |
| 1622 | VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", |
| 1623 | ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], |
| 1624 | ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); |
| 1625 | VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", |
| 1626 | ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], |
| 1627 | ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); |
Christian Borntraeger | 13209ad | 2018-12-28 09:33:35 +0100 | [diff] [blame] | 1628 | VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", |
| 1629 | ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], |
| 1630 | ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); |
Christian Borntraeger | 173aec2 | 2018-12-28 10:59:06 +0100 | [diff] [blame] | 1631 | VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1632 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], |
| 1633 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], |
| 1634 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], |
| 1635 | ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); |
Christian Borntraeger | 4f45b90 | 2018-12-28 10:46:04 +0100 | [diff] [blame] | 1636 | VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", |
| 1637 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], |
| 1638 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], |
| 1639 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], |
| 1640 | ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); |
Christian Borntraeger | 11ba596 | 2019-02-20 11:38:42 -0500 | [diff] [blame] | 1641 | |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1642 | return 0; |
| 1643 | } |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1644 | |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1645 | static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1646 | { |
| 1647 | int ret = -ENXIO; |
| 1648 | |
| 1649 | switch (attr->attr) { |
| 1650 | case KVM_S390_VM_CPU_PROCESSOR: |
| 1651 | ret = kvm_s390_get_processor(kvm, attr); |
| 1652 | break; |
| 1653 | case KVM_S390_VM_CPU_MACHINE: |
| 1654 | ret = kvm_s390_get_machine(kvm, attr); |
| 1655 | break; |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1656 | case KVM_S390_VM_CPU_PROCESSOR_FEAT: |
| 1657 | ret = kvm_s390_get_processor_feat(kvm, attr); |
| 1658 | break; |
| 1659 | case KVM_S390_VM_CPU_MACHINE_FEAT: |
| 1660 | ret = kvm_s390_get_machine_feat(kvm, attr); |
| 1661 | break; |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1662 | case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: |
| 1663 | ret = kvm_s390_get_processor_subfunc(kvm, attr); |
| 1664 | break; |
| 1665 | case KVM_S390_VM_CPU_MACHINE_SUBFUNC: |
| 1666 | ret = kvm_s390_get_machine_subfunc(kvm, attr); |
| 1667 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1668 | } |
| 1669 | return ret; |
| 1670 | } |
| 1671 | |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1672 | static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1673 | { |
| 1674 | int ret; |
| 1675 | |
| 1676 | switch (attr->group) { |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1677 | case KVM_S390_VM_MEM_CTRL: |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1678 | ret = kvm_s390_set_mem_control(kvm, attr); |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1679 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1680 | case KVM_S390_VM_TOD: |
| 1681 | ret = kvm_s390_set_tod(kvm, attr); |
| 1682 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1683 | case KVM_S390_VM_CPU_MODEL: |
| 1684 | ret = kvm_s390_set_cpu_model(kvm, attr); |
| 1685 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1686 | case KVM_S390_VM_CRYPTO: |
| 1687 | ret = kvm_s390_vm_set_crypto(kvm, attr); |
| 1688 | break; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1689 | case KVM_S390_VM_MIGRATION: |
| 1690 | ret = kvm_s390_vm_set_migration(kvm, attr); |
| 1691 | break; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1692 | default: |
| 1693 | ret = -ENXIO; |
| 1694 | break; |
| 1695 | } |
| 1696 | |
| 1697 | return ret; |
| 1698 | } |
| 1699 | |
| 1700 | static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1701 | { |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1702 | int ret; |
| 1703 | |
| 1704 | switch (attr->group) { |
| 1705 | case KVM_S390_VM_MEM_CTRL: |
| 1706 | ret = kvm_s390_get_mem_control(kvm, attr); |
| 1707 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1708 | case KVM_S390_VM_TOD: |
| 1709 | ret = kvm_s390_get_tod(kvm, attr); |
| 1710 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1711 | case KVM_S390_VM_CPU_MODEL: |
| 1712 | ret = kvm_s390_get_cpu_model(kvm, attr); |
| 1713 | break; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1714 | case KVM_S390_VM_MIGRATION: |
| 1715 | ret = kvm_s390_vm_get_migration(kvm, attr); |
| 1716 | break; |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1717 | default: |
| 1718 | ret = -ENXIO; |
| 1719 | break; |
| 1720 | } |
| 1721 | |
| 1722 | return ret; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1726 | { |
| 1727 | int ret; |
| 1728 | |
| 1729 | switch (attr->group) { |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1730 | case KVM_S390_VM_MEM_CTRL: |
| 1731 | switch (attr->attr) { |
| 1732 | case KVM_S390_VM_MEM_ENABLE_CMMA: |
| 1733 | case KVM_S390_VM_MEM_CLR_CMMA: |
David Hildenbrand | f9cbd9b | 2016-03-03 09:48:47 +0100 | [diff] [blame] | 1734 | ret = sclp.has_cmma ? 0 : -ENXIO; |
| 1735 | break; |
Dominik Dingel | 8c0a7ce | 2014-10-31 14:10:41 +0100 | [diff] [blame] | 1736 | case KVM_S390_VM_MEM_LIMIT_SIZE: |
Dominik Dingel | 4f718ea | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1737 | ret = 0; |
| 1738 | break; |
| 1739 | default: |
| 1740 | ret = -ENXIO; |
| 1741 | break; |
| 1742 | } |
| 1743 | break; |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 1744 | case KVM_S390_VM_TOD: |
| 1745 | switch (attr->attr) { |
| 1746 | case KVM_S390_VM_TOD_LOW: |
| 1747 | case KVM_S390_VM_TOD_HIGH: |
| 1748 | ret = 0; |
| 1749 | break; |
| 1750 | default: |
| 1751 | ret = -ENXIO; |
| 1752 | break; |
| 1753 | } |
| 1754 | break; |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1755 | case KVM_S390_VM_CPU_MODEL: |
| 1756 | switch (attr->attr) { |
| 1757 | case KVM_S390_VM_CPU_PROCESSOR: |
| 1758 | case KVM_S390_VM_CPU_MACHINE: |
David Hildenbrand | 15c9705 | 2015-03-19 17:36:43 +0100 | [diff] [blame] | 1759 | case KVM_S390_VM_CPU_PROCESSOR_FEAT: |
| 1760 | case KVM_S390_VM_CPU_MACHINE_FEAT: |
David Hildenbrand | 0a763c7 | 2016-05-18 16:03:47 +0200 | [diff] [blame] | 1761 | case KVM_S390_VM_CPU_MACHINE_SUBFUNC: |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 1762 | case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: |
Michael Mueller | 658b6ed | 2015-02-02 15:49:35 +0100 | [diff] [blame] | 1763 | ret = 0; |
| 1764 | break; |
| 1765 | default: |
| 1766 | ret = -ENXIO; |
| 1767 | break; |
| 1768 | } |
| 1769 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1770 | case KVM_S390_VM_CRYPTO: |
| 1771 | switch (attr->attr) { |
| 1772 | case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: |
| 1773 | case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: |
| 1774 | case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: |
| 1775 | case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: |
| 1776 | ret = 0; |
| 1777 | break; |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 1778 | case KVM_S390_VM_CRYPTO_ENABLE_APIE: |
| 1779 | case KVM_S390_VM_CRYPTO_DISABLE_APIE: |
| 1780 | ret = ap_instructions_available() ? 0 : -ENXIO; |
| 1781 | break; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 1782 | default: |
| 1783 | ret = -ENXIO; |
| 1784 | break; |
| 1785 | } |
| 1786 | break; |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 1787 | case KVM_S390_VM_MIGRATION: |
| 1788 | ret = 0; |
| 1789 | break; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 1790 | default: |
| 1791 | ret = -ENXIO; |
| 1792 | break; |
| 1793 | } |
| 1794 | |
| 1795 | return ret; |
| 1796 | } |
| 1797 | |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1798 | static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) |
| 1799 | { |
| 1800 | uint8_t *keys; |
| 1801 | uint64_t hva; |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1802 | int srcu_idx, i, r = 0; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1803 | |
| 1804 | if (args->flags != 0) |
| 1805 | return -EINVAL; |
| 1806 | |
| 1807 | /* Is this guest using storage keys? */ |
Janosch Frank | 55531b7 | 2018-02-15 16:33:47 +0100 | [diff] [blame] | 1808 | if (!mm_uses_skeys(current->mm)) |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1809 | return KVM_S390_GET_SKEYS_NONE; |
| 1810 | |
| 1811 | /* Enforce sane limit on memory allocation */ |
| 1812 | if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) |
| 1813 | return -EINVAL; |
| 1814 | |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 1815 | keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1816 | if (!keys) |
| 1817 | return -ENOMEM; |
| 1818 | |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1819 | down_read(¤t->mm->mmap_sem); |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1820 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1821 | for (i = 0; i < args->count; i++) { |
| 1822 | hva = gfn_to_hva(kvm, args->start_gfn + i); |
| 1823 | if (kvm_is_error_hva(hva)) { |
| 1824 | r = -EFAULT; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1825 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1826 | } |
| 1827 | |
David Hildenbrand | 154c8c1 | 2016-05-09 11:22:34 +0200 | [diff] [blame] | 1828 | r = get_guest_storage_key(current->mm, hva, &keys[i]); |
| 1829 | if (r) |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1830 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1831 | } |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1832 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1833 | up_read(¤t->mm->mmap_sem); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1834 | |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1835 | if (!r) { |
| 1836 | r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, |
| 1837 | sizeof(uint8_t) * args->count); |
| 1838 | if (r) |
| 1839 | r = -EFAULT; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1840 | } |
| 1841 | |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1842 | kvfree(keys); |
| 1843 | return r; |
| 1844 | } |
| 1845 | |
| 1846 | static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) |
| 1847 | { |
| 1848 | uint8_t *keys; |
| 1849 | uint64_t hva; |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1850 | int srcu_idx, i, r = 0; |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1851 | bool unlocked; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1852 | |
| 1853 | if (args->flags != 0) |
| 1854 | return -EINVAL; |
| 1855 | |
| 1856 | /* Enforce sane limit on memory allocation */ |
| 1857 | if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) |
| 1858 | return -EINVAL; |
| 1859 | |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 1860 | keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1861 | if (!keys) |
| 1862 | return -ENOMEM; |
| 1863 | |
| 1864 | r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, |
| 1865 | sizeof(uint8_t) * args->count); |
| 1866 | if (r) { |
| 1867 | r = -EFAULT; |
| 1868 | goto out; |
| 1869 | } |
| 1870 | |
| 1871 | /* Enable storage key handling for the guest */ |
Dominik Dingel | 14d4a42 | 2015-05-07 15:16:13 +0200 | [diff] [blame] | 1872 | r = s390_enable_skey(); |
| 1873 | if (r) |
| 1874 | goto out; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1875 | |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1876 | i = 0; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1877 | down_read(¤t->mm->mmap_sem); |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1878 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1879 | while (i < args->count) { |
| 1880 | unlocked = false; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1881 | hva = gfn_to_hva(kvm, args->start_gfn + i); |
| 1882 | if (kvm_is_error_hva(hva)) { |
| 1883 | r = -EFAULT; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1884 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /* Lowest order bit is reserved */ |
| 1888 | if (keys[i] & 0x01) { |
| 1889 | r = -EINVAL; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1890 | break; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1891 | } |
| 1892 | |
David Hildenbrand | fe69eab | 2016-05-09 13:08:07 +0200 | [diff] [blame] | 1893 | r = set_guest_storage_key(current->mm, hva, keys[i], 0); |
Janosch Frank | bd096f6 | 2018-07-18 13:40:22 +0100 | [diff] [blame] | 1894 | if (r) { |
| 1895 | r = fixup_user_fault(current, current->mm, hva, |
| 1896 | FAULT_FLAG_WRITE, &unlocked); |
| 1897 | if (r) |
| 1898 | break; |
| 1899 | } |
| 1900 | if (!r) |
| 1901 | i++; |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1902 | } |
Christian Borntraeger | 4f89914 | 2017-07-10 13:35:48 +0200 | [diff] [blame] | 1903 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 1904 | up_read(¤t->mm->mmap_sem); |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 1905 | out: |
| 1906 | kvfree(keys); |
| 1907 | return r; |
| 1908 | } |
| 1909 | |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 1910 | /* |
| 1911 | * Base address and length must be sent at the start of each block, therefore |
| 1912 | * it's cheaper to send some clean data, as long as it's less than the size of |
| 1913 | * two longs. |
| 1914 | */ |
| 1915 | #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) |
| 1916 | /* for consistency */ |
| 1917 | #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) |
| 1918 | |
| 1919 | /* |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 1920 | * Similar to gfn_to_memslot, but returns the index of a memslot also when the |
| 1921 | * address falls in a hole. In that case the index of one of the memslots |
| 1922 | * bordering the hole is returned. |
| 1923 | */ |
| 1924 | static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn) |
| 1925 | { |
| 1926 | int start = 0, end = slots->used_slots; |
| 1927 | int slot = atomic_read(&slots->lru_slot); |
| 1928 | struct kvm_memory_slot *memslots = slots->memslots; |
| 1929 | |
| 1930 | if (gfn >= memslots[slot].base_gfn && |
| 1931 | gfn < memslots[slot].base_gfn + memslots[slot].npages) |
| 1932 | return slot; |
| 1933 | |
| 1934 | while (start < end) { |
| 1935 | slot = start + (end - start) / 2; |
| 1936 | |
| 1937 | if (gfn >= memslots[slot].base_gfn) |
| 1938 | end = slot; |
| 1939 | else |
| 1940 | start = slot + 1; |
| 1941 | } |
| 1942 | |
| 1943 | if (gfn >= memslots[start].base_gfn && |
| 1944 | gfn < memslots[start].base_gfn + memslots[start].npages) { |
| 1945 | atomic_set(&slots->lru_slot, start); |
| 1946 | } |
| 1947 | |
| 1948 | return start; |
| 1949 | } |
| 1950 | |
| 1951 | static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, |
| 1952 | u8 *res, unsigned long bufsize) |
| 1953 | { |
| 1954 | unsigned long pgstev, hva, cur_gfn = args->start_gfn; |
| 1955 | |
| 1956 | args->count = 0; |
| 1957 | while (args->count < bufsize) { |
| 1958 | hva = gfn_to_hva(kvm, cur_gfn); |
| 1959 | /* |
| 1960 | * We return an error if the first value was invalid, but we |
| 1961 | * return successfully if at least one value was copied. |
| 1962 | */ |
| 1963 | if (kvm_is_error_hva(hva)) |
| 1964 | return args->count ? 0 : -EFAULT; |
| 1965 | if (get_pgste(kvm->mm, hva, &pgstev) < 0) |
| 1966 | pgstev = 0; |
| 1967 | res[args->count++] = (pgstev >> 24) & 0x43; |
| 1968 | cur_gfn++; |
| 1969 | } |
| 1970 | |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
| 1974 | static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, |
| 1975 | unsigned long cur_gfn) |
| 1976 | { |
| 1977 | int slotidx = gfn_to_memslot_approx(slots, cur_gfn); |
| 1978 | struct kvm_memory_slot *ms = slots->memslots + slotidx; |
| 1979 | unsigned long ofs = cur_gfn - ms->base_gfn; |
| 1980 | |
| 1981 | if (ms->base_gfn + ms->npages <= cur_gfn) { |
| 1982 | slotidx--; |
| 1983 | /* If we are above the highest slot, wrap around */ |
| 1984 | if (slotidx < 0) |
| 1985 | slotidx = slots->used_slots - 1; |
| 1986 | |
| 1987 | ms = slots->memslots + slotidx; |
| 1988 | ofs = 0; |
| 1989 | } |
| 1990 | ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); |
| 1991 | while ((slotidx > 0) && (ofs >= ms->npages)) { |
| 1992 | slotidx--; |
| 1993 | ms = slots->memslots + slotidx; |
| 1994 | ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0); |
| 1995 | } |
| 1996 | return ms->base_gfn + ofs; |
| 1997 | } |
| 1998 | |
| 1999 | static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, |
| 2000 | u8 *res, unsigned long bufsize) |
| 2001 | { |
| 2002 | unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; |
| 2003 | struct kvm_memslots *slots = kvm_memslots(kvm); |
| 2004 | struct kvm_memory_slot *ms; |
| 2005 | |
| 2006 | cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); |
| 2007 | ms = gfn_to_memslot(kvm, cur_gfn); |
| 2008 | args->count = 0; |
| 2009 | args->start_gfn = cur_gfn; |
| 2010 | if (!ms) |
| 2011 | return 0; |
| 2012 | next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); |
| 2013 | mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages; |
| 2014 | |
| 2015 | while (args->count < bufsize) { |
| 2016 | hva = gfn_to_hva(kvm, cur_gfn); |
| 2017 | if (kvm_is_error_hva(hva)) |
| 2018 | return 0; |
| 2019 | /* Decrement only if we actually flipped the bit to 0 */ |
| 2020 | if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) |
| 2021 | atomic64_dec(&kvm->arch.cmma_dirty_pages); |
| 2022 | if (get_pgste(kvm->mm, hva, &pgstev) < 0) |
| 2023 | pgstev = 0; |
| 2024 | /* Save the value */ |
| 2025 | res[args->count++] = (pgstev >> 24) & 0x43; |
| 2026 | /* If the next bit is too far away, stop. */ |
| 2027 | if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) |
| 2028 | return 0; |
| 2029 | /* If we reached the previous "next", find the next one */ |
| 2030 | if (cur_gfn == next_gfn) |
| 2031 | next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); |
| 2032 | /* Reached the end of memory or of the buffer, stop */ |
| 2033 | if ((next_gfn >= mem_end) || |
| 2034 | (next_gfn - args->start_gfn >= bufsize)) |
| 2035 | return 0; |
| 2036 | cur_gfn++; |
| 2037 | /* Reached the end of the current memslot, take the next one. */ |
| 2038 | if (cur_gfn - ms->base_gfn >= ms->npages) { |
| 2039 | ms = gfn_to_memslot(kvm, cur_gfn); |
| 2040 | if (!ms) |
| 2041 | return 0; |
| 2042 | } |
| 2043 | } |
| 2044 | return 0; |
| 2045 | } |
| 2046 | |
| 2047 | /* |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2048 | * This function searches for the next page with dirty CMMA attributes, and |
| 2049 | * saves the attributes in the buffer up to either the end of the buffer or |
| 2050 | * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; |
| 2051 | * no trailing clean bytes are saved. |
| 2052 | * In case no dirty bits were found, or if CMMA was not enabled or used, the |
| 2053 | * output buffer will indicate 0 as length. |
| 2054 | */ |
| 2055 | static int kvm_s390_get_cmma_bits(struct kvm *kvm, |
| 2056 | struct kvm_s390_cmma_log *args) |
| 2057 | { |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2058 | unsigned long bufsize; |
| 2059 | int srcu_idx, peek, ret; |
| 2060 | u8 *values; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2061 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2062 | if (!kvm->arch.use_cmma) |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2063 | return -ENXIO; |
| 2064 | /* Invalid/unsupported flags were specified */ |
| 2065 | if (args->flags & ~KVM_S390_CMMA_PEEK) |
| 2066 | return -EINVAL; |
| 2067 | /* Migration mode query, and we are not doing a migration */ |
| 2068 | peek = !!(args->flags & KVM_S390_CMMA_PEEK); |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2069 | if (!peek && !kvm->arch.migration_mode) |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2070 | return -EINVAL; |
| 2071 | /* CMMA is disabled or was not used, or the buffer has length zero */ |
| 2072 | bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2073 | if (!bufsize || !kvm->mm->context.uses_cmm) { |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2074 | memset(args, 0, sizeof(*args)); |
| 2075 | return 0; |
| 2076 | } |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2077 | /* We are not peeking, and there are no dirty pages */ |
| 2078 | if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { |
| 2079 | memset(args, 0, sizeof(*args)); |
| 2080 | return 0; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2081 | } |
| 2082 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2083 | values = vmalloc(bufsize); |
| 2084 | if (!values) |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2085 | return -ENOMEM; |
| 2086 | |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2087 | down_read(&kvm->mm->mmap_sem); |
| 2088 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2089 | if (peek) |
| 2090 | ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); |
| 2091 | else |
| 2092 | ret = kvm_s390_get_cmma(kvm, args, values, bufsize); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2093 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 2094 | up_read(&kvm->mm->mmap_sem); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2095 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2096 | if (kvm->arch.migration_mode) |
| 2097 | args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); |
| 2098 | else |
| 2099 | args->remaining = 0; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2100 | |
Claudio Imbrenda | afdad61 | 2018-04-30 18:33:25 +0200 | [diff] [blame] | 2101 | if (copy_to_user((void __user *)args->values, values, args->count)) |
| 2102 | ret = -EFAULT; |
| 2103 | |
| 2104 | vfree(values); |
| 2105 | return ret; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /* |
| 2109 | * This function sets the CMMA attributes for the given pages. If the input |
| 2110 | * buffer has zero length, no action is taken, otherwise the attributes are |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2111 | * set and the mm->context.uses_cmm flag is set. |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2112 | */ |
| 2113 | static int kvm_s390_set_cmma_bits(struct kvm *kvm, |
| 2114 | const struct kvm_s390_cmma_log *args) |
| 2115 | { |
| 2116 | unsigned long hva, mask, pgstev, i; |
| 2117 | uint8_t *bits; |
| 2118 | int srcu_idx, r = 0; |
| 2119 | |
| 2120 | mask = args->mask; |
| 2121 | |
| 2122 | if (!kvm->arch.use_cmma) |
| 2123 | return -ENXIO; |
| 2124 | /* invalid/unsupported flags */ |
| 2125 | if (args->flags != 0) |
| 2126 | return -EINVAL; |
| 2127 | /* Enforce sane limit on memory allocation */ |
| 2128 | if (args->count > KVM_S390_CMMA_SIZE_MAX) |
| 2129 | return -EINVAL; |
| 2130 | /* Nothing to do */ |
| 2131 | if (args->count == 0) |
| 2132 | return 0; |
| 2133 | |
Kees Cook | 42bc47b | 2018-06-12 14:27:11 -0700 | [diff] [blame] | 2134 | bits = vmalloc(array_size(sizeof(*bits), args->count)); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2135 | if (!bits) |
| 2136 | return -ENOMEM; |
| 2137 | |
| 2138 | r = copy_from_user(bits, (void __user *)args->values, args->count); |
| 2139 | if (r) { |
| 2140 | r = -EFAULT; |
| 2141 | goto out; |
| 2142 | } |
| 2143 | |
| 2144 | down_read(&kvm->mm->mmap_sem); |
| 2145 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 2146 | for (i = 0; i < args->count; i++) { |
| 2147 | hva = gfn_to_hva(kvm, args->start_gfn + i); |
| 2148 | if (kvm_is_error_hva(hva)) { |
| 2149 | r = -EFAULT; |
| 2150 | break; |
| 2151 | } |
| 2152 | |
| 2153 | pgstev = bits[i]; |
| 2154 | pgstev = pgstev << 24; |
Claudio Imbrenda | 1bab1c0 | 2016-08-29 15:56:55 +0200 | [diff] [blame] | 2155 | mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2156 | set_pgste_bits(kvm->mm, hva, mask, pgstev); |
| 2157 | } |
| 2158 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 2159 | up_read(&kvm->mm->mmap_sem); |
| 2160 | |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2161 | if (!kvm->mm->context.uses_cmm) { |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2162 | down_write(&kvm->mm->mmap_sem); |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2163 | kvm->mm->context.uses_cmm = 1; |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2164 | up_write(&kvm->mm->mmap_sem); |
| 2165 | } |
| 2166 | out: |
| 2167 | vfree(bits); |
| 2168 | return r; |
| 2169 | } |
| 2170 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2171 | static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp) |
| 2172 | { |
| 2173 | struct kvm_vcpu *vcpu; |
| 2174 | u16 rc, rrc; |
| 2175 | int ret = 0; |
| 2176 | int i; |
| 2177 | |
| 2178 | /* |
| 2179 | * We ignore failures and try to destroy as many CPUs as possible. |
| 2180 | * At the same time we must not free the assigned resources when |
| 2181 | * this fails, as the ultravisor has still access to that memory. |
| 2182 | * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak |
| 2183 | * behind. |
| 2184 | * We want to return the first failure rc and rrc, though. |
| 2185 | */ |
| 2186 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2187 | mutex_lock(&vcpu->mutex); |
| 2188 | if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) { |
| 2189 | *rcp = rc; |
| 2190 | *rrcp = rrc; |
| 2191 | ret = -EIO; |
| 2192 | } |
| 2193 | mutex_unlock(&vcpu->mutex); |
| 2194 | } |
| 2195 | return ret; |
| 2196 | } |
| 2197 | |
| 2198 | static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) |
| 2199 | { |
| 2200 | int i, r = 0; |
| 2201 | u16 dummy; |
| 2202 | |
| 2203 | struct kvm_vcpu *vcpu; |
| 2204 | |
| 2205 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2206 | mutex_lock(&vcpu->mutex); |
| 2207 | r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); |
| 2208 | mutex_unlock(&vcpu->mutex); |
| 2209 | if (r) |
| 2210 | break; |
| 2211 | } |
| 2212 | if (r) |
| 2213 | kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); |
| 2214 | return r; |
| 2215 | } |
| 2216 | |
| 2217 | static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) |
| 2218 | { |
| 2219 | int r = 0; |
| 2220 | u16 dummy; |
| 2221 | void __user *argp = (void __user *)cmd->data; |
| 2222 | |
| 2223 | switch (cmd->cmd) { |
| 2224 | case KVM_PV_ENABLE: { |
| 2225 | r = -EINVAL; |
| 2226 | if (kvm_s390_pv_is_protected(kvm)) |
| 2227 | break; |
| 2228 | |
| 2229 | /* |
| 2230 | * FMT 4 SIE needs esca. As we never switch back to bsca from |
| 2231 | * esca, we need no cleanup in the error cases below |
| 2232 | */ |
| 2233 | r = sca_switch_to_extended(kvm); |
| 2234 | if (r) |
| 2235 | break; |
| 2236 | |
Janosch Frank | fa0c5ea | 2019-07-16 13:08:37 +0200 | [diff] [blame] | 2237 | down_write(¤t->mm->mmap_sem); |
| 2238 | r = gmap_mark_unmergeable(); |
| 2239 | up_write(¤t->mm->mmap_sem); |
| 2240 | if (r) |
| 2241 | break; |
| 2242 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2243 | r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); |
| 2244 | if (r) |
| 2245 | break; |
| 2246 | |
| 2247 | r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); |
| 2248 | if (r) |
| 2249 | kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); |
Christian Borntraeger | 0890dde | 2020-02-03 09:13:37 +0100 | [diff] [blame^] | 2250 | |
| 2251 | /* we need to block service interrupts from now on */ |
| 2252 | set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2253 | break; |
| 2254 | } |
| 2255 | case KVM_PV_DISABLE: { |
| 2256 | r = -EINVAL; |
| 2257 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2258 | break; |
| 2259 | |
| 2260 | r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); |
| 2261 | /* |
| 2262 | * If a CPU could not be destroyed, destroy VM will also fail. |
| 2263 | * There is no point in trying to destroy it. Instead return |
| 2264 | * the rc and rrc from the first CPU that failed destroying. |
| 2265 | */ |
| 2266 | if (r) |
| 2267 | break; |
| 2268 | r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc); |
Christian Borntraeger | 0890dde | 2020-02-03 09:13:37 +0100 | [diff] [blame^] | 2269 | |
| 2270 | /* no need to block service interrupts any more */ |
| 2271 | clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2272 | break; |
| 2273 | } |
| 2274 | case KVM_PV_SET_SEC_PARMS: { |
| 2275 | struct kvm_s390_pv_sec_parm parms = {}; |
| 2276 | void *hdr; |
| 2277 | |
| 2278 | r = -EINVAL; |
| 2279 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2280 | break; |
| 2281 | |
| 2282 | r = -EFAULT; |
| 2283 | if (copy_from_user(&parms, argp, sizeof(parms))) |
| 2284 | break; |
| 2285 | |
| 2286 | /* Currently restricted to 8KB */ |
| 2287 | r = -EINVAL; |
| 2288 | if (parms.length > PAGE_SIZE * 2) |
| 2289 | break; |
| 2290 | |
| 2291 | r = -ENOMEM; |
| 2292 | hdr = vmalloc(parms.length); |
| 2293 | if (!hdr) |
| 2294 | break; |
| 2295 | |
| 2296 | r = -EFAULT; |
| 2297 | if (!copy_from_user(hdr, (void __user *)parms.origin, |
| 2298 | parms.length)) |
| 2299 | r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, |
| 2300 | &cmd->rc, &cmd->rrc); |
| 2301 | |
| 2302 | vfree(hdr); |
| 2303 | break; |
| 2304 | } |
| 2305 | case KVM_PV_UNPACK: { |
| 2306 | struct kvm_s390_pv_unp unp = {}; |
| 2307 | |
| 2308 | r = -EINVAL; |
| 2309 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2310 | break; |
| 2311 | |
| 2312 | r = -EFAULT; |
| 2313 | if (copy_from_user(&unp, argp, sizeof(unp))) |
| 2314 | break; |
| 2315 | |
| 2316 | r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, |
| 2317 | &cmd->rc, &cmd->rrc); |
| 2318 | break; |
| 2319 | } |
| 2320 | case KVM_PV_VERIFY: { |
| 2321 | r = -EINVAL; |
| 2322 | if (!kvm_s390_pv_is_protected(kvm)) |
| 2323 | break; |
| 2324 | |
| 2325 | r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), |
| 2326 | UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); |
| 2327 | KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, |
| 2328 | cmd->rrc); |
| 2329 | break; |
| 2330 | } |
| 2331 | default: |
| 2332 | r = -ENOTTY; |
| 2333 | } |
| 2334 | return r; |
| 2335 | } |
| 2336 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2337 | long kvm_arch_vm_ioctl(struct file *filp, |
| 2338 | unsigned int ioctl, unsigned long arg) |
| 2339 | { |
| 2340 | struct kvm *kvm = filp->private_data; |
| 2341 | void __user *argp = (void __user *)arg; |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 2342 | struct kvm_device_attr attr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2343 | int r; |
| 2344 | |
| 2345 | switch (ioctl) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 2346 | case KVM_S390_INTERRUPT: { |
| 2347 | struct kvm_s390_interrupt s390int; |
| 2348 | |
| 2349 | r = -EFAULT; |
| 2350 | if (copy_from_user(&s390int, argp, sizeof(s390int))) |
| 2351 | break; |
| 2352 | r = kvm_s390_inject_vm(kvm, &s390int); |
| 2353 | break; |
| 2354 | } |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2355 | case KVM_CREATE_IRQCHIP: { |
| 2356 | struct kvm_irq_routing_entry routing; |
| 2357 | |
| 2358 | r = -EINVAL; |
| 2359 | if (kvm->arch.use_irqchip) { |
| 2360 | /* Set up dummy routing. */ |
| 2361 | memset(&routing, 0, sizeof(routing)); |
Nicholas Krause | 152b283 | 2015-08-06 13:05:54 -0400 | [diff] [blame] | 2362 | r = kvm_set_irq_routing(kvm, &routing, 0, 0); |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2363 | } |
| 2364 | break; |
| 2365 | } |
Dominik Dingel | f206165 | 2014-04-09 13:13:00 +0200 | [diff] [blame] | 2366 | case KVM_SET_DEVICE_ATTR: { |
| 2367 | r = -EFAULT; |
| 2368 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 2369 | break; |
| 2370 | r = kvm_s390_vm_set_attr(kvm, &attr); |
| 2371 | break; |
| 2372 | } |
| 2373 | case KVM_GET_DEVICE_ATTR: { |
| 2374 | r = -EFAULT; |
| 2375 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 2376 | break; |
| 2377 | r = kvm_s390_vm_get_attr(kvm, &attr); |
| 2378 | break; |
| 2379 | } |
| 2380 | case KVM_HAS_DEVICE_ATTR: { |
| 2381 | r = -EFAULT; |
| 2382 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 2383 | break; |
| 2384 | r = kvm_s390_vm_has_attr(kvm, &attr); |
| 2385 | break; |
| 2386 | } |
Jason J. Herne | 30ee2a9 | 2014-09-23 09:23:01 -0400 | [diff] [blame] | 2387 | case KVM_S390_GET_SKEYS: { |
| 2388 | struct kvm_s390_skeys args; |
| 2389 | |
| 2390 | r = -EFAULT; |
| 2391 | if (copy_from_user(&args, argp, |
| 2392 | sizeof(struct kvm_s390_skeys))) |
| 2393 | break; |
| 2394 | r = kvm_s390_get_skeys(kvm, &args); |
| 2395 | break; |
| 2396 | } |
| 2397 | case KVM_S390_SET_SKEYS: { |
| 2398 | struct kvm_s390_skeys args; |
| 2399 | |
| 2400 | r = -EFAULT; |
| 2401 | if (copy_from_user(&args, argp, |
| 2402 | sizeof(struct kvm_s390_skeys))) |
| 2403 | break; |
| 2404 | r = kvm_s390_set_skeys(kvm, &args); |
| 2405 | break; |
| 2406 | } |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2407 | case KVM_S390_GET_CMMA_BITS: { |
| 2408 | struct kvm_s390_cmma_log args; |
| 2409 | |
| 2410 | r = -EFAULT; |
| 2411 | if (copy_from_user(&args, argp, sizeof(args))) |
| 2412 | break; |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2413 | mutex_lock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2414 | r = kvm_s390_get_cmma_bits(kvm, &args); |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2415 | mutex_unlock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2416 | if (!r) { |
| 2417 | r = copy_to_user(argp, &args, sizeof(args)); |
| 2418 | if (r) |
| 2419 | r = -EFAULT; |
| 2420 | } |
| 2421 | break; |
| 2422 | } |
| 2423 | case KVM_S390_SET_CMMA_BITS: { |
| 2424 | struct kvm_s390_cmma_log args; |
| 2425 | |
| 2426 | r = -EFAULT; |
| 2427 | if (copy_from_user(&args, argp, sizeof(args))) |
| 2428 | break; |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2429 | mutex_lock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2430 | r = kvm_s390_set_cmma_bits(kvm, &args); |
Christian Borntraeger | 1de1ea7 | 2017-12-22 10:54:20 +0100 | [diff] [blame] | 2431 | mutex_unlock(&kvm->slots_lock); |
Claudio Imbrenda | 4036e38 | 2016-08-04 17:58:47 +0200 | [diff] [blame] | 2432 | break; |
| 2433 | } |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2434 | case KVM_S390_PV_COMMAND: { |
| 2435 | struct kvm_pv_cmd args; |
| 2436 | |
| 2437 | r = 0; |
| 2438 | if (!is_prot_virt_host()) { |
| 2439 | r = -EINVAL; |
| 2440 | break; |
| 2441 | } |
| 2442 | if (copy_from_user(&args, argp, sizeof(args))) { |
| 2443 | r = -EFAULT; |
| 2444 | break; |
| 2445 | } |
| 2446 | if (args.flags) { |
| 2447 | r = -EINVAL; |
| 2448 | break; |
| 2449 | } |
| 2450 | mutex_lock(&kvm->lock); |
| 2451 | r = kvm_s390_handle_pv(kvm, &args); |
| 2452 | mutex_unlock(&kvm->lock); |
| 2453 | if (copy_to_user(argp, &args, sizeof(args))) { |
| 2454 | r = -EFAULT; |
| 2455 | break; |
| 2456 | } |
| 2457 | break; |
| 2458 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2459 | default: |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 2460 | r = -ENOTTY; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | return r; |
| 2464 | } |
| 2465 | |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2466 | static int kvm_s390_apxa_installed(void) |
| 2467 | { |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2468 | struct ap_config_info info; |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2469 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2470 | if (ap_instructions_available()) { |
| 2471 | if (ap_qci(&info) == 0) |
| 2472 | return info.apxa; |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | return 0; |
| 2476 | } |
| 2477 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2478 | /* |
| 2479 | * The format of the crypto control block (CRYCB) is specified in the 3 low |
| 2480 | * order bits of the CRYCB designation (CRYCBD) field as follows: |
| 2481 | * Format 0: Neither the message security assist extension 3 (MSAX3) nor the |
| 2482 | * AP extended addressing (APXA) facility are installed. |
| 2483 | * Format 1: The APXA facility is not installed but the MSAX3 facility is. |
| 2484 | * Format 2: Both the APXA and MSAX3 facilities are installed |
| 2485 | */ |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2486 | static void kvm_s390_set_crycb_format(struct kvm *kvm) |
| 2487 | { |
| 2488 | kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; |
| 2489 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2490 | /* Clear the CRYCB format bits - i.e., set format 0 by default */ |
| 2491 | kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); |
| 2492 | |
| 2493 | /* Check whether MSAX3 is installed */ |
| 2494 | if (!test_kvm_facility(kvm, 76)) |
| 2495 | return; |
| 2496 | |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2497 | if (kvm_s390_apxa_installed()) |
| 2498 | kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; |
| 2499 | else |
| 2500 | kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; |
| 2501 | } |
| 2502 | |
Pierre Morel | 0e237e4 | 2018-10-05 10:31:09 +0200 | [diff] [blame] | 2503 | void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, |
| 2504 | unsigned long *aqm, unsigned long *adm) |
| 2505 | { |
| 2506 | struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; |
| 2507 | |
| 2508 | mutex_lock(&kvm->lock); |
| 2509 | kvm_s390_vcpu_block_all(kvm); |
| 2510 | |
| 2511 | switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { |
| 2512 | case CRYCB_FORMAT2: /* APCB1 use 256 bits */ |
| 2513 | memcpy(crycb->apcb1.apm, apm, 32); |
| 2514 | VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", |
| 2515 | apm[0], apm[1], apm[2], apm[3]); |
| 2516 | memcpy(crycb->apcb1.aqm, aqm, 32); |
| 2517 | VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", |
| 2518 | aqm[0], aqm[1], aqm[2], aqm[3]); |
| 2519 | memcpy(crycb->apcb1.adm, adm, 32); |
| 2520 | VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", |
| 2521 | adm[0], adm[1], adm[2], adm[3]); |
| 2522 | break; |
| 2523 | case CRYCB_FORMAT1: |
| 2524 | case CRYCB_FORMAT0: /* Fall through both use APCB0 */ |
| 2525 | memcpy(crycb->apcb0.apm, apm, 8); |
| 2526 | memcpy(crycb->apcb0.aqm, aqm, 2); |
| 2527 | memcpy(crycb->apcb0.adm, adm, 2); |
| 2528 | VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", |
| 2529 | apm[0], *((unsigned short *)aqm), |
| 2530 | *((unsigned short *)adm)); |
| 2531 | break; |
| 2532 | default: /* Can not happen */ |
| 2533 | break; |
| 2534 | } |
| 2535 | |
| 2536 | /* recreate the shadow crycb for each vcpu */ |
| 2537 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); |
| 2538 | kvm_s390_vcpu_unblock_all(kvm); |
| 2539 | mutex_unlock(&kvm->lock); |
| 2540 | } |
| 2541 | EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); |
| 2542 | |
Tony Krowiak | 42104598 | 2018-09-25 19:16:25 -0400 | [diff] [blame] | 2543 | void kvm_arch_crypto_clear_masks(struct kvm *kvm) |
| 2544 | { |
| 2545 | mutex_lock(&kvm->lock); |
| 2546 | kvm_s390_vcpu_block_all(kvm); |
| 2547 | |
| 2548 | memset(&kvm->arch.crypto.crycb->apcb0, 0, |
| 2549 | sizeof(kvm->arch.crypto.crycb->apcb0)); |
| 2550 | memset(&kvm->arch.crypto.crycb->apcb1, 0, |
| 2551 | sizeof(kvm->arch.crypto.crycb->apcb1)); |
| 2552 | |
Pierre Morel | 0e237e4 | 2018-10-05 10:31:09 +0200 | [diff] [blame] | 2553 | VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); |
Pierre Morel | 6cc571b | 2018-09-25 19:16:30 -0400 | [diff] [blame] | 2554 | /* recreate the shadow crycb for each vcpu */ |
| 2555 | kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); |
Tony Krowiak | 42104598 | 2018-09-25 19:16:25 -0400 | [diff] [blame] | 2556 | kvm_s390_vcpu_unblock_all(kvm); |
| 2557 | mutex_unlock(&kvm->lock); |
| 2558 | } |
| 2559 | EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); |
| 2560 | |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 2561 | static u64 kvm_s390_get_initial_cpuid(void) |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2562 | { |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 2563 | struct cpuid cpuid; |
| 2564 | |
| 2565 | get_cpu_id(&cpuid); |
| 2566 | cpuid.version = 0xff; |
| 2567 | return *((u64 *) &cpuid); |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2570 | static void kvm_s390_crypto_init(struct kvm *kvm) |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2571 | { |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2572 | kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; |
Tony Krowiak | 45c9b47 | 2015-01-13 11:33:26 -0500 | [diff] [blame] | 2573 | kvm_s390_set_crycb_format(kvm); |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2574 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 2575 | if (!test_kvm_facility(kvm, 76)) |
| 2576 | return; |
| 2577 | |
Tony Krowiak | ed6f76b | 2015-02-24 14:06:57 -0500 | [diff] [blame] | 2578 | /* Enable AES/DEA protected key functions by default */ |
| 2579 | kvm->arch.crypto.aes_kw = 1; |
| 2580 | kvm->arch.crypto.dea_kw = 1; |
| 2581 | get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, |
| 2582 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); |
| 2583 | get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, |
| 2584 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2585 | } |
| 2586 | |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2587 | static void sca_dispose(struct kvm *kvm) |
| 2588 | { |
| 2589 | if (kvm->arch.use_esca) |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2590 | free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2591 | else |
| 2592 | free_page((unsigned long)(kvm->arch.sca)); |
| 2593 | kvm->arch.sca = NULL; |
| 2594 | } |
| 2595 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2596 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2597 | { |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2598 | gfp_t alloc_flags = GFP_KERNEL; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2599 | int i, rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2600 | char debug_name[16]; |
Christian Borntraeger | f6c137f | 2014-03-19 11:18:29 +0100 | [diff] [blame] | 2601 | static unsigned long sca_offset; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2602 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2603 | rc = -EINVAL; |
| 2604 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 2605 | if (type & ~KVM_VM_S390_UCONTROL) |
| 2606 | goto out_err; |
| 2607 | if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) |
| 2608 | goto out_err; |
| 2609 | #else |
| 2610 | if (type) |
| 2611 | goto out_err; |
| 2612 | #endif |
| 2613 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2614 | rc = s390_enable_sie(); |
| 2615 | if (rc) |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2616 | goto out_err; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2617 | |
Carsten Otte | b290411 | 2011-10-18 12:27:13 +0200 | [diff] [blame] | 2618 | rc = -ENOMEM; |
| 2619 | |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2620 | if (!sclp.has_64bscao) |
| 2621 | alloc_flags |= GFP_DMA; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2622 | rwlock_init(&kvm->arch.sca_lock); |
David Hildenbrand | 9ac96d7 | 2018-04-27 14:36:12 +0200 | [diff] [blame] | 2623 | /* start with basic SCA */ |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2624 | kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2625 | if (!kvm->arch.sca) |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2626 | goto out_err; |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 2627 | mutex_lock(&kvm_lock); |
David Hildenbrand | c5c2c39 | 2015-10-26 08:41:29 +0100 | [diff] [blame] | 2628 | sca_offset += 16; |
Eugene (jno) Dvurechenski | bc784cc | 2015-04-23 16:09:06 +0200 | [diff] [blame] | 2629 | if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) |
David Hildenbrand | c5c2c39 | 2015-10-26 08:41:29 +0100 | [diff] [blame] | 2630 | sca_offset = 0; |
Eugene (jno) Dvurechenski | bc784cc | 2015-04-23 16:09:06 +0200 | [diff] [blame] | 2631 | kvm->arch.sca = (struct bsca_block *) |
| 2632 | ((char *) kvm->arch.sca + sca_offset); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 2633 | mutex_unlock(&kvm_lock); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2634 | |
| 2635 | sprintf(debug_name, "kvm-%u", current->pid); |
| 2636 | |
Christian Borntraeger | 1cb9cf7 | 2015-07-20 15:04:48 +0200 | [diff] [blame] | 2637 | kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2638 | if (!kvm->arch.dbf) |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2639 | goto out_err; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2640 | |
Michael Mueller | 19114be | 2017-05-30 14:26:02 +0200 | [diff] [blame] | 2641 | BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2642 | kvm->arch.sie_page2 = |
| 2643 | (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 2644 | if (!kvm->arch.sie_page2) |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2645 | goto out_err; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2646 | |
Michael Mueller | 25c84db | 2019-01-31 09:52:41 +0100 | [diff] [blame] | 2647 | kvm->arch.sie_page2->kvm = kvm; |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2648 | kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 2649 | |
| 2650 | for (i = 0; i < kvm_s390_fac_size(); i++) { |
| 2651 | kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] & |
| 2652 | (kvm_s390_fac_base[i] | |
| 2653 | kvm_s390_fac_ext[i]); |
| 2654 | kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] & |
| 2655 | kvm_s390_fac_base[i]; |
| 2656 | } |
Christian Borntraeger | 346fa2f | 2019-02-18 07:48:25 -0500 | [diff] [blame] | 2657 | kvm->arch.model.subfuncs = kvm_s390_available_subfunc; |
Michael Mueller | 981467c | 2015-02-24 13:51:04 +0100 | [diff] [blame] | 2658 | |
David Hildenbrand | 1935222 | 2017-08-29 16:31:08 +0200 | [diff] [blame] | 2659 | /* we are always in czam mode - even on pre z14 machines */ |
| 2660 | set_kvm_facility(kvm->arch.model.fac_mask, 138); |
| 2661 | set_kvm_facility(kvm->arch.model.fac_list, 138); |
| 2662 | /* we emulate STHYI in kvm */ |
Janosch Frank | 95ca2cb | 2016-05-23 15:11:58 +0200 | [diff] [blame] | 2663 | set_kvm_facility(kvm->arch.model.fac_mask, 74); |
| 2664 | set_kvm_facility(kvm->arch.model.fac_list, 74); |
Claudio Imbrenda | 1bab1c0 | 2016-08-29 15:56:55 +0200 | [diff] [blame] | 2665 | if (MACHINE_HAS_TLB_GUEST) { |
| 2666 | set_kvm_facility(kvm->arch.model.fac_mask, 147); |
| 2667 | set_kvm_facility(kvm->arch.model.fac_list, 147); |
| 2668 | } |
Janosch Frank | 95ca2cb | 2016-05-23 15:11:58 +0200 | [diff] [blame] | 2669 | |
Pierre Morel | 05f31e3 | 2019-05-21 17:34:37 +0200 | [diff] [blame] | 2670 | if (css_general_characteristics.aiv && test_facility(65)) |
| 2671 | set_kvm_facility(kvm->arch.model.fac_mask, 65); |
| 2672 | |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 2673 | kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 2674 | kvm->arch.model.ibc = sclp.ibc & 0x0fff; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 2675 | |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2676 | kvm_s390_crypto_init(kvm); |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 2677 | |
Fei Li | 5197839 | 2017-02-17 17:06:26 +0800 | [diff] [blame] | 2678 | mutex_init(&kvm->arch.float_int.ais_lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 2679 | spin_lock_init(&kvm->arch.float_int.lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 2680 | for (i = 0; i < FIRQ_LIST_COUNT; i++) |
| 2681 | INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 2682 | init_waitqueue_head(&kvm->arch.ipte_wq); |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 2683 | mutex_init(&kvm->arch.ipte_mutex); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 2684 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2685 | debug_register_view(kvm->arch.dbf, &debug_sprintf_view); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 2686 | VM_EVENT(kvm, 3, "vm created with type %lu", type); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2687 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2688 | if (type & KVM_VM_S390_UCONTROL) { |
| 2689 | kvm->arch.gmap = NULL; |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2690 | kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2691 | } else { |
Guenther Hutzl | 32e6b23 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2692 | if (sclp.hamax == U64_MAX) |
Martin Schwidefsky | ee71d16 | 2017-04-20 14:43:51 +0200 | [diff] [blame] | 2693 | kvm->arch.mem_limit = TASK_SIZE_MAX; |
Guenther Hutzl | 32e6b23 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2694 | else |
Martin Schwidefsky | ee71d16 | 2017-04-20 14:43:51 +0200 | [diff] [blame] | 2695 | kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, |
Guenther Hutzl | 32e6b23 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 2696 | sclp.hamax + 1); |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2697 | kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2698 | if (!kvm->arch.gmap) |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2699 | goto out_err; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 2700 | kvm->arch.gmap->private = kvm; |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 2701 | kvm->arch.gmap->pfault_enabled = 0; |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 2702 | } |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 2703 | |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 2704 | kvm->arch.use_pfmfi = sclp.has_pfmfi; |
Janosch Frank | 55531b7 | 2018-02-15 16:33:47 +0100 | [diff] [blame] | 2705 | kvm->arch.use_skf = sclp.has_skey; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 2706 | spin_lock_init(&kvm->arch.start_stop_lock); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 2707 | kvm_s390_vsie_init(kvm); |
Michael Mueller | d7c5cb0 | 2017-06-12 14:15:19 +0200 | [diff] [blame] | 2708 | kvm_s390_gisa_init(kvm); |
Christian Borntraeger | 8335713a | 2015-12-08 16:55:27 +0100 | [diff] [blame] | 2709 | KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 2710 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2711 | return 0; |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2712 | out_err: |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2713 | free_page((unsigned long)kvm->arch.sie_page2); |
Dominik Dingel | 40f5b73 | 2015-03-12 13:55:53 +0100 | [diff] [blame] | 2714 | debug_unregister(kvm->arch.dbf); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2715 | sca_dispose(kvm); |
Christian Borntraeger | 78f2613 | 2015-07-22 15:50:58 +0200 | [diff] [blame] | 2716 | KVM_EVENT(3, "creation of vm failed: %d", rc); |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 2717 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2718 | } |
| 2719 | |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2720 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 2721 | { |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2722 | u16 rc, rrc; |
| 2723 | |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2724 | VCPU_EVENT(vcpu, 3, "%s", "free cpu"); |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 2725 | trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); |
Christian Borntraeger | 67335e6 | 2014-03-25 17:09:08 +0100 | [diff] [blame] | 2726 | kvm_s390_clear_local_irqs(vcpu); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 2727 | kvm_clear_async_pf_completion_queue(vcpu); |
Eugene (jno) Dvurechenski | bc784cc | 2015-04-23 16:09:06 +0200 | [diff] [blame] | 2728 | if (!kvm_is_ucontrol(vcpu->kvm)) |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2729 | sca_del_vcpu(vcpu); |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 2730 | |
| 2731 | if (kvm_is_ucontrol(vcpu->kvm)) |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2732 | gmap_remove(vcpu->arch.gmap); |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 2733 | |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 2734 | if (vcpu->kvm->arch.use_cmma) |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 2735 | kvm_s390_vcpu_unsetup_cmma(vcpu); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2736 | /* We can not hold the vcpu mutex here, we are already dying */ |
| 2737 | if (kvm_s390_pv_cpu_get_handle(vcpu)) |
| 2738 | kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2739 | free_page((unsigned long)(vcpu->arch.sie_block)); |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | static void kvm_free_vcpus(struct kvm *kvm) |
| 2743 | { |
| 2744 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2745 | struct kvm_vcpu *vcpu; |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2746 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2747 | kvm_for_each_vcpu(i, vcpu, kvm) |
Sean Christopherson | 4543bdc | 2019-12-18 13:55:14 -0800 | [diff] [blame] | 2748 | kvm_vcpu_destroy(vcpu); |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2749 | |
| 2750 | mutex_lock(&kvm->lock); |
| 2751 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 2752 | kvm->vcpus[i] = NULL; |
| 2753 | |
| 2754 | atomic_set(&kvm->online_vcpus, 0); |
| 2755 | mutex_unlock(&kvm->lock); |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2756 | } |
| 2757 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2758 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 2759 | { |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2760 | u16 rc, rrc; |
| 2761 | |
Christian Borntraeger | d329c03 | 2008-11-26 14:50:27 +0100 | [diff] [blame] | 2762 | kvm_free_vcpus(kvm); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2763 | sca_dispose(kvm); |
Michael Mueller | d7c5cb0 | 2017-06-12 14:15:19 +0200 | [diff] [blame] | 2764 | kvm_s390_gisa_destroy(kvm); |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2765 | /* |
| 2766 | * We are already at the end of life and kvm->lock is not taken. |
| 2767 | * This is ok as the file descriptor is closed by now and nobody |
| 2768 | * can mess with the pv state. To avoid lockdep_assert_held from |
| 2769 | * complaining we do not use kvm_s390_pv_is_protected. |
| 2770 | */ |
| 2771 | if (kvm_s390_pv_get_handle(kvm)) |
| 2772 | kvm_s390_pv_deinit_vm(kvm, &rc, &rrc); |
| 2773 | debug_unregister(kvm->arch.dbf); |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 2774 | free_page((unsigned long)kvm->arch.sie_page2); |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 2775 | if (!kvm_is_ucontrol(kvm)) |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2776 | gmap_remove(kvm->arch.gmap); |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2777 | kvm_s390_destroy_adapters(kvm); |
Christian Borntraeger | 67335e6 | 2014-03-25 17:09:08 +0100 | [diff] [blame] | 2778 | kvm_s390_clear_float_irqs(kvm); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 2779 | kvm_s390_vsie_destroy(kvm); |
Christian Borntraeger | 8335713a | 2015-12-08 16:55:27 +0100 | [diff] [blame] | 2780 | KVM_EVENT(3, "vm 0x%pK destroyed", kvm); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | /* Section: vcpu related */ |
Dominik Dingel | dafd032 | 2014-12-02 16:53:21 +0100 | [diff] [blame] | 2784 | static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) |
| 2785 | { |
Martin Schwidefsky | 6ea427b | 2016-03-08 11:55:04 +0100 | [diff] [blame] | 2786 | vcpu->arch.gmap = gmap_create(current->mm, -1UL); |
Dominik Dingel | dafd032 | 2014-12-02 16:53:21 +0100 | [diff] [blame] | 2787 | if (!vcpu->arch.gmap) |
| 2788 | return -ENOMEM; |
| 2789 | vcpu->arch.gmap->private = vcpu->kvm; |
| 2790 | |
| 2791 | return 0; |
| 2792 | } |
| 2793 | |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2794 | static void sca_del_vcpu(struct kvm_vcpu *vcpu) |
| 2795 | { |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2796 | if (!kvm_s390_use_sca_entries()) |
| 2797 | return; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2798 | read_lock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2799 | if (vcpu->kvm->arch.use_esca) { |
| 2800 | struct esca_block *sca = vcpu->kvm->arch.sca; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2801 | |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2802 | clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); |
David Hildenbrand | 10ce32d | 2015-10-12 12:41:41 +0200 | [diff] [blame] | 2803 | sca->cpu[vcpu->vcpu_id].sda = 0; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2804 | } else { |
| 2805 | struct bsca_block *sca = vcpu->kvm->arch.sca; |
| 2806 | |
| 2807 | clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); |
David Hildenbrand | 10ce32d | 2015-10-12 12:41:41 +0200 | [diff] [blame] | 2808 | sca->cpu[vcpu->vcpu_id].sda = 0; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2809 | } |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2810 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2811 | } |
| 2812 | |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2813 | static void sca_add_vcpu(struct kvm_vcpu *vcpu) |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2814 | { |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2815 | if (!kvm_s390_use_sca_entries()) { |
| 2816 | struct bsca_block *sca = vcpu->kvm->arch.sca; |
| 2817 | |
| 2818 | /* we still need the basic sca for the ipte control */ |
| 2819 | vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); |
| 2820 | vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; |
David Hildenbrand | f07afa0 | 2018-03-06 14:27:58 +0100 | [diff] [blame] | 2821 | return; |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2822 | } |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2823 | read_lock(&vcpu->kvm->arch.sca_lock); |
| 2824 | if (vcpu->kvm->arch.use_esca) { |
| 2825 | struct esca_block *sca = vcpu->kvm->arch.sca; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2826 | |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2827 | sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2828 | vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); |
| 2829 | vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 2830 | vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2831 | set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2832 | } else { |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2833 | struct bsca_block *sca = vcpu->kvm->arch.sca; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2834 | |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2835 | sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block; |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2836 | vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32); |
| 2837 | vcpu->arch.sie_block->scaol = (__u32)(__u64)sca; |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2838 | set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); |
Eugene (jno) Dvurechenski | 7d43bafcf | 2015-04-22 17:09:44 +0200 | [diff] [blame] | 2839 | } |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 2840 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | /* Basic SCA to Extended SCA data copy routines */ |
| 2844 | static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) |
| 2845 | { |
| 2846 | d->sda = s->sda; |
| 2847 | d->sigp_ctrl.c = s->sigp_ctrl.c; |
| 2848 | d->sigp_ctrl.scn = s->sigp_ctrl.scn; |
| 2849 | } |
| 2850 | |
| 2851 | static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) |
| 2852 | { |
| 2853 | int i; |
| 2854 | |
| 2855 | d->ipte_control = s->ipte_control; |
| 2856 | d->mcn[0] = s->mcn; |
| 2857 | for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) |
| 2858 | sca_copy_entry(&d->cpu[i], &s->cpu[i]); |
| 2859 | } |
| 2860 | |
| 2861 | static int sca_switch_to_extended(struct kvm *kvm) |
| 2862 | { |
| 2863 | struct bsca_block *old_sca = kvm->arch.sca; |
| 2864 | struct esca_block *new_sca; |
| 2865 | struct kvm_vcpu *vcpu; |
| 2866 | unsigned int vcpu_idx; |
| 2867 | u32 scaol, scaoh; |
| 2868 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 2869 | if (kvm->arch.use_esca) |
| 2870 | return 0; |
| 2871 | |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2872 | new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO); |
| 2873 | if (!new_sca) |
| 2874 | return -ENOMEM; |
| 2875 | |
| 2876 | scaoh = (u32)((u64)(new_sca) >> 32); |
| 2877 | scaol = (u32)(u64)(new_sca) & ~0x3fU; |
| 2878 | |
| 2879 | kvm_s390_vcpu_block_all(kvm); |
| 2880 | write_lock(&kvm->arch.sca_lock); |
| 2881 | |
| 2882 | sca_copy_b_to_e(new_sca, old_sca); |
| 2883 | |
| 2884 | kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { |
| 2885 | vcpu->arch.sie_block->scaoh = scaoh; |
| 2886 | vcpu->arch.sie_block->scaol = scaol; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 2887 | vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2888 | } |
| 2889 | kvm->arch.sca = new_sca; |
| 2890 | kvm->arch.use_esca = 1; |
| 2891 | |
| 2892 | write_unlock(&kvm->arch.sca_lock); |
| 2893 | kvm_s390_vcpu_unblock_all(kvm); |
| 2894 | |
| 2895 | free_page((unsigned long)old_sca); |
| 2896 | |
Christian Borntraeger | 8335713a | 2015-12-08 16:55:27 +0100 | [diff] [blame] | 2897 | VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", |
| 2898 | old_sca, kvm->arch.sca); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2899 | return 0; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) |
| 2903 | { |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2904 | int rc; |
| 2905 | |
David Hildenbrand | a694067 | 2016-08-08 22:39:32 +0200 | [diff] [blame] | 2906 | if (!kvm_s390_use_sca_entries()) { |
| 2907 | if (id < KVM_MAX_VCPUS) |
| 2908 | return true; |
| 2909 | return false; |
| 2910 | } |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2911 | if (id < KVM_S390_BSCA_CPU_SLOTS) |
| 2912 | return true; |
David Hildenbrand | 76a6dd7 | 2015-11-24 13:33:49 +0100 | [diff] [blame] | 2913 | if (!sclp.has_esca || !sclp.has_64bscao) |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 2914 | return false; |
| 2915 | |
| 2916 | mutex_lock(&kvm->lock); |
| 2917 | rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); |
| 2918 | mutex_unlock(&kvm->lock); |
| 2919 | |
| 2920 | return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; |
Eugene (jno) Dvurechenski | a6e2f68 | 2015-04-21 15:31:59 +0200 | [diff] [blame] | 2921 | } |
| 2922 | |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2923 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 2924 | static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 2925 | { |
| 2926 | WARN_ON_ONCE(vcpu->arch.cputm_start != 0); |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2927 | raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2928 | vcpu->arch.cputm_start = get_tod_clock_fast(); |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2929 | raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 2933 | static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 2934 | { |
| 2935 | WARN_ON_ONCE(vcpu->arch.cputm_start == 0); |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2936 | raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2937 | vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; |
| 2938 | vcpu->arch.cputm_start = 0; |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2939 | raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 2943 | static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 2944 | { |
| 2945 | WARN_ON_ONCE(vcpu->arch.cputm_enabled); |
| 2946 | vcpu->arch.cputm_enabled = true; |
| 2947 | __start_cpu_timer_accounting(vcpu); |
| 2948 | } |
| 2949 | |
| 2950 | /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ |
| 2951 | static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 2952 | { |
| 2953 | WARN_ON_ONCE(!vcpu->arch.cputm_enabled); |
| 2954 | __stop_cpu_timer_accounting(vcpu); |
| 2955 | vcpu->arch.cputm_enabled = false; |
| 2956 | } |
| 2957 | |
| 2958 | static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 2959 | { |
| 2960 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
| 2961 | __enable_cpu_timer_accounting(vcpu); |
| 2962 | preempt_enable(); |
| 2963 | } |
| 2964 | |
| 2965 | static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) |
| 2966 | { |
| 2967 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
| 2968 | __disable_cpu_timer_accounting(vcpu); |
| 2969 | preempt_enable(); |
| 2970 | } |
| 2971 | |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 2972 | /* set the cpu timer - may only be called from the VCPU thread itself */ |
| 2973 | void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) |
| 2974 | { |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2975 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2976 | raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2977 | if (vcpu->arch.cputm_enabled) |
| 2978 | vcpu->arch.cputm_start = get_tod_clock_fast(); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 2979 | vcpu->arch.sie_block->cputm = cputm; |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2980 | raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2981 | preempt_enable(); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 2982 | } |
| 2983 | |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2984 | /* update and get the cpu timer - can also be called from other VCPU threads */ |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 2985 | __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) |
| 2986 | { |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2987 | unsigned int seq; |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2988 | __u64 value; |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 2989 | |
| 2990 | if (unlikely(!vcpu->arch.cputm_enabled)) |
| 2991 | return vcpu->arch.sie_block->cputm; |
| 2992 | |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 2993 | preempt_disable(); /* protect from TOD sync and vcpu_load/put */ |
| 2994 | do { |
| 2995 | seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); |
| 2996 | /* |
| 2997 | * If the writer would ever execute a read in the critical |
| 2998 | * section, e.g. in irq context, we have a deadlock. |
| 2999 | */ |
| 3000 | WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); |
| 3001 | value = vcpu->arch.sie_block->cputm; |
| 3002 | /* if cputm_start is 0, accounting is being started/stopped */ |
| 3003 | if (likely(vcpu->arch.cputm_start)) |
| 3004 | value -= get_tod_clock_fast() - vcpu->arch.cputm_start; |
| 3005 | } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); |
| 3006 | preempt_enable(); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3007 | return value; |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3008 | } |
| 3009 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3010 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 3011 | { |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 3012 | |
David Hildenbrand | 37d9df9 | 2015-03-11 16:47:33 +0100 | [diff] [blame] | 3013 | gmap_enable(vcpu->arch.enabled_gmap); |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3014 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); |
David Hildenbrand | 5ebda31 | 2016-02-22 13:52:27 +0100 | [diff] [blame] | 3015 | if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3016 | __start_cpu_timer_accounting(vcpu); |
David Hildenbrand | 01a745a | 2016-02-12 20:41:56 +0100 | [diff] [blame] | 3017 | vcpu->cpu = cpu; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 3021 | { |
David Hildenbrand | 01a745a | 2016-02-12 20:41:56 +0100 | [diff] [blame] | 3022 | vcpu->cpu = -1; |
David Hildenbrand | 5ebda31 | 2016-02-22 13:52:27 +0100 | [diff] [blame] | 3023 | if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 3024 | __stop_cpu_timer_accounting(vcpu); |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3025 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); |
David Hildenbrand | 37d9df9 | 2015-03-11 16:47:33 +0100 | [diff] [blame] | 3026 | vcpu->arch.enabled_gmap = gmap_get_enabled(); |
| 3027 | gmap_disable(vcpu->arch.enabled_gmap); |
Hendrik Brueckner | 9977e88 | 2015-06-10 12:53:42 +0200 | [diff] [blame] | 3028 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3029 | } |
| 3030 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 3031 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 3032 | { |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 3033 | mutex_lock(&vcpu->kvm->lock); |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 3034 | preempt_disable(); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 3035 | vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; |
David Hildenbrand | d16b52c | 2018-02-07 12:46:44 +0100 | [diff] [blame] | 3036 | vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; |
Fan Zhang | fdf0365 | 2015-05-13 10:58:41 +0200 | [diff] [blame] | 3037 | preempt_enable(); |
Jason J. Herne | 72f2502 | 2014-11-25 09:46:02 -0500 | [diff] [blame] | 3038 | mutex_unlock(&vcpu->kvm->lock); |
David Hildenbrand | 2550882 | 2015-10-12 16:27:23 +0200 | [diff] [blame] | 3039 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
Dominik Dingel | dafd032 | 2014-12-02 16:53:21 +0100 | [diff] [blame] | 3040 | vcpu->arch.gmap = vcpu->kvm->arch.gmap; |
David Hildenbrand | eaa78f3 | 2015-10-12 16:29:01 +0200 | [diff] [blame] | 3041 | sca_add_vcpu(vcpu); |
David Hildenbrand | 2550882 | 2015-10-12 16:27:23 +0200 | [diff] [blame] | 3042 | } |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 3043 | if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) |
| 3044 | vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; |
David Hildenbrand | 37d9df9 | 2015-03-11 16:47:33 +0100 | [diff] [blame] | 3045 | /* make vcpu_load load the right gmap on the first trigger */ |
| 3046 | vcpu->arch.enabled_gmap = vcpu->arch.gmap; |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 3047 | } |
| 3048 | |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3049 | static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) |
| 3050 | { |
| 3051 | if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && |
| 3052 | test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) |
| 3053 | return true; |
| 3054 | return false; |
| 3055 | } |
| 3056 | |
| 3057 | static bool kvm_has_pckmo_ecc(struct kvm *kvm) |
| 3058 | { |
| 3059 | /* At least one ECC subfunction must be present */ |
| 3060 | return kvm_has_pckmo_subfunc(kvm, 32) || |
| 3061 | kvm_has_pckmo_subfunc(kvm, 33) || |
| 3062 | kvm_has_pckmo_subfunc(kvm, 34) || |
| 3063 | kvm_has_pckmo_subfunc(kvm, 40) || |
| 3064 | kvm_has_pckmo_subfunc(kvm, 41); |
| 3065 | |
| 3066 | } |
| 3067 | |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3068 | static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) |
| 3069 | { |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 3070 | /* |
| 3071 | * If the AP instructions are not being interpreted and the MSAX3 |
| 3072 | * facility is not configured for the guest, there is nothing to set up. |
| 3073 | */ |
| 3074 | if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3075 | return; |
| 3076 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 3077 | vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3078 | vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); |
Tony Krowiak | 37940fb | 2018-09-25 19:16:39 -0400 | [diff] [blame] | 3079 | vcpu->arch.sie_block->eca &= ~ECA_APIE; |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3080 | vcpu->arch.sie_block->ecd &= ~ECD_ECC; |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3081 | |
Tony Krowiak | e585b24 | 2018-09-25 19:16:18 -0400 | [diff] [blame] | 3082 | if (vcpu->kvm->arch.crypto.apie) |
| 3083 | vcpu->arch.sie_block->eca |= ECA_APIE; |
| 3084 | |
| 3085 | /* Set up protected key support */ |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3086 | if (vcpu->kvm->arch.crypto.aes_kw) { |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3087 | vcpu->arch.sie_block->ecb3 |= ECB3_AES; |
Christian Borntraeger | 8ec2fa5 | 2019-04-03 03:00:35 -0400 | [diff] [blame] | 3088 | /* ecc is also wrapped with AES key */ |
| 3089 | if (kvm_has_pckmo_ecc(vcpu->kvm)) |
| 3090 | vcpu->arch.sie_block->ecd |= ECD_ECC; |
| 3091 | } |
| 3092 | |
Tony Krowiak | a374e89 | 2014-09-03 10:13:53 +0200 | [diff] [blame] | 3093 | if (vcpu->kvm->arch.crypto.dea_kw) |
| 3094 | vcpu->arch.sie_block->ecb3 |= ECB3_DEA; |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3095 | } |
| 3096 | |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3097 | void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) |
| 3098 | { |
| 3099 | free_page(vcpu->arch.sie_block->cbrlo); |
| 3100 | vcpu->arch.sie_block->cbrlo = 0; |
| 3101 | } |
| 3102 | |
| 3103 | int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) |
| 3104 | { |
| 3105 | vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); |
| 3106 | if (!vcpu->arch.sie_block->cbrlo) |
| 3107 | return -ENOMEM; |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3108 | return 0; |
| 3109 | } |
| 3110 | |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3111 | static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) |
| 3112 | { |
| 3113 | struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; |
| 3114 | |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3115 | vcpu->arch.sie_block->ibc = model->ibc; |
David Hildenbrand | 80bc79d | 2015-12-02 09:43:29 +0100 | [diff] [blame] | 3116 | if (test_kvm_facility(vcpu->kvm, 7)) |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 3117 | vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list; |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3118 | } |
| 3119 | |
Sean Christopherson | ff72bb5 | 2019-12-18 13:55:20 -0800 | [diff] [blame] | 3120 | static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) |
| 3121 | { |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3122 | int rc = 0; |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 3123 | u16 uvrc, uvrrc; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 3124 | |
Cornelia Huck | 9e6dabe | 2011-11-17 11:00:41 +0100 | [diff] [blame] | 3125 | atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | |
| 3126 | CPUSTAT_SM | |
Guenther Hutzl | a4a4f19 | 2015-03-31 14:39:49 +0200 | [diff] [blame] | 3127 | CPUSTAT_STOPPED); |
| 3128 | |
Guenther Hutzl | 53df84f | 2015-02-18 11:13:03 +0100 | [diff] [blame] | 3129 | if (test_kvm_facility(vcpu->kvm, 78)) |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3130 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); |
Guenther Hutzl | 53df84f | 2015-02-18 11:13:03 +0100 | [diff] [blame] | 3131 | else if (test_kvm_facility(vcpu->kvm, 8)) |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3132 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); |
Guenther Hutzl | a4a4f19 | 2015-03-31 14:39:49 +0200 | [diff] [blame] | 3133 | |
Michael Mueller | 91520f1 | 2015-02-27 14:32:11 +0100 | [diff] [blame] | 3134 | kvm_s390_vcpu_setup_model(vcpu); |
| 3135 | |
David Hildenbrand | bdab09f | 2016-04-12 11:07:49 +0200 | [diff] [blame] | 3136 | /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ |
| 3137 | if (MACHINE_HAS_ESOP) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3138 | vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; |
David Hildenbrand | bd50e8e | 2016-03-04 12:23:55 +0100 | [diff] [blame] | 3139 | if (test_kvm_facility(vcpu->kvm, 9)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3140 | vcpu->arch.sie_block->ecb |= ECB_SRSI; |
David Hildenbrand | f597d24 | 2016-04-22 16:26:49 +0200 | [diff] [blame] | 3141 | if (test_kvm_facility(vcpu->kvm, 73)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3142 | vcpu->arch.sie_block->ecb |= ECB_TE; |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3143 | |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3144 | if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3145 | vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; |
Janosch Frank | cd1836f | 2016-08-04 09:57:36 +0200 | [diff] [blame] | 3146 | if (test_kvm_facility(vcpu->kvm, 130)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3147 | vcpu->arch.sie_block->ecb2 |= ECB2_IEP; |
| 3148 | vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; |
David Hildenbrand | 48ee7d3 | 2016-04-04 15:49:34 +0200 | [diff] [blame] | 3149 | if (sclp.has_cei) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3150 | vcpu->arch.sie_block->eca |= ECA_CEI; |
David Hildenbrand | 11ad65b | 2016-04-04 15:46:26 +0200 | [diff] [blame] | 3151 | if (sclp.has_ib) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3152 | vcpu->arch.sie_block->eca |= ECA_IB; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 3153 | if (sclp.has_siif) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3154 | vcpu->arch.sie_block->eca |= ECA_SII; |
David Hildenbrand | 37c5f6c | 2015-05-06 13:18:59 +0200 | [diff] [blame] | 3155 | if (sclp.has_sigpif) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3156 | vcpu->arch.sie_block->eca |= ECA_SIGPI; |
Michael Mueller | 18280d8 | 2015-03-16 16:05:41 +0100 | [diff] [blame] | 3157 | if (test_kvm_facility(vcpu->kvm, 129)) { |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 3158 | vcpu->arch.sie_block->eca |= ECA_VX; |
| 3159 | vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; |
Eric Farman | 13211ea | 2014-04-30 13:39:46 -0400 | [diff] [blame] | 3160 | } |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3161 | if (test_kvm_facility(vcpu->kvm, 139)) |
| 3162 | vcpu->arch.sie_block->ecd |= ECD_MEF; |
Christian Borntraeger | a3da7b4 | 2018-03-08 16:08:49 +0000 | [diff] [blame] | 3163 | if (test_kvm_facility(vcpu->kvm, 156)) |
| 3164 | vcpu->arch.sie_block->ecd |= ECD_ETOKENF; |
Michael Mueller | d7c5cb0 | 2017-06-12 14:15:19 +0200 | [diff] [blame] | 3165 | if (vcpu->arch.sie_block->gd) { |
| 3166 | vcpu->arch.sie_block->eca |= ECA_AIV; |
| 3167 | VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", |
| 3168 | vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); |
| 3169 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 3170 | vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx) |
| 3171 | | SDNXC; |
Fan Zhang | c6e5f16 | 2016-01-07 18:24:29 +0800 | [diff] [blame] | 3172 | vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 3173 | |
| 3174 | if (sclp.has_kss) |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3175 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 3176 | else |
| 3177 | vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; |
Matthew Rosato | 5a5e653 | 2013-01-29 11:48:20 -0500 | [diff] [blame] | 3178 | |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 3179 | if (vcpu->kvm->arch.use_cmma) { |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3180 | rc = kvm_s390_vcpu_setup_cmma(vcpu); |
| 3181 | if (rc) |
| 3182 | return rc; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 3183 | } |
David Hildenbrand | 0ac96caf | 2014-12-12 15:17:31 +0100 | [diff] [blame] | 3184 | hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 3185 | vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 3186 | |
Collin Walling | 67d49d5 | 2018-08-31 12:51:19 -0400 | [diff] [blame] | 3187 | vcpu->arch.sie_block->hpid = HPID_KVM; |
| 3188 | |
Tony Krowiak | 5102ee8 | 2014-06-27 14:46:01 -0400 | [diff] [blame] | 3189 | kvm_s390_vcpu_crypto_setup(vcpu); |
| 3190 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 3191 | mutex_lock(&vcpu->kvm->lock); |
| 3192 | if (kvm_s390_pv_is_protected(vcpu->kvm)) { |
| 3193 | rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); |
| 3194 | if (rc) |
| 3195 | kvm_s390_vcpu_unsetup_cmma(vcpu); |
| 3196 | } |
| 3197 | mutex_unlock(&vcpu->kvm->lock); |
| 3198 | |
Dominik Dingel | b31605c | 2014-03-25 13:47:11 +0100 | [diff] [blame] | 3199 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3200 | } |
| 3201 | |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 3202 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
| 3203 | { |
| 3204 | if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) |
| 3205 | return -EINVAL; |
| 3206 | return 0; |
| 3207 | } |
| 3208 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3209 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3210 | { |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3211 | struct sie_page *sie_page; |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 3212 | int rc; |
Carsten Otte | 4d47555 | 2011-10-18 12:27:12 +0200 | [diff] [blame] | 3213 | |
QingFeng Hao | da72ca4 | 2017-06-07 11:41:19 +0200 | [diff] [blame] | 3214 | BUILD_BUG_ON(sizeof(struct sie_page) != 4096); |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3215 | sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); |
| 3216 | if (!sie_page) |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3217 | return -ENOMEM; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3218 | |
Michael Mueller | 7feb6bb | 2013-06-28 13:30:24 +0200 | [diff] [blame] | 3219 | vcpu->arch.sie_block = &sie_page->sie_block; |
| 3220 | vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; |
| 3221 | |
David Hildenbrand | efed110 | 2015-04-16 12:32:41 +0200 | [diff] [blame] | 3222 | /* the real guest size will always be smaller than msl */ |
| 3223 | vcpu->arch.sie_block->mso = 0; |
| 3224 | vcpu->arch.sie_block->msl = sclp.hamax; |
| 3225 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3226 | vcpu->arch.sie_block->icpua = vcpu->vcpu_id; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 3227 | spin_lock_init(&vcpu->arch.local_int.lock); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3228 | vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin; |
Michael Mueller | 4b9f952 | 2017-06-23 13:51:25 +0200 | [diff] [blame] | 3229 | if (vcpu->arch.sie_block->gd && sclp.has_gisaf) |
| 3230 | vcpu->arch.sie_block->gd |= GISA_FORMAT1; |
David Hildenbrand | 9c23a13 | 2016-02-17 21:53:33 +0100 | [diff] [blame] | 3231 | seqcount_init(&vcpu->arch.cputm_seqcount); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 3232 | |
Sean Christopherson | 321f8ee | 2019-12-18 13:55:10 -0800 | [diff] [blame] | 3233 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
| 3234 | kvm_clear_async_pf_completion_queue(vcpu); |
| 3235 | vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | |
| 3236 | KVM_SYNC_GPRS | |
| 3237 | KVM_SYNC_ACRS | |
| 3238 | KVM_SYNC_CRS | |
| 3239 | KVM_SYNC_ARCH0 | |
| 3240 | KVM_SYNC_PFAULT; |
| 3241 | kvm_s390_set_prefix(vcpu, 0); |
| 3242 | if (test_kvm_facility(vcpu->kvm, 64)) |
| 3243 | vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; |
| 3244 | if (test_kvm_facility(vcpu->kvm, 82)) |
| 3245 | vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; |
| 3246 | if (test_kvm_facility(vcpu->kvm, 133)) |
| 3247 | vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; |
| 3248 | if (test_kvm_facility(vcpu->kvm, 156)) |
| 3249 | vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; |
| 3250 | /* fprs can be synchronized via vrs, even if the guest has no vx. With |
| 3251 | * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. |
| 3252 | */ |
| 3253 | if (MACHINE_HAS_VX) |
| 3254 | vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; |
| 3255 | else |
| 3256 | vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; |
| 3257 | |
| 3258 | if (kvm_is_ucontrol(vcpu->kvm)) { |
| 3259 | rc = __kvm_ucontrol_vcpu_init(vcpu); |
| 3260 | if (rc) |
Sean Christopherson | a2017f1 | 2019-12-18 13:55:11 -0800 | [diff] [blame] | 3261 | goto out_free_sie_block; |
Sean Christopherson | 321f8ee | 2019-12-18 13:55:10 -0800 | [diff] [blame] | 3262 | } |
| 3263 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3264 | VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", |
| 3265 | vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); |
| 3266 | trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3267 | |
Sean Christopherson | ff72bb5 | 2019-12-18 13:55:20 -0800 | [diff] [blame] | 3268 | rc = kvm_s390_vcpu_setup(vcpu); |
| 3269 | if (rc) |
| 3270 | goto out_ucontrol_uninit; |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3271 | return 0; |
| 3272 | |
Sean Christopherson | ff72bb5 | 2019-12-18 13:55:20 -0800 | [diff] [blame] | 3273 | out_ucontrol_uninit: |
| 3274 | if (kvm_is_ucontrol(vcpu->kvm)) |
| 3275 | gmap_remove(vcpu->arch.gmap); |
Wei Yongjun | 7b06bf2 | 2010-03-09 14:37:53 +0800 | [diff] [blame] | 3276 | out_free_sie_block: |
| 3277 | free_page((unsigned long)(vcpu->arch.sie_block)); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3278 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3279 | } |
| 3280 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3281 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 3282 | { |
David Hildenbrand | 9a02206 | 2014-08-05 17:40:47 +0200 | [diff] [blame] | 3283 | return kvm_s390_vcpu_has_irq(vcpu, 0); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 3286 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 3287 | { |
Longpeng(Mike) | 0546c63 | 2017-08-08 12:05:34 +0800 | [diff] [blame] | 3288 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 3289 | } |
| 3290 | |
Christian Borntraeger | 27406cd | 2015-04-14 12:17:34 +0200 | [diff] [blame] | 3291 | void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3292 | { |
Peter Zijlstra | 805de8f4 | 2015-04-24 01:12:32 +0200 | [diff] [blame] | 3293 | atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); |
David Hildenbrand | 61a6df5 | 2015-05-12 08:41:40 +0200 | [diff] [blame] | 3294 | exit_sie(vcpu); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
Christian Borntraeger | 27406cd | 2015-04-14 12:17:34 +0200 | [diff] [blame] | 3297 | void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3298 | { |
Peter Zijlstra | 805de8f4 | 2015-04-24 01:12:32 +0200 | [diff] [blame] | 3299 | atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3300 | } |
| 3301 | |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3302 | static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) |
| 3303 | { |
Peter Zijlstra | 805de8f4 | 2015-04-24 01:12:32 +0200 | [diff] [blame] | 3304 | atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); |
David Hildenbrand | 61a6df5 | 2015-05-12 08:41:40 +0200 | [diff] [blame] | 3305 | exit_sie(vcpu); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3306 | } |
| 3307 | |
David Hildenbrand | 9ea5972 | 2018-09-25 19:16:16 -0400 | [diff] [blame] | 3308 | bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) |
| 3309 | { |
| 3310 | return atomic_read(&vcpu->arch.sie_block->prog20) & |
| 3311 | (PROG_BLOCK_SIE | PROG_REQUEST); |
| 3312 | } |
| 3313 | |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3314 | static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) |
| 3315 | { |
Jason J. Herne | 9bf9fde | 2015-09-16 09:13:50 -0400 | [diff] [blame] | 3316 | atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3319 | /* |
David Hildenbrand | 9ea5972 | 2018-09-25 19:16:16 -0400 | [diff] [blame] | 3320 | * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3321 | * If the CPU is not running (e.g. waiting as idle) the function will |
| 3322 | * return immediately. */ |
| 3323 | void exit_sie(struct kvm_vcpu *vcpu) |
| 3324 | { |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3325 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); |
David Hildenbrand | 9ea5972 | 2018-09-25 19:16:16 -0400 | [diff] [blame] | 3326 | kvm_s390_vsie_kick(vcpu); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3327 | while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) |
| 3328 | cpu_relax(); |
| 3329 | } |
| 3330 | |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3331 | /* Kick a guest cpu out of SIE to process a request synchronously */ |
| 3332 | void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3333 | { |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3334 | kvm_make_request(req, vcpu); |
| 3335 | kvm_s390_vcpu_request(vcpu); |
Christian Borntraeger | 49b99e1 | 2013-05-17 14:41:35 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3338 | static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, |
| 3339 | unsigned long end) |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3340 | { |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3341 | struct kvm *kvm = gmap->private; |
| 3342 | struct kvm_vcpu *vcpu; |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3343 | unsigned long prefix; |
| 3344 | int i; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3345 | |
David Hildenbrand | 65d0b0d | 2015-04-27 16:29:34 +0200 | [diff] [blame] | 3346 | if (gmap_is_shadow(gmap)) |
| 3347 | return; |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3348 | if (start >= 1UL << 31) |
| 3349 | /* We are only interested in prefix pages */ |
| 3350 | return; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3351 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 3352 | /* match against both prefix pages */ |
Martin Schwidefsky | 414d3b0 | 2016-03-08 11:52:54 +0100 | [diff] [blame] | 3353 | prefix = kvm_s390_get_prefix(vcpu); |
| 3354 | if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { |
| 3355 | VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", |
| 3356 | start, end); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3357 | kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3358 | } |
| 3359 | } |
| 3360 | } |
| 3361 | |
Christian Borntraeger | 8b905d2 | 2019-03-05 05:30:02 -0500 | [diff] [blame] | 3362 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
| 3363 | { |
| 3364 | /* do not poll with more than halt_poll_max_steal percent of steal time */ |
| 3365 | if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= |
| 3366 | halt_poll_max_steal) { |
| 3367 | vcpu->stat.halt_no_poll_steal++; |
| 3368 | return true; |
| 3369 | } |
| 3370 | return false; |
| 3371 | } |
| 3372 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 3373 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 3374 | { |
| 3375 | /* kvm common code refers to this, but never calls it */ |
| 3376 | BUG(); |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3380 | static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, |
| 3381 | struct kvm_one_reg *reg) |
| 3382 | { |
| 3383 | int r = -EINVAL; |
| 3384 | |
| 3385 | switch (reg->id) { |
Carsten Otte | 29b7c71 | 2012-05-15 14:15:27 +0200 | [diff] [blame] | 3386 | case KVM_REG_S390_TODPR: |
| 3387 | r = put_user(vcpu->arch.sie_block->todpr, |
| 3388 | (u32 __user *)reg->addr); |
| 3389 | break; |
| 3390 | case KVM_REG_S390_EPOCHDIFF: |
| 3391 | r = put_user(vcpu->arch.sie_block->epoch, |
| 3392 | (u64 __user *)reg->addr); |
| 3393 | break; |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3394 | case KVM_REG_S390_CPU_TIMER: |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3395 | r = put_user(kvm_s390_get_cpu_timer(vcpu), |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3396 | (u64 __user *)reg->addr); |
| 3397 | break; |
| 3398 | case KVM_REG_S390_CLOCK_COMP: |
| 3399 | r = put_user(vcpu->arch.sie_block->ckc, |
| 3400 | (u64 __user *)reg->addr); |
| 3401 | break; |
Dominik Dingel | 536336c | 2013-09-30 10:55:33 +0200 | [diff] [blame] | 3402 | case KVM_REG_S390_PFTOKEN: |
| 3403 | r = put_user(vcpu->arch.pfault_token, |
| 3404 | (u64 __user *)reg->addr); |
| 3405 | break; |
| 3406 | case KVM_REG_S390_PFCOMPARE: |
| 3407 | r = put_user(vcpu->arch.pfault_compare, |
| 3408 | (u64 __user *)reg->addr); |
| 3409 | break; |
| 3410 | case KVM_REG_S390_PFSELECT: |
| 3411 | r = put_user(vcpu->arch.pfault_select, |
| 3412 | (u64 __user *)reg->addr); |
| 3413 | break; |
Christian Borntraeger | 672550f | 2014-02-10 15:32:19 +0100 | [diff] [blame] | 3414 | case KVM_REG_S390_PP: |
| 3415 | r = put_user(vcpu->arch.sie_block->pp, |
| 3416 | (u64 __user *)reg->addr); |
| 3417 | break; |
Christian Borntraeger | afa45ff | 2014-02-10 15:39:23 +0100 | [diff] [blame] | 3418 | case KVM_REG_S390_GBEA: |
| 3419 | r = put_user(vcpu->arch.sie_block->gbea, |
| 3420 | (u64 __user *)reg->addr); |
| 3421 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3422 | default: |
| 3423 | break; |
| 3424 | } |
| 3425 | |
| 3426 | return r; |
| 3427 | } |
| 3428 | |
| 3429 | static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, |
| 3430 | struct kvm_one_reg *reg) |
| 3431 | { |
| 3432 | int r = -EINVAL; |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3433 | __u64 val; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3434 | |
| 3435 | switch (reg->id) { |
Carsten Otte | 29b7c71 | 2012-05-15 14:15:27 +0200 | [diff] [blame] | 3436 | case KVM_REG_S390_TODPR: |
| 3437 | r = get_user(vcpu->arch.sie_block->todpr, |
| 3438 | (u32 __user *)reg->addr); |
| 3439 | break; |
| 3440 | case KVM_REG_S390_EPOCHDIFF: |
| 3441 | r = get_user(vcpu->arch.sie_block->epoch, |
| 3442 | (u64 __user *)reg->addr); |
| 3443 | break; |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3444 | case KVM_REG_S390_CPU_TIMER: |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 3445 | r = get_user(val, (u64 __user *)reg->addr); |
| 3446 | if (!r) |
| 3447 | kvm_s390_set_cpu_timer(vcpu, val); |
Jason J. herne | 46a6dd1 | 2012-05-15 14:15:28 +0200 | [diff] [blame] | 3448 | break; |
| 3449 | case KVM_REG_S390_CLOCK_COMP: |
| 3450 | r = get_user(vcpu->arch.sie_block->ckc, |
| 3451 | (u64 __user *)reg->addr); |
| 3452 | break; |
Dominik Dingel | 536336c | 2013-09-30 10:55:33 +0200 | [diff] [blame] | 3453 | case KVM_REG_S390_PFTOKEN: |
| 3454 | r = get_user(vcpu->arch.pfault_token, |
| 3455 | (u64 __user *)reg->addr); |
David Hildenbrand | 9fbd808 | 2014-10-09 15:01:38 +0200 | [diff] [blame] | 3456 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
| 3457 | kvm_clear_async_pf_completion_queue(vcpu); |
Dominik Dingel | 536336c | 2013-09-30 10:55:33 +0200 | [diff] [blame] | 3458 | break; |
| 3459 | case KVM_REG_S390_PFCOMPARE: |
| 3460 | r = get_user(vcpu->arch.pfault_compare, |
| 3461 | (u64 __user *)reg->addr); |
| 3462 | break; |
| 3463 | case KVM_REG_S390_PFSELECT: |
| 3464 | r = get_user(vcpu->arch.pfault_select, |
| 3465 | (u64 __user *)reg->addr); |
| 3466 | break; |
Christian Borntraeger | 672550f | 2014-02-10 15:32:19 +0100 | [diff] [blame] | 3467 | case KVM_REG_S390_PP: |
| 3468 | r = get_user(vcpu->arch.sie_block->pp, |
| 3469 | (u64 __user *)reg->addr); |
| 3470 | break; |
Christian Borntraeger | afa45ff | 2014-02-10 15:39:23 +0100 | [diff] [blame] | 3471 | case KVM_REG_S390_GBEA: |
| 3472 | r = get_user(vcpu->arch.sie_block->gbea, |
| 3473 | (u64 __user *)reg->addr); |
| 3474 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 3475 | default: |
| 3476 | break; |
| 3477 | } |
| 3478 | |
| 3479 | return r; |
| 3480 | } |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 3481 | |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3482 | static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3483 | { |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 3484 | vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; |
| 3485 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
| 3486 | memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); |
| 3487 | |
| 3488 | kvm_clear_async_pf_completion_queue(vcpu); |
| 3489 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) |
| 3490 | kvm_s390_vcpu_stop(vcpu); |
| 3491 | kvm_s390_clear_local_irqs(vcpu); |
| 3492 | } |
| 3493 | |
| 3494 | static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) |
| 3495 | { |
| 3496 | /* Initial reset is a superset of the normal reset */ |
| 3497 | kvm_arch_vcpu_ioctl_normal_reset(vcpu); |
| 3498 | |
| 3499 | /* this equals initial cpu reset in pop, but we don't switch to ESA */ |
| 3500 | vcpu->arch.sie_block->gpsw.mask = 0; |
| 3501 | vcpu->arch.sie_block->gpsw.addr = 0; |
| 3502 | kvm_s390_set_prefix(vcpu, 0); |
| 3503 | kvm_s390_set_cpu_timer(vcpu, 0); |
| 3504 | vcpu->arch.sie_block->ckc = 0; |
| 3505 | vcpu->arch.sie_block->todpr = 0; |
| 3506 | memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); |
| 3507 | vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; |
| 3508 | vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; |
| 3509 | vcpu->run->s.regs.fpc = 0; |
| 3510 | vcpu->arch.sie_block->gbea = 1; |
| 3511 | vcpu->arch.sie_block->pp = 0; |
| 3512 | vcpu->arch.sie_block->fpf &= ~FPF_BPBC; |
| 3513 | } |
| 3514 | |
| 3515 | static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) |
| 3516 | { |
| 3517 | struct kvm_sync_regs *regs = &vcpu->run->s.regs; |
| 3518 | |
| 3519 | /* Clear reset is a superset of the initial reset */ |
| 3520 | kvm_arch_vcpu_ioctl_initial_reset(vcpu); |
| 3521 | |
| 3522 | memset(®s->gprs, 0, sizeof(regs->gprs)); |
| 3523 | memset(®s->vrs, 0, sizeof(regs->vrs)); |
| 3524 | memset(®s->acrs, 0, sizeof(regs->acrs)); |
| 3525 | memset(®s->gscb, 0, sizeof(regs->gscb)); |
| 3526 | |
| 3527 | regs->etoken = 0; |
| 3528 | regs->etoken_extension = 0; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3532 | { |
Christoffer Dall | 875656f | 2017-12-04 21:35:27 +0100 | [diff] [blame] | 3533 | vcpu_load(vcpu); |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 3534 | memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); |
Christoffer Dall | 875656f | 2017-12-04 21:35:27 +0100 | [diff] [blame] | 3535 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3536 | return 0; |
| 3537 | } |
| 3538 | |
| 3539 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3540 | { |
Christoffer Dall | 1fc9b76 | 2017-12-04 21:35:26 +0100 | [diff] [blame] | 3541 | vcpu_load(vcpu); |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 3542 | memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); |
Christoffer Dall | 1fc9b76 | 2017-12-04 21:35:26 +0100 | [diff] [blame] | 3543 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3544 | return 0; |
| 3545 | } |
| 3546 | |
| 3547 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 3548 | struct kvm_sregs *sregs) |
| 3549 | { |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 3550 | vcpu_load(vcpu); |
| 3551 | |
Christian Borntraeger | 59674c1 | 2012-01-11 11:20:33 +0100 | [diff] [blame] | 3552 | memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3553 | memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); |
Christoffer Dall | b4ef9d4 | 2017-12-04 21:35:29 +0100 | [diff] [blame] | 3554 | |
| 3555 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3556 | return 0; |
| 3557 | } |
| 3558 | |
| 3559 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 3560 | struct kvm_sregs *sregs) |
| 3561 | { |
Christoffer Dall | bcdec41 | 2017-12-04 21:35:28 +0100 | [diff] [blame] | 3562 | vcpu_load(vcpu); |
| 3563 | |
Christian Borntraeger | 59674c1 | 2012-01-11 11:20:33 +0100 | [diff] [blame] | 3564 | memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3565 | memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); |
Christoffer Dall | bcdec41 | 2017-12-04 21:35:28 +0100 | [diff] [blame] | 3566 | |
| 3567 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3568 | return 0; |
| 3569 | } |
| 3570 | |
| 3571 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 3572 | { |
Christoffer Dall | 6a96bc7 | 2017-12-04 21:35:35 +0100 | [diff] [blame] | 3573 | int ret = 0; |
| 3574 | |
| 3575 | vcpu_load(vcpu); |
| 3576 | |
| 3577 | if (test_fp_ctl(fpu->fpc)) { |
| 3578 | ret = -EINVAL; |
| 3579 | goto out; |
| 3580 | } |
Christian Borntraeger | e1788bb | 2016-11-22 09:29:38 +0100 | [diff] [blame] | 3581 | vcpu->run->s.regs.fpc = fpu->fpc; |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3582 | if (MACHINE_HAS_VX) |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3583 | convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, |
| 3584 | (freg_t *) fpu->fprs); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3585 | else |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3586 | memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); |
Christoffer Dall | 6a96bc7 | 2017-12-04 21:35:35 +0100 | [diff] [blame] | 3587 | |
| 3588 | out: |
| 3589 | vcpu_put(vcpu); |
| 3590 | return ret; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 3594 | { |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 3595 | vcpu_load(vcpu); |
| 3596 | |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3597 | /* make sure we have the latest values */ |
| 3598 | save_fpu_regs(); |
| 3599 | if (MACHINE_HAS_VX) |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3600 | convert_vx_to_fp((freg_t *) fpu->fprs, |
| 3601 | (__vector128 *) vcpu->run->s.regs.vrs); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 3602 | else |
David Hildenbrand | a7d4b8f | 2016-08-16 14:38:24 +0200 | [diff] [blame] | 3603 | memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); |
Christian Borntraeger | e1788bb | 2016-11-22 09:29:38 +0100 | [diff] [blame] | 3604 | fpu->fpc = vcpu->run->s.regs.fpc; |
Christoffer Dall | 1393123 | 2017-12-04 21:35:34 +0100 | [diff] [blame] | 3605 | |
| 3606 | vcpu_put(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3607 | return 0; |
| 3608 | } |
| 3609 | |
| 3610 | static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) |
| 3611 | { |
| 3612 | int rc = 0; |
| 3613 | |
David Hildenbrand | 7a42fdc | 2014-05-05 16:26:19 +0200 | [diff] [blame] | 3614 | if (!is_vcpu_stopped(vcpu)) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3615 | rc = -EBUSY; |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 3616 | else { |
| 3617 | vcpu->run->psw_mask = psw.mask; |
| 3618 | vcpu->run->psw_addr = psw.addr; |
| 3619 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3620 | return rc; |
| 3621 | } |
| 3622 | |
| 3623 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 3624 | struct kvm_translation *tr) |
| 3625 | { |
| 3626 | return -EINVAL; /* not implemented yet */ |
| 3627 | } |
| 3628 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3629 | #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ |
| 3630 | KVM_GUESTDBG_USE_HW_BP | \ |
| 3631 | KVM_GUESTDBG_ENABLE) |
| 3632 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3633 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 3634 | struct kvm_guest_debug *dbg) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3635 | { |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3636 | int rc = 0; |
| 3637 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 3638 | vcpu_load(vcpu); |
| 3639 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3640 | vcpu->guest_debug = 0; |
| 3641 | kvm_s390_clear_bp_data(vcpu); |
| 3642 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 3643 | if (dbg->control & ~VALID_GUESTDBG_FLAGS) { |
| 3644 | rc = -EINVAL; |
| 3645 | goto out; |
| 3646 | } |
| 3647 | if (!sclp.has_gpere) { |
| 3648 | rc = -EINVAL; |
| 3649 | goto out; |
| 3650 | } |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3651 | |
| 3652 | if (dbg->control & KVM_GUESTDBG_ENABLE) { |
| 3653 | vcpu->guest_debug = dbg->control; |
| 3654 | /* enforce guest PER */ |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3655 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3656 | |
| 3657 | if (dbg->control & KVM_GUESTDBG_USE_HW_BP) |
| 3658 | rc = kvm_s390_import_bp_data(vcpu, dbg); |
| 3659 | } else { |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3660 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3661 | vcpu->arch.guestdbg.last_bp = 0; |
| 3662 | } |
| 3663 | |
| 3664 | if (rc) { |
| 3665 | vcpu->guest_debug = 0; |
| 3666 | kvm_s390_clear_bp_data(vcpu); |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3667 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
Christoffer Dall | 66b5656 | 2017-12-04 21:35:33 +0100 | [diff] [blame] | 3670 | out: |
| 3671 | vcpu_put(vcpu); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3672 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3673 | } |
| 3674 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3675 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 3676 | struct kvm_mp_state *mp_state) |
| 3677 | { |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 3678 | int ret; |
| 3679 | |
| 3680 | vcpu_load(vcpu); |
| 3681 | |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3682 | /* CHECK_STOP and LOAD are not supported yet */ |
Christoffer Dall | fd23256 | 2017-12-04 21:35:30 +0100 | [diff] [blame] | 3683 | ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : |
| 3684 | KVM_MP_STATE_OPERATING; |
| 3685 | |
| 3686 | vcpu_put(vcpu); |
| 3687 | return ret; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 3691 | struct kvm_mp_state *mp_state) |
| 3692 | { |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3693 | int rc = 0; |
| 3694 | |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 3695 | vcpu_load(vcpu); |
| 3696 | |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3697 | /* user space knows about this interface - let it control the state */ |
| 3698 | vcpu->kvm->arch.user_cpu_state_ctrl = 1; |
| 3699 | |
| 3700 | switch (mp_state->mp_state) { |
| 3701 | case KVM_MP_STATE_STOPPED: |
| 3702 | kvm_s390_vcpu_stop(vcpu); |
| 3703 | break; |
| 3704 | case KVM_MP_STATE_OPERATING: |
| 3705 | kvm_s390_vcpu_start(vcpu); |
| 3706 | break; |
| 3707 | case KVM_MP_STATE_LOAD: |
| 3708 | case KVM_MP_STATE_CHECK_STOP: |
| 3709 | /* fall through - CHECK_STOP and LOAD are not supported yet */ |
| 3710 | default: |
| 3711 | rc = -ENXIO; |
| 3712 | } |
| 3713 | |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 3714 | vcpu_put(vcpu); |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 3715 | return rc; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3716 | } |
| 3717 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3718 | static bool ibs_enabled(struct kvm_vcpu *vcpu) |
| 3719 | { |
David Hildenbrand | 8d5fb0d | 2018-01-23 18:05:31 +0100 | [diff] [blame] | 3720 | return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3721 | } |
| 3722 | |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3723 | static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) |
| 3724 | { |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3725 | retry: |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 3726 | kvm_s390_vcpu_request_handled(vcpu); |
Radim Krčmář | 2fa6e1e | 2017-06-04 14:43:52 +0200 | [diff] [blame] | 3727 | if (!kvm_request_pending(vcpu)) |
Christian Borntraeger | 586b7cc | 2015-07-28 15:03:05 +0200 | [diff] [blame] | 3728 | return 0; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3729 | /* |
| 3730 | * We use MMU_RELOAD just to re-arm the ipte notifier for the |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 3731 | * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3732 | * This ensures that the ipte instruction for this request has |
| 3733 | * already finished. We might race against a second unmapper that |
| 3734 | * wants to set the blocking bit. Lets just retry the request loop. |
| 3735 | */ |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3736 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3737 | int rc; |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 3738 | rc = gmap_mprotect_notify(vcpu->arch.gmap, |
| 3739 | kvm_s390_get_prefix(vcpu), |
| 3740 | PAGE_SIZE * 2, PROT_WRITE); |
Julius Niedworok | aca411a | 2016-08-03 16:39:55 +0200 | [diff] [blame] | 3741 | if (rc) { |
| 3742 | kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3743 | return rc; |
Julius Niedworok | aca411a | 2016-08-03 16:39:55 +0200 | [diff] [blame] | 3744 | } |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3745 | goto retry; |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3746 | } |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3747 | |
David Hildenbrand | d3d692c | 2014-07-29 08:53:36 +0200 | [diff] [blame] | 3748 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
| 3749 | vcpu->arch.sie_block->ihcpu = 0xffff; |
| 3750 | goto retry; |
| 3751 | } |
| 3752 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3753 | if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { |
| 3754 | if (!ibs_enabled(vcpu)) { |
| 3755 | trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 3756 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3757 | } |
| 3758 | goto retry; |
| 3759 | } |
| 3760 | |
| 3761 | if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { |
| 3762 | if (ibs_enabled(vcpu)) { |
| 3763 | trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 3764 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 3765 | } |
| 3766 | goto retry; |
| 3767 | } |
| 3768 | |
David Hildenbrand | 6502a34 | 2016-06-21 14:19:51 +0200 | [diff] [blame] | 3769 | if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { |
| 3770 | vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; |
| 3771 | goto retry; |
| 3772 | } |
| 3773 | |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3774 | if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { |
| 3775 | /* |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3776 | * Disable CMM virtualization; we will emulate the ESSA |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3777 | * instruction manually, in order to provide additional |
| 3778 | * functionalities needed for live migration. |
| 3779 | */ |
| 3780 | vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; |
| 3781 | goto retry; |
| 3782 | } |
| 3783 | |
| 3784 | if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { |
| 3785 | /* |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3786 | * Re-enable CMM virtualization if CMMA is available and |
| 3787 | * CMM has been used. |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3788 | */ |
| 3789 | if ((vcpu->kvm->arch.use_cmma) && |
Janosch Frank | c9f0a2b | 2018-02-16 12:16:14 +0100 | [diff] [blame] | 3790 | (vcpu->kvm->mm->context.uses_cmm)) |
Claudio Imbrenda | 190df4a | 2016-08-04 17:54:42 +0200 | [diff] [blame] | 3791 | vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; |
| 3792 | goto retry; |
| 3793 | } |
| 3794 | |
David Hildenbrand | 0759d06 | 2014-05-13 16:54:32 +0200 | [diff] [blame] | 3795 | /* nothing to do, just clear the request */ |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 3796 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
David Hildenbrand | 3194cdb | 2018-09-25 19:16:17 -0400 | [diff] [blame] | 3797 | /* we left the vsie handler, nothing to do, just clear the request */ |
| 3798 | kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); |
David Hildenbrand | 0759d06 | 2014-05-13 16:54:32 +0200 | [diff] [blame] | 3799 | |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3800 | return 0; |
| 3801 | } |
| 3802 | |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 3803 | void kvm_s390_set_tod_clock(struct kvm *kvm, |
| 3804 | const struct kvm_s390_vm_tod_clock *gtod) |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3805 | { |
| 3806 | struct kvm_vcpu *vcpu; |
| 3807 | struct kvm_s390_tod_clock_ext htod; |
| 3808 | int i; |
| 3809 | |
| 3810 | mutex_lock(&kvm->lock); |
| 3811 | preempt_disable(); |
| 3812 | |
| 3813 | get_tod_clock_ext((char *)&htod); |
| 3814 | |
| 3815 | kvm->arch.epoch = gtod->tod - htod.tod; |
David Hildenbrand | 0e7def5 | 2018-02-07 12:46:43 +0100 | [diff] [blame] | 3816 | kvm->arch.epdx = 0; |
| 3817 | if (test_kvm_facility(kvm, 139)) { |
| 3818 | kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx; |
| 3819 | if (kvm->arch.epoch > gtod->tod) |
| 3820 | kvm->arch.epdx -= 1; |
| 3821 | } |
Collin L. Walling | 8fa1696 | 2016-07-26 15:29:44 -0400 | [diff] [blame] | 3822 | |
| 3823 | kvm_s390_vcpu_block_all(kvm); |
| 3824 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 3825 | vcpu->arch.sie_block->epoch = kvm->arch.epoch; |
| 3826 | vcpu->arch.sie_block->epdx = kvm->arch.epdx; |
| 3827 | } |
| 3828 | |
| 3829 | kvm_s390_vcpu_unblock_all(kvm); |
| 3830 | preempt_enable(); |
| 3831 | mutex_unlock(&kvm->lock); |
| 3832 | } |
| 3833 | |
Thomas Huth | fa576c5 | 2014-05-06 17:20:16 +0200 | [diff] [blame] | 3834 | /** |
| 3835 | * kvm_arch_fault_in_page - fault-in guest page if necessary |
| 3836 | * @vcpu: The corresponding virtual cpu |
| 3837 | * @gpa: Guest physical address |
| 3838 | * @writable: Whether the page should be writable or not |
| 3839 | * |
| 3840 | * Make sure that a guest page has been faulted-in on the host. |
| 3841 | * |
| 3842 | * Return: Zero on success, negative error code otherwise. |
| 3843 | */ |
| 3844 | long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 3845 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 3846 | return gmap_fault(vcpu->arch.gmap, gpa, |
| 3847 | writable ? FAULT_FLAG_WRITE : 0); |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 3848 | } |
| 3849 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3850 | static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, |
| 3851 | unsigned long token) |
| 3852 | { |
| 3853 | struct kvm_s390_interrupt inti; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 3854 | struct kvm_s390_irq irq; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3855 | |
| 3856 | if (start_token) { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 3857 | irq.u.ext.ext_params2 = token; |
| 3858 | irq.type = KVM_S390_INT_PFAULT_INIT; |
| 3859 | WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3860 | } else { |
| 3861 | inti.type = KVM_S390_INT_PFAULT_DONE; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 3862 | inti.parm64 = token; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3863 | WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
| 3868 | struct kvm_async_pf *work) |
| 3869 | { |
| 3870 | trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); |
| 3871 | __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); |
| 3872 | } |
| 3873 | |
| 3874 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, |
| 3875 | struct kvm_async_pf *work) |
| 3876 | { |
| 3877 | trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); |
| 3878 | __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); |
| 3879 | } |
| 3880 | |
| 3881 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, |
| 3882 | struct kvm_async_pf *work) |
| 3883 | { |
| 3884 | /* s390 will always inject the page directly */ |
| 3885 | } |
| 3886 | |
| 3887 | bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) |
| 3888 | { |
| 3889 | /* |
| 3890 | * s390 will always inject the page directly, |
| 3891 | * but we still want check_async_completion to cleanup |
| 3892 | */ |
| 3893 | return true; |
| 3894 | } |
| 3895 | |
| 3896 | static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) |
| 3897 | { |
| 3898 | hva_t hva; |
| 3899 | struct kvm_arch_async_pf arch; |
| 3900 | int rc; |
| 3901 | |
| 3902 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
| 3903 | return 0; |
| 3904 | if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != |
| 3905 | vcpu->arch.pfault_compare) |
| 3906 | return 0; |
| 3907 | if (psw_extint_disabled(vcpu)) |
| 3908 | return 0; |
David Hildenbrand | 9a02206 | 2014-08-05 17:40:47 +0200 | [diff] [blame] | 3909 | if (kvm_s390_vcpu_has_irq(vcpu, 0)) |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3910 | return 0; |
David Hildenbrand | b9224cd | 2018-04-30 17:55:24 +0200 | [diff] [blame] | 3911 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3912 | return 0; |
| 3913 | if (!vcpu->arch.gmap->pfault_enabled) |
| 3914 | return 0; |
| 3915 | |
Heiko Carstens | 81480cc | 2014-01-01 16:36:07 +0100 | [diff] [blame] | 3916 | hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); |
| 3917 | hva += current->thread.gmap_addr & ~PAGE_MASK; |
| 3918 | if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3919 | return 0; |
| 3920 | |
| 3921 | rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); |
| 3922 | return rc; |
| 3923 | } |
| 3924 | |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 3925 | static int vcpu_pre_run(struct kvm_vcpu *vcpu) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3926 | { |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 3927 | int rc, cpuflags; |
Carsten Otte | e168bf8 | 2012-01-04 10:25:22 +0100 | [diff] [blame] | 3928 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 3929 | /* |
| 3930 | * On s390 notifications for arriving pages will be delivered directly |
| 3931 | * to the guest but the house keeping for completed pfaults is |
| 3932 | * handled outside the worker. |
| 3933 | */ |
| 3934 | kvm_check_async_pf_completion(vcpu); |
| 3935 | |
Christian Borntraeger | 7ec7c8c | 2015-12-02 14:27:03 +0100 | [diff] [blame] | 3936 | vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; |
| 3937 | vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3938 | |
| 3939 | if (need_resched()) |
| 3940 | schedule(); |
| 3941 | |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 3942 | if (test_cpu_flag(CIF_MCCK_PENDING)) |
Christian Borntraeger | 71cde58 | 2008-05-21 13:37:34 +0200 | [diff] [blame] | 3943 | s390_handle_mcck(); |
| 3944 | |
Jens Freimann | 7939503 | 2014-04-17 10:10:30 +0200 | [diff] [blame] | 3945 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
| 3946 | rc = kvm_s390_deliver_pending_interrupts(vcpu); |
| 3947 | if (rc) |
| 3948 | return rc; |
| 3949 | } |
Carsten Otte | 0ff3186 | 2008-05-21 13:37:37 +0200 | [diff] [blame] | 3950 | |
Christian Borntraeger | 2c70fe4 | 2013-05-17 14:41:36 +0200 | [diff] [blame] | 3951 | rc = kvm_s390_handle_requests(vcpu); |
| 3952 | if (rc) |
| 3953 | return rc; |
| 3954 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 3955 | if (guestdbg_enabled(vcpu)) { |
| 3956 | kvm_s390_backup_guest_per_regs(vcpu); |
| 3957 | kvm_s390_patch_guest_per_regs(vcpu); |
| 3958 | } |
| 3959 | |
Michael Mueller | 9f30f62 | 2019-01-31 09:52:44 +0100 | [diff] [blame] | 3960 | clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask); |
| 3961 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3962 | vcpu->arch.sie_block->icptcode = 0; |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 3963 | cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); |
| 3964 | VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); |
| 3965 | trace_kvm_s390_sie_enter(vcpu, cpuflags); |
Dominik Dingel | 2b29a9f | 2013-07-26 15:04:00 +0200 | [diff] [blame] | 3966 | |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 3967 | return 0; |
| 3968 | } |
| 3969 | |
Thomas Huth | 492d864 | 2015-02-10 16:11:01 +0100 | [diff] [blame] | 3970 | static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) |
| 3971 | { |
David Hildenbrand | 5631792 | 2016-01-12 17:37:58 +0100 | [diff] [blame] | 3972 | struct kvm_s390_pgm_info pgm_info = { |
| 3973 | .code = PGM_ADDRESSING, |
| 3974 | }; |
| 3975 | u8 opcode, ilen; |
Thomas Huth | 492d864 | 2015-02-10 16:11:01 +0100 | [diff] [blame] | 3976 | int rc; |
| 3977 | |
| 3978 | VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); |
| 3979 | trace_kvm_s390_sie_fault(vcpu); |
| 3980 | |
| 3981 | /* |
| 3982 | * We want to inject an addressing exception, which is defined as a |
| 3983 | * suppressing or terminating exception. However, since we came here |
| 3984 | * by a DAT access exception, the PSW still points to the faulting |
| 3985 | * instruction since DAT exceptions are nullifying. So we've got |
| 3986 | * to look up the current opcode to get the length of the instruction |
| 3987 | * to be able to forward the PSW. |
| 3988 | */ |
David Hildenbrand | 3fa8cad7 | 2016-05-24 12:00:49 +0200 | [diff] [blame] | 3989 | rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); |
David Hildenbrand | 5631792 | 2016-01-12 17:37:58 +0100 | [diff] [blame] | 3990 | ilen = insn_length(opcode); |
David Hildenbrand | 9b0d721 | 2016-01-12 17:40:54 +0100 | [diff] [blame] | 3991 | if (rc < 0) { |
| 3992 | return rc; |
| 3993 | } else if (rc) { |
| 3994 | /* Instruction-Fetching Exceptions - we can't detect the ilen. |
| 3995 | * Forward by arbitrary ilc, injection will take care of |
| 3996 | * nullification if necessary. |
| 3997 | */ |
| 3998 | pgm_info = vcpu->arch.pgm; |
| 3999 | ilen = 4; |
| 4000 | } |
David Hildenbrand | 5631792 | 2016-01-12 17:37:58 +0100 | [diff] [blame] | 4001 | pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; |
| 4002 | kvm_s390_forward_psw(vcpu, ilen); |
| 4003 | return kvm_s390_inject_prog_irq(vcpu, &pgm_info); |
Thomas Huth | 492d864 | 2015-02-10 16:11:01 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4006 | static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) |
| 4007 | { |
QingFeng Hao | 4d62fcc | 2017-06-07 12:03:05 +0200 | [diff] [blame] | 4008 | struct mcck_volatile_info *mcck_info; |
| 4009 | struct sie_page *sie_page; |
| 4010 | |
Dominik Dingel | 2b29a9f | 2013-07-26 15:04:00 +0200 | [diff] [blame] | 4011 | VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", |
| 4012 | vcpu->arch.sie_block->icptcode); |
| 4013 | trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); |
| 4014 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4015 | if (guestdbg_enabled(vcpu)) |
| 4016 | kvm_s390_restore_guest_per_regs(vcpu); |
| 4017 | |
Christian Borntraeger | 7ec7c8c | 2015-12-02 14:27:03 +0100 | [diff] [blame] | 4018 | vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; |
| 4019 | vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4020 | |
QingFeng Hao | 4d62fcc | 2017-06-07 12:03:05 +0200 | [diff] [blame] | 4021 | if (exit_reason == -EINTR) { |
| 4022 | VCPU_EVENT(vcpu, 3, "%s", "machine check"); |
| 4023 | sie_page = container_of(vcpu->arch.sie_block, |
| 4024 | struct sie_page, sie_block); |
| 4025 | mcck_info = &sie_page->mcck_info; |
| 4026 | kvm_s390_reinject_machine_check(vcpu, mcck_info); |
| 4027 | return 0; |
| 4028 | } |
| 4029 | |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4030 | if (vcpu->arch.sie_block->icptcode > 0) { |
| 4031 | int rc = kvm_handle_sie_intercept(vcpu); |
| 4032 | |
| 4033 | if (rc != -EOPNOTSUPP) |
| 4034 | return rc; |
| 4035 | vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; |
| 4036 | vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; |
| 4037 | vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; |
| 4038 | vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; |
| 4039 | return -EREMOTE; |
| 4040 | } else if (exit_reason != -EFAULT) { |
| 4041 | vcpu->stat.exit_null++; |
| 4042 | return 0; |
Thomas Huth | 210b1607 | 2013-09-19 16:26:18 +0200 | [diff] [blame] | 4043 | } else if (kvm_is_ucontrol(vcpu->kvm)) { |
| 4044 | vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; |
| 4045 | vcpu->run->s390_ucontrol.trans_exc_code = |
| 4046 | current->thread.gmap_addr; |
| 4047 | vcpu->run->s390_ucontrol.pgm_code = 0x10; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4048 | return -EREMOTE; |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 4049 | } else if (current->thread.gmap_pfault) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 4050 | trace_kvm_s390_major_guest_pfault(vcpu); |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 4051 | current->thread.gmap_pfault = 0; |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4052 | if (kvm_arch_setup_async_pf(vcpu)) |
| 4053 | return 0; |
| 4054 | return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); |
Dominik Dingel | 24eb3a8 | 2013-06-17 16:25:18 +0200 | [diff] [blame] | 4055 | } |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4056 | return vcpu_post_run_fault_in_sie(vcpu); |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | static int __vcpu_run(struct kvm_vcpu *vcpu) |
| 4060 | { |
| 4061 | int rc, exit_reason; |
| 4062 | |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4063 | /* |
| 4064 | * We try to hold kvm->srcu during most of vcpu_run (except when run- |
| 4065 | * ning the guest), so that memslots (and other stuff) are protected |
| 4066 | */ |
| 4067 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4068 | |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4069 | do { |
| 4070 | rc = vcpu_pre_run(vcpu); |
| 4071 | if (rc) |
| 4072 | break; |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4073 | |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4074 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4075 | /* |
| 4076 | * As PF_VCPU will be used in fault handler, between |
| 4077 | * guest_enter and guest_exit should be no uaccess. |
| 4078 | */ |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4079 | local_irq_disable(); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 4080 | guest_enter_irqoff(); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4081 | __disable_cpu_timer_accounting(vcpu); |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4082 | local_irq_enable(); |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4083 | exit_reason = sie64a(vcpu->arch.sie_block, |
| 4084 | vcpu->run->s.regs.gprs); |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4085 | local_irq_disable(); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4086 | __enable_cpu_timer_accounting(vcpu); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 4087 | guest_exit_irqoff(); |
Christian Borntraeger | 0097d12 | 2015-04-30 13:43:30 +0200 | [diff] [blame] | 4088 | local_irq_enable(); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4089 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4090 | |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4091 | rc = vcpu_post_run(vcpu, exit_reason); |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4092 | } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); |
Thomas Huth | 3fb4c40 | 2013-09-12 10:33:43 +0200 | [diff] [blame] | 4093 | |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4094 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Carsten Otte | e168bf8 | 2012-01-04 10:25:22 +0100 | [diff] [blame] | 4095 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4096 | } |
| 4097 | |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4098 | static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 4099 | { |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4100 | struct runtime_instr_cb *riccb; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4101 | struct gs_cb *gscb; |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4102 | |
| 4103 | riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4104 | gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4105 | vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; |
| 4106 | vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; |
| 4107 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) |
| 4108 | kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); |
| 4109 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { |
| 4110 | memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); |
David Hildenbrand | d3d692c | 2014-07-29 08:53:36 +0200 | [diff] [blame] | 4111 | /* some control register changes require a tlb flush */ |
| 4112 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4113 | } |
| 4114 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4115 | kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4116 | vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; |
| 4117 | vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; |
| 4118 | vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; |
| 4119 | vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; |
| 4120 | } |
| 4121 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { |
| 4122 | vcpu->arch.pfault_token = kvm_run->s.regs.pft; |
| 4123 | vcpu->arch.pfault_select = kvm_run->s.regs.pfs; |
| 4124 | vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; |
David Hildenbrand | 9fbd808 | 2014-10-09 15:01:38 +0200 | [diff] [blame] | 4125 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
| 4126 | kvm_clear_async_pf_completion_queue(vcpu); |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4127 | } |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4128 | /* |
| 4129 | * If userspace sets the riccb (e.g. after migration) to a valid state, |
| 4130 | * we should enable RI here instead of doing the lazy enablement. |
| 4131 | */ |
| 4132 | if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4133 | test_kvm_facility(vcpu->kvm, 64) && |
Alice Frosi | bb59c2d | 2017-09-14 12:35:45 +0200 | [diff] [blame] | 4134 | riccb->v && |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 4135 | !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 4136 | VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 4137 | vcpu->arch.sie_block->ecb3 |= ECB3_RI; |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4138 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4139 | /* |
| 4140 | * If userspace sets the gscb (e.g. after migration) to non-zero, |
| 4141 | * we should enable GS here instead of doing the lazy enablement. |
| 4142 | */ |
| 4143 | if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && |
| 4144 | test_kvm_facility(vcpu->kvm, 133) && |
| 4145 | gscb->gssm && |
| 4146 | !vcpu->arch.gs_enabled) { |
| 4147 | VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); |
| 4148 | vcpu->arch.sie_block->ecb |= ECB_GS; |
| 4149 | vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; |
| 4150 | vcpu->arch.gs_enabled = 1; |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4151 | } |
Christian Borntraeger | 35b3fde | 2018-01-17 14:44:34 +0100 | [diff] [blame] | 4152 | if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && |
| 4153 | test_kvm_facility(vcpu->kvm, 82)) { |
| 4154 | vcpu->arch.sie_block->fpf &= ~FPF_BPBC; |
| 4155 | vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; |
| 4156 | } |
Christian Borntraeger | 31d8b8d | 2016-11-10 14:22:02 +0100 | [diff] [blame] | 4157 | save_access_regs(vcpu->arch.host_acrs); |
| 4158 | restore_access_regs(vcpu->run->s.regs.acrs); |
Christian Borntraeger | e1788bb | 2016-11-22 09:29:38 +0100 | [diff] [blame] | 4159 | /* save host (userspace) fprs/vrs */ |
| 4160 | save_fpu_regs(); |
| 4161 | vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; |
| 4162 | vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; |
| 4163 | if (MACHINE_HAS_VX) |
| 4164 | current->thread.fpu.regs = vcpu->run->s.regs.vrs; |
| 4165 | else |
| 4166 | current->thread.fpu.regs = vcpu->run->s.regs.fprs; |
| 4167 | current->thread.fpu.fpc = vcpu->run->s.regs.fpc; |
| 4168 | if (test_fp_ctl(current->thread.fpu.fpc)) |
| 4169 | /* User space provided an invalid FPC, let's clear it */ |
| 4170 | current->thread.fpu.fpc = 0; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4171 | if (MACHINE_HAS_GS) { |
| 4172 | preempt_disable(); |
| 4173 | __ctl_set_bit(2, 4); |
| 4174 | if (current->thread.gs_cb) { |
| 4175 | vcpu->arch.host_gscb = current->thread.gs_cb; |
| 4176 | save_gs_cb(vcpu->arch.host_gscb); |
| 4177 | } |
| 4178 | if (vcpu->arch.gs_enabled) { |
| 4179 | current->thread.gs_cb = (struct gs_cb *) |
| 4180 | &vcpu->run->s.regs.gscb; |
| 4181 | restore_gs_cb(current->thread.gs_cb); |
| 4182 | } |
| 4183 | preempt_enable(); |
| 4184 | } |
Christian Borntraeger | a3da7b4 | 2018-03-08 16:08:49 +0000 | [diff] [blame] | 4185 | /* SIE will load etoken directly from SDNX and therefore kvm_run */ |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 4186 | |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4187 | kvm_run->kvm_dirty_regs = 0; |
| 4188 | } |
| 4189 | |
| 4190 | static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 4191 | { |
| 4192 | kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; |
| 4193 | kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; |
| 4194 | kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); |
| 4195 | memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4196 | kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4197 | kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; |
| 4198 | kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; |
| 4199 | kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; |
| 4200 | kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; |
| 4201 | kvm_run->s.regs.pft = vcpu->arch.pfault_token; |
| 4202 | kvm_run->s.regs.pfs = vcpu->arch.pfault_select; |
| 4203 | kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; |
Christian Borntraeger | 35b3fde | 2018-01-17 14:44:34 +0100 | [diff] [blame] | 4204 | kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; |
Christian Borntraeger | 31d8b8d | 2016-11-10 14:22:02 +0100 | [diff] [blame] | 4205 | save_access_regs(vcpu->run->s.regs.acrs); |
| 4206 | restore_access_regs(vcpu->arch.host_acrs); |
Christian Borntraeger | e1788bb | 2016-11-22 09:29:38 +0100 | [diff] [blame] | 4207 | /* Save guest register state */ |
| 4208 | save_fpu_regs(); |
| 4209 | vcpu->run->s.regs.fpc = current->thread.fpu.fpc; |
| 4210 | /* Restore will be done lazily at return */ |
| 4211 | current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; |
| 4212 | current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 4213 | if (MACHINE_HAS_GS) { |
| 4214 | __ctl_set_bit(2, 4); |
| 4215 | if (vcpu->arch.gs_enabled) |
| 4216 | save_gs_cb(current->thread.gs_cb); |
| 4217 | preempt_disable(); |
| 4218 | current->thread.gs_cb = vcpu->arch.host_gscb; |
| 4219 | restore_gs_cb(vcpu->arch.host_gscb); |
| 4220 | preempt_enable(); |
| 4221 | if (!vcpu->arch.host_gscb) |
| 4222 | __ctl_clear_bit(2, 4); |
| 4223 | vcpu->arch.host_gscb = NULL; |
| 4224 | } |
Christian Borntraeger | a3da7b4 | 2018-03-08 16:08:49 +0000 | [diff] [blame] | 4225 | /* SIE will save etoken directly into SDNX and therefore kvm_run */ |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4228 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 4229 | { |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4230 | int rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4231 | |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 4232 | if (kvm_run->immediate_exit) |
| 4233 | return -EINTR; |
| 4234 | |
Thomas Huth | 200824f | 2019-09-04 10:51:59 +0200 | [diff] [blame] | 4235 | if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || |
| 4236 | kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) |
| 4237 | return -EINVAL; |
| 4238 | |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4239 | vcpu_load(vcpu); |
| 4240 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4241 | if (guestdbg_exit_pending(vcpu)) { |
| 4242 | kvm_s390_prepare_debug_exit(vcpu); |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4243 | rc = 0; |
| 4244 | goto out; |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4245 | } |
| 4246 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 4247 | kvm_sigset_activate(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4248 | |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 4249 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { |
| 4250 | kvm_s390_vcpu_start(vcpu); |
| 4251 | } else if (is_vcpu_stopped(vcpu)) { |
David Hildenbrand | ea2cdd2 | 2015-05-20 13:24:02 +0200 | [diff] [blame] | 4252 | pr_err_ratelimited("can't run stopped vcpu %d\n", |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 4253 | vcpu->vcpu_id); |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4254 | rc = -EINVAL; |
| 4255 | goto out; |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 4256 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4257 | |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4258 | sync_regs(vcpu, kvm_run); |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4259 | enable_cpu_timer_accounting(vcpu); |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 4260 | |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 4261 | might_fault(); |
Thomas Huth | a76ccff | 2013-09-12 10:33:44 +0200 | [diff] [blame] | 4262 | rc = __vcpu_run(vcpu); |
Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 4263 | |
Christian Ehrhardt | b1d16c4 | 2009-05-20 15:34:56 +0200 | [diff] [blame] | 4264 | if (signal_pending(current) && !rc) { |
| 4265 | kvm_run->exit_reason = KVM_EXIT_INTR; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4266 | rc = -EINTR; |
Christian Ehrhardt | b1d16c4 | 2009-05-20 15:34:56 +0200 | [diff] [blame] | 4267 | } |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4268 | |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 4269 | if (guestdbg_exit_pending(vcpu) && !rc) { |
| 4270 | kvm_s390_prepare_debug_exit(vcpu); |
| 4271 | rc = 0; |
| 4272 | } |
| 4273 | |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4274 | if (rc == -EREMOTE) { |
David Hildenbrand | 71f116b | 2015-10-19 16:24:28 +0200 | [diff] [blame] | 4275 | /* userspace support is needed, kvm_run has been prepared */ |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 4276 | rc = 0; |
| 4277 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4278 | |
David Hildenbrand | db0758b | 2016-02-15 09:42:25 +0100 | [diff] [blame] | 4279 | disable_cpu_timer_accounting(vcpu); |
David Hildenbrand | b028ee3 | 2014-07-17 10:47:43 +0200 | [diff] [blame] | 4280 | store_regs(vcpu, kvm_run); |
Carsten Otte | d7b0b5e | 2009-11-19 14:21:16 +0100 | [diff] [blame] | 4281 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 4282 | kvm_sigset_deactivate(vcpu); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4283 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4284 | vcpu->stat.exit_userspace++; |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 4285 | out: |
| 4286 | vcpu_put(vcpu); |
Heiko Carstens | 7e8e6ab | 2008-04-04 15:12:35 +0200 | [diff] [blame] | 4287 | return rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4290 | /* |
| 4291 | * store status at address |
| 4292 | * we use have two special cases: |
| 4293 | * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit |
| 4294 | * KVM_S390_STORE_STATUS_PREFIXED: -> prefix |
| 4295 | */ |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4296 | int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4297 | { |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 4298 | unsigned char archmode = 1; |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4299 | freg_t fprs[NUM_FPRS]; |
Michael Mueller | fda902c | 2014-05-13 16:58:30 +0200 | [diff] [blame] | 4300 | unsigned int px; |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4301 | u64 clkcomp, cputm; |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4302 | int rc; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4303 | |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4304 | px = kvm_s390_get_prefix(vcpu); |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4305 | if (gpa == KVM_S390_STORE_STATUS_NOADDR) { |
| 4306 | if (write_guest_abs(vcpu, 163, &archmode, 1)) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4307 | return -EFAULT; |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4308 | gpa = 0; |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4309 | } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { |
| 4310 | if (write_guest_real(vcpu, 163, &archmode, 1)) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4311 | return -EFAULT; |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4312 | gpa = px; |
| 4313 | } else |
| 4314 | gpa -= __LC_FPREGS_SAVE_AREA; |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4315 | |
| 4316 | /* manually convert vector registers if necessary */ |
| 4317 | if (MACHINE_HAS_VX) { |
David Hildenbrand | 9522b37 | 2016-03-08 12:24:30 +0100 | [diff] [blame] | 4318 | convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4319 | rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, |
| 4320 | fprs, 128); |
| 4321 | } else { |
| 4322 | rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, |
David Hildenbrand | 6fd8e67 | 2016-01-18 14:46:34 +0100 | [diff] [blame] | 4323 | vcpu->run->s.regs.fprs, 128); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4324 | } |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4325 | rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4326 | vcpu->run->s.regs.gprs, 128); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4327 | rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4328 | &vcpu->arch.sie_block->gpsw, 16); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4329 | rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, |
Michael Mueller | fda902c | 2014-05-13 16:58:30 +0200 | [diff] [blame] | 4330 | &px, 4); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4331 | rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4332 | &vcpu->run->s.regs.fpc, 4); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4333 | rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4334 | &vcpu->arch.sie_block->todpr, 4); |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4335 | cputm = kvm_s390_get_cpu_timer(vcpu); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4336 | rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, |
David Hildenbrand | 4287f24 | 2016-02-15 09:40:12 +0100 | [diff] [blame] | 4337 | &cputm, 8); |
Thomas Huth | 178bd78 | 2013-11-13 20:28:18 +0100 | [diff] [blame] | 4338 | clkcomp = vcpu->arch.sie_block->ckc >> 8; |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4339 | rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4340 | &clkcomp, 8); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4341 | rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4342 | &vcpu->run->s.regs.acrs, 64); |
Martin Schwidefsky | d9a3a09 | 2015-10-23 09:02:32 +0200 | [diff] [blame] | 4343 | rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, |
Heiko Carstens | d0bce60 | 2014-01-01 16:45:58 +0100 | [diff] [blame] | 4344 | &vcpu->arch.sie_block->gcr, 128); |
| 4345 | return rc ? -EFAULT : 0; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4346 | } |
| 4347 | |
Thomas Huth | e879892 | 2013-11-06 15:46:33 +0100 | [diff] [blame] | 4348 | int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) |
| 4349 | { |
| 4350 | /* |
| 4351 | * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy |
Christian Borntraeger | 31d8b8d | 2016-11-10 14:22:02 +0100 | [diff] [blame] | 4352 | * switch in the run ioctl. Let's update our copies before we save |
Thomas Huth | e879892 | 2013-11-06 15:46:33 +0100 | [diff] [blame] | 4353 | * it into the save area |
| 4354 | */ |
Hendrik Brueckner | d0164ee | 2015-06-29 16:43:06 +0200 | [diff] [blame] | 4355 | save_fpu_regs(); |
David Hildenbrand | 9abc2a0 | 2016-01-14 22:12:47 +0100 | [diff] [blame] | 4356 | vcpu->run->s.regs.fpc = current->thread.fpu.fpc; |
Thomas Huth | e879892 | 2013-11-06 15:46:33 +0100 | [diff] [blame] | 4357 | save_access_regs(vcpu->run->s.regs.acrs); |
| 4358 | |
| 4359 | return kvm_s390_store_status_unloaded(vcpu, addr); |
| 4360 | } |
| 4361 | |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4362 | static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) |
| 4363 | { |
| 4364 | kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 4365 | kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | static void __disable_ibs_on_all_vcpus(struct kvm *kvm) |
| 4369 | { |
| 4370 | unsigned int i; |
| 4371 | struct kvm_vcpu *vcpu; |
| 4372 | |
| 4373 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4374 | __disable_ibs_on_vcpu(vcpu); |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) |
| 4379 | { |
David Hildenbrand | 09a400e | 2016-04-04 15:57:08 +0200 | [diff] [blame] | 4380 | if (!sclp.has_ibs) |
| 4381 | return; |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4382 | kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); |
Christian Borntraeger | 8e23654 | 2015-04-09 13:49:04 +0200 | [diff] [blame] | 4383 | kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4384 | } |
| 4385 | |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4386 | void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) |
| 4387 | { |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4388 | int i, online_vcpus, started_vcpus = 0; |
| 4389 | |
| 4390 | if (!is_vcpu_stopped(vcpu)) |
| 4391 | return; |
| 4392 | |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4393 | trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4394 | /* Only one cpu at a time may enter/leave the STOPPED state. */ |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4395 | spin_lock(&vcpu->kvm->arch.start_stop_lock); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4396 | online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); |
| 4397 | |
| 4398 | for (i = 0; i < online_vcpus; i++) { |
| 4399 | if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) |
| 4400 | started_vcpus++; |
| 4401 | } |
| 4402 | |
| 4403 | if (started_vcpus == 0) { |
| 4404 | /* we're the only active VCPU -> speed it up */ |
| 4405 | __enable_ibs_on_vcpu(vcpu); |
| 4406 | } else if (started_vcpus == 1) { |
| 4407 | /* |
| 4408 | * As we are starting a second VCPU, we have to disable |
| 4409 | * the IBS facility on all VCPUs to remove potentially |
| 4410 | * oustanding ENABLE requests. |
| 4411 | */ |
| 4412 | __disable_ibs_on_all_vcpus(vcpu->kvm); |
| 4413 | } |
| 4414 | |
David Hildenbrand | 9daecfc | 2018-01-23 18:05:30 +0100 | [diff] [blame] | 4415 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4416 | /* |
| 4417 | * Another VCPU might have used IBS while we were offline. |
| 4418 | * Let's play safe and flush the VCPU at startup. |
| 4419 | */ |
David Hildenbrand | d3d692c | 2014-07-29 08:53:36 +0200 | [diff] [blame] | 4420 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4421 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4422 | return; |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) |
| 4426 | { |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4427 | int i, online_vcpus, started_vcpus = 0; |
| 4428 | struct kvm_vcpu *started_vcpu = NULL; |
| 4429 | |
| 4430 | if (is_vcpu_stopped(vcpu)) |
| 4431 | return; |
| 4432 | |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4433 | trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4434 | /* Only one cpu at a time may enter/leave the STOPPED state. */ |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4435 | spin_lock(&vcpu->kvm->arch.start_stop_lock); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4436 | online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); |
| 4437 | |
David Hildenbrand | 32f5ff63 | 2014-04-14 12:40:03 +0200 | [diff] [blame] | 4438 | /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 4439 | kvm_s390_clear_stop_irq(vcpu); |
David Hildenbrand | 32f5ff63 | 2014-04-14 12:40:03 +0200 | [diff] [blame] | 4440 | |
David Hildenbrand | ef8f4f4 | 2018-01-23 18:05:29 +0100 | [diff] [blame] | 4441 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4442 | __disable_ibs_on_vcpu(vcpu); |
| 4443 | |
| 4444 | for (i = 0; i < online_vcpus; i++) { |
| 4445 | if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { |
| 4446 | started_vcpus++; |
| 4447 | started_vcpu = vcpu->kvm->vcpus[i]; |
| 4448 | } |
| 4449 | } |
| 4450 | |
| 4451 | if (started_vcpus == 1) { |
| 4452 | /* |
| 4453 | * As we only have one VCPU left, we want to enable the |
| 4454 | * IBS facility for that VCPU to speed it up. |
| 4455 | */ |
| 4456 | __enable_ibs_on_vcpu(started_vcpu); |
| 4457 | } |
| 4458 | |
David Hildenbrand | 433b9ee | 2014-05-06 16:11:14 +0200 | [diff] [blame] | 4459 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
David Hildenbrand | 8ad3575 | 2014-03-14 11:00:21 +0100 | [diff] [blame] | 4460 | return; |
David Hildenbrand | 6852d7b | 2014-03-14 10:59:29 +0100 | [diff] [blame] | 4461 | } |
| 4462 | |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 4463 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 4464 | struct kvm_enable_cap *cap) |
| 4465 | { |
| 4466 | int r; |
| 4467 | |
| 4468 | if (cap->flags) |
| 4469 | return -EINVAL; |
| 4470 | |
| 4471 | switch (cap->cap) { |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 4472 | case KVM_CAP_S390_CSS_SUPPORT: |
| 4473 | if (!vcpu->kvm->arch.css_support) { |
| 4474 | vcpu->kvm->arch.css_support = 1; |
Christian Borntraeger | c92ea7b | 2015-07-22 15:52:10 +0200 | [diff] [blame] | 4475 | VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 4476 | trace_kvm_s390_enable_css(vcpu->kvm); |
| 4477 | } |
| 4478 | r = 0; |
| 4479 | break; |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 4480 | default: |
| 4481 | r = -EINVAL; |
| 4482 | break; |
| 4483 | } |
| 4484 | return r; |
| 4485 | } |
| 4486 | |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4487 | static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, |
| 4488 | struct kvm_s390_mem_op *mop) |
| 4489 | { |
| 4490 | void __user *uaddr = (void __user *)mop->buf; |
| 4491 | void *tmpbuf = NULL; |
| 4492 | int r, srcu_idx; |
| 4493 | const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION |
| 4494 | | KVM_S390_MEMOP_F_CHECK_ONLY; |
| 4495 | |
Thomas Huth | a13b03b | 2019-08-29 14:25:17 +0200 | [diff] [blame] | 4496 | if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4497 | return -EINVAL; |
| 4498 | |
| 4499 | if (mop->size > MEM_OP_MAX_SIZE) |
| 4500 | return -E2BIG; |
| 4501 | |
| 4502 | if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { |
| 4503 | tmpbuf = vmalloc(mop->size); |
| 4504 | if (!tmpbuf) |
| 4505 | return -ENOMEM; |
| 4506 | } |
| 4507 | |
| 4508 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4509 | |
| 4510 | switch (mop->op) { |
| 4511 | case KVM_S390_MEMOP_LOGICAL_READ: |
| 4512 | if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 4513 | r = check_gva_range(vcpu, mop->gaddr, mop->ar, |
| 4514 | mop->size, GACC_FETCH); |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4515 | break; |
| 4516 | } |
| 4517 | r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); |
| 4518 | if (r == 0) { |
| 4519 | if (copy_to_user(uaddr, tmpbuf, mop->size)) |
| 4520 | r = -EFAULT; |
| 4521 | } |
| 4522 | break; |
| 4523 | case KVM_S390_MEMOP_LOGICAL_WRITE: |
| 4524 | if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 4525 | r = check_gva_range(vcpu, mop->gaddr, mop->ar, |
| 4526 | mop->size, GACC_STORE); |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4527 | break; |
| 4528 | } |
| 4529 | if (copy_from_user(tmpbuf, uaddr, mop->size)) { |
| 4530 | r = -EFAULT; |
| 4531 | break; |
| 4532 | } |
| 4533 | r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); |
| 4534 | break; |
| 4535 | default: |
| 4536 | r = -EINVAL; |
| 4537 | } |
| 4538 | |
| 4539 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); |
| 4540 | |
| 4541 | if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) |
| 4542 | kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); |
| 4543 | |
| 4544 | vfree(tmpbuf); |
| 4545 | return r; |
| 4546 | } |
| 4547 | |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 4548 | long kvm_arch_vcpu_async_ioctl(struct file *filp, |
| 4549 | unsigned int ioctl, unsigned long arg) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4550 | { |
| 4551 | struct kvm_vcpu *vcpu = filp->private_data; |
| 4552 | void __user *argp = (void __user *)arg; |
| 4553 | |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 4554 | switch (ioctl) { |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 4555 | case KVM_S390_IRQ: { |
| 4556 | struct kvm_s390_irq s390irq; |
| 4557 | |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 4558 | if (copy_from_user(&s390irq, argp, sizeof(s390irq))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4559 | return -EFAULT; |
| 4560 | return kvm_s390_inject_vcpu(vcpu, &s390irq); |
Jens Freimann | 47b43c5 | 2014-11-11 20:57:06 +0100 | [diff] [blame] | 4561 | } |
Avi Kivity | 9373662 | 2010-05-13 12:35:17 +0300 | [diff] [blame] | 4562 | case KVM_S390_INTERRUPT: { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 4563 | struct kvm_s390_interrupt s390int; |
Thomas Huth | 53936b5 | 2019-09-12 13:54:38 +0200 | [diff] [blame] | 4564 | struct kvm_s390_irq s390irq = {}; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 4565 | |
| 4566 | if (copy_from_user(&s390int, argp, sizeof(s390int))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4567 | return -EFAULT; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 4568 | if (s390int_to_s390irq(&s390int, &s390irq)) |
| 4569 | return -EINVAL; |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4570 | return kvm_s390_inject_vcpu(vcpu, &s390irq); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 4571 | } |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4572 | } |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 4573 | return -ENOIOCTLCMD; |
| 4574 | } |
| 4575 | |
| 4576 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 4577 | unsigned int ioctl, unsigned long arg) |
| 4578 | { |
| 4579 | struct kvm_vcpu *vcpu = filp->private_data; |
| 4580 | void __user *argp = (void __user *)arg; |
| 4581 | int idx; |
| 4582 | long r; |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4583 | |
| 4584 | vcpu_load(vcpu); |
| 4585 | |
| 4586 | switch (ioctl) { |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4587 | case KVM_S390_STORE_STATUS: |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4588 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Christian Borntraeger | 5568089 | 2020-01-31 05:02:00 -0500 | [diff] [blame] | 4589 | r = kvm_s390_store_status_unloaded(vcpu, arg); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 4590 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4591 | break; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4592 | case KVM_S390_SET_INITIAL_PSW: { |
| 4593 | psw_t psw; |
| 4594 | |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4595 | r = -EFAULT; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4596 | if (copy_from_user(&psw, argp, sizeof(psw))) |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4597 | break; |
| 4598 | r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); |
| 4599 | break; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4600 | } |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 4601 | case KVM_S390_CLEAR_RESET: |
| 4602 | r = 0; |
| 4603 | kvm_arch_vcpu_ioctl_clear_reset(vcpu); |
| 4604 | break; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4605 | case KVM_S390_INITIAL_RESET: |
Janosch Frank | 7de3f14 | 2020-01-31 05:02:02 -0500 | [diff] [blame] | 4606 | r = 0; |
| 4607 | kvm_arch_vcpu_ioctl_initial_reset(vcpu); |
| 4608 | break; |
| 4609 | case KVM_S390_NORMAL_RESET: |
| 4610 | r = 0; |
| 4611 | kvm_arch_vcpu_ioctl_normal_reset(vcpu); |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4612 | break; |
Carsten Otte | 14eebd9 | 2012-05-15 14:15:26 +0200 | [diff] [blame] | 4613 | case KVM_SET_ONE_REG: |
| 4614 | case KVM_GET_ONE_REG: { |
| 4615 | struct kvm_one_reg reg; |
| 4616 | r = -EFAULT; |
| 4617 | if (copy_from_user(®, argp, sizeof(reg))) |
| 4618 | break; |
| 4619 | if (ioctl == KVM_SET_ONE_REG) |
| 4620 | r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); |
| 4621 | else |
| 4622 | r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); |
| 4623 | break; |
| 4624 | } |
Carsten Otte | 27e0393 | 2012-01-04 10:25:21 +0100 | [diff] [blame] | 4625 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 4626 | case KVM_S390_UCAS_MAP: { |
| 4627 | struct kvm_s390_ucas_mapping ucasmap; |
| 4628 | |
| 4629 | if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { |
| 4630 | r = -EFAULT; |
| 4631 | break; |
| 4632 | } |
| 4633 | |
| 4634 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
| 4635 | r = -EINVAL; |
| 4636 | break; |
| 4637 | } |
| 4638 | |
| 4639 | r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, |
| 4640 | ucasmap.vcpu_addr, ucasmap.length); |
| 4641 | break; |
| 4642 | } |
| 4643 | case KVM_S390_UCAS_UNMAP: { |
| 4644 | struct kvm_s390_ucas_mapping ucasmap; |
| 4645 | |
| 4646 | if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { |
| 4647 | r = -EFAULT; |
| 4648 | break; |
| 4649 | } |
| 4650 | |
| 4651 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
| 4652 | r = -EINVAL; |
| 4653 | break; |
| 4654 | } |
| 4655 | |
| 4656 | r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, |
| 4657 | ucasmap.length); |
| 4658 | break; |
| 4659 | } |
| 4660 | #endif |
Carsten Otte | ccc7910 | 2012-01-04 10:25:26 +0100 | [diff] [blame] | 4661 | case KVM_S390_VCPU_FAULT: { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 4662 | r = gmap_fault(vcpu->arch.gmap, arg, 0); |
Carsten Otte | ccc7910 | 2012-01-04 10:25:26 +0100 | [diff] [blame] | 4663 | break; |
| 4664 | } |
Cornelia Huck | d6712df | 2012-12-20 15:32:11 +0100 | [diff] [blame] | 4665 | case KVM_ENABLE_CAP: |
| 4666 | { |
| 4667 | struct kvm_enable_cap cap; |
| 4668 | r = -EFAULT; |
| 4669 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 4670 | break; |
| 4671 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 4672 | break; |
| 4673 | } |
Thomas Huth | 41408c28 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 4674 | case KVM_S390_MEM_OP: { |
| 4675 | struct kvm_s390_mem_op mem_op; |
| 4676 | |
| 4677 | if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) |
| 4678 | r = kvm_s390_guest_mem_op(vcpu, &mem_op); |
| 4679 | else |
| 4680 | r = -EFAULT; |
| 4681 | break; |
| 4682 | } |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 4683 | case KVM_S390_SET_IRQ_STATE: { |
| 4684 | struct kvm_s390_irq_state irq_state; |
| 4685 | |
| 4686 | r = -EFAULT; |
| 4687 | if (copy_from_user(&irq_state, argp, sizeof(irq_state))) |
| 4688 | break; |
| 4689 | if (irq_state.len > VCPU_IRQS_MAX_BUF || |
| 4690 | irq_state.len == 0 || |
| 4691 | irq_state.len % sizeof(struct kvm_s390_irq) > 0) { |
| 4692 | r = -EINVAL; |
| 4693 | break; |
| 4694 | } |
Christian Borntraeger | bb64da9 | 2017-11-21 16:02:52 +0100 | [diff] [blame] | 4695 | /* do not use irq_state.flags, it will break old QEMUs */ |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 4696 | r = kvm_s390_set_irq_state(vcpu, |
| 4697 | (void __user *) irq_state.buf, |
| 4698 | irq_state.len); |
| 4699 | break; |
| 4700 | } |
| 4701 | case KVM_S390_GET_IRQ_STATE: { |
| 4702 | struct kvm_s390_irq_state irq_state; |
| 4703 | |
| 4704 | r = -EFAULT; |
| 4705 | if (copy_from_user(&irq_state, argp, sizeof(irq_state))) |
| 4706 | break; |
| 4707 | if (irq_state.len == 0) { |
| 4708 | r = -EINVAL; |
| 4709 | break; |
| 4710 | } |
Christian Borntraeger | bb64da9 | 2017-11-21 16:02:52 +0100 | [diff] [blame] | 4711 | /* do not use irq_state.flags, it will break old QEMUs */ |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 4712 | r = kvm_s390_get_irq_state(vcpu, |
| 4713 | (__u8 __user *) irq_state.buf, |
| 4714 | irq_state.len); |
| 4715 | break; |
| 4716 | } |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4717 | default: |
Carsten Otte | 3e6afcf | 2012-01-04 10:25:30 +0100 | [diff] [blame] | 4718 | r = -ENOTTY; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4719 | } |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 4720 | |
| 4721 | vcpu_put(vcpu); |
Avi Kivity | bc923cc | 2010-05-13 12:21:46 +0300 | [diff] [blame] | 4722 | return r; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4723 | } |
| 4724 | |
Souptick Joarder | 1499fa8 | 2018-04-19 00:49:58 +0530 | [diff] [blame] | 4725 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 4726 | { |
| 4727 | #ifdef CONFIG_KVM_S390_UCONTROL |
| 4728 | if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) |
| 4729 | && (kvm_is_ucontrol(vcpu->kvm))) { |
| 4730 | vmf->page = virt_to_page(vcpu->arch.sie_block); |
| 4731 | get_page(vmf->page); |
| 4732 | return 0; |
| 4733 | } |
| 4734 | #endif |
| 4735 | return VM_FAULT_SIGBUS; |
| 4736 | } |
| 4737 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 4738 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 4739 | unsigned long npages) |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 4740 | { |
| 4741 | return 0; |
| 4742 | } |
| 4743 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4744 | /* Section: memory related */ |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 4745 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 4746 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 4747 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 7b6195a | 2013-02-27 19:44:34 +0900 | [diff] [blame] | 4748 | enum kvm_mr_change change) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4749 | { |
Nick Wang | dd2887e | 2013-03-25 17:22:57 +0100 | [diff] [blame] | 4750 | /* A few sanity checks. We can have memory slots which have to be |
| 4751 | located/ended at a segment boundary (1MB). The memory in userland is |
| 4752 | ok to be fragmented into various different vmas. It is okay to mmap() |
| 4753 | and munmap() stuff in this slot after doing this call at any time */ |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4754 | |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 4755 | if (mem->userspace_addr & 0xffffful) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4756 | return -EINVAL; |
| 4757 | |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 4758 | if (mem->memory_size & 0xffffful) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4759 | return -EINVAL; |
| 4760 | |
Dominik Dingel | a3a92c3 | 2014-12-01 17:24:42 +0100 | [diff] [blame] | 4761 | if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit) |
| 4762 | return -EINVAL; |
| 4763 | |
Janosch Frank | 29b40f1 | 2019-09-30 04:19:18 -0400 | [diff] [blame] | 4764 | /* When we are protected, we should not change the memory slots */ |
| 4765 | if (kvm_s390_pv_get_handle(kvm)) |
| 4766 | return -EINVAL; |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 4767 | return 0; |
| 4768 | } |
| 4769 | |
| 4770 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 4771 | const struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 4772 | const struct kvm_memory_slot *old, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 4773 | const struct kvm_memory_slot *new, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 4774 | enum kvm_mr_change change) |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 4775 | { |
Christian Borntraeger | 19ec166 | 2019-05-24 16:06:23 +0200 | [diff] [blame] | 4776 | int rc = 0; |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 4777 | |
Christian Borntraeger | 19ec166 | 2019-05-24 16:06:23 +0200 | [diff] [blame] | 4778 | switch (change) { |
| 4779 | case KVM_MR_DELETE: |
| 4780 | rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, |
| 4781 | old->npages * PAGE_SIZE); |
| 4782 | break; |
| 4783 | case KVM_MR_MOVE: |
| 4784 | rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, |
| 4785 | old->npages * PAGE_SIZE); |
| 4786 | if (rc) |
| 4787 | break; |
| 4788 | /* FALLTHROUGH */ |
| 4789 | case KVM_MR_CREATE: |
| 4790 | rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, |
| 4791 | mem->guest_phys_addr, mem->memory_size); |
| 4792 | break; |
| 4793 | case KVM_MR_FLAGS_ONLY: |
| 4794 | break; |
| 4795 | default: |
| 4796 | WARN(1, "Unknown KVM MR CHANGE: %d\n", change); |
| 4797 | } |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 4798 | if (rc) |
David Hildenbrand | ea2cdd2 | 2015-05-20 13:24:02 +0200 | [diff] [blame] | 4799 | pr_warn("failed to commit memory region\n"); |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 4800 | return; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4801 | } |
| 4802 | |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 4803 | static inline unsigned long nonhyp_mask(int i) |
| 4804 | { |
| 4805 | unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; |
| 4806 | |
| 4807 | return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); |
| 4808 | } |
| 4809 | |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 4810 | void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) |
| 4811 | { |
| 4812 | vcpu->valid_wakeup = false; |
| 4813 | } |
| 4814 | |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4815 | static int __init kvm_s390_init(void) |
| 4816 | { |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 4817 | int i; |
| 4818 | |
David Hildenbrand | 07197fd | 2015-01-30 16:01:38 +0100 | [diff] [blame] | 4819 | if (!sclp.has_sief2) { |
Michael Mueller | 8d43d57 | 2018-12-10 11:15:16 +0100 | [diff] [blame] | 4820 | pr_info("SIE is not available\n"); |
David Hildenbrand | 07197fd | 2015-01-30 16:01:38 +0100 | [diff] [blame] | 4821 | return -ENODEV; |
| 4822 | } |
| 4823 | |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 4824 | if (nested && hpage) { |
Michael Mueller | 8d43d57 | 2018-12-10 11:15:16 +0100 | [diff] [blame] | 4825 | pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); |
Janosch Frank | a449938 | 2018-07-13 11:28:31 +0100 | [diff] [blame] | 4826 | return -EINVAL; |
| 4827 | } |
| 4828 | |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 4829 | for (i = 0; i < 16; i++) |
Christian Borntraeger | c3b9e3e | 2018-02-09 16:26:29 +0000 | [diff] [blame] | 4830 | kvm_s390_fac_base[i] |= |
Alexander Yarygin | 60a3770 | 2016-04-01 15:38:57 +0300 | [diff] [blame] | 4831 | S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i); |
| 4832 | |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 4833 | return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | static void __exit kvm_s390_exit(void) |
| 4837 | { |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 4838 | kvm_exit(); |
| 4839 | } |
| 4840 | |
| 4841 | module_init(kvm_s390_init); |
| 4842 | module_exit(kvm_s390_exit); |
Cornelia Huck | 566af94 | 2013-05-27 18:42:33 +0200 | [diff] [blame] | 4843 | |
| 4844 | /* |
| 4845 | * Enable autoloading of the kvm module. |
| 4846 | * Note that we add the module alias here instead of virt/kvm/kvm_main.c |
| 4847 | * since x86 takes a different approach. |
| 4848 | */ |
| 4849 | #include <linux/miscdevice.h> |
| 4850 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 4851 | MODULE_ALIAS("devname:kvm"); |