ipc.h 5.3 KB

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