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>
20 /* sigp order codes */
21 #define SIGP_SENSE 0x01
22 #define SIGP_EXTERNAL_CALL 0x02
23 #define SIGP_EMERGENCY 0x03
24 #define SIGP_START 0x04
25 #define SIGP_STOP 0x05
26 #define SIGP_RESTART 0x06
27 #define SIGP_STOP_STORE_STATUS 0x09
28 #define SIGP_INITIAL_CPU_RESET 0x0b
29 #define SIGP_CPU_RESET 0x0c
30 #define SIGP_SET_PREFIX 0x0d
31 #define SIGP_STORE_STATUS_ADDR 0x0e
32 #define SIGP_SET_ARCH 0x12
35 #define SIGP_STAT_EQUIPMENT_CHECK 0x80000000UL
36 #define SIGP_STAT_INCORRECT_STATE 0x00000200UL
37 #define SIGP_STAT_INVALID_PARAMETER 0x00000100UL
38 #define SIGP_STAT_EXT_CALL_PENDING 0x00000080UL
39 #define SIGP_STAT_STOPPED 0x00000040UL
40 #define SIGP_STAT_OPERATOR_INTERV 0x00000020UL
41 #define SIGP_STAT_CHECK_STOP 0x00000010UL
42 #define SIGP_STAT_INOPERATIVE 0x00000004UL
43 #define SIGP_STAT_INVALID_ORDER 0x00000002UL
44 #define SIGP_STAT_RECEIVER_CHECK 0x00000001UL
47 static int __sigp_sense(struct kvm_vcpu
*vcpu
, u16 cpu_addr
,
50 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
53 if (cpu_addr
>= KVM_MAX_VCPUS
)
54 return 3; /* not operational */
57 if (fi
->local_int
[cpu_addr
] == NULL
)
58 rc
= 3; /* not operational */
59 else if (atomic_read(fi
->local_int
[cpu_addr
]->cpuflags
)
61 *reg
&= 0xffffffff00000000UL
;
62 rc
= 1; /* status stored */
64 *reg
&= 0xffffffff00000000UL
;
65 *reg
|= SIGP_STAT_STOPPED
;
66 rc
= 1; /* status stored */
68 spin_unlock(&fi
->lock
);
70 VCPU_EVENT(vcpu
, 4, "sensed status of cpu %x rc %x", cpu_addr
, rc
);
74 static int __sigp_emergency(struct kvm_vcpu
*vcpu
, u16 cpu_addr
)
76 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
77 struct kvm_s390_local_interrupt
*li
;
78 struct kvm_s390_interrupt_info
*inti
;
81 if (cpu_addr
>= KVM_MAX_VCPUS
)
82 return 3; /* not operational */
84 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
88 inti
->type
= KVM_S390_INT_EMERGENCY
;
91 li
= fi
->local_int
[cpu_addr
];
93 rc
= 3; /* not operational */
97 spin_lock_bh(&li
->lock
);
98 list_add_tail(&inti
->list
, &li
->list
);
99 atomic_set(&li
->active
, 1);
100 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
101 if (waitqueue_active(&li
->wq
))
102 wake_up_interruptible(&li
->wq
);
103 spin_unlock_bh(&li
->lock
);
104 rc
= 0; /* order accepted */
106 spin_unlock(&fi
->lock
);
107 VCPU_EVENT(vcpu
, 4, "sent sigp emerg to cpu %x", cpu_addr
);
111 static int __inject_sigp_stop(struct kvm_s390_local_interrupt
*li
, int action
)
113 struct kvm_s390_interrupt_info
*inti
;
115 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
118 inti
->type
= KVM_S390_SIGP_STOP
;
120 spin_lock_bh(&li
->lock
);
121 list_add_tail(&inti
->list
, &li
->list
);
122 atomic_set(&li
->active
, 1);
123 atomic_set_mask(CPUSTAT_STOP_INT
, li
->cpuflags
);
124 li
->action_bits
|= action
;
125 if (waitqueue_active(&li
->wq
))
126 wake_up_interruptible(&li
->wq
);
127 spin_unlock_bh(&li
->lock
);
129 return 0; /* order accepted */
132 static int __sigp_stop(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, int action
)
134 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
135 struct kvm_s390_local_interrupt
*li
;
138 if (cpu_addr
>= KVM_MAX_VCPUS
)
139 return 3; /* not operational */
141 spin_lock(&fi
->lock
);
142 li
= fi
->local_int
[cpu_addr
];
144 rc
= 3; /* not operational */
148 rc
= __inject_sigp_stop(li
, action
);
151 spin_unlock(&fi
->lock
);
152 VCPU_EVENT(vcpu
, 4, "sent sigp stop to cpu %x", cpu_addr
);
156 int kvm_s390_inject_sigp_stop(struct kvm_vcpu
*vcpu
, int action
)
158 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
159 return __inject_sigp_stop(li
, action
);
162 static int __sigp_set_arch(struct kvm_vcpu
*vcpu
, u32 parameter
)
166 switch (parameter
& 0xff) {
168 rc
= 3; /* not operational */
172 rc
= 0; /* order accepted */
180 static int __sigp_set_prefix(struct kvm_vcpu
*vcpu
, u16 cpu_addr
, u32 address
,
183 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
184 struct kvm_s390_local_interrupt
*li
= NULL
;
185 struct kvm_s390_interrupt_info
*inti
;
189 /* make sure that the new value is valid memory */
190 address
= address
& 0x7fffe000u
;
191 if ((copy_from_guest(vcpu
, &tmp
,
192 (u64
) (address
+ vcpu
->arch
.sie_block
->gmsor
) , 1)) ||
193 (copy_from_guest(vcpu
, &tmp
, (u64
) (address
+
194 vcpu
->arch
.sie_block
->gmsor
+ PAGE_SIZE
), 1))) {
195 *reg
|= SIGP_STAT_INVALID_PARAMETER
;
196 return 1; /* invalid parameter */
199 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
203 spin_lock(&fi
->lock
);
204 if (cpu_addr
< KVM_MAX_VCPUS
)
205 li
= fi
->local_int
[cpu_addr
];
208 rc
= 1; /* incorrect state */
209 *reg
&= SIGP_STAT_INCORRECT_STATE
;
214 spin_lock_bh(&li
->lock
);
215 /* cpu must be in stopped state */
216 if (atomic_read(li
->cpuflags
) & CPUSTAT_RUNNING
) {
217 rc
= 1; /* incorrect state */
218 *reg
&= SIGP_STAT_INCORRECT_STATE
;
223 inti
->type
= KVM_S390_SIGP_SET_PREFIX
;
224 inti
->prefix
.address
= address
;
226 list_add_tail(&inti
->list
, &li
->list
);
227 atomic_set(&li
->active
, 1);
228 if (waitqueue_active(&li
->wq
))
229 wake_up_interruptible(&li
->wq
);
230 rc
= 0; /* order accepted */
232 VCPU_EVENT(vcpu
, 4, "set prefix of cpu %02x to %x", cpu_addr
, address
);
234 spin_unlock_bh(&li
->lock
);
236 spin_unlock(&fi
->lock
);
240 int kvm_s390_handle_sigp(struct kvm_vcpu
*vcpu
)
242 int r1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
243 int r3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
244 int base2
= vcpu
->arch
.sie_block
->ipb
>> 28;
245 int disp2
= ((vcpu
->arch
.sie_block
->ipb
& 0x0fff0000) >> 16);
247 u16 cpu_addr
= vcpu
->arch
.guest_gprs
[r3
];
251 /* sigp in userspace can exit */
252 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
253 return kvm_s390_inject_program_int(vcpu
,
254 PGM_PRIVILEGED_OPERATION
);
258 order_code
+= vcpu
->arch
.guest_gprs
[base2
];
261 parameter
= vcpu
->arch
.guest_gprs
[r1
];
263 parameter
= vcpu
->arch
.guest_gprs
[r1
+ 1];
265 switch (order_code
) {
267 vcpu
->stat
.instruction_sigp_sense
++;
268 rc
= __sigp_sense(vcpu
, cpu_addr
,
269 &vcpu
->arch
.guest_gprs
[r1
]);
272 vcpu
->stat
.instruction_sigp_emergency
++;
273 rc
= __sigp_emergency(vcpu
, cpu_addr
);
276 vcpu
->stat
.instruction_sigp_stop
++;
277 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STOP_ON_STOP
);
279 case SIGP_STOP_STORE_STATUS
:
280 vcpu
->stat
.instruction_sigp_stop
++;
281 rc
= __sigp_stop(vcpu
, cpu_addr
, ACTION_STORE_ON_STOP
);
284 vcpu
->stat
.instruction_sigp_arch
++;
285 rc
= __sigp_set_arch(vcpu
, parameter
);
287 case SIGP_SET_PREFIX
:
288 vcpu
->stat
.instruction_sigp_prefix
++;
289 rc
= __sigp_set_prefix(vcpu
, cpu_addr
, parameter
,
290 &vcpu
->arch
.guest_gprs
[r1
]);
293 vcpu
->stat
.instruction_sigp_restart
++;
294 /* user space must know about restart */
302 vcpu
->arch
.sie_block
->gpsw
.mask
&= ~(3ul << 44);
303 vcpu
->arch
.sie_block
->gpsw
.mask
|= (rc
& 3ul) << 44;