1facd9277f38648830165769a1d1e63ad8d99b54
[sfrench/cifs-2.6.git] / arch / s390 / kvm / diag.c
1 /*
2  * handling diagnose instructions
3  *
4  * Copyright IBM Corp. 2008, 2011
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/kvm_host.h>
16 #include <asm/pgalloc.h>
17 #include <asm/virtio-ccw.h>
18 #include "kvm-s390.h"
19 #include "trace.h"
20 #include "trace-s390.h"
21
22 static int diag_release_pages(struct kvm_vcpu *vcpu)
23 {
24         unsigned long start, end;
25         unsigned long prefix  = vcpu->arch.sie_block->prefix;
26
27         start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
28         end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
29
30         if (start & ~PAGE_MASK || end & ~PAGE_MASK || start > end
31             || start < 2 * PAGE_SIZE)
32                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
33
34         VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
35         vcpu->stat.diagnose_10++;
36
37         /* we checked for start > end above */
38         if (end < prefix || start >= prefix + 2 * PAGE_SIZE) {
39                 gmap_discard(start, end, vcpu->arch.gmap);
40         } else {
41                 if (start < prefix)
42                         gmap_discard(start, prefix, vcpu->arch.gmap);
43                 if (end >= prefix)
44                         gmap_discard(prefix + 2 * PAGE_SIZE,
45                                      end, vcpu->arch.gmap);
46         }
47         return 0;
48 }
49
50 static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
51 {
52         VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
53         vcpu->stat.diagnose_44++;
54         kvm_vcpu_on_spin(vcpu);
55         return 0;
56 }
57
58 static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
59 {
60         struct kvm *kvm = vcpu->kvm;
61         struct kvm_vcpu *tcpu;
62         int tid;
63         int i;
64
65         tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
66         vcpu->stat.diagnose_9c++;
67         VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
68
69         if (tid == vcpu->vcpu_id)
70                 return 0;
71
72         kvm_for_each_vcpu(i, tcpu, kvm)
73                 if (tcpu->vcpu_id == tid) {
74                         kvm_vcpu_yield_to(tcpu);
75                         break;
76                 }
77
78         return 0;
79 }
80
81 static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
82 {
83         unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
84         unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
85
86         VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
87         switch (subcode) {
88         case 0:
89         case 1:
90                 page_table_reset_pgste(current->mm, 0, TASK_SIZE);
91                 return -EOPNOTSUPP;
92         case 3:
93                 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
94                 page_table_reset_pgste(current->mm, 0, TASK_SIZE);
95                 break;
96         case 4:
97                 vcpu->run->s390_reset_flags = 0;
98                 page_table_reset_pgste(current->mm, 0, TASK_SIZE);
99                 break;
100         default:
101                 return -EOPNOTSUPP;
102         }
103
104         atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
105         vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
106         vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
107         vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
108         vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
109         VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
110           vcpu->run->s390_reset_flags);
111         trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
112         return -EREMOTE;
113 }
114
115 static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
116 {
117         int ret;
118
119         /* No virtio-ccw notification? Get out quickly. */
120         if (!vcpu->kvm->arch.css_support ||
121             (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
122                 return -EOPNOTSUPP;
123
124         /*
125          * The layout is as follows:
126          * - gpr 2 contains the subchannel id (passed as addr)
127          * - gpr 3 contains the virtqueue index (passed as datamatch)
128          * - gpr 4 contains the index on the bus (optionally)
129          */
130         ret = kvm_io_bus_write_cookie(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
131                                       vcpu->run->s.regs.gprs[2] & 0xffffffff,
132                                       8, &vcpu->run->s.regs.gprs[3],
133                                       vcpu->run->s.regs.gprs[4]);
134
135         /*
136          * Return cookie in gpr 2, but don't overwrite the register if the
137          * diagnose will be handled by userspace.
138          */
139         if (ret != -EOPNOTSUPP)
140                 vcpu->run->s.regs.gprs[2] = ret;
141         /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
142         return ret < 0 ? ret : 0;
143 }
144
145 int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
146 {
147         int code = kvm_s390_get_base_disp_rs(vcpu) & 0xffff;
148
149         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
150                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
151
152         trace_kvm_s390_handle_diag(vcpu, code);
153         switch (code) {
154         case 0x10:
155                 return diag_release_pages(vcpu);
156         case 0x44:
157                 return __diag_time_slice_end(vcpu);
158         case 0x9c:
159                 return __diag_time_slice_end_directed(vcpu);
160         case 0x308:
161                 return __diag_ipl_functions(vcpu);
162         case 0x500:
163                 return __diag_virtio_hypercall(vcpu);
164         default:
165                 return -EOPNOTSUPP;
166         }
167 }