Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bus interface in the serial device #14

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all: $(BIN)
OBJS := \
vm.o \
serial.o \
bus.o \
pci.o \
virtio-pci.o \
virtq.o \
Expand Down
66 changes: 66 additions & 0 deletions src/bus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "bus.h"
#include <stddef.h>

static inline struct dev *bus_find_dev(struct bus *bus, uint64_t addr)
{
struct dev **p = &bus->head;

for (; *p; p = &(*p)->next) {
uint64_t start = (*p)->base;
uint64_t end = start + (*p)->len - 1;
if (addr >= start && addr <= end)
return *p;
}
return NULL;
}

void bus_handle_io(struct bus *bus,
void *data,
uint8_t is_write,
uint64_t addr,
uint8_t size)
{
struct dev *dev = bus_find_dev(bus, addr);

if (dev && addr + size - 1 <= dev->base + dev->len - 1) {
dev->do_io(dev->owner, data, is_write, addr - dev->base, size);
}
}

void bus_register_dev(struct bus *bus, struct dev *dev)
{
dev->next = bus->head;
bus->head = dev;
bus->dev_num++;
}

void bus_deregister_dev(struct bus *bus, struct dev *dev)
{
struct dev **p = &bus->head;

while (*p != dev && *p) {
p = &(*p)->next;
}

if (*p)
*p = (*p)->next;
}

void bus_init(struct bus *bus)
{
bus->dev_num = 0;
bus->head = NULL;
}

void dev_init(struct dev *dev,
uint64_t base,
uint64_t len,
void *owner,
dev_io_fn do_io)
{
dev->base = base;
dev->len = len;
dev->owner = owner;
dev->do_io = do_io;
dev->next = NULL;
}
38 changes: 38 additions & 0 deletions src/bus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <stdint.h>

struct dev;

typedef void (*dev_io_fn)(void *owner,
void *data,
uint8_t is_write,
uint64_t offset,
uint8_t size);

struct dev {
uint64_t base;
uint64_t len;
void *owner;
dev_io_fn do_io;
struct dev *next;
};

struct bus {
uint64_t dev_num;
struct dev *head;
};

void bus_register_dev(struct bus *bus, struct dev *dev);
void bus_deregister_dev(struct bus *bus, struct dev *dev);
void bus_handle_io(struct bus *bus,
void *data,
uint8_t is_write,
uint64_t addr,
uint8_t size);
void bus_init(struct bus *bus);
void dev_init(struct dev *dev,
uint64_t base,
uint64_t len,
void *owner,
dev_io_fn do_io);
64 changes: 0 additions & 64 deletions src/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,6 @@
#include "pci.h"
#include "utils.h"

static inline struct dev *bus_find_dev(struct bus *bus, uint64_t addr)
{
struct dev **p = &bus->head;

for (; *p; p = &(*p)->next) {
uint64_t start = (*p)->base;
uint64_t end = start + (*p)->len - 1;
if (addr >= start && addr <= end)
return *p;
}
return NULL;
}

void bus_handle_io(struct bus *bus,
void *data,
uint8_t is_write,
uint64_t addr,
uint8_t size)
{
struct dev *dev = bus_find_dev(bus, addr);

if (dev && addr + size - 1 <= dev->base + dev->len - 1) {
dev->do_io(dev->owner, data, is_write, addr - dev->base, size);
}
}

void bus_register_dev(struct bus *bus, struct dev *dev)
{
dev->next = bus->head;
bus->head = dev;
bus->dev_num++;
}

void bus_deregister_dev(struct bus *bus, struct dev *dev)
{
struct dev **p = &bus->head;

while (*p != dev && *p) {
p = &(*p)->next;
}

if (*p)
*p = (*p)->next;
}

void bus_init(struct bus *bus)
{
bus->dev_num = 0;
bus->head = NULL;
}

static inline void dev_init(struct dev *dev,
uint64_t base,
uint64_t len,
void *owner,
dev_io_fn do_io)
{
dev->base = base;
dev->len = len;
dev->owner = owner;
dev->do_io = do_io;
dev->next = NULL;
}

static void pci_address_io(void *owner,
void *data,
uint8_t is_write,
Expand Down
33 changes: 2 additions & 31 deletions src/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,7 @@
#include <linux/pci_regs.h>
#include <stdbool.h>
#include <stdint.h>

struct dev;

typedef void (*dev_io_fn)(void *owner,
void *data,
uint8_t is_write,
uint64_t offset,
uint8_t size);

struct dev {
uint64_t base;
uint64_t len;
void *owner;
dev_io_fn do_io;
struct dev *next;
};

struct bus {
uint64_t dev_num;
struct dev *head;
};

void bus_register_dev(struct bus *bus, struct dev *dev);
void bus_deregister_dev(struct bus *bus, struct dev *dev);
void bus_handle_io(struct bus *bus,
void *data,
uint8_t is_write,
uint64_t addr,
uint8_t size);
void bus_init(struct bus *bus);
#include "bus.h"

union pci_config_address {
struct {
Expand Down Expand Up @@ -84,4 +55,4 @@ void pci_dev_init(struct pci_dev *dev,
struct pci *pci,
struct bus *io_bus,
struct bus *mmio_bus);
void pci_init();
void pci_init();
30 changes: 18 additions & 12 deletions src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,23 @@ static void serial_out(serial_dev_t *s, uint16_t offset, void *data)
}
}


static void serial_handle_io(void *owner,
void *data,
uint8_t is_write,
uint64_t offset,
uint8_t size)
{
serial_dev_t *s = (serial_dev_t *) owner;
void (*serial_op)(serial_dev_t *, uint16_t, void *) =
is_write ? serial_out : serial_in;

serial_op(s, offset, data);
}

static void handler(int sig, siginfo_t *si, void *uc) {}

int serial_init(serial_dev_t *s)
int serial_init(serial_dev_t *s, struct bus *bus)
{
sigset_t mask;

Expand Down Expand Up @@ -226,18 +240,10 @@ int serial_init(serial_dev_t *s)
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
return throw_err("Failed to unblock timer signal");

return 0;
}

void serial_handle(serial_dev_t *s, struct kvm_run *r)
{
void *data = (uint8_t *) r + r->io.data_offset;
void (*serial_op)(serial_dev_t *, uint16_t, void *) =
(r->io.direction == KVM_EXIT_IO_OUT) ? serial_out : serial_in;
dev_init(&s->dev, COM1_PORT_BASE, COM1_PORT_SIZE, s, serial_handle_io);
bus_register_dev(bus, &s->dev);

uint32_t c = r->io.count;
for (uint16_t off = r->io.port - COM1_PORT_BASE; c--; data += r->io.size)
serial_op(s, off, data);
return 0;
}

void serial_exit(serial_dev_t *s)
Expand Down
6 changes: 3 additions & 3 deletions src/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

#include <linux/kvm.h>
#include <pthread.h>
#include "bus.h"

#define COM1_PORT_BASE 0x03f8
#define COM1_PORT_SIZE 8
#define COM1_PORT_END (COM1_PORT_BASE + COM1_PORT_SIZE)

typedef struct serial_dev serial_dev_t;

struct serial_dev {
void *priv;
pthread_t main_tid, worker_tid;
int infd; /* file descriptor for serial input */
struct dev dev;
};

void serial_console(serial_dev_t *s);
int serial_init(serial_dev_t *s);
void serial_handle(serial_dev_t *s, struct kvm_run *r);
int serial_init(serial_dev_t *s, struct bus *bus);
void serial_exit(serial_dev_t *s);
14 changes: 5 additions & 9 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ int vm_init(vm_t *v)

vm_init_regs(v);
vm_init_cpu_id(v);
if (serial_init(&v->serial))
return throw_err("Failed to init UART device");
bus_init(&v->io_bus);
bus_init(&v->mmio_bus);
pci_init(&v->pci, &v->io_bus);
if (serial_init(&v->serial, &v->io_bus))
return throw_err("Failed to init UART device");
virtio_blk_init(&v->virtio_blk_dev);
return 0;
}
Expand Down Expand Up @@ -223,13 +223,9 @@ void vm_handle_io(vm_t *v, struct kvm_run *run)
void *data = (void *) run + run->io.data_offset;
bool is_write = run->io.direction == KVM_EXIT_IO_OUT;

if (run->io.port >= COM1_PORT_BASE && run->io.port < COM1_PORT_END) {
serial_handle(&v->serial, run);
} else {
for (int i = 0; i < run->io.count; i++) {
bus_handle_io(&v->io_bus, data, is_write, addr, run->io.size);
addr += run->io.size;
}
for (int i = 0; i < run->io.count; i++) {
bus_handle_io(&v->io_bus, data, is_write, addr, run->io.size);
addr += run->io.size;
}
}

Expand Down