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