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