summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Subv2018-01-07 01:50:55 -0500
committerGravatar bunnei2018-01-07 17:11:47 -0500
commit0368324f7925fe5568072299a0206ec71afee865 (patch)
tree2050e904e2f1933bfadbc96d1a0bc698c39a08ce /src
parentIPC: Skip the entire u64 of the command id when receiving an IPC request. (diff)
downloadyuzu-0368324f7925fe5568072299a0206ec71afee865.tar.gz
yuzu-0368324f7925fe5568072299a0206ec71afee865.tar.xz
yuzu-0368324f7925fe5568072299a0206ec71afee865.zip
IPC Cleanup: Remove 3DS-specific code and translate copy, move and domain objects in IPC requests.
Popping objects from the buffer is still not implemented.
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/ipc.h129
-rw-r--r--src/core/hle/ipc_helpers.h240
-rw-r--r--src/core/hle/kernel/hle_ipc.cpp107
-rw-r--r--src/core/hle/kernel/hle_ipc.h39
-rw-r--r--src/core/hle/service/am/applet_oe.cpp2
-rw-r--r--src/core/hle/service/lm/lm.cpp2
-rw-r--r--src/core/hle/service/sm/controller.cpp2
-rw-r--r--src/core/hle/service/sm/sm.cpp2
8 files changed, 118 insertions, 405 deletions
diff --git a/src/core/hle/ipc.h b/src/core/hle/ipc.h
index 4c21f5024..c2f481624 100644
--- a/src/core/hle/ipc.h
+++ b/src/core/hle/ipc.h
@@ -143,120 +143,25 @@ struct DataPayloadHeader {
143}; 143};
144static_assert(sizeof(DataPayloadHeader) == 8, "DataPayloadRequest size is incorrect"); 144static_assert(sizeof(DataPayloadHeader) == 8, "DataPayloadRequest size is incorrect");
145 145
146struct DomainRequestMessageHeader { 146struct DomainMessageHeader {
147 union { 147 union {
148 BitField<0, 8, u32_le> command; 148 // Used when responding to an IPC request, Server -> Client.
149 BitField<16, 16, u32_le> size; 149 struct {
150 u32_le num_objects;
151 INSERT_PADDING_WORDS(3);
152 };
153
154 // Used when performing an IPC request, Client -> Server.
155 struct {
156 union {
157 BitField<0, 8, u32_le> command;
158 BitField<16, 16, u32_le> size;
159 };
160 u32_le object_id;
161 INSERT_PADDING_WORDS(2);
162 };
150 }; 163 };
151 u32_le object_id;
152 INSERT_PADDING_WORDS(2);
153};
154static_assert(sizeof(DomainRequestMessageHeader) == 16, "DomainRequestMessageHeader size is incorrect");
155
156struct DomainResponseMessageHeader {
157 u32_le num_objects;
158 INSERT_PADDING_WORDS(3);
159};
160static_assert(sizeof(DomainResponseMessageHeader) == 16, "DomainResponseMessageHeader size is incorrect");
161
162enum DescriptorType : u32 {
163 // Buffer related desciptors types (mask : 0x0F)
164 StaticBuffer = 0x02,
165 PXIBuffer = 0x04,
166 MappedBuffer = 0x08,
167 // Handle related descriptors types (mask : 0x30, but need to check for buffer related
168 // descriptors first )
169 CopyHandle = 0x00,
170 MoveHandle = 0x10,
171 CallingPid = 0x20,
172};
173
174constexpr u32 MoveHandleDesc(u32 num_handles = 1) {
175 return MoveHandle | ((num_handles - 1) << 26);
176}
177
178constexpr u32 CopyHandleDesc(u32 num_handles = 1) {
179 return CopyHandle | ((num_handles - 1) << 26);
180}
181
182constexpr u32 CallingPidDesc() {
183 return CallingPid;
184}
185
186constexpr bool IsHandleDescriptor(u32 descriptor) {
187 return (descriptor & 0xF) == 0x0;
188}
189
190constexpr u32 HandleNumberFromDesc(u32 handle_descriptor) {
191 return (handle_descriptor >> 26) + 1;
192}
193
194union StaticBufferDescInfo {
195 u32 raw;
196 BitField<0, 4, u32> descriptor_type;
197 BitField<10, 4, u32> buffer_id;
198 BitField<14, 18, u32> size;
199}; 164};
200 165static_assert(sizeof(DomainMessageHeader) == 16, "DomainMessageHeader size is incorrect");
201inline u32 StaticBufferDesc(size_t size, u8 buffer_id) {
202 StaticBufferDescInfo info{};
203 info.descriptor_type.Assign(StaticBuffer);
204 info.buffer_id.Assign(buffer_id);
205 info.size.Assign(static_cast<u32>(size));
206 return info.raw;
207}
208
209/**
210 * @brief Creates a header describing a buffer to be sent over PXI.
211 * @param size Size of the buffer. Max 0x00FFFFFF.
212 * @param buffer_id The Id of the buffer. Max 0xF.
213 * @param is_read_only true if the buffer is read-only. If false, the buffer is considered to have
214 * read-write access.
215 * @return The created PXI buffer header.
216 *
217 * The next value is a phys-address of a table located in the BASE memregion.
218 */
219inline u32 PXIBufferDesc(u32 size, unsigned buffer_id, bool is_read_only) {
220 u32 type = PXIBuffer;
221 if (is_read_only)
222 type |= 0x2;
223 return type | (size << 8) | ((buffer_id & 0xF) << 4);
224}
225
226enum MappedBufferPermissions : u32 {
227 R = 1,
228 W = 2,
229 RW = R | W,
230};
231
232union MappedBufferDescInfo {
233 u32 raw;
234 BitField<0, 4, u32> flags;
235 BitField<1, 2, MappedBufferPermissions> perms;
236 BitField<4, 28, u32> size;
237};
238
239inline u32 MappedBufferDesc(size_t size, MappedBufferPermissions perms) {
240 MappedBufferDescInfo info{};
241 info.flags.Assign(MappedBuffer);
242 info.perms.Assign(perms);
243 info.size.Assign(static_cast<u32>(size));
244 return info.raw;
245}
246
247inline DescriptorType GetDescriptorType(u32 descriptor) {
248 // Note: Those checks must be done in this order
249 if (IsHandleDescriptor(descriptor))
250 return (DescriptorType)(descriptor & 0x30);
251
252 // handle the fact that the following descriptors can have rights
253 if (descriptor & MappedBuffer)
254 return MappedBuffer;
255
256 if (descriptor & PXIBuffer)
257 return PXIBuffer;
258
259 return StaticBuffer;
260}
261 166
262} // namespace IPC 167} // namespace IPC
diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h
index 705943e6b..368b50011 100644
--- a/src/core/hle/ipc_helpers.h
+++ b/src/core/hle/ipc_helpers.h
@@ -58,18 +58,18 @@ public:
58 RequestBuilder(u32* command_buffer) : RequestHelperBase(command_buffer) {} 58 RequestBuilder(u32* command_buffer) : RequestHelperBase(command_buffer) {}
59 59
60 RequestBuilder(Kernel::HLERequestContext& context, unsigned normal_params_size, 60 RequestBuilder(Kernel::HLERequestContext& context, unsigned normal_params_size,
61 u32 num_handles_to_copy = 0, u32 num_handles_to_move = 0, u32 num_domain_objects = 0) 61 u32 num_handles_to_copy = 0, u32 num_handles_to_move = 0,
62 u32 num_domain_objects = 0)
62 : RequestHelperBase(context) { 63 : RequestHelperBase(context) {
63 memset(cmdbuf, 0, 64); 64 memset(cmdbuf, 0, sizeof(u32) * IPC::COMMAND_BUFFER_LENGTH);
64
65 context.ClearIncomingObjects();
66 65
67 IPC::CommandHeader header{}; 66 IPC::CommandHeader header{};
68 67
69 // The entire size of the raw data section in u32 units, including the 16 bytes of mandatory padding. 68 // The entire size of the raw data section in u32 units, including the 16 bytes of mandatory
69 // padding.
70 u32 raw_data_size = sizeof(IPC::DataPayloadHeader) / 4 + 4 + normal_params_size; 70 u32 raw_data_size = sizeof(IPC::DataPayloadHeader) / 4 + 4 + normal_params_size;
71 if (context.IsDomain()) 71 if (context.IsDomain())
72 raw_data_size += sizeof(DomainResponseMessageHeader) / 4 + num_domain_objects; 72 raw_data_size += sizeof(DomainMessageHeader) / 4 + num_domain_objects;
73 73
74 header.data_size.Assign(raw_data_size); 74 header.data_size.Assign(raw_data_size);
75 if (num_handles_to_copy || num_handles_to_move) { 75 if (num_handles_to_copy || num_handles_to_move) {
@@ -88,7 +88,7 @@ public:
88 AlignWithPadding(); 88 AlignWithPadding();
89 89
90 if (context.IsDomain()) { 90 if (context.IsDomain()) {
91 IPC::DomainResponseMessageHeader domain_header{}; 91 IPC::DomainMessageHeader domain_header{};
92 domain_header.num_objects = num_domain_objects; 92 domain_header.num_objects = num_domain_objects;
93 PushRaw(domain_header); 93 PushRaw(domain_header);
94 } 94 }
@@ -100,12 +100,7 @@ public:
100 100
101 template <class T> 101 template <class T>
102 void PushIpcInterface() { 102 void PushIpcInterface() {
103 auto& request_handlers = context->Domain()->request_handlers; 103 context->AddDomainObject(std::make_shared<T>());
104 request_handlers.emplace_back(std::make_shared<T>());
105 Push(RESULT_SUCCESS);
106 Push<u32>(0); // The error code is the lower word of an u64, so we fill the rest with 0.
107 // Now push the id of the newly-added object.
108 Push<u32>(static_cast<u32>(request_handlers.size()));
109 } 104 }
110 105
111 // Validate on destruction, as there shouldn't be any case where we don't want it 106 // Validate on destruction, as there shouldn't be any case where we don't want it
@@ -127,21 +122,11 @@ public:
127 template <typename T> 122 template <typename T>
128 void PushRaw(const T& value); 123 void PushRaw(const T& value);
129 124
130 // TODO : ensure that translate params are added after all regular params
131 template <typename... H>
132 void PushCopyHandles(H... handles);
133
134 template <typename... H>
135 void PushMoveHandles(H... handles);
136
137 template <typename... O> 125 template <typename... O>
138 void PushObjects(Kernel::SharedPtr<O>... pointers); 126 void PushMoveObjects(Kernel::SharedPtr<O>... pointers);
139
140 void PushCurrentPIDHandle();
141 127
142 void PushStaticBuffer(VAddr buffer_vaddr, size_t size, u8 buffer_id); 128 template <typename... O>
143 129 void PushCopyObjects(Kernel::SharedPtr<O>... pointers);
144 void PushMappedBuffer(VAddr buffer_vaddr, size_t size, MappedBufferPermissions perms);
145}; 130};
146 131
147/// Push /// 132/// Push ///
@@ -189,37 +174,20 @@ void RequestBuilder::Push(const First& first_value, const Other&... other_values
189 Push(other_values...); 174 Push(other_values...);
190} 175}
191 176
192template <typename... H>
193inline void RequestBuilder::PushCopyHandles(H... handles) {
194 Push(CopyHandleDesc(sizeof...(H)));
195 Push(static_cast<Kernel::Handle>(handles)...);
196}
197
198template <typename... H>
199inline void RequestBuilder::PushMoveHandles(H... handles) {
200 Push(MoveHandleDesc(sizeof...(H)));
201 Push(static_cast<Kernel::Handle>(handles)...);
202}
203
204template <typename... O> 177template <typename... O>
205inline void RequestBuilder::PushObjects(Kernel::SharedPtr<O>... pointers) { 178inline void RequestBuilder::PushCopyObjects(Kernel::SharedPtr<O>... pointers) {
206 PushMoveHandles(context->AddOutgoingHandle(std::move(pointers))...); 179 auto objects = {pointers...};
207} 180 for (auto& object : objects) {
208 181 context->AddCopyObject(std::move(object));
209inline void RequestBuilder::PushCurrentPIDHandle() { 182 }
210 Push(CallingPidDesc());
211 Push(u32(0));
212}
213
214inline void RequestBuilder::PushStaticBuffer(VAddr buffer_vaddr, size_t size, u8 buffer_id) {
215 Push(StaticBufferDesc(size, buffer_id));
216 Push(buffer_vaddr);
217} 183}
218 184
219inline void RequestBuilder::PushMappedBuffer(VAddr buffer_vaddr, size_t size, 185template <typename... O>
220 MappedBufferPermissions perms) { 186inline void RequestBuilder::PushMoveObjects(Kernel::SharedPtr<O>... pointers) {
221 Push(MappedBufferDesc(size, perms)); 187 auto objects = {pointers...};
222 Push(buffer_vaddr); 188 for (auto& object : objects) {
189 context->AddMoveObject(std::move(object));
190 }
223} 191}
224 192
225class RequestParser : public RequestHelperBase { 193class RequestParser : public RequestHelperBase {
@@ -229,6 +197,9 @@ public:
229 RequestParser(Kernel::HLERequestContext& context) : RequestHelperBase(context) { 197 RequestParser(Kernel::HLERequestContext& context) : RequestHelperBase(context) {
230 ASSERT_MSG(context.GetDataPayloadOffset(), "context is incomplete"); 198 ASSERT_MSG(context.GetDataPayloadOffset(), "context is incomplete");
231 Skip(context.GetDataPayloadOffset(), false); 199 Skip(context.GetDataPayloadOffset(), false);
200 // Skip the u64 command id, it's already stored in the context
201 static constexpr u32 CommandIdSize = 2;
202 Skip(CommandIdSize, false);
232 } 203 }
233 204
234 RequestBuilder MakeBuilder(u32 normal_params_size, u32 num_handles_to_copy, 205 RequestBuilder MakeBuilder(u32 normal_params_size, u32 num_handles_to_copy,
@@ -249,80 +220,6 @@ public:
249 template <typename First, typename... Other> 220 template <typename First, typename... Other>
250 void Pop(First& first_value, Other&... other_values); 221 void Pop(First& first_value, Other&... other_values);
251 222
252 /// Equivalent to calling `PopHandles<1>()[0]`.
253 Kernel::Handle PopHandle();
254
255 /**
256 * Pops a descriptor containing `N` handles. The handles are returned as an array. The
257 * descriptor must contain exactly `N` handles, it is not permitted to, for example, call
258 * PopHandles<1>() twice to read a multi-handle descriptor with 2 handles, or to make a single
259 * PopHandles<2>() call to read 2 single-handle descriptors.
260 */
261 template <unsigned int N>
262 std::array<Kernel::Handle, N> PopHandles();
263
264 /// Convenience wrapper around PopHandles() which assigns the handles to the passed references.
265 template <typename... H>
266 void PopHandles(H&... handles) {
267 std::tie(handles...) = PopHandles<sizeof...(H)>();
268 }
269
270 /// Equivalent to calling `PopGenericObjects<1>()[0]`.
271 Kernel::SharedPtr<Kernel::Object> PopGenericObject();
272
273 /// Equivalent to calling `std::get<0>(PopObjects<T>())`.
274 template <typename T>
275 Kernel::SharedPtr<T> PopObject();
276
277 /**
278 * Pop a descriptor containing `N` handles and resolves them to Kernel::Object pointers. If a
279 * handle is invalid, null is returned for that object instead. The same caveats from
280 * PopHandles() apply regarding `N` matching the number of handles in the descriptor.
281 */
282 template <unsigned int N>
283 std::array<Kernel::SharedPtr<Kernel::Object>, N> PopGenericObjects();
284
285 /**
286 * Resolves handles to Kernel::Objects as in PopGenericsObjects(), but then also casts them to
287 * the passed `T` types, while verifying that the cast is valid. If the type of an object does
288 * not match, null is returned instead.
289 */
290 template <typename... T>
291 std::tuple<Kernel::SharedPtr<T>...> PopObjects();
292
293 /// Convenience wrapper around PopObjects() which assigns the handles to the passed references.
294 template <typename... T>
295 void PopObjects(Kernel::SharedPtr<T>&... pointers) {
296 std::tie(pointers...) = PopObjects<T...>();
297 }
298
299 /**
300 * @brief Pops the static buffer vaddr
301 * @return The virtual address of the buffer
302 * @param[out] data_size If non-null, the pointed value will be set to the size of the data
303 * @param[out] useStaticBuffersToGetVaddr Indicates if we should read the vaddr from the static
304 * buffers (which is the correct thing to do, but no service presently implement it) instead of
305 * using the same value as the process who sent the request
306 * given by the source process
307 *
308 * Static buffers must be set up before any IPC request using those is sent.
309 * It is the duty of the process (usually services) to allocate and set up the receiving static
310 * buffer information
311 * Please note that the setup uses virtual addresses.
312 */
313 VAddr PopStaticBuffer(size_t* data_size = nullptr, bool useStaticBuffersToGetVaddr = false);
314
315 /**
316 * @brief Pops the mapped buffer vaddr
317 * @return The virtual address of the buffer
318 * @param[out] data_size If non-null, the pointed value will be set to the size of the data
319 * given by the source process
320 * @param[out] buffer_perms If non-null, the pointed value will be set to the permissions of the
321 * buffer
322 */
323 VAddr PopMappedBuffer(size_t* data_size = nullptr,
324 MappedBufferPermissions* buffer_perms = nullptr);
325
326 /** 223 /**
327 * @brief Reads the next normal parameters as a struct, by copying it 224 * @brief Reads the next normal parameters as a struct, by copying it
328 * @note: The output class must be correctly packed/padded to fit hardware layout. 225 * @note: The output class must be correctly packed/padded to fit hardware layout.
@@ -396,91 +293,4 @@ void RequestParser::Pop(First& first_value, Other&... other_values) {
396 Pop(other_values...); 293 Pop(other_values...);
397} 294}
398 295
399inline Kernel::Handle RequestParser::PopHandle() {
400 const u32 handle_descriptor = Pop<u32>();
401 DEBUG_ASSERT_MSG(IsHandleDescriptor(handle_descriptor),
402 "Tried to pop handle(s) but the descriptor is not a handle descriptor");
403 DEBUG_ASSERT_MSG(HandleNumberFromDesc(handle_descriptor) == 1,
404 "Descriptor indicates that there isn't exactly one handle");
405 return Pop<Kernel::Handle>();
406}
407
408template <unsigned int N>
409std::array<Kernel::Handle, N> RequestParser::PopHandles() {
410 u32 handle_descriptor = Pop<u32>();
411 ASSERT_MSG(IsHandleDescriptor(handle_descriptor),
412 "Tried to pop handle(s) but the descriptor is not a handle descriptor");
413 ASSERT_MSG(N == HandleNumberFromDesc(handle_descriptor),
414 "Number of handles doesn't match the descriptor");
415
416 std::array<Kernel::Handle, N> handles{};
417 for (Kernel::Handle& handle : handles) {
418 handle = Pop<Kernel::Handle>();
419 }
420 return handles;
421}
422
423inline Kernel::SharedPtr<Kernel::Object> RequestParser::PopGenericObject() {
424 Kernel::Handle handle = PopHandle();
425 return context->GetIncomingHandle(handle);
426}
427
428template <typename T>
429Kernel::SharedPtr<T> RequestParser::PopObject() {
430 return Kernel::DynamicObjectCast<T>(PopGenericObject());
431}
432
433template <unsigned int N>
434inline std::array<Kernel::SharedPtr<Kernel::Object>, N> RequestParser::PopGenericObjects() {
435 std::array<Kernel::Handle, N> handles = PopHandles<N>();
436 std::array<Kernel::SharedPtr<Kernel::Object>, N> pointers;
437 for (int i = 0; i < N; ++i) {
438 pointers[i] = context->GetIncomingHandle(handles[i]);
439 }
440 return pointers;
441}
442
443namespace detail {
444template <typename... T, size_t... I>
445std::tuple<Kernel::SharedPtr<T>...> PopObjectsHelper(
446 std::array<Kernel::SharedPtr<Kernel::Object>, sizeof...(T)>&& pointers,
447 std::index_sequence<I...>) {
448 return std::make_tuple(Kernel::DynamicObjectCast<T>(std::move(pointers[I]))...);
449}
450} // namespace detail
451
452template <typename... T>
453inline std::tuple<Kernel::SharedPtr<T>...> RequestParser::PopObjects() {
454 return detail::PopObjectsHelper<T...>(PopGenericObjects<sizeof...(T)>(),
455 std::index_sequence_for<T...>{});
456}
457
458inline VAddr RequestParser::PopStaticBuffer(size_t* data_size, bool useStaticBuffersToGetVaddr) {
459 const u32 sbuffer_descriptor = Pop<u32>();
460 StaticBufferDescInfo bufferInfo{sbuffer_descriptor};
461 if (data_size != nullptr)
462 *data_size = bufferInfo.size;
463 if (!useStaticBuffersToGetVaddr)
464 return Pop<VAddr>();
465 else {
466 ASSERT_MSG(0, "remove the assert if multiprocess/IPC translation are implemented.");
467 // The buffer has already been copied to the static buffer by the kernel during
468 // translation
469 Pop<VAddr>(); // Pop the calling process buffer address
470 // and get the vaddr from the static buffers
471 return cmdbuf[(0x100 >> 2) + bufferInfo.buffer_id * 2 + 1];
472 }
473}
474
475inline VAddr RequestParser::PopMappedBuffer(size_t* data_size,
476 MappedBufferPermissions* buffer_perms) {
477 const u32 sbuffer_descriptor = Pop<u32>();
478 MappedBufferDescInfo bufferInfo{sbuffer_descriptor};
479 if (data_size != nullptr)
480 *data_size = bufferInfo.size;
481 if (buffer_perms != nullptr)
482 *buffer_perms = bufferInfo.perms;
483 return Pop<VAddr>();
484}
485
486} // namespace IPC 296} // namespace IPC
diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp
index ac81dbf3f..518e44f33 100644
--- a/src/core/hle/kernel/hle_ipc.cpp
+++ b/src/core/hle/kernel/hle_ipc.cpp
@@ -37,20 +37,6 @@ HLERequestContext::HLERequestContext(SharedPtr<Kernel::ServerSession> server_ses
37 37
38HLERequestContext::~HLERequestContext() = default; 38HLERequestContext::~HLERequestContext() = default;
39 39
40SharedPtr<Object> HLERequestContext::GetIncomingHandle(u32 id_from_cmdbuf) const {
41 ASSERT(id_from_cmdbuf < request_handles.size());
42 return request_handles[id_from_cmdbuf];
43}
44
45u32 HLERequestContext::AddOutgoingHandle(SharedPtr<Object> object) {
46 request_handles.push_back(std::move(object));
47 return static_cast<u32>(request_handles.size() - 1);
48}
49
50void HLERequestContext::ClearIncomingObjects() {
51 request_handles.clear();
52}
53
54void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) { 40void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) {
55 IPC::RequestParser rp(src_cmdbuf); 41 IPC::RequestParser rp(src_cmdbuf);
56 command_header = std::make_unique<IPC::CommandHeader>(rp.PopRaw<IPC::CommandHeader>()); 42 command_header = std::make_unique<IPC::CommandHeader>(rp.PopRaw<IPC::CommandHeader>());
@@ -95,7 +81,7 @@ void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) {
95 // If this is an incoming message, only CommandType "Request" has a domain header 81 // If this is an incoming message, only CommandType "Request" has a domain header
96 // All outgoing domain messages have the domain header 82 // All outgoing domain messages have the domain header
97 domain_message_header = 83 domain_message_header =
98 std::make_unique<IPC::DomainRequestMessageHeader>(rp.PopRaw<IPC::DomainRequestMessageHeader>()); 84 std::make_unique<IPC::DomainMessageHeader>(rp.PopRaw<IPC::DomainMessageHeader>());
99 } 85 }
100 86
101 data_payload_header = 87 data_payload_header =
@@ -107,61 +93,78 @@ void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) {
107 ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'O')); 93 ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'O'));
108 } 94 }
109 95
96 data_payload_offset = rp.GetCurrentOffset();
110 command = rp.Pop<u32_le>(); 97 command = rp.Pop<u32_le>();
111 rp.Skip(1, false); // The command is actually an u64, but we don't use the high part. 98 rp.Skip(1, false); // The command is actually an u64, but we don't use the high part.
112 data_payload_offset = rp.GetCurrentOffset();
113} 99}
114 100
115ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf, 101ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf,
116 Process& src_process, 102 Process& src_process,
117 HandleTable& src_table) { 103 HandleTable& src_table) {
118 ParseCommandBuffer(src_cmdbuf, true); 104 ParseCommandBuffer(src_cmdbuf, true);
119 size_t untranslated_size = data_payload_offset + command_header->data_size; 105 // The data_size already includes the payload header, the padding and the domain header.
120 std::copy_n(src_cmdbuf, untranslated_size, cmd_buf.begin()); 106 size_t size = data_payload_offset + command_header->data_size -
107 sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
108 if (domain_message_header)
109 size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
110 std::copy_n(src_cmdbuf, size, cmd_buf.begin());
121 return RESULT_SUCCESS; 111 return RESULT_SUCCESS;
122} 112}
123 113
124ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process, 114ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
125 HandleTable& dst_table) { 115 HandleTable& dst_table) {
126 ParseCommandBuffer(&cmd_buf[0], false); 116 // The header was already built in the internal command buffer. Attempt to parse it to verify
127 size_t untranslated_size = data_payload_offset + command_header->data_size; 117 // the integrity and then copy it over to the target command buffer.
128 std::copy_n(cmd_buf.begin(), untranslated_size, dst_cmdbuf); 118 ParseCommandBuffer(cmd_buf.data(), false);
119
120 // The data_size already includes the payload header, the padding and the domain header.
121 size_t size = data_payload_offset + command_header->data_size -
122 sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
123 if (domain_message_header)
124 size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
125
126 std::copy_n(cmd_buf.begin(), size, dst_cmdbuf);
129 127
130 if (command_header->enable_handle_descriptor) { 128 if (command_header->enable_handle_descriptor) {
131 size_t command_size = untranslated_size + handle_descriptor_header->num_handles_to_copy + 129 ASSERT_MSG(!move_objects.empty() || !copy_objects.empty(),
132 handle_descriptor_header->num_handles_to_move; 130 "Handle descriptor bit set but no handles to translate");
133 ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH); 131 // We write the translated handles at a specific offset in the command buffer, this space
134 132 // was already reserved when writing the header.
135 size_t untranslated_index = untranslated_size; 133 size_t current_offset =
136 size_t handle_write_offset = 3; 134 (sizeof(IPC::CommandHeader) + sizeof(IPC::HandleDescriptorHeader)) / sizeof(u32);
137 while (untranslated_index < command_size) { 135 ASSERT_MSG(!handle_descriptor_header->send_current_pid, "Sending PID is not implemented");
138 u32 descriptor = cmd_buf[untranslated_index]; 136
139 untranslated_index += 1; 137 ASSERT_MSG(copy_objects.size() == handle_descriptor_header->num_handles_to_copy);
140 138 ASSERT_MSG(move_objects.size() == handle_descriptor_header->num_handles_to_move);
141 switch (IPC::GetDescriptorType(descriptor)) { 139
142 case IPC::DescriptorType::CopyHandle: 140 // We don't make a distinction between copy and move handles when translating since HLE
143 case IPC::DescriptorType::MoveHandle: { 141 // services don't deal with handles directly. However, the guest applications might check
144 // HLE services don't use handles, so we treat both CopyHandle and MoveHandle 142 // for specific values in each of these descriptors.
145 // equally 143 for (auto& object : copy_objects) {
146 u32 num_handles = IPC::HandleNumberFromDesc(descriptor); 144 ASSERT(object != nullptr);
147 for (u32 j = 0; j < num_handles; ++j) { 145 dst_cmdbuf[current_offset++] = Kernel::g_handle_table.Create(object).Unwrap();
148 SharedPtr<Object> object = GetIncomingHandle(cmd_buf[untranslated_index]); 146 }
149 Handle handle = 0; 147
150 if (object != nullptr) { 148 for (auto& object : move_objects) {
151 // TODO(yuriks): Figure out the proper error handling for if this fails 149 ASSERT(object != nullptr);
152 handle = dst_table.Create(object).Unwrap(); 150 dst_cmdbuf[current_offset++] = Kernel::g_handle_table.Create(object).Unwrap();
153 }
154 dst_cmdbuf[handle_write_offset++] = handle;
155 untranslated_index++;
156 }
157 break;
158 }
159 default:
160 UNIMPLEMENTED_MSG("Unsupported handle translation: 0x%08X", descriptor);
161 }
162 } 151 }
163 } 152 }
164 153
154 // TODO(Subv): Translate the X/A/B/W buffers.
155
156 if (IsDomain()) {
157 ASSERT(domain_message_header->num_objects == domain_objects.size());
158 // Write the domain objects to the command buffer, these go after the raw untranslated data.
159 // TODO(Subv): This completely ignores C buffers.
160 size_t domain_offset = size - domain_message_header->num_objects;
161 auto& request_handlers = domain->request_handlers;
162
163 for (auto& object : domain_objects) {
164 request_handlers.emplace_back(object);
165 dst_cmdbuf[domain_offset++] = request_handlers.size();
166 }
167 }
165 return RESULT_SUCCESS; 168 return RESULT_SUCCESS;
166} 169}
167 170
diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h
index b5649931d..48730a2b2 100644
--- a/src/core/hle/kernel/hle_ipc.h
+++ b/src/core/hle/kernel/hle_ipc.h
@@ -110,25 +110,6 @@ public:
110 return server_session; 110 return server_session;
111 } 111 }
112 112
113 /**
114 * Resolves a object id from the request command buffer into a pointer to an object. See the
115 * "HLE handle protocol" section in the class documentation for more details.
116 */
117 SharedPtr<Object> GetIncomingHandle(u32 id_from_cmdbuf) const;
118
119 /**
120 * Adds an outgoing object to the response, returning the id which should be used to reference
121 * it. See the "HLE handle protocol" section in the class documentation for more details.
122 */
123 u32 AddOutgoingHandle(SharedPtr<Object> object);
124
125 /**
126 * Discards all Objects from the context, invalidating all ids. This may be called after reading
127 * out all incoming objects, so that the buffer memory can be re-used for outgoing handles, but
128 * this is not required.
129 */
130 void ClearIncomingObjects();
131
132 void ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming); 113 void ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming);
133 114
134 /// Populates this context with data from the requesting process/thread. 115 /// Populates this context with data from the requesting process/thread.
@@ -158,7 +139,7 @@ public:
158 return buffer_a_desciptors; 139 return buffer_a_desciptors;
159 } 140 }
160 141
161 const std::unique_ptr<IPC::DomainRequestMessageHeader>& GetDomainMessageHeader() const { 142 const std::unique_ptr<IPC::DomainMessageHeader>& GetDomainMessageHeader() const {
162 return domain_message_header; 143 return domain_message_header;
163 } 144 }
164 145
@@ -166,17 +147,31 @@ public:
166 return domain != nullptr; 147 return domain != nullptr;
167 } 148 }
168 149
150 void AddMoveObject(SharedPtr<Object> object) {
151 move_objects.emplace_back(std::move(object));
152 }
153
154 void AddCopyObject(SharedPtr<Object> object) {
155 copy_objects.emplace_back(std::move(object));
156 }
157
158 void AddDomainObject(std::shared_ptr<SessionRequestHandler> object) {
159 domain_objects.emplace_back(std::move(object));
160 }
161
169private: 162private:
170 std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf; 163 std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
171 SharedPtr<Kernel::Domain> domain; 164 SharedPtr<Kernel::Domain> domain;
172 SharedPtr<Kernel::ServerSession> server_session; 165 SharedPtr<Kernel::ServerSession> server_session;
173 // TODO(yuriks): Check common usage of this and optimize size accordingly 166 // TODO(yuriks): Check common usage of this and optimize size accordingly
174 boost::container::small_vector<SharedPtr<Object>, 8> request_handles; 167 boost::container::small_vector<SharedPtr<Object>, 8> move_objects;
168 boost::container::small_vector<SharedPtr<Object>, 8> copy_objects;
169 boost::container::small_vector<std::shared_ptr<SessionRequestHandler>, 8> domain_objects;
175 170
176 std::unique_ptr<IPC::CommandHeader> command_header; 171 std::unique_ptr<IPC::CommandHeader> command_header;
177 std::unique_ptr<IPC::HandleDescriptorHeader> handle_descriptor_header; 172 std::unique_ptr<IPC::HandleDescriptorHeader> handle_descriptor_header;
178 std::unique_ptr<IPC::DataPayloadHeader> data_payload_header; 173 std::unique_ptr<IPC::DataPayloadHeader> data_payload_header;
179 std::unique_ptr<IPC::DomainRequestMessageHeader> domain_message_header; 174 std::unique_ptr<IPC::DomainMessageHeader> domain_message_header;
180 std::vector<IPC::BufferDescriptorX> buffer_x_desciptors; 175 std::vector<IPC::BufferDescriptorX> buffer_x_desciptors;
181 std::vector<IPC::BufferDescriptorABW> buffer_a_desciptors; 176 std::vector<IPC::BufferDescriptorABW> buffer_a_desciptors;
182 std::vector<IPC::BufferDescriptorABW> buffer_b_desciptors; 177 std::vector<IPC::BufferDescriptorABW> buffer_b_desciptors;
diff --git a/src/core/hle/service/am/applet_oe.cpp b/src/core/hle/service/am/applet_oe.cpp
index 6fe7bdce5..34cc67f34 100644
--- a/src/core/hle/service/am/applet_oe.cpp
+++ b/src/core/hle/service/am/applet_oe.cpp
@@ -73,7 +73,7 @@ private:
73 73
74 IPC::RequestBuilder rb{ctx, 2, 1}; 74 IPC::RequestBuilder rb{ctx, 2, 1};
75 rb.Push(RESULT_SUCCESS); 75 rb.Push(RESULT_SUCCESS);
76 rb.PushObjects(event); 76 rb.PushCopyObjects(event);
77 77
78 LOG_WARNING(Service, "(STUBBED) called"); 78 LOG_WARNING(Service, "(STUBBED) called");
79 } 79 }
diff --git a/src/core/hle/service/lm/lm.cpp b/src/core/hle/service/lm/lm.cpp
index bf9e98bb5..edbee39f5 100644
--- a/src/core/hle/service/lm/lm.cpp
+++ b/src/core/hle/service/lm/lm.cpp
@@ -150,7 +150,7 @@ void LM::Initialize(Kernel::HLERequestContext& ctx) {
150 (*session)->GetObjectId()); 150 (*session)->GetObjectId());
151 IPC::RequestBuilder rb{ctx, 1, 0, 1}; 151 IPC::RequestBuilder rb{ctx, 1, 0, 1};
152 rb.Push(RESULT_SUCCESS); 152 rb.Push(RESULT_SUCCESS);
153 rb.PushObjects(std::move(session).Unwrap()); 153 rb.PushMoveObjects(std::move(session).Unwrap());
154 registered_loggers.emplace_back(std::move(client_port)); 154 registered_loggers.emplace_back(std::move(client_port));
155 } else { 155 } else {
156 UNIMPLEMENTED(); 156 UNIMPLEMENTED();
diff --git a/src/core/hle/service/sm/controller.cpp b/src/core/hle/service/sm/controller.cpp
index 8d17e0ef6..7acc5c3b2 100644
--- a/src/core/hle/service/sm/controller.cpp
+++ b/src/core/hle/service/sm/controller.cpp
@@ -24,7 +24,7 @@ void Controller::ConvertSessionToDomain(Kernel::HLERequestContext& ctx) {
24void Controller::DuplicateSession(Kernel::HLERequestContext& ctx) { 24void Controller::DuplicateSession(Kernel::HLERequestContext& ctx) {
25 IPC::RequestBuilder rb{ctx, 1, 0, 1}; 25 IPC::RequestBuilder rb{ctx, 1, 0, 1};
26 rb.Push(RESULT_SUCCESS); 26 rb.Push(RESULT_SUCCESS);
27 rb.PushObjects(ctx.ServerSession()); 27 rb.PushMoveObjects(ctx.ServerSession());
28 28
29 LOG_DEBUG(Service, "called"); 29 LOG_DEBUG(Service, "called");
30} 30}
diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp
index 279908cae..f1d93ea2b 100644
--- a/src/core/hle/service/sm/sm.cpp
+++ b/src/core/hle/service/sm/sm.cpp
@@ -121,7 +121,7 @@ void SM::GetService(Kernel::HLERequestContext& ctx) {
121 (*session)->GetObjectId()); 121 (*session)->GetObjectId());
122 IPC::RequestBuilder rb = rp.MakeBuilder(2, 0, 1); 122 IPC::RequestBuilder rb = rp.MakeBuilder(2, 0, 1);
123 rb.Push<u64>(0); 123 rb.Push<u64>(0);
124 rb.PushObjects(std::move(session).Unwrap()); 124 rb.PushMoveObjects(std::move(session).Unwrap());
125 } 125 }
126} 126}
127 127