summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/thread.cpp
blob: cfc5327a3e0d704045591d478e0de68d976e471d (plain) (blame)
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
// Copyright 2014 Citra Emulator Project / PPSSPP Project
// Licensed under GPLv2
// Refer to the license.txt file included.  

#include <stdio.h>

#include <list>
#include <vector>
#include <map>
#include <string>

#include "common/common.h"
#include "common/thread_queue_list.h"

#include "core/core.h"
#include "core/mem_map.h"
#include "core/hle/hle.h"
#include "core/hle/syscall.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/thread.h"

// Enums

enum ThreadStatus {
    THREADSTATUS_RUNNING        = 1,
    THREADSTATUS_READY          = 2,
    THREADSTATUS_WAIT           = 4,
    THREADSTATUS_SUSPEND        = 8,
    THREADSTATUS_DORMANT        = 16,
    THREADSTATUS_DEAD           = 32,
    THREADSTATUS_WAITSUSPEND    = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
};

enum WaitType {
    WAITTYPE_NONE,
    WAITTYPE_SLEEP,
    WAITTYPE_SEMA,
    WAITTYPE_EVENTFLAG,
    WAITTYPE_THREADEND,
    WAITTYPE_VBLANK,
    WAITTYPE_MUTEX,
    WAITTYPE_SYNCH,
};

class Thread : public Kernel::Object {
public:

    const char* GetName() { return name; }
    const char* GetTypeName() { return "Thread"; }

    static Kernel::HandleType GetStaticHandleType() {  return Kernel::HandleType::Thread; }
    Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Thread; }

    inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
    inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
    inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
    inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
    inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }

    ThreadContext context;

    u32 status;
    u32 entry_point;
    u32 stack_top;
    u32 stack_size;

    s32 initial_priority;
    s32 current_priority;

    s32 processor_id;

    WaitType wait_type;

    char name[Kernel::MAX_NAME_LENGTH + 1];
};

// Lists all thread ids that aren't deleted/etc.
std::vector<Handle> g_thread_queue;

// Lists only ready thread ids.
Common::ThreadQueueList<Handle> g_thread_ready_queue;

Handle g_current_thread_handle;

Thread* g_current_thread;


inline Thread* __GetCurrentThread() {
    return g_current_thread;
}

inline void __SetCurrentThread(Thread* t) {
    g_current_thread = t;
    g_current_thread_handle = t->GetHandle();
}

////////////////////////////////////////////////////////////////////////////////////////////////////

/// Saves the current CPU context
void __KernelSaveContext(ThreadContext& ctx) {
    ctx.cpu_registers[0] = Core::g_app_core->GetReg(0);
    ctx.cpu_registers[1] = Core::g_app_core->GetReg(1);
    ctx.cpu_registers[2] = Core::g_app_core->GetReg(2);
    ctx.cpu_registers[3] = Core::g_app_core->GetReg(3);
    ctx.cpu_registers[4] = Core::g_app_core->GetReg(4);
    ctx.cpu_registers[5] = Core::g_app_core->GetReg(5);
    ctx.cpu_registers[6] = Core::g_app_core->GetReg(6);
    ctx.cpu_registers[7] = Core::g_app_core->GetReg(7);
    ctx.cpu_registers[8] = Core::g_app_core->GetReg(8);
    ctx.cpu_registers[9] = Core::g_app_core->GetReg(9);
    ctx.cpu_registers[10] = Core::g_app_core->GetReg(10);
    ctx.cpu_registers[11] = Core::g_app_core->GetReg(11);
    ctx.cpu_registers[12] = Core::g_app_core->GetReg(12);
    ctx.sp = Core::g_app_core->GetReg(13);
    ctx.lr = Core::g_app_core->GetReg(14);
    ctx.pc = Core::g_app_core->GetPC();
    ctx.cpsr = Core::g_app_core->GetCPSR();
}

/// Loads a CPU context
void __KernelLoadContext(const ThreadContext& ctx) {
    Core::g_app_core->SetReg(0, ctx.cpu_registers[0]);
    Core::g_app_core->SetReg(1, ctx.cpu_registers[1]);
    Core::g_app_core->SetReg(2, ctx.cpu_registers[2]);
    Core::g_app_core->SetReg(3, ctx.cpu_registers[3]);
    Core::g_app_core->SetReg(4, ctx.cpu_registers[4]);
    Core::g_app_core->SetReg(5, ctx.cpu_registers[5]);
    Core::g_app_core->SetReg(6, ctx.cpu_registers[6]);
    Core::g_app_core->SetReg(7, ctx.cpu_registers[7]);
    Core::g_app_core->SetReg(8, ctx.cpu_registers[8]);
    Core::g_app_core->SetReg(9, ctx.cpu_registers[9]);
    Core::g_app_core->SetReg(10, ctx.cpu_registers[10]);
    Core::g_app_core->SetReg(11, ctx.cpu_registers[11]);
    Core::g_app_core->SetReg(12, ctx.cpu_registers[12]);
    Core::g_app_core->SetReg(13, ctx.sp);
    Core::g_app_core->SetReg(14, ctx.lr);
    //Core::g_app_core->SetReg(15, ctx.pc);

    Core::g_app_core->SetPC(ctx.pc);
    Core::g_app_core->SetCPSR(ctx.cpsr);
}

/// Resets a thread
void __KernelResetThread(Thread* t, s32 lowest_priority) {
    memset(&t->context, 0, sizeof(ThreadContext));

    t->context.pc = t->entry_point;
    t->context.sp = t->stack_top;
    
    if (t->current_priority < lowest_priority) {
        t->current_priority = t->initial_priority;
    }
        
    t->wait_type = WAITTYPE_NONE;
}

/// Change a thread to "ready" state
void __KernelChangeReadyState(Thread* t, bool ready) {
    Handle handle = t->GetHandle();
    if (t->IsReady()) {
        if (!ready) {
            g_thread_ready_queue.remove(t->current_priority, handle);
        }
    }  else if (ready) {
        if (t->IsRunning()) {
            g_thread_ready_queue.push_front(t->current_priority, handle);
        } else {
            g_thread_ready_queue.push_back(t->current_priority, handle);
        }
        t->status = THREADSTATUS_READY;
    }
}

/// Changes a threads state
void __KernelChangeThreadState(Thread* t, ThreadStatus new_status) {
    if (!t || t->status == new_status) {
        return;
    }
    __KernelChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
    t->status = new_status;
    
    if (new_status == THREADSTATUS_WAIT) {
        if (t->wait_type == WAITTYPE_NONE) {
            printf("ERROR: Waittype none not allowed here\n");
        }
    }
}

/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
void __KernelCallThread(Thread* t) {
    // Stop waiting
    if (t->wait_type != WAITTYPE_NONE) {
        t->wait_type = WAITTYPE_NONE;
    }
    __KernelChangeThreadState(t, THREADSTATUS_READY);
}

/// Creates a new thread
Thread* __KernelCreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
    s32 processor_id, u32 stack_top, int stack_size) {

    Thread* t = new Thread;
    
    handle = Kernel::g_object_pool.Create(t);
    
    g_thread_queue.push_back(handle);
    g_thread_ready_queue.prepare(priority);
    
    t->status = THREADSTATUS_DORMANT;
    t->entry_point = entry_point;
    t->stack_top = stack_top;
    t->stack_size = stack_size;
    t->initial_priority = t->current_priority = priority;
    t->processor_id = processor_id;
    t->wait_type = WAITTYPE_NONE;
    
    strncpy(t->name, name, Kernel::MAX_NAME_LENGTH);
    t->name[Kernel::MAX_NAME_LENGTH] = '\0';
    
    return t;
}

/// Creates a new thread - wrapper for external user
Handle __KernelCreateThread(const char* name, u32 entry_point, s32 priority, s32 processor_id,
    u32 stack_top, int stack_size) {
    if (name == NULL) {
        ERROR_LOG(KERNEL, "__KernelCreateThread(): NULL name");
        return -1;
    }
    if ((u32)stack_size < 0x200) {
        ERROR_LOG(KERNEL, "__KernelCreateThread(name=%s): invalid stack_size=0x%08X", name, 
            stack_size);
        return -1;
    }
    if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
        s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
        WARN_LOG(KERNEL, "__KernelCreateThread(name=%s): invalid priority=0x%08X, clamping to %08X",
            name, priority, new_priority);
        // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
        // validity of this
        priority = new_priority;
    }
    if (!Memory::GetPointer(entry_point)) {
        ERROR_LOG(KERNEL, "__KernelCreateThread(name=%s): invalid entry %08x", name, entry_point);
        return -1;
    }
    Handle handle;
    Thread* t = __KernelCreateThread(handle, name, entry_point, priority, processor_id, stack_top, 
        stack_size);

    HLE::EatCycles(32000);

    // This won't schedule to the new thread, but it may to one woken from eating cycles.
    // Technically, this should not eat all at once, and reschedule in the middle, but that's hard.
    HLE::ReSchedule("thread created");

    __KernelCallThread(t);
    
    return handle;
}

/// Switches CPU context to that of the specified thread
void __KernelSwitchContext(Thread* t, const char* reason) {
    Thread* cur = __GetCurrentThread();
    
    // Save context for current thread
    if (cur) {
        __KernelSaveContext(cur->context);
        
        if (cur->IsRunning()) {
            __KernelChangeReadyState(cur, true);
        }
    }
    // Load context of new thread
    if (t) {
        __SetCurrentThread(t);
        __KernelChangeReadyState(t, false);
        t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
        t->wait_type = WAITTYPE_NONE;
        __KernelLoadContext(t->context);
    } else {
        __SetCurrentThread(NULL);
    }
}

/// Gets the next thread that is ready to be run by priority
Thread* __KernelNextThread() {
    Handle next;
    Thread* cur = __GetCurrentThread();
    
    if (cur && cur->IsRunning()) {
        next = g_thread_ready_queue.pop_first_better(cur->current_priority);
    } else  {
        next = g_thread_ready_queue.pop_first();
    }
    if (next < 0) {
        return NULL;
    }
    return Kernel::g_object_pool.GetFast<Thread>(next);
}

/// Sets up the primary application thread
Handle __KernelSetupMainThread(s32 priority, int stack_size) {
    Handle handle;
    
    // Initialize new "main" thread
    Thread* t = __KernelCreateThread(handle, "main", Core::g_app_core->GetPC(), priority, 
        THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
    
    __KernelResetThread(t, 0);
    
    // If running another thread already, set it to "ready" state
    Thread* cur = __GetCurrentThread();
    if (cur && cur->IsRunning()) {
        __KernelChangeReadyState(cur, true);
    }
    
    // Run new "main" thread
    __SetCurrentThread(t);
    t->status = THREADSTATUS_RUNNING;
    __KernelLoadContext(t->context);

    return handle;
}

/// Resumes a thread from waiting by marking it as "ready"
void __KernelResumeThreadFromWait(Handle handle) {
    u32 error;
    Thread* t = Kernel::g_object_pool.Get<Thread>(handle, error);
    if (t) {
        t->status &= ~THREADSTATUS_WAIT;
        if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
            __KernelChangeReadyState(t, true);
        }
    }
}

/// Puts a thread in the wait state for the given type/reason
void __KernelWaitCurThread(WaitType wait_type, const char* reason) {
    Thread* t = __GetCurrentThread();
    t->wait_type = wait_type;
    __KernelChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
}

/// Reschedules to the next available thread (call after current thread is suspended)
void __KernelReschedule(const char* reason) {
    Thread* next = __KernelNextThread();
    if (next > 0) {
        __KernelSwitchContext(next, reason);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

/// Put current thread in a wait state - on WaitSynchronization
void __KernelWaitThread_Synchronization() {
    // TODO(bunnei): Just a placeholder function for now... FixMe
    __KernelWaitCurThread(WAITTYPE_SYNCH, "waitSynchronization called");
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void __KernelThreadingInit() {
}

void __KernelThreadingShutdown() {
}