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