2 * S/390 misc helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/main-loop.h"
24 #include "exec/memory.h"
25 #include "qemu/host-utils.h"
26 #include "exec/helper-proto.h"
27 #include "qemu/timer.h"
28 #include "exec/address-spaces.h"
29 #include "exec/exec-all.h"
30 #include "exec/cpu_ldst.h"
32 #if !defined(CONFIG_USER_ONLY)
33 #include "sysemu/cpus.h"
34 #include "sysemu/sysemu.h"
35 #include "hw/s390x/ebcdic.h"
38 /* #define DEBUG_HELPER */
40 #define HELPER_LOG(x...) qemu_log(x)
42 #define HELPER_LOG(x...)
45 /* Raise an exception dynamically from a helper function. */
46 void QEMU_NORETURN
runtime_exception(CPUS390XState
*env
, int excp
,
49 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
51 cs
->exception_index
= EXCP_PGM
;
52 env
->int_pgm_code
= excp
;
53 env
->int_pgm_ilen
= ILEN_AUTO
;
55 /* Use the (ultimate) callers address to find the insn that trapped. */
56 cpu_restore_state(cs
, retaddr
);
61 /* Raise an exception statically from a TB. */
62 void HELPER(exception
)(CPUS390XState
*env
, uint32_t excp
)
64 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
66 HELPER_LOG("%s: exception %d\n", __func__
, excp
);
67 cs
->exception_index
= excp
;
71 void program_interrupt(CPUS390XState
*env
, uint32_t code
, int ilen
)
73 S390CPU
*cpu
= s390_env_get_cpu(env
);
75 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
80 struct kvm_s390_irq irq
= {
81 .type
= KVM_S390_PROGRAM_INT
,
85 kvm_s390_vcpu_interrupt(cpu
, &irq
);
88 CPUState
*cs
= CPU(cpu
);
90 env
->int_pgm_code
= code
;
91 env
->int_pgm_ilen
= ilen
;
92 cs
->exception_index
= EXCP_PGM
;
97 #ifndef CONFIG_USER_ONLY
99 /* SCLP service call */
100 uint32_t HELPER(servc
)(CPUS390XState
*env
, uint64_t r1
, uint64_t r2
)
102 qemu_mutex_lock_iothread();
103 int r
= sclp_service_call(env
, r1
, r2
);
105 program_interrupt(env
, -r
, 4);
108 qemu_mutex_unlock_iothread();
112 void HELPER(diag
)(CPUS390XState
*env
, uint32_t r1
, uint32_t r3
, uint32_t num
)
119 qemu_mutex_lock_iothread();
120 r
= s390_virtio_hypercall(env
);
121 qemu_mutex_unlock_iothread();
129 handle_diag_308(env
, r1
, r3
);
138 program_interrupt(env
, PGM_OPERATION
, ILEN_AUTO
);
143 void HELPER(spx
)(CPUS390XState
*env
, uint64_t a1
)
145 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
146 uint32_t prefix
= a1
& 0x7fffe000;
149 HELPER_LOG("prefix: %#x\n", prefix
);
150 tlb_flush_page(cs
, 0);
151 tlb_flush_page(cs
, TARGET_PAGE_SIZE
);
155 uint64_t HELPER(stck
)(CPUS390XState
*env
)
159 time
= env
->tod_offset
+
160 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - env
->tod_basetime
);
165 /* Set Clock Comparator */
166 void HELPER(sckc
)(CPUS390XState
*env
, uint64_t time
)
174 /* difference between origins */
175 time
-= env
->tod_offset
;
178 time
= tod2time(time
);
180 timer_mod(env
->tod_timer
, env
->tod_basetime
+ time
);
183 /* Store Clock Comparator */
184 uint64_t HELPER(stckc
)(CPUS390XState
*env
)
190 void HELPER(spt
)(CPUS390XState
*env
, uint64_t time
)
197 time
= tod2time(time
);
199 env
->cputm
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + time
;
201 timer_mod(env
->cpu_timer
, env
->cputm
);
204 /* Store CPU Timer */
205 uint64_t HELPER(stpt
)(CPUS390XState
*env
)
207 return time2tod(env
->cputm
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
210 /* Store System Information */
211 uint32_t HELPER(stsi
)(CPUS390XState
*env
, uint64_t a0
,
212 uint64_t r0
, uint64_t r1
)
214 S390CPU
*cpu
= s390_env_get_cpu(env
);
218 if ((r0
& STSI_LEVEL_MASK
) <= STSI_LEVEL_3
&&
219 ((r0
& STSI_R0_RESERVED_MASK
) || (r1
& STSI_R1_RESERVED_MASK
))) {
220 /* valid function code, invalid reserved bits */
221 program_interrupt(env
, PGM_SPECIFICATION
, 2);
224 sel1
= r0
& STSI_R0_SEL1_MASK
;
225 sel2
= r1
& STSI_R1_SEL2_MASK
;
227 /* XXX: spec exception if sysib is not 4k-aligned */
229 switch (r0
& STSI_LEVEL_MASK
) {
231 if ((sel1
== 1) && (sel2
== 1)) {
232 /* Basic Machine Configuration */
233 struct sysib_111 sysib
;
236 memset(&sysib
, 0, sizeof(sysib
));
237 ebcdic_put(sysib
.manuf
, "QEMU ", 16);
238 /* same as machine type number in STORE CPU ID, but in EBCDIC */
239 snprintf(type
, ARRAY_SIZE(type
), "%X", cpu
->model
->def
->type
);
240 ebcdic_put(sysib
.type
, type
, 4);
241 /* model number (not stored in STORE CPU ID for z/Architecure) */
242 ebcdic_put(sysib
.model
, "QEMU ", 16);
243 ebcdic_put(sysib
.sequence
, "QEMU ", 16);
244 ebcdic_put(sysib
.plant
, "QEMU", 4);
245 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
246 } else if ((sel1
== 2) && (sel2
== 1)) {
247 /* Basic Machine CPU */
248 struct sysib_121 sysib
;
250 memset(&sysib
, 0, sizeof(sysib
));
251 /* XXX make different for different CPUs? */
252 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
253 ebcdic_put(sysib
.plant
, "QEMU", 4);
254 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
255 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
256 } else if ((sel1
== 2) && (sel2
== 2)) {
257 /* Basic Machine CPUs */
258 struct sysib_122 sysib
;
260 memset(&sysib
, 0, sizeof(sysib
));
261 stl_p(&sysib
.capability
, 0x443afc29);
262 /* XXX change when SMP comes */
263 stw_p(&sysib
.total_cpus
, 1);
264 stw_p(&sysib
.active_cpus
, 1);
265 stw_p(&sysib
.standby_cpus
, 0);
266 stw_p(&sysib
.reserved_cpus
, 0);
267 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
274 if ((sel1
== 2) && (sel2
== 1)) {
276 struct sysib_221 sysib
;
278 memset(&sysib
, 0, sizeof(sysib
));
279 /* XXX make different for different CPUs? */
280 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
281 ebcdic_put(sysib
.plant
, "QEMU", 4);
282 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
283 stw_p(&sysib
.cpu_id
, 0);
284 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
285 } else if ((sel1
== 2) && (sel2
== 2)) {
287 struct sysib_222 sysib
;
289 memset(&sysib
, 0, sizeof(sysib
));
290 stw_p(&sysib
.lpar_num
, 0);
292 /* XXX change when SMP comes */
293 stw_p(&sysib
.total_cpus
, 1);
294 stw_p(&sysib
.conf_cpus
, 1);
295 stw_p(&sysib
.standby_cpus
, 0);
296 stw_p(&sysib
.reserved_cpus
, 0);
297 ebcdic_put(sysib
.name
, "QEMU ", 8);
298 stl_p(&sysib
.caf
, 1000);
299 stw_p(&sysib
.dedicated_cpus
, 0);
300 stw_p(&sysib
.shared_cpus
, 0);
301 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
309 if ((sel1
== 2) && (sel2
== 2)) {
311 struct sysib_322 sysib
;
313 memset(&sysib
, 0, sizeof(sysib
));
315 /* XXX change when SMP comes */
316 stw_p(&sysib
.vm
[0].total_cpus
, 1);
317 stw_p(&sysib
.vm
[0].conf_cpus
, 1);
318 stw_p(&sysib
.vm
[0].standby_cpus
, 0);
319 stw_p(&sysib
.vm
[0].reserved_cpus
, 0);
320 ebcdic_put(sysib
.vm
[0].name
, "KVMguest", 8);
321 stl_p(&sysib
.vm
[0].caf
, 1000);
322 ebcdic_put(sysib
.vm
[0].cpi
, "KVM/Linux ", 16);
323 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
329 case STSI_LEVEL_CURRENT
:
330 env
->regs
[0] = STSI_LEVEL_3
;
340 uint32_t HELPER(sigp
)(CPUS390XState
*env
, uint64_t order_code
, uint32_t r1
,
343 int cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
345 HELPER_LOG("%s: %016" PRIx64
" %08x %016" PRIx64
"\n",
346 __func__
, order_code
, r1
, cpu_addr
);
348 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
349 as parameter (input). Status (output) is always R1. */
351 switch (order_code
& SIGP_ORDER_MASK
) {
356 /* enumerate CPU status */
358 /* XXX implement when SMP comes */
361 env
->regs
[r1
] &= 0xffffffff00000000ULL
;
364 #if !defined(CONFIG_USER_ONLY)
366 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
367 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
370 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
371 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
376 fprintf(stderr
, "XXX unknown sigp: 0x%" PRIx64
"\n", order_code
);
377 cc
= SIGP_CC_NOT_OPERATIONAL
;
384 #ifndef CONFIG_USER_ONLY
385 void HELPER(xsch
)(CPUS390XState
*env
, uint64_t r1
)
387 S390CPU
*cpu
= s390_env_get_cpu(env
);
388 qemu_mutex_lock_iothread();
389 ioinst_handle_xsch(cpu
, r1
);
390 qemu_mutex_unlock_iothread();
393 void HELPER(csch
)(CPUS390XState
*env
, uint64_t r1
)
395 S390CPU
*cpu
= s390_env_get_cpu(env
);
396 qemu_mutex_lock_iothread();
397 ioinst_handle_csch(cpu
, r1
);
398 qemu_mutex_unlock_iothread();
401 void HELPER(hsch
)(CPUS390XState
*env
, uint64_t r1
)
403 S390CPU
*cpu
= s390_env_get_cpu(env
);
404 qemu_mutex_lock_iothread();
405 ioinst_handle_hsch(cpu
, r1
);
406 qemu_mutex_unlock_iothread();
409 void HELPER(msch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
411 S390CPU
*cpu
= s390_env_get_cpu(env
);
412 qemu_mutex_lock_iothread();
413 ioinst_handle_msch(cpu
, r1
, inst
>> 16);
414 qemu_mutex_unlock_iothread();
417 void HELPER(rchp
)(CPUS390XState
*env
, uint64_t r1
)
419 S390CPU
*cpu
= s390_env_get_cpu(env
);
420 qemu_mutex_lock_iothread();
421 ioinst_handle_rchp(cpu
, r1
);
422 qemu_mutex_unlock_iothread();
425 void HELPER(rsch
)(CPUS390XState
*env
, uint64_t r1
)
427 S390CPU
*cpu
= s390_env_get_cpu(env
);
428 qemu_mutex_lock_iothread();
429 ioinst_handle_rsch(cpu
, r1
);
430 qemu_mutex_unlock_iothread();
433 void HELPER(ssch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
435 S390CPU
*cpu
= s390_env_get_cpu(env
);
436 qemu_mutex_lock_iothread();
437 ioinst_handle_ssch(cpu
, r1
, inst
>> 16);
438 qemu_mutex_unlock_iothread();
441 void HELPER(stsch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
443 S390CPU
*cpu
= s390_env_get_cpu(env
);
444 qemu_mutex_lock_iothread();
445 ioinst_handle_stsch(cpu
, r1
, inst
>> 16);
446 qemu_mutex_unlock_iothread();
449 void HELPER(tsch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
451 S390CPU
*cpu
= s390_env_get_cpu(env
);
452 qemu_mutex_lock_iothread();
453 ioinst_handle_tsch(cpu
, r1
, inst
>> 16);
454 qemu_mutex_unlock_iothread();
457 void HELPER(chsc
)(CPUS390XState
*env
, uint64_t inst
)
459 S390CPU
*cpu
= s390_env_get_cpu(env
);
460 qemu_mutex_lock_iothread();
461 ioinst_handle_chsc(cpu
, inst
>> 16);
462 qemu_mutex_unlock_iothread();
466 #ifndef CONFIG_USER_ONLY
467 void HELPER(per_check_exception
)(CPUS390XState
*env
)
469 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
471 if (env
->per_perc_atmid
) {
472 env
->int_pgm_code
= PGM_PER
;
473 env
->int_pgm_ilen
= get_ilen(cpu_ldub_code(env
, env
->per_address
));
475 cs
->exception_index
= EXCP_PGM
;
480 void HELPER(per_branch
)(CPUS390XState
*env
, uint64_t from
, uint64_t to
)
482 if ((env
->cregs
[9] & PER_CR9_EVENT_BRANCH
)) {
483 if (!(env
->cregs
[9] & PER_CR9_CONTROL_BRANCH_ADDRESS
)
484 || get_per_in_range(env
, to
)) {
485 env
->per_address
= from
;
486 env
->per_perc_atmid
= PER_CODE_EVENT_BRANCH
| get_per_atmid(env
);
491 void HELPER(per_ifetch
)(CPUS390XState
*env
, uint64_t addr
)
493 if ((env
->cregs
[9] & PER_CR9_EVENT_IFETCH
) && get_per_in_range(env
, addr
)) {
494 env
->per_address
= addr
;
495 env
->per_perc_atmid
= PER_CODE_EVENT_IFETCH
| get_per_atmid(env
);
497 /* If the instruction has to be nullified, trigger the
498 exception immediately. */
499 if (env
->cregs
[9] & PER_CR9_EVENT_NULLIFICATION
) {
500 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
502 env
->per_perc_atmid
|= PER_CODE_EVENT_NULLIFICATION
;
503 env
->int_pgm_code
= PGM_PER
;
504 env
->int_pgm_ilen
= get_ilen(cpu_ldub_code(env
, addr
));
506 cs
->exception_index
= EXCP_PGM
;
513 /* The maximum bit defined at the moment is 129. */
514 #define MAX_STFL_WORDS 3
516 /* Canonicalize the current cpu's features into the 64-bit words required
517 by STFLE. Return the index-1 of the max word that is non-zero. */
518 static unsigned do_stfle(CPUS390XState
*env
, uint64_t words
[MAX_STFL_WORDS
])
520 S390CPU
*cpu
= s390_env_get_cpu(env
);
521 const unsigned long *features
= cpu
->model
->features
;
522 unsigned max_bit
= 0;
525 memset(words
, 0, sizeof(uint64_t) * MAX_STFL_WORDS
);
527 if (test_bit(S390_FEAT_ZARCH
, features
)) {
528 /* z/Architecture is always active if around */
529 words
[0] = 1ull << (63 - 2);
532 for (feat
= find_first_bit(features
, S390_FEAT_MAX
);
533 feat
< S390_FEAT_MAX
;
534 feat
= find_next_bit(features
, S390_FEAT_MAX
, feat
+ 1)) {
535 const S390FeatDef
*def
= s390_feat_def(feat
);
536 if (def
->type
== S390_FEAT_TYPE_STFL
) {
537 unsigned bit
= def
->bit
;
541 assert(bit
/ 64 < MAX_STFL_WORDS
);
542 words
[bit
/ 64] |= 1ULL << (63 - bit
% 64);
549 void HELPER(stfl
)(CPUS390XState
*env
)
551 uint64_t words
[MAX_STFL_WORDS
];
553 do_stfle(env
, words
);
554 cpu_stl_data(env
, 200, words
[0] >> 32);
557 uint32_t HELPER(stfle
)(CPUS390XState
*env
, uint64_t addr
)
559 uint64_t words
[MAX_STFL_WORDS
];
560 unsigned count_m1
= env
->regs
[0] & 0xff;
561 unsigned max_m1
= do_stfle(env
, words
);
564 for (i
= 0; i
<= count_m1
; ++i
) {
565 cpu_stq_data(env
, addr
+ 8 * i
, words
[i
]);
568 env
->regs
[0] = deposit64(env
->regs
[0], 0, 8, max_m1
);
569 return (count_m1
>= max_m1
? 0 : 3);