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