aio-win32: avoid out-of-bounds access to the events array
[qemu/ar7.git] / target-s390x / helper.c
blob67ab1065aa45d4eac3b352931e61280cb44a08e8
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 #include "exec/cpu_ldst.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "sysemu/sysemu.h"
27 #endif
29 //#define DEBUG_S390
30 //#define DEBUG_S390_PTE
31 //#define DEBUG_S390_STDOUT
33 #ifdef DEBUG_S390
34 #ifdef DEBUG_S390_STDOUT
35 #define DPRINTF(fmt, ...) \
36 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
37 qemu_log(fmt, ##__VA_ARGS__); } while (0)
38 #else
39 #define DPRINTF(fmt, ...) \
40 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
41 #endif
42 #else
43 #define DPRINTF(fmt, ...) \
44 do { } while (0)
45 #endif
47 #ifdef DEBUG_S390_PTE
48 #define PTE_DPRINTF DPRINTF
49 #else
50 #define PTE_DPRINTF(fmt, ...) \
51 do { } while (0)
52 #endif
54 #ifndef CONFIG_USER_ONLY
55 void s390x_tod_timer(void *opaque)
57 S390CPU *cpu = opaque;
58 CPUS390XState *env = &cpu->env;
60 env->pending_int |= INTERRUPT_TOD;
61 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
64 void s390x_cpu_timer(void *opaque)
66 S390CPU *cpu = opaque;
67 CPUS390XState *env = &cpu->env;
69 env->pending_int |= INTERRUPT_CPUTIMER;
70 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
72 #endif
74 S390CPU *cpu_s390x_init(const char *cpu_model)
76 S390CPU *cpu;
78 cpu = S390_CPU(object_new(TYPE_S390_CPU));
80 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
82 return cpu;
85 #if defined(CONFIG_USER_ONLY)
87 void s390_cpu_do_interrupt(CPUState *cs)
89 cs->exception_index = -1;
92 int s390_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
93 int rw, int mmu_idx)
95 S390CPU *cpu = S390_CPU(cs);
97 cs->exception_index = EXCP_PGM;
98 cpu->env.int_pgm_code = PGM_ADDRESSING;
99 /* On real machines this value is dropped into LowMem. Since this
100 is userland, simply put this someplace that cpu_loop can find it. */
101 cpu->env.__excp_addr = address;
102 return 1;
105 #else /* !CONFIG_USER_ONLY */
107 /* Ensure to exit the TB after this call! */
108 static void trigger_pgm_exception(CPUS390XState *env, uint32_t code,
109 uint32_t ilen)
111 CPUState *cs = CPU(s390_env_get_cpu(env));
113 cs->exception_index = EXCP_PGM;
114 env->int_pgm_code = code;
115 env->int_pgm_ilen = ilen;
118 static int trans_bits(CPUS390XState *env, uint64_t mode)
120 S390CPU *cpu = s390_env_get_cpu(env);
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(CPU(cpu), "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 CPUState *cs = CPU(s390_env_get_cpu(env));
145 int ilen = ILEN_LATER_INC;
146 int bits = trans_bits(env, mode) | 4;
148 DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
150 stq_phys(cs->as,
151 env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
152 trigger_pgm_exception(env, PGM_PROTECTION, ilen);
155 static void trigger_page_fault(CPUS390XState *env, target_ulong vaddr,
156 uint32_t type, uint64_t asc, int rw)
158 CPUState *cs = CPU(s390_env_get_cpu(env));
159 int ilen = ILEN_LATER;
160 int bits = trans_bits(env, asc);
162 /* Code accesses have an undefined ilc. */
163 if (rw == 2) {
164 ilen = 2;
167 DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
169 stq_phys(cs->as,
170 env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
171 trigger_pgm_exception(env, type, ilen);
175 * Translate real address to absolute (= physical)
176 * address by taking care of the prefix mapping.
178 static target_ulong mmu_real2abs(CPUS390XState *env, target_ulong raddr)
180 if (raddr < 0x2000) {
181 return raddr + env->psa; /* Map the lowcore. */
182 } else if (raddr >= env->psa && raddr < env->psa + 0x2000) {
183 return raddr - env->psa; /* Map the 0 page. */
185 return raddr;
188 /* Decode page table entry (normal 4KB page) */
189 static int mmu_translate_pte(CPUS390XState *env, target_ulong vaddr,
190 uint64_t asc, uint64_t asce,
191 target_ulong *raddr, int *flags, int rw)
193 if (asce & _PAGE_INVALID) {
194 DPRINTF("%s: PTE=0x%" PRIx64 " invalid\n", __func__, asce);
195 trigger_page_fault(env, vaddr, PGM_PAGE_TRANS, asc, rw);
196 return -1;
199 if (asce & _PAGE_RO) {
200 *flags &= ~PAGE_WRITE;
203 *raddr = asce & _ASCE_ORIGIN;
205 PTE_DPRINTF("%s: PTE=0x%" PRIx64 "\n", __func__, asce);
207 return 0;
210 /* Decode EDAT1 segment frame absolute address (1MB page) */
211 static int mmu_translate_sfaa(CPUS390XState *env, target_ulong vaddr,
212 uint64_t asc, uint64_t asce, target_ulong *raddr,
213 int *flags, int rw)
215 if (asce & _SEGMENT_ENTRY_INV) {
216 DPRINTF("%s: SEG=0x%" PRIx64 " invalid\n", __func__, asce);
217 trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw);
218 return -1;
221 if (asce & _SEGMENT_ENTRY_RO) {
222 *flags &= ~PAGE_WRITE;
225 *raddr = (asce & 0xfffffffffff00000ULL) | (vaddr & 0xfffff);
227 PTE_DPRINTF("%s: SEG=0x%" PRIx64 "\n", __func__, asce);
229 return 0;
232 static int mmu_translate_asce(CPUS390XState *env, target_ulong vaddr,
233 uint64_t asc, uint64_t asce, int level,
234 target_ulong *raddr, int *flags, int rw)
236 CPUState *cs = CPU(s390_env_get_cpu(env));
237 uint64_t offs = 0;
238 uint64_t origin;
239 uint64_t new_asce;
241 PTE_DPRINTF("%s: 0x%" PRIx64 "\n", __func__, asce);
243 if (((level != _ASCE_TYPE_SEGMENT) && (asce & _REGION_ENTRY_INV)) ||
244 ((level == _ASCE_TYPE_SEGMENT) && (asce & _SEGMENT_ENTRY_INV))) {
245 /* XXX different regions have different faults */
246 DPRINTF("%s: invalid region\n", __func__);
247 trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw);
248 return -1;
251 if ((level <= _ASCE_TYPE_MASK) && ((asce & _ASCE_TYPE_MASK) != level)) {
252 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
253 return -1;
256 if (asce & _ASCE_REAL_SPACE) {
257 /* direct mapping */
259 *raddr = vaddr;
260 return 0;
263 origin = asce & _ASCE_ORIGIN;
265 switch (level) {
266 case _ASCE_TYPE_REGION1 + 4:
267 offs = (vaddr >> 50) & 0x3ff8;
268 break;
269 case _ASCE_TYPE_REGION1:
270 offs = (vaddr >> 39) & 0x3ff8;
271 break;
272 case _ASCE_TYPE_REGION2:
273 offs = (vaddr >> 28) & 0x3ff8;
274 break;
275 case _ASCE_TYPE_REGION3:
276 offs = (vaddr >> 17) & 0x3ff8;
277 break;
278 case _ASCE_TYPE_SEGMENT:
279 offs = (vaddr >> 9) & 0x07f8;
280 origin = asce & _SEGMENT_ENTRY_ORIGIN;
281 break;
284 /* XXX region protection flags */
285 /* *flags &= ~PAGE_WRITE */
287 new_asce = ldq_phys(cs->as, origin + offs);
288 PTE_DPRINTF("%s: 0x%" PRIx64 " + 0x%" PRIx64 " => 0x%016" PRIx64 "\n",
289 __func__, origin, offs, new_asce);
291 if (level == _ASCE_TYPE_SEGMENT) {
292 /* 4KB page */
293 return mmu_translate_pte(env, vaddr, asc, new_asce, raddr, flags, rw);
294 } else if (level - 4 == _ASCE_TYPE_SEGMENT &&
295 (new_asce & _SEGMENT_ENTRY_FC) && (env->cregs[0] & CR0_EDAT)) {
296 /* 1MB page */
297 return mmu_translate_sfaa(env, vaddr, asc, new_asce, raddr, flags, rw);
298 } else {
299 /* yet another region */
300 return mmu_translate_asce(env, vaddr, asc, new_asce, level - 4, raddr,
301 flags, rw);
305 static int mmu_translate_asc(CPUS390XState *env, target_ulong vaddr,
306 uint64_t asc, target_ulong *raddr, int *flags,
307 int rw)
309 uint64_t asce = 0;
310 int level, new_level;
311 int r;
313 switch (asc) {
314 case PSW_ASC_PRIMARY:
315 PTE_DPRINTF("%s: asc=primary\n", __func__);
316 asce = env->cregs[1];
317 break;
318 case PSW_ASC_SECONDARY:
319 PTE_DPRINTF("%s: asc=secondary\n", __func__);
320 asce = env->cregs[7];
321 break;
322 case PSW_ASC_HOME:
323 PTE_DPRINTF("%s: asc=home\n", __func__);
324 asce = env->cregs[13];
325 break;
328 switch (asce & _ASCE_TYPE_MASK) {
329 case _ASCE_TYPE_REGION1:
330 break;
331 case _ASCE_TYPE_REGION2:
332 if (vaddr & 0xffe0000000000000ULL) {
333 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
334 " 0xffe0000000000000ULL\n", __func__, vaddr);
335 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
336 return -1;
338 break;
339 case _ASCE_TYPE_REGION3:
340 if (vaddr & 0xfffffc0000000000ULL) {
341 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
342 " 0xfffffc0000000000ULL\n", __func__, vaddr);
343 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
344 return -1;
346 break;
347 case _ASCE_TYPE_SEGMENT:
348 if (vaddr & 0xffffffff80000000ULL) {
349 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
350 " 0xffffffff80000000ULL\n", __func__, vaddr);
351 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
352 return -1;
354 break;
357 /* fake level above current */
358 level = asce & _ASCE_TYPE_MASK;
359 new_level = level + 4;
360 asce = (asce & ~_ASCE_TYPE_MASK) | (new_level & _ASCE_TYPE_MASK);
362 r = mmu_translate_asce(env, vaddr, asc, asce, new_level, raddr, flags, rw);
364 if ((rw == 1) && !(*flags & PAGE_WRITE)) {
365 trigger_prot_fault(env, vaddr, asc);
366 return -1;
369 return r;
372 int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
373 target_ulong *raddr, int *flags)
375 int r = -1;
376 uint8_t *sk;
378 *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
379 vaddr &= TARGET_PAGE_MASK;
381 if (!(env->psw.mask & PSW_MASK_DAT)) {
382 *raddr = vaddr;
383 r = 0;
384 goto out;
387 switch (asc) {
388 case PSW_ASC_PRIMARY:
389 case PSW_ASC_HOME:
390 r = mmu_translate_asc(env, vaddr, asc, raddr, flags, rw);
391 break;
392 case PSW_ASC_SECONDARY:
394 * Instruction: Primary
395 * Data: Secondary
397 if (rw == 2) {
398 r = mmu_translate_asc(env, vaddr, PSW_ASC_PRIMARY, raddr, flags,
399 rw);
400 *flags &= ~(PAGE_READ | PAGE_WRITE);
401 } else {
402 r = mmu_translate_asc(env, vaddr, PSW_ASC_SECONDARY, raddr, flags,
403 rw);
404 *flags &= ~(PAGE_EXEC);
406 break;
407 case PSW_ASC_ACCREG:
408 default:
409 hw_error("guest switched to unknown asc mode\n");
410 break;
413 out:
414 /* Convert real address -> absolute address */
415 *raddr = mmu_real2abs(env, *raddr);
417 if (*raddr <= ram_size) {
418 sk = &env->storage_keys[*raddr / TARGET_PAGE_SIZE];
419 if (*flags & PAGE_READ) {
420 *sk |= SK_R;
423 if (*flags & PAGE_WRITE) {
424 *sk |= SK_C;
428 return r;
431 int s390_cpu_handle_mmu_fault(CPUState *cs, vaddr orig_vaddr,
432 int rw, int mmu_idx)
434 S390CPU *cpu = S390_CPU(cs);
435 CPUS390XState *env = &cpu->env;
436 uint64_t asc = env->psw.mask & PSW_MASK_ASC;
437 target_ulong vaddr, raddr;
438 int prot;
440 DPRINTF("%s: address 0x%" VADDR_PRIx " rw %d mmu_idx %d\n",
441 __func__, orig_vaddr, rw, mmu_idx);
443 orig_vaddr &= TARGET_PAGE_MASK;
444 vaddr = orig_vaddr;
446 /* 31-Bit mode */
447 if (!(env->psw.mask & PSW_MASK_64)) {
448 vaddr &= 0x7fffffff;
451 if (mmu_translate(env, vaddr, rw, asc, &raddr, &prot)) {
452 /* Translation ended in exception */
453 return 1;
456 /* check out of RAM access */
457 if (raddr > (ram_size + virtio_size)) {
458 DPRINTF("%s: raddr %" PRIx64 " > ram_size %" PRIx64 "\n", __func__,
459 (uint64_t)raddr, (uint64_t)ram_size);
460 trigger_pgm_exception(env, PGM_ADDRESSING, ILEN_LATER);
461 return 1;
464 DPRINTF("%s: set tlb %" PRIx64 " -> %" PRIx64 " (%x)\n", __func__,
465 (uint64_t)vaddr, (uint64_t)raddr, prot);
467 tlb_set_page(cs, orig_vaddr, raddr, prot,
468 mmu_idx, TARGET_PAGE_SIZE);
470 return 0;
473 hwaddr s390_cpu_get_phys_page_debug(CPUState *cs, vaddr vaddr)
475 S390CPU *cpu = S390_CPU(cs);
476 CPUS390XState *env = &cpu->env;
477 target_ulong raddr;
478 int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
479 int old_exc = cs->exception_index;
480 uint64_t asc = env->psw.mask & PSW_MASK_ASC;
482 /* 31-Bit mode */
483 if (!(env->psw.mask & PSW_MASK_64)) {
484 vaddr &= 0x7fffffff;
487 mmu_translate(env, vaddr, 2, asc, &raddr, &prot);
488 cs->exception_index = old_exc;
490 return raddr;
493 hwaddr s390_cpu_get_phys_addr_debug(CPUState *cs, vaddr vaddr)
495 hwaddr phys_addr;
496 target_ulong page;
498 page = vaddr & TARGET_PAGE_MASK;
499 phys_addr = cpu_get_phys_page_debug(cs, page);
500 phys_addr += (vaddr & ~TARGET_PAGE_MASK);
502 return phys_addr;
505 void load_psw(CPUS390XState *env, uint64_t mask, uint64_t addr)
507 if (mask & PSW_MASK_WAIT) {
508 S390CPU *cpu = s390_env_get_cpu(env);
509 CPUState *cs = CPU(cpu);
510 if (!(mask & (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK))) {
511 if (s390_del_running_cpu(cpu) == 0) {
512 #ifndef CONFIG_USER_ONLY
513 qemu_system_shutdown_request();
514 #endif
517 cs->halted = 1;
518 cs->exception_index = EXCP_HLT;
521 env->psw.addr = addr;
522 env->psw.mask = mask;
523 env->cc_op = (mask >> 44) & 3;
526 static uint64_t get_psw_mask(CPUS390XState *env)
528 uint64_t r;
530 env->cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
532 r = env->psw.mask;
533 r &= ~PSW_MASK_CC;
534 assert(!(env->cc_op & ~3));
535 r |= (uint64_t)env->cc_op << 44;
537 return r;
540 static LowCore *cpu_map_lowcore(CPUS390XState *env)
542 S390CPU *cpu = s390_env_get_cpu(env);
543 LowCore *lowcore;
544 hwaddr len = sizeof(LowCore);
546 lowcore = cpu_physical_memory_map(env->psa, &len, 1);
548 if (len < sizeof(LowCore)) {
549 cpu_abort(CPU(cpu), "Could not map lowcore\n");
552 return lowcore;
555 static void cpu_unmap_lowcore(LowCore *lowcore)
557 cpu_physical_memory_unmap(lowcore, sizeof(LowCore), 1, sizeof(LowCore));
560 void *s390_cpu_physical_memory_map(CPUS390XState *env, hwaddr addr, hwaddr *len,
561 int is_write)
563 hwaddr start = addr;
565 /* Mind the prefix area. */
566 if (addr < 8192) {
567 /* Map the lowcore. */
568 start += env->psa;
569 *len = MIN(*len, 8192 - addr);
570 } else if ((addr >= env->psa) && (addr < env->psa + 8192)) {
571 /* Map the 0 page. */
572 start -= env->psa;
573 *len = MIN(*len, 8192 - start);
576 return cpu_physical_memory_map(start, len, is_write);
579 void s390_cpu_physical_memory_unmap(CPUS390XState *env, void *addr, hwaddr len,
580 int is_write)
582 cpu_physical_memory_unmap(addr, len, is_write, len);
585 static void do_svc_interrupt(CPUS390XState *env)
587 uint64_t mask, addr;
588 LowCore *lowcore;
590 lowcore = cpu_map_lowcore(env);
592 lowcore->svc_code = cpu_to_be16(env->int_svc_code);
593 lowcore->svc_ilen = cpu_to_be16(env->int_svc_ilen);
594 lowcore->svc_old_psw.mask = cpu_to_be64(get_psw_mask(env));
595 lowcore->svc_old_psw.addr = cpu_to_be64(env->psw.addr + env->int_svc_ilen);
596 mask = be64_to_cpu(lowcore->svc_new_psw.mask);
597 addr = be64_to_cpu(lowcore->svc_new_psw.addr);
599 cpu_unmap_lowcore(lowcore);
601 load_psw(env, mask, addr);
604 static void do_program_interrupt(CPUS390XState *env)
606 uint64_t mask, addr;
607 LowCore *lowcore;
608 int ilen = env->int_pgm_ilen;
610 switch (ilen) {
611 case ILEN_LATER:
612 ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
613 break;
614 case ILEN_LATER_INC:
615 ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
616 env->psw.addr += ilen;
617 break;
618 default:
619 assert(ilen == 2 || ilen == 4 || ilen == 6);
622 qemu_log_mask(CPU_LOG_INT, "%s: code=0x%x ilen=%d\n",
623 __func__, env->int_pgm_code, ilen);
625 lowcore = cpu_map_lowcore(env);
627 lowcore->pgm_ilen = cpu_to_be16(ilen);
628 lowcore->pgm_code = cpu_to_be16(env->int_pgm_code);
629 lowcore->program_old_psw.mask = cpu_to_be64(get_psw_mask(env));
630 lowcore->program_old_psw.addr = cpu_to_be64(env->psw.addr);
631 mask = be64_to_cpu(lowcore->program_new_psw.mask);
632 addr = be64_to_cpu(lowcore->program_new_psw.addr);
634 cpu_unmap_lowcore(lowcore);
636 DPRINTF("%s: %x %x %" PRIx64 " %" PRIx64 "\n", __func__,
637 env->int_pgm_code, ilen, env->psw.mask,
638 env->psw.addr);
640 load_psw(env, mask, addr);
643 #define VIRTIO_SUBCODE_64 0x0D00
645 static void do_ext_interrupt(CPUS390XState *env)
647 S390CPU *cpu = s390_env_get_cpu(env);
648 uint64_t mask, addr;
649 LowCore *lowcore;
650 ExtQueue *q;
652 if (!(env->psw.mask & PSW_MASK_EXT)) {
653 cpu_abort(CPU(cpu), "Ext int w/o ext mask\n");
656 if (env->ext_index < 0 || env->ext_index > MAX_EXT_QUEUE) {
657 cpu_abort(CPU(cpu), "Ext queue overrun: %d\n", env->ext_index);
660 q = &env->ext_queue[env->ext_index];
661 lowcore = cpu_map_lowcore(env);
663 lowcore->ext_int_code = cpu_to_be16(q->code);
664 lowcore->ext_params = cpu_to_be32(q->param);
665 lowcore->ext_params2 = cpu_to_be64(q->param64);
666 lowcore->external_old_psw.mask = cpu_to_be64(get_psw_mask(env));
667 lowcore->external_old_psw.addr = cpu_to_be64(env->psw.addr);
668 lowcore->cpu_addr = cpu_to_be16(env->cpu_num | VIRTIO_SUBCODE_64);
669 mask = be64_to_cpu(lowcore->external_new_psw.mask);
670 addr = be64_to_cpu(lowcore->external_new_psw.addr);
672 cpu_unmap_lowcore(lowcore);
674 env->ext_index--;
675 if (env->ext_index == -1) {
676 env->pending_int &= ~INTERRUPT_EXT;
679 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
680 env->psw.mask, env->psw.addr);
682 load_psw(env, mask, addr);
685 static void do_io_interrupt(CPUS390XState *env)
687 S390CPU *cpu = s390_env_get_cpu(env);
688 LowCore *lowcore;
689 IOIntQueue *q;
690 uint8_t isc;
691 int disable = 1;
692 int found = 0;
694 if (!(env->psw.mask & PSW_MASK_IO)) {
695 cpu_abort(CPU(cpu), "I/O int w/o I/O mask\n");
698 for (isc = 0; isc < ARRAY_SIZE(env->io_index); isc++) {
699 uint64_t isc_bits;
701 if (env->io_index[isc] < 0) {
702 continue;
704 if (env->io_index[isc] > MAX_IO_QUEUE) {
705 cpu_abort(CPU(cpu), "I/O queue overrun for isc %d: %d\n",
706 isc, env->io_index[isc]);
709 q = &env->io_queue[env->io_index[isc]][isc];
710 isc_bits = ISC_TO_ISC_BITS(IO_INT_WORD_ISC(q->word));
711 if (!(env->cregs[6] & isc_bits)) {
712 disable = 0;
713 continue;
715 if (!found) {
716 uint64_t mask, addr;
718 found = 1;
719 lowcore = cpu_map_lowcore(env);
721 lowcore->subchannel_id = cpu_to_be16(q->id);
722 lowcore->subchannel_nr = cpu_to_be16(q->nr);
723 lowcore->io_int_parm = cpu_to_be32(q->parm);
724 lowcore->io_int_word = cpu_to_be32(q->word);
725 lowcore->io_old_psw.mask = cpu_to_be64(get_psw_mask(env));
726 lowcore->io_old_psw.addr = cpu_to_be64(env->psw.addr);
727 mask = be64_to_cpu(lowcore->io_new_psw.mask);
728 addr = be64_to_cpu(lowcore->io_new_psw.addr);
730 cpu_unmap_lowcore(lowcore);
732 env->io_index[isc]--;
734 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
735 env->psw.mask, env->psw.addr);
736 load_psw(env, mask, addr);
738 if (env->io_index[isc] >= 0) {
739 disable = 0;
741 continue;
744 if (disable) {
745 env->pending_int &= ~INTERRUPT_IO;
750 static void do_mchk_interrupt(CPUS390XState *env)
752 S390CPU *cpu = s390_env_get_cpu(env);
753 uint64_t mask, addr;
754 LowCore *lowcore;
755 MchkQueue *q;
756 int i;
758 if (!(env->psw.mask & PSW_MASK_MCHECK)) {
759 cpu_abort(CPU(cpu), "Machine check w/o mchk mask\n");
762 if (env->mchk_index < 0 || env->mchk_index > MAX_MCHK_QUEUE) {
763 cpu_abort(CPU(cpu), "Mchk queue overrun: %d\n", env->mchk_index);
766 q = &env->mchk_queue[env->mchk_index];
768 if (q->type != 1) {
769 /* Don't know how to handle this... */
770 cpu_abort(CPU(cpu), "Unknown machine check type %d\n", q->type);
772 if (!(env->cregs[14] & (1 << 28))) {
773 /* CRW machine checks disabled */
774 return;
777 lowcore = cpu_map_lowcore(env);
779 for (i = 0; i < 16; i++) {
780 lowcore->floating_pt_save_area[i] = cpu_to_be64(env->fregs[i].ll);
781 lowcore->gpregs_save_area[i] = cpu_to_be64(env->regs[i]);
782 lowcore->access_regs_save_area[i] = cpu_to_be32(env->aregs[i]);
783 lowcore->cregs_save_area[i] = cpu_to_be64(env->cregs[i]);
785 lowcore->prefixreg_save_area = cpu_to_be32(env->psa);
786 lowcore->fpt_creg_save_area = cpu_to_be32(env->fpc);
787 lowcore->tod_progreg_save_area = cpu_to_be32(env->todpr);
788 lowcore->cpu_timer_save_area[0] = cpu_to_be32(env->cputm >> 32);
789 lowcore->cpu_timer_save_area[1] = cpu_to_be32((uint32_t)env->cputm);
790 lowcore->clock_comp_save_area[0] = cpu_to_be32(env->ckc >> 32);
791 lowcore->clock_comp_save_area[1] = cpu_to_be32((uint32_t)env->ckc);
793 lowcore->mcck_interruption_code[0] = cpu_to_be32(0x00400f1d);
794 lowcore->mcck_interruption_code[1] = cpu_to_be32(0x40330000);
795 lowcore->mcck_old_psw.mask = cpu_to_be64(get_psw_mask(env));
796 lowcore->mcck_old_psw.addr = cpu_to_be64(env->psw.addr);
797 mask = be64_to_cpu(lowcore->mcck_new_psw.mask);
798 addr = be64_to_cpu(lowcore->mcck_new_psw.addr);
800 cpu_unmap_lowcore(lowcore);
802 env->mchk_index--;
803 if (env->mchk_index == -1) {
804 env->pending_int &= ~INTERRUPT_MCHK;
807 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
808 env->psw.mask, env->psw.addr);
810 load_psw(env, mask, addr);
813 void s390_cpu_do_interrupt(CPUState *cs)
815 S390CPU *cpu = S390_CPU(cs);
816 CPUS390XState *env = &cpu->env;
818 qemu_log_mask(CPU_LOG_INT, "%s: %d at pc=%" PRIx64 "\n",
819 __func__, cs->exception_index, env->psw.addr);
821 s390_add_running_cpu(cpu);
822 /* handle machine checks */
823 if ((env->psw.mask & PSW_MASK_MCHECK) &&
824 (cs->exception_index == -1)) {
825 if (env->pending_int & INTERRUPT_MCHK) {
826 cs->exception_index = EXCP_MCHK;
829 /* handle external interrupts */
830 if ((env->psw.mask & PSW_MASK_EXT) &&
831 cs->exception_index == -1) {
832 if (env->pending_int & INTERRUPT_EXT) {
833 /* code is already in env */
834 cs->exception_index = EXCP_EXT;
835 } else if (env->pending_int & INTERRUPT_TOD) {
836 cpu_inject_ext(cpu, 0x1004, 0, 0);
837 cs->exception_index = EXCP_EXT;
838 env->pending_int &= ~INTERRUPT_EXT;
839 env->pending_int &= ~INTERRUPT_TOD;
840 } else if (env->pending_int & INTERRUPT_CPUTIMER) {
841 cpu_inject_ext(cpu, 0x1005, 0, 0);
842 cs->exception_index = EXCP_EXT;
843 env->pending_int &= ~INTERRUPT_EXT;
844 env->pending_int &= ~INTERRUPT_TOD;
847 /* handle I/O interrupts */
848 if ((env->psw.mask & PSW_MASK_IO) &&
849 (cs->exception_index == -1)) {
850 if (env->pending_int & INTERRUPT_IO) {
851 cs->exception_index = EXCP_IO;
855 switch (cs->exception_index) {
856 case EXCP_PGM:
857 do_program_interrupt(env);
858 break;
859 case EXCP_SVC:
860 do_svc_interrupt(env);
861 break;
862 case EXCP_EXT:
863 do_ext_interrupt(env);
864 break;
865 case EXCP_IO:
866 do_io_interrupt(env);
867 break;
868 case EXCP_MCHK:
869 do_mchk_interrupt(env);
870 break;
872 cs->exception_index = -1;
874 if (!env->pending_int) {
875 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
879 #endif /* CONFIG_USER_ONLY */