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 #ifndef CONFIG_USER_ONLY
73 /* SCLP service call */
74 uint32_t HELPER(servc
)(CPUS390XState
*env
, uint64_t r1
, uint64_t r2
)
76 qemu_mutex_lock_iothread();
77 int r
= sclp_service_call(env
, r1
, r2
);
79 program_interrupt(env
, -r
, 4);
82 qemu_mutex_unlock_iothread();
86 void HELPER(diag
)(CPUS390XState
*env
, uint32_t r1
, uint32_t r3
, uint32_t num
)
93 qemu_mutex_lock_iothread();
94 r
= s390_virtio_hypercall(env
);
95 qemu_mutex_unlock_iothread();
103 handle_diag_308(env
, r1
, r3
);
112 program_interrupt(env
, PGM_OPERATION
, ILEN_AUTO
);
117 void HELPER(spx
)(CPUS390XState
*env
, uint64_t a1
)
119 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
120 uint32_t prefix
= a1
& 0x7fffe000;
123 HELPER_LOG("prefix: %#x\n", prefix
);
124 tlb_flush_page(cs
, 0);
125 tlb_flush_page(cs
, TARGET_PAGE_SIZE
);
129 uint64_t HELPER(stck
)(CPUS390XState
*env
)
133 time
= env
->tod_offset
+
134 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - env
->tod_basetime
);
139 /* Set Clock Comparator */
140 void HELPER(sckc
)(CPUS390XState
*env
, uint64_t time
)
148 /* difference between origins */
149 time
-= env
->tod_offset
;
152 time
= tod2time(time
);
154 timer_mod(env
->tod_timer
, env
->tod_basetime
+ time
);
157 /* Store Clock Comparator */
158 uint64_t HELPER(stckc
)(CPUS390XState
*env
)
164 void HELPER(spt
)(CPUS390XState
*env
, uint64_t time
)
171 time
= tod2time(time
);
173 env
->cputm
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + time
;
175 timer_mod(env
->cpu_timer
, env
->cputm
);
178 /* Store CPU Timer */
179 uint64_t HELPER(stpt
)(CPUS390XState
*env
)
181 return time2tod(env
->cputm
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
184 /* Store System Information */
185 uint32_t HELPER(stsi
)(CPUS390XState
*env
, uint64_t a0
,
186 uint64_t r0
, uint64_t r1
)
188 S390CPU
*cpu
= s390_env_get_cpu(env
);
192 if ((r0
& STSI_LEVEL_MASK
) <= STSI_LEVEL_3
&&
193 ((r0
& STSI_R0_RESERVED_MASK
) || (r1
& STSI_R1_RESERVED_MASK
))) {
194 /* valid function code, invalid reserved bits */
195 program_interrupt(env
, PGM_SPECIFICATION
, 4);
198 sel1
= r0
& STSI_R0_SEL1_MASK
;
199 sel2
= r1
& STSI_R1_SEL2_MASK
;
201 /* XXX: spec exception if sysib is not 4k-aligned */
203 switch (r0
& STSI_LEVEL_MASK
) {
205 if ((sel1
== 1) && (sel2
== 1)) {
206 /* Basic Machine Configuration */
207 struct sysib_111 sysib
;
210 memset(&sysib
, 0, sizeof(sysib
));
211 ebcdic_put(sysib
.manuf
, "QEMU ", 16);
212 /* same as machine type number in STORE CPU ID, but in EBCDIC */
213 snprintf(type
, ARRAY_SIZE(type
), "%X", cpu
->model
->def
->type
);
214 ebcdic_put(sysib
.type
, type
, 4);
215 /* model number (not stored in STORE CPU ID for z/Architecure) */
216 ebcdic_put(sysib
.model
, "QEMU ", 16);
217 ebcdic_put(sysib
.sequence
, "QEMU ", 16);
218 ebcdic_put(sysib
.plant
, "QEMU", 4);
219 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
220 } else if ((sel1
== 2) && (sel2
== 1)) {
221 /* Basic Machine CPU */
222 struct sysib_121 sysib
;
224 memset(&sysib
, 0, sizeof(sysib
));
225 /* XXX make different for different CPUs? */
226 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
227 ebcdic_put(sysib
.plant
, "QEMU", 4);
228 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
229 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
230 } else if ((sel1
== 2) && (sel2
== 2)) {
231 /* Basic Machine CPUs */
232 struct sysib_122 sysib
;
234 memset(&sysib
, 0, sizeof(sysib
));
235 stl_p(&sysib
.capability
, 0x443afc29);
236 /* XXX change when SMP comes */
237 stw_p(&sysib
.total_cpus
, 1);
238 stw_p(&sysib
.active_cpus
, 1);
239 stw_p(&sysib
.standby_cpus
, 0);
240 stw_p(&sysib
.reserved_cpus
, 0);
241 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
248 if ((sel1
== 2) && (sel2
== 1)) {
250 struct sysib_221 sysib
;
252 memset(&sysib
, 0, sizeof(sysib
));
253 /* XXX make different for different CPUs? */
254 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
255 ebcdic_put(sysib
.plant
, "QEMU", 4);
256 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
257 stw_p(&sysib
.cpu_id
, 0);
258 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
259 } else if ((sel1
== 2) && (sel2
== 2)) {
261 struct sysib_222 sysib
;
263 memset(&sysib
, 0, sizeof(sysib
));
264 stw_p(&sysib
.lpar_num
, 0);
266 /* XXX change when SMP comes */
267 stw_p(&sysib
.total_cpus
, 1);
268 stw_p(&sysib
.conf_cpus
, 1);
269 stw_p(&sysib
.standby_cpus
, 0);
270 stw_p(&sysib
.reserved_cpus
, 0);
271 ebcdic_put(sysib
.name
, "QEMU ", 8);
272 stl_p(&sysib
.caf
, 1000);
273 stw_p(&sysib
.dedicated_cpus
, 0);
274 stw_p(&sysib
.shared_cpus
, 0);
275 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
283 if ((sel1
== 2) && (sel2
== 2)) {
285 struct sysib_322 sysib
;
287 memset(&sysib
, 0, sizeof(sysib
));
289 /* XXX change when SMP comes */
290 stw_p(&sysib
.vm
[0].total_cpus
, 1);
291 stw_p(&sysib
.vm
[0].conf_cpus
, 1);
292 stw_p(&sysib
.vm
[0].standby_cpus
, 0);
293 stw_p(&sysib
.vm
[0].reserved_cpus
, 0);
294 ebcdic_put(sysib
.vm
[0].name
, "KVMguest", 8);
295 stl_p(&sysib
.vm
[0].caf
, 1000);
296 ebcdic_put(sysib
.vm
[0].cpi
, "KVM/Linux ", 16);
297 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
303 case STSI_LEVEL_CURRENT
:
304 env
->regs
[0] = STSI_LEVEL_3
;
314 uint32_t HELPER(sigp
)(CPUS390XState
*env
, uint64_t order_code
, uint32_t r1
,
317 int cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
319 HELPER_LOG("%s: %016" PRIx64
" %08x %016" PRIx64
"\n",
320 __func__
, order_code
, r1
, cpu_addr
);
322 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
323 as parameter (input). Status (output) is always R1. */
325 switch (order_code
& SIGP_ORDER_MASK
) {
330 /* enumerate CPU status */
332 /* XXX implement when SMP comes */
335 env
->regs
[r1
] &= 0xffffffff00000000ULL
;
338 #if !defined(CONFIG_USER_ONLY)
340 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
341 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
344 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
345 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
350 fprintf(stderr
, "XXX unknown sigp: 0x%" PRIx64
"\n", order_code
);
351 cc
= SIGP_CC_NOT_OPERATIONAL
;
358 #ifndef CONFIG_USER_ONLY
359 void HELPER(xsch
)(CPUS390XState
*env
, uint64_t r1
)
361 S390CPU
*cpu
= s390_env_get_cpu(env
);
362 qemu_mutex_lock_iothread();
363 ioinst_handle_xsch(cpu
, r1
);
364 qemu_mutex_unlock_iothread();
367 void HELPER(csch
)(CPUS390XState
*env
, uint64_t r1
)
369 S390CPU
*cpu
= s390_env_get_cpu(env
);
370 qemu_mutex_lock_iothread();
371 ioinst_handle_csch(cpu
, r1
);
372 qemu_mutex_unlock_iothread();
375 void HELPER(hsch
)(CPUS390XState
*env
, uint64_t r1
)
377 S390CPU
*cpu
= s390_env_get_cpu(env
);
378 qemu_mutex_lock_iothread();
379 ioinst_handle_hsch(cpu
, r1
);
380 qemu_mutex_unlock_iothread();
383 void HELPER(msch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
385 S390CPU
*cpu
= s390_env_get_cpu(env
);
386 qemu_mutex_lock_iothread();
387 ioinst_handle_msch(cpu
, r1
, inst
>> 16);
388 qemu_mutex_unlock_iothread();
391 void HELPER(rchp
)(CPUS390XState
*env
, uint64_t r1
)
393 S390CPU
*cpu
= s390_env_get_cpu(env
);
394 qemu_mutex_lock_iothread();
395 ioinst_handle_rchp(cpu
, r1
);
396 qemu_mutex_unlock_iothread();
399 void HELPER(rsch
)(CPUS390XState
*env
, uint64_t r1
)
401 S390CPU
*cpu
= s390_env_get_cpu(env
);
402 qemu_mutex_lock_iothread();
403 ioinst_handle_rsch(cpu
, r1
);
404 qemu_mutex_unlock_iothread();
407 void HELPER(ssch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
409 S390CPU
*cpu
= s390_env_get_cpu(env
);
410 qemu_mutex_lock_iothread();
411 ioinst_handle_ssch(cpu
, r1
, inst
>> 16);
412 qemu_mutex_unlock_iothread();
415 void HELPER(stsch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
417 S390CPU
*cpu
= s390_env_get_cpu(env
);
418 qemu_mutex_lock_iothread();
419 ioinst_handle_stsch(cpu
, r1
, inst
>> 16);
420 qemu_mutex_unlock_iothread();
423 void HELPER(tsch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
425 S390CPU
*cpu
= s390_env_get_cpu(env
);
426 qemu_mutex_lock_iothread();
427 ioinst_handle_tsch(cpu
, r1
, inst
>> 16);
428 qemu_mutex_unlock_iothread();
431 void HELPER(chsc
)(CPUS390XState
*env
, uint64_t inst
)
433 S390CPU
*cpu
= s390_env_get_cpu(env
);
434 qemu_mutex_lock_iothread();
435 ioinst_handle_chsc(cpu
, inst
>> 16);
436 qemu_mutex_unlock_iothread();
440 #ifndef CONFIG_USER_ONLY
441 void HELPER(per_check_exception
)(CPUS390XState
*env
)
443 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
445 if (env
->per_perc_atmid
) {
446 env
->int_pgm_code
= PGM_PER
;
447 env
->int_pgm_ilen
= get_ilen(cpu_ldub_code(env
, env
->per_address
));
449 cs
->exception_index
= EXCP_PGM
;
454 void HELPER(per_branch
)(CPUS390XState
*env
, uint64_t from
, uint64_t to
)
456 if ((env
->cregs
[9] & PER_CR9_EVENT_BRANCH
)) {
457 if (!(env
->cregs
[9] & PER_CR9_CONTROL_BRANCH_ADDRESS
)
458 || get_per_in_range(env
, to
)) {
459 env
->per_address
= from
;
460 env
->per_perc_atmid
= PER_CODE_EVENT_BRANCH
| get_per_atmid(env
);
465 void HELPER(per_ifetch
)(CPUS390XState
*env
, uint64_t addr
)
467 if ((env
->cregs
[9] & PER_CR9_EVENT_IFETCH
) && get_per_in_range(env
, addr
)) {
468 env
->per_address
= addr
;
469 env
->per_perc_atmid
= PER_CODE_EVENT_IFETCH
| get_per_atmid(env
);
471 /* If the instruction has to be nullified, trigger the
472 exception immediately. */
473 if (env
->cregs
[9] & PER_CR9_EVENT_NULLIFICATION
) {
474 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
476 env
->per_perc_atmid
|= PER_CODE_EVENT_NULLIFICATION
;
477 env
->int_pgm_code
= PGM_PER
;
478 env
->int_pgm_ilen
= get_ilen(cpu_ldub_code(env
, addr
));
480 cs
->exception_index
= EXCP_PGM
;
487 /* The maximum bit defined at the moment is 129. */
488 #define MAX_STFL_WORDS 3
490 /* Canonicalize the current cpu's features into the 64-bit words required
491 by STFLE. Return the index-1 of the max word that is non-zero. */
492 static unsigned do_stfle(CPUS390XState
*env
, uint64_t words
[MAX_STFL_WORDS
])
494 S390CPU
*cpu
= s390_env_get_cpu(env
);
495 const unsigned long *features
= cpu
->model
->features
;
496 unsigned max_bit
= 0;
499 memset(words
, 0, sizeof(uint64_t) * MAX_STFL_WORDS
);
501 if (test_bit(S390_FEAT_ZARCH
, features
)) {
502 /* z/Architecture is always active if around */
503 words
[0] = 1ull << (63 - 2);
506 for (feat
= find_first_bit(features
, S390_FEAT_MAX
);
507 feat
< S390_FEAT_MAX
;
508 feat
= find_next_bit(features
, S390_FEAT_MAX
, feat
+ 1)) {
509 const S390FeatDef
*def
= s390_feat_def(feat
);
510 if (def
->type
== S390_FEAT_TYPE_STFL
) {
511 unsigned bit
= def
->bit
;
515 assert(bit
/ 64 < MAX_STFL_WORDS
);
516 words
[bit
/ 64] |= 1ULL << (63 - bit
% 64);
523 void HELPER(stfl
)(CPUS390XState
*env
)
525 uint64_t words
[MAX_STFL_WORDS
];
527 do_stfle(env
, words
);
528 cpu_stl_data(env
, 200, words
[0] >> 32);
531 uint32_t HELPER(stfle
)(CPUS390XState
*env
, uint64_t addr
)
533 uint64_t words
[MAX_STFL_WORDS
];
534 unsigned count_m1
= env
->regs
[0] & 0xff;
535 unsigned max_m1
= do_stfle(env
, words
);
538 for (i
= 0; i
<= count_m1
; ++i
) {
539 cpu_stq_data(env
, addr
+ 8 * i
, words
[i
]);
542 env
->regs
[0] = deposit64(env
->regs
[0], 0, 8, max_m1
);
543 return (count_m1
>= max_m1
? 0 : 3);