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
34 #define SIGP_SENSE_RUNNING 0x15
37 #define SIGP_STAT_EQUIPMENT_CHECK 0x80000000UL
38 #define SIGP_STAT_NOT_RUNNING 0x00000400UL
39 #define SIGP_STAT_INCORRECT_STATE 0x00000200UL
40 #define SIGP_STAT_INVALID_PARAMETER 0x00000100UL
41 #define SIGP_STAT_EXT_CALL_PENDING 0x00000080UL
42 #define SIGP_STAT_STOPPED 0x00000040UL
43 #define SIGP_STAT_OPERATOR_INTERV 0x00000020UL
44 #define SIGP_STAT_CHECK_STOP 0x00000010UL
45 #define SIGP_STAT_INOPERATIVE 0x00000004UL
46 #define SIGP_STAT_INVALID_ORDER 0x00000002UL
47 #define SIGP_STAT_RECEIVER_CHECK 0x00000001UL
50 static int __sigp_sense(struct kvm_vcpu
*vcpu
, u16 cpu_addr
,
53 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
56 if (cpu_addr
>= KVM_MAX_VCPUS
)
57 return 3; /* not operational */
60 if (fi
->local_int
[cpu_addr
] == NULL
)
61 rc
= 3; /* not operational */
62 else if (!(atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
64 *reg
&= 0xffffffff00000000UL
;
65 rc
= 1; /* status stored */
67 *reg
&= 0xffffffff00000000UL
;
68 *reg
|= SIGP_STAT_STOPPED
;
69 rc
= 1; /* status stored */
71 spin_unlock(&fi
->lock
);
73 VCPU_EVENT(vcpu
, 4, "sensed status of cpu %x rc %x", cpu_addr
, rc
);
77 static int __sigp_emergency(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
79 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
80 struct kvm_s390_local_interrupt
*li
;
81 struct kvm_s390_interrupt_info
*inti
;
84 if (cpu_addr
>= KVM_MAX_VCPUS
)
85 return 3; /* not operational */
87 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
91 inti
->type
= KVM_S390_INT_EMERGENCY
;
92 inti
->emerg
.code
= vcpu
->vcpu_id
;
95 li
= fi
->local_int
[cpu_addr
];
97 rc
= 3; /* not operational */
101 spin_lock_bh(&li
->lock
);
102 list_add_tail(&inti
->list
, &li
->list
);
103 atomic_set(&li
->active
, 1);
104 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
105 if (waitqueue_active(&li
->wq
))
106 wake_up_interruptible(&li
->wq
);
107 spin_unlock_bh(&li
->lock
);
108 rc
= 0; /* order accepted */
109 VCPU_EVENT(vcpu
, 4, "sent sigp emerg to cpu %x", cpu_addr
);
111 spin_unlock(&fi
->lock
);
115 static int __sigp_external_call(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
117 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
118 struct kvm_s390_local_interrupt
*li
;
119 struct kvm_s390_interrupt_info
*inti
;
122 if (cpu_addr
>= KVM_MAX_VCPUS
)
123 return 3; /* not operational */
125 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
129 inti
->type
= KVM_S390_INT_EXTERNAL_CALL
;
130 inti
->extcall
.code
= vcpu
->vcpu_id
;
132 spin_lock(&fi
->lock
);
133 li
= fi
->local_int
[cpu_addr
];
135 rc
= 3; /* not operational */
139 spin_lock_bh(&li
->lock
);
140 list_add_tail(&inti
->list
, &li
->list
);
141 atomic_set(&li
->active
, 1);
142 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
143 if (waitqueue_active(&li
->wq
))
144 wake_up_interruptible(&li
->wq
);
145 spin_unlock_bh(&li
->lock
);
146 rc
= 0; /* order accepted */
147 VCPU_EVENT(vcpu
, 4, "sent sigp ext call to cpu %x", cpu_addr
);
149 spin_unlock(&fi
->lock
);
153 static int __inject_sigp_stop(struct kvm_s390_local_interrupt
*li
, int action
)
155 struct kvm_s390_interrupt_info
*inti
;
157 inti
= kzalloc(sizeof(*inti
), GFP_ATOMIC
);
160 inti
->type
= KVM_S390_SIGP_STOP
;
162 spin_lock_bh(&li
->lock
);
163 if ((atomic_read(li
->cpuflags
) & CPUSTAT_STOPPED
))
165 list_add_tail(&inti
->list
, &li
->list
);
166 atomic_set(&li
->active
, 1);
167 atomic_set_mask(CPUSTAT_STOP_INT
, li
->cpuflags
);
168 li
->action_bits
|= action
;
169 if (waitqueue_active(&li
->wq
))
170 wake_up_interruptible(&li
->wq
);
172 spin_unlock_bh(&li
->lock
);
174 return 0; /* order accepted */
177 static int __sigp_stop(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, int action
)
179 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
180 struct kvm_s390_local_interrupt
*li
;
183 if (cpu_addr
>= KVM_MAX_VCPUS
)
184 return 3; /* not operational */
186 spin_lock(&fi
->lock
);
187 li
= fi
->local_int
[cpu_addr
];
189 rc
= 3; /* not operational */
193 rc
= __inject_sigp_stop(li
, action
);
196 spin_unlock(&fi
->lock
);
197 VCPU_EVENT(vcpu
, 4, "sent sigp stop to cpu %x", cpu_addr
);
201 int kvm_s390_inject_sigp_stop(struct kvm_vcpu
*vcpu
, int action
)
203 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
204 return __inject_sigp_stop(li
, action
);
207 static int __sigp_set_arch(struct kvm_vcpu
*vcpu
, u32 parameter
)
211 switch (parameter
& 0xff) {
213 rc
= 3; /* not operational */
217 rc
= 0; /* order accepted */
225 static int __sigp_set_prefix(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, u32 address
,
228 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
229 struct kvm_s390_local_interrupt
*li
= NULL
;
230 struct kvm_s390_interrupt_info
*inti
;
234 /* make sure that the new value is valid memory */
235 address
= address
& 0x7fffe000u
;
236 if (copy_from_guest_absolute(vcpu
, &tmp
, address
, 1) ||
237 copy_from_guest_absolute(vcpu
, &tmp
, address
+ PAGE_SIZE
, 1)) {
238 *reg
|= SIGP_STAT_INVALID_PARAMETER
;
239 return 1; /* invalid parameter */
242 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
246 spin_lock(&fi
->lock
);
247 if (cpu_addr
< KVM_MAX_VCPUS
)
248 li
= fi
->local_int
[cpu_addr
];
251 rc
= 1; /* incorrect state */
252 *reg
&= SIGP_STAT_INCORRECT_STATE
;
257 spin_lock_bh(&li
->lock
);
258 /* cpu must be in stopped state */
259 if (!(atomic_read(li
->cpuflags
) & CPUSTAT_STOPPED
)) {
260 rc
= 1; /* incorrect state */
261 *reg
&= SIGP_STAT_INCORRECT_STATE
;
266 inti
->type
= KVM_S390_SIGP_SET_PREFIX
;
267 inti
->prefix
.address
= address
;
269 list_add_tail(&inti
->list
, &li
->list
);
270 atomic_set(&li
->active
, 1);
271 if (waitqueue_active(&li
->wq
))
272 wake_up_interruptible(&li
->wq
);
273 rc
= 0; /* order accepted */
275 VCPU_EVENT(vcpu
, 4, "set prefix of cpu %02x to %x", cpu_addr
, address
);
277 spin_unlock_bh(&li
->lock
);
279 spin_unlock(&fi
->lock
);
283 static int __sigp_sense_running(struct kvm_vcpu
*vcpu
, u16 cpu_addr
,
287 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
289 if (cpu_addr
>= KVM_MAX_VCPUS
)
290 return 3; /* not operational */
292 spin_lock(&fi
->lock
);
293 if (fi
->local_int
[cpu_addr
] == NULL
)
294 rc
= 3; /* not operational */
296 if (atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
302 *reg
&= 0xffffffff00000000UL
;
303 *reg
|= SIGP_STAT_NOT_RUNNING
;
307 spin_unlock(&fi
->lock
);
309 VCPU_EVENT(vcpu
, 4, "sensed running status of cpu %x rc %x", cpu_addr
,
315 static int __sigp_restart(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
318 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
319 struct kvm_s390_local_interrupt
*li
;
321 if (cpu_addr
>= KVM_MAX_VCPUS
)
322 return 3; /* not operational */
324 spin_lock(&fi
->lock
);
325 li
= fi
->local_int
[cpu_addr
];
327 rc
= 3; /* not operational */
331 spin_lock_bh(&li
->lock
);
332 if (li
->action_bits
& ACTION_STOP_ON_STOP
)
335 VCPU_EVENT(vcpu
, 4, "sigp restart %x to handle userspace",
337 spin_unlock_bh(&li
->lock
);
339 spin_unlock(&fi
->lock
);
343 int kvm_s390_handle_sigp(struct kvm_vcpu
*vcpu
)
345 int r1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
346 int r3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
347 int base2
= vcpu
->arch
.sie_block
->ipb
>> 28;
348 int disp2
= ((vcpu
->arch
.sie_block
->ipb
& 0x0fff0000) >> 16);
350 u16 cpu_addr
= vcpu
->run
->s
.regs
.gprs
[r3
];
354 /* sigp in userspace can exit */
355 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
356 return kvm_s390_inject_program_int(vcpu
,
357 PGM_PRIVILEGED_OPERATION
);
361 order_code
+= vcpu
->run
->s
.regs
.gprs
[base2
];
364 parameter
= vcpu
->run
->s
.regs
.gprs
[r1
];
366 parameter
= vcpu
->run
->s
.regs
.gprs
[r1
+ 1];
368 switch (order_code
) {
370 vcpu
->stat
.instruction_sigp_sense
++;
371 rc
= __sigp_sense(vcpu
, cpu_addr
,
372 &vcpu
->run
->s
.regs
.gprs
[r1
]);
374 case SIGP_EXTERNAL_CALL
:
375 vcpu
->stat
.instruction_sigp_external_call
++;
376 rc
= __sigp_external_call(vcpu
, cpu_addr
);
379 vcpu
->stat
.instruction_sigp_emergency
++;
380 rc
= __sigp_emergency(vcpu
, cpu_addr
);
383 vcpu
->stat
.instruction_sigp_stop
++;
384 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STOP_ON_STOP
);
386 case SIGP_STOP_STORE_STATUS
:
387 vcpu
->stat
.instruction_sigp_stop
++;
388 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STORE_ON_STOP
|
389 ACTION_STOP_ON_STOP
);
392 vcpu
->stat
.instruction_sigp_arch
++;
393 rc
= __sigp_set_arch(vcpu
, parameter
);
395 case SIGP_SET_PREFIX
:
396 vcpu
->stat
.instruction_sigp_prefix
++;
397 rc
= __sigp_set_prefix(vcpu
, cpu_addr
, parameter
,
398 &vcpu
->run
->s
.regs
.gprs
[r1
]);
400 case SIGP_SENSE_RUNNING
:
401 vcpu
->stat
.instruction_sigp_sense_running
++;
402 rc
= __sigp_sense_running(vcpu
, cpu_addr
,
403 &vcpu
->run
->s
.regs
.gprs
[r1
]);
406 vcpu
->stat
.instruction_sigp_restart
++;
407 rc
= __sigp_restart(vcpu
, cpu_addr
);
408 if (rc
== 2) /* busy */
410 /* user space must know about restart */
418 vcpu
->arch
.sie_block
->gpsw
.mask
&= ~(3ul << 44);
419 vcpu
->arch
.sie_block
->gpsw
.mask
|= (rc
& 3ul) << 44;