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