summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/thread.cpp
blob: b6d02aa12023629e2e3a0f12ee9b3796cbb2f1fd (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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// 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 "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"

struct ThreadQueueList {
    // Number of queues (number of priority levels starting at 0.)
    static const int NUM_QUEUES = 128;
    // Initial number of threads a single queue can handle.
    static const int INITIAL_CAPACITY = 32;

    struct Queue {
        // Next ever-been-used queue (worse priority.)
        Queue *next;
        // First valid item in data.
        int first;
        // One after last valid item in data.
        int end;
        // A too-large array with room on the front and end.
        UID *data;
        // Size of data array.
        int capacity;
    };

    ThreadQueueList() {
        memset(queues, 0, sizeof(queues));
        first = invalid();
    }

    ~ThreadQueueList() {
        for (int i = 0; i < NUM_QUEUES; ++i) {
            if (queues[i].data != NULL) {
                free(queues[i].data);
            }
        }
    }

    // Only for debugging, returns priority level.
    int contains(const UID uid) {
        for (int i = 0; i < NUM_QUEUES; ++i) {
            if (queues[i].data == NULL) {
                continue;
            }
            Queue *cur = &queues[i];
            for (int j = cur->first; j < cur->end; ++j) {
                if (cur->data[j] == uid) {
                    return i;
                }
            }
        }
        return -1;
    }

    inline UID pop_first() {
        Queue *cur = first;
        while (cur != invalid()) {
            if (cur->end - cur->first > 0) {
                return cur->data[cur->first++];
            }
            cur = cur->next;
        }

        _dbg_assert_msg_(KERNEL, false, "ThreadQueueList should not be empty.");
        return 0;
    }

    inline UID pop_first_better(u32 priority) {
        Queue *cur = first;
        Queue *stop = &queues[priority];
        while (cur < stop) {
            if (cur->end - cur->first > 0) {
                return cur->data[cur->first++];
            }
            cur = cur->next;
        }
        return 0;
    }

    inline void push_front(u32 priority, const UID thread_id) {
        Queue *cur = &queues[priority];
        cur->data[--cur->first] = thread_id;
        if (cur->first == 0) {
            rebalance(priority);
        }
    }

    inline void push_back(u32 priority, const UID thread_id)
    {
        Queue *cur = &queues[priority];
        cur->data[cur->end++] = thread_id;
        if (cur->end == cur->capacity) {
            rebalance(priority);
        }
    }

    inline void remove(u32 priority, const UID thread_id) {
        Queue *cur = &queues[priority];
        _dbg_assert_msg_(KERNEL, cur->next != NULL, "ThreadQueueList::Queue should already be linked up.");

        for (int i = cur->first; i < cur->end; ++i) {
            if (cur->data[i] == thread_id) {
                int remaining = --cur->end - i;
                if (remaining > 0) {
                    memmove(&cur->data[i], &cur->data[i + 1], remaining * sizeof(UID));
                }
                return;
            }
        }

        // Wasn't there.
    }

    inline void rotate(u32 priority) {
        Queue *cur = &queues[priority];
        _dbg_assert_msg_(KERNEL, cur->next != NULL, "ThreadQueueList::Queue should already be linked up.");

        if (cur->end - cur->first > 1) {
            cur->data[cur->end++] = cur->data[cur->first++];
            if (cur->end == cur->capacity) {
                rebalance(priority);
            }
        }
    }

    inline void clear() {
        for (int i = 0; i < NUM_QUEUES; ++i) {
            if (queues[i].data != NULL) {
                free(queues[i].data);
            }
        }
        memset(queues, 0, sizeof(queues));
        first = invalid();
    }

    inline bool empty(u32 priority) const {
        const Queue *cur = &queues[priority];
        return cur->first == cur->end;
    }

    inline void prepare(u32 priority) {
        Queue *cur = &queues[priority];
        if (cur->next == NULL) {
            link(priority, INITIAL_CAPACITY);
        }
    }

private:
    Queue *invalid() const {
        return (Queue *)-1;
    }

    void link(u32 priority, int size) {
        _dbg_assert_msg_(KERNEL, queues[priority].data == NULL, "ThreadQueueList::Queue should only be initialized once.");

        if (size <= INITIAL_CAPACITY) {
            size = INITIAL_CAPACITY;
        } else {
            int goal = size;
            size = INITIAL_CAPACITY;
            while (size < goal)
                size *= 2;
        }
        Queue *cur = &queues[priority];
        cur->data = (UID*)malloc(sizeof(UID)* size);
        cur->capacity = size;
        cur->first = size / 2;
        cur->end = size / 2;

        for (int i = (int)priority - 1; i >= 0; --i) {
            if (queues[i].next != NULL) {
                cur->next = queues[i].next;
                queues[i].next = cur;
                return;
            }
        }

        cur->next = first;
        first = cur;
    }

    void rebalance(u32 priority) {
        Queue *cur = &queues[priority];
        int size = cur->end - cur->first;
        if (size >= cur->capacity - 2) {
            UID* new_data = (UID*)realloc(cur->data, cur->capacity * 2 * sizeof(UID));
            if (new_data != NULL) {
                cur->capacity *= 2;
                cur->data = new_data;
            }
        }

        int newFirst = (cur->capacity - size) / 2;
        if (newFirst != cur->first) {
            memmove(&cur->data[newFirst], &cur->data[cur->first], size * sizeof(UID));
            cur->first = newFirst;
            cur->end = newFirst + size;
        }
    }

    // The first queue that's ever been used.
    Queue* first;
    // The priority level queues of thread ids.
    Queue queues[NUM_QUEUES];
};

// Supposed to represent a real CTR struct... but not sure of the correct fields yet.
struct NativeThread {
    //u32         Pointer to vtable
    //u32         Reference count
    //KProcess*   Process the thread belongs to (virtual address)
    //u32         Thread id
    //u32*        ptr = *(KThread+0x8C) - 0xB0
    //u32*        End-address of the page for this thread allocated in the 0xFF4XX000 region. Thus, 
    //      if the beginning of this mapped page is 0xFF401000, this ptr would be 0xFF402000.
    //KThread*    Previous ? (virtual address)
    //KThread*    Next ? (virtual address)

    u32_le native_size;
    char name[KERNELOBJECT_MAX_NAME_LENGTH + 1];

    // Threading stuff
    u32_le status;
    u32_le entry_point;
    u32_le initial_stack;
    u32_le stack_top;
    u32_le stack_size;
    
    u32_le arg;
    u32_le processor_id;

    s32_le initial_priority;
    s32_le current_priority;
};

struct ThreadWaitInfo {
    u32 wait_value;
    u32 timeout_ptr;
};

class Thread : public KernelObject {
public:
    /*const char *GetName() { return nt.name; }*/
    const char *GetTypeName() { return "Thread"; }
    //void GetQuickInfo(char *ptr, int size)
    //{
    //    sprintf(ptr, "pc= %08x sp= %08x %s %s %s %s %s %s (wt=%i wid=%i wv= %08x )",
    //        context.pc, context.r[13], // 13 is stack pointer
    //        (nt.status & THREADSTATUS_RUNNING) ? "RUN" : "",
    //        (nt.status & THREADSTATUS_READY) ? "READY" : "",
    //        (nt.status & THREADSTATUS_WAIT) ? "WAIT" : "",
    //        (nt.status & THREADSTATUS_SUSPEND) ? "SUSPEND" : "",
    //        (nt.status & THREADSTATUS_DORMANT) ? "DORMANT" : "",
    //        (nt.status & THREADSTATUS_DEAD) ? "DEAD" : "",
    //        nt.waitType,
    //        nt.waitID,
    //        waitInfo.waitValue);
    //}

    //static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_THID; }
    static KernelIDType GetStaticIDType() { return KERNEL_ID_TYPE_THREAD; }
    KernelIDType GetIDType() const { return KERNEL_ID_TYPE_THREAD; }

    bool SetupStack(u32 stack_top, int stack_size) {
        current_stack.start = stack_top; 
        nt.initial_stack = current_stack.start;
        nt.stack_size = stack_size;
        return true;
    }

    //bool FillStack() {
    //    // Fill the stack.
    //    if ((nt.attr & PSP_THREAD_ATTR_NO_FILLSTACK) == 0) {
    //        Memory::Memset(current_stack.start, 0xFF, nt.stack_size);
    //    }
    //    context.r[MIPS_REG_SP] = current_stack.start + nt.stack_size;
    //    current_stack.end = context.r[MIPS_REG_SP];
    //    // The k0 section is 256 bytes at the top of the stack.
    //    context.r[MIPS_REG_SP] -= 256;
    //    context.r[MIPS_REG_K0] = context.r[MIPS_REG_SP];
    //    u32 k0 = context.r[MIPS_REG_K0];
    //    Memory::Memset(k0, 0, 0x100);
    //    Memory::Write_U32(GetUID(), k0 + 0xc0);
    //    Memory::Write_U32(nt.initialStack, k0 + 0xc8);
    //    Memory::Write_U32(0xffffffff, k0 + 0xf8);
    //    Memory::Write_U32(0xffffffff, k0 + 0xfc);
    //    // After k0 comes the arguments, which is done by sceKernelStartThread().

    //    Memory::Write_U32(GetUID(), nt.initialStack);
    //    return true;
    //}

    //void FreeStack() {
    //    if (current_stack.start != 0) {
    //        DEBUG_LOG(KERNEL, "Freeing thread stack %s", nt.name);

    //        if ((nt.attr & PSP_THREAD_ATTR_CLEAR_STACK) != 0 && nt.initialStack != 0) {
    //            Memory::Memset(nt.initialStack, 0, nt.stack_size);
    //        }

    //        if (nt.attr & PSP_THREAD_ATTR_KERNEL) {
    //            kernelMemory.Free(current_stack.start);
    //        }
    //        else {
    //            userMemory.Free(current_stack.start);
    //        }
    //        current_stack.start = 0;
    //    }
    //}

    //bool PushExtendedStack(u32 size) {
    //    u32 stack = userMemory.Alloc(size, true, (std::string("extended/") + nt.name).c_str());
    //    if (stack == (u32)-1)
    //        return false;

    //    pushed_stacks.push_back(current_stack);
    //    current_stack.start = stack;
    //    current_stack.end = stack + size;
    //    nt.initialStack = current_stack.start;
    //    nt.stack_size = current_stack.end - current_stack.start;

    //    // We still drop the thread_id at the bottom and fill it, but there's no k0.
    //    Memory::Memset(current_stack.start, 0xFF, nt.stack_size);
    //    Memory::Write_U32(GetUID(), nt.initialStack);
    //    return true;
    //}

    //bool PopExtendedStack() {
    //    if (pushed_stacks.size() == 0) {
    //        return false;
    //    }
    //    userMemory.Free(current_stack.start);
    //    current_stack = pushed_stacks.back();
    //    pushed_stacks.pop_back();
    //    nt.initialStack = current_stack.start;
    //    nt.stack_size = current_stack.end - current_stack.start;
    //    return true;
    //}

    Thread() {
        current_stack.start = 0;
    }

    // Can't use a destructor since savestates will call that too.
    //void Cleanup() {
    //    // Callbacks are automatically deleted when their owning thread is deleted.
    //    for (auto it = callbacks.begin(), end = callbacks.end(); it != end; ++it)
    //        g_kernel_objects.Destroy<Callback>(*it);

    //    if (pushed_stacks.size() != 0)
    //    {
    //        WARN_LOG(KERNEL, "Thread ended within an extended stack");
    //        for (size_t i = 0; i < pushed_stacks.size(); ++i)
    //            userMemory.Free(pushed_stacks[i].start);
    //    }
    //    FreeStack();
    //}

    void setReturnValue(u32 retval);
    void setReturnValue(u64 retval);
    void resumeFromWait();
    //bool isWaitingFor(WaitType type, int id);
    //int getWaitID(WaitType type);
    ThreadWaitInfo getWaitInfo();

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

    NativeThread nt;

    ThreadWaitInfo waitInfo;
    UID moduleId;

    //bool isProcessingCallbacks;
    //u32 currentMipscallId;
    //UID currentCallbackId;

    ThreadContext context;

    std::vector<UID> callbacks;

    std::list<u32> pending_calls;

    struct StackInfo {
        u32 start;
        u32 end;
    };
    // This is a stack of... stacks, since sceKernelExtendThreadStack() can recurse.
    // These are stacks that aren't "active" right now, but will pop off once the func returns.
    std::vector<StackInfo> pushed_stacks;

    StackInfo current_stack;

    // For thread end.
    std::vector<UID> waiting_threads;
    // Key is the callback id it was for, or if no callback, the thread id.
    std::map<UID, u64> paused_waits;
};

void ThreadContext::reset() {
    for (int i = 0; i < 16; i++) {
        reg[i] = 0;
    }
    cpsr = 0;
}

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

// Lists only ready thread ids
ThreadQueueList g_thread_ready_queue;

UID         g_current_thread            = 0;
Thread*     g_current_thread_ptr        = NULL;
const char* g_hle_current_thread_name   = NULL;

/// Creates a new thread
Thread* __KernelCreateThread(UID& id, UID module_id, const char* name, u32 priority, 
    u32 entry_point, u32 arg, u32 stack_top, u32 processor_id, int stack_size) {

    Thread *t = new Thread;
    id = g_kernel_objects.Create(t);

    g_thread_queue.push_back(id);
    g_thread_ready_queue.prepare(priority);

    memset(&t->nt, 0xCD, sizeof(t->nt));

    t->nt.entry_point = entry_point;
    t->nt.native_size = sizeof(t->nt);
    t->nt.initial_priority = t->nt.current_priority = priority;
    t->nt.status = THREADSTATUS_DORMANT;
    t->nt.initial_stack = t->nt.stack_top = stack_top;
    t->nt.stack_size = stack_size;
    t->nt.processor_id = processor_id;

    strncpy(t->nt.name, name, KERNELOBJECT_MAX_NAME_LENGTH);
    t->nt.name[KERNELOBJECT_MAX_NAME_LENGTH] = '\0';

    t->nt.stack_size = stack_size;
    t->SetupStack(stack_top, stack_size);

    return t;
}

UID __KernelCreateThread(UID module_id, const char* name, u32 priority, u32 entry_point, u32 arg, 
    u32 stack_top, u32 processor_id, int stack_size) {
    UID id;
    __KernelCreateThread(id, module_id, name, priority, entry_point, arg, stack_top, processor_id, 
        stack_size);
    
    HLE::EatCycles(32000);
    HLE::ReSchedule("thread created");
    
    return id;
}

/// Resets the specified thread back to initial calling state
void __KernelResetThread(Thread *t, int lowest_priority) {
    t->context.reset();
    t->context.pc = t->nt.entry_point;
    t->context.reg[13] = t->nt.initial_stack;

    // If the thread would be better than lowestPriority, reset to its initial.  Yes, kinda odd...
    if (t->nt.current_priority < lowest_priority) {
        t->nt.current_priority = t->nt.initial_priority;
    }

    memset(&t->waitInfo, 0, sizeof(t->waitInfo));
}

/// Returns the current executing thread
inline Thread *__GetCurrentThread() {
    return g_current_thread_ptr;
}

/// Sets the current executing thread
inline void __SetCurrentThread(Thread *thread, UID thread_id, const char *name) {
    g_current_thread = thread_id;
    g_current_thread_ptr = thread;
    g_hle_current_thread_name = name;
}

// TODO: Use __KernelChangeThreadState instead?  It has other affects...
void __KernelChangeReadyState(Thread *thread, UID thread_id, bool ready) {
    // Passing the id as a parameter is just an optimization, if it's wrong it will cause havoc.
    _dbg_assert_msg_(KERNEL, thread->GetUID() == thread_id, "Incorrect thread_id");
    int prio = thread->nt.current_priority;

    if (thread->IsReady()) {
        if (!ready)
            g_thread_ready_queue.remove(prio, thread_id);
    } else if (ready) {
        if (thread->IsRunning()) {
            g_thread_ready_queue.push_front(prio, thread_id);
        } else {
            g_thread_ready_queue.push_back(prio, thread_id);
        }
        thread->nt.status = THREADSTATUS_READY;
    }
}

void __KernelChangeReadyState(UID thread_id, bool ready) {
    u32 error;
    Thread *thread = g_kernel_objects.Get<Thread>(thread_id, error);
    if (thread) {
        __KernelChangeReadyState(thread, thread_id, ready);
    } else {
        WARN_LOG(KERNEL, "Trying to change the ready state of an unknown thread?");
    }
}

/// Returns NULL if the current thread is fine.
Thread* __KernelNextThread() {
    UID best_thread;

    // If the current thread is running, it's a valid candidate.
    Thread *cur = __GetCurrentThread();
    if (cur && cur->IsRunning()) {
        best_thread = g_thread_ready_queue.pop_first_better(cur->nt.current_priority);
        if (best_thread != 0) {
            __KernelChangeReadyState(cur, g_current_thread, true);
        }
    } else {
        best_thread = g_thread_ready_queue.pop_first();
    }
    // Assume g_thread_ready_queue has not become corrupt.
    if (best_thread != 0) {
        return g_kernel_objects.GetFast<Thread>(best_thread);
    } else {
        return NULL;
    }
}

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

/// Loads a CPU context
void __KernelLoadContext(ThreadContext *ctx) {
    Core::g_app_core->SetReg(0, ctx->reg[0]);
    Core::g_app_core->SetReg(1, ctx->reg[1]);
    Core::g_app_core->SetReg(2, ctx->reg[2]);
    Core::g_app_core->SetReg(3, ctx->reg[3]);
    Core::g_app_core->SetReg(4, ctx->reg[4]);
    Core::g_app_core->SetReg(5, ctx->reg[5]);
    Core::g_app_core->SetReg(6, ctx->reg[6]);
    Core::g_app_core->SetReg(7, ctx->reg[7]);
    Core::g_app_core->SetReg(8, ctx->reg[8]);
    Core::g_app_core->SetReg(9, ctx->reg[9]);
    Core::g_app_core->SetReg(10, ctx->reg[10]);
    Core::g_app_core->SetReg(11, ctx->reg[11]);
    Core::g_app_core->SetReg(12, ctx->reg[12]);
    Core::g_app_core->SetReg(13, ctx->reg[13]);
    Core::g_app_core->SetReg(14, ctx->reg[14]);
    Core::g_app_core->SetReg(15, ctx->reg[15]);
    Core::g_app_core->SetPC(ctx->pc);
    Core::g_app_core->SetCPSR(ctx->cpsr);
}

/// Switches thread context
void __KernelSwitchContext(Thread *target, const char *reason) {
    u32 old_pc = 0;
    UID old_uid = 0;
    const char *old_name = g_hle_current_thread_name != NULL ? g_hle_current_thread_name : "(none)";
    Thread *cur = __GetCurrentThread();

    if (cur) { // It might just have been deleted.
        __KernelSaveContext(&cur->context);
        old_pc = Core::g_app_core->GetPC();
        old_uid = cur->GetUID();

        // Normally this is taken care of in __KernelNextThread().
        if (cur->IsRunning())
            __KernelChangeReadyState(cur, old_uid, true);
    }
    if (target) {
        __SetCurrentThread(target, target->GetUID(), target->nt.name);
        __KernelChangeReadyState(target, g_current_thread, false);

        target->nt.status = (target->nt.status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;

        __KernelLoadContext(&target->context);
    } else {
        __SetCurrentThread(NULL, 0, NULL);
    }
}

bool __KernelSwitchToThread(UID thread_id, const char *reason) {
    if (!reason) {
        reason = "switch to thread";
    }
    if (g_current_thread == thread_id) {
        return false;
    }
    u32 error;
    Thread *t = g_kernel_objects.Get<Thread>(thread_id, error);
    if (!t) {
        ERROR_LOG(KERNEL, "__KernelSwitchToThread: %x doesn't exist", thread_id);
        HLE::ReSchedule("switch to deleted thread");
    } else if (t->IsReady() || t->IsRunning()) {
        Thread *current = __GetCurrentThread();
        if (current && current->IsRunning()) {
            __KernelChangeReadyState(current, g_current_thread, true);
        }
        __KernelSwitchContext(t, reason);
        return true;
    } else {
        HLE::ReSchedule("switch to waiting thread");
    }
    return false;
}

/// Sets up the root (primary) thread of execution
UID __KernelSetupRootThread(UID module_id, int arg, int prio, int stack_size) {
    UID id;

    Thread *thread = __KernelCreateThread(id, module_id, "root", prio, Core::g_app_core->GetPC(),
        arg, Memory::SCRATCHPAD_VADDR_END, 0xFFFFFFFE, stack_size=stack_size);

    if (thread->current_stack.start == 0) {
        ERROR_LOG(KERNEL, "Unable to allocate stack for root thread.");
    }
    __KernelResetThread(thread, 0);

    Thread *prev_thread = __GetCurrentThread();
    if (prev_thread && prev_thread->IsRunning())
        __KernelChangeReadyState(g_current_thread, true);
    __SetCurrentThread(thread, id, "root");
    thread->nt.status = THREADSTATUS_RUNNING; // do not schedule

    strcpy(thread->nt.name, "root");

    __KernelLoadContext(&thread->context);
    
    // NOTE(bunnei): Not sure this is really correct, ignore args for now...
    //Core::g_app_core->SetReg(0, args); 
    //Core::g_app_core->SetReg(13, (args + 0xf) & ~0xf); // Setup SP - probably not correct
    //u32 location = Core::g_app_core->GetReg(13); // SP 
    //Core::g_app_core->SetReg(1, location);
    
    //if (argp)
    //    Memory::Memcpy(location, argp, args);
    //// Let's assume same as starting a new thread, 64 bytes for safety/kernel.
    //Core::g_app_core->SetReg(13, Core::g_app_core->GetReg(13) - 64);

    return id;
}

int __KernelRotateThreadReadyQueue(int priority) {
    Thread *cur = __GetCurrentThread();

    // 0 is special, it means "my current priority."
    if (priority == 0) {
        priority = cur->nt.current_priority;
    }
    //if (priority <= 0x07 || priority > 0x77)
    //    return SCE_KERNEL_ERROR_ILLEGAL_PRIORITY;

    if (!g_thread_ready_queue.empty(priority)) {
        // In other words, yield to everyone else.
        if (cur->nt.current_priority == priority) {
            g_thread_ready_queue.push_back(priority, g_current_thread);
            cur->nt.status = (cur->nt.status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;

        // Yield the next thread of this priority to all other threads of same priority.
        } else {
            g_thread_ready_queue.rotate(priority);
        }
    }
    HLE::EatCycles(250);
    HLE::ReSchedule("rotatethreadreadyqueue");

    return 0;
}

void __KernelThreadingInit() {
}

void __KernelThreadingShutdown() {
}