xref: /cloud-hypervisor/devices/src/lib.rs (revision 2571e59438597f53aa4993cd70d6462fe1364ba7)
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 event_monitor;
14 #[macro_use]
15 extern crate log;
16 
17 pub mod acpi;
18 #[cfg(target_arch = "aarch64")]
19 pub mod gic;
20 pub mod interrupt_controller;
21 #[cfg(target_arch = "x86_64")]
22 pub mod ioapic;
23 pub mod legacy;
24 pub mod pvpanic;
25 pub mod tpm;
26 
27 pub use self::acpi::{AcpiGedDevice, AcpiPmTimerDevice, AcpiShutdownDevice};
28 pub use self::pvpanic::{PvPanicDevice, PVPANIC_DEVICE_MMIO_SIZE};
29 
30 bitflags! {
31     pub struct AcpiNotificationFlags: u8 {
32         const NO_DEVICES_CHANGED = 0;
33         const CPU_DEVICES_CHANGED = 0b1;
34         const MEMORY_DEVICES_CHANGED = 0b10;
35         const PCI_DEVICES_CHANGED = 0b100;
36         const POWER_BUTTON_CHANGED = 0b1000;
37     }
38 }
39 
40 #[cfg(target_arch = "aarch64")]
41 macro_rules! generate_read_fn {
42     ($fn_name: ident, $data_type: ty, $byte_type: ty, $type_size: expr, $endian_type: ident) => {
43         pub fn $fn_name(input: &[$byte_type]) -> $data_type {
44             assert!($type_size == std::mem::size_of::<$data_type>());
45             let mut array = [0u8; $type_size];
46             for (byte, read) in array.iter_mut().zip(input.iter().cloned()) {
47                 *byte = read as u8;
48             }
49             <$data_type>::$endian_type(array)
50         }
51     };
52 }
53 
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         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