twizzler_abi/runtime/object/
handle.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Implements some helper types and functions for working with objects in this runtime.

use core::{marker::PhantomData, ptr::NonNull};

use twizzler_rt_abi::object::{MapFlags, ObjectHandle};

use crate::{
    object::{ObjID, Protections, MAX_SIZE, NULLPAGE_SIZE},
    runtime::object::slot::global_allocate,
    rustc_alloc::boxed::Box,
    syscall::{
        sys_object_create, sys_object_map, BackingType, LifetimeType, ObjectCreate,
        ObjectCreateFlags,
    },
};

#[allow(dead_code)]
pub(crate) struct InternalObject<T> {
    slot: usize,
    runtime_handle: ObjectHandle,
    _pd: PhantomData<T>,
}

impl<T> core::fmt::Debug for InternalObject<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("InternalObject")
            .field("slot", &self.slot)
            .field("runtime_handle", &self.runtime_handle)
            .finish()
    }
}

impl<T> InternalObject<T> {
    #[allow(dead_code)]
    pub(crate) fn create_data_and_map() -> Option<Self> {
        let id = sys_object_create(
            ObjectCreate::new(
                BackingType::Normal,
                LifetimeType::Volatile,
                None,
                ObjectCreateFlags::empty(),
            ),
            &[],
            &[],
        )
        .ok()?;
        let slot = global_allocate()?;
        let _map = sys_object_map(
            None,
            id,
            slot,
            Protections::READ | Protections::WRITE,
            crate::syscall::MapFlags::empty(),
        )
        .ok()?;

        let start = (slot * MAX_SIZE) as *mut _;
        let meta = (((slot + 1) * MAX_SIZE) - NULLPAGE_SIZE) as *mut _;

        Some(Self {
            slot,
            runtime_handle: unsafe {
                ObjectHandle::new(
                    id,
                    super::new_runtime_info().cast(),
                    start,
                    meta,
                    MapFlags::READ | MapFlags::WRITE,
                    MAX_SIZE as u32,
                )
            },
            _pd: PhantomData,
        })
    }

    #[allow(dead_code)]
    pub(crate) fn base(&self) -> &T {
        let (start, _) = super::slot::slot_to_start_and_meta(self.slot);
        unsafe { ((start + NULLPAGE_SIZE) as *const T).as_ref().unwrap() }
    }

    #[allow(dead_code)]
    pub(crate) unsafe fn base_mut(&self) -> &mut T {
        let (start, _) = super::slot::slot_to_start_and_meta(self.slot);
        unsafe { ((start + NULLPAGE_SIZE) as *mut T).as_mut().unwrap() }
    }

    #[allow(dead_code)]
    pub(crate) fn id(&self) -> ObjID {
        self.runtime_handle.id()
    }

    #[allow(dead_code)]
    pub(crate) fn slot(&self) -> usize {
        self.slot
    }

    #[allow(dead_code)]
    pub(crate) fn map(id: ObjID, prot: Protections) -> Option<Self> {
        let slot = super::slot::global_allocate()?;
        crate::syscall::sys_object_map(None, id, slot, prot, crate::syscall::MapFlags::empty())
            .ok()?;

        let start = (slot * MAX_SIZE) as *mut _;
        let meta = (((slot + 1) * MAX_SIZE) - NULLPAGE_SIZE) as *mut _;

        Some(Self {
            runtime_handle: unsafe {
                ObjectHandle::new(
                    id,
                    super::new_runtime_info().cast(),
                    start,
                    meta,
                    prot.into(),
                    MAX_SIZE as u32,
                )
            },
            slot,
            _pd: PhantomData,
        })
    }

    #[allow(dead_code)]
    pub(crate) fn offset<P>(&self, offset: usize) -> Option<*const P> {
        if offset >= NULLPAGE_SIZE && offset < MAX_SIZE {
            Some(unsafe { self.runtime_handle.start().add(offset) as *const P })
        } else {
            None
        }
    }

    #[allow(dead_code)]
    pub(crate) fn offset_mut<P>(&mut self, offset: usize) -> Option<*mut P> {
        if offset >= NULLPAGE_SIZE && offset < MAX_SIZE {
            Some(unsafe { self.runtime_handle.start().add(offset) as *mut P })
        } else {
            None
        }
    }
}

impl From<Protections> for MapFlags {
    fn from(p: Protections) -> Self {
        let mut f = MapFlags::empty();
        if p.contains(Protections::READ) {
            f.insert(MapFlags::READ);
        }

        if p.contains(Protections::WRITE) {
            f.insert(MapFlags::WRITE);
        }

        if p.contains(Protections::EXEC) {
            f.insert(MapFlags::EXEC);
        }
        f
    }
}