blob: b5312050b224e248222e3eafa6926861463709f0 [file] [log] [blame]
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001/*
2 * intercept.c - in-kernel handling for sie intercepts
3 *
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +02004 * Copyright IBM Corp. 2008,2009
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01005 *
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_host.h>
15#include <linux/errno.h>
16#include <linux/pagemap.h>
17
18#include <asm/kvm_host.h>
19
20#include "kvm-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010021#include "gaccess.h"
22
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020023static int handle_lctlg(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010024{
25 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
26 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
27 int base2 = vcpu->arch.sie_block->ipb >> 28;
28 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
29 ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
30 u64 useraddr;
31 int reg, rc;
32
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020033 vcpu->stat.instruction_lctlg++;
Carsten Otteba5c1e92008-03-25 18:47:26 +010034 if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
Heiko Carstensb8e660b2010-02-26 22:37:41 +010035 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +010036
37 useraddr = disp2;
38 if (base2)
39 useraddr += vcpu->arch.guest_gprs[base2];
40
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020041 if (useraddr & 7)
42 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
43
Carsten Otteba5c1e92008-03-25 18:47:26 +010044 reg = reg1;
45
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020046 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
Carsten Otteba5c1e92008-03-25 18:47:26 +010047 disp2);
48
49 do {
50 rc = get_guest_u64(vcpu, useraddr,
51 &vcpu->arch.sie_block->gcr[reg]);
52 if (rc == -EFAULT) {
53 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
54 break;
55 }
56 useraddr += 8;
57 if (reg == reg3)
58 break;
59 reg = (reg + 1) % 16;
60 } while (1);
61 return 0;
62}
63
64static int handle_lctl(struct kvm_vcpu *vcpu)
65{
66 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
67 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
68 int base2 = vcpu->arch.sie_block->ipb >> 28;
69 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
70 u64 useraddr;
71 u32 val = 0;
72 int reg, rc;
73
74 vcpu->stat.instruction_lctl++;
75
76 useraddr = disp2;
77 if (base2)
78 useraddr += vcpu->arch.guest_gprs[base2];
79
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020080 if (useraddr & 3)
81 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
82
Carsten Otteba5c1e92008-03-25 18:47:26 +010083 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
84 disp2);
85
86 reg = reg1;
87 do {
88 rc = get_guest_u32(vcpu, useraddr, &val);
89 if (rc == -EFAULT) {
90 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
91 break;
92 }
93 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
94 vcpu->arch.sie_block->gcr[reg] |= val;
95 useraddr += 4;
96 if (reg == reg3)
97 break;
98 reg = (reg + 1) % 16;
99 } while (1);
100 return 0;
101}
102
103static intercept_handler_t instruction_handlers[256] = {
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100104 [0x83] = kvm_s390_handle_diag,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100105 [0xae] = kvm_s390_handle_sigp,
Christian Borntraeger70455a32009-01-22 10:28:29 +0100106 [0xb2] = kvm_s390_handle_b2,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100107 [0xb7] = handle_lctl,
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200108 [0xe5] = kvm_s390_handle_e5,
Christian Borntraegerf5e10b02008-07-25 15:52:44 +0200109 [0xeb] = handle_lctlg,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100110};
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100111
112static int handle_noop(struct kvm_vcpu *vcpu)
113{
114 switch (vcpu->arch.sie_block->icptcode) {
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +0200115 case 0x0:
116 vcpu->stat.exit_null++;
117 break;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100118 case 0x10:
119 vcpu->stat.exit_external_request++;
120 break;
121 case 0x14:
122 vcpu->stat.exit_external_interrupt++;
123 break;
124 default:
125 break; /* nothing */
126 }
127 return 0;
128}
129
130static int handle_stop(struct kvm_vcpu *vcpu)
131{
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200132 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100133
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100134 vcpu->stat.exit_stop_request++;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100135 atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100136 spin_lock_bh(&vcpu->arch.local_int.lock);
137 if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
138 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
Christian Borntraeger971eb772010-06-12 08:54:13 +0200139 rc = kvm_s390_vcpu_store_status(vcpu,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100140 KVM_S390_STORE_STATUS_NOADDR);
141 if (rc >= 0)
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100142 rc = -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100143 }
144
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200145 if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
146 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
147 rc = SIE_INTERCEPT_RERUNVCPU;
148 vcpu->run->exit_reason = KVM_EXIT_INTR;
149 }
150
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100151 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
152 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
153 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100154 rc = -EOPNOTSUPP;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200155 }
156
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100157 spin_unlock_bh(&vcpu->arch.local_int.lock);
158 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100159}
160
161static int handle_validity(struct kvm_vcpu *vcpu)
162{
163 int viwhy = vcpu->arch.sie_block->ipb >> 16;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200164 int rc;
165
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100166 vcpu->stat.exit_validity++;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200167 if ((viwhy == 0x37) && (vcpu->arch.sie_block->prefix
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +0200168 <= kvm_s390_vcpu_get_memsize(vcpu) - 2*PAGE_SIZE)) {
Carsten Otte3edbcff2009-05-12 17:21:52 +0200169 rc = fault_in_pages_writeable((char __user *)
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +0200170 vcpu->arch.sie_block->gmsor +
Carsten Otte3edbcff2009-05-12 17:21:52 +0200171 vcpu->arch.sie_block->prefix,
172 2*PAGE_SIZE);
173 if (rc)
174 /* user will receive sigsegv, exit to user */
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100175 rc = -EOPNOTSUPP;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200176 } else
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100177 rc = -EOPNOTSUPP;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200178
179 if (rc)
180 VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
181 viwhy);
182 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100183}
184
Carsten Otteba5c1e92008-03-25 18:47:26 +0100185static int handle_instruction(struct kvm_vcpu *vcpu)
186{
187 intercept_handler_t handler;
188
189 vcpu->stat.exit_instruction++;
190 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
191 if (handler)
192 return handler(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100193 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100194}
195
196static int handle_prog(struct kvm_vcpu *vcpu)
197{
198 vcpu->stat.exit_program_interruption++;
199 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
200}
201
202static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
203{
204 int rc, rc2;
205
206 vcpu->stat.exit_instr_and_program++;
207 rc = handle_instruction(vcpu);
208 rc2 = handle_prog(vcpu);
209
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100210 if (rc == -EOPNOTSUPP)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100211 vcpu->arch.sie_block->icptcode = 0x04;
212 if (rc)
213 return rc;
214 return rc2;
215}
216
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100217static const intercept_handler_t intercept_funcs[] = {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100218 [0x00 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100219 [0x04 >> 2] = handle_instruction,
220 [0x08 >> 2] = handle_prog,
221 [0x0C >> 2] = handle_instruction_and_prog,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100222 [0x10 >> 2] = handle_noop,
223 [0x14 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100224 [0x1C >> 2] = kvm_s390_handle_wait,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100225 [0x20 >> 2] = handle_validity,
226 [0x28 >> 2] = handle_stop,
227};
228
229int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
230{
231 intercept_handler_t func;
232 u8 code = vcpu->arch.sie_block->icptcode;
233
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100234 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100235 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100236 func = intercept_funcs[code >> 2];
237 if (func)
238 return func(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100239 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100240}