xref: /qemu/hw/s390x/sclp.c (revision bb185de4)
1 /*
2  * SCLP Support
3  *
4  * Copyright IBM, Corp. 2012
5  *
6  * Authors:
7  *  Christian Borntraeger <borntraeger@de.ibm.com>
8  *  Heinz Graalfs <graalfs@linux.vnet.ibm.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
11  * option) any later version.  See the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qemu/units.h"
17 #include "qapi/error.h"
18 #include "hw/boards.h"
19 #include "hw/s390x/sclp.h"
20 #include "hw/s390x/event-facility.h"
21 #include "hw/s390x/s390-pci-bus.h"
22 #include "hw/s390x/ipl.h"
23 #include "hw/s390x/cpu-topology.h"
24 #include "hw/s390x/s390-virtio-ccw.h"
25 
get_sclp_device(void)26 static SCLPDevice *get_sclp_device(void)
27 {
28     static SCLPDevice *sclp;
29 
30     if (!sclp) {
31         sclp = S390_CCW_MACHINE(qdev_get_machine())->sclp;
32     }
33     return sclp;
34 }
35 
sclp_command_code_valid(uint32_t code)36 static inline bool sclp_command_code_valid(uint32_t code)
37 {
38     switch (code & SCLP_CMD_CODE_MASK) {
39     case SCLP_CMDW_READ_SCP_INFO:
40     case SCLP_CMDW_READ_SCP_INFO_FORCED:
41     case SCLP_CMDW_READ_CPU_INFO:
42     case SCLP_CMDW_CONFIGURE_IOA:
43     case SCLP_CMDW_DECONFIGURE_IOA:
44     case SCLP_CMD_READ_EVENT_DATA:
45     case SCLP_CMD_WRITE_EVENT_DATA:
46     case SCLP_CMD_WRITE_EVENT_MASK:
47         return true;
48     }
49     return false;
50 }
51 
sccb_verify_boundary(uint64_t sccb_addr,uint16_t sccb_len,uint32_t code)52 static bool sccb_verify_boundary(uint64_t sccb_addr, uint16_t sccb_len,
53                                  uint32_t code)
54 {
55     uint64_t sccb_max_addr = sccb_addr + sccb_len - 1;
56     uint64_t sccb_boundary = (sccb_addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
57 
58     switch (code & SCLP_CMD_CODE_MASK) {
59     case SCLP_CMDW_READ_SCP_INFO:
60     case SCLP_CMDW_READ_SCP_INFO_FORCED:
61     case SCLP_CMDW_READ_CPU_INFO:
62         /*
63          * An extended-length SCCB is only allowed for Read SCP/CPU Info and
64          * is allowed to exceed the 4k boundary. The respective commands will
65          * set the length field to the required length if an insufficient
66          * SCCB length is provided.
67          */
68         if (s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB)) {
69             return true;
70         }
71         /* fallthrough */
72     default:
73         if (sccb_max_addr < sccb_boundary) {
74             return true;
75         }
76     }
77 
78     return false;
79 }
80 
prepare_cpu_entries(MachineState * ms,CPUEntry * entry,int * count)81 static void prepare_cpu_entries(MachineState *ms, CPUEntry *entry, int *count)
82 {
83     uint8_t features[SCCB_CPU_FEATURE_LEN] = { 0 };
84     int i;
85 
86     s390_get_feat_block(S390_FEAT_TYPE_SCLP_CPU, features);
87     for (i = 0, *count = 0; i < ms->possible_cpus->len; i++) {
88         if (!ms->possible_cpus->cpus[i].cpu) {
89             continue;
90         }
91         entry[*count].address = ms->possible_cpus->cpus[i].arch_id;
92         entry[*count].type = 0;
93         memcpy(entry[*count].features, features, sizeof(features));
94         (*count)++;
95     }
96 }
97 
98 #define SCCB_REQ_LEN(s, max_cpus) (sizeof(s) + max_cpus * sizeof(CPUEntry))
99 
ext_len_sccb_supported(SCCBHeader header)100 static inline bool ext_len_sccb_supported(SCCBHeader header)
101 {
102     return s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) &&
103            header.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE;
104 }
105 
106 /* Provide information about the configuration, CPUs and storage */
read_SCP_info(SCLPDevice * sclp,SCCB * sccb)107 static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)
108 {
109     ReadInfo *read_info = (ReadInfo *) sccb;
110     MachineState *machine = MACHINE(qdev_get_machine());
111     int cpu_count;
112     int rnsize, rnmax;
113     int required_len = SCCB_REQ_LEN(ReadInfo, machine->possible_cpus->len);
114     int offset_cpu = s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) ?
115                      offsetof(ReadInfo, entries) :
116                      SCLP_READ_SCP_INFO_FIXED_CPU_OFFSET;
117     CPUEntry *entries_start = (void *)sccb + offset_cpu;
118 
119     if (be16_to_cpu(sccb->h.length) < required_len) {
120         if (ext_len_sccb_supported(sccb->h)) {
121             sccb->h.length = cpu_to_be16(required_len);
122         }
123         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
124         return;
125     }
126 
127     if (s390_has_topology()) {
128         read_info->stsi_parm = SCLP_READ_SCP_INFO_MNEST;
129     }
130 
131     /* CPU information */
132     prepare_cpu_entries(machine, entries_start, &cpu_count);
133     read_info->entries_cpu = cpu_to_be16(cpu_count);
134     read_info->offset_cpu = cpu_to_be16(offset_cpu);
135     read_info->highest_cpu = cpu_to_be16(machine->smp.max_cpus - 1);
136 
137     read_info->ibc_val = cpu_to_be32(s390_get_ibc_val());
138 
139     /* Configuration Characteristic (Extension) */
140     s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR,
141                          read_info->conf_char);
142     s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT,
143                          read_info->conf_char_ext);
144 
145     if (s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB)) {
146         s390_get_feat_block(S390_FEAT_TYPE_SCLP_FAC134,
147                             &read_info->fac134);
148     }
149 
150     read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
151                                         SCLP_HAS_IOA_RECONFIG);
152 
153     read_info->mha_pow = s390_get_mha_pow();
154     read_info->hmfai = cpu_to_be32(s390_get_hmfai());
155 
156     rnsize = 1 << (sclp->increment_size - 20);
157     if (rnsize <= 128) {
158         read_info->rnsize = rnsize;
159     } else {
160         read_info->rnsize = 0;
161         read_info->rnsize2 = cpu_to_be32(rnsize);
162     }
163 
164     /* we don't support standby memory, maxram_size is never exposed */
165     rnmax = machine->ram_size >> sclp->increment_size;
166     if (rnmax < 0x10000) {
167         read_info->rnmax = cpu_to_be16(rnmax);
168     } else {
169         read_info->rnmax = cpu_to_be16(0);
170         read_info->rnmax2 = cpu_to_be64(rnmax);
171     }
172 
173     s390_ipl_convert_loadparm((char *)S390_CCW_MACHINE(machine)->loadparm,
174                                 read_info->loadparm);
175 
176     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
177 }
178 
179 /* Provide information about the CPU */
sclp_read_cpu_info(SCLPDevice * sclp,SCCB * sccb)180 static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb)
181 {
182     MachineState *machine = MACHINE(qdev_get_machine());
183     ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
184     int cpu_count;
185     int required_len = SCCB_REQ_LEN(ReadCpuInfo, machine->possible_cpus->len);
186 
187     if (be16_to_cpu(sccb->h.length) < required_len) {
188         if (ext_len_sccb_supported(sccb->h)) {
189             sccb->h.length = cpu_to_be16(required_len);
190         }
191         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
192         return;
193     }
194 
195     prepare_cpu_entries(machine, cpu_info->entries, &cpu_count);
196     cpu_info->nr_configured = cpu_to_be16(cpu_count);
197     cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries));
198     cpu_info->nr_standby = cpu_to_be16(0);
199 
200     /* The standby offset is 16-byte for each CPU */
201     cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured
202         + cpu_info->nr_configured*sizeof(CPUEntry));
203 
204 
205     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
206 }
207 
sclp_configure_io_adapter(SCLPDevice * sclp,SCCB * sccb,bool configure)208 static void sclp_configure_io_adapter(SCLPDevice *sclp, SCCB *sccb,
209                                       bool configure)
210 {
211     int rc;
212 
213     if (be16_to_cpu(sccb->h.length) < 16) {
214         rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH;
215         goto out_err;
216     }
217 
218     switch (((IoaCfgSccb *)sccb)->atype) {
219     case SCLP_RECONFIG_PCI_ATYPE:
220         if (s390_has_feat(S390_FEAT_ZPCI)) {
221             if (configure) {
222                 s390_pci_sclp_configure(sccb);
223             } else {
224                 s390_pci_sclp_deconfigure(sccb);
225             }
226             return;
227         }
228         /* fallthrough */
229     default:
230         rc = SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED;
231     }
232 
233  out_err:
234     sccb->h.response_code = cpu_to_be16(rc);
235 }
236 
sclp_execute(SCLPDevice * sclp,SCCB * sccb,uint32_t code)237 static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
238 {
239     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
240     SCLPEventFacility *ef = sclp->event_facility;
241     SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
242 
243     switch (code & SCLP_CMD_CODE_MASK) {
244     case SCLP_CMDW_READ_SCP_INFO:
245     case SCLP_CMDW_READ_SCP_INFO_FORCED:
246         sclp_c->read_SCP_info(sclp, sccb);
247         break;
248     case SCLP_CMDW_READ_CPU_INFO:
249         sclp_c->read_cpu_info(sclp, sccb);
250         break;
251     case SCLP_CMDW_CONFIGURE_IOA:
252         sclp_configure_io_adapter(sclp, sccb, true);
253         break;
254     case SCLP_CMDW_DECONFIGURE_IOA:
255         sclp_configure_io_adapter(sclp, sccb, false);
256         break;
257     default:
258         efc->command_handler(ef, sccb, code);
259         break;
260     }
261 }
262 
263 /*
264  * We only need the address to have something valid for the
265  * service_interrupt call.
266  */
267 #define SCLP_PV_DUMMY_ADDR 0x4000
sclp_service_call_protected(S390CPU * cpu,uint64_t sccb,uint32_t code)268 int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
269 {
270     CPUS390XState *env = &cpu->env;
271     SCLPDevice *sclp = get_sclp_device();
272     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
273     SCCBHeader header;
274     g_autofree SCCB *work_sccb = NULL;
275 
276     s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader));
277 
278     work_sccb = g_malloc0(be16_to_cpu(header.length));
279     s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb,
280                          be16_to_cpu(header.length));
281 
282     if (!sclp_command_code_valid(code)) {
283         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
284         goto out_write;
285     }
286 
287     sclp_c->execute(sclp, work_sccb, code);
288 out_write:
289     s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb,
290                           be16_to_cpu(work_sccb->h.length));
291     sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
292     return 0;
293 }
294 
sclp_service_call(S390CPU * cpu,uint64_t sccb,uint32_t code)295 int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code)
296 {
297     CPUS390XState *env = &cpu->env;
298     SCLPDevice *sclp = get_sclp_device();
299     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
300     SCCBHeader header;
301     g_autofree SCCB *work_sccb = NULL;
302 
303     /* first some basic checks on program checks */
304     if (env->psw.mask & PSW_MASK_PSTATE) {
305         return -PGM_PRIVILEGED;
306     }
307     if (cpu_physical_memory_is_io(sccb)) {
308         return -PGM_ADDRESSING;
309     }
310     if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
311         || (sccb & ~0x7ffffff8UL) != 0) {
312         return -PGM_SPECIFICATION;
313     }
314 
315     /* the header contains the actual length of the sccb */
316     cpu_physical_memory_read(sccb, &header, sizeof(SCCBHeader));
317 
318     /* Valid sccb sizes */
319     if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) {
320         return -PGM_SPECIFICATION;
321     }
322 
323     /*
324      * we want to work on a private copy of the sccb, to prevent guests
325      * from playing dirty tricks by modifying the memory content after
326      * the host has checked the values
327      */
328     work_sccb = g_malloc0(be16_to_cpu(header.length));
329     cpu_physical_memory_read(sccb, work_sccb, be16_to_cpu(header.length));
330 
331     if (!sclp_command_code_valid(code)) {
332         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
333         goto out_write;
334     }
335 
336     if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb->h.length), code)) {
337         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
338         goto out_write;
339     }
340 
341     sclp_c->execute(sclp, work_sccb, code);
342 out_write:
343     cpu_physical_memory_write(sccb, work_sccb,
344                               be16_to_cpu(work_sccb->h.length));
345 
346     sclp_c->service_interrupt(sclp, sccb);
347 
348     return 0;
349 }
350 
service_interrupt(SCLPDevice * sclp,uint32_t sccb)351 static void service_interrupt(SCLPDevice *sclp, uint32_t sccb)
352 {
353     SCLPEventFacility *ef = sclp->event_facility;
354     SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
355 
356     uint32_t param = sccb & ~3;
357 
358     /* Indicate whether an event is still pending */
359     param |= efc->event_pending(ef) ? 1 : 0;
360 
361     if (!param) {
362         /* No need to send an interrupt, there's nothing to be notified about */
363         return;
364     }
365     s390_sclp_extint(param);
366 }
367 
sclp_service_interrupt(uint32_t sccb)368 void sclp_service_interrupt(uint32_t sccb)
369 {
370     SCLPDevice *sclp = get_sclp_device();
371     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
372 
373     sclp_c->service_interrupt(sclp, sccb);
374 }
375 
376 /* qemu object creation and initialization functions */
sclp_realize(DeviceState * dev,Error ** errp)377 static void sclp_realize(DeviceState *dev, Error **errp)
378 {
379     MachineState *machine = MACHINE(qdev_get_machine());
380     SCLPDevice *sclp = SCLP(dev);
381     uint64_t hw_limit;
382     int ret;
383 
384     /*
385      * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long
386      * as we can't find a fitting bus via the qom tree, we have to add the
387      * event facility to the sysbus, so e.g. a sclp console can be created.
388      */
389     if (!sysbus_realize(SYS_BUS_DEVICE(sclp->event_facility), errp)) {
390         return;
391     }
392 
393     ret = s390_set_memory_limit(machine->maxram_size, &hw_limit);
394     if (ret == -E2BIG) {
395         error_setg(errp, "host supports a maximum of %" PRIu64 " GB",
396                    hw_limit / GiB);
397     } else if (ret) {
398         error_setg(errp, "setting the guest size failed");
399     }
400 }
401 
sclp_memory_init(SCLPDevice * sclp)402 static void sclp_memory_init(SCLPDevice *sclp)
403 {
404     MachineState *machine = MACHINE(qdev_get_machine());
405     MachineClass *machine_class = MACHINE_GET_CLASS(qdev_get_machine());
406     ram_addr_t initial_mem = machine->ram_size;
407     int increment_size = 20;
408 
409     /* The storage increment size is a multiple of 1M and is a power of 2.
410      * For some machine types, the number of storage increments must be
411      * MAX_STORAGE_INCREMENTS or fewer.
412      * The variable 'increment_size' is an exponent of 2 that can be
413      * used to calculate the size (in bytes) of an increment. */
414     while (machine_class->fixup_ram_size != NULL &&
415            (initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
416         increment_size++;
417     }
418     sclp->increment_size = increment_size;
419 }
420 
sclp_init(Object * obj)421 static void sclp_init(Object *obj)
422 {
423     SCLPDevice *sclp = SCLP(obj);
424     Object *new;
425 
426     new = object_new(TYPE_SCLP_EVENT_FACILITY);
427     object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new);
428     object_unref(new);
429     sclp->event_facility = EVENT_FACILITY(new);
430 
431     sclp_memory_init(sclp);
432 }
433 
sclp_class_init(ObjectClass * oc,void * data)434 static void sclp_class_init(ObjectClass *oc, void *data)
435 {
436     SCLPDeviceClass *sc = SCLP_CLASS(oc);
437     DeviceClass *dc = DEVICE_CLASS(oc);
438 
439     dc->desc = "SCLP (Service-Call Logical Processor)";
440     dc->realize = sclp_realize;
441     dc->hotpluggable = false;
442     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
443     /*
444      * Reason: Creates TYPE_SCLP_EVENT_FACILITY in sclp_init
445      * which is a non-pluggable sysbus device
446      */
447     dc->user_creatable = false;
448 
449     sc->read_SCP_info = read_SCP_info;
450     sc->read_cpu_info = sclp_read_cpu_info;
451     sc->execute = sclp_execute;
452     sc->service_interrupt = service_interrupt;
453 }
454 
455 static const TypeInfo sclp_info = {
456     .name = TYPE_SCLP,
457     .parent = TYPE_DEVICE,
458     .instance_init = sclp_init,
459     .instance_size = sizeof(SCLPDevice),
460     .class_init = sclp_class_init,
461     .class_size = sizeof(SCLPDeviceClass),
462 };
463 
register_types(void)464 static void register_types(void)
465 {
466     type_register_static(&sclp_info);
467 }
468 type_init(register_types);
469