2 * handling 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>
22 static int __sigp_sense(struct kvm_vcpu
*vcpu
, u16 cpu_addr
,
25 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
28 if (cpu_addr
>= KVM_MAX_VCPUS
)
29 return SIGP_CC_NOT_OPERATIONAL
;
32 if (fi
->local_int
[cpu_addr
] == NULL
)
33 rc
= SIGP_CC_NOT_OPERATIONAL
;
34 else if (!(atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
35 & (CPUSTAT_ECALL_PEND
| CPUSTAT_STOPPED
)))
36 rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
38 *reg
&= 0xffffffff00000000UL
;
39 if (atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
41 *reg
|= SIGP_STATUS_EXT_CALL_PENDING
;
42 if (atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
44 *reg
|= SIGP_STATUS_STOPPED
;
45 rc
= SIGP_CC_STATUS_STORED
;
47 spin_unlock(&fi
->lock
);
49 VCPU_EVENT(vcpu
, 4, "sensed status of cpu %x rc %x", cpu_addr
, rc
);
53 static int __sigp_emergency(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
55 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
56 struct kvm_s390_local_interrupt
*li
;
57 struct kvm_s390_interrupt_info
*inti
;
60 if (cpu_addr
>= KVM_MAX_VCPUS
)
61 return SIGP_CC_NOT_OPERATIONAL
;
63 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
67 inti
->type
= KVM_S390_INT_EMERGENCY
;
68 inti
->emerg
.code
= vcpu
->vcpu_id
;
71 li
= fi
->local_int
[cpu_addr
];
73 rc
= SIGP_CC_NOT_OPERATIONAL
;
77 spin_lock_bh(&li
->lock
);
78 list_add_tail(&inti
->list
, &li
->list
);
79 atomic_set(&li
->active
, 1);
80 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
81 if (waitqueue_active(&li
->wq
))
82 wake_up_interruptible(&li
->wq
);
83 spin_unlock_bh(&li
->lock
);
84 rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
85 VCPU_EVENT(vcpu
, 4, "sent sigp emerg to cpu %x", cpu_addr
);
87 spin_unlock(&fi
->lock
);
91 static int __sigp_external_call(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
93 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
94 struct kvm_s390_local_interrupt
*li
;
95 struct kvm_s390_interrupt_info
*inti
;
98 if (cpu_addr
>= KVM_MAX_VCPUS
)
99 return SIGP_CC_NOT_OPERATIONAL
;
101 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
105 inti
->type
= KVM_S390_INT_EXTERNAL_CALL
;
106 inti
->extcall
.code
= vcpu
->vcpu_id
;
108 spin_lock(&fi
->lock
);
109 li
= fi
->local_int
[cpu_addr
];
111 rc
= SIGP_CC_NOT_OPERATIONAL
;
115 spin_lock_bh(&li
->lock
);
116 list_add_tail(&inti
->list
, &li
->list
);
117 atomic_set(&li
->active
, 1);
118 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
119 if (waitqueue_active(&li
->wq
))
120 wake_up_interruptible(&li
->wq
);
121 spin_unlock_bh(&li
->lock
);
122 rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
123 VCPU_EVENT(vcpu
, 4, "sent sigp ext call to cpu %x", cpu_addr
);
125 spin_unlock(&fi
->lock
);
129 static int __inject_sigp_stop(struct kvm_s390_local_interrupt
*li
, int action
)
131 struct kvm_s390_interrupt_info
*inti
;
133 inti
= kzalloc(sizeof(*inti
), GFP_ATOMIC
);
136 inti
->type
= KVM_S390_SIGP_STOP
;
138 spin_lock_bh(&li
->lock
);
139 if ((atomic_read(li
->cpuflags
) & CPUSTAT_STOPPED
))
141 list_add_tail(&inti
->list
, &li
->list
);
142 atomic_set(&li
->active
, 1);
143 atomic_set_mask(CPUSTAT_STOP_INT
, li
->cpuflags
);
144 li
->action_bits
|= action
;
145 if (waitqueue_active(&li
->wq
))
146 wake_up_interruptible(&li
->wq
);
148 spin_unlock_bh(&li
->lock
);
150 return SIGP_CC_ORDER_CODE_ACCEPTED
;
153 static int __sigp_stop(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, int action
)
155 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
156 struct kvm_s390_local_interrupt
*li
;
159 if (cpu_addr
>= KVM_MAX_VCPUS
)
160 return SIGP_CC_NOT_OPERATIONAL
;
162 spin_lock(&fi
->lock
);
163 li
= fi
->local_int
[cpu_addr
];
165 rc
= SIGP_CC_NOT_OPERATIONAL
;
169 rc
= __inject_sigp_stop(li
, action
);
172 spin_unlock(&fi
->lock
);
173 VCPU_EVENT(vcpu
, 4, "sent sigp stop to cpu %x", cpu_addr
);
177 int kvm_s390_inject_sigp_stop(struct kvm_vcpu
*vcpu
, int action
)
179 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
180 return __inject_sigp_stop(li
, action
);
183 static int __sigp_set_arch(struct kvm_vcpu
*vcpu
, u32 parameter
)
187 switch (parameter
& 0xff) {
189 rc
= SIGP_CC_NOT_OPERATIONAL
;
193 rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
201 static int __sigp_set_prefix(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, u32 address
,
204 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
205 struct kvm_s390_local_interrupt
*li
= NULL
;
206 struct kvm_s390_interrupt_info
*inti
;
210 /* make sure that the new value is valid memory */
211 address
= address
& 0x7fffe000u
;
212 if (copy_from_guest_absolute(vcpu
, &tmp
, address
, 1) ||
213 copy_from_guest_absolute(vcpu
, &tmp
, address
+ PAGE_SIZE
, 1)) {
214 *reg
&= 0xffffffff00000000UL
;
215 *reg
|= SIGP_STATUS_INVALID_PARAMETER
;
216 return SIGP_CC_STATUS_STORED
;
219 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
223 spin_lock(&fi
->lock
);
224 if (cpu_addr
< KVM_MAX_VCPUS
)
225 li
= fi
->local_int
[cpu_addr
];
228 *reg
&= 0xffffffff00000000UL
;
229 *reg
|= SIGP_STATUS_INCORRECT_STATE
;
230 rc
= SIGP_CC_STATUS_STORED
;
235 spin_lock_bh(&li
->lock
);
236 /* cpu must be in stopped state */
237 if (!(atomic_read(li
->cpuflags
) & CPUSTAT_STOPPED
)) {
238 *reg
&= 0xffffffff00000000UL
;
239 *reg
|= SIGP_STATUS_INCORRECT_STATE
;
240 rc
= SIGP_CC_STATUS_STORED
;
245 inti
->type
= KVM_S390_SIGP_SET_PREFIX
;
246 inti
->prefix
.address
= address
;
248 list_add_tail(&inti
->list
, &li
->list
);
249 atomic_set(&li
->active
, 1);
250 if (waitqueue_active(&li
->wq
))
251 wake_up_interruptible(&li
->wq
);
252 rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
254 VCPU_EVENT(vcpu
, 4, "set prefix of cpu %02x to %x", cpu_addr
, address
);
256 spin_unlock_bh(&li
->lock
);
258 spin_unlock(&fi
->lock
);
262 static int __sigp_sense_running(struct kvm_vcpu
*vcpu
, u16 cpu_addr
,
266 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
268 if (cpu_addr
>= KVM_MAX_VCPUS
)
269 return SIGP_CC_NOT_OPERATIONAL
;
271 spin_lock(&fi
->lock
);
272 if (fi
->local_int
[cpu_addr
] == NULL
)
273 rc
= SIGP_CC_NOT_OPERATIONAL
;
275 if (atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
278 rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
281 *reg
&= 0xffffffff00000000UL
;
282 *reg
|= SIGP_STATUS_NOT_RUNNING
;
283 rc
= SIGP_CC_STATUS_STORED
;
286 spin_unlock(&fi
->lock
);
288 VCPU_EVENT(vcpu
, 4, "sensed running status of cpu %x rc %x", cpu_addr
,
294 static int __sigp_restart(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
296 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
297 struct kvm_s390_local_interrupt
*li
;
298 int rc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
300 if (cpu_addr
>= KVM_MAX_VCPUS
)
301 return SIGP_CC_NOT_OPERATIONAL
;
303 spin_lock(&fi
->lock
);
304 li
= fi
->local_int
[cpu_addr
];
306 rc
= SIGP_CC_NOT_OPERATIONAL
;
310 spin_lock_bh(&li
->lock
);
311 if (li
->action_bits
& ACTION_STOP_ON_STOP
)
314 VCPU_EVENT(vcpu
, 4, "sigp restart %x to handle userspace",
316 spin_unlock_bh(&li
->lock
);
318 spin_unlock(&fi
->lock
);
322 int kvm_s390_handle_sigp(struct kvm_vcpu
*vcpu
)
324 int r1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
325 int r3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
326 int base2
= vcpu
->arch
.sie_block
->ipb
>> 28;
327 int disp2
= ((vcpu
->arch
.sie_block
->ipb
& 0x0fff0000) >> 16);
329 u16 cpu_addr
= vcpu
->run
->s
.regs
.gprs
[r3
];
333 /* sigp in userspace can exit */
334 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
335 return kvm_s390_inject_program_int(vcpu
,
336 PGM_PRIVILEGED_OPERATION
);
340 order_code
+= vcpu
->run
->s
.regs
.gprs
[base2
];
343 parameter
= vcpu
->run
->s
.regs
.gprs
[r1
];
345 parameter
= vcpu
->run
->s
.regs
.gprs
[r1
+ 1];
347 switch (order_code
) {
349 vcpu
->stat
.instruction_sigp_sense
++;
350 rc
= __sigp_sense(vcpu
, cpu_addr
,
351 &vcpu
->run
->s
.regs
.gprs
[r1
]);
353 case SIGP_EXTERNAL_CALL
:
354 vcpu
->stat
.instruction_sigp_external_call
++;
355 rc
= __sigp_external_call(vcpu
, cpu_addr
);
357 case SIGP_EMERGENCY_SIGNAL
:
358 vcpu
->stat
.instruction_sigp_emergency
++;
359 rc
= __sigp_emergency(vcpu
, cpu_addr
);
362 vcpu
->stat
.instruction_sigp_stop
++;
363 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STOP_ON_STOP
);
365 case SIGP_STOP_AND_STORE_STATUS
:
366 vcpu
->stat
.instruction_sigp_stop
++;
367 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STORE_ON_STOP
|
368 ACTION_STOP_ON_STOP
);
370 case SIGP_SET_ARCHITECTURE
:
371 vcpu
->stat
.instruction_sigp_arch
++;
372 rc
= __sigp_set_arch(vcpu
, parameter
);
374 case SIGP_SET_PREFIX
:
375 vcpu
->stat
.instruction_sigp_prefix
++;
376 rc
= __sigp_set_prefix(vcpu
, cpu_addr
, parameter
,
377 &vcpu
->run
->s
.regs
.gprs
[r1
]);
379 case SIGP_SENSE_RUNNING
:
380 vcpu
->stat
.instruction_sigp_sense_running
++;
381 rc
= __sigp_sense_running(vcpu
, cpu_addr
,
382 &vcpu
->run
->s
.regs
.gprs
[r1
]);
385 vcpu
->stat
.instruction_sigp_restart
++;
386 rc
= __sigp_restart(vcpu
, cpu_addr
);
387 if (rc
== SIGP_CC_BUSY
)
389 /* user space must know about restart */
397 vcpu
->arch
.sie_block
->gpsw
.mask
&= ~(3ul << 44);
398 vcpu
->arch
.sie_block
->gpsw
.mask
|= (rc
& 3ul) << 44;