2020-04-19 23:04:05 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-03-27 20:36:02 +01:00
|
|
|
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
|
2020-10-07 17:41:13 +02:00
|
|
|
#include <unistd.h>
|
2020-10-28 17:00:39 +01:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <android/log.h>
|
2020-03-26 15:33:19 +01:00
|
|
|
#include <nce.h>
|
2020-10-07 17:41:13 +02:00
|
|
|
#include <os.h>
|
2019-09-24 22:54:27 +02:00
|
|
|
#include "KProcess.h"
|
|
|
|
|
|
|
|
namespace skyline::kernel::type {
|
2020-10-21 19:09:35 +02:00
|
|
|
KThread::KThread(const DeviceState &state, KHandle handle, KProcess *parent, size_t id, void *entry, u64 argument, void *stackTop, i8 priority, i8 idealCore) : handle(handle), parent(parent), id(id), entry(entry), entryArgument(argument), stackTop(stackTop), idealCore(idealCore), coreId(idealCore), KSyncObject(state, KType::KThread) {
|
|
|
|
affinityMask.set(coreId);
|
2019-09-24 22:54:27 +02:00
|
|
|
UpdatePriority(priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread::~KThread() {
|
2019-11-22 15:59:50 +01:00
|
|
|
Kill();
|
2019-09-24 22:54:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 13:38:27 +02:00
|
|
|
/**
|
|
|
|
* @brief Our delegator for sigaction, we need to do this due to sigchain hooking bionic's sigaction and it intercepting signals before they're passed onto userspace
|
|
|
|
* This not only leads to performance degradation but also requires host TLS to be in the TLS register which we cannot ensure for in-guest signals
|
|
|
|
*/
|
|
|
|
inline void Sigaction(int signal, const struct sigaction &action, struct sigaction *oldAction = nullptr) {
|
|
|
|
static decltype(&sigaction) realSigaction{};
|
|
|
|
if (!realSigaction) {
|
|
|
|
void *libc{dlopen("libc.so", RTLD_LOCAL | RTLD_LAZY)};
|
|
|
|
if (!libc)
|
|
|
|
throw exception("dlopen-ing libc has failed with: {}", dlerror());
|
|
|
|
realSigaction = reinterpret_cast<decltype(&sigaction)>(dlsym(libc, "sigaction"));
|
|
|
|
if (!realSigaction)
|
|
|
|
throw exception("Cannot find 'sigaction' in libc: {}", dlerror());
|
|
|
|
}
|
|
|
|
if (realSigaction(signal, &action, oldAction) < 0)
|
|
|
|
throw exception("sigaction has failed with {}", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:41:13 +02:00
|
|
|
void KThread::StartThread() {
|
|
|
|
pthread_setname_np(pthread_self(), fmt::format("HOS-{}", id).c_str());
|
2020-10-28 17:00:39 +01:00
|
|
|
state.logger->UpdateTag();
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2020-10-07 17:41:13 +02:00
|
|
|
if (!ctx.tpidrroEl0)
|
|
|
|
ctx.tpidrroEl0 = parent->AllocateTlsSlot();
|
2020-10-10 17:53:14 +02:00
|
|
|
|
|
|
|
ctx.state = &state;
|
2020-10-07 17:41:13 +02:00
|
|
|
state.ctx = &ctx;
|
2020-10-10 17:53:14 +02:00
|
|
|
state.thread = shared_from_this();
|
2020-10-07 17:41:13 +02:00
|
|
|
|
|
|
|
struct sigaction sigact{
|
2020-10-10 17:53:14 +02:00
|
|
|
.sa_sigaction = &nce::NCE::SignalHandler,
|
2020-10-07 17:41:13 +02:00
|
|
|
.sa_flags = SA_SIGINFO,
|
|
|
|
};
|
2020-10-17 13:38:27 +02:00
|
|
|
for (int signal : {SIGILL, SIGTRAP, SIGBUS, SIGFPE, SIGSEGV})
|
|
|
|
Sigaction(signal, sigact);
|
2020-10-07 17:41:13 +02:00
|
|
|
|
|
|
|
asm volatile(
|
2020-10-10 17:53:14 +02:00
|
|
|
"MRS X0, TPIDR_EL0\n\t"
|
2020-10-07 17:41:13 +02:00
|
|
|
"MSR TPIDR_EL0, %x0\n\t" // Set TLS to ThreadContext
|
2020-10-13 17:41:49 +02:00
|
|
|
"STR X0, [%x0, #0x2A0]\n\t" // Write ThreadContext::hostTpidrEl0
|
2020-10-07 17:41:13 +02:00
|
|
|
"MOV X0, SP\n\t"
|
2020-10-13 17:41:49 +02:00
|
|
|
"STR X0, [%x0, #0x2A8]\n\t" // Write ThreadContext::hostSp
|
2020-10-10 17:53:14 +02:00
|
|
|
"MOV SP, %x1\n\t" // Replace SP with guest stack
|
|
|
|
"MOV LR, %x2\n\t" // Store entry in Link Register so it is 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
|
2020-10-07 17:41:13 +02:00
|
|
|
"MOV X2, XZR\n\t"
|
|
|
|
"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"
|
|
|
|
:
|
2020-10-17 13:38:27 +02:00
|
|
|
: "r"(&ctx), "r"(stackTop), "r"(entry), "r"(entryArgument), "r"(handle)
|
2020-10-07 17:41:13 +02:00
|
|
|
: "x0", "x1", "lr"
|
|
|
|
);
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::Start(bool self) {
|
|
|
|
if (!running) {
|
|
|
|
running = true;
|
|
|
|
state.logger->Debug("Starting thread #{}", id);
|
|
|
|
if (self)
|
|
|
|
StartThread();
|
|
|
|
else
|
|
|
|
thread.emplace(&KThread::StartThread, this);
|
2019-11-17 21:19:01 +01:00
|
|
|
}
|
2019-09-24 22:54:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 15:59:50 +01:00
|
|
|
void KThread::Kill() {
|
2020-10-07 17:41:13 +02:00
|
|
|
if (running) {
|
|
|
|
running = false;
|
|
|
|
Signal();
|
2019-11-22 15:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:02:27 +02:00
|
|
|
void KThread::UpdatePriority(i8 priority) {
|
2019-09-24 22:54:27 +02:00
|
|
|
this->priority = priority;
|
2020-10-07 17:41:13 +02:00
|
|
|
auto priorityValue{constant::AndroidPriority.Rescale(constant::HosPriority, priority)};
|
2020-03-25 18:59:37 +01:00
|
|
|
|
2020-10-07 17:41:13 +02:00
|
|
|
if (setpriority(PRIO_PROCESS, getpid(), priorityValue) == -1)
|
|
|
|
throw exception("Couldn't set thread priority to {} for #{}", priorityValue, id);
|
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
|
|
|
}
|
2019-09-24 22:54:27 +02:00
|
|
|
}
|