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"
25 #include "exec/memory.h"
26 #include "qemu/host-utils.h"
27 #include "exec/helper-proto.h"
28 #include "qemu/timer.h"
29 #include "exec/address-spaces.h"
30 #include "exec/exec-all.h"
31 #include "exec/cpu_ldst.h"
33 #if !defined(CONFIG_USER_ONLY)
34 #include "sysemu/cpus.h"
35 #include "sysemu/sysemu.h"
36 #include "hw/s390x/ebcdic.h"
37 #include "hw/s390x/s390-virtio-hcall.h"
38 #include "hw/s390x/sclp.h"
41 /* #define DEBUG_HELPER */
43 #define HELPER_LOG(x...) qemu_log(x)
45 #define HELPER_LOG(x...)
48 /* Raise an exception statically from a TB. */
49 void HELPER(exception
)(CPUS390XState
*env
, uint32_t excp
)
51 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
53 HELPER_LOG("%s: exception %d\n", __func__
, excp
);
54 cs
->exception_index
= excp
;
58 #ifndef CONFIG_USER_ONLY
60 /* SCLP service call */
61 uint32_t HELPER(servc
)(CPUS390XState
*env
, uint64_t r1
, uint64_t r2
)
63 qemu_mutex_lock_iothread();
64 int r
= sclp_service_call(env
, r1
, r2
);
66 program_interrupt(env
, -r
, 4);
69 qemu_mutex_unlock_iothread();
73 void HELPER(diag
)(CPUS390XState
*env
, uint32_t r1
, uint32_t r3
, uint32_t num
)
80 qemu_mutex_lock_iothread();
81 r
= s390_virtio_hypercall(env
);
82 qemu_mutex_unlock_iothread();
90 qemu_mutex_lock_iothread();
91 handle_diag_308(env
, r1
, r3
);
92 qemu_mutex_unlock_iothread();
96 /* time bomb (watchdog) */
97 r
= handle_diag_288(env
, r1
, r3
);
105 program_interrupt(env
, PGM_SPECIFICATION
, ILEN_AUTO
);
110 void HELPER(spx
)(CPUS390XState
*env
, uint64_t a1
)
112 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
113 uint32_t prefix
= a1
& 0x7fffe000;
116 HELPER_LOG("prefix: %#x\n", prefix
);
117 tlb_flush_page(cs
, 0);
118 tlb_flush_page(cs
, TARGET_PAGE_SIZE
);
122 uint64_t HELPER(stck
)(CPUS390XState
*env
)
126 time
= env
->tod_offset
+
127 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - env
->tod_basetime
);
132 /* Set Clock Comparator */
133 void HELPER(sckc
)(CPUS390XState
*env
, uint64_t time
)
141 /* difference between origins */
142 time
-= env
->tod_offset
;
145 time
= tod2time(time
);
147 timer_mod(env
->tod_timer
, env
->tod_basetime
+ time
);
150 /* Store Clock Comparator */
151 uint64_t HELPER(stckc
)(CPUS390XState
*env
)
157 void HELPER(spt
)(CPUS390XState
*env
, uint64_t time
)
164 time
= tod2time(time
);
166 env
->cputm
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + time
;
168 timer_mod(env
->cpu_timer
, env
->cputm
);
171 /* Store CPU Timer */
172 uint64_t HELPER(stpt
)(CPUS390XState
*env
)
174 return time2tod(env
->cputm
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
177 /* Store System Information */
178 uint32_t HELPER(stsi
)(CPUS390XState
*env
, uint64_t a0
,
179 uint64_t r0
, uint64_t r1
)
181 S390CPU
*cpu
= s390_env_get_cpu(env
);
185 if ((r0
& STSI_LEVEL_MASK
) <= STSI_LEVEL_3
&&
186 ((r0
& STSI_R0_RESERVED_MASK
) || (r1
& STSI_R1_RESERVED_MASK
))) {
187 /* valid function code, invalid reserved bits */
188 program_interrupt(env
, PGM_SPECIFICATION
, 4);
191 sel1
= r0
& STSI_R0_SEL1_MASK
;
192 sel2
= r1
& STSI_R1_SEL2_MASK
;
194 /* XXX: spec exception if sysib is not 4k-aligned */
196 switch (r0
& STSI_LEVEL_MASK
) {
198 if ((sel1
== 1) && (sel2
== 1)) {
199 /* Basic Machine Configuration */
200 struct sysib_111 sysib
;
203 memset(&sysib
, 0, sizeof(sysib
));
204 ebcdic_put(sysib
.manuf
, "QEMU ", 16);
205 /* same as machine type number in STORE CPU ID, but in EBCDIC */
206 snprintf(type
, ARRAY_SIZE(type
), "%X", cpu
->model
->def
->type
);
207 ebcdic_put(sysib
.type
, type
, 4);
208 /* model number (not stored in STORE CPU ID for z/Architecure) */
209 ebcdic_put(sysib
.model
, "QEMU ", 16);
210 ebcdic_put(sysib
.sequence
, "QEMU ", 16);
211 ebcdic_put(sysib
.plant
, "QEMU", 4);
212 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
213 } else if ((sel1
== 2) && (sel2
== 1)) {
214 /* Basic Machine CPU */
215 struct sysib_121 sysib
;
217 memset(&sysib
, 0, sizeof(sysib
));
218 /* XXX make different for different CPUs? */
219 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
220 ebcdic_put(sysib
.plant
, "QEMU", 4);
221 stw_p(&sysib
.cpu_addr
, env
->core_id
);
222 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
223 } else if ((sel1
== 2) && (sel2
== 2)) {
224 /* Basic Machine CPUs */
225 struct sysib_122 sysib
;
227 memset(&sysib
, 0, sizeof(sysib
));
228 stl_p(&sysib
.capability
, 0x443afc29);
229 /* XXX change when SMP comes */
230 stw_p(&sysib
.total_cpus
, 1);
231 stw_p(&sysib
.active_cpus
, 1);
232 stw_p(&sysib
.standby_cpus
, 0);
233 stw_p(&sysib
.reserved_cpus
, 0);
234 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
241 if ((sel1
== 2) && (sel2
== 1)) {
243 struct sysib_221 sysib
;
245 memset(&sysib
, 0, sizeof(sysib
));
246 /* XXX make different for different CPUs? */
247 ebcdic_put(sysib
.sequence
, "QEMUQEMUQEMUQEMU", 16);
248 ebcdic_put(sysib
.plant
, "QEMU", 4);
249 stw_p(&sysib
.cpu_addr
, env
->core_id
);
250 stw_p(&sysib
.cpu_id
, 0);
251 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
252 } else if ((sel1
== 2) && (sel2
== 2)) {
254 struct sysib_222 sysib
;
256 memset(&sysib
, 0, sizeof(sysib
));
257 stw_p(&sysib
.lpar_num
, 0);
259 /* XXX change when SMP comes */
260 stw_p(&sysib
.total_cpus
, 1);
261 stw_p(&sysib
.conf_cpus
, 1);
262 stw_p(&sysib
.standby_cpus
, 0);
263 stw_p(&sysib
.reserved_cpus
, 0);
264 ebcdic_put(sysib
.name
, "QEMU ", 8);
265 stl_p(&sysib
.caf
, 1000);
266 stw_p(&sysib
.dedicated_cpus
, 0);
267 stw_p(&sysib
.shared_cpus
, 0);
268 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
276 if ((sel1
== 2) && (sel2
== 2)) {
278 struct sysib_322 sysib
;
280 memset(&sysib
, 0, sizeof(sysib
));
282 /* XXX change when SMP comes */
283 stw_p(&sysib
.vm
[0].total_cpus
, 1);
284 stw_p(&sysib
.vm
[0].conf_cpus
, 1);
285 stw_p(&sysib
.vm
[0].standby_cpus
, 0);
286 stw_p(&sysib
.vm
[0].reserved_cpus
, 0);
287 ebcdic_put(sysib
.vm
[0].name
, "KVMguest", 8);
288 stl_p(&sysib
.vm
[0].caf
, 1000);
289 ebcdic_put(sysib
.vm
[0].cpi
, "KVM/Linux ", 16);
290 cpu_physical_memory_write(a0
, &sysib
, sizeof(sysib
));
296 case STSI_LEVEL_CURRENT
:
297 env
->regs
[0] = STSI_LEVEL_3
;
307 uint32_t HELPER(sigp
)(CPUS390XState
*env
, uint64_t order_code
, uint32_t r1
,
312 /* TODO: needed to inject interrupts - push further down */
313 qemu_mutex_lock_iothread();
314 cc
= handle_sigp(env
, order_code
& SIGP_ORDER_MASK
, r1
, r3
);
315 qemu_mutex_unlock_iothread();
321 #ifndef CONFIG_USER_ONLY
322 void HELPER(xsch
)(CPUS390XState
*env
, uint64_t r1
)
324 S390CPU
*cpu
= s390_env_get_cpu(env
);
325 qemu_mutex_lock_iothread();
326 ioinst_handle_xsch(cpu
, r1
, GETPC());
327 qemu_mutex_unlock_iothread();
330 void HELPER(csch
)(CPUS390XState
*env
, uint64_t r1
)
332 S390CPU
*cpu
= s390_env_get_cpu(env
);
333 qemu_mutex_lock_iothread();
334 ioinst_handle_csch(cpu
, r1
, GETPC());
335 qemu_mutex_unlock_iothread();
338 void HELPER(hsch
)(CPUS390XState
*env
, uint64_t r1
)
340 S390CPU
*cpu
= s390_env_get_cpu(env
);
341 qemu_mutex_lock_iothread();
342 ioinst_handle_hsch(cpu
, r1
, GETPC());
343 qemu_mutex_unlock_iothread();
346 void HELPER(msch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
348 S390CPU
*cpu
= s390_env_get_cpu(env
);
349 qemu_mutex_lock_iothread();
350 ioinst_handle_msch(cpu
, r1
, inst
>> 16, GETPC());
351 qemu_mutex_unlock_iothread();
354 void HELPER(rchp
)(CPUS390XState
*env
, uint64_t r1
)
356 S390CPU
*cpu
= s390_env_get_cpu(env
);
357 qemu_mutex_lock_iothread();
358 ioinst_handle_rchp(cpu
, r1
, GETPC());
359 qemu_mutex_unlock_iothread();
362 void HELPER(rsch
)(CPUS390XState
*env
, uint64_t r1
)
364 S390CPU
*cpu
= s390_env_get_cpu(env
);
365 qemu_mutex_lock_iothread();
366 ioinst_handle_rsch(cpu
, r1
, GETPC());
367 qemu_mutex_unlock_iothread();
370 void HELPER(ssch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
372 S390CPU
*cpu
= s390_env_get_cpu(env
);
373 qemu_mutex_lock_iothread();
374 ioinst_handle_ssch(cpu
, r1
, inst
>> 16, GETPC());
375 qemu_mutex_unlock_iothread();
378 void HELPER(stsch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
380 S390CPU
*cpu
= s390_env_get_cpu(env
);
381 qemu_mutex_lock_iothread();
382 ioinst_handle_stsch(cpu
, r1
, inst
>> 16, GETPC());
383 qemu_mutex_unlock_iothread();
386 void HELPER(tsch
)(CPUS390XState
*env
, uint64_t r1
, uint64_t inst
)
388 S390CPU
*cpu
= s390_env_get_cpu(env
);
389 qemu_mutex_lock_iothread();
390 ioinst_handle_tsch(cpu
, r1
, inst
>> 16, GETPC());
391 qemu_mutex_unlock_iothread();
394 void HELPER(chsc
)(CPUS390XState
*env
, uint64_t inst
)
396 S390CPU
*cpu
= s390_env_get_cpu(env
);
397 qemu_mutex_lock_iothread();
398 ioinst_handle_chsc(cpu
, inst
>> 16, GETPC());
399 qemu_mutex_unlock_iothread();
403 #ifndef CONFIG_USER_ONLY
404 void HELPER(per_check_exception
)(CPUS390XState
*env
)
408 if (env
->per_perc_atmid
) {
410 * FIXME: ILEN_AUTO is most probably the right thing to use. ilen
411 * always has to match the instruction referenced in the PSW. E.g.
412 * if a PER interrupt is triggered via EXECUTE, we have to use ilen
413 * of EXECUTE, while per_address contains the target of EXECUTE.
415 ilen
= get_ilen(cpu_ldub_code(env
, env
->per_address
));
416 program_interrupt(env
, PGM_PER
, ilen
);
420 /* Check if an address is within the PER starting address and the PER
421 ending address. The address range might loop. */
422 static inline bool get_per_in_range(CPUS390XState
*env
, uint64_t addr
)
424 if (env
->cregs
[10] <= env
->cregs
[11]) {
425 return env
->cregs
[10] <= addr
&& addr
<= env
->cregs
[11];
427 return env
->cregs
[10] <= addr
|| addr
<= env
->cregs
[11];
431 void HELPER(per_branch
)(CPUS390XState
*env
, uint64_t from
, uint64_t to
)
433 if ((env
->cregs
[9] & PER_CR9_EVENT_BRANCH
)) {
434 if (!(env
->cregs
[9] & PER_CR9_CONTROL_BRANCH_ADDRESS
)
435 || get_per_in_range(env
, to
)) {
436 env
->per_address
= from
;
437 env
->per_perc_atmid
= PER_CODE_EVENT_BRANCH
| get_per_atmid(env
);
442 void HELPER(per_ifetch
)(CPUS390XState
*env
, uint64_t addr
)
444 if ((env
->cregs
[9] & PER_CR9_EVENT_IFETCH
) && get_per_in_range(env
, addr
)) {
445 env
->per_address
= addr
;
446 env
->per_perc_atmid
= PER_CODE_EVENT_IFETCH
| get_per_atmid(env
);
448 /* If the instruction has to be nullified, trigger the
449 exception immediately. */
450 if (env
->cregs
[9] & PER_CR9_EVENT_NULLIFICATION
) {
451 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
453 env
->per_perc_atmid
|= PER_CODE_EVENT_NULLIFICATION
;
454 env
->int_pgm_code
= PGM_PER
;
455 env
->int_pgm_ilen
= get_ilen(cpu_ldub_code(env
, addr
));
457 cs
->exception_index
= EXCP_PGM
;
464 static uint8_t stfl_bytes
[2048];
465 static unsigned int used_stfl_bytes
;
467 static void prepare_stfl(void)
469 static bool initialized
;
472 /* racy, but we don't care, the same values are always written */
477 s390_get_feat_block(S390_FEAT_TYPE_STFL
, stfl_bytes
);
478 for (i
= 0; i
< sizeof(stfl_bytes
); i
++) {
480 used_stfl_bytes
= i
+ 1;
486 #ifndef CONFIG_USER_ONLY
487 void HELPER(stfl
)(CPUS390XState
*env
)
491 lowcore
= cpu_map_lowcore(env
);
493 memcpy(&lowcore
->stfl_fac_list
, stfl_bytes
, sizeof(lowcore
->stfl_fac_list
));
494 cpu_unmap_lowcore(lowcore
);
498 uint32_t HELPER(stfle
)(CPUS390XState
*env
, uint64_t addr
)
500 const uintptr_t ra
= GETPC();
501 const int count_bytes
= ((env
->regs
[0] & 0xff) + 1) * 8;
502 const int max_bytes
= ROUND_UP(used_stfl_bytes
, 8);
506 s390_program_interrupt(env
, PGM_SPECIFICATION
, 4, ra
);
510 for (i
= 0; i
< count_bytes
; ++i
) {
511 cpu_stb_data_ra(env
, addr
+ i
, stfl_bytes
[i
], ra
);
514 env
->regs
[0] = deposit64(env
->regs
[0], 0, 8, (max_bytes
/ 8) - 1);
515 return count_bytes
>= max_bytes
? 0 : 3;