2012-12-13 18:48:53 +04:00
|
|
|
/*
|
|
|
|
* QEMU x86 ISA testdev
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Avi Kivity, Gerd Hoffmann, Marcelo Tosatti
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This device is used to test KVM features specific to the x86 port, such
|
|
|
|
* as emulation, power management, interrupt routing, among others. It's meant
|
|
|
|
* to be used like:
|
|
|
|
*
|
|
|
|
* qemu-system-x86_64 -device pc-testdev -serial stdio \
|
|
|
|
* -device isa-debug-exit,iobase=0xf4,iosize=0x4 \
|
|
|
|
* -kernel /home/lmr/Code/virt-test.git/kvm/unittests/msr.flat
|
|
|
|
*
|
|
|
|
* Where msr.flat is one of the KVM unittests, present on a separate repo,
|
2018-11-08 14:15:29 +03:00
|
|
|
* https://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
|
2012-12-13 18:48:53 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:03 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/isa/isa.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2012-12-13 18:48:53 +04:00
|
|
|
|
|
|
|
#define IOMEM_LEN 0x10000
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct PCTestdev {
|
2012-12-13 18:48:53 +04:00
|
|
|
ISADevice parent_obj;
|
|
|
|
|
|
|
|
MemoryRegion ioport;
|
2013-07-22 17:54:38 +04:00
|
|
|
MemoryRegion ioport_byte;
|
2012-12-13 18:48:53 +04:00
|
|
|
MemoryRegion flush;
|
|
|
|
MemoryRegion irq;
|
|
|
|
MemoryRegion iomem;
|
|
|
|
uint32_t ioport_data;
|
|
|
|
char iomem_buf[IOMEM_LEN];
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
|
|
|
typedef struct PCTestdev PCTestdev;
|
2012-12-13 18:48:53 +04:00
|
|
|
|
|
|
|
#define TYPE_TESTDEV "pc-testdev"
|
2020-09-01 00:07:33 +03:00
|
|
|
DECLARE_INSTANCE_CHECKER(PCTestdev, TESTDEV,
|
|
|
|
TYPE_TESTDEV)
|
2012-12-13 18:48:53 +04:00
|
|
|
|
2018-09-12 19:01:14 +03:00
|
|
|
static uint64_t test_irq_line_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_irq_line_write(void *opaque, hwaddr addr, uint64_t data,
|
2012-12-13 18:48:53 +04:00
|
|
|
unsigned len)
|
|
|
|
{
|
2013-01-07 15:59:43 +04:00
|
|
|
PCTestdev *dev = opaque;
|
|
|
|
ISADevice *isa = ISA_DEVICE(dev);
|
2012-12-13 18:48:53 +04:00
|
|
|
|
|
|
|
qemu_set_irq(isa_get_irq(isa, addr), !!data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_irq_ops = {
|
2018-09-12 19:01:14 +03:00
|
|
|
.read = test_irq_line_read,
|
|
|
|
.write = test_irq_line_write,
|
2012-12-13 18:48:53 +04:00
|
|
|
.valid.min_access_size = 1,
|
|
|
|
.valid.max_access_size = 1,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_ioport_write(void *opaque, hwaddr addr, uint64_t data,
|
|
|
|
unsigned len)
|
|
|
|
{
|
2013-01-07 15:59:43 +04:00
|
|
|
PCTestdev *dev = opaque;
|
2013-07-22 17:54:26 +04:00
|
|
|
int bits = len * 8;
|
|
|
|
int start_bit = (addr & 3) * 8;
|
|
|
|
uint32_t mask = ((uint32_t)-1 >> (32 - bits)) << start_bit;
|
|
|
|
dev->ioport_data &= ~mask;
|
|
|
|
dev->ioport_data |= data << start_bit;
|
2012-12-13 18:48:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t test_ioport_read(void *opaque, hwaddr addr, unsigned len)
|
|
|
|
{
|
2013-01-07 15:59:43 +04:00
|
|
|
PCTestdev *dev = opaque;
|
2013-07-22 17:54:26 +04:00
|
|
|
int bits = len * 8;
|
|
|
|
int start_bit = (addr & 3) * 8;
|
|
|
|
uint32_t mask = ((uint32_t)-1 >> (32 - bits)) << start_bit;
|
|
|
|
return (dev->ioport_data & mask) >> start_bit;
|
2012-12-13 18:48:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_ioport_ops = {
|
|
|
|
.read = test_ioport_read,
|
|
|
|
.write = test_ioport_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2013-07-22 17:54:38 +04:00
|
|
|
static const MemoryRegionOps test_ioport_byte_ops = {
|
|
|
|
.read = test_ioport_read,
|
|
|
|
.write = test_ioport_write,
|
|
|
|
.valid.min_access_size = 1,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
.impl.min_access_size = 1,
|
|
|
|
.impl.max_access_size = 1,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2018-09-12 19:01:14 +03:00
|
|
|
static uint64_t test_flush_page_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_flush_page_write(void *opaque, hwaddr addr, uint64_t data,
|
2012-12-13 18:48:53 +04:00
|
|
|
unsigned len)
|
|
|
|
{
|
|
|
|
hwaddr page = 4096;
|
2020-02-19 22:20:42 +03:00
|
|
|
void *a = cpu_physical_memory_map(data & ~0xffful, &page, false);
|
2012-12-13 18:48:53 +04:00
|
|
|
|
|
|
|
/* We might not be able to get the full page, only mprotect what we actually
|
|
|
|
have mapped */
|
2013-01-05 12:33:43 +04:00
|
|
|
#if defined(CONFIG_POSIX)
|
2012-12-13 18:48:53 +04:00
|
|
|
mprotect(a, page, PROT_NONE);
|
|
|
|
mprotect(a, page, PROT_READ|PROT_WRITE);
|
2013-01-05 12:33:43 +04:00
|
|
|
#endif
|
2012-12-13 18:48:53 +04:00
|
|
|
cpu_physical_memory_unmap(a, page, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_flush_ops = {
|
2018-09-12 19:01:14 +03:00
|
|
|
.read = test_flush_page_read,
|
|
|
|
.write = test_flush_page_write,
|
2012-12-13 18:48:53 +04:00
|
|
|
.valid.min_access_size = 4,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint64_t test_iomem_read(void *opaque, hwaddr addr, unsigned len)
|
|
|
|
{
|
2013-01-07 15:59:43 +04:00
|
|
|
PCTestdev *dev = opaque;
|
2012-12-13 18:48:53 +04:00
|
|
|
uint64_t ret = 0;
|
|
|
|
memcpy(&ret, &dev->iomem_buf[addr], len);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_iomem_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned len)
|
|
|
|
{
|
2013-01-07 15:59:43 +04:00
|
|
|
PCTestdev *dev = opaque;
|
2012-12-13 18:48:53 +04:00
|
|
|
memcpy(&dev->iomem_buf[addr], &val, len);
|
|
|
|
dev->iomem_buf[addr] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_iomem_ops = {
|
|
|
|
.read = test_iomem_read,
|
|
|
|
.write = test_iomem_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
static void testdev_realizefn(DeviceState *d, Error **errp)
|
2012-12-13 18:48:53 +04:00
|
|
|
{
|
2012-11-25 05:37:14 +04:00
|
|
|
ISADevice *isa = ISA_DEVICE(d);
|
|
|
|
PCTestdev *dev = TESTDEV(d);
|
2012-12-13 18:48:53 +04:00
|
|
|
MemoryRegion *mem = isa_address_space(isa);
|
|
|
|
MemoryRegion *io = isa_address_space_io(isa);
|
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&dev->ioport, OBJECT(dev), &test_ioport_ops, dev,
|
2012-12-13 18:48:53 +04:00
|
|
|
"pc-testdev-ioport", 4);
|
2013-07-22 17:54:38 +04:00
|
|
|
memory_region_init_io(&dev->ioport_byte, OBJECT(dev),
|
|
|
|
&test_ioport_byte_ops, dev,
|
|
|
|
"pc-testdev-ioport-byte", 4);
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&dev->flush, OBJECT(dev), &test_flush_ops, dev,
|
2012-12-13 18:48:53 +04:00
|
|
|
"pc-testdev-flush-page", 4);
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&dev->irq, OBJECT(dev), &test_irq_ops, dev,
|
2012-12-13 18:48:53 +04:00
|
|
|
"pc-testdev-irq-line", 24);
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&dev->iomem, OBJECT(dev), &test_iomem_ops, dev,
|
2012-12-13 18:48:53 +04:00
|
|
|
"pc-testdev-iomem", IOMEM_LEN);
|
|
|
|
|
|
|
|
memory_region_add_subregion(io, 0xe0, &dev->ioport);
|
|
|
|
memory_region_add_subregion(io, 0xe4, &dev->flush);
|
2013-07-22 17:54:38 +04:00
|
|
|
memory_region_add_subregion(io, 0xe8, &dev->ioport_byte);
|
2012-12-13 18:48:53 +04:00
|
|
|
memory_region_add_subregion(io, 0x2000, &dev->irq);
|
|
|
|
memory_region_add_subregion(mem, 0xff000000, &dev->iomem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testdev_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2012-11-25 05:37:14 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-12-13 18:48:53 +04:00
|
|
|
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
2012-11-25 05:37:14 +04:00
|
|
|
dc->realize = testdev_realizefn;
|
2012-12-13 18:48:53 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo testdev_info = {
|
2012-12-13 18:48:53 +04:00
|
|
|
.name = TYPE_TESTDEV,
|
|
|
|
.parent = TYPE_ISA_DEVICE,
|
2013-01-07 15:59:43 +04:00
|
|
|
.instance_size = sizeof(PCTestdev),
|
2012-12-13 18:48:53 +04:00
|
|
|
.class_init = testdev_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void testdev_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&testdev_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(testdev_register_types)
|