diff options
Diffstat (limited to 'arch/s390/kvm/intercept.c')
| -rw-r--r-- | arch/s390/kvm/intercept.c | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/arch/s390/kvm/intercept.c b/arch/s390/kvm/intercept.c new file mode 100644 index 000000000000..349581a26103 --- /dev/null +++ b/arch/s390/kvm/intercept.c | |||
| @@ -0,0 +1,216 @@ | |||
| 1 | /* | ||
| 2 | * intercept.c - in-kernel handling for sie intercepts | ||
| 3 | * | ||
| 4 | * Copyright IBM Corp. 2008 | ||
| 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" | ||
| 21 | #include "gaccess.h" | ||
| 22 | |||
| 23 | static int handle_lctg(struct kvm_vcpu *vcpu) | ||
| 24 | { | ||
| 25 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | ||
| 26 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | ||
| 27 | int base2 = vcpu->arch.sie_block->ipb >> 28; | ||
| 28 | int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) + | ||
| 29 | ((vcpu->arch.sie_block->ipb & 0xff00) << 4); | ||
| 30 | u64 useraddr; | ||
| 31 | int reg, rc; | ||
| 32 | |||
| 33 | vcpu->stat.instruction_lctg++; | ||
| 34 | if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f) | ||
| 35 | return -ENOTSUPP; | ||
| 36 | |||
| 37 | useraddr = disp2; | ||
| 38 | if (base2) | ||
| 39 | useraddr += vcpu->arch.guest_gprs[base2]; | ||
| 40 | |||
| 41 | reg = reg1; | ||
| 42 | |||
| 43 | VCPU_EVENT(vcpu, 5, "lctg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2, | ||
| 44 | disp2); | ||
| 45 | |||
| 46 | do { | ||
| 47 | rc = get_guest_u64(vcpu, useraddr, | ||
| 48 | &vcpu->arch.sie_block->gcr[reg]); | ||
| 49 | if (rc == -EFAULT) { | ||
| 50 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | useraddr += 8; | ||
| 54 | if (reg == reg3) | ||
| 55 | break; | ||
| 56 | reg = (reg + 1) % 16; | ||
| 57 | } while (1); | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | static int handle_lctl(struct kvm_vcpu *vcpu) | ||
| 62 | { | ||
| 63 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | ||
| 64 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | ||
| 65 | int base2 = vcpu->arch.sie_block->ipb >> 28; | ||
| 66 | int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16); | ||
| 67 | u64 useraddr; | ||
| 68 | u32 val = 0; | ||
| 69 | int reg, rc; | ||
| 70 | |||
| 71 | vcpu->stat.instruction_lctl++; | ||
| 72 | |||
| 73 | useraddr = disp2; | ||
| 74 | if (base2) | ||
| 75 | useraddr += vcpu->arch.guest_gprs[base2]; | ||
| 76 | |||
| 77 | VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2, | ||
| 78 | disp2); | ||
| 79 | |||
| 80 | reg = reg1; | ||
| 81 | do { | ||
| 82 | rc = get_guest_u32(vcpu, useraddr, &val); | ||
| 83 | if (rc == -EFAULT) { | ||
| 84 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | ||
| 85 | break; | ||
| 86 | } | ||
| 87 | vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; | ||
| 88 | vcpu->arch.sie_block->gcr[reg] |= val; | ||
| 89 | useraddr += 4; | ||
| 90 | if (reg == reg3) | ||
| 91 | break; | ||
| 92 | reg = (reg + 1) % 16; | ||
| 93 | } while (1); | ||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | static intercept_handler_t instruction_handlers[256] = { | ||
| 98 | [0x83] = kvm_s390_handle_diag, | ||
| 99 | [0xae] = kvm_s390_handle_sigp, | ||
| 100 | [0xb2] = kvm_s390_handle_priv, | ||
| 101 | [0xb7] = handle_lctl, | ||
| 102 | [0xeb] = handle_lctg, | ||
| 103 | }; | ||
| 104 | |||
| 105 | static int handle_noop(struct kvm_vcpu *vcpu) | ||
| 106 | { | ||
| 107 | switch (vcpu->arch.sie_block->icptcode) { | ||
| 108 | case 0x10: | ||
| 109 | vcpu->stat.exit_external_request++; | ||
| 110 | break; | ||
| 111 | case 0x14: | ||
| 112 | vcpu->stat.exit_external_interrupt++; | ||
| 113 | break; | ||
| 114 | default: | ||
| 115 | break; /* nothing */ | ||
| 116 | } | ||
| 117 | return 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | static int handle_stop(struct kvm_vcpu *vcpu) | ||
| 121 | { | ||
| 122 | int rc; | ||
| 123 | |||
| 124 | vcpu->stat.exit_stop_request++; | ||
| 125 | atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); | ||
| 126 | spin_lock_bh(&vcpu->arch.local_int.lock); | ||
| 127 | if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) { | ||
| 128 | vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP; | ||
| 129 | rc = __kvm_s390_vcpu_store_status(vcpu, | ||
| 130 | KVM_S390_STORE_STATUS_NOADDR); | ||
| 131 | if (rc >= 0) | ||
| 132 | rc = -ENOTSUPP; | ||
| 133 | } | ||
| 134 | |||
| 135 | if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) { | ||
| 136 | vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP; | ||
| 137 | VCPU_EVENT(vcpu, 3, "%s", "cpu stopped"); | ||
| 138 | rc = -ENOTSUPP; | ||
| 139 | } else | ||
| 140 | rc = 0; | ||
| 141 | spin_unlock_bh(&vcpu->arch.local_int.lock); | ||
| 142 | return rc; | ||
| 143 | } | ||
| 144 | |||
| 145 | static int handle_validity(struct kvm_vcpu *vcpu) | ||
| 146 | { | ||
| 147 | int viwhy = vcpu->arch.sie_block->ipb >> 16; | ||
| 148 | vcpu->stat.exit_validity++; | ||
| 149 | if (viwhy == 0x37) { | ||
| 150 | fault_in_pages_writeable((char __user *) | ||
| 151 | vcpu->kvm->arch.guest_origin + | ||
| 152 | vcpu->arch.sie_block->prefix, | ||
| 153 | PAGE_SIZE); | ||
| 154 | return 0; | ||
| 155 | } | ||
| 156 | VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d", | ||
| 157 | viwhy); | ||
| 158 | return -ENOTSUPP; | ||
| 159 | } | ||
| 160 | |||
| 161 | static int handle_instruction(struct kvm_vcpu *vcpu) | ||
| 162 | { | ||
| 163 | intercept_handler_t handler; | ||
| 164 | |||
| 165 | vcpu->stat.exit_instruction++; | ||
| 166 | handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8]; | ||
| 167 | if (handler) | ||
| 168 | return handler(vcpu); | ||
| 169 | return -ENOTSUPP; | ||
| 170 | } | ||
| 171 | |||
| 172 | static int handle_prog(struct kvm_vcpu *vcpu) | ||
| 173 | { | ||
| 174 | vcpu->stat.exit_program_interruption++; | ||
| 175 | return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc); | ||
| 176 | } | ||
| 177 | |||
| 178 | static int handle_instruction_and_prog(struct kvm_vcpu *vcpu) | ||
| 179 | { | ||
| 180 | int rc, rc2; | ||
| 181 | |||
| 182 | vcpu->stat.exit_instr_and_program++; | ||
| 183 | rc = handle_instruction(vcpu); | ||
| 184 | rc2 = handle_prog(vcpu); | ||
| 185 | |||
| 186 | if (rc == -ENOTSUPP) | ||
| 187 | vcpu->arch.sie_block->icptcode = 0x04; | ||
| 188 | if (rc) | ||
| 189 | return rc; | ||
| 190 | return rc2; | ||
| 191 | } | ||
| 192 | |||
| 193 | static const intercept_handler_t intercept_funcs[0x48 >> 2] = { | ||
| 194 | [0x00 >> 2] = handle_noop, | ||
| 195 | [0x04 >> 2] = handle_instruction, | ||
| 196 | [0x08 >> 2] = handle_prog, | ||
| 197 | [0x0C >> 2] = handle_instruction_and_prog, | ||
| 198 | [0x10 >> 2] = handle_noop, | ||
| 199 | [0x14 >> 2] = handle_noop, | ||
| 200 | [0x1C >> 2] = kvm_s390_handle_wait, | ||
| 201 | [0x20 >> 2] = handle_validity, | ||
| 202 | [0x28 >> 2] = handle_stop, | ||
| 203 | }; | ||
| 204 | |||
| 205 | int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu) | ||
| 206 | { | ||
| 207 | intercept_handler_t func; | ||
| 208 | u8 code = vcpu->arch.sie_block->icptcode; | ||
| 209 | |||
| 210 | if (code & 3 || code > 0x48) | ||
| 211 | return -ENOTSUPP; | ||
| 212 | func = intercept_funcs[code >> 2]; | ||
| 213 | if (func) | ||
| 214 | return func(vcpu); | ||
| 215 | return -ENOTSUPP; | ||
| 216 | } | ||
