Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mshv-bindings: Refactor codebase to add support for ARM64 #140

Merged
merged 3 commits into from
Apr 18, 2024
Merged
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
21,116 changes: 21,116 additions & 0 deletions mshv-bindings/src/arm64/bindings.rs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions mshv-bindings/src/arm64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright © 2024, Microsoft Corporation
//
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
//

#[allow(
clippy::too_many_arguments,
clippy::missing_safety_doc,
clippy::useless_transmute,
clippy::unnecessary_cast,
clippy::non_canonical_clone_impl,
non_camel_case_types,
non_snake_case,
non_upper_case_globals
)]
pub mod bindings;
#[allow(ambiguous_glob_reexports, unused_imports)]
pub use bindings::*;
40 changes: 9 additions & 31 deletions mshv-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,20 @@
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
//

#[cfg(target_arch = "x86_64")]
#[macro_use]
#[cfg(feature = "fam-wrappers")]
extern crate vmm_sys_util;

#[allow(
clippy::too_many_arguments,
clippy::missing_safety_doc,
clippy::useless_transmute,
clippy::unnecessary_cast,
clippy::non_canonical_clone_impl,
non_camel_case_types,
non_snake_case,
non_upper_case_globals
)]
pub mod bindings;
#[allow(ambiguous_glob_reexports, unused_imports)]
pub use bindings::*;
pub mod regs;
pub use regs::*;
pub mod snp;
pub use snp::*;
#[cfg(target_arch = "x86_64")]
mod x86_64;
#[cfg(target_arch = "x86_64")]
pub use self::x86_64::*;

#[cfg(feature = "with-serde")]
extern crate serde;

#[cfg(feature = "with-serde")]
extern crate serde_derive;
pub mod hvdef;
pub use hvdef::*;
mod unmarshal;

#[cfg(feature = "fam-wrappers")]
mod fam_wrappers;

#[cfg(feature = "fam-wrappers")]
pub use fam_wrappers::*;

#[cfg(feature = "with-serde")]
mod serializers;
#[cfg(target_arch = "aarch64")]
mod arm64;
#[cfg(target_arch = "aarch64")]
pub use self::arm64::*;
File renamed without changes.
38 changes: 38 additions & 0 deletions mshv-bindings/src/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright © 2024, Microsoft Corporation
//
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
//

#[allow(
clippy::too_many_arguments,
clippy::missing_safety_doc,
clippy::useless_transmute,
clippy::unnecessary_cast,
clippy::non_canonical_clone_impl,
non_camel_case_types,
non_snake_case,
non_upper_case_globals
)]
pub mod bindings;
#[allow(ambiguous_glob_reexports, unused_imports)]
pub use bindings::*;
pub mod regs;
pub use regs::*;
pub mod snp;
pub use snp::*;

#[cfg(feature = "with-serde")]
extern crate serde;

#[cfg(feature = "with-serde")]
extern crate serde_derive;
mod unmarshal;

#[cfg(feature = "fam-wrappers")]
mod fam_wrappers;

#[cfg(feature = "fam-wrappers")]
pub use fam_wrappers::*;

#[cfg(feature = "with-serde")]
mod serializers;
File renamed without changes.
File renamed without changes.
25 changes: 21 additions & 4 deletions scripts/generate_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def check_installed(cmd):
return which(cmd) is not None


def install_kernel_headers(kernel_src_path):
def install_kernel_headers(kernel_src_path, arch):
kernel_hdr_path = tempfile.mkdtemp(prefix="linux")
logging.debug(f"Installing kernel headers at {kernel_hdr_path}")
subprocess.run(
[
"make",
"headers_install",
"ARCH=x86",
f"ARCH={arch}",
f"INSTALL_HDR_PATH={kernel_hdr_path}",
"-C",
kernel_src_path,
Expand Down Expand Up @@ -94,6 +94,13 @@ def update_bindings_comment(bindings_file):
f.write("".join(lines))


def get_arch_location(arch):
if arch == "x86":
return "x86_64"

return arch


def main(args):
bindgen = check_installed("bindgen")
if not bindgen:
Expand All @@ -105,13 +112,14 @@ def main(args):
logging.error("Please install make.")
return -1

kernel_hdr_path = install_kernel_headers(args.kernel_src_path)
kernel_hdr_path = install_kernel_headers(args.kernel_src_path, args.arch)
generate_unified_mshv_headers(kernel_hdr_path)

bindgen_args = "--no-doc-comments --with-derive-default "

bindgen_args += args.bindgen_args
output_file = f"{args.output}/bindings.rs"
arch_location = get_arch_location(args.arch)
output_file = f"{args.output}/{arch_location}/bindings.rs"

run_bindgen(kernel_hdr_path, output_file, bindgen_args)
update_bindings_comment(output_file)
Expand Down Expand Up @@ -160,6 +168,15 @@ def main(args):
help="Log level for logging (default: %(default)s)",
)

parser.add_argument(
"--arch",
"-a",
dest="arch",
choices=["x86", "arm64"],
jinankjain marked this conversation as resolved.
Show resolved Hide resolved
default="x86",
help="Architecture for binding generation (default: %(default)s)",
)

log_level = {"info": logging.INFO, "debug": logging.DEBUG, "error": logging.ERROR}

args = parser.parse_args()
Expand Down