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