2019-09-24 22:54:27 +02:00
|
|
|
#include "os.h"
|
|
|
|
#include "kernel/svc.h"
|
|
|
|
#include "loader/nro.h"
|
|
|
|
|
|
|
|
namespace skyline::kernel {
|
|
|
|
OS::OS(std::shared_ptr<Logger> &logger, std::shared_ptr<Settings> &settings) : state(this, thisProcess, thisThread, std::make_shared<NCE>(), settings, logger), serviceManager(state) {}
|
|
|
|
|
|
|
|
void OS::Execute(const std::string &romFile) {
|
|
|
|
state.nce->Initialize(state);
|
|
|
|
std::string romExt = romFile.substr(romFile.find_last_of('.') + 1);
|
|
|
|
std::transform(romExt.begin(), romExt.end(), romExt.begin(), [](unsigned char c) { return std::tolower(c); });
|
2019-10-13 10:04:47 +02:00
|
|
|
auto process = CreateProcess(constant::BaseAddr, constant::DefStackSize);
|
|
|
|
if (romExt == "nro") {
|
|
|
|
loader::NroLoader loader(romFile);
|
|
|
|
loader.LoadProcessData(process, state);
|
|
|
|
} else
|
2019-09-24 22:54:27 +02:00
|
|
|
throw exception("Unsupported ROM extension.");
|
2019-10-13 10:04:47 +02:00
|
|
|
process->threadMap.at(process->mainThread)->Start(); // The kernel itself is responsible for starting the main thread
|
2019-09-24 22:54:27 +02:00
|
|
|
state.nce->Execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function executed by all child processes after cloning
|
|
|
|
*/
|
|
|
|
int ExecuteChild(void *) {
|
|
|
|
ptrace(PTRACE_TRACEME);
|
|
|
|
asm volatile("Brk #0xFF"); // BRK #constant::brkRdy (So we know when the thread/process is ready)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<type::KProcess> OS::CreateProcess(u64 address, size_t stackSize) {
|
|
|
|
auto *stack = static_cast<u8 *>(mmap(nullptr, stackSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS | MAP_STACK, -1, 0)); // NOLINT(hicpp-signed-bitwise)
|
|
|
|
if (stack == MAP_FAILED)
|
|
|
|
throw exception("Failed to allocate stack memory");
|
|
|
|
if (mprotect(stack, PAGE_SIZE, PROT_NONE)) {
|
|
|
|
munmap(stack, stackSize);
|
|
|
|
throw exception("Failed to create guard pages");
|
|
|
|
}
|
2019-10-13 10:04:47 +02:00
|
|
|
pid_t pid = clone(&ExecuteChild, stack + stackSize, CLONE_FILES | CLONE_FS | SIGCHLD, nullptr); // NOLINT(hicpp-signed-bitwise)
|
2019-09-24 22:54:27 +02:00
|
|
|
if (pid == -1)
|
|
|
|
throw exception(fmt::format("Call to clone() has failed: {}", strerror(errno)));
|
2019-10-13 10:04:47 +02:00
|
|
|
std::shared_ptr<type::KProcess> process = std::make_shared<kernel::type::KProcess>(state, pid, address, reinterpret_cast<u64>(stack), stackSize);
|
|
|
|
processMap[pid] = process;
|
2019-09-24 22:54:27 +02:00
|
|
|
processVec.push_back(pid);
|
|
|
|
state.logger->Write(Logger::Debug, "Successfully created process with PID: {}", pid);
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS::KillThread(pid_t pid) {
|
2019-10-13 10:04:47 +02:00
|
|
|
auto process = processMap.at(pid);
|
2019-09-24 22:54:27 +02:00
|
|
|
if (process->mainThread == pid) {
|
|
|
|
state.logger->Write(Logger::Debug, "Exiting process with PID: {}", pid);
|
|
|
|
// Erasing all shared_ptr instances to the process will call the destructor
|
|
|
|
// However, in the case these are not all instances of it we wouldn't want to call the destructor
|
|
|
|
for (auto&[key, value]: process->threadMap)
|
2019-10-13 10:04:47 +02:00
|
|
|
processMap.erase(key);
|
2019-09-24 22:54:27 +02:00
|
|
|
processVec.erase(std::remove(processVec.begin(), processVec.end(), pid), processVec.end());
|
|
|
|
} else {
|
|
|
|
state.logger->Write(Logger::Debug, "Exiting thread with TID: {}", pid);
|
|
|
|
process->handleTable.erase(process->threadMap[pid]->handle);
|
|
|
|
process->threadMap.erase(pid);
|
2019-10-13 10:04:47 +02:00
|
|
|
processMap.erase(pid);
|
2019-09-24 22:54:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:04:47 +02:00
|
|
|
void OS::SvcHandler(u16 svc) {
|
2019-09-24 22:54:27 +02:00
|
|
|
if (svc::SvcTable[svc]) {
|
|
|
|
state.logger->Write(Logger::Debug, "SVC called 0x{:X}", svc);
|
|
|
|
(*svc::SvcTable[svc])(state);
|
|
|
|
} else
|
|
|
|
throw exception(fmt::format("Unimplemented SVC 0x{:X}", svc));
|
|
|
|
}
|
2019-10-13 10:04:47 +02:00
|
|
|
|
|
|
|
std::shared_ptr<kernel::type::KSharedMemory> OS::MapSharedKernel(const u64 address, const size_t size, const memory::Permission kernelPermission, const memory::Permission remotePermission, const memory::Type type) {
|
|
|
|
return std::make_shared<kernel::type::KSharedMemory>(state, 0, address, size, kernelPermission, remotePermission, type);
|
|
|
|
}
|
2019-09-24 22:54:27 +02:00
|
|
|
}
|