OptsVisitor: rebase opts_type_uint64() to parse_uint_full()
[qemu-kvm.git] / target-s390x / helper.c
blob61abfd7d9e34fbd4d1d028efba03c23053107a9a
1 /*
2 * S/390 helpers
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2011 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/gdbstub.h"
23 #include "qemu/timer.h"
24 #ifndef CONFIG_USER_ONLY
25 #include "sysemu/sysemu.h"
26 #endif
28 //#define DEBUG_S390
29 //#define DEBUG_S390_PTE
30 //#define DEBUG_S390_STDOUT
32 #ifdef DEBUG_S390
33 #ifdef DEBUG_S390_STDOUT
34 #define DPRINTF(fmt, ...) \
35 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
36 qemu_log(fmt, ##__VA_ARGS__); } while (0)
37 #else
38 #define DPRINTF(fmt, ...) \
39 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
40 #endif
41 #else
42 #define DPRINTF(fmt, ...) \
43 do { } while (0)
44 #endif
46 #ifdef DEBUG_S390_PTE
47 #define PTE_DPRINTF DPRINTF
48 #else
49 #define PTE_DPRINTF(fmt, ...) \
50 do { } while (0)
51 #endif
53 #ifndef CONFIG_USER_ONLY
54 void s390x_tod_timer(void *opaque)
56 S390CPU *cpu = opaque;
57 CPUS390XState *env = &cpu->env;
59 env->pending_int |= INTERRUPT_TOD;
60 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
63 void s390x_cpu_timer(void *opaque)
65 S390CPU *cpu = opaque;
66 CPUS390XState *env = &cpu->env;
68 env->pending_int |= INTERRUPT_CPUTIMER;
69 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
71 #endif
73 S390CPU *cpu_s390x_init(const char *cpu_model)
75 S390CPU *cpu;
76 CPUS390XState *env;
78 cpu = S390_CPU(object_new(TYPE_S390_CPU));
79 env = &cpu->env;
80 env->cpu_model_str = cpu_model;
82 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
84 return cpu;
87 #if defined(CONFIG_USER_ONLY)
89 void s390_cpu_do_interrupt(CPUState *cs)
91 S390CPU *cpu = S390_CPU(cs);
92 CPUS390XState *env = &cpu->env;
94 env->exception_index = -1;
97 int cpu_s390x_handle_mmu_fault(CPUS390XState *env, target_ulong address,
98 int rw, int mmu_idx)
100 env->exception_index = EXCP_PGM;
101 env->int_pgm_code = PGM_ADDRESSING;
102 /* On real machines this value is dropped into LowMem. Since this
103 is userland, simply put this someplace that cpu_loop can find it. */
104 env->__excp_addr = address;
105 return 1;
108 #else /* !CONFIG_USER_ONLY */
110 /* Ensure to exit the TB after this call! */
111 static void trigger_pgm_exception(CPUS390XState *env, uint32_t code,
112 uint32_t ilen)
114 env->exception_index = EXCP_PGM;
115 env->int_pgm_code = code;
116 env->int_pgm_ilen = ilen;
119 static int trans_bits(CPUS390XState *env, uint64_t mode)
121 int bits = 0;
123 switch (mode) {
124 case PSW_ASC_PRIMARY:
125 bits = 1;
126 break;
127 case PSW_ASC_SECONDARY:
128 bits = 2;
129 break;
130 case PSW_ASC_HOME:
131 bits = 3;
132 break;
133 default:
134 cpu_abort(env, "unknown asc mode\n");
135 break;
138 return bits;
141 static void trigger_prot_fault(CPUS390XState *env, target_ulong vaddr,
142 uint64_t mode)
144 int ilen = ILEN_LATER_INC;
145 int bits = trans_bits(env, mode) | 4;
147 DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
149 stq_phys(env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
150 trigger_pgm_exception(env, PGM_PROTECTION, ilen);
153 static void trigger_page_fault(CPUS390XState *env, target_ulong vaddr,
154 uint32_t type, uint64_t asc, int rw)
156 int ilen = ILEN_LATER;
157 int bits = trans_bits(env, asc);
159 /* Code accesses have an undefined ilc. */
160 if (rw == 2) {
161 ilen = 2;
164 DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
166 stq_phys(env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
167 trigger_pgm_exception(env, type, ilen);
170 static int mmu_translate_asce(CPUS390XState *env, target_ulong vaddr,
171 uint64_t asc, uint64_t asce, int level,
172 target_ulong *raddr, int *flags, int rw)
174 uint64_t offs = 0;
175 uint64_t origin;
176 uint64_t new_asce;
178 PTE_DPRINTF("%s: 0x%" PRIx64 "\n", __func__, asce);
180 if (((level != _ASCE_TYPE_SEGMENT) && (asce & _REGION_ENTRY_INV)) ||
181 ((level == _ASCE_TYPE_SEGMENT) && (asce & _SEGMENT_ENTRY_INV))) {
182 /* XXX different regions have different faults */
183 DPRINTF("%s: invalid region\n", __func__);
184 trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw);
185 return -1;
188 if ((level <= _ASCE_TYPE_MASK) && ((asce & _ASCE_TYPE_MASK) != level)) {
189 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
190 return -1;
193 if (asce & _ASCE_REAL_SPACE) {
194 /* direct mapping */
196 *raddr = vaddr;
197 return 0;
200 origin = asce & _ASCE_ORIGIN;
202 switch (level) {
203 case _ASCE_TYPE_REGION1 + 4:
204 offs = (vaddr >> 50) & 0x3ff8;
205 break;
206 case _ASCE_TYPE_REGION1:
207 offs = (vaddr >> 39) & 0x3ff8;
208 break;
209 case _ASCE_TYPE_REGION2:
210 offs = (vaddr >> 28) & 0x3ff8;
211 break;
212 case _ASCE_TYPE_REGION3:
213 offs = (vaddr >> 17) & 0x3ff8;
214 break;
215 case _ASCE_TYPE_SEGMENT:
216 offs = (vaddr >> 9) & 0x07f8;
217 origin = asce & _SEGMENT_ENTRY_ORIGIN;
218 break;
221 /* XXX region protection flags */
222 /* *flags &= ~PAGE_WRITE */
224 new_asce = ldq_phys(origin + offs);
225 PTE_DPRINTF("%s: 0x%" PRIx64 " + 0x%" PRIx64 " => 0x%016" PRIx64 "\n",
226 __func__, origin, offs, new_asce);
228 if (level != _ASCE_TYPE_SEGMENT) {
229 /* yet another region */
230 return mmu_translate_asce(env, vaddr, asc, new_asce, level - 4, raddr,
231 flags, rw);
234 /* PTE */
235 if (new_asce & _PAGE_INVALID) {
236 DPRINTF("%s: PTE=0x%" PRIx64 " invalid\n", __func__, new_asce);
237 trigger_page_fault(env, vaddr, PGM_PAGE_TRANS, asc, rw);
238 return -1;
241 if (new_asce & _PAGE_RO) {
242 *flags &= ~PAGE_WRITE;
245 *raddr = new_asce & _ASCE_ORIGIN;
247 PTE_DPRINTF("%s: PTE=0x%" PRIx64 "\n", __func__, new_asce);
249 return 0;
252 static int mmu_translate_asc(CPUS390XState *env, target_ulong vaddr,
253 uint64_t asc, target_ulong *raddr, int *flags,
254 int rw)
256 uint64_t asce = 0;
257 int level, new_level;
258 int r;
260 switch (asc) {
261 case PSW_ASC_PRIMARY:
262 PTE_DPRINTF("%s: asc=primary\n", __func__);
263 asce = env->cregs[1];
264 break;
265 case PSW_ASC_SECONDARY:
266 PTE_DPRINTF("%s: asc=secondary\n", __func__);
267 asce = env->cregs[7];
268 break;
269 case PSW_ASC_HOME:
270 PTE_DPRINTF("%s: asc=home\n", __func__);
271 asce = env->cregs[13];
272 break;
275 switch (asce & _ASCE_TYPE_MASK) {
276 case _ASCE_TYPE_REGION1:
277 break;
278 case _ASCE_TYPE_REGION2:
279 if (vaddr & 0xffe0000000000000ULL) {
280 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
281 " 0xffe0000000000000ULL\n", __func__, vaddr);
282 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
283 return -1;
285 break;
286 case _ASCE_TYPE_REGION3:
287 if (vaddr & 0xfffffc0000000000ULL) {
288 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
289 " 0xfffffc0000000000ULL\n", __func__, vaddr);
290 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
291 return -1;
293 break;
294 case _ASCE_TYPE_SEGMENT:
295 if (vaddr & 0xffffffff80000000ULL) {
296 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
297 " 0xffffffff80000000ULL\n", __func__, vaddr);
298 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
299 return -1;
301 break;
304 /* fake level above current */
305 level = asce & _ASCE_TYPE_MASK;
306 new_level = level + 4;
307 asce = (asce & ~_ASCE_TYPE_MASK) | (new_level & _ASCE_TYPE_MASK);
309 r = mmu_translate_asce(env, vaddr, asc, asce, new_level, raddr, flags, rw);
311 if ((rw == 1) && !(*flags & PAGE_WRITE)) {
312 trigger_prot_fault(env, vaddr, asc);
313 return -1;
316 return r;
319 int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
320 target_ulong *raddr, int *flags)
322 int r = -1;
323 uint8_t *sk;
325 *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
326 vaddr &= TARGET_PAGE_MASK;
328 if (!(env->psw.mask & PSW_MASK_DAT)) {
329 *raddr = vaddr;
330 r = 0;
331 goto out;
334 switch (asc) {
335 case PSW_ASC_PRIMARY:
336 case PSW_ASC_HOME:
337 r = mmu_translate_asc(env, vaddr, asc, raddr, flags, rw);
338 break;
339 case PSW_ASC_SECONDARY:
341 * Instruction: Primary
342 * Data: Secondary
344 if (rw == 2) {
345 r = mmu_translate_asc(env, vaddr, PSW_ASC_PRIMARY, raddr, flags,
346 rw);
347 *flags &= ~(PAGE_READ | PAGE_WRITE);
348 } else {
349 r = mmu_translate_asc(env, vaddr, PSW_ASC_SECONDARY, raddr, flags,
350 rw);
351 *flags &= ~(PAGE_EXEC);
353 break;
354 case PSW_ASC_ACCREG:
355 default:
356 hw_error("guest switched to unknown asc mode\n");
357 break;
360 out:
361 /* Convert real address -> absolute address */
362 if (*raddr < 0x2000) {
363 *raddr = *raddr + env->psa;
366 if (*raddr <= ram_size) {
367 sk = &env->storage_keys[*raddr / TARGET_PAGE_SIZE];
368 if (*flags & PAGE_READ) {
369 *sk |= SK_R;
372 if (*flags & PAGE_WRITE) {
373 *sk |= SK_C;
377 return r;
380 int cpu_s390x_handle_mmu_fault(CPUS390XState *env, target_ulong orig_vaddr,
381 int rw, int mmu_idx)
383 uint64_t asc = env->psw.mask & PSW_MASK_ASC;
384 target_ulong vaddr, raddr;
385 int prot;
387 DPRINTF("%s: address 0x%" PRIx64 " rw %d mmu_idx %d\n",
388 __func__, orig_vaddr, rw, mmu_idx);
390 orig_vaddr &= TARGET_PAGE_MASK;
391 vaddr = orig_vaddr;
393 /* 31-Bit mode */
394 if (!(env->psw.mask & PSW_MASK_64)) {
395 vaddr &= 0x7fffffff;
398 if (mmu_translate(env, vaddr, rw, asc, &raddr, &prot)) {
399 /* Translation ended in exception */
400 return 1;
403 /* check out of RAM access */
404 if (raddr > (ram_size + virtio_size)) {
405 DPRINTF("%s: raddr %" PRIx64 " > ram_size %" PRIx64 "\n", __func__,
406 (uint64_t)raddr, (uint64_t)ram_size);
407 trigger_pgm_exception(env, PGM_ADDRESSING, ILEN_LATER);
408 return 1;
411 DPRINTF("%s: set tlb %" PRIx64 " -> %" PRIx64 " (%x)\n", __func__,
412 (uint64_t)vaddr, (uint64_t)raddr, prot);
414 tlb_set_page(env, orig_vaddr, raddr, prot,
415 mmu_idx, TARGET_PAGE_SIZE);
417 return 0;
420 hwaddr s390_cpu_get_phys_page_debug(CPUState *cs, vaddr vaddr)
422 S390CPU *cpu = S390_CPU(cs);
423 CPUS390XState *env = &cpu->env;
424 target_ulong raddr;
425 int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
426 int old_exc = env->exception_index;
427 uint64_t asc = env->psw.mask & PSW_MASK_ASC;
429 /* 31-Bit mode */
430 if (!(env->psw.mask & PSW_MASK_64)) {
431 vaddr &= 0x7fffffff;
434 mmu_translate(env, vaddr, 2, asc, &raddr, &prot);
435 env->exception_index = old_exc;
437 return raddr;
440 void load_psw(CPUS390XState *env, uint64_t mask, uint64_t addr)
442 if (mask & PSW_MASK_WAIT) {
443 S390CPU *cpu = s390_env_get_cpu(env);
444 CPUState *cs = CPU(cpu);
445 if (!(mask & (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK))) {
446 if (s390_del_running_cpu(cpu) == 0) {
447 #ifndef CONFIG_USER_ONLY
448 qemu_system_shutdown_request();
449 #endif
452 cs->halted = 1;
453 env->exception_index = EXCP_HLT;
456 env->psw.addr = addr;
457 env->psw.mask = mask;
458 env->cc_op = (mask >> 44) & 3;
461 static uint64_t get_psw_mask(CPUS390XState *env)
463 uint64_t r;
465 env->cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
467 r = env->psw.mask;
468 r &= ~PSW_MASK_CC;
469 assert(!(env->cc_op & ~3));
470 r |= (uint64_t)env->cc_op << 44;
472 return r;
475 static LowCore *cpu_map_lowcore(CPUS390XState *env)
477 LowCore *lowcore;
478 hwaddr len = sizeof(LowCore);
480 lowcore = cpu_physical_memory_map(env->psa, &len, 1);
482 if (len < sizeof(LowCore)) {
483 cpu_abort(env, "Could not map lowcore\n");
486 return lowcore;
489 static void cpu_unmap_lowcore(LowCore *lowcore)
491 cpu_physical_memory_unmap(lowcore, sizeof(LowCore), 1, sizeof(LowCore));
494 void *s390_cpu_physical_memory_map(CPUS390XState *env, hwaddr addr, hwaddr *len,
495 int is_write)
497 hwaddr start = addr;
499 /* Mind the prefix area. */
500 if (addr < 8192) {
501 /* Map the lowcore. */
502 start += env->psa;
503 *len = MIN(*len, 8192 - addr);
504 } else if ((addr >= env->psa) && (addr < env->psa + 8192)) {
505 /* Map the 0 page. */
506 start -= env->psa;
507 *len = MIN(*len, 8192 - start);
510 return cpu_physical_memory_map(start, len, is_write);
513 void s390_cpu_physical_memory_unmap(CPUS390XState *env, void *addr, hwaddr len,
514 int is_write)
516 cpu_physical_memory_unmap(addr, len, is_write, len);
519 static void do_svc_interrupt(CPUS390XState *env)
521 uint64_t mask, addr;
522 LowCore *lowcore;
524 lowcore = cpu_map_lowcore(env);
526 lowcore->svc_code = cpu_to_be16(env->int_svc_code);
527 lowcore->svc_ilen = cpu_to_be16(env->int_svc_ilen);
528 lowcore->svc_old_psw.mask = cpu_to_be64(get_psw_mask(env));
529 lowcore->svc_old_psw.addr = cpu_to_be64(env->psw.addr + env->int_svc_ilen);
530 mask = be64_to_cpu(lowcore->svc_new_psw.mask);
531 addr = be64_to_cpu(lowcore->svc_new_psw.addr);
533 cpu_unmap_lowcore(lowcore);
535 load_psw(env, mask, addr);
538 static void do_program_interrupt(CPUS390XState *env)
540 uint64_t mask, addr;
541 LowCore *lowcore;
542 int ilen = env->int_pgm_ilen;
544 switch (ilen) {
545 case ILEN_LATER:
546 ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
547 break;
548 case ILEN_LATER_INC:
549 ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
550 env->psw.addr += ilen;
551 break;
552 default:
553 assert(ilen == 2 || ilen == 4 || ilen == 6);
556 qemu_log_mask(CPU_LOG_INT, "%s: code=0x%x ilen=%d\n",
557 __func__, env->int_pgm_code, ilen);
559 lowcore = cpu_map_lowcore(env);
561 lowcore->pgm_ilen = cpu_to_be16(ilen);
562 lowcore->pgm_code = cpu_to_be16(env->int_pgm_code);
563 lowcore->program_old_psw.mask = cpu_to_be64(get_psw_mask(env));
564 lowcore->program_old_psw.addr = cpu_to_be64(env->psw.addr);
565 mask = be64_to_cpu(lowcore->program_new_psw.mask);
566 addr = be64_to_cpu(lowcore->program_new_psw.addr);
568 cpu_unmap_lowcore(lowcore);
570 DPRINTF("%s: %x %x %" PRIx64 " %" PRIx64 "\n", __func__,
571 env->int_pgm_code, ilen, env->psw.mask,
572 env->psw.addr);
574 load_psw(env, mask, addr);
577 #define VIRTIO_SUBCODE_64 0x0D00
579 static void do_ext_interrupt(CPUS390XState *env)
581 uint64_t mask, addr;
582 LowCore *lowcore;
583 ExtQueue *q;
585 if (!(env->psw.mask & PSW_MASK_EXT)) {
586 cpu_abort(env, "Ext int w/o ext mask\n");
589 if (env->ext_index < 0 || env->ext_index > MAX_EXT_QUEUE) {
590 cpu_abort(env, "Ext queue overrun: %d\n", env->ext_index);
593 q = &env->ext_queue[env->ext_index];
594 lowcore = cpu_map_lowcore(env);
596 lowcore->ext_int_code = cpu_to_be16(q->code);
597 lowcore->ext_params = cpu_to_be32(q->param);
598 lowcore->ext_params2 = cpu_to_be64(q->param64);
599 lowcore->external_old_psw.mask = cpu_to_be64(get_psw_mask(env));
600 lowcore->external_old_psw.addr = cpu_to_be64(env->psw.addr);
601 lowcore->cpu_addr = cpu_to_be16(env->cpu_num | VIRTIO_SUBCODE_64);
602 mask = be64_to_cpu(lowcore->external_new_psw.mask);
603 addr = be64_to_cpu(lowcore->external_new_psw.addr);
605 cpu_unmap_lowcore(lowcore);
607 env->ext_index--;
608 if (env->ext_index == -1) {
609 env->pending_int &= ~INTERRUPT_EXT;
612 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
613 env->psw.mask, env->psw.addr);
615 load_psw(env, mask, addr);
618 static void do_io_interrupt(CPUS390XState *env)
620 LowCore *lowcore;
621 IOIntQueue *q;
622 uint8_t isc;
623 int disable = 1;
624 int found = 0;
626 if (!(env->psw.mask & PSW_MASK_IO)) {
627 cpu_abort(env, "I/O int w/o I/O mask\n");
630 for (isc = 0; isc < ARRAY_SIZE(env->io_index); isc++) {
631 uint64_t isc_bits;
633 if (env->io_index[isc] < 0) {
634 continue;
636 if (env->io_index[isc] > MAX_IO_QUEUE) {
637 cpu_abort(env, "I/O queue overrun for isc %d: %d\n",
638 isc, env->io_index[isc]);
641 q = &env->io_queue[env->io_index[isc]][isc];
642 isc_bits = ISC_TO_ISC_BITS(IO_INT_WORD_ISC(q->word));
643 if (!(env->cregs[6] & isc_bits)) {
644 disable = 0;
645 continue;
647 if (!found) {
648 uint64_t mask, addr;
650 found = 1;
651 lowcore = cpu_map_lowcore(env);
653 lowcore->subchannel_id = cpu_to_be16(q->id);
654 lowcore->subchannel_nr = cpu_to_be16(q->nr);
655 lowcore->io_int_parm = cpu_to_be32(q->parm);
656 lowcore->io_int_word = cpu_to_be32(q->word);
657 lowcore->io_old_psw.mask = cpu_to_be64(get_psw_mask(env));
658 lowcore->io_old_psw.addr = cpu_to_be64(env->psw.addr);
659 mask = be64_to_cpu(lowcore->io_new_psw.mask);
660 addr = be64_to_cpu(lowcore->io_new_psw.addr);
662 cpu_unmap_lowcore(lowcore);
664 env->io_index[isc]--;
666 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
667 env->psw.mask, env->psw.addr);
668 load_psw(env, mask, addr);
670 if (env->io_index[isc] >= 0) {
671 disable = 0;
673 continue;
676 if (disable) {
677 env->pending_int &= ~INTERRUPT_IO;
682 static void do_mchk_interrupt(CPUS390XState *env)
684 uint64_t mask, addr;
685 LowCore *lowcore;
686 MchkQueue *q;
687 int i;
689 if (!(env->psw.mask & PSW_MASK_MCHECK)) {
690 cpu_abort(env, "Machine check w/o mchk mask\n");
693 if (env->mchk_index < 0 || env->mchk_index > MAX_MCHK_QUEUE) {
694 cpu_abort(env, "Mchk queue overrun: %d\n", env->mchk_index);
697 q = &env->mchk_queue[env->mchk_index];
699 if (q->type != 1) {
700 /* Don't know how to handle this... */
701 cpu_abort(env, "Unknown machine check type %d\n", q->type);
703 if (!(env->cregs[14] & (1 << 28))) {
704 /* CRW machine checks disabled */
705 return;
708 lowcore = cpu_map_lowcore(env);
710 for (i = 0; i < 16; i++) {
711 lowcore->floating_pt_save_area[i] = cpu_to_be64(env->fregs[i].ll);
712 lowcore->gpregs_save_area[i] = cpu_to_be64(env->regs[i]);
713 lowcore->access_regs_save_area[i] = cpu_to_be32(env->aregs[i]);
714 lowcore->cregs_save_area[i] = cpu_to_be64(env->cregs[i]);
716 lowcore->prefixreg_save_area = cpu_to_be32(env->psa);
717 lowcore->fpt_creg_save_area = cpu_to_be32(env->fpc);
718 lowcore->tod_progreg_save_area = cpu_to_be32(env->todpr);
719 lowcore->cpu_timer_save_area[0] = cpu_to_be32(env->cputm >> 32);
720 lowcore->cpu_timer_save_area[1] = cpu_to_be32((uint32_t)env->cputm);
721 lowcore->clock_comp_save_area[0] = cpu_to_be32(env->ckc >> 32);
722 lowcore->clock_comp_save_area[1] = cpu_to_be32((uint32_t)env->ckc);
724 lowcore->mcck_interruption_code[0] = cpu_to_be32(0x00400f1d);
725 lowcore->mcck_interruption_code[1] = cpu_to_be32(0x40330000);
726 lowcore->mcck_old_psw.mask = cpu_to_be64(get_psw_mask(env));
727 lowcore->mcck_old_psw.addr = cpu_to_be64(env->psw.addr);
728 mask = be64_to_cpu(lowcore->mcck_new_psw.mask);
729 addr = be64_to_cpu(lowcore->mcck_new_psw.addr);
731 cpu_unmap_lowcore(lowcore);
733 env->mchk_index--;
734 if (env->mchk_index == -1) {
735 env->pending_int &= ~INTERRUPT_MCHK;
738 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
739 env->psw.mask, env->psw.addr);
741 load_psw(env, mask, addr);
744 void s390_cpu_do_interrupt(CPUState *cs)
746 S390CPU *cpu = S390_CPU(cs);
747 CPUS390XState *env = &cpu->env;
749 qemu_log_mask(CPU_LOG_INT, "%s: %d at pc=%" PRIx64 "\n",
750 __func__, env->exception_index, env->psw.addr);
752 s390_add_running_cpu(cpu);
753 /* handle machine checks */
754 if ((env->psw.mask & PSW_MASK_MCHECK) &&
755 (env->exception_index == -1)) {
756 if (env->pending_int & INTERRUPT_MCHK) {
757 env->exception_index = EXCP_MCHK;
760 /* handle external interrupts */
761 if ((env->psw.mask & PSW_MASK_EXT) &&
762 env->exception_index == -1) {
763 if (env->pending_int & INTERRUPT_EXT) {
764 /* code is already in env */
765 env->exception_index = EXCP_EXT;
766 } else if (env->pending_int & INTERRUPT_TOD) {
767 cpu_inject_ext(cpu, 0x1004, 0, 0);
768 env->exception_index = EXCP_EXT;
769 env->pending_int &= ~INTERRUPT_EXT;
770 env->pending_int &= ~INTERRUPT_TOD;
771 } else if (env->pending_int & INTERRUPT_CPUTIMER) {
772 cpu_inject_ext(cpu, 0x1005, 0, 0);
773 env->exception_index = EXCP_EXT;
774 env->pending_int &= ~INTERRUPT_EXT;
775 env->pending_int &= ~INTERRUPT_TOD;
778 /* handle I/O interrupts */
779 if ((env->psw.mask & PSW_MASK_IO) &&
780 (env->exception_index == -1)) {
781 if (env->pending_int & INTERRUPT_IO) {
782 env->exception_index = EXCP_IO;
786 switch (env->exception_index) {
787 case EXCP_PGM:
788 do_program_interrupt(env);
789 break;
790 case EXCP_SVC:
791 do_svc_interrupt(env);
792 break;
793 case EXCP_EXT:
794 do_ext_interrupt(env);
795 break;
796 case EXCP_IO:
797 do_io_interrupt(env);
798 break;
799 case EXCP_MCHK:
800 do_mchk_interrupt(env);
801 break;
803 env->exception_index = -1;
805 if (!env->pending_int) {
806 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
810 #endif /* CONFIG_USER_ONLY */