1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use 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};
1011fn take_vdso_vmos() -> Result<HashMap<zx::Name, zx::Vmo>, VdsoError> {
12let mut vmos = HashMap::new();
13let mut i = 0;
14while let Some(handle) = take_startup_handle(HandleInfo::new(HandleType::VdsoVmo, i)) {
15let vmo = zx::Vmo::from(handle);
16let name = vmo.get_name().map_err(VdsoError::GetName)?;
17 vmos.insert(name, vmo);
18 i += 1;
19 }
20Ok(vmos)
21}
2223pub fn get_vdso_vmo(name: &zx::Name) -> Result<zx::Vmo, VdsoError> {
24lazy_static! {
25static ref VMOS: HashMap<zx::Name, zx::Vmo> =
26 take_vdso_vmos().expect("Failed to take vDSO VMOs");
27 }
28if 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 {
32Err(VdsoError::NotFound(*name))
33 }
34}
3536/// Returns an owned VMO handle to the stable vDSO, duplicated from the handle
37/// provided to this process through its processargs bootstrap message.
38pub fn get_stable_vdso_vmo() -> Result<zx::Vmo, VdsoError> {
39 get_vdso_vmo(&zx::Name::new_lossy("vdso/stable"))
40}
4142/// Returns an owned VMO handle to the next vDSO, duplicated from the handle
43/// provided to this process through its processargs bootstrap message.
44pub fn get_next_vdso_vmo() -> Result<zx::Vmo, VdsoError> {
45 get_vdso_vmo(&zx::Name::new_lossy("vdso/next"))
46}