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/>.
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"
30 //#define DEBUG_S390_PTE
31 //#define DEBUG_S390_STDOUT
34 #ifdef DEBUG_S390_STDOUT
35 #define DPRINTF(fmt, ...) \
36 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
37 qemu_log(fmt, ##__VA_ARGS__); } while (0)
39 #define DPRINTF(fmt, ...) \
40 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) \
48 #define PTE_DPRINTF DPRINTF
50 #define PTE_DPRINTF(fmt, ...) \
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
);
74 S390CPU
*cpu_s390x_init(const char *cpu_model
)
78 cpu
= S390_CPU(object_new(TYPE_S390_CPU
));
80 object_property_set_bool(OBJECT(cpu
), true, "realized", NULL
);
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
,
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
;
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
,
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
);
124 case PSW_ASC_PRIMARY
:
127 case PSW_ASC_SECONDARY
:
134 cpu_abort(CPU(cpu
), "unknown asc mode\n");
141 static void trigger_prot_fault(CPUS390XState
*env
, target_ulong vaddr
,
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
);
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. */
167 DPRINTF("%s: vaddr=%016" PRIx64
" bits=%d\n", __func__
, vaddr
, bits
);
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. */
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
);
199 if (asce
& _PAGE_RO
) {
200 *flags
&= ~PAGE_WRITE
;
203 *raddr
= asce
& _ASCE_ORIGIN
;
205 PTE_DPRINTF("%s: PTE=0x%" PRIx64
"\n", __func__
, asce
);
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
,
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
);
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
);
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
));
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
);
251 if ((level
<= _ASCE_TYPE_MASK
) && ((asce
& _ASCE_TYPE_MASK
) != level
)) {
252 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
);
256 if (asce
& _ASCE_REAL_SPACE
) {
263 origin
= asce
& _ASCE_ORIGIN
;
266 case _ASCE_TYPE_REGION1
+ 4:
267 offs
= (vaddr
>> 50) & 0x3ff8;
269 case _ASCE_TYPE_REGION1
:
270 offs
= (vaddr
>> 39) & 0x3ff8;
272 case _ASCE_TYPE_REGION2
:
273 offs
= (vaddr
>> 28) & 0x3ff8;
275 case _ASCE_TYPE_REGION3
:
276 offs
= (vaddr
>> 17) & 0x3ff8;
278 case _ASCE_TYPE_SEGMENT
:
279 offs
= (vaddr
>> 9) & 0x07f8;
280 origin
= asce
& _SEGMENT_ENTRY_ORIGIN
;
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
) {
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
)) {
297 return mmu_translate_sfaa(env
, vaddr
, asc
, new_asce
, raddr
, flags
, rw
);
299 /* yet another region */
300 return mmu_translate_asce(env
, vaddr
, asc
, new_asce
, level
- 4, raddr
,
305 static int mmu_translate_asc(CPUS390XState
*env
, target_ulong vaddr
,
306 uint64_t asc
, target_ulong
*raddr
, int *flags
,
310 int level
, new_level
;
314 case PSW_ASC_PRIMARY
:
315 PTE_DPRINTF("%s: asc=primary\n", __func__
);
316 asce
= env
->cregs
[1];
318 case PSW_ASC_SECONDARY
:
319 PTE_DPRINTF("%s: asc=secondary\n", __func__
);
320 asce
= env
->cregs
[7];
323 PTE_DPRINTF("%s: asc=home\n", __func__
);
324 asce
= env
->cregs
[13];
328 switch (asce
& _ASCE_TYPE_MASK
) {
329 case _ASCE_TYPE_REGION1
:
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
);
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
);
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
);
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
);
372 int mmu_translate(CPUS390XState
*env
, target_ulong vaddr
, int rw
, uint64_t asc
,
373 target_ulong
*raddr
, int *flags
)
378 *flags
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
379 vaddr
&= TARGET_PAGE_MASK
;
381 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
388 case PSW_ASC_PRIMARY
:
390 r
= mmu_translate_asc(env
, vaddr
, asc
, raddr
, flags
, rw
);
392 case PSW_ASC_SECONDARY
:
394 * Instruction: Primary
398 r
= mmu_translate_asc(env
, vaddr
, PSW_ASC_PRIMARY
, raddr
, flags
,
400 *flags
&= ~(PAGE_READ
| PAGE_WRITE
);
402 r
= mmu_translate_asc(env
, vaddr
, PSW_ASC_SECONDARY
, raddr
, flags
,
404 *flags
&= ~(PAGE_EXEC
);
409 hw_error("guest switched to unknown asc mode\n");
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
) {
423 if (*flags
& PAGE_WRITE
) {
431 int s390_cpu_handle_mmu_fault(CPUState
*cs
, vaddr orig_vaddr
,
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
;
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
;
447 if (!(env
->psw
.mask
& PSW_MASK_64
)) {
451 if (mmu_translate(env
, vaddr
, rw
, asc
, &raddr
, &prot
)) {
452 /* Translation ended in exception */
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
);
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
);
473 hwaddr
s390_cpu_get_phys_page_debug(CPUState
*cs
, vaddr vaddr
)
475 S390CPU
*cpu
= S390_CPU(cs
);
476 CPUS390XState
*env
= &cpu
->env
;
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
;
483 if (!(env
->psw
.mask
& PSW_MASK_64
)) {
487 mmu_translate(env
, vaddr
, 2, asc
, &raddr
, &prot
);
488 cs
->exception_index
= old_exc
;
493 hwaddr
s390_cpu_get_phys_addr_debug(CPUState
*cs
, vaddr vaddr
)
498 page
= vaddr
& TARGET_PAGE_MASK
;
499 phys_addr
= cpu_get_phys_page_debug(cs
, page
);
500 phys_addr
+= (vaddr
& ~TARGET_PAGE_MASK
);
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();
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
)
530 env
->cc_op
= calc_cc(env
, env
->cc_op
, env
->cc_src
, env
->cc_dst
, env
->cc_vr
);
534 assert(!(env
->cc_op
& ~3));
535 r
|= (uint64_t)env
->cc_op
<< 44;
540 static LowCore
*cpu_map_lowcore(CPUS390XState
*env
)
542 S390CPU
*cpu
= s390_env_get_cpu(env
);
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");
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
,
565 /* Mind the prefix area. */
567 /* Map the lowcore. */
569 *len
= MIN(*len
, 8192 - addr
);
570 } else if ((addr
>= env
->psa
) && (addr
< env
->psa
+ 8192)) {
571 /* Map the 0 page. */
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
,
582 cpu_physical_memory_unmap(addr
, len
, is_write
, len
);
585 static void do_svc_interrupt(CPUS390XState
*env
)
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
)
608 int ilen
= env
->int_pgm_ilen
;
612 ilen
= get_ilen(cpu_ldub_code(env
, env
->psw
.addr
));
615 ilen
= get_ilen(cpu_ldub_code(env
, env
->psw
.addr
));
616 env
->psw
.addr
+= ilen
;
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
,
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
);
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
);
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
);
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
++) {
701 if (env
->io_index
[isc
] < 0) {
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
)) {
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) {
745 env
->pending_int
&= ~INTERRUPT_IO
;
750 static void do_mchk_interrupt(CPUS390XState
*env
)
752 S390CPU
*cpu
= s390_env_get_cpu(env
);
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
];
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 */
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
);
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
) {
857 do_program_interrupt(env
);
860 do_svc_interrupt(env
);
863 do_ext_interrupt(env
);
866 do_io_interrupt(env
);
869 do_mchk_interrupt(env
);
872 cs
->exception_index
= -1;
874 if (!env
->pending_int
) {
875 cs
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
879 #endif /* CONFIG_USER_ONLY */