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