xref: /qemu/hw/s390x/event-facility.c (revision 1721fe75df1cbabf2665a2b76a6e7b5bc0fc036b)
1 /*
2  * SCLP
3  *    Event Facility
4  *       handles SCLP event types
5  *          - Signal Quiesce - system power down
6  *          - ASCII Console Data - VT220 read and write
7  *          - Control-Program Identification - Send OS data from guest to host
8  *
9  * Copyright IBM, Corp. 2012
10  *
11  * Authors:
12  *  Heinz Graalfs <graalfs@de.ibm.com>
13  *
14  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
15  * option) any later version.  See the COPYING file in the top-level directory.
16  *
17  */
18 
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "qemu/module.h"
22 
23 #include "hw/s390x/sclp.h"
24 #include "migration/vmstate.h"
25 #include "hw/s390x/event-facility.h"
26 
27 typedef struct SCLPEventsBus {
28     BusState qbus;
29 } SCLPEventsBus;
30 
31 /* we need to save 32 bit chunks for compatibility */
32 #if HOST_BIG_ENDIAN
33 #define RECV_MASK_LOWER 1
34 #define RECV_MASK_UPPER 0
35 #else /* little endian host */
36 #define RECV_MASK_LOWER 0
37 #define RECV_MASK_UPPER 1
38 #endif
39 
40 struct SCLPEventFacility {
41     SysBusDevice parent_obj;
42     SCLPEventsBus sbus;
43     SCLPEvent quiesce, cpu_hotplug;
44     SCLPEventCPI cpi;
45     /* guest's receive mask */
46     union {
47         uint32_t receive_mask_pieces[2];
48         sccb_mask_t receive_mask;
49     };
50     /* length of the receive mask */
51     uint16_t mask_length;
52 };
53 
54 /* return true if any child has event pending set */
event_pending(SCLPEventFacility * ef)55 static bool event_pending(SCLPEventFacility *ef)
56 {
57     BusChild *kid;
58     SCLPEvent *event;
59     SCLPEventClass *event_class;
60 
61     QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
62         event = SCLP_EVENT(kid->child);
63         event_class = SCLP_EVENT_GET_CLASS(event);
64         if (event->event_pending &&
65             event_class->get_send_mask() & ef->receive_mask) {
66             return true;
67         }
68     }
69     return false;
70 }
71 
get_host_send_mask(SCLPEventFacility * ef)72 static sccb_mask_t get_host_send_mask(SCLPEventFacility *ef)
73 {
74     sccb_mask_t mask;
75     BusChild *kid;
76     SCLPEventClass *child;
77 
78     mask = 0;
79 
80     QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
81         DeviceState *qdev = kid->child;
82         child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev);
83         mask |= child->get_send_mask();
84     }
85     return mask;
86 }
87 
get_host_receive_mask(SCLPEventFacility * ef)88 static sccb_mask_t get_host_receive_mask(SCLPEventFacility *ef)
89 {
90     sccb_mask_t mask;
91     BusChild *kid;
92     SCLPEventClass *child;
93 
94     mask = 0;
95 
96     QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
97         DeviceState *qdev = kid->child;
98         child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev);
99         mask |= child->get_receive_mask();
100     }
101     return mask;
102 }
103 
write_event_length_check(SCCB * sccb)104 static uint16_t write_event_length_check(SCCB *sccb)
105 {
106     int slen;
107     unsigned elen = 0;
108     EventBufferHeader *event;
109     WriteEventData *wed = (WriteEventData *) sccb;
110 
111     event = (EventBufferHeader *) &wed->ebh;
112     for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) {
113         elen = be16_to_cpu(event->length);
114         if (elen < sizeof(*event) || elen > slen) {
115             return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR;
116         }
117         event = (void *) event + elen;
118     }
119     if (slen) {
120         return SCLP_RC_INCONSISTENT_LENGTHS;
121     }
122     return SCLP_RC_NORMAL_COMPLETION;
123 }
124 
handle_write_event_buf(SCLPEventFacility * ef,EventBufferHeader * event_buf,SCCB * sccb)125 static uint16_t handle_write_event_buf(SCLPEventFacility *ef,
126                                        EventBufferHeader *event_buf, SCCB *sccb)
127 {
128     uint16_t rc;
129     BusChild *kid;
130     SCLPEvent *event;
131     SCLPEventClass *ec;
132 
133     rc = SCLP_RC_INVALID_FUNCTION;
134 
135     QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
136         DeviceState *qdev = kid->child;
137         event = (SCLPEvent *) qdev;
138         ec = SCLP_EVENT_GET_CLASS(event);
139 
140         if (ec->write_event_data &&
141             ec->can_handle_event(event_buf->type)) {
142             rc = ec->write_event_data(event, event_buf);
143             break;
144         }
145     }
146     return rc;
147 }
148 
handle_sccb_write_events(SCLPEventFacility * ef,SCCB * sccb)149 static uint16_t handle_sccb_write_events(SCLPEventFacility *ef, SCCB *sccb)
150 {
151     uint16_t rc;
152     int slen;
153     unsigned elen = 0;
154     EventBufferHeader *event_buf;
155     WriteEventData *wed = (WriteEventData *) sccb;
156 
157     event_buf = &wed->ebh;
158     rc = SCLP_RC_NORMAL_COMPLETION;
159 
160     /* loop over all contained event buffers */
161     for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) {
162         elen = be16_to_cpu(event_buf->length);
163 
164         /* in case of a previous error mark all trailing buffers
165          * as not accepted */
166         if (rc != SCLP_RC_NORMAL_COMPLETION) {
167             event_buf->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
168         } else {
169             rc = handle_write_event_buf(ef, event_buf, sccb);
170         }
171         event_buf = (void *) event_buf + elen;
172     }
173     return rc;
174 }
175 
write_event_data(SCLPEventFacility * ef,SCCB * sccb)176 static void write_event_data(SCLPEventFacility *ef, SCCB *sccb)
177 {
178     if (sccb->h.function_code != SCLP_FC_NORMAL_WRITE) {
179         sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
180         return;
181     }
182     if (be16_to_cpu(sccb->h.length) < 8) {
183         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
184         return;
185     }
186     /* first do a sanity check of the write events */
187     sccb->h.response_code = cpu_to_be16(write_event_length_check(sccb));
188 
189     /* if no early error, then execute */
190     if (sccb->h.response_code == be16_to_cpu(SCLP_RC_NORMAL_COMPLETION)) {
191         sccb->h.response_code =
192                 cpu_to_be16(handle_sccb_write_events(ef, sccb));
193     }
194 }
195 
handle_sccb_read_events(SCLPEventFacility * ef,SCCB * sccb,sccb_mask_t mask)196 static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb,
197                                         sccb_mask_t mask)
198 {
199     uint16_t rc;
200     int slen;
201     unsigned elen;
202     BusChild *kid;
203     SCLPEvent *event;
204     SCLPEventClass *ec;
205     EventBufferHeader *event_buf;
206     ReadEventData *red = (ReadEventData *) sccb;
207 
208     event_buf = &red->ebh;
209     event_buf->length = 0;
210     slen = sccb_data_len(sccb);
211 
212     rc = SCLP_RC_NO_EVENT_BUFFERS_STORED;
213 
214     QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
215         DeviceState *qdev = kid->child;
216         event = (SCLPEvent *) qdev;
217         ec = SCLP_EVENT_GET_CLASS(event);
218 
219         if (mask & ec->get_send_mask()) {
220             if (ec->read_event_data(event, event_buf, &slen)) {
221                 elen = be16_to_cpu(event_buf->length);
222                 event_buf = (EventBufferHeader *) ((char *)event_buf + elen);
223                 rc = SCLP_RC_NORMAL_COMPLETION;
224             }
225         }
226     }
227 
228     if (sccb->h.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE) {
229         /* architecture suggests to reset variable-length-response bit */
230         sccb->h.control_mask[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE;
231         /* with a new length value */
232         sccb->h.length = cpu_to_be16(SCCB_SIZE - slen);
233     }
234     return rc;
235 }
236 
237 /* copy up to src_len bytes and fill the rest of dst with zeroes */
copy_mask(uint8_t * dst,uint8_t * src,uint16_t dst_len,uint16_t src_len)238 static void copy_mask(uint8_t *dst, uint8_t *src, uint16_t dst_len,
239                       uint16_t src_len)
240 {
241     int i;
242 
243     for (i = 0; i < dst_len; i++) {
244         dst[i] = i < src_len ? src[i] : 0;
245     }
246 }
247 
read_event_data(SCLPEventFacility * ef,SCCB * sccb)248 static void read_event_data(SCLPEventFacility *ef, SCCB *sccb)
249 {
250     sccb_mask_t sclp_active_selection_mask;
251     sccb_mask_t sclp_cp_receive_mask;
252 
253     ReadEventData *red = (ReadEventData *) sccb;
254 
255     if (be16_to_cpu(sccb->h.length) != SCCB_SIZE) {
256         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
257         return;
258     }
259 
260     switch (sccb->h.function_code) {
261     case SCLP_UNCONDITIONAL_READ:
262         sccb->h.response_code = cpu_to_be16(
263             handle_sccb_read_events(ef, sccb, ef->receive_mask));
264         break;
265     case SCLP_SELECTIVE_READ:
266         /* get active selection mask */
267         sclp_cp_receive_mask = ef->receive_mask;
268 
269         copy_mask((uint8_t *)&sclp_active_selection_mask, (uint8_t *)&red->mask,
270                   sizeof(sclp_active_selection_mask), ef->mask_length);
271         sclp_active_selection_mask = be64_to_cpu(sclp_active_selection_mask);
272         if (!sclp_cp_receive_mask ||
273             (sclp_active_selection_mask & ~sclp_cp_receive_mask)) {
274             sccb->h.response_code =
275                     cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK);
276         } else {
277             sccb->h.response_code = cpu_to_be16(
278                 handle_sccb_read_events(ef, sccb, sclp_active_selection_mask));
279         }
280         break;
281     default:
282         sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
283     }
284 }
285 
write_event_mask(SCLPEventFacility * ef,SCCB * sccb)286 static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb)
287 {
288     WriteEventMask *we_mask = (WriteEventMask *) sccb;
289     uint16_t mask_length = be16_to_cpu(we_mask->mask_length);
290     sccb_mask_t tmp_mask;
291 
292     if (!mask_length || mask_length > SCLP_EVENT_MASK_LEN_MAX) {
293         sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH);
294         return;
295     }
296 
297     /*
298      * Note: We currently only support masks up to 8 byte length;
299      *       the remainder is filled up with zeroes. Older Linux
300      *       kernels use a 4 byte mask length, newer ones can use both
301      *       8 or 4 depending on what is available on the host.
302      */
303 
304     /* keep track of the guest's capability masks */
305     copy_mask((uint8_t *)&tmp_mask, WEM_CP_RECEIVE_MASK(we_mask, mask_length),
306               sizeof(tmp_mask), mask_length);
307     ef->receive_mask = be64_to_cpu(tmp_mask);
308 
309     /* return the SCLP's capability masks to the guest */
310     tmp_mask = cpu_to_be64(get_host_receive_mask(ef));
311     copy_mask(WEM_RECEIVE_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask,
312               mask_length, sizeof(tmp_mask));
313     tmp_mask = cpu_to_be64(get_host_send_mask(ef));
314     copy_mask(WEM_SEND_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask,
315               mask_length, sizeof(tmp_mask));
316 
317     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
318     ef->mask_length = mask_length;
319 }
320 
321 /* qemu object creation and initialization functions */
322 
323 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
324 
325 static const TypeInfo sclp_events_bus_info = {
326     .name = TYPE_SCLP_EVENTS_BUS,
327     .parent = TYPE_BUS,
328 };
329 
command_handler(SCLPEventFacility * ef,SCCB * sccb,uint64_t code)330 static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code)
331 {
332     switch (code & SCLP_CMD_CODE_MASK) {
333     case SCLP_CMD_READ_EVENT_DATA:
334         read_event_data(ef, sccb);
335         break;
336     case SCLP_CMD_WRITE_EVENT_DATA:
337         write_event_data(ef, sccb);
338         break;
339     case SCLP_CMD_WRITE_EVENT_MASK:
340         write_event_mask(ef, sccb);
341         break;
342     }
343 }
344 
vmstate_event_facility_mask64_needed(void * opaque)345 static bool vmstate_event_facility_mask64_needed(void *opaque)
346 {
347     SCLPEventFacility *ef = opaque;
348 
349     return (ef->receive_mask & 0xFFFFFFFF) != 0;
350 }
351 
352 static const VMStateDescription vmstate_event_facility_mask64 = {
353     .name = "vmstate-event-facility/mask64",
354     .version_id = 0,
355     .minimum_version_id = 0,
356     .needed = vmstate_event_facility_mask64_needed,
357     .fields = (const VMStateField[]) {
358         VMSTATE_UINT32(receive_mask_pieces[RECV_MASK_LOWER], SCLPEventFacility),
359         VMSTATE_END_OF_LIST()
360      }
361 };
362 
363 static const VMStateDescription vmstate_event_facility_mask_length = {
364     .name = "vmstate-event-facility/mask_length",
365     .version_id = 0,
366     .minimum_version_id = 0,
367     .fields = (const VMStateField[]) {
368         VMSTATE_UINT16(mask_length, SCLPEventFacility),
369         VMSTATE_END_OF_LIST()
370      }
371 };
372 
373 static const VMStateDescription vmstate_event_facility = {
374     .name = "vmstate-event-facility",
375     .version_id = 0,
376     .minimum_version_id = 0,
377     .fields = (const VMStateField[]) {
378         VMSTATE_UINT32(receive_mask_pieces[RECV_MASK_UPPER], SCLPEventFacility),
379         VMSTATE_END_OF_LIST()
380      },
381     .subsections = (const VMStateDescription * const []) {
382         &vmstate_event_facility_mask64,
383         &vmstate_event_facility_mask_length,
384         NULL
385      }
386 };
387 
init_event_facility(Object * obj)388 static void init_event_facility(Object *obj)
389 {
390     SCLPEventFacility *event_facility = EVENT_FACILITY(obj);
391     DeviceState *sdev = DEVICE(obj);
392 
393     event_facility->mask_length = 4;
394 
395     /* Spawn a new bus for SCLP events */
396     qbus_init(&event_facility->sbus, sizeof(event_facility->sbus),
397               TYPE_SCLP_EVENTS_BUS, sdev, NULL);
398 
399     object_initialize_child(obj, TYPE_SCLP_QUIESCE,
400                             &event_facility->quiesce,
401                             TYPE_SCLP_QUIESCE);
402 
403     object_initialize_child(obj, TYPE_SCLP_CPU_HOTPLUG,
404                             &event_facility->cpu_hotplug,
405                             TYPE_SCLP_CPU_HOTPLUG);
406 }
407 
realize_event_facility(DeviceState * dev,Error ** errp)408 static void realize_event_facility(DeviceState *dev, Error **errp)
409 {
410     SCLPEventFacility *event_facility = EVENT_FACILITY(dev);
411 
412     if (!qdev_realize(DEVICE(&event_facility->quiesce),
413                       BUS(&event_facility->sbus), errp)) {
414         return;
415     }
416     if (!qdev_realize(DEVICE(&event_facility->cpu_hotplug),
417                       BUS(&event_facility->sbus), errp)) {
418         qdev_unrealize(DEVICE(&event_facility->quiesce));
419         return;
420     }
421 }
422 
reset_event_facility(DeviceState * dev)423 static void reset_event_facility(DeviceState *dev)
424 {
425     SCLPEventFacility *sdev = EVENT_FACILITY(dev);
426 
427     sdev->receive_mask = 0;
428 }
429 
init_event_facility_class(ObjectClass * klass,const void * data)430 static void init_event_facility_class(ObjectClass *klass, const void *data)
431 {
432     SysBusDeviceClass *sbdc = SYS_BUS_DEVICE_CLASS(klass);
433     DeviceClass *dc = DEVICE_CLASS(sbdc);
434     SCLPEventFacilityClass *k = EVENT_FACILITY_CLASS(dc);
435 
436     dc->realize = realize_event_facility;
437     device_class_set_legacy_reset(dc, reset_event_facility);
438     dc->vmsd = &vmstate_event_facility;
439     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
440     k->command_handler = command_handler;
441     k->event_pending = event_pending;
442 }
443 
444 static const TypeInfo sclp_event_facility_info = {
445     .name          = TYPE_SCLP_EVENT_FACILITY,
446     .parent        = TYPE_SYS_BUS_DEVICE,
447     .instance_init = init_event_facility,
448     .instance_size = sizeof(SCLPEventFacility),
449     .class_init    = init_event_facility_class,
450     .class_size    = sizeof(SCLPEventFacilityClass),
451 };
452 
event_realize(DeviceState * qdev,Error ** errp)453 static void event_realize(DeviceState *qdev, Error **errp)
454 {
455     SCLPEvent *event = SCLP_EVENT(qdev);
456     SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event);
457 
458     if (child->init) {
459         int rc = child->init(event);
460         if (rc < 0) {
461             error_setg(errp, "SCLP event initialization failed.");
462             return;
463         }
464     }
465 }
466 
event_class_init(ObjectClass * klass,const void * data)467 static void event_class_init(ObjectClass *klass, const void *data)
468 {
469     DeviceClass *dc = DEVICE_CLASS(klass);
470 
471     dc->bus_type = TYPE_SCLP_EVENTS_BUS;
472     dc->realize = event_realize;
473 }
474 
475 static const TypeInfo sclp_event_type_info = {
476     .name = TYPE_SCLP_EVENT,
477     .parent = TYPE_DEVICE,
478     .instance_size = sizeof(SCLPEvent),
479     .class_init = event_class_init,
480     .class_size = sizeof(SCLPEventClass),
481     .abstract = true,
482 };
483 
register_types(void)484 static void register_types(void)
485 {
486     type_register_static(&sclp_events_bus_info);
487     type_register_static(&sclp_event_facility_info);
488     type_register_static(&sclp_event_type_info);
489 }
490 
type_init(register_types)491 type_init(register_types)
492 
493 BusState *sclp_get_event_facility_bus(SCLPEventFacility *ef)
494 {
495     return BUS(&ef->sbus);
496 }
497