Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/base/src/lib.rs
5394 views
1
// Copyright 2020 The ChromiumOS Authors
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
//! Safe, cross-platform-compatible wrappers for system interfaces.
6
7
mod alloc;
8
mod clock;
9
pub mod custom_serde;
10
pub mod descriptor;
11
pub mod descriptor_reflection;
12
mod errno;
13
mod event;
14
mod file_traits;
15
mod iobuf;
16
mod mmap;
17
mod notifiers;
18
mod periodic_logger;
19
mod shm;
20
pub mod syslog;
21
pub mod test_utils;
22
mod timer;
23
mod tube;
24
mod types;
25
mod volatile_memory;
26
mod wait_context;
27
mod worker_thread;
28
mod write_zeroes;
29
30
pub mod sys;
31
pub use alloc::LayoutAllocation;
32
33
pub use clock::Clock;
34
pub use clock::FakeClock;
35
pub use errno::errno_result;
36
pub use errno::Error;
37
pub use errno::Result;
38
pub use event::Event;
39
pub use event::EventWaitResult;
40
pub use file_traits::FileAllocate;
41
pub use file_traits::FileGetLen;
42
pub use file_traits::FileReadWriteAtVolatile;
43
pub use file_traits::FileReadWriteVolatile;
44
pub use file_traits::FileSetLen;
45
pub use file_traits::FileSync;
46
pub use iobuf::IoBufMut;
47
pub use mmap::Error as MmapError;
48
pub use mmap::ExternalMapping;
49
pub use mmap::MappedRegion;
50
pub use mmap::MemoryMapping;
51
pub use mmap::MemoryMappingBuilder;
52
pub use mmap::Result as MmapResult;
53
pub use notifiers::CloseNotifier;
54
pub use notifiers::ReadNotifier;
55
pub use platform::ioctl::ioctl;
56
pub use platform::ioctl::ioctl_with_mut_ptr;
57
pub use platform::ioctl::ioctl_with_mut_ref;
58
pub use platform::ioctl::ioctl_with_ptr;
59
pub use platform::ioctl::ioctl_with_ref;
60
pub use platform::ioctl::ioctl_with_val;
61
pub use platform::ioctl::IoctlNr;
62
pub use shm::SharedMemory;
63
use sys::platform;
64
pub use timer::FakeTimer;
65
pub use timer::Timer;
66
pub use timer::TimerTrait;
67
pub use tube::Error as TubeError;
68
#[cfg(any(windows, feature = "proto_tube"))]
69
pub use tube::ProtoTube;
70
pub use tube::RecvTube;
71
pub use tube::Result as TubeResult;
72
pub use tube::SendTube;
73
pub use tube::Tube;
74
pub use types::fold_into_i32;
75
pub use types::NegativeI32;
76
pub use types::U31;
77
pub use volatile_memory::VolatileMemory;
78
pub use volatile_memory::VolatileMemoryError;
79
pub use volatile_memory::VolatileMemoryResult;
80
pub use volatile_memory::VolatileSlice;
81
pub use wait_context::EventToken;
82
pub use wait_context::EventType;
83
pub use wait_context::TriggeredEvent;
84
pub use wait_context::WaitContext;
85
pub use worker_thread::WorkerThread;
86
pub use write_zeroes::PunchHole;
87
pub use write_zeroes::WriteZeroesAt;
88
89
// TODO(b/233233301): reorganize platform specific exports under platform
90
// namespaces instead of exposing them directly in base::.
91
cfg_if::cfg_if! {
92
if #[cfg(any(target_os = "android", target_os = "linux"))] {
93
pub use sys::linux;
94
95
// descriptor/fd related exports.
96
pub use linux::{
97
clone_descriptor, safe_descriptor_from_path,
98
validate_raw_descriptor, clear_descriptor_cloexec,
99
safe_descriptor_from_cmdline_fd,
100
};
101
102
// Event/signal related exports.
103
pub use linux::{
104
block_signal, clear_signal, get_blocked_signals, new_pipe_full,
105
register_rt_signal_handler, signal, unblock_signal, Killable, SIGRTMIN,
106
AcpiNotifyEvent, NetlinkGenericSocket, SignalFd, Terminal,
107
};
108
109
pub use linux::{
110
drop_capabilities, pipe, read_raw_stdin
111
};
112
pub use linux::{enable_core_scheduling, set_rt_prio_limit, set_rt_round_robin};
113
pub use linux::{flock, FlockOperation};
114
pub use linux::{getegid, geteuid};
115
pub use linux::{gettid, kill_process_group, reap_child};
116
pub use linux::logical_core_capacity;
117
pub use linux::logical_core_cluster_id;
118
pub use linux::logical_core_frequencies_khz;
119
pub use linux::logical_core_max_freq_khz;
120
pub use linux::is_cpu_online;
121
pub use linux::sched_attr;
122
pub use linux::sched_setattr;
123
pub use linux::UnlinkUnixListener;
124
pub use linux::EventExt;
125
pub use linux::Gid;
126
}
127
}
128
129
cfg_if::cfg_if! {
130
if #[cfg(windows)] {
131
pub use sys::windows;
132
133
pub use windows::{EventTrigger, EventExt, WaitContextExt};
134
pub use windows::IoBuf;
135
pub use windows::MemoryMappingBuilderWindows;
136
pub use windows::set_thread_priority;
137
pub use windows::{give_foregrounding_permission, Console};
138
pub use windows::{named_pipes, named_pipes::PipeConnection};
139
pub use windows::{SafeMultimediaHandle, MAXIMUM_WAIT_OBJECTS};
140
pub use windows::set_sparse_file;
141
pub use windows::ioctl::ioctl_with_ptr_sized;
142
pub use windows::create_overlapped;
143
pub use windows::device_io_control;
144
pub use windows::is_cpu_online;
145
pub use windows::number_of_logical_cores;
146
pub use windows::number_of_online_cores;
147
pub use windows::pagesize;
148
pub use windows::read_overlapped_blocking;
149
150
pub use tube::{
151
deserialize_and_recv, serialize_and_send, set_alias_pid, set_duplicate_handle_tube,
152
DuplicateHandleRequest, DuplicateHandleResponse, DuplicateHandleTube
153
};
154
pub use tube::PipeTube;
155
pub use tube::FlushOnDropTube;
156
pub use windows::{set_audio_thread_priority, thread};
157
pub use windows::Pid;
158
pub use windows::Terminal;
159
}
160
}
161
162
cfg_if::cfg_if! {
163
if #[cfg(unix)] {
164
pub use sys::unix;
165
166
pub use unix::IoBuf;
167
pub use unix::net::UnixSeqpacket;
168
pub use unix::net::UnixSeqpacketListener;
169
pub use unix::net::UnlinkUnixSeqpacketListener;
170
pub use unix::ScmSocket;
171
pub use unix::SCM_SOCKET_MAX_FD_COUNT;
172
pub use unix::add_fd_flags;
173
pub use unix::clear_fd_flags;
174
pub use unix::number_of_logical_cores;
175
pub use unix::number_of_online_cores;
176
pub use unix::pagesize;
177
pub use unix::Pid;
178
}
179
}
180
181
pub use descriptor_reflection::deserialize_with_descriptors;
182
pub use descriptor_reflection::with_as_descriptor;
183
pub use descriptor_reflection::with_raw_descriptor;
184
pub use descriptor_reflection::FileSerdeWrapper;
185
pub use descriptor_reflection::SerializeDescriptors;
186
pub use log::debug;
187
pub use log::error;
188
pub use log::info;
189
pub use log::trace;
190
pub use log::warn;
191
pub use mmap::Protection;
192
pub use platform::get_cpu_affinity;
193
pub use platform::getpid;
194
pub use platform::open_file_or_duplicate;
195
pub use platform::platform_timer_resolution::enable_high_res_timers;
196
pub use platform::set_cpu_affinity;
197
pub use platform::set_thread_name;
198
pub use platform::BlockingMode;
199
pub use platform::EventContext;
200
pub use platform::FramingMode;
201
pub use platform::MemoryMappingArena;
202
pub use platform::RawDescriptor;
203
pub use platform::StreamChannel;
204
pub use platform::INVALID_DESCRIPTOR;
205
use uuid::Uuid;
206
207
pub use crate::descriptor::AsRawDescriptor;
208
pub use crate::descriptor::AsRawDescriptors;
209
pub use crate::descriptor::Descriptor;
210
pub use crate::descriptor::FromRawDescriptor;
211
pub use crate::descriptor::IntoRawDescriptor;
212
pub use crate::descriptor::SafeDescriptor;
213
214
/// An empty trait that helps reset timer resolution to its previous state.
215
// TODO(b:232103460): Maybe this needs to be thought through.
216
pub trait EnabledHighResTimer {}
217
218
/// Creates a UUID.
219
pub fn generate_uuid() -> String {
220
let mut buf = Uuid::encode_buffer();
221
Uuid::new_v4()
222
.as_hyphenated()
223
.encode_lower(&mut buf)
224
.to_owned()
225
}
226
227
use serde::Deserialize;
228
use serde::Serialize;
229
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
230
pub enum VmEventType {
231
Exit,
232
Reset,
233
DeviceCrashed,
234
Crash,
235
Panic(u8),
236
WatchdogReset,
237
}
238
239
/// Uses the system's page size in bytes to round the given value up to the nearest page boundary.
240
#[inline(always)]
241
pub fn round_up_to_page_size(v: usize) -> usize {
242
let page_mask = pagesize() - 1;
243
(v + page_mask) & !page_mask
244
}
245
246