2 * sigp.c - handlinge interprocessor communication
4 * Copyright IBM Corp. 2008,2009
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.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Christian Ehrhardt <ehrhardt@de.ibm.com>
15 #include <linux/kvm.h>
16 #include <linux/kvm_host.h>
17 #include <linux/slab.h>
21 /* sigp order codes */
22 #define SIGP_SENSE 0x01
23 #define SIGP_EXTERNAL_CALL 0x02
24 #define SIGP_EMERGENCY 0x03
25 #define SIGP_START 0x04
26 #define SIGP_STOP 0x05
27 #define SIGP_RESTART 0x06
28 #define SIGP_STOP_STORE_STATUS 0x09
29 #define SIGP_INITIAL_CPU_RESET 0x0b
30 #define SIGP_CPU_RESET 0x0c
31 #define SIGP_SET_PREFIX 0x0d
32 #define SIGP_STORE_STATUS_ADDR 0x0e
33 #define SIGP_SET_ARCH 0x12
36 #define SIGP_STAT_EQUIPMENT_CHECK 0x80000000UL
37 #define SIGP_STAT_INCORRECT_STATE 0x00000200UL
38 #define SIGP_STAT_INVALID_PARAMETER 0x00000100UL
39 #define SIGP_STAT_EXT_CALL_PENDING 0x00000080UL
40 #define SIGP_STAT_STOPPED 0x00000040UL
41 #define SIGP_STAT_OPERATOR_INTERV 0x00000020UL
42 #define SIGP_STAT_CHECK_STOP 0x00000010UL
43 #define SIGP_STAT_INOPERATIVE 0x00000004UL
44 #define SIGP_STAT_INVALID_ORDER 0x00000002UL
45 #define SIGP_STAT_RECEIVER_CHECK 0x00000001UL
48 static int __sigp_sense(struct kvm_vcpu
*vcpu
, u16 cpu_addr
,
51 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
54 if (cpu_addr
>= KVM_MAX_VCPUS
)
55 return 3; /* not operational */
58 if (fi
->local_int
[cpu_addr
] == NULL
)
59 rc
= 3; /* not operational */
60 else if (atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
62 *reg
&= 0xffffffff00000000UL
;
63 rc
= 1; /* status stored */
65 *reg
&= 0xffffffff00000000UL
;
66 *reg
|= SIGP_STAT_STOPPED
;
67 rc
= 1; /* status stored */
69 spin_unlock(&fi
->lock
);
71 VCPU_EVENT(vcpu
, 4, "sensed status of cpu %x rc %x", cpu_addr
, rc
);
75 static int __sigp_emergency(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
77 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
78 struct kvm_s390_local_interrupt
*li
;
79 struct kvm_s390_interrupt_info
*inti
;
82 if (cpu_addr
>= KVM_MAX_VCPUS
)
83 return 3; /* not operational */
85 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
89 inti
->type
= KVM_S390_INT_EMERGENCY
;
92 li
= fi
->local_int
[cpu_addr
];
94 rc
= 3; /* not operational */
98 spin_lock_bh(&li
->lock
);
99 list_add_tail(&inti
->list
, &li
->list
);
100 atomic_set(&li
->active
, 1);
101 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
102 if (waitqueue_active(&li
->wq
))
103 wake_up_interruptible(&li
->wq
);
104 spin_unlock_bh(&li
->lock
);
105 rc
= 0; /* order accepted */
107 spin_unlock(&fi
->lock
);
108 VCPU_EVENT(vcpu
, 4, "sent sigp emerg to cpu %x", cpu_addr
);
112 static int __inject_sigp_stop(struct kvm_s390_local_interrupt
*li
, int action
)
114 struct kvm_s390_interrupt_info
*inti
;
116 inti
= kzalloc(sizeof(*inti
), GFP_ATOMIC
);
119 inti
->type
= KVM_S390_SIGP_STOP
;
121 spin_lock_bh(&li
->lock
);
122 list_add_tail(&inti
->list
, &li
->list
);
123 atomic_set(&li
->active
, 1);
124 atomic_set_mask(CPUSTAT_STOP_INT
, li
->cpuflags
);
125 li
->action_bits
|= action
;
126 if (waitqueue_active(&li
->wq
))
127 wake_up_interruptible(&li
->wq
);
128 spin_unlock_bh(&li
->lock
);
130 return 0; /* order accepted */
133 static int __sigp_stop(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, int action
)
135 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
136 struct kvm_s390_local_interrupt
*li
;
139 if (cpu_addr
>= KVM_MAX_VCPUS
)
140 return 3; /* not operational */
142 spin_lock(&fi
->lock
);
143 li
= fi
->local_int
[cpu_addr
];
145 rc
= 3; /* not operational */
149 rc
= __inject_sigp_stop(li
, action
);
152 spin_unlock(&fi
->lock
);
153 VCPU_EVENT(vcpu
, 4, "sent sigp stop to cpu %x", cpu_addr
);
157 int kvm_s390_inject_sigp_stop(struct kvm_vcpu
*vcpu
, int action
)
159 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
160 return __inject_sigp_stop(li
, action
);
163 static int __sigp_set_arch(struct kvm_vcpu
*vcpu
, u32 parameter
)
167 switch (parameter
& 0xff) {
169 rc
= 3; /* not operational */
173 rc
= 0; /* order accepted */
181 static int __sigp_set_prefix(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, u32 address
,
184 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
185 struct kvm_s390_local_interrupt
*li
= NULL
;
186 struct kvm_s390_interrupt_info
*inti
;
190 /* make sure that the new value is valid memory */
191 address
= address
& 0x7fffe000u
;
192 if (copy_from_guest_absolute(vcpu
, &tmp
, address
, 1) ||
193 copy_from_guest_absolute(vcpu
, &tmp
, address
+ PAGE_SIZE
, 1)) {
194 *reg
|= SIGP_STAT_INVALID_PARAMETER
;
195 return 1; /* invalid parameter */
198 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
202 spin_lock(&fi
->lock
);
203 if (cpu_addr
< KVM_MAX_VCPUS
)
204 li
= fi
->local_int
[cpu_addr
];
207 rc
= 1; /* incorrect state */
208 *reg
&= SIGP_STAT_INCORRECT_STATE
;
213 spin_lock_bh(&li
->lock
);
214 /* cpu must be in stopped state */
215 if (atomic_read(li
->cpuflags
) & CPUSTAT_RUNNING
) {
216 rc
= 1; /* incorrect state */
217 *reg
&= SIGP_STAT_INCORRECT_STATE
;
222 inti
->type
= KVM_S390_SIGP_SET_PREFIX
;
223 inti
->prefix
.address
= address
;
225 list_add_tail(&inti
->list
, &li
->list
);
226 atomic_set(&li
->active
, 1);
227 if (waitqueue_active(&li
->wq
))
228 wake_up_interruptible(&li
->wq
);
229 rc
= 0; /* order accepted */
231 VCPU_EVENT(vcpu
, 4, "set prefix of cpu %02x to %x", cpu_addr
, address
);
233 spin_unlock_bh(&li
->lock
);
235 spin_unlock(&fi
->lock
);
239 int kvm_s390_handle_sigp(struct kvm_vcpu
*vcpu
)
241 int r1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
242 int r3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
243 int base2
= vcpu
->arch
.sie_block
->ipb
>> 28;
244 int disp2
= ((vcpu
->arch
.sie_block
->ipb
& 0x0fff0000) >> 16);
246 u16 cpu_addr
= vcpu
->arch
.guest_gprs
[r3
];
250 /* sigp in userspace can exit */
251 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
252 return kvm_s390_inject_program_int(vcpu
,
253 PGM_PRIVILEGED_OPERATION
);
257 order_code
+= vcpu
->arch
.guest_gprs
[base2
];
260 parameter
= vcpu
->arch
.guest_gprs
[r1
];
262 parameter
= vcpu
->arch
.guest_gprs
[r1
+ 1];
264 switch (order_code
) {
266 vcpu
->stat
.instruction_sigp_sense
++;
267 rc
= __sigp_sense(vcpu
, cpu_addr
,
268 &vcpu
->arch
.guest_gprs
[r1
]);
271 vcpu
->stat
.instruction_sigp_emergency
++;
272 rc
= __sigp_emergency(vcpu
, cpu_addr
);
275 vcpu
->stat
.instruction_sigp_stop
++;
276 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STOP_ON_STOP
);
278 case SIGP_STOP_STORE_STATUS
:
279 vcpu
->stat
.instruction_sigp_stop
++;
280 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STORE_ON_STOP
);
283 vcpu
->stat
.instruction_sigp_arch
++;
284 rc
= __sigp_set_arch(vcpu
, parameter
);
286 case SIGP_SET_PREFIX
:
287 vcpu
->stat
.instruction_sigp_prefix
++;
288 rc
= __sigp_set_prefix(vcpu
, cpu_addr
, parameter
,
289 &vcpu
->arch
.guest_gprs
[r1
]);
292 vcpu
->stat
.instruction_sigp_restart
++;
293 /* user space must know about restart */
301 vcpu
->arch
.sie_block
->gpsw
.mask
&= ~(3ul << 44);
302 vcpu
->arch
.sie_block
->gpsw
.mask
|= (rc
& 3ul) << 44;