Skip to content
This repository was archived by the owner on Jul 17, 2025. It is now read-only.

Commit bee5859

Browse files
committed
Change CPUThreads struct to use abomonation for serialization instead of serde
1 parent 63b7182 commit bee5859

File tree

7 files changed

+38
-11
lines changed

7 files changed

+38
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,23 @@ gdbstub = { git = "https://github.com/daniel5151/gdbstub.git", branch = "dev/0.6
5353
gdbstub_arch = { git = "https://github.com/daniel5151/gdbstub.git", branch = "dev/0.6" }
5454
# Optional external libraries:
5555
addr2line = { version = "0.16", default-features = false, features = ["rustc-demangle"], optional = true }
56-
abomonation = { git="https://github.com/hunhoffe/abomonation.git", branch="no-std", default-features = false, optional = true }
57-
core2 = { version = "0.3", default-features = false, features = [ "alloc" ], optional = true }
5856
smoltcp = { version = "0.8.0", default-features = false, features = [ "alloc", "log", "proto-ipv4", "proto-igmp", "proto-dhcpv4", "socket-raw", "socket-icmp", "socket-udp", "socket-tcp" ], optional = true }
5957

6058
[[bin]]
6159
name = "nrk"
6260
path = "src/main.rs"
6361

6462
[target.'cfg(target_os = "none")'.dependencies]
63+
abomonation = { git="https://github.com/hunhoffe/abomonation.git", branch="no-std", default-features = false, optional = true }
64+
core2 = { version = "0.3", default-features = false, features = [ "alloc" ], optional = true }
6565
serde = { version = "1", default-features = false, features = ["alloc", "derive"], optional = true }
6666
serde_cbor = { version = "0.11", default-features = false, optional = true }
6767

6868
[target.'cfg(not(target_os = "none"))'.dependencies]
6969
libc = { version = "0.2.53", default-features = false }
7070
csv = "1.1"
71+
abomonation = { git="https://github.com/hunhoffe/abomonation.git", branch="no-std", default-features = false, optional = true }
72+
core2 = { version = "0.3", default-features = false, features = [ "alloc" ], optional = true }
7173
serde = { version = "1" }
7274
serde_cbor = { version = "0.11" }
7375
rand = { version = "0.8", features = ["small_rng"] }
@@ -86,7 +88,7 @@ testutils = { path = "testutils" }
8688
cc = "1.0"
8789

8890
[features]
89-
default = ["addr2line", "serde", "serde_cbor"]
91+
default = ["addr2line", "serde", "serde_cbor", "core2", "abomonation"]
9092
# Enable Ethernet based networking.
9193
ethernet = ["smoltcp"]
9294
# Enable shared memory based communication.
@@ -95,7 +97,7 @@ shmem = []
9597
# kernels that communicate with the control-plane for coarse-grained policy
9698
# decisions. Use in combination with `shmem` or `ethernet` feature to control
9799
# transport.
98-
rackscale = ["core2", "abomonation", "rpc"]
100+
rackscale = ["rpc"]
99101
# server code (supply --kgdb to run.py).
100102
gdb = []
101103

kernel/src/arch/x86_64/syscall.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloc::boxed::Box;
55
use alloc::vec::Vec;
66
use core::convert::TryInto;
77

8+
use abomonation::encode;
89
use fallible_collections::{FallibleVec, FallibleVecGlobal};
910
use log::{debug, info, trace, warn};
1011
use x86::bits64::paging::{PAddr, VAddr, BASE_PAGE_SIZE, LARGE_PAGE_SIZE};
@@ -80,8 +81,14 @@ impl<T: Arch86SystemDispatch> SystemDispatch<u64> for T {
8081
})?;
8182
}
8283

83-
// TODO(dependency): Get rid of serde/serde_cbor, use something sane instead
84-
let serialized = serde_cbor::to_vec(&return_threads).unwrap();
84+
// We know that we will need room for at least all of the hw threads. abomonation
85+
// may increase size as needed.
86+
let mut serialized =
87+
Vec::try_with_capacity(num_threads * core::mem::size_of::<kpi::system::CpuThread>())
88+
.expect("Failed to allocate memory for serialized data");
89+
unsafe { encode(&return_threads, &mut serialized) }
90+
.expect("Failed to serialize hw_threads");
91+
8592
if serialized.len() <= vaddr_buf_len as usize {
8693
let mut user_slice = UserSlice::new(
8794
current_pid()?,

lib/kpi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
99
x86 = { version = "0.52", features = ["unstable"] }
1010
abomonation = { git="https://github.com/hunhoffe/abomonation.git", branch="no-std", default-features = false }
1111
bitflags = "1.2"
12+
core2 = { version = "0.3", default-features = false, features = [ "alloc" ] }
1213
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
1314
serde_cbor = { version = "0.11", default-features = false, features = ["alloc"] }
1415
log = "0.4"

lib/kpi/src/process.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ impl CoreToken {
4444
}
4545
}
4646

47+
// TODO: still use serde instead of abomonation because abomonation doesn't
48+
// know how to handle 'static string.
4749
#[derive(Serialize, Deserialize, Debug, Default, Copy, Clone, Eq, PartialEq)]
4850
pub struct ProcessInfo {
4951
pub has_tls: bool,

lib/kpi/src/syscalls/system.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
77
use alloc::vec::Vec;
88

9+
use abomonation::decode;
10+
911
use crate::{syscall, *};
1012

1113
use crate::system::{CoreId, CpuThread};
@@ -30,8 +32,18 @@ impl System {
3032
let len = len as usize;
3133
debug_assert!(len <= buf.len());
3234
buf.resize(len, 0);
33-
let deserialized: Vec<CpuThread> = serde_cbor::from_slice(&buf).unwrap();
34-
Ok(deserialized)
35+
if let Some((deserialized, remaining)) =
36+
unsafe { decode::<Vec<CpuThread>>(&mut buf[..len]) }
37+
{
38+
if remaining.len() > 0 {
39+
Err(SystemCallError::InternalError)
40+
} else {
41+
// This does perform a copy, which is less than ideal.
42+
Ok(deserialized.to_vec())
43+
}
44+
} else {
45+
Err(SystemCallError::InternalError)
46+
}
3547
} else {
3648
Err(SystemCallError::from(r))
3749
}

lib/kpi/src/system.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
//! Data structures to exchange system-wide information between kernel and user-space.
5-
6-
use serde::{Deserialize, Serialize};
5+
use abomonation::{unsafe_abomonate, Abomonation};
6+
use core2::io::Result as IOResult;
7+
use core2::io::Write;
78

89
/// A system global ID for a CPU hardware thread.
910
pub type GlobalThreadId = usize;
@@ -20,7 +21,7 @@ pub type PackageId = usize;
2021
/// Affinity region, a NUMA node (consists of a bunch of threads/core/packages and memory regions).
2122
pub type NodeId = usize;
2223

23-
#[derive(Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, Debug)]
24+
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Copy, Clone)]
2425
pub struct CpuThread {
2526
/// ID the thread, global within a system.
2627
pub id: GlobalThreadId,
@@ -33,3 +34,4 @@ pub struct CpuThread {
3334
/// ID of the thread (relative to the core (usually either 0 or 1)).
3435
pub thread_id: ThreadId,
3536
}
37+
unsafe_abomonate!(CpuThread: id, node_id, package_id, core_id, thread_id);

0 commit comments

Comments
 (0)