twizzler_abi/syscall/
create.rs

1use bitflags::bitflags;
2use twizzler_rt_abi::{object::Protections, Result};
3
4use super::{convert_codes_to_result, twzerr, Syscall};
5use crate::{arch::syscall::raw_syscall, object::ObjID};
6
7#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Default)]
8#[repr(C)]
9/// Specifications for an object-copy from a source object. The specified ranges are
10/// source:[src_start, src_start + len) copied to `<some unspecified destination
11/// object>:[dest_start, dest_start + len)`. Each range must start within an object, and end within
12/// the object.
13pub struct ObjectSource {
14    /// The ID of the source object, or zero for filling destination with zero.
15    pub id: ObjID,
16    /// The offset into the source object to start the copy. If id is zero, this field is reserved
17    /// for future use.
18    pub src_start: u64,
19    /// The offset into the dest object to start the copy or zero.
20    pub dest_start: u64,
21    /// The length of the copy or zero.
22    pub len: usize,
23}
24
25impl ObjectSource {
26    /// Construct a new ObjectSource.
27    pub fn new_copy(id: ObjID, src_start: u64, dest_start: u64, len: usize) -> Self {
28        Self {
29            id,
30            src_start,
31            dest_start,
32            len,
33        }
34    }
35
36    /// Construct a new ObjectSource.
37    pub fn new_zero(dest_start: u64, len: usize) -> Self {
38        Self {
39            id: ObjID::new(0),
40            src_start: 0,
41            dest_start,
42            len,
43        }
44    }
45}
46
47#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Default)]
48#[repr(C)]
49/// The backing memory type for this object. Currently doesn't do anything.
50pub enum BackingType {
51    /// The default, let the kernel decide based on the [LifetimeType] of the object.
52    #[default]
53    Normal = 0,
54}
55
56#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Default)]
57#[repr(C)]
58/// The base lifetime type of the object. Note that this does not ensure that the object is stored
59/// in a specific type of memory, the kernel is allowed to migrate objects with the Normal
60/// [BackingType] as it sees fit. For more information on object lifetime, see [the book](https://twizzler-operating-system.github.io/nightly/book/object_lifetime.html).
61pub enum LifetimeType {
62    /// This object is volatile, and is expected to be deleted after a power cycle.
63    #[default]
64    Volatile = 0,
65    /// This object is persistent, and should be deleted only after an explicit delete call.
66    Persistent = 1,
67}
68
69bitflags! {
70    /// Flags to pass to the object create system call.
71    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
72    pub struct ObjectCreateFlags: u32 {
73        const DELETE = 1;
74        const NO_NONCE = 2;
75    }
76}
77
78bitflags! {
79    /// Flags controlling how a particular object tie operates.
80    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
81    pub struct CreateTieFlags: u32 {
82    }
83}
84
85#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq)]
86#[repr(C)]
87/// Full object creation specification, minus ties.
88pub struct ObjectCreate {
89    pub kuid: ObjID,
90    pub bt: BackingType,
91    pub lt: LifetimeType,
92    pub flags: ObjectCreateFlags,
93    pub def_prot: Protections,
94}
95impl ObjectCreate {
96    /// Build a new object create specification.
97    pub fn new(
98        bt: BackingType,
99        lt: LifetimeType,
100        kuid: Option<ObjID>,
101        flags: ObjectCreateFlags,
102        def_prot: Protections,
103    ) -> Self {
104        Self {
105            kuid: kuid.unwrap_or_else(|| ObjID::new(0)),
106            bt,
107            lt,
108            flags,
109            def_prot,
110        }
111    }
112}
113
114impl Default for ObjectCreate {
115    fn default() -> Self {
116        Self::new(
117            BackingType::Normal,
118            LifetimeType::Volatile,
119            None,
120            ObjectCreateFlags::empty(),
121            Protections::all(),
122        )
123    }
124}
125
126#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq)]
127#[repr(C)]
128/// A specification of ties to create.
129/// (see [the book](https://twizzler-operating-system.github.io/nightly/book/object_lifetime.html) for more information on ties).
130pub struct CreateTieSpec {
131    pub id: ObjID,
132    pub flags: CreateTieFlags,
133}
134
135impl CreateTieSpec {
136    /// Create a new CreateTieSpec.
137    pub fn new(id: ObjID, flags: CreateTieFlags) -> Self {
138        Self { id, flags }
139    }
140}
141
142/// Create an object, returning either its ID or an error.
143pub fn sys_object_create(
144    create: ObjectCreate,
145    sources: &[ObjectSource],
146    ties: &[CreateTieSpec],
147) -> Result<ObjID> {
148    let args = [
149        &create as *const ObjectCreate as u64,
150        sources.as_ptr() as u64,
151        sources.len() as u64,
152        ties.as_ptr() as u64,
153        ties.len() as u64,
154    ];
155    let (code, val) = unsafe { raw_syscall(Syscall::ObjectCreate, &args) };
156    convert_codes_to_result(
157        code,
158        val,
159        |c, _| c == 0,
160        |x, y| crate::object::ObjID::from_parts([x, y]),
161        twzerr,
162    )
163}