s390x/tcg: specification exception for unknown diag
[qemu.git] / target / s390x / misc_helper.c
blob5a1eba02ab7e75745640290cbe7bbb0fd8b1a6fd
1 /*
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"
23 #include "cpu.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"
36 #endif
38 /* #define DEBUG_HELPER */
39 #ifdef DEBUG_HELPER
40 #define HELPER_LOG(x...) qemu_log(x)
41 #else
42 #define HELPER_LOG(x...)
43 #endif
45 /* Raise an exception dynamically from a helper function. */
46 void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
47 uintptr_t retaddr)
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);
58 cpu_loop_exit(cs);
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;
68 cpu_loop_exit(cs);
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);
78 if (r < 0) {
79 program_interrupt(env, -r, 4);
80 r = 0;
82 qemu_mutex_unlock_iothread();
83 return r;
86 void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)
88 uint64_t r;
90 switch (num) {
91 case 0x500:
92 /* KVM hypercall */
93 qemu_mutex_lock_iothread();
94 r = s390_virtio_hypercall(env);
95 qemu_mutex_unlock_iothread();
96 break;
97 case 0x44:
98 /* yield */
99 r = 0;
100 break;
101 case 0x308:
102 /* ipl */
103 handle_diag_308(env, r1, r3);
104 r = 0;
105 break;
106 default:
107 r = -1;
108 break;
111 if (r) {
112 program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO);
116 /* Set Prefix */
117 void HELPER(spx)(CPUS390XState *env, uint64_t a1)
119 CPUState *cs = CPU(s390_env_get_cpu(env));
120 uint32_t prefix = a1 & 0x7fffe000;
122 env->psa = prefix;
123 HELPER_LOG("prefix: %#x\n", prefix);
124 tlb_flush_page(cs, 0);
125 tlb_flush_page(cs, TARGET_PAGE_SIZE);
128 /* Store Clock */
129 uint64_t HELPER(stck)(CPUS390XState *env)
131 uint64_t time;
133 time = env->tod_offset +
134 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime);
136 return time;
139 /* Set Clock Comparator */
140 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
142 if (time == -1ULL) {
143 return;
146 env->ckc = time;
148 /* difference between origins */
149 time -= env->tod_offset;
151 /* nanoseconds */
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)
160 return env->ckc;
163 /* Set CPU Timer */
164 void HELPER(spt)(CPUS390XState *env, uint64_t time)
166 if (time == -1ULL) {
167 return;
170 /* nanoseconds */
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);
189 int cc = 0;
190 int sel1, sel2;
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) {
204 case STSI_LEVEL_1:
205 if ((sel1 == 1) && (sel2 == 1)) {
206 /* Basic Machine Configuration */
207 struct sysib_111 sysib;
208 char type[5] = {};
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));
242 } else {
243 cc = 3;
245 break;
246 case STSI_LEVEL_2:
248 if ((sel1 == 2) && (sel2 == 1)) {
249 /* LPAR CPU */
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)) {
260 /* LPAR CPUs */
261 struct sysib_222 sysib;
263 memset(&sysib, 0, sizeof(sysib));
264 stw_p(&sysib.lpar_num, 0);
265 sysib.lcpuc = 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));
276 } else {
277 cc = 3;
279 break;
281 case STSI_LEVEL_3:
283 if ((sel1 == 2) && (sel2 == 2)) {
284 /* VM CPUs */
285 struct sysib_322 sysib;
287 memset(&sysib, 0, sizeof(sysib));
288 sysib.count = 1;
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));
298 } else {
299 cc = 3;
301 break;
303 case STSI_LEVEL_CURRENT:
304 env->regs[0] = STSI_LEVEL_3;
305 break;
306 default:
307 cc = 3;
308 break;
311 return cc;
314 uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
315 uint64_t cpu_addr)
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) {
326 case SIGP_SET_ARCH:
327 /* switch arch */
328 break;
329 case SIGP_SENSE:
330 /* enumerate CPU status */
331 if (cpu_addr) {
332 /* XXX implement when SMP comes */
333 return 3;
335 env->regs[r1] &= 0xffffffff00000000ULL;
336 cc = 1;
337 break;
338 #if !defined(CONFIG_USER_ONLY)
339 case SIGP_RESTART:
340 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
341 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
342 break;
343 case SIGP_STOP:
344 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
345 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
346 break;
347 #endif
348 default:
349 /* unknown sigp */
350 fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);
351 cc = SIGP_CC_NOT_OPERATIONAL;
354 return cc;
356 #endif
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();
438 #endif
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;
450 cpu_loop_exit(cs);
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;
481 cpu_loop_exit(cs);
485 #endif
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;
497 S390Feat feat;
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;
512 if (bit > max_bit) {
513 max_bit = bit;
515 assert(bit / 64 < MAX_STFL_WORDS);
516 words[bit / 64] |= 1ULL << (63 - bit % 64);
520 return max_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);
536 unsigned i;
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);