hipc: begin implementing domains. fixes ro + sm together
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2019 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 <stratosphere.hpp>
|
||||
|
||||
namespace sts::sf::cmif {
|
||||
|
||||
ServerDomainManager::Domain::~Domain() {
|
||||
while (!this->entries.empty()) {
|
||||
Entry *entry = &this->entries.front();
|
||||
{
|
||||
std::scoped_lock lk(this->manager->entry_owner_lock);
|
||||
STS_ASSERT(entry->owner == this);
|
||||
entry->owner = nullptr;
|
||||
}
|
||||
entry->object.Reset();
|
||||
this->entries.pop_front();
|
||||
this->manager->entry_manager.FreeEntry(entry);
|
||||
}
|
||||
}
|
||||
|
||||
Result ServerDomainManager::Domain::ReserveIds(DomainObjectId *out_ids, size_t count) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
Entry *entry = this->manager->entry_manager.AllocateEntry();
|
||||
R_UNLESS(entry != nullptr, ResultServiceFrameworkOutOfDomainEntries);
|
||||
STS_ASSERT(entry->owner == nullptr);
|
||||
out_ids[i] = this->manager->entry_manager.GetId(entry);
|
||||
}
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result ServerDomainManager::Domain::AlterReservedIds(const DomainObjectId *old_reserved_ids, const DomainObjectId *new_reserved_ids, size_t count) {
|
||||
this->manager->entry_manager.ReallocateEntries(old_reserved_ids, new_reserved_ids, count);
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
void ServerDomainManager::Domain::UnreserveIds(const DomainObjectId *ids, size_t count) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
Entry *entry = this->manager->entry_manager.GetEntry(ids[i]);
|
||||
STS_ASSERT(entry != nullptr);
|
||||
STS_ASSERT(entry->owner == nullptr);
|
||||
this->manager->entry_manager.FreeEntry(entry);
|
||||
}
|
||||
}
|
||||
|
||||
void ServerDomainManager::Domain::RegisterObject(DomainObjectId id, ServiceObjectHolder &&obj) {
|
||||
Entry *entry = this->manager->entry_manager.GetEntry(id);
|
||||
STS_ASSERT(entry != nullptr);
|
||||
{
|
||||
std::scoped_lock lk(this->manager->entry_owner_lock);
|
||||
STS_ASSERT(entry->owner == nullptr);
|
||||
entry->owner = this;
|
||||
this->entries.push_back(*entry);
|
||||
}
|
||||
entry->object = std::move(obj);
|
||||
}
|
||||
|
||||
ServiceObjectHolder ServerDomainManager::Domain::UnregisterObject(DomainObjectId id) {
|
||||
ServiceObjectHolder obj;
|
||||
Entry *entry = this->manager->entry_manager.GetEntry(id);
|
||||
if (entry == nullptr) {
|
||||
return ServiceObjectHolder();
|
||||
}
|
||||
{
|
||||
std::scoped_lock lk(this->manager->entry_owner_lock);
|
||||
if (entry->owner != this) {
|
||||
return ServiceObjectHolder();
|
||||
}
|
||||
entry->owner = nullptr;
|
||||
obj = std::move(entry->object);
|
||||
this->entries.erase(this->entries.iterator_to(*entry));
|
||||
}
|
||||
this->manager->entry_manager.FreeEntry(entry);
|
||||
return obj;
|
||||
}
|
||||
|
||||
ServiceObjectHolder ServerDomainManager::Domain::GetObject(DomainObjectId id) {
|
||||
Entry *entry = this->manager->entry_manager.GetEntry(id);
|
||||
if (entry == nullptr) {
|
||||
return ServiceObjectHolder();
|
||||
}
|
||||
{
|
||||
std::scoped_lock lk(this->manager->entry_owner_lock);
|
||||
if (entry->owner != this) {
|
||||
return ServiceObjectHolder();
|
||||
}
|
||||
}
|
||||
return entry->object.Clone();
|
||||
}
|
||||
|
||||
ServerDomainManager::EntryManager::EntryManager(DomainEntryStorage *entry_storage, size_t entry_count) {
|
||||
this->entries = reinterpret_cast<Entry *>(entry_storage);
|
||||
this->num_entries = entry_count;
|
||||
for (size_t i = 0; i < this->num_entries; i++) {
|
||||
Entry *entry = new (this->entries + i) Entry();
|
||||
this->free_list.push_back(*entry);
|
||||
}
|
||||
}
|
||||
|
||||
ServerDomainManager::EntryManager::~EntryManager() {
|
||||
for (size_t i = 0; i < this->num_entries; i++) {
|
||||
this->entries[i].~Entry();
|
||||
}
|
||||
}
|
||||
|
||||
ServerDomainManager::Entry *ServerDomainManager::EntryManager::AllocateEntry() {
|
||||
std::scoped_lock lk(this->lock);
|
||||
if (this->free_list.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
Entry *e = &this->free_list.front();
|
||||
this->free_list.pop_front();
|
||||
return e;
|
||||
}
|
||||
|
||||
void ServerDomainManager::EntryManager::FreeEntry(Entry *entry) {
|
||||
std::scoped_lock lk(this->lock);
|
||||
STS_ASSERT(entry->owner == nullptr);
|
||||
STS_ASSERT(!entry->object);
|
||||
this->free_list.push_front(*entry);
|
||||
}
|
||||
|
||||
void ServerDomainManager::EntryManager::ReallocateEntries(const DomainObjectId *old_reserved_ids, const DomainObjectId *new_reserved_ids, size_t count) {
|
||||
std::scoped_lock lk(this->lock);
|
||||
|
||||
/* Free old ids. */
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const auto id = old_reserved_ids[i];
|
||||
Entry *entry = this->GetEntry(id);
|
||||
if (id != InvalidDomainObjectId) {
|
||||
STS_ASSERT(entry != nullptr);
|
||||
STS_ASSERT(entry->owner == nullptr);
|
||||
STS_ASSERT(!entry->object);
|
||||
this->free_list.push_front(*entry);
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate new IDs. */
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const auto id = old_reserved_ids[i];
|
||||
Entry *entry = this->GetEntry(id);
|
||||
if (id != InvalidDomainObjectId) {
|
||||
STS_ASSERT(entry != nullptr);
|
||||
this->free_list.erase(this->free_list.iterator_to(*entry));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,7 +40,7 @@ namespace sts::sf::cmif {
|
||||
|
||||
/* Invoke handler. */
|
||||
CmifOutHeader *out_header = nullptr;
|
||||
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*out_header), in_raw_data.GetSize() - sizeof(*out_header)));
|
||||
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*in_header), in_raw_data.GetSize() - sizeof(*in_header)));
|
||||
|
||||
/* Forward forwardable results, otherwise ensure we can send result to user. */
|
||||
R_TRY_CATCH(command_result) {
|
||||
@@ -84,7 +84,7 @@ namespace sts::sf::cmif {
|
||||
|
||||
/* Invoke handler. */
|
||||
CmifOutHeader *out_header = nullptr;
|
||||
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*out_header), in_raw_data.GetSize() - sizeof(*out_header)));
|
||||
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*in_header), in_raw_data.GetSize() - sizeof(*in_header)));
|
||||
|
||||
/* Forward forwardable results, otherwise ensure we can send result to user. */
|
||||
R_TRY_CATCH(command_result) {
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2019 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 <stratosphere.hpp>
|
||||
|
||||
namespace sts::sf::hipc {
|
||||
|
||||
namespace impl {
|
||||
|
||||
class HipcManager : public IServiceObject {
|
||||
private:
|
||||
enum class CommandId {
|
||||
ConvertCurrentObjectToDomain = 0,
|
||||
CopyFromCurrentDomain = 1,
|
||||
CloneCurrentObject = 2,
|
||||
QueryPointerBufferSize = 3,
|
||||
CloneCurrentObjectEx = 4,
|
||||
};
|
||||
private:
|
||||
ServerDomainSessionManager *manager;
|
||||
ServerSession *session;
|
||||
bool is_mitm_session;
|
||||
private:
|
||||
Result CloneCurrentObjectImpl(Handle *out_client_handle, ServerSessionManager *tagged_manager) {
|
||||
/* Clone the object. */
|
||||
cmif::ServiceObjectHolder &&clone = this->session->srv_obj_holder.Clone();
|
||||
R_UNLESS(clone, ResultHipcDomainObjectNotFound);
|
||||
|
||||
/* Create new session handles. */
|
||||
Handle server_handle;
|
||||
R_ASSERT(hipc::CreateSession(&server_handle, out_client_handle));
|
||||
|
||||
/* Register with manager. */
|
||||
if (!is_mitm_session) {
|
||||
R_ASSERT(tagged_manager->RegisterSession(server_handle, std::move(clone)));
|
||||
} else {
|
||||
std::shared_ptr<::Service> forward_service = this->session->forward_service;
|
||||
R_ASSERT(tagged_manager->RegisterMitmSession(server_handle, std::move(clone), std::move(forward_service)));
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
public:
|
||||
explicit HipcManager(ServerDomainSessionManager *m, ServerSession *s) : manager(m), session(s), is_mitm_session(s->forward_service != nullptr) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* TODO: ConvertCurrentObjectToDomain */
|
||||
/* TODO: CopyFromCurrentDomain */
|
||||
|
||||
Result CloneCurrentObject(sf::OutMoveHandle out) {
|
||||
return this->CloneCurrentObjectImpl(out.GetHandlePointer(), this->manager);
|
||||
}
|
||||
|
||||
void QueryPointerBufferSize(sf::Out<u16> out) {
|
||||
out.SetValue(this->session->pointer_buffer.GetSize());
|
||||
}
|
||||
|
||||
Result CloneCurrentObjectEx(sf::OutMoveHandle out, u32 tag) {
|
||||
return this->CloneCurrentObjectImpl(out.GetHandlePointer(), this->manager->GetSessionManagerByTag(tag));
|
||||
}
|
||||
|
||||
public:
|
||||
DEFINE_SERVICE_DISPATCH_TABLE {
|
||||
/* TODO: MAKE_SERVICE_COMMAND_META(ConvertCurrentObjectToDomain), */
|
||||
/* TODO: MAKE_SERVICE_COMMAND_META(CopyFromCurrentDomain), */
|
||||
MAKE_SERVICE_COMMAND_META(CloneCurrentObject),
|
||||
MAKE_SERVICE_COMMAND_META(QueryPointerBufferSize),
|
||||
MAKE_SERVICE_COMMAND_META(CloneCurrentObjectEx),
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Result ServerDomainSessionManager::DispatchManagerRequest(ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
|
||||
/* Make a stack object, and pass a shared pointer to it to DispatchRequest. */
|
||||
/* Note: This is safe, as no additional references to the hipc manager can ever be stored. */
|
||||
/* The shared pointer to stack object is definitely gross, though. */
|
||||
impl::HipcManager hipc_manager(this, session);
|
||||
return this->DispatchRequest(cmif::ServiceObjectHolder(std::shared_ptr<impl::HipcManager>(&hipc_manager, [](impl::HipcManager *) { /* Empty deleter. */ })), session, in_message, out_message);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user