Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * handling privileged instructions |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 3 | * |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2008, 2013 |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License (version 2 only) |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 11 | * Christian Borntraeger <borntraeger@de.ibm.com> |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kvm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 16 | #include <linux/errno.h> |
Heiko Carstens | b13b5dc | 2013-03-25 17:22:55 +0100 | [diff] [blame] | 17 | #include <linux/compat.h> |
Ingo Molnar | 589ee62 | 2017-02-04 00:16:44 +0100 | [diff] [blame] | 18 | #include <linux/mm_types.h> |
| 19 | |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 20 | #include <asm/asm-offsets.h> |
Heiko Carstens | e769ece | 2013-07-26 15:04:01 +0200 | [diff] [blame] | 21 | #include <asm/facility.h> |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 22 | #include <asm/current.h> |
| 23 | #include <asm/debug.h> |
| 24 | #include <asm/ebcdic.h> |
| 25 | #include <asm/sysinfo.h> |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 26 | #include <asm/pgtable.h> |
| 27 | #include <asm/pgalloc.h> |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 28 | #include <asm/gmap.h> |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 29 | #include <asm/io.h> |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 30 | #include <asm/ptrace.h> |
| 31 | #include <asm/compat.h> |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 32 | #include <asm/sclp.h> |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 33 | #include "gaccess.h" |
| 34 | #include "kvm-s390.h" |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 35 | #include "trace.h" |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 36 | |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 37 | static int handle_ri(struct kvm_vcpu *vcpu) |
| 38 | { |
| 39 | if (test_kvm_facility(vcpu->kvm, 64)) { |
Christian Borntraeger | 4d5f2c0 | 2017-02-09 17:15:41 +0100 | [diff] [blame] | 40 | VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)"); |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 41 | vcpu->arch.sie_block->ecb3 |= ECB3_RI; |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 42 | kvm_s390_retry_instr(vcpu); |
| 43 | return 0; |
| 44 | } else |
| 45 | return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); |
| 46 | } |
| 47 | |
| 48 | int kvm_s390_handle_aa(struct kvm_vcpu *vcpu) |
| 49 | { |
| 50 | if ((vcpu->arch.sie_block->ipa & 0xf) <= 4) |
| 51 | return handle_ri(vcpu); |
| 52 | else |
| 53 | return -EOPNOTSUPP; |
| 54 | } |
| 55 | |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 56 | static int handle_gs(struct kvm_vcpu *vcpu) |
| 57 | { |
| 58 | if (test_kvm_facility(vcpu->kvm, 133)) { |
| 59 | VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)"); |
| 60 | preempt_disable(); |
| 61 | __ctl_set_bit(2, 4); |
| 62 | current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb; |
| 63 | restore_gs_cb(current->thread.gs_cb); |
| 64 | preempt_enable(); |
| 65 | vcpu->arch.sie_block->ecb |= ECB_GS; |
| 66 | vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; |
| 67 | vcpu->arch.gs_enabled = 1; |
| 68 | kvm_s390_retry_instr(vcpu); |
| 69 | return 0; |
| 70 | } else |
| 71 | return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); |
| 72 | } |
| 73 | |
| 74 | int kvm_s390_handle_e3(struct kvm_vcpu *vcpu) |
| 75 | { |
| 76 | int code = vcpu->arch.sie_block->ipb & 0xff; |
| 77 | |
| 78 | if (code == 0x49 || code == 0x4d) |
| 79 | return handle_gs(vcpu); |
| 80 | else |
| 81 | return -EOPNOTSUPP; |
| 82 | } |
Thomas Huth | 6a3f95a | 2013-09-12 10:33:49 +0200 | [diff] [blame] | 83 | /* Handle SCK (SET CLOCK) interception */ |
| 84 | static int handle_set_clock(struct kvm_vcpu *vcpu) |
| 85 | { |
David Hildenbrand | 25ed167 | 2015-05-12 09:49:14 +0200 | [diff] [blame] | 86 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 87 | u8 ar; |
David Hildenbrand | 25ed167 | 2015-05-12 09:49:14 +0200 | [diff] [blame] | 88 | u64 op2, val; |
Thomas Huth | 6a3f95a | 2013-09-12 10:33:49 +0200 | [diff] [blame] | 89 | |
| 90 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 91 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 92 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 93 | op2 = kvm_s390_get_base_disp_s(vcpu, &ar); |
Thomas Huth | 6a3f95a | 2013-09-12 10:33:49 +0200 | [diff] [blame] | 94 | if (op2 & 7) /* Operand must be on a doubleword boundary */ |
| 95 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 96 | rc = read_guest(vcpu, op2, ar, &val, sizeof(val)); |
Heiko Carstens | 0e7a3f9 | 2014-01-01 16:50:11 +0100 | [diff] [blame] | 97 | if (rc) |
| 98 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Thomas Huth | 6a3f95a | 2013-09-12 10:33:49 +0200 | [diff] [blame] | 99 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 100 | VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val); |
David Hildenbrand | 25ed167 | 2015-05-12 09:49:14 +0200 | [diff] [blame] | 101 | kvm_s390_set_tod_clock(vcpu->kvm, val); |
Thomas Huth | 6a3f95a | 2013-09-12 10:33:49 +0200 | [diff] [blame] | 102 | |
| 103 | kvm_s390_set_psw_cc(vcpu, 0); |
| 104 | return 0; |
| 105 | } |
| 106 | |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 107 | static int handle_set_prefix(struct kvm_vcpu *vcpu) |
| 108 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 109 | u64 operand2; |
Heiko Carstens | 665170c | 2014-01-01 16:47:12 +0100 | [diff] [blame] | 110 | u32 address; |
| 111 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 112 | u8 ar; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 113 | |
| 114 | vcpu->stat.instruction_spx++; |
| 115 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 116 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 117 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 118 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 119 | operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 120 | |
| 121 | /* must be word boundary */ |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 122 | if (operand2 & 3) |
| 123 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 124 | |
| 125 | /* get the value */ |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 126 | rc = read_guest(vcpu, operand2, ar, &address, sizeof(address)); |
Heiko Carstens | 665170c | 2014-01-01 16:47:12 +0100 | [diff] [blame] | 127 | if (rc) |
| 128 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 129 | |
Heiko Carstens | 665170c | 2014-01-01 16:47:12 +0100 | [diff] [blame] | 130 | address &= 0x7fffe000u; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 131 | |
Heiko Carstens | 665170c | 2014-01-01 16:47:12 +0100 | [diff] [blame] | 132 | /* |
| 133 | * Make sure the new value is valid memory. We only need to check the |
| 134 | * first page, since address is 8k aligned and memory pieces are always |
| 135 | * at least 1MB aligned and have at least a size of 1MB. |
| 136 | */ |
| 137 | if (kvm_is_error_gpa(vcpu->kvm, address)) |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 138 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 139 | |
Christian Borntraeger | 8d26cf7 | 2012-01-11 11:19:32 +0100 | [diff] [blame] | 140 | kvm_s390_set_prefix(vcpu, address); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 141 | trace_kvm_s390_handle_prefix(vcpu, 1, address); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int handle_store_prefix(struct kvm_vcpu *vcpu) |
| 146 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 147 | u64 operand2; |
| 148 | u32 address; |
Heiko Carstens | f748f4a | 2014-01-01 16:52:47 +0100 | [diff] [blame] | 149 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 150 | u8 ar; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 151 | |
| 152 | vcpu->stat.instruction_stpx++; |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 153 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 154 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 155 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 156 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 157 | operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 158 | |
| 159 | /* must be word boundary */ |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 160 | if (operand2 & 3) |
| 161 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 162 | |
Michael Mueller | fda902c | 2014-05-13 16:58:30 +0200 | [diff] [blame] | 163 | address = kvm_s390_get_prefix(vcpu); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 164 | |
| 165 | /* get the value */ |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 166 | rc = write_guest(vcpu, operand2, ar, &address, sizeof(address)); |
Heiko Carstens | f748f4a | 2014-01-01 16:52:47 +0100 | [diff] [blame] | 167 | if (rc) |
| 168 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 169 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 170 | VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 171 | trace_kvm_s390_handle_prefix(vcpu, 0, address); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int handle_store_cpu_address(struct kvm_vcpu *vcpu) |
| 176 | { |
Heiko Carstens | 8b96de0 | 2014-01-01 16:53:27 +0100 | [diff] [blame] | 177 | u16 vcpu_id = vcpu->vcpu_id; |
| 178 | u64 ga; |
| 179 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 180 | u8 ar; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 181 | |
| 182 | vcpu->stat.instruction_stap++; |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 183 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 184 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 185 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 186 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 187 | ga = kvm_s390_get_base_disp_s(vcpu, &ar); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 188 | |
Heiko Carstens | 8b96de0 | 2014-01-01 16:53:27 +0100 | [diff] [blame] | 189 | if (ga & 1) |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 190 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 191 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 192 | rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id)); |
Heiko Carstens | 8b96de0 | 2014-01-01 16:53:27 +0100 | [diff] [blame] | 193 | if (rc) |
| 194 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 195 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 196 | VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga); |
Heiko Carstens | 8b96de0 | 2014-01-01 16:53:27 +0100 | [diff] [blame] | 197 | trace_kvm_s390_handle_stap(vcpu, ga); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 201 | static int __skey_check_enable(struct kvm_vcpu *vcpu) |
Dominik Dingel | 693ffc0 | 2014-01-14 18:11:14 +0100 | [diff] [blame] | 202 | { |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 203 | int rc = 0; |
David Hildenbrand | 11ddcd4 | 2016-05-10 09:40:09 +0200 | [diff] [blame] | 204 | |
| 205 | trace_kvm_s390_skey_related_inst(vcpu); |
Dominik Dingel | 693ffc0 | 2014-01-14 18:11:14 +0100 | [diff] [blame] | 206 | if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE))) |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 207 | return rc; |
Dominik Dingel | 693ffc0 | 2014-01-14 18:11:14 +0100 | [diff] [blame] | 208 | |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 209 | rc = s390_enable_skey(); |
David Hildenbrand | 11ddcd4 | 2016-05-10 09:40:09 +0200 | [diff] [blame] | 210 | VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc); |
| 211 | if (!rc) |
| 212 | vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE); |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 213 | return rc; |
Dominik Dingel | 693ffc0 | 2014-01-14 18:11:14 +0100 | [diff] [blame] | 214 | } |
| 215 | |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 216 | static int try_handle_skey(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 217 | { |
David Hildenbrand | 11ddcd4 | 2016-05-10 09:40:09 +0200 | [diff] [blame] | 218 | int rc; |
Dominik Dingel | 693ffc0 | 2014-01-14 18:11:14 +0100 | [diff] [blame] | 219 | |
David Hildenbrand | 11ddcd4 | 2016-05-10 09:40:09 +0200 | [diff] [blame] | 220 | vcpu->stat.instruction_storage_key++; |
| 221 | rc = __skey_check_enable(vcpu); |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 222 | if (rc) |
| 223 | return rc; |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 224 | if (sclp.has_skey) { |
| 225 | /* with storage-key facility, SIE interprets it for us */ |
| 226 | kvm_s390_retry_instr(vcpu); |
| 227 | VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation"); |
| 228 | return -EAGAIN; |
| 229 | } |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 230 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 231 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 232 | return 0; |
| 233 | } |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 234 | |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 235 | static int handle_iske(struct kvm_vcpu *vcpu) |
| 236 | { |
| 237 | unsigned long addr; |
| 238 | unsigned char key; |
| 239 | int reg1, reg2; |
| 240 | int rc; |
| 241 | |
| 242 | rc = try_handle_skey(vcpu); |
| 243 | if (rc) |
| 244 | return rc != -EAGAIN ? rc : 0; |
| 245 | |
| 246 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
| 247 | |
| 248 | addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; |
| 249 | addr = kvm_s390_logical_to_effective(vcpu, addr); |
| 250 | addr = kvm_s390_real_to_abs(vcpu, addr); |
| 251 | addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr)); |
| 252 | if (kvm_is_error_hva(addr)) |
| 253 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 254 | |
| 255 | down_read(¤t->mm->mmap_sem); |
| 256 | rc = get_guest_storage_key(current->mm, addr, &key); |
| 257 | up_read(¤t->mm->mmap_sem); |
| 258 | if (rc) |
| 259 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 260 | vcpu->run->s.regs.gprs[reg1] &= ~0xff; |
| 261 | vcpu->run->s.regs.gprs[reg1] |= key; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int handle_rrbe(struct kvm_vcpu *vcpu) |
| 266 | { |
| 267 | unsigned long addr; |
| 268 | int reg1, reg2; |
| 269 | int rc; |
| 270 | |
| 271 | rc = try_handle_skey(vcpu); |
| 272 | if (rc) |
| 273 | return rc != -EAGAIN ? rc : 0; |
| 274 | |
| 275 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
| 276 | |
| 277 | addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; |
| 278 | addr = kvm_s390_logical_to_effective(vcpu, addr); |
| 279 | addr = kvm_s390_real_to_abs(vcpu, addr); |
| 280 | addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr)); |
| 281 | if (kvm_is_error_hva(addr)) |
| 282 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 283 | |
| 284 | down_read(¤t->mm->mmap_sem); |
| 285 | rc = reset_guest_reference_bit(current->mm, addr); |
| 286 | up_read(¤t->mm->mmap_sem); |
| 287 | if (rc < 0) |
| 288 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 289 | |
| 290 | kvm_s390_set_psw_cc(vcpu, rc); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | #define SSKE_NQ 0x8 |
| 295 | #define SSKE_MR 0x4 |
| 296 | #define SSKE_MC 0x2 |
| 297 | #define SSKE_MB 0x1 |
| 298 | static int handle_sske(struct kvm_vcpu *vcpu) |
| 299 | { |
| 300 | unsigned char m3 = vcpu->arch.sie_block->ipb >> 28; |
| 301 | unsigned long start, end; |
| 302 | unsigned char key, oldkey; |
| 303 | int reg1, reg2; |
| 304 | int rc; |
| 305 | |
| 306 | rc = try_handle_skey(vcpu); |
| 307 | if (rc) |
| 308 | return rc != -EAGAIN ? rc : 0; |
| 309 | |
| 310 | if (!test_kvm_facility(vcpu->kvm, 8)) |
| 311 | m3 &= ~SSKE_MB; |
| 312 | if (!test_kvm_facility(vcpu->kvm, 10)) |
| 313 | m3 &= ~(SSKE_MC | SSKE_MR); |
| 314 | if (!test_kvm_facility(vcpu->kvm, 14)) |
| 315 | m3 &= ~SSKE_NQ; |
| 316 | |
| 317 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
| 318 | |
| 319 | key = vcpu->run->s.regs.gprs[reg1] & 0xfe; |
| 320 | start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; |
| 321 | start = kvm_s390_logical_to_effective(vcpu, start); |
| 322 | if (m3 & SSKE_MB) { |
| 323 | /* start already designates an absolute address */ |
| 324 | end = (start + (1UL << 20)) & ~((1UL << 20) - 1); |
| 325 | } else { |
| 326 | start = kvm_s390_real_to_abs(vcpu, start); |
| 327 | end = start + PAGE_SIZE; |
| 328 | } |
| 329 | |
| 330 | while (start != end) { |
| 331 | unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start)); |
| 332 | |
| 333 | if (kvm_is_error_hva(addr)) |
| 334 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 335 | |
| 336 | down_read(¤t->mm->mmap_sem); |
| 337 | rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey, |
| 338 | m3 & SSKE_NQ, m3 & SSKE_MR, |
| 339 | m3 & SSKE_MC); |
| 340 | up_read(¤t->mm->mmap_sem); |
| 341 | if (rc < 0) |
| 342 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 343 | start += PAGE_SIZE; |
Heiko Carstens | 0b92515 | 2017-01-02 08:51:02 +0100 | [diff] [blame] | 344 | } |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 345 | |
| 346 | if (m3 & (SSKE_MC | SSKE_MR)) { |
| 347 | if (m3 & SSKE_MB) { |
| 348 | /* skey in reg1 is unpredictable */ |
| 349 | kvm_s390_set_psw_cc(vcpu, 3); |
| 350 | } else { |
| 351 | kvm_s390_set_psw_cc(vcpu, rc); |
| 352 | vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL; |
| 353 | vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8; |
| 354 | } |
| 355 | } |
| 356 | if (m3 & SSKE_MB) { |
| 357 | if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) |
| 358 | vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK; |
| 359 | else |
| 360 | vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL; |
| 361 | end = kvm_s390_logical_to_effective(vcpu, end); |
| 362 | vcpu->run->s.regs.gprs[reg2] |= end; |
| 363 | } |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 367 | static int handle_ipte_interlock(struct kvm_vcpu *vcpu) |
| 368 | { |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 369 | vcpu->stat.instruction_ipte_interlock++; |
Thomas Huth | 04b41ac | 2014-11-12 17:13:29 +0100 | [diff] [blame] | 370 | if (psw_bits(vcpu->arch.sie_block->gpsw).p) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 371 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 372 | wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu)); |
David Hildenbrand | 0e8bc06 | 2015-11-04 13:47:58 +0100 | [diff] [blame] | 373 | kvm_s390_retry_instr(vcpu); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 374 | VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation"); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Thomas Huth | aca8424 | 2013-09-12 10:33:48 +0200 | [diff] [blame] | 378 | static int handle_test_block(struct kvm_vcpu *vcpu) |
| 379 | { |
Thomas Huth | aca8424 | 2013-09-12 10:33:48 +0200 | [diff] [blame] | 380 | gpa_t addr; |
| 381 | int reg2; |
| 382 | |
| 383 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 384 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 385 | |
| 386 | kvm_s390_get_regs_rre(vcpu, NULL, ®2); |
| 387 | addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; |
Thomas Huth | e45efa2 | 2014-03-07 12:14:23 +0100 | [diff] [blame] | 388 | addr = kvm_s390_logical_to_effective(vcpu, addr); |
Alexander Yarygin | dd9e5b7 | 2015-03-03 14:26:14 +0300 | [diff] [blame] | 389 | if (kvm_s390_check_low_addr_prot_real(vcpu, addr)) |
Thomas Huth | e45efa2 | 2014-03-07 12:14:23 +0100 | [diff] [blame] | 390 | return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); |
Thomas Huth | aca8424 | 2013-09-12 10:33:48 +0200 | [diff] [blame] | 391 | addr = kvm_s390_real_to_abs(vcpu, addr); |
| 392 | |
Heiko Carstens | ef23e77 | 2014-01-01 16:53:49 +0100 | [diff] [blame] | 393 | if (kvm_is_error_gpa(vcpu->kvm, addr)) |
Thomas Huth | aca8424 | 2013-09-12 10:33:48 +0200 | [diff] [blame] | 394 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 395 | /* |
| 396 | * We don't expect errors on modern systems, and do not care |
| 397 | * about storage keys (yet), so let's just clear the page. |
| 398 | */ |
Heiko Carstens | ef23e77 | 2014-01-01 16:53:49 +0100 | [diff] [blame] | 399 | if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE)) |
Thomas Huth | aca8424 | 2013-09-12 10:33:48 +0200 | [diff] [blame] | 400 | return -EFAULT; |
| 401 | kvm_s390_set_psw_cc(vcpu, 0); |
| 402 | vcpu->run->s.regs.gprs[0] = 0; |
| 403 | return 0; |
| 404 | } |
| 405 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 406 | static int handle_tpi(struct kvm_vcpu *vcpu) |
| 407 | { |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 408 | struct kvm_s390_interrupt_info *inti; |
Heiko Carstens | 4799b55 | 2014-01-01 16:55:48 +0100 | [diff] [blame] | 409 | unsigned long len; |
| 410 | u32 tpi_data[3]; |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 411 | int rc; |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 412 | u64 addr; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 413 | u8 ar; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 414 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 415 | addr = kvm_s390_get_base_disp_s(vcpu, &ar); |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 416 | if (addr & 3) |
| 417 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 418 | |
Thomas Huth | f092669 | 2013-10-09 14:15:54 +0200 | [diff] [blame] | 419 | inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0); |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 420 | if (!inti) { |
| 421 | kvm_s390_set_psw_cc(vcpu, 0); |
| 422 | return 0; |
| 423 | } |
| 424 | |
Heiko Carstens | 4799b55 | 2014-01-01 16:55:48 +0100 | [diff] [blame] | 425 | tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr; |
| 426 | tpi_data[1] = inti->io.io_int_parm; |
| 427 | tpi_data[2] = inti->io.io_int_word; |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 428 | if (addr) { |
| 429 | /* |
| 430 | * Store the two-word I/O interruption code into the |
| 431 | * provided area. |
| 432 | */ |
Heiko Carstens | 4799b55 | 2014-01-01 16:55:48 +0100 | [diff] [blame] | 433 | len = sizeof(tpi_data) - 4; |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 434 | rc = write_guest(vcpu, addr, ar, &tpi_data, len); |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 435 | if (rc) { |
| 436 | rc = kvm_s390_inject_prog_cond(vcpu, rc); |
| 437 | goto reinject_interrupt; |
| 438 | } |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 439 | } else { |
| 440 | /* |
| 441 | * Store the three-word I/O interruption code into |
| 442 | * the appropriate lowcore area. |
| 443 | */ |
Heiko Carstens | 4799b55 | 2014-01-01 16:55:48 +0100 | [diff] [blame] | 444 | len = sizeof(tpi_data); |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 445 | if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) { |
| 446 | /* failed writes to the low core are not recoverable */ |
Heiko Carstens | 4799b55 | 2014-01-01 16:55:48 +0100 | [diff] [blame] | 447 | rc = -EFAULT; |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 448 | goto reinject_interrupt; |
| 449 | } |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 450 | } |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 451 | |
| 452 | /* irq was successfully handed to the guest */ |
| 453 | kfree(inti); |
| 454 | kvm_s390_set_psw_cc(vcpu, 1); |
| 455 | return 0; |
| 456 | reinject_interrupt: |
Cornelia Huck | 2f32d4e | 2014-01-08 18:07:54 +0100 | [diff] [blame] | 457 | /* |
| 458 | * If we encounter a problem storing the interruption code, the |
| 459 | * instruction is suppressed from the guest's view: reinject the |
| 460 | * interrupt. |
| 461 | */ |
David Hildenbrand | 15462e3 | 2015-02-04 15:59:11 +0100 | [diff] [blame] | 462 | if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) { |
| 463 | kfree(inti); |
| 464 | rc = -EFAULT; |
| 465 | } |
David Hildenbrand | 261520d | 2015-02-04 15:53:42 +0100 | [diff] [blame] | 466 | /* don't set the cc, a pgm irq was injected or we drop to user space */ |
Heiko Carstens | 4799b55 | 2014-01-01 16:55:48 +0100 | [diff] [blame] | 467 | return rc ? -EFAULT : 0; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static int handle_tsch(struct kvm_vcpu *vcpu) |
| 471 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 472 | struct kvm_s390_interrupt_info *inti = NULL; |
| 473 | const u64 isc_mask = 0xffUL << 24; /* all iscs set */ |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 474 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 475 | /* a valid schid has at least one bit set */ |
| 476 | if (vcpu->run->s.regs.gprs[1]) |
| 477 | inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask, |
| 478 | vcpu->run->s.regs.gprs[1]); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * Prepare exit to userspace. |
| 482 | * We indicate whether we dequeued a pending I/O interrupt |
| 483 | * so that userspace can re-inject it if the instruction gets |
| 484 | * a program check. While this may re-order the pending I/O |
| 485 | * interrupts, this is no problem since the priority is kept |
| 486 | * intact. |
| 487 | */ |
| 488 | vcpu->run->exit_reason = KVM_EXIT_S390_TSCH; |
| 489 | vcpu->run->s390_tsch.dequeued = !!inti; |
| 490 | if (inti) { |
| 491 | vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id; |
| 492 | vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr; |
| 493 | vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm; |
| 494 | vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word; |
| 495 | } |
| 496 | vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb; |
| 497 | kfree(inti); |
| 498 | return -EREMOTE; |
| 499 | } |
| 500 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 501 | static int handle_io_inst(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 502 | { |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 503 | VCPU_EVENT(vcpu, 4, "%s", "I/O instruction"); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 504 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 505 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 506 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 507 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 508 | if (vcpu->kvm->arch.css_support) { |
| 509 | /* |
| 510 | * Most I/O instructions will be handled by userspace. |
| 511 | * Exceptions are tpi and the interrupt portion of tsch. |
| 512 | */ |
| 513 | if (vcpu->arch.sie_block->ipa == 0xb236) |
| 514 | return handle_tpi(vcpu); |
| 515 | if (vcpu->arch.sie_block->ipa == 0xb235) |
| 516 | return handle_tsch(vcpu); |
| 517 | /* Handle in userspace. */ |
| 518 | return -EOPNOTSUPP; |
| 519 | } else { |
| 520 | /* |
Hendrik Brueckner | b4a9601 | 2013-12-13 12:53:42 +0100 | [diff] [blame] | 521 | * Set condition code 3 to stop the guest from issuing channel |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 522 | * I/O instructions. |
| 523 | */ |
Thomas Huth | ea828eb | 2013-07-26 15:04:06 +0200 | [diff] [blame] | 524 | kvm_s390_set_psw_cc(vcpu, 3); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 525 | return 0; |
| 526 | } |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 529 | static int handle_stfl(struct kvm_vcpu *vcpu) |
| 530 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 531 | int rc; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 532 | unsigned int fac; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 533 | |
| 534 | vcpu->stat.instruction_stfl++; |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 535 | |
| 536 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 537 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 538 | |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 539 | /* |
| 540 | * We need to shift the lower 32 facility bits (bit 0-31) from a u64 |
| 541 | * into a u32 memory representation. They will remain bits 0-31. |
| 542 | */ |
David Hildenbrand | c54f0d6 | 2015-12-02 08:53:52 +0100 | [diff] [blame] | 543 | fac = *vcpu->kvm->arch.model.fac_list >> 32; |
Heiko Carstens | c667aea | 2015-12-31 10:29:00 +0100 | [diff] [blame] | 544 | rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list), |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 545 | &fac, sizeof(fac)); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 546 | if (rc) |
Heiko Carstens | 0f9701c | 2014-01-01 16:56:41 +0100 | [diff] [blame] | 547 | return rc; |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 548 | VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac); |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 549 | trace_kvm_s390_handle_stfl(vcpu, fac); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 553 | #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA) |
| 554 | #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL |
Heiko Carstens | d21683e | 2013-03-25 17:22:49 +0100 | [diff] [blame] | 555 | #define PSW_ADDR_24 0x0000000000ffffffUL |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 556 | #define PSW_ADDR_31 0x000000007fffffffUL |
| 557 | |
Thomas Huth | a3fb577 | 2014-04-17 09:10:40 +0200 | [diff] [blame] | 558 | int is_valid_psw(psw_t *psw) |
| 559 | { |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 560 | if (psw->mask & PSW_MASK_UNASSIGNED) |
| 561 | return 0; |
| 562 | if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) { |
| 563 | if (psw->addr & ~PSW_ADDR_31) |
| 564 | return 0; |
| 565 | } |
| 566 | if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24)) |
| 567 | return 0; |
| 568 | if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA) |
| 569 | return 0; |
Thomas Huth | a3fb577 | 2014-04-17 09:10:40 +0200 | [diff] [blame] | 570 | if (psw->addr & 1) |
| 571 | return 0; |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 572 | return 1; |
| 573 | } |
| 574 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 575 | int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) |
| 576 | { |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 577 | psw_t *gpsw = &vcpu->arch.sie_block->gpsw; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 578 | psw_compat_t new_psw; |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 579 | u64 addr; |
Heiko Carstens | 2d8bcae | 2014-01-01 16:57:42 +0100 | [diff] [blame] | 580 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 581 | u8 ar; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 582 | |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 583 | if (gpsw->mask & PSW_MASK_PSTATE) |
Thomas Huth | 208dd75 | 2013-06-20 17:21:59 +0200 | [diff] [blame] | 584 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 585 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 586 | addr = kvm_s390_get_base_disp_s(vcpu, &ar); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 587 | if (addr & 7) |
| 588 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Heiko Carstens | 2d8bcae | 2014-01-01 16:57:42 +0100 | [diff] [blame] | 589 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 590 | rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw)); |
Heiko Carstens | 2d8bcae | 2014-01-01 16:57:42 +0100 | [diff] [blame] | 591 | if (rc) |
| 592 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 593 | if (!(new_psw.mask & PSW32_MASK_BASE)) |
| 594 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 595 | gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32; |
| 596 | gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE; |
| 597 | gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE; |
| 598 | if (!is_valid_psw(gpsw)) |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 599 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static int handle_lpswe(struct kvm_vcpu *vcpu) |
| 604 | { |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 605 | psw_t new_psw; |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 606 | u64 addr; |
Heiko Carstens | 2d8bcae | 2014-01-01 16:57:42 +0100 | [diff] [blame] | 607 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 608 | u8 ar; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 609 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 610 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 611 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 612 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 613 | addr = kvm_s390_get_base_disp_s(vcpu, &ar); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 614 | if (addr & 7) |
| 615 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 616 | rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw)); |
Heiko Carstens | 2d8bcae | 2014-01-01 16:57:42 +0100 | [diff] [blame] | 617 | if (rc) |
| 618 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame] | 619 | vcpu->arch.sie_block->gpsw = new_psw; |
| 620 | if (!is_valid_psw(&vcpu->arch.sie_block->gpsw)) |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 621 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 625 | static int handle_stidp(struct kvm_vcpu *vcpu) |
| 626 | { |
David Hildenbrand | 9bb0ec0 | 2016-04-04 14:27:51 +0200 | [diff] [blame] | 627 | u64 stidp_data = vcpu->kvm->arch.model.cpuid; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 628 | u64 operand2; |
Heiko Carstens | 7d777d7 | 2014-01-01 16:58:16 +0100 | [diff] [blame] | 629 | int rc; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 630 | u8 ar; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 631 | |
| 632 | vcpu->stat.instruction_stidp++; |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 633 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 634 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 635 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 636 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 637 | operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 638 | |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 639 | if (operand2 & 7) |
| 640 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 641 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 642 | rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data)); |
Heiko Carstens | 7d777d7 | 2014-01-01 16:58:16 +0100 | [diff] [blame] | 643 | if (rc) |
| 644 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 645 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 646 | VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem) |
| 651 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 652 | int cpus = 0; |
| 653 | int n; |
| 654 | |
Jens Freimann | ff520a6 | 2014-02-24 10:11:41 +0100 | [diff] [blame] | 655 | cpus = atomic_read(&vcpu->kvm->online_vcpus); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 656 | |
| 657 | /* deal with other level 3 hypervisors */ |
Heiko Carstens | caf757c | 2012-09-06 14:42:13 +0200 | [diff] [blame] | 658 | if (stsi(mem, 3, 2, 2)) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 659 | mem->count = 0; |
| 660 | if (mem->count < 8) |
| 661 | mem->count++; |
| 662 | for (n = mem->count - 1; n > 0 ; n--) |
| 663 | memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0])); |
| 664 | |
Ekaterina Tumanova | b75f4c9 | 2015-03-03 09:54:41 +0100 | [diff] [blame] | 665 | memset(&mem->vm[0], 0, sizeof(mem->vm[0])); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 666 | mem->vm[0].cpus_total = cpus; |
| 667 | mem->vm[0].cpus_configured = cpus; |
| 668 | mem->vm[0].cpus_standby = 0; |
| 669 | mem->vm[0].cpus_reserved = 0; |
| 670 | mem->vm[0].caf = 1000; |
| 671 | memcpy(mem->vm[0].name, "KVMguest", 8); |
| 672 | ASCEBC(mem->vm[0].name, 8); |
| 673 | memcpy(mem->vm[0].cpi, "KVM/Linux ", 16); |
| 674 | ASCEBC(mem->vm[0].cpi, 16); |
| 675 | } |
| 676 | |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 677 | static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar, |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 678 | u8 fc, u8 sel1, u16 sel2) |
| 679 | { |
| 680 | vcpu->run->exit_reason = KVM_EXIT_S390_STSI; |
| 681 | vcpu->run->s390_stsi.addr = addr; |
| 682 | vcpu->run->s390_stsi.ar = ar; |
| 683 | vcpu->run->s390_stsi.fc = fc; |
| 684 | vcpu->run->s390_stsi.sel1 = sel1; |
| 685 | vcpu->run->s390_stsi.sel2 = sel2; |
| 686 | } |
| 687 | |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 688 | static int handle_stsi(struct kvm_vcpu *vcpu) |
| 689 | { |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 690 | int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28; |
| 691 | int sel1 = vcpu->run->s.regs.gprs[0] & 0xff; |
| 692 | int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff; |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 693 | unsigned long mem = 0; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 694 | u64 operand2; |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 695 | int rc = 0; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 696 | u8 ar; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 697 | |
| 698 | vcpu->stat.instruction_stsi++; |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 699 | VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 700 | |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 701 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 702 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 703 | |
Thomas Huth | 87d41fb | 2013-06-20 17:22:05 +0200 | [diff] [blame] | 704 | if (fc > 3) { |
Thomas Huth | ea828eb | 2013-07-26 15:04:06 +0200 | [diff] [blame] | 705 | kvm_s390_set_psw_cc(vcpu, 3); |
Thomas Huth | 87d41fb | 2013-06-20 17:22:05 +0200 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | if (vcpu->run->s.regs.gprs[0] & 0x0fffff00 |
| 710 | || vcpu->run->s.regs.gprs[1] & 0xffff0000) |
| 711 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 712 | |
| 713 | if (fc == 0) { |
| 714 | vcpu->run->s.regs.gprs[0] = 3 << 28; |
Thomas Huth | ea828eb | 2013-07-26 15:04:06 +0200 | [diff] [blame] | 715 | kvm_s390_set_psw_cc(vcpu, 0); |
Thomas Huth | 87d41fb | 2013-06-20 17:22:05 +0200 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 719 | operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 720 | |
Thomas Huth | 87d41fb | 2013-06-20 17:22:05 +0200 | [diff] [blame] | 721 | if (operand2 & 0xfff) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 722 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 723 | |
| 724 | switch (fc) { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 725 | case 1: /* same handling for 1 and 2 */ |
| 726 | case 2: |
| 727 | mem = get_zeroed_page(GFP_KERNEL); |
| 728 | if (!mem) |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 729 | goto out_no_data; |
Heiko Carstens | caf757c | 2012-09-06 14:42:13 +0200 | [diff] [blame] | 730 | if (stsi((void *) mem, fc, sel1, sel2)) |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 731 | goto out_no_data; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 732 | break; |
| 733 | case 3: |
| 734 | if (sel1 != 2 || sel2 != 2) |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 735 | goto out_no_data; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 736 | mem = get_zeroed_page(GFP_KERNEL); |
| 737 | if (!mem) |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 738 | goto out_no_data; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 739 | handle_stsi_3_2_2(vcpu, (void *) mem); |
| 740 | break; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 743 | rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE); |
Heiko Carstens | 645c5bc | 2014-01-01 16:58:59 +0100 | [diff] [blame] | 744 | if (rc) { |
| 745 | rc = kvm_s390_inject_prog_cond(vcpu, rc); |
| 746 | goto out; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 747 | } |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 748 | if (vcpu->kvm->arch.user_stsi) { |
| 749 | insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2); |
| 750 | rc = -EREMOTE; |
| 751 | } |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 752 | trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 753 | free_page(mem); |
Thomas Huth | ea828eb | 2013-07-26 15:04:06 +0200 | [diff] [blame] | 754 | kvm_s390_set_psw_cc(vcpu, 0); |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 755 | vcpu->run->s.regs.gprs[0] = 0; |
Ekaterina Tumanova | e44fc8c | 2015-01-30 16:55:56 +0100 | [diff] [blame] | 756 | return rc; |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 757 | out_no_data: |
Thomas Huth | ea828eb | 2013-07-26 15:04:06 +0200 | [diff] [blame] | 758 | kvm_s390_set_psw_cc(vcpu, 3); |
Heiko Carstens | 645c5bc | 2014-01-01 16:58:59 +0100 | [diff] [blame] | 759 | out: |
Heiko Carstens | c51f068 | 2013-03-25 17:22:54 +0100 | [diff] [blame] | 760 | free_page(mem); |
Heiko Carstens | db4a29c | 2013-03-25 17:22:53 +0100 | [diff] [blame] | 761 | return rc; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 764 | static const intercept_handler_t b2_handlers[256] = { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 765 | [0x02] = handle_stidp, |
Thomas Huth | 6a3f95a | 2013-09-12 10:33:49 +0200 | [diff] [blame] | 766 | [0x04] = handle_set_clock, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 767 | [0x10] = handle_set_prefix, |
| 768 | [0x11] = handle_store_prefix, |
| 769 | [0x12] = handle_store_cpu_address, |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 770 | [0x14] = kvm_s390_handle_vsie, |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 771 | [0x21] = handle_ipte_interlock, |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 772 | [0x29] = handle_iske, |
| 773 | [0x2a] = handle_rrbe, |
| 774 | [0x2b] = handle_sske, |
Thomas Huth | aca8424 | 2013-09-12 10:33:48 +0200 | [diff] [blame] | 775 | [0x2c] = handle_test_block, |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 776 | [0x30] = handle_io_inst, |
| 777 | [0x31] = handle_io_inst, |
| 778 | [0x32] = handle_io_inst, |
| 779 | [0x33] = handle_io_inst, |
| 780 | [0x34] = handle_io_inst, |
| 781 | [0x35] = handle_io_inst, |
| 782 | [0x36] = handle_io_inst, |
| 783 | [0x37] = handle_io_inst, |
| 784 | [0x38] = handle_io_inst, |
| 785 | [0x39] = handle_io_inst, |
| 786 | [0x3a] = handle_io_inst, |
| 787 | [0x3b] = handle_io_inst, |
| 788 | [0x3c] = handle_io_inst, |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 789 | [0x50] = handle_ipte_interlock, |
Christian Borntraeger | c0a6bfd | 2017-02-27 21:14:47 +0100 | [diff] [blame] | 790 | [0x56] = handle_sthyi, |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 791 | [0x5f] = handle_io_inst, |
| 792 | [0x74] = handle_io_inst, |
| 793 | [0x76] = handle_io_inst, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 794 | [0x7d] = handle_stsi, |
| 795 | [0xb1] = handle_stfl, |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 796 | [0xb2] = handle_lpswe, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 797 | }; |
| 798 | |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 799 | int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 800 | { |
| 801 | intercept_handler_t handler; |
| 802 | |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 803 | /* |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 804 | * A lot of B2 instructions are priviledged. Here we check for |
| 805 | * the privileged ones, that we can handle in the kernel. |
| 806 | * Anything else goes to userspace. |
| 807 | */ |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 808 | handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 809 | if (handler) |
| 810 | return handler(vcpu); |
| 811 | |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 812 | return -EOPNOTSUPP; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 813 | } |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 814 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 815 | static int handle_epsw(struct kvm_vcpu *vcpu) |
| 816 | { |
| 817 | int reg1, reg2; |
| 818 | |
Thomas Huth | aeb87c3 | 2013-06-12 13:54:57 +0200 | [diff] [blame] | 819 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 820 | |
| 821 | /* This basically extracts the mask half of the psw. */ |
Thomas Huth | 843200e | 2013-07-26 15:04:05 +0200 | [diff] [blame] | 822 | vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 823 | vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32; |
| 824 | if (reg2) { |
Thomas Huth | 843200e | 2013-07-26 15:04:05 +0200 | [diff] [blame] | 825 | vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 826 | vcpu->run->s.regs.gprs[reg2] |= |
Thomas Huth | 843200e | 2013-07-26 15:04:05 +0200 | [diff] [blame] | 827 | vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 828 | } |
| 829 | return 0; |
| 830 | } |
| 831 | |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 832 | #define PFMF_RESERVED 0xfffc0101UL |
| 833 | #define PFMF_SK 0x00020000UL |
| 834 | #define PFMF_CF 0x00010000UL |
| 835 | #define PFMF_UI 0x00008000UL |
| 836 | #define PFMF_FSC 0x00007000UL |
| 837 | #define PFMF_NQ 0x00000800UL |
| 838 | #define PFMF_MR 0x00000400UL |
| 839 | #define PFMF_MC 0x00000200UL |
| 840 | #define PFMF_KEY 0x000000feUL |
| 841 | |
| 842 | static int handle_pfmf(struct kvm_vcpu *vcpu) |
| 843 | { |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 844 | bool mr = false, mc = false, nq; |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 845 | int reg1, reg2; |
| 846 | unsigned long start, end; |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 847 | unsigned char key; |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 848 | |
| 849 | vcpu->stat.instruction_pfmf++; |
| 850 | |
| 851 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
| 852 | |
Heiko Carstens | 03c0280 | 2015-11-13 13:31:58 +0100 | [diff] [blame] | 853 | if (!test_kvm_facility(vcpu->kvm, 8)) |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 854 | return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); |
| 855 | |
| 856 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
Thomas Huth | 208dd75 | 2013-06-20 17:21:59 +0200 | [diff] [blame] | 857 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 858 | |
| 859 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED) |
| 860 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 861 | |
David Hildenbrand | edc5b05 | 2016-03-04 11:08:09 +0100 | [diff] [blame] | 862 | /* Only provide non-quiescing support if enabled for the guest */ |
| 863 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && |
| 864 | !test_kvm_facility(vcpu->kvm, 14)) |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 865 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 866 | |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 867 | /* Only provide conditional-SSKE support if enabled for the guest */ |
| 868 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK && |
| 869 | test_kvm_facility(vcpu->kvm, 10)) { |
| 870 | mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR; |
| 871 | mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC; |
| 872 | } |
| 873 | |
| 874 | nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ; |
| 875 | key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY; |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 876 | start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; |
Thomas Huth | a02689f | 2014-11-10 15:59:32 +0100 | [diff] [blame] | 877 | start = kvm_s390_logical_to_effective(vcpu, start); |
Thomas Huth | fb34c603 | 2013-09-09 17:58:38 +0200 | [diff] [blame] | 878 | |
David Hildenbrand | 6164a2e | 2016-04-13 10:09:47 +0200 | [diff] [blame] | 879 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) { |
| 880 | if (kvm_s390_check_low_addr_prot_real(vcpu, start)) |
| 881 | return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); |
| 882 | } |
| 883 | |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 884 | switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) { |
| 885 | case 0x00000000: |
David Hildenbrand | 6164a2e | 2016-04-13 10:09:47 +0200 | [diff] [blame] | 886 | /* only 4k frames specify a real address */ |
| 887 | start = kvm_s390_real_to_abs(vcpu, start); |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 888 | end = (start + (1UL << 12)) & ~((1UL << 12) - 1); |
| 889 | break; |
| 890 | case 0x00001000: |
| 891 | end = (start + (1UL << 20)) & ~((1UL << 20) - 1); |
| 892 | break; |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 893 | case 0x00002000: |
Guenther Hutzl | 53df84f | 2015-02-18 11:13:03 +0100 | [diff] [blame] | 894 | /* only support 2G frame size if EDAT2 is available and we are |
| 895 | not in 24-bit addressing mode */ |
| 896 | if (!test_kvm_facility(vcpu->kvm, 78) || |
| 897 | psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT) |
| 898 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 899 | end = (start + (1UL << 31)) & ~((1UL << 31) - 1); |
Guenther Hutzl | 53df84f | 2015-02-18 11:13:03 +0100 | [diff] [blame] | 900 | break; |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 901 | default: |
| 902 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 903 | } |
Thomas Huth | a02689f | 2014-11-10 15:59:32 +0100 | [diff] [blame] | 904 | |
David Hildenbrand | 695be0e | 2016-05-12 14:07:05 +0200 | [diff] [blame] | 905 | while (start != end) { |
David Hildenbrand | 6164a2e | 2016-04-13 10:09:47 +0200 | [diff] [blame] | 906 | unsigned long useraddr; |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 907 | |
Thomas Huth | fb34c603 | 2013-09-09 17:58:38 +0200 | [diff] [blame] | 908 | /* Translate guest address to host address */ |
David Hildenbrand | 6164a2e | 2016-04-13 10:09:47 +0200 | [diff] [blame] | 909 | useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start)); |
Thomas Huth | fb34c603 | 2013-09-09 17:58:38 +0200 | [diff] [blame] | 910 | if (kvm_is_error_hva(useraddr)) |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 911 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 912 | |
| 913 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) { |
| 914 | if (clear_user((void __user *)useraddr, PAGE_SIZE)) |
| 915 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 916 | } |
| 917 | |
| 918 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) { |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 919 | int rc = __skey_check_enable(vcpu); |
| 920 | |
| 921 | if (rc) |
| 922 | return rc; |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 923 | down_read(¤t->mm->mmap_sem); |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 924 | rc = cond_set_guest_storage_key(current->mm, useraddr, |
| 925 | key, NULL, nq, mr, mc); |
Martin Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 926 | up_read(¤t->mm->mmap_sem); |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 927 | if (rc < 0) |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 928 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 929 | } |
| 930 | |
| 931 | start += PAGE_SIZE; |
| 932 | } |
David Hildenbrand | 2c26d1d | 2016-04-13 15:47:21 +0200 | [diff] [blame] | 933 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) { |
| 934 | if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) { |
| 935 | vcpu->run->s.regs.gprs[reg2] = end; |
| 936 | } else { |
| 937 | vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL; |
| 938 | end = kvm_s390_logical_to_effective(vcpu, end); |
| 939 | vcpu->run->s.regs.gprs[reg2] |= end; |
| 940 | } |
| 941 | } |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 945 | static int handle_essa(struct kvm_vcpu *vcpu) |
| 946 | { |
| 947 | /* entries expected to be 1FF */ |
| 948 | int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3; |
David Hildenbrand | 4a5e7e3 | 2016-04-12 13:32:25 +0200 | [diff] [blame] | 949 | unsigned long *cbrlo; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 950 | struct gmap *gmap; |
| 951 | int i; |
| 952 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 953 | VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 954 | gmap = vcpu->arch.gmap; |
| 955 | vcpu->stat.instruction_essa++; |
Dominik Dingel | e6db1d6 | 2015-05-07 15:41:57 +0200 | [diff] [blame] | 956 | if (!vcpu->kvm->arch.use_cmma) |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 957 | return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); |
| 958 | |
| 959 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 960 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 961 | |
| 962 | if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6) |
| 963 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 964 | |
David Hildenbrand | 0e8bc06 | 2015-11-04 13:47:58 +0100 | [diff] [blame] | 965 | /* Retry the ESSA instruction */ |
| 966 | kvm_s390_retry_instr(vcpu); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 967 | vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */ |
| 968 | cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo); |
| 969 | down_read(&gmap->mm->mmap_sem); |
David Hildenbrand | 4a5e7e3 | 2016-04-12 13:32:25 +0200 | [diff] [blame] | 970 | for (i = 0; i < entries; ++i) |
| 971 | __gmap_zap(gmap, cbrlo[i]); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 972 | up_read(&gmap->mm->mmap_sem); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 976 | static const intercept_handler_t b9_handlers[256] = { |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 977 | [0x8a] = handle_ipte_interlock, |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 978 | [0x8d] = handle_epsw, |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 979 | [0x8e] = handle_ipte_interlock, |
| 980 | [0x8f] = handle_ipte_interlock, |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 981 | [0xab] = handle_essa, |
Christian Borntraeger | 69d0d3a | 2013-06-12 13:54:53 +0200 | [diff] [blame] | 982 | [0xaf] = handle_pfmf, |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 983 | }; |
| 984 | |
| 985 | int kvm_s390_handle_b9(struct kvm_vcpu *vcpu) |
| 986 | { |
| 987 | intercept_handler_t handler; |
| 988 | |
| 989 | /* This is handled just as for the B2 instructions. */ |
| 990 | handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
Thomas Huth | 5087dfa | 2013-06-20 17:22:01 +0200 | [diff] [blame] | 991 | if (handler) |
| 992 | return handler(vcpu); |
| 993 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 994 | return -EOPNOTSUPP; |
| 995 | } |
| 996 | |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 997 | int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu) |
| 998 | { |
| 999 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; |
| 1000 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1001 | int reg, rc, nr_regs; |
| 1002 | u32 ctl_array[16]; |
Heiko Carstens | f987a3e | 2014-01-01 16:59:21 +0100 | [diff] [blame] | 1003 | u64 ga; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 1004 | u8 ar; |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1005 | |
| 1006 | vcpu->stat.instruction_lctl++; |
| 1007 | |
| 1008 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 1009 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 1010 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1011 | ga = kvm_s390_get_base_disp_rs(vcpu, &ar); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1012 | |
Heiko Carstens | f987a3e | 2014-01-01 16:59:21 +0100 | [diff] [blame] | 1013 | if (ga & 3) |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1014 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 1015 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 1016 | VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); |
Heiko Carstens | f987a3e | 2014-01-01 16:59:21 +0100 | [diff] [blame] | 1017 | trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1018 | |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1019 | nr_regs = ((reg3 - reg1) & 0xf) + 1; |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1020 | rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32)); |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1021 | if (rc) |
| 1022 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1023 | reg = reg1; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1024 | nr_regs = 0; |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1025 | do { |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1026 | vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1027 | vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++]; |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1028 | if (reg == reg3) |
| 1029 | break; |
| 1030 | reg = (reg + 1) % 16; |
| 1031 | } while (1); |
Christian Borntraeger | 2dca485 | 2014-10-31 09:24:20 +0100 | [diff] [blame] | 1032 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1036 | int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu) |
| 1037 | { |
| 1038 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; |
| 1039 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1040 | int reg, rc, nr_regs; |
| 1041 | u32 ctl_array[16]; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1042 | u64 ga; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 1043 | u8 ar; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1044 | |
| 1045 | vcpu->stat.instruction_stctl++; |
| 1046 | |
| 1047 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 1048 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 1049 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1050 | ga = kvm_s390_get_base_disp_rs(vcpu, &ar); |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1051 | |
| 1052 | if (ga & 3) |
| 1053 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 1054 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 1055 | VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1056 | trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga); |
| 1057 | |
| 1058 | reg = reg1; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1059 | nr_regs = 0; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1060 | do { |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1061 | ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg]; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1062 | if (reg == reg3) |
| 1063 | break; |
| 1064 | reg = (reg + 1) % 16; |
| 1065 | } while (1); |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1066 | rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32)); |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1067 | return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1070 | static int handle_lctlg(struct kvm_vcpu *vcpu) |
| 1071 | { |
| 1072 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; |
| 1073 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1074 | int reg, rc, nr_regs; |
| 1075 | u64 ctl_array[16]; |
| 1076 | u64 ga; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 1077 | u8 ar; |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1078 | |
| 1079 | vcpu->stat.instruction_lctlg++; |
| 1080 | |
| 1081 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 1082 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 1083 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1084 | ga = kvm_s390_get_base_disp_rsy(vcpu, &ar); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1085 | |
Heiko Carstens | f987a3e | 2014-01-01 16:59:21 +0100 | [diff] [blame] | 1086 | if (ga & 7) |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1087 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 1088 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 1089 | VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); |
Heiko Carstens | f987a3e | 2014-01-01 16:59:21 +0100 | [diff] [blame] | 1090 | trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1091 | |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1092 | nr_regs = ((reg3 - reg1) & 0xf) + 1; |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1093 | rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64)); |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1094 | if (rc) |
| 1095 | return kvm_s390_inject_prog_cond(vcpu, rc); |
| 1096 | reg = reg1; |
| 1097 | nr_regs = 0; |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1098 | do { |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1099 | vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++]; |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1100 | if (reg == reg3) |
| 1101 | break; |
| 1102 | reg = (reg + 1) % 16; |
| 1103 | } while (1); |
Christian Borntraeger | 2dca485 | 2014-10-31 09:24:20 +0100 | [diff] [blame] | 1104 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1105 | return 0; |
| 1106 | } |
| 1107 | |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1108 | static int handle_stctg(struct kvm_vcpu *vcpu) |
| 1109 | { |
| 1110 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; |
| 1111 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1112 | int reg, rc, nr_regs; |
| 1113 | u64 ctl_array[16]; |
| 1114 | u64 ga; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 1115 | u8 ar; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1116 | |
| 1117 | vcpu->stat.instruction_stctg++; |
| 1118 | |
| 1119 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 1120 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 1121 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1122 | ga = kvm_s390_get_base_disp_rsy(vcpu, &ar); |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1123 | |
| 1124 | if (ga & 7) |
| 1125 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 1126 | |
Christian Borntraeger | 7cbde76 | 2015-07-21 12:44:57 +0200 | [diff] [blame] | 1127 | VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1128 | trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga); |
| 1129 | |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1130 | reg = reg1; |
| 1131 | nr_regs = 0; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1132 | do { |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1133 | ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg]; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1134 | if (reg == reg3) |
| 1135 | break; |
| 1136 | reg = (reg + 1) % 16; |
| 1137 | } while (1); |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1138 | rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64)); |
Heiko Carstens | fc56eb6 | 2014-10-29 10:07:16 +0100 | [diff] [blame] | 1139 | return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0; |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 1142 | static const intercept_handler_t eb_handlers[256] = { |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1143 | [0x2f] = handle_lctlg, |
David Hildenbrand | aba0750 | 2014-01-23 10:47:13 +0100 | [diff] [blame] | 1144 | [0x25] = handle_stctg, |
Fan Zhang | 80cd876 | 2016-08-15 04:53:22 +0200 | [diff] [blame] | 1145 | [0x60] = handle_ri, |
| 1146 | [0x61] = handle_ri, |
| 1147 | [0x62] = handle_ri, |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 1148 | }; |
| 1149 | |
Thomas Huth | 953ed88 | 2013-06-20 17:22:04 +0200 | [diff] [blame] | 1150 | int kvm_s390_handle_eb(struct kvm_vcpu *vcpu) |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 1151 | { |
| 1152 | intercept_handler_t handler; |
| 1153 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 1154 | handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff]; |
| 1155 | if (handler) |
| 1156 | return handler(vcpu); |
| 1157 | return -EOPNOTSUPP; |
| 1158 | } |
| 1159 | |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 1160 | static int handle_tprot(struct kvm_vcpu *vcpu) |
| 1161 | { |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 1162 | u64 address1, address2; |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 1163 | unsigned long hva, gpa; |
| 1164 | int ret = 0, cc = 0; |
| 1165 | bool writable; |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 1166 | u8 ar; |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 1167 | |
| 1168 | vcpu->stat.instruction_tprot++; |
| 1169 | |
Thomas Huth | f9f6bbc | 2013-06-20 17:22:00 +0200 | [diff] [blame] | 1170 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 1171 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 1172 | |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 1173 | kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL); |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 1174 | |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 1175 | /* we only handle the Linux memory detection case: |
| 1176 | * access key == 0 |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 1177 | * everything else goes to userspace. */ |
| 1178 | if (address2 & 0xf0) |
| 1179 | return -EOPNOTSUPP; |
| 1180 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 1181 | ipte_lock(vcpu); |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 1182 | ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE); |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 1183 | if (ret == PGM_PROTECTION) { |
| 1184 | /* Write protected? Try again with read-only... */ |
| 1185 | cc = 1; |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 1186 | ret = guest_translate_address(vcpu, address1, ar, &gpa, |
| 1187 | GACC_FETCH); |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 1188 | } |
| 1189 | if (ret) { |
| 1190 | if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) { |
| 1191 | ret = kvm_s390_inject_program_int(vcpu, ret); |
| 1192 | } else if (ret > 0) { |
| 1193 | /* Translation not available */ |
| 1194 | kvm_s390_set_psw_cc(vcpu, 3); |
| 1195 | ret = 0; |
| 1196 | } |
| 1197 | goto out_unlock; |
| 1198 | } |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 1199 | |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 1200 | hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable); |
| 1201 | if (kvm_is_error_hva(hva)) { |
| 1202 | ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 1203 | } else { |
| 1204 | if (!writable) |
| 1205 | cc = 1; /* Write not permitted ==> read-only */ |
| 1206 | kvm_s390_set_psw_cc(vcpu, cc); |
| 1207 | /* Note: CC2 only occurs for storage keys (not supported yet) */ |
| 1208 | } |
| 1209 | out_unlock: |
| 1210 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) |
| 1211 | ipte_unlock(vcpu); |
| 1212 | return ret; |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | int kvm_s390_handle_e5(struct kvm_vcpu *vcpu) |
| 1216 | { |
| 1217 | /* For e5xx... instructions we only handle TPROT */ |
| 1218 | if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01) |
| 1219 | return handle_tprot(vcpu); |
| 1220 | return -EOPNOTSUPP; |
| 1221 | } |
| 1222 | |
Cornelia Huck | 8c3f61e | 2012-04-24 09:24:44 +0200 | [diff] [blame] | 1223 | static int handle_sckpf(struct kvm_vcpu *vcpu) |
| 1224 | { |
| 1225 | u32 value; |
| 1226 | |
| 1227 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
Thomas Huth | 208dd75 | 2013-06-20 17:21:59 +0200 | [diff] [blame] | 1228 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
Cornelia Huck | 8c3f61e | 2012-04-24 09:24:44 +0200 | [diff] [blame] | 1229 | |
| 1230 | if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000) |
| 1231 | return kvm_s390_inject_program_int(vcpu, |
| 1232 | PGM_SPECIFICATION); |
| 1233 | |
| 1234 | value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff; |
| 1235 | vcpu->arch.sie_block->todpr = value; |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
David Hildenbrand | 9acc317 | 2016-07-18 09:18:13 +0200 | [diff] [blame] | 1240 | static int handle_ptff(struct kvm_vcpu *vcpu) |
| 1241 | { |
| 1242 | /* we don't emulate any control instructions yet */ |
| 1243 | kvm_s390_set_psw_cc(vcpu, 3); |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Cornelia Huck | 7797535 | 2012-12-20 15:32:06 +0100 | [diff] [blame] | 1247 | static const intercept_handler_t x01_handlers[256] = { |
David Hildenbrand | 9acc317 | 2016-07-18 09:18:13 +0200 | [diff] [blame] | 1248 | [0x04] = handle_ptff, |
Cornelia Huck | 8c3f61e | 2012-04-24 09:24:44 +0200 | [diff] [blame] | 1249 | [0x07] = handle_sckpf, |
| 1250 | }; |
| 1251 | |
| 1252 | int kvm_s390_handle_01(struct kvm_vcpu *vcpu) |
| 1253 | { |
| 1254 | intercept_handler_t handler; |
| 1255 | |
| 1256 | handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
| 1257 | if (handler) |
| 1258 | return handler(vcpu); |
| 1259 | return -EOPNOTSUPP; |
| 1260 | } |