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)]
9pub struct ObjectSource {
14 pub id: ObjID,
16 pub src_start: u64,
19 pub dest_start: u64,
21 pub len: usize,
23}
24
25impl ObjectSource {
26 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 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)]
49pub enum BackingType {
51 #[default]
53 Normal = 0,
54}
55
56#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Default)]
57#[repr(C)]
58pub enum LifetimeType {
62 #[default]
64 Volatile = 0,
65 Persistent = 1,
67}
68
69bitflags! {
70 #[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 #[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)]
87pub 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 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)]
128pub struct CreateTieSpec {
131 pub id: ObjID,
132 pub flags: CreateTieFlags,
133}
134
135impl CreateTieSpec {
136 pub fn new(id: ObjID, flags: CreateTieFlags) -> Self {
138 Self { id, flags }
139 }
140}
141
142pub 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}