1use crate::error::VdsoError;
6use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
7use lazy_static::lazy_static;
8use std::collections::HashMap;
9use zx::{self as zx, AsHandleRef, HandleBased};
10
11fn take_vdso_vmos() -> Result<HashMap<zx::Name, zx::Vmo>, VdsoError> {
12 let mut vmos = HashMap::new();
13 let mut i = 0;
14 while let Some(handle) = take_startup_handle(HandleInfo::new(HandleType::VdsoVmo, i)) {
15 let vmo = zx::Vmo::from(handle);
16 let name = vmo.get_name().map_err(VdsoError::GetName)?;
17 vmos.insert(name, vmo);
18 i += 1;
19 }
20 Ok(vmos)
21}
22
23pub fn get_vdso_vmo(name: &zx::Name) -> Result<zx::Vmo, VdsoError> {
24 lazy_static! {
25 static ref VMOS: HashMap<zx::Name, zx::Vmo> =
26 take_vdso_vmos().expect("Failed to take vDSO VMOs");
27 }
28 if let Some(vmo) = VMOS.get(name) {
29 vmo.duplicate_handle(zx::Rights::SAME_RIGHTS)
30 .map_err(|status| VdsoError::CouldNotDuplicate { name: *name, status })
31 } else {
32 Err(VdsoError::NotFound(*name))
33 }
34}
35
36pub fn get_stable_vdso_vmo() -> Result<zx::Vmo, VdsoError> {
39 get_vdso_vmo(&zx::Name::new_lossy("vdso/stable"))
40}
41
42pub fn get_next_vdso_vmo() -> Result<zx::Vmo, VdsoError> {
45 get_vdso_vmo(&zx::Name::new_lossy("vdso/next"))
46}