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 | * |
| 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.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 | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 17 | #include <asm/asm-offsets.h> |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 18 | #include <asm/current.h> |
| 19 | #include <asm/debug.h> |
| 20 | #include <asm/ebcdic.h> |
| 21 | #include <asm/sysinfo.h> |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 22 | #include <asm/ptrace.h> |
| 23 | #include <asm/compat.h> |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 24 | #include "gaccess.h" |
| 25 | #include "kvm-s390.h" |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 26 | #include "trace.h" |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 27 | |
| 28 | static int handle_set_prefix(struct kvm_vcpu *vcpu) |
| 29 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 30 | u64 operand2; |
| 31 | u32 address = 0; |
| 32 | u8 tmp; |
| 33 | |
| 34 | vcpu->stat.instruction_spx++; |
| 35 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 36 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 37 | |
| 38 | /* must be word boundary */ |
| 39 | if (operand2 & 3) { |
| 40 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 41 | goto out; |
| 42 | } |
| 43 | |
| 44 | /* get the value */ |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 45 | if (get_guest(vcpu, address, (u32 __user *) operand2)) { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 46 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 47 | goto out; |
| 48 | } |
| 49 | |
| 50 | address = address & 0x7fffe000u; |
| 51 | |
| 52 | /* make sure that the new value is valid memory */ |
| 53 | if (copy_from_guest_absolute(vcpu, &tmp, address, 1) || |
| 54 | (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) { |
| 55 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 56 | goto out; |
| 57 | } |
| 58 | |
Christian Borntraeger | 8d26cf7 | 2012-01-11 11:19:32 +0100 | [diff] [blame] | 59 | kvm_s390_set_prefix(vcpu, address); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 60 | |
| 61 | VCPU_EVENT(vcpu, 5, "setting prefix to %x", address); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 62 | trace_kvm_s390_handle_prefix(vcpu, 1, address); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 63 | out: |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int handle_store_prefix(struct kvm_vcpu *vcpu) |
| 68 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 69 | u64 operand2; |
| 70 | u32 address; |
| 71 | |
| 72 | vcpu->stat.instruction_stpx++; |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 73 | |
| 74 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 75 | |
| 76 | /* must be word boundary */ |
| 77 | if (operand2 & 3) { |
| 78 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | address = vcpu->arch.sie_block->prefix; |
| 83 | address = address & 0x7fffe000u; |
| 84 | |
| 85 | /* get the value */ |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 86 | if (put_guest(vcpu, address, (u32 __user *)operand2)) { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 87 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | VCPU_EVENT(vcpu, 5, "storing prefix to %x", address); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 92 | trace_kvm_s390_handle_prefix(vcpu, 0, address); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 93 | out: |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int handle_store_cpu_address(struct kvm_vcpu *vcpu) |
| 98 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 99 | u64 useraddr; |
| 100 | int rc; |
| 101 | |
| 102 | vcpu->stat.instruction_stap++; |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 103 | |
| 104 | useraddr = kvm_s390_get_base_disp_s(vcpu); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 105 | |
| 106 | if (useraddr & 1) { |
| 107 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 108 | goto out; |
| 109 | } |
| 110 | |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 111 | rc = put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 112 | if (rc) { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 113 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 114 | goto out; |
| 115 | } |
| 116 | |
Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 117 | VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 118 | trace_kvm_s390_handle_stap(vcpu, useraddr); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 119 | out: |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int handle_skey(struct kvm_vcpu *vcpu) |
| 124 | { |
| 125 | vcpu->stat.instruction_storage_key++; |
| 126 | vcpu->arch.sie_block->gpsw.addr -= 4; |
| 127 | VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation"); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 131 | static int handle_tpi(struct kvm_vcpu *vcpu) |
| 132 | { |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 133 | struct kvm_s390_interrupt_info *inti; |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 134 | u64 addr; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 135 | int cc; |
| 136 | |
| 137 | addr = kvm_s390_get_base_disp_s(vcpu); |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 138 | if (addr & 3) { |
| 139 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 140 | goto out; |
| 141 | } |
| 142 | cc = 0; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 143 | inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0); |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 144 | if (!inti) |
| 145 | goto no_interrupt; |
| 146 | cc = 1; |
| 147 | if (addr) { |
| 148 | /* |
| 149 | * Store the two-word I/O interruption code into the |
| 150 | * provided area. |
| 151 | */ |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 152 | put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr); |
| 153 | put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2)); |
| 154 | put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4)); |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 155 | } else { |
| 156 | /* |
| 157 | * Store the three-word I/O interruption code into |
| 158 | * the appropriate lowcore area. |
| 159 | */ |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 160 | put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID); |
| 161 | put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR); |
| 162 | put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM); |
| 163 | put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD); |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 164 | } |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 165 | kfree(inti); |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 166 | no_interrupt: |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 167 | /* Set condition code and we're done. */ |
| 168 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
| 169 | vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44; |
Heiko Carstens | 7c959e8 | 2013-03-05 13:14:46 +0100 | [diff] [blame] | 170 | out: |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int handle_tsch(struct kvm_vcpu *vcpu) |
| 175 | { |
| 176 | struct kvm_s390_interrupt_info *inti; |
| 177 | |
| 178 | inti = kvm_s390_get_io_int(vcpu->kvm, 0, |
| 179 | vcpu->run->s.regs.gprs[1]); |
| 180 | |
| 181 | /* |
| 182 | * Prepare exit to userspace. |
| 183 | * We indicate whether we dequeued a pending I/O interrupt |
| 184 | * so that userspace can re-inject it if the instruction gets |
| 185 | * a program check. While this may re-order the pending I/O |
| 186 | * interrupts, this is no problem since the priority is kept |
| 187 | * intact. |
| 188 | */ |
| 189 | vcpu->run->exit_reason = KVM_EXIT_S390_TSCH; |
| 190 | vcpu->run->s390_tsch.dequeued = !!inti; |
| 191 | if (inti) { |
| 192 | vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id; |
| 193 | vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr; |
| 194 | vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm; |
| 195 | vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word; |
| 196 | } |
| 197 | vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb; |
| 198 | kfree(inti); |
| 199 | return -EREMOTE; |
| 200 | } |
| 201 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 202 | static int handle_io_inst(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 203 | { |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 204 | VCPU_EVENT(vcpu, 4, "%s", "I/O instruction"); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 205 | |
| 206 | if (vcpu->kvm->arch.css_support) { |
| 207 | /* |
| 208 | * Most I/O instructions will be handled by userspace. |
| 209 | * Exceptions are tpi and the interrupt portion of tsch. |
| 210 | */ |
| 211 | if (vcpu->arch.sie_block->ipa == 0xb236) |
| 212 | return handle_tpi(vcpu); |
| 213 | if (vcpu->arch.sie_block->ipa == 0xb235) |
| 214 | return handle_tsch(vcpu); |
| 215 | /* Handle in userspace. */ |
| 216 | return -EOPNOTSUPP; |
| 217 | } else { |
| 218 | /* |
| 219 | * Set condition code 3 to stop the guest from issueing channel |
| 220 | * I/O instructions. |
| 221 | */ |
| 222 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
| 223 | vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44; |
| 224 | return 0; |
| 225 | } |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 228 | static int handle_stfl(struct kvm_vcpu *vcpu) |
| 229 | { |
Martin Schwidefsky | 14375bc | 2010-10-25 16:10:51 +0200 | [diff] [blame] | 230 | unsigned int facility_list; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 231 | int rc; |
| 232 | |
| 233 | vcpu->stat.instruction_stfl++; |
Christian Borntraeger | a0046b6 | 2008-08-29 13:29:45 +0200 | [diff] [blame] | 234 | /* only pass the facility bits, which we can handle */ |
Martin Schwidefsky | 14375bc | 2010-10-25 16:10:51 +0200 | [diff] [blame] | 235 | facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 236 | |
| 237 | rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list), |
| 238 | &facility_list, sizeof(facility_list)); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 239 | if (rc) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 240 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 241 | else { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 242 | VCPU_EVENT(vcpu, 5, "store facility list value %x", |
| 243 | facility_list); |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 244 | trace_kvm_s390_handle_stfl(vcpu, facility_list); |
| 245 | } |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 249 | static void handle_new_psw(struct kvm_vcpu *vcpu) |
| 250 | { |
| 251 | /* Check whether the new psw is enabled for machine checks. */ |
| 252 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK) |
| 253 | kvm_s390_deliver_pending_machine_checks(vcpu); |
| 254 | } |
| 255 | |
| 256 | #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA) |
| 257 | #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL |
Heiko Carstens | d21683e | 2013-03-25 17:22:49 +0100 | [diff] [blame] | 258 | #define PSW_ADDR_24 0x0000000000ffffffUL |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 259 | #define PSW_ADDR_31 0x000000007fffffffUL |
| 260 | |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 261 | static int is_valid_psw(psw_t *psw) { |
| 262 | if (psw->mask & PSW_MASK_UNASSIGNED) |
| 263 | return 0; |
| 264 | if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) { |
| 265 | if (psw->addr & ~PSW_ADDR_31) |
| 266 | return 0; |
| 267 | } |
| 268 | if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24)) |
| 269 | return 0; |
| 270 | if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA) |
| 271 | return 0; |
| 272 | return 1; |
| 273 | } |
| 274 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 275 | int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) |
| 276 | { |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 277 | psw_t *gpsw = &vcpu->arch.sie_block->gpsw; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 278 | psw_compat_t new_psw; |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 279 | u64 addr; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 280 | |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 281 | if (gpsw->mask & PSW_MASK_PSTATE) |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 282 | return kvm_s390_inject_program_int(vcpu, |
| 283 | PGM_PRIVILEGED_OPERATION); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 284 | addr = kvm_s390_get_base_disp_s(vcpu); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 285 | if (addr & 7) |
| 286 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 287 | if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw))) |
| 288 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 289 | if (!(new_psw.mask & PSW32_MASK_BASE)) |
| 290 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 291 | gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32; |
| 292 | gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE; |
| 293 | gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE; |
| 294 | if (!is_valid_psw(gpsw)) |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 295 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 296 | handle_new_psw(vcpu); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int handle_lpswe(struct kvm_vcpu *vcpu) |
| 301 | { |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 302 | psw_t new_psw; |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 303 | u64 addr; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 304 | |
| 305 | addr = kvm_s390_get_base_disp_s(vcpu); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 306 | if (addr & 7) |
| 307 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 308 | if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw))) |
| 309 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
Heiko Carstens | 3736b87 | 2013-03-25 17:22:52 +0100 | [diff] [blame^] | 310 | vcpu->arch.sie_block->gpsw = new_psw; |
| 311 | if (!is_valid_psw(&vcpu->arch.sie_block->gpsw)) |
Heiko Carstens | 6fd0fcc | 2013-03-25 17:22:51 +0100 | [diff] [blame] | 312 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 313 | handle_new_psw(vcpu); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 317 | static int handle_stidp(struct kvm_vcpu *vcpu) |
| 318 | { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 319 | u64 operand2; |
| 320 | int rc; |
| 321 | |
| 322 | vcpu->stat.instruction_stidp++; |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 323 | |
| 324 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 325 | |
| 326 | if (operand2 & 7) { |
| 327 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 328 | goto out; |
| 329 | } |
| 330 | |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 331 | rc = put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 332 | if (rc) { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 333 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 334 | goto out; |
| 335 | } |
| 336 | |
| 337 | VCPU_EVENT(vcpu, 5, "%s", "store cpu id"); |
| 338 | out: |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem) |
| 343 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 344 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 345 | int cpus = 0; |
| 346 | int n; |
| 347 | |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 348 | spin_lock(&fi->lock); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 349 | for (n = 0; n < KVM_MAX_VCPUS; n++) |
| 350 | if (fi->local_int[n]) |
| 351 | cpus++; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 352 | spin_unlock(&fi->lock); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 353 | |
| 354 | /* deal with other level 3 hypervisors */ |
Heiko Carstens | caf757c | 2012-09-06 14:42:13 +0200 | [diff] [blame] | 355 | if (stsi(mem, 3, 2, 2)) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 356 | mem->count = 0; |
| 357 | if (mem->count < 8) |
| 358 | mem->count++; |
| 359 | for (n = mem->count - 1; n > 0 ; n--) |
| 360 | memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0])); |
| 361 | |
| 362 | mem->vm[0].cpus_total = cpus; |
| 363 | mem->vm[0].cpus_configured = cpus; |
| 364 | mem->vm[0].cpus_standby = 0; |
| 365 | mem->vm[0].cpus_reserved = 0; |
| 366 | mem->vm[0].caf = 1000; |
| 367 | memcpy(mem->vm[0].name, "KVMguest", 8); |
| 368 | ASCEBC(mem->vm[0].name, 8); |
| 369 | memcpy(mem->vm[0].cpi, "KVM/Linux ", 16); |
| 370 | ASCEBC(mem->vm[0].cpi, 16); |
| 371 | } |
| 372 | |
| 373 | static int handle_stsi(struct kvm_vcpu *vcpu) |
| 374 | { |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 375 | int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28; |
| 376 | int sel1 = vcpu->run->s.regs.gprs[0] & 0xff; |
| 377 | int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 378 | u64 operand2; |
| 379 | unsigned long mem; |
| 380 | |
| 381 | vcpu->stat.instruction_stsi++; |
| 382 | VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2); |
| 383 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 384 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 385 | |
| 386 | if (operand2 & 0xfff && fc > 0) |
| 387 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 388 | |
| 389 | switch (fc) { |
| 390 | case 0: |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 391 | vcpu->run->s.regs.gprs[0] = 3 << 28; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 392 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
| 393 | return 0; |
| 394 | case 1: /* same handling for 1 and 2 */ |
| 395 | case 2: |
| 396 | mem = get_zeroed_page(GFP_KERNEL); |
| 397 | if (!mem) |
| 398 | goto out_fail; |
Heiko Carstens | caf757c | 2012-09-06 14:42:13 +0200 | [diff] [blame] | 399 | if (stsi((void *) mem, fc, sel1, sel2)) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 400 | goto out_mem; |
| 401 | break; |
| 402 | case 3: |
| 403 | if (sel1 != 2 || sel2 != 2) |
| 404 | goto out_fail; |
| 405 | mem = get_zeroed_page(GFP_KERNEL); |
| 406 | if (!mem) |
| 407 | goto out_fail; |
| 408 | handle_stsi_3_2_2(vcpu, (void *) mem); |
| 409 | break; |
| 410 | default: |
| 411 | goto out_fail; |
| 412 | } |
| 413 | |
| 414 | if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) { |
| 415 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 416 | goto out_mem; |
| 417 | } |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 418 | trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2); |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 419 | free_page(mem); |
| 420 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 421 | vcpu->run->s.regs.gprs[0] = 0; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 422 | return 0; |
| 423 | out_mem: |
| 424 | free_page(mem); |
| 425 | out_fail: |
| 426 | /* condition code 3 */ |
| 427 | vcpu->arch.sie_block->gpsw.mask |= 3ul << 44; |
| 428 | return 0; |
| 429 | } |
| 430 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 431 | static const intercept_handler_t b2_handlers[256] = { |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 432 | [0x02] = handle_stidp, |
| 433 | [0x10] = handle_set_prefix, |
| 434 | [0x11] = handle_store_prefix, |
| 435 | [0x12] = handle_store_cpu_address, |
| 436 | [0x29] = handle_skey, |
| 437 | [0x2a] = handle_skey, |
| 438 | [0x2b] = handle_skey, |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 439 | [0x30] = handle_io_inst, |
| 440 | [0x31] = handle_io_inst, |
| 441 | [0x32] = handle_io_inst, |
| 442 | [0x33] = handle_io_inst, |
| 443 | [0x34] = handle_io_inst, |
| 444 | [0x35] = handle_io_inst, |
| 445 | [0x36] = handle_io_inst, |
| 446 | [0x37] = handle_io_inst, |
| 447 | [0x38] = handle_io_inst, |
| 448 | [0x39] = handle_io_inst, |
| 449 | [0x3a] = handle_io_inst, |
| 450 | [0x3b] = handle_io_inst, |
| 451 | [0x3c] = handle_io_inst, |
| 452 | [0x5f] = handle_io_inst, |
| 453 | [0x74] = handle_io_inst, |
| 454 | [0x76] = handle_io_inst, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 455 | [0x7d] = handle_stsi, |
| 456 | [0xb1] = handle_stfl, |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 457 | [0xb2] = handle_lpswe, |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 458 | }; |
| 459 | |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 460 | int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 461 | { |
| 462 | intercept_handler_t handler; |
| 463 | |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 464 | /* |
| 465 | * a lot of B2 instructions are priviledged. We first check for |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 466 | * the privileged ones, that we can handle in the kernel. If the |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 467 | * kernel can handle this instruction, we check for the problem |
| 468 | * state bit and (a) handle the instruction or (b) send a code 2 |
| 469 | * program check. |
| 470 | * Anything else goes to userspace.*/ |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 471 | handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 472 | if (handler) { |
| 473 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 474 | return kvm_s390_inject_program_int(vcpu, |
| 475 | PGM_PRIVILEGED_OPERATION); |
| 476 | else |
| 477 | return handler(vcpu); |
| 478 | } |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 479 | return -EOPNOTSUPP; |
Christian Borntraeger | 453423d | 2008-03-25 18:47:29 +0100 | [diff] [blame] | 480 | } |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 481 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 482 | static int handle_epsw(struct kvm_vcpu *vcpu) |
| 483 | { |
| 484 | int reg1, reg2; |
| 485 | |
| 486 | reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24; |
| 487 | reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16; |
| 488 | |
| 489 | /* This basically extracts the mask half of the psw. */ |
| 490 | vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000; |
| 491 | vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32; |
| 492 | if (reg2) { |
| 493 | vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000; |
| 494 | vcpu->run->s.regs.gprs[reg2] |= |
| 495 | vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff; |
| 496 | } |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static const intercept_handler_t b9_handlers[256] = { |
| 501 | [0x8d] = handle_epsw, |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 502 | [0x9c] = handle_io_inst, |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | int kvm_s390_handle_b9(struct kvm_vcpu *vcpu) |
| 506 | { |
| 507 | intercept_handler_t handler; |
| 508 | |
| 509 | /* This is handled just as for the B2 instructions. */ |
| 510 | handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
| 511 | if (handler) { |
| 512 | if ((handler != handle_epsw) && |
| 513 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)) |
| 514 | return kvm_s390_inject_program_int(vcpu, |
| 515 | PGM_PRIVILEGED_OPERATION); |
| 516 | else |
| 517 | return handler(vcpu); |
| 518 | } |
| 519 | return -EOPNOTSUPP; |
| 520 | } |
| 521 | |
Cornelia Huck | f379aae | 2012-12-20 15:32:10 +0100 | [diff] [blame] | 522 | static const intercept_handler_t eb_handlers[256] = { |
| 523 | [0x8a] = handle_io_inst, |
| 524 | }; |
| 525 | |
| 526 | int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu) |
| 527 | { |
| 528 | intercept_handler_t handler; |
| 529 | |
| 530 | /* All eb instructions that end up here are privileged. */ |
| 531 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 532 | return kvm_s390_inject_program_int(vcpu, |
| 533 | PGM_PRIVILEGED_OPERATION); |
| 534 | handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff]; |
| 535 | if (handler) |
| 536 | return handler(vcpu); |
| 537 | return -EOPNOTSUPP; |
| 538 | } |
| 539 | |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 540 | static int handle_tprot(struct kvm_vcpu *vcpu) |
| 541 | { |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 542 | u64 address1, address2; |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 543 | struct vm_area_struct *vma; |
Christian Borntraeger | 1eddb85 | 2011-11-17 11:00:43 +0100 | [diff] [blame] | 544 | unsigned long user_address; |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 545 | |
| 546 | vcpu->stat.instruction_tprot++; |
| 547 | |
Cornelia Huck | b1c571a | 2012-12-20 15:32:07 +0100 | [diff] [blame] | 548 | kvm_s390_get_base_disp_sse(vcpu, &address1, &address2); |
| 549 | |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 550 | /* we only handle the Linux memory detection case: |
| 551 | * access key == 0 |
| 552 | * guest DAT == off |
| 553 | * everything else goes to userspace. */ |
| 554 | if (address2 & 0xf0) |
| 555 | return -EOPNOTSUPP; |
| 556 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) |
| 557 | return -EOPNOTSUPP; |
| 558 | |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 559 | down_read(¤t->mm->mmap_sem); |
Heiko Carstens | 59a1fa2 | 2013-03-05 13:14:42 +0100 | [diff] [blame] | 560 | user_address = __gmap_translate(address1, vcpu->arch.gmap); |
| 561 | if (IS_ERR_VALUE(user_address)) |
| 562 | goto out_inject; |
Christian Borntraeger | 1eddb85 | 2011-11-17 11:00:43 +0100 | [diff] [blame] | 563 | vma = find_vma(current->mm, user_address); |
Heiko Carstens | 59a1fa2 | 2013-03-05 13:14:42 +0100 | [diff] [blame] | 564 | if (!vma) |
| 565 | goto out_inject; |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 566 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
| 567 | if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ)) |
| 568 | vcpu->arch.sie_block->gpsw.mask |= (1ul << 44); |
| 569 | if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ)) |
| 570 | vcpu->arch.sie_block->gpsw.mask |= (2ul << 44); |
| 571 | |
| 572 | up_read(¤t->mm->mmap_sem); |
| 573 | return 0; |
Heiko Carstens | 59a1fa2 | 2013-03-05 13:14:42 +0100 | [diff] [blame] | 574 | |
| 575 | out_inject: |
| 576 | up_read(¤t->mm->mmap_sem); |
| 577 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
Christian Borntraeger | bb25b9b | 2011-07-24 10:48:17 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | int kvm_s390_handle_e5(struct kvm_vcpu *vcpu) |
| 581 | { |
| 582 | /* For e5xx... instructions we only handle TPROT */ |
| 583 | if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01) |
| 584 | return handle_tprot(vcpu); |
| 585 | return -EOPNOTSUPP; |
| 586 | } |
| 587 | |
Cornelia Huck | 8c3f61e | 2012-04-24 09:24:44 +0200 | [diff] [blame] | 588 | static int handle_sckpf(struct kvm_vcpu *vcpu) |
| 589 | { |
| 590 | u32 value; |
| 591 | |
| 592 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 593 | return kvm_s390_inject_program_int(vcpu, |
| 594 | PGM_PRIVILEGED_OPERATION); |
| 595 | |
| 596 | if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000) |
| 597 | return kvm_s390_inject_program_int(vcpu, |
| 598 | PGM_SPECIFICATION); |
| 599 | |
| 600 | value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff; |
| 601 | vcpu->arch.sie_block->todpr = value; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
Cornelia Huck | 7797535 | 2012-12-20 15:32:06 +0100 | [diff] [blame] | 606 | static const intercept_handler_t x01_handlers[256] = { |
Cornelia Huck | 8c3f61e | 2012-04-24 09:24:44 +0200 | [diff] [blame] | 607 | [0x07] = handle_sckpf, |
| 608 | }; |
| 609 | |
| 610 | int kvm_s390_handle_01(struct kvm_vcpu *vcpu) |
| 611 | { |
| 612 | intercept_handler_t handler; |
| 613 | |
| 614 | handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
| 615 | if (handler) |
| 616 | return handler(vcpu); |
| 617 | return -EOPNOTSUPP; |
| 618 | } |