2017-06-05 06:52:19 +02:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-12-13 19:56:53 +01:00
|
|
|
#include <algorithm>
|
2017-06-09 08:52:30 +02:00
|
|
|
#include <array>
|
2018-02-22 04:03:46 +01:00
|
|
|
#include <chrono>
|
2017-06-06 07:39:26 +02:00
|
|
|
#include <memory>
|
2017-11-10 00:13:11 +01:00
|
|
|
#include <string>
|
2017-06-05 06:52:19 +02:00
|
|
|
#include <vector>
|
2017-06-11 21:51:05 +02:00
|
|
|
#include <boost/container/small_vector.hpp>
|
2017-06-09 08:52:30 +02:00
|
|
|
#include "common/common_types.h"
|
2017-06-09 14:23:13 +02:00
|
|
|
#include "common/swap.h"
|
2017-06-09 08:52:30 +02:00
|
|
|
#include "core/hle/ipc.h"
|
2018-08-02 04:40:00 +02:00
|
|
|
#include "core/hle/kernel/object.h"
|
2017-06-07 06:20:52 +02:00
|
|
|
#include "core/hle/kernel/server_session.h"
|
2017-06-05 06:52:19 +02:00
|
|
|
|
2017-06-07 06:20:52 +02:00
|
|
|
namespace Service {
|
|
|
|
class ServiceFrameworkBase;
|
|
|
|
}
|
2017-06-05 06:52:19 +02:00
|
|
|
|
2019-02-02 21:55:45 +01:00
|
|
|
namespace Memory {
|
|
|
|
class MemorySystem;
|
|
|
|
}
|
|
|
|
|
2017-06-07 06:20:52 +02:00
|
|
|
namespace Kernel {
|
2017-06-05 06:52:19 +02:00
|
|
|
|
2017-06-09 14:23:13 +02:00
|
|
|
class HandleTable;
|
|
|
|
class Process;
|
2017-11-10 00:13:11 +01:00
|
|
|
class Thread;
|
|
|
|
class Event;
|
2019-02-02 21:18:01 +01:00
|
|
|
class HLERequestContext;
|
2019-02-02 21:55:45 +01:00
|
|
|
class KernelSystem;
|
2017-06-09 14:23:13 +02:00
|
|
|
|
2017-06-05 06:52:19 +02: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 07:39:26 +02:00
|
|
|
class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
|
2017-06-05 06:52:19 +02:00
|
|
|
public:
|
2017-06-06 23:51:42 +02:00
|
|
|
virtual ~SessionRequestHandler() = default;
|
|
|
|
|
2017-06-05 06:52:19 +02:00
|
|
|
/**
|
|
|
|
* Handles a sync request from the emulated application.
|
2019-02-02 21:18:01 +01:00
|
|
|
* @param context holds all the information relevant to his request (ServerSession, Translated
|
|
|
|
* command buffer, etc).
|
2017-06-05 06:52:19 +02:00
|
|
|
*/
|
2019-02-02 21:18:01 +01:00
|
|
|
virtual void HandleSyncRequest(Kernel::HLERequestContext& context) = 0;
|
2017-06-05 06:52:19 +02: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.
|
|
|
|
*/
|
2019-03-23 21:04:19 +01:00
|
|
|
virtual void ClientConnected(std::shared_ptr<ServerSession> server_session);
|
2017-06-05 06:52:19 +02: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.
|
|
|
|
*/
|
2019-03-23 21:04:19 +01:00
|
|
|
virtual void ClientDisconnected(std::shared_ptr<ServerSession> server_session);
|
2017-12-13 19:56:53 +01:00
|
|
|
|
|
|
|
/// Empty placeholder structure for services with no per-session data. The session data classes
|
|
|
|
/// in each service must inherit from this.
|
2018-01-03 16:52:46 +01:00
|
|
|
struct SessionDataBase {
|
|
|
|
virtual ~SessionDataBase() = default;
|
|
|
|
};
|
2017-06-05 06:52:19 +02:00
|
|
|
|
|
|
|
protected:
|
2017-12-13 19:56:53 +01:00
|
|
|
/// Creates the storage for the session data of the service.
|
|
|
|
virtual std::unique_ptr<SessionDataBase> MakeSessionData() const = 0;
|
|
|
|
|
|
|
|
/// Returns the session data associated with the server session.
|
|
|
|
template <typename T>
|
2019-03-23 21:04:19 +01:00
|
|
|
T* GetSessionData(std::shared_ptr<ServerSession> session) {
|
2017-12-13 19:56:53 +01:00
|
|
|
static_assert(std::is_base_of<SessionDataBase, T>(),
|
|
|
|
"T is not a subclass of SessionDataBase");
|
|
|
|
auto itr = std::find_if(connected_sessions.begin(), connected_sessions.end(),
|
|
|
|
[&](const SessionInfo& info) { return info.session == session; });
|
|
|
|
ASSERT(itr != connected_sessions.end());
|
|
|
|
return static_cast<T*>(itr->data.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SessionInfo {
|
2019-03-23 21:04:19 +01:00
|
|
|
SessionInfo(std::shared_ptr<ServerSession> session, std::unique_ptr<SessionDataBase> data);
|
2017-12-13 19:56:53 +01:00
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<ServerSession> session;
|
2017-12-13 19:56:53 +01:00
|
|
|
std::unique_ptr<SessionDataBase> data;
|
|
|
|
};
|
|
|
|
/// 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.
|
|
|
|
std::vector<SessionInfo> connected_sessions;
|
2017-06-05 06:52:19 +02:00
|
|
|
};
|
|
|
|
|
2017-07-14 21:24:57 +02:00
|
|
|
class MappedBuffer {
|
|
|
|
public:
|
2019-02-02 21:55:45 +01:00
|
|
|
MappedBuffer(Memory::MemorySystem& memory, const Process& process, u32 descriptor,
|
|
|
|
VAddr address, u32 id);
|
2017-07-14 21:24:57 +02:00
|
|
|
|
|
|
|
// interface for service
|
2018-09-06 22:03:28 +02:00
|
|
|
void Read(void* dest_buffer, std::size_t offset, std::size_t size);
|
|
|
|
void Write(const void* src_buffer, std::size_t offset, std::size_t size);
|
|
|
|
std::size_t GetSize() const {
|
2017-07-14 21:24:57 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface for ipc helper
|
|
|
|
u32 GenerateDescriptor() const {
|
|
|
|
return IPC::MappedBufferDesc(size, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetId() const {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class HLERequestContext;
|
2019-02-02 21:55:45 +01:00
|
|
|
Memory::MemorySystem* memory;
|
2017-07-14 21:24:57 +02:00
|
|
|
u32 id;
|
|
|
|
VAddr address;
|
|
|
|
const Process* process;
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t size;
|
2017-07-14 21:24:57 +02:00
|
|
|
IPC::MappedBufferPermissions perms;
|
|
|
|
};
|
|
|
|
|
2017-06-07 06:20:52 +02:00
|
|
|
/**
|
|
|
|
* Class containing information about an in-flight IPC request being handled by an HLE service
|
2018-10-23 17:40:57 +02:00
|
|
|
* implementation.
|
2017-06-09 14:23:13 +02: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-07-14 21:24:57 +02:00
|
|
|
*
|
|
|
|
* HLE mapped buffer protocol
|
|
|
|
* ==========================
|
|
|
|
*
|
|
|
|
* HLE services don't have their own virtual memory space, a tweaked protocol is used to simulate
|
|
|
|
* memory mapping. The kernel will wrap the incoming buffers into a memory interface on which HLE
|
|
|
|
* services can operate, and insert a id in the buffer where the vaddr would normally be. The
|
|
|
|
* service then calls GetMappedBuffer with that id to get the memory interface. On response, like
|
|
|
|
* real services pushing back the mapped buffer address to unmap it, HLE services push back the
|
|
|
|
* id of the memory interface and let kernel convert it back to client vaddr. No real unmapping is
|
|
|
|
* needed in this case, though.
|
2017-06-07 06:20:52 +02:00
|
|
|
*/
|
|
|
|
class HLERequestContext {
|
|
|
|
public:
|
2019-03-23 21:04:19 +01:00
|
|
|
HLERequestContext(KernelSystem& kernel, std::shared_ptr<ServerSession> session);
|
2017-06-07 06:20:52 +02:00
|
|
|
~HLERequestContext();
|
|
|
|
|
|
|
|
/// Returns a pointer to the IPC command buffer for this request.
|
2017-06-09 08:52:30 +02:00
|
|
|
u32* CommandBuffer() {
|
|
|
|
return cmd_buf.data();
|
2017-06-07 06:20:52 +02: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.
|
|
|
|
*/
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<ServerSession> Session() const {
|
2017-06-07 06:20:52 +02:00
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
using WakeupCallback = std::function<void(
|
|
|
|
std::shared_ptr<Thread> thread, HLERequestContext& context, ThreadWakeupReason reason)>;
|
2017-11-10 00:13:11 +01:00
|
|
|
|
2018-02-22 04:03:46 +01:00
|
|
|
/**
|
2017-11-10 00:13:11 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<Event> SleepClientThread(std::shared_ptr<Thread> thread,
|
|
|
|
const std::string& reason,
|
|
|
|
std::chrono::nanoseconds timeout,
|
|
|
|
WakeupCallback&& callback);
|
2017-11-10 00:13:11 +01:00
|
|
|
|
2017-06-09 14:23:13 +02: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.
|
|
|
|
*/
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<Object> GetIncomingHandle(u32 id_from_cmdbuf) const;
|
2017-06-09 14:23:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-03-23 21:04:19 +01:00
|
|
|
u32 AddOutgoingHandle(std::shared_ptr<Object> object);
|
2017-06-09 08:55:18 +02:00
|
|
|
|
2017-06-11 02:57:08 +02: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-11-05 18:50:22 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the static buffer identified by the input buffer_id. The static buffer *must* have
|
|
|
|
* been created in PopulateFromIncomingCommandBuffer by way of an input StaticBuffer descriptor.
|
|
|
|
*/
|
|
|
|
const std::vector<u8>& GetStaticBuffer(u8 buffer_id) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a static buffer that will be copied to the target process when the request is
|
|
|
|
* translated.
|
|
|
|
*/
|
|
|
|
void AddStaticBuffer(u8 buffer_id, std::vector<u8> data);
|
|
|
|
|
2017-07-14 21:24:57 +02:00
|
|
|
/**
|
|
|
|
* Gets a memory interface by the id from the request command buffer. See the "HLE mapped buffer
|
|
|
|
* protocol" section in the class documentation for more details.
|
|
|
|
*/
|
|
|
|
MappedBuffer& GetMappedBuffer(u32 id_from_cmdbuf);
|
|
|
|
|
2017-06-19 01:05:12 +02:00
|
|
|
/// Populates this context with data from the requesting process/thread.
|
2018-10-20 03:04:18 +02:00
|
|
|
ResultCode PopulateFromIncomingCommandBuffer(const u32_le* src_cmdbuf, Process& src_process);
|
2017-06-19 01:05:12 +02:00
|
|
|
/// Writes data from this context back to the requesting process/thread.
|
2018-10-20 03:04:18 +02:00
|
|
|
ResultCode WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process) const;
|
2017-06-09 14:23:13 +02:00
|
|
|
|
2017-06-19 01:05:12 +02:00
|
|
|
private:
|
2019-02-02 21:55:45 +01:00
|
|
|
KernelSystem& kernel;
|
2017-06-09 08:52:30 +02:00
|
|
|
std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<ServerSession> session;
|
2017-06-11 21:51:05 +02:00
|
|
|
// TODO(yuriks): Check common usage of this and optimize size accordingly
|
2019-03-23 21:04:19 +01:00
|
|
|
boost::container::small_vector<std::shared_ptr<Object>, 8> request_handles;
|
2017-11-05 18:50:22 +01:00
|
|
|
// The static buffers will be created when the IPC request is translated.
|
|
|
|
std::array<std::vector<u8>, IPC::MAX_STATIC_BUFFERS> static_buffers;
|
2017-07-14 21:24:57 +02:00
|
|
|
// The mapped buffers will be created when the IPC request is translated
|
|
|
|
boost::container::small_vector<MappedBuffer, 8> request_mapped_buffers;
|
2017-06-07 06:20:52 +02:00
|
|
|
};
|
|
|
|
|
2017-06-05 06:52:19 +02:00
|
|
|
} // namespace Kernel
|