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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! define SyscallArgs and SyscallRet
//! provide helper functions/trait impls to pack/unpack
//! SyscallArgs and SyscallRet.
//! io:Error is not implemented for better no_std support.

use core::result::Result;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// The 6 arguments of a syscall, raw untyped version.
///
/// TODO: Use a helper function to convert to a structured Syscall+Args enum.
#[derive(PartialEq, Debug, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SyscallArgs {
    pub arg0: u64,
    pub arg1: u64,
    pub arg2: u64,
    pub arg3: u64,
    pub arg4: u64,
    pub arg5: u64,
}

impl SyscallArgs {
    pub fn new(a0: u64, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64) -> Self {
        SyscallArgs {
            arg0: a0,
            arg1: a1,
            arg2: a2,
            arg3: a3,
            arg4: a4,
            arg5: a5,
        }
    }
}

impl From<&[u64; 6]> for SyscallArgs {
    fn from(args: &[u64; 6]) -> Self {
        SyscallArgs {
            arg0: args[0],
            arg1: args[1],
            arg2: args[2],
            arg3: args[3],
            arg4: args[4],
            arg5: args[5],
        }
    }
}

impl From<&[u64; 5]> for SyscallArgs {
    fn from(args: &[u64; 5]) -> Self {
        SyscallArgs {
            arg0: args[0],
            arg1: args[1],
            arg2: args[2],
            arg3: args[3],
            arg4: args[4],
            arg5: 0,
        }
    }
}

impl From<&[u64; 4]> for SyscallArgs {
    fn from(args: &[u64; 4]) -> Self {
        SyscallArgs {
            arg0: args[0],
            arg1: args[1],
            arg2: args[2],
            arg3: args[3],
            arg4: 0,
            arg5: 0,
        }
    }
}

impl From<&[u64; 3]> for SyscallArgs {
    fn from(args: &[u64; 3]) -> Self {
        SyscallArgs {
            arg0: args[0],
            arg1: args[1],
            arg2: args[2],
            arg3: 0,
            arg4: 0,
            arg5: 0,
        }
    }
}

impl From<&[u64; 2]> for SyscallArgs {
    fn from(args: &[u64; 2]) -> Self {
        SyscallArgs {
            arg0: args[0],
            arg1: args[1],
            arg2: 0,
            arg3: 0,
            arg4: 0,
            arg5: 0,
        }
    }
}

impl From<&[u64; 1]> for SyscallArgs {
    fn from(args: &[u64; 1]) -> Self {
        SyscallArgs {
            arg0: args[0],
            arg1: 0,
            arg2: 0,
            arg3: 0,
            arg4: 0,
            arg5: 0,
        }
    }
}

impl From<&[u64; 0]> for SyscallArgs {
    fn from(_args: &[u64; 0]) -> Self {
        SyscallArgs {
            arg0: 0,
            arg1: 0,
            arg2: 0,
            arg3: 0,
            arg4: 0,
            arg5: 0,
        }
    }
}

#[macro_export]
macro_rules! syscall_args {
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr) => {
        $crate::SyscallArgs::new($a, $b, $c, $d, $e, $f)
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr) => {
        $crate::SyscallArgs::new($a, $b, $c, $d, $e, 0)
    };
    ($a:expr, $b:expr, $c:expr, $d:expr) => {
        $crate::SyscallArgs::new($a, $b, $c, $d, 0, 0)
    };
    ($a:expr, $b:expr, $c:expr) => {
        $crate::SyscallArgs::new($a, $b, $c, 0, 0, 0)
    };
    ($a:expr, $b:expr) => {
        $crate::SyscallArgs::new($a, $b, 0, 0, 0, 0)
    };
    ($a:expr) => {
        $crate::SyscallArgs::new($a, 0, 0, 0, 0, 0)
    };
    () => {
        $crate::SyscallArgs::new(0, 0, 0, 0, 0, 0)
    };
}

#[test]
fn syscall_args_macro_test() {
    assert_eq!(
        syscall_args!(1, 2, 3, 4, 5, 6),
        SyscallArgs::new(1, 2, 3, 4, 5, 6)
    );
    assert_eq!(
        syscall_args!(1, 2, 3, 4, 5),
        SyscallArgs::new(1, 2, 3, 4, 5, 0)
    );
    assert_eq!(
        syscall_args!(1, 2, 3, 4),
        SyscallArgs::new(1, 2, 3, 4, 0, 0)
    );
    assert_eq!(syscall_args!(1, 2, 3), SyscallArgs::new(1, 2, 3, 0, 0, 0));
    assert_eq!(syscall_args!(1, 2), SyscallArgs::new(1, 2, 0, 0, 0, 0));
    assert_eq!(syscall_args!(1), SyscallArgs::new(1, 0, 0, 0, 0, 0));
    assert_eq!(syscall_args!(), SyscallArgs::new(0, 0, 0, 0, 0, 0));
}

#[test]
fn syscall_args_from_u64_slice() {
    assert_eq!(
        SyscallArgs::from(&[1, 2, 3, 4, 5, 6]),
        syscall_args!(1, 2, 3, 4, 5, 6)
    );
    assert_eq!(
        SyscallArgs::from(&[1, 2, 3, 4, 5]),
        syscall_args!(1, 2, 3, 4, 5)
    );
    assert_eq!(SyscallArgs::from(&[1, 2, 3, 4]), syscall_args!(1, 2, 3, 4));
    assert_eq!(SyscallArgs::from(&[1, 2, 3]), syscall_args!(1, 2, 3));
    assert_eq!(SyscallArgs::from(&[1, 2]), syscall_args!(1, 2));
    assert_eq!(SyscallArgs::from(&[1]), syscall_args!(1));
    assert_eq!(SyscallArgs::from(&[0]), syscall_args!());
}

/// syscall return value
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct SyscallRet {
    pub retval: i64,
}

impl From<i64> for SyscallRet {
    fn from(retval: i64) -> Self {
        SyscallRet { retval }
    }
}

impl From<Result<i64, i64>> for SyscallRet {
    fn from(res: Result<i64, i64>) -> Self {
        match res {
            Ok(ret_ok) => SyscallRet { retval: ret_ok },
            Err(ret_err) => SyscallRet { retval: -ret_err },
        }
    }
}

impl From<Result<i64, i32>> for SyscallRet {
    fn from(res: Result<i64, i32>) -> Self {
        match res {
            Ok(ret_ok) => SyscallRet { retval: ret_ok },
            Err(ret_err) => SyscallRet {
                retval: -ret_err as i64,
            },
        }
    }
}

impl From<SyscallRet> for Result<i64, i64> {
    fn from(res: SyscallRet) -> Result<i64, i64> {
        crate::helper::syscall_ret(res.retval)
    }
}

impl From<SyscallRet> for Result<i64, i32> {
    fn from(res: SyscallRet) -> Result<i64, i32> {
        crate::helper::syscall_ret(res.retval).map_err(|e| e as i32)
    }
}

#[test]
fn syscall_ret_err_test() {
    let ok_value = 0x7fff_1234_5678i64;
    let err_value = 22i64;
    let ok1: Result<i64, i64> = Ok(ok_value);
    assert_eq!(SyscallRet::from(ok1), SyscallRet::from(ok_value));

    let err1: Result<i64, i64> = Err(err_value);
    assert_eq!(SyscallRet::from(err1), SyscallRet::from(-err_value as i64));

    let ok2: Result<i64, i32> = Ok(ok_value);
    assert_eq!(SyscallRet::from(ok2), SyscallRet::from(ok_value));

    let err2: Result<i64, i32> = Err(err_value as i32);
    assert_eq!(SyscallRet::from(err2), SyscallRet::from(-err_value as i64));

    let res1: SyscallRet = ok1.into();
    assert_eq!(res1, SyscallRet::from(ok_value));

    let res2: SyscallRet = err1.into();
    assert_eq!(res2, SyscallRet::from(-err_value as i64));

    let ok1_1: Result<i64, i64> = res1.into();
    assert_eq!(ok1_1, ok1);

    let err1_1: Result<i64, i64> = res2.into();
    assert_eq!(err1_1, err1);
}