Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * in-kernel handling for sie intercepts |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2008, 2009 |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +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_host.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | |
| 18 | #include <asm/kvm_host.h> |
| 19 | |
| 20 | #include "kvm-s390.h" |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 21 | #include "gaccess.h" |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 22 | #include "trace.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 23 | #include "trace-s390.h" |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 24 | |
Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 25 | static int handle_lctlg(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 26 | { |
| 27 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; |
| 28 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 29 | u64 useraddr; |
| 30 | int reg, rc; |
| 31 | |
Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 32 | vcpu->stat.instruction_lctlg++; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 33 | if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f) |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 34 | return -EOPNOTSUPP; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 35 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame^] | 36 | useraddr = kvm_s390_get_base_disp_rsy(vcpu); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 37 | |
Christian Borntraeger | 5a00a5e | 2008-07-25 15:53:12 +0200 | [diff] [blame] | 38 | if (useraddr & 7) |
| 39 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 40 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 41 | reg = reg1; |
| 42 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame^] | 43 | VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, |
| 44 | useraddr); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 45 | trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 46 | |
| 47 | do { |
| 48 | rc = get_guest_u64(vcpu, useraddr, |
| 49 | &vcpu->arch.sie_block->gcr[reg]); |
| 50 | if (rc == -EFAULT) { |
| 51 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 52 | break; |
| 53 | } |
| 54 | useraddr += 8; |
| 55 | if (reg == reg3) |
| 56 | break; |
| 57 | reg = (reg + 1) % 16; |
| 58 | } while (1); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int handle_lctl(struct kvm_vcpu *vcpu) |
| 63 | { |
| 64 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; |
| 65 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 66 | u64 useraddr; |
| 67 | u32 val = 0; |
| 68 | int reg, rc; |
| 69 | |
| 70 | vcpu->stat.instruction_lctl++; |
| 71 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame^] | 72 | useraddr = kvm_s390_get_base_disp_rs(vcpu); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 73 | |
Christian Borntraeger | 5a00a5e | 2008-07-25 15:53:12 +0200 | [diff] [blame] | 74 | if (useraddr & 3) |
| 75 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 76 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame^] | 77 | VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, |
| 78 | useraddr); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 79 | trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 80 | |
| 81 | reg = reg1; |
| 82 | do { |
| 83 | rc = get_guest_u32(vcpu, useraddr, &val); |
| 84 | if (rc == -EFAULT) { |
| 85 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 86 | break; |
| 87 | } |
| 88 | vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; |
| 89 | vcpu->arch.sie_block->gcr[reg] |= val; |
| 90 | useraddr += 4; |
| 91 | if (reg == reg3) |
| 92 | break; |
| 93 | reg = (reg + 1) % 16; |
| 94 | } while (1); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Cornelia Huck | 7797535 | 2012-12-20 15:32:06 +0100 | [diff] [blame] | 98 | static const intercept_handler_t instruction_handlers[256] = { |
Cornelia Huck | 8c3f61e | 2012-04-24 09:24:44 +0200 | [diff] [blame] | 99 | [0x01] = kvm_s390_handle_01, |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 100 | [0x83] = kvm_s390_handle_diag, |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 101 | [0xae] = kvm_s390_handle_sigp, |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 102 | [0xb2] = kvm_s390_handle_b2, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 103 | [0xb7] = handle_lctl, |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 104 | [0xe5] = kvm_s390_handle_e5, |
Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 105 | [0xeb] = handle_lctlg, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 106 | }; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 107 | |
| 108 | static int handle_noop(struct kvm_vcpu *vcpu) |
| 109 | { |
| 110 | switch (vcpu->arch.sie_block->icptcode) { |
Christian Borntraeger | 0eaeafa | 2008-05-07 09:22:53 +0200 | [diff] [blame] | 111 | case 0x0: |
| 112 | vcpu->stat.exit_null++; |
| 113 | break; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 114 | case 0x10: |
| 115 | vcpu->stat.exit_external_request++; |
| 116 | break; |
| 117 | case 0x14: |
| 118 | vcpu->stat.exit_external_interrupt++; |
| 119 | break; |
| 120 | default: |
| 121 | break; /* nothing */ |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int handle_stop(struct kvm_vcpu *vcpu) |
| 127 | { |
Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 128 | int rc = 0; |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 129 | |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 130 | vcpu->stat.exit_stop_request++; |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 131 | spin_lock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 132 | |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 133 | trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits); |
| 134 | |
Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 135 | if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) { |
| 136 | vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP; |
| 137 | rc = SIE_INTERCEPT_RERUNVCPU; |
| 138 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
| 139 | } |
| 140 | |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 141 | if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) { |
Cornelia Huck | 9e6dabe | 2011-11-17 11:00:41 +0100 | [diff] [blame] | 142 | atomic_set_mask(CPUSTAT_STOPPED, |
| 143 | &vcpu->arch.sie_block->cpuflags); |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 144 | vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP; |
| 145 | VCPU_EVENT(vcpu, 3, "%s", "cpu stopped"); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 146 | rc = -EOPNOTSUPP; |
Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Jens Freimann | 9e0d547 | 2012-02-06 10:59:03 +0100 | [diff] [blame] | 149 | if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) { |
| 150 | vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP; |
| 151 | /* store status must be called unlocked. Since local_int.lock |
| 152 | * only protects local_int.* and not guest memory we can give |
| 153 | * up the lock here */ |
| 154 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
| 155 | rc = kvm_s390_vcpu_store_status(vcpu, |
| 156 | KVM_S390_STORE_STATUS_NOADDR); |
| 157 | if (rc >= 0) |
| 158 | rc = -EOPNOTSUPP; |
| 159 | } else |
| 160 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 161 | return rc; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static int handle_validity(struct kvm_vcpu *vcpu) |
| 165 | { |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 166 | unsigned long vmaddr; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 167 | int viwhy = vcpu->arch.sie_block->ipb >> 16; |
Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 168 | int rc; |
| 169 | |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 170 | vcpu->stat.exit_validity++; |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 171 | trace_kvm_s390_intercept_validity(vcpu, viwhy); |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 172 | if (viwhy == 0x37) { |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 173 | vmaddr = gmap_fault(vcpu->arch.sie_block->prefix, |
| 174 | vcpu->arch.gmap); |
| 175 | if (IS_ERR_VALUE(vmaddr)) { |
| 176 | rc = -EOPNOTSUPP; |
| 177 | goto out; |
| 178 | } |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 179 | rc = fault_in_pages_writeable((char __user *) vmaddr, |
| 180 | PAGE_SIZE); |
| 181 | if (rc) { |
| 182 | /* user will receive sigsegv, exit to user */ |
| 183 | rc = -EOPNOTSUPP; |
| 184 | goto out; |
| 185 | } |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 186 | vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE, |
| 187 | vcpu->arch.gmap); |
| 188 | if (IS_ERR_VALUE(vmaddr)) { |
| 189 | rc = -EOPNOTSUPP; |
| 190 | goto out; |
| 191 | } |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 192 | rc = fault_in_pages_writeable((char __user *) vmaddr, |
| 193 | PAGE_SIZE); |
| 194 | if (rc) { |
| 195 | /* user will receive sigsegv, exit to user */ |
| 196 | rc = -EOPNOTSUPP; |
| 197 | goto out; |
| 198 | } |
Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 199 | } else |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 200 | rc = -EOPNOTSUPP; |
Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 201 | |
Carsten Otte | 598841c | 2011-07-24 10:48:21 +0200 | [diff] [blame] | 202 | out: |
Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 203 | if (rc) |
| 204 | VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d", |
| 205 | viwhy); |
| 206 | return rc; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 209 | static int handle_instruction(struct kvm_vcpu *vcpu) |
| 210 | { |
| 211 | intercept_handler_t handler; |
| 212 | |
| 213 | vcpu->stat.exit_instruction++; |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 214 | trace_kvm_s390_intercept_instruction(vcpu, |
| 215 | vcpu->arch.sie_block->ipa, |
| 216 | vcpu->arch.sie_block->ipb); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 217 | handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8]; |
| 218 | if (handler) |
| 219 | return handler(vcpu); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 220 | return -EOPNOTSUPP; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static int handle_prog(struct kvm_vcpu *vcpu) |
| 224 | { |
| 225 | vcpu->stat.exit_program_interruption++; |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 226 | trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 227 | return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc); |
| 228 | } |
| 229 | |
| 230 | static int handle_instruction_and_prog(struct kvm_vcpu *vcpu) |
| 231 | { |
| 232 | int rc, rc2; |
| 233 | |
| 234 | vcpu->stat.exit_instr_and_program++; |
| 235 | rc = handle_instruction(vcpu); |
| 236 | rc2 = handle_prog(vcpu); |
| 237 | |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 238 | if (rc == -EOPNOTSUPP) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 239 | vcpu->arch.sie_block->icptcode = 0x04; |
| 240 | if (rc) |
| 241 | return rc; |
| 242 | return rc2; |
| 243 | } |
| 244 | |
Christian Borntraeger | 062d5e9 | 2010-01-21 12:19:07 +0100 | [diff] [blame] | 245 | static const intercept_handler_t intercept_funcs[] = { |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 246 | [0x00 >> 2] = handle_noop, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 247 | [0x04 >> 2] = handle_instruction, |
| 248 | [0x08 >> 2] = handle_prog, |
| 249 | [0x0C >> 2] = handle_instruction_and_prog, |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 250 | [0x10 >> 2] = handle_noop, |
| 251 | [0x14 >> 2] = handle_noop, |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 252 | [0x1C >> 2] = kvm_s390_handle_wait, |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 253 | [0x20 >> 2] = handle_validity, |
| 254 | [0x28 >> 2] = handle_stop, |
| 255 | }; |
| 256 | |
| 257 | int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu) |
| 258 | { |
| 259 | intercept_handler_t func; |
| 260 | u8 code = vcpu->arch.sie_block->icptcode; |
| 261 | |
Christian Borntraeger | 062d5e9 | 2010-01-21 12:19:07 +0100 | [diff] [blame] | 262 | if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs)) |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 263 | return -EOPNOTSUPP; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 264 | func = intercept_funcs[code >> 2]; |
| 265 | if (func) |
| 266 | return func(vcpu); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 267 | return -EOPNOTSUPP; |
Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 268 | } |