Skip to content

Commit

Permalink
rust: add PL011 device model
Browse files Browse the repository at this point in the history
This commit adds a re-implementation of hw/char/pl011.c in Rust.

How to build:

1. Configure a QEMU build with:
   --enable-system --target-list=aarch64-softmmu --enable-rust
2. Launching a VM with qemu-system-aarch64 should use the Rust version
   of the pl011 device

Co-authored-by: Junjie Mao <[email protected]>
Co-authored-by: Paolo Bonzini <[email protected]>
Signed-off-by: Junjie Mao <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
Signed-off-by: Manos Pitsidianakis <[email protected]>
Link: https://lore.kernel.org/r/6ec1d4fb8db2a1d7ba94c73e65d9770371b7857d.1727961605.git.manos.pitsidianakis@linaro.org
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini and junjiemao1 committed Oct 11, 2024
1 parent 2b74dd9 commit d0f0cd5
Show file tree
Hide file tree
Showing 37 changed files with 1,906 additions and 12 deletions.
5 changes: 5 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,11 @@ F: include/hw/*/microbit*.h
F: tests/qtest/microbit-test.c
F: docs/system/arm/nrf.rst

ARM PL011 Rust device
M: Manos Pitsidianakis <[email protected]>
S: Maintained
F: rust/hw/char/pl011/

AVR Machines
-------------

Expand Down
30 changes: 20 additions & 10 deletions hw/arm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ config ARM_VIRT
select PCI_EXPRESS
select PCI_EXPRESS_GENERIC_BRIDGE
select PFLASH_CFI01
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL031 # RTC
select PL061 # GPIO
select GPIO_PWR
Expand Down Expand Up @@ -73,7 +74,8 @@ config HIGHBANK
select AHCI
select ARM_TIMER # sp804
select ARM_V7M
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL022 # SPI
select PL031 # RTC
select PL061 # GPIO
Expand All @@ -86,7 +88,8 @@ config INTEGRATOR
depends on TCG && ARM
select ARM_TIMER
select INTEGRATOR_DEBUG
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL031 # RTC
select PL041 # audio
select PL050 # keyboard/mouse
Expand All @@ -104,7 +107,8 @@ config MUSCA
default y
depends on TCG && ARM
select ARMSSE
select PL011
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL031
select SPLIT_IRQ
select UNIMP
Expand Down Expand Up @@ -169,7 +173,8 @@ config REALVIEW
select WM8750 # audio codec
select LSI_SCSI_PCI
select PCI
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL031 # RTC
select PL041 # audio codec
select PL050 # keyboard/mouse
Expand All @@ -194,7 +199,8 @@ config SBSA_REF
select PCI_EXPRESS
select PCI_EXPRESS_GENERIC_BRIDGE
select PFLASH_CFI01
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL031 # RTC
select PL061 # GPIO
select USB_XHCI_SYSBUS
Expand All @@ -218,7 +224,8 @@ config STELLARIS
select ARM_V7M
select CMSDK_APB_WATCHDOG
select I2C
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL022 # SPI
select PL061 # GPIO
select SSD0303 # OLED display
Expand Down Expand Up @@ -278,7 +285,8 @@ config VEXPRESS
select ARM_TIMER # sp804
select LAN9118
select PFLASH_CFI01
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select PL041 # audio codec
select PL181 # display
select REALVIEW
Expand Down Expand Up @@ -362,7 +370,8 @@ config RASPI
default y
depends on TCG && ARM
select FRAMEBUFFER
select PL011 # UART
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select SDHCI
select USB_DWC2
select BCM2835_SPI
Expand Down Expand Up @@ -437,7 +446,8 @@ config XLNX_VERSAL
select ARM_GIC
select CPU_CLUSTER
select DEVICE_TREE
select PL011
select PL011 if !HAVE_RUST # UART
select X_PL011_RUST if HAVE_RUST # UART
select CADENCE
select VIRTIO_MMIO
select UNIMP
Expand Down
24 changes: 24 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3512,6 +3512,7 @@ qom_ss = ss.source_set()
system_ss = ss.source_set()
specific_fuzz_ss = ss.source_set()
specific_ss = ss.source_set()
rust_devices_ss = ss.source_set()
stub_ss = ss.source_set()
trace_ss = ss.source_set()
user_ss = ss.source_set()
Expand Down Expand Up @@ -4059,6 +4060,29 @@ foreach target : target_dirs
arch_srcs += target_specific.sources()
arch_deps += target_specific.dependencies()

if have_rust and have_system
target_rust = rust_devices_ss.apply(config_target, strict: false)
crates = []
foreach dep : target_rust.dependencies()
crates += dep.get_variable('crate')
endforeach
if crates.length() > 0
rlib_rs = custom_target('rust_' + target.underscorify() + '.rs',
output: 'rust_' + target.underscorify() + '.rs',
command: [find_program('scripts/rust/rust_root_crate.sh')] + crates,
capture: true,
build_by_default: true,
build_always_stale: true)
rlib = static_library('rust_' + target.underscorify(),
rlib_rs,
dependencies: target_rust.dependencies(),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
rust_args: rustc_args,
rust_abi: 'c')
arch_deps += declare_dependency(link_whole: [rlib])
endif
endif

# allow using headers from the dependencies but do not include the sources,
# because this emulator only needs those in "objects". For external
# dependencies, the full dependency is included below in the executable.
Expand Down
1 change: 1 addition & 0 deletions rust/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source hw/Kconfig
2 changes: 2 additions & 0 deletions rust/hw/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# devices Kconfig
source char/Kconfig
3 changes: 3 additions & 0 deletions rust/hw/char/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
config X_PL011_RUST
bool
default y if HAVE_RUST
1 change: 1 addition & 0 deletions rust/hw/char/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir('pl011')
2 changes: 2 additions & 0 deletions rust/hw/char/pl011/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore generated bindings file overrides.
src/bindings.rs.inc
134 changes: 134 additions & 0 deletions rust/hw/char/pl011/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions rust/hw/char/pl011/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "pl011"
version = "0.1.0"
edition = "2021"
authors = ["Manos Pitsidianakis <[email protected]>"]
license = "GPL-2.0-or-later"
readme = "README.md"
homepage = "https://www.qemu.org"
description = "pl011 device model for QEMU"
repository = "https://gitlab.com/epilys/rust-for-qemu"
resolver = "2"
publish = false
keywords = []
categories = []

[lib]
crate-type = ["staticlib"]

[dependencies]
bilge = { version = "0.2.0" }
bilge-impl = { version = "0.2.0" }
qemu_api = { path = "../../../qemu-api" }
qemu_api_macros = { path = "../../../qemu-api-macros" }

# Do not include in any global workspace
[workspace]
31 changes: 31 additions & 0 deletions rust/hw/char/pl011/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# PL011 QEMU Device Model

This library implements a device model for the PrimeCell® UART (PL011)
device in QEMU.

## Build static lib

Host build target must be explicitly specified:

```sh
cargo build --target x86_64-unknown-linux-gnu
```

Replace host target triplet if necessary.

## Generate Rust documentation

To generate docs for this crate, including private items:

```sh
cargo doc --no-deps --document-private-items --target x86_64-unknown-linux-gnu
```

To include direct dependencies like `bilge` (bitmaps for register types):

```sh
cargo tree --depth 1 -e normal --prefix none \
| cut -d' ' -f1 \
| xargs printf -- '-p %s\n' \
| xargs cargo doc --no-deps --document-private-items --target x86_64-unknown-linux-gnu
```
26 changes: 26 additions & 0 deletions rust/hw/char/pl011/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
subproject('bilge-0.2-rs', required: true)
subproject('bilge-impl-0.2-rs', required: true)

bilge_dep = dependency('bilge-0.2-rs')
bilge_impl_dep = dependency('bilge-impl-0.2-rs')

_libpl011_rs = static_library(
'pl011',
files('src/lib.rs'),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
rust_abi: 'rust',
dependencies: [
bilge_dep,
bilge_impl_dep,
qemu_api,
qemu_api_macros,
],
)

rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency(
link_whole: [_libpl011_rs],
# Putting proc macro crates in `dependencies` is necessary for Meson to find
# them when compiling the root per-target static rust lib.
dependencies: [bilge_impl_dep, qemu_api_macros],
variables: {'crate': 'pl011'},
)])
Loading

0 comments on commit d0f0cd5

Please sign in to comment.