xref: /aosp_15_r20/external/crosvm/kvm_sys/bindgen.sh (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1#!/usr/bin/env bash
2# Copyright 2022 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Regenerate kvm_sys bindgen bindings.
7
8set -euo pipefail
9cd "$(dirname "${BASH_SOURCE[0]}")/.."
10
11source tools/impl/bindgen-common.sh
12
13KVM_EXTRAS="// Added by kvm_sys/bindgen.sh
14use zerocopy::AsBytes;
15use zerocopy::FromBytes;
16use zerocopy::FromZeroes;
17
18// TODO(b/316337317): Update if new memslot flag is accepted in upstream
19pub const KVM_MEM_NON_COHERENT_DMA: u32 = 8;
20pub const KVM_CAP_USER_CONFIGURE_NONCOHERENT_DMA: u32 = 236;
21
22// TODO(qwandor): Update this once the pKVM patches are merged upstream with a stable capability ID.
23pub const KVM_CAP_ARM_PROTECTED_VM: u32 = 0xffbadab1;
24pub const KVM_CAP_ARM_PROTECTED_VM_FLAGS_SET_FW_IPA: u32 = 0;
25pub const KVM_CAP_ARM_PROTECTED_VM_FLAGS_INFO: u32 = 1;
26pub const KVM_VM_TYPE_ARM_PROTECTED: u32 = 0x80000000;
27pub const KVM_X86_PKVM_PROTECTED_VM: u32 = 28;
28pub const KVM_DEV_VFIO_PVIOMMU: u32 = 2;
29pub const KVM_DEV_VFIO_PVIOMMU_ATTACH: u32 = 1;
30#[repr(C)]
31#[derive(Debug, Default, Copy, Clone)]
32pub struct kvm_vfio_iommu_info {
33    pub device_fd: i32,
34    pub nr_sids: u32,
35}
36pub const KVM_DEV_VFIO_PVIOMMU_GET_INFO: u32 = 2;
37#[repr(C)]
38#[derive(Debug, Default, Copy, Clone)]
39pub struct kvm_vfio_iommu_config {
40    pub device_fd: i32,
41    pub sid_idx: u32,
42    pub vsid: u32,
43}"
44
45X86_64_EXTRAS="
46// This is how zerocopy's author deal with bindings for __BindgenBitfieldUnit<Storage>, see:
47// https://fuchsia-review.googlesource.com/c/859278/8/src/starnix/lib/linux_uapi/generate.py
48unsafe impl<Storage> AsBytes for __BindgenBitfieldUnit<Storage>
49where
50    Storage: AsBytes,
51{
52    fn only_derive_is_allowed_to_implement_this_trait() {}
53}
54
55unsafe impl<Storage> FromBytes for __BindgenBitfieldUnit<Storage>
56where
57    Storage: FromBytes,
58{
59    fn only_derive_is_allowed_to_implement_this_trait() {}
60}
61
62unsafe impl<Storage> FromZeroes for __BindgenBitfieldUnit<Storage>
63where
64    Storage: FromZeroes,
65{
66    fn only_derive_is_allowed_to_implement_this_trait() {}
67}"
68
69bindgen_generate \
70    --raw-line "${KVM_EXTRAS}" \
71    --raw-line "${X86_64_EXTRAS}" \
72    --blocklist-item='__kernel.*' \
73    --blocklist-item='__BITS_PER_LONG' \
74    --blocklist-item='__FD_SETSIZE' \
75    --blocklist-item='_?IOC.*' \
76    --with-derive-custom "kvm_regs=FromZeroes,FromBytes,AsBytes" \
77    --with-derive-custom "kvm_sregs=FromZeroes,FromBytes,AsBytes" \
78    --with-derive-custom "kvm_fpu=FromZeroes,FromBytes,AsBytes" \
79    --with-derive-custom "kvm_debugregs=FromZeroes,FromBytes,AsBytes" \
80    --with-derive-custom "kvm_xcr=FromZeroes,FromBytes,AsBytes" \
81    --with-derive-custom "kvm_xcrs=FromZeroes,FromBytes,AsBytes" \
82    --with-derive-custom "kvm_lapic_state=FromZeroes,FromBytes,AsBytes" \
83    --with-derive-custom "kvm_mp_state=FromZeroes,FromBytes,AsBytes" \
84    --with-derive-custom "kvm_vcpu_events=FromZeroes,FromBytes,AsBytes" \
85    --with-derive-custom "kvm_vcpu_events__bindgen_ty_1=FromZeroes,FromBytes,AsBytes" \
86    --with-derive-custom "kvm_vcpu_events__bindgen_ty_2=FromZeroes,FromBytes,AsBytes" \
87    --with-derive-custom "kvm_vcpu_events__bindgen_ty_3=FromZeroes,FromBytes,AsBytes" \
88    --with-derive-custom "kvm_vcpu_events__bindgen_ty_4=FromZeroes,FromBytes,AsBytes" \
89    --with-derive-custom "kvm_vcpu_events__bindgen_ty_5=FromZeroes,FromBytes,AsBytes" \
90    --with-derive-custom "kvm_dtable=FromZeroes,FromBytes,AsBytes" \
91    --with-derive-custom "kvm_segment=FromZeroes,FromBytes,AsBytes" \
92    --with-derive-custom "kvm_pic_state=FromZeroes,FromBytes,AsBytes" \
93    --with-derive-custom "kvm_ioapic_state=FromZeroes,FromBytes,AsBytes" \
94    --with-derive-custom "kvm_pit_state2=FromZeroes,FromBytes,AsBytes" \
95    --with-derive-custom "kvm_clock_data=FromZeroes,FromBytes,AsBytes" \
96    --with-derive-custom "kvm_ioapic_state__bindgen_ty_1=FromZeroes,FromBytes,AsBytes" \
97    --with-derive-custom "kvm_ioapic_state__bindgen_ty_1__bindgen_ty_1=FromZeroes,FromBytes,AsBytes" \
98    --with-derive-custom "kvm_pit_channel_state=FromZeroes,FromBytes,AsBytes" \
99    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/kvm.h" \
100    -- \
101    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
102    | replace_linux_int_types \
103    > kvm_sys/src/x86/bindings.rs
104
105bindgen_generate \
106    --raw-line "${KVM_EXTRAS}" \
107    --blocklist-item='__kernel.*' \
108    --blocklist-item='__BITS_PER_LONG' \
109    --blocklist-item='__FD_SETSIZE' \
110    --blocklist-item='_?IOC.*' \
111    --with-derive-custom "kvm_regs=FromZeroes,FromBytes,AsBytes" \
112    --with-derive-custom "kvm_sregs=FromZeroes,FromBytes,AsBytes" \
113    --with-derive-custom "kvm_fpu=FromZeroes,FromBytes,AsBytes" \
114    --with-derive-custom "kvm_vcpu_events=FromZeroes,FromBytes,AsBytes" \
115    --with-derive-custom "kvm_vcpu_events__bindgen_ty_1=FromZeroes,FromBytes,AsBytes" \
116    --with-derive-custom "kvm_mp_state=FromZeroes,FromBytes,AsBytes" \
117    --with-derive-custom "user_fpsimd_state=FromZeroes,FromBytes,AsBytes" \
118    --with-derive-custom "user_pt_regs=FromZeroes,FromBytes,AsBytes" \
119    "${BINDGEN_LINUX_ARM64_HEADERS}/include/linux/kvm.h" \
120    -- \
121    -isystem "${BINDGEN_LINUX_ARM64_HEADERS}/include" \
122    | replace_linux_int_types \
123    > kvm_sys/src/aarch64/bindings.rs
124
125bindgen_generate \
126    --raw-line "${KVM_EXTRAS}" \
127    --blocklist-item='__kernel.*' \
128    --blocklist-item='__BITS_PER_LONG' \
129    --blocklist-item='__FD_SETSIZE' \
130    --blocklist-item='_?IOC.*' \
131    "${BINDGEN_LINUX_RISCV_HEADERS}/include/linux/kvm.h" \
132    -- \
133    -isystem "${BINDGEN_LINUX_RISCV_HEADERS}/include" \
134    | replace_linux_int_types \
135    > kvm_sys/src/riscv64/bindings.rs
136