libctru  v2.3.1
ipc.h
Go to the documentation of this file.
1 /**
2  * @file ipc.h
3  * @brief Inter Process Communication helpers
4  */
5 #pragma once
6 
7 #include <3ds/types.h>
8 
9 /// IPC buffer access rights.
10 typedef enum
11 {
12  IPC_BUFFER_R = BIT(1), ///< Readable
13  IPC_BUFFER_W = BIT(2), ///< Writable
14  IPC_BUFFER_RW = IPC_BUFFER_R | IPC_BUFFER_W ///< Readable and Writable
16 
17 /**
18  * @brief Creates a command header to be used for IPC
19  * @param command_id ID of the command to create a header for.
20  * @param normal_params Size of the normal parameters in words. Up to 63.
21  * @param translate_params Size of the translate parameters in words. Up to 63.
22  * @return The created IPC header.
23  *
24  * Normal parameters are sent directly to the process while the translate parameters might go through modifications and checks by the kernel.
25  * The translate parameters are described by headers generated with the IPC_Desc_* functions.
26  *
27  * @note While #normal_params is equivalent to the number of normal parameters, #translate_params includes the size occupied by the translate parameters headers.
28  */
29 static inline u32 IPC_MakeHeader(u16 command_id, unsigned normal_params, unsigned translate_params)
30 {
31  return ((u32) command_id << 16) | (((u32) normal_params & 0x3F) << 6) | (((u32) translate_params & 0x3F) << 0);
32 }
33 
34 /**
35  * @brief Creates a header to share handles
36  * @param number The number of handles following this header. Max 64.
37  * @return The created shared handles header.
38  *
39  * The #number next values are handles that will be shared between the two processes.
40  *
41  * @note Zero values will have no effect.
42  */
43 static inline u32 IPC_Desc_SharedHandles(unsigned number)
44 {
45  return ((u32)(number - 1) << 26);
46 }
47 
48 /**
49  * @brief Creates the header to transfer handle ownership
50  * @param number The number of handles following this header. Max 64.
51  * @return The created handle transfer header.
52  *
53  * The #number next values are handles that will be duplicated and closed by the other process.
54  *
55  * @note Zero values will have no effect.
56  */
57 static inline u32 IPC_Desc_MoveHandles(unsigned number)
58 {
59  return ((u32)(number - 1) << 26) | 0x10;
60 }
61 
62 /**
63  * @brief Returns the code to ask the kernel to fill the handle with the current process ID.
64  * @return The code to request the current process ID.
65  *
66  * The next value is a placeholder that will be replaced by the current process ID by the kernel.
67  */
68 static inline u32 IPC_Desc_CurProcessId(void)
69 {
70  return 0x20;
71 }
72 
73 static inline CTR_DEPRECATED u32 IPC_Desc_CurProcessHandle(void)
74 {
75  return IPC_Desc_CurProcessId();
76 }
77 
78 /**
79  * @brief Creates a header describing a static buffer.
80  * @param size Size of the buffer. Max ?0x03FFFF?.
81  * @param buffer_id The Id of the buffer. Max 0xF.
82  * @return The created static buffer header.
83  *
84  * The next value is a pointer to the buffer. It will be copied to TLS offset 0x180 + static_buffer_id*8.
85  */
86 static inline u32 IPC_Desc_StaticBuffer(size_t size, unsigned buffer_id)
87 {
88  return (size << 14) | ((buffer_id & 0xF) << 10) | 0x2;
89 }
90 
91 /**
92  * @brief Creates a header describing a buffer to be sent over PXI.
93  * @param size Size of the buffer. Max 0x00FFFFFF.
94  * @param buffer_id The Id of the buffer. Max 0xF.
95  * @param is_read_only true if the buffer is read-only. If false, the buffer is considered to have read-write access.
96  * @return The created PXI buffer header.
97  *
98  * The next value is a phys-address of a table located in the BASE memregion.
99  */
100 static inline u32 IPC_Desc_PXIBuffer(size_t size, unsigned buffer_id, bool is_read_only)
101 {
102  u8 type = 0x4;
103  if(is_read_only)type = 0x6;
104  return (size << 8) | ((buffer_id & 0xF) << 4) | type;
105 }
106 
107 /**
108  * @brief Creates a header describing a buffer from the main memory.
109  * @param size Size of the buffer. Max 0x0FFFFFFF.
110  * @param rights The rights of the buffer for the destination process.
111  * @return The created buffer header.
112  *
113  * The next value is a pointer to the buffer.
114  */
115 static inline u32 IPC_Desc_Buffer(size_t size, IPC_BufferRights rights)
116 {
117  return (size << 4) | 0x8 | rights;
118 }
static u32 IPC_Desc_SharedHandles(unsigned number)
Creates a header to share handles.
Definition: ipc.h:43
static u32 IPC_Desc_StaticBuffer(size_t size, unsigned buffer_id)
Creates a header describing a static buffer.
Definition: ipc.h:86
static u32 IPC_Desc_MoveHandles(unsigned number)
Creates the header to transfer handle ownership.
Definition: ipc.h:57
static u32 IPC_Desc_CurProcessId(void)
Returns the code to ask the kernel to fill the handle with the current process ID.
Definition: ipc.h:68
static u32 IPC_MakeHeader(u16 command_id, unsigned normal_params, unsigned translate_params)
Creates a command header to be used for IPC.
Definition: ipc.h:29
static u32 IPC_Desc_Buffer(size_t size, IPC_BufferRights rights)
Creates a header describing a buffer from the main memory.
Definition: ipc.h:115
IPC_BufferRights
IPC buffer access rights.
Definition: ipc.h:11
@ IPC_BUFFER_W
Writable.
Definition: ipc.h:13
@ IPC_BUFFER_RW
Readable and Writable.
Definition: ipc.h:14
@ IPC_BUFFER_R
Readable.
Definition: ipc.h:12
static u32 IPC_Desc_PXIBuffer(size_t size, unsigned buffer_id, bool is_read_only)
Creates a header describing a buffer to be sent over PXI.
Definition: ipc.h:100
Various system types.
#define CTR_DEPRECATED
Flags a function as deprecated.
Definition: types.h:56
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
uint32_t u32
32-bit unsigned integer
Definition: types.h:23