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/>.
23 #include "host-utils.h"
27 #include "qemu-timer.h"
29 #include <linux/kvm.h>
32 #if !defined(CONFIG_USER_ONLY)
33 #include "softmmu_exec.h"
37 /* #define DEBUG_HELPER */
39 #define HELPER_LOG(x...) qemu_log(x)
41 #define HELPER_LOG(x...)
44 /* raise an exception */
45 void HELPER(exception
)(CPUS390XState
*env
, uint32_t excp
)
47 HELPER_LOG("%s: exception %d\n", __func__
, excp
);
48 env
->exception_index
= excp
;
52 #ifndef CONFIG_USER_ONLY
53 void program_interrupt(CPUS390XState
*env
, uint32_t code
, int ilc
)
55 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
60 kvm_s390_interrupt(env
, KVM_S390_PROGRAM_INT
, code
);
63 env
->int_pgm_code
= code
;
64 env
->int_pgm_ilc
= ilc
;
65 env
->exception_index
= EXCP_PGM
;
70 /* SCLP service call */
71 uint32_t HELPER(servc
)(CPUS390XState
*env
, uint32_t r1
, uint64_t r2
)
75 r
= sclp_service_call(r1
, r2
);
77 program_interrupt(env
, -r
, 4);
84 uint64_t HELPER(diag
)(CPUS390XState
*env
, uint32_t num
, uint64_t mem
,
92 r
= s390_virtio_hypercall(env
, mem
, code
);
108 program_interrupt(env
, PGM_OPERATION
, ILC_LATER_INC
);
115 void HELPER(stidp
)(CPUS390XState
*env
, uint64_t a1
)
117 cpu_stq_data(env
, a1
, env
->cpu_num
);
121 void HELPER(spx
)(CPUS390XState
*env
, uint64_t a1
)
125 prefix
= cpu_ldl_data(env
, a1
);
126 env
->psa
= prefix
& 0xfffff000;
127 qemu_log("prefix: %#x\n", prefix
);
128 tlb_flush_page(env
, 0);
129 tlb_flush_page(env
, TARGET_PAGE_SIZE
);
133 uint32_t HELPER(sck
)(uint64_t a1
)
135 /* XXX not implemented - is it necessary? */
140 static inline uint64_t clock_value(CPUS390XState
*env
)
144 time
= env
->tod_offset
+
145 time2tod(qemu_get_clock_ns(vm_clock
) - env
->tod_basetime
);
151 uint32_t HELPER(stck
)(CPUS390XState
*env
, uint64_t a1
)
153 cpu_stq_data(env
, a1
, clock_value(env
));
158 /* Store Clock Extended */
159 uint32_t HELPER(stcke
)(CPUS390XState
*env
, uint64_t a1
)
161 cpu_stb_data(env
, a1
, 0);
162 /* basically the same value as stck */
163 cpu_stq_data(env
, a1
+ 1, clock_value(env
) | env
->cpu_num
);
164 /* more fine grained than stck */
165 cpu_stq_data(env
, a1
+ 9, 0);
166 /* XXX programmable fields */
167 cpu_stw_data(env
, a1
+ 17, 0);
172 /* Set Clock Comparator */
173 void HELPER(sckc
)(CPUS390XState
*env
, uint64_t a1
)
175 uint64_t time
= cpu_ldq_data(env
, a1
);
181 /* difference between now and then */
182 time
-= clock_value(env
);
184 time
= (time
* 125) >> 9;
186 qemu_mod_timer(env
->tod_timer
, qemu_get_clock_ns(vm_clock
) + time
);
189 /* Store Clock Comparator */
190 void HELPER(stckc
)(CPUS390XState
*env
, uint64_t a1
)
193 cpu_stq_data(env
, a1
, 0);
197 void HELPER(spt
)(CPUS390XState
*env
, uint64_t a1
)
199 uint64_t time
= cpu_ldq_data(env
, a1
);
206 time
= (time
* 125) >> 9;
208 qemu_mod_timer(env
->cpu_timer
, qemu_get_clock_ns(vm_clock
) + time
);
211 /* Store CPU Timer */
212 void HELPER(stpt
)(CPUS390XState
*env
, uint64_t a1
)
215 cpu_stq_data(env
, a1
, 0);
218 /* Store System Information */
219 uint32_t HELPER(stsi
)(CPUS390XState
*env
, uint64_t a0
, uint32_t r0
,
225 if ((r0
& STSI_LEVEL_MASK
) <= STSI_LEVEL_3
&&
226 ((r0
& STSI_R0_RESERVED_MASK
) || (r1
& STSI_R1_RESERVED_MASK
))) {
227 /* valid function code, invalid reserved bits */
228 program_interrupt(env
, PGM_SPECIFICATION
, 2);
231 sel1
= r0
& STSI_R0_SEL1_MASK
;
232 sel2
= r1
& STSI_R1_SEL2_MASK
;
234 /* XXX: spec exception if sysib is not 4k-aligned */
236 switch (r0
& STSI_LEVEL_MASK
) {
238 if ((sel1
== 1) && (sel2
== 1)) {
239 /* Basic Machine Configuration */
240 struct sysib_111 sysib
;
242 memset(&sysib
, 0, sizeof(sysib
));
243 ebcdic_put(sysib
.manuf
, "QEMU ", 16);
244 /* same as machine type number in STORE CPU ID */
245 ebcdic_put(sysib
.type
, "QEMU", 4);
246 /* same as model number in STORE CPU ID */
247 ebcdic_put(sysib
.model
, "QEMU ", 16);
248 ebcdic_put(sysib
.sequence
, "QEMU ", 16);
249 ebcdic_put(sysib
.plant
, "QEMU", 4);
250 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
251 } else if ((sel1
== 2) && (sel2
== 1)) {
252 /* Basic Machine CPU */
253 struct sysib_121 sysib
;
255 memset(&sysib
, 0, sizeof(sysib
));
256 /* XXX make different for different CPUs? */
257 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
258 ebcdic_put(sysib
.plant
, "QEMU", 4);
259 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
260 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
261 } else if ((sel1
== 2) && (sel2
== 2)) {
262 /* Basic Machine CPUs */
263 struct sysib_122 sysib
;
265 memset(&sysib
, 0, sizeof(sysib
));
266 stl_p(&sysib
.capability
, 0x443afc29);
267 /* XXX change when SMP comes */
268 stw_p(&sysib
.total_cpus
, 1);
269 stw_p(&sysib
.active_cpus
, 1);
270 stw_p(&sysib
.standby_cpus
, 0);
271 stw_p(&sysib
.reserved_cpus
, 0);
272 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
279 if ((sel1
== 2) && (sel2
== 1)) {
281 struct sysib_221 sysib
;
283 memset(&sysib
, 0, sizeof(sysib
));
284 /* XXX make different for different CPUs? */
285 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
286 ebcdic_put(sysib
.plant
, "QEMU", 4);
287 stw_p(&sysib
.cpu_addr
, env
->cpu_num
);
288 stw_p(&sysib
.cpu_id
, 0);
289 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
290 } else if ((sel1
== 2) && (sel2
== 2)) {
292 struct sysib_222 sysib
;
294 memset(&sysib
, 0, sizeof(sysib
));
295 stw_p(&sysib
.lpar_num
, 0);
297 /* XXX change when SMP comes */
298 stw_p(&sysib
.total_cpus
, 1);
299 stw_p(&sysib
.conf_cpus
, 1);
300 stw_p(&sysib
.standby_cpus
, 0);
301 stw_p(&sysib
.reserved_cpus
, 0);
302 ebcdic_put(sysib
.name
, "QEMU ", 8);
303 stl_p(&sysib
.caf
, 1000);
304 stw_p(&sysib
.dedicated_cpus
, 0);
305 stw_p(&sysib
.shared_cpus
, 0);
306 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
314 if ((sel1
== 2) && (sel2
== 2)) {
316 struct sysib_322 sysib
;
318 memset(&sysib
, 0, sizeof(sysib
));
320 /* XXX change when SMP comes */
321 stw_p(&sysib
.vm
[0].total_cpus
, 1);
322 stw_p(&sysib
.vm
[0].conf_cpus
, 1);
323 stw_p(&sysib
.vm
[0].standby_cpus
, 0);
324 stw_p(&sysib
.vm
[0].reserved_cpus
, 0);
325 ebcdic_put(sysib
.vm
[0].name
, "KVMguest", 8);
326 stl_p(&sysib
.vm
[0].caf
, 1000);
327 ebcdic_put(sysib
.vm
[0].cpi
, "KVM/Linux ", 16);
328 cpu_physical_memory_rw(a0
, (uint8_t *)&sysib
, sizeof(sysib
), 1);
334 case STSI_LEVEL_CURRENT
:
335 env
->regs
[0] = STSI_LEVEL_3
;
345 uint32_t HELPER(sigp
)(CPUS390XState
*env
, uint64_t order_code
, uint32_t r1
,
350 HELPER_LOG("%s: %016" PRIx64
" %08x %016" PRIx64
"\n",
351 __func__
, order_code
, r1
, cpu_addr
);
353 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
354 as parameter (input). Status (output) is always R1. */
356 switch (order_code
) {
361 /* enumerate CPU status */
363 /* XXX implement when SMP comes */
366 env
->regs
[r1
] &= 0xffffffff00000000ULL
;
369 #if !defined(CONFIG_USER_ONLY)
371 qemu_system_reset_request();
375 qemu_system_shutdown_request();
381 fprintf(stderr
, "XXX unknown sigp: 0x%" PRIx64
"\n", order_code
);