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