qga: convert to use error checked base64 decode
[qemu/ar7.git] / hw / s390x / event-facility.c
blob907b48560c2dbebad0afe7846dc1c9d56323c544
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
8 * Copyright IBM, Corp. 2012
10 * Authors:
11 * Heinz Graalfs <graalfs@de.ibm.com>
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.
18 #include "sysemu/sysemu.h"
20 #include "hw/s390x/sclp.h"
21 #include "hw/s390x/event-facility.h"
23 typedef struct SCLPEventsBus {
24 BusState qbus;
25 } SCLPEventsBus;
27 struct SCLPEventFacility {
28 SysBusDevice parent_obj;
29 SCLPEventsBus sbus;
30 /* guest' receive mask */
31 unsigned int receive_mask;
34 /* return true if any child has event pending set */
35 static bool event_pending(SCLPEventFacility *ef)
37 BusChild *kid;
38 SCLPEvent *event;
39 SCLPEventClass *event_class;
41 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
42 DeviceState *qdev = kid->child;
43 event = DO_UPCAST(SCLPEvent, qdev, qdev);
44 event_class = SCLP_EVENT_GET_CLASS(event);
45 if (event->event_pending &&
46 event_class->get_send_mask() & ef->receive_mask) {
47 return true;
50 return false;
53 static unsigned int get_host_send_mask(SCLPEventFacility *ef)
55 unsigned int mask;
56 BusChild *kid;
57 SCLPEventClass *child;
59 mask = 0;
61 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
62 DeviceState *qdev = kid->child;
63 child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev);
64 mask |= child->get_send_mask();
66 return mask;
69 static unsigned int get_host_receive_mask(SCLPEventFacility *ef)
71 unsigned int mask;
72 BusChild *kid;
73 SCLPEventClass *child;
75 mask = 0;
77 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
78 DeviceState *qdev = kid->child;
79 child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev);
80 mask |= child->get_receive_mask();
82 return mask;
85 static uint16_t write_event_length_check(SCCB *sccb)
87 int slen;
88 unsigned elen = 0;
89 EventBufferHeader *event;
90 WriteEventData *wed = (WriteEventData *) sccb;
92 event = (EventBufferHeader *) &wed->ebh;
93 for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) {
94 elen = be16_to_cpu(event->length);
95 if (elen < sizeof(*event) || elen > slen) {
96 return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR;
98 event = (void *) event + elen;
100 if (slen) {
101 return SCLP_RC_INCONSISTENT_LENGTHS;
103 return SCLP_RC_NORMAL_COMPLETION;
106 static uint16_t handle_write_event_buf(SCLPEventFacility *ef,
107 EventBufferHeader *event_buf, SCCB *sccb)
109 uint16_t rc;
110 BusChild *kid;
111 SCLPEvent *event;
112 SCLPEventClass *ec;
114 rc = SCLP_RC_INVALID_FUNCTION;
116 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
117 DeviceState *qdev = kid->child;
118 event = (SCLPEvent *) qdev;
119 ec = SCLP_EVENT_GET_CLASS(event);
121 if (ec->write_event_data &&
122 ec->can_handle_event(event_buf->type)) {
123 rc = ec->write_event_data(event, event_buf);
124 break;
127 return rc;
130 static uint16_t handle_sccb_write_events(SCLPEventFacility *ef, SCCB *sccb)
132 uint16_t rc;
133 int slen;
134 unsigned elen = 0;
135 EventBufferHeader *event_buf;
136 WriteEventData *wed = (WriteEventData *) sccb;
138 event_buf = &wed->ebh;
139 rc = SCLP_RC_NORMAL_COMPLETION;
141 /* loop over all contained event buffers */
142 for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) {
143 elen = be16_to_cpu(event_buf->length);
145 /* in case of a previous error mark all trailing buffers
146 * as not accepted */
147 if (rc != SCLP_RC_NORMAL_COMPLETION) {
148 event_buf->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
149 } else {
150 rc = handle_write_event_buf(ef, event_buf, sccb);
152 event_buf = (void *) event_buf + elen;
154 return rc;
157 static void write_event_data(SCLPEventFacility *ef, SCCB *sccb)
159 if (sccb->h.function_code != SCLP_FC_NORMAL_WRITE) {
160 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
161 goto out;
163 if (be16_to_cpu(sccb->h.length) < 8) {
164 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
165 goto out;
167 /* first do a sanity check of the write events */
168 sccb->h.response_code = cpu_to_be16(write_event_length_check(sccb));
170 /* if no early error, then execute */
171 if (sccb->h.response_code == be16_to_cpu(SCLP_RC_NORMAL_COMPLETION)) {
172 sccb->h.response_code =
173 cpu_to_be16(handle_sccb_write_events(ef, sccb));
176 out:
177 return;
180 static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb,
181 unsigned int mask)
183 uint16_t rc;
184 int slen;
185 unsigned elen;
186 BusChild *kid;
187 SCLPEvent *event;
188 SCLPEventClass *ec;
189 EventBufferHeader *event_buf;
190 ReadEventData *red = (ReadEventData *) sccb;
192 event_buf = &red->ebh;
193 event_buf->length = 0;
194 slen = sizeof(sccb->data);
196 rc = SCLP_RC_NO_EVENT_BUFFERS_STORED;
198 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) {
199 DeviceState *qdev = kid->child;
200 event = (SCLPEvent *) qdev;
201 ec = SCLP_EVENT_GET_CLASS(event);
203 if (mask & ec->get_send_mask()) {
204 if (ec->read_event_data(event, event_buf, &slen)) {
205 elen = be16_to_cpu(event_buf->length);
206 event_buf = (EventBufferHeader *) ((char *)event_buf + elen);
207 rc = SCLP_RC_NORMAL_COMPLETION;
212 if (sccb->h.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE) {
213 /* architecture suggests to reset variable-length-response bit */
214 sccb->h.control_mask[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE;
215 /* with a new length value */
216 sccb->h.length = cpu_to_be16(SCCB_SIZE - slen);
218 return rc;
221 static void read_event_data(SCLPEventFacility *ef, SCCB *sccb)
223 unsigned int sclp_active_selection_mask;
224 unsigned int sclp_cp_receive_mask;
226 ReadEventData *red = (ReadEventData *) sccb;
228 if (be16_to_cpu(sccb->h.length) != SCCB_SIZE) {
229 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
230 goto out;
233 sclp_cp_receive_mask = ef->receive_mask;
235 /* get active selection mask */
236 switch (sccb->h.function_code) {
237 case SCLP_UNCONDITIONAL_READ:
238 sclp_active_selection_mask = sclp_cp_receive_mask;
239 break;
240 case SCLP_SELECTIVE_READ:
241 sclp_active_selection_mask = be32_to_cpu(red->mask);
242 if (!sclp_cp_receive_mask ||
243 (sclp_active_selection_mask & ~sclp_cp_receive_mask)) {
244 sccb->h.response_code =
245 cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK);
246 goto out;
248 break;
249 default:
250 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
251 goto out;
253 sccb->h.response_code = cpu_to_be16(
254 handle_sccb_read_events(ef, sccb, sclp_active_selection_mask));
256 out:
257 return;
260 static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb)
262 WriteEventMask *we_mask = (WriteEventMask *) sccb;
264 /* Attention: We assume that Linux uses 4-byte masks, what it actually
265 does. Architecture allows for masks of variable size, though */
266 if (be16_to_cpu(we_mask->mask_length) != 4) {
267 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH);
268 goto out;
271 /* keep track of the guest's capability masks */
272 ef->receive_mask = be32_to_cpu(we_mask->cp_receive_mask);
274 /* return the SCLP's capability masks to the guest */
275 we_mask->send_mask = cpu_to_be32(get_host_send_mask(ef));
276 we_mask->receive_mask = cpu_to_be32(get_host_receive_mask(ef));
278 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
280 out:
281 return;
284 /* qemu object creation and initialization functions */
286 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
288 static void sclp_events_bus_realize(BusState *bus, Error **errp)
290 BusChild *kid;
292 /* TODO: recursive realization has to be done in common code */
293 QTAILQ_FOREACH(kid, &bus->children, sibling) {
294 DeviceState *dev = kid->child;
296 object_property_set_bool(OBJECT(dev), true, "realized", errp);
297 if (*errp) {
298 return;
303 static void sclp_events_bus_class_init(ObjectClass *klass, void *data)
305 BusClass *bc = BUS_CLASS(klass);
307 bc->realize = sclp_events_bus_realize;
310 static const TypeInfo sclp_events_bus_info = {
311 .name = TYPE_SCLP_EVENTS_BUS,
312 .parent = TYPE_BUS,
313 .class_init = sclp_events_bus_class_init,
316 static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code)
318 switch (code & SCLP_CMD_CODE_MASK) {
319 case SCLP_CMD_READ_EVENT_DATA:
320 read_event_data(ef, sccb);
321 break;
322 case SCLP_CMD_WRITE_EVENT_DATA:
323 write_event_data(ef, sccb);
324 break;
325 case SCLP_CMD_WRITE_EVENT_MASK:
326 write_event_mask(ef, sccb);
327 break;
328 default:
329 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
330 break;
334 static const VMStateDescription vmstate_event_facility = {
335 .name = "vmstate-event-facility",
336 .version_id = 0,
337 .minimum_version_id = 0,
338 .fields = (VMStateField[]) {
339 VMSTATE_UINT32(receive_mask, SCLPEventFacility),
340 VMSTATE_END_OF_LIST()
344 static void init_event_facility(Object *obj)
346 SCLPEventFacility *event_facility = EVENT_FACILITY(obj);
347 DeviceState *sdev = DEVICE(obj);
348 Object *new;
350 /* Spawn a new bus for SCLP events */
351 qbus_create_inplace(&event_facility->sbus, sizeof(event_facility->sbus),
352 TYPE_SCLP_EVENTS_BUS, sdev, NULL);
354 new = object_new(TYPE_SCLP_QUIESCE);
355 object_property_add_child(obj, TYPE_SCLP_QUIESCE, new, NULL);
356 object_unref(new);
357 qdev_set_parent_bus(DEVICE(new), &event_facility->sbus.qbus);
359 new = object_new(TYPE_SCLP_CPU_HOTPLUG);
360 object_property_add_child(obj, TYPE_SCLP_CPU_HOTPLUG, new, NULL);
361 object_unref(new);
362 qdev_set_parent_bus(DEVICE(new), &event_facility->sbus.qbus);
363 /* the facility will automatically realize the devices via the bus */
366 static void reset_event_facility(DeviceState *dev)
368 SCLPEventFacility *sdev = EVENT_FACILITY(dev);
370 sdev->receive_mask = 0;
373 static void init_event_facility_class(ObjectClass *klass, void *data)
375 SysBusDeviceClass *sbdc = SYS_BUS_DEVICE_CLASS(klass);
376 DeviceClass *dc = DEVICE_CLASS(sbdc);
377 SCLPEventFacilityClass *k = EVENT_FACILITY_CLASS(dc);
379 dc->reset = reset_event_facility;
380 dc->vmsd = &vmstate_event_facility;
381 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
382 k->command_handler = command_handler;
383 k->event_pending = event_pending;
386 static const TypeInfo sclp_event_facility_info = {
387 .name = TYPE_SCLP_EVENT_FACILITY,
388 .parent = TYPE_SYS_BUS_DEVICE,
389 .instance_init = init_event_facility,
390 .instance_size = sizeof(SCLPEventFacility),
391 .class_init = init_event_facility_class,
392 .class_size = sizeof(SCLPEventFacilityClass),
395 static void event_realize(DeviceState *qdev, Error **errp)
397 SCLPEvent *event = SCLP_EVENT(qdev);
398 SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event);
400 if (child->init) {
401 int rc = child->init(event);
402 if (rc < 0) {
403 error_setg(errp, "SCLP event initialization failed.");
404 return;
409 static void event_unrealize(DeviceState *qdev, Error **errp)
411 SCLPEvent *event = SCLP_EVENT(qdev);
412 SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event);
413 if (child->exit) {
414 int rc = child->exit(event);
415 if (rc < 0) {
416 error_setg(errp, "SCLP event exit failed.");
417 return;
422 static void event_class_init(ObjectClass *klass, void *data)
424 DeviceClass *dc = DEVICE_CLASS(klass);
426 dc->bus_type = TYPE_SCLP_EVENTS_BUS;
427 dc->realize = event_realize;
428 dc->unrealize = event_unrealize;
431 static const TypeInfo sclp_event_type_info = {
432 .name = TYPE_SCLP_EVENT,
433 .parent = TYPE_DEVICE,
434 .instance_size = sizeof(SCLPEvent),
435 .class_init = event_class_init,
436 .class_size = sizeof(SCLPEventClass),
437 .abstract = true,
440 static void register_types(void)
442 type_register_static(&sclp_events_bus_info);
443 type_register_static(&sclp_event_facility_info);
444 type_register_static(&sclp_event_type_info);
447 type_init(register_types)