Skip to content

Commit 5c0a837

Browse files
authored
Merge pull request #292 from TimePrinciple/monorepo
monorepo: Merge `kvm-bindings` into `kvm-ioctls`
2 parents d7cfcad + 37ec24a commit 5c0a837

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+14322
-27
lines changed

.buildkite/custom-tests.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"tests": [
3+
{
4+
"test_name": "build-fam-gnu",
5+
"command": "cargo build --release --features=fam-wrappers",
6+
"platform": [
7+
"x86_64",
8+
"aarch64",
9+
"riscv64"
10+
]
11+
},
12+
{
13+
"test_name": "build-fam-musl",
14+
"command": "cargo build --release --features=fam-wrappers --target {target_platform}-unknown-linux-musl",
15+
"platform": [
16+
"x86_64",
17+
"aarch64"
18+
]
19+
},
20+
{
21+
"test_name": "build-serde-gnu",
22+
"command": "cargo build --release --features=serde",
23+
"platform": [
24+
"x86_64",
25+
"aarch64",
26+
"riscv64"
27+
]
28+
},
29+
{
30+
"test_name": "build-serde-musl",
31+
"command": "cargo build --release --features=serde --target {target_platform}-unknown-linux-musl",
32+
"platform": [
33+
"x86_64",
34+
"aarch64"
35+
]
36+
}
37+
]
38+
}

.cargo/config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
# This workaround is needed because the linker is unable to find __addtf3,
2+
# __multf3 and __subtf3.
3+
# Related issue: https://github.com/rust-lang/compiler-builtins/issues/201
14
[target.aarch64-unknown-linux-musl]
2-
rustflags = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc" ]
5+
rustflags = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc"]

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Cargo.lock
2-
/target
2+
target
33
.idea
44
**/*.rs.bk
55
**/.pytest_cache/

Cargo.toml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
[package]
2-
name = "kvm-ioctls"
3-
version = "0.19.0"
4-
authors = ["Amazon Firecracker Team <[email protected]>"]
5-
description = "Safe wrappers over KVM ioctls"
6-
repository = "https://github.com/rust-vmm/kvm-ioctls"
7-
readme = "README.md"
8-
keywords = ["kvm"]
9-
license = "Apache-2.0 OR MIT"
10-
edition = "2021"
11-
12-
[dependencies]
13-
libc = "0.2.39"
14-
kvm-bindings = { version = "0.10.0", features = ["fam-wrappers"] }
15-
vmm-sys-util = "0.12.1"
16-
bitflags = "2.4.1"
17-
18-
[dev-dependencies]
19-
byteorder = "1.2.1"
1+
[workspace]
2+
resolver = "2"
3+
members = [
4+
"kvm-bindings",
5+
"kvm-ioctls",
6+
]

coverage_config_aarch64.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 77.1,
2+
"coverage_score": 60.9,
33
"exclude_path": "",
4-
"crate_features": ""
4+
"crate_features": "fam-wrappers,serde"
55
}

coverage_config_x86_64.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 92.61,
3-
"exclude_path": "",
4-
"crate_features": ""
2+
"coverage_score": 91.46,
3+
"exclude_path": ".*bindings\\.rs",
4+
"crate_features": "fam-wrappers,serde"
55
}

kvm-bindings/CHANGELOG.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Changelog
2+
## [Unreleased]
3+
4+
### Added
5+
6+
### Changed
7+
8+
### Removed
9+
10+
## [0.10.0]
11+
12+
### Added
13+
14+
- RISC-V KVM bindings for Linux kernel v6.9, including serialization support.
15+
16+
## [0.9.1]
17+
18+
### Changed
19+
20+
- Fixed and validated manual (De)Serialize implementations to work with
21+
`serde_json` crate.
22+
23+
## [0.9.0]
24+
25+
### Changed
26+
27+
- Replaced the v6.2 bindings of arm64, x86\_64 with the v6.9 ones.
28+
29+
### Removed
30+
31+
- Removed v6.2 bindings.
32+
33+
## [0.8.2]
34+
35+
### Changed
36+
37+
- Improve performance of bindings deserialization by \~5% by avoiding
38+
a temporary allocation.
39+
40+
## [0.8.1]
41+
42+
### Fixed
43+
44+
- Implement `Default` for `kvm_xsave2`, which fixes usage of `Xsave`
45+
unconditionally causing compile errors in downstream crates.
46+
47+
## [0.8.0]
48+
49+
### Added
50+
51+
- An opt-in feature `serde` that enables [`serde`](https://serde.rs)-based
52+
(de)serialization of various bindings.
53+
54+
## [0.7.0]
55+
56+
### Changed
57+
- API change in the bindings from upstream kernel changes:
58+
* system\_event has been made into a new union
59+
- The x86 module has been renamed to x86\_64 for consistency (matches the kernel
60+
architecture directory name)
61+
- Added all features to the generated docs.rs documentation.
62+
63+
### Removed
64+
65+
- Dropped "x86" (32-bit) x86 support
66+
67+
## [0.6.0]
68+
69+
### Changed
70+
71+
- Updated vmm-sys-utils dependency to 0.11.0
72+
- Switched to specifying dependencies using caret requirements
73+
instead of comparision requirements
74+
75+
### Added
76+
77+
- Implement `PartialEq` for fam\_wrappers
78+
79+
## [0.5.0]
80+
81+
### Changed
82+
83+
- Replaced the v4.20 bindings with the v5.13 ones.
84+
85+
### Removed
86+
87+
- Removed v4.14 bindings.
88+
89+
## [0.4.0]
90+
91+
- vmm-sys-utils dependency bumped to match kvm-ioctls.
92+
93+
## [0.3.0]
94+
95+
### Added
96+
97+
- Enabled `fam-wrappers` support on arm and arm64.
98+
- Added fam-wrapper for the arm specific `kvm_reg_list` struct.
99+
100+
## [0.2.0]
101+
102+
### Added
103+
104+
- Added opt-in feature `fam-wrappers` that enables exporting
105+
safe wrappers over generated structs with flexible array
106+
members. This optional feature has an external dependency
107+
on `vmm-sys-util`.
108+
- Added safe fam-wrappers for `kvm_msr_list`, `kvm_msrs`,
109+
and `kvm_cpuid2`.
110+
111+
## [0.1.1]
112+
113+
### Changed
114+
115+
- Do not enforce rust Edition 2018.
116+
117+
## [0.1.0]
118+
119+
### Added
120+
121+
- KVM bindings for Linux kernel version 4.14 and 4.20 with
122+
support for arm, arm64, x86 and x86_64.

kvm-bindings/CONTRIBUTING.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Contributing to kvm-bindings
2+
3+
## Dependencies
4+
5+
### Bindgen
6+
The bindings are currently generated using
7+
[bindgen](https://crates.io/crates/bindgen) version 0.70.1:
8+
```bash
9+
cargo install bindgen-cli --vers 0.70.1
10+
```
11+
12+
### Linux Kernel
13+
Generating bindings depends on the Linux kernel, so you need to have the
14+
repository on your machine:
15+
16+
```bash
17+
git clone https://github.com/torvalds/linux.git
18+
```
19+
20+
## Updating bindings / adding a new architecture
21+
22+
When adding a new architecture, the bindings must be generated for all existing
23+
versions for consistency reasons.
24+
25+
### Example for arm64 and kernel version 6.9
26+
27+
For this example we assume that you have both linux and kvm-bindings
28+
repositories in your root.
29+
30+
```bash
31+
# Step 1 (if adding a new architecture): Create a new module using the name of the architecture in src/
32+
pushd kvm-bindings
33+
mkdir src/arm64
34+
popd
35+
36+
# linux is the repository that you cloned at the previous step.
37+
pushd linux
38+
# Step 2: Checkout the version you want to generate the bindings for.
39+
git checkout v6.9
40+
41+
# Step 3: Generate the bindings.
42+
# This will generate the headers for the targeted architecture and place them
43+
# in the user specified directory
44+
45+
export ARCH=arm64
46+
make headers_install ARCH=$ARCH INSTALL_HDR_PATH="$ARCH"_headers
47+
pushd "$ARCH"_headers
48+
bindgen include/linux/kvm.h -o bindings.rs \
49+
--impl-debug --with-derive-default \
50+
--with-derive-partialeq --impl-partialeq \
51+
-- -Iinclude
52+
popd
53+
54+
# Step 4: Copy the generated file to the arm64 module.
55+
popd
56+
cp linux/"$ARCH"_headers/bindings.rs kvm-bindings/src/arm64
57+
58+
```
59+
60+
Steps 2, 3 and 4 must be repeated for all existing architectures.
61+
62+
Now that we have the bindings generated, for a new architecture we can copy the
63+
module file from one of the existing modules.
64+
65+
```bash
66+
cp arm/mod.rs arm64/
67+
```
68+
69+
Also, you will need to add the new architecture to `kvm-bindings/lib.rs`.
70+
71+
When regenerating bindings, care must be taken to re-add various `zerocopy`
72+
derives under the `serde` feature. All items that require derives are
73+
listed in the `x86_64/serialize.rs` and `arm64/serialize.rs` inside the
74+
`serde_impls!` macro invocation, and missing derives will cause these
75+
modules to fail compilation. For all items listed here, the following
76+
derive should be present:
77+
78+
```rs
79+
#[cfg_attr(
80+
feature = "serde",
81+
derive(zerocopy::AsBytes, zerocopy::FromBytes, zerocopy::FromZeroes)
82+
)]
83+
```
84+
85+
Any types whose name contains a suffix akin to `__bindgen_ty_<number>` and
86+
which is contained in any struct listed in `serialize.rs` will also need
87+
to have this derive added (otherwise compilation will fail). Note that
88+
these types are not explicitly listed in `serialize.rs`, as their names
89+
can change across `bindgen.rs` versions.
90+
91+
Lastly, in `x86_64/bindings.rs`, the derives also need to be added to
92+
`struct __BindgenBitfieldUnit<Storage>` and `struct __IncompleteArrayField<T>`.
93+
Additionally, these structs need to have their layout changed from `#[repr(C)]`
94+
to `#[repr(transparent)]`. This is needed because `zerocopy` traits can only be
95+
derived on generic structures that are `repr(transparent)` or `repr(packed)`.
96+
97+
### Future Improvements
98+
All the above steps are scriptable, so in the next iteration I will add a
99+
script to generate the bindings.
100+
101+
# Testing
102+
103+
This crate is tested using
104+
[rust-vmm-ci](https://github.com/rust-vmm/rust-vmm-ci) and
105+
[Buildkite](https://buildkite.com/) pipelines. Each new feature added to this crate must be
106+
accompanied by Buildkite steps for testing the following:
107+
- Release builds (using musl/gnu) with the new feature on arm and x86
108+
- Coverage test as specified in the
109+
[rust-vmm-ci readme](https://github.com/rust-vmm/rust-vmm-ci#getting-started-with-rust-vmm-ci).

kvm-bindings/Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "kvm-bindings"
3+
version = "0.10.0"
4+
authors = ["Amazon firecracker team <[email protected]>"]
5+
description = "Rust FFI bindings to KVM generated using bindgen."
6+
repository = "https://github.com/rust-vmm/kvm-bindings"
7+
readme = "README.md"
8+
keywords = ["kvm"]
9+
license = "Apache-2.0"
10+
11+
[package.metadata.docs.rs]
12+
all-features = true
13+
rustdoc-args = ["--cfg", "docsrs"]
14+
15+
[features]
16+
fam-wrappers = ["vmm-sys-util"]
17+
# It is not needed to enable the `serde` feature of `vmm-sys-util` here, because due to how cargo merges features,
18+
# if a downstream crate enables vmm-sys-util in its Cargo.toml, it will get enabled globally.
19+
serde = ["dep:serde", "serde/derive", "dep:zerocopy"]
20+
21+
22+
[dependencies]
23+
vmm-sys-util = { version = "0.12.1", optional = true }
24+
serde = { version = "1.0.0", optional = true, features = ["derive"] }
25+
zerocopy = { version = "0.7.32", optional = true, features = ["derive"] }
26+
27+
[dev-dependencies]
28+
bincode = "1.3.3"
29+
serde_json = "1.0.125"
File renamed without changes.

0 commit comments

Comments
 (0)