blob: 09aec7b42e7eb64b8c0f943459648fd49a8885a9 [file] [log] [blame]
Alexander Graf10ec5112009-12-05 12:44:21 +01001/*
2 * S/390 helpers
3 *
4 * Copyright (c) 2009 Ulrich Hecht
Alexander Grafd5a43962011-03-23 10:58:07 +01005 * Copyright (c) 2011 Alexander Graf
Alexander Graf10ec5112009-12-05 12:44:21 +01006 *
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
Blue Swirl70539e12010-03-07 15:48:43 +000018 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Alexander Graf10ec5112009-12-05 12:44:21 +010019 */
20
Alexander Graf10ec5112009-12-05 12:44:21 +010021#include "cpu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010022#include "exec/gdbstub.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/timer.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010024#include "exec/cpu_ldst.h"
Alexander Grafef815222011-10-07 09:51:50 +020025#ifndef CONFIG_USER_ONLY
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Alexander Grafef815222011-10-07 09:51:50 +020027#endif
Alexander Graf10ec5112009-12-05 12:44:21 +010028
Alexander Grafd5a43962011-03-23 10:58:07 +010029//#define DEBUG_S390
30//#define DEBUG_S390_PTE
31//#define DEBUG_S390_STDOUT
32
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
46
47#ifdef DEBUG_S390_PTE
48#define PTE_DPRINTF DPRINTF
49#else
50#define PTE_DPRINTF(fmt, ...) \
51 do { } while (0)
52#endif
53
54#ifndef CONFIG_USER_ONLY
Andreas Färber8f22e0d2012-04-02 13:56:29 +020055void s390x_tod_timer(void *opaque)
Alexander Grafd5a43962011-03-23 10:58:07 +010056{
Andreas Färberb8ba6792012-04-02 14:00:43 +020057 S390CPU *cpu = opaque;
58 CPUS390XState *env = &cpu->env;
Alexander Grafd5a43962011-03-23 10:58:07 +010059
60 env->pending_int |= INTERRUPT_TOD;
Andreas Färberc3affe52013-01-18 15:03:43 +010061 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
Alexander Grafd5a43962011-03-23 10:58:07 +010062}
63
Andreas Färber8f22e0d2012-04-02 13:56:29 +020064void s390x_cpu_timer(void *opaque)
Alexander Grafd5a43962011-03-23 10:58:07 +010065{
Andreas Färberb8ba6792012-04-02 14:00:43 +020066 S390CPU *cpu = opaque;
67 CPUS390XState *env = &cpu->env;
Alexander Grafd5a43962011-03-23 10:58:07 +010068
69 env->pending_int |= INTERRUPT_CPUTIMER;
Andreas Färberc3affe52013-01-18 15:03:43 +010070 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD);
Alexander Grafd5a43962011-03-23 10:58:07 +010071}
72#endif
Alexander Graf10c339a2009-12-05 12:44:26 +010073
Andreas Färber564b8632012-05-03 04:13:04 +020074S390CPU *cpu_s390x_init(const char *cpu_model)
Alexander Graf10ec5112009-12-05 12:44:21 +010075{
Andreas Färber29e4bcb2012-04-02 11:39:23 +020076 S390CPU *cpu;
Alexander Graf10ec5112009-12-05 12:44:21 +010077
Andreas Färber29e4bcb2012-04-02 11:39:23 +020078 cpu = S390_CPU(object_new(TYPE_S390_CPU));
Andreas Färber1f136632013-01-16 04:00:41 +010079
80 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
81
Andreas Färber564b8632012-05-03 04:13:04 +020082 return cpu;
Alexander Graf10ec5112009-12-05 12:44:21 +010083}
84
Alexander Grafd5a43962011-03-23 10:58:07 +010085#if defined(CONFIG_USER_ONLY)
86
Andreas Färber97a8ea52013-02-02 10:57:51 +010087void s390_cpu_do_interrupt(CPUState *cs)
Alexander Grafd5a43962011-03-23 10:58:07 +010088{
Andreas Färber27103422013-08-26 08:31:06 +020089 cs->exception_index = -1;
Alexander Grafd5a43962011-03-23 10:58:07 +010090}
91
Andreas Färber75104542013-08-26 03:01:33 +020092int s390_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
93 int rw, int mmu_idx)
Alexander Grafd5a43962011-03-23 10:58:07 +010094{
Andreas Färber75104542013-08-26 03:01:33 +020095 S390CPU *cpu = S390_CPU(cs);
96
Andreas Färber27103422013-08-26 08:31:06 +020097 cs->exception_index = EXCP_PGM;
Andreas Färber75104542013-08-26 03:01:33 +020098 cpu->env.int_pgm_code = PGM_ADDRESSING;
Richard Hendersond5a103c2012-09-14 19:31:57 -070099 /* On real machines this value is dropped into LowMem. Since this
100 is userland, simply put this someplace that cpu_loop can find it. */
Andreas Färber75104542013-08-26 03:01:33 +0200101 cpu->env.__excp_addr = address;
Alexander Grafd5a43962011-03-23 10:58:07 +0100102 return 1;
103}
104
Andreas Färberb7e516c2012-05-05 15:43:31 +0200105#else /* !CONFIG_USER_ONLY */
Alexander Graf10c339a2009-12-05 12:44:26 +0100106
Alexander Grafd5a43962011-03-23 10:58:07 +0100107/* Ensure to exit the TB after this call! */
Blue Swirl71e47082012-09-02 07:33:30 +0000108static void trigger_pgm_exception(CPUS390XState *env, uint32_t code,
Richard Hendersond5a103c2012-09-14 19:31:57 -0700109 uint32_t ilen)
Alexander Graf10c339a2009-12-05 12:44:26 +0100110{
Andreas Färber27103422013-08-26 08:31:06 +0200111 CPUState *cs = CPU(s390_env_get_cpu(env));
112
113 cs->exception_index = EXCP_PGM;
Alexander Grafd5a43962011-03-23 10:58:07 +0100114 env->int_pgm_code = code;
Richard Hendersond5a103c2012-09-14 19:31:57 -0700115 env->int_pgm_ilen = ilen;
Alexander Grafd5a43962011-03-23 10:58:07 +0100116}
Alexander Graf10c339a2009-12-05 12:44:26 +0100117
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100118static int trans_bits(CPUS390XState *env, uint64_t mode)
Alexander Grafd5a43962011-03-23 10:58:07 +0100119{
Andreas Färbera47dddd2013-09-03 17:38:47 +0200120 S390CPU *cpu = s390_env_get_cpu(env);
Alexander Grafd5a43962011-03-23 10:58:07 +0100121 int bits = 0;
Alexander Graf10c339a2009-12-05 12:44:26 +0100122
Alexander Grafd5a43962011-03-23 10:58:07 +0100123 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:
Andreas Färbera47dddd2013-09-03 17:38:47 +0200134 cpu_abort(CPU(cpu), "unknown asc mode\n");
Alexander Grafd5a43962011-03-23 10:58:07 +0100135 break;
136 }
Alexander Graf10c339a2009-12-05 12:44:26 +0100137
Alexander Grafd5a43962011-03-23 10:58:07 +0100138 return bits;
139}
140
Blue Swirl71e47082012-09-02 07:33:30 +0000141static void trigger_prot_fault(CPUS390XState *env, target_ulong vaddr,
142 uint64_t mode)
Alexander Grafd5a43962011-03-23 10:58:07 +0100143{
Andreas Färber2efc6be2014-03-09 19:40:08 +0100144 CPUState *cs = CPU(s390_env_get_cpu(env));
Richard Hendersond5a103c2012-09-14 19:31:57 -0700145 int ilen = ILEN_LATER_INC;
Alexander Grafd5a43962011-03-23 10:58:07 +0100146 int bits = trans_bits(env, mode) | 4;
147
Blue Swirl71e47082012-09-02 07:33:30 +0000148 DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
Alexander Grafd5a43962011-03-23 10:58:07 +0100149
Edgar E. Iglesiasf6066042013-11-28 00:11:44 +0100150 stq_phys(cs->as,
151 env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
Richard Hendersond5a103c2012-09-14 19:31:57 -0700152 trigger_pgm_exception(env, PGM_PROTECTION, ilen);
Alexander Grafd5a43962011-03-23 10:58:07 +0100153}
154
Blue Swirl71e47082012-09-02 07:33:30 +0000155static void trigger_page_fault(CPUS390XState *env, target_ulong vaddr,
156 uint32_t type, uint64_t asc, int rw)
Alexander Grafd5a43962011-03-23 10:58:07 +0100157{
Andreas Färber2efc6be2014-03-09 19:40:08 +0100158 CPUState *cs = CPU(s390_env_get_cpu(env));
Richard Hendersond5a103c2012-09-14 19:31:57 -0700159 int ilen = ILEN_LATER;
Alexander Grafd5a43962011-03-23 10:58:07 +0100160 int bits = trans_bits(env, asc);
161
Richard Hendersond5a103c2012-09-14 19:31:57 -0700162 /* Code accesses have an undefined ilc. */
Alexander Grafd5a43962011-03-23 10:58:07 +0100163 if (rw == 2) {
Richard Hendersond5a103c2012-09-14 19:31:57 -0700164 ilen = 2;
Alexander Grafd5a43962011-03-23 10:58:07 +0100165 }
166
Blue Swirl71e47082012-09-02 07:33:30 +0000167 DPRINTF("%s: vaddr=%016" PRIx64 " bits=%d\n", __func__, vaddr, bits);
Alexander Grafd5a43962011-03-23 10:58:07 +0100168
Edgar E. Iglesiasf6066042013-11-28 00:11:44 +0100169 stq_phys(cs->as,
170 env->psa + offsetof(LowCore, trans_exc_code), vaddr | bits);
Richard Hendersond5a103c2012-09-14 19:31:57 -0700171 trigger_pgm_exception(env, type, ilen);
Alexander Grafd5a43962011-03-23 10:58:07 +0100172}
173
Thomas Huth422f32c2013-09-03 13:31:36 +0200174/**
175 * Translate real address to absolute (= physical)
176 * address by taking care of the prefix mapping.
177 */
178static target_ulong mmu_real2abs(CPUS390XState *env, target_ulong raddr)
179{
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. */
184 }
185 return raddr;
186}
187
Thomas Huthc4400202014-04-25 15:37:19 +0200188/* Decode page table entry (normal 4KB page) */
189static int mmu_translate_pte(CPUS390XState *env, target_ulong vaddr,
190 uint64_t asc, uint64_t asce,
191 target_ulong *raddr, int *flags, int rw)
192{
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;
197 }
198
199 if (asce & _PAGE_RO) {
200 *flags &= ~PAGE_WRITE;
201 }
202
203 *raddr = asce & _ASCE_ORIGIN;
204
205 PTE_DPRINTF("%s: PTE=0x%" PRIx64 "\n", __func__, asce);
206
207 return 0;
208}
209
210/* Decode EDAT1 segment frame absolute address (1MB page) */
211static int mmu_translate_sfaa(CPUS390XState *env, target_ulong vaddr,
212 uint64_t asc, uint64_t asce, target_ulong *raddr,
213 int *flags, int rw)
214{
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;
219 }
220
221 if (asce & _SEGMENT_ENTRY_RO) {
222 *flags &= ~PAGE_WRITE;
223 }
224
225 *raddr = (asce & 0xfffffffffff00000ULL) | (vaddr & 0xfffff);
226
227 PTE_DPRINTF("%s: SEG=0x%" PRIx64 "\n", __func__, asce);
228
229 return 0;
230}
231
Blue Swirl71e47082012-09-02 07:33:30 +0000232static 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)
Alexander Grafd5a43962011-03-23 10:58:07 +0100235{
Andreas Färber2efc6be2014-03-09 19:40:08 +0100236 CPUState *cs = CPU(s390_env_get_cpu(env));
Alexander Grafd5a43962011-03-23 10:58:07 +0100237 uint64_t offs = 0;
238 uint64_t origin;
239 uint64_t new_asce;
240
Blue Swirl71e47082012-09-02 07:33:30 +0000241 PTE_DPRINTF("%s: 0x%" PRIx64 "\n", __func__, asce);
Alexander Grafd5a43962011-03-23 10:58:07 +0100242
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 */
Blue Swirl71e47082012-09-02 07:33:30 +0000246 DPRINTF("%s: invalid region\n", __func__);
Alexander Grafd5a43962011-03-23 10:58:07 +0100247 trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw);
248 return -1;
249 }
250
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;
254 }
255
256 if (asce & _ASCE_REAL_SPACE) {
257 /* direct mapping */
258
259 *raddr = vaddr;
260 return 0;
261 }
262
263 origin = asce & _ASCE_ORIGIN;
264
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;
282 }
283
284 /* XXX region protection flags */
285 /* *flags &= ~PAGE_WRITE */
286
Edgar E. Iglesias2c174492013-12-17 14:05:40 +1000287 new_asce = ldq_phys(cs->as, origin + offs);
Alexander Grafd5a43962011-03-23 10:58:07 +0100288 PTE_DPRINTF("%s: 0x%" PRIx64 " + 0x%" PRIx64 " => 0x%016" PRIx64 "\n",
Blue Swirl71e47082012-09-02 07:33:30 +0000289 __func__, origin, offs, new_asce);
Alexander Grafd5a43962011-03-23 10:58:07 +0100290
Thomas Huthc4400202014-04-25 15:37:19 +0200291 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 {
Alexander Grafd5a43962011-03-23 10:58:07 +0100299 /* yet another region */
300 return mmu_translate_asce(env, vaddr, asc, new_asce, level - 4, raddr,
301 flags, rw);
302 }
Alexander Graf10c339a2009-12-05 12:44:26 +0100303}
Alexander Grafd5a43962011-03-23 10:58:07 +0100304
Blue Swirl71e47082012-09-02 07:33:30 +0000305static int mmu_translate_asc(CPUS390XState *env, target_ulong vaddr,
306 uint64_t asc, target_ulong *raddr, int *flags,
307 int rw)
Alexander Grafd5a43962011-03-23 10:58:07 +0100308{
309 uint64_t asce = 0;
310 int level, new_level;
311 int r;
312
313 switch (asc) {
314 case PSW_ASC_PRIMARY:
Blue Swirl71e47082012-09-02 07:33:30 +0000315 PTE_DPRINTF("%s: asc=primary\n", __func__);
Alexander Grafd5a43962011-03-23 10:58:07 +0100316 asce = env->cregs[1];
317 break;
318 case PSW_ASC_SECONDARY:
Blue Swirl71e47082012-09-02 07:33:30 +0000319 PTE_DPRINTF("%s: asc=secondary\n", __func__);
Alexander Grafd5a43962011-03-23 10:58:07 +0100320 asce = env->cregs[7];
321 break;
322 case PSW_ASC_HOME:
Blue Swirl71e47082012-09-02 07:33:30 +0000323 PTE_DPRINTF("%s: asc=home\n", __func__);
Alexander Grafd5a43962011-03-23 10:58:07 +0100324 asce = env->cregs[13];
325 break;
326 }
327
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
Blue Swirl71e47082012-09-02 07:33:30 +0000334 " 0xffe0000000000000ULL\n", __func__, vaddr);
Alexander Grafd5a43962011-03-23 10:58:07 +0100335 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
336 return -1;
337 }
338 break;
339 case _ASCE_TYPE_REGION3:
340 if (vaddr & 0xfffffc0000000000ULL) {
341 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
Blue Swirl71e47082012-09-02 07:33:30 +0000342 " 0xfffffc0000000000ULL\n", __func__, vaddr);
Alexander Grafd5a43962011-03-23 10:58:07 +0100343 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
344 return -1;
345 }
346 break;
347 case _ASCE_TYPE_SEGMENT:
348 if (vaddr & 0xffffffff80000000ULL) {
349 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
Blue Swirl71e47082012-09-02 07:33:30 +0000350 " 0xffffffff80000000ULL\n", __func__, vaddr);
Alexander Grafd5a43962011-03-23 10:58:07 +0100351 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw);
352 return -1;
353 }
354 break;
355 }
356
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);
361
362 r = mmu_translate_asce(env, vaddr, asc, asce, new_level, raddr, flags, rw);
363
364 if ((rw == 1) && !(*flags & PAGE_WRITE)) {
365 trigger_prot_fault(env, vaddr, asc);
366 return -1;
367 }
368
369 return r;
370}
371
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100372int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
Alexander Grafd5a43962011-03-23 10:58:07 +0100373 target_ulong *raddr, int *flags)
374{
375 int r = -1;
Alexander Grafb9959132011-07-14 11:49:08 +0200376 uint8_t *sk;
Alexander Grafd5a43962011-03-23 10:58:07 +0100377
378 *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
379 vaddr &= TARGET_PAGE_MASK;
380
381 if (!(env->psw.mask & PSW_MASK_DAT)) {
382 *raddr = vaddr;
383 r = 0;
384 goto out;
385 }
386
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:
393 /*
394 * Instruction: Primary
395 * Data: Secondary
396 */
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);
405 }
406 break;
407 case PSW_ASC_ACCREG:
408 default:
409 hw_error("guest switched to unknown asc mode\n");
410 break;
411 }
412
Blue Swirl71e47082012-09-02 07:33:30 +0000413 out:
Alexander Grafd5a43962011-03-23 10:58:07 +0100414 /* Convert real address -> absolute address */
Thomas Huth422f32c2013-09-03 13:31:36 +0200415 *raddr = mmu_real2abs(env, *raddr);
Alexander Grafd5a43962011-03-23 10:58:07 +0100416
Alexander Grafb9959132011-07-14 11:49:08 +0200417 if (*raddr <= ram_size) {
418 sk = &env->storage_keys[*raddr / TARGET_PAGE_SIZE];
419 if (*flags & PAGE_READ) {
420 *sk |= SK_R;
421 }
422
423 if (*flags & PAGE_WRITE) {
424 *sk |= SK_C;
425 }
426 }
427
Alexander Grafd5a43962011-03-23 10:58:07 +0100428 return r;
429}
430
Andreas Färber75104542013-08-26 03:01:33 +0200431int s390_cpu_handle_mmu_fault(CPUState *cs, vaddr orig_vaddr,
432 int rw, int mmu_idx)
Alexander Grafd5a43962011-03-23 10:58:07 +0100433{
Andreas Färber75104542013-08-26 03:01:33 +0200434 S390CPU *cpu = S390_CPU(cs);
435 CPUS390XState *env = &cpu->env;
Alexander Grafd5a43962011-03-23 10:58:07 +0100436 uint64_t asc = env->psw.mask & PSW_MASK_ASC;
437 target_ulong vaddr, raddr;
438 int prot;
439
Andreas Färber75104542013-08-26 03:01:33 +0200440 DPRINTF("%s: address 0x%" VADDR_PRIx " rw %d mmu_idx %d\n",
Andreas Färber07cc7d12013-01-27 03:32:03 +0000441 __func__, orig_vaddr, rw, mmu_idx);
Alexander Grafd5a43962011-03-23 10:58:07 +0100442
Blue Swirl71e47082012-09-02 07:33:30 +0000443 orig_vaddr &= TARGET_PAGE_MASK;
444 vaddr = orig_vaddr;
Alexander Grafd5a43962011-03-23 10:58:07 +0100445
446 /* 31-Bit mode */
447 if (!(env->psw.mask & PSW_MASK_64)) {
448 vaddr &= 0x7fffffff;
449 }
450
451 if (mmu_translate(env, vaddr, rw, asc, &raddr, &prot)) {
452 /* Translation ended in exception */
453 return 1;
454 }
455
456 /* check out of RAM access */
457 if (raddr > (ram_size + virtio_size)) {
Andreas Färbera6f921b2013-01-27 03:32:04 +0000458 DPRINTF("%s: raddr %" PRIx64 " > ram_size %" PRIx64 "\n", __func__,
459 (uint64_t)raddr, (uint64_t)ram_size);
Richard Hendersond5a103c2012-09-14 19:31:57 -0700460 trigger_pgm_exception(env, PGM_ADDRESSING, ILEN_LATER);
Alexander Grafd5a43962011-03-23 10:58:07 +0100461 return 1;
462 }
463
Blue Swirl71e47082012-09-02 07:33:30 +0000464 DPRINTF("%s: set tlb %" PRIx64 " -> %" PRIx64 " (%x)\n", __func__,
Alexander Grafd5a43962011-03-23 10:58:07 +0100465 (uint64_t)vaddr, (uint64_t)raddr, prot);
466
Andreas Färber0c591eb2013-09-03 13:59:37 +0200467 tlb_set_page(cs, orig_vaddr, raddr, prot,
Alexander Grafd5a43962011-03-23 10:58:07 +0100468 mmu_idx, TARGET_PAGE_SIZE);
469
470 return 0;
471}
472
Andreas Färber00b941e2013-06-29 18:55:54 +0200473hwaddr s390_cpu_get_phys_page_debug(CPUState *cs, vaddr vaddr)
Alexander Grafd5a43962011-03-23 10:58:07 +0100474{
Andreas Färber00b941e2013-06-29 18:55:54 +0200475 S390CPU *cpu = S390_CPU(cs);
476 CPUS390XState *env = &cpu->env;
Alexander Grafd5a43962011-03-23 10:58:07 +0100477 target_ulong raddr;
478 int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Andreas Färber27103422013-08-26 08:31:06 +0200479 int old_exc = cs->exception_index;
Alexander Grafd5a43962011-03-23 10:58:07 +0100480 uint64_t asc = env->psw.mask & PSW_MASK_ASC;
481
482 /* 31-Bit mode */
483 if (!(env->psw.mask & PSW_MASK_64)) {
484 vaddr &= 0x7fffffff;
485 }
486
487 mmu_translate(env, vaddr, 2, asc, &raddr, &prot);
Andreas Färber27103422013-08-26 08:31:06 +0200488 cs->exception_index = old_exc;
Alexander Grafd5a43962011-03-23 10:58:07 +0100489
490 return raddr;
491}
492
David Hildenbrand770a6372012-09-03 13:09:10 +0200493hwaddr s390_cpu_get_phys_addr_debug(CPUState *cs, vaddr vaddr)
494{
495 hwaddr phys_addr;
496 target_ulong page;
497
498 page = vaddr & TARGET_PAGE_MASK;
499 phys_addr = cpu_get_phys_page_debug(cs, page);
500 phys_addr += (vaddr & ~TARGET_PAGE_MASK);
501
502 return phys_addr;
503}
504
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100505void load_psw(CPUS390XState *env, uint64_t mask, uint64_t addr)
Alexander Grafd5a43962011-03-23 10:58:07 +0100506{
Alexander Grafd5a43962011-03-23 10:58:07 +0100507 env->psw.addr = addr;
508 env->psw.mask = mask;
Richard Henderson51855ec2012-09-24 12:06:15 -0700509 env->cc_op = (mask >> 44) & 3;
David Hildenbrandeb24f7c2014-09-30 10:57:29 +0200510
511 if (mask & PSW_MASK_WAIT) {
512 S390CPU *cpu = s390_env_get_cpu(env);
513 if (s390_cpu_halt(cpu) == 0) {
514#ifndef CONFIG_USER_ONLY
515 qemu_system_shutdown_request();
516#endif
517 }
518 }
Alexander Grafd5a43962011-03-23 10:58:07 +0100519}
520
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100521static uint64_t get_psw_mask(CPUS390XState *env)
Alexander Grafd5a43962011-03-23 10:58:07 +0100522{
Richard Henderson51855ec2012-09-24 12:06:15 -0700523 uint64_t r;
Alexander Grafd5a43962011-03-23 10:58:07 +0100524
525 env->cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
526
Richard Henderson51855ec2012-09-24 12:06:15 -0700527 r = env->psw.mask;
528 r &= ~PSW_MASK_CC;
Alexander Grafd5a43962011-03-23 10:58:07 +0100529 assert(!(env->cc_op & ~3));
Richard Henderson51855ec2012-09-24 12:06:15 -0700530 r |= (uint64_t)env->cc_op << 44;
Alexander Grafd5a43962011-03-23 10:58:07 +0100531
532 return r;
533}
534
Cornelia Huck4782a232013-01-24 02:28:01 +0000535static LowCore *cpu_map_lowcore(CPUS390XState *env)
536{
Andreas Färbera47dddd2013-09-03 17:38:47 +0200537 S390CPU *cpu = s390_env_get_cpu(env);
Cornelia Huck4782a232013-01-24 02:28:01 +0000538 LowCore *lowcore;
539 hwaddr len = sizeof(LowCore);
540
541 lowcore = cpu_physical_memory_map(env->psa, &len, 1);
542
543 if (len < sizeof(LowCore)) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200544 cpu_abort(CPU(cpu), "Could not map lowcore\n");
Cornelia Huck4782a232013-01-24 02:28:01 +0000545 }
546
547 return lowcore;
548}
549
550static void cpu_unmap_lowcore(LowCore *lowcore)
551{
552 cpu_physical_memory_unmap(lowcore, sizeof(LowCore), 1, sizeof(LowCore));
553}
554
Cornelia Huck38322ed2013-01-24 02:28:02 +0000555void *s390_cpu_physical_memory_map(CPUS390XState *env, hwaddr addr, hwaddr *len,
556 int is_write)
557{
558 hwaddr start = addr;
559
560 /* Mind the prefix area. */
561 if (addr < 8192) {
562 /* Map the lowcore. */
563 start += env->psa;
564 *len = MIN(*len, 8192 - addr);
565 } else if ((addr >= env->psa) && (addr < env->psa + 8192)) {
566 /* Map the 0 page. */
567 start -= env->psa;
568 *len = MIN(*len, 8192 - start);
569 }
570
571 return cpu_physical_memory_map(start, len, is_write);
572}
573
574void s390_cpu_physical_memory_unmap(CPUS390XState *env, void *addr, hwaddr len,
575 int is_write)
576{
577 cpu_physical_memory_unmap(addr, len, is_write, len);
578}
579
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100580static void do_svc_interrupt(CPUS390XState *env)
Alexander Grafd5a43962011-03-23 10:58:07 +0100581{
582 uint64_t mask, addr;
583 LowCore *lowcore;
Alexander Grafd5a43962011-03-23 10:58:07 +0100584
Cornelia Huck4782a232013-01-24 02:28:01 +0000585 lowcore = cpu_map_lowcore(env);
Alexander Grafd5a43962011-03-23 10:58:07 +0100586
587 lowcore->svc_code = cpu_to_be16(env->int_svc_code);
Richard Hendersond5a103c2012-09-14 19:31:57 -0700588 lowcore->svc_ilen = cpu_to_be16(env->int_svc_ilen);
Alexander Grafd5a43962011-03-23 10:58:07 +0100589 lowcore->svc_old_psw.mask = cpu_to_be64(get_psw_mask(env));
Richard Hendersond5a103c2012-09-14 19:31:57 -0700590 lowcore->svc_old_psw.addr = cpu_to_be64(env->psw.addr + env->int_svc_ilen);
Alexander Grafd5a43962011-03-23 10:58:07 +0100591 mask = be64_to_cpu(lowcore->svc_new_psw.mask);
592 addr = be64_to_cpu(lowcore->svc_new_psw.addr);
593
Cornelia Huck4782a232013-01-24 02:28:01 +0000594 cpu_unmap_lowcore(lowcore);
Alexander Grafd5a43962011-03-23 10:58:07 +0100595
596 load_psw(env, mask, addr);
597}
598
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100599static void do_program_interrupt(CPUS390XState *env)
Alexander Grafd5a43962011-03-23 10:58:07 +0100600{
601 uint64_t mask, addr;
602 LowCore *lowcore;
Richard Hendersond5a103c2012-09-14 19:31:57 -0700603 int ilen = env->int_pgm_ilen;
Alexander Grafd5a43962011-03-23 10:58:07 +0100604
Richard Hendersond5a103c2012-09-14 19:31:57 -0700605 switch (ilen) {
606 case ILEN_LATER:
607 ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
Alexander Grafd5a43962011-03-23 10:58:07 +0100608 break;
Richard Hendersond5a103c2012-09-14 19:31:57 -0700609 case ILEN_LATER_INC:
610 ilen = get_ilen(cpu_ldub_code(env, env->psw.addr));
611 env->psw.addr += ilen;
Alexander Grafd5a43962011-03-23 10:58:07 +0100612 break;
Richard Hendersond5a103c2012-09-14 19:31:57 -0700613 default:
614 assert(ilen == 2 || ilen == 4 || ilen == 6);
Alexander Grafd5a43962011-03-23 10:58:07 +0100615 }
616
Richard Hendersond5a103c2012-09-14 19:31:57 -0700617 qemu_log_mask(CPU_LOG_INT, "%s: code=0x%x ilen=%d\n",
618 __func__, env->int_pgm_code, ilen);
Alexander Grafd5a43962011-03-23 10:58:07 +0100619
Cornelia Huck4782a232013-01-24 02:28:01 +0000620 lowcore = cpu_map_lowcore(env);
Alexander Grafd5a43962011-03-23 10:58:07 +0100621
Richard Hendersond5a103c2012-09-14 19:31:57 -0700622 lowcore->pgm_ilen = cpu_to_be16(ilen);
Alexander Grafd5a43962011-03-23 10:58:07 +0100623 lowcore->pgm_code = cpu_to_be16(env->int_pgm_code);
624 lowcore->program_old_psw.mask = cpu_to_be64(get_psw_mask(env));
625 lowcore->program_old_psw.addr = cpu_to_be64(env->psw.addr);
626 mask = be64_to_cpu(lowcore->program_new_psw.mask);
627 addr = be64_to_cpu(lowcore->program_new_psw.addr);
628
Cornelia Huck4782a232013-01-24 02:28:01 +0000629 cpu_unmap_lowcore(lowcore);
Alexander Grafd5a43962011-03-23 10:58:07 +0100630
Blue Swirl71e47082012-09-02 07:33:30 +0000631 DPRINTF("%s: %x %x %" PRIx64 " %" PRIx64 "\n", __func__,
Richard Hendersond5a103c2012-09-14 19:31:57 -0700632 env->int_pgm_code, ilen, env->psw.mask,
Alexander Grafd5a43962011-03-23 10:58:07 +0100633 env->psw.addr);
634
635 load_psw(env, mask, addr);
636}
637
638#define VIRTIO_SUBCODE_64 0x0D00
639
Andreas Färbera4e3ad12012-03-14 01:38:22 +0100640static void do_ext_interrupt(CPUS390XState *env)
Alexander Grafd5a43962011-03-23 10:58:07 +0100641{
Andreas Färbera47dddd2013-09-03 17:38:47 +0200642 S390CPU *cpu = s390_env_get_cpu(env);
Alexander Grafd5a43962011-03-23 10:58:07 +0100643 uint64_t mask, addr;
644 LowCore *lowcore;
Alexander Grafd5a43962011-03-23 10:58:07 +0100645 ExtQueue *q;
646
647 if (!(env->psw.mask & PSW_MASK_EXT)) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200648 cpu_abort(CPU(cpu), "Ext int w/o ext mask\n");
Alexander Grafd5a43962011-03-23 10:58:07 +0100649 }
650
651 if (env->ext_index < 0 || env->ext_index > MAX_EXT_QUEUE) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200652 cpu_abort(CPU(cpu), "Ext queue overrun: %d\n", env->ext_index);
Alexander Grafd5a43962011-03-23 10:58:07 +0100653 }
654
655 q = &env->ext_queue[env->ext_index];
Cornelia Huck4782a232013-01-24 02:28:01 +0000656 lowcore = cpu_map_lowcore(env);
Alexander Grafd5a43962011-03-23 10:58:07 +0100657
658 lowcore->ext_int_code = cpu_to_be16(q->code);
659 lowcore->ext_params = cpu_to_be32(q->param);
660 lowcore->ext_params2 = cpu_to_be64(q->param64);
661 lowcore->external_old_psw.mask = cpu_to_be64(get_psw_mask(env));
662 lowcore->external_old_psw.addr = cpu_to_be64(env->psw.addr);
663 lowcore->cpu_addr = cpu_to_be16(env->cpu_num | VIRTIO_SUBCODE_64);
664 mask = be64_to_cpu(lowcore->external_new_psw.mask);
665 addr = be64_to_cpu(lowcore->external_new_psw.addr);
666
Cornelia Huck4782a232013-01-24 02:28:01 +0000667 cpu_unmap_lowcore(lowcore);
Alexander Grafd5a43962011-03-23 10:58:07 +0100668
669 env->ext_index--;
670 if (env->ext_index == -1) {
671 env->pending_int &= ~INTERRUPT_EXT;
672 }
673
Blue Swirl71e47082012-09-02 07:33:30 +0000674 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
Alexander Grafd5a43962011-03-23 10:58:07 +0100675 env->psw.mask, env->psw.addr);
676
677 load_psw(env, mask, addr);
678}
Alexander Graf3110e292011-04-15 17:32:48 +0200679
Cornelia Huck5d69c542013-01-24 02:28:04 +0000680static void do_io_interrupt(CPUS390XState *env)
681{
Andreas Färbera47dddd2013-09-03 17:38:47 +0200682 S390CPU *cpu = s390_env_get_cpu(env);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000683 LowCore *lowcore;
684 IOIntQueue *q;
685 uint8_t isc;
686 int disable = 1;
687 int found = 0;
688
689 if (!(env->psw.mask & PSW_MASK_IO)) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200690 cpu_abort(CPU(cpu), "I/O int w/o I/O mask\n");
Cornelia Huck5d69c542013-01-24 02:28:04 +0000691 }
692
693 for (isc = 0; isc < ARRAY_SIZE(env->io_index); isc++) {
Cornelia Huck91b0a8f2013-02-07 02:20:51 +0000694 uint64_t isc_bits;
695
Cornelia Huck5d69c542013-01-24 02:28:04 +0000696 if (env->io_index[isc] < 0) {
697 continue;
698 }
699 if (env->io_index[isc] > MAX_IO_QUEUE) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200700 cpu_abort(CPU(cpu), "I/O queue overrun for isc %d: %d\n",
Cornelia Huck5d69c542013-01-24 02:28:04 +0000701 isc, env->io_index[isc]);
702 }
703
704 q = &env->io_queue[env->io_index[isc]][isc];
Cornelia Huck91b0a8f2013-02-07 02:20:51 +0000705 isc_bits = ISC_TO_ISC_BITS(IO_INT_WORD_ISC(q->word));
706 if (!(env->cregs[6] & isc_bits)) {
Cornelia Huck5d69c542013-01-24 02:28:04 +0000707 disable = 0;
708 continue;
709 }
Cornelia Huckbd9a8d82013-02-07 02:20:50 +0000710 if (!found) {
711 uint64_t mask, addr;
Cornelia Huck5d69c542013-01-24 02:28:04 +0000712
Cornelia Huckbd9a8d82013-02-07 02:20:50 +0000713 found = 1;
714 lowcore = cpu_map_lowcore(env);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000715
Cornelia Huckbd9a8d82013-02-07 02:20:50 +0000716 lowcore->subchannel_id = cpu_to_be16(q->id);
717 lowcore->subchannel_nr = cpu_to_be16(q->nr);
718 lowcore->io_int_parm = cpu_to_be32(q->parm);
719 lowcore->io_int_word = cpu_to_be32(q->word);
720 lowcore->io_old_psw.mask = cpu_to_be64(get_psw_mask(env));
721 lowcore->io_old_psw.addr = cpu_to_be64(env->psw.addr);
722 mask = be64_to_cpu(lowcore->io_new_psw.mask);
723 addr = be64_to_cpu(lowcore->io_new_psw.addr);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000724
Cornelia Huckbd9a8d82013-02-07 02:20:50 +0000725 cpu_unmap_lowcore(lowcore);
726
727 env->io_index[isc]--;
728
729 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
730 env->psw.mask, env->psw.addr);
731 load_psw(env, mask, addr);
732 }
Stefan Weilb22dd122013-02-03 21:33:16 +0100733 if (env->io_index[isc] >= 0) {
Cornelia Huck5d69c542013-01-24 02:28:04 +0000734 disable = 0;
735 }
Cornelia Huckbd9a8d82013-02-07 02:20:50 +0000736 continue;
Cornelia Huck5d69c542013-01-24 02:28:04 +0000737 }
738
739 if (disable) {
740 env->pending_int &= ~INTERRUPT_IO;
741 }
742
Cornelia Huck5d69c542013-01-24 02:28:04 +0000743}
744
745static void do_mchk_interrupt(CPUS390XState *env)
746{
Andreas Färbera47dddd2013-09-03 17:38:47 +0200747 S390CPU *cpu = s390_env_get_cpu(env);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000748 uint64_t mask, addr;
749 LowCore *lowcore;
750 MchkQueue *q;
751 int i;
752
753 if (!(env->psw.mask & PSW_MASK_MCHECK)) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200754 cpu_abort(CPU(cpu), "Machine check w/o mchk mask\n");
Cornelia Huck5d69c542013-01-24 02:28:04 +0000755 }
756
757 if (env->mchk_index < 0 || env->mchk_index > MAX_MCHK_QUEUE) {
Andreas Färbera47dddd2013-09-03 17:38:47 +0200758 cpu_abort(CPU(cpu), "Mchk queue overrun: %d\n", env->mchk_index);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000759 }
760
761 q = &env->mchk_queue[env->mchk_index];
762
763 if (q->type != 1) {
764 /* Don't know how to handle this... */
Andreas Färbera47dddd2013-09-03 17:38:47 +0200765 cpu_abort(CPU(cpu), "Unknown machine check type %d\n", q->type);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000766 }
767 if (!(env->cregs[14] & (1 << 28))) {
768 /* CRW machine checks disabled */
769 return;
770 }
771
772 lowcore = cpu_map_lowcore(env);
773
774 for (i = 0; i < 16; i++) {
775 lowcore->floating_pt_save_area[i] = cpu_to_be64(env->fregs[i].ll);
776 lowcore->gpregs_save_area[i] = cpu_to_be64(env->regs[i]);
777 lowcore->access_regs_save_area[i] = cpu_to_be32(env->aregs[i]);
778 lowcore->cregs_save_area[i] = cpu_to_be64(env->cregs[i]);
779 }
780 lowcore->prefixreg_save_area = cpu_to_be32(env->psa);
781 lowcore->fpt_creg_save_area = cpu_to_be32(env->fpc);
782 lowcore->tod_progreg_save_area = cpu_to_be32(env->todpr);
783 lowcore->cpu_timer_save_area[0] = cpu_to_be32(env->cputm >> 32);
784 lowcore->cpu_timer_save_area[1] = cpu_to_be32((uint32_t)env->cputm);
785 lowcore->clock_comp_save_area[0] = cpu_to_be32(env->ckc >> 32);
786 lowcore->clock_comp_save_area[1] = cpu_to_be32((uint32_t)env->ckc);
787
788 lowcore->mcck_interruption_code[0] = cpu_to_be32(0x00400f1d);
789 lowcore->mcck_interruption_code[1] = cpu_to_be32(0x40330000);
790 lowcore->mcck_old_psw.mask = cpu_to_be64(get_psw_mask(env));
791 lowcore->mcck_old_psw.addr = cpu_to_be64(env->psw.addr);
792 mask = be64_to_cpu(lowcore->mcck_new_psw.mask);
793 addr = be64_to_cpu(lowcore->mcck_new_psw.addr);
794
795 cpu_unmap_lowcore(lowcore);
796
797 env->mchk_index--;
798 if (env->mchk_index == -1) {
799 env->pending_int &= ~INTERRUPT_MCHK;
800 }
801
802 DPRINTF("%s: %" PRIx64 " %" PRIx64 "\n", __func__,
803 env->psw.mask, env->psw.addr);
804
805 load_psw(env, mask, addr);
806}
807
Andreas Färber97a8ea52013-02-02 10:57:51 +0100808void s390_cpu_do_interrupt(CPUState *cs)
Alexander Graf3110e292011-04-15 17:32:48 +0200809{
Andreas Färber97a8ea52013-02-02 10:57:51 +0100810 S390CPU *cpu = S390_CPU(cs);
811 CPUS390XState *env = &cpu->env;
Andreas Färberf9466732013-01-30 12:48:24 +0000812
Richard Henderson0d404542012-09-24 14:55:51 -0700813 qemu_log_mask(CPU_LOG_INT, "%s: %d at pc=%" PRIx64 "\n",
Andreas Färber27103422013-08-26 08:31:06 +0200814 __func__, cs->exception_index, env->psw.addr);
Alexander Grafd5a43962011-03-23 10:58:07 +0100815
David Hildenbrandeb24f7c2014-09-30 10:57:29 +0200816 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
Cornelia Huck5d69c542013-01-24 02:28:04 +0000817 /* handle machine checks */
818 if ((env->psw.mask & PSW_MASK_MCHECK) &&
Andreas Färber27103422013-08-26 08:31:06 +0200819 (cs->exception_index == -1)) {
Cornelia Huck5d69c542013-01-24 02:28:04 +0000820 if (env->pending_int & INTERRUPT_MCHK) {
Andreas Färber27103422013-08-26 08:31:06 +0200821 cs->exception_index = EXCP_MCHK;
Cornelia Huck5d69c542013-01-24 02:28:04 +0000822 }
823 }
Alexander Grafd5a43962011-03-23 10:58:07 +0100824 /* handle external interrupts */
825 if ((env->psw.mask & PSW_MASK_EXT) &&
Andreas Färber27103422013-08-26 08:31:06 +0200826 cs->exception_index == -1) {
Alexander Grafd5a43962011-03-23 10:58:07 +0100827 if (env->pending_int & INTERRUPT_EXT) {
828 /* code is already in env */
Andreas Färber27103422013-08-26 08:31:06 +0200829 cs->exception_index = EXCP_EXT;
Alexander Grafd5a43962011-03-23 10:58:07 +0100830 } else if (env->pending_int & INTERRUPT_TOD) {
Andreas Färberf9466732013-01-30 12:48:24 +0000831 cpu_inject_ext(cpu, 0x1004, 0, 0);
Andreas Färber27103422013-08-26 08:31:06 +0200832 cs->exception_index = EXCP_EXT;
Alexander Grafd5a43962011-03-23 10:58:07 +0100833 env->pending_int &= ~INTERRUPT_EXT;
834 env->pending_int &= ~INTERRUPT_TOD;
835 } else if (env->pending_int & INTERRUPT_CPUTIMER) {
Andreas Färberf9466732013-01-30 12:48:24 +0000836 cpu_inject_ext(cpu, 0x1005, 0, 0);
Andreas Färber27103422013-08-26 08:31:06 +0200837 cs->exception_index = EXCP_EXT;
Alexander Grafd5a43962011-03-23 10:58:07 +0100838 env->pending_int &= ~INTERRUPT_EXT;
839 env->pending_int &= ~INTERRUPT_TOD;
840 }
841 }
Cornelia Huck5d69c542013-01-24 02:28:04 +0000842 /* handle I/O interrupts */
843 if ((env->psw.mask & PSW_MASK_IO) &&
Andreas Färber27103422013-08-26 08:31:06 +0200844 (cs->exception_index == -1)) {
Cornelia Huck5d69c542013-01-24 02:28:04 +0000845 if (env->pending_int & INTERRUPT_IO) {
Andreas Färber27103422013-08-26 08:31:06 +0200846 cs->exception_index = EXCP_IO;
Cornelia Huck5d69c542013-01-24 02:28:04 +0000847 }
848 }
Alexander Grafd5a43962011-03-23 10:58:07 +0100849
Andreas Färber27103422013-08-26 08:31:06 +0200850 switch (cs->exception_index) {
Alexander Grafd5a43962011-03-23 10:58:07 +0100851 case EXCP_PGM:
852 do_program_interrupt(env);
853 break;
854 case EXCP_SVC:
855 do_svc_interrupt(env);
856 break;
857 case EXCP_EXT:
858 do_ext_interrupt(env);
859 break;
Cornelia Huck5d69c542013-01-24 02:28:04 +0000860 case EXCP_IO:
861 do_io_interrupt(env);
862 break;
863 case EXCP_MCHK:
864 do_mchk_interrupt(env);
865 break;
Alexander Grafd5a43962011-03-23 10:58:07 +0100866 }
Andreas Färber27103422013-08-26 08:31:06 +0200867 cs->exception_index = -1;
Alexander Grafd5a43962011-03-23 10:58:07 +0100868
869 if (!env->pending_int) {
Andreas Färber259186a2013-01-17 18:51:17 +0100870 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
Alexander Grafd5a43962011-03-23 10:58:07 +0100871 }
Alexander Graf3110e292011-04-15 17:32:48 +0200872}
Alexander Grafd5a43962011-03-23 10:58:07 +0100873
Richard Henderson02bb9bb2014-09-13 09:45:19 -0700874bool s390_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
875{
876 if (interrupt_request & CPU_INTERRUPT_HARD) {
877 S390CPU *cpu = S390_CPU(cs);
878 CPUS390XState *env = &cpu->env;
879
880 if (env->psw.mask & PSW_MASK_EXT) {
881 s390_cpu_do_interrupt(cs);
882 return true;
883 }
884 }
885 return false;
886}
Alexander Grafd5a43962011-03-23 10:58:07 +0100887#endif /* CONFIG_USER_ONLY */