Skip to content

Commit

Permalink
kern: implement KEvent, KWritableEvent, KServerSession::OnClientClosed
Browse files Browse the repository at this point in the history
  • Loading branch information
SciresM committed Jul 10, 2020
1 parent d0d8914 commit 16c9c53
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ namespace ams::kern::arch::arm64 {
return this->page_table.MakeAndOpenPageGroup(out, address, num_pages, state_mask, state, perm_mask, perm, attr_mask, attr);
}

Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size) {
return this->page_table.UnlockForIpcUserBuffer(address, size);
}

bool GetPhysicalAddress(KPhysicalAddress *out, KProcessAddress address) const {
return this->page_table.GetPhysicalAddress(out, address);
}
Expand Down
25 changes: 24 additions & 1 deletion libraries/libmesosphere/include/mesosphere/kern_k_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,36 @@
#include <mesosphere/kern_k_auto_object.hpp>
#include <mesosphere/kern_slab_helpers.hpp>
#include <mesosphere/kern_k_readable_event.hpp>
#include <mesosphere/kern_k_writable_event.hpp>

namespace ams::kern {

class KEvent final : public KAutoObjectWithSlabHeapAndContainer<KEvent, KAutoObjectWithList> {
MESOSPHERE_AUTOOBJECT_TRAITS(KEvent, KAutoObject);
private:
KReadableEvent readable_event;
KWritableEvent writable_event;
KProcess *owner;
bool initialized;
public:
/* TODO: This is a placeholder definition. */
constexpr KEvent()
: readable_event(), writable_event(), owner(), initialized()
{
/* ... */
}

virtual ~KEvent() { /* ... */ }

void Initialize();
virtual void Finalize() override;

virtual bool IsInitialized() const override { return this->initialized; }
virtual uintptr_t GetPostDestroyArgument() const override { return reinterpret_cast<uintptr_t>(this->owner); }

static void PostDestroy(uintptr_t arg);

KReadableEvent &GetReadableEvent() { return this->readable_event; }
KWritableEvent &GetWritableEvent() { return this->writable_event; }
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ namespace ams::kern {
return this->CheckMemoryState(nullptr, nullptr, nullptr, addr, size, state_mask, state, perm_mask, perm, attr_mask, attr, ignore_attr);
}

Result UnlockMemory(KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, KMemoryPermission new_perm, u32 lock_attr, const KPageGroup *pg);

Result QueryInfoImpl(KMemoryInfo *out_info, ams::svc::PageInfo *out_page, KProcessAddress address) const;
Result AllocateAndMapPagesImpl(PageLinkedList *page_list, KProcessAddress address, size_t num_pages, const KPageProperties properties);
Result MapPageGroupImpl(PageLinkedList *page_list, KProcessAddress address, const KPageGroup &pg, const KPageProperties properties, bool reuse_ll);
Expand Down Expand Up @@ -273,6 +275,8 @@ namespace ams::kern {
Result UnmapPageGroup(KProcessAddress address, const KPageGroup &pg, KMemoryState state);

Result MakeAndOpenPageGroup(KPageGroup *out, KProcessAddress address, size_t num_pages, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr);

Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size);
public:
KProcessAddress GetAddressSpaceStart() const { return this->address_space_start; }
KProcessAddress GetHeapRegionStart() const { return this->heap_region_start; }
Expand Down
114 changes: 113 additions & 1 deletion libraries/libmesosphere/include/mesosphere/kern_k_session_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,125 @@
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_auto_object.hpp>
#include <mesosphere/kern_slab_helpers.hpp>
#include <mesosphere/kern_k_writable_event.hpp>
#include <mesosphere/kern_k_thread.hpp>
#include <mesosphere/kern_k_process.hpp>
#include <mesosphere/kern_k_memory_block.hpp>

namespace ams::kern {

class KSessionRequest final : public KSlabAllocated<KSessionRequest>, public KAutoObject, public util::IntrusiveListBaseNode<KSessionRequest> {
MESOSPHERE_AUTOOBJECT_TRAITS(KSessionRequest, KAutoObject);
public:
/* TODO: This is a placeholder definition. */
class SessionMappings {
private:
static constexpr size_t NumStaticMappings = 8;

class Mapping {
private:
KProcessAddress client_address;
KProcessAddress server_address;
size_t size;
KMemoryState state;
public:
constexpr void Set(KProcessAddress c, KProcessAddress s, size_t sz, KMemoryState st) {
this->client_address = c;
this->server_address = s;
this->size = sz;
this->state = st;
}

constexpr KProcessAddress GetClientAddress() const { return this->client_address; }
constexpr KProcessAddress GetServerAddress() const { return this->server_address; }
constexpr size_t GetSize() const { return this->size; }
constexpr KMemoryState GetMemoryState() const { return this->state; }
};
private:
Mapping static_mappings[NumStaticMappings];
Mapping *mappings;
u8 num_send;
u8 num_recv;
u8 num_exch;
public:
constexpr explicit SessionMappings() : static_mappings(), mappings(), num_send(), num_recv(), num_exch() { /* ... */ }

void Initialize() { /* ... */ }
void Finalize();

size_t GetSendCount() const { return this->num_send; }
size_t GetReceiveCount() const { return this->num_recv; }
size_t GetExchangeCount() const { return this->num_exch; }

/* TODO: More functionality. */
};
private:
SessionMappings mappings;
KThread *thread;
KProcess *server;
KWritableEvent *event;
uintptr_t address;
size_t size;
public:
constexpr KSessionRequest() : mappings(), thread(), server(), event(), address(), size() { /* ... */ }
virtual ~KSessionRequest() { /* ... */ }

static KSessionRequest *Create() {
KSessionRequest *req = KSessionRequest::Allocate();
if (req != nullptr) {
KAutoObject::Create(req);
}
return req;
}

virtual void Destroy() override {
this->Finalize();
KSessionRequest::Free(this);
}

void Initialize(KWritableEvent *event, uintptr_t address, size_t size) {
this->mappings.Initialize();

this->thread = std::addressof(GetCurrentThread());
this->event = event;
this->address = address;
this->size = size;

this->thread->Open();
if (this->event != nullptr) {
this->event->Open();
}
}

virtual void Finalize() override {
this->mappings.Finalize();

if (this->thread) {
this->thread->Close();
}
if (this->event) {
this->event->Close();
}
if (this->server) {
this->server->Close();
}
}

static void PostDestroy(uintptr_t arg) { /* ... */ }

KThread *GetThread() const { return this->thread; }
KWritableEvent *GetEvent() const { return this->event; }
uintptr_t GetAddress() const { return this->address; }
size_t GetSize() const { return this->size; }
KProcess *GetServerProcess() const { return this->server; }

void ClearThread() { this->thread = nullptr; }
void ClearEvent() { this->event = nullptr; }

size_t GetSendCount() const { return this->mappings.GetSendCount(); }
size_t GetReceiveCount() const { return this->mappings.GetReceiveCount(); }
size_t GetExchangeCount() const { return this->mappings.GetExchangeCount(); }

/* TODO: More functionality. */
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_auto_object.hpp>
#include <mesosphere/kern_slab_helpers.hpp>

namespace ams::kern {

class KEvent;

class KWritableEvent final : public KAutoObjectWithSlabHeapAndContainer<KWritableEvent, KAutoObjectWithList> {
MESOSPHERE_AUTOOBJECT_TRAITS(KWritableEvent, KAutoObject);
private:
KEvent *parent;
public:
constexpr explicit KWritableEvent() : parent(nullptr) { /* ... */ }
virtual ~KWritableEvent() { /* ... */ }

virtual void Destroy() override;

static void PostDestroy(uintptr_t arg) { /* ... */ }

void Initialize(KEvent *p);
Result Signal();
Result Clear();

constexpr KEvent *GetParent() const { return this->parent; }
};

}
58 changes: 58 additions & 0 deletions libraries/libmesosphere/source/kern_k_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mesosphere.hpp>

namespace ams::kern {

void KEvent::Initialize() {
MESOSPHERE_ASSERT_THIS();

/* Increment reference count. */
/* Because reference count is one on creation, this will result */
/* in a reference count of two. Thus, when both readable and */
/* writable events are closed this object will be destroyed. */
this->Open();

/* Create our sub events. */
KAutoObject::Create(std::addressof(this->readable_event));
KAutoObject::Create(std::addressof(this->writable_event));

/* Initialize our sub sessions. */
this->readable_event.Initialize(this);
this->writable_event.Initialize(this);

/* Set our owner process. */
this->owner = GetCurrentProcessPointer();
this->owner->Open();

/* Mark initialized. */
this->initialized = true;
}

void KEvent::Finalize() {
MESOSPHERE_ASSERT_THIS();

KAutoObjectWithSlabHeapAndContainer<KEvent, KAutoObjectWithList>::Finalize();
}

void KEvent::PostDestroy(uintptr_t arg) {
/* Release the event count resource the owner process holds. */
KProcess *owner = reinterpret_cast<KProcess *>(arg);
owner->ReleaseResource(ams::svc::LimitableResource_EventCountMax, 1);
owner->Close();
}

}
57 changes: 56 additions & 1 deletion libraries/libmesosphere/source/kern_k_page_table_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,52 @@ namespace ams::kern {
return ResultSuccess();
}

Result KPageTableBase::UnlockMemory(KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, KMemoryPermission new_perm, u32 lock_attr, const KPageGroup *pg) {
/* Validate basic preconditions. */
MESOSPHERE_ASSERT((attr_mask & lock_attr) == lock_attr);
MESOSPHERE_ASSERT((attr & lock_attr) == lock_attr);

/* Validate the unlock request. */
const size_t num_pages = size / PageSize;
R_UNLESS(this->Contains(addr, size), svc::ResultInvalidCurrentMemory());

/* Lock the table. */
KScopedLightLock lk(this->general_lock);

/* Check the state. */
KMemoryState old_state;
KMemoryPermission old_perm;
KMemoryAttribute old_attr;
R_TRY(this->CheckMemoryState(std::addressof(old_state), std::addressof(old_perm), std::addressof(old_attr), addr, size, state_mask | KMemoryState_FlagReferenceCounted, state | KMemoryState_FlagReferenceCounted, perm_mask, perm, attr_mask, attr));

/* Check the page group. */
if (pg != nullptr) {
R_UNLESS(this->IsValidPageGroup(*pg, addr, num_pages), svc::ResultInvalidMemoryRegion());
}

/* Decide on new perm and attr. */
new_perm = (new_perm != KMemoryPermission_None) ? new_perm : old_perm;
KMemoryAttribute new_attr = static_cast<KMemoryAttribute>(old_attr & ~lock_attr);

/* Create an update allocator. */
KMemoryBlockManagerUpdateAllocator allocator(this->memory_block_slab_manager);
R_TRY(allocator.GetResult());

/* Update permission, if we need to. */
if (new_perm != old_perm) {
/* We're going to perform an update, so create a helper. */
KScopedPageTableUpdater updater(this);

const KPageProperties properties = { new_perm, false, (old_attr & KMemoryAttribute_Uncached) != 0, false };
R_TRY(this->Operate(updater.GetPageList(), addr, num_pages, Null<KPhysicalAddress>, false, properties, OperationType_ChangePermissions, false));
}

/* Apply the memory block updates. */
this->memory_block_manager.Update(std::addressof(allocator), addr, num_pages, old_state, new_perm, new_attr);

return ResultSuccess();
}

Result KPageTableBase::QueryInfoImpl(KMemoryInfo *out_info, ams::svc::PageInfo *out_page, KProcessAddress address) const {
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
MESOSPHERE_ASSERT(out_info != nullptr);
Expand Down Expand Up @@ -1086,7 +1132,7 @@ namespace ams::kern {

Result KPageTableBase::MakeAndOpenPageGroup(KPageGroup *out, KProcessAddress address, size_t num_pages, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr) {
/* Ensure that the page group isn't null. */
AMS_ASSERT(out != nullptr);
MESOSPHERE_ASSERT(out != nullptr);

/* Make sure that the region we're mapping is valid for the table. */
const size_t size = num_pages * PageSize;
Expand All @@ -1107,4 +1153,13 @@ namespace ams::kern {
return ResultSuccess();
}

Result KPageTableBase::UnlockForIpcUserBuffer(KProcessAddress address, size_t size) {
return this->UnlockMemory(address, size,
KMemoryState_FlagCanIpcUserBuffer, KMemoryState_FlagCanIpcUserBuffer,
KMemoryPermission_None, KMemoryPermission_None,
KMemoryAttribute_All, KMemoryAttribute_AnyLocked | KMemoryAttribute_Locked,
KMemoryPermission_UserReadWrite,
KMemoryAttribute_AnyLocked | KMemoryAttribute_Locked, nullptr);
}

}
Loading

0 comments on commit 16c9c53

Please sign in to comment.