From: "Lluís Vilanova" <vilanova@ac.upc.edu>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] Help writing a trivial device
Date: Mon, 26 Sep 2011 00:23:21 +0200 [thread overview]
Message-ID: <87litcs1ly.fsf@ginnungagap.bsc.es> (raw)
Hi. I started writing a trivial device on QEMU that should get called on every
read and write on the memory it provides.
The problems are that:
1) Cannot start QEMU with KVM when the device is enabled:
kvm_set_phys_mem: error registering slot: Invalid argument
2) The driver never gets called on a read/write to its memory
I'm sure this is due to some error in my code, but I'm clueless as to what it
could be.
The testing system is a Linux 2.6.32, with this:
int fd = open("/sys/devices/pci0000:00/000000:00:004.00/resource0", O_RDWR);
void *addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
printf("-> %ld\n", *(uint64_t*)addr);
The device is something like (some code changed for brevity):
typedef struct State
{
PCIDevice dev;
MemoryRegion control;
} State;
static uint64_t control_io_read(void *opaque, target_phys_addr_t addr, unsigned size)
{
return 0xcafe;
}
static void control_io_write(void *opaque, target_phys_addr_t addr, uint64_t data, unsigned size)
{
/* do something */
}
static const MemoryRegionOps control_ops = {
.read = control_io_read,
.write = control_io_write,
.endianness = DEVICE_NATIVE_ENDIAN,
.valid = {
.min_access_size = 8,
.max_access_size = 8,
},
};
static int init(PCIDevice *dev)
{
State *s = DO_UPCAST(State, dev, dev);
memory_region_init_io(&s->control, &control_ops, s, "backdoor.control",
TARGET_PAGE_SIZE);
pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->control);
return 0;
}
static int fini(PCIDevice *dev)
{
State *s = DO_UPCAST(State, dev, dev);
memory_region_destroy(&s->control);
return 0;
}
static PCIDeviceInfo info = {
.qdev.name = "foo",
.qdev.size = sizeof(State),
.init = init,
.exit = fini,
.vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
.device_id = 0x1005,
.class_id = PCI_CLASS_MEMORY_RAM,
};
static void register_devices(void)
{
pci_qdev_register(&info);
}
device_init(register_devices)
Is there something blatantly wrong in the device code?
Thanks a lot,
Lluis
--
"And it's much the same thing with knowledge, for whenever you learn
something new, the whole world becomes that much richer."
-- The Princess of Pure Reason, as told by Norton Juster in The Phantom
Tollbooth
next reply other threads:[~2011-09-25 22:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-25 22:23 Lluís Vilanova [this message]
2011-09-26 7:51 ` [Qemu-devel] Help writing a trivial device Stefan Hajnoczi
2011-09-26 12:54 ` Lluís Vilanova
2011-09-26 18:03 ` Lluís Vilanova
2011-09-27 7:08 ` Stefan Hajnoczi
2011-09-27 13:03 ` Lluís Vilanova
2011-09-27 6:15 ` Zhi Yong Wu
2011-09-27 6:38 ` Avi Kivity
2011-09-27 6:34 ` Zhi Yong Wu
2011-09-27 13:05 ` Lluís Vilanova
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87litcs1ly.fsf@ginnungagap.bsc.es \
--to=vilanova@ac.upc.edu \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).