This looks _a_lot_ like what we're doing for the SPUs in the cell processor,
except that we're using different calls into the kernel. Have you looked
into what we have implemented there? The code is in
arch/powerpc/platforms/cell/spufs. I think it would be a good abstraction
to use for you as well, maybe we could even move to a common infrastructure,
as I have heard from a few other projects that want to do similar things.
The main differences to your interface are:
- A file system is used instead of a character device
- Directories, not open file descriptors represent contexts
- Two new syscalls were introduced (spu_create/spu_run)
- instead of ioctls, files represent different bits of information,
you can read/write, poll or mmap them.
Your example above could translate to something like:
int kvm_fd = kvm_create("/kvm/my_vcpu")
int mem_fd = openat(kvm_fd, "mem", O_RDWR);
void *mem = mmap(mem_fd, ...); // main memory
void *fbmem = mmap(mem_fd, ...); // frame buffer memory
int regs_fd = openat(kvm_fd, "regs", O_RDWR);
int irq_fd = openat(kvm_fd, "regs", O_WRONLY);
if (debugger) {
int fd = openat(fvm_fd, "debug", O_WRONLY);
write(fd, "1", 1);
close(fd);
}
while (1) {
int exit_reason = kvm_run(kvm_fd, &kvm_descriptor);
switch (exit reason) {
handle mmio, I/O etc. might call
write(irq_fd, &interrupt_packet, sizeof (interrupt_packet));
pread(regs_fd, &rax, sizeof rax, KVM_REG_RAX);
}