xref: /linux-6.15/include/uapi/linux/android/binder.h (revision feba3900)
1 /*
2  * Copyright (C) 2008 Google, Inc.
3  *
4  * Based on, but no longer compatible with, the original
5  * OpenBinder.org binder driver interface, which is:
6  *
7  * Copyright (c) 2005 Palmsource, Inc.
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef _UAPI_LINUX_BINDER_H
21 #define _UAPI_LINUX_BINDER_H
22 
23 #include <linux/types.h>
24 #include <linux/ioctl.h>
25 
26 #define B_PACK_CHARS(c1, c2, c3, c4) \
27 	((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
28 #define B_TYPE_LARGE 0x85
29 
30 enum {
31 	BINDER_TYPE_BINDER	= B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE),
32 	BINDER_TYPE_WEAK_BINDER	= B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE),
33 	BINDER_TYPE_HANDLE	= B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE),
34 	BINDER_TYPE_WEAK_HANDLE	= B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE),
35 	BINDER_TYPE_FD		= B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE),
36 };
37 
38 enum {
39 	FLAT_BINDER_FLAG_PRIORITY_MASK = 0xff,
40 	FLAT_BINDER_FLAG_ACCEPTS_FDS = 0x100,
41 };
42 
43 #ifdef BINDER_IPC_32BIT
44 typedef __u32 binder_size_t;
45 typedef __u32 binder_uintptr_t;
46 #else
47 typedef __u64 binder_size_t;
48 typedef __u64 binder_uintptr_t;
49 #endif
50 
51 /**
52  * struct binder_object_header - header shared by all binder metadata objects.
53  * @type:	type of the object
54  */
55 struct binder_object_header {
56 	__u32        type;
57 };
58 
59 /*
60  * This is the flattened representation of a Binder object for transfer
61  * between processes.  The 'offsets' supplied as part of a binder transaction
62  * contains offsets into the data where these structures occur.  The Binder
63  * driver takes care of re-writing the structure type and data as it moves
64  * between processes.
65  */
66 struct flat_binder_object {
67 	struct binder_object_header	hdr;
68 	__u32				flags;
69 
70 	/* 8 bytes of data. */
71 	union {
72 		binder_uintptr_t	binder;	/* local object */
73 		__u32			handle;	/* remote object */
74 	};
75 
76 	/* extra data associated with local object */
77 	binder_uintptr_t	cookie;
78 };
79 
80 /**
81  * struct binder_fd_object - describes a filedescriptor to be fixed up.
82  * @hdr:	common header structure
83  * @pad_flags:	padding to remain compatible with old userspace code
84  * @pad_binder:	padding to remain compatible with old userspace code
85  * @fd:		file descriptor
86  * @cookie:	opaque data, used by user-space
87  */
88 struct binder_fd_object {
89 	struct binder_object_header	hdr;
90 	__u32				pad_flags;
91 	union {
92 		binder_uintptr_t	pad_binder;
93 		__u32			fd;
94 	};
95 
96 	binder_uintptr_t		cookie;
97 };
98 /*
99  * On 64-bit platforms where user code may run in 32-bits the driver must
100  * translate the buffer (and local binder) addresses appropriately.
101  */
102 
103 struct binder_write_read {
104 	binder_size_t		write_size;	/* bytes to write */
105 	binder_size_t		write_consumed;	/* bytes consumed by driver */
106 	binder_uintptr_t	write_buffer;
107 	binder_size_t		read_size;	/* bytes to read */
108 	binder_size_t		read_consumed;	/* bytes consumed by driver */
109 	binder_uintptr_t	read_buffer;
110 };
111 
112 /* Use with BINDER_VERSION, driver fills in fields. */
113 struct binder_version {
114 	/* driver protocol version -- increment with incompatible change */
115 	__s32       protocol_version;
116 };
117 
118 /* This is the current protocol version. */
119 #ifdef BINDER_IPC_32BIT
120 #define BINDER_CURRENT_PROTOCOL_VERSION 7
121 #else
122 #define BINDER_CURRENT_PROTOCOL_VERSION 8
123 #endif
124 
125 #define BINDER_WRITE_READ		_IOWR('b', 1, struct binder_write_read)
126 #define BINDER_SET_IDLE_TIMEOUT		_IOW('b', 3, __s64)
127 #define BINDER_SET_MAX_THREADS		_IOW('b', 5, __u32)
128 #define BINDER_SET_IDLE_PRIORITY	_IOW('b', 6, __s32)
129 #define BINDER_SET_CONTEXT_MGR		_IOW('b', 7, __s32)
130 #define BINDER_THREAD_EXIT		_IOW('b', 8, __s32)
131 #define BINDER_VERSION			_IOWR('b', 9, struct binder_version)
132 
133 /*
134  * NOTE: Two special error codes you should check for when calling
135  * in to the driver are:
136  *
137  * EINTR -- The operation has been interupted.  This should be
138  * handled by retrying the ioctl() until a different error code
139  * is returned.
140  *
141  * ECONNREFUSED -- The driver is no longer accepting operations
142  * from your process.  That is, the process is being destroyed.
143  * You should handle this by exiting from your process.  Note
144  * that once this error code is returned, all further calls to
145  * the driver from any thread will return this same code.
146  */
147 
148 enum transaction_flags {
149 	TF_ONE_WAY	= 0x01,	/* this is a one-way call: async, no return */
150 	TF_ROOT_OBJECT	= 0x04,	/* contents are the component's root object */
151 	TF_STATUS_CODE	= 0x08,	/* contents are a 32-bit status code */
152 	TF_ACCEPT_FDS	= 0x10,	/* allow replies with file descriptors */
153 };
154 
155 struct binder_transaction_data {
156 	/* The first two are only used for bcTRANSACTION and brTRANSACTION,
157 	 * identifying the target and contents of the transaction.
158 	 */
159 	union {
160 		/* target descriptor of command transaction */
161 		__u32	handle;
162 		/* target descriptor of return transaction */
163 		binder_uintptr_t ptr;
164 	} target;
165 	binder_uintptr_t	cookie;	/* target object cookie */
166 	__u32		code;		/* transaction command */
167 
168 	/* General information about the transaction. */
169 	__u32	        flags;
170 	pid_t		sender_pid;
171 	uid_t		sender_euid;
172 	binder_size_t	data_size;	/* number of bytes of data */
173 	binder_size_t	offsets_size;	/* number of bytes of offsets */
174 
175 	/* If this transaction is inline, the data immediately
176 	 * follows here; otherwise, it ends with a pointer to
177 	 * the data buffer.
178 	 */
179 	union {
180 		struct {
181 			/* transaction data */
182 			binder_uintptr_t	buffer;
183 			/* offsets from buffer to flat_binder_object structs */
184 			binder_uintptr_t	offsets;
185 		} ptr;
186 		__u8	buf[8];
187 	} data;
188 };
189 
190 struct binder_ptr_cookie {
191 	binder_uintptr_t ptr;
192 	binder_uintptr_t cookie;
193 };
194 
195 struct binder_handle_cookie {
196 	__u32 handle;
197 	binder_uintptr_t cookie;
198 } __packed;
199 
200 struct binder_pri_desc {
201 	__s32 priority;
202 	__u32 desc;
203 };
204 
205 struct binder_pri_ptr_cookie {
206 	__s32 priority;
207 	binder_uintptr_t ptr;
208 	binder_uintptr_t cookie;
209 };
210 
211 enum binder_driver_return_protocol {
212 	BR_ERROR = _IOR('r', 0, __s32),
213 	/*
214 	 * int: error code
215 	 */
216 
217 	BR_OK = _IO('r', 1),
218 	/* No parameters! */
219 
220 	BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
221 	BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
222 	/*
223 	 * binder_transaction_data: the received command.
224 	 */
225 
226 	BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
227 	/*
228 	 * not currently supported
229 	 * int: 0 if the last bcATTEMPT_ACQUIRE was not successful.
230 	 * Else the remote object has acquired a primary reference.
231 	 */
232 
233 	BR_DEAD_REPLY = _IO('r', 5),
234 	/*
235 	 * The target of the last transaction (either a bcTRANSACTION or
236 	 * a bcATTEMPT_ACQUIRE) is no longer with us.  No parameters.
237 	 */
238 
239 	BR_TRANSACTION_COMPLETE = _IO('r', 6),
240 	/*
241 	 * No parameters... always refers to the last transaction requested
242 	 * (including replies).  Note that this will be sent even for
243 	 * asynchronous transactions.
244 	 */
245 
246 	BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
247 	BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
248 	BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
249 	BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
250 	/*
251 	 * void *:	ptr to binder
252 	 * void *: cookie for binder
253 	 */
254 
255 	BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
256 	/*
257 	 * not currently supported
258 	 * int:	priority
259 	 * void *: ptr to binder
260 	 * void *: cookie for binder
261 	 */
262 
263 	BR_NOOP = _IO('r', 12),
264 	/*
265 	 * No parameters.  Do nothing and examine the next command.  It exists
266 	 * primarily so that we can replace it with a BR_SPAWN_LOOPER command.
267 	 */
268 
269 	BR_SPAWN_LOOPER = _IO('r', 13),
270 	/*
271 	 * No parameters.  The driver has determined that a process has no
272 	 * threads waiting to service incoming transactions.  When a process
273 	 * receives this command, it must spawn a new service thread and
274 	 * register it via bcENTER_LOOPER.
275 	 */
276 
277 	BR_FINISHED = _IO('r', 14),
278 	/*
279 	 * not currently supported
280 	 * stop threadpool thread
281 	 */
282 
283 	BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
284 	/*
285 	 * void *: cookie
286 	 */
287 	BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
288 	/*
289 	 * void *: cookie
290 	 */
291 
292 	BR_FAILED_REPLY = _IO('r', 17),
293 	/*
294 	 * The the last transaction (either a bcTRANSACTION or
295 	 * a bcATTEMPT_ACQUIRE) failed (e.g. out of memory).  No parameters.
296 	 */
297 };
298 
299 enum binder_driver_command_protocol {
300 	BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
301 	BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
302 	/*
303 	 * binder_transaction_data: the sent command.
304 	 */
305 
306 	BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
307 	/*
308 	 * not currently supported
309 	 * int:  0 if the last BR_ATTEMPT_ACQUIRE was not successful.
310 	 * Else you have acquired a primary reference on the object.
311 	 */
312 
313 	BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
314 	/*
315 	 * void *: ptr to transaction data received on a read
316 	 */
317 
318 	BC_INCREFS = _IOW('c', 4, __u32),
319 	BC_ACQUIRE = _IOW('c', 5, __u32),
320 	BC_RELEASE = _IOW('c', 6, __u32),
321 	BC_DECREFS = _IOW('c', 7, __u32),
322 	/*
323 	 * int:	descriptor
324 	 */
325 
326 	BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
327 	BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
328 	/*
329 	 * void *: ptr to binder
330 	 * void *: cookie for binder
331 	 */
332 
333 	BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
334 	/*
335 	 * not currently supported
336 	 * int: priority
337 	 * int: descriptor
338 	 */
339 
340 	BC_REGISTER_LOOPER = _IO('c', 11),
341 	/*
342 	 * No parameters.
343 	 * Register a spawned looper thread with the device.
344 	 */
345 
346 	BC_ENTER_LOOPER = _IO('c', 12),
347 	BC_EXIT_LOOPER = _IO('c', 13),
348 	/*
349 	 * No parameters.
350 	 * These two commands are sent as an application-level thread
351 	 * enters and exits the binder loop, respectively.  They are
352 	 * used so the binder can have an accurate count of the number
353 	 * of looping threads it has available.
354 	 */
355 
356 	BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14,
357 						struct binder_handle_cookie),
358 	/*
359 	 * int: handle
360 	 * void *: cookie
361 	 */
362 
363 	BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15,
364 						struct binder_handle_cookie),
365 	/*
366 	 * int: handle
367 	 * void *: cookie
368 	 */
369 
370 	BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
371 	/*
372 	 * void *: cookie
373 	 */
374 };
375 
376 #endif /* _UAPI_LINUX_BINDER_H */
377 
378