blob: 5ee56e5acc2396f594854dd8a3e0e99bc3b72720 [file] [log] [blame]
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * in-kernel handling for sie intercepts
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +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"
Cornelia Huck5786fff2012-07-23 17:20:29 +020022#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020023#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010024
Cornelia Huckf379aae2012-12-20 15:32:10 +010025
Cornelia Huck77975352012-12-20 15:32:06 +010026static const intercept_handler_t instruction_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +020027 [0x01] = kvm_s390_handle_01,
Cornelia Huck48a3e952012-12-20 15:32:09 +010028 [0x82] = kvm_s390_handle_lpsw,
Christian Borntraegere28acfe2008-03-25 18:47:34 +010029 [0x83] = kvm_s390_handle_diag,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010030 [0xae] = kvm_s390_handle_sigp,
Christian Borntraeger70455a32009-01-22 10:28:29 +010031 [0xb2] = kvm_s390_handle_b2,
Thomas Huth953ed882013-06-20 17:22:04 +020032 [0xb7] = kvm_s390_handle_lctl,
Cornelia Huck48a3e952012-12-20 15:32:09 +010033 [0xb9] = kvm_s390_handle_b9,
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020034 [0xe5] = kvm_s390_handle_e5,
Thomas Huth953ed882013-06-20 17:22:04 +020035 [0xeb] = kvm_s390_handle_eb,
Carsten Otteba5c1e92008-03-25 18:47:26 +010036};
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010037
38static int handle_noop(struct kvm_vcpu *vcpu)
39{
40 switch (vcpu->arch.sie_block->icptcode) {
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +020041 case 0x0:
42 vcpu->stat.exit_null++;
43 break;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010044 case 0x10:
45 vcpu->stat.exit_external_request++;
46 break;
47 case 0x14:
48 vcpu->stat.exit_external_interrupt++;
49 break;
50 default:
51 break; /* nothing */
52 }
53 return 0;
54}
55
56static int handle_stop(struct kvm_vcpu *vcpu)
57{
Christian Ehrhardt9ace9032009-05-20 15:34:55 +020058 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010059
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010060 vcpu->stat.exit_stop_request++;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010061 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010062
Cornelia Huckade38c32012-07-23 17:20:30 +020063 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
64
Christian Ehrhardt9ace9032009-05-20 15:34:55 +020065 if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
66 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
67 rc = SIE_INTERCEPT_RERUNVCPU;
68 vcpu->run->exit_reason = KVM_EXIT_INTR;
69 }
70
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010071 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
Cornelia Huck9e6dabe2011-11-17 11:00:41 +010072 atomic_set_mask(CPUSTAT_STOPPED,
73 &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010074 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
75 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
Heiko Carstensb8e660b2010-02-26 22:37:41 +010076 rc = -EOPNOTSUPP;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +020077 }
78
Jens Freimann9e0d5472012-02-06 10:59:03 +010079 if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
80 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
81 /* store status must be called unlocked. Since local_int.lock
82 * only protects local_int.* and not guest memory we can give
83 * up the lock here */
84 spin_unlock_bh(&vcpu->arch.local_int.lock);
85 rc = kvm_s390_vcpu_store_status(vcpu,
86 KVM_S390_STORE_STATUS_NOADDR);
87 if (rc >= 0)
88 rc = -EOPNOTSUPP;
89 } else
90 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010091 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010092}
93
94static int handle_validity(struct kvm_vcpu *vcpu)
95{
96 int viwhy = vcpu->arch.sie_block->ipb >> 16;
Carsten Otte3edbcff2009-05-12 17:21:52 +020097
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010098 vcpu->stat.exit_validity++;
Cornelia Huck5786fff2012-07-23 17:20:29 +020099 trace_kvm_s390_intercept_validity(vcpu, viwhy);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200100 WARN_ONCE(true, "kvm: unhandled validity intercept 0x%x\n", viwhy);
101 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100102}
103
Carsten Otteba5c1e92008-03-25 18:47:26 +0100104static int handle_instruction(struct kvm_vcpu *vcpu)
105{
106 intercept_handler_t handler;
107
108 vcpu->stat.exit_instruction++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200109 trace_kvm_s390_intercept_instruction(vcpu,
110 vcpu->arch.sie_block->ipa,
111 vcpu->arch.sie_block->ipb);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100112 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
113 if (handler)
114 return handler(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100115 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100116}
117
118static int handle_prog(struct kvm_vcpu *vcpu)
119{
120 vcpu->stat.exit_program_interruption++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200121 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100122 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
123}
124
125static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
126{
127 int rc, rc2;
128
129 vcpu->stat.exit_instr_and_program++;
130 rc = handle_instruction(vcpu);
131 rc2 = handle_prog(vcpu);
132
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100133 if (rc == -EOPNOTSUPP)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100134 vcpu->arch.sie_block->icptcode = 0x04;
135 if (rc)
136 return rc;
137 return rc2;
138}
139
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100140static const intercept_handler_t intercept_funcs[] = {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100141 [0x00 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100142 [0x04 >> 2] = handle_instruction,
143 [0x08 >> 2] = handle_prog,
144 [0x0C >> 2] = handle_instruction_and_prog,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100145 [0x10 >> 2] = handle_noop,
146 [0x14 >> 2] = handle_noop,
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100147 [0x18 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100148 [0x1C >> 2] = kvm_s390_handle_wait,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100149 [0x20 >> 2] = handle_validity,
150 [0x28 >> 2] = handle_stop,
151};
152
153int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
154{
155 intercept_handler_t func;
156 u8 code = vcpu->arch.sie_block->icptcode;
157
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100158 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100159 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100160 func = intercept_funcs[code >> 2];
161 if (func)
162 return func(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100163 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100164}