1 /* 2 * in-kernel handling for sie intercepts 3 * 4 * Copyright IBM Corp. 2008, 2014 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 #include <asm/asm-offsets.h> 20 #include <asm/irq.h> 21 #include <asm/sysinfo.h> 22 23 #include "kvm-s390.h" 24 #include "gaccess.h" 25 #include "trace.h" 26 #include "trace-s390.h" 27 28 29 static const intercept_handler_t instruction_handlers[256] = { 30 [0x01] = kvm_s390_handle_01, 31 [0x82] = kvm_s390_handle_lpsw, 32 [0x83] = kvm_s390_handle_diag, 33 [0xaa] = kvm_s390_handle_aa, 34 [0xae] = kvm_s390_handle_sigp, 35 [0xb2] = kvm_s390_handle_b2, 36 [0xb6] = kvm_s390_handle_stctl, 37 [0xb7] = kvm_s390_handle_lctl, 38 [0xb9] = kvm_s390_handle_b9, 39 [0xe3] = kvm_s390_handle_e3, 40 [0xe5] = kvm_s390_handle_e5, 41 [0xeb] = kvm_s390_handle_eb, 42 }; 43 44 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu) 45 { 46 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block; 47 u8 ilen = 0; 48 49 switch (vcpu->arch.sie_block->icptcode) { 50 case ICPT_INST: 51 case ICPT_INSTPROGI: 52 case ICPT_OPEREXC: 53 case ICPT_PARTEXEC: 54 case ICPT_IOINST: 55 /* instruction only stored for these icptcodes */ 56 ilen = insn_length(vcpu->arch.sie_block->ipa >> 8); 57 /* Use the length of the EXECUTE instruction if necessary */ 58 if (sie_block->icptstatus & 1) { 59 ilen = (sie_block->icptstatus >> 4) & 0x6; 60 if (!ilen) 61 ilen = 4; 62 } 63 break; 64 case ICPT_PROGI: 65 /* bit 1+2 of pgmilc are the ilc, so we directly get ilen */ 66 ilen = vcpu->arch.sie_block->pgmilc & 0x6; 67 break; 68 } 69 return ilen; 70 } 71 72 static int handle_noop(struct kvm_vcpu *vcpu) 73 { 74 switch (vcpu->arch.sie_block->icptcode) { 75 case 0x10: 76 vcpu->stat.exit_external_request++; 77 break; 78 default: 79 break; /* nothing */ 80 } 81 return 0; 82 } 83 84 static int handle_stop(struct kvm_vcpu *vcpu) 85 { 86 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; 87 int rc = 0; 88 uint8_t flags, stop_pending; 89 90 vcpu->stat.exit_stop_request++; 91 92 /* delay the stop if any non-stop irq is pending */ 93 if (kvm_s390_vcpu_has_irq(vcpu, 1)) 94 return 0; 95 96 /* avoid races with the injection/SIGP STOP code */ 97 spin_lock(&li->lock); 98 flags = li->irq.stop.flags; 99 stop_pending = kvm_s390_is_stop_irq_pending(vcpu); 100 spin_unlock(&li->lock); 101 102 trace_kvm_s390_stop_request(stop_pending, flags); 103 if (!stop_pending) 104 return 0; 105 106 if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) { 107 rc = kvm_s390_vcpu_store_status(vcpu, 108 KVM_S390_STORE_STATUS_NOADDR); 109 if (rc) 110 return rc; 111 } 112 113 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 114 kvm_s390_vcpu_stop(vcpu); 115 return -EOPNOTSUPP; 116 } 117 118 static int handle_validity(struct kvm_vcpu *vcpu) 119 { 120 int viwhy = vcpu->arch.sie_block->ipb >> 16; 121 122 vcpu->stat.exit_validity++; 123 trace_kvm_s390_intercept_validity(vcpu, viwhy); 124 KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy, 125 current->pid, vcpu->kvm); 126 127 /* do not warn on invalid runtime instrumentation mode */ 128 WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n", 129 viwhy); 130 return -EINVAL; 131 } 132 133 static int handle_instruction(struct kvm_vcpu *vcpu) 134 { 135 intercept_handler_t handler; 136 137 vcpu->stat.exit_instruction++; 138 trace_kvm_s390_intercept_instruction(vcpu, 139 vcpu->arch.sie_block->ipa, 140 vcpu->arch.sie_block->ipb); 141 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8]; 142 if (handler) 143 return handler(vcpu); 144 return -EOPNOTSUPP; 145 } 146 147 static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu) 148 { 149 struct kvm_s390_pgm_info pgm_info = { 150 .code = vcpu->arch.sie_block->iprcc, 151 /* the PSW has already been rewound */ 152 .flags = KVM_S390_PGM_FLAGS_NO_REWIND, 153 }; 154 155 switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) { 156 case PGM_AFX_TRANSLATION: 157 case PGM_ASX_TRANSLATION: 158 case PGM_EX_TRANSLATION: 159 case PGM_LFX_TRANSLATION: 160 case PGM_LSTE_SEQUENCE: 161 case PGM_LSX_TRANSLATION: 162 case PGM_LX_TRANSLATION: 163 case PGM_PRIMARY_AUTHORITY: 164 case PGM_SECONDARY_AUTHORITY: 165 case PGM_SPACE_SWITCH: 166 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc; 167 break; 168 case PGM_ALEN_TRANSLATION: 169 case PGM_ALE_SEQUENCE: 170 case PGM_ASTE_INSTANCE: 171 case PGM_ASTE_SEQUENCE: 172 case PGM_ASTE_VALIDITY: 173 case PGM_EXTENDED_AUTHORITY: 174 pgm_info.exc_access_id = vcpu->arch.sie_block->eai; 175 break; 176 case PGM_ASCE_TYPE: 177 case PGM_PAGE_TRANSLATION: 178 case PGM_REGION_FIRST_TRANS: 179 case PGM_REGION_SECOND_TRANS: 180 case PGM_REGION_THIRD_TRANS: 181 case PGM_SEGMENT_TRANSLATION: 182 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc; 183 pgm_info.exc_access_id = vcpu->arch.sie_block->eai; 184 pgm_info.op_access_id = vcpu->arch.sie_block->oai; 185 break; 186 case PGM_MONITOR: 187 pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn; 188 pgm_info.mon_code = vcpu->arch.sie_block->tecmc; 189 break; 190 case PGM_VECTOR_PROCESSING: 191 case PGM_DATA: 192 pgm_info.data_exc_code = vcpu->arch.sie_block->dxc; 193 break; 194 case PGM_PROTECTION: 195 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc; 196 pgm_info.exc_access_id = vcpu->arch.sie_block->eai; 197 break; 198 default: 199 break; 200 } 201 202 if (vcpu->arch.sie_block->iprcc & PGM_PER) { 203 pgm_info.per_code = vcpu->arch.sie_block->perc; 204 pgm_info.per_atmid = vcpu->arch.sie_block->peratmid; 205 pgm_info.per_address = vcpu->arch.sie_block->peraddr; 206 pgm_info.per_access_id = vcpu->arch.sie_block->peraid; 207 } 208 return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 209 } 210 211 /* 212 * restore ITDB to program-interruption TDB in guest lowcore 213 * and set TX abort indication if required 214 */ 215 static int handle_itdb(struct kvm_vcpu *vcpu) 216 { 217 struct kvm_s390_itdb *itdb; 218 int rc; 219 220 if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu)) 221 return 0; 222 if (current->thread.per_flags & PER_FLAG_NO_TE) 223 return 0; 224 itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba; 225 rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb)); 226 if (rc) 227 return rc; 228 memset(itdb, 0, sizeof(*itdb)); 229 230 return 0; 231 } 232 233 #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER) 234 235 static int handle_prog(struct kvm_vcpu *vcpu) 236 { 237 psw_t psw; 238 int rc; 239 240 vcpu->stat.exit_program_interruption++; 241 242 if (guestdbg_enabled(vcpu) && per_event(vcpu)) { 243 rc = kvm_s390_handle_per_event(vcpu); 244 if (rc) 245 return rc; 246 /* the interrupt might have been filtered out completely */ 247 if (vcpu->arch.sie_block->iprcc == 0) 248 return 0; 249 } 250 251 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc); 252 if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) { 253 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t)); 254 if (rc) 255 return rc; 256 /* Avoid endless loops of specification exceptions */ 257 if (!is_valid_psw(&psw)) 258 return -EOPNOTSUPP; 259 } 260 rc = handle_itdb(vcpu); 261 if (rc) 262 return rc; 263 264 return inject_prog_on_prog_intercept(vcpu); 265 } 266 267 /** 268 * handle_external_interrupt - used for external interruption interceptions 269 * 270 * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if 271 * the new PSW does not have external interrupts disabled. In the first case, 272 * we've got to deliver the interrupt manually, and in the second case, we 273 * drop to userspace to handle the situation there. 274 */ 275 static int handle_external_interrupt(struct kvm_vcpu *vcpu) 276 { 277 u16 eic = vcpu->arch.sie_block->eic; 278 struct kvm_s390_irq irq; 279 psw_t newpsw; 280 int rc; 281 282 vcpu->stat.exit_external_interrupt++; 283 284 rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t)); 285 if (rc) 286 return rc; 287 /* We can not handle clock comparator or timer interrupt with bad PSW */ 288 if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) && 289 (newpsw.mask & PSW_MASK_EXT)) 290 return -EOPNOTSUPP; 291 292 switch (eic) { 293 case EXT_IRQ_CLK_COMP: 294 irq.type = KVM_S390_INT_CLOCK_COMP; 295 break; 296 case EXT_IRQ_CPU_TIMER: 297 irq.type = KVM_S390_INT_CPU_TIMER; 298 break; 299 case EXT_IRQ_EXTERNAL_CALL: 300 irq.type = KVM_S390_INT_EXTERNAL_CALL; 301 irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr; 302 rc = kvm_s390_inject_vcpu(vcpu, &irq); 303 /* ignore if another external call is already pending */ 304 if (rc == -EBUSY) 305 return 0; 306 return rc; 307 default: 308 return -EOPNOTSUPP; 309 } 310 311 return kvm_s390_inject_vcpu(vcpu, &irq); 312 } 313 314 /** 315 * Handle MOVE PAGE partial execution interception. 316 * 317 * This interception can only happen for guests with DAT disabled and 318 * addresses that are currently not mapped in the host. Thus we try to 319 * set up the mappings for the corresponding user pages here (or throw 320 * addressing exceptions in case of illegal guest addresses). 321 */ 322 static int handle_mvpg_pei(struct kvm_vcpu *vcpu) 323 { 324 unsigned long srcaddr, dstaddr; 325 int reg1, reg2, rc; 326 327 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 328 329 /* Make sure that the source is paged-in */ 330 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2], 331 reg2, &srcaddr, GACC_FETCH); 332 if (rc) 333 return kvm_s390_inject_prog_cond(vcpu, rc); 334 rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0); 335 if (rc != 0) 336 return rc; 337 338 /* Make sure that the destination is paged-in */ 339 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1], 340 reg1, &dstaddr, GACC_STORE); 341 if (rc) 342 return kvm_s390_inject_prog_cond(vcpu, rc); 343 rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1); 344 if (rc != 0) 345 return rc; 346 347 kvm_s390_retry_instr(vcpu); 348 349 return 0; 350 } 351 352 static int handle_partial_execution(struct kvm_vcpu *vcpu) 353 { 354 vcpu->stat.exit_pei++; 355 356 if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */ 357 return handle_mvpg_pei(vcpu); 358 if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */ 359 return kvm_s390_handle_sigp_pei(vcpu); 360 361 return -EOPNOTSUPP; 362 } 363 364 /* 365 * Handle the sthyi instruction that provides the guest with system 366 * information, like current CPU resources available at each level of 367 * the machine. 368 */ 369 int handle_sthyi(struct kvm_vcpu *vcpu) 370 { 371 int reg1, reg2, r = 0; 372 u64 code, addr, cc = 0, rc = 0; 373 struct sthyi_sctns *sctns = NULL; 374 375 if (!test_kvm_facility(vcpu->kvm, 74)) 376 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 377 378 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 379 code = vcpu->run->s.regs.gprs[reg1]; 380 addr = vcpu->run->s.regs.gprs[reg2]; 381 382 vcpu->stat.instruction_sthyi++; 383 VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr); 384 trace_kvm_s390_handle_sthyi(vcpu, code, addr); 385 386 if (reg1 == reg2 || reg1 & 1 || reg2 & 1) 387 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 388 389 if (code & 0xffff) { 390 cc = 3; 391 rc = 4; 392 goto out; 393 } 394 395 if (addr & ~PAGE_MASK) 396 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 397 398 sctns = (void *)get_zeroed_page(GFP_KERNEL); 399 if (!sctns) 400 return -ENOMEM; 401 402 cc = sthyi_fill(sctns, &rc); 403 404 out: 405 if (!cc) { 406 r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE); 407 if (r) { 408 free_page((unsigned long)sctns); 409 return kvm_s390_inject_prog_cond(vcpu, r); 410 } 411 } 412 413 free_page((unsigned long)sctns); 414 vcpu->run->s.regs.gprs[reg2 + 1] = rc; 415 kvm_s390_set_psw_cc(vcpu, cc); 416 return r; 417 } 418 419 static int handle_operexc(struct kvm_vcpu *vcpu) 420 { 421 psw_t oldpsw, newpsw; 422 int rc; 423 424 vcpu->stat.exit_operation_exception++; 425 trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa, 426 vcpu->arch.sie_block->ipb); 427 428 if (vcpu->arch.sie_block->ipa == 0xb256) 429 return handle_sthyi(vcpu); 430 431 if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0) 432 return -EOPNOTSUPP; 433 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t)); 434 if (rc) 435 return rc; 436 /* 437 * Avoid endless loops of operation exceptions, if the pgm new 438 * PSW will cause a new operation exception. 439 * The heuristic checks if the pgm new psw is within 6 bytes before 440 * the faulting psw address (with same DAT, AS settings) and the 441 * new psw is not a wait psw and the fault was not triggered by 442 * problem state. 443 */ 444 oldpsw = vcpu->arch.sie_block->gpsw; 445 if (oldpsw.addr - newpsw.addr <= 6 && 446 !(newpsw.mask & PSW_MASK_WAIT) && 447 !(oldpsw.mask & PSW_MASK_PSTATE) && 448 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) && 449 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) 450 return -EOPNOTSUPP; 451 452 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 453 } 454 455 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu) 456 { 457 int rc, per_rc = 0; 458 459 if (kvm_is_ucontrol(vcpu->kvm)) 460 return -EOPNOTSUPP; 461 462 switch (vcpu->arch.sie_block->icptcode) { 463 case ICPT_EXTREQ: 464 case ICPT_IOREQ: 465 return handle_noop(vcpu); 466 case ICPT_INST: 467 rc = handle_instruction(vcpu); 468 break; 469 case ICPT_PROGI: 470 return handle_prog(vcpu); 471 case ICPT_EXTINT: 472 return handle_external_interrupt(vcpu); 473 case ICPT_WAIT: 474 return kvm_s390_handle_wait(vcpu); 475 case ICPT_VALIDITY: 476 return handle_validity(vcpu); 477 case ICPT_STOP: 478 return handle_stop(vcpu); 479 case ICPT_OPEREXC: 480 rc = handle_operexc(vcpu); 481 break; 482 case ICPT_PARTEXEC: 483 rc = handle_partial_execution(vcpu); 484 break; 485 case ICPT_KSS: 486 rc = kvm_s390_skey_check_enable(vcpu); 487 break; 488 default: 489 return -EOPNOTSUPP; 490 } 491 492 /* process PER, also if the instrution is processed in user space */ 493 if (vcpu->arch.sie_block->icptstatus & 0x02 && 494 (!rc || rc == -EOPNOTSUPP)) 495 per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu); 496 return per_rc ? per_rc : rc; 497 } 498