2012-12-13 22:48:53 +08: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,
|
|
|
|
* git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
|
|
|
|
*/
|
|
|
|
|
2016-01-27 02:17:03 +08:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 22:40:22 +08:00
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "hw/qdev.h"
|
2013-02-06 00:06:20 +08:00
|
|
|
#include "hw/isa/isa.h"
|
2012-12-13 22:48:53 +08:00
|
|
|
|
|
|
|
#define IOMEM_LEN 0x10000
|
|
|
|
|
|
|
|
typedef struct PCTestdev {
|
|
|
|
ISADevice parent_obj;
|
|
|
|
|
|
|
|
MemoryRegion ioport;
|
2013-07-22 21:54:38 +08:00
|
|
|
MemoryRegion ioport_byte;
|
2012-12-13 22:48:53 +08:00
|
|
|
MemoryRegion flush;
|
|
|
|
MemoryRegion irq;
|
|
|
|
MemoryRegion iomem;
|
|
|
|
uint32_t ioport_data;
|
|
|
|
char iomem_buf[IOMEM_LEN];
|
|
|
|
} PCTestdev;
|
|
|
|
|
|
|
|
#define TYPE_TESTDEV "pc-testdev"
|
|
|
|
#define TESTDEV(obj) \
|
2013-01-07 19:59:43 +08:00
|
|
|
OBJECT_CHECK(PCTestdev, (obj), TYPE_TESTDEV)
|
2012-12-13 22:48:53 +08:00
|
|
|
|
|
|
|
static void test_irq_line(void *opaque, hwaddr addr, uint64_t data,
|
|
|
|
unsigned len)
|
|
|
|
{
|
2013-01-07 19:59:43 +08:00
|
|
|
PCTestdev *dev = opaque;
|
|
|
|
ISADevice *isa = ISA_DEVICE(dev);
|
2012-12-13 22:48:53 +08:00
|
|
|
|
|
|
|
qemu_set_irq(isa_get_irq(isa, addr), !!data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_irq_ops = {
|
|
|
|
.write = test_irq_line,
|
|
|
|
.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 19:59:43 +08:00
|
|
|
PCTestdev *dev = opaque;
|
2013-07-22 21:54:26 +08: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 22:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t test_ioport_read(void *opaque, hwaddr addr, unsigned len)
|
|
|
|
{
|
2013-01-07 19:59:43 +08:00
|
|
|
PCTestdev *dev = opaque;
|
2013-07-22 21:54:26 +08: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 22:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_ioport_ops = {
|
|
|
|
.read = test_ioport_read,
|
|
|
|
.write = test_ioport_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2013-07-22 21:54:38 +08: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,
|
|
|
|
};
|
|
|
|
|
2012-12-13 22:48:53 +08:00
|
|
|
static void test_flush_page(void *opaque, hwaddr addr, uint64_t data,
|
|
|
|
unsigned len)
|
|
|
|
{
|
|
|
|
hwaddr page = 4096;
|
|
|
|
void *a = cpu_physical_memory_map(data & ~0xffful, &page, 0);
|
|
|
|
|
|
|
|
/* We might not be able to get the full page, only mprotect what we actually
|
|
|
|
have mapped */
|
2013-01-05 16:33:43 +08:00
|
|
|
#if defined(CONFIG_POSIX)
|
2012-12-13 22:48:53 +08:00
|
|
|
mprotect(a, page, PROT_NONE);
|
|
|
|
mprotect(a, page, PROT_READ|PROT_WRITE);
|
2013-01-05 16:33:43 +08:00
|
|
|
#endif
|
2012-12-13 22:48:53 +08:00
|
|
|
cpu_physical_memory_unmap(a, page, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps test_flush_ops = {
|
|
|
|
.write = test_flush_page,
|
|
|
|
.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 19:59:43 +08:00
|
|
|
PCTestdev *dev = opaque;
|
2012-12-13 22:48:53 +08: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 19:59:43 +08:00
|
|
|
PCTestdev *dev = opaque;
|
2012-12-13 22:48:53 +08: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 09:37:14 +08:00
|
|
|
static void testdev_realizefn(DeviceState *d, Error **errp)
|
2012-12-13 22:48:53 +08:00
|
|
|
{
|
2012-11-25 09:37:14 +08:00
|
|
|
ISADevice *isa = ISA_DEVICE(d);
|
|
|
|
PCTestdev *dev = TESTDEV(d);
|
2012-12-13 22:48:53 +08:00
|
|
|
MemoryRegion *mem = isa_address_space(isa);
|
|
|
|
MemoryRegion *io = isa_address_space_io(isa);
|
|
|
|
|
2013-06-07 09:25:08 +08:00
|
|
|
memory_region_init_io(&dev->ioport, OBJECT(dev), &test_ioport_ops, dev,
|
2012-12-13 22:48:53 +08:00
|
|
|
"pc-testdev-ioport", 4);
|
2013-07-22 21:54:38 +08:00
|
|
|
memory_region_init_io(&dev->ioport_byte, OBJECT(dev),
|
|
|
|
&test_ioport_byte_ops, dev,
|
|
|
|
"pc-testdev-ioport-byte", 4);
|
2013-06-07 09:25:08 +08:00
|
|
|
memory_region_init_io(&dev->flush, OBJECT(dev), &test_flush_ops, dev,
|
2012-12-13 22:48:53 +08:00
|
|
|
"pc-testdev-flush-page", 4);
|
2013-06-07 09:25:08 +08:00
|
|
|
memory_region_init_io(&dev->irq, OBJECT(dev), &test_irq_ops, dev,
|
2012-12-13 22:48:53 +08:00
|
|
|
"pc-testdev-irq-line", 24);
|
2013-06-07 09:25:08 +08:00
|
|
|
memory_region_init_io(&dev->iomem, OBJECT(dev), &test_iomem_ops, dev,
|
2012-12-13 22:48:53 +08: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 21:54:38 +08:00
|
|
|
memory_region_add_subregion(io, 0xe8, &dev->ioport_byte);
|
2012-12-13 22:48:53 +08: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 09:37:14 +08:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-12-13 22:48:53 +08:00
|
|
|
|
2013-07-29 22:17:45 +08:00
|
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
2012-11-25 09:37:14 +08:00
|
|
|
dc->realize = testdev_realizefn;
|
2012-12-13 22:48:53 +08:00
|
|
|
}
|
|
|
|
|
2013-01-10 23:19:07 +08:00
|
|
|
static const TypeInfo testdev_info = {
|
2012-12-13 22:48:53 +08:00
|
|
|
.name = TYPE_TESTDEV,
|
|
|
|
.parent = TYPE_ISA_DEVICE,
|
2013-01-07 19:59:43 +08:00
|
|
|
.instance_size = sizeof(PCTestdev),
|
2012-12-13 22:48:53 +08:00
|
|
|
.class_init = testdev_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void testdev_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&testdev_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(testdev_register_types)
|