-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc.rs
406 lines (334 loc) · 10.1 KB
/
svc.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::os::reslimit::LimitType;
use crate::{
os::{
mem::{MemoryOperation, MemoryPermission, QueryResult},
BorrowedHandle, OwnedHandle, RawHandle
},
result::Result,
sync::{ArbitrationType, ResetType},
};
use core::arch::asm;
use core::{
convert::{TryFrom, TryInto},
time::Duration,
};
use ctru_rt_macros::svc;
pub trait FromRegister {
unsafe fn from_register(reg: u32) -> Self;
}
impl FromRegister for u32 {
unsafe fn from_register(reg: u32) -> Self {
reg
}
}
impl FromRegister for usize {
unsafe fn from_register(reg: u32) -> Self {
reg as usize
}
}
impl FromRegister for i32 {
unsafe fn from_register(reg: u32) -> Self {
reg as i32
}
}
pub trait IntoRegister {
type Register;
unsafe fn into_register(self) -> Self::Register;
}
macro_rules! into_register_implicit {
($($t:ty as $into:ty),* $(,)?) => {
$(
impl IntoRegister for $t {
type Register = $into;
unsafe fn into_register(self) -> Self::Register {
self as Self::Register
}
}
)*
}
}
into_register_implicit! {
u32 as u32,
i32 as i32,
usize as usize,
bool as u32,
MemoryPermission as u32,
unsafe extern "C" fn(usize) as u32,
}
impl<T> IntoRegister for *mut T {
type Register = *mut T;
unsafe fn into_register(self) -> *mut T {
self
}
}
impl<T> IntoRegister for *const T {
type Register = *const T;
unsafe fn into_register(self) -> *const T {
self
}
}
pub unsafe fn control_memory(
op: MemoryOperation,
addr0: usize,
addr1: usize,
size: usize,
permission: MemoryPermission,
) -> Result<usize> {
let op = op.0;
let dest_addr = svc!(0x01: (op, addr0, addr1, size, permission) -> usize)?;
Ok(dest_addr)
}
pub unsafe fn query_memory(addr: usize) -> Result<QueryResult> {
let (base_process_virtual_address, size, permission, state, page_flags) =
svc!(0x02: (_, _, addr) -> (usize, usize, u32, u32, u32))?;
// TODO: yeah, let's hope the kernel always returns valid values here
let permission = core::mem::transmute(permission);
let state = core::mem::transmute(state);
Ok(QueryResult {
base_process_virtual_address,
size,
permission,
state,
page_flags,
})
}
pub fn exit_process() -> ! {
unsafe { svc!(0x03: () -> !) }
}
/// # SAFETY:
/// * `stacktop` must be aligned to 8 bytes
pub unsafe fn create_thread(
priority: i32,
entry_point: unsafe extern "C" fn(usize),
argument: usize,
stacktop: *mut u8,
processor_id: i32,
) -> Result<OwnedHandle> {
svc!(0x08: (priority, entry_point, argument, stacktop, processor_id) -> OwnedHandle)
}
pub fn exit_thread() -> ! {
unsafe { svc!(0x09: () -> !) }
}
/// Pause the current thread for the specified duration.
pub fn sleep_thread(duration: Timeout) {
let (ns_high, ns_low) = (duration.reg_high(), duration.reg_low());
unsafe {
let _ = svc!(0x0a: (ns_low, ns_high));
}
}
pub fn get_thread_priority(handle: BorrowedHandle) -> Result<i32> {
unsafe { svc!(0x0b: (_, handle) -> i32) }
}
pub fn create_mutex(initially_locked: bool) -> Result<OwnedHandle> {
unsafe { svc!(0x13: (initially_locked) -> OwnedHandle) }
}
pub fn release_mutex(handle: BorrowedHandle) -> Result<()> {
unsafe { svc!(0x14: (handle)) }
}
pub fn create_event(reset_type: ResetType) -> Result<OwnedHandle> {
let reset_type = reset_type as u32;
unsafe { svc!(0x17: (reset_type) -> OwnedHandle) }
}
pub fn signal_event(handle: BorrowedHandle) -> Result<()> {
unsafe { svc!(0x18: (handle)) }
}
pub fn clear_event(handle: BorrowedHandle) -> Result<()> {
unsafe { svc!(0x19: (handle)) }
}
pub unsafe fn create_memory_block(
address: usize,
size: usize,
my_permissions: MemoryPermission,
other_permissions: MemoryPermission,
) -> Result<OwnedHandle> {
svc!(0x1e: (other_permissions, address, size, my_permissions) -> OwnedHandle)
}
pub unsafe fn map_memory_block(
handle: BorrowedHandle,
address: usize,
my_permissions: MemoryPermission,
other_permissions: MemoryPermission,
) -> Result<()> {
svc!(0x1f: (handle, address, my_permissions, other_permissions))
}
pub unsafe fn unmap_memory_block(handle: BorrowedHandle, addr: usize) -> Result<()> {
svc!(0x20: (handle, addr))
}
pub fn create_address_arbiter() -> Result<OwnedHandle> {
unsafe { svc!(0x21: () -> OwnedHandle) }
}
pub fn arbitrate_address(
handle: BorrowedHandle,
address: usize,
arbitration_type: ArbitrationType,
value: i32,
timeout: Timeout,
) -> Result<()> {
let (ns_low, ns_high) = (timeout.reg_low(), timeout.reg_high());
unsafe { svc!(0x22: (handle, address, arbitration_type, value, ns_low, ns_high)) }
}
pub unsafe fn close_handle(handle: RawHandle) -> Result<()> {
svc!(0x23: (handle))
}
pub fn wait_synchronization(handle: BorrowedHandle, timeout: Timeout) -> Result<()> {
let (ns_high, ns_low) = (timeout.reg_high(), timeout.reg_low());
unsafe { svc!(0x24: (handle, _, ns_high, ns_low)) }
}
pub fn wait_synchronization_many(
handles: &[BorrowedHandle],
wait_all: bool,
timeout: Timeout,
) -> Result<i32> {
let (ns_high, ns_low) = (timeout.reg_high(), timeout.reg_low());
let num_handles = handles.len();
let handles: *const BorrowedHandle = handles.as_ptr();
let signaled = unsafe { svc!(0x25: (ns_low, handles, num_handles, wait_all, ns_high) -> i32) }?;
Ok(signaled)
}
const WAIT_FIRST: bool = false;
const WAIT_ALL: bool = true;
pub fn wait_synchronization_all(handles: &[BorrowedHandle], timeout: Timeout) -> Result<()> {
let _ = wait_synchronization_many(handles, WAIT_ALL, timeout)?;
Ok(())
}
pub fn wait_synchronization_any(handles: &[BorrowedHandle], timeout: Timeout) -> Result<usize> {
let signaled = wait_synchronization_many(handles, WAIT_ALL, timeout)?;
match usize::try_from(signaled) {
Ok(index) => Ok(index),
Err(_) => panic!(
"svc::wait_synchronization_any returned negative handle index {}, \
expected a non-negative index into a list of {} handles",
signaled,
handles.len(),
),
}
}
pub fn duplicate_handle(handle: BorrowedHandle) -> Result<OwnedHandle> {
unsafe { svc!(0x27: (_, handle) -> OwnedHandle) }
}
pub fn get_system_tick_count() -> u64 {
let tick_low: u32;
let tick_high: u32;
unsafe {
asm!("svc 0x28", lateout("r0") tick_high, lateout("r1") tick_low);
}
(tick_high as u64) << 32 | tick_low as u64
}
pub unsafe fn get_system_info(sysinfo_type: u32, param: i32) -> Result<i64> {
let (out_low, out_high) = svc!(0x2a: (_, sysinfo_type, param) -> (u32, u32))?;
Ok(((out_high as i64) << 32) | out_low as i64)
}
pub fn connect_to_port(port_name: &str) -> Result<OwnedHandle> {
let port_name = port_name.as_ptr();
unsafe { svc!(0x2d: (_, port_name) -> OwnedHandle) }
}
#[inline]
pub unsafe fn send_sync_request(handle: BorrowedHandle, command_buffer: *mut u32) -> Result<*mut u32> {
svc!(0x32: (handle))?;
Ok(command_buffer)
}
pub fn get_process_id(process_handle: BorrowedHandle) -> Result<u32> {
unsafe { svc!(0x35: (_, process_handle) -> u32) }
}
pub fn get_resource_limit(process_handle: BorrowedHandle) -> Result<OwnedHandle> {
let mut out_handle: u32 = 0;
let out_handle_ptr = &mut out_handle as *mut u32;
unsafe {
svc!(
0x38:
(
out_handle_ptr, // TODO: is it really necessary to pass this in?
process_handle,
) -> OwnedHandle
)
}
}
pub fn get_resource_limit_values<const N: usize>(
limits_handle: BorrowedHandle,
values: &mut [i64; N],
limit_types: &[LimitType; N],
) -> Result<()> {
let values = values.as_mut_ptr();
let limit_types = limit_types.as_ptr();
unsafe { svc!(0x39: (values, limits_handle, limit_types, N)) }
}
pub fn get_resource_limit_current_values<const N: usize>(
limits_handle: BorrowedHandle,
values: &mut [i64; N],
limit_types: &[LimitType; N],
) -> Result<()> {
let values = values.as_mut_ptr();
let limit_types = limit_types.as_ptr();
unsafe { svc!(0x3a: (values, limits_handle, limit_types, N)) }
}
#[derive(Debug)]
pub enum UserBreakReason {
Panic = 0,
Assert = 1,
User = 2,
LoadRo = 3,
UnloadRo = 4,
}
pub fn user_break(reason: UserBreakReason) -> ! {
let reason = reason as u32;
unsafe { svc!(0x3c: (reason) -> !) }
#[allow(unreachable_code)]
loop {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
}
#[inline(always)]
#[doc(hidden)]
pub fn output_debug_bytes(bytes: &[u8]) {
let ptr = bytes.as_ptr();
let len = bytes.len();
unsafe {
let _ = svc!(0x3d: (ptr, len));
}
}
pub fn output_debug_string(message: &str) {
output_debug_bytes(message.as_bytes())
}
pub fn stop_point() {
unsafe { asm!("svc 0xff") }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Timeout(i64);
impl Timeout {
pub const fn from_nanoseconds(nanoseconds: i64) -> Self {
Self(nanoseconds)
}
pub const fn from_seconds(seconds: i64) -> Self {
Self(seconds * 1_000_000_000)
}
pub const fn forever() -> Self {
Self::from_nanoseconds(i64::max_value())
}
pub const fn none() -> Self {
Self::from_nanoseconds(0)
}
#[inline]
pub(crate) const fn reg_high(self) -> u32 {
((self.0 as u64) >> 32) as u32
}
#[inline]
pub(crate) const fn reg_low(self) -> u32 {
self.0 as u64 as u32
}
}
impl From<Duration> for Timeout {
fn from(duration: Duration) -> Self {
match duration.as_nanos().try_into() {
Ok(ns) => Self::from_nanoseconds(ns),
Err(_) => Self::forever(),
}
}
}
impl From<i64> for Timeout {
fn from(nanoseconds: i64) -> Self {
Self::from_nanoseconds(nanoseconds)
}
}