Commit graph

30 commits

Author SHA1 Message Date
Pierre-Clément Tosi
a5ed157de7 aarch64: fdt: Fix CPU compatible to "arm,armv8"
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>
2024-04-17 23:33:51 +00:00
Jakob Vukalovic
3b29c72913 cros_fdt: Preserve FDT node/prop insertion order
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>
2024-03-28 10:46:22 +00:00
Jakob Vukalovic
f72868a451 cros_fdt: Do not apply children of filtered nodes
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>
2024-01-22 10:37:57 +00:00
Jakob Vukalovic
59e5ba50ea cros_fdt: Improve Fdt size checks
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>
2023-12-14 21:21:47 +00:00
Pierre-Clément Tosi
1abdd9af88 fdt: Add support for pKVM-VFIO pvIOMMUs
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>
2023-11-15 10:50:46 +00:00
Jakob Vukalovic
12dead57f2 cros_fdt: Implement overlay filtering
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>
2023-11-10 09:38:05 +00:00
Daniel Verkamp
64d1c50ab3 cros_fdt: embed test data with include_bytes!()
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>
2023-11-08 18:44:54 +00:00
Jakob Vukalovic
8b21047281 cros_fdt: Add device tree overlay tests
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>
2023-11-07 18:15:21 +00:00
Jakob Vukalovic
6daa3e6f03 cros_fdt: Add device tree overlay support
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>
2023-11-07 18:07:24 +00:00
Jakob Vukalovic
c963fbc5a4 cros_fdt: Implement node paths API
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>
2023-11-02 16:44:15 +00:00
Jakob Vukalovic
6ba5fab07a cros_fdt: Implement FdtNode properties API
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>
2023-11-02 16:44:15 +00:00
Jakob Vukalovic
3828313a65 cros_fdt: Relax rules for string properties
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>
2023-10-31 09:48:55 +00:00
Jakob Vukalovic
7e0f768f6b cros_fdt: Add Fdt and FdtNode tests
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>
2023-10-31 09:46:39 +00:00
Jakob Vukalovic
a7dc630170 cros_fdt: Load FDTs from binary blobs
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>
2023-10-31 09:38:33 +00:00
Jakob Vukalovic
36dc6db76e cros_fdt: Validate node and property names
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>
2023-10-31 09:34:07 +00:00
Jakob Vukalovic
3f8e5003d6 cros_fdt: Write FDT to io::Write instance
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>
2023-10-31 09:29:23 +00:00
Jakob Vukalovic
e1cd60be91 cros_fdt: Introduce FdtHeader
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>
2023-10-25 18:02:26 +00:00
Jakob Vukalovic
f16d67a2ff cros_fdt: Restructure Fdt
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>
2023-10-25 17:56:10 +00:00
Daniel Verkamp
afe25df1be cros_fdt: allow newline in string values
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>
2023-10-19 23:08:27 +00:00
Jakob Vukalovic
bd4bea767c cros_fdt: Reorder properties of test nodes
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>
2023-10-18 10:28:32 +00:00
Jakob Vukalovic
73534c684f cros_fdt: Introduce ToFdtPropval trait
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>
2023-10-18 10:24:15 +00:00
Jakob Vukalovic
6a7b20d48b cros_fdt: Rename FdtWriter to Fdt
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>
2023-10-16 11:04:06 +00:00
Daniel Verkamp
ce5a78e83b clippy fixes for Rust 1.70 and 1.71
Change-Id: If86c6cd531b854293a93208de5254664f5ee6bec
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4637612
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2023-07-13 20:29:05 +00:00
Elliot Berman
c8a638fae1 crosvm: fdt: Add option to dump generated devicetree blob
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>
2023-03-16 18:16:28 +00:00
Steven Moreland
e8bebb22e1 Revert "crosvm: fdt: Add option to dump generated devicetree blob"
This reverts commit 77f6d72395.

Change-Id: I95aef3fd289af0ec748e85faa84151582d43a963
(cherry picked from commit 46e133a9bf823cafacb9511eba5c9c4ae8630185)
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4346932
Reviewed-by: Dennis Kempin <denniskempin@google.com>
2023-03-16 18:16:28 +00:00
Daniel Verkamp
4ba59643b9 cros_fdt: remove max_size from FdtWriter::finish()
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>
2023-02-28 18:36:13 +00:00
Elliot Berman
77f6d72395 crosvm: fdt: Add option to dump generated devicetree blob
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>
2023-01-20 20:26:55 +00:00
Daniel Verkamp
ac0fc378a3 Fix remaining Chrome/Chromium OS instances
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>
2023-01-03 22:14:30 +00:00
Daniel Verkamp
dfe9869d6c Rust 1.65: Fix clippy borrow_deref_ref, needless_borrow, and explicit_auto_deref lints
BUG=b:260784028
TEST=tools/clippy

Change-Id: I85cd5e096bc7b977fe1d9b231c08338b65e71780
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4064714
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-11-30 19:33:31 +00:00
Daniel Verkamp
474d2eea17 arch: split fdt code into its own crate
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>
2022-10-19 17:43:19 +00:00