kernel: pass ref in CodeSet

This commit is contained in:
Weiyi Wang 2018-10-12 15:21:32 -04:00
parent 7449ba85a6
commit 213b259cf1
10 changed files with 56 additions and 30 deletions

View file

@ -13,6 +13,7 @@ namespace Kernel {
class AddressArbiter; class AddressArbiter;
class Event; class Event;
class Mutex; class Mutex;
class CodeSet;
enum class ResetType { enum class ResetType {
OneShot, OneShot,
@ -50,6 +51,8 @@ public:
* @return Pointer to new Mutex object * @return Pointer to new Mutex object
*/ */
SharedPtr<Mutex> CreateMutex(bool initial_locked, std::string name = "Unknown"); SharedPtr<Mutex> CreateMutex(bool initial_locked, std::string name = "Unknown");
SharedPtr<CodeSet> CreateCodeSet(std::string name, u64 program_id);
}; };
} // namespace Kernel } // namespace Kernel

View file

@ -20,8 +20,8 @@ namespace Kernel {
// Lists all processes that exist in the current session. // Lists all processes that exist in the current session.
static std::vector<SharedPtr<Process>> process_list; static std::vector<SharedPtr<Process>> process_list;
SharedPtr<CodeSet> CodeSet::Create(std::string name, u64 program_id) { SharedPtr<CodeSet> KernelSystem::CreateCodeSet(std::string name, u64 program_id) {
SharedPtr<CodeSet> codeset(new CodeSet); SharedPtr<CodeSet> codeset(new CodeSet(*this));
codeset->name = std::move(name); codeset->name = std::move(name);
codeset->program_id = program_id; codeset->program_id = program_id;
@ -29,7 +29,7 @@ SharedPtr<CodeSet> CodeSet::Create(std::string name, u64 program_id) {
return codeset; return codeset;
} }
CodeSet::CodeSet() {} CodeSet::CodeSet(KernelSystem& system) {}
CodeSet::~CodeSet() {} CodeSet::~CodeSet() {}
u32 Process::next_process_id; u32 Process::next_process_id;

View file

@ -62,8 +62,6 @@ struct CodeSet final : public Object {
u32 size = 0; u32 size = 0;
}; };
static SharedPtr<CodeSet> Create(std::string name, u64 program_id);
std::string GetTypeName() const override { std::string GetTypeName() const override {
return "CodeSet"; return "CodeSet";
} }
@ -111,8 +109,10 @@ struct CodeSet final : public Object {
u64 program_id; u64 program_id;
private: private:
CodeSet(); explicit CodeSet(KernelSystem& kernel);
~CodeSet() override; ~CodeSet() override;
friend class KernelSystem;
}; };
class Process final : public Object { class Process final : public Object {

View file

@ -217,7 +217,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
} }
// Create the CodeSet // Create the CodeSet
SharedPtr<CodeSet> code_set = CodeSet::Create("", 0); SharedPtr<CodeSet> code_set = Core::System::GetInstance().Kernel().CreateCodeSet("", 0);
code_set->CodeSegment().offset = loadinfo.seg_ptrs[0] - program_image.data(); code_set->CodeSegment().offset = loadinfo.seg_ptrs[0] - program_image.data();
code_set->CodeSegment().addr = loadinfo.seg_addrs[0]; code_set->CodeSegment().addr = loadinfo.seg_addrs[0];

View file

@ -8,6 +8,7 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/core.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "core/hle/kernel/resource_limit.h" #include "core/hle/kernel/resource_limit.h"
#include "core/loader/elf.h" #include "core/loader/elf.h"
@ -299,7 +300,7 @@ SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
std::vector<u8> program_image(total_image_size); std::vector<u8> program_image(total_image_size);
std::size_t current_image_position = 0; std::size_t current_image_position = 0;
SharedPtr<CodeSet> codeset = CodeSet::Create("", 0); SharedPtr<CodeSet> codeset = Core::System::GetInstance().Kernel().CreateCodeSet("", 0);
for (unsigned int i = 0; i < header->e_phnum; ++i) { for (unsigned int i = 0; i < header->e_phnum; ++i) {
Elf32_Phdr* p = &segments[i]; Elf32_Phdr* p = &segments[i];

View file

@ -75,7 +75,8 @@ ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr<Kernel::Process>& proces
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
(const char*)overlay_ncch->exheader_header.codeset_info.name, 8); (const char*)overlay_ncch->exheader_header.codeset_info.name, 8);
SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id); SharedPtr<CodeSet> codeset =
Core::System::GetInstance().Kernel().CreateCodeSet(process_name, program_id);
codeset->CodeSegment().offset = 0; codeset->CodeSegment().offset = 0;
codeset->CodeSegment().addr = overlay_ncch->exheader_header.codeset_info.text.address; codeset->CodeSegment().addr = overlay_ncch->exheader_header.codeset_info.text.address;

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "core/core.h" #include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "core/memory.h" #include "core/memory.h"
#include "core/memory_setup.h" #include "core/memory_setup.h"
@ -15,7 +16,10 @@ static Memory::PageTable* page_table = nullptr;
TestEnvironment::TestEnvironment(bool mutable_memory_) TestEnvironment::TestEnvironment(bool mutable_memory_)
: mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) { : mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) {
Kernel::g_current_process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); CoreTiming::Init();
kernel = std::make_unique<Kernel::KernelSystem>(0);
Kernel::g_current_process = Kernel::Process::Create(kernel->CreateCodeSet("", 0));
page_table = &Kernel::g_current_process->vm_manager.page_table; page_table = &Kernel::g_current_process->vm_manager.page_table;
page_table->pointers.fill(nullptr); page_table->pointers.fill(nullptr);
@ -30,6 +34,8 @@ TestEnvironment::TestEnvironment(bool mutable_memory_)
TestEnvironment::~TestEnvironment() { TestEnvironment::~TestEnvironment() {
Memory::UnmapRegion(*page_table, 0x80000000, 0x80000000); Memory::UnmapRegion(*page_table, 0x80000000, 0x80000000);
Memory::UnmapRegion(*page_table, 0x00000000, 0x80000000); Memory::UnmapRegion(*page_table, 0x00000000, 0x80000000);
CoreTiming::Shutdown();
} }
void TestEnvironment::SetMemory64(VAddr vaddr, u64 value) { void TestEnvironment::SetMemory64(VAddr vaddr, u64 value) {

View file

@ -5,8 +5,8 @@
#include <tuple> #include <tuple>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/kernel/kernel.h"
#include "core/mmio.h" #include "core/mmio.h"
namespace ArmTests { namespace ArmTests {
@ -79,6 +79,8 @@ private:
bool mutable_memory; bool mutable_memory;
std::shared_ptr<TestMemory> test_memory; std::shared_ptr<TestMemory> test_memory;
std::vector<WriteRecord> write_records; std::vector<WriteRecord> write_records;
std::unique_ptr<Kernel::KernelSystem> kernel;
}; };
} // namespace ArmTests } // namespace ArmTests

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include "core/core_timing.h"
#include "core/hle/ipc.h" #include "core/hle/ipc.h"
#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/client_port.h"
#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/client_session.h"
@ -14,16 +15,17 @@
namespace Kernel { namespace Kernel {
static SharedPtr<Object> MakeObject() { static SharedPtr<Object> MakeObject(Kernel::KernelSystem& kernel) {
static Kernel::KernelSystem kernel(0);
return kernel.CreateEvent(ResetType::OneShot); return kernel.CreateEvent(ResetType::OneShot);
} }
TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel]") { TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel]") {
CoreTiming::Init();
Kernel::KernelSystem kernel(0);
auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair());
HLERequestContext context(std::move(session)); HLERequestContext context(std::move(session));
auto process = Process::Create(CodeSet::Create("", 0)); auto process = Process::Create(kernel.CreateCodeSet("", 0));
HandleTable handle_table; HandleTable handle_table;
SECTION("works with empty cmdbuf") { SECTION("works with empty cmdbuf") {
@ -53,7 +55,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
} }
SECTION("translates move handles") { SECTION("translates move handles") {
auto a = MakeObject(); auto a = MakeObject(kernel);
Handle a_handle = handle_table.Create(a).Unwrap(); Handle a_handle = handle_table.Create(a).Unwrap();
const u32_le input[]{ const u32_le input[]{
IPC::MakeHeader(0, 0, 2), IPC::MakeHeader(0, 0, 2),
@ -69,7 +71,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
} }
SECTION("translates copy handles") { SECTION("translates copy handles") {
auto a = MakeObject(); auto a = MakeObject(kernel);
Handle a_handle = handle_table.Create(a).Unwrap(); Handle a_handle = handle_table.Create(a).Unwrap();
const u32_le input[]{ const u32_le input[]{
IPC::MakeHeader(0, 0, 2), IPC::MakeHeader(0, 0, 2),
@ -85,9 +87,9 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
} }
SECTION("translates multi-handle descriptors") { SECTION("translates multi-handle descriptors") {
auto a = MakeObject(); auto a = MakeObject(kernel);
auto b = MakeObject(); auto b = MakeObject(kernel);
auto c = MakeObject(); auto c = MakeObject(kernel);
const u32_le input[]{ const u32_le input[]{
IPC::MakeHeader(0, 0, 5), IPC::MoveHandleDesc(2), IPC::MakeHeader(0, 0, 5), IPC::MoveHandleDesc(2),
handle_table.Create(a).Unwrap(), handle_table.Create(b).Unwrap(), handle_table.Create(a).Unwrap(), handle_table.Create(b).Unwrap(),
@ -191,7 +193,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
buffer_mapped->size(), MemoryState::Private); buffer_mapped->size(), MemoryState::Private);
REQUIRE(result.Code() == RESULT_SUCCESS); REQUIRE(result.Code() == RESULT_SUCCESS);
auto a = MakeObject(); auto a = MakeObject(kernel);
const u32_le input[]{ const u32_le input[]{
IPC::MakeHeader(0, 2, 8), IPC::MakeHeader(0, 2, 8),
0x12345678, 0x12345678,
@ -223,13 +225,17 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel
REQUIRE(process->vm_manager.UnmapRange(target_address_mapped, buffer_mapped->size()) == REQUIRE(process->vm_manager.UnmapRange(target_address_mapped, buffer_mapped->size()) ==
RESULT_SUCCESS); RESULT_SUCCESS);
} }
CoreTiming::Shutdown();
} }
TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
CoreTiming::Init();
Kernel::KernelSystem kernel(0);
auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair());
HLERequestContext context(std::move(session)); HLERequestContext context(std::move(session));
auto process = Process::Create(CodeSet::Create("", 0)); auto process = Process::Create(kernel.CreateCodeSet("", 0));
HandleTable handle_table; HandleTable handle_table;
auto* input = context.CommandBuffer(); auto* input = context.CommandBuffer();
u32_le output[IPC::COMMAND_BUFFER_LENGTH]; u32_le output[IPC::COMMAND_BUFFER_LENGTH];
@ -256,8 +262,8 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
} }
SECTION("translates move/copy handles") { SECTION("translates move/copy handles") {
auto a = MakeObject(); auto a = MakeObject(kernel);
auto b = MakeObject(); auto b = MakeObject(kernel);
input[0] = IPC::MakeHeader(0, 0, 4); input[0] = IPC::MakeHeader(0, 0, 4);
input[1] = IPC::MoveHandleDesc(1); input[1] = IPC::MoveHandleDesc(1);
input[2] = context.AddOutgoingHandle(a); input[2] = context.AddOutgoingHandle(a);
@ -282,9 +288,9 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
} }
SECTION("translates multi-handle descriptors") { SECTION("translates multi-handle descriptors") {
auto a = MakeObject(); auto a = MakeObject(kernel);
auto b = MakeObject(); auto b = MakeObject(kernel);
auto c = MakeObject(); auto c = MakeObject(kernel);
input[0] = IPC::MakeHeader(0, 0, 5); input[0] = IPC::MakeHeader(0, 0, 5);
input[1] = IPC::MoveHandleDesc(2); input[1] = IPC::MoveHandleDesc(2);
input[2] = context.AddOutgoingHandle(a); input[2] = context.AddOutgoingHandle(a);
@ -362,6 +368,8 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
REQUIRE(process->vm_manager.UnmapRange(target_address, output_buffer->size()) == REQUIRE(process->vm_manager.UnmapRange(target_address, output_buffer->size()) ==
RESULT_SUCCESS); RESULT_SUCCESS);
} }
CoreTiming::Shutdown();
} }
} // namespace Kernel } // namespace Kernel

View file

@ -3,14 +3,17 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include "core/core_timing.h"
#include "core/hle/kernel/memory.h" #include "core/hle/kernel/memory.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "core/hle/shared_page.h" #include "core/hle/shared_page.h"
#include "core/memory.h" #include "core/memory.h"
TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") { TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") {
CoreTiming::Init();
Kernel::KernelSystem kernel(0);
SECTION("these regions should not be mapped on an empty process") { SECTION("these regions should not be mapped on an empty process") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0));
CHECK(Memory::IsValidVirtualAddress(*process, Memory::PROCESS_IMAGE_VADDR) == false); CHECK(Memory::IsValidVirtualAddress(*process, Memory::PROCESS_IMAGE_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::HEAP_VADDR) == false); CHECK(Memory::IsValidVirtualAddress(*process, Memory::HEAP_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::LINEAR_HEAP_VADDR) == false); CHECK(Memory::IsValidVirtualAddress(*process, Memory::LINEAR_HEAP_VADDR) == false);
@ -21,14 +24,14 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") {
} }
SECTION("CONFIG_MEMORY_VADDR and SHARED_PAGE_VADDR should be valid after mapping them") { SECTION("CONFIG_MEMORY_VADDR and SHARED_PAGE_VADDR should be valid after mapping them") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0));
Kernel::MapSharedPages(process->vm_manager); Kernel::MapSharedPages(process->vm_manager);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == true); CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == true);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == true); CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == true);
} }
SECTION("special regions should be valid after mapping them") { SECTION("special regions should be valid after mapping them") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0));
SECTION("VRAM") { SECTION("VRAM") {
Kernel::HandleSpecialMapping(process->vm_manager, Kernel::HandleSpecialMapping(process->vm_manager,
{Memory::VRAM_VADDR, Memory::VRAM_SIZE, false, false}); {Memory::VRAM_VADDR, Memory::VRAM_SIZE, false, false});
@ -43,9 +46,11 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") {
} }
SECTION("Unmapping a VAddr should make it invalid") { SECTION("Unmapping a VAddr should make it invalid") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0));
Kernel::MapSharedPages(process->vm_manager); Kernel::MapSharedPages(process->vm_manager);
process->vm_manager.UnmapRange(Memory::CONFIG_MEMORY_VADDR, Memory::CONFIG_MEMORY_SIZE); process->vm_manager.UnmapRange(Memory::CONFIG_MEMORY_VADDR, Memory::CONFIG_MEMORY_SIZE);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == false); CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == false);
} }
CoreTiming::Shutdown();
} }