Use the potentially more efficient clone_from() function when cloning
and assigning.
BUG=b:344974550
TEST=tools/clippy
Change-Id: I0601b59a80fdebbc09b212110c6e68e23a52bcfe
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5604659
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Ensure that every Cargo.toml dependency on a third-party crates.io crate
specifies at least a major version, or a minor version for 0.x crates,
to ensure that if a new major version is published, it cannot cause API
breaks.
The versions are selected to match the ones already in Cargo.lock, so
this should have no functional change, but it will help prevent new "*"
versions from being introduced via copy-and-paste.
For rationale, see the Cargo FAQ:
<https://doc.rust-lang.org/cargo/faq.html#can-libraries-use--as-a-version-for-their-dependencies>
`minijail`, `audio_streams`, and `cras` are left as "*" for now, since
they have unusual situations (imported from a submodule and/or replaced
at build time with ebuild magic).
BUG=None
TEST=tools/dev_container tools/presubmit
TEST=verify Cargo.lock is unchanged
Change-Id: Ifa18199f812f01d2d10bfb4146b3353c1a76527c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5555656
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
The "arm,arm-v8" compatible string is not documented in the Linux kernel
DT bindings [1] and the intended string probably was "arm,armv8" so
update the DT generation code and test files to use that instead.
[1]: https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/cpus.yaml
BUG=b:335470851
Change-Id: I537b7599a89988244714e2c7af766d6b01e398bc
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5464099
Auto-Submit: Pierre-Clément Tosi <ptosi@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Due to the use of BTreeMap for node and property storage, nodes and
properties are iterated over and written in alphabetical order.
This change replaces BTreeMap with IndexMap which preserves original
insertion order.
Bug: 331402446
Test: cd cros_fdt && cargo test
Change-Id: I10fc74a21bcfbdc71bdc46d61d850809e35773f3
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5401894
Reviewed-by: Steven Moreland <smoreland@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
When a filtered DT overlay is applied, cros_fdt applies the filtered
node(s) and its dependencies. Until now, the filtered node's
dependencies were:
- its parent nodes
- all nodes in the subtree rooted at the filtered node, ie its child
nodes
- all nodes referenced by their phandle value from any of the above
Including all child nodes as dependencies is incorrect. Simply
referencing a bus node from any node in the DT would include all of
the devices on a bus. For VM device assignment, it would populate
the DT with devices that are not being assigned.
This change updates the filtering algorithm to stop treating child
nodes of a filtered node as dependecies.
Bug: 320481473
Test: cd cros_fdt && cargo test
Change-Id: I3c393d41a4c497c043917767d555880896c10eb5
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5217131
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Pierre-Clément Tosi <ptosi@google.com>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Calling Fdt::from_blob with a byte slice shorter than FDT header size
currently panics. This changes makes from_blob return an error instead.
It also ensures other sizes and offsets in the header make sense,
before continuing to parse other FDT structures.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I37ae3cc360fd3cb88ed670320ab5536ac1bd9691
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5116143
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Expose pvIOMMUs to guests through the device tree. Due to the topology
of 1-to-1 mapping between VFIO devices and pvIOMMUs set `#iommu-cells`
to 0 as DMA masters (VFIO devices) can be uniquely identified from their
assigned pvIOMMU.
TEST=tools/run_tests
BUG=b:306110430
Change-Id: Iddac86500901b51e61de3c9b060c3bc541c64866
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5009547
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
For large overlay files, sometimes it is not neccessary to apply the
entire overlay, and only a few nodes must be applied instead. For
example, a large overlay file may contain nodes for all devices that can
be assigned to a VM via VFIO. However, in most cases only a small subset
of devices will actually be assigned, so only a small subset of the FDT
overlay must be applied.
This change implements device tree overlay filtering. Ie, it allows the
API user to choose which overlay nodes will be applied to the base.
It is possible to pass a set of symbols (node labels) to `apply_overlay`
function, which act as a filter for the device tree nodes.
This requires defining node labels (symbols) in DT source and compiling
DT overlays (`-@` option in dtc), so that the node symbols are exported
in `__symbols__` node.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I2238cf08b85e1825f668f83d8edadd2446a28869
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855967
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This allows the test binary to run without opening any files from the
source directory, fixing the ChromeOS crosvm ebuild tests.
TEST=`FEATURES=test emerge-hatch crosvm`
Change-Id: I3ced2d69f2ffb43305e8e13fc9f8b6a548120b44
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5013626
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Add test for the device tree overlay mechanism.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I8e8d129344e1ad4ddf0d7af7b3f21c0c9d9ca038
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855966
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This change adds support for applying device tree overlays onto existing
Fdt instances. It ensures overlay phandles do not overlap with base
phandles.
The algorithm is roughly:
- Find the maximum phandle node property value used in the base FDT
(delta)
- Increment all phandle property values in the overlay by delta to avoid
phandle collisions
- For every property in the overlay where a phandle value pointing to a
node inside the overlay is used, increment the value by delta so the
references are once again correct
- For every property in the overlay where a phandle value points to a
node outside the overlay, update that location with phandle values
from base, so that the value points to a correct node in base FDT
- Update nodes and properties in base FDT with nodes and properties from
the overlay
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: Ibff3c6dc3a9a1201c10da9004b295140d47929cd
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855965
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
To make querying device tree node paths in device tree easier, implement
a `Path` struct. Node paths are used extensively in overlay "metadata"
which makes it possible to find phandle references stored as plain u32
property values.
Furthermore, node paths exported by an overlay will be used to resolve
device tree symbols (node labels) once overlay filtering is implemented.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I9bae0a5643c054731c9d3b4cd92bb69de2b807d4
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855962
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Implement additional property APIs on FdtNodes. Beside the generic
property getter method, implement special getter and setter for phandle
values with an offset parameter. These will be used for updating phandle
references stored inside larger property values. Also, each FDT property type
now implements a new `AsFdtPropval` trait for parsing a binary property
value into a supported type.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I2d9e2259e49f8a88e99d67b25672b1add7c0b6b5
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855961
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
The device tree spec says string properties should be printable, but the
kernel device tree API and libfdt do not verify that. For compatibility,
this change only requires the string properties do not contain zero bytes.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I67fa4fddaf7b90d161afdcca74a80e3ea46817f6
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4960117
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
This change adds tests for the updated Fdt and FdtNode API.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I3dfd03d98118bf48807754a914cd7f941e029d79
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855964
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
A prerequisite for applying external FDT overlays is being able to load
them from byte vectors. This change implements deserialization of Fdt
structure from a byte slice using `Fdt::from_blob` function.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: Ic2d5398b8b49bbe44f248aea70ef13f3e890a532
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855963
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Validate FDT node and property names according to the device tree
specification.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I51bd16f6b750b26909ff0795b008407a6bcde891
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4916799
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
To avoid allocating and merging many vectors, pass an io::Write instance
to `write_blob` functions and write directly to an output buffer.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I5a96d5b08eb224d81ffca7b85d6e3415bfedf257
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4960116
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
This change moves the header-related code to a separate struct. FDT
overlay support will also require FDT deserialization from byte
vectors, so it makes sense to move implementations into separate
structures and keep Fdt implementation tidy.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I835b557d12b913e13a665fba3657160b77798add
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4916798
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
To make updating nodes and node properties easier, this change
restructures the internals of the Fdt struct. The single binary blob
that made up the contents of the FDT binary is replaced with with a tree
structure of `FdtNodes`.
This change allows adding new nodes and properties nested inside an
existing node structure. With prevous implementation which kept FDT data
stored in its binary form, inserting nodes and properties would require
a lot of byte shifts and lookups. These changes will make applying FDT
overlays much easier.
The API is also simplified, writing properties after adding subnodes is
no longer an error, and the concept of "opening" and "closing" FDT nodes
is no longer used.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: I1ad90771c0a4a0bb29f0322773d55c82d4cd5575
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4916796
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
This is technically outside the spec - it says that strings are
"printable" without further elaboration, which traditionally matches the
ASCII characters in [0x20, 0x7f). However, ARCVM on ChromeOS uses a
string value that contains newline (0x0a) characters, which means that
the tightened range of allowed characters prevents the VM from booting.
Allow 0x0a in addition to the normal printable ASCII characters to fix
ARCVM boot. This should ideally be fixed on the ARCVM side, but allow it
for now to unblock continued work on the FDT code without reverting.
BUG=b:306454590
TEST=Boot ARCVM on hatch
Change-Id: Ibe97417191319dc2dbec5ce067feb2ed7d463e04
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4956218
Reviewed-by: Lepton Wu <lepton@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
In preparation for updating the internal structure of Fdt struct,
reorder (sort) the properties of FDT nodes in test functions. This does
not affect the function of the output device tree, only the order of
its node properties.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: Ieaeb23074a041d125c7e558a60fb14486ecea4b5
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4943011
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Modifying FDTs and applying overlays will require additional APIs to
read property values. Ideally, we want to avoid implementing a new
getter method for each supported property value type, and instead have
a single `get_prop` method, and the same for alreaddy existing setters.
Therefore, before implementing property value getters, this change
replaces the seven setter methods for each supported property value
type with a single `set_prop` method taking a type which implements
`ToFdtPropval` trait. Also, implement the trait for each supported type.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: Iac05b4b2064d98657e19058fa25ffc689789c8b3
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4916795
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Currently crosvm has no support for generating device tree nodes for
platform devices assigned via VFIO. That limits usage of platfor VFIO
devices in VMs.
Additional DT nodes can be added to the DT via the overlay mechanism.
This and the following patches add support for amending the VM device
tree by adding support for device tree overlays.
As the first step, rename the FdtWriter struct to Fdt. It makes sense
to use a more general name as the crate gains additional features.
Bug: b/296796644
Test: cd cros_fdt && cargo test
Change-Id: Id6b943f23f7d89151dc66565f694eb0f988265d2
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4855960
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Jakob Vukalović <jakobvukalovic@google.com>
Add an option to dump the crosvm-generated devicetree blob. This option
can be helpful in understanding what devices are reported to the guest
VM and help diagnose issues related to improper devicetree.
Bug: 249043819
Change-Id: Id18ddf0838520c84f648ed8584db2ef8235ed636
Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
(cherry picked from commit 7acecab0b1d4f0428b71823f0e2982d385213279)
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4346951
Commit-Queue: Steven Moreland <smoreland@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Rather than checking the maximum size inside FdtWriter, just return the
full Vec<u8> that has already been generated and let the caller do the
check. Additionally, finish() previously always padded the Vec up to the
provided max_size, so the caller could not determine how much fdt space
was actually used; after this change, the padding is removed, so the
caller can observe how much space was actually used.
BUG=b:268397895
TEST=cargo test -p cros_fdt
TEST=Boot Crostini on arm
TEST=Boot ARCVM on x86_64
Change-Id: Ifd7d30581c7afffb09373ce9c34e4d7f9fe0d2b5
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4277061
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Add an option to dump the crosvm-generated devicetree blob. This option
can be helpful in understanding what devices are reported to the guest
VM and help diagnose issues related to improper devicetree.
Bug: 249043819
Change-Id: Ic6cfe9c12cc01d7aaa66c193b027e1f66d8a02b3
Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4174623
Commit-Queue: Frederick Mayle <fmayle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
These should be written as ChromeOS and ChromiumOS (without the space)
to match the updated branding. The copyright headers were already
migrated to the new style (https://crrev.com/c/3894243), but there were
some more instances left over.
BUG=None
TEST=tools/cargo-doc
Change-Id: I8c76aea2eb33b2e370ab71ee9b5cc0a4cfd00585
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4129934
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This will let the hypervisor crate use types and functions from the new
cros_fdt crate (it could not depend on arch, since that would cause a
circular dependency).
No functional change, just code movement.
BUG=b:253416076
TEST=cargo build
TEST=cargo build --target=aarch64-unknown-linux-gnu
Change-Id: I62d906941867c45f1b77ff1db6923d915ce2123e
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3965088
Reviewed-by: Steven Moreland <smoreland@google.com>
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>