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/>.
22 #include "exec/memory.h"
23 #include "qemu/host-utils.h"
26 #include "sysemu/kvm.h"
27 #include "qemu/timer.h"
29 #include <linux/kvm.h>
32 #if !defined(CONFIG_USER_ONLY)
33 #include "exec/softmmu_exec.h"
34 #include "sysemu/cpus.h"
35 #include "sysemu/sysemu.h"
36 #include "hw/s390x/ebcdic.h"
39 /* #define DEBUG_HELPER */
41 #define HELPER_LOG(x...) qemu_log(x)
43 #define HELPER_LOG(x...)
46 /* Raise an exception dynamically from a helper function. */
47 void QEMU_NORETURN
runtime_exception(CPUS390XState
*env
, int excp
,
50 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
53 cs
->exception_index
= EXCP_PGM
;
54 env
->int_pgm_code
= excp
;
56 /* Use the (ultimate) callers address to find the insn that trapped. */
57 cpu_restore_state(cs
, retaddr
);
59 /* Advance past the insn. */
60 t
= cpu_ldub_code(env
, env
->psw
.addr
);
61 env
->int_pgm_ilen
= t
= get_ilen(t
);
62 env
->psw
.addr
+= 2 * t
;
67 /* Raise an exception statically from a TB. */
68 void HELPER(exception
)(CPUS390XState
*env
, uint32_t excp
)
70 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
72 HELPER_LOG("%s: exception %d\n", __func__
, excp
);
73 cs
->exception_index
= excp
;
77 #ifndef CONFIG_USER_ONLY
79 void program_interrupt(CPUS390XState
*env
, uint32_t code
, int ilen
)
81 S390CPU
*cpu
= s390_env_get_cpu(env
);
83 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
88 kvm_s390_interrupt(cpu
, KVM_S390_PROGRAM_INT
, code
);
91 CPUState
*cs
= CPU(cpu
);
93 env
->int_pgm_code
= code
;
94 env
->int_pgm_ilen
= ilen
;
95 cs
->exception_index
= EXCP_PGM
;
100 /* SCLP service call */
101 uint32_t HELPER(servc
)(CPUS390XState
*env
, uint64_t r1
, uint64_t r2
)
103 int r
= sclp_service_call(env
, r1
, r2
);
105 program_interrupt(env
, -r
, 4);
111 #ifndef CONFIG_USER_ONLY
112 static void cpu_reset_all(void)
118 scc
= S390_CPU_GET_CLASS(cs
);
123 static void cpu_full_reset_all(void)
132 static int modified_clear_reset(S390CPU
*cpu
)
134 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
137 cpu_synchronize_all_states();
138 cpu_full_reset_all();
139 io_subsystem_reset();
140 scc
->load_normal(CPU(cpu
));
141 cpu_synchronize_all_post_reset();
146 static int load_normal_reset(S390CPU
*cpu
)
148 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
151 cpu_synchronize_all_states();
153 io_subsystem_reset();
154 scc
->initial_cpu_reset(CPU(cpu
));
155 scc
->load_normal(CPU(cpu
));
156 cpu_synchronize_all_post_reset();
161 #define DIAG_308_RC_NO_CONF 0x0102
162 #define DIAG_308_RC_INVALID 0x0402
163 void handle_diag_308(CPUS390XState
*env
, uint64_t r1
, uint64_t r3
)
165 uint64_t addr
= env
->regs
[r1
];
166 uint64_t subcode
= env
->regs
[r3
];
168 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
169 program_interrupt(env
, PGM_PRIVILEGED
, ILEN_LATER_INC
);
173 if ((subcode
& ~0x0ffffULL
) || (subcode
> 6)) {
174 program_interrupt(env
, PGM_SPECIFICATION
, ILEN_LATER_INC
);
180 modified_clear_reset(s390_env_get_cpu(env
));
183 load_normal_reset(s390_env_get_cpu(env
));
186 if ((r1
& 1) || (addr
& 0x0fffULL
)) {
187 program_interrupt(env
, PGM_SPECIFICATION
, ILEN_LATER_INC
);
190 env
->regs
[r1
+1] = DIAG_308_RC_INVALID
;
193 if ((r1
& 1) || (addr
& 0x0fffULL
)) {
194 program_interrupt(env
, PGM_SPECIFICATION
, ILEN_LATER_INC
);
197 env
->regs
[r1
+1] = DIAG_308_RC_NO_CONF
;
200 hw_error("Unhandled diag308 subcode %" PRIx64
, subcode
);
207 uint64_t HELPER(diag
)(CPUS390XState
*env
, uint32_t num
, uint64_t mem
,
215 r
= s390_virtio_hypercall(env
);
231 program_interrupt(env
, PGM_OPERATION
, ILEN_LATER_INC
);
238 void HELPER(spx
)(CPUS390XState
*env
, uint64_t a1
)
240 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
241 uint32_t prefix
= a1
& 0x7fffe000;
244 qemu_log("prefix: %#x\n", prefix
);
245 tlb_flush_page(cs
, 0);
246 tlb_flush_page(cs
, TARGET_PAGE_SIZE
);
249 static inline uint64_t clock_value(CPUS390XState
*env
)
253 time
= env
->tod_offset
+
254 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - env
->tod_basetime
);
260 uint64_t HELPER(stck
)(CPUS390XState
*env
)
262 return clock_value(env
);
265 /* Set Clock Comparator */
266 void HELPER(sckc
)(CPUS390XState
*env
, uint64_t time
)
272 /* difference between now and then */
273 time
-= clock_value(env
);
275 time
= (time
* 125) >> 9;
277 timer_mod(env
->tod_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + time
);
280 /* Store Clock Comparator */
281 uint64_t HELPER(stckc
)(CPUS390XState
*env
)
288 void HELPER(spt
)(CPUS390XState
*env
, uint64_t time
)
295 time
= (time
* 125) >> 9;
297 timer_mod(env
->cpu_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + time
);
300 /* Store CPU Timer */
301 uint64_t HELPER(stpt
)(CPUS390XState
*env
)
307 /* Store System Information */
308 uint32_t HELPER(stsi
)(CPUS390XState
*env
, uint64_t a0
,
309 uint64_t r0
, uint64_t r1
)
314 if ((r0
& STSI_LEVEL_MASK
) <= STSI_LEVEL_3
&&
315 ((r0
& STSI_R0_RESERVED_MASK
) || (r1
& STSI_R1_RESERVED_MASK
))) {
316 /* valid function code, invalid reserved bits */
317 program_interrupt(env
, PGM_SPECIFICATION
, 2);
320 sel1
= r0
& STSI_R0_SEL1_MASK
;
321 sel2
= r1
& STSI_R1_SEL2_MASK
;
323 /* XXX: spec exception if sysib is not 4k-aligned */
325 switch (r0
& STSI_LEVEL_MASK
) {
327 if ((sel1
== 1) && (sel2
== 1)) {
328 /* Basic Machine Configuration */
329 struct sysib_111 sysib
;
331 memset(&sysib
, 0, sizeof(sysib
));
332 ebcdic_put(sysib
.manuf
, "QEMU ", 16);
333 /* same as machine type number in STORE CPU ID */
334 ebcdic_put(sysib
.type
, "QEMU", 4);
335 /* same as model number in STORE CPU ID */
336 ebcdic_put(sysib
.model
, "QEMU ", 16);
337 ebcdic_put(sysib
.sequence
, "QEMU ", 16);
338 ebcdic_put(sysib
.plant
, "QEMU", 4);
339 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
340 } else if ((sel1
== 2) && (sel2
== 1)) {
341 /* Basic Machine CPU */
342 struct sysib_121 sysib
;
344 memset(&sysib
, 0, sizeof(sysib
));
345 /* XXX make different for different CPUs? */
346 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
347 ebcdic_put(sysib
.plant
, "QEMU", 4);
348 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
349 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
350 } else if ((sel1
== 2) && (sel2
== 2)) {
351 /* Basic Machine CPUs */
352 struct sysib_122 sysib
;
354 memset(&sysib
, 0, sizeof(sysib
));
355 stl_p(&sysib
.capability
, 0x443afc29);
356 /* XXX change when SMP comes */
357 stw_p(&sysib
.total_cpus
, 1);
358 stw_p(&sysib
.active_cpus
, 1);
359 stw_p(&sysib
.standby_cpus
, 0);
360 stw_p(&sysib
.reserved_cpus
, 0);
361 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
368 if ((sel1
== 2) && (sel2
== 1)) {
370 struct sysib_221 sysib
;
372 memset(&sysib
, 0, sizeof(sysib
));
373 /* XXX make different for different CPUs? */
374 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
375 ebcdic_put(sysib
.plant
, "QEMU", 4);
376 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
377 stw_p(&sysib
.cpu_id
, 0);
378 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
379 } else if ((sel1
== 2) && (sel2
== 2)) {
381 struct sysib_222 sysib
;
383 memset(&sysib
, 0, sizeof(sysib
));
384 stw_p(&sysib
.lpar_num
, 0);
386 /* XXX change when SMP comes */
387 stw_p(&sysib
.total_cpus
, 1);
388 stw_p(&sysib
.conf_cpus
, 1);
389 stw_p(&sysib
.standby_cpus
, 0);
390 stw_p(&sysib
.reserved_cpus
, 0);
391 ebcdic_put(sysib
.name
, "QEMU ", 8);
392 stl_p(&sysib
.caf
, 1000);
393 stw_p(&sysib
.dedicated_cpus
, 0);
394 stw_p(&sysib
.shared_cpus
, 0);
395 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
403 if ((sel1
== 2) && (sel2
== 2)) {
405 struct sysib_322 sysib
;
407 memset(&sysib
, 0, sizeof(sysib
));
409 /* XXX change when SMP comes */
410 stw_p(&sysib
.vm
[0].total_cpus
, 1);
411 stw_p(&sysib
.vm
[0].conf_cpus
, 1);
412 stw_p(&sysib
.vm
[0].standby_cpus
, 0);
413 stw_p(&sysib
.vm
[0].reserved_cpus
, 0);
414 ebcdic_put(sysib
.vm
[0].name
, "KVMguest", 8);
415 stl_p(&sysib
.vm
[0].caf
, 1000);
416 ebcdic_put(sysib
.vm
[0].cpi
, "KVM/Linux ", 16);
417 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
423 case STSI_LEVEL_CURRENT
:
424 env
->regs
[0] = STSI_LEVEL_3
;
434 uint32_t HELPER(sigp
)(CPUS390XState
*env
, uint64_t order_code
, uint32_t r1
,
439 HELPER_LOG("%s: %016" PRIx64
" %08x %016" PRIx64
"\n",
440 __func__
, order_code
, r1
, cpu_addr
);
442 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
443 as parameter (input). Status (output) is always R1. */
445 switch (order_code
) {
450 /* enumerate CPU status */
452 /* XXX implement when SMP comes */
455 env
->regs
[r1
] &= 0xffffffff00000000ULL
;
458 #if !defined(CONFIG_USER_ONLY)
460 qemu_system_reset_request();
461 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
464 qemu_system_shutdown_request();
465 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
470 fprintf(stderr
, "XXX unknown sigp: 0x%" PRIx64
"\n", order_code
);