blob: 6f9cfa50037246d8d37bb5dd81a21d643ff3f544 [file] [log] [blame]
Christian Borntraegere28acfe2008-03-25 18:47:34 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling diagnose instructions
Christian Borntraegere28acfe2008-03-25 18:47:34 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2011
Christian Borntraegere28acfe2008-03-25 18:47:34 +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.h>
15#include <linux/kvm_host.h>
Martin Schwidefskydeedabb2013-05-21 17:29:52 +020016#include <asm/pgalloc.h>
Cornelia Huck10ccaa12013-02-28 12:33:21 +010017#include <asm/virtio-ccw.h>
Christian Borntraegere28acfe2008-03-25 18:47:34 +010018#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020019#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020020#include "trace-s390.h"
Christian Borntraegere28acfe2008-03-25 18:47:34 +010021
Christian Borntraeger388186b2011-10-30 15:17:03 +010022static int diag_release_pages(struct kvm_vcpu *vcpu)
23{
24 unsigned long start, end;
25 unsigned long prefix = vcpu->arch.sie_block->prefix;
26
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010027 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;
Christian Borntraeger388186b2011-10-30 15:17:03 +010029
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
Christian Borntraegere28acfe2008-03-25 18:47:34 +010050static 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++;
Christian Borntraeger8733ac32012-04-25 15:30:39 +020054 kvm_vcpu_on_spin(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +010055 return 0;
56}
57
Konstantin Weitz41628d32012-04-25 15:30:38 +020058static 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
Christian Borntraegere28acfe2008-03-25 18:47:34 +010081static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
82{
83 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010084 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +010085
86 VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
87 switch (subcode) {
88 case 3:
89 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
Martin Schwidefskydeedabb2013-05-21 17:29:52 +020090 page_table_reset_pgste(current->mm, 0, TASK_SIZE);
Christian Borntraegere28acfe2008-03-25 18:47:34 +010091 break;
92 case 4:
93 vcpu->run->s390_reset_flags = 0;
Martin Schwidefskydeedabb2013-05-21 17:29:52 +020094 page_table_reset_pgste(current->mm, 0, TASK_SIZE);
Christian Borntraegere28acfe2008-03-25 18:47:34 +010095 break;
96 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +010097 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +010098 }
99
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100100 atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100101 vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
102 vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
103 vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
104 vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
Heiko Carstens33e19112009-01-09 12:14:56 +0100105 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100106 vcpu->run->s390_reset_flags);
Cornelia Huckade38c32012-07-23 17:20:30 +0200107 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100108 return -EREMOTE;
109}
110
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100111static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
112{
Thomas Huth800c1062013-09-12 10:33:45 +0200113 int ret;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100114
115 /* No virtio-ccw notification? Get out quickly. */
116 if (!vcpu->kvm->arch.css_support ||
117 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
118 return -EOPNOTSUPP;
119
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100120 /*
121 * The layout is as follows:
122 * - gpr 2 contains the subchannel id (passed as addr)
123 * - gpr 3 contains the virtqueue index (passed as datamatch)
Cornelia Huck85dfe872013-07-03 16:30:54 +0200124 * - gpr 4 contains the index on the bus (optionally)
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100125 */
Cornelia Huck85dfe872013-07-03 16:30:54 +0200126 ret = kvm_io_bus_write_cookie(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
Dominik Dingelff1f3cb2013-12-09 18:30:01 +0100127 vcpu->run->s.regs.gprs[2] & 0xffffffff,
Cornelia Huck85dfe872013-07-03 16:30:54 +0200128 8, &vcpu->run->s.regs.gprs[3],
129 vcpu->run->s.regs.gprs[4]);
Cornelia Huck85dfe872013-07-03 16:30:54 +0200130
131 /*
132 * Return cookie in gpr 2, but don't overwrite the register if the
133 * diagnose will be handled by userspace.
134 */
135 if (ret != -EOPNOTSUPP)
136 vcpu->run->s.regs.gprs[2] = ret;
137 /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100138 return ret < 0 ? ret : 0;
139}
140
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100141int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
142{
Heiko Carstens743db272013-11-11 13:56:47 +0100143 int code = kvm_s390_get_base_disp_rs(vcpu) & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100144
Thomas Huth93e17502013-06-20 17:22:02 +0200145 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
146 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
147
Cornelia Huck5786fff2012-07-23 17:20:29 +0200148 trace_kvm_s390_handle_diag(vcpu, code);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100149 switch (code) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100150 case 0x10:
151 return diag_release_pages(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100152 case 0x44:
153 return __diag_time_slice_end(vcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200154 case 0x9c:
155 return __diag_time_slice_end_directed(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100156 case 0x308:
157 return __diag_ipl_functions(vcpu);
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100158 case 0x500:
159 return __diag_virtio_hypercall(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100160 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100161 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100162 }
163}