Skip to content

Commit

Permalink
it gets worse
Browse files Browse the repository at this point in the history
  • Loading branch information
tombl committed Dec 7, 2024
1 parent 982c619 commit ca54a3d
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 140 deletions.
33 changes: 14 additions & 19 deletions arch/wasm/drivers/virtio_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ void wasm_import(virtio, get_config)(u32 id, unsigned offset, void *buf,
unsigned len);
void wasm_import(virtio, set_config)(u32 id, unsigned offset, const void *buf,
unsigned len);

int wasm_import(virtio, get_features)(u32 id, u64 *features);
int wasm_import(virtio, set_features)(u32 id, u64 features);
int wasm_import(virtio, set_vring_enable)(u32 id, u32 index, bool enable);
int wasm_import(virtio, set_vring_num)(u32 id, u32 index, u32 num);
int wasm_import(virtio, set_vring_addr)(u32 id, u32 index, dma_addr_t desc,
dma_addr_t used, dma_addr_t avail);

int wasm_import(virtio, configure_interrupt)(u32 id, u32 irq, bool *is_config,
bool *is_vring);
bool *is_vring);

int wasm_import(virtio, enable_vring)(u32 id, u32 index, u32 size,
dma_addr_t desc, dma_addr_t used,
dma_addr_t avail);
int wasm_import(virtio, disable_vring)(u32 id, u32 index);

int wasm_import(virtio, notify)(u32 id, u32 index);

#define to_virtio_wasm_device(_plat_dev) \
Expand Down Expand Up @@ -104,8 +108,7 @@ static void vw_del_vqs(struct virtio_device *vdev)

list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
vring_del_virtqueue(vq);
WARN(wasm_virtio_set_vring_enable(vw_dev->host_id, vq->index,
false) < 0,
WARN(wasm_virtio_disable_vring(vw_dev->host_id, vq->index) < 0,
"failed to disable vq %i[%i]\n", vw_dev->host_id,
vq->index);
}
Expand Down Expand Up @@ -153,18 +156,10 @@ static struct virtqueue *vw_setup_vq(struct virtio_device *vdev, unsigned index,
vq->num_max = num;
num = virtqueue_get_vring_size(vq);

rc = wasm_virtio_set_vring_num(vw_dev->host_id, vq->index, num);
if (rc)
goto error;

rc = wasm_virtio_set_vring_addr(vw_dev->host_id, vq->index,
virtqueue_get_desc_addr(vq),
virtqueue_get_used_addr(vq),
virtqueue_get_avail_addr(vq));
if (rc)
goto error;

rc = wasm_virtio_set_vring_enable(vw_dev->host_id, vq->index, true);
rc = wasm_virtio_enable_vring(vw_dev->host_id, vq->index, num,
virtqueue_get_desc_addr(vq),
virtqueue_get_used_addr(vq),
virtqueue_get_avail_addr(vq));
if (rc)
goto error;

Expand Down
63 changes: 43 additions & 20 deletions tools/wasm/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@ export type Unwrap<T> = T extends Type<infer U> ? U : never;
export function Struct<T extends object>(
layout: { [K in keyof T]: Type<T[K]> },
) {
let size = 0;

const TheStruct = class {
_dv: DataView;
#dv: DataView;
constructor(dv: DataView) {
this._dv = dv;
this.#dv = dv;
}
static {
for (
const [key, type] of Object.entries(
layout as Record<PropertyKey, Type<unknown>>,
)
) {
const offset = size;
Object.defineProperty(this.prototype, key, {
get() {
return type.get(this.#dv, offset);
},
set(value) {
type.set(this.#dv, offset, value);
},
});
size += type.size;
}
}
} as { new (dv: DataView): T };

let size = 0;
for (
const [key, type] of Object.entries(
layout as Record<PropertyKey, Type<unknown>>,
)
) {
const offset = size;
Object.defineProperty(TheStruct.prototype, key, {
get() {
return type.get(this._dv, offset);
},
set(value) {
type.set(this._dv, offset, value);
},
});
size += type.size;
}

const type: Type<T> = {
get(dv, offset) {
if (offset !== 0) dv = new DataView(dv.buffer, dv.byteOffset + offset);
Expand All @@ -48,6 +50,27 @@ export function Struct<T extends object>(
return Object.assign(TheStruct, type);
}

export function FixedArray<T>(
{ get, set, size }: Type<T>,
length: number,
): Type<T[]> {
return {
get(dv, offset) {
const arr = Array<T>(length);
for (let i = 0; i < length; i++) {
arr[i] = get(dv, offset + size * i);
}
return arr;
},
set(dv, offset, value) {
for (let i = 0; i < length; i++) {
set(dv, offset + size * i, value[i]!);
}
},
size: size * length,
};
}

export const U8: Type<number> = {
get(dv, offset) {
return dv.getUint8(offset);
Expand Down
5 changes: 2 additions & 3 deletions tools/wasm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ export class Machine extends EventEmitter<{
set_config: unavailable,
get_features: unavailable,
set_features: unavailable,
set_vring_enable: unavailable,
set_vring_num: unavailable,
set_vring_addr: unavailable,
enable_vring: unavailable,
disable_vring: unavailable,
configure_interrupt: unavailable,
notify: unavailable,
},
Expand Down
22 changes: 13 additions & 9 deletions tools/wasm/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@ export interface Imports {
buf_addr: number,
buf_len: number,
): void;

get_features(dev: number, features_addr: number): number;
set_features(dev: number, features: bigint): number;
set_vring_enable(dev: number, vq: number, enable: number): number;
set_vring_num(dev: number, vq: number, num: number): number;
set_vring_addr(
dev: number,
vq: number,
desc: number,
used: number,
avail: number,
): number;

configure_interrupt(
dev: number,
irq: number,
is_config_addr: number,
is_vring_addr: number,
): number;

enable_vring(
dev: number,
vq: number,
size: number,
desc_addr: number,
used_addr: number,
avail_addr: number,
): number;
disable_vring(dev: number, vq: number): number;

notify(dev: number, vq: number): number;
};
}
Expand Down
Loading

0 comments on commit ca54a3d

Please sign in to comment.