فهرست منبع

Loaders: Don't automatically set the current process every time we load an application.

The loaders will now just create a Kernel::Process, construct it and return it to the caller, which is responsible for setting it as the current process and configuring the global page table.
Subv 8 سال پیش
والد
کامیت
7f48aa8d25
8فایلهای تغییر یافته به همراه40 افزوده شده و 37 حذف شده
  1. 4 2
      src/core/core.cpp
  2. 7 8
      src/core/loader/3dsx.cpp
  3. 1 1
      src/core/loader/3dsx.h
  4. 7 8
      src/core/loader/elf.cpp
  5. 1 1
      src/core/loader/elf.h
  6. 8 5
      src/core/loader/loader.h
  7. 9 10
      src/core/loader/ncch.cpp
  8. 3 2
      src/core/loader/ncch.h

+ 4 - 2
src/core/core.cpp

@@ -13,6 +13,7 @@
 #include "core/core_timing.h"
 #include "core/core_timing.h"
 #include "core/gdbstub/gdbstub.h"
 #include "core/gdbstub/gdbstub.h"
 #include "core/hle/kernel/kernel.h"
 #include "core/hle/kernel/kernel.h"
+#include "core/hle/kernel/process.h"
 #include "core/hle/kernel/thread.h"
 #include "core/hle/kernel/thread.h"
 #include "core/hle/service/service.h"
 #include "core/hle/service/service.h"
 #include "core/hw/hw.h"
 #include "core/hw/hw.h"
@@ -100,7 +101,7 @@ System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& file
         return init_result;
         return init_result;
     }
     }
 
 
-    const Loader::ResultStatus load_result{app_loader->Load()};
+    const Loader::ResultStatus load_result{app_loader->Load(Kernel::g_current_process)};
     if (Loader::ResultStatus::Success != load_result) {
     if (Loader::ResultStatus::Success != load_result) {
         LOG_CRITICAL(Core, "Failed to load ROM (Error %i)!", load_result);
         LOG_CRITICAL(Core, "Failed to load ROM (Error %i)!", load_result);
         System::Shutdown();
         System::Shutdown();
@@ -114,6 +115,7 @@ System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& file
             return ResultStatus::ErrorLoader;
             return ResultStatus::ErrorLoader;
         }
         }
     }
     }
+    Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
     status = ResultStatus::Success;
     status = ResultStatus::Success;
     return status;
     return status;
 }
 }
@@ -196,4 +198,4 @@ void System::Shutdown() {
     LOG_DEBUG(Core, "Shutdown OK");
     LOG_DEBUG(Core, "Shutdown OK");
 }
 }
 
 
-} // namespace
+} // namespace Core

+ 7 - 8
src/core/loader/3dsx.cpp

@@ -91,8 +91,8 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets)
     return loadinfo->seg_addrs[2] + addr - offsets[1];
     return loadinfo->seg_addrs[2] + addr - offsets[1];
 }
 }
 
 
-using Kernel::SharedPtr;
 using Kernel::CodeSet;
 using Kernel::CodeSet;
+using Kernel::SharedPtr;
 
 
 static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
 static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
                                    SharedPtr<CodeSet>* out_codeset) {
                                    SharedPtr<CodeSet>* out_codeset) {
@@ -255,7 +255,7 @@ FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) {
     return FileType::Error;
     return FileType::Error;
 }
 }
 
 
-ResultStatus AppLoader_THREEDSX::Load() {
+ResultStatus AppLoader_THREEDSX::Load(Kernel::SharedPtr<Kernel::Process>& process) {
     if (is_loaded)
     if (is_loaded)
         return ResultStatus::ErrorAlreadyLoaded;
         return ResultStatus::ErrorAlreadyLoaded;
 
 
@@ -267,16 +267,15 @@ ResultStatus AppLoader_THREEDSX::Load() {
         return ResultStatus::Error;
         return ResultStatus::Error;
     codeset->name = filename;
     codeset->name = filename;
 
 
-    Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
-    Kernel::g_current_process->svc_access_mask.set();
-    Kernel::g_current_process->address_mappings = default_address_mappings;
-    Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
+    process = Kernel::Process::Create(std::move(codeset));
+    process->svc_access_mask.set();
+    process->address_mappings = default_address_mappings;
 
 
     // Attach the default resource limit (APPLICATION) to the process
     // Attach the default resource limit (APPLICATION) to the process
-    Kernel::g_current_process->resource_limit =
+    process->resource_limit =
         Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
         Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
 
 
-    Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
+    process->Run(48, Kernel::DEFAULT_STACK_SIZE);
 
 
     Service::FS::RegisterSelfNCCH(*this);
     Service::FS::RegisterSelfNCCH(*this);
 
 

+ 1 - 1
src/core/loader/3dsx.h

@@ -31,7 +31,7 @@ public:
         return IdentifyType(file);
         return IdentifyType(file);
     }
     }
 
 
-    ResultStatus Load() override;
+    ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
 
 
     ResultStatus ReadIcon(std::vector<u8>& buffer) override;
     ResultStatus ReadIcon(std::vector<u8>& buffer) override;
 
 

+ 7 - 8
src/core/loader/elf.cpp

@@ -13,8 +13,8 @@
 #include "core/loader/elf.h"
 #include "core/loader/elf.h"
 #include "core/memory.h"
 #include "core/memory.h"
 
 
-using Kernel::SharedPtr;
 using Kernel::CodeSet;
 using Kernel::CodeSet;
+using Kernel::SharedPtr;
 
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 // ELF Header Constants
 // ELF Header Constants
@@ -375,7 +375,7 @@ FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
     return FileType::Error;
     return FileType::Error;
 }
 }
 
 
-ResultStatus AppLoader_ELF::Load() {
+ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) {
     if (is_loaded)
     if (is_loaded)
         return ResultStatus::ErrorAlreadyLoaded;
         return ResultStatus::ErrorAlreadyLoaded;
 
 
@@ -394,16 +394,15 @@ ResultStatus AppLoader_ELF::Load() {
     SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
     SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
     codeset->name = filename;
     codeset->name = filename;
 
 
-    Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
-    Kernel::g_current_process->svc_access_mask.set();
-    Kernel::g_current_process->address_mappings = default_address_mappings;
-    Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
+    process = Kernel::Process::Create(std::move(codeset));
+    process->svc_access_mask.set();
+    process->address_mappings = default_address_mappings;
 
 
     // Attach the default resource limit (APPLICATION) to the process
     // Attach the default resource limit (APPLICATION) to the process
-    Kernel::g_current_process->resource_limit =
+    process->resource_limit =
         Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
         Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
 
 
-    Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
+    process->Run(48, Kernel::DEFAULT_STACK_SIZE);
 
 
     is_loaded = true;
     is_loaded = true;
     return ResultStatus::Success;
     return ResultStatus::Success;

+ 1 - 1
src/core/loader/elf.h

@@ -30,7 +30,7 @@ public:
         return IdentifyType(file);
         return IdentifyType(file);
     }
     }
 
 
-    ResultStatus Load() override;
+    ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
 
 
 private:
 private:
     std::string filename;
     std::string filename;

+ 8 - 5
src/core/loader/loader.h

@@ -13,10 +13,12 @@
 #include <boost/optional.hpp>
 #include <boost/optional.hpp>
 #include "common/common_types.h"
 #include "common/common_types.h"
 #include "common/file_util.h"
 #include "common/file_util.h"
+#include "core/hle/kernel/kernel.h"
 
 
 namespace Kernel {
 namespace Kernel {
 struct AddressMapping;
 struct AddressMapping;
-}
+class Process;
+} // namespace Kernel
 
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 // Loader namespace
 // Loader namespace
@@ -92,10 +94,11 @@ public:
     virtual FileType GetFileType() = 0;
     virtual FileType GetFileType() = 0;
 
 
     /**
     /**
-     * Load the application
-     * @return ResultStatus result of function
+     * Load the application and return the created Process instance
+     * @param process The newly created process.
+     * @return The status result of the operation.
      */
      */
-    virtual ResultStatus Load() = 0;
+    virtual ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) = 0;
 
 
     /**
     /**
      * Loads the system mode that this application needs.
      * Loads the system mode that this application needs.
@@ -206,4 +209,4 @@ extern const std::initializer_list<Kernel::AddressMapping> default_address_mappi
  */
  */
 std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
 std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
 
 
-} // namespace
+} // namespace Loader

+ 9 - 10
src/core/loader/ncch.cpp

@@ -67,9 +67,9 @@ std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMo
                           ResultStatus::Success);
                           ResultStatus::Success);
 }
 }
 
 
-ResultStatus AppLoader_NCCH::LoadExec() {
-    using Kernel::SharedPtr;
+ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr<Kernel::Process>& process) {
     using Kernel::CodeSet;
     using Kernel::CodeSet;
+    using Kernel::SharedPtr;
 
 
     if (!is_loaded)
     if (!is_loaded)
         return ResultStatus::ErrorNotLoaded;
         return ResultStatus::ErrorNotLoaded;
@@ -107,16 +107,15 @@ ResultStatus AppLoader_NCCH::LoadExec() {
         codeset->entrypoint = codeset->code.addr;
         codeset->entrypoint = codeset->code.addr;
         codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
         codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
 
 
-        Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
-        Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
+        process = Kernel::Process::Create(std::move(codeset));
 
 
         // Attach a resource limit to the process based on the resource limit category
         // Attach a resource limit to the process based on the resource limit category
-        Kernel::g_current_process->resource_limit =
+        process->resource_limit =
             Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>(
             Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>(
                 overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category));
                 overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category));
 
 
         // Set the default CPU core for this process
         // Set the default CPU core for this process
-        Kernel::g_current_process->ideal_processor =
+        process->ideal_processor =
             overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor;
             overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor;
 
 
         // Copy data while converting endianness
         // Copy data while converting endianness
@@ -124,11 +123,11 @@ ResultStatus AppLoader_NCCH::LoadExec() {
             kernel_caps;
             kernel_caps;
         std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
         std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
                     begin(kernel_caps));
                     begin(kernel_caps));
-        Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
+        process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
 
 
         s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
         s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
         u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size;
         u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size;
-        Kernel::g_current_process->Run(priority, stack_size);
+        process->Run(priority, stack_size);
         return ResultStatus::Success;
         return ResultStatus::Success;
     }
     }
     return ResultStatus::Error;
     return ResultStatus::Error;
@@ -151,7 +150,7 @@ void AppLoader_NCCH::ParseRegionLockoutInfo() {
     }
     }
 }
 }
 
 
-ResultStatus AppLoader_NCCH::Load() {
+ResultStatus AppLoader_NCCH::Load(Kernel::SharedPtr<Kernel::Process>& process) {
     u64_le ncch_program_id;
     u64_le ncch_program_id;
 
 
     if (is_loaded)
     if (is_loaded)
@@ -183,7 +182,7 @@ ResultStatus AppLoader_NCCH::Load() {
 
 
     is_loaded = true; // Set state to loaded
     is_loaded = true; // Set state to loaded
 
 
-    result = LoadExec(); // Load the executable into memory for booting
+    result = LoadExec(process); // Load the executable into memory for booting
     if (ResultStatus::Success != result)
     if (ResultStatus::Success != result)
         return result;
         return result;
 
 

+ 3 - 2
src/core/loader/ncch.h

@@ -33,7 +33,7 @@ public:
         return IdentifyType(file);
         return IdentifyType(file);
     }
     }
 
 
-    ResultStatus Load() override;
+    ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
 
 
     /**
     /**
      * Loads the Exheader and returns the system mode for this application.
      * Loads the Exheader and returns the system mode for this application.
@@ -62,9 +62,10 @@ public:
 private:
 private:
     /**
     /**
      * Loads .code section into memory for booting
      * Loads .code section into memory for booting
+     * @param process The newly created process
      * @return ResultStatus result of function
      * @return ResultStatus result of function
      */
      */
-    ResultStatus LoadExec();
+    ResultStatus LoadExec(Kernel::SharedPtr<Kernel::Process>& process);
 
 
     /// Reads the region lockout info in the SMDH and send it to CFG service
     /// Reads the region lockout info in the SMDH and send it to CFG service
     void ParseRegionLockoutInfo();
     void ParseRegionLockoutInfo();