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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

//! Helper functions and types shared between multiple `arch` crates.
//!
//! This function contains functions and types that do not have to be in the
//! core kernel and are architecture-specific, but are shared by two or more
//! `arch` crates. While these could also live in a dedicated crate, we use the
//! `kernel` crate as all `arch` crates already depend on it.

use crate::syscall::SyscallReturn;
use crate::ErrorCode;

/// Helper function to split a [`u64`] into a higher and lower [`u32`].
///
/// Used in encoding 64-bit wide system call return values on 32-bit
/// platforms.
#[inline]
fn u64_to_be_u32s(src: u64) -> (u32, u32) {
    let src_bytes = src.to_be_bytes();
    let src_msb = u32::from_be_bytes([src_bytes[0], src_bytes[1], src_bytes[2], src_bytes[3]]);
    let src_lsb = u32::from_be_bytes([src_bytes[4], src_bytes[5], src_bytes[6], src_bytes[7]]);

    (src_msb, src_lsb)
}

/// Enumeration of the system call return type variant identifiers described in
/// TRD104.
///
/// Each variant is associated with the respective variant identifier that would
/// be passed along with the return value to userspace.
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum TRD104SyscallReturnVariant {
    Failure = 0,
    FailureU32 = 1,
    FailureU32U32 = 2,
    FailureU64 = 3,
    Success = 128,
    SuccessU32 = 129,
    SuccessU32U32 = 130,
    SuccessU64 = 131,
    SuccessU32U32U32 = 132,
    SuccessU32U64 = 133,
}

/// System call return variants defined as defined in TRD104. These are a strict
/// subset of the variants defined in the core kernel's [`SyscallReturn`]
/// enum. For documentation on the individual variants, refer to this type
/// instead.
#[derive(Copy, Clone, Debug)]
pub enum TRD104SyscallReturn {
    Failure(ErrorCode),
    FailureU32(ErrorCode, u32),
    FailureU32U32(ErrorCode, u32, u32),
    FailureU64(ErrorCode, u64),
    Success,
    SuccessU32(u32),
    SuccessU32U32(u32, u32),
    SuccessU32U32U32(u32, u32, u32),
    SuccessU64(u64),
    SuccessU32U64(u32, u64),
    AllowReadWriteSuccess(*mut u8, usize),
    AllowReadWriteFailure(ErrorCode, *mut u8, usize),
    UserspaceReadableAllowSuccess(*mut u8, usize),
    UserspaceReadableAllowFailure(ErrorCode, *mut u8, usize),
    AllowReadOnlySuccess(*const u8, usize),
    AllowReadOnlyFailure(ErrorCode, *const u8, usize),
    SubscribeSuccess(*const (), usize),
    SubscribeFailure(ErrorCode, *const (), usize),
    YieldWaitFor(usize, usize, usize),
}

impl TRD104SyscallReturn {
    /// Map from the kernel's [`SyscallReturn`] enum to the subset of return
    /// values specified in TRD104. This ensures backwards compatibility with
    /// architectures implementing the ABI as specified in TRD104.
    pub fn from_syscall_return(syscall_return: SyscallReturn) -> Self {
        match syscall_return {
            // Identical variants:
            SyscallReturn::Failure(a) => TRD104SyscallReturn::Failure(a),
            SyscallReturn::FailureU32(a, b) => TRD104SyscallReturn::FailureU32(a, b),
            SyscallReturn::FailureU32U32(a, b, c) => TRD104SyscallReturn::FailureU32U32(a, b, c),
            SyscallReturn::FailureU64(a, b) => TRD104SyscallReturn::FailureU64(a, b),
            SyscallReturn::Success => TRD104SyscallReturn::Success,
            SyscallReturn::SuccessU32(a) => TRD104SyscallReturn::SuccessU32(a),
            SyscallReturn::SuccessU32U32(a, b) => TRD104SyscallReturn::SuccessU32U32(a, b),
            SyscallReturn::SuccessU32U32U32(a, b, c) => {
                TRD104SyscallReturn::SuccessU32U32U32(a, b, c)
            }
            SyscallReturn::SuccessU64(a) => TRD104SyscallReturn::SuccessU64(a),
            SyscallReturn::SuccessU32U64(a, b) => TRD104SyscallReturn::SuccessU32U64(a, b),
            SyscallReturn::AllowReadWriteSuccess(a, b) => {
                TRD104SyscallReturn::AllowReadWriteSuccess(a, b)
            }
            SyscallReturn::AllowReadWriteFailure(a, b, c) => {
                TRD104SyscallReturn::AllowReadWriteFailure(a, b, c)
            }
            SyscallReturn::UserspaceReadableAllowSuccess(a, b) => {
                TRD104SyscallReturn::UserspaceReadableAllowSuccess(a, b)
            }
            SyscallReturn::UserspaceReadableAllowFailure(a, b, c) => {
                TRD104SyscallReturn::UserspaceReadableAllowFailure(a, b, c)
            }
            SyscallReturn::AllowReadOnlySuccess(a, b) => {
                TRD104SyscallReturn::AllowReadOnlySuccess(a, b)
            }
            SyscallReturn::AllowReadOnlyFailure(a, b, c) => {
                TRD104SyscallReturn::AllowReadOnlyFailure(a, b, c)
            }
            SyscallReturn::SubscribeSuccess(a, b) => TRD104SyscallReturn::SubscribeSuccess(a, b),
            SyscallReturn::SubscribeFailure(a, b, c) => {
                TRD104SyscallReturn::SubscribeFailure(a, b, c)
            }
            SyscallReturn::YieldWaitFor(a, b, c) => TRD104SyscallReturn::YieldWaitFor(a, b, c),

            // Compatibility mapping:
            SyscallReturn::SuccessAddr(a) => TRD104SyscallReturn::SuccessU32(a as u32),
            SyscallReturn::SuccessPtr(a) => {
                TRD104SyscallReturn::SuccessU32(a.as_ptr::<()>() as u32)
            }
        }
    }
}

/// Encode the system call return value into 4 registers, following the encoding
/// specified in TRD104. Architectures which do not follow TRD104 are free to
/// define their own encoding.
pub fn encode_syscall_return_trd104(
    syscall_return: &TRD104SyscallReturn,
    a0: &mut u32,
    a1: &mut u32,
    a2: &mut u32,
    a3: &mut u32,
) {
    match *syscall_return {
        TRD104SyscallReturn::Failure(e) => {
            *a0 = TRD104SyscallReturnVariant::Failure as u32;
            *a1 = usize::from(e) as u32;
        }
        TRD104SyscallReturn::FailureU32(e, data0) => {
            *a0 = TRD104SyscallReturnVariant::FailureU32 as u32;
            *a1 = usize::from(e) as u32;
            *a2 = data0;
        }
        TRD104SyscallReturn::FailureU32U32(e, data0, data1) => {
            *a0 = TRD104SyscallReturnVariant::FailureU32U32 as u32;
            *a1 = usize::from(e) as u32;
            *a2 = data0;
            *a3 = data1;
        }
        TRD104SyscallReturn::FailureU64(e, data0) => {
            let (data0_msb, data0_lsb) = u64_to_be_u32s(data0);
            *a0 = TRD104SyscallReturnVariant::FailureU64 as u32;
            *a1 = usize::from(e) as u32;
            *a2 = data0_lsb;
            *a3 = data0_msb;
        }
        TRD104SyscallReturn::Success => {
            *a0 = TRD104SyscallReturnVariant::Success as u32;
        }
        TRD104SyscallReturn::SuccessU32(data0) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32 as u32;
            *a1 = data0;
        }
        TRD104SyscallReturn::SuccessU32U32(data0, data1) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32U32 as u32;
            *a1 = data0;
            *a2 = data1;
        }
        TRD104SyscallReturn::SuccessU32U32U32(data0, data1, data2) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32U32U32 as u32;
            *a1 = data0;
            *a2 = data1;
            *a3 = data2;
        }
        TRD104SyscallReturn::SuccessU64(data0) => {
            let (data0_msb, data0_lsb) = u64_to_be_u32s(data0);

            *a0 = TRD104SyscallReturnVariant::SuccessU64 as u32;
            *a1 = data0_lsb;
            *a2 = data0_msb;
        }
        TRD104SyscallReturn::SuccessU32U64(data0, data1) => {
            let (data1_msb, data1_lsb) = u64_to_be_u32s(data1);

            *a0 = TRD104SyscallReturnVariant::SuccessU32U64 as u32;
            *a1 = data0;
            *a2 = data1_lsb;
            *a3 = data1_msb;
        }
        TRD104SyscallReturn::AllowReadWriteSuccess(ptr, len) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32U32 as u32;
            *a1 = ptr as u32;
            *a2 = len as u32;
        }
        TRD104SyscallReturn::UserspaceReadableAllowSuccess(ptr, len) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32U32 as u32;
            *a1 = ptr as u32;
            *a2 = len as u32;
        }
        TRD104SyscallReturn::AllowReadWriteFailure(err, ptr, len) => {
            *a0 = TRD104SyscallReturnVariant::FailureU32U32 as u32;
            *a1 = usize::from(err) as u32;
            *a2 = ptr as u32;
            *a3 = len as u32;
        }
        TRD104SyscallReturn::UserspaceReadableAllowFailure(err, ptr, len) => {
            *a0 = TRD104SyscallReturnVariant::FailureU32U32 as u32;
            *a1 = usize::from(err) as u32;
            *a2 = ptr as u32;
            *a3 = len as u32;
        }
        TRD104SyscallReturn::AllowReadOnlySuccess(ptr, len) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32U32 as u32;
            *a1 = ptr as u32;
            *a2 = len as u32;
        }
        TRD104SyscallReturn::AllowReadOnlyFailure(err, ptr, len) => {
            *a0 = TRD104SyscallReturnVariant::FailureU32U32 as u32;
            *a1 = usize::from(err) as u32;
            *a2 = ptr as u32;
            *a3 = len as u32;
        }
        TRD104SyscallReturn::SubscribeSuccess(ptr, data) => {
            *a0 = TRD104SyscallReturnVariant::SuccessU32U32 as u32;
            *a1 = ptr as u32;
            *a2 = data as u32;
        }
        TRD104SyscallReturn::SubscribeFailure(err, ptr, data) => {
            *a0 = TRD104SyscallReturnVariant::FailureU32U32 as u32;
            *a1 = usize::from(err) as u32;
            *a2 = ptr as u32;
            *a3 = data as u32;
        }
        TRD104SyscallReturn::YieldWaitFor(data0, data1, data2) => {
            *a0 = data0 as u32;
            *a1 = data1 as u32;
            *a2 = data2 as u32;
        }
    }
}