xref: /cloud-hypervisor/devices/src/lib.rs (revision 5e52729453cb62edbe4fb3a4aa24f8cca31e667e)
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 #[cfg(target_arch = "aarch64")]
37 macro_rules! generate_read_fn {
38     ($fn_name: ident, $data_type: ty, $byte_type: ty, $type_size: expr, $endian_type: ident) => {
39         pub fn $fn_name(input: &[$byte_type]) -> $data_type {
40             assert!($type_size == std::mem::size_of::<$data_type>());
41             let mut array = [0u8; $type_size];
42             for (byte, read) in array.iter_mut().zip(input.iter().cloned()) {
43                 *byte = read as u8;
44             }
45             <$data_type>::$endian_type(array)
46         }
47     };
48 }
49 
50 #[cfg(target_arch = "aarch64")]
51 macro_rules! generate_write_fn {
52     ($fn_name: ident, $data_type: ty, $byte_type: ty, $endian_type: ident) => {
53         pub fn $fn_name(buf: &mut [$byte_type], n: $data_type) {
54             for (byte, read) in buf
55                 .iter_mut()
56                 .zip(<$data_type>::$endian_type(n).iter().cloned())
57             {
58                 *byte = read as $byte_type;
59             }
60         }
61     };
62 }
63 
64 #[cfg(target_arch = "aarch64")]
65 generate_read_fn!(read_le_u16, u16, u8, 2, from_le_bytes);
66 #[cfg(target_arch = "aarch64")]
67 generate_read_fn!(read_le_u32, u32, u8, 4, from_le_bytes);
68 #[cfg(target_arch = "aarch64")]
69 generate_read_fn!(read_le_u64, u64, u8, 8, from_le_bytes);
70 #[cfg(target_arch = "aarch64")]
71 generate_read_fn!(read_le_i32, i32, i8, 4, from_le_bytes);
72 
73 #[cfg(target_arch = "aarch64")]
74 generate_read_fn!(read_be_u16, u16, u8, 2, from_be_bytes);
75 #[cfg(target_arch = "aarch64")]
76 generate_read_fn!(read_be_u32, u32, u8, 4, from_be_bytes);
77 
78 #[cfg(target_arch = "aarch64")]
79 generate_write_fn!(write_le_u16, u16, u8, to_le_bytes);
80 #[cfg(target_arch = "aarch64")]
81 generate_write_fn!(write_le_u32, u32, u8, to_le_bytes);
82 #[cfg(target_arch = "aarch64")]
83 generate_write_fn!(write_le_u64, u64, u8, to_le_bytes);
84 #[cfg(target_arch = "aarch64")]
85 generate_write_fn!(write_le_i32, i32, i8, to_le_bytes);
86 
87 #[cfg(target_arch = "aarch64")]
88 generate_write_fn!(write_be_u16, u16, u8, to_be_bytes);
89 #[cfg(target_arch = "aarch64")]
90 generate_write_fn!(write_be_u32, u32, u8, to_be_bytes);
91