skyline/app/src/main/cpp/skyline/kernel/types/KThread.cpp
PixelyIon fe5061a8e0 Address CR Comments (#132) + Change Core Migration API
This addresses all CR comments including more codebase-wide changes arising from certain review comments like proper usage of its/it's and consistent contraction of it is into it's. 

An overhaul was made to the presentation and formatting of `KThread.h` and `LoadBalance` works has been superseded by `GetOptimalCoreForThread` which can be used alongside `InsertThread` or `MigrateToCore`. It makes the API far more atomic and neater. This was a major point of contention for the design prior, it's simplified some code and potentially improved performance.
2021-03-05 14:55:34 +05:30

230 lines
8.0 KiB
C++

// SPDX-License-Identifier: MPL-2.0
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
#include <cxxabi.h>
#include <unistd.h>
#include <common/signal.h>
#include <nce.h>
#include <os.h>
#include "KProcess.h"
#include "KThread.h"
namespace skyline::kernel::type {
KThread::KThread(const DeviceState &state, KHandle handle, KProcess *parent, size_t id, void *entry, u64 argument, void *stackTop, u8 priority, i8 idealCore) : handle(handle), parent(parent), id(id), entry(entry), entryArgument(argument), stackTop(stackTop), priority(priority), basePriority(priority), idealCore(idealCore), coreId(idealCore), KSyncObject(state, KType::KThread) {
affinityMask.set(coreId);
}
KThread::~KThread() {
Kill(true);
if (thread.joinable())
thread.join();
if (preemptionTimer)
timer_delete(preemptionTimer);
}
void KThread::StartThread() {
std::array<char, 16> threadName;
pthread_getname_np(pthread, threadName.data(), threadName.size());
pthread_setname_np(pthread, fmt::format("HOS-{}", id).c_str());
state.logger->UpdateTag();
if (!ctx.tpidrroEl0)
ctx.tpidrroEl0 = parent->AllocateTlsSlot();
ctx.state = &state;
state.ctx = &ctx;
state.thread = shared_from_this();
if (setjmp(originalCtx)) { // Returns 1 if it's returning from guest, 0 otherwise
state.scheduler->RemoveThread();
{
std::lock_guard lock(statusMutex);
running = false;
ready = false;
statusCondition.notify_all();
}
Signal();
if (threadName[0] != 'H' || threadName[1] != 'O' || threadName[2] != 'S' || threadName[3] != '-') {
pthread_setname_np(pthread, threadName.data());
state.logger->UpdateTag();
}
return;
}
struct sigevent event{
.sigev_signo = Scheduler::YieldSignal,
.sigev_notify = SIGEV_THREAD_ID,
.sigev_notify_thread_id = gettid(),
};
if (timer_create(CLOCK_THREAD_CPUTIME_ID, &event, &preemptionTimer))
throw exception("timer_create has failed with '{}'", strerror(errno));
signal::SetSignalHandler({SIGINT, SIGILL, SIGTRAP, SIGBUS, SIGFPE, SIGSEGV}, nce::NCE::SignalHandler);
signal::SetSignalHandler({Scheduler::YieldSignal}, Scheduler::SignalHandler, false); // We want futexes to fail and their predicates rechecked
{
std::lock_guard lock(statusMutex);
ready = true;
statusCondition.notify_all();
}
try {
if (!Scheduler::YieldPending)
state.scheduler->WaitSchedule();
while (Scheduler::YieldPending) {
// If there is a yield pending on us after thread creation
state.scheduler->Rotate();
Scheduler::YieldPending = false;
state.scheduler->WaitSchedule();
}
asm volatile(
"MRS X0, TPIDR_EL0\n\t"
"MSR TPIDR_EL0, %x0\n\t" // Set TLS to ThreadContext
"STR X0, [%x0, #0x2A0]\n\t" // Write ThreadContext::hostTpidrEl0
"MOV X0, SP\n\t"
"STR X0, [%x0, #0x2A8]\n\t" // Write ThreadContext::hostSp
"MOV SP, %x1\n\t" // Replace SP with guest stack
"MOV LR, %x2\n\t" // Store entry in Link Register so it's jumped to on return
"MOV X0, %x3\n\t" // Store the argument in X0
"MOV X1, %x4\n\t" // Store the thread handle in X1, NCA applications require this
"MOV X2, XZR\n\t" // Zero out other GP and SIMD registers, not doing this will break applications
"MOV X3, XZR\n\t"
"MOV X4, XZR\n\t"
"MOV X5, XZR\n\t"
"MOV X6, XZR\n\t"
"MOV X7, XZR\n\t"
"MOV X8, XZR\n\t"
"MOV X9, XZR\n\t"
"MOV X10, XZR\n\t"
"MOV X11, XZR\n\t"
"MOV X12, XZR\n\t"
"MOV X13, XZR\n\t"
"MOV X14, XZR\n\t"
"MOV X15, XZR\n\t"
"MOV X16, XZR\n\t"
"MOV X17, XZR\n\t"
"MOV X18, XZR\n\t"
"MOV X19, XZR\n\t"
"MOV X20, XZR\n\t"
"MOV X21, XZR\n\t"
"MOV X22, XZR\n\t"
"MOV X23, XZR\n\t"
"MOV X24, XZR\n\t"
"MOV X25, XZR\n\t"
"MOV X26, XZR\n\t"
"MOV X27, XZR\n\t"
"MOV X28, XZR\n\t"
"MOV X29, XZR\n\t"
"MSR FPSR, XZR\n\t"
"MSR FPCR, XZR\n\t"
"DUP V0.16B, WZR\n\t"
"DUP V1.16B, WZR\n\t"
"DUP V2.16B, WZR\n\t"
"DUP V3.16B, WZR\n\t"
"DUP V4.16B, WZR\n\t"
"DUP V5.16B, WZR\n\t"
"DUP V6.16B, WZR\n\t"
"DUP V7.16B, WZR\n\t"
"DUP V8.16B, WZR\n\t"
"DUP V9.16B, WZR\n\t"
"DUP V10.16B, WZR\n\t"
"DUP V11.16B, WZR\n\t"
"DUP V12.16B, WZR\n\t"
"DUP V13.16B, WZR\n\t"
"DUP V14.16B, WZR\n\t"
"DUP V15.16B, WZR\n\t"
"DUP V16.16B, WZR\n\t"
"DUP V17.16B, WZR\n\t"
"DUP V18.16B, WZR\n\t"
"DUP V19.16B, WZR\n\t"
"DUP V20.16B, WZR\n\t"
"DUP V21.16B, WZR\n\t"
"DUP V22.16B, WZR\n\t"
"DUP V23.16B, WZR\n\t"
"DUP V24.16B, WZR\n\t"
"DUP V25.16B, WZR\n\t"
"DUP V26.16B, WZR\n\t"
"DUP V27.16B, WZR\n\t"
"DUP V28.16B, WZR\n\t"
"DUP V29.16B, WZR\n\t"
"DUP V30.16B, WZR\n\t"
"DUP V31.16B, WZR\n\t"
"RET"
:
: "r"(&ctx), "r"(stackTop), "r"(entry), "r"(entryArgument), "r"(handle)
: "x0", "x1", "lr"
);
__builtin_unreachable();
} catch (const std::exception &e) {
state.logger->Error(e.what());
if (id) {
signal::BlockSignal({SIGINT});
state.process->Kill(false);
}
abi::__cxa_end_catch();
std::longjmp(originalCtx, true);
} catch (const signal::SignalException &e) {
if (e.signal != SIGINT) {
state.logger->Error(e.what());
if (id) {
signal::BlockSignal({SIGINT});
state.process->Kill(false);
}
}
abi::__cxa_end_catch();
std::longjmp(originalCtx, true);
}
}
void KThread::Start(bool self) {
std::unique_lock lock(statusMutex);
if (!running) {
{
std::lock_guard migrationLock(coreMigrationMutex);
auto thisShared{shared_from_this()};
coreId = state.scheduler->GetOptimalCoreForThread(thisShared).id;
state.scheduler->InsertThread(thisShared);
}
running = true;
killed = false;
statusCondition.notify_all();
if (self) {
pthread = pthread_self();
lock.unlock();
StartThread();
} else {
thread = std::thread(&KThread::StartThread, this);
pthread = thread.native_handle();
}
}
}
void KThread::Kill(bool join) {
std::unique_lock lock(statusMutex);
if (!killed && running) {
statusCondition.wait(lock, [this]() { return ready || killed; });
if (!killed) {
pthread_kill(pthread, SIGINT);
killed = true;
statusCondition.notify_all();
}
}
if (join)
statusCondition.wait(lock, [this]() { return !running; });
}
void KThread::SendSignal(int signal) {
std::unique_lock lock(statusMutex);
statusCondition.wait(lock, [this]() { return ready || killed; });
if (!killed && running)
pthread_kill(pthread, signal);
}
}