slkern/vmrd.c

66 lines
1.3 KiB
C

// This is NEW CODE, a VM ram/disk access driver for use in the VM
// This driver is intended to be very simple to read/write blocks.
#include "types.h"
#include "param.h"
#include "riscv.h"
#include "defs.h"
#include "vmrd.h"
sched_spinlock_t vmrd_lock;
// This doesn't really belong here but is used to enable/disable debug
// tracing in the VM.
void vmrd_settracing(int onoroff) {
if (onoroff) {
vmrd_action = 't';
} else {
vmrd_action = 'e';
}
return;
}
// Returns a major version number >= 1 if the virtual ram/disk device is
// present and 0 otherwise.
int vmrd_present() {
if (vmrd_magic == VMRD_MAGIC) {
return 1; // Only 1 version exists for now
} else {
return 0;
}
}
// Initialises vmrd, returning a non-zero value on failure (e.g. if not present)
int vmrd_init() {
if (!vmrd_present()) {
return -1;
}
initlock(&vmrd_lock, "VMRD");
return 0;
}
// Performs a read/write of a block.
int vmrd_rw(diskio_buffer_t* buffer, int writing) {
acquire(&vmrd_lock);
vmrd_blksize = (unsigned int) buffer->owner->blocksize;
vmrd_memaddr = (unsigned long) buffer->data;
vmrd_blkaddr = buffer->blocknumber;
vmrd_action = writing ? 'w' : 'r';
while (vmrd_action != 's') {
if (vmrd_action == 'f') {
release(&vmrd_lock);
return -1;
}
}
release(&vmrd_lock);
return 0;
}