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