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