2017-06-05 04:52:19 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-06-09 06:52:30 +00:00
|
|
|
#include <array>
|
2017-06-06 05:39:26 +00:00
|
|
|
#include <memory>
|
2017-06-05 04:52:19 +00:00
|
|
|
#include <vector>
|
2017-06-11 19:51:05 +00:00
|
|
|
#include <boost/container/small_vector.hpp>
|
2017-06-09 06:52:30 +00:00
|
|
|
#include "common/common_types.h"
|
2017-06-09 12:23:13 +00:00
|
|
|
#include "common/swap.h"
|
2017-06-09 06:52:30 +00:00
|
|
|
#include "core/hle/ipc.h"
|
2017-06-05 04:52:19 +00:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2017-06-07 04:20:52 +00:00
|
|
|
#include "core/hle/kernel/server_session.h"
|
2017-06-05 04:52:19 +00:00
|
|
|
|
2017-06-07 04:20:52 +00:00
|
|
|
namespace Service {
|
|
|
|
class ServiceFrameworkBase;
|
|
|
|
}
|
2017-06-05 04:52:19 +00:00
|
|
|
|
2017-06-07 04:20:52 +00:00
|
|
|
namespace Kernel {
|
2017-06-05 04:52:19 +00:00
|
|
|
|
2017-06-09 12:23:13 +00:00
|
|
|
class HandleTable;
|
|
|
|
class Process;
|
|
|
|
|
2017-06-05 04:52:19 +00:00
|
|
|
/**
|
|
|
|
* Interface implemented by HLE Session handlers.
|
|
|
|
* This can be provided to a ServerSession in order to hook into several relevant events
|
|
|
|
* (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
|
|
|
|
*/
|
2017-06-06 05:39:26 +00:00
|
|
|
class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
|
2017-06-05 04:52:19 +00:00
|
|
|
public:
|
2017-06-06 21:51:42 +00:00
|
|
|
virtual ~SessionRequestHandler() = default;
|
|
|
|
|
2017-06-05 04:52:19 +00:00
|
|
|
/**
|
|
|
|
* Handles a sync request from the emulated application.
|
|
|
|
* @param server_session The ServerSession that was triggered for this sync request,
|
|
|
|
* it should be used to differentiate which client (As in ClientSession) we're answering to.
|
|
|
|
* TODO(Subv): Use a wrapper structure to hold all the information relevant to
|
|
|
|
* this request (ServerSession, Originator thread, Translated command buffer, etc).
|
|
|
|
* @returns ResultCode the result code of the translate operation.
|
|
|
|
*/
|
2017-06-06 21:51:42 +00:00
|
|
|
virtual void HandleSyncRequest(SharedPtr<ServerSession> server_session) = 0;
|
2017-06-05 04:52:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals that a client has just connected to this HLE handler and keeps the
|
|
|
|
* associated ServerSession alive for the duration of the connection.
|
|
|
|
* @param server_session Owning pointer to the ServerSession associated with the connection.
|
|
|
|
*/
|
2017-06-06 21:51:42 +00:00
|
|
|
void ClientConnected(SharedPtr<ServerSession> server_session);
|
2017-06-05 04:52:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals that a client has just disconnected from this HLE handler and releases the
|
|
|
|
* associated ServerSession.
|
|
|
|
* @param server_session ServerSession associated with the connection.
|
|
|
|
*/
|
2017-06-06 21:51:42 +00:00
|
|
|
void ClientDisconnected(SharedPtr<ServerSession> server_session);
|
2017-06-05 04:52:19 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// List of sessions that are connected to this handler.
|
|
|
|
/// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
|
|
|
|
// for the duration of the connection.
|
2017-06-06 21:51:42 +00:00
|
|
|
std::vector<SharedPtr<ServerSession>> connected_sessions;
|
2017-06-05 04:52:19 +00:00
|
|
|
};
|
|
|
|
|
2017-06-07 04:20:52 +00:00
|
|
|
/**
|
|
|
|
* Class containing information about an in-flight IPC request being handled by an HLE service
|
|
|
|
* implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and
|
|
|
|
* when possible use the APIs in this class to service the request.
|
2017-06-09 12:23:13 +00:00
|
|
|
*
|
|
|
|
* HLE handle protocol
|
|
|
|
* ===================
|
|
|
|
*
|
|
|
|
* To avoid needing HLE services to keep a separate handle table, or having to directly modify the
|
|
|
|
* requester's table, a tweaked protocol is used to receive and send handles in requests. The kernel
|
|
|
|
* will decode the incoming handles into object pointers and insert a id in the buffer where the
|
|
|
|
* handle would normally be. The service then calls GetIncomingHandle() with that id to get the
|
|
|
|
* pointer to the object. Similarly, instead of inserting a handle into the command buffer, the
|
|
|
|
* service calls AddOutgoingHandle() and stores the returned id where the handle would normally go.
|
|
|
|
*
|
|
|
|
* The end result is similar to just giving services their own real handle tables, but since these
|
|
|
|
* ids are local to a specific context, it avoids requiring services to manage handles for objects
|
|
|
|
* across multiple calls and ensuring that unneeded handles are cleaned up.
|
2017-06-07 04:20:52 +00:00
|
|
|
*/
|
|
|
|
class HLERequestContext {
|
|
|
|
public:
|
2017-06-18 23:05:12 +00:00
|
|
|
HLERequestContext(SharedPtr<ServerSession> session);
|
2017-06-07 04:20:52 +00:00
|
|
|
~HLERequestContext();
|
|
|
|
|
|
|
|
/// Returns a pointer to the IPC command buffer for this request.
|
2017-06-09 06:52:30 +00:00
|
|
|
u32* CommandBuffer() {
|
|
|
|
return cmd_buf.data();
|
2017-06-07 04:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the session through which this request was made. This can be used as a map key to
|
|
|
|
* access per-client data on services.
|
|
|
|
*/
|
|
|
|
SharedPtr<ServerSession> Session() const {
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2017-06-09 12:23:13 +00:00
|
|
|
/**
|
|
|
|
* Resolves a object id from the request command buffer into a pointer to an object. See the
|
|
|
|
* "HLE handle protocol" section in the class documentation for more details.
|
|
|
|
*/
|
|
|
|
SharedPtr<Object> GetIncomingHandle(u32 id_from_cmdbuf) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an outgoing object to the response, returning the id which should be used to reference
|
|
|
|
* it. See the "HLE handle protocol" section in the class documentation for more details.
|
|
|
|
*/
|
|
|
|
u32 AddOutgoingHandle(SharedPtr<Object> object);
|
2017-06-09 06:55:18 +00:00
|
|
|
|
2017-06-11 00:57:08 +00:00
|
|
|
/**
|
|
|
|
* Discards all Objects from the context, invalidating all ids. This may be called after reading
|
|
|
|
* out all incoming objects, so that the buffer memory can be re-used for outgoing handles, but
|
|
|
|
* this is not required.
|
|
|
|
*/
|
|
|
|
void ClearIncomingObjects();
|
|
|
|
|
2017-10-15 02:18:42 +00:00
|
|
|
void ParseCommandBuffer(u32_le* src_cmdbuf);
|
|
|
|
|
2017-06-18 23:05:12 +00:00
|
|
|
/// Populates this context with data from the requesting process/thread.
|
2017-10-15 02:18:42 +00:00
|
|
|
ResultCode PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf, Process& src_process,
|
2017-06-09 12:23:13 +00:00
|
|
|
HandleTable& src_table);
|
2017-06-18 23:05:12 +00:00
|
|
|
/// Writes data from this context back to the requesting process/thread.
|
2017-06-09 12:23:13 +00:00
|
|
|
ResultCode WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
|
2017-10-15 02:18:42 +00:00
|
|
|
HandleTable& dst_table);
|
|
|
|
|
|
|
|
u32_le GetCommand() const {
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::CommandType GetCommandType() const {
|
|
|
|
return command_header->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned GetDataPayloadOffset() const {
|
|
|
|
return data_payload_offset;
|
|
|
|
}
|
2017-06-09 12:23:13 +00:00
|
|
|
|
2017-06-18 23:05:12 +00:00
|
|
|
private:
|
2017-06-09 06:52:30 +00:00
|
|
|
std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
|
2017-06-07 04:20:52 +00:00
|
|
|
SharedPtr<ServerSession> session;
|
2017-06-11 19:51:05 +00:00
|
|
|
// TODO(yuriks): Check common usage of this and optimize size accordingly
|
|
|
|
boost::container::small_vector<SharedPtr<Object>, 8> request_handles;
|
2017-10-15 02:18:42 +00:00
|
|
|
|
|
|
|
std::unique_ptr<IPC::CommandHeader> command_header;
|
|
|
|
std::unique_ptr<IPC::HandleDescriptorHeader> handle_descriptor_header;
|
|
|
|
std::unique_ptr<IPC::DataPayloadHeader> data_payload_header;
|
|
|
|
|
|
|
|
unsigned data_payload_offset{};
|
|
|
|
u32_le command{};
|
2017-06-07 04:20:52 +00:00
|
|
|
};
|
|
|
|
|
2017-06-05 04:52:19 +00:00
|
|
|
} // namespace Kernel
|