kvm: vmx: hook preemption timer support
[cascardo/linux.git] / arch / s390 / kvm / priv.c
1 /*
2  * handling privileged instructions
3  *
4  * Copyright IBM Corp. 2008, 2013
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>
15 #include <linux/gfp.h>
16 #include <linux/errno.h>
17 #include <linux/compat.h>
18 #include <asm/asm-offsets.h>
19 #include <asm/facility.h>
20 #include <asm/current.h>
21 #include <asm/debug.h>
22 #include <asm/ebcdic.h>
23 #include <asm/sysinfo.h>
24 #include <asm/pgtable.h>
25 #include <asm/pgalloc.h>
26 #include <asm/gmap.h>
27 #include <asm/io.h>
28 #include <asm/ptrace.h>
29 #include <asm/compat.h>
30 #include <asm/sclp.h>
31 #include "gaccess.h"
32 #include "kvm-s390.h"
33 #include "trace.h"
34
35 /* Handle SCK (SET CLOCK) interception */
36 static int handle_set_clock(struct kvm_vcpu *vcpu)
37 {
38         int rc;
39         ar_t ar;
40         u64 op2, val;
41
42         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
43                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
44
45         op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
46         if (op2 & 7)    /* Operand must be on a doubleword boundary */
47                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48         rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
49         if (rc)
50                 return kvm_s390_inject_prog_cond(vcpu, rc);
51
52         VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
53         kvm_s390_set_tod_clock(vcpu->kvm, val);
54
55         kvm_s390_set_psw_cc(vcpu, 0);
56         return 0;
57 }
58
59 static int handle_set_prefix(struct kvm_vcpu *vcpu)
60 {
61         u64 operand2;
62         u32 address;
63         int rc;
64         ar_t ar;
65
66         vcpu->stat.instruction_spx++;
67
68         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
69                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
70
71         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
72
73         /* must be word boundary */
74         if (operand2 & 3)
75                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
76
77         /* get the value */
78         rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
79         if (rc)
80                 return kvm_s390_inject_prog_cond(vcpu, rc);
81
82         address &= 0x7fffe000u;
83
84         /*
85          * Make sure the new value is valid memory. We only need to check the
86          * first page, since address is 8k aligned and memory pieces are always
87          * at least 1MB aligned and have at least a size of 1MB.
88          */
89         if (kvm_is_error_gpa(vcpu->kvm, address))
90                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
91
92         kvm_s390_set_prefix(vcpu, address);
93         trace_kvm_s390_handle_prefix(vcpu, 1, address);
94         return 0;
95 }
96
97 static int handle_store_prefix(struct kvm_vcpu *vcpu)
98 {
99         u64 operand2;
100         u32 address;
101         int rc;
102         ar_t ar;
103
104         vcpu->stat.instruction_stpx++;
105
106         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
107                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
108
109         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
110
111         /* must be word boundary */
112         if (operand2 & 3)
113                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
114
115         address = kvm_s390_get_prefix(vcpu);
116
117         /* get the value */
118         rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
119         if (rc)
120                 return kvm_s390_inject_prog_cond(vcpu, rc);
121
122         VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
123         trace_kvm_s390_handle_prefix(vcpu, 0, address);
124         return 0;
125 }
126
127 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
128 {
129         u16 vcpu_id = vcpu->vcpu_id;
130         u64 ga;
131         int rc;
132         ar_t ar;
133
134         vcpu->stat.instruction_stap++;
135
136         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
137                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
138
139         ga = kvm_s390_get_base_disp_s(vcpu, &ar);
140
141         if (ga & 1)
142                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
143
144         rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
145         if (rc)
146                 return kvm_s390_inject_prog_cond(vcpu, rc);
147
148         VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
149         trace_kvm_s390_handle_stap(vcpu, ga);
150         return 0;
151 }
152
153 static int __skey_check_enable(struct kvm_vcpu *vcpu)
154 {
155         int rc = 0;
156
157         trace_kvm_s390_skey_related_inst(vcpu);
158         if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
159                 return rc;
160
161         rc = s390_enable_skey();
162         VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
163         if (!rc)
164                 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
165         return rc;
166 }
167
168 static int try_handle_skey(struct kvm_vcpu *vcpu)
169 {
170         int rc;
171
172         vcpu->stat.instruction_storage_key++;
173         rc = __skey_check_enable(vcpu);
174         if (rc)
175                 return rc;
176         if (sclp.has_skey) {
177                 /* with storage-key facility, SIE interprets it for us */
178                 kvm_s390_retry_instr(vcpu);
179                 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
180                 return -EAGAIN;
181         }
182         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
183                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
184         return 0;
185 }
186
187 static int handle_iske(struct kvm_vcpu *vcpu)
188 {
189         unsigned long addr;
190         unsigned char key;
191         int reg1, reg2;
192         int rc;
193
194         rc = try_handle_skey(vcpu);
195         if (rc)
196                 return rc != -EAGAIN ? rc : 0;
197
198         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
199
200         addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
201         addr = kvm_s390_logical_to_effective(vcpu, addr);
202         addr = kvm_s390_real_to_abs(vcpu, addr);
203         addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
204         if (kvm_is_error_hva(addr))
205                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
206
207         down_read(&current->mm->mmap_sem);
208         rc = get_guest_storage_key(current->mm, addr, &key);
209         up_read(&current->mm->mmap_sem);
210         if (rc)
211                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
212         vcpu->run->s.regs.gprs[reg1] &= ~0xff;
213         vcpu->run->s.regs.gprs[reg1] |= key;
214         return 0;
215 }
216
217 static int handle_rrbe(struct kvm_vcpu *vcpu)
218 {
219         unsigned long addr;
220         int reg1, reg2;
221         int rc;
222
223         rc = try_handle_skey(vcpu);
224         if (rc)
225                 return rc != -EAGAIN ? rc : 0;
226
227         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
228
229         addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
230         addr = kvm_s390_logical_to_effective(vcpu, addr);
231         addr = kvm_s390_real_to_abs(vcpu, addr);
232         addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
233         if (kvm_is_error_hva(addr))
234                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
235
236         down_read(&current->mm->mmap_sem);
237         rc = reset_guest_reference_bit(current->mm, addr);
238         up_read(&current->mm->mmap_sem);
239         if (rc < 0)
240                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
241
242         kvm_s390_set_psw_cc(vcpu, rc);
243         return 0;
244 }
245
246 #define SSKE_NQ 0x8
247 #define SSKE_MR 0x4
248 #define SSKE_MC 0x2
249 #define SSKE_MB 0x1
250 static int handle_sske(struct kvm_vcpu *vcpu)
251 {
252         unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
253         unsigned long start, end;
254         unsigned char key, oldkey;
255         int reg1, reg2;
256         int rc;
257
258         rc = try_handle_skey(vcpu);
259         if (rc)
260                 return rc != -EAGAIN ? rc : 0;
261
262         if (!test_kvm_facility(vcpu->kvm, 8))
263                 m3 &= ~SSKE_MB;
264         if (!test_kvm_facility(vcpu->kvm, 10))
265                 m3 &= ~(SSKE_MC | SSKE_MR);
266         if (!test_kvm_facility(vcpu->kvm, 14))
267                 m3 &= ~SSKE_NQ;
268
269         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
270
271         key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
272         start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
273         start = kvm_s390_logical_to_effective(vcpu, start);
274         if (m3 & SSKE_MB) {
275                 /* start already designates an absolute address */
276                 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
277         } else {
278                 start = kvm_s390_real_to_abs(vcpu, start);
279                 end = start + PAGE_SIZE;
280         }
281
282         while (start != end) {
283                 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
284
285                 if (kvm_is_error_hva(addr))
286                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
287
288                 down_read(&current->mm->mmap_sem);
289                 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
290                                                 m3 & SSKE_NQ, m3 & SSKE_MR,
291                                                 m3 & SSKE_MC);
292                 up_read(&current->mm->mmap_sem);
293                 if (rc < 0)
294                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
295                 start += PAGE_SIZE;
296         };
297
298         if (m3 & (SSKE_MC | SSKE_MR)) {
299                 if (m3 & SSKE_MB) {
300                         /* skey in reg1 is unpredictable */
301                         kvm_s390_set_psw_cc(vcpu, 3);
302                 } else {
303                         kvm_s390_set_psw_cc(vcpu, rc);
304                         vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
305                         vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
306                 }
307         }
308         if (m3 & SSKE_MB) {
309                 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
310                         vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
311                 else
312                         vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
313                 end = kvm_s390_logical_to_effective(vcpu, end);
314                 vcpu->run->s.regs.gprs[reg2] |= end;
315         }
316         return 0;
317 }
318
319 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
320 {
321         vcpu->stat.instruction_ipte_interlock++;
322         if (psw_bits(vcpu->arch.sie_block->gpsw).p)
323                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
324         wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
325         kvm_s390_retry_instr(vcpu);
326         VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
327         return 0;
328 }
329
330 static int handle_test_block(struct kvm_vcpu *vcpu)
331 {
332         gpa_t addr;
333         int reg2;
334
335         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
336                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
337
338         kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
339         addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
340         addr = kvm_s390_logical_to_effective(vcpu, addr);
341         if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
342                 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
343         addr = kvm_s390_real_to_abs(vcpu, addr);
344
345         if (kvm_is_error_gpa(vcpu->kvm, addr))
346                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
347         /*
348          * We don't expect errors on modern systems, and do not care
349          * about storage keys (yet), so let's just clear the page.
350          */
351         if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
352                 return -EFAULT;
353         kvm_s390_set_psw_cc(vcpu, 0);
354         vcpu->run->s.regs.gprs[0] = 0;
355         return 0;
356 }
357
358 static int handle_tpi(struct kvm_vcpu *vcpu)
359 {
360         struct kvm_s390_interrupt_info *inti;
361         unsigned long len;
362         u32 tpi_data[3];
363         int rc;
364         u64 addr;
365         ar_t ar;
366
367         addr = kvm_s390_get_base_disp_s(vcpu, &ar);
368         if (addr & 3)
369                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
370
371         inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
372         if (!inti) {
373                 kvm_s390_set_psw_cc(vcpu, 0);
374                 return 0;
375         }
376
377         tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
378         tpi_data[1] = inti->io.io_int_parm;
379         tpi_data[2] = inti->io.io_int_word;
380         if (addr) {
381                 /*
382                  * Store the two-word I/O interruption code into the
383                  * provided area.
384                  */
385                 len = sizeof(tpi_data) - 4;
386                 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
387                 if (rc) {
388                         rc = kvm_s390_inject_prog_cond(vcpu, rc);
389                         goto reinject_interrupt;
390                 }
391         } else {
392                 /*
393                  * Store the three-word I/O interruption code into
394                  * the appropriate lowcore area.
395                  */
396                 len = sizeof(tpi_data);
397                 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
398                         /* failed writes to the low core are not recoverable */
399                         rc = -EFAULT;
400                         goto reinject_interrupt;
401                 }
402         }
403
404         /* irq was successfully handed to the guest */
405         kfree(inti);
406         kvm_s390_set_psw_cc(vcpu, 1);
407         return 0;
408 reinject_interrupt:
409         /*
410          * If we encounter a problem storing the interruption code, the
411          * instruction is suppressed from the guest's view: reinject the
412          * interrupt.
413          */
414         if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
415                 kfree(inti);
416                 rc = -EFAULT;
417         }
418         /* don't set the cc, a pgm irq was injected or we drop to user space */
419         return rc ? -EFAULT : 0;
420 }
421
422 static int handle_tsch(struct kvm_vcpu *vcpu)
423 {
424         struct kvm_s390_interrupt_info *inti = NULL;
425         const u64 isc_mask = 0xffUL << 24; /* all iscs set */
426
427         /* a valid schid has at least one bit set */
428         if (vcpu->run->s.regs.gprs[1])
429                 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
430                                            vcpu->run->s.regs.gprs[1]);
431
432         /*
433          * Prepare exit to userspace.
434          * We indicate whether we dequeued a pending I/O interrupt
435          * so that userspace can re-inject it if the instruction gets
436          * a program check. While this may re-order the pending I/O
437          * interrupts, this is no problem since the priority is kept
438          * intact.
439          */
440         vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
441         vcpu->run->s390_tsch.dequeued = !!inti;
442         if (inti) {
443                 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
444                 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
445                 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
446                 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
447         }
448         vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
449         kfree(inti);
450         return -EREMOTE;
451 }
452
453 static int handle_io_inst(struct kvm_vcpu *vcpu)
454 {
455         VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
456
457         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
458                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
459
460         if (vcpu->kvm->arch.css_support) {
461                 /*
462                  * Most I/O instructions will be handled by userspace.
463                  * Exceptions are tpi and the interrupt portion of tsch.
464                  */
465                 if (vcpu->arch.sie_block->ipa == 0xb236)
466                         return handle_tpi(vcpu);
467                 if (vcpu->arch.sie_block->ipa == 0xb235)
468                         return handle_tsch(vcpu);
469                 /* Handle in userspace. */
470                 return -EOPNOTSUPP;
471         } else {
472                 /*
473                  * Set condition code 3 to stop the guest from issuing channel
474                  * I/O instructions.
475                  */
476                 kvm_s390_set_psw_cc(vcpu, 3);
477                 return 0;
478         }
479 }
480
481 static int handle_stfl(struct kvm_vcpu *vcpu)
482 {
483         int rc;
484         unsigned int fac;
485
486         vcpu->stat.instruction_stfl++;
487
488         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
489                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
490
491         /*
492          * We need to shift the lower 32 facility bits (bit 0-31) from a u64
493          * into a u32 memory representation. They will remain bits 0-31.
494          */
495         fac = *vcpu->kvm->arch.model.fac_list >> 32;
496         rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
497                             &fac, sizeof(fac));
498         if (rc)
499                 return rc;
500         VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
501         trace_kvm_s390_handle_stfl(vcpu, fac);
502         return 0;
503 }
504
505 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
506 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
507 #define PSW_ADDR_24 0x0000000000ffffffUL
508 #define PSW_ADDR_31 0x000000007fffffffUL
509
510 int is_valid_psw(psw_t *psw)
511 {
512         if (psw->mask & PSW_MASK_UNASSIGNED)
513                 return 0;
514         if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
515                 if (psw->addr & ~PSW_ADDR_31)
516                         return 0;
517         }
518         if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
519                 return 0;
520         if ((psw->mask & PSW_MASK_ADDR_MODE) ==  PSW_MASK_EA)
521                 return 0;
522         if (psw->addr & 1)
523                 return 0;
524         return 1;
525 }
526
527 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
528 {
529         psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
530         psw_compat_t new_psw;
531         u64 addr;
532         int rc;
533         ar_t ar;
534
535         if (gpsw->mask & PSW_MASK_PSTATE)
536                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
537
538         addr = kvm_s390_get_base_disp_s(vcpu, &ar);
539         if (addr & 7)
540                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
541
542         rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
543         if (rc)
544                 return kvm_s390_inject_prog_cond(vcpu, rc);
545         if (!(new_psw.mask & PSW32_MASK_BASE))
546                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
547         gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
548         gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
549         gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
550         if (!is_valid_psw(gpsw))
551                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
552         return 0;
553 }
554
555 static int handle_lpswe(struct kvm_vcpu *vcpu)
556 {
557         psw_t new_psw;
558         u64 addr;
559         int rc;
560         ar_t ar;
561
562         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
563                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
564
565         addr = kvm_s390_get_base_disp_s(vcpu, &ar);
566         if (addr & 7)
567                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
568         rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
569         if (rc)
570                 return kvm_s390_inject_prog_cond(vcpu, rc);
571         vcpu->arch.sie_block->gpsw = new_psw;
572         if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
573                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
574         return 0;
575 }
576
577 static int handle_stidp(struct kvm_vcpu *vcpu)
578 {
579         u64 stidp_data = vcpu->kvm->arch.model.cpuid;
580         u64 operand2;
581         int rc;
582         ar_t ar;
583
584         vcpu->stat.instruction_stidp++;
585
586         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
587                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
588
589         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
590
591         if (operand2 & 7)
592                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
593
594         rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
595         if (rc)
596                 return kvm_s390_inject_prog_cond(vcpu, rc);
597
598         VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
599         return 0;
600 }
601
602 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
603 {
604         int cpus = 0;
605         int n;
606
607         cpus = atomic_read(&vcpu->kvm->online_vcpus);
608
609         /* deal with other level 3 hypervisors */
610         if (stsi(mem, 3, 2, 2))
611                 mem->count = 0;
612         if (mem->count < 8)
613                 mem->count++;
614         for (n = mem->count - 1; n > 0 ; n--)
615                 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
616
617         memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
618         mem->vm[0].cpus_total = cpus;
619         mem->vm[0].cpus_configured = cpus;
620         mem->vm[0].cpus_standby = 0;
621         mem->vm[0].cpus_reserved = 0;
622         mem->vm[0].caf = 1000;
623         memcpy(mem->vm[0].name, "KVMguest", 8);
624         ASCEBC(mem->vm[0].name, 8);
625         memcpy(mem->vm[0].cpi, "KVM/Linux       ", 16);
626         ASCEBC(mem->vm[0].cpi, 16);
627 }
628
629 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
630                                  u8 fc, u8 sel1, u16 sel2)
631 {
632         vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
633         vcpu->run->s390_stsi.addr = addr;
634         vcpu->run->s390_stsi.ar = ar;
635         vcpu->run->s390_stsi.fc = fc;
636         vcpu->run->s390_stsi.sel1 = sel1;
637         vcpu->run->s390_stsi.sel2 = sel2;
638 }
639
640 static int handle_stsi(struct kvm_vcpu *vcpu)
641 {
642         int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
643         int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
644         int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
645         unsigned long mem = 0;
646         u64 operand2;
647         int rc = 0;
648         ar_t ar;
649
650         vcpu->stat.instruction_stsi++;
651         VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
652
653         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
654                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
655
656         if (fc > 3) {
657                 kvm_s390_set_psw_cc(vcpu, 3);
658                 return 0;
659         }
660
661         if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
662             || vcpu->run->s.regs.gprs[1] & 0xffff0000)
663                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
664
665         if (fc == 0) {
666                 vcpu->run->s.regs.gprs[0] = 3 << 28;
667                 kvm_s390_set_psw_cc(vcpu, 0);
668                 return 0;
669         }
670
671         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
672
673         if (operand2 & 0xfff)
674                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
675
676         switch (fc) {
677         case 1: /* same handling for 1 and 2 */
678         case 2:
679                 mem = get_zeroed_page(GFP_KERNEL);
680                 if (!mem)
681                         goto out_no_data;
682                 if (stsi((void *) mem, fc, sel1, sel2))
683                         goto out_no_data;
684                 break;
685         case 3:
686                 if (sel1 != 2 || sel2 != 2)
687                         goto out_no_data;
688                 mem = get_zeroed_page(GFP_KERNEL);
689                 if (!mem)
690                         goto out_no_data;
691                 handle_stsi_3_2_2(vcpu, (void *) mem);
692                 break;
693         }
694
695         rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
696         if (rc) {
697                 rc = kvm_s390_inject_prog_cond(vcpu, rc);
698                 goto out;
699         }
700         if (vcpu->kvm->arch.user_stsi) {
701                 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
702                 rc = -EREMOTE;
703         }
704         trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
705         free_page(mem);
706         kvm_s390_set_psw_cc(vcpu, 0);
707         vcpu->run->s.regs.gprs[0] = 0;
708         return rc;
709 out_no_data:
710         kvm_s390_set_psw_cc(vcpu, 3);
711 out:
712         free_page(mem);
713         return rc;
714 }
715
716 static const intercept_handler_t b2_handlers[256] = {
717         [0x02] = handle_stidp,
718         [0x04] = handle_set_clock,
719         [0x10] = handle_set_prefix,
720         [0x11] = handle_store_prefix,
721         [0x12] = handle_store_cpu_address,
722         [0x21] = handle_ipte_interlock,
723         [0x29] = handle_iske,
724         [0x2a] = handle_rrbe,
725         [0x2b] = handle_sske,
726         [0x2c] = handle_test_block,
727         [0x30] = handle_io_inst,
728         [0x31] = handle_io_inst,
729         [0x32] = handle_io_inst,
730         [0x33] = handle_io_inst,
731         [0x34] = handle_io_inst,
732         [0x35] = handle_io_inst,
733         [0x36] = handle_io_inst,
734         [0x37] = handle_io_inst,
735         [0x38] = handle_io_inst,
736         [0x39] = handle_io_inst,
737         [0x3a] = handle_io_inst,
738         [0x3b] = handle_io_inst,
739         [0x3c] = handle_io_inst,
740         [0x50] = handle_ipte_interlock,
741         [0x5f] = handle_io_inst,
742         [0x74] = handle_io_inst,
743         [0x76] = handle_io_inst,
744         [0x7d] = handle_stsi,
745         [0xb1] = handle_stfl,
746         [0xb2] = handle_lpswe,
747 };
748
749 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
750 {
751         intercept_handler_t handler;
752
753         /*
754          * A lot of B2 instructions are priviledged. Here we check for
755          * the privileged ones, that we can handle in the kernel.
756          * Anything else goes to userspace.
757          */
758         handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
759         if (handler)
760                 return handler(vcpu);
761
762         return -EOPNOTSUPP;
763 }
764
765 static int handle_epsw(struct kvm_vcpu *vcpu)
766 {
767         int reg1, reg2;
768
769         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
770
771         /* This basically extracts the mask half of the psw. */
772         vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
773         vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
774         if (reg2) {
775                 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
776                 vcpu->run->s.regs.gprs[reg2] |=
777                         vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
778         }
779         return 0;
780 }
781
782 #define PFMF_RESERVED   0xfffc0101UL
783 #define PFMF_SK         0x00020000UL
784 #define PFMF_CF         0x00010000UL
785 #define PFMF_UI         0x00008000UL
786 #define PFMF_FSC        0x00007000UL
787 #define PFMF_NQ         0x00000800UL
788 #define PFMF_MR         0x00000400UL
789 #define PFMF_MC         0x00000200UL
790 #define PFMF_KEY        0x000000feUL
791
792 static int handle_pfmf(struct kvm_vcpu *vcpu)
793 {
794         bool mr = false, mc = false, nq;
795         int reg1, reg2;
796         unsigned long start, end;
797         unsigned char key;
798
799         vcpu->stat.instruction_pfmf++;
800
801         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
802
803         if (!test_kvm_facility(vcpu->kvm, 8))
804                 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
805
806         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
807                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
808
809         if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
810                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
811
812         /* Only provide non-quiescing support if enabled for the guest */
813         if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
814             !test_kvm_facility(vcpu->kvm, 14))
815                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
816
817         /* Only provide conditional-SSKE support if enabled for the guest */
818         if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
819             test_kvm_facility(vcpu->kvm, 10)) {
820                 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
821                 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
822         }
823
824         nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
825         key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
826         start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
827         start = kvm_s390_logical_to_effective(vcpu, start);
828
829         if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
830                 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
831                         return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
832         }
833
834         switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
835         case 0x00000000:
836                 /* only 4k frames specify a real address */
837                 start = kvm_s390_real_to_abs(vcpu, start);
838                 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
839                 break;
840         case 0x00001000:
841                 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
842                 break;
843         case 0x00002000:
844                 /* only support 2G frame size if EDAT2 is available and we are
845                    not in 24-bit addressing mode */
846                 if (!test_kvm_facility(vcpu->kvm, 78) ||
847                     psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
848                         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
849                 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
850                 break;
851         default:
852                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
853         }
854
855         while (start != end) {
856                 unsigned long useraddr;
857
858                 /* Translate guest address to host address */
859                 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
860                 if (kvm_is_error_hva(useraddr))
861                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
862
863                 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
864                         if (clear_user((void __user *)useraddr, PAGE_SIZE))
865                                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
866                 }
867
868                 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
869                         int rc = __skey_check_enable(vcpu);
870
871                         if (rc)
872                                 return rc;
873                         down_read(&current->mm->mmap_sem);
874                         rc = cond_set_guest_storage_key(current->mm, useraddr,
875                                                         key, NULL, nq, mr, mc);
876                         up_read(&current->mm->mmap_sem);
877                         if (rc < 0)
878                                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
879                 }
880
881                 start += PAGE_SIZE;
882         }
883         if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
884                 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
885                         vcpu->run->s.regs.gprs[reg2] = end;
886                 } else {
887                         vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
888                         end = kvm_s390_logical_to_effective(vcpu, end);
889                         vcpu->run->s.regs.gprs[reg2] |= end;
890                 }
891         }
892         return 0;
893 }
894
895 static int handle_essa(struct kvm_vcpu *vcpu)
896 {
897         /* entries expected to be 1FF */
898         int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
899         unsigned long *cbrlo;
900         struct gmap *gmap;
901         int i;
902
903         VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
904         gmap = vcpu->arch.gmap;
905         vcpu->stat.instruction_essa++;
906         if (!vcpu->kvm->arch.use_cmma)
907                 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
908
909         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
910                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
911
912         if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
913                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
914
915         /* Retry the ESSA instruction */
916         kvm_s390_retry_instr(vcpu);
917         vcpu->arch.sie_block->cbrlo &= PAGE_MASK;       /* reset nceo */
918         cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
919         down_read(&gmap->mm->mmap_sem);
920         for (i = 0; i < entries; ++i)
921                 __gmap_zap(gmap, cbrlo[i]);
922         up_read(&gmap->mm->mmap_sem);
923         return 0;
924 }
925
926 static const intercept_handler_t b9_handlers[256] = {
927         [0x8a] = handle_ipte_interlock,
928         [0x8d] = handle_epsw,
929         [0x8e] = handle_ipte_interlock,
930         [0x8f] = handle_ipte_interlock,
931         [0xab] = handle_essa,
932         [0xaf] = handle_pfmf,
933 };
934
935 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
936 {
937         intercept_handler_t handler;
938
939         /* This is handled just as for the B2 instructions. */
940         handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
941         if (handler)
942                 return handler(vcpu);
943
944         return -EOPNOTSUPP;
945 }
946
947 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
948 {
949         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
950         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
951         int reg, rc, nr_regs;
952         u32 ctl_array[16];
953         u64 ga;
954         ar_t ar;
955
956         vcpu->stat.instruction_lctl++;
957
958         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
959                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
960
961         ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
962
963         if (ga & 3)
964                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
965
966         VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
967         trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
968
969         nr_regs = ((reg3 - reg1) & 0xf) + 1;
970         rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
971         if (rc)
972                 return kvm_s390_inject_prog_cond(vcpu, rc);
973         reg = reg1;
974         nr_regs = 0;
975         do {
976                 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
977                 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
978                 if (reg == reg3)
979                         break;
980                 reg = (reg + 1) % 16;
981         } while (1);
982         kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
983         return 0;
984 }
985
986 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
987 {
988         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
989         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
990         int reg, rc, nr_regs;
991         u32 ctl_array[16];
992         u64 ga;
993         ar_t ar;
994
995         vcpu->stat.instruction_stctl++;
996
997         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
998                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
999
1000         ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1001
1002         if (ga & 3)
1003                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1004
1005         VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1006         trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1007
1008         reg = reg1;
1009         nr_regs = 0;
1010         do {
1011                 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1012                 if (reg == reg3)
1013                         break;
1014                 reg = (reg + 1) % 16;
1015         } while (1);
1016         rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1017         return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1018 }
1019
1020 static int handle_lctlg(struct kvm_vcpu *vcpu)
1021 {
1022         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1023         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1024         int reg, rc, nr_regs;
1025         u64 ctl_array[16];
1026         u64 ga;
1027         ar_t ar;
1028
1029         vcpu->stat.instruction_lctlg++;
1030
1031         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1032                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1033
1034         ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1035
1036         if (ga & 7)
1037                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1038
1039         VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1040         trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1041
1042         nr_regs = ((reg3 - reg1) & 0xf) + 1;
1043         rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1044         if (rc)
1045                 return kvm_s390_inject_prog_cond(vcpu, rc);
1046         reg = reg1;
1047         nr_regs = 0;
1048         do {
1049                 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1050                 if (reg == reg3)
1051                         break;
1052                 reg = (reg + 1) % 16;
1053         } while (1);
1054         kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1055         return 0;
1056 }
1057
1058 static int handle_stctg(struct kvm_vcpu *vcpu)
1059 {
1060         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1061         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1062         int reg, rc, nr_regs;
1063         u64 ctl_array[16];
1064         u64 ga;
1065         ar_t ar;
1066
1067         vcpu->stat.instruction_stctg++;
1068
1069         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1070                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1071
1072         ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1073
1074         if (ga & 7)
1075                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1076
1077         VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1078         trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1079
1080         reg = reg1;
1081         nr_regs = 0;
1082         do {
1083                 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1084                 if (reg == reg3)
1085                         break;
1086                 reg = (reg + 1) % 16;
1087         } while (1);
1088         rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1089         return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1090 }
1091
1092 static const intercept_handler_t eb_handlers[256] = {
1093         [0x2f] = handle_lctlg,
1094         [0x25] = handle_stctg,
1095 };
1096
1097 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1098 {
1099         intercept_handler_t handler;
1100
1101         handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1102         if (handler)
1103                 return handler(vcpu);
1104         return -EOPNOTSUPP;
1105 }
1106
1107 static int handle_tprot(struct kvm_vcpu *vcpu)
1108 {
1109         u64 address1, address2;
1110         unsigned long hva, gpa;
1111         int ret = 0, cc = 0;
1112         bool writable;
1113         ar_t ar;
1114
1115         vcpu->stat.instruction_tprot++;
1116
1117         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1118                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1119
1120         kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1121
1122         /* we only handle the Linux memory detection case:
1123          * access key == 0
1124          * everything else goes to userspace. */
1125         if (address2 & 0xf0)
1126                 return -EOPNOTSUPP;
1127         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1128                 ipte_lock(vcpu);
1129         ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
1130         if (ret == PGM_PROTECTION) {
1131                 /* Write protected? Try again with read-only... */
1132                 cc = 1;
1133                 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1134                                               GACC_FETCH);
1135         }
1136         if (ret) {
1137                 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1138                         ret = kvm_s390_inject_program_int(vcpu, ret);
1139                 } else if (ret > 0) {
1140                         /* Translation not available */
1141                         kvm_s390_set_psw_cc(vcpu, 3);
1142                         ret = 0;
1143                 }
1144                 goto out_unlock;
1145         }
1146
1147         hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1148         if (kvm_is_error_hva(hva)) {
1149                 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1150         } else {
1151                 if (!writable)
1152                         cc = 1;         /* Write not permitted ==> read-only */
1153                 kvm_s390_set_psw_cc(vcpu, cc);
1154                 /* Note: CC2 only occurs for storage keys (not supported yet) */
1155         }
1156 out_unlock:
1157         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1158                 ipte_unlock(vcpu);
1159         return ret;
1160 }
1161
1162 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1163 {
1164         /* For e5xx... instructions we only handle TPROT */
1165         if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1166                 return handle_tprot(vcpu);
1167         return -EOPNOTSUPP;
1168 }
1169
1170 static int handle_sckpf(struct kvm_vcpu *vcpu)
1171 {
1172         u32 value;
1173
1174         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1175                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1176
1177         if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1178                 return kvm_s390_inject_program_int(vcpu,
1179                                                    PGM_SPECIFICATION);
1180
1181         value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1182         vcpu->arch.sie_block->todpr = value;
1183
1184         return 0;
1185 }
1186
1187 static const intercept_handler_t x01_handlers[256] = {
1188         [0x07] = handle_sckpf,
1189 };
1190
1191 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1192 {
1193         intercept_handler_t handler;
1194
1195         handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1196         if (handler)
1197                 return handler(vcpu);
1198         return -EOPNOTSUPP;
1199 }