xref: /cloud-hypervisor/devices/src/lib.rs (revision 6f8bd27cf7629733582d930519e98d19e90afb16)
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 pub mod acpi;
16 #[cfg(target_arch = "aarch64")]
17 pub mod gic;
18 pub mod interrupt_controller;
19 #[cfg(target_arch = "x86_64")]
20 pub mod ioapic;
21 pub mod legacy;
22 pub mod tpm;
23 
24 pub use self::acpi::{AcpiGedDevice, AcpiPmTimerDevice, AcpiShutdownDevice};
25 
26 bitflags! {
27     pub struct AcpiNotificationFlags: u8 {
28         const NO_DEVICES_CHANGED = 0;
29         const CPU_DEVICES_CHANGED = 0b1;
30         const MEMORY_DEVICES_CHANGED = 0b10;
31         const PCI_DEVICES_CHANGED = 0b100;
32         const POWER_BUTTON_CHANGED = 0b1000;
33     }
34 }
35 
36 #[allow(unused_macros)]
37 #[cfg(target_arch = "aarch64")]
38 macro_rules! generate_read_fn {
39     ($fn_name: ident, $data_type: ty, $byte_type: ty, $type_size: expr, $endian_type: ident) => {
40         #[allow(dead_code)]
41         pub fn $fn_name(input: &[$byte_type]) -> $data_type {
42             assert!($type_size == std::mem::size_of::<$data_type>());
43             let mut array = [0u8; $type_size];
44             for (byte, read) in array.iter_mut().zip(input.iter().cloned()) {
45                 *byte = read as u8;
46             }
47             <$data_type>::$endian_type(array)
48         }
49     };
50 }
51 
52 #[allow(unused_macros)]
53 #[cfg(target_arch = "aarch64")]
54 macro_rules! generate_write_fn {
55     ($fn_name: ident, $data_type: ty, $byte_type: ty, $endian_type: ident) => {
56         #[allow(dead_code)]
57         pub fn $fn_name(buf: &mut [$byte_type], n: $data_type) {
58             for (byte, read) in buf
59                 .iter_mut()
60                 .zip(<$data_type>::$endian_type(n).iter().cloned())
61             {
62                 *byte = read as $byte_type;
63             }
64         }
65     };
66 }
67 
68 #[cfg(target_arch = "aarch64")]
69 generate_read_fn!(read_le_u16, u16, u8, 2, from_le_bytes);
70 #[cfg(target_arch = "aarch64")]
71 generate_read_fn!(read_le_u32, u32, u8, 4, from_le_bytes);
72 #[cfg(target_arch = "aarch64")]
73 generate_read_fn!(read_le_u64, u64, u8, 8, from_le_bytes);
74 #[cfg(target_arch = "aarch64")]
75 generate_read_fn!(read_le_i32, i32, i8, 4, from_le_bytes);
76 
77 #[cfg(target_arch = "aarch64")]
78 generate_read_fn!(read_be_u16, u16, u8, 2, from_be_bytes);
79 #[cfg(target_arch = "aarch64")]
80 generate_read_fn!(read_be_u32, u32, u8, 4, from_be_bytes);
81 
82 #[cfg(target_arch = "aarch64")]
83 generate_write_fn!(write_le_u16, u16, u8, to_le_bytes);
84 #[cfg(target_arch = "aarch64")]
85 generate_write_fn!(write_le_u32, u32, u8, to_le_bytes);
86 #[cfg(target_arch = "aarch64")]
87 generate_write_fn!(write_le_u64, u64, u8, to_le_bytes);
88 #[cfg(target_arch = "aarch64")]
89 generate_write_fn!(write_le_i32, i32, i8, to_le_bytes);
90 
91 #[cfg(target_arch = "aarch64")]
92 generate_write_fn!(write_be_u16, u16, u8, to_be_bytes);
93 #[cfg(target_arch = "aarch64")]
94 generate_write_fn!(write_be_u32, u32, u8, to_be_bytes);
95