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