Skip to content

Commit

Permalink
Add read() hypercall
Browse files Browse the repository at this point in the history
  • Loading branch information
cw00h committed Jan 26, 2023
1 parent 67ddf8d commit dd68aba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
9 changes: 8 additions & 1 deletion defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
#define PORT_GETNUMEXITS 0xEB
#define PORT_DISPLAY 0xEC
#define PORT_OPEN 0xED
#define PORT_READ 0xEE

#define MAX_FD 256
#define MAX_FD 256

struct rw_args{
int fd;
void *buf;
size_t count;
};
26 changes: 26 additions & 0 deletions guest.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ int open(const char *filename) {
return temp;
}

int read(int fd, void *buf, size_t count) {
struct rw_args args[1];
args[0].fd = fd;
args[0].buf = buf;
args[0].count = count;

uint32_t temp = (char *)args - (char *)0; // Cast struct read_args * to uint32_t
outb(PORT_READ, temp);
temp = inb(PORT_READ);
return temp;
}

void
__attribute__((noreturn))
__attribute__((section(".start")))
Expand All @@ -41,30 +53,44 @@ _start(void) {
int fd;

/* Print Hello world */
display("----------------------------------------\n");
for (p = "Hello, world!\n"; *p; ++p)
outb(PORT_PRINT_CHAR, *p);

/* Test printVal() */
display("----------------------------------------\n");
printVal(1234);
outb(PORT_PRINT_CHAR, '\n');

/* Test getNumExits() */
display("----------------------------------------\n");
uint32_t numExits = getNumExits();
printVal(numExits);
outb(PORT_PRINT_CHAR, '\n');

/* Test display() */
display("----------------------------------------\n");
printVal(getNumExits());
display("\nHello world again!\n");
printVal(getNumExits());
display("\n");

/* Test open() */
display("----------------------------------------\n");
display("Opening test.txt\n");
fd = open("test.txt");
printVal(fd);
display("\n");

/* Test read() */
display("----------------------------------------\n");
char buf[100];
display("Reading test.txt - read returned ");
printVal(read(fd, buf, sizeof(buf)));

display("\nContents of test.txt:\n");
display(buf);

/* Halt after setting 0x400 & rax as 42 */
*(long *) 0x400 = 42;
for (;;)
Expand Down
40 changes: 38 additions & 2 deletions kvm-hello-world.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ void handle_open(struct vm *vm, struct vcpu *vcpu) {

filename = vm->mem + (uint64_t)*((char **)p);

files[min_fd].fd = open(filename, O_RDONLY);
fd = files[min_fd].fd = open(filename, O_RDONLY);
files[min_fd].in_use = 1;
fd = files[min_fd].fd;

printf("opened %s as fd %d. min_fd is %d\n", filename, files[min_fd].fd, min_fd);
}
Expand All @@ -201,6 +200,39 @@ void handle_open(struct vm *vm, struct vcpu *vcpu) {
}
}

void handle_read(struct vm *vm, struct vcpu *vcpu) {
static int ret = 0;
if(vcpu->kvm_run->io.direction == KVM_EXIT_IO_OUT) {
char *p = (char *)vcpu->kvm_run + vcpu->kvm_run->io.data_offset;
struct rw_args *args = (struct rw_args*)(vm->mem + (uint64_t)(*((char **)p)));

int fd = args->fd;
if(fd < 0 || fd >= MAX_FD) { // Invalid fd
ret = -1;
return;
}
for(int i = 0; i < MAX_FD; i++) {
if(files[i].fd == fd) {
if(!files[i].in_use) { // Unopened fd
ret = -1;
return;
}
break;
}
}

void *buf = vm->mem + (uint64_t)(args->buf);

size_t count = args->count;

ret = read(fd, buf, count);
}
else if(vcpu->kvm_run->io.direction == KVM_EXIT_IO_IN) {
char *p = (char *)vcpu->kvm_run + vcpu->kvm_run->io.data_offset;
*((int32_t *)p) = ret;
}
}

int run_vm(struct vm *vm, struct vcpu *vcpu, size_t sz)
{
struct kvm_regs regs;
Expand Down Expand Up @@ -246,6 +278,10 @@ int run_vm(struct vm *vm, struct vcpu *vcpu, size_t sz)
handle_open(vm, vcpu);
break;

case PORT_READ:
handle_read(vm, vcpu);
break;

default:
fprintf(stderr, "Unvalid hypercall\n");
exit(1);
Expand Down

0 comments on commit dd68aba

Please sign in to comment.