Skip to content

Commit

Permalink
virtio: Small refactor for virtio con
Browse files Browse the repository at this point in the history
refactor the interfaces between different virtio console
layers for a better OO design.

Signed-off-by: Jingyao Zhou <[email protected]>
  • Loading branch information
abrandnewusername committed Jan 16, 2023
1 parent 1bfd64c commit 813be71
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
/***
* @struct virtio_con
* Virtio Console Driver Interface
* @param {unsigned int} iobase IO Port base for virtio con device
* @param {virtio_emul_t *} emul Virtio console emulation interface: VMM <-> Guest
* @param {struct console_passthrough} emul_driver_funcs Virtio console emulation functions: VMM <-> Guest
* @param {ps_io_ops_t} ioops Platform support io ops datastructure
* @param {unsigned int} iobase IO Port base for virtio con device
* @param {virtio_emul_t *} emul Virtio console emulation interface: VMM <-> Guest
* @param {struct eth_driver *} emul_driver Backend console driver interface: VMM <-> console driver
* @param {struct virtio_console_passthrough} emul_driver_funcs Virtio console emulation functions: VMM <-> Guest
* @param {ps_io_ops_t} ioops Platform support io ops datastructure
*/
typedef struct virtio_con {
unsigned int iobase;
virtio_emul_t *emul;
struct console_passthrough emul_driver_funcs;
struct virtio_console_driver *emul_driver;
struct virtio_console_passthrough emul_driver_funcs;
ps_io_ops_t ioops;
} virtio_con_t;

Expand All @@ -55,4 +57,4 @@ virtio_con_t *common_make_virtio_con(vm_t *vm,
ioport_type_t port_type,
unsigned int interrupt_pin,
unsigned int interrupt_line,
struct console_passthrough backend);
struct virtio_console_passthrough backend);
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,44 @@

#pragma once

typedef void (*console_handle_irq_fn_t)(void *cookie);
typedef struct virtio_emul virtio_emul_t;

typedef void (*console_putchar_fn_t)(int port, char c);

struct console_passthrough {
console_handle_irq_fn_t handleIRQ;
console_putchar_fn_t putchar;
void *console_data;
/***
* @struct virtio_console_callbacks
* Callback functions provided by the emul layer of virtio console
*
* @param backend_putchar putchar
*/
typedef struct virtio_console_callbacks {
void (*emul_putchar)(int port, virtio_emul_t *con, char *buffer, uint32_t head, uint32_t tail);
} virtio_console_callbacks_t;

/***
* @struct virtio_console_passthrough
* Virtion console backend layer interface
*
* @param handleIRQ handle IRQ
* @param backend_putchar putchar
* @param console_data data specified by the backend
*/
typedef struct virtio_console_passthrough {
void (*handleIRQ)(void *cookie);
console_putchar_fn_t backend_putchar;
void *console_data;
} virtio_console_passthrough_t;

/***
* @struct virtio_console_driver
* Structure to hold the interface for a virtio console driver
*
* @param backend_fn backend layer interface
* @param emul_cb emul layer interface
*/
struct virtio_console_driver {
virtio_console_passthrough_t backend_fn;
virtio_console_callbacks_t emul_cb;
};

typedef int (*console_driver_init)(struct console_passthrough *driver, ps_io_ops_t io_ops, void *config);
typedef int (*console_driver_init)(struct virtio_console_driver *driver, ps_io_ops_t io_ops, void *config);
7 changes: 4 additions & 3 deletions libsel4vmmplatsupport/src/drivers/virtio_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ static int virtio_con_io_out(void *cookie, unsigned int port_no, unsigned int si
return 0;
}

static int emul_con_driver_init(struct console_passthrough *driver, ps_io_ops_t io_ops, void *config)
static int emul_con_driver_init(struct virtio_console_driver *driver, ps_io_ops_t io_ops, void *config)
{
virtio_con_t *con = (virtio_con_t *)config;
*driver = con->emul_driver_funcs;
driver->backend_fn = con->emul_driver_funcs;
con->emul_driver = driver;
return 0;
}

Expand Down Expand Up @@ -88,7 +89,7 @@ static vmm_pci_entry_t vmm_virtio_console_pci_bar(unsigned int iobase,

virtio_con_t *common_make_virtio_con(vm_t *vm, vmm_pci_space_t *pci, vmm_io_port_list_t *ioport,
ioport_range_t ioport_range, ioport_type_t port_type, unsigned int interrupt_pin, unsigned int interrupt_line,
struct console_passthrough backend)
struct virtio_console_passthrough backend)
{
int err = ps_new_stdlib_malloc_ops(&ops.malloc_ops);
ZF_LOGF_IF(err, "Failed to get malloc ops");
Expand Down
22 changes: 14 additions & 8 deletions libsel4vmmplatsupport/src/drivers/virtio_console_emul.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
static char buf[VUART_BUFLEN];

typedef struct console_virtio_emul_internal {
struct console_passthrough driver;
struct virtio_console_driver driver;
/* counter for console ports */
unsigned int con_count;
/* what queue did data come through? */
Expand Down Expand Up @@ -96,7 +96,7 @@ static void emul_con_rx_complete(virtio_emul_t *emul, int queue, char *buf, uint
/* record that we've used this descriptor chain now */
virtq->last_idx[queue]++;
/* notify the guest that there is something in its used ring */
con->driver.handleIRQ(con->driver.console_data);
con->driver.backend_fn.handleIRQ(con->driver.backend_fn.console_data);
}
}

Expand All @@ -120,6 +120,10 @@ void virtio_console_putchar(int port, virtio_emul_t *emul, char *buffer, uint32_
emul_con_rx_complete(emul, vq_num, buffer, head, tail);
}

static virtio_console_callbacks_t emul_callbacks = {
.emul_putchar = virtio_console_putchar
};

/* Write to port attached to queue number */
static void port_write(virtio_emul_t *emul, int queue, char *buffer, unsigned int len)
{
Expand All @@ -134,7 +138,7 @@ static void port_write(virtio_emul_t *emul, int queue, char *buffer, unsigned in

/* forward it */
for (int i = 0; i < len; i++) {
con->driver.putchar(port, buffer[i]);
con->driver.backend_fn.backend_putchar(port, buffer[i]);
}
}

Expand Down Expand Up @@ -232,7 +236,7 @@ static void emul_con_notify_tx(virtio_emul_t *emul)
idx++;
struct vring_used_elem used_elem = {desc_head, 0};
ring_used_add(emul, &virtq->vring[con->queue_num], used_elem);
con->driver.handleIRQ(con->driver.console_data);
con->driver.backend_fn.handleIRQ(con->driver.backend_fn.console_data);
}
/* update which parts of the ring we have processed */
virtq->last_idx[con->queue_num] = idx;
Expand Down Expand Up @@ -299,16 +303,18 @@ void *console_virtio_emul_init(virtio_emul_t *emul, ps_io_ops_t io_ops, console_
goto error;
}

emul->device_io_in = console_device_emul_io_in;
emul->device_io_out = console_device_emul_io_out;
emul->notify = emul_con_notify_tx;
internal->con_count = 0;
internal->driver.emul_cb = emul_callbacks;

err = driver(&internal->driver, io_ops, config);
if (err) {
ZF_LOGE("Failed to initialize driver");
goto error;
}

emul->device_io_in = console_device_emul_io_in;
emul->device_io_out = console_device_emul_io_out;
emul->notify = emul_con_notify_tx;
internal->con_count = 0;
return (void *)internal;
error:
if (emul) {
Expand Down

0 comments on commit 813be71

Please sign in to comment.