xref: /openbmc/qemu/target/s390x/tcg/misc_helper.c (revision dd1df0fa0cfe90201d451eb9d89403b221afe02f)
1 /*
2  *  S/390 misc helper routines
3  *
4  *  Copyright (c) 2009 Ulrich Hecht
5  *  Copyright (c) 2009 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.1 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 "qemu/osdep.h"
22 #include "qemu/cutils.h"
23 #include "qemu/log.h"
24 #include "cpu.h"
25 #include "s390x-internal.h"
26 #include "qemu/host-utils.h"
27 #include "exec/helper-proto.h"
28 #include "qemu/timer.h"
29 #include "exec/cputlb.h"
30 #include "accel/tcg/cpu-ldst.h"
31 #include "exec/target_page.h"
32 #include "qapi/error.h"
33 #include "tcg_s390x.h"
34 #include "s390-tod.h"
35 
36 #if !defined(CONFIG_USER_ONLY)
37 #include "system/cpus.h"
38 #include "system/system.h"
39 #include "hw/s390x/ebcdic.h"
40 #include "hw/s390x/s390-hypercall.h"
41 #include "hw/s390x/sclp.h"
42 #include "hw/s390x/s390_flic.h"
43 #include "hw/s390x/ioinst.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/boards.h"
46 #include "hw/s390x/tod.h"
47 #include CONFIG_DEVICES
48 #endif
49 
50 /* #define DEBUG_HELPER */
51 #ifdef DEBUG_HELPER
52 #define HELPER_LOG(x...) qemu_log(x)
53 #else
54 #define HELPER_LOG(x...)
55 #endif
56 
57 /* Raise an exception statically from a TB.  */
HELPER(exception)58 void HELPER(exception)(CPUS390XState *env, uint32_t excp)
59 {
60     CPUState *cs = env_cpu(env);
61 
62     HELPER_LOG("%s: exception %d\n", __func__, excp);
63     cs->exception_index = excp;
64     cpu_loop_exit(cs);
65 }
66 
67 /* Store CPU Timer (also used for EXTRACT CPU TIME) */
HELPER(stpt)68 uint64_t HELPER(stpt)(CPUS390XState *env)
69 {
70 #if defined(CONFIG_USER_ONLY)
71     /*
72      * Fake a descending CPU timer. We could get negative values here,
73      * but we don't care as it is up to the OS when to process that
74      * interrupt and reset to > 0.
75      */
76     return UINT64_MAX - (uint64_t)cpu_get_host_ticks();
77 #else
78     return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
79 #endif
80 }
81 
82 /* Store Clock */
HELPER(stck)83 uint64_t HELPER(stck)(CPUS390XState *env)
84 {
85 #ifdef CONFIG_USER_ONLY
86     struct timespec ts;
87     uint64_t ns;
88 
89     clock_gettime(CLOCK_REALTIME, &ts);
90     ns = ts.tv_sec * NANOSECONDS_PER_SECOND + ts.tv_nsec;
91 
92     return TOD_UNIX_EPOCH + time2tod(ns);
93 #else
94     S390TODState *td = s390_get_todstate();
95     S390TODClass *tdc = S390_TOD_GET_CLASS(td);
96     S390TOD tod;
97 
98     tdc->get(td, &tod, &error_abort);
99     return tod.low;
100 #endif
101 }
102 
103 #ifndef CONFIG_USER_ONLY
104 /* SCLP service call */
HELPER(servc)105 uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
106 {
107     bql_lock();
108     int r = sclp_service_call(env_archcpu(env), r1, r2);
109     bql_unlock();
110     if (r < 0) {
111         tcg_s390_program_interrupt(env, -r, GETPC());
112     }
113     return r;
114 }
115 
HELPER(diag)116 void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)
117 {
118     uint64_t r;
119 
120     switch (num) {
121 #ifdef CONFIG_S390_CCW_VIRTIO
122     case 0x500:
123         /* QEMU/KVM hypercall */
124         bql_lock();
125         handle_diag_500(env_archcpu(env), GETPC());
126         bql_unlock();
127         r = 0;
128         break;
129 #endif /* CONFIG_S390_CCW_VIRTIO */
130     case 0x44:
131         /* yield */
132         r = 0;
133         break;
134     case 0x308:
135         /* ipl */
136         bql_lock();
137         handle_diag_308(env, r1, r3, GETPC());
138         bql_unlock();
139         r = 0;
140         break;
141     case 0x288:
142         /* time bomb (watchdog) */
143         r = handle_diag_288(env, r1, r3);
144         break;
145     default:
146         r = -1;
147         break;
148     }
149 
150     if (r) {
151         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
152     }
153 }
154 
155 /* Set Prefix */
HELPER(spx)156 void HELPER(spx)(CPUS390XState *env, uint64_t a1)
157 {
158     const uint32_t prefix = a1 & 0x7fffe000;
159     const uint32_t old_prefix = env->psa;
160     CPUState *cs = env_cpu(env);
161 
162     if (prefix == old_prefix) {
163         return;
164     }
165     /*
166      * Since prefix got aligned to 8k and memory increments are a multiple of
167      * 8k checking the first page is sufficient
168      */
169     if (!mmu_absolute_addr_valid(prefix, true)) {
170         tcg_s390_program_interrupt(env, PGM_ADDRESSING, GETPC());
171     }
172 
173     env->psa = prefix;
174     HELPER_LOG("prefix: %#x\n", prefix);
175     tlb_flush_page(cs, 0);
176     tlb_flush_page(cs, TARGET_PAGE_SIZE);
177     if (prefix != 0) {
178         tlb_flush_page(cs, prefix);
179         tlb_flush_page(cs, prefix + TARGET_PAGE_SIZE);
180     }
181     if (old_prefix != 0) {
182         tlb_flush_page(cs, old_prefix);
183         tlb_flush_page(cs, old_prefix + TARGET_PAGE_SIZE);
184     }
185 }
186 
update_ckc_timer(CPUS390XState * env)187 static void update_ckc_timer(CPUS390XState *env)
188 {
189     S390TODState *td = s390_get_todstate();
190     uint64_t time;
191 
192     /* stop the timer and remove pending CKC IRQs */
193     timer_del(env->tod_timer);
194     g_assert(bql_locked());
195     env->pending_int &= ~INTERRUPT_EXT_CLOCK_COMPARATOR;
196 
197     /* the tod has to exceed the ckc, this can never happen if ckc is all 1's */
198     if (env->ckc == -1ULL) {
199         return;
200     }
201 
202     if (env->ckc < td->base.low) {
203         time = 0;
204     } else {
205         /* difference between origins */
206         time = env->ckc - td->base.low;
207 
208         /* nanoseconds */
209         time = tod2time(time);
210     }
211 
212     timer_mod(env->tod_timer, time);
213 }
214 
215 /* Set Clock Comparator */
HELPER(sckc)216 void HELPER(sckc)(CPUS390XState *env, uint64_t ckc)
217 {
218     env->ckc = ckc;
219 
220     bql_lock();
221     update_ckc_timer(env);
222     bql_unlock();
223 }
224 
tcg_s390_tod_updated(CPUState * cs,run_on_cpu_data opaque)225 void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque)
226 {
227     update_ckc_timer(cpu_env(cs));
228 }
229 
230 /* Set Clock */
HELPER(sck)231 uint32_t HELPER(sck)(CPUS390XState *env, uint64_t tod_low)
232 {
233     S390TODState *td = s390_get_todstate();
234     S390TODClass *tdc = S390_TOD_GET_CLASS(td);
235     S390TOD tod = {
236         .high = 0,
237         .low = tod_low,
238     };
239 
240     bql_lock();
241     tdc->set(td, &tod, &error_abort);
242     bql_unlock();
243     return 0;
244 }
245 
246 /* Set Tod Programmable Field */
HELPER(sckpf)247 void HELPER(sckpf)(CPUS390XState *env, uint64_t r0)
248 {
249     uint32_t val = r0;
250 
251     if (val & 0xffff0000) {
252         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
253     }
254     env->todpr = val;
255 }
256 
257 /* Store Clock Comparator */
HELPER(stckc)258 uint64_t HELPER(stckc)(CPUS390XState *env)
259 {
260     return env->ckc;
261 }
262 
263 /* Set CPU Timer */
HELPER(spt)264 void HELPER(spt)(CPUS390XState *env, uint64_t time)
265 {
266     if (time == -1ULL) {
267         return;
268     }
269 
270     /* nanoseconds */
271     time = tod2time(time);
272 
273     env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
274 
275     timer_mod(env->cpu_timer, env->cputm);
276 }
277 
278 /* Store System Information */
HELPER(stsi)279 uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, uint64_t r0, uint64_t r1)
280 {
281     const uintptr_t ra = GETPC();
282     const uint32_t sel1 = r0 & STSI_R0_SEL1_MASK;
283     const uint32_t sel2 = r1 & STSI_R1_SEL2_MASK;
284     const MachineState *ms = MACHINE(qdev_get_machine());
285     uint16_t total_cpus = 0, conf_cpus = 0, reserved_cpus = 0;
286     S390CPU *cpu = env_archcpu(env);
287     SysIB sysib = { };
288     int i, cc = 0;
289 
290     if ((r0 & STSI_R0_FC_MASK) > STSI_R0_FC_LEVEL_3) {
291         /* invalid function code: no other checks are performed */
292         return 3;
293     }
294 
295     if ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK)) {
296         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
297     }
298 
299     if ((r0 & STSI_R0_FC_MASK) == STSI_R0_FC_CURRENT) {
300         /* query the current level: no further checks are performed */
301         env->regs[0] = STSI_R0_FC_LEVEL_3;
302         return 0;
303     }
304 
305     if (a0 & ~TARGET_PAGE_MASK) {
306         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
307     }
308 
309     /* count the cpus and split them into configured and reserved ones */
310     for (i = 0; i < ms->possible_cpus->len; i++) {
311         total_cpus++;
312         if (ms->possible_cpus->cpus[i].cpu) {
313             conf_cpus++;
314         } else {
315             reserved_cpus++;
316         }
317     }
318 
319     /*
320      * In theory, we could report Level 1 / Level 2 as current. However,
321      * the Linux kernel will detect this as running under LPAR and assume
322      * that we have a sclp linemode console (which is always present on
323      * LPAR, but not the default for QEMU), therefore not displaying boot
324      * messages and making booting a Linux kernel under TCG harder.
325      *
326      * For now we fake the same SMP configuration on all levels.
327      *
328      * TODO: We could later make the level configurable via the machine
329      *       and change defaults (linemode console) based on machine type
330      *       and accelerator.
331      */
332     switch (r0 & STSI_R0_FC_MASK) {
333     case STSI_R0_FC_LEVEL_1:
334         if ((sel1 == 1) && (sel2 == 1)) {
335             /* Basic Machine Configuration */
336             char type[5] = {};
337 
338             ebcdic_put(sysib.sysib_111.manuf, "QEMU            ", 16);
339             /* same as machine type number in STORE CPU ID, but in EBCDIC */
340             snprintf(type, ARRAY_SIZE(type), "%X", cpu->model->def->type);
341             ebcdic_put(sysib.sysib_111.type, type, 4);
342             /* model number (not stored in STORE CPU ID for z/Architecture) */
343             ebcdic_put(sysib.sysib_111.model, "QEMU            ", 16);
344             ebcdic_put(sysib.sysib_111.sequence, "QEMU            ", 16);
345             ebcdic_put(sysib.sysib_111.plant, "QEMU", 4);
346         } else if ((sel1 == 2) && (sel2 == 1)) {
347             /* Basic Machine CPU */
348             ebcdic_put(sysib.sysib_121.sequence, "QEMUQEMUQEMUQEMU", 16);
349             ebcdic_put(sysib.sysib_121.plant, "QEMU", 4);
350             sysib.sysib_121.cpu_addr = cpu_to_be16(env->core_id);
351         } else if ((sel1 == 2) && (sel2 == 2)) {
352             /* Basic Machine CPUs */
353             sysib.sysib_122.capability = cpu_to_be32(0x443afc29);
354             sysib.sysib_122.total_cpus = cpu_to_be16(total_cpus);
355             sysib.sysib_122.conf_cpus = cpu_to_be16(conf_cpus);
356             sysib.sysib_122.reserved_cpus = cpu_to_be16(reserved_cpus);
357         } else {
358             cc = 3;
359         }
360         break;
361     case STSI_R0_FC_LEVEL_2:
362         if ((sel1 == 2) && (sel2 == 1)) {
363             /* LPAR CPU */
364             ebcdic_put(sysib.sysib_221.sequence, "QEMUQEMUQEMUQEMU", 16);
365             ebcdic_put(sysib.sysib_221.plant, "QEMU", 4);
366             sysib.sysib_221.cpu_addr = cpu_to_be16(env->core_id);
367         } else if ((sel1 == 2) && (sel2 == 2)) {
368             /* LPAR CPUs */
369             sysib.sysib_222.lcpuc = 0x80; /* dedicated */
370             sysib.sysib_222.total_cpus = cpu_to_be16(total_cpus);
371             sysib.sysib_222.conf_cpus = cpu_to_be16(conf_cpus);
372             sysib.sysib_222.reserved_cpus = cpu_to_be16(reserved_cpus);
373             ebcdic_put(sysib.sysib_222.name, "QEMU    ", 8);
374             sysib.sysib_222.caf = cpu_to_be32(1000);
375             sysib.sysib_222.dedicated_cpus = cpu_to_be16(conf_cpus);
376         } else {
377             cc = 3;
378         }
379         break;
380     case STSI_R0_FC_LEVEL_3:
381         if ((sel1 == 2) && (sel2 == 2)) {
382             /* VM CPUs */
383             sysib.sysib_322.count = 1;
384             sysib.sysib_322.vm[0].total_cpus = cpu_to_be16(total_cpus);
385             sysib.sysib_322.vm[0].conf_cpus = cpu_to_be16(conf_cpus);
386             sysib.sysib_322.vm[0].reserved_cpus = cpu_to_be16(reserved_cpus);
387             sysib.sysib_322.vm[0].caf = cpu_to_be32(1000);
388             /* Linux kernel uses this to distinguish us from z/VM */
389             ebcdic_put(sysib.sysib_322.vm[0].cpi, "KVM/Linux       ", 16);
390             sysib.sysib_322.vm[0].ext_name_encoding = 2; /* UTF-8 */
391 
392             /* If our VM has a name, use the real name */
393             if (qemu_name) {
394                 memset(sysib.sysib_322.vm[0].name, 0x40,
395                        sizeof(sysib.sysib_322.vm[0].name));
396                 ebcdic_put(sysib.sysib_322.vm[0].name, qemu_name,
397                            MIN(sizeof(sysib.sysib_322.vm[0].name),
398                                strlen(qemu_name)));
399                 strpadcpy((char *)sysib.sysib_322.ext_names[0],
400                           sizeof(sysib.sysib_322.ext_names[0]),
401                           qemu_name, '\0');
402 
403             } else {
404                 ebcdic_put(sysib.sysib_322.vm[0].name, "TCGguest", 8);
405                 strcpy((char *)sysib.sysib_322.ext_names[0], "TCGguest");
406             }
407 
408             /* add the uuid */
409             memcpy(sysib.sysib_322.vm[0].uuid, &qemu_uuid,
410                    sizeof(sysib.sysib_322.vm[0].uuid));
411         } else {
412             cc = 3;
413         }
414         break;
415     }
416 
417     if (cc == 0) {
418         if (s390_cpu_virt_mem_write(cpu, a0, 0, &sysib, sizeof(sysib))) {
419             s390_cpu_virt_mem_handle_exc(cpu, ra);
420         }
421     }
422 
423     return cc;
424 }
425 
HELPER(sigp)426 uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
427                       uint32_t r3)
428 {
429     int cc;
430 
431     /* TODO: needed to inject interrupts  - push further down */
432     bql_lock();
433     cc = handle_sigp(env, order_code & SIGP_ORDER_MASK, r1, r3);
434     bql_unlock();
435 
436     return cc;
437 }
438 #endif
439 
440 #ifndef CONFIG_USER_ONLY
HELPER(xsch)441 void HELPER(xsch)(CPUS390XState *env, uint64_t r1)
442 {
443     S390CPU *cpu = env_archcpu(env);
444     bql_lock();
445     ioinst_handle_xsch(cpu, r1, GETPC());
446     bql_unlock();
447 }
448 
HELPER(csch)449 void HELPER(csch)(CPUS390XState *env, uint64_t r1)
450 {
451     S390CPU *cpu = env_archcpu(env);
452     bql_lock();
453     ioinst_handle_csch(cpu, r1, GETPC());
454     bql_unlock();
455 }
456 
HELPER(hsch)457 void HELPER(hsch)(CPUS390XState *env, uint64_t r1)
458 {
459     S390CPU *cpu = env_archcpu(env);
460     bql_lock();
461     ioinst_handle_hsch(cpu, r1, GETPC());
462     bql_unlock();
463 }
464 
HELPER(msch)465 void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
466 {
467     S390CPU *cpu = env_archcpu(env);
468     bql_lock();
469     ioinst_handle_msch(cpu, r1, inst >> 16, GETPC());
470     bql_unlock();
471 }
472 
HELPER(rchp)473 void HELPER(rchp)(CPUS390XState *env, uint64_t r1)
474 {
475     S390CPU *cpu = env_archcpu(env);
476     bql_lock();
477     ioinst_handle_rchp(cpu, r1, GETPC());
478     bql_unlock();
479 }
480 
HELPER(rsch)481 void HELPER(rsch)(CPUS390XState *env, uint64_t r1)
482 {
483     S390CPU *cpu = env_archcpu(env);
484     bql_lock();
485     ioinst_handle_rsch(cpu, r1, GETPC());
486     bql_unlock();
487 }
488 
HELPER(sal)489 void HELPER(sal)(CPUS390XState *env, uint64_t r1)
490 {
491     S390CPU *cpu = env_archcpu(env);
492 
493     bql_lock();
494     ioinst_handle_sal(cpu, r1, GETPC());
495     bql_unlock();
496 }
497 
HELPER(schm)498 void HELPER(schm)(CPUS390XState *env, uint64_t r1, uint64_t r2, uint64_t inst)
499 {
500     S390CPU *cpu = env_archcpu(env);
501 
502     bql_lock();
503     ioinst_handle_schm(cpu, r1, r2, inst >> 16, GETPC());
504     bql_unlock();
505 }
506 
HELPER(ssch)507 void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
508 {
509     S390CPU *cpu = env_archcpu(env);
510     bql_lock();
511     ioinst_handle_ssch(cpu, r1, inst >> 16, GETPC());
512     bql_unlock();
513 }
514 
HELPER(stcrw)515 void HELPER(stcrw)(CPUS390XState *env, uint64_t inst)
516 {
517     S390CPU *cpu = env_archcpu(env);
518 
519     bql_lock();
520     ioinst_handle_stcrw(cpu, inst >> 16, GETPC());
521     bql_unlock();
522 }
523 
HELPER(stsch)524 void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
525 {
526     S390CPU *cpu = env_archcpu(env);
527     bql_lock();
528     ioinst_handle_stsch(cpu, r1, inst >> 16, GETPC());
529     bql_unlock();
530 }
531 
HELPER(tpi)532 uint32_t HELPER(tpi)(CPUS390XState *env, uint64_t addr)
533 {
534     const uintptr_t ra = GETPC();
535     S390CPU *cpu = env_archcpu(env);
536     QEMUS390FLICState *flic = s390_get_qemu_flic(s390_get_flic());
537     QEMUS390FlicIO *io = NULL;
538     LowCore *lowcore;
539 
540     if (addr & 0x3) {
541         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
542     }
543 
544     bql_lock();
545     io = qemu_s390_flic_dequeue_io(flic, env->cregs[6]);
546     if (!io) {
547         bql_unlock();
548         return 0;
549     }
550 
551     if (addr) {
552         struct {
553             uint16_t id;
554             uint16_t nr;
555             uint32_t parm;
556         } intc = {
557             .id = cpu_to_be16(io->id),
558             .nr = cpu_to_be16(io->nr),
559             .parm = cpu_to_be32(io->parm),
560         };
561 
562         if (s390_cpu_virt_mem_write(cpu, addr, 0, &intc, sizeof(intc))) {
563             /* writing failed, reinject and properly clean up */
564             s390_io_interrupt(io->id, io->nr, io->parm, io->word);
565             bql_unlock();
566             g_free(io);
567             s390_cpu_virt_mem_handle_exc(cpu, ra);
568             return 0;
569         }
570     } else {
571         /* no protection applies */
572         lowcore = cpu_map_lowcore(env);
573         lowcore->subchannel_id = cpu_to_be16(io->id);
574         lowcore->subchannel_nr = cpu_to_be16(io->nr);
575         lowcore->io_int_parm = cpu_to_be32(io->parm);
576         lowcore->io_int_word = cpu_to_be32(io->word);
577         cpu_unmap_lowcore(lowcore);
578     }
579 
580     g_free(io);
581     bql_unlock();
582     return 1;
583 }
584 
HELPER(tsch)585 void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
586 {
587     S390CPU *cpu = env_archcpu(env);
588     bql_lock();
589     ioinst_handle_tsch(cpu, r1, inst >> 16, GETPC());
590     bql_unlock();
591 }
592 
HELPER(chsc)593 void HELPER(chsc)(CPUS390XState *env, uint64_t inst)
594 {
595     S390CPU *cpu = env_archcpu(env);
596     bql_lock();
597     ioinst_handle_chsc(cpu, inst >> 16, GETPC());
598     bql_unlock();
599 }
600 #endif
601 
602 #ifndef CONFIG_USER_ONLY
per_raise_exception(CPUS390XState * env)603 static G_NORETURN void per_raise_exception(CPUS390XState *env)
604 {
605     trigger_pgm_exception(env, PGM_PER);
606     cpu_loop_exit(env_cpu(env));
607 }
608 
per_raise_exception_log(CPUS390XState * env)609 static G_NORETURN void per_raise_exception_log(CPUS390XState *env)
610 {
611     qemu_log_mask(CPU_LOG_INT, "PER interrupt after 0x%" PRIx64 "\n",
612                   env->per_address);
613     per_raise_exception(env);
614 }
615 
HELPER(per_check_exception)616 void HELPER(per_check_exception)(CPUS390XState *env)
617 {
618     /* psw_addr, per_address and int_pgm_ilen are already set. */
619     if (unlikely(env->per_perc_atmid)) {
620         per_raise_exception_log(env);
621     }
622 }
623 
624 /* Check if an address is within the PER starting address and the PER
625    ending address.  The address range might loop.  */
get_per_in_range(CPUS390XState * env,uint64_t addr)626 static inline bool get_per_in_range(CPUS390XState *env, uint64_t addr)
627 {
628     if (env->cregs[10] <= env->cregs[11]) {
629         return env->cregs[10] <= addr && addr <= env->cregs[11];
630     } else {
631         return env->cregs[10] <= addr || addr <= env->cregs[11];
632     }
633 }
634 
HELPER(per_branch)635 void HELPER(per_branch)(CPUS390XState *env, uint64_t dest, uint32_t ilen)
636 {
637     if ((env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS)
638         && !get_per_in_range(env, dest)) {
639         return;
640     }
641 
642     env->psw.addr = dest;
643     env->int_pgm_ilen = ilen;
644     env->per_address = env->gbea;
645     env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env);
646     per_raise_exception_log(env);
647 }
648 
HELPER(per_ifetch)649 void HELPER(per_ifetch)(CPUS390XState *env, uint32_t ilen)
650 {
651     if (get_per_in_range(env, env->psw.addr)) {
652         env->per_address = env->psw.addr;
653         env->int_pgm_ilen = ilen;
654         env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env);
655 
656         /* If the instruction has to be nullified, trigger the
657            exception immediately. */
658         if (env->cregs[9] & PER_CR9_EVENT_IFETCH_NULLIFICATION) {
659             env->per_perc_atmid |= PER_CODE_EVENT_NULLIFICATION;
660             qemu_log_mask(CPU_LOG_INT, "PER interrupt before 0x%" PRIx64 "\n",
661                           env->per_address);
662             per_raise_exception(env);
663         }
664     }
665 }
666 
HELPER(per_store_real)667 void HELPER(per_store_real)(CPUS390XState *env, uint32_t ilen)
668 {
669     /* PSW is saved just before calling the helper.  */
670     env->per_address = env->psw.addr;
671     env->int_pgm_ilen = ilen;
672     env->per_perc_atmid = PER_CODE_EVENT_STORE_REAL | get_per_atmid(env);
673     per_raise_exception_log(env);
674 }
675 #endif
676 
677 static uint8_t stfl_bytes[2048];
678 static unsigned int used_stfl_bytes;
679 
prepare_stfl(void)680 static void prepare_stfl(void)
681 {
682     static bool initialized;
683     int i;
684 
685     /* racy, but we don't care, the same values are always written */
686     if (initialized) {
687         return;
688     }
689 
690     s390_get_feat_block(S390_FEAT_TYPE_STFL, stfl_bytes);
691     for (i = 0; i < sizeof(stfl_bytes); i++) {
692         if (stfl_bytes[i]) {
693             used_stfl_bytes = i + 1;
694         }
695     }
696     initialized = true;
697 }
698 
699 #ifndef CONFIG_USER_ONLY
HELPER(stfl)700 void HELPER(stfl)(CPUS390XState *env)
701 {
702     LowCore *lowcore;
703 
704     lowcore = cpu_map_lowcore(env);
705     prepare_stfl();
706     memcpy(&lowcore->stfl_fac_list, stfl_bytes, sizeof(lowcore->stfl_fac_list));
707     cpu_unmap_lowcore(lowcore);
708 }
709 #endif
710 
HELPER(stfle)711 uint32_t HELPER(stfle)(CPUS390XState *env, uint64_t addr)
712 {
713     const uintptr_t ra = GETPC();
714     const int count_bytes = ((env->regs[0] & 0xff) + 1) * 8;
715     int max_bytes;
716     int i;
717 
718     if (addr & 0x7) {
719         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
720     }
721 
722     prepare_stfl();
723     max_bytes = ROUND_UP(used_stfl_bytes, 8);
724 
725     /*
726      * The PoP says that doublewords beyond the highest-numbered facility
727      * bit may or may not be stored.  However, existing hardware appears to
728      * not store the words, and existing software depend on that.
729      */
730     for (i = 0; i < MIN(count_bytes, max_bytes); ++i) {
731         cpu_stb_data_ra(env, addr + i, stfl_bytes[i], ra);
732     }
733 
734     env->regs[0] = deposit64(env->regs[0], 0, 8, (max_bytes / 8) - 1);
735     return count_bytes >= max_bytes ? 0 : 3;
736 }
737 
738 #ifndef CONFIG_USER_ONLY
739 /*
740  * Note: we ignore any return code of the functions called for the pci
741  * instructions, as the only time they return !0 is when the stub is
742  * called, and in that case we didn't even offer the zpci facility.
743  * The only exception is SIC, where program checks need to be handled
744  * by the caller.
745  */
HELPER(clp)746 void HELPER(clp)(CPUS390XState *env, uint32_t r2)
747 {
748     S390CPU *cpu = env_archcpu(env);
749 
750     bql_lock();
751     clp_service_call(cpu, r2, GETPC());
752     bql_unlock();
753 }
754 
HELPER(pcilg)755 void HELPER(pcilg)(CPUS390XState *env, uint32_t r1, uint32_t r2)
756 {
757     S390CPU *cpu = env_archcpu(env);
758 
759     bql_lock();
760     pcilg_service_call(cpu, r1, r2, GETPC());
761     bql_unlock();
762 }
763 
HELPER(pcistg)764 void HELPER(pcistg)(CPUS390XState *env, uint32_t r1, uint32_t r2)
765 {
766     S390CPU *cpu = env_archcpu(env);
767 
768     bql_lock();
769     pcistg_service_call(cpu, r1, r2, GETPC());
770     bql_unlock();
771 }
772 
HELPER(stpcifc)773 void HELPER(stpcifc)(CPUS390XState *env, uint32_t r1, uint64_t fiba,
774                      uint32_t ar)
775 {
776     S390CPU *cpu = env_archcpu(env);
777 
778     bql_lock();
779     stpcifc_service_call(cpu, r1, fiba, ar, GETPC());
780     bql_unlock();
781 }
782 
HELPER(sic)783 void HELPER(sic)(CPUS390XState *env, uint64_t r1, uint64_t r3)
784 {
785     S390CPU *cpu = env_archcpu(env);
786     int r;
787 
788     bql_lock();
789     r = css_do_sic(cpu, (r3 >> 27) & 0x7, r1 & 0xffff);
790     bql_unlock();
791     /* css_do_sic() may actually return a PGM_xxx value to inject */
792     if (r) {
793         tcg_s390_program_interrupt(env, -r, GETPC());
794     }
795 }
796 
HELPER(rpcit)797 void HELPER(rpcit)(CPUS390XState *env, uint32_t r1, uint32_t r2)
798 {
799     S390CPU *cpu = env_archcpu(env);
800 
801     bql_lock();
802     rpcit_service_call(cpu, r1, r2, GETPC());
803     bql_unlock();
804 }
805 
HELPER(pcistb)806 void HELPER(pcistb)(CPUS390XState *env, uint32_t r1, uint32_t r3,
807                     uint64_t gaddr, uint32_t ar)
808 {
809     S390CPU *cpu = env_archcpu(env);
810 
811     bql_lock();
812     pcistb_service_call(cpu, r1, r3, gaddr, ar, GETPC());
813     bql_unlock();
814 }
815 
HELPER(mpcifc)816 void HELPER(mpcifc)(CPUS390XState *env, uint32_t r1, uint64_t fiba,
817                     uint32_t ar)
818 {
819     S390CPU *cpu = env_archcpu(env);
820 
821     bql_lock();
822     mpcifc_service_call(cpu, r1, fiba, ar, GETPC());
823     bql_unlock();
824 }
825 #endif
826