kern: finish KProcess::Initialize() for KIPs

This commit is contained in:
Michael Scire 2020-02-19 06:46:59 -08:00
parent d9c3908caf
commit b857153964
6 changed files with 209 additions and 5 deletions

View file

@ -30,7 +30,76 @@ namespace ams::kern {
}
Result KProcess::Initialize(const ams::svc::CreateProcessParameter &params) {
MESOSPHERE_TODO_IMPLEMENT();
/* TODO: Validate intended kernel version. */
/* How should we do this? */
/* Create and clear the process local region. */
R_TRY(this->CreateThreadLocalRegion(std::addressof(this->plr_address)));
std::memset(this->GetThreadLocalRegionPointer(this->plr_address), 0, ams::svc::ThreadLocalRegionSize);
/* Copy in the name from parameters. */
static_assert(sizeof(params.name) < sizeof(this->name));
std::memcpy(this->name, params.name, sizeof(params.name));
this->name[sizeof(params.name)] = 0;
/* Set misc fields. */
this->state = State_Created;
this->main_thread_stack_size = 0;
this->creation_time = KHardwareTimer::GetTick();
this->used_kernel_memory_size = 0;
this->ideal_core_id = 0;
this->flags = params.flags;
this->version = params.version;
this->program_id = params.program_id;
this->code_address = params.code_address;
this->code_size = params.code_num_pages * PageSize;
this->is_application = (params.flags & ams::svc::CreateProcessFlag_IsApplication);
this->is_jit_debug = false;
/* Set thread fields. */
for (size_t i = 0; i < cpu::NumCores; i++) {
this->running_threads[i] = nullptr;
this->running_thread_idle_counts[i] = 0;
this->pinned_threads[i] = nullptr;
}
/* Set max memory based on address space type. */
switch ((params.flags & ams::svc::CreateProcessFlag_AddressSpaceMask)) {
case ams::svc::CreateProcessFlag_AddressSpace32Bit:
case ams::svc::CreateProcessFlag_AddressSpace64BitDeprecated:
case ams::svc::CreateProcessFlag_AddressSpace64Bit:
this->max_process_memory = this->page_table.GetHeapRegionSize();
break;
case ams::svc::CreateProcessFlag_AddressSpace32BitWithoutAlias:
this->max_process_memory = this->page_table.GetHeapRegionSize() + this->page_table.GetAliasRegionSize();
break;
MESOSPHERE_UNREACHABLE_DEFAULT_CASE();
}
/* Generate random entropy. */
KSystemControl::GenerateRandomBytes(this->entropy, sizeof(this->entropy));
/* Clear remaining fields. */
this->num_threads = 0;
this->peak_num_threads = 0;
this->num_created_threads = 0;
this->num_process_switches = 0;
this->num_thread_switches = 0;
this->num_fpu_switches = 0;
this->num_supervisor_calls = 0;
this->num_ipc_messages = 0;
this->is_signaled = false;
this->attached_object = nullptr;
this->exception_thread = nullptr;
this->is_suspended = false;
this->memory_release_hint = 0;
this->schedule_count = 0;
/* We're initialized! */
this->is_initialized = true;
return ResultSuccess();
}
Result KProcess::Initialize(const ams::svc::CreateProcessParameter &params, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool) {
@ -40,7 +109,7 @@ namespace ams::kern {
/* Set members. */
this->memory_pool = pool;
this->resource_limit = resource_limit;
this->resource_limit = res_limit;
this->system_resource_address = Null<KVirtualAddress>;
this->system_resource_num_pages = 0;
@ -87,6 +156,72 @@ namespace ams::kern {
MESOSPHERE_TODO_IMPLEMENT();
}
Result KProcess::CreateThreadLocalRegion(KProcessAddress *out) {
KThreadLocalPage *tlp = nullptr;
KProcessAddress tlr = Null<KProcessAddress>;
/* See if we can get a region from a partially used TLP. */
{
KScopedSchedulerLock sl;
if (auto it = this->partially_used_tlp_tree.begin(); it != partially_used_tlp_tree.end()) {
tlr = it->Reserve();
MESOSPHERE_ABORT_UNLESS(tlr != Null<KProcessAddress>);
if (it->IsAllUsed()) {
tlp = std::addressof(*it);
this->partially_used_tlp_tree.erase(it);
this->fully_used_tlp_tree.insert(*tlp);
}
*out = tlr;
return ResultSuccess();
}
}
/* Allocate a new page. */
tlp = KThreadLocalPage::Allocate();
R_UNLESS(tlp != nullptr, svc::ResultOutOfMemory());
auto tlp_guard = SCOPE_GUARD { KThreadLocalPage::Free(tlp); };
/* Initialize the new page. */
R_TRY(tlp->Initialize(this));
/* Reserve a TLR. */
tlr = tlp->Reserve();
MESOSPHERE_ABORT_UNLESS(tlr != Null<KProcessAddress>);
/* Insert into our tree. */
{
KScopedSchedulerLock sl;
if (tlp->IsAllUsed()) {
this->fully_used_tlp_tree.insert(*tlp);
} else {
this->partially_used_tlp_tree.insert(*tlp);
}
}
/* We succeeded! */
tlp_guard.Cancel();
*out = tlr;
return ResultSuccess();
}
void *KProcess::GetThreadLocalRegionPointer(KProcessAddress addr) {
KThreadLocalPage *tlp = nullptr;
{
KScopedSchedulerLock sl;
if (auto it = this->partially_used_tlp_tree.find(KThreadLocalPage(util::AlignDown(GetInteger(addr), PageSize))); it != this->partially_used_tlp_tree.end()) {
tlp = std::addressof(*it);
} else if (auto it = this->fully_used_tlp_tree.find(KThreadLocalPage(util::AlignDown(GetInteger(addr), PageSize))); it != this->fully_used_tlp_tree.end()) {
tlp = std::addressof(*it);
} else {
return nullptr;
}
}
return static_cast<u8 *>(tlp->GetPointer()) + (GetInteger(addr) & (PageSize - 1));
}
void KProcess::SetPreemptionState() {
MESOSPHERE_TODO_IMPLEMENT();
}