2018-01-13 16:22:39 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-06-05 00:52:19 -04:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-06-09 02:52:30 -04:00
|
|
|
#include <array>
|
2017-06-06 01:39:26 -04:00
|
|
|
#include <memory>
|
2018-03-18 20:22:46 -04:00
|
|
|
#include <string>
|
2017-06-05 00:52:19 -04:00
|
|
|
#include <vector>
|
2017-06-11 15:51:05 -04:00
|
|
|
#include <boost/container/small_vector.hpp>
|
2017-06-09 02:52:30 -04:00
|
|
|
#include "common/common_types.h"
|
2017-06-09 08:23:13 -04:00
|
|
|
#include "common/swap.h"
|
2017-06-09 02:52:30 -04:00
|
|
|
#include "core/hle/ipc.h"
|
2017-06-05 00:52:19 -04:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2017-06-07 00:20:52 -04:00
|
|
|
#include "core/hle/kernel/server_session.h"
|
2018-03-18 20:17:06 -04:00
|
|
|
#include "core/hle/kernel/thread.h"
|
2017-06-05 00:52:19 -04:00
|
|
|
|
2017-06-07 00:20:52 -04:00
|
|
|
namespace Service {
|
|
|
|
class ServiceFrameworkBase;
|
|
|
|
}
|
2017-06-05 00:52:19 -04:00
|
|
|
|
2017-06-07 00:20:52 -04:00
|
|
|
namespace Kernel {
|
2017-06-05 00:52:19 -04:00
|
|
|
|
2017-12-29 00:36:22 -05:00
|
|
|
class Domain;
|
2017-06-09 08:23:13 -04:00
|
|
|
class HandleTable;
|
2017-12-29 00:36:22 -05:00
|
|
|
class HLERequestContext;
|
2017-06-09 08:23:13 -04:00
|
|
|
class Process;
|
2018-03-18 20:22:46 -04:00
|
|
|
class Event;
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-06-05 00:52:19 -04: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 01:39:26 -04:00
|
|
|
class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
|
2017-06-05 00:52:19 -04:00
|
|
|
public:
|
2017-06-06 17:51:42 -04:00
|
|
|
virtual ~SessionRequestHandler() = default;
|
|
|
|
|
2017-06-05 00:52:19 -04: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-12-29 00:36:22 -05:00
|
|
|
virtual ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) = 0;
|
2017-06-05 00:52:19 -04: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 17:51:42 -04:00
|
|
|
void ClientConnected(SharedPtr<ServerSession> server_session);
|
2017-06-05 00:52:19 -04: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 17:51:42 -04:00
|
|
|
void ClientDisconnected(SharedPtr<ServerSession> server_session);
|
2017-06-05 00:52:19 -04: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 17:51:42 -04:00
|
|
|
std::vector<SharedPtr<ServerSession>> connected_sessions;
|
2017-06-05 00:52:19 -04:00
|
|
|
};
|
|
|
|
|
2017-06-07 00:20:52 -04: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 08:23:13 -04: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 00:20:52 -04:00
|
|
|
*/
|
|
|
|
class HLERequestContext {
|
|
|
|
public:
|
2017-12-29 00:36:22 -05:00
|
|
|
HLERequestContext(SharedPtr<Kernel::ServerSession> session);
|
2017-06-07 00:20:52 -04:00
|
|
|
~HLERequestContext();
|
|
|
|
|
|
|
|
/// Returns a pointer to the IPC command buffer for this request.
|
2017-06-09 02:52:30 -04:00
|
|
|
u32* CommandBuffer() {
|
|
|
|
return cmd_buf.data();
|
2017-06-07 00:20:52 -04: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.
|
|
|
|
*/
|
2018-01-23 18:03:09 -05:00
|
|
|
const SharedPtr<Kernel::ServerSession>& Session() const {
|
2017-12-29 00:36:22 -05:00
|
|
|
return server_session;
|
2017-06-07 00:20:52 -04:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:22:46 -04:00
|
|
|
using WakeupCallback = std::function<void(SharedPtr<Thread> thread, HLERequestContext& context,
|
|
|
|
ThreadWakeupReason reason)>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts the specified guest thread to sleep until the returned event is signaled or until the
|
|
|
|
* specified timeout expires.
|
|
|
|
* @param thread Thread to be put to sleep.
|
|
|
|
* @param reason Reason for pausing the thread, to be used for debugging purposes.
|
|
|
|
* @param timeout Timeout in nanoseconds after which the thread will be awoken and the callback
|
|
|
|
* invoked with a Timeout reason.
|
|
|
|
* @param callback Callback to be invoked when the thread is resumed. This callback must write
|
|
|
|
* the entire command response once again, regardless of the state of it before this function
|
|
|
|
* was called.
|
|
|
|
* @returns Event that when signaled will resume the thread and call the callback function.
|
|
|
|
*/
|
|
|
|
SharedPtr<Event> SleepClientThread(SharedPtr<Thread> thread, const std::string& reason,
|
|
|
|
u64 timeout, WakeupCallback&& callback);
|
|
|
|
|
2017-10-15 01:24:22 -04:00
|
|
|
void ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming);
|
2017-10-14 22:18:42 -04:00
|
|
|
|
2017-06-18 19:05:12 -04:00
|
|
|
/// Populates this context with data from the requesting process/thread.
|
2017-10-14 22:18:42 -04:00
|
|
|
ResultCode PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf, Process& src_process,
|
2017-06-09 08:23:13 -04:00
|
|
|
HandleTable& src_table);
|
2017-06-18 19:05:12 -04:00
|
|
|
/// Writes data from this context back to the requesting process/thread.
|
2018-03-18 20:17:06 -04:00
|
|
|
ResultCode WriteToOutgoingCommandBuffer(Thread& thread);
|
2017-10-14 22:18:42 -04:00
|
|
|
|
|
|
|
u32_le GetCommand() const {
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::CommandType GetCommandType() const {
|
|
|
|
return command_header->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned GetDataPayloadOffset() const {
|
|
|
|
return data_payload_offset;
|
|
|
|
}
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-18 21:38:01 -04:00
|
|
|
const std::vector<IPC::BufferDescriptorX>& BufferDescriptorX() const {
|
|
|
|
return buffer_x_desciptors;
|
|
|
|
}
|
|
|
|
|
2018-01-06 21:14:14 -05:00
|
|
|
const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorA() const {
|
|
|
|
return buffer_a_desciptors;
|
|
|
|
}
|
|
|
|
|
2018-01-07 21:25:01 -05:00
|
|
|
const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorB() const {
|
|
|
|
return buffer_b_desciptors;
|
|
|
|
}
|
|
|
|
|
2018-01-18 14:54:34 -05:00
|
|
|
const std::vector<IPC::BufferDescriptorC>& BufferDescriptorC() const {
|
|
|
|
return buffer_c_desciptors;
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:18:42 -04:00
|
|
|
const std::shared_ptr<IPC::DomainMessageHeader>& GetDomainMessageHeader() const {
|
2017-12-29 00:36:22 -05:00
|
|
|
return domain_message_header;
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:41:20 -05:00
|
|
|
/// Helper function to read a buffer using the appropriate buffer descriptor
|
2018-05-01 16:28:36 -04:00
|
|
|
std::vector<u8> ReadBuffer(int buffer_index = 0) const;
|
2018-02-13 21:41:20 -05:00
|
|
|
|
|
|
|
/// Helper function to write a buffer using the appropriate buffer descriptor
|
2018-05-01 16:28:36 -04:00
|
|
|
size_t WriteBuffer(const void* buffer, size_t size, int buffer_index = 0) const;
|
2018-02-13 21:41:20 -05:00
|
|
|
|
|
|
|
/// Helper function to write a buffer using the appropriate buffer descriptor
|
2018-05-01 16:28:36 -04:00
|
|
|
size_t WriteBuffer(const std::vector<u8>& buffer, int buffer_index = 0) const;
|
2018-02-13 21:41:20 -05:00
|
|
|
|
2018-02-14 00:14:17 -05:00
|
|
|
/// Helper function to get the size of the input buffer
|
2018-05-01 16:28:36 -04:00
|
|
|
size_t GetReadBufferSize(int buffer_index = 0) const;
|
2018-02-14 00:14:17 -05:00
|
|
|
|
2018-02-13 21:41:20 -05:00
|
|
|
/// Helper function to get the size of the output buffer
|
2018-05-01 16:28:36 -04:00
|
|
|
size_t GetWriteBufferSize(int buffer_index = 0) const;
|
2018-02-13 21:41:20 -05:00
|
|
|
|
2018-01-16 13:05:21 -05:00
|
|
|
template <typename T>
|
2018-01-07 09:22:20 -05:00
|
|
|
SharedPtr<T> GetCopyObject(size_t index) {
|
|
|
|
ASSERT(index < copy_objects.size());
|
2018-01-11 22:24:09 -05:00
|
|
|
return DynamicObjectCast<T>(copy_objects[index]);
|
2018-01-07 09:22:20 -05:00
|
|
|
}
|
|
|
|
|
2018-01-16 13:05:21 -05:00
|
|
|
template <typename T>
|
2018-01-07 09:22:20 -05:00
|
|
|
SharedPtr<T> GetMoveObject(size_t index) {
|
|
|
|
ASSERT(index < move_objects.size());
|
2018-01-11 22:24:09 -05:00
|
|
|
return DynamicObjectCast<T>(move_objects[index]);
|
2018-01-07 09:22:20 -05:00
|
|
|
}
|
|
|
|
|
2018-01-07 01:50:55 -05:00
|
|
|
void AddMoveObject(SharedPtr<Object> object) {
|
|
|
|
move_objects.emplace_back(std::move(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddCopyObject(SharedPtr<Object> object) {
|
|
|
|
copy_objects.emplace_back(std::move(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddDomainObject(std::shared_ptr<SessionRequestHandler> object) {
|
|
|
|
domain_objects.emplace_back(std::move(object));
|
|
|
|
}
|
|
|
|
|
2018-01-15 15:31:10 -05:00
|
|
|
/// Clears the list of objects so that no lingering objects are written accidentally to the
|
|
|
|
/// response buffer.
|
|
|
|
void ClearIncomingObjects() {
|
|
|
|
move_objects.clear();
|
|
|
|
copy_objects.clear();
|
|
|
|
domain_objects.clear();
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:58:25 -05:00
|
|
|
size_t NumMoveObjects() const {
|
|
|
|
return move_objects.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t NumCopyObjects() const {
|
|
|
|
return copy_objects.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t NumDomainObjects() const {
|
|
|
|
return domain_objects.size();
|
|
|
|
}
|
|
|
|
|
2018-02-15 09:22:11 -05:00
|
|
|
std::string Description() const;
|
|
|
|
|
2017-06-18 19:05:12 -04:00
|
|
|
private:
|
2017-06-09 02:52:30 -04:00
|
|
|
std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
|
2017-12-29 00:36:22 -05:00
|
|
|
SharedPtr<Kernel::ServerSession> server_session;
|
2017-06-11 15:51:05 -04:00
|
|
|
// TODO(yuriks): Check common usage of this and optimize size accordingly
|
2018-01-07 01:50:55 -05:00
|
|
|
boost::container::small_vector<SharedPtr<Object>, 8> move_objects;
|
|
|
|
boost::container::small_vector<SharedPtr<Object>, 8> copy_objects;
|
|
|
|
boost::container::small_vector<std::shared_ptr<SessionRequestHandler>, 8> domain_objects;
|
2017-10-14 22:18:42 -04:00
|
|
|
|
2018-03-18 20:18:42 -04:00
|
|
|
std::shared_ptr<IPC::CommandHeader> command_header;
|
|
|
|
std::shared_ptr<IPC::HandleDescriptorHeader> handle_descriptor_header;
|
|
|
|
std::shared_ptr<IPC::DataPayloadHeader> data_payload_header;
|
|
|
|
std::shared_ptr<IPC::DomainMessageHeader> domain_message_header;
|
2017-10-18 21:38:01 -04:00
|
|
|
std::vector<IPC::BufferDescriptorX> buffer_x_desciptors;
|
|
|
|
std::vector<IPC::BufferDescriptorABW> buffer_a_desciptors;
|
|
|
|
std::vector<IPC::BufferDescriptorABW> buffer_b_desciptors;
|
|
|
|
std::vector<IPC::BufferDescriptorABW> buffer_w_desciptors;
|
2018-01-18 14:54:34 -05:00
|
|
|
std::vector<IPC::BufferDescriptorC> buffer_c_desciptors;
|
2017-10-14 22:18:42 -04:00
|
|
|
|
|
|
|
unsigned data_payload_offset{};
|
2018-01-18 14:54:34 -05:00
|
|
|
unsigned buffer_c_offset{};
|
2017-10-14 22:18:42 -04:00
|
|
|
u32_le command{};
|
2017-06-07 00:20:52 -04:00
|
|
|
};
|
|
|
|
|
2017-06-05 00:52:19 -04:00
|
|
|
} // namespace Kernel
|