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
use crate::result::*;
use crate::svc;
use crate::mem::alloc;
use crate::wait;
use crate::util;
use core::ptr;
use core::arch::asm;

pub mod rc;

pub type ThreadName = util::CString<0x20>;

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(u8)]
pub enum ThreadState {
    #[default]
    NotInitialized = 0,
    Initialized = 1,
    DestroyedBeforeStarted = 2,
    Started = 3,
    Terminated = 4
}

pub struct ThreadEntry {
    pub entry_impl: svc::ThreadEntrypointFn,
    pub raw_entry: *const u8,
    pub raw_args: *const u8
}

impl ThreadEntry {
    pub fn new<T: Copy, F: 'static + Fn(&T)>(entry_impl: svc::ThreadEntrypointFn, entry: F, args: &T) -> Self {
        Self {
            entry_impl,
            raw_entry: &entry as *const _ as *const u8,
            raw_args: args as *const _ as *const u8
        }
    }

    pub fn run<T: Copy, F: 'static + Fn(&T)>(&self) {
        unsafe {
            let entry = self.raw_entry as *const F;
            let args = &*(self.raw_args as *const T);
            (*entry)(args);
        }
    }
}

extern fn thread_entry_impl<T: Copy, F: 'static + Fn(&T)>(thread_ref_v: *mut u8) -> ! {
    let thread_ref = thread_ref_v as *mut Thread;
    set_current_thread(thread_ref);

    unsafe {
        if let Some(entry_ref) = (*thread_ref).entry.as_ref() {
            entry_ref.run::<T, F>();
        }
    }

    exit()
}

pub const PRIORITY_AUTO: i32 = -1;

// Note: our thread type attempts to kind-of mimic the official nn::os::ThreadType struct, at least so that the thread name is properly accessible from TLS by, for instance, creport -- thus all the reserved fields
// We act like nn::os::ThreadType version 1

const CURRENT_THREAD_VERSION: u16 = 1;

#[repr(C)]
pub struct Thread {
    pub self_ref: *mut Thread,
    pub state: ThreadState,
    pub owns_stack: bool,
    pub pad: [u8; 2],
    pub handle: svc::Handle,
    pub stack: *mut u8,
    pub stack_size: usize,
    pub reserved: [u8; 0x26],
    pub version: u16,
    pub reserved_2: [u8; 0xF8],
    pub entry: Option<ThreadEntry>,
    pub reserved_3: [u8; 0x28],
    pub name: ThreadName,
    pub name_addr: *mut u8,
    pub reserved_4: [u8; 0x20]
}

impl Thread {
    pub const fn empty() -> Self {
        Self {
            self_ref: ptr::null_mut(),
            state: ThreadState::NotInitialized,
            owns_stack: false,
            pad: [0; 2],
            handle: 0,
            stack: ptr::null_mut(),
            stack_size: 0,
            reserved: [0; 0x26],
            version: CURRENT_THREAD_VERSION,
            reserved_2: [0; 0xF8],
            entry: None,
            reserved_3: [0; 0x28],
            name: ThreadName::new(),
            name_addr: ptr::null_mut(),
            reserved_4: [0; 0x20]
        }
    }

    fn new_impl(handle: svc::Handle, state: ThreadState, name: &str, stack: *mut u8, stack_size: usize, owns_stack: bool, entry: Option<ThreadEntry>) -> Result<Self> {
        let mut thread = Self {
            self_ref: ptr::null_mut(),
            state,
            owns_stack,
            pad: [0; 2],
            handle,
            stack,
            stack_size,
            reserved: [0; 0x26],
            version: CURRENT_THREAD_VERSION,
            reserved_2: [0; 0xF8],
            entry,
            reserved_3: [0; 0x28],
            name: ThreadName::new(),
            name_addr: ptr::null_mut(),
            reserved_4: [0; 0x20]
        };
        thread.self_ref = &mut thread;
        thread.name_addr = &mut thread.name as *mut ThreadName as *mut u8;
        thread.name.set_str(name);
        Ok(thread)
    }

    #[inline]
    pub fn new_remote(handle: svc::Handle, name: &str, stack: *mut u8, stack_size: usize) -> Result<Self> {
        Self::new_impl(handle, ThreadState::Started, name, stack, stack_size, false, None)
    }
    
    pub fn new_with_stack<T: Copy, F: 'static + Fn(&T)>(entry: F, args: &T, name: &str, stack: *mut u8, stack_size: usize) -> Result<Self> {
        result_return_unless!(!stack.is_null(), rc::ResultInvalidStack);
        // TODO: also check alignment

        let thread_entry = ThreadEntry::new(thread_entry_impl::<T, F>, entry, args);
        Self::new_impl(svc::INVALID_HANDLE, ThreadState::NotInitialized, name, stack, stack_size, false, Some(thread_entry))
    }
    
    pub fn new<T: Copy, F: 'static + Fn(&T)>(entry: F, args: &T, name: &str, stack_size: usize) -> Result<Self> {
        let stack = alloc::allocate(alloc::PAGE_ALIGNMENT, stack_size)?;

        let thread_entry = ThreadEntry::new(thread_entry_impl::<T, F>, entry, args);
        Self::new_impl(svc::INVALID_HANDLE, ThreadState::NotInitialized, name, stack, stack_size, true, Some(thread_entry))
    }

    pub fn initialize(&mut self, priority: i32, processor_id: i32) -> Result<()> {
        result_return_unless!(self.state == ThreadState::NotInitialized, rc::ResultInvalidState);

        let mut priority_value = priority;
        if priority_value == PRIORITY_AUTO {
            priority_value = get_current_thread().get_priority()?;
        }

        self.handle = svc::create_thread(self.entry.as_ref().unwrap().entry_impl, self as *mut _ as *mut u8, (self.stack as usize + self.stack_size) as *const u8, priority_value, processor_id)?;
        
        self.state = ThreadState::Initialized;
        Ok(())
    }

    pub fn start(&mut self) -> Result<()> {
        result_return_unless!((self.state == ThreadState::Initialized) || (self.state == ThreadState::Terminated), rc::ResultInvalidState);

        svc::start_thread(self.handle)?;

        self.state = ThreadState::Started;
        Ok(())
    }

    pub fn join(&mut self) -> Result<()> {
        result_return_unless!(self.state == ThreadState::Started, rc::ResultInvalidState);
        
        wait::wait_handles(&[self.handle], -1)?;

        self.state = ThreadState::Terminated;
        Ok(())
    }

    #[inline]
    pub fn is_remote(&self) -> bool {
        self.entry.is_none()
    }

    #[inline]
    pub fn get_handle(&self) -> svc::Handle {
        self.handle
    }

    pub fn get_priority(&self) -> Result<i32> {
        result_return_unless!(self.state != ThreadState::NotInitialized, rc::ResultInvalidState);

        svc::get_thread_priority(self.handle)
    }

    pub fn get_id(&self) -> Result<u64> {
        result_return_unless!(self.state != ThreadState::NotInitialized, rc::ResultInvalidState);
        
        svc::get_thread_id(self.handle)
    } 
}

impl Drop for Thread {
    fn drop(&mut self) {
        // If it's still active, finalize it
        if self.state == ThreadState::Started {
            let _ = self.join();
        }

        if self.owns_stack {
            alloc::release(self.stack, alloc::PAGE_ALIGNMENT, self.stack_size);
        }

        // If a thread is not created (like the main thread) the entry field will have nothing, and we definitely should not close threads we did not create...
        if !self.is_remote() {
            let _ = svc::close_handle(self.handle);
        }

        self.state = ThreadState::NotInitialized;
    }
}

// Note: https://switchbrew.org/wiki/Thread_Local_Region

#[derive(Copy, Clone)]
#[repr(C)]
pub struct ThreadLocalRegion {
    pub msg_buffer: [u8; 0x100],
    pub disable_counter: u16,
    pub interrupt_flag: u16,
    pub reserved_1: [u8; 0x4],
    pub reserved_2: [u8; 0x78],
    pub tls: [u8; 0x50],
    pub locale_ptr: *mut u8,
    pub errno_val: i64,
    pub thread_data: [u8; 0x8],
    pub eh_globals: [u8; 0x8],
    pub thread_ptr: *mut u8,
    pub thread_ref: *mut Thread,
}
const_assert!(core::mem::size_of::<ThreadLocalRegion>() == 0x200);

#[inline(always)]
pub fn get_thread_local_region() -> *mut ThreadLocalRegion {
    let tlr: *mut ThreadLocalRegion;
    unsafe {
        asm!(
            "mrs {}, tpidrro_el0",
            out(reg) tlr
        );
    }
    tlr
}

pub fn set_current_thread(thread_ref: *mut Thread) {
    unsafe {
        (*thread_ref).self_ref = thread_ref;
        (*thread_ref).name_addr = &mut (*thread_ref).name as *mut _ as *mut u8;

        let tlr = get_thread_local_region();
        (*tlr).thread_ref = thread_ref;
    }
}

pub fn get_current_thread() -> &'static mut Thread {
    unsafe {
        let tlr = get_thread_local_region();
        &mut *(*tlr).thread_ref
    }
}

pub fn sleep(timeout: i64) -> Result<()> {
    svc::sleep_thread(timeout)
}

pub fn exit() -> ! {
    svc::exit_thread()
}