Skip to content

Commit

Permalink
not better
Browse files Browse the repository at this point in the history
  • Loading branch information
tombl committed Dec 29, 2024
1 parent 9cfa19b commit 19742e6
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 62 deletions.
4 changes: 1 addition & 3 deletions arch/wasm/kernel/irq.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#define pr_fmt(fmt) "wasm/irq: " fmt

#include <asm/smp.h>
#include <linux/cpu.h>
#include <linux/bitops.h>
Expand Down Expand Up @@ -82,7 +80,7 @@ void arch_local_irq_restore(unsigned long flags)
static int wasm_irq_map(struct irq_domain *d, unsigned int irq,
irq_hw_number_t hw)
{
pr_info("wasm_irq_map: %d -> %lu\n", irq, hw);
pr_info("map irq: %d -> %lu\n", irq, hw);

irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_percpu_irq);

Expand Down
2 changes: 1 addition & 1 deletion block/bdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ int lookup_bdev(const char *pathname, dev_t *dev)
if (!may_open_dev(&path))
goto out_path_put;

pr_info("rdev: %p %d\n", inode, inode->i_rdev);
pr_info("rdev: %p %d %x\n", inode, inode->i_rdev, inode->i_mode);
*dev = inode->i_rdev;
error = 0;
out_path_put:
Expand Down
1 change: 0 additions & 1 deletion drivers/block/virtio_blk.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// SPDX-License-Identifier: GPL-2.0-only
//#define DEBUG
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/blkdev.h>
Expand Down
3 changes: 2 additions & 1 deletion drivers/virtio/virtio_ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright 2007 Rusty Russell IBM Corporation
*/
#define DEBUG
#define pr_fmt(fmt) "vring: " fmt

#include <linux/virtio.h>
Expand Down Expand Up @@ -1386,7 +1387,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
in_sgs, data, gfp);
if (err != -ENOMEM) {
END_USE(vq);
// END_USE(vq);
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _kernel:
$(Q)$(MAKE) -C ../.. tools/wasm/src/build/vmlinux.wasm tools/wasm/src/build/sections.json

dist/index.js: _kernel $(shell find src -type f)
$(Q)$(ESBUILD) --bundle src/index.ts --outdir=dist --format=esm --splitting --sourcemap --loader:.wasm=file --loader:.squashfs=file
$(Q)$(ESBUILD) --bundle src/index.ts --outdir=dist --format=esm --splitting --sourcemap --loader:.wasm=file --loader:.img=file

dist/index.d.ts: _kernel $(shell find src -type f -name '*.ts')
$(Q)$(TSC)
1 change: 1 addition & 0 deletions tools/wasm/src/disk.img
2 changes: 1 addition & 1 deletion tools/wasm/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare module "*.wasm" {
export default url;
}

declare module "*.squashfs" {
declare module "*.img" {
const url: string;
export default url;
}
2 changes: 2 additions & 0 deletions tools/wasm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export class Machine extends EventEmitter<{

const instance =
(await WebAssembly.instantiate(vmlinux, imports)) as Instance;
// @ts-expect-error
globalThis.instance = instance;
instance.exports.boot();
}
}
Binary file removed tools/wasm/src/root.squashfs
Binary file not shown.
118 changes: 64 additions & 54 deletions tools/wasm/src/virtio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
} from "./bytes.ts";
import { assert } from "./util.ts";
import type { Imports } from "./wasm.ts";
import squashfsUrl from "./root.squashfs";
import rootfsUrl from "./disk.img";

const squashfs = new Uint8Array(
await (await fetch(new URL(squashfsUrl, import.meta.url))).arrayBuffer(),
const rootfs = new Uint8Array(
await (await fetch(new URL(rootfsUrl, import.meta.url))).arrayBuffer(),
);

const TransportFeatures = {
Expand Down Expand Up @@ -121,7 +121,7 @@ class Virtqueue {
this.desc = FixedArray(VirtqDescriptor, size).get(mem, desc_addr);
}

pop() {
#pop() {
let i = this.#advance();
if (i === null) return null;
const head = i;
Expand Down Expand Up @@ -157,6 +157,11 @@ class Virtqueue {
return chain;
}

*[Symbol.iterator]() {
let chain;
while (chain = this.#pop()) yield chain;
}

#advance() {
const desc = this.desc[this.avail_idx];
assert(desc);
Expand Down Expand Up @@ -239,7 +244,7 @@ export class BlockDevice extends VirtioDevice<BlockDeviceConfig> {
constructor() {
super();
this.features |= BlockDeviceFeatures.FLUSH | BlockDeviceFeatures.RO;
this.config.capacity = BigInt(squashfs.length);
this.config.capacity = BigInt(rootfs.length / 512);
}

override notify(vq: number) {
Expand All @@ -248,47 +253,49 @@ export class BlockDevice extends VirtioDevice<BlockDeviceConfig> {
const queue = this.vqs[vq];
assert(queue);

const chain = queue.pop();
assert(chain);

const [header, data, status, trailing] = chain;
assert(header && !header.writable, "header must be readonly");
assert(
header.array.byteLength === BlockDeviceRequest.size,
`header size is ${header.array.byteLength}`,
);
assert(data, "data must exist");
assert(status && status.writable, "status must be writable");
assert(
status.array.byteLength === 1,
`status size is ${status.array.byteLength}`,
);
assert(!trailing, "too many descriptors");

const request = new BlockDeviceRequest(header.array);

let n = 0;
switch (request.type) {
case BlockDeviceRequestType.IN: {
assert(data.writable, "data must be writable when IN");
const sector = Number(request.sector);
const start = sector * 512;
const end = start + 512;
if (end > squashfs.length) {
status.array[0] = BlockDeviceStatus.IOERR;
} else {
data.array.set(squashfs.slice(start, end));
n = 512;
for (const chain of queue) {
const [header, data, status, trailing] = chain;
assert(header && !header.writable, "header must be readonly");
assert(
header.array.byteLength === BlockDeviceRequest.size,
`header size is ${header.array.byteLength}`,
);
assert(data, "data must exist");
assert(status && status.writable, "status must be writable");
assert(
status.array.byteLength === 1,
`status size is ${status.array.byteLength}`,
);
assert(!trailing, "too many descriptors");

const request = new BlockDeviceRequest(header.array);

let n = 0;
switch (request.type) {
case BlockDeviceRequestType.IN: {
assert(data.writable, "data must be writable when IN");
const start = Number(request.sector) * 512;
let end = start + data.array.byteLength;
if (end >= rootfs.length) end = rootfs.length - 1;
data.array.set(rootfs.subarray(start, end));
n = end - start;
status.array[0] = BlockDeviceStatus.OK;
break;
}
break;
default:
console.error("unknown request type", request.type);
status.array[0] = BlockDeviceStatus.UNSUPP;
}
default:
console.error("unknown request type", request.type);
status.array[0] = BlockDeviceStatus.UNSUPP;
}

chain.release(n);
console.log(
request.type,
request.sector,
status.array[0],
btoa(String.fromCharCode(...data.array)),
);

chain.release(n);
}
this.trigger_interrupt("vring");
}
}
Expand All @@ -304,20 +311,20 @@ export class EntropyDevice extends VirtioDevice<EmptyStruct> {
const queue = this.vqs[vq];
assert(queue);

const chain = queue.pop();
assert(chain);
let n = 0;
for (const { array, writable } of chain) {
assert(writable);
for (const chain of queue) {
let n = 0;
for (const { array, writable } of chain) {
assert(writable);

// can't use crypto.getRandomValues on a SharedArrayBuffer
const arr = new Uint8Array(array.length);
crypto.getRandomValues(arr);
array.set(arr);
// can't use crypto.getRandomValues on a SharedArrayBuffer
const arr = new Uint8Array(array.length);
crypto.getRandomValues(arr);
array.set(arr);

n += array.byteLength;
n += array.byteLength;
}
chain.release(n);
}
chain.release(n);

this.trigger_interrupt("vring");
}
Expand All @@ -340,7 +347,10 @@ export function virtio_imports(
set_features(dev, features) {
const device = devices[dev];
assert(device);
device.features = features;
assert(
device.features === features,
"the kernel should accept every feature we offer, and no more",
);
},

enable_vring(dev, vq, size, desc_addr) {
Expand Down

0 comments on commit 19742e6

Please sign in to comment.