xref: /cloud-hypervisor/devices/src/lib.rs (revision 9af2968a7dc47b89bf07ea9dc5e735084efcfa3a)
1 // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style license that can be
6 // found in the LICENSE-BSD-3-Clause file.
7 
8 //! Emulates virtual and hardware devices.
9 
10 #[macro_use]
11 extern crate bitflags;
12 #[macro_use]
13 extern crate log;
14 
15 #[cfg(feature = "acpi")]
16 pub mod acpi;
17 #[cfg(target_arch = "aarch64")]
18 pub mod gic;
19 pub mod interrupt_controller;
20 #[cfg(target_arch = "x86_64")]
21 pub mod ioapic;
22 pub mod legacy;
23 
24 #[cfg(feature = "acpi")]
25 pub use self::acpi::{AcpiGedDevice, AcpiPmTimerDevice, AcpiShutdownDevice};
26 
27 bitflags! {
28     pub struct AcpiNotificationFlags: u8 {
29         const NO_DEVICES_CHANGED = 0;
30         const CPU_DEVICES_CHANGED = 0b1;
31         const MEMORY_DEVICES_CHANGED = 0b10;
32         const PCI_DEVICES_CHANGED = 0b100;
33         const POWER_BUTTON_CHANGED = 0b1000;
34     }
35 }
36 
37 #[allow(unused_macros)]
38 #[cfg(target_arch = "aarch64")]
39 macro_rules! generate_read_fn {
40     ($fn_name: ident, $data_type: ty, $byte_type: ty, $type_size: expr, $endian_type: ident) => {
41         #[allow(dead_code)]
42         pub fn $fn_name(input: &[$byte_type]) -> $data_type {
43             assert!($type_size == std::mem::size_of::<$data_type>());
44             let mut array = [0u8; $type_size];
45             for (byte, read) in array.iter_mut().zip(input.iter().cloned()) {
46                 *byte = read as u8;
47             }
48             <$data_type>::$endian_type(array)
49         }
50     };
51 }
52 
53 #[allow(unused_macros)]
54 #[cfg(target_arch = "aarch64")]
55 macro_rules! generate_write_fn {
56     ($fn_name: ident, $data_type: ty, $byte_type: ty, $endian_type: ident) => {
57         #[allow(dead_code)]
58         pub fn $fn_name(buf: &mut [$byte_type], n: $data_type) {
59             for (byte, read) in buf
60                 .iter_mut()
61                 .zip(<$data_type>::$endian_type(n).iter().cloned())
62             {
63                 *byte = read as $byte_type;
64             }
65         }
66     };
67 }
68 
69 #[cfg(target_arch = "aarch64")]
70 generate_read_fn!(read_le_u16, u16, u8, 2, from_le_bytes);
71 #[cfg(target_arch = "aarch64")]
72 generate_read_fn!(read_le_u32, u32, u8, 4, from_le_bytes);
73 #[cfg(target_arch = "aarch64")]
74 generate_read_fn!(read_le_u64, u64, u8, 8, from_le_bytes);
75 #[cfg(target_arch = "aarch64")]
76 generate_read_fn!(read_le_i32, i32, i8, 4, from_le_bytes);
77 
78 #[cfg(target_arch = "aarch64")]
79 generate_read_fn!(read_be_u16, u16, u8, 2, from_be_bytes);
80 #[cfg(target_arch = "aarch64")]
81 generate_read_fn!(read_be_u32, u32, u8, 4, from_be_bytes);
82 
83 #[cfg(target_arch = "aarch64")]
84 generate_write_fn!(write_le_u16, u16, u8, to_le_bytes);
85 #[cfg(target_arch = "aarch64")]
86 generate_write_fn!(write_le_u32, u32, u8, to_le_bytes);
87 #[cfg(target_arch = "aarch64")]
88 generate_write_fn!(write_le_u64, u64, u8, to_le_bytes);
89 #[cfg(target_arch = "aarch64")]
90 generate_write_fn!(write_le_i32, i32, i8, to_le_bytes);
91 
92 #[cfg(target_arch = "aarch64")]
93 generate_write_fn!(write_be_u16, u16, u8, to_be_bytes);
94 #[cfg(target_arch = "aarch64")]
95 generate_write_fn!(write_be_u32, u32, u8, to_be_bytes);
96