summaryrefslogtreecommitdiff
path: root/src/core/hle/ipc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/ipc.h')
-rw-r--r--src/core/hle/ipc.h162
1 files changed, 162 insertions, 0 deletions
diff --git a/src/core/hle/ipc.h b/src/core/hle/ipc.h
new file mode 100644
index 000000000..90ea4d191
--- /dev/null
+++ b/src/core/hle/ipc.h
@@ -0,0 +1,162 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_types.h"
8#include "core/hle/kernel/thread.h"
9#include "core/memory.h"
10
11namespace Kernel {
12class ServerSession;
13
14// TODO(Subv): Move these declarations out of here
15static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
16
17/**
18 * Returns a pointer to the command buffer in the current thread's TLS
19 * TODO(Subv): This is not entirely correct, the command buffer should be copied from
20 * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to
21 * the service handler process' memory.
22 * @param offset Optional offset into command buffer
23 * @return Pointer to command buffer
24 */
25inline u32* GetCommandBuffer(const int offset = 0) {
26 return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset +
27 offset);
28}
29}
30
31namespace IPC {
32
33enum DescriptorType : u32 {
34 // Buffer related desciptors types (mask : 0x0F)
35 StaticBuffer = 0x02,
36 PXIBuffer = 0x04,
37 MappedBuffer = 0x08,
38 // Handle related descriptors types (mask : 0x30, but need to check for buffer related
39 // descriptors first )
40 CopyHandle = 0x00,
41 MoveHandle = 0x10,
42 CallingPid = 0x20,
43};
44
45/**
46 * @brief Creates a command header to be used for IPC
47 * @param command_id ID of the command to create a header for.
48 * @param normal_params Size of the normal parameters in words. Up to 63.
49 * @param translate_params_size Size of the translate parameters in words. Up to 63.
50 * @return The created IPC header.
51 *
52 * Normal parameters are sent directly to the process while the translate parameters might go
53 * through modifications and checks by the kernel.
54 * The translate parameters are described by headers generated with the IPC::*Desc functions.
55 *
56 * @note While #normal_params is equivalent to the number of normal parameters,
57 * #translate_params_size includes the size occupied by the translate parameters headers.
58 */
59constexpr u32 MakeHeader(u16 command_id, unsigned int normal_params,
60 unsigned int translate_params_size) {
61 return (u32(command_id) << 16) | ((u32(normal_params) & 0x3F) << 6) |
62 (u32(translate_params_size) & 0x3F);
63}
64
65union Header {
66 u32 raw;
67 BitField<0, 6, u32> translate_params_size;
68 BitField<6, 6, u32> normal_params;
69 BitField<16, 16, u32> command_id;
70};
71
72inline Header ParseHeader(u32 header) {
73 return {header};
74}
75
76constexpr u32 MoveHandleDesc(u32 num_handles = 1) {
77 return MoveHandle | ((num_handles - 1) << 26);
78}
79
80constexpr u32 CopyHandleDesc(u32 num_handles = 1) {
81 return CopyHandle | ((num_handles - 1) << 26);
82}
83
84constexpr u32 CallingPidDesc() {
85 return CallingPid;
86}
87
88constexpr bool isHandleDescriptor(u32 descriptor) {
89 return (descriptor & 0xF) == 0x0;
90}
91
92constexpr u32 HandleNumberFromDesc(u32 handle_descriptor) {
93 return (handle_descriptor >> 26) + 1;
94}
95
96constexpr u32 StaticBufferDesc(u32 size, u8 buffer_id) {
97 return StaticBuffer | (size << 14) | ((buffer_id & 0xF) << 10);
98}
99
100union StaticBufferDescInfo {
101 u32 raw;
102 BitField<10, 4, u32> buffer_id;
103 BitField<14, 18, u32> size;
104};
105
106inline StaticBufferDescInfo ParseStaticBufferDesc(const u32 desc) {
107 return {desc};
108}
109
110/**
111 * @brief Creates a header describing a buffer to be sent over PXI.
112 * @param size Size of the buffer. Max 0x00FFFFFF.
113 * @param buffer_id The Id of the buffer. Max 0xF.
114 * @param is_read_only true if the buffer is read-only. If false, the buffer is considered to have
115 * read-write access.
116 * @return The created PXI buffer header.
117 *
118 * The next value is a phys-address of a table located in the BASE memregion.
119 */
120inline u32 PXIBufferDesc(u32 size, unsigned buffer_id, bool is_read_only) {
121 u32 type = PXIBuffer;
122 if (is_read_only)
123 type |= 0x2;
124 return type | (size << 8) | ((buffer_id & 0xF) << 4);
125}
126
127enum MappedBufferPermissions {
128 R = 1,
129 W = 2,
130 RW = R | W,
131};
132
133constexpr u32 MappedBufferDesc(u32 size, MappedBufferPermissions perms) {
134 return MappedBuffer | (size << 4) | (u32(perms) << 1);
135}
136
137union MappedBufferDescInfo {
138 u32 raw;
139 BitField<4, 28, u32> size;
140 BitField<1, 2, MappedBufferPermissions> perms;
141};
142
143inline MappedBufferDescInfo ParseMappedBufferDesc(const u32 desc) {
144 return{ desc };
145}
146
147inline DescriptorType GetDescriptorType(u32 descriptor) {
148 // Note: Those checks must be done in this order
149 if (isHandleDescriptor(descriptor))
150 return (DescriptorType)(descriptor & 0x30);
151
152 // handle the fact that the following descriptors can have rights
153 if (descriptor & MappedBuffer)
154 return MappedBuffer;
155
156 if (descriptor & PXIBuffer)
157 return PXIBuffer;
158
159 return StaticBuffer;
160}
161
162} // namespace IPC