/* * IOport passthough based on the ("port E9") emulation debugcon.c * from https://gitlab.com/qemu-project/qemu/-/blob/master/hw/char/debugcon.c * * This allows a helper software outside QEMU to process read requests and writes to ioport * addresses, preserving the order. * * Copyright (c) 2003-2004 Fabrice Bellard * Copyright (c) 2008 Citrix Systems, Inc. * Copyright (c) Intel Corporation; author: H. Peter Anvin * Copyright (c) 2021-2024 Michael John Wensley * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /* ioport read testcase: length=10 from=0 to=9 08 00 00 00 00 00 00 00 00 01 umask 0; socat -x UNIX-LISTEN:/tmp/ioacpi,fork FILE:/dev/zero meaning read 1 octet from address 0 that is 8 octets long */ #include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/module.h" #include "chardev/char-fe.h" #include "hw/irq.h" #include "hw/isa/isa.h" #include "hw/qdev-properties.h" #include "hw/qdev-properties-system.h" #include "qom/object.h" #define TYPE_ISA_IOPORT_DEVICE "isa-ioport" OBJECT_DECLARE_SIMPLE_TYPE(ISADebugconState, ISA_IOPORT_DEVICE) /* #define DEBUG_IOPORT */ typedef struct DebugconState { MemoryRegion io; CharBackend chr; qemu_irq irq[ISA_NUM_IRQS]; } DebugconState; struct ISADebugconState { ISADevice parent_obj; uint32_t iobase; uint32_t iolength; DebugconState state; }; static void ioport_ioport_write(void *opaque, hwaddr addr, uint64_t val, unsigned width) { DebugconState *s = opaque; unsigned char address_size = sizeof(hwaddr); unsigned char data_size = width; #ifdef DEBUG_IOPORT printf(" [ioport: write addr=0x%04" HWADDR_PRIx " val=0x%02" PRIx64 "]\n", addr, val); #endif /* XXX this blocks entire thread. Rewrite to use * qemu_chr_fe_write and background I/O callbacks */ qemu_chr_fe_write_all(&s->chr, &address_size, 1); qemu_chr_fe_write_all(&s->chr, (const unsigned char *)&addr, address_size); qemu_chr_fe_write_all(&s->chr, &data_size, 1); qemu_chr_fe_write_all(&s->chr, (const unsigned char *)&val, data_size); } /* We are always interested in data from the helper, * if not requested by this module, * it is treated as a request to inject an interrupt */ static int ioport_chr_can_read(void *opaque) { /* DebugconState *s = opaque; */ return 64; } /* especially interested in invoking interrupt 9 acpi, 16550A driver invokes irq by number */ /* isa irq is from 0 to 15 "ISA_NUM_IRQS" */ /* tested with guest /proc/interrupts 9: */ static void ioport_chr_read(void *opaque, const uint8_t *buf, int size) { DebugconState *s = opaque; #ifdef DEBUG_IOPORT printf(" [ioport: enter ioport_chr_read, with %d]\n", size); #endif if (size) { if (buf[0] < ISA_NUM_IRQS) { qemu_irq_pulse(s->irq[buf[0]]); } } } static uint64_t ioport_ioport_read(void *opaque, hwaddr addr, unsigned width) { DebugconState *s = opaque; uint64_t val = 0xFFFFFFFFFFFFFFFF; unsigned char address_size = sizeof(hwaddr); unsigned char data_size = 0x80 | width; // unsigned char data_size = width; qemu_chr_fe_write_all(&s->chr, &address_size, 1); qemu_chr_fe_write_all(&s->chr, (const unsigned char *)&addr, address_size); qemu_chr_fe_write_all(&s->chr, &data_size, 1); qemu_chr_fe_read_all(&s->chr, (unsigned char *)&val, width); #ifdef DEBUG_IOPORT printf("ioport: read addr=0x%04" HWADDR_PRIx "\n", addr); #endif return val; } /* uint64 implies a maximum width of 8 octets */ static const MemoryRegionOps ioport_ops = { .read = ioport_ioport_read, .write = ioport_ioport_write, .valid.min_access_size = 1, .valid.max_access_size = 8, .endianness = DEVICE_LITTLE_ENDIAN, }; static void ioport_realize_core(DebugconState *s, Error **errp) { if (!qemu_chr_fe_backend_connected(&s->chr)) { error_setg(errp, "Can't create ioport device, empty char device"); return; } qemu_chr_fe_set_handlers(&s->chr, ioport_chr_can_read, ioport_chr_read, NULL, NULL, s, NULL, true); } static void ioport_isa_realizefn(DeviceState *dev, Error **errp) { ISADevice *d = ISA_DEVICE(dev); ISADebugconState *isa = ISA_IOPORT_DEVICE(dev); DebugconState *s = &isa->state; Error *err = NULL; for (int i = 0; i < ISA_NUM_IRQS; i++) { s->irq[i] = isa_get_irq(d, i); } ioport_realize_core(s, &err); if (err != NULL) { error_propagate(errp, err); return; } memory_region_init_io(&s->io, OBJECT(dev), &ioport_ops, s, TYPE_ISA_IOPORT_DEVICE, isa->iolength); memory_region_add_subregion(isa_address_space_io(d), isa->iobase, &s->io); } static Property ioport_isa_properties[] = { DEFINE_PROP_UINT32("iobase", ISADebugconState, iobase, 0xe9), DEFINE_PROP_UINT32("iolength", ISADebugconState, iolength, 1), DEFINE_PROP_CHR("chardev", ISADebugconState, state.chr), DEFINE_PROP_END_OF_LIST(), }; static void ioport_isa_class_initfn(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); dc->realize = ioport_isa_realizefn; device_class_set_props(dc, ioport_isa_properties); set_bit(DEVICE_CATEGORY_MISC, dc->categories); } static const TypeInfo ioport_isa_info = { .name = TYPE_ISA_IOPORT_DEVICE, .parent = TYPE_ISA_DEVICE, .instance_size = sizeof(ISADebugconState), .class_init = ioport_isa_class_initfn, }; static void ioport_register_types(void) { type_register_static(&ioport_isa_info); } type_init(ioport_register_types)