Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ bench = false # https://bheisler.github.io/criterion.rs/book/faq.html#cargo-benc
[profile.bench]
lto = true
codegen-units = 1

[build-dependencies]
bindgen = "0.69"
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
[![crates.io](https://img.shields.io/crates/v/linux-loader)](https://crates.io/crates/linux-loader)
[![docs.rs](https://img.shields.io/docsrs/linux-loader)](https://docs.rs/linux-loader/)

## Build Requirements

The build process uses [bindgen](https://rust-lang.github.io/rust-bindgen/) to generate Rust bindings from C headers at build time, which requires **libclang** to be installed.

**Installing libclang:**
- **Ubuntu/Debian**: `apt-get install libclang-dev`
- **macOS**: `xcode-select --install` (or `brew install llvm`)
- **Fedora/RHEL**: `dnf install clang-devel`

The `linux-loader` crate offers support for loading raw ELF (`vmlinux`) and
compressed big zImage (`bzImage`) format kernel images on `x86_64` and PE
(`Image`) kernel images on `aarch64` and `riscv64`. ELF support includes the
Expand Down Expand Up @@ -118,6 +127,16 @@ impl MyVMM {

Done!

## Generated Code

The x86_64 boot parameter structures (`bootparam`, `elf`, and `start_info` modules) are automatically generated from Linux kernel UAPI headers during build time. The C headers are vendored in the `third-party/linux-headers/` directory:

- `bootparam.h` - Linux boot protocol structures (from commit `48b1320a674e1ff5de2fad8606bee38f724594dc`)
- `elf.h` - ELF64 format structures (from commit `48b1320a674e1ff5de2fad8606bee38f724594dc`)
- `start_info.h` - PVH boot protocol structures (from commit `48b1320a674e1ff5de2fad8606bee38f724594dc`)

These headers define stable userspace ABIs and are automatically converted to Rust code during the build process using bindgen. The generated files are placed in the build output directory and are not checked into version control.

## Testing

See [`docs/TESTING.md`](docs/TESTING.md).
Expand Down
112 changes: 112 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) 2019 Intel Corporation. All rights reserved.
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-BSD-3-Clause file.
//
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause

use std::env;
use std::path::PathBuf;

fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

// Tell cargo to rerun build script if headers change
println!("cargo:rerun-if-changed=third-party/linux-headers/bootparam.h");
println!("cargo:rerun-if-changed=third-party/linux-headers/elf.h");
println!("cargo:rerun-if-changed=third-party/linux-headers/start_info.h");

// Generate bootparam.rs
let bootparam_bindings = bindgen::Builder::default()
.header("third-party/linux-headers/bootparam.h")
.derive_default(true)
.derive_partialeq(true)
.generate_comments(false)
.use_core()
.ctypes_prefix("::std::os::raw")
// Add copyright header
.raw_line("// Copyright (c) 2019 Intel Corporation. All rights reserved.")
.raw_line("// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.")
.raw_line("//")
.raw_line("// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.")
.raw_line("// Use of this source code is governed by a BSD-style license that can be")
.raw_line("// found in the LICENSE-BSD-3-Clause file.")
.raw_line("//")
.raw_line("// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause")
.raw_line("")
.raw_line("/*")
.raw_line(" * automatically generated by rust-bindgen")
.raw_line(" * From upstream linux arch/x86/include/uapi/asm/bootparam.h at commit:")
.raw_line(" * 48b1320a674e1ff5de2fad8606bee38f724594dc")
.raw_line(" */")
.raw_line("")
.generate()
.expect("Unable to generate bootparam bindings");

bootparam_bindings
.write_to_file(out_dir.join("bootparam.rs"))
.expect("Couldn't write bootparam.rs");

// Generate elf.rs
let elf_bindings = bindgen::Builder::default()
.header("third-party/linux-headers/elf.h")
.derive_default(true)
.derive_partialeq(true)
.generate_comments(false)
.use_core()
.ctypes_prefix("::std::os::raw")
// Add copyright header
.raw_line("// Copyright © 2020, Oracle and/or its affiliates.")
.raw_line("//")
.raw_line("// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.")
.raw_line("//")
.raw_line("// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.")
.raw_line("// Use of this source code is governed by a BSD-style license that can be")
.raw_line("// found in the LICENSE-BSD-3-Clause file.")
.raw_line("//")
.raw_line("// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause")
.raw_line("")
.raw_line("/*")
.raw_line(" * automatically generated by rust-bindgen")
.raw_line(" * From upstream linux include/uapi/linux/elf.h at commit:")
.raw_line(" * 48b1320a674e1ff5de2fad8606bee38f724594dc")
.raw_line(" */")
.raw_line("")
.generate()
.expect("Unable to generate elf bindings");

elf_bindings
.write_to_file(out_dir.join("elf.rs"))
.expect("Couldn't write elf.rs");

// Generate start_info.rs
let start_info_bindings = bindgen::Builder::default()
.header("third-party/linux-headers/start_info.h")
.derive_default(true)
.derive_partialeq(true)
.generate_comments(false)
.use_core()
.ctypes_prefix("::std::os::raw")
// Add copyright header
.raw_line("// Copyright © 2020, Oracle and/or its affiliates.")
.raw_line("//")
.raw_line("// Copyright (c) 2019 Intel Corporation. All rights reserved.")
.raw_line("// Copyright (c) 2016, Citrix Systems, Inc.")
.raw_line("//")
.raw_line("// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause AND MIT")
.raw_line("")
.raw_line("/*")
.raw_line(" * automatically generated by rust-bindgen")
.raw_line(" * From upstream linux include/xen/interface/hvm/start_info.h at commit:")
.raw_line(" * 48b1320a674e1ff5de2fad8606bee38f724594dc")
.raw_line(" */")
.raw_line("")
.generate()
.expect("Unable to generate start_info bindings");

start_info_bindings
.write_to_file(out_dir.join("start_info.rs"))
.expect("Couldn't write start_info.rs");
}
Loading