Skip to content

Commit

Permalink
Add write() hypercall
Browse files Browse the repository at this point in the history
  • Loading branch information
cw00h committed Jan 26, 2023
1 parent fc54872 commit 142c4f6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define PORT_DISPLAY 0xEC
#define PORT_OPEN 0xED
#define PORT_READ 0xEE
#define PORT_WRITE 0xEF

#define MAX_FD 256

Expand Down
29 changes: 25 additions & 4 deletions guest.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,30 @@ int read(int fd, void *buf, size_t count) {
args[0].buf = buf;
args[0].count = count;

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

int write(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 rw_args * to uint32_t
outb(PORT_WRITE, temp);
temp = inb(PORT_WRITE);
return temp;
}

void
__attribute__((noreturn))
__attribute__((section(".start")))
_start(void) {
const char *p;
int fd, fd1, fd2, fd3;
int fd, fd1, fd2, fd3, fd4;

/* Print Hello world */
display("----------------------------------------\n");
Expand Down Expand Up @@ -92,8 +104,7 @@ _start(void) {
display("\nContents of test.txt:\n");
display(buf);

// Clear buf
for(int i = 0; i < 100; i++) buf[i] = 0;
for(int i = 0; i < 100; i++) buf[i] = 0; // Clear buf

/* Test multiple opening & reading */
display("----------------------------------------\n");
Expand Down Expand Up @@ -126,7 +137,17 @@ _start(void) {

/* Test write() */
display("----------------------------------------\n");
display("Writing to test.txt - write returned ");
printVal(write(fd, "Writing someting\n", 17));
display("\n");

display("Contents of test.txt:\n");
// Reopen test.txt
open("test.txt");
fd4 = open("test.txt");
read(fd4, buf, sizeof(buf));
display(buf);
for(int i = 0; i < 100; i++) buf[i] = 0;

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

// Fetch filename from guest & open the file
filename = vm->mem + (uint64_t)*((char **)p);
fd = files[min_fd].fd = open(filename, O_RDONLY);
fd = files[min_fd].fd = open(filename, O_RDWR);
files[min_fd].in_use = 1;
}
else if(vcpu->kvm_run->io.direction == KVM_EXIT_IO_IN) {
Expand Down Expand Up @@ -224,7 +224,6 @@ void handle_read(struct vm *vm, struct vcpu *vcpu) {
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) {
Expand All @@ -234,6 +233,40 @@ void handle_read(struct vm *vm, struct vcpu *vcpu) {
}
}

void handle_write(struct vm *vm, struct vcpu *vcpu) {
static int ret = 0;
if(vcpu->kvm_run->io.direction == KVM_EXIT_IO_OUT) {
// Fetch arguments from guest
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 = write(fd, buf, count);
}
else if(vcpu->kvm_run->io.direction == KVM_EXIT_IO_IN) {
// Return the number of bytes written to the guest
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 @@ -283,6 +316,10 @@ int run_vm(struct vm *vm, struct vcpu *vcpu, size_t sz)
handle_read(vm, vcpu);
break;

case PORT_WRITE:
handle_write(vm, vcpu);
break;

default:
fprintf(stderr, "Unvalid hypercall\n");
exit(1);
Expand Down
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Test file for open, read, and write
Writing someting

0 comments on commit 142c4f6

Please sign in to comment.