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