From cbed32526b43673e8330a0a686424a8f283d0e21 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 22 Aug 2023 15:52:30 +0200 Subject: [PATCH] KERNEL: Fixed holeybytes --- Cargo.lock | 975 +---------------------------------- Cargo.toml | 2 +- kernel/Cargo.toml | 1 + kernel/src/holeybytes.rs | 83 +++ kernel/src/host.rs | 90 ---- kernel/src/kmain.rs | 24 +- kernel/src/lib.rs | 15 +- kernel/src/scheduler.rs | 76 --- kernel/src/task.rs | 138 +++++ repbuild/holeybytes/ecall.hb | Bin 112 -> 38 bytes 10 files changed, 256 insertions(+), 1148 deletions(-) create mode 100644 kernel/src/holeybytes.rs delete mode 100644 kernel/src/host.rs delete mode 100644 kernel/src/scheduler.rs create mode 100644 kernel/src/task.rs diff --git a/Cargo.lock b/Cargo.lock index 13f2fd3b..ad230899 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,21 +12,6 @@ dependencies = [ "versioning", ] -[[package]] -name = "addr2line" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4fa78e18c64fce05e902adecd7a5eed15a5e0a3439f7b0e169f0252214865e3" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "ahash" version = "0.7.6" @@ -55,27 +40,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "anyhow" -version = "1.0.75" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" - [[package]] name = "autocfg" version = "1.1.0" @@ -88,27 +52,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" -[[package]] -name = "backtrace" -version = "0.3.68" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4319208da049c43661739c5fade2ba182f09d1dc2299b32298d3a31692b17e12" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - -[[package]] -name = "base64" -version = "0.21.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" - [[package]] name = "bit" version = "0.1.1" @@ -127,54 +70,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bumpalo" -version = "3.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" - [[package]] name = "byteorder" version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "bytes" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" - -[[package]] -name = "cc" -version = "1.0.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" -dependencies = [ - "libc", -] - [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "time", - "wasm-bindgen", - "winapi", -] - [[package]] name = "clparse" version = "0.1.0" @@ -185,18 +92,6 @@ dependencies = [ "toml", ] -[[package]] -name = "convert_case" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" - -[[package]] -name = "core-foundation-sys" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" - [[package]] name = "crossbeam-queue" version = "0.3.8" @@ -222,10 +117,8 @@ version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ - "convert_case", "proc-macro2", "quote", - "rustc_version", "syn 1.0.109", ] @@ -252,37 +145,6 @@ dependencies = [ "byteorder", ] -[[package]] -name = "encoding_rs" -version = "0.8.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "error-stack" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f00447f331c7f726db5b8532ebc9163519eed03c6d7c8b73c90b3ff5646ac85" -dependencies = [ - "anyhow", - "rustc_version", -] - -[[package]] -name = "fatfs" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05669f8e7e2d7badc545c513710f0eba09c2fbef683eb859fd79c46c355048e0" -dependencies = [ - "bitflags", - "byteorder", - "chrono", - "log", -] - [[package]] name = "float-cmp" version = "0.8.0" @@ -292,81 +154,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "form_urlencoded" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "futures-channel" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" -dependencies = [ - "futures-core", -] - -[[package]] -name = "futures-core" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" - -[[package]] -name = "futures-io" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" - -[[package]] -name = "futures-macro" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.29", -] - -[[package]] -name = "futures-sink" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" - -[[package]] -name = "futures-task" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" - -[[package]] -name = "futures-util" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" -dependencies = [ - "futures-core", - "futures-io", - "futures-macro", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - [[package]] name = "getrandom" version = "0.2.10" @@ -375,32 +162,7 @@ checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", -] - -[[package]] -name = "gimli" -version = "0.27.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" - -[[package]] -name = "h2" -version = "0.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", + "wasi", ] [[package]] @@ -435,148 +197,6 @@ dependencies = [ "hbbytecode", ] -[[package]] -name = "hermit-abi" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" - -[[package]] -name = "http" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" -dependencies = [ - "bytes", - "http", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" - -[[package]] -name = "httpdate" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" - -[[package]] -name = "hyper" -version = "0.14.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" -dependencies = [ - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "socket2 0.4.9", - "tokio", - "tower-service", - "tracing", - "want", -] - -[[package]] -name = "hyper-rustls" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d78e1e73ec14cf7375674f74d7dde185c8206fd9dea6fb6295e8a98098aaa97" -dependencies = [ - "futures-util", - "http", - "hyper", - "rustls", - "tokio", - "tokio-rustls", -] - -[[package]] -name = "iana-time-zone" -version = "0.1.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - -[[package]] -name = "idna" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - -[[package]] -name = "indexmap" -version = "1.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" -dependencies = [ - "autocfg", - "hashbrown 0.12.3", -] - -[[package]] -name = "ipnet" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6" - -[[package]] -name = "itoa" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" - -[[package]] -name = "js-sys" -version = "0.3.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "kernel" version = "0.2.0" @@ -588,12 +208,13 @@ dependencies = [ "embedded-graphics", "hashbrown 0.14.0", "hbvm", + "kiam", "limine", "log", "rdrand", "sbi", "slab", - "spin 0.9.8", + "spin", "uart_16550", "versioning", "virtio-drivers", @@ -602,6 +223,12 @@ dependencies = [ "xml", ] +[[package]] +name = "kiam" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0fc32485e41ae5e9dedd1442f36dec998431adc9f321224c2c5d645f38fdcb" + [[package]] name = "libc" version = "0.2.147" @@ -629,44 +256,12 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - [[package]] name = "micromath" version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc4010833aea396656c2f91ee704d51a6f1329ec2ab56ffd00bfd56f7481ea94" -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - -[[package]] -name = "miniz_oxide" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" -dependencies = [ - "adler", -] - -[[package]] -name = "mio" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" -dependencies = [ - "libc", - "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys", -] - [[package]] name = "num-traits" version = "0.2.16" @@ -676,25 +271,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi", - "libc", -] - -[[package]] -name = "object" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bda667d9f2b5051b8833f59f3bf748b28ef54f850f4fcb389a252aa383866d1" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.18.0" @@ -707,24 +283,6 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" -[[package]] -name = "percent-encoding" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" - -[[package]] -name = "pin-project-lite" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12cc1b0bf1727a77a54b6654e7b5f1af8604923edc8b81885f8ec92f9e3f0a05" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "proc-macro2" version = "1.0.66" @@ -767,128 +325,12 @@ dependencies = [ "rand_core", ] -[[package]] -name = "repbuild" -version = "0.2.0" -dependencies = [ - "derive_more", - "error-stack", - "fatfs", - "reqwest", -] - -[[package]] -name = "reqwest" -version = "0.11.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20b9b67e2ca7dd9e9f9285b759de30ff538aab981abaaf7bc9bd90b84a0126c3" -dependencies = [ - "base64", - "bytes", - "encoding_rs", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "hyper", - "hyper-rustls", - "ipnet", - "js-sys", - "log", - "mime", - "once_cell", - "percent-encoding", - "pin-project-lite", - "rustls", - "rustls-pemfile", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-rustls", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "webpki-roots", - "winreg", -] - -[[package]] -name = "ring" -version = "0.16.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" -dependencies = [ - "cc", - "libc", - "once_cell", - "spin 0.5.2", - "untrusted", - "web-sys", - "winapi", -] - -[[package]] -name = "rustc-demangle" -version = "0.1.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" - -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver", -] - -[[package]] -name = "rustls" -version = "0.21.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d1feddffcfcc0b33f5c6ce9a29e341e4cd59c3f78e7ee45f4a40c038b1d6cbb" -dependencies = [ - "log", - "ring", - "rustls-webpki", - "sct", -] - -[[package]] -name = "rustls-pemfile" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" -dependencies = [ - "base64", -] - -[[package]] -name = "rustls-webpki" -version = "0.101.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d93931baf2d282fff8d3a532bbfd7653f734643161b87e3e01e59a04439bf0d" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "rustversion" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" -[[package]] -name = "ryu" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" - [[package]] name = "sbi" version = "0.2.0" @@ -901,22 +343,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "sct" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" -dependencies = [ - "ring", - "untrusted", -] - -[[package]] -name = "semver" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" - [[package]] name = "serde" version = "1.0.185" @@ -937,29 +363,6 @@ dependencies = [ "syn 2.0.29", ] -[[package]] -name = "serde_json" -version = "1.0.105" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - [[package]] name = "slab" version = "0.4.8" @@ -969,32 +372,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "socket2" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "socket2" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "spin" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" - [[package]] name = "spin" version = "0.9.8" @@ -1026,72 +403,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "time" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" -dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", -] - -[[package]] -name = "tinyvec" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - -[[package]] -name = "tokio" -version = "1.32.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17ed6077ed6cd6c74735e21f37eb16dc3935f96878b1fe961074089cc80893f9" -dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "num_cpus", - "pin-project-lite", - "socket2 0.5.3", - "windows-sys", -] - -[[package]] -name = "tokio-rustls" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" -dependencies = [ - "rustls", - "tokio", -] - -[[package]] -name = "tokio-util" -version = "0.7.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", - "tracing", -] - [[package]] name = "toml" version = "0.5.9" @@ -1101,38 +412,6 @@ dependencies = [ "serde", ] -[[package]] -name = "tower-service" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" - -[[package]] -name = "tracing" -version = "0.1.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" -dependencies = [ - "cfg-if", - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" -dependencies = [ - "once_cell", -] - -[[package]] -name = "try-lock" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" - [[package]] name = "uart_16550" version = "0.2.19" @@ -1144,44 +423,12 @@ dependencies = [ "x86_64", ] -[[package]] -name = "unicode-bidi" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" - [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" -[[package]] -name = "unicode-normalization" -version = "0.1.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" -dependencies = [ - "tinyvec", -] - -[[package]] -name = "untrusted" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" - -[[package]] -name = "url" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb" -dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", -] - [[package]] name = "version_check" version = "0.9.4" @@ -1213,216 +460,12 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" -[[package]] -name = "want" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" -dependencies = [ - "try-lock", -] - -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn 2.0.29", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03" -dependencies = [ - "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.29", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" - -[[package]] -name = "web-sys" -version = "0.3.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "webpki-roots" -version = "0.25.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - -[[package]] -name = "winreg" -version = "0.50.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" -dependencies = [ - "cfg-if", - "windows-sys", -] - [[package]] name = "x2apic" version = "0.4.2" diff --git a/Cargo.toml b/Cargo.toml index 9d2b277e..47c7f158 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] resolver = "2" -members = ["kernel", "repbuild"] +members = ["kernel"] diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 2588850c..2765f2d2 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -15,6 +15,7 @@ xml.git = "https://git.ablecorp.us/ableos/ableos_userland" versioning.git = "https://git.ablecorp.us/ableos/ableos_userland" able_graphics_library.git = "https://git.ablecorp.us/ableos/ableos_userland" hashbrown = "*" +kiam = "0.1.1" [dependencies.limine] version = "0.1" diff --git a/kernel/src/holeybytes.rs b/kernel/src/holeybytes.rs new file mode 100644 index 00000000..225ce3d3 --- /dev/null +++ b/kernel/src/holeybytes.rs @@ -0,0 +1,83 @@ +use core::{future::Future, task::Poll}; + +use hbvm::{ + mem::{softpaging::icache::ICache, Address}, + Vm, VmRunError, VmRunOk, +}; + +use { + alloc::boxed::Box, + hbvm::mem::softpaging::{HandlePageFault, SoftPagedMem}, +}; + +const TIMER_QUOTIENT: usize = 100; + +pub struct ExecThread<'p> { + vm: Vm, TIMER_QUOTIENT>, +} + +unsafe impl<'p> Send for ExecThread<'p> {} + +impl<'p> ExecThread<'p> { + pub unsafe fn new(program: &'p [u8], entrypoint: Address) -> Self { + ExecThread { + vm: unsafe { + Vm::new( + SoftPagedMem { + root_pt: Box::into_raw(Box::default()), + pf_handler: PageFaultHandler, + program, + icache: ICache::default(), + }, + entrypoint, + ) + }, + } + } +} + +impl<'p> Future for ExecThread<'p> { + type Output = Result<(), VmRunError>; + + fn poll( + mut self: core::pin::Pin<&mut Self>, + cx: &mut core::task::Context<'_>, + ) -> Poll { + match self.vm.run() { + Err(err) => return Poll::Ready(Err(err)), + Ok(VmRunOk::End) => return Poll::Ready(Ok(())), + Ok(VmRunOk::Ecall) => { + log::info!("Ecall"); + log::info!("{:?}", self.vm.registers); + } + Ok(VmRunOk::Timer) => (), + } + + cx.waker().wake_by_ref(); + Poll::Pending + } +} + +struct PageFaultHandler; +impl HandlePageFault for PageFaultHandler { + fn page_fault( + &mut self, + reason: hbvm::mem::MemoryAccessReason, + pagetable: &mut hbvm::mem::softpaging::paging::PageTable, + vaddr: hbvm::mem::Address, + size: hbvm::mem::softpaging::PageSize, + dataptr: *mut u8, + ) -> bool + where + Self: Sized, + { + log::error!( + "REASON: {reason} \ + vaddr: {vaddr} \ + size: {size:?} \ + Dataptr {dataptr:p}", + ); + + false + } +} diff --git a/kernel/src/host.rs b/kernel/src/host.rs deleted file mode 100644 index 5cb0c29c..00000000 --- a/kernel/src/host.rs +++ /dev/null @@ -1,90 +0,0 @@ -//! the system interface - -// use { -// crate::ipc::message::Message, -// alloc::vec::Vec, -// crossbeam_queue::{ArrayQueue, SegQueue}, -// // hbvm::engine::Engine, -// log::trace, -// HostError::MemoryError, -// }; -/// Host errors -pub enum HostError { - /// A host memory error - MemoryError, -} - -// / Check f0 register for the handle -// / check f1 for the message ptr -// / check f2 for the message length -// pub fn ipc_send(engine: &mut Engine) -> Result<(), HostError> { -// let _handle = engine.registers.f0; -// let message_start = engine.registers.f1; -// let message_length = engine.registers.f2; - -// let mut ipc_msg: Vec = alloc::vec![]; - -// for x in message_start..message_start + message_length { -// let byte = engine.read_mem_addr_8(x); -// match byte { -// Ok(byte) => ipc_msg.push(byte), -// Err(_) => return Err(MemoryError), -// } -// } -// log::trace!("Message bytes {:?}", ipc_msg); -// Ok(()) -// } - -// // pub fn ipc_recv(_engine: &mut Engine) {} - -// /// Check f0 for the buffer type -// /// 0 means an unbound buffer -// /// 1 means a bound buffer -// /// Check f1 if the buffer is bound -// /// -// /// f2 Return a handle to the sender -// /// f3 returns a handle the the reciever -// pub fn ipc_mkbuf(engine: &mut Engine) { -// match engine.registers.f0 as usize { -// 0 => { -// trace!("Making a new ipc unbound buffer"); -// let _buf: SegQueue = SegQueue::new(); -// } -// 1 => { -// let buf_len = engine.registers.f1 as usize; -// trace!("Making a new ipc buffer with capacity {}", buf_len); -// let _buf: ArrayQueue = ArrayQueue::new(buf_len); -// } -// _ => {} -// } -// } - -// // pub fn rpc_discover(_engine: &mut Engine) {} -// // pub fn rpc_register(_engine: &mut Engine) {} -// // pub fn rpc_call(_engine: &mut Engine) {} - -use hbvm::mem::softpaging::HandlePageFault; -/// AbleOS HBVM traphandler -pub struct TrapHandler; -impl HandlePageFault for TrapHandler { - fn page_fault( - &mut self, - reason: hbvm::mem::MemoryAccessReason, - pagetable: &mut hbvm::mem::softpaging::paging::PageTable, - vaddr: hbvm::mem::Address, - size: hbvm::mem::softpaging::PageSize, - dataptr: *mut u8, - ) -> bool - where - Self: Sized, - { - log::error!( - "REASON: {reason} \ - vaddr: {vaddr} \ - size: {size:?} \ - Dataptr {dataptr:p}", - ); - - false - } -} diff --git a/kernel/src/kmain.rs b/kernel/src/kmain.rs index 029b6f68..be037f1a 100644 --- a/kernel/src/kmain.rs +++ b/kernel/src/kmain.rs @@ -1,11 +1,14 @@ //! AbleOS Kernel Entrypoint +use hbvm::mem::Address; + +use crate::holeybytes::ExecThread; + // use crate::arch::sloop; use { crate::{ bootmodules::{build_cmd, BootModules}, device_tree::DeviceTree, - scheduler::Scheduler, }, alloc::format, log::{debug, info, trace}, @@ -16,7 +19,7 @@ use { pub fn kmain(cmdline: &str, boot_modules: BootModules) -> ! { debug!("Entered kmain"); - let kcmd = build_cmd("Kernel Command Line", &cmdline); + let kcmd = build_cmd("Kernel Command Line", cmdline); trace!("Cmdline: {kcmd:?}"); for (i, bm) in boot_modules.iter().enumerate() { @@ -39,13 +42,20 @@ pub fn kmain(cmdline: &str, boot_modules: BootModules) -> ! { // capabilities::example(); - let mut sched = Scheduler::new(); + let mut executor = crate::task::Executor::default(); + unsafe { + for module in boot_modules.into_iter().take(2) { + executor.spawn(async move { + if let Err(e) = ExecThread::new(&module.bytes, Address::new(4)).await { + log::error!("{e:?}"); + } + }); + } - sched.new_process(boot_modules[0].bytes.clone()); - sched.new_process(boot_modules[1].bytes.clone()); + executor.run(); + }; - sched.run(); - // spin_loop(); + crate::arch::spin_loop() } pub static DEVICE_TREE: Lazy> = Lazy::new(|| { diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 882fe60a..7b040176 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -12,7 +12,6 @@ ptr_sub_ptr, custom_test_frameworks )] -#![deny(clippy::pedantic, missing_docs, warnings)] #![allow(dead_code)] #![test_runner(crate::test_runner)] @@ -21,16 +20,16 @@ extern crate alloc; mod allocator; mod arch; mod bootmodules; -pub mod capabilities; -pub mod device_tree; -pub mod handle; -pub mod host; -pub mod ipc; +mod capabilities; +mod device_tree; +mod handle; +mod holeybytes; +mod ipc; mod kmain; mod logger; mod memory; -mod scheduler; -pub mod utils; +mod task; +mod utils; use versioning::Version; diff --git a/kernel/src/scheduler.rs b/kernel/src/scheduler.rs deleted file mode 100644 index 899ea396..00000000 --- a/kernel/src/scheduler.rs +++ /dev/null @@ -1,76 +0,0 @@ -use { - alloc::{collections::VecDeque, rc::Rc, slice, vec::Vec}, - hbvm::validate::validate, -}; - -use {crate::host::TrapHandler, hbvm::Vm}; -const TIMER_QUOTIENT: usize = 100; - -pub struct Scheduler<'a> { - data: VecDeque>, -} - -// NOTE: This is a very simple schduler and it sucks and should be replaced with a better one -// Written By Yours Truly: Munir - -impl Scheduler<'_> { - pub fn new() -> Self { - Self { - data: VecDeque::new(), - } - } - pub fn new_process(&mut self, program: Vec) { - let prog = program.clone(); - let prog_arc = Rc::new(prog); - - let binding = Rc::try_unwrap(prog_arc).ok().unwrap(); - - #[allow(clippy::redundant_else)] - if let Err(e) = validate(&program.as_slice()) { - log::error!("Program validation error: {e:?}"); - } else { - log::info!("valid program"); - unsafe { - let slice = slice::from_raw_parts(binding.as_ptr(), binding.len()); - let mut vm = Vm::new_unchecked(&*slice, TrapHandler); - vm.memory.insert_test_page(); - self.data.push_front(vm); - } - } - } - - pub fn run(&mut self) -> ! { - loop { - // If there are no programs to run then sleep. - if self.data.is_empty() { - use crate::arch::spin_loop; - spin_loop(); - } - - let mut prog = self.data.pop_front().unwrap(); - let ret = prog.run(); - match ret { - Ok(oki) => match oki { - hbvm::VmRunOk::End => { - log::info!( - "Program ended. {} programs remaining.", - // Add one here because we pop a program - self.data.len() + 1 - ) - } - hbvm::VmRunOk::Timer => { - log::info!("Timer exhausted. Scheduled program"); - self.data.push_back(prog); - } - hbvm::VmRunOk::Ecall => { - // panic!(); - log::info!("{:?}", prog.registers); - self.data.push_back(prog); - } - }, - - Err(_) => {} - } - } - } -} diff --git a/kernel/src/task.rs b/kernel/src/task.rs new file mode 100644 index 00000000..ea39f849 --- /dev/null +++ b/kernel/src/task.rs @@ -0,0 +1,138 @@ +#![allow(unused)] + +use { + alloc::{boxed::Box, collections::BTreeMap, sync::Arc, task::Wake}, + core::{ + future::Future, + pin::Pin, + task::{Context, Poll, Waker}, + }, + crossbeam_queue::SegQueue, + kiam::when, + slab::Slab, + spin::RwLock, +}; + +static SPAWN_QUEUE: RwLock> = RwLock::new(None); +pub fn spawn(future: impl Future + Send + 'static) { + match &*SPAWN_QUEUE.read() { + Some(s) => s.push(Task::new(future)), + None => panic!("no task executor is running"), + } +} + +pub fn yield_now() -> impl Future { + struct YieldNow(bool); + impl Future for YieldNow { + type Output = (); + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + if self.0 { + Poll::Ready(()) + } else { + self.0 = true; + cx.waker().wake_by_ref(); + Poll::Pending + } + } + } + + YieldNow(false) +} + +#[derive(Default)] +pub struct Executor { + tasks: Slab, + queue: TaskQueue, + to_spawn: SpawnQueue, + wakers: BTreeMap, +} + +impl Executor { + pub fn spawn(&mut self, future: impl Future + Send + 'static) { + self.queue + .push(TaskId(self.tasks.insert(Task::new(future)))); + } + + pub fn run(&mut self) { + { + let mut global_spawner = SPAWN_QUEUE.write(); + if global_spawner.is_some() { + panic!("Task executor is already running"); + } + + *global_spawner = Some(Arc::clone(&self.to_spawn)); + } + + loop { + when! { + let Some(id) = self + .to_spawn + .pop() + .map(|t| TaskId(self.tasks.insert(t))) + .or_else(|| self.queue.pop()) + => { + let Some(task) = self.tasks.get_mut(id.0) else { + panic!("Attempted to get task from empty slot: {}", id.0); + }; + + let mut cx = Context::from_waker(self.wakers.entry(id).or_insert_with(|| { + Waker::from(Arc::new(TaskWaker { + id, + queue: Arc::clone(&self.queue), + })) + })); + + match task.poll(&mut cx) { + Poll::Ready(()) => { + self.tasks.remove(id.0); + self.wakers.remove(&id); + } + Poll::Pending => (), + } + }, + self.tasks.is_empty() => break, + _ => (), + } + } + + *SPAWN_QUEUE.write() = None; + } +} + +struct Task { + future: Pin + Send>>, +} + +impl Task { + pub fn new(future: impl Future + Send + 'static) -> Self { + Self { + future: Box::pin(future), + } + } + + fn poll(&mut self, cx: &mut Context) -> Poll<()> { + self.future.as_mut().poll(cx) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +struct TaskId(usize); + +type TaskQueue = Arc>; +type SpawnQueue = Arc>; + +struct TaskWaker { + id: TaskId, + queue: TaskQueue, +} + +impl Wake for TaskWaker { + fn wake(self: Arc) { + self.wake_by_ref(); + } + + fn wake_by_ref(self: &Arc) { + self.queue.push(self.id); + } +} diff --git a/repbuild/holeybytes/ecall.hb b/repbuild/holeybytes/ecall.hb index 3ecd89a82ad402cc2f2d05840c3c5ee47f3b3b2e..6550b824e929e59c0474f5cb473a184d887a9d53 100644 GIT binary patch literal 38 bcmd;Z}0=( literal 112 ycmWe&WME)n00RLgMmUq13C?6;hBH}N;7m4FIFp?X&g5W+GdVe6Obv+f5E1}Ep8+la