GNU Linux-libre 4.19.304-gnu1
[releases.git] / arch / s390 / kvm / priv.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * handling privileged instructions
4  *
5  * Copyright IBM Corp. 2008, 2018
6  *
7  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8  *               Christian Borntraeger <borntraeger@de.ibm.com>
9  */
10
11 #include <linux/kvm.h>
12 #include <linux/gfp.h>
13 #include <linux/errno.h>
14 #include <linux/compat.h>
15 #include <linux/mm_types.h>
16
17 #include <asm/asm-offsets.h>
18 #include <asm/facility.h>
19 #include <asm/current.h>
20 #include <asm/debug.h>
21 #include <asm/ebcdic.h>
22 #include <asm/sysinfo.h>
23 #include <asm/pgtable.h>
24 #include <asm/page-states.h>
25 #include <asm/pgalloc.h>
26 #include <asm/gmap.h>
27 #include <asm/io.h>
28 #include <asm/ptrace.h>
29 #include <asm/sclp.h>
30 #include "gaccess.h"
31 #include "kvm-s390.h"
32 #include "trace.h"
33
34 static int handle_ri(struct kvm_vcpu *vcpu)
35 {
36         vcpu->stat.instruction_ri++;
37
38         if (test_kvm_facility(vcpu->kvm, 64)) {
39                 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
40                 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
41                 kvm_s390_retry_instr(vcpu);
42                 return 0;
43         } else
44                 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
45 }
46
47 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
48 {
49         if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
50                 return handle_ri(vcpu);
51         else
52                 return -EOPNOTSUPP;
53 }
54
55 static int handle_gs(struct kvm_vcpu *vcpu)
56 {
57         vcpu->stat.instruction_gs++;
58
59         if (test_kvm_facility(vcpu->kvm, 133)) {
60                 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
61                 preempt_disable();
62                 __ctl_set_bit(2, 4);
63                 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
64                 restore_gs_cb(current->thread.gs_cb);
65                 preempt_enable();
66                 vcpu->arch.sie_block->ecb |= ECB_GS;
67                 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
68                 vcpu->arch.gs_enabled = 1;
69                 kvm_s390_retry_instr(vcpu);
70                 return 0;
71         } else
72                 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
73 }
74
75 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
76 {
77         int code = vcpu->arch.sie_block->ipb & 0xff;
78
79         if (code == 0x49 || code == 0x4d)
80                 return handle_gs(vcpu);
81         else
82                 return -EOPNOTSUPP;
83 }
84 /* Handle SCK (SET CLOCK) interception */
85 static int handle_set_clock(struct kvm_vcpu *vcpu)
86 {
87         struct kvm_s390_vm_tod_clock gtod = { 0 };
88         int rc;
89         u8 ar;
90         u64 op2;
91
92         vcpu->stat.instruction_sck++;
93
94         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
95                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
96
97         op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
98         if (op2 & 7)    /* Operand must be on a doubleword boundary */
99                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
100         rc = read_guest(vcpu, op2, ar, &gtod.tod, sizeof(gtod.tod));
101         if (rc)
102                 return kvm_s390_inject_prog_cond(vcpu, rc);
103
104         VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
105         kvm_s390_set_tod_clock(vcpu->kvm, &gtod);
106
107         kvm_s390_set_psw_cc(vcpu, 0);
108         return 0;
109 }
110
111 static int handle_set_prefix(struct kvm_vcpu *vcpu)
112 {
113         u64 operand2;
114         u32 address;
115         int rc;
116         u8 ar;
117
118         vcpu->stat.instruction_spx++;
119
120         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
121                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
122
123         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
124
125         /* must be word boundary */
126         if (operand2 & 3)
127                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
128
129         /* get the value */
130         rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
131         if (rc)
132                 return kvm_s390_inject_prog_cond(vcpu, rc);
133
134         address &= 0x7fffe000u;
135
136         /*
137          * Make sure the new value is valid memory. We only need to check the
138          * first page, since address is 8k aligned and memory pieces are always
139          * at least 1MB aligned and have at least a size of 1MB.
140          */
141         if (kvm_is_error_gpa(vcpu->kvm, address))
142                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
143
144         kvm_s390_set_prefix(vcpu, address);
145         trace_kvm_s390_handle_prefix(vcpu, 1, address);
146         return 0;
147 }
148
149 static int handle_store_prefix(struct kvm_vcpu *vcpu)
150 {
151         u64 operand2;
152         u32 address;
153         int rc;
154         u8 ar;
155
156         vcpu->stat.instruction_stpx++;
157
158         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
159                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
160
161         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
162
163         /* must be word boundary */
164         if (operand2 & 3)
165                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
166
167         address = kvm_s390_get_prefix(vcpu);
168
169         /* get the value */
170         rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
171         if (rc)
172                 return kvm_s390_inject_prog_cond(vcpu, rc);
173
174         VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
175         trace_kvm_s390_handle_prefix(vcpu, 0, address);
176         return 0;
177 }
178
179 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
180 {
181         u16 vcpu_id = vcpu->vcpu_id;
182         u64 ga;
183         int rc;
184         u8 ar;
185
186         vcpu->stat.instruction_stap++;
187
188         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
189                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
190
191         ga = kvm_s390_get_base_disp_s(vcpu, &ar);
192
193         if (ga & 1)
194                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
195
196         rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
197         if (rc)
198                 return kvm_s390_inject_prog_cond(vcpu, rc);
199
200         VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
201         trace_kvm_s390_handle_stap(vcpu, ga);
202         return 0;
203 }
204
205 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
206 {
207         int rc;
208
209         trace_kvm_s390_skey_related_inst(vcpu);
210         /* Already enabled? */
211         if (vcpu->arch.skey_enabled)
212                 return 0;
213
214         rc = s390_enable_skey();
215         VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
216         if (rc)
217                 return rc;
218
219         if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
220                 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
221         if (!vcpu->kvm->arch.use_skf)
222                 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
223         else
224                 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
225         vcpu->arch.skey_enabled = true;
226         return 0;
227 }
228
229 static int try_handle_skey(struct kvm_vcpu *vcpu)
230 {
231         int rc;
232
233         rc = kvm_s390_skey_check_enable(vcpu);
234         if (rc)
235                 return rc;
236         if (vcpu->kvm->arch.use_skf) {
237                 /* with storage-key facility, SIE interprets it for us */
238                 kvm_s390_retry_instr(vcpu);
239                 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
240                 return -EAGAIN;
241         }
242         return 0;
243 }
244
245 static int handle_iske(struct kvm_vcpu *vcpu)
246 {
247         unsigned long gaddr, vmaddr;
248         unsigned char key;
249         int reg1, reg2;
250         bool unlocked;
251         int rc;
252
253         vcpu->stat.instruction_iske++;
254
255         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
256                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
257
258         rc = try_handle_skey(vcpu);
259         if (rc)
260                 return rc != -EAGAIN ? rc : 0;
261
262         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
263
264         gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
265         gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
266         gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
267         vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
268         if (kvm_is_error_hva(vmaddr))
269                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
270 retry:
271         unlocked = false;
272         down_read(&current->mm->mmap_sem);
273         rc = get_guest_storage_key(current->mm, vmaddr, &key);
274
275         if (rc) {
276                 rc = fixup_user_fault(current, current->mm, vmaddr,
277                                       FAULT_FLAG_WRITE, &unlocked);
278                 if (!rc) {
279                         up_read(&current->mm->mmap_sem);
280                         goto retry;
281                 }
282         }
283         up_read(&current->mm->mmap_sem);
284         if (rc == -EFAULT)
285                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
286         if (rc < 0)
287                 return rc;
288         vcpu->run->s.regs.gprs[reg1] &= ~0xff;
289         vcpu->run->s.regs.gprs[reg1] |= key;
290         return 0;
291 }
292
293 static int handle_rrbe(struct kvm_vcpu *vcpu)
294 {
295         unsigned long vmaddr, gaddr;
296         int reg1, reg2;
297         bool unlocked;
298         int rc;
299
300         vcpu->stat.instruction_rrbe++;
301
302         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
303                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
304
305         rc = try_handle_skey(vcpu);
306         if (rc)
307                 return rc != -EAGAIN ? rc : 0;
308
309         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
310
311         gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
312         gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
313         gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
314         vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
315         if (kvm_is_error_hva(vmaddr))
316                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
317 retry:
318         unlocked = false;
319         down_read(&current->mm->mmap_sem);
320         rc = reset_guest_reference_bit(current->mm, vmaddr);
321         if (rc < 0) {
322                 rc = fixup_user_fault(current, current->mm, vmaddr,
323                                       FAULT_FLAG_WRITE, &unlocked);
324                 if (!rc) {
325                         up_read(&current->mm->mmap_sem);
326                         goto retry;
327                 }
328         }
329         up_read(&current->mm->mmap_sem);
330         if (rc == -EFAULT)
331                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
332         if (rc < 0)
333                 return rc;
334         kvm_s390_set_psw_cc(vcpu, rc);
335         return 0;
336 }
337
338 #define SSKE_NQ 0x8
339 #define SSKE_MR 0x4
340 #define SSKE_MC 0x2
341 #define SSKE_MB 0x1
342 static int handle_sske(struct kvm_vcpu *vcpu)
343 {
344         unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
345         unsigned long start, end;
346         unsigned char key, oldkey;
347         int reg1, reg2;
348         bool unlocked;
349         int rc;
350
351         vcpu->stat.instruction_sske++;
352
353         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
354                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
355
356         rc = try_handle_skey(vcpu);
357         if (rc)
358                 return rc != -EAGAIN ? rc : 0;
359
360         if (!test_kvm_facility(vcpu->kvm, 8))
361                 m3 &= ~SSKE_MB;
362         if (!test_kvm_facility(vcpu->kvm, 10))
363                 m3 &= ~(SSKE_MC | SSKE_MR);
364         if (!test_kvm_facility(vcpu->kvm, 14))
365                 m3 &= ~SSKE_NQ;
366
367         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
368
369         key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
370         start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
371         start = kvm_s390_logical_to_effective(vcpu, start);
372         if (m3 & SSKE_MB) {
373                 /* start already designates an absolute address */
374                 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
375         } else {
376                 start = kvm_s390_real_to_abs(vcpu, start);
377                 end = start + PAGE_SIZE;
378         }
379
380         while (start != end) {
381                 unsigned long vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
382                 unlocked = false;
383
384                 if (kvm_is_error_hva(vmaddr))
385                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
386
387                 down_read(&current->mm->mmap_sem);
388                 rc = cond_set_guest_storage_key(current->mm, vmaddr, key, &oldkey,
389                                                 m3 & SSKE_NQ, m3 & SSKE_MR,
390                                                 m3 & SSKE_MC);
391
392                 if (rc < 0) {
393                         rc = fixup_user_fault(current, current->mm, vmaddr,
394                                               FAULT_FLAG_WRITE, &unlocked);
395                         rc = !rc ? -EAGAIN : rc;
396                 }
397                 up_read(&current->mm->mmap_sem);
398                 if (rc == -EFAULT)
399                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
400                 if (rc == -EAGAIN)
401                         continue;
402                 if (rc < 0)
403                         return rc;
404                 start += PAGE_SIZE;
405         }
406
407         if (m3 & (SSKE_MC | SSKE_MR)) {
408                 if (m3 & SSKE_MB) {
409                         /* skey in reg1 is unpredictable */
410                         kvm_s390_set_psw_cc(vcpu, 3);
411                 } else {
412                         kvm_s390_set_psw_cc(vcpu, rc);
413                         vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
414                         vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
415                 }
416         }
417         if (m3 & SSKE_MB) {
418                 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
419                         vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
420                 else
421                         vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
422                 end = kvm_s390_logical_to_effective(vcpu, end);
423                 vcpu->run->s.regs.gprs[reg2] |= end;
424         }
425         return 0;
426 }
427
428 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
429 {
430         vcpu->stat.instruction_ipte_interlock++;
431         if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
432                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
433         wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
434         kvm_s390_retry_instr(vcpu);
435         VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
436         return 0;
437 }
438
439 static int handle_test_block(struct kvm_vcpu *vcpu)
440 {
441         gpa_t addr;
442         int reg2;
443
444         vcpu->stat.instruction_tb++;
445
446         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
447                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
448
449         kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
450         addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
451         addr = kvm_s390_logical_to_effective(vcpu, addr);
452         if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
453                 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
454         addr = kvm_s390_real_to_abs(vcpu, addr);
455
456         if (kvm_is_error_gpa(vcpu->kvm, addr))
457                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
458         /*
459          * We don't expect errors on modern systems, and do not care
460          * about storage keys (yet), so let's just clear the page.
461          */
462         if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
463                 return -EFAULT;
464         kvm_s390_set_psw_cc(vcpu, 0);
465         vcpu->run->s.regs.gprs[0] = 0;
466         return 0;
467 }
468
469 static int handle_tpi(struct kvm_vcpu *vcpu)
470 {
471         struct kvm_s390_interrupt_info *inti;
472         unsigned long len;
473         u32 tpi_data[3];
474         int rc;
475         u64 addr;
476         u8 ar;
477
478         vcpu->stat.instruction_tpi++;
479
480         addr = kvm_s390_get_base_disp_s(vcpu, &ar);
481         if (addr & 3)
482                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
483
484         inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
485         if (!inti) {
486                 kvm_s390_set_psw_cc(vcpu, 0);
487                 return 0;
488         }
489
490         tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
491         tpi_data[1] = inti->io.io_int_parm;
492         tpi_data[2] = inti->io.io_int_word;
493         if (addr) {
494                 /*
495                  * Store the two-word I/O interruption code into the
496                  * provided area.
497                  */
498                 len = sizeof(tpi_data) - 4;
499                 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
500                 if (rc) {
501                         rc = kvm_s390_inject_prog_cond(vcpu, rc);
502                         goto reinject_interrupt;
503                 }
504         } else {
505                 /*
506                  * Store the three-word I/O interruption code into
507                  * the appropriate lowcore area.
508                  */
509                 len = sizeof(tpi_data);
510                 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
511                         /* failed writes to the low core are not recoverable */
512                         rc = -EFAULT;
513                         goto reinject_interrupt;
514                 }
515         }
516
517         /* irq was successfully handed to the guest */
518         kfree(inti);
519         kvm_s390_set_psw_cc(vcpu, 1);
520         return 0;
521 reinject_interrupt:
522         /*
523          * If we encounter a problem storing the interruption code, the
524          * instruction is suppressed from the guest's view: reinject the
525          * interrupt.
526          */
527         if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
528                 kfree(inti);
529                 rc = -EFAULT;
530         }
531         /* don't set the cc, a pgm irq was injected or we drop to user space */
532         return rc ? -EFAULT : 0;
533 }
534
535 static int handle_tsch(struct kvm_vcpu *vcpu)
536 {
537         struct kvm_s390_interrupt_info *inti = NULL;
538         const u64 isc_mask = 0xffUL << 24; /* all iscs set */
539
540         vcpu->stat.instruction_tsch++;
541
542         /* a valid schid has at least one bit set */
543         if (vcpu->run->s.regs.gprs[1])
544                 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
545                                            vcpu->run->s.regs.gprs[1]);
546
547         /*
548          * Prepare exit to userspace.
549          * We indicate whether we dequeued a pending I/O interrupt
550          * so that userspace can re-inject it if the instruction gets
551          * a program check. While this may re-order the pending I/O
552          * interrupts, this is no problem since the priority is kept
553          * intact.
554          */
555         vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
556         vcpu->run->s390_tsch.dequeued = !!inti;
557         if (inti) {
558                 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
559                 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
560                 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
561                 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
562         }
563         vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
564         kfree(inti);
565         return -EREMOTE;
566 }
567
568 static int handle_io_inst(struct kvm_vcpu *vcpu)
569 {
570         VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
571
572         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
573                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
574
575         if (vcpu->kvm->arch.css_support) {
576                 /*
577                  * Most I/O instructions will be handled by userspace.
578                  * Exceptions are tpi and the interrupt portion of tsch.
579                  */
580                 if (vcpu->arch.sie_block->ipa == 0xb236)
581                         return handle_tpi(vcpu);
582                 if (vcpu->arch.sie_block->ipa == 0xb235)
583                         return handle_tsch(vcpu);
584                 /* Handle in userspace. */
585                 vcpu->stat.instruction_io_other++;
586                 return -EOPNOTSUPP;
587         } else {
588                 /*
589                  * Set condition code 3 to stop the guest from issuing channel
590                  * I/O instructions.
591                  */
592                 kvm_s390_set_psw_cc(vcpu, 3);
593                 return 0;
594         }
595 }
596
597 static int handle_stfl(struct kvm_vcpu *vcpu)
598 {
599         int rc;
600         unsigned int fac;
601
602         vcpu->stat.instruction_stfl++;
603
604         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
605                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
606
607         /*
608          * We need to shift the lower 32 facility bits (bit 0-31) from a u64
609          * into a u32 memory representation. They will remain bits 0-31.
610          */
611         fac = *vcpu->kvm->arch.model.fac_list >> 32;
612         rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
613                             &fac, sizeof(fac));
614         if (rc)
615                 return rc;
616         VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
617         trace_kvm_s390_handle_stfl(vcpu, fac);
618         return 0;
619 }
620
621 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
622 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
623 #define PSW_ADDR_24 0x0000000000ffffffUL
624 #define PSW_ADDR_31 0x000000007fffffffUL
625
626 int is_valid_psw(psw_t *psw)
627 {
628         if (psw->mask & PSW_MASK_UNASSIGNED)
629                 return 0;
630         if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
631                 if (psw->addr & ~PSW_ADDR_31)
632                         return 0;
633         }
634         if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
635                 return 0;
636         if ((psw->mask & PSW_MASK_ADDR_MODE) ==  PSW_MASK_EA)
637                 return 0;
638         if (psw->addr & 1)
639                 return 0;
640         return 1;
641 }
642
643 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
644 {
645         psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
646         psw_compat_t new_psw;
647         u64 addr;
648         int rc;
649         u8 ar;
650
651         vcpu->stat.instruction_lpsw++;
652
653         if (gpsw->mask & PSW_MASK_PSTATE)
654                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
655
656         addr = kvm_s390_get_base_disp_s(vcpu, &ar);
657         if (addr & 7)
658                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
659
660         rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
661         if (rc)
662                 return kvm_s390_inject_prog_cond(vcpu, rc);
663         if (!(new_psw.mask & PSW32_MASK_BASE))
664                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
665         gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
666         gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
667         gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
668         if (!is_valid_psw(gpsw))
669                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
670         return 0;
671 }
672
673 static int handle_lpswe(struct kvm_vcpu *vcpu)
674 {
675         psw_t new_psw;
676         u64 addr;
677         int rc;
678         u8 ar;
679
680         vcpu->stat.instruction_lpswe++;
681
682         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
683                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
684
685         addr = kvm_s390_get_base_disp_s(vcpu, &ar);
686         if (addr & 7)
687                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
688         rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
689         if (rc)
690                 return kvm_s390_inject_prog_cond(vcpu, rc);
691         vcpu->arch.sie_block->gpsw = new_psw;
692         if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
693                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
694         return 0;
695 }
696
697 static int handle_stidp(struct kvm_vcpu *vcpu)
698 {
699         u64 stidp_data = vcpu->kvm->arch.model.cpuid;
700         u64 operand2;
701         int rc;
702         u8 ar;
703
704         vcpu->stat.instruction_stidp++;
705
706         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
707                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
708
709         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
710
711         if (operand2 & 7)
712                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
713
714         rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
715         if (rc)
716                 return kvm_s390_inject_prog_cond(vcpu, rc);
717
718         VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
719         return 0;
720 }
721
722 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
723 {
724         int cpus = 0;
725         int n;
726
727         cpus = atomic_read(&vcpu->kvm->online_vcpus);
728
729         /* deal with other level 3 hypervisors */
730         if (stsi(mem, 3, 2, 2))
731                 mem->count = 0;
732         if (mem->count < 8)
733                 mem->count++;
734         for (n = mem->count - 1; n > 0 ; n--)
735                 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
736
737         memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
738         mem->vm[0].cpus_total = cpus;
739         mem->vm[0].cpus_configured = cpus;
740         mem->vm[0].cpus_standby = 0;
741         mem->vm[0].cpus_reserved = 0;
742         mem->vm[0].caf = 1000;
743         memcpy(mem->vm[0].name, "KVMguest", 8);
744         ASCEBC(mem->vm[0].name, 8);
745         memcpy(mem->vm[0].cpi, "KVM/Linux       ", 16);
746         ASCEBC(mem->vm[0].cpi, 16);
747 }
748
749 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
750                                  u8 fc, u8 sel1, u16 sel2)
751 {
752         vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
753         vcpu->run->s390_stsi.addr = addr;
754         vcpu->run->s390_stsi.ar = ar;
755         vcpu->run->s390_stsi.fc = fc;
756         vcpu->run->s390_stsi.sel1 = sel1;
757         vcpu->run->s390_stsi.sel2 = sel2;
758 }
759
760 static int handle_stsi(struct kvm_vcpu *vcpu)
761 {
762         int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
763         int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
764         int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
765         unsigned long mem = 0;
766         u64 operand2;
767         int rc = 0;
768         u8 ar;
769
770         vcpu->stat.instruction_stsi++;
771         VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
772
773         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
774                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
775
776         if (fc > 3) {
777                 kvm_s390_set_psw_cc(vcpu, 3);
778                 return 0;
779         }
780
781         if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
782             || vcpu->run->s.regs.gprs[1] & 0xffff0000)
783                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
784
785         if (fc == 0) {
786                 vcpu->run->s.regs.gprs[0] = 3 << 28;
787                 kvm_s390_set_psw_cc(vcpu, 0);
788                 return 0;
789         }
790
791         operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
792
793         if (operand2 & 0xfff)
794                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
795
796         switch (fc) {
797         case 1: /* same handling for 1 and 2 */
798         case 2:
799                 mem = get_zeroed_page(GFP_KERNEL);
800                 if (!mem)
801                         goto out_no_data;
802                 if (stsi((void *) mem, fc, sel1, sel2))
803                         goto out_no_data;
804                 break;
805         case 3:
806                 if (sel1 != 2 || sel2 != 2)
807                         goto out_no_data;
808                 mem = get_zeroed_page(GFP_KERNEL);
809                 if (!mem)
810                         goto out_no_data;
811                 handle_stsi_3_2_2(vcpu, (void *) mem);
812                 break;
813         }
814
815         rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
816         if (rc) {
817                 rc = kvm_s390_inject_prog_cond(vcpu, rc);
818                 goto out;
819         }
820         if (vcpu->kvm->arch.user_stsi) {
821                 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
822                 rc = -EREMOTE;
823         }
824         trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
825         free_page(mem);
826         kvm_s390_set_psw_cc(vcpu, 0);
827         vcpu->run->s.regs.gprs[0] = 0;
828         return rc;
829 out_no_data:
830         kvm_s390_set_psw_cc(vcpu, 3);
831 out:
832         free_page(mem);
833         return rc;
834 }
835
836 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
837 {
838         switch (vcpu->arch.sie_block->ipa & 0x00ff) {
839         case 0x02:
840                 return handle_stidp(vcpu);
841         case 0x04:
842                 return handle_set_clock(vcpu);
843         case 0x10:
844                 return handle_set_prefix(vcpu);
845         case 0x11:
846                 return handle_store_prefix(vcpu);
847         case 0x12:
848                 return handle_store_cpu_address(vcpu);
849         case 0x14:
850                 return kvm_s390_handle_vsie(vcpu);
851         case 0x21:
852         case 0x50:
853                 return handle_ipte_interlock(vcpu);
854         case 0x29:
855                 return handle_iske(vcpu);
856         case 0x2a:
857                 return handle_rrbe(vcpu);
858         case 0x2b:
859                 return handle_sske(vcpu);
860         case 0x2c:
861                 return handle_test_block(vcpu);
862         case 0x30:
863         case 0x31:
864         case 0x32:
865         case 0x33:
866         case 0x34:
867         case 0x35:
868         case 0x36:
869         case 0x37:
870         case 0x38:
871         case 0x39:
872         case 0x3a:
873         case 0x3b:
874         case 0x3c:
875         case 0x5f:
876         case 0x74:
877         case 0x76:
878                 return handle_io_inst(vcpu);
879         case 0x56:
880                 return handle_sthyi(vcpu);
881         case 0x7d:
882                 return handle_stsi(vcpu);
883         case 0xb1:
884                 return handle_stfl(vcpu);
885         case 0xb2:
886                 return handle_lpswe(vcpu);
887         default:
888                 return -EOPNOTSUPP;
889         }
890 }
891
892 static int handle_epsw(struct kvm_vcpu *vcpu)
893 {
894         int reg1, reg2;
895
896         vcpu->stat.instruction_epsw++;
897
898         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
899
900         /* This basically extracts the mask half of the psw. */
901         vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
902         vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
903         if (reg2) {
904                 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
905                 vcpu->run->s.regs.gprs[reg2] |=
906                         vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
907         }
908         return 0;
909 }
910
911 #define PFMF_RESERVED   0xfffc0101UL
912 #define PFMF_SK         0x00020000UL
913 #define PFMF_CF         0x00010000UL
914 #define PFMF_UI         0x00008000UL
915 #define PFMF_FSC        0x00007000UL
916 #define PFMF_NQ         0x00000800UL
917 #define PFMF_MR         0x00000400UL
918 #define PFMF_MC         0x00000200UL
919 #define PFMF_KEY        0x000000feUL
920
921 static int handle_pfmf(struct kvm_vcpu *vcpu)
922 {
923         bool mr = false, mc = false, nq;
924         int reg1, reg2;
925         unsigned long start, end;
926         unsigned char key;
927
928         vcpu->stat.instruction_pfmf++;
929
930         kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
931
932         if (!test_kvm_facility(vcpu->kvm, 8))
933                 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
934
935         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
936                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
937
938         if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
939                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
940
941         /* Only provide non-quiescing support if enabled for the guest */
942         if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
943             !test_kvm_facility(vcpu->kvm, 14))
944                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
945
946         /* Only provide conditional-SSKE support if enabled for the guest */
947         if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
948             test_kvm_facility(vcpu->kvm, 10)) {
949                 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
950                 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
951         }
952
953         nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
954         key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
955         start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
956         start = kvm_s390_logical_to_effective(vcpu, start);
957
958         if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
959                 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
960                         return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
961         }
962
963         switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
964         case 0x00000000:
965                 /* only 4k frames specify a real address */
966                 start = kvm_s390_real_to_abs(vcpu, start);
967                 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
968                 break;
969         case 0x00001000:
970                 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
971                 break;
972         case 0x00002000:
973                 /* only support 2G frame size if EDAT2 is available and we are
974                    not in 24-bit addressing mode */
975                 if (!test_kvm_facility(vcpu->kvm, 78) ||
976                     psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
977                         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
978                 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
979                 break;
980         default:
981                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
982         }
983
984         while (start != end) {
985                 unsigned long vmaddr;
986                 bool unlocked = false;
987
988                 /* Translate guest address to host address */
989                 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
990                 if (kvm_is_error_hva(vmaddr))
991                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
992
993                 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
994                         if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE))
995                                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
996                 }
997
998                 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
999                         int rc = kvm_s390_skey_check_enable(vcpu);
1000
1001                         if (rc)
1002                                 return rc;
1003                         down_read(&current->mm->mmap_sem);
1004                         rc = cond_set_guest_storage_key(current->mm, vmaddr,
1005                                                         key, NULL, nq, mr, mc);
1006                         if (rc < 0) {
1007                                 rc = fixup_user_fault(current, current->mm, vmaddr,
1008                                                       FAULT_FLAG_WRITE, &unlocked);
1009                                 rc = !rc ? -EAGAIN : rc;
1010                         }
1011                         up_read(&current->mm->mmap_sem);
1012                         if (rc == -EFAULT)
1013                                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1014                         if (rc == -EAGAIN)
1015                                 continue;
1016                         if (rc < 0)
1017                                 return rc;
1018                 }
1019                 start += PAGE_SIZE;
1020         }
1021         if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1022                 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
1023                         vcpu->run->s.regs.gprs[reg2] = end;
1024                 } else {
1025                         vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
1026                         end = kvm_s390_logical_to_effective(vcpu, end);
1027                         vcpu->run->s.regs.gprs[reg2] |= end;
1028                 }
1029         }
1030         return 0;
1031 }
1032
1033 /*
1034  * Must be called with relevant read locks held (kvm->mm->mmap_sem, kvm->srcu)
1035  */
1036 static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
1037 {
1038         int r1, r2, nappended, entries;
1039         unsigned long gfn, hva, res, pgstev, ptev;
1040         unsigned long *cbrlo;
1041
1042         /*
1043          * We don't need to set SD.FPF.SK to 1 here, because if we have a
1044          * machine check here we either handle it or crash
1045          */
1046
1047         kvm_s390_get_regs_rre(vcpu, &r1, &r2);
1048         gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1049         hva = gfn_to_hva(vcpu->kvm, gfn);
1050         entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1051
1052         if (kvm_is_error_hva(hva))
1053                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1054
1055         nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1056         if (nappended < 0) {
1057                 res = orc ? 0x10 : 0;
1058                 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1059                 return 0;
1060         }
1061         res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1062         /*
1063          * Set the block-content state part of the result. 0 means resident, so
1064          * nothing to do if the page is valid. 2 is for preserved pages
1065          * (non-present and non-zero), and 3 for zero pages (non-present and
1066          * zero).
1067          */
1068         if (ptev & _PAGE_INVALID) {
1069                 res |= 2;
1070                 if (pgstev & _PGSTE_GPS_ZERO)
1071                         res |= 1;
1072         }
1073         if (pgstev & _PGSTE_GPS_NODAT)
1074                 res |= 0x20;
1075         vcpu->run->s.regs.gprs[r1] = res;
1076         /*
1077          * It is possible that all the normal 511 slots were full, in which case
1078          * we will now write in the 512th slot, which is reserved for host use.
1079          * In both cases we let the normal essa handling code process all the
1080          * slots, including the reserved one, if needed.
1081          */
1082         if (nappended > 0) {
1083                 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1084                 cbrlo[entries] = gfn << PAGE_SHIFT;
1085         }
1086
1087         if (orc) {
1088                 struct kvm_memory_slot *ms = gfn_to_memslot(vcpu->kvm, gfn);
1089
1090                 /* Increment only if we are really flipping the bit */
1091                 if (ms && !test_and_set_bit(gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
1092                         atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages);
1093         }
1094
1095         return nappended;
1096 }
1097
1098 static int handle_essa(struct kvm_vcpu *vcpu)
1099 {
1100         /* entries expected to be 1FF */
1101         int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1102         unsigned long *cbrlo;
1103         struct gmap *gmap;
1104         int i, orc;
1105
1106         VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
1107         gmap = vcpu->arch.gmap;
1108         vcpu->stat.instruction_essa++;
1109         if (!vcpu->kvm->arch.use_cmma)
1110                 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1111
1112         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1113                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1114         /* Check for invalid operation request code */
1115         orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1116         /* ORCs 0-6 are always valid */
1117         if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1118                                                 : ESSA_SET_STABLE_IF_RESIDENT))
1119                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1120
1121         if (!vcpu->kvm->arch.migration_mode) {
1122                 /*
1123                  * CMMA is enabled in the KVM settings, but is disabled in
1124                  * the SIE block and in the mm_context, and we are not doing
1125                  * a migration. Enable CMMA in the mm_context.
1126                  * Since we need to take a write lock to write to the context
1127                  * to avoid races with storage keys handling, we check if the
1128                  * value really needs to be written to; if the value is
1129                  * already correct, we do nothing and avoid the lock.
1130                  */
1131                 if (vcpu->kvm->mm->context.uses_cmm == 0) {
1132                         down_write(&vcpu->kvm->mm->mmap_sem);
1133                         vcpu->kvm->mm->context.uses_cmm = 1;
1134                         up_write(&vcpu->kvm->mm->mmap_sem);
1135                 }
1136                 /*
1137                  * If we are here, we are supposed to have CMMA enabled in
1138                  * the SIE block. Enabling CMMA works on a per-CPU basis,
1139                  * while the context use_cmma flag is per process.
1140                  * It's possible that the context flag is enabled and the
1141                  * SIE flag is not, so we set the flag always; if it was
1142                  * already set, nothing changes, otherwise we enable it
1143                  * on this CPU too.
1144                  */
1145                 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1146                 /* Retry the ESSA instruction */
1147                 kvm_s390_retry_instr(vcpu);
1148         } else {
1149                 int srcu_idx;
1150
1151                 down_read(&vcpu->kvm->mm->mmap_sem);
1152                 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1153                 i = __do_essa(vcpu, orc);
1154                 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1155                 up_read(&vcpu->kvm->mm->mmap_sem);
1156                 if (i < 0)
1157                         return i;
1158                 /* Account for the possible extra cbrl entry */
1159                 entries += i;
1160         }
1161         vcpu->arch.sie_block->cbrlo &= PAGE_MASK;       /* reset nceo */
1162         cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1163         down_read(&gmap->mm->mmap_sem);
1164         for (i = 0; i < entries; ++i)
1165                 __gmap_zap(gmap, cbrlo[i]);
1166         up_read(&gmap->mm->mmap_sem);
1167         return 0;
1168 }
1169
1170 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1171 {
1172         switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1173         case 0x8a:
1174         case 0x8e:
1175         case 0x8f:
1176                 return handle_ipte_interlock(vcpu);
1177         case 0x8d:
1178                 return handle_epsw(vcpu);
1179         case 0xab:
1180                 return handle_essa(vcpu);
1181         case 0xaf:
1182                 return handle_pfmf(vcpu);
1183         default:
1184                 return -EOPNOTSUPP;
1185         }
1186 }
1187
1188 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1189 {
1190         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1191         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1192         int reg, rc, nr_regs;
1193         u32 ctl_array[16];
1194         u64 ga;
1195         u8 ar;
1196
1197         vcpu->stat.instruction_lctl++;
1198
1199         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1200                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1201
1202         ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1203
1204         if (ga & 3)
1205                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1206
1207         VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1208         trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
1209
1210         nr_regs = ((reg3 - reg1) & 0xf) + 1;
1211         rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1212         if (rc)
1213                 return kvm_s390_inject_prog_cond(vcpu, rc);
1214         reg = reg1;
1215         nr_regs = 0;
1216         do {
1217                 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1218                 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1219                 if (reg == reg3)
1220                         break;
1221                 reg = (reg + 1) % 16;
1222         } while (1);
1223         kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1224         return 0;
1225 }
1226
1227 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1228 {
1229         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1230         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1231         int reg, rc, nr_regs;
1232         u32 ctl_array[16];
1233         u64 ga;
1234         u8 ar;
1235
1236         vcpu->stat.instruction_stctl++;
1237
1238         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1239                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1240
1241         ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1242
1243         if (ga & 3)
1244                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1245
1246         VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1247         trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1248
1249         reg = reg1;
1250         nr_regs = 0;
1251         do {
1252                 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1253                 if (reg == reg3)
1254                         break;
1255                 reg = (reg + 1) % 16;
1256         } while (1);
1257         rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1258         return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1259 }
1260
1261 static int handle_lctlg(struct kvm_vcpu *vcpu)
1262 {
1263         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1264         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1265         int reg, rc, nr_regs;
1266         u64 ctl_array[16];
1267         u64 ga;
1268         u8 ar;
1269
1270         vcpu->stat.instruction_lctlg++;
1271
1272         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1273                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1274
1275         ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1276
1277         if (ga & 7)
1278                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1279
1280         VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1281         trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1282
1283         nr_regs = ((reg3 - reg1) & 0xf) + 1;
1284         rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1285         if (rc)
1286                 return kvm_s390_inject_prog_cond(vcpu, rc);
1287         reg = reg1;
1288         nr_regs = 0;
1289         do {
1290                 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1291                 if (reg == reg3)
1292                         break;
1293                 reg = (reg + 1) % 16;
1294         } while (1);
1295         kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1296         return 0;
1297 }
1298
1299 static int handle_stctg(struct kvm_vcpu *vcpu)
1300 {
1301         int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1302         int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1303         int reg, rc, nr_regs;
1304         u64 ctl_array[16];
1305         u64 ga;
1306         u8 ar;
1307
1308         vcpu->stat.instruction_stctg++;
1309
1310         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1311                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1312
1313         ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1314
1315         if (ga & 7)
1316                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1317
1318         VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1319         trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1320
1321         reg = reg1;
1322         nr_regs = 0;
1323         do {
1324                 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1325                 if (reg == reg3)
1326                         break;
1327                 reg = (reg + 1) % 16;
1328         } while (1);
1329         rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1330         return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1331 }
1332
1333 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1334 {
1335         switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1336         case 0x25:
1337                 return handle_stctg(vcpu);
1338         case 0x2f:
1339                 return handle_lctlg(vcpu);
1340         case 0x60:
1341         case 0x61:
1342         case 0x62:
1343                 return handle_ri(vcpu);
1344         default:
1345                 return -EOPNOTSUPP;
1346         }
1347 }
1348
1349 static int handle_tprot(struct kvm_vcpu *vcpu)
1350 {
1351         u64 address1, address2;
1352         unsigned long hva, gpa;
1353         int ret = 0, cc = 0;
1354         bool writable;
1355         u8 ar;
1356
1357         vcpu->stat.instruction_tprot++;
1358
1359         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1360                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1361
1362         kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1363
1364         /* we only handle the Linux memory detection case:
1365          * access key == 0
1366          * everything else goes to userspace. */
1367         if (address2 & 0xf0)
1368                 return -EOPNOTSUPP;
1369         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1370                 ipte_lock(vcpu);
1371         ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
1372         if (ret == PGM_PROTECTION) {
1373                 /* Write protected? Try again with read-only... */
1374                 cc = 1;
1375                 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1376                                               GACC_FETCH);
1377         }
1378         if (ret) {
1379                 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1380                         ret = kvm_s390_inject_program_int(vcpu, ret);
1381                 } else if (ret > 0) {
1382                         /* Translation not available */
1383                         kvm_s390_set_psw_cc(vcpu, 3);
1384                         ret = 0;
1385                 }
1386                 goto out_unlock;
1387         }
1388
1389         hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1390         if (kvm_is_error_hva(hva)) {
1391                 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1392         } else {
1393                 if (!writable)
1394                         cc = 1;         /* Write not permitted ==> read-only */
1395                 kvm_s390_set_psw_cc(vcpu, cc);
1396                 /* Note: CC2 only occurs for storage keys (not supported yet) */
1397         }
1398 out_unlock:
1399         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1400                 ipte_unlock(vcpu);
1401         return ret;
1402 }
1403
1404 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1405 {
1406         switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1407         case 0x01:
1408                 return handle_tprot(vcpu);
1409         default:
1410                 return -EOPNOTSUPP;
1411         }
1412 }
1413
1414 static int handle_sckpf(struct kvm_vcpu *vcpu)
1415 {
1416         u32 value;
1417
1418         vcpu->stat.instruction_sckpf++;
1419
1420         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1421                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1422
1423         if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1424                 return kvm_s390_inject_program_int(vcpu,
1425                                                    PGM_SPECIFICATION);
1426
1427         value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1428         vcpu->arch.sie_block->todpr = value;
1429
1430         return 0;
1431 }
1432
1433 static int handle_ptff(struct kvm_vcpu *vcpu)
1434 {
1435         vcpu->stat.instruction_ptff++;
1436
1437         /* we don't emulate any control instructions yet */
1438         kvm_s390_set_psw_cc(vcpu, 3);
1439         return 0;
1440 }
1441
1442 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1443 {
1444         switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1445         case 0x04:
1446                 return handle_ptff(vcpu);
1447         case 0x07:
1448                 return handle_sckpf(vcpu);
1449         default:
1450                 return -EOPNOTSUPP;
1451         }
1452 }