From 0355ea6121725bef63adce792e59ecf11b978935 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 07:48:04 +0000 Subject: [PATCH 01/64] Bump virtio_drivers dependency Signed-off-by: Nick Spinale --- Cargo.lock | 41 +++++-------------- .../helpers/virtio-hal-impl/Cargo.toml | 2 +- .../pds/virtio-blk-driver/Cargo.toml | 2 +- .../pds/virtio-blk-driver/src/main.rs | 4 +- .../pds/virtio-net-driver/Cargo.toml | 2 +- .../manifest-scope.nix | 7 +--- 6 files changed, 16 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e327a89bb..2b297942b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,7 +61,7 @@ dependencies = [ "cfg-if", "once_cell", "version_check", - "zerocopy 0.7.32", + "zerocopy", ] [[package]] @@ -2275,7 +2275,7 @@ dependencies = [ "cfg-if", "sel4-atomic-ptr", "volatile", - "zerocopy 0.7.32", + "zerocopy", ] [[package]] @@ -2452,7 +2452,7 @@ dependencies = [ "num_enum", "postcard", "serde", - "zerocopy 0.7.32", + "zerocopy", ] [[package]] @@ -2575,7 +2575,7 @@ version = "0.1.0" dependencies = [ "log", "sel4-externally-shared", - "zerocopy 0.7.32", + "zerocopy", ] [[package]] @@ -2599,7 +2599,7 @@ version = "0.1.0" dependencies = [ "num_enum", "sel4-shared-ring-buffer", - "zerocopy 0.7.32", + "zerocopy", ] [[package]] @@ -2674,7 +2674,7 @@ name = "sel4-simple-task-runtime-config-types" version = "0.1.0" dependencies = [ "serde", - "zerocopy 0.7.32", + "zerocopy", ] [[package]] @@ -3273,13 +3273,13 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "virtio-drivers" -version = "0.5.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c1b8da1e59e77cdae29b32fb936614dc849352d7f58da199072067fe402bbcf" +checksum = "ff69e69ff755c004bb027891b58f951225832c84f6a734f9e1fdf0118aa91f91" dependencies = [ "bitflags 2.4.2", "log", - "zerocopy 0.6.6", + "zerocopy", ] [[package]] @@ -3524,16 +3524,6 @@ dependencies = [ "time", ] -[[package]] -name = "zerocopy" -version = "0.6.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6" -dependencies = [ - "byteorder", - "zerocopy-derive 0.6.6", -] - [[package]] name = "zerocopy" version = "0.7.32" @@ -3541,18 +3531,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" dependencies = [ "byteorder", - "zerocopy-derive 0.7.32", -] - -[[package]] -name = "zerocopy-derive" -version = "0.6.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.52", + "zerocopy-derive", ] [[package]] diff --git a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml b/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml index 65d683681..4026cc348 100644 --- a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml +++ b/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml @@ -22,4 +22,4 @@ sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-alloc sel4-externally-shared = { path = "../../../../../sel4-externally-shared" } sel4-immediate-sync-once-cell = { path = "../../../../../sel4-immediate-sync-once-cell" } sel4-sync = { path = "../../../../../sel4-sync" } -virtio-drivers = { version = "0.5.0", default-features = false } +virtio-drivers = { version = "0.7.2", default-features = false } diff --git a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml index bb2052530..bbaba5519 100644 --- a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml @@ -28,7 +28,7 @@ sel4-logging = { path = "../../../../../sel4-logging" } sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } -virtio-drivers = { version = "0.5.0", default-features = false } +virtio-drivers = { version = "0.7.2", default-features = false } [dependencies.sel4-shared-ring-buffer-block-io-types] path = "../../../../../sel4-shared-ring-buffer/block-io/types" diff --git a/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs b/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs index 3e8a1661c..789401b25 100644 --- a/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs +++ b/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs @@ -126,7 +126,7 @@ impl Handler for HandlerImpl { unsafe { let pending_entry = &mut *pending_entry; self.dev - .complete_read_block( + .complete_read_blocks( token, &pending_entry.virtio_req, buf_ptr.as_mut(), @@ -172,7 +172,7 @@ impl Handler for HandlerImpl { let token = unsafe { let pending_entry = &mut *pending_entry; self.dev - .read_block_nb( + .read_blocks_nb( pending_entry .client_req .start_block_idx() diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml index 0ca15c7f4..0c6aafeb4 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml @@ -28,4 +28,4 @@ sel4-logging = { path = "../../../../../sel4-logging" } sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } -virtio-drivers = { version = "0.5.0", default-features = false, features = ["alloc"] } +virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } diff --git a/hacking/cargo-manifest-management/manifest-scope.nix b/hacking/cargo-manifest-management/manifest-scope.nix index 3e63ccd28..aa23877e9 100644 --- a/hacking/cargo-manifest-management/manifest-scope.nix +++ b/hacking/cargo-manifest-management/manifest-scope.nix @@ -120,7 +120,7 @@ in rec { synstructure = "0.12.6"; tock-registers = "0.8.1"; unwinding = "0.1.6"; - virtio-drivers = "0.5.0"; + virtio-drivers = "0.7.2"; webpki-roots = "0.26"; zerocopy = "0.7.32"; }; @@ -207,11 +207,6 @@ in rec { virtioDriversWith = features: filterOutEmptyFeatureList { version = versions.virtio-drivers; - # git = "https://github.com/rcore-os/virtio-drivers.git"; - # rev = "7385d61153aff3236d7083d143bbeef9c2eb7326"; # first bad - # rev = "cfb8a80cb64c2382586dd9919a82ad40bdbd4892"; # bad - # rev = "a2d79f1a0a0e98a33257ced9d151828feecfd23c"; # good - # rev = "bc78b54f32b39bc3aeda4f43dbd17f51d665f3d9"; # good default-features = false; inherit features; }; From b1afdb9572cda183289f7765d28b2f7d6b61eb39 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 07:49:50 +0000 Subject: [PATCH 02/64] Remove unused dependency Signed-off-by: Nick Spinale --- Cargo.lock | 1 - .../microkit/http-server/helpers/virtio-hal-impl/Cargo.nix | 3 +-- .../microkit/http-server/helpers/virtio-hal-impl/Cargo.toml | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b297942b..9c05d268a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1215,7 +1215,6 @@ dependencies = [ name = "microkit-http-server-example-virtio-hal-impl" version = "0.1.0" dependencies = [ - "log", "sel4-bounce-buffer-allocator", "sel4-externally-shared", "sel4-immediate-sync-once-cell", diff --git a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix b/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix index 6e807e8c5..60ff47347 100644 --- a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix +++ b/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix @@ -4,12 +4,11 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ mk, localCrates, versions, virtioDriversWith }: +{ mk, localCrates, virtioDriversWith }: mk { package.name = "microkit-http-server-example-virtio-hal-impl"; dependencies = { - inherit (versions) log; virtio-drivers = virtioDriversWith []; inherit (localCrates) sel4-sync diff --git a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml b/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml index 4026cc348..4100be3ac 100644 --- a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml +++ b/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml @@ -17,7 +17,6 @@ edition = "2021" license = "BSD-2-Clause" [dependencies] -log = "0.4.17" sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-allocator" } sel4-externally-shared = { path = "../../../../../sel4-externally-shared" } sel4-immediate-sync-once-cell = { path = "../../../../../sel4-immediate-sync-once-cell" } From e92624b6c6175243d64b1383f4295c352e5061d3 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 07:54:09 +0000 Subject: [PATCH 03/64] crates/sel4-drivers/virtio-hal-impl: Move here Signed-off-by: Nick Spinale --- Cargo.lock | 26 +++++++++---------- Cargo.toml | 2 +- .../sel4-virtio-hal-impl}/Cargo.nix | 2 +- .../sel4-virtio-hal-impl}/Cargo.toml | 10 +++---- .../sel4-virtio-hal-impl}/src/lib.rs | 0 .../pds/virtio-blk-driver/Cargo.nix | 2 +- .../pds/virtio-blk-driver/Cargo.toml | 2 +- .../pds/virtio-blk-driver/src/main.rs | 2 +- .../pds/virtio-net-driver/Cargo.nix | 2 +- .../pds/virtio-net-driver/Cargo.toml | 2 +- .../pds/virtio-net-driver/src/main.rs | 2 +- 11 files changed, 26 insertions(+), 26 deletions(-) rename crates/{examples/microkit/http-server/helpers/virtio-hal-impl => drivers/sel4-virtio-hal-impl}/Cargo.nix (84%) rename crates/{examples/microkit/http-server/helpers/virtio-hal-impl => drivers/sel4-virtio-hal-impl}/Cargo.toml (59%) rename crates/{examples/microkit/http-server/helpers/virtio-hal-impl => drivers/sel4-virtio-hal-impl}/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 9c05d268a..0f24d3e37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1191,7 +1191,6 @@ version = "0.1.0" dependencies = [ "log", "microkit-http-server-example-virtio-blk-driver-interface-types", - "microkit-http-server-example-virtio-hal-impl", "sel4", "sel4-bounce-buffer-allocator", "sel4-externally-shared", @@ -1201,6 +1200,7 @@ dependencies = [ "sel4-microkit-message", "sel4-shared-ring-buffer", "sel4-shared-ring-buffer-block-io-types", + "sel4-virtio-hal-impl", "virtio-drivers", ] @@ -1211,23 +1211,11 @@ dependencies = [ "serde", ] -[[package]] -name = "microkit-http-server-example-virtio-hal-impl" -version = "0.1.0" -dependencies = [ - "sel4-bounce-buffer-allocator", - "sel4-externally-shared", - "sel4-immediate-sync-once-cell", - "sel4-sync", - "virtio-drivers", -] - [[package]] name = "microkit-http-server-example-virtio-net-driver" version = "0.1.0" dependencies = [ "log", - "microkit-http-server-example-virtio-hal-impl", "microkit-http-server-example-virtio-net-driver-interface-types", "sel4", "sel4-bounce-buffer-allocator", @@ -1237,6 +1225,7 @@ dependencies = [ "sel4-microkit", "sel4-microkit-message", "sel4-shared-ring-buffer", + "sel4-virtio-hal-impl", "virtio-drivers", ] @@ -2730,6 +2719,17 @@ dependencies = [ "sel4-panicking-env", ] +[[package]] +name = "sel4-virtio-hal-impl" +version = "0.1.0" +dependencies = [ + "sel4-bounce-buffer-allocator", + "sel4-externally-shared", + "sel4-immediate-sync-once-cell", + "sel4-sync", + "virtio-drivers", +] + [[package]] name = "semver" version = "1.0.22" diff --git a/Cargo.toml b/Cargo.toml index 243cd1519..4ea85dedc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ default-members = [] resolver = "2" members = [ + "crates/drivers/sel4-virtio-hal-impl", "crates/examples/microkit/banscii/pds/artist", "crates/examples/microkit/banscii/pds/artist/interface-types", "crates/examples/microkit/banscii/pds/assistant", @@ -22,7 +23,6 @@ members = [ "crates/examples/microkit/banscii/pds/pl011-driver/core", "crates/examples/microkit/banscii/pds/pl011-driver/interface-types", "crates/examples/microkit/hello/pds/hello", - "crates/examples/microkit/http-server/helpers/virtio-hal-impl", "crates/examples/microkit/http-server/pds/pl031-driver", "crates/examples/microkit/http-server/pds/pl031-driver/core", "crates/examples/microkit/http-server/pds/pl031-driver/interface-types", diff --git a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix b/crates/drivers/sel4-virtio-hal-impl/Cargo.nix similarity index 84% rename from crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix rename to crates/drivers/sel4-virtio-hal-impl/Cargo.nix index 60ff47347..cdcdf2b8f 100644 --- a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.nix +++ b/crates/drivers/sel4-virtio-hal-impl/Cargo.nix @@ -7,7 +7,7 @@ { mk, localCrates, virtioDriversWith }: mk { - package.name = "microkit-http-server-example-virtio-hal-impl"; + package.name = "sel4-virtio-hal-impl"; dependencies = { virtio-drivers = virtioDriversWith []; inherit (localCrates) diff --git a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml b/crates/drivers/sel4-virtio-hal-impl/Cargo.toml similarity index 59% rename from crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml rename to crates/drivers/sel4-virtio-hal-impl/Cargo.toml index 4100be3ac..68c4dfd38 100644 --- a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/Cargo.toml +++ b/crates/drivers/sel4-virtio-hal-impl/Cargo.toml @@ -10,15 +10,15 @@ # [package] -name = "microkit-http-server-example-virtio-hal-impl" +name = "sel4-virtio-hal-impl" version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" license = "BSD-2-Clause" [dependencies] -sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-allocator" } -sel4-externally-shared = { path = "../../../../../sel4-externally-shared" } -sel4-immediate-sync-once-cell = { path = "../../../../../sel4-immediate-sync-once-cell" } -sel4-sync = { path = "../../../../../sel4-sync" } +sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" } +sel4-externally-shared = { path = "../../sel4-externally-shared" } +sel4-immediate-sync-once-cell = { path = "../../sel4-immediate-sync-once-cell" } +sel4-sync = { path = "../../sel4-sync" } virtio-drivers = { version = "0.7.2", default-features = false } diff --git a/crates/examples/microkit/http-server/helpers/virtio-hal-impl/src/lib.rs b/crates/drivers/sel4-virtio-hal-impl/src/lib.rs similarity index 100% rename from crates/examples/microkit/http-server/helpers/virtio-hal-impl/src/lib.rs rename to crates/drivers/sel4-virtio-hal-impl/src/lib.rs diff --git a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.nix b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.nix index c312486d6..74c1b7ada 100644 --- a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.nix +++ b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.nix @@ -21,7 +21,7 @@ mk { sel4-shared-ring-buffer sel4-shared-ring-buffer-block-io-types sel4-bounce-buffer-allocator - microkit-http-server-example-virtio-hal-impl + sel4-virtio-hal-impl microkit-http-server-example-virtio-blk-driver-interface-types ; diff --git a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml index bbaba5519..475c169c6 100644 --- a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml @@ -19,7 +19,6 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" microkit-http-server-example-virtio-blk-driver-interface-types = { path = "interface-types" } -microkit-http-server-example-virtio-hal-impl = { path = "../../helpers/virtio-hal-impl" } sel4 = { path = "../../../../../sel4" } sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-allocator" } sel4-externally-shared = { path = "../../../../../sel4-externally-shared", features = ["unstable"] } @@ -28,6 +27,7 @@ sel4-logging = { path = "../../../../../sel4-logging" } sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } +sel4-virtio-hal-impl = { path = "../../../../../drivers/sel4-virtio-hal-impl" } virtio-drivers = { version = "0.7.2", default-features = false } [dependencies.sel4-shared-ring-buffer-block-io-types] diff --git a/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs b/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs index 789401b25..49877fbaa 100644 --- a/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs +++ b/crates/examples/microkit/http-server/pds/virtio-blk-driver/src/main.rs @@ -33,7 +33,7 @@ use sel4_shared_ring_buffer_block_io_types::{ }; use microkit_http_server_example_virtio_blk_driver_interface_types::*; -use microkit_http_server_example_virtio_hal_impl::HalImpl; +use sel4_virtio_hal_impl::HalImpl; mod config; diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix index e0e22ad4a..8105c43b0 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix @@ -20,7 +20,7 @@ mk { sel4-immediate-sync-once-cell sel4-shared-ring-buffer sel4-bounce-buffer-allocator - microkit-http-server-example-virtio-hal-impl + sel4-virtio-hal-impl microkit-http-server-example-virtio-net-driver-interface-types ; diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml index 0c6aafeb4..998703ded 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml @@ -18,7 +18,6 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" -microkit-http-server-example-virtio-hal-impl = { path = "../../helpers/virtio-hal-impl" } microkit-http-server-example-virtio-net-driver-interface-types = { path = "interface-types" } sel4 = { path = "../../../../../sel4" } sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-allocator" } @@ -28,4 +27,5 @@ sel4-logging = { path = "../../../../../sel4-logging" } sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } +sel4-virtio-hal-impl = { path = "../../../../../drivers/sel4-virtio-hal-impl" } virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs b/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs index 9a85c7227..afcfdc71f 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs @@ -24,8 +24,8 @@ use sel4_microkit::{ use sel4_microkit_message::MessageInfoExt as _; use sel4_shared_ring_buffer::{roles::Use, RingBuffers}; -use microkit_http_server_example_virtio_hal_impl::HalImpl; use microkit_http_server_example_virtio_net_driver_interface_types::*; +use sel4_virtio_hal_impl::HalImpl; mod config; From d27393bcc237d8de9017d6ed5a6aaa7313f84fe2 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 08:12:27 +0000 Subject: [PATCH 04/64] crates/sel4-shared-ring-buffer/smoltcp: Use entire DeviceCapabilities struct Signed-off-by: Nick Spinale --- .../microkit/http-server/pds/server/src/main.rs | 8 ++++++-- crates/sel4-shared-ring-buffer/smoltcp/src/inner.rs | 12 +++++++----- crates/sel4-shared-ring-buffer/smoltcp/src/lib.rs | 8 +++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/examples/microkit/http-server/pds/server/src/main.rs b/crates/examples/microkit/http-server/pds/server/src/main.rs index dd5cc17dd..a5fa7e96f 100644 --- a/crates/examples/microkit/http-server/pds/server/src/main.rs +++ b/crates/examples/microkit/http-server/pds/server/src/main.rs @@ -14,7 +14,7 @@ use alloc::sync::Arc; use core::time::Duration; use smoltcp::iface::Config; -use smoltcp::phy::{Device, Medium}; +use smoltcp::phy::{Device, DeviceCapabilities, Medium}; use smoltcp::wire::{EthernetAddress, HardwareAddress}; use sel4_async_block_io::{ @@ -120,7 +120,11 @@ fn init() -> impl Handler { ), 16, 2048, - 1500, + { + let mut caps = DeviceCapabilities::default(); + caps.max_transmission_unit = 1500; + caps + }, ) .unwrap() }; diff --git a/crates/sel4-shared-ring-buffer/smoltcp/src/inner.rs b/crates/sel4-shared-ring-buffer/smoltcp/src/inner.rs index 654e375fe..093f8d93c 100644 --- a/crates/sel4-shared-ring-buffer/smoltcp/src/inner.rs +++ b/crates/sel4-shared-ring-buffer/smoltcp/src/inner.rs @@ -8,6 +8,8 @@ use core::alloc::Layout; use core::ops::Range; use core::ptr::NonNull; +use smoltcp::phy::DeviceCapabilities; + use sel4_bounce_buffer_allocator::{AbstractBounceBufferAllocator, BounceBufferAllocator}; use sel4_externally_shared::ExternallySharedRef; use sel4_shared_ring_buffer::{ @@ -23,7 +25,7 @@ pub(crate) struct Inner { tx_ring_buffers: RingBuffers<'static, Provide, fn()>, rx_buffers: SlotTracker, tx_buffers: SlotTracker, - mtu: usize, + caps: DeviceCapabilities, } pub(crate) type RxBufferIndex = usize; @@ -68,7 +70,7 @@ impl Inner { tx_ring_buffers: RingBuffers<'static, Provide, fn()>, num_rx_buffers: usize, rx_buffer_size: usize, - mtu: usize, + caps: DeviceCapabilities, ) -> Result { let rx_buffers = SlotTracker::new_occupied((0..num_rx_buffers).map(|i| { let range = bounce_buffer_allocator @@ -93,12 +95,12 @@ impl Inner { tx_ring_buffers, rx_buffers, tx_buffers, - mtu, + caps, }) } - pub(crate) fn mtu(&self) -> usize { - self.mtu + pub(crate) fn caps(&self) -> &DeviceCapabilities { + &self.caps } pub(crate) fn poll(&mut self) -> Result { diff --git a/crates/sel4-shared-ring-buffer/smoltcp/src/lib.rs b/crates/sel4-shared-ring-buffer/smoltcp/src/lib.rs index 8fffadded..5b94cb307 100644 --- a/crates/sel4-shared-ring-buffer/smoltcp/src/lib.rs +++ b/crates/sel4-shared-ring-buffer/smoltcp/src/lib.rs @@ -43,7 +43,7 @@ impl DeviceImpl { tx_ring_buffers: RingBuffers<'static, Provide, fn()>, num_rx_buffers: usize, rx_buffer_size: usize, - mtu: usize, + caps: DeviceCapabilities, ) -> Result { Ok(Self { inner: Rc::new(RefCell::new(Inner::new( @@ -53,7 +53,7 @@ impl DeviceImpl { tx_ring_buffers, num_rx_buffers, rx_buffer_size, - mtu, + caps, )?)), }) } @@ -86,9 +86,7 @@ impl Device for DeviceImpl { type TxToken<'a> = TxToken where A: 'a; fn capabilities(&self) -> DeviceCapabilities { - let mut cap = DeviceCapabilities::default(); - cap.max_transmission_unit = self.inner().borrow().mtu(); - cap + self.inner().borrow().caps().clone() } fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { From 7c90e63f916165f818ded5766d47e010891ad71a Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 19:39:33 +0000 Subject: [PATCH 05/64] crates/sel4-async/io: Use embedded-io-async error types Signed-off-by: Nick Spinale --- Cargo.lock | 21 ++++++++++++++++++- crates/sel4-async/io/Cargo.nix | 3 +++ crates/sel4-async/io/Cargo.toml | 3 +++ crates/sel4-async/io/src/lib.rs | 5 +---- crates/sel4-async/network/rustls/Cargo.nix | 2 +- crates/sel4-async/network/rustls/Cargo.toml | 1 + crates/sel4-async/network/rustls/src/conn.rs | 6 +++--- crates/sel4-async/network/rustls/src/error.rs | 10 +++++++++ crates/sel4-async/network/src/lib.rs | 10 +++++++-- .../manifest-scope.nix | 2 ++ 10 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f24d3e37..d71697194 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -617,6 +617,21 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "embedded-io-async" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff09972d4073aa8c299395be75161d582e7629cd663171d62af73c8d50dba3f" +dependencies = [ + "embedded-io 0.6.1", +] + [[package]] name = "env_logger" version = "0.10.2" @@ -1571,7 +1586,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a55c51ee6c0db07e68448e336cf8ea4131a620edefebf9893e759b2d793420f8" dependencies = [ "cobs", - "embedded-io", + "embedded-io 0.4.0", "serde", ] @@ -1921,6 +1936,9 @@ dependencies = [ [[package]] name = "sel4-async-io" version = "0.1.0" +dependencies = [ + "embedded-io-async", +] [[package]] name = "sel4-async-network" @@ -1935,6 +1953,7 @@ dependencies = [ name = "sel4-async-network-rustls" version = "0.1.0" dependencies = [ + "embedded-io-async", "log", "rustls", "sel4-async-io", diff --git a/crates/sel4-async/io/Cargo.nix b/crates/sel4-async/io/Cargo.nix index 3af5bb001..24501a35e 100644 --- a/crates/sel4-async/io/Cargo.nix +++ b/crates/sel4-async/io/Cargo.nix @@ -12,4 +12,7 @@ mk rec { }); package.name = "sel4-async-io"; package.license = "MIT OR Apache-2.0"; + dependencies = { + inherit (versions) embedded-io-async; + }; } diff --git a/crates/sel4-async/io/Cargo.toml b/crates/sel4-async/io/Cargo.toml index bf0818008..4370cadc3 100644 --- a/crates/sel4-async/io/Cargo.toml +++ b/crates/sel4-async/io/Cargo.toml @@ -15,3 +15,6 @@ version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" license = "MIT OR Apache-2.0" + +[dependencies] +embedded-io-async = "0.6.1" diff --git a/crates/sel4-async/io/src/lib.rs b/crates/sel4-async/io/src/lib.rs index acf2a8b74..698167a3c 100644 --- a/crates/sel4-async/io/src/lib.rs +++ b/crates/sel4-async/io/src/lib.rs @@ -9,14 +9,11 @@ #![no_std] -use core::fmt; use core::future::poll_fn; use core::pin::Pin; use core::task::{Context, Poll}; -pub trait ErrorType { - type Error: fmt::Debug; -} +pub use embedded_io_async::{Error, ErrorKind, ErrorType}; pub trait Read: ErrorType { fn poll_read( diff --git a/crates/sel4-async/network/rustls/Cargo.nix b/crates/sel4-async/network/rustls/Cargo.nix index 3721a241b..73476f90a 100644 --- a/crates/sel4-async/network/rustls/Cargo.nix +++ b/crates/sel4-async/network/rustls/Cargo.nix @@ -16,7 +16,7 @@ mk rec { inherit (localCrates) sel4-async-io ; - inherit (versions) log; + inherit (versions) log embedded-io-async; rustls = rustlsWith [] // (localCrates.rustls or {}); }; } diff --git a/crates/sel4-async/network/rustls/Cargo.toml b/crates/sel4-async/network/rustls/Cargo.toml index 5bc8bb4b1..d8fefe268 100644 --- a/crates/sel4-async/network/rustls/Cargo.toml +++ b/crates/sel4-async/network/rustls/Cargo.toml @@ -17,6 +17,7 @@ edition = "2021" license = "Apache-2.0 OR ISC OR MIT" [dependencies] +embedded-io-async = "0.6.1" log = "0.4.17" rustls = { version = "0.23.5", default-features = false, features = ["logging", "ring", "tls12"] } sel4-async-io = { path = "../../io" } diff --git a/crates/sel4-async/network/rustls/src/conn.rs b/crates/sel4-async/network/rustls/src/conn.rs index cca2a3bb1..295ff4364 100644 --- a/crates/sel4-async/network/rustls/src/conn.rs +++ b/crates/sel4-async/network/rustls/src/conn.rs @@ -23,7 +23,7 @@ use rustls::unbuffered::{ }; use rustls::{unbuffered::UnbufferedConnectionCommon, ClientConfig, ServerConfig, SideData}; -use sel4_async_io::{Read, Write}; +use sel4_async_io::{ErrorType, Read, Write}; use crate::{ utils::{poll_read, poll_write, try_or_resize_and_retry, Buffer, WriteCursor}, @@ -288,9 +288,9 @@ impl TlsStream { } } -impl sel4_async_io::ErrorType for TlsStream +impl ErrorType for TlsStream where - IO: sel4_async_io::ErrorType, + IO: ErrorType, { type Error = Error; } diff --git a/crates/sel4-async/network/rustls/src/error.rs b/crates/sel4-async/network/rustls/src/error.rs index cb0a6af51..f491eb5a3 100644 --- a/crates/sel4-async/network/rustls/src/error.rs +++ b/crates/sel4-async/network/rustls/src/error.rs @@ -4,9 +4,13 @@ // SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT // +use core::fmt::Debug; + use rustls::unbuffered::{EncodeError, EncryptError}; use rustls::Error as TlsError; +use sel4_async_io::{Error as AsyncIOError, ErrorKind}; + #[derive(Debug)] pub enum Error { TransitError(E), @@ -33,3 +37,9 @@ impl From for Error { Self::EncryptError(err) } } + +impl AsyncIOError for Error { + fn kind(&self) -> ErrorKind { + ErrorKind::Other + } +} diff --git a/crates/sel4-async/network/src/lib.rs b/crates/sel4-async/network/src/lib.rs index ded7b52db..2c3b2b1fb 100644 --- a/crates/sel4-async/network/src/lib.rs +++ b/crates/sel4-async/network/src/lib.rs @@ -29,7 +29,7 @@ use smoltcp::{ wire::{DnsQueryType, IpAddress, IpCidr, IpEndpoint, IpListenEndpoint, Ipv4Address, Ipv4Cidr}, }; -use sel4_async_io::{Read, Write}; +use sel4_async_io::{Error as AsyncIOError, ErrorKind, ErrorType, Read, Write}; pub(crate) const DEFAULT_KEEP_ALIVE_INTERVAL: u64 = 75000; pub(crate) const DEFAULT_TCP_SOCKET_BUFFER_SIZE: usize = 65535; @@ -82,6 +82,12 @@ pub enum TcpSocketError { ConnectionResetDuringConnect, } +impl AsyncIOError for TcpSocketError { + fn kind(&self) -> ErrorKind { + ErrorKind::Other + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum DnsError { StartQueryError(dns::StartQueryError), @@ -290,7 +296,7 @@ impl Socket> { } } -impl sel4_async_io::ErrorType for Socket> { +impl ErrorType for Socket> { type Error = TcpSocketError; } diff --git a/hacking/cargo-manifest-management/manifest-scope.nix b/hacking/cargo-manifest-management/manifest-scope.nix index aa23877e9..ebb3d5632 100644 --- a/hacking/cargo-manifest-management/manifest-scope.nix +++ b/hacking/cargo-manifest-management/manifest-scope.nix @@ -93,6 +93,8 @@ in rec { async-unsync = "0.2.2"; cfg-if = "1.0.0"; clap = "4.4.6"; + embedded-hal-nb = "1.0"; + embedded-io-async = "0.6.1"; fallible-iterator = "0.2.0"; fdt = "0.1.5"; futures = "0.3.28"; From 5a390bae2af3231977770f7321d734cdbffc2f12 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 20:22:28 +0000 Subject: [PATCH 06/64] crates/sel4-async/io: Leverage the embedded-io-async crate Signed-off-by: Nick Spinale --- Cargo.lock | 1 + .../http-server/pds/server/core/Cargo.nix | 2 +- .../http-server/pds/server/core/Cargo.toml | 1 + .../http-server/pds/server/core/src/lib.rs | 11 +- .../http-server/pds/server/core/src/server.rs | 3 +- crates/sel4-async/io/src/lib.rs | 203 ++++++++++++------ 6 files changed, 147 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d71697194..c0ebef385 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1156,6 +1156,7 @@ dependencies = [ name = "microkit-http-server-example-server-core" version = "0.1.0" dependencies = [ + "embedded-io-async", "futures", "httparse", "log", diff --git a/crates/examples/microkit/http-server/pds/server/core/Cargo.nix b/crates/examples/microkit/http-server/pds/server/core/Cargo.nix index 21b9d43bb..ff9f6ad08 100644 --- a/crates/examples/microkit/http-server/pds/server/core/Cargo.nix +++ b/crates/examples/microkit/http-server/pds/server/core/Cargo.nix @@ -9,7 +9,7 @@ mk { package.name = "microkit-http-server-example-server-core"; dependencies = { - inherit (versions) log webpki-roots; + inherit (versions) log embedded-io-async webpki-roots; futures = { version = versions.futures; diff --git a/crates/examples/microkit/http-server/pds/server/core/Cargo.toml b/crates/examples/microkit/http-server/pds/server/core/Cargo.toml index aa4c68936..591033ea9 100644 --- a/crates/examples/microkit/http-server/pds/server/core/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/server/core/Cargo.toml @@ -17,6 +17,7 @@ edition = "2021" license = "BSD-2-Clause" [dependencies] +embedded-io-async = "0.6.1" futures = { version = "0.3.28", default-features = false, features = ["async-await", "alloc"] } httparse = { version = "1.8.0", default-features = false } log = "0.4.17" diff --git a/crates/examples/microkit/http-server/pds/server/core/src/lib.rs b/crates/examples/microkit/http-server/pds/server/core/src/lib.rs index 923c2d3b6..f089d9ae0 100644 --- a/crates/examples/microkit/http-server/pds/server/core/src/lib.rs +++ b/crates/examples/microkit/http-server/pds/server/core/src/lib.rs @@ -14,6 +14,7 @@ use alloc::sync::Arc; use alloc::vec; use core::time::Duration; +use embedded_io_async::ReadExactError; use futures::future::{self, LocalBoxFuture}; use futures::task::LocalSpawnExt; use rustls::pki_types::{PrivateKeyDer, UnixTime}; @@ -22,7 +23,7 @@ use rustls::ServerConfig; use sel4_async_block_io::{access::ReadOnly, constant_block_sizes, BlockIO}; use sel4_async_block_io_fat as fat; -use sel4_async_io::ReadExactError; +use sel4_async_io::EmbeddedIOAsyncAdapter; use sel4_async_network::{ManagedInterface, TcpSocket, TcpSocketError}; use sel4_async_network_rustls::{Error as AsyncRustlsError, ServerConnector}; use sel4_async_network_rustls_utils::TimeProviderImpl; @@ -118,7 +119,9 @@ async fn use_socket_for_http Result<(), ReadExactError> { socket.accept(HTTP_PORT).await?; - server.handle_connection(&mut socket).await?; + server + .handle_connection(&mut EmbeddedIOAsyncAdapter(&mut socket)) + .await?; socket.close(); Ok(()) } @@ -135,7 +138,9 @@ async fn use_socket_for_https, buf: &mut [u8], ) -> Poll>; +} - // // // +pub trait Write: ErrorType { + fn poll_write( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll>; - #[allow(async_fn_in_trait)] - async fn read(&mut self, buf: &mut [u8]) -> Result - where - Self: Unpin, - { - let mut pin = Pin::new(self); - poll_fn(move |cx| pin.as_mut().poll_read(cx, buf)).await + fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) } +} - #[allow(async_fn_in_trait)] - async fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), ReadExactError> - where - Self: Unpin, - { - while !buf.is_empty() { - match self.read(buf).await { - Ok(0) => break, - Ok(n) => buf = &mut buf[n..], - Err(e) => return Err(ReadExactError::Other(e)), - } - } - if buf.is_empty() { - Ok(()) - } else { - Err(ReadExactError::UnexpectedEof) - } +impl Read for &mut T { + fn poll_read( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut [u8], + ) -> Poll> { + T::poll_read(Pin::new(*Pin::into_inner(self)), cx, buf) } } -pub trait Write: ErrorType { +impl Write for &mut T { fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], - ) -> Poll>; + ) -> Poll> { + T::poll_write(Pin::new(*Pin::into_inner(self)), cx, buf) + } - fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>; + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + T::poll_flush(Pin::new(*Pin::into_inner(self)), cx) + } +} - // // // +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct EmbeddedIOAsyncAdapter(pub T); + +impl ErrorType for EmbeddedIOAsyncAdapter { + type Error = T::Error; +} - #[allow(async_fn_in_trait)] - async fn write(&mut self, buf: &[u8]) -> Result - where - Self: Unpin, - { - let mut pin = Pin::new(self); - poll_fn(|cx| pin.as_mut().poll_write(cx, buf)).await +impl eio::Read for EmbeddedIOAsyncAdapter +where + T: Read + Unpin, +{ + async fn read(&mut self, buf: &mut [u8]) -> Result { + poll_fn(|cx| Pin::new(&mut self.0).poll_read(cx, buf)).await } +} - #[allow(async_fn_in_trait)] - async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> - where - Self: Unpin, - { - let mut buf = buf; - while !buf.is_empty() { - match self.write(buf).await { - Ok(0) => panic!("write() returned Ok(0)"), - Ok(n) => buf = &buf[n..], - Err(e) => return Err(e), - } - } - Ok(()) +impl eio::Write for EmbeddedIOAsyncAdapter +where + T: Write + Unpin, +{ + async fn write(&mut self, buf: &[u8]) -> Result { + poll_fn(|cx| Pin::new(&mut self.0).poll_write(cx, buf)).await } - #[allow(async_fn_in_trait)] - async fn flush(&mut self) -> Result<(), Self::Error> - where - Self: Unpin, - { - let mut pin = Pin::new(self); - poll_fn(|cx| pin.as_mut().poll_flush(cx)).await + async fn flush(&mut self) -> Result<(), Self::Error> { + poll_fn(|cx| Pin::new(&mut self.0).poll_flush(cx)).await } } -/// Error returned by [`Read::read_exact`] -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum ReadExactError { - /// An EOF error was encountered before reading the exact amount of requested bytes. - UnexpectedEof, - /// Error returned by the inner Read. - Other(E), +pub trait ReadCancelSafe: eio::Read {} +pub trait FlushCancelSafe: eio::Write {} +pub trait WriteCancelSafe: FlushCancelSafe {} + +impl ReadCancelSafe for EmbeddedIOAsyncAdapter {} +impl FlushCancelSafe for EmbeddedIOAsyncAdapter {} +impl WriteCancelSafe for EmbeddedIOAsyncAdapter {} + +impl Read for EmbeddedIOAsyncAdapter +where + T: eio::Read + ReadCancelSafe + Unpin, +{ + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut [u8], + ) -> Poll> { + pin!(self.0.read(buf)).poll(cx) + } +} + +impl Write for EmbeddedIOAsyncAdapter +where + T: eio::Write + WriteCancelSafe + Unpin, +{ + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + pin!(self.0.write(buf)).poll(cx) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + pin!(self.0.flush()).poll(cx) + } +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct EmbeddedIOAsyncAdapterUsingReady(pub T); + +impl ErrorType for EmbeddedIOAsyncAdapterUsingReady { + type Error = T::Error; +} + +impl Read for EmbeddedIOAsyncAdapterUsingReady +where + T: eio::Read + eio::ReadReady + Unpin, +{ + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut [u8], + ) -> Poll> { + if self.0.read_ready()? { + match pin!(self.0.read(buf)).poll(cx) { + Poll::Ready(r) => Poll::Ready(r), + Poll::Pending => unreachable!(), + } + } else { + Poll::Pending + } + } } -impl From for ReadExactError { - fn from(err: E) -> Self { - Self::Other(err) +impl Write for EmbeddedIOAsyncAdapterUsingReady +where + T: eio::Write + eio::WriteReady + FlushCancelSafe + Unpin, +{ + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + if self.0.write_ready()? { + match pin!(self.0.write(buf)).poll(cx) { + Poll::Ready(r) => Poll::Ready(r), + Poll::Pending => unreachable!(), + } + } else { + Poll::Pending + } + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + pin!(self.0.flush()).poll(cx) } } From bd5320bd71f0e75aec37cd46af12da57349f174c Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 08:26:19 +0000 Subject: [PATCH 07/64] crates/sel4-microkit/smoltcp-device-adapter: Move out of sel4-hal-adapters Signed-off-by: Nick Spinale --- Cargo.lock | 18 ++++++++--- Cargo.toml | 1 + crates/sel4-hal-adapters/Cargo.nix | 17 +--------- crates/sel4-hal-adapters/Cargo.toml | 22 ------------- crates/sel4-hal-adapters/src/lib.rs | 3 -- crates/sel4-hal-adapters/src/smoltcp/mod.rs | 8 ----- .../src/smoltcp/phy/device.rs | 10 ------ .../sel4-hal-adapters/src/smoltcp/phy/mod.rs | 12 ------- .../smoltcp-device-adapter/Cargo.nix | 27 ++++++++++++++++ .../smoltcp-device-adapter/Cargo.toml | 31 +++++++++++++++++++ .../smoltcp-device-adapter/src/lib.rs} | 2 ++ 11 files changed, 76 insertions(+), 75 deletions(-) delete mode 100644 crates/sel4-hal-adapters/src/smoltcp/mod.rs delete mode 100644 crates/sel4-hal-adapters/src/smoltcp/phy/device.rs delete mode 100644 crates/sel4-hal-adapters/src/smoltcp/phy/mod.rs create mode 100644 crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix create mode 100644 crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml rename crates/{sel4-hal-adapters/src/smoltcp/phy/handler.rs => sel4-microkit/smoltcp-device-adapter/src/lib.rs} (99%) diff --git a/Cargo.lock b/Cargo.lock index c0ebef385..b9fd148d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2299,13 +2299,9 @@ name = "sel4-hal-adapters" version = "0.1.0" dependencies = [ "log", - "sel4-bounce-buffer-allocator", - "sel4-externally-shared", "sel4-microkit", "sel4-microkit-message", - "sel4-shared-ring-buffer", "serde", - "smoltcp", ] [[package]] @@ -2463,6 +2459,20 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "sel4-microkit-smoltcp-device-adapter" +version = "0.1.0" +dependencies = [ + "log", + "sel4-bounce-buffer-allocator", + "sel4-externally-shared", + "sel4-microkit", + "sel4-microkit-message", + "sel4-shared-ring-buffer", + "serde", + "smoltcp", +] + [[package]] name = "sel4-mod-in-out-dir" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 4ea85dedc..ec856d96f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -115,6 +115,7 @@ members = [ "crates/sel4-microkit/macros", "crates/sel4-microkit/message", "crates/sel4-microkit/message/types", + "crates/sel4-microkit/smoltcp-device-adapter", "crates/sel4-mod-in-out-dir", "crates/sel4-newlib", "crates/sel4-one-ref-cell", diff --git a/crates/sel4-hal-adapters/Cargo.nix b/crates/sel4-hal-adapters/Cargo.nix index 0f9f18613..c3a382bff 100644 --- a/crates/sel4-hal-adapters/Cargo.nix +++ b/crates/sel4-hal-adapters/Cargo.nix @@ -5,7 +5,7 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ mk, versions, localCrates, smoltcpWith, serdeWith, authors }: +{ mk, versions, localCrates, serdeWith, authors }: mk { package.name = "sel4-hal-adapters"; @@ -15,24 +15,9 @@ mk { ]; dependencies = { inherit (versions) log; - smoltcp = smoltcpWith [] // { optional = true; }; serde = serdeWith []; } // (with localCrates; { inherit sel4-microkit-message; sel4-microkit = sel4-microkit // { default-features = false; }; - - # smoltcp-phy deps - sel4-bounce-buffer-allocator = sel4-bounce-buffer-allocator // { optional = true; }; - sel4-externally-shared = sel4-externally-shared // { optional = true; features = ["unstable"]; }; - sel4-shared-ring-buffer = sel4-shared-ring-buffer // { optional = true; }; }); - features = { - default = ["smoltcp-hal"]; - smoltcp-hal = [ - "smoltcp" - "sel4-shared-ring-buffer" - "sel4-externally-shared" - "sel4-bounce-buffer-allocator" - ]; - }; } diff --git a/crates/sel4-hal-adapters/Cargo.toml b/crates/sel4-hal-adapters/Cargo.toml index 2f5357367..3322b06e0 100644 --- a/crates/sel4-hal-adapters/Cargo.toml +++ b/crates/sel4-hal-adapters/Cargo.toml @@ -16,30 +16,8 @@ authors = ["Nick Spinale ", "Ben Hamlin " + ]; + dependencies = { + inherit (versions) log; + smoltcp = smoltcpWith []; + serde = serdeWith []; + } // (with localCrates; { + inherit sel4-microkit-message; + sel4-microkit = sel4-microkit // { default-features = false; }; + sel4-bounce-buffer-allocator = sel4-bounce-buffer-allocator; + sel4-externally-shared = sel4-externally-shared // { features = [ "unstable" ]; }; + sel4-shared-ring-buffer = sel4-shared-ring-buffer; + }); +} diff --git a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml b/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml new file mode 100644 index 000000000..44abd6c54 --- /dev/null +++ b/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml @@ -0,0 +1,31 @@ +# +# Copyright 2023, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# +# +# This file is generated from './Cargo.nix'. You can edit this file directly +# if you are not using this project's Cargo manifest management tools. +# See 'hacking/cargo-manifest-management/README.md' for more information. +# + +[package] +name = "sel4-microkit-smoltcp-device-adapter" +version = "0.1.0" +authors = ["Nick Spinale ", "Ben Hamlin "] +edition = "2021" +license = "BSD-2-Clause" + +[dependencies] +log = "0.4.17" +sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" } +sel4-externally-shared = { path = "../../sel4-externally-shared", features = ["unstable"] } +sel4-microkit = { path = "..", default-features = false } +sel4-microkit-message = { path = "../message" } +sel4-shared-ring-buffer = { path = "../../sel4-shared-ring-buffer" } +serde = { version = "1.0.147", default-features = false } + +[dependencies.smoltcp] +version = "0.10.0" +default-features = false +features = ["proto-ipv4", "proto-dhcpv4", "proto-dns", "socket-dhcpv4", "socket-dns", "socket-tcp"] diff --git a/crates/sel4-hal-adapters/src/smoltcp/phy/handler.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs similarity index 99% rename from crates/sel4-hal-adapters/src/smoltcp/phy/handler.rs rename to crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs index a71995866..b5bfa56ed 100644 --- a/crates/sel4-hal-adapters/src/smoltcp/phy/handler.rs +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs @@ -5,6 +5,8 @@ // SPDX-License-Identifier: BSD-2-Clause // +#![no_std] + //! A generic microkit handler for implementors of [`smoltcp::phy::Device`]. use smoltcp::{ From 929a0e43d1d80f1948769f9de4e75c7614fbc822 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 08:35:46 +0000 Subject: [PATCH 08/64] crates/sel4-hal-adapters: Update code This serial adapter module had mistakenly not been being included. Fix up dependencies, make it compile, and then update it to account for changes in surrounding libraries. Signed-off-by: Nick Spinale --- Cargo.lock | 20 +- crates/sel4-hal-adapters/Cargo.nix | 2 +- crates/sel4-hal-adapters/Cargo.toml | 2 + .../sel4-hal-adapters/src/embedded_hal/mod.rs | 191 ++++++++---------- crates/sel4-hal-adapters/src/lib.rs | 2 + 5 files changed, 103 insertions(+), 114 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b9fd148d8..0e92cfa75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -611,6 +611,22 @@ dependencies = [ "void", ] +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-hal-nb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" +dependencies = [ + "embedded-hal 1.0.0", + "nb 1.1.0", +] + [[package]] name = "embedded-io" version = "0.4.0" @@ -1771,7 +1787,7 @@ checksum = "aa3145d2fae3778b1e31ec2e827b228bdc6abd9b74bb5705ba46dcb82069bc4f" dependencies = [ "bit_field", "critical-section", - "embedded-hal", + "embedded-hal 0.2.7", ] [[package]] @@ -2298,6 +2314,8 @@ dependencies = [ name = "sel4-hal-adapters" version = "0.1.0" dependencies = [ + "embedded-hal-nb", + "heapless", "log", "sel4-microkit", "sel4-microkit-message", diff --git a/crates/sel4-hal-adapters/Cargo.nix b/crates/sel4-hal-adapters/Cargo.nix index c3a382bff..bd250b391 100644 --- a/crates/sel4-hal-adapters/Cargo.nix +++ b/crates/sel4-hal-adapters/Cargo.nix @@ -14,7 +14,7 @@ mk { "Ben Hamlin " ]; dependencies = { - inherit (versions) log; + inherit (versions) log embedded-hal-nb heapless; serde = serdeWith []; } // (with localCrates; { inherit sel4-microkit-message; diff --git a/crates/sel4-hal-adapters/Cargo.toml b/crates/sel4-hal-adapters/Cargo.toml index 3322b06e0..f1ba642a1 100644 --- a/crates/sel4-hal-adapters/Cargo.toml +++ b/crates/sel4-hal-adapters/Cargo.toml @@ -17,6 +17,8 @@ edition = "2021" license = "BSD-2-Clause" [dependencies] +embedded-hal-nb = "1.0" +heapless = "0.7.16" log = "0.4.17" sel4-microkit = { path = "../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../sel4-microkit/message" } diff --git a/crates/sel4-hal-adapters/src/embedded_hal/mod.rs b/crates/sel4-hal-adapters/src/embedded_hal/mod.rs index 4505f6a6b..2eba2287c 100644 --- a/crates/sel4-hal-adapters/src/embedded_hal/mod.rs +++ b/crates/sel4-hal-adapters/src/embedded_hal/mod.rs @@ -5,18 +5,15 @@ // SPDX-License-Identifier: BSD-2-Clause // -#![no_std] - -use num_enum::{IntoPrimitive, TryFromPrimitive}; -use zerocopy::{AsBytes, FromBytes}; use core::fmt; + +use embedded_hal_nb::nb; +use embedded_hal_nb::serial::{self, Write as _}; use heapless::Deque; +use serde::{Deserialize, Serialize}; -use embedded_hal::serial; -use embedded_hal::prelude::_embedded_hal_serial_Write; -use sel4cp::{Channel, Handler}; -use sel4cp::message::{MessageInfo, NoMessageValue, StatusMessageLabel}; -use sel4cp::message::MessageInfoRecvError; +use sel4_microkit::{Channel, Handler, MessageInfo}; +use sel4_microkit_message::MessageInfoExt; /// Handle messages using an implementor of [serial::Read] and [serial::Write]. #[derive(Clone, Debug)] @@ -33,9 +30,9 @@ pub struct SerialHandler { notify: bool, } -impl SerialHandler +impl SerialHandler where - Device: serial::Read + serial::Write + IrqDevice + Device: serial::Read + serial::Write + IrqDevice, { pub fn new(device: Device, serial: Channel, client: Channel) -> Self { Self { @@ -54,29 +51,17 @@ pub trait IrqDevice { #[non_exhaustive] #[derive(Clone, Debug)] -pub enum SerialHandlerError -where - Device: serial::Read + serial::Write, - >::Error: core::fmt::Debug + Clone, - >::Error: core::fmt::Debug + Clone, -{ - ReadError(>::Error), - WriteError(>::Error), +pub enum SerialHandlerError { + DeviceError(E), BufferFull, // XXX Other errors? } -impl fmt::Display for SerialHandlerError -where - Device: serial::Read + serial::Write, - >::Error: core::fmt::Debug + Clone, - >::Error: core::fmt::Debug + Clone, -{ +impl fmt::Display for SerialHandlerError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - SerialHandlerError::ReadError(_) => write!(f, "SerialHandlerError::ReadError"), - SerialHandlerError::WriteError(_) => write!(f, "SerialHandlerError::WriteError"), - SerialHandlerError::BufferFull => write!(f, "SerialHandlerError::BufferFull"), + match self { + SerialHandlerError::DeviceError(err) => write!(f, "device error: {err}"), + SerialHandlerError::BufferFull => write!(f, "buffer full"), } } } @@ -84,10 +69,9 @@ where impl Handler for SerialHandler where Device: serial::Read + serial::Write + IrqDevice, - >::Error: core::fmt::Debug + Clone, - >::Error: core::fmt::Debug + Clone, + ::Error: fmt::Display, { - type Error = SerialHandlerError; + type Error = SerialHandlerError; fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { // TODO Handle errors @@ -104,7 +88,7 @@ where self.notify = false; } } else { - unreachable!() // XXX Is this actually unreachable? + panic!("unexpected channel: {channel:?}"); } Ok(()) } @@ -116,28 +100,24 @@ where ) -> Result { // TODO Handle errors if channel == self.client { - match msg_info.label().try_into().ok() /* XXX Handle errors? */ { - Some(RequestTag::Write) => match msg_info.recv() { - Ok(WriteRequest { val }) => { - // Blocking write - while let Err(nb::Error::WouldBlock) = self.device.write(val) {} - Ok(MessageInfo::send(StatusMessageLabel::Ok, NoMessageValue)) + Ok(match msg_info.recv_using_postcard::() { + Ok(req) => match req { + Request::PutChar { val } => { + nb::block!(self.device.write(val)).unwrap(); + MessageInfo::send_empty() } - Err(_) => Ok(MessageInfo::send(StatusMessageLabel::Error, NoMessageValue)), - }, - Some(RequestTag::Read) => match self.buffer.pop_front() { - Some(val) => { - Ok(MessageInfo::send(ReadResponseTag::Some, ReadSomeResponse { val })) - } - None => { - self.notify = true; - Ok(MessageInfo::send(ReadResponseTag::None, NoMessageValue)) + Request::GetChar => { + let val = self.buffer.pop_front(); + if val.is_some() { + self.notify = true; + } + MessageInfo::send_using_postcard(GetCharSomeResponse { val }).unwrap() } }, - None => Ok(MessageInfo::send(StatusMessageLabel::Error, NoMessageValue)), - } + Err(_) => MessageInfo::send_unspecified_error(), + }) } else { - unreachable!() // XXX Is this actually unreachable? + panic!("unexpected channel: {channel:?}"); } } } @@ -147,7 +127,7 @@ where /// and [fmt::Write]. #[derive(Clone, Debug, PartialEq, Eq)] pub struct SerialDriver { - pub channel: Channel + pub channel: Channel, } impl SerialDriver { @@ -157,50 +137,52 @@ impl SerialDriver { } #[derive(Clone, Debug)] -pub enum ReadError { - RecvError(MessageInfoRecvError), - InvalidResponse, - EOF, +pub enum SerialDriverError { + ReadError(IpcError), + WriteError(IpcError), } -impl serial::Read for SerialDriver { - type Error = ReadError; +#[derive(Clone, Debug)] +pub enum IpcError { + GotError, + GotInvalidResponse, +} - // XXX Unclear if this blocks or how to prevent it from doing so... - fn read(&mut self) -> nb::Result { - let msg_info = self.channel - .pp_call(MessageInfo::send(RequestTag::Read, NoMessageValue)); - - match msg_info.label().try_into() { - Ok(ReadResponseTag::Some) => match msg_info.recv() { - Ok(ReadSomeResponse { val }) => Ok(val), - Err(e) => Err(nb::Error::Other(ReadError::RecvError(e))), - }, - Ok(ReadResponseTag::None) => Err(nb::Error::Other(ReadError::EOF)), - Err(_) => Err(nb::Error::Other(ReadError::InvalidResponse)), - } +impl serial::Error for SerialDriverError { + fn kind(&self) -> serial::ErrorKind { + serial::ErrorKind::Other } } -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum WriteError { - SendError, - InvalidResponse, +impl serial::ErrorType for SerialDriver { + type Error = SerialDriverError; } -impl serial::Write for SerialDriver { - type Error = WriteError; +impl serial::Read for SerialDriver { + fn read(&mut self) -> nb::Result { + let req = Request::GetChar; + let resp = self + .channel + .pp_call(MessageInfo::send_using_postcard(req).unwrap()) + .recv_using_postcard::() + .map_err(|_| { + nb::Error::Other(SerialDriverError::ReadError(IpcError::GotInvalidResponse)) + })?; + resp.val.ok_or(nb::Error::WouldBlock) + } +} - // XXX Unclear if this blocks or how to prevent it from doing so... +impl serial::Write for SerialDriver { + // TODO dont' block? fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> { - let msg_info = self.channel - .pp_call(MessageInfo::send(RequestTag::Write, WriteRequest { val })); - - match msg_info.label().try_into() { - Ok(StatusMessageLabel::Ok) => Ok(()), - Ok(StatusMessageLabel::Error) => Err(nb::Error::Other(WriteError::SendError)), - Err(_) => Err(nb::Error::Other(WriteError::InvalidResponse)), - } + let req = Request::PutChar { val }; + self.channel + .pp_call(MessageInfo::send_using_postcard(req).unwrap()) + .recv_empty() + .map_err(|_| { + nb::Error::Other(SerialDriverError::WriteError(IpcError::GotInvalidResponse)) + })?; + Ok(()) } fn flush(&mut self) -> nb::Result<(), Self::Error> { @@ -208,39 +190,24 @@ impl serial::Write for SerialDriver { } } -// XXX There's already an implementation of core::fmt::Write for serial::Write +// XXX There's already an implementation of fmt::Write for serial::Write // in embedded_hal::fmt, but I'm not clear on how to use it. impl fmt::Write for SerialDriver { fn write_str(&mut self, s: &str) -> fmt::Result { - s.as_bytes().iter().copied().for_each(|b| { let _ = self.write(b); }); + s.as_bytes().iter().copied().for_each(|b| { + let _ = self.write(b); + }); Ok(()) } } -#[derive(Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)] -#[cfg_attr(target_pointer_width = "32", repr(u32))] -#[cfg_attr(target_pointer_width = "64", repr(u64))] -pub enum RequestTag { - Write, - Read, -} - -#[derive(Clone, Copy, PartialEq, Eq, AsBytes, FromBytes)] -#[repr(C)] -pub struct WriteRequest { - pub val: u8, -} - -#[derive(Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)] -#[cfg_attr(target_pointer_width = "32", repr(u32))] -#[cfg_attr(target_pointer_width = "64", repr(u64))] -pub enum ReadResponseTag { - None, - Some, +#[derive(Debug, Serialize, Deserialize)] +pub enum Request { + PutChar { val: u8 }, + GetChar, } -#[derive(Clone, Copy, PartialEq, Eq, AsBytes, FromBytes)] -#[repr(C)] -pub struct ReadSomeResponse { - pub val: u8, +#[derive(Debug, Serialize, Deserialize)] +pub struct GetCharSomeResponse { + pub val: Option, } diff --git a/crates/sel4-hal-adapters/src/lib.rs b/crates/sel4-hal-adapters/src/lib.rs index 2cfaf94ee..1238318df 100644 --- a/crates/sel4-hal-adapters/src/lib.rs +++ b/crates/sel4-hal-adapters/src/lib.rs @@ -6,3 +6,5 @@ // #![no_std] + +pub mod embedded_hal; From 85bf8008fd93f4e49ce730b46fd519fe35585737 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 21:31:16 +0000 Subject: [PATCH 09/64] crates/sel4-hal-adapters: Rename module Signed-off-by: Nick Spinale --- crates/sel4-hal-adapters/src/lib.rs | 2 +- crates/sel4-hal-adapters/src/{embedded_hal => serial}/mod.rs | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename crates/sel4-hal-adapters/src/{embedded_hal => serial}/mod.rs (100%) diff --git a/crates/sel4-hal-adapters/src/lib.rs b/crates/sel4-hal-adapters/src/lib.rs index 1238318df..8aca06e7e 100644 --- a/crates/sel4-hal-adapters/src/lib.rs +++ b/crates/sel4-hal-adapters/src/lib.rs @@ -7,4 +7,4 @@ #![no_std] -pub mod embedded_hal; +pub mod serial; diff --git a/crates/sel4-hal-adapters/src/embedded_hal/mod.rs b/crates/sel4-hal-adapters/src/serial/mod.rs similarity index 100% rename from crates/sel4-hal-adapters/src/embedded_hal/mod.rs rename to crates/sel4-hal-adapters/src/serial/mod.rs From 230d07298045915ab7665048c16d0eeef0d355d0 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 21:47:19 +0000 Subject: [PATCH 10/64] crates/sel4-hal-adapters: Split into modules Signed-off-by: Nick Spinale --- crates/sel4-hal-adapters/src/serial/client.rs | 95 ++++++++ crates/sel4-hal-adapters/src/serial/common.rs | 19 ++ crates/sel4-hal-adapters/src/serial/driver.rs | 124 +++++++++++ crates/sel4-hal-adapters/src/serial/mod.rs | 208 +----------------- 4 files changed, 241 insertions(+), 205 deletions(-) create mode 100644 crates/sel4-hal-adapters/src/serial/client.rs create mode 100644 crates/sel4-hal-adapters/src/serial/common.rs create mode 100644 crates/sel4-hal-adapters/src/serial/driver.rs diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-hal-adapters/src/serial/client.rs new file mode 100644 index 000000000..dcd6f7ef7 --- /dev/null +++ b/crates/sel4-hal-adapters/src/serial/client.rs @@ -0,0 +1,95 @@ +// +// Copyright 2023, Colias Group, LLC +// Copyright 2023, Galois, Inc. +// +// SPDX-License-Identifier: BSD-2-Clause +// + +use core::fmt; + +use embedded_hal_nb::nb; +use embedded_hal_nb::serial::{self, Write as _}; + +use sel4_microkit::{Channel, MessageInfo}; +use sel4_microkit_message::MessageInfoExt; + +use super::common::*; + +/// Device-independent embedded_hal::serial interface to a serial-device +/// component. Interact with it using [serial::Read], [serial::Write], +/// and [fmt::Write]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SerialDriver { + pub channel: Channel, +} + +impl SerialDriver { + pub fn new(channel: Channel) -> Self { + SerialDriver { channel } + } +} + +#[derive(Clone, Debug)] +pub enum SerialDriverError { + ReadError(IpcError), + WriteError(IpcError), +} + +#[derive(Clone, Debug)] +pub enum IpcError { + GotError, + GotInvalidResponse, +} + +impl serial::Error for SerialDriverError { + fn kind(&self) -> serial::ErrorKind { + serial::ErrorKind::Other + } +} + +impl serial::ErrorType for SerialDriver { + type Error = SerialDriverError; +} + +impl serial::Read for SerialDriver { + fn read(&mut self) -> nb::Result { + let req = Request::GetChar; + let resp = self + .channel + .pp_call(MessageInfo::send_using_postcard(req).unwrap()) + .recv_using_postcard::() + .map_err(|_| { + nb::Error::Other(SerialDriverError::ReadError(IpcError::GotInvalidResponse)) + })?; + resp.val.ok_or(nb::Error::WouldBlock) + } +} + +impl serial::Write for SerialDriver { + // TODO dont' block? + fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> { + let req = Request::PutChar { val }; + self.channel + .pp_call(MessageInfo::send_using_postcard(req).unwrap()) + .recv_empty() + .map_err(|_| { + nb::Error::Other(SerialDriverError::WriteError(IpcError::GotInvalidResponse)) + })?; + Ok(()) + } + + fn flush(&mut self) -> nb::Result<(), Self::Error> { + todo!() + } +} + +// XXX There's already an implementation of fmt::Write for serial::Write +// in embedded_hal::fmt, but I'm not clear on how to use it. +impl fmt::Write for SerialDriver { + fn write_str(&mut self, s: &str) -> fmt::Result { + s.as_bytes().iter().copied().for_each(|b| { + let _ = self.write(b); + }); + Ok(()) + } +} diff --git a/crates/sel4-hal-adapters/src/serial/common.rs b/crates/sel4-hal-adapters/src/serial/common.rs new file mode 100644 index 000000000..9c86d7298 --- /dev/null +++ b/crates/sel4-hal-adapters/src/serial/common.rs @@ -0,0 +1,19 @@ +// +// Copyright 2023, Colias Group, LLC +// Copyright 2023, Galois, Inc. +// +// SPDX-License-Identifier: BSD-2-Clause +// + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub enum Request { + PutChar { val: u8 }, + GetChar, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct GetCharSomeResponse { + pub val: Option, +} diff --git a/crates/sel4-hal-adapters/src/serial/driver.rs b/crates/sel4-hal-adapters/src/serial/driver.rs new file mode 100644 index 000000000..f81245ac8 --- /dev/null +++ b/crates/sel4-hal-adapters/src/serial/driver.rs @@ -0,0 +1,124 @@ +// +// Copyright 2023, Colias Group, LLC +// Copyright 2023, Galois, Inc. +// +// SPDX-License-Identifier: BSD-2-Clause +// + +use core::fmt; + +use embedded_hal_nb::nb; +use embedded_hal_nb::serial; +use heapless::Deque; + +use sel4_microkit::{Channel, Handler, MessageInfo}; +use sel4_microkit_message::MessageInfoExt; + +use super::common::*; + +/// Handle messages using an implementor of [serial::Read] and [serial::Write]. +#[derive(Clone, Debug)] +pub struct SerialHandler { + /// Device implementing [serial::Read] and [serial::Write]. + device: Device, + /// Channel for this component. + serial: Channel, + /// Channel for client component. + client: Channel, + /// Read buffer. + buffer: Deque, + /// Whether to notify client. + notify: bool, +} + +impl SerialHandler +where + Device: serial::Read + serial::Write + IrqDevice, +{ + pub fn new(device: Device, serial: Channel, client: Channel) -> Self { + Self { + device, + serial, + client, + buffer: Deque::new(), + notify: true, + } + } +} + +pub trait IrqDevice { + fn handle_irq(&self); +} + +#[non_exhaustive] +#[derive(Clone, Debug)] +pub enum SerialHandlerError { + DeviceError(E), + BufferFull, + // XXX Other errors? +} + +impl fmt::Display for SerialHandlerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + SerialHandlerError::DeviceError(err) => write!(f, "device error: {err}"), + SerialHandlerError::BufferFull => write!(f, "buffer full"), + } + } +} + +impl Handler for SerialHandler +where + Device: serial::Read + serial::Write + IrqDevice, + ::Error: fmt::Display, +{ + type Error = SerialHandlerError; + + fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { + // TODO Handle errors + if channel == self.serial { + while let Ok(c) = self.device.read() { + if let Err(_) = self.buffer.push_back(c) { + return Err(SerialHandlerError::BufferFull); + } + } + self.device.handle_irq(); + self.serial.irq_ack().unwrap(); + if self.notify { + self.client.notify(); + self.notify = false; + } + } else { + panic!("unexpected channel: {channel:?}"); + } + Ok(()) + } + + fn protected( + &mut self, + channel: Channel, + msg_info: MessageInfo, + ) -> Result { + // TODO Handle errors + if channel == self.client { + Ok(match msg_info.recv_using_postcard::() { + Ok(req) => match req { + Request::PutChar { val } => { + nb::block!(self.device.write(val)).unwrap(); + MessageInfo::send_empty() + } + Request::GetChar => { + let val = self.buffer.pop_front(); + if val.is_some() { + self.notify = true; + } + MessageInfo::send_using_postcard(GetCharSomeResponse { val }).unwrap() + } + }, + Err(_) => MessageInfo::send_unspecified_error(), + }) + } else { + panic!("unexpected channel: {channel:?}"); + } + } +} diff --git a/crates/sel4-hal-adapters/src/serial/mod.rs b/crates/sel4-hal-adapters/src/serial/mod.rs index 2eba2287c..cf7bc257c 100644 --- a/crates/sel4-hal-adapters/src/serial/mod.rs +++ b/crates/sel4-hal-adapters/src/serial/mod.rs @@ -5,209 +5,7 @@ // SPDX-License-Identifier: BSD-2-Clause // -use core::fmt; +mod common; -use embedded_hal_nb::nb; -use embedded_hal_nb::serial::{self, Write as _}; -use heapless::Deque; -use serde::{Deserialize, Serialize}; - -use sel4_microkit::{Channel, Handler, MessageInfo}; -use sel4_microkit_message::MessageInfoExt; - -/// Handle messages using an implementor of [serial::Read] and [serial::Write]. -#[derive(Clone, Debug)] -pub struct SerialHandler { - /// Device implementing [serial::Read] and [serial::Write]. - device: Device, - /// Channel for this component. - serial: Channel, - /// Channel for client component. - client: Channel, - /// Read buffer. - buffer: Deque, - /// Whether to notify client. - notify: bool, -} - -impl SerialHandler -where - Device: serial::Read + serial::Write + IrqDevice, -{ - pub fn new(device: Device, serial: Channel, client: Channel) -> Self { - Self { - device, - serial, - client, - buffer: Deque::new(), - notify: true, - } - } -} - -pub trait IrqDevice { - fn handle_irq(&self); -} - -#[non_exhaustive] -#[derive(Clone, Debug)] -pub enum SerialHandlerError { - DeviceError(E), - BufferFull, - // XXX Other errors? -} - -impl fmt::Display for SerialHandlerError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - SerialHandlerError::DeviceError(err) => write!(f, "device error: {err}"), - SerialHandlerError::BufferFull => write!(f, "buffer full"), - } - } -} - -impl Handler for SerialHandler -where - Device: serial::Read + serial::Write + IrqDevice, - ::Error: fmt::Display, -{ - type Error = SerialHandlerError; - - fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { - // TODO Handle errors - if channel == self.serial { - while let Ok(c) = self.device.read() { - if let Err(_) = self.buffer.push_back(c) { - return Err(SerialHandlerError::BufferFull); - } - } - self.device.handle_irq(); - self.serial.irq_ack().unwrap(); - if self.notify { - self.client.notify(); - self.notify = false; - } - } else { - panic!("unexpected channel: {channel:?}"); - } - Ok(()) - } - - fn protected( - &mut self, - channel: Channel, - msg_info: MessageInfo, - ) -> Result { - // TODO Handle errors - if channel == self.client { - Ok(match msg_info.recv_using_postcard::() { - Ok(req) => match req { - Request::PutChar { val } => { - nb::block!(self.device.write(val)).unwrap(); - MessageInfo::send_empty() - } - Request::GetChar => { - let val = self.buffer.pop_front(); - if val.is_some() { - self.notify = true; - } - MessageInfo::send_using_postcard(GetCharSomeResponse { val }).unwrap() - } - }, - Err(_) => MessageInfo::send_unspecified_error(), - }) - } else { - panic!("unexpected channel: {channel:?}"); - } - } -} - -/// Device-independent embedded_hal::serial interface to a serial-device -/// component. Interact with it using [serial::Read], [serial::Write], -/// and [fmt::Write]. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SerialDriver { - pub channel: Channel, -} - -impl SerialDriver { - pub fn new(channel: Channel) -> Self { - SerialDriver { channel } - } -} - -#[derive(Clone, Debug)] -pub enum SerialDriverError { - ReadError(IpcError), - WriteError(IpcError), -} - -#[derive(Clone, Debug)] -pub enum IpcError { - GotError, - GotInvalidResponse, -} - -impl serial::Error for SerialDriverError { - fn kind(&self) -> serial::ErrorKind { - serial::ErrorKind::Other - } -} - -impl serial::ErrorType for SerialDriver { - type Error = SerialDriverError; -} - -impl serial::Read for SerialDriver { - fn read(&mut self) -> nb::Result { - let req = Request::GetChar; - let resp = self - .channel - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard::() - .map_err(|_| { - nb::Error::Other(SerialDriverError::ReadError(IpcError::GotInvalidResponse)) - })?; - resp.val.ok_or(nb::Error::WouldBlock) - } -} - -impl serial::Write for SerialDriver { - // TODO dont' block? - fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> { - let req = Request::PutChar { val }; - self.channel - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_empty() - .map_err(|_| { - nb::Error::Other(SerialDriverError::WriteError(IpcError::GotInvalidResponse)) - })?; - Ok(()) - } - - fn flush(&mut self) -> nb::Result<(), Self::Error> { - todo!() - } -} - -// XXX There's already an implementation of fmt::Write for serial::Write -// in embedded_hal::fmt, but I'm not clear on how to use it. -impl fmt::Write for SerialDriver { - fn write_str(&mut self, s: &str) -> fmt::Result { - s.as_bytes().iter().copied().for_each(|b| { - let _ = self.write(b); - }); - Ok(()) - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub enum Request { - PutChar { val: u8 }, - GetChar, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct GetCharSomeResponse { - pub val: Option, -} +pub mod client; +pub mod driver; From 4468fa056554a2b9e790f7a06080b483cdcd5c7a Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 21:49:59 +0000 Subject: [PATCH 11/64] crates/sel4-hal-adapters: Simplify idents Signed-off-by: Nick Spinale --- crates/sel4-hal-adapters/src/serial/client.rs | 28 ++++++++----------- crates/sel4-hal-adapters/src/serial/driver.rs | 18 ++++++------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-hal-adapters/src/serial/client.rs index dcd6f7ef7..e82a2686b 100644 --- a/crates/sel4-hal-adapters/src/serial/client.rs +++ b/crates/sel4-hal-adapters/src/serial/client.rs @@ -19,18 +19,18 @@ use super::common::*; /// component. Interact with it using [serial::Read], [serial::Write], /// and [fmt::Write]. #[derive(Clone, Debug, PartialEq, Eq)] -pub struct SerialDriver { +pub struct Client { pub channel: Channel, } -impl SerialDriver { +impl Client { pub fn new(channel: Channel) -> Self { - SerialDriver { channel } + Client { channel } } } #[derive(Clone, Debug)] -pub enum SerialDriverError { +pub enum Error { ReadError(IpcError), WriteError(IpcError), } @@ -41,40 +41,36 @@ pub enum IpcError { GotInvalidResponse, } -impl serial::Error for SerialDriverError { +impl serial::Error for Error { fn kind(&self) -> serial::ErrorKind { serial::ErrorKind::Other } } -impl serial::ErrorType for SerialDriver { - type Error = SerialDriverError; +impl serial::ErrorType for Client { + type Error = Error; } -impl serial::Read for SerialDriver { +impl serial::Read for Client { fn read(&mut self) -> nb::Result { let req = Request::GetChar; let resp = self .channel .pp_call(MessageInfo::send_using_postcard(req).unwrap()) .recv_using_postcard::() - .map_err(|_| { - nb::Error::Other(SerialDriverError::ReadError(IpcError::GotInvalidResponse)) - })?; + .map_err(|_| nb::Error::Other(Error::ReadError(IpcError::GotInvalidResponse)))?; resp.val.ok_or(nb::Error::WouldBlock) } } -impl serial::Write for SerialDriver { +impl serial::Write for Client { // TODO dont' block? fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> { let req = Request::PutChar { val }; self.channel .pp_call(MessageInfo::send_using_postcard(req).unwrap()) .recv_empty() - .map_err(|_| { - nb::Error::Other(SerialDriverError::WriteError(IpcError::GotInvalidResponse)) - })?; + .map_err(|_| nb::Error::Other(Error::WriteError(IpcError::GotInvalidResponse)))?; Ok(()) } @@ -85,7 +81,7 @@ impl serial::Write for SerialDriver { // XXX There's already an implementation of fmt::Write for serial::Write // in embedded_hal::fmt, but I'm not clear on how to use it. -impl fmt::Write for SerialDriver { +impl fmt::Write for Client { fn write_str(&mut self, s: &str) -> fmt::Result { s.as_bytes().iter().copied().for_each(|b| { let _ = self.write(b); diff --git a/crates/sel4-hal-adapters/src/serial/driver.rs b/crates/sel4-hal-adapters/src/serial/driver.rs index f81245ac8..7496be0f8 100644 --- a/crates/sel4-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-hal-adapters/src/serial/driver.rs @@ -18,7 +18,7 @@ use super::common::*; /// Handle messages using an implementor of [serial::Read] and [serial::Write]. #[derive(Clone, Debug)] -pub struct SerialHandler { +pub struct Driver { /// Device implementing [serial::Read] and [serial::Write]. device: Device, /// Channel for this component. @@ -31,7 +31,7 @@ pub struct SerialHandler { notify: bool, } -impl SerialHandler +impl Driver where Device: serial::Read + serial::Write + IrqDevice, { @@ -52,34 +52,34 @@ pub trait IrqDevice { #[non_exhaustive] #[derive(Clone, Debug)] -pub enum SerialHandlerError { +pub enum Error { DeviceError(E), BufferFull, // XXX Other errors? } -impl fmt::Display for SerialHandlerError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - SerialHandlerError::DeviceError(err) => write!(f, "device error: {err}"), - SerialHandlerError::BufferFull => write!(f, "buffer full"), + Self::DeviceError(err) => write!(f, "device error: {err}"), + Self::BufferFull => write!(f, "buffer full"), } } } -impl Handler for SerialHandler +impl Handler for Driver where Device: serial::Read + serial::Write + IrqDevice, ::Error: fmt::Display, { - type Error = SerialHandlerError; + type Error = Error; fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { // TODO Handle errors if channel == self.serial { while let Ok(c) = self.device.read() { if let Err(_) = self.buffer.push_back(c) { - return Err(SerialHandlerError::BufferFull); + return Err(Error::BufferFull); } } self.device.handle_irq(); From ac94fe5c9a37d6aacc5820d2c7382d3a76a8cff7 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 21:53:09 +0000 Subject: [PATCH 12/64] crates/sel4-hal-adapters: Reorder items Signed-off-by: Nick Spinale --- crates/sel4-hal-adapters/src/serial/client.rs | 36 ++++++++-------- crates/sel4-hal-adapters/src/serial/driver.rs | 41 ++++++++++--------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-hal-adapters/src/serial/client.rs index e82a2686b..ffe3719dd 100644 --- a/crates/sel4-hal-adapters/src/serial/client.rs +++ b/crates/sel4-hal-adapters/src/serial/client.rs @@ -29,24 +29,6 @@ impl Client { } } -#[derive(Clone, Debug)] -pub enum Error { - ReadError(IpcError), - WriteError(IpcError), -} - -#[derive(Clone, Debug)] -pub enum IpcError { - GotError, - GotInvalidResponse, -} - -impl serial::Error for Error { - fn kind(&self) -> serial::ErrorKind { - serial::ErrorKind::Other - } -} - impl serial::ErrorType for Client { type Error = Error; } @@ -89,3 +71,21 @@ impl fmt::Write for Client { Ok(()) } } + +#[derive(Clone, Debug)] +pub enum Error { + ReadError(IpcError), + WriteError(IpcError), +} + +#[derive(Clone, Debug)] +pub enum IpcError { + GotError, + GotInvalidResponse, +} + +impl serial::Error for Error { + fn kind(&self) -> serial::ErrorKind { + serial::ErrorKind::Other + } +} diff --git a/crates/sel4-hal-adapters/src/serial/driver.rs b/crates/sel4-hal-adapters/src/serial/driver.rs index 7496be0f8..ec48385e0 100644 --- a/crates/sel4-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-hal-adapters/src/serial/driver.rs @@ -16,6 +16,10 @@ use sel4_microkit_message::MessageInfoExt; use super::common::*; +pub trait IrqDevice { + fn handle_irq(&self); +} + /// Handle messages using an implementor of [serial::Read] and [serial::Write]. #[derive(Clone, Debug)] pub struct Driver { @@ -46,26 +50,6 @@ where } } -pub trait IrqDevice { - fn handle_irq(&self); -} - -#[non_exhaustive] -#[derive(Clone, Debug)] -pub enum Error { - DeviceError(E), - BufferFull, - // XXX Other errors? -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Self::DeviceError(err) => write!(f, "device error: {err}"), - Self::BufferFull => write!(f, "buffer full"), - } - } -} impl Handler for Driver where @@ -122,3 +106,20 @@ where } } } + +#[non_exhaustive] +#[derive(Clone, Debug)] +pub enum Error { + DeviceError(E), + BufferFull, + // XXX Other errors? +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::DeviceError(err) => write!(f, "device error: {err}"), + Self::BufferFull => write!(f, "buffer full"), + } + } +} From 5f69824baff8d188045759d625119467e4500db5 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 21:54:24 +0000 Subject: [PATCH 13/64] Rename GetCharSomeResponse -> GetCharResponse Signed-off-by: Nick Spinale --- crates/examples/microkit/banscii/pds/assistant/src/main.rs | 2 +- .../banscii/pds/pl011-driver/interface-types/src/lib.rs | 2 +- crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs | 2 +- crates/sel4-hal-adapters/src/serial/client.rs | 2 +- crates/sel4-hal-adapters/src/serial/common.rs | 2 +- crates/sel4-hal-adapters/src/serial/driver.rs | 3 +-- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/examples/microkit/banscii/pds/assistant/src/main.rs b/crates/examples/microkit/banscii/pds/assistant/src/main.rs index e95f19824..c2885f9c6 100644 --- a/crates/examples/microkit/banscii/pds/assistant/src/main.rs +++ b/crates/examples/microkit/banscii/pds/assistant/src/main.rs @@ -190,7 +190,7 @@ fn newline() { fn get_char() -> Option { let req = pl011_driver::Request::GetChar; - let resp: pl011_driver::GetCharSomeResponse = PL011_DRIVER + let resp: pl011_driver::GetCharResponse = PL011_DRIVER .pp_call(MessageInfo::send_using_postcard(req).unwrap()) .recv_using_postcard() .unwrap(); diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs b/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs index d85257074..b9e27f5bf 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs +++ b/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs @@ -15,6 +15,6 @@ pub enum Request { } #[derive(Debug, Serialize, Deserialize)] -pub struct GetCharSomeResponse { +pub struct GetCharResponse { pub val: Option, } diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs b/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs index baccc5de6..c5a5c9545 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs +++ b/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs @@ -79,7 +79,7 @@ impl Handler for HandlerImpl { if val.is_some() { self.notify = true; } - MessageInfo::send_using_postcard(GetCharSomeResponse { val }).unwrap() + MessageInfo::send_using_postcard(GetCharResponse { val }).unwrap() } }, Err(_) => MessageInfo::send_unspecified_error(), diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-hal-adapters/src/serial/client.rs index ffe3719dd..22393248c 100644 --- a/crates/sel4-hal-adapters/src/serial/client.rs +++ b/crates/sel4-hal-adapters/src/serial/client.rs @@ -39,7 +39,7 @@ impl serial::Read for Client { let resp = self .channel .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard::() + .recv_using_postcard::() .map_err(|_| nb::Error::Other(Error::ReadError(IpcError::GotInvalidResponse)))?; resp.val.ok_or(nb::Error::WouldBlock) } diff --git a/crates/sel4-hal-adapters/src/serial/common.rs b/crates/sel4-hal-adapters/src/serial/common.rs index 9c86d7298..c209267bd 100644 --- a/crates/sel4-hal-adapters/src/serial/common.rs +++ b/crates/sel4-hal-adapters/src/serial/common.rs @@ -14,6 +14,6 @@ pub enum Request { } #[derive(Debug, Serialize, Deserialize)] -pub struct GetCharSomeResponse { +pub struct GetCharResponse { pub val: Option, } diff --git a/crates/sel4-hal-adapters/src/serial/driver.rs b/crates/sel4-hal-adapters/src/serial/driver.rs index ec48385e0..b68e2bf39 100644 --- a/crates/sel4-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-hal-adapters/src/serial/driver.rs @@ -50,7 +50,6 @@ where } } - impl Handler for Driver where Device: serial::Read + serial::Write + IrqDevice, @@ -96,7 +95,7 @@ where if val.is_some() { self.notify = true; } - MessageInfo::send_using_postcard(GetCharSomeResponse { val }).unwrap() + MessageInfo::send_using_postcard(GetCharResponse { val }).unwrap() } }, Err(_) => MessageInfo::send_unspecified_error(), From 79aaf58612e44a3d1a1ab3b7d810d8b1476b1323 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 22:00:53 +0000 Subject: [PATCH 14/64] crates/sel4-hal-adapters: Refactor errors Signed-off-by: Nick Spinale --- crates/sel4-hal-adapters/src/serial/client.rs | 41 ++++++++++--------- crates/sel4-hal-adapters/src/serial/common.rs | 13 +++++- crates/sel4-hal-adapters/src/serial/driver.rs | 27 +++++++----- crates/sel4-hal-adapters/src/serial/mod.rs | 2 + 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-hal-adapters/src/serial/client.rs index 22393248c..e864302f4 100644 --- a/crates/sel4-hal-adapters/src/serial/client.rs +++ b/crates/sel4-hal-adapters/src/serial/client.rs @@ -15,7 +15,7 @@ use sel4_microkit_message::MessageInfoExt; use super::common::*; -/// Device-independent embedded_hal::serial interface to a serial-device +/// Device-independent embedded_hal_nb::serial interface to a serial-device /// component. Interact with it using [serial::Read], [serial::Write], /// and [fmt::Write]. #[derive(Clone, Debug, PartialEq, Eq)] @@ -27,6 +27,16 @@ impl Client { pub fn new(channel: Channel) -> Self { Client { channel } } + + pub fn blocking_write(&mut self, val: u8) -> Result<(), Error> { + let req = Request::PutChar { val }; + self.channel + .pp_call(MessageInfo::send_using_postcard(req).unwrap()) + .recv_using_postcard::>() + .map_err(|_| Error::InvalidResponse)? + .map_err(Error::PutCharError)?; + Ok(()) + } } impl serial::ErrorType for Client { @@ -39,25 +49,23 @@ impl serial::Read for Client { let resp = self .channel .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard::() - .map_err(|_| nb::Error::Other(Error::ReadError(IpcError::GotInvalidResponse)))?; + .recv_using_postcard::>() + .map_err(|_| Error::InvalidResponse) + .map_err(nb::Error::Other)? + .map_err(Error::GetCharError) + .map_err(nb::Error::Other)?; resp.val.ok_or(nb::Error::WouldBlock) } } impl serial::Write for Client { - // TODO dont' block? fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> { - let req = Request::PutChar { val }; - self.channel - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_empty() - .map_err(|_| nb::Error::Other(Error::WriteError(IpcError::GotInvalidResponse)))?; + self.blocking_write(val).map_err(nb::Error::Other)?; Ok(()) } fn flush(&mut self) -> nb::Result<(), Self::Error> { - todo!() + Ok(()) } } @@ -72,16 +80,11 @@ impl fmt::Write for Client { } } -#[derive(Clone, Debug)] +#[derive(Debug, Copy, Clone)] pub enum Error { - ReadError(IpcError), - WriteError(IpcError), -} - -#[derive(Clone, Debug)] -pub enum IpcError { - GotError, - GotInvalidResponse, + PutCharError(PutCharError), + GetCharError(GetCharError), + InvalidResponse, } impl serial::Error for Error { diff --git a/crates/sel4-hal-adapters/src/serial/common.rs b/crates/sel4-hal-adapters/src/serial/common.rs index c209267bd..03fa4c2ea 100644 --- a/crates/sel4-hal-adapters/src/serial/common.rs +++ b/crates/sel4-hal-adapters/src/serial/common.rs @@ -8,12 +8,21 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] -pub enum Request { +pub(crate) enum Request { PutChar { val: u8 }, GetChar, } #[derive(Debug, Serialize, Deserialize)] -pub struct GetCharResponse { +pub(crate) struct PutCharResponse; + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct GetCharResponse { pub val: Option, } + +#[derive(Debug, Copy, Clone, Serialize, Deserialize)] +pub struct PutCharError; + +#[derive(Debug, Copy, Clone, Serialize, Deserialize)] +pub struct GetCharError; diff --git a/crates/sel4-hal-adapters/src/serial/driver.rs b/crates/sel4-hal-adapters/src/serial/driver.rs index b68e2bf39..649c5b6c3 100644 --- a/crates/sel4-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-hal-adapters/src/serial/driver.rs @@ -85,19 +85,24 @@ where // TODO Handle errors if channel == self.client { Ok(match msg_info.recv_using_postcard::() { - Ok(req) => match req { - Request::PutChar { val } => { - nb::block!(self.device.write(val)).unwrap(); - MessageInfo::send_empty() - } - Request::GetChar => { - let val = self.buffer.pop_front(); - if val.is_some() { - self.notify = true; + Ok(req) => { + match req { + Request::PutChar { val } => MessageInfo::send_using_postcard( + match nb::block!(self.device.write(val)) { + Ok(_) => Ok(PutCharResponse), + Err(_) => Err(PutCharError), + }, + ) + .unwrap(), + Request::GetChar => { + let val = self.buffer.pop_front(); + if val.is_some() { + self.notify = true; + } + MessageInfo::send_using_postcard::>(Ok(GetCharResponse { val })).unwrap() } - MessageInfo::send_using_postcard(GetCharResponse { val }).unwrap() } - }, + } Err(_) => MessageInfo::send_unspecified_error(), }) } else { diff --git a/crates/sel4-hal-adapters/src/serial/mod.rs b/crates/sel4-hal-adapters/src/serial/mod.rs index cf7bc257c..6f77cb8b5 100644 --- a/crates/sel4-hal-adapters/src/serial/mod.rs +++ b/crates/sel4-hal-adapters/src/serial/mod.rs @@ -9,3 +9,5 @@ mod common; pub mod client; pub mod driver; + +pub use common::{GetCharError, PutCharError}; From 3f7bc07ca836c2900bf72e908a825659e30846d8 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 22:19:28 +0000 Subject: [PATCH 15/64] crates/sel4-hal-adapters: Remove fmt::Write impl This is provided by embedded-hal-nb Signed-off-by: Nick Spinale --- crates/sel4-hal-adapters/src/serial/client.rs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-hal-adapters/src/serial/client.rs index e864302f4..2c1f6e869 100644 --- a/crates/sel4-hal-adapters/src/serial/client.rs +++ b/crates/sel4-hal-adapters/src/serial/client.rs @@ -5,10 +5,8 @@ // SPDX-License-Identifier: BSD-2-Clause // -use core::fmt; - use embedded_hal_nb::nb; -use embedded_hal_nb::serial::{self, Write as _}; +use embedded_hal_nb::serial; use sel4_microkit::{Channel, MessageInfo}; use sel4_microkit_message::MessageInfoExt; @@ -69,17 +67,6 @@ impl serial::Write for Client { } } -// XXX There's already an implementation of fmt::Write for serial::Write -// in embedded_hal::fmt, but I'm not clear on how to use it. -impl fmt::Write for Client { - fn write_str(&mut self, s: &str) -> fmt::Result { - s.as_bytes().iter().copied().for_each(|b| { - let _ = self.write(b); - }); - Ok(()) - } -} - #[derive(Debug, Copy, Clone)] pub enum Error { PutCharError(PutCharError), From e882b3b8a82035a7d71e63ba5e2b72c0acc2d9cf Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 22:21:04 +0000 Subject: [PATCH 16/64] crates/sel4-microkit/embedded-hal-adapters: Move here Signed-off-by: Nick Spinale --- Cargo.lock | 24 +++++++++---------- Cargo.toml | 2 +- .../embedded-hal-adapters}/Cargo.nix | 2 +- .../embedded-hal-adapters}/Cargo.toml | 6 ++--- .../embedded-hal-adapters}/src/lib.rs | 0 .../src/serial/client.rs | 0 .../src/serial/common.rs | 0 .../src/serial/driver.rs | 0 .../embedded-hal-adapters}/src/serial/mod.rs | 0 9 files changed, 17 insertions(+), 17 deletions(-) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/Cargo.nix (89%) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/Cargo.toml (78%) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/src/lib.rs (100%) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/src/serial/client.rs (100%) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/src/serial/common.rs (100%) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/src/serial/driver.rs (100%) rename crates/{sel4-hal-adapters => sel4-microkit/embedded-hal-adapters}/src/serial/mod.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 0e92cfa75..e7050ba02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2310,18 +2310,6 @@ dependencies = [ "serde_json", ] -[[package]] -name = "sel4-hal-adapters" -version = "0.1.0" -dependencies = [ - "embedded-hal-nb", - "heapless", - "log", - "sel4-microkit", - "sel4-microkit-message", - "serde", -] - [[package]] name = "sel4-immediate-sync-once-cell" version = "0.1.0" @@ -2449,6 +2437,18 @@ dependencies = [ "sel4-immutable-cell", ] +[[package]] +name = "sel4-microkit-embedded-hal-adapters" +version = "0.1.0" +dependencies = [ + "embedded-hal-nb", + "heapless", + "log", + "sel4-microkit", + "sel4-microkit-message", + "serde", +] + [[package]] name = "sel4-microkit-macros" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ec856d96f..61e309cda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,7 +99,6 @@ members = [ "crates/sel4-elf-header", "crates/sel4-externally-shared", "crates/sel4-generate-target-specs", - "crates/sel4-hal-adapters", "crates/sel4-immediate-sync-once-cell", "crates/sel4-immutable-cell", "crates/sel4-initialize-tls", @@ -112,6 +111,7 @@ members = [ "crates/sel4-logging", "crates/sel4-microkit", "crates/sel4-microkit/base", + "crates/sel4-microkit/embedded-hal-adapters", "crates/sel4-microkit/macros", "crates/sel4-microkit/message", "crates/sel4-microkit/message/types", diff --git a/crates/sel4-hal-adapters/Cargo.nix b/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix similarity index 89% rename from crates/sel4-hal-adapters/Cargo.nix rename to crates/sel4-microkit/embedded-hal-adapters/Cargo.nix index bd250b391..f292e5f68 100644 --- a/crates/sel4-hal-adapters/Cargo.nix +++ b/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix @@ -8,7 +8,7 @@ { mk, versions, localCrates, serdeWith, authors }: mk { - package.name = "sel4-hal-adapters"; + package.name = "sel4-microkit-embedded-hal-adapters"; package.authors = with authors; [ nspin "Ben Hamlin " diff --git a/crates/sel4-hal-adapters/Cargo.toml b/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml similarity index 78% rename from crates/sel4-hal-adapters/Cargo.toml rename to crates/sel4-microkit/embedded-hal-adapters/Cargo.toml index f1ba642a1..0c3bf58f6 100644 --- a/crates/sel4-hal-adapters/Cargo.toml +++ b/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml @@ -10,7 +10,7 @@ # [package] -name = "sel4-hal-adapters" +name = "sel4-microkit-embedded-hal-adapters" version = "0.1.0" authors = ["Nick Spinale ", "Ben Hamlin "] edition = "2021" @@ -20,6 +20,6 @@ license = "BSD-2-Clause" embedded-hal-nb = "1.0" heapless = "0.7.16" log = "0.4.17" -sel4-microkit = { path = "../sel4-microkit", default-features = false } -sel4-microkit-message = { path = "../sel4-microkit/message" } +sel4-microkit = { path = "..", default-features = false } +sel4-microkit-message = { path = "../message" } serde = { version = "1.0.147", default-features = false } diff --git a/crates/sel4-hal-adapters/src/lib.rs b/crates/sel4-microkit/embedded-hal-adapters/src/lib.rs similarity index 100% rename from crates/sel4-hal-adapters/src/lib.rs rename to crates/sel4-microkit/embedded-hal-adapters/src/lib.rs diff --git a/crates/sel4-hal-adapters/src/serial/client.rs b/crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs similarity index 100% rename from crates/sel4-hal-adapters/src/serial/client.rs rename to crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs diff --git a/crates/sel4-hal-adapters/src/serial/common.rs b/crates/sel4-microkit/embedded-hal-adapters/src/serial/common.rs similarity index 100% rename from crates/sel4-hal-adapters/src/serial/common.rs rename to crates/sel4-microkit/embedded-hal-adapters/src/serial/common.rs diff --git a/crates/sel4-hal-adapters/src/serial/driver.rs b/crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs similarity index 100% rename from crates/sel4-hal-adapters/src/serial/driver.rs rename to crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs diff --git a/crates/sel4-hal-adapters/src/serial/mod.rs b/crates/sel4-microkit/embedded-hal-adapters/src/serial/mod.rs similarity index 100% rename from crates/sel4-hal-adapters/src/serial/mod.rs rename to crates/sel4-microkit/embedded-hal-adapters/src/serial/mod.rs From 1ba20bbb2fdba87dc78b20e64c6e87ad5159a21f Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Wed, 12 Jun 2024 02:36:13 +0000 Subject: [PATCH 17/64] crates/sel4-microkit/smoltcp-device-adapter: Split into modules Signed-off-by: Nick Spinale --- .../smoltcp-device-adapter/src/client.rs | 30 +++ .../smoltcp-device-adapter/src/common.rs | 21 +++ .../smoltcp-device-adapter/src/driver.rs | 169 +++++++++++++++++ .../smoltcp-device-adapter/src/lib.rs | 177 +----------------- 4 files changed, 223 insertions(+), 174 deletions(-) create mode 100644 crates/sel4-microkit/smoltcp-device-adapter/src/client.rs create mode 100644 crates/sel4-microkit/smoltcp-device-adapter/src/common.rs create mode 100644 crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs new file mode 100644 index 000000000..053566d1f --- /dev/null +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs @@ -0,0 +1,30 @@ +// +// Copyright 2024, Colias Group, LLC +// +// SPDX-License-Identifier: BSD-2-Clause +// + +use sel4_microkit::{Channel, MessageInfo}; +use sel4_microkit_message::MessageInfoExt as _; + +use super::common::*; + +pub struct Client { + channel: Channel, +} + +impl Client { + pub fn new(channel: Channel) -> Self { + Self { channel } + } + + pub fn get_mac_address(&self) -> MacAddress { + let req = Request::GetMacAddress; + let resp: GetMacAddressResponse = self + .channel + .pp_call(MessageInfo::send_using_postcard(req).unwrap()) + .recv_using_postcard() + .unwrap(); + resp.mac_address + } +} diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs new file mode 100644 index 000000000..2dbadc6e1 --- /dev/null +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs @@ -0,0 +1,21 @@ +// +// Copyright 2023, Colias Group, LLC +// Copyright 2023, Galois, Inc. +// +// SPDX-License-Identifier: BSD-2-Clause +// + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct MacAddress(pub [u8; 6]); + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) enum Request { + GetMacAddress, +} + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct GetMacAddressResponse { + pub mac_address: MacAddress, +} diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs new file mode 100644 index 000000000..76c5df826 --- /dev/null +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs @@ -0,0 +1,169 @@ +// +// Copyright 2023, Colias Group, LLC +// Copyright 2023, Galois, Inc. +// +// SPDX-License-Identifier: BSD-2-Clause +// + +//! A generic microkit handler for implementors of [`smoltcp::phy::Device`]. + +use smoltcp::{ + phy::{self, RxToken, TxToken}, + time::Instant, +}; + +use sel4_externally_shared::ExternallySharedRef; +use sel4_microkit::{Channel, Handler, Infallible, MessageInfo}; +use sel4_microkit_message::MessageInfoExt as _; +use sel4_shared_ring_buffer::{roles::Use, RingBuffers}; + +use super::common::*; + +pub trait IrqAck { + fn irq_ack(&mut self); +} + +pub trait HasMac { + fn mac_address(&self) -> [u8; 6]; +} + +pub struct Driver { + dev: Device, + client_region: ExternallySharedRef<'static, [u8]>, + client_region_paddr: usize, + rx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, + tx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, + device_channel: Channel, + client_channel: Channel, +} + +impl Driver { + pub fn new( + dev: Device, + client_region: ExternallySharedRef<'static, [u8]>, + client_region_paddr: usize, + rx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, + tx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, + device_channel: Channel, + client_channel: Channel, + ) -> Self { + // XXX We could maybe initialize DMA here, so we don't need to do + // it in main. Also maybe initialize the ring buffers. + Self { + dev, + client_region, + client_region_paddr, + rx_ring_buffers, + tx_ring_buffers, + device_channel, + client_channel, + } + } +} + +impl Handler for Driver { + type Error = Infallible; + + fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { + if channel == self.device_channel || channel == self.client_channel { + let mut notify_rx = false; + + while !self.rx_ring_buffers.free_mut().is_empty().unwrap() { + let rx_tok = match self.dev.receive(Instant::ZERO) { + Some((rx_tok, _tx_tok)) => rx_tok, + None => break, + }; + + let desc = self.rx_ring_buffers.free_mut().dequeue().unwrap().unwrap(); + let desc_len = usize::try_from(desc.len()).unwrap(); + + rx_tok.consume(|rx_buf| { + assert!(desc_len >= rx_buf.len()); + let buf_range = { + let start = desc.encoded_addr() - self.client_region_paddr; + start..start + rx_buf.len() + }; + self.client_region + .as_mut_ptr() + .index(buf_range) + .copy_from_slice(&rx_buf); + }); + + self.rx_ring_buffers + .used_mut() + .enqueue(desc, true) + .unwrap() + .unwrap(); + notify_rx = true; + } + + if notify_rx { + self.rx_ring_buffers.notify().unwrap(); + } + + let mut notify_tx = false; + + while !self.tx_ring_buffers.free_mut().is_empty().unwrap() { + let tx_tok = match self.dev.transmit(Instant::ZERO) { + Some(tx_tok) => tx_tok, + None => break, + }; + + let desc = self.tx_ring_buffers.free_mut().dequeue().unwrap().unwrap(); + let tx_len = usize::try_from(desc.len()).unwrap(); + + tx_tok.consume(tx_len, |tx_buf| { + let buf_range = { + let start = desc.encoded_addr() - self.client_region_paddr; + start..start + tx_len + }; + self.client_region + .as_ptr() + .index(buf_range) + .copy_into_slice(tx_buf); + }); + + self.tx_ring_buffers + .used_mut() + .enqueue(desc, true) + .unwrap() + .unwrap(); + notify_tx = true; + } + + if notify_tx { + self.tx_ring_buffers.notify().unwrap(); + } + + self.dev.irq_ack(); + self.device_channel.irq_ack().unwrap(); + } else { + unreachable!() + } + + Ok(()) + } + + fn protected( + &mut self, + channel: Channel, + msg_info: MessageInfo, + ) -> Result { + Ok(if channel == self.client_channel { + match msg_info.recv_using_postcard::() { + Ok(req) => match req { + Request::GetMacAddress => { + let mac_address = self.dev.mac_address(); + MessageInfo::send_using_postcard(GetMacAddressResponse { + mac_address: MacAddress(mac_address), + }) + .unwrap() + } + }, + Err(_) => MessageInfo::send_unspecified_error(), + } + } else { + unreachable!() + }) + } +} diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs index b5bfa56ed..059c30108 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs @@ -7,178 +7,7 @@ #![no_std] -//! A generic microkit handler for implementors of [`smoltcp::phy::Device`]. +mod common; -use smoltcp::{ - phy::{self, RxToken, TxToken}, - time::Instant, -}; - -use serde::{Deserialize, Serialize}; - -use sel4_externally_shared::ExternallySharedRef; -use sel4_microkit::{Channel, Handler, Infallible, MessageInfo}; -use sel4_microkit_message::MessageInfoExt as _; -use sel4_shared_ring_buffer::{roles::Use, RingBuffers}; - -pub trait IrqAck { - fn irq_ack(&mut self); -} - -pub trait HasMac { - fn mac_address(&self) -> [u8; 6]; -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct MacAddress(pub [u8; 6]); - -#[derive(Debug, Serialize, Deserialize)] -pub enum Request { - GetMacAddress, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct GetMacAddressResponse { - pub mac_address: MacAddress, -} - -pub struct PhyDeviceHandler { - dev: Device, - client_region: ExternallySharedRef<'static, [u8]>, - client_region_paddr: usize, - rx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, - tx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, - device_channel: Channel, - client_channel: Channel, -} - -impl PhyDeviceHandler { - pub fn new( - dev: Device, - client_region: ExternallySharedRef<'static, [u8]>, - client_region_paddr: usize, - rx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, - tx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, - device_channel: Channel, - client_channel: Channel, - ) -> Self { - // XXX We could maybe initialize DMA here, so we don't need to do - // it in main. Also maybe initialize the ring buffers. - Self { - dev, - client_region, - client_region_paddr, - rx_ring_buffers, - tx_ring_buffers, - device_channel, - client_channel, - } - } -} - -impl Handler for PhyDeviceHandler { - type Error = Infallible; - - fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { - if channel == self.device_channel || channel == self.client_channel { - let mut notify_rx = false; - - while !self.rx_ring_buffers.free_mut().is_empty().unwrap() { - let rx_tok = match self.dev.receive(Instant::ZERO) { - Some((rx_tok, _tx_tok)) => rx_tok, - None => break, - }; - - let desc = self.rx_ring_buffers.free_mut().dequeue().unwrap().unwrap(); - let desc_len = usize::try_from(desc.len()).unwrap(); - - rx_tok.consume(|rx_buf| { - assert!(desc_len >= rx_buf.len()); - let buf_range = { - let start = desc.encoded_addr() - self.client_region_paddr; - start..start + rx_buf.len() - }; - self.client_region - .as_mut_ptr() - .index(buf_range) - .copy_from_slice(&rx_buf); - }); - - self.rx_ring_buffers - .used_mut() - .enqueue(desc, true) - .unwrap() - .unwrap(); - notify_rx = true; - } - - if notify_rx { - self.rx_ring_buffers.notify().unwrap(); - } - - let mut notify_tx = false; - - while !self.tx_ring_buffers.free_mut().is_empty().unwrap() { - let tx_tok = match self.dev.transmit(Instant::ZERO) { - Some(tx_tok) => tx_tok, - None => break, - }; - - let desc = self.tx_ring_buffers.free_mut().dequeue().unwrap().unwrap(); - let tx_len = usize::try_from(desc.len()).unwrap(); - - tx_tok.consume(tx_len, |tx_buf| { - let buf_range = { - let start = desc.encoded_addr() - self.client_region_paddr; - start..start + tx_len - }; - self.client_region - .as_ptr() - .index(buf_range) - .copy_into_slice(tx_buf); - }); - - self.tx_ring_buffers - .used_mut() - .enqueue(desc, true) - .unwrap() - .unwrap(); - notify_tx = true; - } - - if notify_tx { - self.tx_ring_buffers.notify().unwrap(); - } - - self.dev.irq_ack(); - self.device_channel.irq_ack().unwrap(); - } else { - unreachable!() - } - - Ok(()) - } - - fn protected( - &mut self, - channel: Channel, - msg_info: MessageInfo, - ) -> Result { - Ok(if channel == self.client_channel { - match msg_info.recv_using_postcard::() { - Ok(req) => match req { - Request::GetMacAddress => { - let mac_address = self.dev.mac_address(); - MessageInfo::send_using_postcard(GetMacAddressResponse { - mac_address: MacAddress(mac_address), - }) - .unwrap() - } - }, - Err(_) => MessageInfo::send_unspecified_error(), - } - } else { - unreachable!() - }) - } -} +pub mod client; +pub mod driver; From 099fa728f069ddb40b66a75feb596da307424834 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 11 Jun 2024 06:55:18 +0000 Subject: [PATCH 18/64] nix: Add non-ukit config for zcu102 Signed-off-by: Nick Spinale --- hacking/nix/scope/worlds.nix | 61 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/hacking/nix/scope/worlds.nix b/hacking/nix/scope/worlds.nix index ee3de6204..42cebfa7c 100644 --- a/hacking/nix/scope/worlds.nix +++ b/hacking/nix/scope/worlds.nix @@ -150,29 +150,46 @@ in rec { mkInstanceForPlatform = platUtils.rpi4.mkInstanceForPlatform; }; - zcu102 = mkWorld { - isMicrokit = true; - microkitConfig = { - board = "zcu102"; - config = "debug"; - }; - canSimulate = true; - mkInstanceForPlatform = platUtils.qemu.mkMkInstanceForPlatform { - mkQemuCmd = loader: [ - "${pkgsBuildBuild.this.qemuForSeL4Xilinx}/bin/qemu-system-aarch64" - # "${pkgsBuildBuild.this.qemuForSeL4}/bin/qemu-system-aarch64" - "-machine" "xlnx-zcu102" - # "-machine" "arm-generic-fdt" - # "-hw-dtb" "${pkgsBuildBuild.this.qemuForSeL4Xilinx.devicetrees}/SINGLE_ARCH//zcu102-arm.dtb" - "-m" "size=4G" - "-nographic" - "-serial" "mon:stdio" - "-device" "loader,file=${loader},addr=0x40000000,cpu-num=0" - "-device" "loader,addr=0xfd1a0104,data=0x0000000e,data-len=4" - ]; + zcu102 = + let + mk = { isMicrokit ? false }: + mkWorld ({ + inherit isMicrokit; + } // (if isMicrokit then { + microkitConfig = { + board = "zcu102"; + config = "debug"; + }; + } else { + kernelConfig = kernelConfigCommon // { + KernelSel4Arch = mkString "aarch64"; + KernelPlatform = mkString "zynqmp"; + KernelARMPlatform = mkString "zcu102"; + }; + }) // { + canSimulate = true; + mkInstanceForPlatform = platUtils.qemu.mkMkInstanceForPlatform { + mkQemuCmd = loader: [ + "${pkgsBuildBuild.this.qemuForSeL4Xilinx}/bin/qemu-system-aarch64" + # "${pkgsBuildBuild.this.qemuForSeL4}/bin/qemu-system-aarch64" + "-machine" "xlnx-zcu102" + # "-machine" "arm-generic-fdt" + # "-hw-dtb" "${pkgsBuildBuild.this.qemuForSeL4Xilinx.devicetrees}/SINGLE_ARCH//zcu102-arm.dtb" + "-m" "size=4G" + "-nographic" + "-serial" "mon:stdio" + ] ++ (if isMicrokit then [ + "-device" "loader,file=${loader},addr=0x40000000,cpu-num=0" + "-device" "loader,addr=0xfd1a0104,data=0x0000000e,data-len=4" + ] else [ + "-kernel" loader + ]); + }; + }); + in { + default = mk {}; + microkit = mk { isMicrokit = true; }; }; - - }; }; aarch32 = From dfd9d5f5405c9faaeae55c2dc4f139e125b80674 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Wed, 12 Jun 2024 02:45:28 +0000 Subject: [PATCH 19/64] Use driver abstractions in examples Signed-off-by: Nick Spinale --- Cargo.lock | 64 +++++---- Cargo.toml | 8 +- .../sel4-driver-traits}/Cargo.nix | 6 +- .../sel4-driver-traits}/Cargo.toml | 4 +- crates/drivers/sel4-driver-traits/src/lib.rs | 20 +++ crates/drivers/sel4-pl011-driver/Cargo.nix | 15 +++ .../sel4-pl011-driver}/Cargo.toml | 4 +- .../sel4-pl011-driver}/src/device.rs | 0 crates/drivers/sel4-pl011-driver/src/lib.rs | 64 +++++++++ .../Cargo.nix | 17 +++ .../Cargo.toml | 11 +- .../src/lib.rs | 111 ++++++++++++++++ .../examples/microkit/banscii/banscii.system | 2 +- .../microkit/banscii/pds/assistant/Cargo.nix | 5 +- .../microkit/banscii/pds/assistant/Cargo.toml | 5 +- .../banscii/pds/assistant/src/main.rs | 93 ++++++-------- .../banscii/pds/pl011-driver/core/src/lib.rs | 42 ------ .../pl011-driver/interface-types/Cargo.nix | 14 -- .../pl011-driver/interface-types/src/lib.rs | 20 --- .../banscii/pds/pl011-driver/src/main.rs | 92 ------------- .../{pl011-driver => serial-driver}/Cargo.nix | 7 +- .../Cargo.toml | 9 +- .../banscii/pds/serial-driver/src/main.rs | 23 ++++ .../microkit/http-server/pds/server/Cargo.nix | 2 +- .../http-server/pds/server/Cargo.toml | 6 +- .../http-server/pds/server/src/main.rs | 3 +- .../http-server/pds/server/src/net_client.rs | 30 ----- .../pds/virtio-net-driver/Cargo.nix | 3 +- .../pds/virtio-net-driver/Cargo.toml | 7 +- .../interface-types/Cargo.nix | 14 -- .../interface-types/src/lib.rs | 22 ---- .../pds/virtio-net-driver/src/main.rs | 121 ++---------------- .../embedded-hal-adapters/Cargo.nix | 1 + .../embedded-hal-adapters/Cargo.toml | 1 + .../src/serial/client.rs | 2 +- .../src/serial/driver.rs | 12 +- .../smoltcp-device-adapter/Cargo.nix | 1 + .../smoltcp-device-adapter/Cargo.toml | 1 + .../smoltcp-device-adapter/src/client.rs | 1 + .../smoltcp-device-adapter/src/common.rs | 3 +- .../smoltcp-device-adapter/src/driver.rs | 35 ++--- .../instances/microkit/banscii/default.nix | 6 +- 42 files changed, 410 insertions(+), 497 deletions(-) rename crates/{examples/microkit/banscii/pds/pl011-driver/core => drivers/sel4-driver-traits}/Cargo.nix (52%) rename crates/{examples/microkit/banscii/pds/pl011-driver/interface-types => drivers/sel4-driver-traits}/Cargo.toml (80%) create mode 100644 crates/drivers/sel4-driver-traits/src/lib.rs create mode 100644 crates/drivers/sel4-pl011-driver/Cargo.nix rename crates/{examples/microkit/banscii/pds/pl011-driver/core => drivers/sel4-pl011-driver}/Cargo.toml (81%) rename crates/{examples/microkit/banscii/pds/pl011-driver/core => drivers/sel4-pl011-driver}/src/device.rs (100%) create mode 100644 crates/drivers/sel4-pl011-driver/src/lib.rs create mode 100644 crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix rename crates/{examples/microkit/http-server/pds/virtio-net-driver/interface-types => drivers/sel4-virtio-net-driver-for-smoltcp}/Cargo.toml (55%) create mode 100644 crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs delete mode 100644 crates/examples/microkit/banscii/pds/pl011-driver/core/src/lib.rs delete mode 100644 crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.nix delete mode 100644 crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs delete mode 100644 crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs rename crates/examples/microkit/banscii/pds/{pl011-driver => serial-driver}/Cargo.nix (65%) rename crates/examples/microkit/banscii/pds/{pl011-driver => serial-driver}/Cargo.toml (73%) create mode 100644 crates/examples/microkit/banscii/pds/serial-driver/src/main.rs delete mode 100644 crates/examples/microkit/http-server/pds/server/src/net_client.rs delete mode 100644 crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.nix delete mode 100644 crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index e7050ba02..d7f56f75f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -184,10 +184,11 @@ version = "0.1.0" dependencies = [ "banscii-artist-interface-types", "banscii-assistant-core", - "banscii-pl011-driver-interface-types", + "embedded-hal-nb", "hex", "sel4-externally-shared", "sel4-microkit", + "sel4-microkit-embedded-hal-adapters", "sel4-microkit-message", ] @@ -210,28 +211,13 @@ dependencies = [ ] [[package]] -name = "banscii-pl011-driver" +name = "banscii-serial-driver" version = "0.1.0" dependencies = [ - "banscii-pl011-driver-core", - "banscii-pl011-driver-interface-types", - "heapless", "sel4-microkit", + "sel4-microkit-embedded-hal-adapters", "sel4-microkit-message", -] - -[[package]] -name = "banscii-pl011-driver-core" -version = "0.1.0" -dependencies = [ - "tock-registers", -] - -[[package]] -name = "banscii-pl011-driver-interface-types" -version = "0.1.0" -dependencies = [ - "serde", + "sel4-pl011-driver", ] [[package]] @@ -1145,7 +1131,6 @@ dependencies = [ "microkit-http-server-example-server-core", "microkit-http-server-example-sp804-driver-interface-types", "microkit-http-server-example-virtio-blk-driver-interface-types", - "microkit-http-server-example-virtio-net-driver-interface-types", "rcgen", "sel4", "sel4-async-block-io", @@ -1159,6 +1144,7 @@ dependencies = [ "sel4-logging", "sel4-microkit", "sel4-microkit-message", + "sel4-microkit-smoltcp-device-adapter", "sel4-newlib", "sel4-shared-ring-buffer", "sel4-shared-ring-buffer-block-io", @@ -1248,7 +1234,6 @@ name = "microkit-http-server-example-virtio-net-driver" version = "0.1.0" dependencies = [ "log", - "microkit-http-server-example-virtio-net-driver-interface-types", "sel4", "sel4-bounce-buffer-allocator", "sel4-externally-shared", @@ -1256,18 +1241,13 @@ dependencies = [ "sel4-logging", "sel4-microkit", "sel4-microkit-message", + "sel4-microkit-smoltcp-device-adapter", "sel4-shared-ring-buffer", "sel4-virtio-hal-impl", + "sel4-virtio-net-driver-for-smoltcp", "virtio-drivers", ] -[[package]] -name = "microkit-http-server-example-virtio-net-driver-interface-types" -version = "0.1.0" -dependencies = [ - "serde", -] - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -2288,6 +2268,13 @@ dependencies = [ "lock_api", ] +[[package]] +name = "sel4-driver-traits" +version = "0.1.0" +dependencies = [ + "serde", +] + [[package]] name = "sel4-elf-header" version = "0.1.0" @@ -2444,6 +2431,7 @@ dependencies = [ "embedded-hal-nb", "heapless", "log", + "sel4-driver-traits", "sel4-microkit", "sel4-microkit-message", "serde", @@ -2483,6 +2471,7 @@ version = "0.1.0" dependencies = [ "log", "sel4-bounce-buffer-allocator", + "sel4-driver-traits", "sel4-externally-shared", "sel4-microkit", "sel4-microkit-message", @@ -2527,6 +2516,15 @@ dependencies = [ name = "sel4-panicking-env" version = "0.1.0" +[[package]] +name = "sel4-pl011-driver" +version = "0.1.0" +dependencies = [ + "embedded-hal-nb", + "sel4-driver-traits", + "tock-registers", +] + [[package]] name = "sel4-platform-info" version = "0.1.0" @@ -2778,6 +2776,16 @@ dependencies = [ "virtio-drivers", ] +[[package]] +name = "sel4-virtio-net-driver-for-smoltcp" +version = "0.1.0" +dependencies = [ + "log", + "sel4-driver-traits", + "smoltcp", + "virtio-drivers", +] + [[package]] name = "semver" version = "1.0.22" diff --git a/Cargo.toml b/Cargo.toml index 61e309cda..d6267d9e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,15 +13,16 @@ default-members = [] resolver = "2" members = [ + "crates/drivers/sel4-driver-traits", + "crates/drivers/sel4-pl011-driver", "crates/drivers/sel4-virtio-hal-impl", + "crates/drivers/sel4-virtio-net-driver-for-smoltcp", "crates/examples/microkit/banscii/pds/artist", "crates/examples/microkit/banscii/pds/artist/interface-types", "crates/examples/microkit/banscii/pds/assistant", "crates/examples/microkit/banscii/pds/assistant/core", "crates/examples/microkit/banscii/pds/assistant/core/test", - "crates/examples/microkit/banscii/pds/pl011-driver", - "crates/examples/microkit/banscii/pds/pl011-driver/core", - "crates/examples/microkit/banscii/pds/pl011-driver/interface-types", + "crates/examples/microkit/banscii/pds/serial-driver", "crates/examples/microkit/hello/pds/hello", "crates/examples/microkit/http-server/pds/pl031-driver", "crates/examples/microkit/http-server/pds/pl031-driver/core", @@ -34,7 +35,6 @@ members = [ "crates/examples/microkit/http-server/pds/virtio-blk-driver", "crates/examples/microkit/http-server/pds/virtio-blk-driver/interface-types", "crates/examples/microkit/http-server/pds/virtio-net-driver", - "crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types", "crates/examples/root-task/example-root-task", "crates/examples/root-task/example-root-task-without-runtime", "crates/examples/root-task/hello", diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/core/Cargo.nix b/crates/drivers/sel4-driver-traits/Cargo.nix similarity index 52% rename from crates/examples/microkit/banscii/pds/pl011-driver/core/Cargo.nix rename to crates/drivers/sel4-driver-traits/Cargo.nix index 80a6de944..d89ae04a8 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/core/Cargo.nix +++ b/crates/drivers/sel4-driver-traits/Cargo.nix @@ -4,11 +4,11 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ mk, versions }: +{ mk, serdeWith }: mk { - package.name = "banscii-pl011-driver-core"; + package.name = "sel4-driver-traits"; dependencies = { - inherit (versions) tock-registers; + serde = serdeWith [ "derive" ]; }; } diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.toml b/crates/drivers/sel4-driver-traits/Cargo.toml similarity index 80% rename from crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.toml rename to crates/drivers/sel4-driver-traits/Cargo.toml index bb1ee0c08..1668801a2 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.toml +++ b/crates/drivers/sel4-driver-traits/Cargo.toml @@ -10,11 +10,11 @@ # [package] -name = "banscii-pl011-driver-interface-types" +name = "sel4-driver-traits" version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" license = "BSD-2-Clause" [dependencies] -serde = { version = "1.0.147", default-features = false } +serde = { version = "1.0.147", default-features = false, features = ["derive"] } diff --git a/crates/drivers/sel4-driver-traits/src/lib.rs b/crates/drivers/sel4-driver-traits/src/lib.rs new file mode 100644 index 000000000..4c5019ebf --- /dev/null +++ b/crates/drivers/sel4-driver-traits/src/lib.rs @@ -0,0 +1,20 @@ +// +// Copyright 2024, Colias Group, LLC +// +// SPDX-License-Identifier: BSD-2-Clause +// + +#![no_std] + +use serde::{Deserialize, Serialize}; + +pub trait HandleInterrupt { + fn handle_interrupt(&mut self); +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub struct MacAddress(pub [u8; 6]); + +pub trait GetMacAddress { + fn get_mac_address(&mut self) -> MacAddress; +} diff --git a/crates/drivers/sel4-pl011-driver/Cargo.nix b/crates/drivers/sel4-pl011-driver/Cargo.nix new file mode 100644 index 000000000..901104115 --- /dev/null +++ b/crates/drivers/sel4-pl011-driver/Cargo.nix @@ -0,0 +1,15 @@ +# +# Copyright 2023, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# + +{ mk, versions, localCrates }: + +mk { + package.name = "sel4-pl011-driver"; + dependencies = { + inherit (versions) tock-registers embedded-hal-nb; + inherit (localCrates) sel4-driver-traits; + }; +} diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/core/Cargo.toml b/crates/drivers/sel4-pl011-driver/Cargo.toml similarity index 81% rename from crates/examples/microkit/banscii/pds/pl011-driver/core/Cargo.toml rename to crates/drivers/sel4-pl011-driver/Cargo.toml index f323ffa80..81073bfc2 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/core/Cargo.toml +++ b/crates/drivers/sel4-pl011-driver/Cargo.toml @@ -10,11 +10,13 @@ # [package] -name = "banscii-pl011-driver-core" +name = "sel4-pl011-driver" version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" license = "BSD-2-Clause" [dependencies] +embedded-hal-nb = "1.0" +sel4-driver-traits = { path = "../sel4-driver-traits" } tock-registers = "0.8.1" diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/core/src/device.rs b/crates/drivers/sel4-pl011-driver/src/device.rs similarity index 100% rename from crates/examples/microkit/banscii/pds/pl011-driver/core/src/device.rs rename to crates/drivers/sel4-pl011-driver/src/device.rs diff --git a/crates/drivers/sel4-pl011-driver/src/lib.rs b/crates/drivers/sel4-pl011-driver/src/lib.rs new file mode 100644 index 000000000..d70624cd2 --- /dev/null +++ b/crates/drivers/sel4-pl011-driver/src/lib.rs @@ -0,0 +1,64 @@ +// +// Copyright 2023, Colias Group, LLC +// +// SPDX-License-Identifier: BSD-2-Clause +// + +#![no_std] + +use core::convert::Infallible; + +use embedded_hal_nb::nb; +use embedded_hal_nb::serial; + +use sel4_driver_traits::HandleInterrupt; + +mod device; + +use device::Device; + +pub struct Driver { + device: Device, +} + +impl Driver { + #[allow(clippy::missing_safety_doc)] + pub unsafe fn new(ptr: *mut ()) -> Self { + let mut this = Self { + device: Device::new(ptr.cast()), + }; + this.init(); + this + } + + pub fn init(&mut self) { + self.device.init(); + } +} + +impl serial::ErrorType for Driver { + type Error = Infallible; +} + +impl serial::Read for Driver { + fn read(&mut self) -> nb::Result { + self.device.get_char().ok_or(nb::Error::WouldBlock) + } +} + +impl serial::Write for Driver { + fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + self.device.put_char(word); + Ok(()) + } + + fn flush(&mut self) -> nb::Result<(), Self::Error> { + Ok(()) + } +} + +impl HandleInterrupt for Driver { + fn handle_interrupt(&mut self) { + self.device.clear_all_interrupts() + } +} diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix new file mode 100644 index 000000000..9792a9f72 --- /dev/null +++ b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix @@ -0,0 +1,17 @@ +# +# Copyright 2024, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# + +{ mk, versions, localCrates, smoltcpWith, virtioDriversWith }: + +mk { + package.name = "sel4-virtio-net-driver-for-smoltcp"; + dependencies = { + inherit (versions) log; + smoltcp = smoltcpWith []; + virtio-drivers = virtioDriversWith [ "alloc" ]; + inherit (localCrates) sel4-driver-traits; + }; +} diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.toml b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml similarity index 55% rename from crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.toml rename to crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml index 1c7696b08..76f741a9b 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.toml +++ b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml @@ -10,11 +10,18 @@ # [package] -name = "microkit-http-server-example-virtio-net-driver-interface-types" +name = "sel4-virtio-net-driver-for-smoltcp" version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" license = "BSD-2-Clause" [dependencies] -serde = { version = "1.0.147", default-features = false } +log = "0.4.17" +sel4-driver-traits = { path = "../sel4-driver-traits" } +virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } + +[dependencies.smoltcp] +version = "0.10.0" +default-features = false +features = ["proto-ipv4", "proto-dhcpv4", "proto-dns", "socket-dhcpv4", "socket-dns", "socket-tcp"] diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs new file mode 100644 index 000000000..e9d76bde7 --- /dev/null +++ b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs @@ -0,0 +1,111 @@ +// +// Copyright 2023, Colias Group, LLC +// +// SPDX-License-Identifier: BSD-2-Clause +// + +#![no_std] + +extern crate alloc; + +use alloc::rc::Rc; +use core::cell::RefCell; + +use log::trace; +use smoltcp::phy::{Device, DeviceCapabilities, Medium, RxToken, TxToken}; +use smoltcp::time::Instant; +use virtio_drivers::device::net::{RxBuffer, VirtIONet}; +use virtio_drivers::{transport::Transport, Error, Hal}; + +use sel4_driver_traits::{GetMacAddress, HandleInterrupt, MacAddress}; + +pub const NET_QUEUE_SIZE: usize = 16; + +pub type DeviceImpl = VirtIONet; + +pub struct DeviceWrapper { + inner: Rc>>, +} + +impl DeviceWrapper { + pub fn new(dev: DeviceImpl) -> Self { + DeviceWrapper { + inner: Rc::new(RefCell::new(dev)), + } + } +} + +impl HandleInterrupt for DeviceWrapper { + fn handle_interrupt(&mut self) { + self.inner.borrow_mut().ack_interrupt(); + } +} + +impl GetMacAddress for DeviceWrapper { + fn get_mac_address(&mut self) -> MacAddress { + MacAddress(self.inner.borrow().mac_address()) + } +} + +impl Device for DeviceWrapper { + type RxToken<'a> = VirtioRxToken where Self: 'a; + type TxToken<'a> = VirtioTxToken where Self: 'a; + + fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { + match self.inner.borrow_mut().receive() { + Ok(buf) => Some(( + VirtioRxToken(self.inner.clone(), buf), + VirtioTxToken(self.inner.clone()), + )), + Err(Error::NotReady) => None, + Err(err) => panic!("receive failed: {}", err), + } + } + + fn transmit(&mut self, _timestamp: Instant) -> Option> { + Some(VirtioTxToken(self.inner.clone())) + } + + fn capabilities(&self) -> DeviceCapabilities { + let mut caps = DeviceCapabilities::default(); + caps.max_transmission_unit = 1536; + caps.max_burst_size = Some(1); + caps.medium = Medium::Ethernet; + caps + } +} + +pub struct VirtioRxToken(Rc>>, RxBuffer); + +impl RxToken for VirtioRxToken { + fn consume(self, f: F) -> R + where + F: FnOnce(&mut [u8]) -> R, + { + let mut rx_buf = self.1; + trace!( + "RECV {} bytes: {:02X?}", + rx_buf.packet_len(), + rx_buf.packet() + ); + let result = f(rx_buf.packet_mut()); + self.0.borrow_mut().recycle_rx_buffer(rx_buf).unwrap(); + result + } +} + +pub struct VirtioTxToken(Rc>>); + +impl TxToken for VirtioTxToken { + fn consume(self, len: usize, f: F) -> R + where + F: FnOnce(&mut [u8]) -> R, + { + let mut dev = self.0.borrow_mut(); + let mut tx_buf = dev.new_tx_buffer(len); + let result = f(tx_buf.packet_mut()); + trace!("SEND {} bytes: {:02X?}", len, tx_buf.packet()); + dev.send(tx_buf).unwrap(); + result + } +} diff --git a/crates/examples/microkit/banscii/banscii.system b/crates/examples/microkit/banscii/banscii.system index 6212de86f..c946ea1d6 100644 --- a/crates/examples/microkit/banscii/banscii.system +++ b/crates/examples/microkit/banscii/banscii.system @@ -12,7 +12,7 @@ - + diff --git a/crates/examples/microkit/banscii/pds/assistant/Cargo.nix b/crates/examples/microkit/banscii/pds/assistant/Cargo.nix index f17dccdd7..36c5a3fe0 100644 --- a/crates/examples/microkit/banscii/pds/assistant/Cargo.nix +++ b/crates/examples/microkit/banscii/pds/assistant/Cargo.nix @@ -4,16 +4,17 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ mk, localCrates }: +{ mk, versions, localCrates }: mk { package.name = "banscii-assistant"; dependencies = { + inherit (versions) embedded-hal-nb; hex = { version = "0.4.3"; default-features = false; features = [ "alloc" ]; }; inherit (localCrates) sel4-microkit-message + sel4-microkit-embedded-hal-adapters banscii-assistant-core - banscii-pl011-driver-interface-types banscii-artist-interface-types ; sel4-externally-shared = localCrates.sel4-externally-shared // { features = [ "unstable" ]; }; diff --git a/crates/examples/microkit/banscii/pds/assistant/Cargo.toml b/crates/examples/microkit/banscii/pds/assistant/Cargo.toml index 6f6c1bc6f..5bb70c532 100644 --- a/crates/examples/microkit/banscii/pds/assistant/Cargo.toml +++ b/crates/examples/microkit/banscii/pds/assistant/Cargo.toml @@ -19,7 +19,7 @@ license = "BSD-2-Clause" [dependencies] banscii-artist-interface-types = { path = "../artist/interface-types" } banscii-assistant-core = { path = "core" } -banscii-pl011-driver-interface-types = { path = "../pl011-driver/interface-types" } +embedded-hal-nb = "1.0" hex = { version = "0.4.3", default-features = false, features = ["alloc"] } sel4-externally-shared = { path = "../../../../../sel4-externally-shared", features = ["unstable"] } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } @@ -28,3 +28,6 @@ sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } path = "../../../../../sel4-microkit" default-features = false features = ["alloc"] + +[dependencies.sel4-microkit-embedded-hal-adapters] +path = "../../../../../sel4-microkit/embedded-hal-adapters" diff --git a/crates/examples/microkit/banscii/pds/assistant/src/main.rs b/crates/examples/microkit/banscii/pds/assistant/src/main.rs index c2885f9c6..025f3a972 100644 --- a/crates/examples/microkit/banscii/pds/assistant/src/main.rs +++ b/crates/examples/microkit/banscii/pds/assistant/src/main.rs @@ -11,11 +11,12 @@ extern crate alloc; use alloc::vec; use alloc::vec::Vec; -use core::fmt; use core::fmt::Write; use core::mem; use core::str; +use embedded_hal_nb::serial::{self, Read as _, Write as _}; + use sel4_externally_shared::{ access::{ReadOnly, ReadWrite}, ExternallySharedRef, ExternallySharedRefExt, @@ -23,13 +24,15 @@ use sel4_externally_shared::{ use sel4_microkit::{ memory_region_symbol, protection_domain, Channel, Handler, Infallible, MessageInfo, }; +use sel4_microkit_embedded_hal_adapters::serial::client::{ + Client as SerialClient, Error as SerialClientError, +}; use sel4_microkit_message::MessageInfoExt as _; use banscii_artist_interface_types as artist; use banscii_assistant_core::Draft; -use banscii_pl011_driver_interface_types as pl011_driver; -const PL011_DRIVER: Channel = Channel::new(0); +const SERIAL_DRIVER: Channel = Channel::new(0); const ARTIST: Channel = Channel::new(1); const REGION_SIZE: usize = 0x4_000; @@ -48,16 +51,20 @@ fn init() -> impl Handler { ) }; - prompt(); - - HandlerImpl { + let mut this = HandlerImpl { + serial_client: SerialClient::new(SERIAL_DRIVER), region_in, region_out, buffer: Vec::new(), - } + }; + + this.prompt(); + + this } struct HandlerImpl { + serial_client: SerialClient, region_in: ExternallySharedRef<'static, [u8], ReadOnly>, region_out: ExternallySharedRef<'static, [u8], ReadWrite>, buffer: Vec, @@ -68,23 +75,23 @@ impl Handler for HandlerImpl { fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { match channel { - PL011_DRIVER => { - while let Some(b) = get_char() { + SERIAL_DRIVER => { + while let Ok(b) = self.serial_client.read() { if let b'\n' | b'\r' = b { - newline(); + self.newline(); if !self.buffer.is_empty() { self.try_create(); } - prompt(); + self.prompt(); } else { let c = char::from(b); if c.is_ascii() && !c.is_ascii_control() { if self.buffer.len() == MAX_SUBJECT_LEN { - writeln!(PutCharWrite, "\n(char limit reached)").unwrap(); + writeln!(self.writer(), "\n(char limit reached)").unwrap(); self.try_create(); - prompt(); + self.prompt(); } - put_char(b); + self.serial_client.write(b).unwrap(); self.buffer.push(b); } } @@ -107,7 +114,7 @@ impl HandlerImpl { self.create(subject); } Err(_) => { - writeln!(PutCharWrite, "error: input is not valid utf-8").unwrap(); + writeln!(self.writer(), "error: input is not valid utf-8").unwrap(); } }; self.buffer.clear(); @@ -158,62 +165,36 @@ impl HandlerImpl { buf }; - newline(); + self.newline(); for row in 0..height { for col in 0..width { let i = row * width + col; let b = pixel_data[i]; - put_char(b); + self.serial_client.write(b).unwrap(); } - newline(); + self.newline(); } - newline(); + self.newline(); - writeln!(PutCharWrite, "Signature:").unwrap(); + writeln!(self.writer(), "Signature:").unwrap(); for line in signature.chunks(32) { - writeln!(PutCharWrite, "{}", hex::encode(line)).unwrap(); + writeln!(self.writer(), "{}", hex::encode(line)).unwrap(); } - newline(); + self.newline(); } -} - -fn prompt() { - write!(PutCharWrite, "banscii> ").unwrap(); -} -fn newline() { - writeln!(PutCharWrite).unwrap(); -} - -fn get_char() -> Option { - let req = pl011_driver::Request::GetChar; - let resp: pl011_driver::GetCharResponse = PL011_DRIVER - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard() - .unwrap(); - resp.val -} - -fn put_char(val: u8) { - let req = pl011_driver::Request::PutChar { val }; - PL011_DRIVER - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_empty() - .unwrap(); -} - -fn put_str(s: &str) { - s.as_bytes().iter().copied().for_each(put_char) -} + fn prompt(&mut self) { + write!(self.writer(), "banscii> ").unwrap(); + } -struct PutCharWrite; + fn newline(&mut self) { + writeln!(self.writer()).unwrap(); + } -impl fmt::Write for PutCharWrite { - fn write_str(&mut self, s: &str) -> fmt::Result { - put_str(s); - Ok(()) + fn writer(&mut self) -> &mut dyn serial::Write { + &mut self.serial_client as &mut dyn serial::Write } } diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/core/src/lib.rs b/crates/examples/microkit/banscii/pds/pl011-driver/core/src/lib.rs deleted file mode 100644 index a90ed7e55..000000000 --- a/crates/examples/microkit/banscii/pds/pl011-driver/core/src/lib.rs +++ /dev/null @@ -1,42 +0,0 @@ -// -// Copyright 2023, Colias Group, LLC -// -// SPDX-License-Identifier: BSD-2-Clause -// - -#![no_std] - -mod device; - -use device::Device; - -pub struct Driver { - device: Device, -} - -impl Driver { - #[allow(clippy::missing_safety_doc)] - pub unsafe fn new(ptr: *mut ()) -> Self { - let mut this = Self { - device: Device::new(ptr.cast()), - }; - this.init(); - this - } - - fn init(&mut self) { - self.device.init(); - } - - pub fn put_char(&self, c: u8) { - self.device.put_char(c) - } - - pub fn get_char(&self) -> Option { - self.device.get_char() - } - - pub fn handle_interrupt(&self) { - self.device.clear_all_interrupts() - } -} diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.nix b/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.nix deleted file mode 100644 index 2f76b762e..000000000 --- a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/Cargo.nix +++ /dev/null @@ -1,14 +0,0 @@ -# -# Copyright 2023, Colias Group, LLC -# -# SPDX-License-Identifier: BSD-2-Clause -# - -{ mk, versions, serdeWith }: - -mk { - package.name = "banscii-pl011-driver-interface-types"; - dependencies = { - serde = serdeWith []; - }; -} diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs b/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs deleted file mode 100644 index b9e27f5bf..000000000 --- a/crates/examples/microkit/banscii/pds/pl011-driver/interface-types/src/lib.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright 2023, Colias Group, LLC -// -// SPDX-License-Identifier: BSD-2-Clause -// - -#![no_std] - -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub enum Request { - PutChar { val: u8 }, - GetChar, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct GetCharResponse { - pub val: Option, -} diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs b/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs deleted file mode 100644 index c5a5c9545..000000000 --- a/crates/examples/microkit/banscii/pds/pl011-driver/src/main.rs +++ /dev/null @@ -1,92 +0,0 @@ -// -// Copyright 2023, Colias Group, LLC -// -// SPDX-License-Identifier: BSD-2-Clause -// - -#![no_std] -#![no_main] - -use heapless::Deque; - -use sel4_microkit::{ - memory_region_symbol, protection_domain, Channel, Handler, Infallible, MessageInfo, -}; -use sel4_microkit_message::MessageInfoExt as _; - -use banscii_pl011_driver_core::Driver; -use banscii_pl011_driver_interface_types::*; - -const DEVICE: Channel = Channel::new(0); -const ASSISTANT: Channel = Channel::new(1); - -#[protection_domain] -fn init() -> HandlerImpl { - let driver = - unsafe { Driver::new(memory_region_symbol!(pl011_register_block: *mut ()).as_ptr()) }; - HandlerImpl { - driver, - buffer: Deque::new(), - notify: true, - } -} - -struct HandlerImpl { - driver: Driver, - buffer: Deque, - notify: bool, -} - -impl Handler for HandlerImpl { - type Error = Infallible; - - fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { - match channel { - DEVICE => { - while let Some(c) = self.driver.get_char() { - if self.buffer.push_back(c).is_err() { - break; - } - } - self.driver.handle_interrupt(); - DEVICE.irq_ack().unwrap(); - if self.notify { - ASSISTANT.notify(); - self.notify = false; - } - } - _ => { - unreachable!() - } - } - Ok(()) - } - - fn protected( - &mut self, - channel: Channel, - msg_info: MessageInfo, - ) -> Result { - Ok(match channel { - ASSISTANT => match msg_info.recv_using_postcard::() { - Ok(req) => match req { - Request::PutChar { val } => { - self.driver.put_char(val); - MessageInfo::send_empty() - } - Request::GetChar => { - let val = self.buffer.pop_front(); - if val.is_some() { - self.notify = true; - } - MessageInfo::send_using_postcard(GetCharResponse { val }).unwrap() - } - }, - Err(_) => MessageInfo::send_unspecified_error(), - }, - _ => { - unreachable!() - } - }) - } -} diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/Cargo.nix b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix similarity index 65% rename from crates/examples/microkit/banscii/pds/pl011-driver/Cargo.nix rename to crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix index 37c8a91b6..18b03492d 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/Cargo.nix +++ b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix @@ -7,13 +7,12 @@ { mk, localCrates, versions }: mk { - package.name = "banscii-pl011-driver"; + package.name = "banscii-serial-driver"; dependencies = { - inherit (versions) heapless; inherit (localCrates) sel4-microkit-message - banscii-pl011-driver-core - banscii-pl011-driver-interface-types + sel4-microkit-embedded-hal-adapters + sel4-pl011-driver ; sel4-microkit = localCrates.sel4-microkit // { default-features = false; }; }; diff --git a/crates/examples/microkit/banscii/pds/pl011-driver/Cargo.toml b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml similarity index 73% rename from crates/examples/microkit/banscii/pds/pl011-driver/Cargo.toml rename to crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml index 28db6aa18..9c298f4df 100644 --- a/crates/examples/microkit/banscii/pds/pl011-driver/Cargo.toml +++ b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml @@ -10,15 +10,16 @@ # [package] -name = "banscii-pl011-driver" +name = "banscii-serial-driver" version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" license = "BSD-2-Clause" [dependencies] -banscii-pl011-driver-core = { path = "core" } -banscii-pl011-driver-interface-types = { path = "interface-types" } -heapless = "0.7.16" sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } +sel4-pl011-driver = { path = "../../../../../drivers/sel4-pl011-driver" } + +[dependencies.sel4-microkit-embedded-hal-adapters] +path = "../../../../../sel4-microkit/embedded-hal-adapters" diff --git a/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs b/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs new file mode 100644 index 000000000..e99aa9af7 --- /dev/null +++ b/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs @@ -0,0 +1,23 @@ +// +// Copyright 2023, Colias Group, LLC +// +// SPDX-License-Identifier: BSD-2-Clause +// + +#![no_std] +#![no_main] + +use sel4_microkit::{memory_region_symbol, protection_domain, Channel}; +use sel4_microkit_embedded_hal_adapters::serial::driver::Driver; +use sel4_pl011_driver::Driver as DriverImpl; + +const DEVICE: Channel = Channel::new(0); +const ASSISTANT: Channel = Channel::new(1); + +#[protection_domain] +fn init() -> Driver { + let mut driver_impl = + unsafe { DriverImpl::new(memory_region_symbol!(pl011_register_block: *mut ()).as_ptr()) }; + driver_impl.init(); + Driver::new(driver_impl, DEVICE, ASSISTANT) +} diff --git a/crates/examples/microkit/http-server/pds/server/Cargo.nix b/crates/examples/microkit/http-server/pds/server/Cargo.nix index e47df6ebc..497dae44d 100644 --- a/crates/examples/microkit/http-server/pds/server/Cargo.nix +++ b/crates/examples/microkit/http-server/pds/server/Cargo.nix @@ -32,6 +32,7 @@ mk { sel4-logging sel4-immediate-sync-once-cell sel4-microkit-message + sel4-microkit-smoltcp-device-adapter sel4-async-single-threaded-executor sel4-async-network sel4-async-time @@ -46,7 +47,6 @@ mk { microkit-http-server-example-pl031-driver-interface-types microkit-http-server-example-sp804-driver-interface-types microkit-http-server-example-virtio-blk-driver-interface-types - microkit-http-server-example-virtio-net-driver-interface-types ; sel4-newlib = localCrates.sel4-newlib // { diff --git a/crates/examples/microkit/http-server/pds/server/Cargo.toml b/crates/examples/microkit/http-server/pds/server/Cargo.toml index f6c6e7b36..b322e0cb6 100644 --- a/crates/examples/microkit/http-server/pds/server/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/server/Cargo.toml @@ -45,9 +45,6 @@ path = "../sp804-driver/interface-types" [dependencies.microkit-http-server-example-virtio-blk-driver-interface-types] path = "../virtio-blk-driver/interface-types" -[dependencies.microkit-http-server-example-virtio-net-driver-interface-types] -path = "../virtio-net-driver/interface-types" - [dependencies.sel4-async-single-threaded-executor] path = "../../../../../sel4-async/single-threaded-executor" @@ -56,6 +53,9 @@ path = "../../../../../sel4-microkit" default-features = false features = ["alloc"] +[dependencies.sel4-microkit-smoltcp-device-adapter] +path = "../../../../../sel4-microkit/smoltcp-device-adapter" + [dependencies.sel4-shared-ring-buffer-block-io-types] path = "../../../../../sel4-shared-ring-buffer/block-io/types" diff --git a/crates/examples/microkit/http-server/pds/server/src/main.rs b/crates/examples/microkit/http-server/pds/server/src/main.rs index a5fa7e96f..78c2aec0d 100644 --- a/crates/examples/microkit/http-server/pds/server/src/main.rs +++ b/crates/examples/microkit/http-server/pds/server/src/main.rs @@ -25,6 +25,7 @@ use sel4_bounce_buffer_allocator::{Basic, BounceBufferAllocator}; use sel4_externally_shared::{ExternallySharedRef, ExternallySharedRefExt}; use sel4_logging::{LevelFilter, Logger, LoggerBuilder}; use sel4_microkit::{memory_region_symbol, protection_domain, Handler}; +use sel4_microkit_smoltcp_device_adapter::client::Client as NetClient; use sel4_newlib as _; use sel4_shared_ring_buffer::RingBuffers; use sel4_shared_ring_buffer_block_io::SharedRingBufferBlockIO; @@ -35,14 +36,12 @@ use microkit_http_server_example_server_core::run_server; mod block_client; mod config; mod handler; -mod net_client; mod rtc_client; mod timer_client; use block_client::BlockClient; use config::channels; use handler::HandlerImpl; -use net_client::NetClient; use rtc_client::RtcClient; use timer_client::TimerClient; diff --git a/crates/examples/microkit/http-server/pds/server/src/net_client.rs b/crates/examples/microkit/http-server/pds/server/src/net_client.rs deleted file mode 100644 index 34dd3b950..000000000 --- a/crates/examples/microkit/http-server/pds/server/src/net_client.rs +++ /dev/null @@ -1,30 +0,0 @@ -// -// Copyright 2023, Colias Group, LLC -// -// SPDX-License-Identifier: BSD-2-Clause -// - -use sel4_microkit::MessageInfo; -use sel4_microkit_message::MessageInfoExt as _; - -use microkit_http_server_example_virtio_net_driver_interface_types::*; - -pub struct NetClient { - channel: sel4_microkit::Channel, -} - -impl NetClient { - pub fn new(channel: sel4_microkit::Channel) -> Self { - Self { channel } - } - - pub fn get_mac_address(&self) -> MacAddress { - let req = Request::GetMacAddress; - let resp: GetMacAddressResponse = self - .channel - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard() - .unwrap(); - resp.mac_address - } -} diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix index 8105c43b0..530b8b91b 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.nix @@ -15,13 +15,14 @@ mk { inherit (localCrates) sel4-microkit-message + sel4-microkit-smoltcp-device-adapter sel4 sel4-logging sel4-immediate-sync-once-cell sel4-shared-ring-buffer sel4-bounce-buffer-allocator sel4-virtio-hal-impl - microkit-http-server-example-virtio-net-driver-interface-types + sel4-virtio-net-driver-for-smoltcp ; sel4-externally-shared = localCrates.sel4-externally-shared // { features = [ "unstable" ]; }; diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml index 998703ded..04efe5187 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml @@ -18,7 +18,6 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" -microkit-http-server-example-virtio-net-driver-interface-types = { path = "interface-types" } sel4 = { path = "../../../../../sel4" } sel4-bounce-buffer-allocator = { path = "../../../../../sel4-bounce-buffer-allocator" } sel4-externally-shared = { path = "../../../../../sel4-externally-shared", features = ["unstable"] } @@ -29,3 +28,9 @@ sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } sel4-virtio-hal-impl = { path = "../../../../../drivers/sel4-virtio-hal-impl" } virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } + +[dependencies.sel4-microkit-smoltcp-device-adapter] +path = "../../../../../sel4-microkit/smoltcp-device-adapter" + +[dependencies.sel4-virtio-net-driver-for-smoltcp] +path = "../../../../../drivers/sel4-virtio-net-driver-for-smoltcp" diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.nix b/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.nix deleted file mode 100644 index f1eccf4fb..000000000 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/Cargo.nix +++ /dev/null @@ -1,14 +0,0 @@ -# -# Copyright 2023, Colias Group, LLC -# -# SPDX-License-Identifier: BSD-2-Clause -# - -{ mk, localCrates, serdeWith }: - -mk { - package.name = "microkit-http-server-example-virtio-net-driver-interface-types"; - dependencies = { - serde = serdeWith []; - }; -} diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/src/lib.rs b/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/src/lib.rs deleted file mode 100644 index b98ccd9e2..000000000 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/interface-types/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -// -// Copyright 2023, Colias Group, LLC -// -// SPDX-License-Identifier: BSD-2-Clause -// - -#![no_std] - -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub struct MacAddress(pub [u8; 6]); - -#[derive(Debug, Serialize, Deserialize)] -pub enum Request { - GetMacAddress, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct GetMacAddressResponse { - pub mac_address: MacAddress, -} diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs b/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs index afcfdc71f..3893d5192 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/src/main.rs @@ -18,14 +18,11 @@ use virtio_drivers::{ }; use sel4_externally_shared::{ExternallySharedRef, ExternallySharedRefExt}; -use sel4_microkit::{ - memory_region_symbol, protection_domain, var, Channel, Handler, Infallible, MessageInfo, -}; -use sel4_microkit_message::MessageInfoExt as _; +use sel4_microkit::{memory_region_symbol, protection_domain, var}; +use sel4_microkit_smoltcp_device_adapter::driver::Driver; use sel4_shared_ring_buffer::{roles::Use, RingBuffers}; - -use microkit_http_server_example_virtio_net_driver_interface_types::*; use sel4_virtio_hal_impl::HalImpl; +use sel4_virtio_net_driver_for_smoltcp::DeviceWrapper; mod config; @@ -37,7 +34,7 @@ const NET_BUFFER_LEN: usize = 2048; #[protection_domain( heap_size = 512 * 1024, )] -fn init() -> HandlerImpl { +fn init() -> Driver> { HalImpl::init( config::VIRTIO_NET_DRIVER_DMA_SIZE, *var!(virtio_net_driver_dma_vaddr: usize = 0), @@ -80,112 +77,12 @@ fn init() -> HandlerImpl { dev.ack_interrupt(); channels::DEVICE.irq_ack().unwrap(); - HandlerImpl { - dev, + Driver::new( + DeviceWrapper::new(dev), client_region, rx_ring_buffers, tx_ring_buffers, - } -} - -struct HandlerImpl { - dev: VirtIONet, - client_region: ExternallySharedRef<'static, [u8]>, - rx_ring_buffers: RingBuffers<'static, Use, fn()>, - tx_ring_buffers: RingBuffers<'static, Use, fn()>, -} - -impl Handler for HandlerImpl { - type Error = Infallible; - - fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { - match channel { - channels::DEVICE | channels::CLIENT => { - let mut notify_rx = false; - - while self.dev.can_recv() && !self.rx_ring_buffers.free_mut().is_empty().unwrap() { - let rx_buf = self.dev.receive().unwrap(); - let desc = self.rx_ring_buffers.free_mut().dequeue().unwrap().unwrap(); - let desc_len = usize::try_from(desc.len()).unwrap(); - assert!(desc_len >= rx_buf.packet_len()); - let buf_range = { - let start = desc.encoded_addr(); - start..start + rx_buf.packet_len() - }; - self.client_region - .as_mut_ptr() - .index(buf_range) - .copy_from_slice(rx_buf.packet()); - self.dev.recycle_rx_buffer(rx_buf).unwrap(); - self.rx_ring_buffers - .used_mut() - .enqueue_and_commit(desc) - .unwrap() - .unwrap(); - notify_rx = true; - } - - if notify_rx { - self.rx_ring_buffers.notify(); - } - - let mut notify_tx = false; - - while !self.tx_ring_buffers.free_mut().is_empty().unwrap() && self.dev.can_send() { - let desc = self.tx_ring_buffers.free_mut().dequeue().unwrap().unwrap(); - let buf_range = { - let start = desc.encoded_addr(); - start..start + usize::try_from(desc.len()).unwrap() - }; - let mut tx_buf = self.dev.new_tx_buffer(buf_range.len()); - self.client_region - .as_ptr() - .index(buf_range) - .copy_into_slice(tx_buf.packet_mut()); - self.dev.send(tx_buf).unwrap(); - self.tx_ring_buffers - .used_mut() - .enqueue_and_commit(desc) - .unwrap() - .unwrap(); - notify_tx = true; - } - - if notify_tx { - self.tx_ring_buffers.notify(); - } - - self.dev.ack_interrupt(); - channels::DEVICE.irq_ack().unwrap(); - } - _ => { - unreachable!() - } - } - Ok(()) - } - - fn protected( - &mut self, - channel: Channel, - msg_info: MessageInfo, - ) -> Result { - Ok(match channel { - channels::CLIENT => match msg_info.recv_using_postcard::() { - Ok(req) => match req { - Request::GetMacAddress => { - let mac_address = self.dev.mac_address(); - MessageInfo::send_using_postcard(GetMacAddressResponse { - mac_address: MacAddress(mac_address), - }) - .unwrap() - } - }, - Err(_) => MessageInfo::send_unspecified_error(), - }, - _ => { - unreachable!() - } - }) - } + channels::DEVICE, + channels::CLIENT, + ) } diff --git a/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix b/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix index f292e5f68..67723a05f 100644 --- a/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix +++ b/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix @@ -17,6 +17,7 @@ mk { inherit (versions) log embedded-hal-nb heapless; serde = serdeWith []; } // (with localCrates; { + inherit sel4-driver-traits; inherit sel4-microkit-message; sel4-microkit = sel4-microkit // { default-features = false; }; }); diff --git a/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml b/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml index 0c3bf58f6..2ee1df94e 100644 --- a/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml +++ b/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml @@ -20,6 +20,7 @@ license = "BSD-2-Clause" embedded-hal-nb = "1.0" heapless = "0.7.16" log = "0.4.17" +sel4-driver-traits = { path = "../../drivers/sel4-driver-traits" } sel4-microkit = { path = "..", default-features = false } sel4-microkit-message = { path = "../message" } serde = { version = "1.0.147", default-features = false } diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs b/crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs index 2c1f6e869..23c9996e4 100644 --- a/crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs +++ b/crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs @@ -18,7 +18,7 @@ use super::common::*; /// and [fmt::Write]. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Client { - pub channel: Channel, + channel: Channel, } impl Client { diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs b/crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs index 649c5b6c3..67d33cdb0 100644 --- a/crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs @@ -11,14 +11,14 @@ use embedded_hal_nb::nb; use embedded_hal_nb::serial; use heapless::Deque; +use sel4_driver_traits::HandleInterrupt; use sel4_microkit::{Channel, Handler, MessageInfo}; use sel4_microkit_message::MessageInfoExt; use super::common::*; -pub trait IrqDevice { - fn handle_irq(&self); -} +// TODO +// Factor buffering out into wrapper for serial::{Read,Write} /// Handle messages using an implementor of [serial::Read] and [serial::Write]. #[derive(Clone, Debug)] @@ -37,7 +37,7 @@ pub struct Driver { impl Driver where - Device: serial::Read + serial::Write + IrqDevice, + Device: serial::Read + serial::Write + HandleInterrupt, { pub fn new(device: Device, serial: Channel, client: Channel) -> Self { Self { @@ -52,7 +52,7 @@ where impl Handler for Driver where - Device: serial::Read + serial::Write + IrqDevice, + Device: serial::Read + serial::Write + HandleInterrupt, ::Error: fmt::Display, { type Error = Error; @@ -65,7 +65,7 @@ where return Err(Error::BufferFull); } } - self.device.handle_irq(); + self.device.handle_interrupt(); self.serial.irq_ack().unwrap(); if self.notify { self.client.notify(); diff --git a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix b/crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix index 026134b8b..00866fab5 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix +++ b/crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix @@ -19,6 +19,7 @@ mk { serde = serdeWith []; } // (with localCrates; { inherit sel4-microkit-message; + inherit sel4-driver-traits; sel4-microkit = sel4-microkit // { default-features = false; }; sel4-bounce-buffer-allocator = sel4-bounce-buffer-allocator; sel4-externally-shared = sel4-externally-shared // { features = [ "unstable" ]; }; diff --git a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml b/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml index 44abd6c54..a7b69d159 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml +++ b/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml @@ -19,6 +19,7 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" } +sel4-driver-traits = { path = "../../drivers/sel4-driver-traits" } sel4-externally-shared = { path = "../../sel4-externally-shared", features = ["unstable"] } sel4-microkit = { path = "..", default-features = false } sel4-microkit-message = { path = "../message" } diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs index 053566d1f..e63a3b5da 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs @@ -4,6 +4,7 @@ // SPDX-License-Identifier: BSD-2-Clause // +use sel4_driver_traits::MacAddress; use sel4_microkit::{Channel, MessageInfo}; use sel4_microkit_message::MessageInfoExt as _; diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs index 2dbadc6e1..8d9e0530c 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs @@ -7,8 +7,7 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize)] -pub struct MacAddress(pub [u8; 6]); +use sel4_driver_traits::MacAddress; #[derive(Debug, Serialize, Deserialize)] pub(crate) enum Request { diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs index 76c5df826..dc42e7b1c 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs @@ -12,6 +12,7 @@ use smoltcp::{ time::Instant, }; +use sel4_driver_traits::{GetMacAddress, HandleInterrupt}; use sel4_externally_shared::ExternallySharedRef; use sel4_microkit::{Channel, Handler, Infallible, MessageInfo}; use sel4_microkit_message::MessageInfoExt as _; @@ -19,20 +20,11 @@ use sel4_shared_ring_buffer::{roles::Use, RingBuffers}; use super::common::*; -pub trait IrqAck { - fn irq_ack(&mut self); -} - -pub trait HasMac { - fn mac_address(&self) -> [u8; 6]; -} - pub struct Driver { dev: Device, client_region: ExternallySharedRef<'static, [u8]>, - client_region_paddr: usize, - rx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, - tx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, + rx_ring_buffers: RingBuffers<'static, Use, fn()>, + tx_ring_buffers: RingBuffers<'static, Use, fn()>, device_channel: Channel, client_channel: Channel, } @@ -41,9 +33,8 @@ impl Driver { pub fn new( dev: Device, client_region: ExternallySharedRef<'static, [u8]>, - client_region_paddr: usize, - rx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, - tx_ring_buffers: RingBuffers<'static, Use, fn() -> Result<(), Infallible>>, + rx_ring_buffers: RingBuffers<'static, Use, fn()>, + tx_ring_buffers: RingBuffers<'static, Use, fn()>, device_channel: Channel, client_channel: Channel, ) -> Self { @@ -52,7 +43,6 @@ impl Driver { Self { dev, client_region, - client_region_paddr, rx_ring_buffers, tx_ring_buffers, device_channel, @@ -61,7 +51,7 @@ impl Driver { } } -impl Handler for Driver { +impl Handler for Driver { type Error = Infallible; fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> { @@ -80,7 +70,7 @@ impl Handler for Driver { rx_tok.consume(|rx_buf| { assert!(desc_len >= rx_buf.len()); let buf_range = { - let start = desc.encoded_addr() - self.client_region_paddr; + let start = desc.encoded_addr(); start..start + rx_buf.len() }; self.client_region @@ -98,7 +88,7 @@ impl Handler for Driver { } if notify_rx { - self.rx_ring_buffers.notify().unwrap(); + self.rx_ring_buffers.notify(); } let mut notify_tx = false; @@ -114,7 +104,7 @@ impl Handler for Driver { tx_tok.consume(tx_len, |tx_buf| { let buf_range = { - let start = desc.encoded_addr() - self.client_region_paddr; + let start = desc.encoded_addr(); start..start + tx_len }; self.client_region @@ -132,10 +122,10 @@ impl Handler for Driver { } if notify_tx { - self.tx_ring_buffers.notify().unwrap(); + self.tx_ring_buffers.notify(); } - self.dev.irq_ack(); + self.dev.handle_interrupt(); self.device_channel.irq_ack().unwrap(); } else { unreachable!() @@ -153,9 +143,8 @@ impl Handler for Driver { match msg_info.recv_using_postcard::() { Ok(req) => match req { Request::GetMacAddress => { - let mac_address = self.dev.mac_address(); MessageInfo::send_using_postcard(GetMacAddressResponse { - mac_address: MacAddress(mac_address), + mac_address: self.dev.get_mac_address(), }) .unwrap() } diff --git a/hacking/nix/scope/world/instances/microkit/banscii/default.nix b/hacking/nix/scope/world/instances/microkit/banscii/default.nix index 94e3fb567..469f0baee 100644 --- a/hacking/nix/scope/world/instances/microkit/banscii/default.nix +++ b/hacking/nix/scope/world/instances/microkit/banscii/default.nix @@ -23,8 +23,8 @@ let pds = { - pl011-driver = mkPD rec { - rootCrate = crates.banscii-pl011-driver; + serial-driver = mkPD rec { + rootCrate = crates.banscii-serial-driver; release = true; }; assistant = mkPD rec { @@ -50,7 +50,7 @@ lib.fix (self: mkMicrokitInstance { searchPath = symlinkJoin { name = "x"; paths = [ - "${pds.pl011-driver}/bin" + "${pds.serial-driver}/bin" "${pds.assistant}/bin" "${pds.artist}/bin" ]; From 2db6cb478675363d626016fc1f3fd36f5a07d0e4 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Wed, 12 Jun 2024 06:34:23 +0000 Subject: [PATCH 20/64] Fix attribution Signed-off-by: Nick Spinale --- .../sel4-virtio-net-driver-for-smoltcp/Cargo.nix | 14 +++++++++++--- .../sel4-virtio-net-driver-for-smoltcp/Cargo.toml | 6 +++--- .../sel4-virtio-net-driver-for-smoltcp/src/lib.rs | 3 ++- .../smoltcp-device-adapter/src/client.rs | 1 + 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix index 9792a9f72..ed9052eb5 100644 --- a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix +++ b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix @@ -1,13 +1,21 @@ # # Copyright 2024, Colias Group, LLC # -# SPDX-License-Identifier: BSD-2-Clause +# SPDX-License-Identifier: MIT # -{ mk, versions, localCrates, smoltcpWith, virtioDriversWith }: +{ mk, mkDefaultFrontmatterWithReuseArgs, defaultReuseFrontmatterArgs, versions, localCrates, smoltcpWith, virtioDriversWith, authors }: -mk { +mk rec { + nix.frontmatter = mkDefaultFrontmatterWithReuseArgs (defaultReuseFrontmatterArgs // { + licenseID = package.license; + }); package.name = "sel4-virtio-net-driver-for-smoltcp"; + package.authors = with authors; [ + nspin + "Runji Wang " + ]; + package.license = "MIT"; dependencies = { inherit (versions) log; smoltcp = smoltcpWith []; diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml index 76f741a9b..86ca935c5 100644 --- a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml +++ b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml @@ -1,7 +1,7 @@ # # Copyright 2023, Colias Group, LLC # -# SPDX-License-Identifier: BSD-2-Clause +# SPDX-License-Identifier: MIT # # # This file is generated from './Cargo.nix'. You can edit this file directly @@ -12,9 +12,9 @@ [package] name = "sel4-virtio-net-driver-for-smoltcp" version = "0.1.0" -authors = ["Nick Spinale "] +authors = ["Nick Spinale ", "Runji Wang "] edition = "2021" -license = "BSD-2-Clause" +license = "MIT" [dependencies] log = "0.4.17" diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs index e9d76bde7..4f0784e72 100644 --- a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs +++ b/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs @@ -1,7 +1,8 @@ // // Copyright 2023, Colias Group, LLC +// Copyright (c) 2019-2020 rCore Developers // -// SPDX-License-Identifier: BSD-2-Clause +// SPDX-License-Identifier: MIT // #![no_std] diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs b/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs index e63a3b5da..0784fca9c 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs +++ b/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs @@ -1,5 +1,6 @@ // // Copyright 2024, Colias Group, LLC +// Copyright 2023, Galois, Inc. // // SPDX-License-Identifier: BSD-2-Clause // From e430ff52cddf3259aa5593c026e8fc1bb6ede37e Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Wed, 12 Jun 2024 06:46:02 +0000 Subject: [PATCH 21/64] crates/drivers: Initialize serial device in new() Signed-off-by: Nick Spinale --- crates/drivers/sel4-pl011-driver/src/lib.rs | 2 +- crates/examples/microkit/banscii/pds/serial-driver/src/main.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/drivers/sel4-pl011-driver/src/lib.rs b/crates/drivers/sel4-pl011-driver/src/lib.rs index d70624cd2..b75e1c3aa 100644 --- a/crates/drivers/sel4-pl011-driver/src/lib.rs +++ b/crates/drivers/sel4-pl011-driver/src/lib.rs @@ -31,7 +31,7 @@ impl Driver { this } - pub fn init(&mut self) { + fn init(&mut self) { self.device.init(); } } diff --git a/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs b/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs index e99aa9af7..2ad8acaf0 100644 --- a/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs +++ b/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs @@ -16,8 +16,7 @@ const ASSISTANT: Channel = Channel::new(1); #[protection_domain] fn init() -> Driver { - let mut driver_impl = + let driver_impl = unsafe { DriverImpl::new(memory_region_symbol!(pl011_register_block: *mut ()).as_ptr()) }; - driver_impl.init(); Driver::new(driver_impl, DEVICE, ASSISTANT) } From 316230bdb36c41df63248da8456c202d208a0b09 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Wed, 12 Jun 2024 06:36:56 +0000 Subject: [PATCH 22/64] Set up Banscii for portability Signed-off-by: Nick Spinale --- ...banscii.system => banscii.system.template} | 10 ++--- .../banscii/generate_system_description.py | 38 +++++++++++++++++++ .../banscii/pds/serial-driver/Cargo.nix | 6 ++- .../banscii/pds/serial-driver/Cargo.toml | 5 ++- .../banscii/pds/serial-driver/src/main.rs | 4 +- hacking/nix/scope/microkit/default.nix | 1 + .../instances/microkit/banscii/default.nix | 20 ++++++++-- 7 files changed, 73 insertions(+), 11 deletions(-) rename crates/examples/microkit/banscii/{banscii.system => banscii.system.template} (78%) create mode 100644 crates/examples/microkit/banscii/generate_system_description.py diff --git a/crates/examples/microkit/banscii/banscii.system b/crates/examples/microkit/banscii/banscii.system.template similarity index 78% rename from crates/examples/microkit/banscii/banscii.system rename to crates/examples/microkit/banscii/banscii.system.template index c946ea1d6..5790d7ab0 100644 --- a/crates/examples/microkit/banscii/banscii.system +++ b/crates/examples/microkit/banscii/banscii.system.template @@ -6,15 +6,15 @@ --> - + - + - - + + @@ -30,7 +30,7 @@ - + diff --git a/crates/examples/microkit/banscii/generate_system_description.py b/crates/examples/microkit/banscii/generate_system_description.py new file mode 100644 index 000000000..8244b917e --- /dev/null +++ b/crates/examples/microkit/banscii/generate_system_description.py @@ -0,0 +1,38 @@ +# +# Copyright 2024, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# + +import argparse +import json +import sys +from jinja2 import Template + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--template', type=argparse.FileType('r'), required=True) + parser.add_argument('--board', required=True) + parser.add_argument('-o', type=argparse.FileType('w'), required=True) + args = parser.parse_args() + run(args) + +def run(args): + template = Template(args.template.read()) + context = mk_context(args.board) + rendered = template.render(context) + args.o.write(rendered) + +def mk_context(board): + context = {} + + if board == 'qemu_virt_aarch64': + context['serial_mmio_phys_addr'] = 0x9000000 + context['serial_irq'] = 33 + else: + raise Exception('unsupported configuration') + + return context + +if __name__ == '__main__': + main() diff --git a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix index 18b03492d..34fbe8f42 100644 --- a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix +++ b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.nix @@ -12,8 +12,12 @@ mk { inherit (localCrates) sel4-microkit-message sel4-microkit-embedded-hal-adapters - sel4-pl011-driver + ; sel4-microkit = localCrates.sel4-microkit // { default-features = false; }; + sel4-pl011-driver = localCrates.sel4-pl011-driver // { optional = true; }; + }; + features = { + board-qemu_virt_aarch64 = [ "sel4-pl011-driver" ]; }; } diff --git a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml index 9c298f4df..44517db38 100644 --- a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml +++ b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml @@ -16,10 +16,13 @@ authors = ["Nick Spinale "] edition = "2021" license = "BSD-2-Clause" +[features] +board-qemu_virt_aarch64 = ["sel4-pl011-driver"] + [dependencies] sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } -sel4-pl011-driver = { path = "../../../../../drivers/sel4-pl011-driver" } +sel4-pl011-driver = { path = "../../../../../drivers/sel4-pl011-driver", optional = true } [dependencies.sel4-microkit-embedded-hal-adapters] path = "../../../../../sel4-microkit/embedded-hal-adapters" diff --git a/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs b/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs index 2ad8acaf0..c3a0be9f0 100644 --- a/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs +++ b/crates/examples/microkit/banscii/pds/serial-driver/src/main.rs @@ -9,6 +9,8 @@ use sel4_microkit::{memory_region_symbol, protection_domain, Channel}; use sel4_microkit_embedded_hal_adapters::serial::driver::Driver; + +#[cfg(feature = "board-qemu_virt_aarch64")] use sel4_pl011_driver::Driver as DriverImpl; const DEVICE: Channel = Channel::new(0); @@ -17,6 +19,6 @@ const ASSISTANT: Channel = Channel::new(1); #[protection_domain] fn init() -> Driver { let driver_impl = - unsafe { DriverImpl::new(memory_region_symbol!(pl011_register_block: *mut ()).as_ptr()) }; + unsafe { DriverImpl::new(memory_region_symbol!(serial_register_block: *mut ()).as_ptr()) }; Driver::new(driver_impl, DEVICE, ASSISTANT) } diff --git a/hacking/nix/scope/microkit/default.nix b/hacking/nix/scope/microkit/default.nix index 4a32fe5de..07bba147e 100644 --- a/hacking/nix/scope/microkit/default.nix +++ b/hacking/nix/scope/microkit/default.nix @@ -128,6 +128,7 @@ let ]; passthru = rec { + inherit systemXML; loader = "${self}/loader.img"; links = [ { name = "pds"; path = searchPath; } diff --git a/hacking/nix/scope/world/instances/microkit/banscii/default.nix b/hacking/nix/scope/world/instances/microkit/banscii/default.nix index 469f0baee..bbdcf71d5 100644 --- a/hacking/nix/scope/world/instances/microkit/banscii/default.nix +++ b/hacking/nix/scope/world/instances/microkit/banscii/default.nix @@ -7,7 +7,7 @@ { lib, stdenv , buildPackages, pkgsBuildBuild , linkFarm, symlinkJoin, writeText, writeScript, runCommand -, callPackage +, python3Packages , microkit , mkTask , sources @@ -16,16 +16,19 @@ , mkSeL4RustTargetTriple , mkMicrokitInstance , worldConfig +, seL4ConfigJSON , canSimulate , mkPD }: let + inherit (worldConfig.microkitConfig) board; + pds = { serial-driver = mkPD rec { rootCrate = crates.banscii-serial-driver; - release = true; + features = [ "board-${board}" ]; }; assistant = mkPD rec { rootCrate = crates.banscii-assistant; @@ -44,6 +47,8 @@ let }; }; + srcPath = relativePath: sources.srcRoot + "/crates/examples/microkit/banscii/${relativePath}"; + in lib.fix (self: mkMicrokitInstance { system = microkit.mkSystem { @@ -55,7 +60,16 @@ lib.fix (self: mkMicrokitInstance { "${pds.artist}/bin" ]; }; - systemXML = sources.srcRoot + "/crates/examples/microkit/banscii/banscii.system"; + systemXML = runCommand "banscii.system" { + nativeBuildInputs = [ + python3Packages.jinja2 + ]; + } '' + python3 ${srcPath "generate_system_description.py"} \ + --template ${srcPath "banscii.system.template"} \ + --board ${board} \ + -o $out + ''; }; } // { inherit pds; From 13c4357726b88ab587d07f8fa820e18efeaf916b Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Fri, 14 Jun 2024 14:04:25 -0400 Subject: [PATCH 23/64] nix: Work around issue with Z3 upstream aarch64 binary release https://github.com/Z3Prover/z3/issues/7201 Signed-off-by: Nick Spinale --- hacking/nix/scope/shell-for-hacking.nix | 5 +++-- hacking/nix/scope/shell-for-makefile.nix | 4 +++- hacking/nix/scope/verus/z3.nix | 18 +++++++++++++----- hacking/nix/scope/world/instances/default.nix | 4 ++-- hacking/nix/top-level/default.nix | 6 ++++-- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/hacking/nix/scope/shell-for-hacking.nix b/hacking/nix/scope/shell-for-hacking.nix index 063f75af4..5965dd223 100644 --- a/hacking/nix/scope/shell-for-hacking.nix +++ b/hacking/nix/scope/shell-for-hacking.nix @@ -41,11 +41,12 @@ mkShell (shellForMakefile.apply { perl cmake rustPlatform.bindgenHook - kani - verus strace cntr cachix + ] ++ lib.optionals hostPlatform.isx86_64 [ + kani + verus ]; buildInputs = [ diff --git a/hacking/nix/scope/shell-for-makefile.nix b/hacking/nix/scope/shell-for-makefile.nix index 2ba156d5b..96996ff2c 100644 --- a/hacking/nix/scope/shell-for-makefile.nix +++ b/hacking/nix/scope/shell-for-makefile.nix @@ -4,7 +4,8 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ mkShell +{ lib, hostPlatform +, mkShell , python3 , reuse , cargo-audit @@ -22,6 +23,7 @@ let python3 reuse cargo-audit + ] ++ lib.optionals hostPlatform.isx86_64 [ kani ]; }; diff --git a/hacking/nix/scope/verus/z3.nix b/hacking/nix/scope/verus/z3.nix index ee36ba914..3a4bb21f5 100644 --- a/hacking/nix/scope/verus/z3.nix +++ b/hacking/nix/scope/verus/z3.nix @@ -13,10 +13,18 @@ let version = "4.12.5"; - arch = { - "x86_64" = "x64"; - "aarch64" = "arm64"; - }.${hostPlatform.parsed.cpu.name}; + byArch = { + "x86_64" = { + arch = "x64"; + sha256 = "sha256-8DZXTV4gKckgT/81A8/mjd9B+m/euzm+7ZnhvzVbf+4="; + }; + "aarch64" = { + arch = "arm64"; + sha256 = "sha256-FeX6pi5lvRitHAUbGqbxdqqyjeEU0yRsaQsj8vyj12k="; + }; + }; + + inherit (byArch.${hostPlatform.parsed.cpu.name}) arch sha256; filename = "z3-${version}-${arch}-glibc-2.35"; @@ -27,7 +35,7 @@ stdenv.mkDerivation { src = fetchurl { url = "https://github.com/Z3Prover/z3/releases/download/z3-${version}/${filename}.zip"; - sha256 = "sha256-8DZXTV4gKckgT/81A8/mjd9B+m/euzm+7ZnhvzVbf+4="; + inherit sha256; }; nativeBuildInputs = [ diff --git a/hacking/nix/scope/world/instances/default.nix b/hacking/nix/scope/world/instances/default.nix index fe6bf3d86..2535d2875 100644 --- a/hacking/nix/scope/world/instances/default.nix +++ b/hacking/nix/scope/world/instances/default.nix @@ -4,7 +4,7 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ lib, stdenv, hostPlatform, buildPackages +{ lib, stdenv, hostPlatform, buildPlatform, buildPackages , callPackage , runCommand, linkFarm, writeText, writeScript @@ -208,7 +208,7 @@ in rec { inherit canSimulate; }); - verus = maybe (haveFullRuntime && hostPlatform.is64bit) (callPackage ./verus.nix { + verus = maybe (haveFullRuntime && hostPlatform.is64bit && buildPlatform.isx86_64) (callPackage ./verus.nix { inherit canSimulate; }); diff --git a/hacking/nix/top-level/default.nix b/hacking/nix/top-level/default.nix index 389722a0f..02ebf1277 100644 --- a/hacking/nix/top-level/default.nix +++ b/hacking/nix/top-level/default.nix @@ -62,8 +62,10 @@ in { sel4testInstancesList - pkgs.build.this.kani - pkgs.build.this.verus + (lib.optionals pkgs.build.hostPlatform.isx86_64 [ + pkgs.build.this.kani + pkgs.build.this.verus + ]) pkgs.host.aarch32.none.this.worlds.default.seL4 pkgs.host.ia32.none.this.worlds.default.seL4 From 9b1861252225234cde1062ce9c45f58cc58d7811 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 15 Jun 2024 00:34:26 -0400 Subject: [PATCH 24/64] crates/sel4-microkit/drivers: Organize crates Signed-off-by: Nick Spinale --- Cargo.toml | 4 ++-- .../microkit/banscii/pds/assistant/Cargo.toml | 2 +- .../microkit/banscii/pds/serial-driver/Cargo.toml | 2 +- .../microkit/http-server/pds/server/Cargo.toml | 2 +- .../http-server/pds/virtio-net-driver/Cargo.toml | 2 +- .../{ => drivers}/embedded-hal-adapters/Cargo.nix | 0 .../{ => drivers}/embedded-hal-adapters/Cargo.toml | 6 +++--- .../{ => drivers}/embedded-hal-adapters/src/lib.rs | 0 .../embedded-hal-adapters/src/serial/client.rs | 0 .../embedded-hal-adapters/src/serial/common.rs | 0 .../embedded-hal-adapters/src/serial/driver.rs | 0 .../embedded-hal-adapters/src/serial/mod.rs | 0 .../{ => drivers}/smoltcp-device-adapter/Cargo.nix | 0 .../{ => drivers}/smoltcp-device-adapter/Cargo.toml | 12 ++++++------ .../smoltcp-device-adapter/src/client.rs | 0 .../smoltcp-device-adapter/src/common.rs | 0 .../smoltcp-device-adapter/src/driver.rs | 0 .../{ => drivers}/smoltcp-device-adapter/src/lib.rs | 0 18 files changed, 15 insertions(+), 15 deletions(-) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/Cargo.nix (100%) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/Cargo.toml (78%) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/src/lib.rs (100%) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/src/serial/client.rs (100%) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/src/serial/common.rs (100%) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/src/serial/driver.rs (100%) rename crates/sel4-microkit/{ => drivers}/embedded-hal-adapters/src/serial/mod.rs (100%) rename crates/sel4-microkit/{ => drivers}/smoltcp-device-adapter/Cargo.nix (100%) rename crates/sel4-microkit/{ => drivers}/smoltcp-device-adapter/Cargo.toml (64%) rename crates/sel4-microkit/{ => drivers}/smoltcp-device-adapter/src/client.rs (100%) rename crates/sel4-microkit/{ => drivers}/smoltcp-device-adapter/src/common.rs (100%) rename crates/sel4-microkit/{ => drivers}/smoltcp-device-adapter/src/driver.rs (100%) rename crates/sel4-microkit/{ => drivers}/smoltcp-device-adapter/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index d6267d9e0..f5247aa4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,11 +111,11 @@ members = [ "crates/sel4-logging", "crates/sel4-microkit", "crates/sel4-microkit/base", - "crates/sel4-microkit/embedded-hal-adapters", + "crates/sel4-microkit/drivers/embedded-hal-adapters", + "crates/sel4-microkit/drivers/smoltcp-device-adapter", "crates/sel4-microkit/macros", "crates/sel4-microkit/message", "crates/sel4-microkit/message/types", - "crates/sel4-microkit/smoltcp-device-adapter", "crates/sel4-mod-in-out-dir", "crates/sel4-newlib", "crates/sel4-one-ref-cell", diff --git a/crates/examples/microkit/banscii/pds/assistant/Cargo.toml b/crates/examples/microkit/banscii/pds/assistant/Cargo.toml index 5bb70c532..ac994a068 100644 --- a/crates/examples/microkit/banscii/pds/assistant/Cargo.toml +++ b/crates/examples/microkit/banscii/pds/assistant/Cargo.toml @@ -30,4 +30,4 @@ default-features = false features = ["alloc"] [dependencies.sel4-microkit-embedded-hal-adapters] -path = "../../../../../sel4-microkit/embedded-hal-adapters" +path = "../../../../../sel4-microkit/drivers/embedded-hal-adapters" diff --git a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml index 44517db38..3909ceb79 100644 --- a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml +++ b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml @@ -25,4 +25,4 @@ sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-pl011-driver = { path = "../../../../../drivers/sel4-pl011-driver", optional = true } [dependencies.sel4-microkit-embedded-hal-adapters] -path = "../../../../../sel4-microkit/embedded-hal-adapters" +path = "../../../../../sel4-microkit/drivers/embedded-hal-adapters" diff --git a/crates/examples/microkit/http-server/pds/server/Cargo.toml b/crates/examples/microkit/http-server/pds/server/Cargo.toml index b322e0cb6..84fd73116 100644 --- a/crates/examples/microkit/http-server/pds/server/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/server/Cargo.toml @@ -54,7 +54,7 @@ default-features = false features = ["alloc"] [dependencies.sel4-microkit-smoltcp-device-adapter] -path = "../../../../../sel4-microkit/smoltcp-device-adapter" +path = "../../../../../sel4-microkit/drivers/smoltcp-device-adapter" [dependencies.sel4-shared-ring-buffer-block-io-types] path = "../../../../../sel4-shared-ring-buffer/block-io/types" diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml index 04efe5187..2672ea8c0 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml @@ -30,7 +30,7 @@ sel4-virtio-hal-impl = { path = "../../../../../drivers/sel4-virtio-hal-impl" } virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } [dependencies.sel4-microkit-smoltcp-device-adapter] -path = "../../../../../sel4-microkit/smoltcp-device-adapter" +path = "../../../../../sel4-microkit/drivers/smoltcp-device-adapter" [dependencies.sel4-virtio-net-driver-for-smoltcp] path = "../../../../../drivers/sel4-virtio-net-driver-for-smoltcp" diff --git a/crates/sel4-microkit/embedded-hal-adapters/Cargo.nix b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.nix similarity index 100% rename from crates/sel4-microkit/embedded-hal-adapters/Cargo.nix rename to crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.nix diff --git a/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml similarity index 78% rename from crates/sel4-microkit/embedded-hal-adapters/Cargo.toml rename to crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml index 2ee1df94e..fd020c32c 100644 --- a/crates/sel4-microkit/embedded-hal-adapters/Cargo.toml +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml @@ -20,7 +20,7 @@ license = "BSD-2-Clause" embedded-hal-nb = "1.0" heapless = "0.7.16" log = "0.4.17" -sel4-driver-traits = { path = "../../drivers/sel4-driver-traits" } -sel4-microkit = { path = "..", default-features = false } -sel4-microkit-message = { path = "../message" } +sel4-driver-traits = { path = "../../../drivers/sel4-driver-traits" } +sel4-microkit = { path = "../..", default-features = false } +sel4-microkit-message = { path = "../../message" } serde = { version = "1.0.147", default-features = false } diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/lib.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/lib.rs similarity index 100% rename from crates/sel4-microkit/embedded-hal-adapters/src/lib.rs rename to crates/sel4-microkit/drivers/embedded-hal-adapters/src/lib.rs diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/client.rs similarity index 100% rename from crates/sel4-microkit/embedded-hal-adapters/src/serial/client.rs rename to crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/client.rs diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/serial/common.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/common.rs similarity index 100% rename from crates/sel4-microkit/embedded-hal-adapters/src/serial/common.rs rename to crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/common.rs diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs similarity index 100% rename from crates/sel4-microkit/embedded-hal-adapters/src/serial/driver.rs rename to crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs diff --git a/crates/sel4-microkit/embedded-hal-adapters/src/serial/mod.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/mod.rs similarity index 100% rename from crates/sel4-microkit/embedded-hal-adapters/src/serial/mod.rs rename to crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/mod.rs diff --git a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.nix similarity index 100% rename from crates/sel4-microkit/smoltcp-device-adapter/Cargo.nix rename to crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.nix diff --git a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml similarity index 64% rename from crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml rename to crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml index a7b69d159..489e46e2f 100644 --- a/crates/sel4-microkit/smoltcp-device-adapter/Cargo.toml +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml @@ -18,12 +18,12 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" -sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" } -sel4-driver-traits = { path = "../../drivers/sel4-driver-traits" } -sel4-externally-shared = { path = "../../sel4-externally-shared", features = ["unstable"] } -sel4-microkit = { path = "..", default-features = false } -sel4-microkit-message = { path = "../message" } -sel4-shared-ring-buffer = { path = "../../sel4-shared-ring-buffer" } +sel4-bounce-buffer-allocator = { path = "../../../sel4-bounce-buffer-allocator" } +sel4-driver-traits = { path = "../../../drivers/sel4-driver-traits" } +sel4-externally-shared = { path = "../../../sel4-externally-shared", features = ["unstable"] } +sel4-microkit = { path = "../..", default-features = false } +sel4-microkit-message = { path = "../../message" } +sel4-shared-ring-buffer = { path = "../../../sel4-shared-ring-buffer" } serde = { version = "1.0.147", default-features = false } [dependencies.smoltcp] diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/client.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/client.rs similarity index 100% rename from crates/sel4-microkit/smoltcp-device-adapter/src/client.rs rename to crates/sel4-microkit/drivers/smoltcp-device-adapter/src/client.rs diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/common.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/common.rs similarity index 100% rename from crates/sel4-microkit/smoltcp-device-adapter/src/common.rs rename to crates/sel4-microkit/drivers/smoltcp-device-adapter/src/common.rs diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/driver.rs similarity index 100% rename from crates/sel4-microkit/smoltcp-device-adapter/src/driver.rs rename to crates/sel4-microkit/drivers/smoltcp-device-adapter/src/driver.rs diff --git a/crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/lib.rs similarity index 100% rename from crates/sel4-microkit/smoltcp-device-adapter/src/lib.rs rename to crates/sel4-microkit/drivers/smoltcp-device-adapter/src/lib.rs From 13313e6cbff2a34ca5b2e101d3f9dfc8b12f5408 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 15 Jun 2024 00:37:47 -0400 Subject: [PATCH 25/64] crates/drivers: Organize crates Signed-off-by: Nick Spinale --- Cargo.toml | 8 ++++---- .../{sel4-pl011-driver => devices/pl011}/Cargo.nix | 0 .../{sel4-pl011-driver => devices/pl011}/Cargo.toml | 2 +- .../{sel4-pl011-driver => devices/pl011}/src/device.rs | 0 .../{sel4-pl011-driver => devices/pl011}/src/lib.rs | 0 .../virtio/hal-impl}/Cargo.nix | 0 .../virtio/hal-impl}/Cargo.toml | 8 ++++---- .../virtio/hal-impl}/src/lib.rs | 0 .../virtio/net-driver-for-smoltcp}/Cargo.nix | 0 .../virtio/net-driver-for-smoltcp}/Cargo.toml | 2 +- .../virtio/net-driver-for-smoltcp}/src/lib.rs | 0 .../drivers/{sel4-driver-traits => interfaces}/Cargo.nix | 0 .../drivers/{sel4-driver-traits => interfaces}/Cargo.toml | 0 .../drivers/{sel4-driver-traits => interfaces}/src/lib.rs | 0 .../microkit/banscii/pds/serial-driver/Cargo.toml | 2 +- .../microkit/http-server/pds/virtio-blk-driver/Cargo.toml | 2 +- .../microkit/http-server/pds/virtio-net-driver/Cargo.toml | 4 ++-- .../drivers/embedded-hal-adapters/Cargo.toml | 2 +- .../drivers/smoltcp-device-adapter/Cargo.toml | 2 +- 19 files changed, 16 insertions(+), 16 deletions(-) rename crates/drivers/{sel4-pl011-driver => devices/pl011}/Cargo.nix (100%) rename crates/drivers/{sel4-pl011-driver => devices/pl011}/Cargo.toml (90%) rename crates/drivers/{sel4-pl011-driver => devices/pl011}/src/device.rs (100%) rename crates/drivers/{sel4-pl011-driver => devices/pl011}/src/lib.rs (100%) rename crates/drivers/{sel4-virtio-hal-impl => devices/virtio/hal-impl}/Cargo.nix (100%) rename crates/drivers/{sel4-virtio-hal-impl => devices/virtio/hal-impl}/Cargo.toml (65%) rename crates/drivers/{sel4-virtio-hal-impl => devices/virtio/hal-impl}/src/lib.rs (100%) rename crates/drivers/{sel4-virtio-net-driver-for-smoltcp => devices/virtio/net-driver-for-smoltcp}/Cargo.nix (100%) rename crates/drivers/{sel4-virtio-net-driver-for-smoltcp => devices/virtio/net-driver-for-smoltcp}/Cargo.toml (93%) rename crates/drivers/{sel4-virtio-net-driver-for-smoltcp => devices/virtio/net-driver-for-smoltcp}/src/lib.rs (100%) rename crates/drivers/{sel4-driver-traits => interfaces}/Cargo.nix (100%) rename crates/drivers/{sel4-driver-traits => interfaces}/Cargo.toml (100%) rename crates/drivers/{sel4-driver-traits => interfaces}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index f5247aa4e..b833080b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,10 @@ default-members = [] resolver = "2" members = [ - "crates/drivers/sel4-driver-traits", - "crates/drivers/sel4-pl011-driver", - "crates/drivers/sel4-virtio-hal-impl", - "crates/drivers/sel4-virtio-net-driver-for-smoltcp", + "crates/drivers/devices/pl011", + "crates/drivers/devices/virtio/hal-impl", + "crates/drivers/devices/virtio/net-driver-for-smoltcp", + "crates/drivers/interfaces", "crates/examples/microkit/banscii/pds/artist", "crates/examples/microkit/banscii/pds/artist/interface-types", "crates/examples/microkit/banscii/pds/assistant", diff --git a/crates/drivers/sel4-pl011-driver/Cargo.nix b/crates/drivers/devices/pl011/Cargo.nix similarity index 100% rename from crates/drivers/sel4-pl011-driver/Cargo.nix rename to crates/drivers/devices/pl011/Cargo.nix diff --git a/crates/drivers/sel4-pl011-driver/Cargo.toml b/crates/drivers/devices/pl011/Cargo.toml similarity index 90% rename from crates/drivers/sel4-pl011-driver/Cargo.toml rename to crates/drivers/devices/pl011/Cargo.toml index 81073bfc2..c71ec1b81 100644 --- a/crates/drivers/sel4-pl011-driver/Cargo.toml +++ b/crates/drivers/devices/pl011/Cargo.toml @@ -18,5 +18,5 @@ license = "BSD-2-Clause" [dependencies] embedded-hal-nb = "1.0" -sel4-driver-traits = { path = "../sel4-driver-traits" } +sel4-driver-traits = { path = "../../interfaces" } tock-registers = "0.8.1" diff --git a/crates/drivers/sel4-pl011-driver/src/device.rs b/crates/drivers/devices/pl011/src/device.rs similarity index 100% rename from crates/drivers/sel4-pl011-driver/src/device.rs rename to crates/drivers/devices/pl011/src/device.rs diff --git a/crates/drivers/sel4-pl011-driver/src/lib.rs b/crates/drivers/devices/pl011/src/lib.rs similarity index 100% rename from crates/drivers/sel4-pl011-driver/src/lib.rs rename to crates/drivers/devices/pl011/src/lib.rs diff --git a/crates/drivers/sel4-virtio-hal-impl/Cargo.nix b/crates/drivers/devices/virtio/hal-impl/Cargo.nix similarity index 100% rename from crates/drivers/sel4-virtio-hal-impl/Cargo.nix rename to crates/drivers/devices/virtio/hal-impl/Cargo.nix diff --git a/crates/drivers/sel4-virtio-hal-impl/Cargo.toml b/crates/drivers/devices/virtio/hal-impl/Cargo.toml similarity index 65% rename from crates/drivers/sel4-virtio-hal-impl/Cargo.toml rename to crates/drivers/devices/virtio/hal-impl/Cargo.toml index 68c4dfd38..476c3a253 100644 --- a/crates/drivers/sel4-virtio-hal-impl/Cargo.toml +++ b/crates/drivers/devices/virtio/hal-impl/Cargo.toml @@ -17,8 +17,8 @@ edition = "2021" license = "BSD-2-Clause" [dependencies] -sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" } -sel4-externally-shared = { path = "../../sel4-externally-shared" } -sel4-immediate-sync-once-cell = { path = "../../sel4-immediate-sync-once-cell" } -sel4-sync = { path = "../../sel4-sync" } +sel4-bounce-buffer-allocator = { path = "../../../../sel4-bounce-buffer-allocator" } +sel4-externally-shared = { path = "../../../../sel4-externally-shared" } +sel4-immediate-sync-once-cell = { path = "../../../../sel4-immediate-sync-once-cell" } +sel4-sync = { path = "../../../../sel4-sync" } virtio-drivers = { version = "0.7.2", default-features = false } diff --git a/crates/drivers/sel4-virtio-hal-impl/src/lib.rs b/crates/drivers/devices/virtio/hal-impl/src/lib.rs similarity index 100% rename from crates/drivers/sel4-virtio-hal-impl/src/lib.rs rename to crates/drivers/devices/virtio/hal-impl/src/lib.rs diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.nix similarity index 100% rename from crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.nix rename to crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.nix diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml similarity index 93% rename from crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml rename to crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml index 86ca935c5..4a53e0a80 100644 --- a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/Cargo.toml +++ b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml @@ -18,7 +18,7 @@ license = "MIT" [dependencies] log = "0.4.17" -sel4-driver-traits = { path = "../sel4-driver-traits" } +sel4-driver-traits = { path = "../../../interfaces" } virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } [dependencies.smoltcp] diff --git a/crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs b/crates/drivers/devices/virtio/net-driver-for-smoltcp/src/lib.rs similarity index 100% rename from crates/drivers/sel4-virtio-net-driver-for-smoltcp/src/lib.rs rename to crates/drivers/devices/virtio/net-driver-for-smoltcp/src/lib.rs diff --git a/crates/drivers/sel4-driver-traits/Cargo.nix b/crates/drivers/interfaces/Cargo.nix similarity index 100% rename from crates/drivers/sel4-driver-traits/Cargo.nix rename to crates/drivers/interfaces/Cargo.nix diff --git a/crates/drivers/sel4-driver-traits/Cargo.toml b/crates/drivers/interfaces/Cargo.toml similarity index 100% rename from crates/drivers/sel4-driver-traits/Cargo.toml rename to crates/drivers/interfaces/Cargo.toml diff --git a/crates/drivers/sel4-driver-traits/src/lib.rs b/crates/drivers/interfaces/src/lib.rs similarity index 100% rename from crates/drivers/sel4-driver-traits/src/lib.rs rename to crates/drivers/interfaces/src/lib.rs diff --git a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml index 3909ceb79..62d801010 100644 --- a/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml +++ b/crates/examples/microkit/banscii/pds/serial-driver/Cargo.toml @@ -22,7 +22,7 @@ board-qemu_virt_aarch64 = ["sel4-pl011-driver"] [dependencies] sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } -sel4-pl011-driver = { path = "../../../../../drivers/sel4-pl011-driver", optional = true } +sel4-pl011-driver = { path = "../../../../../drivers/devices/pl011", optional = true } [dependencies.sel4-microkit-embedded-hal-adapters] path = "../../../../../sel4-microkit/drivers/embedded-hal-adapters" diff --git a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml index 475c169c6..85d0ec920 100644 --- a/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-blk-driver/Cargo.toml @@ -27,7 +27,7 @@ sel4-logging = { path = "../../../../../sel4-logging" } sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } -sel4-virtio-hal-impl = { path = "../../../../../drivers/sel4-virtio-hal-impl" } +sel4-virtio-hal-impl = { path = "../../../../../drivers/devices/virtio/hal-impl" } virtio-drivers = { version = "0.7.2", default-features = false } [dependencies.sel4-shared-ring-buffer-block-io-types] diff --git a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml index 2672ea8c0..450754601 100644 --- a/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml +++ b/crates/examples/microkit/http-server/pds/virtio-net-driver/Cargo.toml @@ -26,11 +26,11 @@ sel4-logging = { path = "../../../../../sel4-logging" } sel4-microkit = { path = "../../../../../sel4-microkit", default-features = false } sel4-microkit-message = { path = "../../../../../sel4-microkit/message" } sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" } -sel4-virtio-hal-impl = { path = "../../../../../drivers/sel4-virtio-hal-impl" } +sel4-virtio-hal-impl = { path = "../../../../../drivers/devices/virtio/hal-impl" } virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } [dependencies.sel4-microkit-smoltcp-device-adapter] path = "../../../../../sel4-microkit/drivers/smoltcp-device-adapter" [dependencies.sel4-virtio-net-driver-for-smoltcp] -path = "../../../../../drivers/sel4-virtio-net-driver-for-smoltcp" +path = "../../../../../drivers/devices/virtio/net-driver-for-smoltcp" diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml index fd020c32c..b3b95e5f7 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml @@ -20,7 +20,7 @@ license = "BSD-2-Clause" embedded-hal-nb = "1.0" heapless = "0.7.16" log = "0.4.17" -sel4-driver-traits = { path = "../../../drivers/sel4-driver-traits" } +sel4-driver-traits = { path = "../../../drivers/interfaces" } sel4-microkit = { path = "../..", default-features = false } sel4-microkit-message = { path = "../../message" } serde = { version = "1.0.147", default-features = false } diff --git a/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml index 489e46e2f..40972b912 100644 --- a/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml @@ -19,7 +19,7 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" sel4-bounce-buffer-allocator = { path = "../../../sel4-bounce-buffer-allocator" } -sel4-driver-traits = { path = "../../../drivers/sel4-driver-traits" } +sel4-driver-traits = { path = "../../../drivers/interfaces" } sel4-externally-shared = { path = "../../../sel4-externally-shared", features = ["unstable"] } sel4-microkit = { path = "../..", default-features = false } sel4-microkit-message = { path = "../../message" } From 4831e5eb718ad421aa233fe0504c4de193d328ff Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 15 Jun 2024 00:40:08 -0400 Subject: [PATCH 26/64] Rename sel4-driver-traits -> sel4-driver-interfaces Signed-off-by: Nick Spinale --- Cargo.lock | 10 +++++----- crates/drivers/devices/pl011/Cargo.nix | 2 +- crates/drivers/devices/pl011/Cargo.toml | 2 +- crates/drivers/devices/pl011/src/lib.rs | 2 +- .../devices/virtio/net-driver-for-smoltcp/Cargo.nix | 2 +- .../devices/virtio/net-driver-for-smoltcp/Cargo.toml | 2 +- .../devices/virtio/net-driver-for-smoltcp/src/lib.rs | 2 +- crates/drivers/interfaces/Cargo.nix | 2 +- crates/drivers/interfaces/Cargo.toml | 2 +- .../drivers/embedded-hal-adapters/Cargo.nix | 2 +- .../drivers/embedded-hal-adapters/Cargo.toml | 2 +- .../drivers/embedded-hal-adapters/src/serial/driver.rs | 2 +- .../drivers/smoltcp-device-adapter/Cargo.nix | 2 +- .../drivers/smoltcp-device-adapter/Cargo.toml | 2 +- .../drivers/smoltcp-device-adapter/src/client.rs | 2 +- .../drivers/smoltcp-device-adapter/src/common.rs | 2 +- .../drivers/smoltcp-device-adapter/src/driver.rs | 2 +- 17 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7f56f75f..b3176e14c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2269,7 +2269,7 @@ dependencies = [ ] [[package]] -name = "sel4-driver-traits" +name = "sel4-driver-interfaces" version = "0.1.0" dependencies = [ "serde", @@ -2431,7 +2431,7 @@ dependencies = [ "embedded-hal-nb", "heapless", "log", - "sel4-driver-traits", + "sel4-driver-interfaces", "sel4-microkit", "sel4-microkit-message", "serde", @@ -2471,7 +2471,7 @@ version = "0.1.0" dependencies = [ "log", "sel4-bounce-buffer-allocator", - "sel4-driver-traits", + "sel4-driver-interfaces", "sel4-externally-shared", "sel4-microkit", "sel4-microkit-message", @@ -2521,7 +2521,7 @@ name = "sel4-pl011-driver" version = "0.1.0" dependencies = [ "embedded-hal-nb", - "sel4-driver-traits", + "sel4-driver-interfaces", "tock-registers", ] @@ -2781,7 +2781,7 @@ name = "sel4-virtio-net-driver-for-smoltcp" version = "0.1.0" dependencies = [ "log", - "sel4-driver-traits", + "sel4-driver-interfaces", "smoltcp", "virtio-drivers", ] diff --git a/crates/drivers/devices/pl011/Cargo.nix b/crates/drivers/devices/pl011/Cargo.nix index 901104115..2b203a65c 100644 --- a/crates/drivers/devices/pl011/Cargo.nix +++ b/crates/drivers/devices/pl011/Cargo.nix @@ -10,6 +10,6 @@ mk { package.name = "sel4-pl011-driver"; dependencies = { inherit (versions) tock-registers embedded-hal-nb; - inherit (localCrates) sel4-driver-traits; + inherit (localCrates) sel4-driver-interfaces; }; } diff --git a/crates/drivers/devices/pl011/Cargo.toml b/crates/drivers/devices/pl011/Cargo.toml index c71ec1b81..3116ada86 100644 --- a/crates/drivers/devices/pl011/Cargo.toml +++ b/crates/drivers/devices/pl011/Cargo.toml @@ -18,5 +18,5 @@ license = "BSD-2-Clause" [dependencies] embedded-hal-nb = "1.0" -sel4-driver-traits = { path = "../../interfaces" } +sel4-driver-interfaces = { path = "../../interfaces" } tock-registers = "0.8.1" diff --git a/crates/drivers/devices/pl011/src/lib.rs b/crates/drivers/devices/pl011/src/lib.rs index b75e1c3aa..ded850fa2 100644 --- a/crates/drivers/devices/pl011/src/lib.rs +++ b/crates/drivers/devices/pl011/src/lib.rs @@ -11,7 +11,7 @@ use core::convert::Infallible; use embedded_hal_nb::nb; use embedded_hal_nb::serial; -use sel4_driver_traits::HandleInterrupt; +use sel4_driver_interfaces::HandleInterrupt; mod device; diff --git a/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.nix b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.nix index ed9052eb5..be3c137fe 100644 --- a/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.nix +++ b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.nix @@ -20,6 +20,6 @@ mk rec { inherit (versions) log; smoltcp = smoltcpWith []; virtio-drivers = virtioDriversWith [ "alloc" ]; - inherit (localCrates) sel4-driver-traits; + inherit (localCrates) sel4-driver-interfaces; }; } diff --git a/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml index 4a53e0a80..04a556b0a 100644 --- a/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml +++ b/crates/drivers/devices/virtio/net-driver-for-smoltcp/Cargo.toml @@ -18,7 +18,7 @@ license = "MIT" [dependencies] log = "0.4.17" -sel4-driver-traits = { path = "../../../interfaces" } +sel4-driver-interfaces = { path = "../../../interfaces" } virtio-drivers = { version = "0.7.2", default-features = false, features = ["alloc"] } [dependencies.smoltcp] diff --git a/crates/drivers/devices/virtio/net-driver-for-smoltcp/src/lib.rs b/crates/drivers/devices/virtio/net-driver-for-smoltcp/src/lib.rs index 4f0784e72..63a8bd717 100644 --- a/crates/drivers/devices/virtio/net-driver-for-smoltcp/src/lib.rs +++ b/crates/drivers/devices/virtio/net-driver-for-smoltcp/src/lib.rs @@ -18,7 +18,7 @@ use smoltcp::time::Instant; use virtio_drivers::device::net::{RxBuffer, VirtIONet}; use virtio_drivers::{transport::Transport, Error, Hal}; -use sel4_driver_traits::{GetMacAddress, HandleInterrupt, MacAddress}; +use sel4_driver_interfaces::{GetMacAddress, HandleInterrupt, MacAddress}; pub const NET_QUEUE_SIZE: usize = 16; diff --git a/crates/drivers/interfaces/Cargo.nix b/crates/drivers/interfaces/Cargo.nix index d89ae04a8..513c75070 100644 --- a/crates/drivers/interfaces/Cargo.nix +++ b/crates/drivers/interfaces/Cargo.nix @@ -7,7 +7,7 @@ { mk, serdeWith }: mk { - package.name = "sel4-driver-traits"; + package.name = "sel4-driver-interfaces"; dependencies = { serde = serdeWith [ "derive" ]; }; diff --git a/crates/drivers/interfaces/Cargo.toml b/crates/drivers/interfaces/Cargo.toml index 1668801a2..d2fd52c23 100644 --- a/crates/drivers/interfaces/Cargo.toml +++ b/crates/drivers/interfaces/Cargo.toml @@ -10,7 +10,7 @@ # [package] -name = "sel4-driver-traits" +name = "sel4-driver-interfaces" version = "0.1.0" authors = ["Nick Spinale "] edition = "2021" diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.nix b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.nix index 67723a05f..50c67bc0b 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.nix +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.nix @@ -17,7 +17,7 @@ mk { inherit (versions) log embedded-hal-nb heapless; serde = serdeWith []; } // (with localCrates; { - inherit sel4-driver-traits; + inherit sel4-driver-interfaces; inherit sel4-microkit-message; sel4-microkit = sel4-microkit // { default-features = false; }; }); diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml index b3b95e5f7..105c16660 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/Cargo.toml @@ -20,7 +20,7 @@ license = "BSD-2-Clause" embedded-hal-nb = "1.0" heapless = "0.7.16" log = "0.4.17" -sel4-driver-traits = { path = "../../../drivers/interfaces" } +sel4-driver-interfaces = { path = "../../../drivers/interfaces" } sel4-microkit = { path = "../..", default-features = false } sel4-microkit-message = { path = "../../message" } serde = { version = "1.0.147", default-features = false } diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs index 67d33cdb0..a0d466cce 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs @@ -11,7 +11,7 @@ use embedded_hal_nb::nb; use embedded_hal_nb::serial; use heapless::Deque; -use sel4_driver_traits::HandleInterrupt; +use sel4_driver_interfaces::HandleInterrupt; use sel4_microkit::{Channel, Handler, MessageInfo}; use sel4_microkit_message::MessageInfoExt; diff --git a/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.nix b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.nix index 00866fab5..b9d19cce1 100644 --- a/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.nix +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.nix @@ -19,7 +19,7 @@ mk { serde = serdeWith []; } // (with localCrates; { inherit sel4-microkit-message; - inherit sel4-driver-traits; + inherit sel4-driver-interfaces; sel4-microkit = sel4-microkit // { default-features = false; }; sel4-bounce-buffer-allocator = sel4-bounce-buffer-allocator; sel4-externally-shared = sel4-externally-shared // { features = [ "unstable" ]; }; diff --git a/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml index 40972b912..3b86dee20 100644 --- a/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/Cargo.toml @@ -19,7 +19,7 @@ license = "BSD-2-Clause" [dependencies] log = "0.4.17" sel4-bounce-buffer-allocator = { path = "../../../sel4-bounce-buffer-allocator" } -sel4-driver-traits = { path = "../../../drivers/interfaces" } +sel4-driver-interfaces = { path = "../../../drivers/interfaces" } sel4-externally-shared = { path = "../../../sel4-externally-shared", features = ["unstable"] } sel4-microkit = { path = "../..", default-features = false } sel4-microkit-message = { path = "../../message" } diff --git a/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/client.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/client.rs index 0784fca9c..f1a0a3505 100644 --- a/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/client.rs +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/client.rs @@ -5,7 +5,7 @@ // SPDX-License-Identifier: BSD-2-Clause // -use sel4_driver_traits::MacAddress; +use sel4_driver_interfaces::MacAddress; use sel4_microkit::{Channel, MessageInfo}; use sel4_microkit_message::MessageInfoExt as _; diff --git a/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/common.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/common.rs index 8d9e0530c..6464cbafe 100644 --- a/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/common.rs +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/common.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; -use sel4_driver_traits::MacAddress; +use sel4_driver_interfaces::MacAddress; #[derive(Debug, Serialize, Deserialize)] pub(crate) enum Request { diff --git a/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/driver.rs b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/driver.rs index dc42e7b1c..49c74a614 100644 --- a/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/driver.rs +++ b/crates/sel4-microkit/drivers/smoltcp-device-adapter/src/driver.rs @@ -12,7 +12,7 @@ use smoltcp::{ time::Instant, }; -use sel4_driver_traits::{GetMacAddress, HandleInterrupt}; +use sel4_driver_interfaces::{GetMacAddress, HandleInterrupt}; use sel4_externally_shared::ExternallySharedRef; use sel4_microkit::{Channel, Handler, Infallible, MessageInfo}; use sel4_microkit_message::MessageInfoExt as _; From 378aba65cf4e39202f8d9dca51e39ce8e27fa513 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 15 Jun 2024 01:13:26 -0400 Subject: [PATCH 27/64] Use structured reponse types for Microkit IPC In sel4-embedded-hal-adapters Signed-off-by: Nick Spinale --- .../src/serial/client.rs | 39 ++++++++++--------- .../src/serial/driver.rs | 20 +++++----- .../serial/{common.rs => message_types.rs} | 15 ++++--- .../embedded-hal-adapters/src/serial/mod.rs | 4 +- 4 files changed, 39 insertions(+), 39 deletions(-) rename crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/{common.rs => message_types.rs} (60%) diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/client.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/client.rs index 23c9996e4..467208e2b 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/client.rs +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/client.rs @@ -11,7 +11,7 @@ use embedded_hal_nb::serial; use sel4_microkit::{Channel, MessageInfo}; use sel4_microkit_message::MessageInfoExt; -use super::common::*; +use super::message_types::*; /// Device-independent embedded_hal_nb::serial interface to a serial-device /// component. Interact with it using [serial::Read], [serial::Write], @@ -26,13 +26,20 @@ impl Client { Client { channel } } - pub fn blocking_write(&mut self, val: u8) -> Result<(), Error> { - let req = Request::PutChar { val }; + fn request(&self, req: Request) -> Result { self.channel .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard::>() + .recv_using_postcard::() .map_err(|_| Error::InvalidResponse)? - .map_err(Error::PutCharError)?; + .map_err(Error::ErrorResponse) + } + + pub fn blocking_write(&mut self, val: u8) -> Result<(), Error> { + let resp = self.request(Request::PutChar { val })?; + match resp { + SuccessResponse::PutChar => (), + _ => return Err(Error::UnexpectedResponse), + } Ok(()) } } @@ -43,22 +50,18 @@ impl serial::ErrorType for Client { impl serial::Read for Client { fn read(&mut self) -> nb::Result { - let req = Request::GetChar; - let resp = self - .channel - .pp_call(MessageInfo::send_using_postcard(req).unwrap()) - .recv_using_postcard::>() - .map_err(|_| Error::InvalidResponse) - .map_err(nb::Error::Other)? - .map_err(Error::GetCharError) - .map_err(nb::Error::Other)?; - resp.val.ok_or(nb::Error::WouldBlock) + let resp = self.request(Request::GetChar)?; + let val = match resp { + SuccessResponse::GetChar { val } => val, + _ => return Err(Error::UnexpectedResponse.into()), + }; + val.ok_or(nb::Error::WouldBlock) } } impl serial::Write for Client { fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> { - self.blocking_write(val).map_err(nb::Error::Other)?; + self.blocking_write(val)?; Ok(()) } @@ -69,9 +72,9 @@ impl serial::Write for Client { #[derive(Debug, Copy, Clone)] pub enum Error { - PutCharError(PutCharError), - GetCharError(GetCharError), + ErrorResponse(ErrorResponse), InvalidResponse, + UnexpectedResponse, } impl serial::Error for Error { diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs index a0d466cce..d5fd5ede6 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/driver.rs @@ -15,7 +15,7 @@ use sel4_driver_interfaces::HandleInterrupt; use sel4_microkit::{Channel, Handler, MessageInfo}; use sel4_microkit_message::MessageInfoExt; -use super::common::*; +use super::message_types::*; // TODO // Factor buffering out into wrapper for serial::{Read,Write} @@ -86,22 +86,20 @@ where if channel == self.client { Ok(match msg_info.recv_using_postcard::() { Ok(req) => { - match req { - Request::PutChar { val } => MessageInfo::send_using_postcard( - match nb::block!(self.device.write(val)) { - Ok(_) => Ok(PutCharResponse), - Err(_) => Err(PutCharError), - }, - ) - .unwrap(), + let resp = match req { + Request::PutChar { val } => match nb::block!(self.device.write(val)) { + Ok(_) => Ok(SuccessResponse::PutChar), + Err(_) => Err(ErrorResponse::WriteError), + }, Request::GetChar => { let val = self.buffer.pop_front(); if val.is_some() { self.notify = true; } - MessageInfo::send_using_postcard::>(Ok(GetCharResponse { val })).unwrap() + Ok(SuccessResponse::GetChar { val }) } - } + }; + MessageInfo::send_using_postcard(resp).unwrap() } Err(_) => MessageInfo::send_unspecified_error(), }) diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/common.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/message_types.rs similarity index 60% rename from crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/common.rs rename to crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/message_types.rs index 03fa4c2ea..efb3c0432 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/common.rs +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/message_types.rs @@ -13,16 +13,15 @@ pub(crate) enum Request { GetChar, } -#[derive(Debug, Serialize, Deserialize)] -pub(crate) struct PutCharResponse; +pub(crate) type Response = Result; #[derive(Debug, Serialize, Deserialize)] -pub(crate) struct GetCharResponse { - pub val: Option, +pub(crate) enum SuccessResponse { + PutChar, + GetChar { val: Option }, } #[derive(Debug, Copy, Clone, Serialize, Deserialize)] -pub struct PutCharError; - -#[derive(Debug, Copy, Clone, Serialize, Deserialize)] -pub struct GetCharError; +pub enum ErrorResponse { + WriteError, +} diff --git a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/mod.rs b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/mod.rs index 6f77cb8b5..a87bbfb09 100644 --- a/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/mod.rs +++ b/crates/sel4-microkit/drivers/embedded-hal-adapters/src/serial/mod.rs @@ -5,9 +5,9 @@ // SPDX-License-Identifier: BSD-2-Clause // -mod common; +mod message_types; pub mod client; pub mod driver; -pub use common::{GetCharError, PutCharError}; +pub use message_types::ErrorResponse; From 14860167e21a9238a312086eb815b054d4f4e546 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 15 Jun 2024 01:24:14 -0400 Subject: [PATCH 28/64] crates/sel4-microkit: Ensure empty message is empty Signed-off-by: Nick Spinale --- crates/sel4-microkit/message/types/src/lib.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/sel4-microkit/message/types/src/lib.rs b/crates/sel4-microkit/message/types/src/lib.rs index 0f926953c..e487b37b2 100644 --- a/crates/sel4-microkit/message/types/src/lib.rs +++ b/crates/sel4-microkit/message/types/src/lib.rs @@ -99,13 +99,22 @@ impl MessageValueSend for EmptyMessageValue { } impl MessageValueRecv for EmptyMessageValue { - type Error = Infallible; + type Error = RecvEmptyMessageValueError; - fn read_message_value(_buf: &[u8]) -> Result { - Ok(Self) + fn read_message_value(buf: &[u8]) -> Result { + if buf.is_empty() { + Ok(Self) + } else { + Err(Self::Error::MessageIsNotEmpty) + } } } +#[derive(Copy, Clone, Debug)] +pub enum RecvEmptyMessageValueError { + MessageIsNotEmpty, +} + // // // #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] From 3dc8a2c5bbcb48d8811cda1aec570d23aebb49fd Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 15 Jun 2024 01:36:16 -0400 Subject: [PATCH 29/64] crates/sel4-microkit/message: Rework Signed-off-by: Nick Spinale --- crates/sel4-microkit/message/src/lib.rs | 92 ++++++--- crates/sel4-microkit/message/types/src/lib.rs | 177 ++++++++++-------- 2 files changed, 173 insertions(+), 96 deletions(-) diff --git a/crates/sel4-microkit/message/src/lib.rs b/crates/sel4-microkit/message/src/lib.rs index f5f7757cd..b78a2a0d4 100644 --- a/crates/sel4-microkit/message/src/lib.rs +++ b/crates/sel4-microkit/message/src/lib.rs @@ -15,8 +15,8 @@ use serde::{Deserialize, Serialize}; use sel4_microkit_base::{with_msg_bytes, with_msg_bytes_mut, MessageInfo, MessageRegisterValue}; use sel4_microkit_message_types::{ - EmptyMessage, MessageLabel, MessageRecv, MessageSend, MessageValueRecv, MessageValueSend, - TriviallyLabeled, TryFromDefaultMessageLabelError, + EmptyMessage, EmptyMessageValue, MessageLabel, MessageRecv, MessageSend, MessageValueRecv, + MessageValueSend, TriviallyLabeled, }; #[cfg(feature = "postcard")] @@ -24,14 +24,66 @@ use sel4_microkit_message_types::MessageValueUsingPostcard; pub use sel4_microkit_message_types as types; -pub const UNSPECIFIED_ERROR_LABEL: MessageLabel = (1 << MessageInfo::label_width()) - 1; +const MAX_MESSAGE_LABEL: MessageLabel = + !0 >> (mem::size_of::() * 8 - MessageInfo::label_width()); + +// // // + +pub const UNSPECIFIED_ERROR_MESSAGE_LABEL: MessageLabel = MAX_MESSAGE_LABEL; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +pub struct UnspecifiedErrorMessage; + +impl From> + for UnspecifiedErrorMessage +{ + fn from(_: TriviallyLabeled) -> Self { + Default::default() + } +} + +impl From + for TriviallyLabeled +{ + fn from(_: UnspecifiedErrorMessage) -> Self { + Default::default() + } +} + +impl MessageSend for UnspecifiedErrorMessage { + type Label = as MessageSend>::Label; + + type Error = as MessageSend>::Error; + + fn write_message(self, buf: &mut [u8]) -> Result<(Self::Label, usize), Self::Error> { + >::from(self) + .write_message(buf) + } +} + +impl MessageRecv for UnspecifiedErrorMessage { + type Label = as MessageRecv>::Label; + + type Error = as MessageRecv>::Error; + + fn read_message(label: Self::Label, buf: &[u8]) -> Result { + >::read_message( + label, buf, + ) + .map(Into::into) + } +} + +// // // pub trait MessageInfoExt: Sized { fn send(val: T) -> Result; fn recv(self) -> Result>; - fn send_unspecified_error() -> Self; + fn send_unspecified_error() -> Self { + Self::send(UnspecifiedErrorMessage).unwrap_or_else(|absurdity| match absurdity {}) + } fn send_empty() -> Self { Self::send(EmptyMessage).unwrap_or_else(|absurdity| match absurdity {}) @@ -42,13 +94,14 @@ pub trait MessageInfoExt: Sized { } fn send_with_trivial_label(val: T) -> Result { - Self::send(TriviallyLabeled(val)) + type Helper = TriviallyLabeled; // get default LABEL + Self::send(Helper::new(val)) } fn recv_with_trivial_label( self, ) -> Result>> { - self.recv().map(|TriviallyLabeled(val)| val) + self.recv().map(TriviallyLabeled::into_inner) } #[cfg(feature = "postcard")] @@ -61,13 +114,7 @@ pub trait MessageInfoExt: Sized { #[cfg(feature = "postcard")] fn recv_using_postcard Deserialize<'a>>( self, - ) -> Result< - T, - MessageRecvError< - TryFromDefaultMessageLabelError, - as MessageValueRecv>::Error, - >, - > { + ) -> Result>>> { self.recv_with_trivial_label() .map(|MessageValueUsingPostcard(val)| val) } @@ -77,14 +124,15 @@ impl MessageInfoExt for MessageInfo { fn send(val: T) -> Result { let (label, num_bytes) = with_msg_bytes_mut(|buf| val.write_message(buf))?; let label = label.into(); - assert!(label < UNSPECIFIED_ERROR_LABEL); // TODO return error instead? + assert!(label <= MAX_MESSAGE_LABEL); + // assert!(label != UNSPECIFIED_ERROR_MESSAGE_LABEL); Ok(Self::new(label, bytes_to_mrs(num_bytes))) } fn recv(self) -> Result> { - if self.label() >= UNSPECIFIED_ERROR_LABEL { - return Err(MessageRecvError::Unspecified); - } + // if self.label() == UNSPECIFIED_ERROR_MESSAGE_LABEL) { + // return Err(MessageRecvError::Unspecified); + // } let label = self .label() .try_into() @@ -93,9 +141,9 @@ impl MessageInfoExt for MessageInfo { .map_err(MessageRecvError::ValueError) } - fn send_unspecified_error() -> Self { - Self::new(UNSPECIFIED_ERROR_LABEL, 0) - } + // fn send_unspecified_error() -> Self { + // Self::new(UNSPECIFIED_ERROR_MESSAGE_LABEL, 0) + // } } pub type MessageRecvErrorFor = MessageRecvError< @@ -107,7 +155,7 @@ pub type MessageRecvErrorFor = MessageRecvError< pub enum MessageRecvError { LabelError(E1), ValueError(E2), - Unspecified, + // Unspecified, } impl fmt::Display for MessageRecvError { @@ -115,7 +163,7 @@ impl fmt::Display for MessageRecvError write!(f, "label error: {}", err), Self::ValueError(err) => write!(f, "value error: {}", err), - Self::Unspecified => write!(f, "unspecified error"), + // Self::Unspecified => write!(f, "unspecified error"), } } } diff --git a/crates/sel4-microkit/message/types/src/lib.rs b/crates/sel4-microkit/message/types/src/lib.rs index e487b37b2..9477f8840 100644 --- a/crates/sel4-microkit/message/types/src/lib.rs +++ b/crates/sel4-microkit/message/types/src/lib.rs @@ -55,38 +55,6 @@ pub trait MessageRecv: Sized { // // // -impl MessageValueSend for T { - type Error = SendAsBytesError; - - fn write_message_value(self, buf: &mut [u8]) -> Result { - self.write_to_prefix(buf) - .ok_or(SendAsBytesError::ValueTooLarge)?; - Ok(mem::size_of_val(&self)) - } -} - -impl MessageValueRecv for T { - type Error = RecvFromBytesError; - - fn read_message_value(buf: &[u8]) -> Result { - Unalign::::read_from_prefix(buf) - .ok_or(RecvFromBytesError::MessageTooShort) - .map(|unalign| unalign.get()) - } -} - -#[derive(Copy, Clone, Debug)] -pub enum SendAsBytesError { - ValueTooLarge, -} - -#[derive(Copy, Clone, Debug)] -pub enum RecvFromBytesError { - MessageTooShort, -} - -// // // - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct EmptyMessageValue; @@ -117,88 +85,149 @@ pub enum RecvEmptyMessageValueError { // // // -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -pub struct EmptyMessage; - -impl MessageSend for EmptyMessage { - type Label = DefaultMessageLabel; +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +pub struct ConstMessageLabel(()); - type Error = Infallible; +impl ConstMessageLabel