dma/rc4030: document register at offset 0x210
[qemu/ar7.git] / target-s390x / misc_helper.c
blobb375ab724bd1937ab3f97eaedaea922bb1911d5c
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 "cpu.h"
22 #include "exec/memory.h"
23 #include "qemu/host-utils.h"
24 #include "exec/helper-proto.h"
25 #include <string.h>
26 #include "sysemu/kvm.h"
27 #include "qemu/timer.h"
28 #include "exec/address-spaces.h"
29 #ifdef CONFIG_KVM
30 #include <linux/kvm.h>
31 #endif
32 #include "exec/cpu_ldst.h"
34 #if !defined(CONFIG_USER_ONLY)
35 #include "sysemu/cpus.h"
36 #include "sysemu/sysemu.h"
37 #include "hw/s390x/ebcdic.h"
38 #include "hw/s390x/ipl.h"
39 #endif
41 /* #define DEBUG_HELPER */
42 #ifdef DEBUG_HELPER
43 #define HELPER_LOG(x...) qemu_log(x)
44 #else
45 #define HELPER_LOG(x...)
46 #endif
48 /* Raise an exception dynamically from a helper function. */
49 void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
50 uintptr_t retaddr)
52 CPUState *cs = CPU(s390_env_get_cpu(env));
53 int t;
55 cs->exception_index = EXCP_PGM;
56 env->int_pgm_code = excp;
58 /* Use the (ultimate) callers address to find the insn that trapped. */
59 cpu_restore_state(cs, retaddr);
61 /* Advance past the insn. */
62 t = cpu_ldub_code(env, env->psw.addr);
63 env->int_pgm_ilen = t = get_ilen(t);
64 env->psw.addr += t;
66 cpu_loop_exit(cs);
69 /* Raise an exception statically from a TB. */
70 void HELPER(exception)(CPUS390XState *env, uint32_t excp)
72 CPUState *cs = CPU(s390_env_get_cpu(env));
74 HELPER_LOG("%s: exception %d\n", __func__, excp);
75 cs->exception_index = excp;
76 cpu_loop_exit(cs);
79 #ifndef CONFIG_USER_ONLY
81 void program_interrupt(CPUS390XState *env, uint32_t code, int ilen)
83 S390CPU *cpu = s390_env_get_cpu(env);
85 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
86 env->psw.addr);
88 if (kvm_enabled()) {
89 #ifdef CONFIG_KVM
90 struct kvm_s390_irq irq = {
91 .type = KVM_S390_PROGRAM_INT,
92 .u.pgm.code = code,
95 kvm_s390_vcpu_interrupt(cpu, &irq);
96 #endif
97 } else {
98 CPUState *cs = CPU(cpu);
100 env->int_pgm_code = code;
101 env->int_pgm_ilen = ilen;
102 cs->exception_index = EXCP_PGM;
103 cpu_loop_exit(cs);
107 /* SCLP service call */
108 uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
110 int r = sclp_service_call(env, r1, r2);
111 if (r < 0) {
112 program_interrupt(env, -r, 4);
113 return 0;
115 return r;
118 #ifndef CONFIG_USER_ONLY
119 static int modified_clear_reset(S390CPU *cpu)
121 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
122 CPUState *t;
124 pause_all_vcpus();
125 cpu_synchronize_all_states();
126 CPU_FOREACH(t) {
127 run_on_cpu(t, s390_do_cpu_full_reset, t);
129 cmma_reset(cpu);
130 io_subsystem_reset();
131 scc->load_normal(CPU(cpu));
132 cpu_synchronize_all_post_reset();
133 resume_all_vcpus();
134 return 0;
137 static int load_normal_reset(S390CPU *cpu)
139 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
140 CPUState *t;
142 pause_all_vcpus();
143 cpu_synchronize_all_states();
144 CPU_FOREACH(t) {
145 run_on_cpu(t, s390_do_cpu_reset, t);
147 cmma_reset(cpu);
148 io_subsystem_reset();
149 scc->initial_cpu_reset(CPU(cpu));
150 scc->load_normal(CPU(cpu));
151 cpu_synchronize_all_post_reset();
152 resume_all_vcpus();
153 return 0;
156 #define DIAG_308_RC_OK 0x0001
157 #define DIAG_308_RC_NO_CONF 0x0102
158 #define DIAG_308_RC_INVALID 0x0402
160 void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3)
162 uint64_t addr = env->regs[r1];
163 uint64_t subcode = env->regs[r3];
164 IplParameterBlock *iplb;
166 if (env->psw.mask & PSW_MASK_PSTATE) {
167 program_interrupt(env, PGM_PRIVILEGED, ILEN_LATER_INC);
168 return;
171 if ((subcode & ~0x0ffffULL) || (subcode > 6)) {
172 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
173 return;
176 switch (subcode) {
177 case 0:
178 modified_clear_reset(s390_env_get_cpu(env));
179 break;
180 case 1:
181 load_normal_reset(s390_env_get_cpu(env));
182 break;
183 case 5:
184 if ((r1 & 1) || (addr & 0x0fffULL)) {
185 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
186 return;
188 if (!address_space_access_valid(&address_space_memory, addr,
189 sizeof(IplParameterBlock), false)) {
190 program_interrupt(env, PGM_ADDRESSING, ILEN_LATER_INC);
191 return;
193 iplb = g_malloc0(sizeof(struct IplParameterBlock));
194 cpu_physical_memory_read(addr, iplb, sizeof(struct IplParameterBlock));
195 if (!s390_ipl_update_diag308(iplb)) {
196 env->regs[r1 + 1] = DIAG_308_RC_OK;
197 } else {
198 env->regs[r1 + 1] = DIAG_308_RC_INVALID;
200 g_free(iplb);
201 return;
202 case 6:
203 if ((r1 & 1) || (addr & 0x0fffULL)) {
204 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
205 return;
207 if (!address_space_access_valid(&address_space_memory, addr,
208 sizeof(IplParameterBlock), true)) {
209 program_interrupt(env, PGM_ADDRESSING, ILEN_LATER_INC);
210 return;
212 iplb = s390_ipl_get_iplb();
213 if (iplb) {
214 cpu_physical_memory_write(addr, iplb,
215 sizeof(struct IplParameterBlock));
216 env->regs[r1 + 1] = DIAG_308_RC_OK;
217 } else {
218 env->regs[r1 + 1] = DIAG_308_RC_NO_CONF;
220 return;
221 default:
222 hw_error("Unhandled diag308 subcode %" PRIx64, subcode);
223 break;
226 #endif
228 /* DIAG */
229 uint64_t HELPER(diag)(CPUS390XState *env, uint32_t num, uint64_t mem,
230 uint64_t code)
232 uint64_t r;
234 switch (num) {
235 case 0x500:
236 /* KVM hypercall */
237 r = s390_virtio_hypercall(env);
238 break;
239 case 0x44:
240 /* yield */
241 r = 0;
242 break;
243 case 0x308:
244 /* ipl */
245 r = 0;
246 break;
247 default:
248 r = -1;
249 break;
252 if (r) {
253 program_interrupt(env, PGM_OPERATION, ILEN_LATER_INC);
256 return r;
259 /* Set Prefix */
260 void HELPER(spx)(CPUS390XState *env, uint64_t a1)
262 CPUState *cs = CPU(s390_env_get_cpu(env));
263 uint32_t prefix = a1 & 0x7fffe000;
265 env->psa = prefix;
266 qemu_log("prefix: %#x\n", prefix);
267 tlb_flush_page(cs, 0);
268 tlb_flush_page(cs, TARGET_PAGE_SIZE);
271 /* Store Clock */
272 uint64_t HELPER(stck)(CPUS390XState *env)
274 uint64_t time;
276 time = env->tod_offset +
277 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime);
279 return time;
282 /* Set Clock Comparator */
283 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
285 if (time == -1ULL) {
286 return;
289 env->ckc = time;
291 /* difference between origins */
292 time -= env->tod_offset;
294 /* nanoseconds */
295 time = tod2time(time);
297 timer_mod(env->tod_timer, env->tod_basetime + time);
300 /* Store Clock Comparator */
301 uint64_t HELPER(stckc)(CPUS390XState *env)
303 return env->ckc;
306 /* Set CPU Timer */
307 void HELPER(spt)(CPUS390XState *env, uint64_t time)
309 if (time == -1ULL) {
310 return;
313 /* nanoseconds */
314 time = tod2time(time);
316 env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
318 timer_mod(env->cpu_timer, env->cputm);
321 /* Store CPU Timer */
322 uint64_t HELPER(stpt)(CPUS390XState *env)
324 return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
327 /* Store System Information */
328 uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0,
329 uint64_t r0, uint64_t r1)
331 int cc = 0;
332 int sel1, sel2;
334 if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 &&
335 ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) {
336 /* valid function code, invalid reserved bits */
337 program_interrupt(env, PGM_SPECIFICATION, 2);
340 sel1 = r0 & STSI_R0_SEL1_MASK;
341 sel2 = r1 & STSI_R1_SEL2_MASK;
343 /* XXX: spec exception if sysib is not 4k-aligned */
345 switch (r0 & STSI_LEVEL_MASK) {
346 case STSI_LEVEL_1:
347 if ((sel1 == 1) && (sel2 == 1)) {
348 /* Basic Machine Configuration */
349 struct sysib_111 sysib;
351 memset(&sysib, 0, sizeof(sysib));
352 ebcdic_put(sysib.manuf, "QEMU ", 16);
353 /* same as machine type number in STORE CPU ID */
354 ebcdic_put(sysib.type, "QEMU", 4);
355 /* same as model number in STORE CPU ID */
356 ebcdic_put(sysib.model, "QEMU ", 16);
357 ebcdic_put(sysib.sequence, "QEMU ", 16);
358 ebcdic_put(sysib.plant, "QEMU", 4);
359 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
360 } else if ((sel1 == 2) && (sel2 == 1)) {
361 /* Basic Machine CPU */
362 struct sysib_121 sysib;
364 memset(&sysib, 0, sizeof(sysib));
365 /* XXX make different for different CPUs? */
366 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
367 ebcdic_put(sysib.plant, "QEMU", 4);
368 stw_p(&sysib.cpu_addr, env->cpu_num);
369 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
370 } else if ((sel1 == 2) && (sel2 == 2)) {
371 /* Basic Machine CPUs */
372 struct sysib_122 sysib;
374 memset(&sysib, 0, sizeof(sysib));
375 stl_p(&sysib.capability, 0x443afc29);
376 /* XXX change when SMP comes */
377 stw_p(&sysib.total_cpus, 1);
378 stw_p(&sysib.active_cpus, 1);
379 stw_p(&sysib.standby_cpus, 0);
380 stw_p(&sysib.reserved_cpus, 0);
381 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
382 } else {
383 cc = 3;
385 break;
386 case STSI_LEVEL_2:
388 if ((sel1 == 2) && (sel2 == 1)) {
389 /* LPAR CPU */
390 struct sysib_221 sysib;
392 memset(&sysib, 0, sizeof(sysib));
393 /* XXX make different for different CPUs? */
394 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
395 ebcdic_put(sysib.plant, "QEMU", 4);
396 stw_p(&sysib.cpu_addr, env->cpu_num);
397 stw_p(&sysib.cpu_id, 0);
398 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
399 } else if ((sel1 == 2) && (sel2 == 2)) {
400 /* LPAR CPUs */
401 struct sysib_222 sysib;
403 memset(&sysib, 0, sizeof(sysib));
404 stw_p(&sysib.lpar_num, 0);
405 sysib.lcpuc = 0;
406 /* XXX change when SMP comes */
407 stw_p(&sysib.total_cpus, 1);
408 stw_p(&sysib.conf_cpus, 1);
409 stw_p(&sysib.standby_cpus, 0);
410 stw_p(&sysib.reserved_cpus, 0);
411 ebcdic_put(sysib.name, "QEMU ", 8);
412 stl_p(&sysib.caf, 1000);
413 stw_p(&sysib.dedicated_cpus, 0);
414 stw_p(&sysib.shared_cpus, 0);
415 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
416 } else {
417 cc = 3;
419 break;
421 case STSI_LEVEL_3:
423 if ((sel1 == 2) && (sel2 == 2)) {
424 /* VM CPUs */
425 struct sysib_322 sysib;
427 memset(&sysib, 0, sizeof(sysib));
428 sysib.count = 1;
429 /* XXX change when SMP comes */
430 stw_p(&sysib.vm[0].total_cpus, 1);
431 stw_p(&sysib.vm[0].conf_cpus, 1);
432 stw_p(&sysib.vm[0].standby_cpus, 0);
433 stw_p(&sysib.vm[0].reserved_cpus, 0);
434 ebcdic_put(sysib.vm[0].name, "KVMguest", 8);
435 stl_p(&sysib.vm[0].caf, 1000);
436 ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16);
437 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
438 } else {
439 cc = 3;
441 break;
443 case STSI_LEVEL_CURRENT:
444 env->regs[0] = STSI_LEVEL_3;
445 break;
446 default:
447 cc = 3;
448 break;
451 return cc;
454 uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
455 uint64_t cpu_addr)
457 int cc = SIGP_CC_ORDER_CODE_ACCEPTED;
459 HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n",
460 __func__, order_code, r1, cpu_addr);
462 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
463 as parameter (input). Status (output) is always R1. */
465 switch (order_code) {
466 case SIGP_SET_ARCH:
467 /* switch arch */
468 break;
469 case SIGP_SENSE:
470 /* enumerate CPU status */
471 if (cpu_addr) {
472 /* XXX implement when SMP comes */
473 return 3;
475 env->regs[r1] &= 0xffffffff00000000ULL;
476 cc = 1;
477 break;
478 #if !defined(CONFIG_USER_ONLY)
479 case SIGP_RESTART:
480 qemu_system_reset_request();
481 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
482 break;
483 case SIGP_STOP:
484 qemu_system_shutdown_request();
485 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
486 break;
487 #endif
488 default:
489 /* unknown sigp */
490 fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);
491 cc = SIGP_CC_NOT_OPERATIONAL;
494 return cc;
496 #endif