xref: /xnu-11215.41.3/osfmk/kern/ipc_mig.h (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 
32 #ifndef _KERN_IPC_MIG_H_
33 #define _KERN_IPC_MIG_H_
34 
35 #include <mach/mig.h>
36 #include <mach/mach_types.h>
37 #include <mach/message.h>
38 #include <kern/kern_types.h>
39 
40 #include <sys/cdefs.h>
41 
42 #ifdef  XNU_KERNEL_PRIVATE
43 
44 #include <sys/kdebug.h>
45 
46 struct ipc_kmsg;
47 
48 /*
49  * Define the trace points for MIG-generated calls.  One traces the input parameters
50  * to MIG called things, another traces the outputs, and one traces bad message IDs.
51  */
52 #ifdef _MIG_TRACE_PARAMETERS_
53 
54 #define __BeforeRcvCallTrace(msgid, arg1, arg2, arg3, arg4)         \
55 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
56 	    KDBG_MIGCODE(msgid) | DBG_FUNC_START,                   \
57 	    (unsigned int)(arg1),                                   \
58 	    (unsigned int)(arg2),                                   \
59 	    (unsigned int)(arg3),                                   \
60 	    (unsigned int)(arg4),                                   \
61 	    (unsigned int)(0));
62 
63 #define __AfterRcvCallTrace(msgid, arg1, arg2, arg3, arg4)          \
64 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
65 	    KDBG_MIGCODE(msgid) | DBG_FUNC_END,                     \
66 	    (unsigned int)(arg1),                                   \
67 	    (unsigned int)(arg2),                                   \
68 	    (unsigned int)(arg3),                                   \
69 	    (unsigned int)(arg4),                                   \
70 	    (unsigned int)(0));
71 
72 #define __BeforeSimpleCallTrace(msgid, arg1, arg2, arg3, arg4)      \
73 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
74 	    KDBG_MIGCODE(msgid) | DBG_FUNC_START,                   \
75 	    (unsigned int)(arg1),                                   \
76 	    (unsigned int)(arg2),                                   \
77 	    (unsigned int)(arg3),                                   \
78 	    (unsigned int)(arg4),                                   \
79 	    (unsigned int)(0));
80 
81 #define __AfterSimpleCallTrace(msgid, arg1, arg2, arg3, arg4)       \
82 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
83 	    KDBG_MIGCODE(msgid) | DBG_FUNC_END,                     \
84 	    (unsigned int)(arg1),                                   \
85 	    (unsigned int)(arg2),                                   \
86 	    (unsigned int)(arg3),                                   \
87 	    (unsigned int)(arg4),                                   \
88 	    (unsigned int)(0));
89 
90 #define __BeforeKobjectServerTrace(msgid)       ((void)0)
91 #define __AfterKobjectServerTrace(msgid)        ((void)0)
92 
93 #else /* !_MIG_TRACE_PARAMETERS_ */
94 
95 #define __BeforeKobjectServerTrace(msgid)                           \
96 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
97 	    KDBG_MIGCODE(msgid) | DBG_FUNC_START, 0u, 0u, 0u, 0u, 0u)
98 
99 #define __AfterKobjectServerTrace(msgid)                            \
100 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
101 	    KDBG_MIGCODE(msgid) | DBG_FUNC_END, 0u, 0u, 0u, 0u, 0u)
102 
103 #endif /* !_MIG_TRACE_PARAMETERS_ */
104 
105 #define _MIG_MSGID_INVALID(msgid)                                   \
106 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,                     \
107 	    MACHDBG_CODE(DBG_MACH_MSGID_INVALID, (msgid)), 0u, 0u, 0u, 0u, 0u)
108 
109 #endif  /* XNU_KERNEL_PRIVATE */
110 
111 __BEGIN_DECLS
112 
113 /* Send a message from the kernel */
114 
115 #if XNU_KERNEL_PRIVATE
116 extern mach_msg_return_t mach_msg_send_from_kernel(
117 	mach_msg_header_t       *msg,
118 	mach_msg_size_t         send_size);
119 #else
120 extern mach_msg_return_t mach_msg_send_from_kernel_proper(
121 	mach_msg_header_t       *msg,
122 	mach_msg_size_t         send_size);
123 
124 #define mach_msg_send_from_kernel mach_msg_send_from_kernel_proper
125 #endif
126 
127 /*
128  * Allocate kernel message buffer(s) large enough to fit the message,
129  * and accept a block that populates the message content.
130  *
131  *     - descriptor_count: Descriptor count in the outgoing message. If non-zero,
132  *       kernel expects a complex message, and vice-versa.
133  *
134  *     - payload_size: Size of data not processed by kernel (i.e. size of data
135  *       following the header, or last descriptor if the message is complex).
136  *       This is NOT the total size of the outgoing message.
137  *
138  * For memory safety the kmsg buffers allocated may occupy non-contiguous memory
139  * and it is the caller's responsibility to correctly set up the message
140  * based on content's offset from the mach message header, as follows:
141  *
142  * void (^builder)(mach_msg_header_t *header, mach_msg_descriptor_t *descs, void *payload))
143  *
144  *            header* ───────► ┌─────────────────────────┐
145  *                             │                         │
146  *                             │    mach_msg_header_t    │
147  *                             │                         │
148  *                             ├─────────────────────────┤
149  *                             │(optional)mach_msg_body_t│
150  *             descs* ───────► ├─────────────────────────┤
151  *                             │                         │
152  *                             │                         │
153  *                             │                         │
154  *                             │  (optional)descriptors  │
155  *                             │                         │
156  *                             │                         │
157  *                             │                         │
158  *                             └─────────────────────────┘
159  *                              ~~~~~~~~void space~~~~~~~
160  *           payload* ───────► ┌─────────────────────────┐
161  *                             │                         │
162  *                             │                         │
163  *                             │    other data in msg    │
164  *                             │                         │
165  *                             │                         │
166  *                             └─────────────────────────┘
167  *
168  *     header: Points to the start of the message, caller should populate this
169  *          with the mach message header.
170  *
171  *     descs: Pointers to the start of kernel descriptors region, if caller requested
172  *          a non-zero descriptor_count. Or NULL if descriptor_count is 0 (meaning
173  *          the messsage is not complex). Caller should populate this with descriptors.
174  *
175  *     payload: Points to the start of data not processed by kernel, which is after
176  *          the last descriptor, if caller requested a non-zero payload_size. Or
177  *          NULL if payload_size is 0.
178  *
179  *     Mach message body (descriptor count) will be set after the builder accordingly.
180  */
181 #if MACH_KERNEL_PRIVATE || !XNU_KERNEL_PRIVATE
182 
183 extern mach_msg_return_t kernel_mach_msg_send_with_builder(
184 	mach_msg_size_t         descriptor_count,
185 	mach_msg_size_t         payload_size,    /* Warning: NOT total send size */
186 	void                  (^builder)(mach_msg_header_t *header,
187 	mach_msg_descriptor_t  *__counted_by(descriptor_count)descs, /* Nullable */
188 	void                   *__sized_by(payload_size)payload));   /* Nullable */
189 
190 #endif /* MACH_KERNEL_PRIVATE || !XNU_KERNEL_PRIVATE */
191 #if XNU_KERNEL_PRIVATE
192 
193 extern mach_msg_return_t mach_msg_rpc_from_kernel(
194 	mach_msg_header_t      *msg,
195 	mach_msg_size_t         send_size,
196 	mach_msg_size_t         rcv_size);
197 
198 extern void mach_msg_destroy_from_kernel(
199 	mach_msg_header_t      *msg);
200 
201 #else
202 
203 extern mach_msg_return_t mach_msg_rpc_from_kernel_proper(
204 	mach_msg_header_t      *msg,
205 	mach_msg_size_t         send_size,
206 	mach_msg_size_t         rcv_size);
207 
208 extern void mach_msg_destroy_from_kernel_proper(
209 	mach_msg_header_t      *msg);
210 
211 #define mach_msg_rpc_from_kernel    mach_msg_rpc_from_kernel_proper
212 #define mach_msg_destroy_from_kernel mach_msg_destroy_from_kernel_proper
213 
214 #endif
215 
216 #ifdef XNU_KERNEL_PRIVATE
217 
218 mach_msg_return_t kernel_mach_msg_rpc(
219 	mach_msg_header_t      *msg,
220 	mach_msg_size_t         send_size,
221 	mach_msg_size_t         rcv_size,
222 	boolean_t               interruptible,
223 	boolean_t              *message_moved);
224 
225 extern mach_msg_return_t kernel_mach_msg_send(
226 	mach_msg_header_t      *msg,
227 	mach_msg_size_t         send_size,
228 	mach_msg_option64_t     option,
229 	mach_msg_timeout_t      timeout_val,
230 	boolean_t              *message_moved);
231 
232 extern mach_msg_return_t kernel_mach_msg_send_kmsg(
233 	struct ipc_kmsg        *kmsg);
234 
235 extern mach_msg_return_t kernel_mach_msg_send_with_builder_internal(
236 	mach_msg_size_t         desc_count,
237 	mach_msg_size_t         payload_size, /* Not total send size */
238 	mach_msg_option64_t     option,
239 	mach_msg_timeout_t      timeout_val,
240 	boolean_t              *message_moved,
241 	void                  (^builder)(mach_msg_header_t *,
242 	mach_msg_descriptor_t *, void *));
243 
244 extern mach_msg_return_t mach_msg_send_from_kernel_with_options(
245 	mach_msg_header_t      *msg,
246 	mach_msg_size_t         send_size,
247 	mach_msg_option64_t     option,
248 	mach_msg_timeout_t      timeout_val);
249 
250 #endif /* XNU_KERNEL_PRIVATE */
251 #ifdef  MACH_KERNEL_PRIVATE
252 
253 extern void mach_msg_receive_continue(void);
254 
255 #endif  /* MACH_KERNEL_PRIVATE */
256 
257 __END_DECLS
258 
259 #endif  /* _KERN_IPC_MIG_H_ */
260