xref: /openbmc/qemu/target/s390x/diag.c (revision 8f968b6a)
1 /*
2  * S390x DIAG instruction helper functions
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include "qemu/osdep.h"
16 #include "cpu.h"
17 #include "internal.h"
18 #include "hw/watchdog/wdt_diag288.h"
19 #include "sysemu/cpus.h"
20 #include "hw/s390x/ipl.h"
21 #include "hw/s390x/s390-virtio-ccw.h"
22 #include "hw/s390x/pv.h"
23 #include "kvm_s390x.h"
24 
25 int handle_diag_288(CPUS390XState *env, uint64_t r1, uint64_t r3)
26 {
27     uint64_t func = env->regs[r1];
28     uint64_t timeout = env->regs[r1 + 1];
29     uint64_t action = env->regs[r3];
30     Object *obj;
31     DIAG288State *diag288;
32     DIAG288Class *diag288_class;
33 
34     if (r1 % 2 || action != 0) {
35         return -1;
36     }
37 
38     /* Timeout must be more than 15 seconds except for timer deletion */
39     if (func != WDT_DIAG288_CANCEL && timeout < 15) {
40         return -1;
41     }
42 
43     obj = object_resolve_path_type("", TYPE_WDT_DIAG288, NULL);
44     if (!obj) {
45         return -1;
46     }
47 
48     diag288 = DIAG288(obj);
49     diag288_class = DIAG288_GET_CLASS(diag288);
50     return diag288_class->handle_timer(diag288, func, timeout);
51 }
52 
53 static int diag308_parm_check(CPUS390XState *env, uint64_t r1, uint64_t addr,
54                               uintptr_t ra, bool write)
55 {
56     /* Handled by the Ultravisor */
57     if (s390_is_pv()) {
58         return 0;
59     }
60     if ((r1 & 1) || (addr & ~TARGET_PAGE_MASK)) {
61         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
62         return -1;
63     }
64     if (!address_space_access_valid(&address_space_memory, addr,
65                                     sizeof(IplParameterBlock), write,
66                                     MEMTXATTRS_UNSPECIFIED)) {
67         s390_program_interrupt(env, PGM_ADDRESSING, ra);
68         return -1;
69     }
70     return 0;
71 }
72 
73 void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
74 {
75     bool valid;
76     CPUState *cs = env_cpu(env);
77     S390CPU *cpu = S390_CPU(cs);
78     uint64_t addr =  env->regs[r1];
79     uint64_t subcode = env->regs[r3];
80     IplParameterBlock *iplb;
81 
82     if (env->psw.mask & PSW_MASK_PSTATE) {
83         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
84         return;
85     }
86 
87     if (subcode & ~0x0ffffULL) {
88         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
89         return;
90     }
91 
92     if (subcode >= DIAG308_PV_SET && !s390_has_feat(S390_FEAT_UNPACK)) {
93         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
94         return;
95     }
96 
97     switch (subcode) {
98     case DIAG308_RESET_MOD_CLR:
99         s390_ipl_reset_request(cs, S390_RESET_MODIFIED_CLEAR);
100         break;
101     case DIAG308_RESET_LOAD_NORM:
102         s390_ipl_reset_request(cs, S390_RESET_LOAD_NORMAL);
103         break;
104     case DIAG308_LOAD_CLEAR:
105         /* Well we still lack the clearing bit... */
106         s390_ipl_reset_request(cs, S390_RESET_REIPL);
107         break;
108     case DIAG308_SET:
109     case DIAG308_PV_SET:
110         if (diag308_parm_check(env, r1, addr, ra, false)) {
111             return;
112         }
113         iplb = g_new0(IplParameterBlock, 1);
114         if (!s390_is_pv()) {
115             cpu_physical_memory_read(addr, iplb, sizeof(iplb->len));
116         } else {
117             s390_cpu_pv_mem_read(cpu, 0, iplb, sizeof(iplb->len));
118         }
119 
120         if (!iplb_valid_len(iplb)) {
121             env->regs[r1 + 1] = DIAG_308_RC_INVALID;
122             goto out;
123         }
124 
125         if (!s390_is_pv()) {
126             cpu_physical_memory_read(addr, iplb, be32_to_cpu(iplb->len));
127         } else {
128             s390_cpu_pv_mem_read(cpu, 0, iplb, be32_to_cpu(iplb->len));
129         }
130 
131         valid = subcode == DIAG308_PV_SET ? iplb_valid_pv(iplb) : iplb_valid(iplb);
132         if (!valid) {
133             env->regs[r1 + 1] = DIAG_308_RC_INVALID;
134             goto out;
135         }
136 
137         s390_ipl_update_diag308(iplb);
138         env->regs[r1 + 1] = DIAG_308_RC_OK;
139 out:
140         g_free(iplb);
141         return;
142     case DIAG308_STORE:
143     case DIAG308_PV_STORE:
144         if (diag308_parm_check(env, r1, addr, ra, true)) {
145             return;
146         }
147         if (subcode == DIAG308_PV_STORE) {
148             iplb = s390_ipl_get_iplb_pv();
149         } else {
150             iplb = s390_ipl_get_iplb();
151         }
152         if (!iplb) {
153             env->regs[r1 + 1] = DIAG_308_RC_NO_CONF;
154             return;
155         }
156 
157         if (!s390_is_pv()) {
158             cpu_physical_memory_write(addr, iplb, be32_to_cpu(iplb->len));
159         } else {
160             s390_cpu_pv_mem_write(cpu, 0, iplb, be32_to_cpu(iplb->len));
161         }
162         env->regs[r1 + 1] = DIAG_308_RC_OK;
163         return;
164     case DIAG308_PV_START:
165         iplb = s390_ipl_get_iplb_pv();
166         if (!iplb) {
167             env->regs[r1 + 1] = DIAG_308_RC_NO_PV_CONF;
168             return;
169         }
170 
171         if (kvm_s390_get_hpage_1m()) {
172             error_report("Protected VMs can currently not be backed with "
173                          "huge pages");
174             env->regs[r1 + 1] = DIAG_308_RC_INVAL_FOR_PV;
175             return;
176         }
177 
178         s390_ipl_reset_request(cs, S390_RESET_PV);
179         break;
180     default:
181         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
182         break;
183     }
184 }
185