]> Git Repo - qemu.git/blob - target-s390x/helper.c
Merge remote-tracking branch 'remotes/alon/pull-libcacard.glusterfs' into staging
[qemu.git] / target-s390x / helper.c
1 /*
2  *  S/390 helpers
3  *
4  *  Copyright (c) 2009 Ulrich Hecht
5  *  Copyright (c) 2011 Alexander Graf
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
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
27
28 //#define DEBUG_S390
29 //#define DEBUG_S390_PTE
30 //#define DEBUG_S390_STDOUT
31
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
45
46 #ifdef DEBUG_S390_PTE
47 #define PTE_DPRINTF DPRINTF
48 #else
49 #define PTE_DPRINTF(fmt, ...) \
50     do { } while (0)
51 #endif
52
53 #ifndef CONFIG_USER_ONLY
54 void s390x_tod_timer(void *opaque)
55 {
56     S390CPU *cpu = opaque;
57     CPUS390XState *env = &cpu->env;
58
59     env->pending_int |= INTERRUPT_TOD;
60     cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
61 }
62
63 void s390x_cpu_timer(void *opaque)
64 {
65     S390CPU *cpu = opaque;
66     CPUS390XState *env = &cpu->env;
67
68     env->pending_int |= INTERRUPT_CPUTIMER;
69     cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
70 }
71 #endif
72
73 S390CPU *cpu_s390x_init(const char *cpu_model)
74 {
75     S390CPU *cpu;
76
77     cpu = S390_CPU(object_new(TYPE_S390_CPU));
78
79     object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
80
81     return cpu;
82 }
83
84 #if defined(CONFIG_USER_ONLY)
85
86 void s390_cpu_do_interrupt(CPUState *cs)
87 {
88     S390CPU *cpu = S390_CPU(cs);
89     CPUS390XState *env = &cpu->env;
90
91     env->exception_index = -1;
92 }
93
94 int cpu_s390x_handle_mmu_fault(CPUS390XState *env, target_ulong address,
95                                int rw, int mmu_idx)
96 {
97     env->exception_index = EXCP_PGM;
98     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     env->__excp_addr = address;
102     return 1;
103 }
104
105 #else /* !CONFIG_USER_ONLY */
106
107 /* Ensure to exit the TB after this call! */
108 static void trigger_pgm_exception(CPUS390XState *env, uint32_t code,
109                                   uint32_t ilen)
110 {
111     env->exception_index = EXCP_PGM;
112     env->int_pgm_code = code;
113     env->int_pgm_ilen = ilen;
114 }
115
116 static int trans_bits(CPUS390XState *env, uint64_t mode)
117 {
118     int bits = 0;
119
120     switch (mode) {
121     case PSW_ASC_PRIMARY:
122         bits = 1;
123         break;
124     case PSW_ASC_SECONDARY:
125         bits = 2;
126         break;
127     case PSW_ASC_HOME:
128         bits = 3;
129         break;
130     default:
131         cpu_abort(env, "unknown asc mode\n");
132         break;
133     }
134
135     return bits;
136 }
137
138 static void trigger_prot_fault(CPUS390XState *env, target_ulong vaddr,
139                                uint64_t mode)
140 {
141     CPUState *cs = ENV_GET_CPU(env);
142     int ilen = ILEN_LATER_INC;
143     int bits = trans_bits(env, mode) | 4;
144
145     DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
146
147     stq_phys(cs->as,
148              env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
149     trigger_pgm_exception(env, PGM_PROTECTION, ilen);
150 }
151
152 static void trigger_page_fault(CPUS390XState *env, target_ulong vaddr,
153                                uint32_t type, uint64_t asc, int rw)
154 {
155     CPUState *cs = ENV_GET_CPU(env);
156     int ilen = ILEN_LATER;
157     int bits = trans_bits(env, asc);
158
159     /* Code accesses have an undefined ilc.  */
160     if (rw == 2) {
161         ilen = 2;
162     }
163
164     DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
165
166     stq_phys(cs->as,
167              env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
168     trigger_pgm_exception(env, type, ilen);
169 }
170
171 static int mmu_translate_asce(CPUS390XState *env, target_ulong vaddr,
172                               uint64_t asc, uint64_t asce, int level,
173                               target_ulong *raddr, int *flags, int rw)
174 {
175     CPUState *cs = ENV_GET_CPU(env);
176     uint64_t offs = 0;
177     uint64_t origin;
178     uint64_t new_asce;
179
180     PTE_DPRINTF("%s: 0x%" PRIx64 "\n", __func__, asce);
181
182     if (((level != _ASCE_TYPE_SEGMENT) && (asce & _REGION_ENTRY_INV)) ||
183         ((level == _ASCE_TYPE_SEGMENT) && (asce & _SEGMENT_ENTRY_INV))) {
184         /* XXX different regions have different faults */
185         DPRINTF("%s: invalid region\n", __func__);
186         trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw);
187         return -1;
188     }
189
190     if ((level <= _ASCE_TYPE_MASK) && ((asce & _ASCE_TYPE_MASK) != level)) {
191         trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
192         return -1;
193     }
194
195     if (asce & _ASCE_REAL_SPACE) {
196         /* direct mapping */
197
198         *raddr = vaddr;
199         return 0;
200     }
201
202     origin = asce & _ASCE_ORIGIN;
203
204     switch (level) {
205     case _ASCE_TYPE_REGION1 + 4:
206         offs = (vaddr >> 50) & 0x3ff8;
207         break;
208     case _ASCE_TYPE_REGION1:
209         offs = (vaddr >> 39) & 0x3ff8;
210         break;
211     case _ASCE_TYPE_REGION2:
212         offs = (vaddr >> 28) & 0x3ff8;
213         break;
214     case _ASCE_TYPE_REGION3:
215         offs = (vaddr >> 17) & 0x3ff8;
216         break;
217     case _ASCE_TYPE_SEGMENT:
218         offs = (vaddr >> 9) & 0x07f8;
219         origin = asce & _SEGMENT_ENTRY_ORIGIN;
220         break;
221     }
222
223     /* XXX region protection flags */
224     /* *flags &= ~PAGE_WRITE */
225
226     new_asce = ldq_phys(cs->as, origin + offs);
227     PTE_DPRINTF("%s: 0x%" PRIx64 " + 0x%" PRIx64 " => 0x%016" PRIx64 "\n",
228                 __func__, origin, offs, new_asce);
229
230     if (level != _ASCE_TYPE_SEGMENT) {
231         /* yet another region */
232         return mmu_translate_asce(env, vaddr, asc, new_asce, level - 4, raddr,
233                                   flags, rw);
234     }
235
236     /* PTE */
237     if (new_asce & _PAGE_INVALID) {
238         DPRINTF("%s: PTE=0x%" PRIx64 " invalid\n", __func__, new_asce);
239         trigger_page_fault(env, vaddr, PGM_PAGE_TRANS, asc, rw);
240         return -1;
241     }
242
243     if (new_asce & _PAGE_RO) {
244         *flags &= ~PAGE_WRITE;
245     }
246
247     *raddr = new_asce & _ASCE_ORIGIN;
248
249     PTE_DPRINTF("%s: PTE=0x%" PRIx64 "\n", __func__, new_asce);
250
251     return 0;
252 }
253
254 static int mmu_translate_asc(CPUS390XState *env, target_ulong vaddr,
255                              uint64_t asc, target_ulong *raddr, int *flags,
256                              int rw)
257 {
258     uint64_t asce = 0;
259     int level, new_level;
260     int r;
261
262     switch (asc) {
263     case PSW_ASC_PRIMARY:
264         PTE_DPRINTF("%s: asc=primary\n", __func__);
265         asce = env->cregs[1];
266         break;
267     case PSW_ASC_SECONDARY:
268         PTE_DPRINTF("%s: asc=secondary\n", __func__);
269         asce = env->cregs[7];
270         break;
271     case PSW_ASC_HOME:
272         PTE_DPRINTF("%s: asc=home\n", __func__);
273         asce = env->cregs[13];
274         break;
275     }
276
277     switch (asce & _ASCE_TYPE_MASK) {
278     case _ASCE_TYPE_REGION1:
279         break;
280     case _ASCE_TYPE_REGION2:
281         if (vaddr & 0xffe0000000000000ULL) {
282             DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
283                     " 0xffe0000000000000ULL\n", __func__, vaddr);
284             trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
285             return -1;
286         }
287         break;
288     case _ASCE_TYPE_REGION3:
289         if (vaddr & 0xfffffc0000000000ULL) {
290             DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
291                     " 0xfffffc0000000000ULL\n", __func__, vaddr);
292             trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
293             return -1;
294         }
295         break;
296     case _ASCE_TYPE_SEGMENT:
297         if (vaddr & 0xffffffff80000000ULL) {
298             DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
299                     " 0xffffffff80000000ULL\n", __func__, vaddr);
300             trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
301             return -1;
302         }
303         break;
304     }
305
306     /* fake level above current */
307     level = asce & _ASCE_TYPE_MASK;
308     new_level = level + 4;
309     asce = (asce & ~_ASCE_TYPE_MASK) | (new_level & _ASCE_TYPE_MASK);
310
311     r = mmu_translate_asce(env, vaddr, asc, asce, new_level, raddr, flags, rw);
312
313     if ((rw == 1) && !(*flags & PAGE_WRITE)) {
314         trigger_prot_fault(env, vaddr, asc);
315         return -1;
316     }
317
318     return r;
319 }
320
321 int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
322                   target_ulong *raddr, int *flags)
323 {
324     int r = -1;
325     uint8_t *sk;
326
327     *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
328     vaddr &= TARGET_PAGE_MASK;
329
330     if (!(env->psw.mask & PSW_MASK_DAT)) {
331         *raddr = vaddr;
332         r = 0;
333         goto out;
334     }
335
336     switch (asc) {
337     case PSW_ASC_PRIMARY:
338     case PSW_ASC_HOME:
339         r = mmu_translate_asc(env, vaddr, asc, raddr, flags, rw);
340         break;
341     case PSW_ASC_SECONDARY:
342         /*
343          * Instruction: Primary
344          * Data: Secondary
345          */
346         if (rw == 2) {
347             r = mmu_translate_asc(env, vaddr, PSW_ASC_PRIMARY, raddr, flags,
348                                   rw);
349             *flags &= ~(PAGE_READ | PAGE_WRITE);
350         } else {
351             r = mmu_translate_asc(env, vaddr, PSW_ASC_SECONDARY, raddr, flags,
352                                   rw);
353             *flags &= ~(PAGE_EXEC);
354         }
355         break;
356     case PSW_ASC_ACCREG:
357     default:
358         hw_error("guest switched to unknown asc mode\n");
359         break;
360     }
361
362  out:
363     /* Convert real address -> absolute address */
364     if (*raddr < 0x2000) {
365         *raddr = *raddr + env->psa;
366     }
367
368     if (*raddr <= ram_size) {
369         sk = &env->storage_keys[*raddr / TARGET_PAGE_SIZE];
370         if (*flags & PAGE_READ) {
371             *sk |= SK_R;
372         }
373
374         if (*flags & PAGE_WRITE) {
375             *sk |= SK_C;
376         }
377     }
378
379     return r;
380 }
381
382 int cpu_s390x_handle_mmu_fault(CPUS390XState *env, target_ulong orig_vaddr,
383                                int rw, int mmu_idx)
384 {
385     uint64_t asc = env->psw.mask & PSW_MASK_ASC;
386     target_ulong vaddr, raddr;
387     int prot;
388
389     DPRINTF("%s: address 0x%" PRIx64 " rw %d mmu_idx %d\n",
390             __func__, orig_vaddr, rw, mmu_idx);
391
392     orig_vaddr &= TARGET_PAGE_MASK;
393     vaddr = orig_vaddr;
394
395     /* 31-Bit mode */
396     if (!(env->psw.mask & PSW_MASK_64)) {
397         vaddr &= 0x7fffffff;
398     }
399
400     if (mmu_translate(env, vaddr, rw, asc, &raddr, &prot)) {
401         /* Translation ended in exception */
402         return 1;
403     }
404
405     /* check out of RAM access */
406     if (raddr > (ram_size + virtio_size)) {
407         DPRINTF("%s: raddr %" PRIx64 " > ram_size %" PRIx64 "\n", __func__,
408                 (uint64_t)raddr, (uint64_t)ram_size);
409         trigger_pgm_exception(env, PGM_ADDRESSING, ILEN_LATER);
410         return 1;
411     }
412
413     DPRINTF("%s: set tlb %" PRIx64 " -> %" PRIx64 " (%x)\n", __func__,
414             (uint64_t)vaddr, (uint64_t)raddr, prot);
415
416     tlb_set_page(env, orig_vaddr, raddr, prot,
417                  mmu_idx, TARGET_PAGE_SIZE);
418
419     return 0;
420 }
421
422 hwaddr s390_cpu_get_phys_page_debug(CPUState *cs, vaddr vaddr)
423 {
424     S390CPU *cpu = S390_CPU(cs);
425     CPUS390XState *env = &cpu->env;
426     target_ulong raddr;
427     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
428     int old_exc = env->exception_index;
429     uint64_t asc = env->psw.mask & PSW_MASK_ASC;
430
431     /* 31-Bit mode */
432     if (!(env->psw.mask & PSW_MASK_64)) {
433         vaddr &= 0x7fffffff;
434     }
435
436     mmu_translate(env, vaddr, 2, asc, &raddr, &prot);
437     env->exception_index = old_exc;
438
439     return raddr;
440 }
441
442 void load_psw(CPUS390XState *env, uint64_t mask, uint64_t addr)
443 {
444     if (mask & PSW_MASK_WAIT) {
445         S390CPU *cpu = s390_env_get_cpu(env);
446         CPUState *cs = CPU(cpu);
447         if (!(mask & (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK))) {
448             if (s390_del_running_cpu(cpu) == 0) {
449 #ifndef CONFIG_USER_ONLY
450                 qemu_system_shutdown_request();
451 #endif
452             }
453         }
454         cs->halted = 1;
455         env->exception_index = EXCP_HLT;
456     }
457
458     env->psw.addr = addr;
459     env->psw.mask = mask;
460     env->cc_op = (mask >> 44) & 3;
461 }
462
463 static uint64_t get_psw_mask(CPUS390XState *env)
464 {
465     uint64_t r;
466
467     env->cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
468
469     r = env->psw.mask;
470     r &= ~PSW_MASK_CC;
471     assert(!(env->cc_op & ~3));
472     r |= (uint64_t)env->cc_op << 44;
473
474     return r;
475 }
476
477 static LowCore *cpu_map_lowcore(CPUS390XState *env)
478 {
479     LowCore *lowcore;
480     hwaddr len = sizeof(LowCore);
481
482     lowcore = cpu_physical_memory_map(env->psa, &len, 1);
483
484     if (len < sizeof(LowCore)) {
485         cpu_abort(env, "Could not map lowcore\n");
486     }
487
488     return lowcore;
489 }
490
491 static void cpu_unmap_lowcore(LowCore *lowcore)
492 {
493     cpu_physical_memory_unmap(lowcore, sizeof(LowCore), 1, sizeof(LowCore));
494 }
495
496 void *s390_cpu_physical_memory_map(CPUS390XState *env, hwaddr addr, hwaddr *len,
497                                    int is_write)
498 {
499     hwaddr start = addr;
500
501     /* Mind the prefix area. */
502     if (addr < 8192) {
503         /* Map the lowcore. */
504         start += env->psa;
505         *len = MIN(*len, 8192 - addr);
506     } else if ((addr >= env->psa) && (addr < env->psa + 8192)) {
507         /* Map the 0 page. */
508         start -= env->psa;
509         *len = MIN(*len, 8192 - start);
510     }
511
512     return cpu_physical_memory_map(start, len, is_write);
513 }
514
515 void s390_cpu_physical_memory_unmap(CPUS390XState *env, void *addr, hwaddr len,
516                                     int is_write)
517 {
518     cpu_physical_memory_unmap(addr, len, is_write, len);
519 }
520
521 static void do_svc_interrupt(CPUS390XState *env)
522 {
523     uint64_t mask, addr;
524     LowCore *lowcore;
525
526     lowcore = cpu_map_lowcore(env);
527
528     lowcore->svc_code = cpu_to_be16(env->int_svc_code);
529     lowcore->svc_ilen = cpu_to_be16(env->int_svc_ilen);
530     lowcore->svc_old_psw.mask = cpu_to_be64(get_psw_mask(env));
531     lowcore->svc_old_psw.addr = cpu_to_be64(env->psw.addr + env->int_svc_ilen);
532     mask = be64_to_cpu(lowcore->svc_new_psw.mask);
533     addr = be64_to_cpu(lowcore->svc_new_psw.addr);
534
535     cpu_unmap_lowcore(lowcore);
536
537     load_psw(env, mask, addr);
538 }
539
540 static void do_program_interrupt(CPUS390XState *env)
541 {
542     uint64_t mask, addr;
543     LowCore *lowcore;
544     int ilen = env->int_pgm_ilen;
545
546     switch (ilen) {
547     case ILEN_LATER:
548         ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
549         break;
550     case ILEN_LATER_INC:
551         ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
552         env->psw.addr += ilen;
553         break;
554     default:
555         assert(ilen == 2 || ilen == 4 || ilen == 6);
556     }
557
558     qemu_log_mask(CPU_LOG_INT, "%s: code=0x%x ilen=%d\n",
559                   __func__, env->int_pgm_code, ilen);
560
561     lowcore = cpu_map_lowcore(env);
562
563     lowcore->pgm_ilen = cpu_to_be16(ilen);
564     lowcore->pgm_code = cpu_to_be16(env->int_pgm_code);
565     lowcore->program_old_psw.mask = cpu_to_be64(get_psw_mask(env));
566     lowcore->program_old_psw.addr = cpu_to_be64(env->psw.addr);
567     mask = be64_to_cpu(lowcore->program_new_psw.mask);
568     addr = be64_to_cpu(lowcore->program_new_psw.addr);
569
570     cpu_unmap_lowcore(lowcore);
571
572     DPRINTF("%s: %x %x %" PRIx64 " %" PRIx64 "\n", __func__,
573             env->int_pgm_code, ilen, env->psw.mask,
574             env->psw.addr);
575
576     load_psw(env, mask, addr);
577 }
578
579 #define VIRTIO_SUBCODE_64 0x0D00
580
581 static void do_ext_interrupt(CPUS390XState *env)
582 {
583     uint64_t mask, addr;
584     LowCore *lowcore;
585     ExtQueue *q;
586
587     if (!(env->psw.mask & PSW_MASK_EXT)) {
588         cpu_abort(env, "Ext int w/o ext mask\n");
589     }
590
591     if (env->ext_index < 0 || env->ext_index > MAX_EXT_QUEUE) {
592         cpu_abort(env, "Ext queue overrun: %d\n", env->ext_index);
593     }
594
595     q = &env->ext_queue[env->ext_index];
596     lowcore = cpu_map_lowcore(env);
597
598     lowcore->ext_int_code = cpu_to_be16(q->code);
599     lowcore->ext_params = cpu_to_be32(q->param);
600     lowcore->ext_params2 = cpu_to_be64(q->param64);
601     lowcore->external_old_psw.mask = cpu_to_be64(get_psw_mask(env));
602     lowcore->external_old_psw.addr = cpu_to_be64(env->psw.addr);
603     lowcore->cpu_addr = cpu_to_be16(env->cpu_num | VIRTIO_SUBCODE_64);
604     mask = be64_to_cpu(lowcore->external_new_psw.mask);
605     addr = be64_to_cpu(lowcore->external_new_psw.addr);
606
607     cpu_unmap_lowcore(lowcore);
608
609     env->ext_index--;
610     if (env->ext_index == -1) {
611         env->pending_int &= ~INTERRUPT_EXT;
612     }
613
614     DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
615             env->psw.mask, env->psw.addr);
616
617     load_psw(env, mask, addr);
618 }
619
620 static void do_io_interrupt(CPUS390XState *env)
621 {
622     LowCore *lowcore;
623     IOIntQueue *q;
624     uint8_t isc;
625     int disable = 1;
626     int found = 0;
627
628     if (!(env->psw.mask & PSW_MASK_IO)) {
629         cpu_abort(env, "I/O int w/o I/O mask\n");
630     }
631
632     for (isc = 0; isc < ARRAY_SIZE(env->io_index); isc++) {
633         uint64_t isc_bits;
634
635         if (env->io_index[isc] < 0) {
636             continue;
637         }
638         if (env->io_index[isc] > MAX_IO_QUEUE) {
639             cpu_abort(env, "I/O queue overrun for isc %d: %d\n",
640                       isc, env->io_index[isc]);
641         }
642
643         q = &env->io_queue[env->io_index[isc]][isc];
644         isc_bits = ISC_TO_ISC_BITS(IO_INT_WORD_ISC(q->word));
645         if (!(env->cregs[6] & isc_bits)) {
646             disable = 0;
647             continue;
648         }
649         if (!found) {
650             uint64_t mask, addr;
651
652             found = 1;
653             lowcore = cpu_map_lowcore(env);
654
655             lowcore->subchannel_id = cpu_to_be16(q->id);
656             lowcore->subchannel_nr = cpu_to_be16(q->nr);
657             lowcore->io_int_parm = cpu_to_be32(q->parm);
658             lowcore->io_int_word = cpu_to_be32(q->word);
659             lowcore->io_old_psw.mask = cpu_to_be64(get_psw_mask(env));
660             lowcore->io_old_psw.addr = cpu_to_be64(env->psw.addr);
661             mask = be64_to_cpu(lowcore->io_new_psw.mask);
662             addr = be64_to_cpu(lowcore->io_new_psw.addr);
663
664             cpu_unmap_lowcore(lowcore);
665
666             env->io_index[isc]--;
667
668             DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
669                     env->psw.mask, env->psw.addr);
670             load_psw(env, mask, addr);
671         }
672         if (env->io_index[isc] >= 0) {
673             disable = 0;
674         }
675         continue;
676     }
677
678     if (disable) {
679         env->pending_int &= ~INTERRUPT_IO;
680     }
681
682 }
683
684 static void do_mchk_interrupt(CPUS390XState *env)
685 {
686     uint64_t mask, addr;
687     LowCore *lowcore;
688     MchkQueue *q;
689     int i;
690
691     if (!(env->psw.mask & PSW_MASK_MCHECK)) {
692         cpu_abort(env, "Machine check w/o mchk mask\n");
693     }
694
695     if (env->mchk_index < 0 || env->mchk_index > MAX_MCHK_QUEUE) {
696         cpu_abort(env, "Mchk queue overrun: %d\n", env->mchk_index);
697     }
698
699     q = &env->mchk_queue[env->mchk_index];
700
701     if (q->type != 1) {
702         /* Don't know how to handle this... */
703         cpu_abort(env, "Unknown machine check type %d\n", q->type);
704     }
705     if (!(env->cregs[14] & (1 << 28))) {
706         /* CRW machine checks disabled */
707         return;
708     }
709
710     lowcore = cpu_map_lowcore(env);
711
712     for (i = 0; i < 16; i++) {
713         lowcore->floating_pt_save_area[i] = cpu_to_be64(env->fregs[i].ll);
714         lowcore->gpregs_save_area[i] = cpu_to_be64(env->regs[i]);
715         lowcore->access_regs_save_area[i] = cpu_to_be32(env->aregs[i]);
716         lowcore->cregs_save_area[i] = cpu_to_be64(env->cregs[i]);
717     }
718     lowcore->prefixreg_save_area = cpu_to_be32(env->psa);
719     lowcore->fpt_creg_save_area = cpu_to_be32(env->fpc);
720     lowcore->tod_progreg_save_area = cpu_to_be32(env->todpr);
721     lowcore->cpu_timer_save_area[0] = cpu_to_be32(env->cputm >> 32);
722     lowcore->cpu_timer_save_area[1] = cpu_to_be32((uint32_t)env->cputm);
723     lowcore->clock_comp_save_area[0] = cpu_to_be32(env->ckc >> 32);
724     lowcore->clock_comp_save_area[1] = cpu_to_be32((uint32_t)env->ckc);
725
726     lowcore->mcck_interruption_code[0] = cpu_to_be32(0x00400f1d);
727     lowcore->mcck_interruption_code[1] = cpu_to_be32(0x40330000);
728     lowcore->mcck_old_psw.mask = cpu_to_be64(get_psw_mask(env));
729     lowcore->mcck_old_psw.addr = cpu_to_be64(env->psw.addr);
730     mask = be64_to_cpu(lowcore->mcck_new_psw.mask);
731     addr = be64_to_cpu(lowcore->mcck_new_psw.addr);
732
733     cpu_unmap_lowcore(lowcore);
734
735     env->mchk_index--;
736     if (env->mchk_index == -1) {
737         env->pending_int &= ~INTERRUPT_MCHK;
738     }
739
740     DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
741             env->psw.mask, env->psw.addr);
742
743     load_psw(env, mask, addr);
744 }
745
746 void s390_cpu_do_interrupt(CPUState *cs)
747 {
748     S390CPU *cpu = S390_CPU(cs);
749     CPUS390XState *env = &cpu->env;
750
751     qemu_log_mask(CPU_LOG_INT, "%s: %d at pc=%" PRIx64 "\n",
752                   __func__, env->exception_index, env->psw.addr);
753
754     s390_add_running_cpu(cpu);
755     /* handle machine checks */
756     if ((env->psw.mask & PSW_MASK_MCHECK) &&
757         (env->exception_index == -1)) {
758         if (env->pending_int & INTERRUPT_MCHK) {
759             env->exception_index = EXCP_MCHK;
760         }
761     }
762     /* handle external interrupts */
763     if ((env->psw.mask & PSW_MASK_EXT) &&
764         env->exception_index == -1) {
765         if (env->pending_int & INTERRUPT_EXT) {
766             /* code is already in env */
767             env->exception_index = EXCP_EXT;
768         } else if (env->pending_int & INTERRUPT_TOD) {
769             cpu_inject_ext(cpu, 0x1004, 0, 0);
770             env->exception_index = EXCP_EXT;
771             env->pending_int &= ~INTERRUPT_EXT;
772             env->pending_int &= ~INTERRUPT_TOD;
773         } else if (env->pending_int & INTERRUPT_CPUTIMER) {
774             cpu_inject_ext(cpu, 0x1005, 0, 0);
775             env->exception_index = EXCP_EXT;
776             env->pending_int &= ~INTERRUPT_EXT;
777             env->pending_int &= ~INTERRUPT_TOD;
778         }
779     }
780     /* handle I/O interrupts */
781     if ((env->psw.mask & PSW_MASK_IO) &&
782         (env->exception_index == -1)) {
783         if (env->pending_int & INTERRUPT_IO) {
784             env->exception_index = EXCP_IO;
785         }
786     }
787
788     switch (env->exception_index) {
789     case EXCP_PGM:
790         do_program_interrupt(env);
791         break;
792     case EXCP_SVC:
793         do_svc_interrupt(env);
794         break;
795     case EXCP_EXT:
796         do_ext_interrupt(env);
797         break;
798     case EXCP_IO:
799         do_io_interrupt(env);
800         break;
801     case EXCP_MCHK:
802         do_mchk_interrupt(env);
803         break;
804     }
805     env->exception_index = -1;
806
807     if (!env->pending_int) {
808         cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
809     }
810 }
811
812 #endif /* CONFIG_USER_ONLY */
This page took 0.068552 seconds and 4 git commands to generate.