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/)
|
|
|
|
|
2019-09-24 22:54:27 +02:00
|
|
|
#include "os.h"
|
|
|
|
#include "loader/nro.h"
|
2020-01-07 03:36:08 +01:00
|
|
|
#include "nce/guest.h"
|
2019-09-24 22:54:27 +02:00
|
|
|
|
|
|
|
namespace skyline::kernel {
|
2020-01-21 08:16:57 +01:00
|
|
|
OS::OS(std::shared_ptr<JvmManager> &jvmManager, std::shared_ptr<Logger> &logger, std::shared_ptr<Settings> &settings) : state(this, process, jvmManager, settings, logger), memory(state), serviceManager(state) {}
|
2019-09-24 22:54:27 +02:00
|
|
|
|
2020-04-22 19:02:27 +02:00
|
|
|
void OS::Execute(int romFd, TitleFormat romType) {
|
2020-01-07 03:36:08 +01:00
|
|
|
std::shared_ptr<loader::Loader> loader;
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2019-12-02 18:40:53 +01:00
|
|
|
if (romType == TitleFormat::NRO) {
|
2020-01-07 03:36:08 +01:00
|
|
|
loader = std::make_shared<loader::NroLoader>(romFd);
|
2019-10-13 10:04:47 +02:00
|
|
|
} else
|
2019-09-24 22:54:27 +02:00
|
|
|
throw exception("Unsupported ROM extension.");
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2020-01-21 08:16:57 +01:00
|
|
|
auto process = CreateProcess(constant::BaseAddress, 0, constant::DefStackSize);
|
2020-01-07 03:36:08 +01:00
|
|
|
loader->LoadProcessData(process, state);
|
2020-01-21 08:16:57 +01:00
|
|
|
process->InitializeMemory();
|
|
|
|
process->threads.at(process->pid)->Start(); // The kernel itself is responsible for starting the main thread
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2019-09-24 22:54:27 +02:00
|
|
|
state.nce->Execute();
|
|
|
|
}
|
|
|
|
|
2020-01-07 03:36:08 +01:00
|
|
|
std::shared_ptr<type::KProcess> OS::CreateProcess(u64 entry, u64 argument, size_t stackSize) {
|
2020-03-25 18:59:37 +01:00
|
|
|
auto stack = std::make_shared<type::KSharedMemory>(state, 0, stackSize, memory::Permission{true, true, false}, memory::states::Reserved, MAP_NORESERVE | MAP_STACK);
|
2020-03-23 17:10:22 +01:00
|
|
|
stack->guest = stack->kernel;
|
|
|
|
if (mprotect(reinterpret_cast<void *>(stack->guest.address), PAGE_SIZE, PROT_NONE))
|
2019-09-24 22:54:27 +02:00
|
|
|
throw exception("Failed to create guard pages");
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2020-03-25 18:59:37 +01:00
|
|
|
auto tlsMem = std::make_shared<type::KSharedMemory>(state, 0, (sizeof(ThreadContext) + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1), memory::Permission{true, true, false}, memory::states::Reserved);
|
2020-01-07 03:36:08 +01:00
|
|
|
tlsMem->guest = tlsMem->kernel;
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2020-04-22 19:02:27 +02:00
|
|
|
auto pid = clone(reinterpret_cast<int (*)(void *)>(&guest::GuestEntry), reinterpret_cast<void *>(stack->guest.address + stackSize), CLONE_FILES | CLONE_FS | CLONE_SETTLS | SIGCHLD, reinterpret_cast<void *>(entry), nullptr, reinterpret_cast<void *>(tlsMem->guest.address));
|
2019-09-24 22:54:27 +02:00
|
|
|
if (pid == -1)
|
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
|
|
|
throw exception("Call to clone() has failed: {}", strerror(errno));
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2020-01-11 05:52:25 +01:00
|
|
|
state.logger->Debug("Successfully created process with PID: {}", pid);
|
2020-03-23 17:10:22 +01:00
|
|
|
process = std::make_shared<kernel::type::KProcess>(state, pid, argument, stack, tlsMem);
|
2020-03-25 19:57:05 +01:00
|
|
|
|
2019-09-24 22:54:27 +02:00
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS::KillThread(pid_t pid) {
|
2020-01-01 14:11:25 +01:00
|
|
|
if (process->pid == pid) {
|
2019-11-22 15:59:50 +01:00
|
|
|
state.logger->Debug("Killing process with PID: {}", pid);
|
2020-01-21 08:16:57 +01:00
|
|
|
for (auto &thread: process->threads)
|
2020-01-11 05:52:25 +01:00
|
|
|
thread.second->Kill();
|
2019-09-24 22:54:27 +02:00
|
|
|
} else {
|
2019-11-22 15:59:50 +01:00
|
|
|
state.logger->Debug("Killing thread with TID: {}", pid);
|
2020-01-21 08:16:57 +01:00
|
|
|
process->threads.at(pid)->Kill();
|
2019-09-24 22:54:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|