twizzler_abi/syscall/
map.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use core::mem::MaybeUninit;

use bitflags::bitflags;
use num_enum::{FromPrimitive, IntoPrimitive};

use super::{convert_codes_to_result, justval, Syscall};
use crate::{
    arch::syscall::raw_syscall,
    object::{ObjID, Protections},
};

#[derive(
    Debug,
    Copy,
    Clone,
    PartialEq,
    PartialOrd,
    Ord,
    Eq,
    IntoPrimitive,
    FromPrimitive,
    thiserror::Error,
)]
#[repr(u64)]
/// Possible error values for [sys_object_map].
pub enum ObjectMapError {
    #[num_enum(default)]
    /// An unknown error occurred.
    #[error("unknown error")]
    Unknown = 0,
    /// The specified object was not found.
    #[error("object not found")]
    ObjectNotFound = 1,
    /// The specified slot was invalid.
    #[error("invalid slot")]
    InvalidSlot = 2,
    /// The specified protections were invalid.
    #[error("invalid protections")]
    InvalidProtections = 3,
    /// An argument was invalid.
    #[error("invalid argument")]
    InvalidArgument = 4,
}

impl core::error::Error for ObjectMapError {}

bitflags! {
    /// Flags to pass to [sys_object_map].
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub struct MapFlags: u32 {
    }
}

/// Map an object into the address space with the specified protections.
pub fn sys_object_map(
    handle: Option<ObjID>,
    id: ObjID,
    slot: usize,
    prot: Protections,
    flags: MapFlags,
) -> Result<usize, ObjectMapError> {
    let (hi, lo) = id.split();
    let args = [
        hi,
        lo,
        slot as u64,
        prot.bits() as u64,
        flags.bits() as u64,
        &handle as *const Option<ObjID> as usize as u64,
    ];
    let (code, val) = unsafe { raw_syscall(Syscall::ObjectMap, &args) };
    convert_codes_to_result(code, val, |c, _| c != 0, |_, v| v as usize, justval)
}

#[derive(
    Debug,
    Copy,
    Clone,
    PartialEq,
    PartialOrd,
    Ord,
    Eq,
    IntoPrimitive,
    FromPrimitive,
    thiserror::Error,
)]
#[repr(u64)]
/// Possible error values for [sys_object_unmap].
pub enum ObjectUnmapError {
    /// An unknown error occurred.
    #[num_enum(default)]
    #[error("unknown error")]
    Unknown = 0,
    /// The specified slot was invalid.
    #[error("invalid slot")]
    InvalidSlot = 1,
    /// An argument was invalid.
    #[error("invalid argument")]
    InvalidArgument = 2,
}

impl core::error::Error for ObjectUnmapError {}

bitflags! {
    /// Flags to pass to [sys_object_unmap].
    pub struct UnmapFlags: u32 {
    }
}

/// Unmaps an object from the address space specified by `handle` (or the current address space if
/// none is specified).
pub fn sys_object_unmap(
    handle: Option<ObjID>,
    slot: usize,
    flags: UnmapFlags,
) -> Result<(), ObjectUnmapError> {
    let (hi, lo) = handle.unwrap_or_else(|| 0.into()).split();
    let args = [hi, lo, slot as u64, flags.bits() as u64];
    let (code, val) = unsafe { raw_syscall(Syscall::ObjectUnmap, &args) };
    convert_codes_to_result(code, val, |c, _| c != 0, |_, _| (), justval)
}

#[derive(
    Debug,
    Copy,
    Clone,
    PartialEq,
    PartialOrd,
    Ord,
    Eq,
    FromPrimitive,
    IntoPrimitive,
    thiserror::Error,
)]
#[repr(u64)]
/// Possible error values for [sys_object_unmap].
pub enum ObjectReadMapError {
    /// An unknown error occurred.
    #[num_enum(default)]
    #[error("unknown error")]
    Unknown = 0,
    /// The specified slot was invalid.
    #[error("invalid slot")]
    InvalidSlot = 1,
    /// An argument was invalid.
    #[error("invalid argument")]
    InvalidArgument = 2,
}

impl core::error::Error for ObjectReadMapError {}

/// Information about an object mapping.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct MapInfo {
    /// The mapped object ID.
    pub id: ObjID,
    /// The protections of the mapping.
    pub prot: Protections,
    /// The slot.
    pub slot: usize,
    /// The mapping flags.
    pub flags: MapFlags,
}

/// Reads the map information about a given slot in the address space specified by `handle` (or
/// current address space if none is specified).
pub fn sys_object_read_map(
    handle: Option<ObjID>,
    slot: usize,
) -> Result<MapInfo, ObjectReadMapError> {
    let (hi, lo) = handle.unwrap_or_else(|| 0.into()).split();
    let mut map_info = MaybeUninit::<MapInfo>::uninit();
    let args = [
        hi,
        lo,
        slot as u64,
        &mut map_info as *mut MaybeUninit<MapInfo> as usize as u64,
    ];
    let (code, val) = unsafe { raw_syscall(Syscall::ObjectReadMap, &args) };
    convert_codes_to_result(
        code,
        val,
        |c, _| c != 0,
        |_, _| unsafe { map_info.assume_init() },
        justval,
    )
}