| 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 */ |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 */ |
| 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 | |
| 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 | |
| 286 | static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb) |
| 287 | { |
| 288 | WriteEventMask *we_mask = (WriteEventMask *) sccb; |
| 289 | uint16_t sccb_length = be16_to_cpu(sccb->h.length); |
| 290 | uint16_t mask_length = be16_to_cpu(we_mask->mask_length); |
| 291 | sccb_mask_t tmp_mask; |
| 292 | |
| 293 | if (!mask_length || mask_length > SCLP_EVENT_MASK_LEN_MAX) { |
| 294 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | if (sccb_length < sizeof(WriteEventMask) + 4 * mask_length) { |
| 299 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Note: We currently only support masks up to 8 byte length; |
| 305 | * the remainder is filled up with zeroes. Older Linux |
| 306 | * kernels use a 4 byte mask length, newer ones can use both |
| 307 | * 8 or 4 depending on what is available on the host. |
| 308 | */ |
| 309 | |
| 310 | /* keep track of the guest's capability masks */ |
| 311 | copy_mask((uint8_t *)&tmp_mask, WEM_CP_RECEIVE_MASK(we_mask, mask_length), |
| 312 | sizeof(tmp_mask), mask_length); |
| 313 | ef->receive_mask = be64_to_cpu(tmp_mask); |
| 314 | |
| 315 | /* return the SCLP's capability masks to the guest */ |
| 316 | tmp_mask = cpu_to_be64(get_host_receive_mask(ef)); |
| 317 | copy_mask(WEM_RECEIVE_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask, |
| 318 | mask_length, sizeof(tmp_mask)); |
| 319 | tmp_mask = cpu_to_be64(get_host_send_mask(ef)); |
| 320 | copy_mask(WEM_SEND_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask, |
| 321 | mask_length, sizeof(tmp_mask)); |
| 322 | |
| 323 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION); |
| 324 | ef->mask_length = mask_length; |
| 325 | } |
| 326 | |
| 327 | /* qemu object creation and initialization functions */ |
| 328 | |
| 329 | #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus" |
| 330 | |
| 331 | static const TypeInfo sclp_events_bus_info = { |
| 332 | .name = TYPE_SCLP_EVENTS_BUS, |
| 333 | .parent = TYPE_BUS, |
| 334 | }; |
| 335 | |
| 336 | static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code) |
| 337 | { |
| 338 | switch (code & SCLP_CMD_CODE_MASK) { |
| 339 | case SCLP_CMD_READ_EVENT_DATA: |
| 340 | read_event_data(ef, sccb); |
| 341 | break; |
| 342 | case SCLP_CMD_WRITE_EVENT_DATA: |
| 343 | write_event_data(ef, sccb); |
| 344 | break; |
| 345 | case SCLP_CMD_WRITE_EVENT_MASK: |
| 346 | write_event_mask(ef, sccb); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static bool vmstate_event_facility_mask64_needed(void *opaque) |
| 352 | { |
| 353 | SCLPEventFacility *ef = opaque; |
| 354 | |
| 355 | return (ef->receive_mask & 0xFFFFFFFF) != 0; |
| 356 | } |
| 357 | |
| 358 | static const VMStateDescription vmstate_event_facility_mask64 = { |
| 359 | .name = "vmstate-event-facility/mask64", |
| 360 | .version_id = 0, |
| 361 | .minimum_version_id = 0, |
| 362 | .needed = vmstate_event_facility_mask64_needed, |
| 363 | .fields = (const VMStateField[]) { |
| 364 | VMSTATE_UINT32(receive_mask_pieces[RECV_MASK_LOWER], SCLPEventFacility), |
| 365 | VMSTATE_END_OF_LIST() |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | static const VMStateDescription vmstate_event_facility_mask_length = { |
| 370 | .name = "vmstate-event-facility/mask_length", |
| 371 | .version_id = 0, |
| 372 | .minimum_version_id = 0, |
| 373 | .fields = (const VMStateField[]) { |
| 374 | VMSTATE_UINT16(mask_length, SCLPEventFacility), |
| 375 | VMSTATE_END_OF_LIST() |
| 376 | } |
| 377 | }; |
| 378 | |
| 379 | static const VMStateDescription vmstate_event_facility = { |
| 380 | .name = "vmstate-event-facility", |
| 381 | .version_id = 0, |
| 382 | .minimum_version_id = 0, |
| 383 | .fields = (const VMStateField[]) { |
| 384 | VMSTATE_UINT32(receive_mask_pieces[RECV_MASK_UPPER], SCLPEventFacility), |
| 385 | VMSTATE_END_OF_LIST() |
| 386 | }, |
| 387 | .subsections = (const VMStateDescription * const []) { |
| 388 | &vmstate_event_facility_mask64, |
| 389 | &vmstate_event_facility_mask_length, |
| 390 | NULL |
| 391 | } |
| 392 | }; |
| 393 | |
| 394 | static void init_event_facility(Object *obj) |
| 395 | { |
| 396 | SCLPEventFacility *event_facility = EVENT_FACILITY(obj); |
| 397 | DeviceState *sdev = DEVICE(obj); |
| 398 | |
| 399 | event_facility->mask_length = 4; |
| 400 | |
| 401 | /* Spawn a new bus for SCLP events */ |
| 402 | qbus_init(&event_facility->sbus, sizeof(event_facility->sbus), |
| 403 | TYPE_SCLP_EVENTS_BUS, sdev, NULL); |
| 404 | |
| 405 | object_initialize_child(obj, TYPE_SCLP_QUIESCE, |
| 406 | &event_facility->quiesce, |
| 407 | TYPE_SCLP_QUIESCE); |
| 408 | |
| 409 | object_initialize_child(obj, TYPE_SCLP_CPU_HOTPLUG, |
| 410 | &event_facility->cpu_hotplug, |
| 411 | TYPE_SCLP_CPU_HOTPLUG); |
| 412 | } |
| 413 | |
| 414 | static void realize_event_facility(DeviceState *dev, Error **errp) |
| 415 | { |
| 416 | SCLPEventFacility *event_facility = EVENT_FACILITY(dev); |
| 417 | |
| 418 | if (!qdev_realize(DEVICE(&event_facility->quiesce), |
| 419 | BUS(&event_facility->sbus), errp)) { |
| 420 | return; |
| 421 | } |
| 422 | if (!qdev_realize(DEVICE(&event_facility->cpu_hotplug), |
| 423 | BUS(&event_facility->sbus), errp)) { |
| 424 | qdev_unrealize(DEVICE(&event_facility->quiesce)); |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static void reset_event_facility(DeviceState *dev) |
| 430 | { |
| 431 | SCLPEventFacility *sdev = EVENT_FACILITY(dev); |
| 432 | |
| 433 | sdev->receive_mask = 0; |
| 434 | } |
| 435 | |
| 436 | static void init_event_facility_class(ObjectClass *klass, const void *data) |
| 437 | { |
| 438 | SysBusDeviceClass *sbdc = SYS_BUS_DEVICE_CLASS(klass); |
| 439 | DeviceClass *dc = DEVICE_CLASS(sbdc); |
| 440 | SCLPEventFacilityClass *k = EVENT_FACILITY_CLASS(dc); |
| 441 | |
| 442 | dc->realize = realize_event_facility; |
| 443 | device_class_set_legacy_reset(dc, reset_event_facility); |
| 444 | dc->vmsd = &vmstate_event_facility; |
| 445 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 446 | k->command_handler = command_handler; |
| 447 | k->event_pending = event_pending; |
| 448 | } |
| 449 | |
| 450 | static const TypeInfo sclp_event_facility_info = { |
| 451 | .name = TYPE_SCLP_EVENT_FACILITY, |
| 452 | .parent = TYPE_SYS_BUS_DEVICE, |
| 453 | .instance_init = init_event_facility, |
| 454 | .instance_size = sizeof(SCLPEventFacility), |
| 455 | .class_init = init_event_facility_class, |
| 456 | .class_size = sizeof(SCLPEventFacilityClass), |
| 457 | }; |
| 458 | |
| 459 | static void event_realize(DeviceState *qdev, Error **errp) |
| 460 | { |
| 461 | SCLPEvent *event = SCLP_EVENT(qdev); |
| 462 | SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event); |
| 463 | |
| 464 | if (child->init) { |
| 465 | int rc = child->init(event); |
| 466 | if (rc < 0) { |
| 467 | error_setg(errp, "SCLP event initialization failed."); |
| 468 | return; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static void event_class_init(ObjectClass *klass, const void *data) |
| 474 | { |
| 475 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 476 | |
| 477 | dc->bus_type = TYPE_SCLP_EVENTS_BUS; |
| 478 | dc->realize = event_realize; |
| 479 | } |
| 480 | |
| 481 | static const TypeInfo sclp_event_type_info = { |
| 482 | .name = TYPE_SCLP_EVENT, |
| 483 | .parent = TYPE_DEVICE, |
| 484 | .instance_size = sizeof(SCLPEvent), |
| 485 | .class_init = event_class_init, |
| 486 | .class_size = sizeof(SCLPEventClass), |
| 487 | .abstract = true, |
| 488 | }; |
| 489 | |
| 490 | static void register_types(void) |
| 491 | { |
| 492 | type_register_static(&sclp_events_bus_info); |
| 493 | type_register_static(&sclp_event_facility_info); |
| 494 | type_register_static(&sclp_event_type_info); |
| 495 | } |
| 496 | |
| 497 | type_init(register_types) |
| 498 | |
| 499 | BusState *sclp_get_event_facility_bus(SCLPEventFacility *ef) |
| 500 | { |
| 501 | return BUS(&ef->sbus); |
| 502 | } |