Merge tag 'pull-nbd-2024-08-26' of https://repo.or.cz/qemu/ericb into staging
[qemu/armbru.git] / target / s390x / sigp.c
blobad0ad61177da589489c1d42947ceeb3a475b95e2
1 /*
2 * s390x SIGP instruction handling
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "cpu.h"
13 #include "s390x-internal.h"
14 #include "hw/boards.h"
15 #include "sysemu/hw_accel.h"
16 #include "sysemu/runstate.h"
17 #include "exec/address-spaces.h"
18 #include "exec/exec-all.h"
19 #include "sysemu/tcg.h"
20 #include "trace.h"
21 #include "qapi/qapi-types-machine.h"
23 QemuMutex qemu_sigp_mutex;
25 typedef struct SigpInfo {
26 uint64_t param;
27 int cc;
28 uint64_t *status_reg;
29 } SigpInfo;
31 static void set_sigp_status(SigpInfo *si, uint64_t status)
33 *si->status_reg &= 0xffffffff00000000ULL;
34 *si->status_reg |= status;
35 si->cc = SIGP_CC_STATUS_STORED;
38 static void sigp_sense(S390CPU *dst_cpu, SigpInfo *si)
40 uint8_t state = s390_cpu_get_state(dst_cpu);
41 bool ext_call = dst_cpu->env.pending_int & INTERRUPT_EXTERNAL_CALL;
42 uint64_t status = 0;
44 if (!tcg_enabled()) {
45 /* handled in KVM */
46 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
47 return;
50 /* sensing without locks is racy, but it's the same for real hw */
51 if (state != S390_CPU_STATE_STOPPED && !ext_call) {
52 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
53 } else {
54 if (ext_call) {
55 status |= SIGP_STAT_EXT_CALL_PENDING;
57 if (state == S390_CPU_STATE_STOPPED) {
58 status |= SIGP_STAT_STOPPED;
60 set_sigp_status(si, status);
64 static void sigp_external_call(S390CPU *src_cpu, S390CPU *dst_cpu, SigpInfo *si)
66 int ret;
68 if (!tcg_enabled()) {
69 /* handled in KVM */
70 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
71 return;
74 ret = cpu_inject_external_call(dst_cpu, src_cpu->env.core_id);
75 if (!ret) {
76 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
77 } else {
78 set_sigp_status(si, SIGP_STAT_EXT_CALL_PENDING);
82 static void sigp_emergency(S390CPU *src_cpu, S390CPU *dst_cpu, SigpInfo *si)
84 if (!tcg_enabled()) {
85 /* handled in KVM */
86 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
87 return;
90 cpu_inject_emergency_signal(dst_cpu, src_cpu->env.core_id);
91 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
94 static void sigp_start(CPUState *cs, run_on_cpu_data arg)
96 S390CPU *cpu = S390_CPU(cs);
97 SigpInfo *si = arg.host_ptr;
99 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_STOPPED) {
100 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
101 return;
104 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
105 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
108 static void sigp_stop(CPUState *cs, run_on_cpu_data arg)
110 S390CPU *cpu = S390_CPU(cs);
111 SigpInfo *si = arg.host_ptr;
113 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) {
114 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
115 return;
118 /* disabled wait - sleeping in user space */
119 if (cs->halted) {
120 s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
121 } else {
122 /* execute the stop function */
123 cpu->env.sigp_order = SIGP_STOP;
124 cpu_inject_stop(cpu);
126 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
129 static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)
131 S390CPU *cpu = S390_CPU(cs);
132 SigpInfo *si = arg.host_ptr;
134 /* disabled wait - sleeping in user space */
135 if (s390_cpu_get_state(cpu) == S390_CPU_STATE_OPERATING && cs->halted) {
136 s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
139 switch (s390_cpu_get_state(cpu)) {
140 case S390_CPU_STATE_OPERATING:
141 cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
142 cpu_inject_stop(cpu);
143 /* store will be performed in do_stop_interrupt() */
144 break;
145 case S390_CPU_STATE_STOPPED:
146 /* already stopped, just store the status */
147 cpu_synchronize_state(cs);
148 s390_store_status(cpu, S390_STORE_STATUS_DEF_ADDR, true);
149 break;
151 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
154 static void sigp_store_status_at_address(CPUState *cs, run_on_cpu_data arg)
156 S390CPU *cpu = S390_CPU(cs);
157 SigpInfo *si = arg.host_ptr;
158 uint32_t address = si->param & 0x7ffffe00u;
160 /* cpu has to be stopped */
161 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_STOPPED) {
162 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
163 return;
166 cpu_synchronize_state(cs);
168 if (s390_store_status(cpu, address, false)) {
169 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
170 return;
172 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
175 #define ADTL_SAVE_LC_MASK 0xfUL
176 static void sigp_store_adtl_status(CPUState *cs, run_on_cpu_data arg)
178 S390CPU *cpu = S390_CPU(cs);
179 SigpInfo *si = arg.host_ptr;
180 uint8_t lc = si->param & ADTL_SAVE_LC_MASK;
181 hwaddr addr = si->param & ~ADTL_SAVE_LC_MASK;
182 hwaddr len = 1UL << (lc ? lc : 10);
184 if (!s390_has_feat(S390_FEAT_VECTOR) &&
185 !s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
186 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
187 return;
190 /* cpu has to be stopped */
191 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_STOPPED) {
192 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
193 return;
196 /* address must be aligned to length */
197 if (addr & (len - 1)) {
198 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
199 return;
202 /* no GS: only lc == 0 is valid */
203 if (!s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
204 lc != 0) {
205 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
206 return;
209 /* GS: 0, 10, 11, 12 are valid */
210 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
211 lc != 0 &&
212 lc != 10 &&
213 lc != 11 &&
214 lc != 12) {
215 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
216 return;
219 cpu_synchronize_state(cs);
221 if (s390_store_adtl_status(cpu, addr, len)) {
222 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
223 return;
225 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
228 static void sigp_restart(CPUState *cs, run_on_cpu_data arg)
230 S390CPU *cpu = S390_CPU(cs);
231 SigpInfo *si = arg.host_ptr;
233 switch (s390_cpu_get_state(cpu)) {
234 case S390_CPU_STATE_STOPPED:
235 /* the restart irq has to be delivered prior to any other pending irq */
236 cpu_synchronize_state(cs);
238 * Set OPERATING (and unhalting) before loading the restart PSW.
239 * s390_cpu_set_psw() will then properly halt the CPU again if
240 * necessary (TCG).
242 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
243 do_restart_interrupt(&cpu->env);
244 break;
245 case S390_CPU_STATE_OPERATING:
246 cpu_inject_restart(cpu);
247 break;
249 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
252 static void sigp_initial_cpu_reset(CPUState *cs, run_on_cpu_data arg)
254 S390CPU *cpu = S390_CPU(cs);
255 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
256 SigpInfo *si = arg.host_ptr;
258 cpu_synchronize_state(cs);
259 scc->reset(cs, S390_CPU_RESET_INITIAL);
260 cpu_synchronize_post_reset(cs);
261 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
264 static void sigp_cpu_reset(CPUState *cs, run_on_cpu_data arg)
266 S390CPU *cpu = S390_CPU(cs);
267 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
268 SigpInfo *si = arg.host_ptr;
270 cpu_synchronize_state(cs);
271 scc->reset(cs, S390_CPU_RESET_NORMAL);
272 cpu_synchronize_post_reset(cs);
273 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
276 static void sigp_set_prefix(CPUState *cs, run_on_cpu_data arg)
278 S390CPU *cpu = S390_CPU(cs);
279 SigpInfo *si = arg.host_ptr;
280 uint32_t addr = si->param & 0x7fffe000u;
282 cpu_synchronize_state(cs);
284 if (!address_space_access_valid(&address_space_memory, addr,
285 sizeof(struct LowCore), false,
286 MEMTXATTRS_UNSPECIFIED)) {
287 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
288 return;
291 /* cpu has to be stopped */
292 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_STOPPED) {
293 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
294 return;
297 cpu->env.psa = addr;
298 tlb_flush(cs);
299 cpu_synchronize_post_init(cs);
300 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
303 static void sigp_cond_emergency(S390CPU *src_cpu, S390CPU *dst_cpu,
304 SigpInfo *si)
306 const uint64_t psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
307 uint16_t p_asn, s_asn, asn;
308 uint64_t psw_addr, psw_mask;
309 bool idle;
311 if (!tcg_enabled()) {
312 /* handled in KVM */
313 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
314 return;
317 /* this looks racy, but these values are only used when STOPPED */
318 idle = CPU(dst_cpu)->halted;
319 psw_addr = dst_cpu->env.psw.addr;
320 psw_mask = dst_cpu->env.psw.mask;
321 asn = si->param;
322 p_asn = dst_cpu->env.cregs[4] & 0xffff; /* Primary ASN */
323 s_asn = dst_cpu->env.cregs[3] & 0xffff; /* Secondary ASN */
325 if (s390_cpu_get_state(dst_cpu) != S390_CPU_STATE_STOPPED ||
326 (psw_mask & psw_int_mask) != psw_int_mask ||
327 (idle && psw_addr != 0) ||
328 (!idle && (asn == p_asn || asn == s_asn))) {
329 cpu_inject_emergency_signal(dst_cpu, src_cpu->env.core_id);
330 } else {
331 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
334 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
337 static void sigp_sense_running(S390CPU *dst_cpu, SigpInfo *si)
339 if (!tcg_enabled()) {
340 /* handled in KVM */
341 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
342 return;
345 /* sensing without locks is racy, but it's the same for real hw */
346 if (!s390_has_feat(S390_FEAT_SENSE_RUNNING_STATUS)) {
347 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
348 return;
351 /* If halted (which includes also STOPPED), it is not running */
352 if (CPU(dst_cpu)->halted) {
353 set_sigp_status(si, SIGP_STAT_NOT_RUNNING);
354 } else {
355 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
359 static int handle_sigp_single_dst(S390CPU *cpu, S390CPU *dst_cpu, uint8_t order,
360 uint64_t param, uint64_t *status_reg)
362 SigpInfo si = {
363 .param = param,
364 .status_reg = status_reg,
367 /* cpu available? */
368 if (dst_cpu == NULL) {
369 return SIGP_CC_NOT_OPERATIONAL;
372 /* only resets can break pending orders */
373 if (dst_cpu->env.sigp_order != 0 &&
374 order != SIGP_CPU_RESET &&
375 order != SIGP_INITIAL_CPU_RESET) {
376 return SIGP_CC_BUSY;
379 switch (order) {
380 case SIGP_SENSE:
381 sigp_sense(dst_cpu, &si);
382 break;
383 case SIGP_EXTERNAL_CALL:
384 sigp_external_call(cpu, dst_cpu, &si);
385 break;
386 case SIGP_EMERGENCY:
387 sigp_emergency(cpu, dst_cpu, &si);
388 break;
389 case SIGP_START:
390 run_on_cpu(CPU(dst_cpu), sigp_start, RUN_ON_CPU_HOST_PTR(&si));
391 break;
392 case SIGP_STOP:
393 run_on_cpu(CPU(dst_cpu), sigp_stop, RUN_ON_CPU_HOST_PTR(&si));
394 break;
395 case SIGP_RESTART:
396 run_on_cpu(CPU(dst_cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
397 break;
398 case SIGP_STOP_STORE_STATUS:
399 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, RUN_ON_CPU_HOST_PTR(&si));
400 break;
401 case SIGP_STORE_STATUS_ADDR:
402 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, RUN_ON_CPU_HOST_PTR(&si));
403 break;
404 case SIGP_STORE_ADTL_STATUS:
405 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, RUN_ON_CPU_HOST_PTR(&si));
406 break;
407 case SIGP_SET_PREFIX:
408 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, RUN_ON_CPU_HOST_PTR(&si));
409 break;
410 case SIGP_INITIAL_CPU_RESET:
411 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
412 break;
413 case SIGP_CPU_RESET:
414 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
415 break;
416 case SIGP_COND_EMERGENCY:
417 sigp_cond_emergency(cpu, dst_cpu, &si);
418 break;
419 case SIGP_SENSE_RUNNING:
420 sigp_sense_running(dst_cpu, &si);
421 break;
422 default:
423 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
426 return si.cc;
429 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
430 uint64_t *status_reg)
432 *status_reg &= 0xffffffff00000000ULL;
434 /* Reject set arch order, with czam we're always in z/Arch mode. */
435 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
436 return SIGP_CC_STATUS_STORED;
439 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
441 static MachineState *ms;
443 if (!ms) {
444 ms = MACHINE(qdev_get_machine());
445 g_assert(ms->possible_cpus);
448 /* CPU address corresponds to the core_id and the index */
449 if (cpu_addr >= ms->possible_cpus->len) {
450 return NULL;
452 return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
455 int handle_sigp(CPUS390XState *env, uint8_t order, uint64_t r1, uint64_t r3)
457 uint64_t *status_reg = &env->regs[r1];
458 uint64_t param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
459 S390CPU *cpu = env_archcpu(env);
460 S390CPU *dst_cpu = NULL;
461 int ret;
463 if (qemu_mutex_trylock(&qemu_sigp_mutex)) {
464 ret = SIGP_CC_BUSY;
465 goto out;
468 switch (order) {
469 case SIGP_SET_ARCH:
470 ret = sigp_set_architecture(cpu, param, status_reg);
471 break;
472 default:
473 /* all other sigp orders target a single vcpu */
474 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
475 ret = handle_sigp_single_dst(cpu, dst_cpu, order, param, status_reg);
477 qemu_mutex_unlock(&qemu_sigp_mutex);
479 out:
480 trace_sigp_finished(order, CPU(cpu)->cpu_index,
481 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
482 g_assert(ret >= 0);
484 return ret;
487 int s390_cpu_restart(S390CPU *cpu)
489 SigpInfo si = {};
491 run_on_cpu(CPU(cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
492 return 0;
495 void do_stop_interrupt(CPUS390XState *env)
497 S390CPU *cpu = env_archcpu(env);
500 * Complete the STOP operation before exposing the CPU as
501 * STOPPED to the system.
503 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
504 s390_store_status(cpu, S390_STORE_STATUS_DEF_ADDR, true);
506 env->sigp_order = 0;
507 if (s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu) == 0) {
508 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
510 env->pending_int &= ~INTERRUPT_STOP;
513 void s390_init_sigp(void)
515 qemu_mutex_init(&qemu_sigp_mutex);