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