binder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * Copyright (C) 2008 Google, Inc.
  4. *
  5. * Based on, but no longer compatible with, the original
  6. * OpenBinder.org binder driver interface, which is:
  7. *
  8. * Copyright (c) 2005 Palmsource, Inc.
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #ifndef _UAPI_LINUX_BINDER_H
  21. #define _UAPI_LINUX_BINDER_H
  22. #include <linux/types.h>
  23. #include <linux/ioctl.h>
  24. #define B_PACK_CHARS(c1, c2, c3, c4) \
  25. ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
  26. #define B_TYPE_LARGE 0x85
  27. enum {
  28. BINDER_TYPE_BINDER = B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE),
  29. BINDER_TYPE_WEAK_BINDER = B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE),
  30. BINDER_TYPE_HANDLE = B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE),
  31. BINDER_TYPE_WEAK_HANDLE = B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE),
  32. BINDER_TYPE_FD = B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE),
  33. BINDER_TYPE_FDA = B_PACK_CHARS('f', 'd', 'a', B_TYPE_LARGE),
  34. BINDER_TYPE_PTR = B_PACK_CHARS('p', 't', '*', B_TYPE_LARGE),
  35. };
  36. /**
  37. * enum flat_binder_object_shifts: shift values for flat_binder_object_flags
  38. * @FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT: shift for getting scheduler policy.
  39. *
  40. */
  41. enum flat_binder_object_shifts {
  42. FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT = 9,
  43. };
  44. /**
  45. * enum flat_binder_object_flags - flags for use in flat_binder_object.flags
  46. */
  47. enum flat_binder_object_flags {
  48. /**
  49. * @FLAT_BINDER_FLAG_PRIORITY_MASK: bit-mask for min scheduler priority
  50. *
  51. * These bits can be used to set the minimum scheduler priority
  52. * at which transactions into this node should run. Valid values
  53. * in these bits depend on the scheduler policy encoded in
  54. * @FLAT_BINDER_FLAG_SCHED_POLICY_MASK.
  55. *
  56. * For SCHED_NORMAL/SCHED_BATCH, the valid range is between [-20..19]
  57. * For SCHED_FIFO/SCHED_RR, the value can run between [1..99]
  58. */
  59. FLAT_BINDER_FLAG_PRIORITY_MASK = 0xff,
  60. /**
  61. * @FLAT_BINDER_FLAG_ACCEPTS_FDS: whether the node accepts fds.
  62. */
  63. FLAT_BINDER_FLAG_ACCEPTS_FDS = 0x100,
  64. /**
  65. * @FLAT_BINDER_FLAG_SCHED_POLICY_MASK: bit-mask for scheduling policy
  66. *
  67. * These two bits can be used to set the min scheduling policy at which
  68. * transactions on this node should run. These match the UAPI
  69. * scheduler policy values, eg:
  70. * 00b: SCHED_NORMAL
  71. * 01b: SCHED_FIFO
  72. * 10b: SCHED_RR
  73. * 11b: SCHED_BATCH
  74. */
  75. FLAT_BINDER_FLAG_SCHED_POLICY_MASK =
  76. 3U << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT,
  77. /**
  78. * @FLAT_BINDER_FLAG_INHERIT_RT: whether the node inherits RT policy
  79. *
  80. * Only when set, calls into this node will inherit a real-time
  81. * scheduling policy from the caller (for synchronous transactions).
  82. */
  83. FLAT_BINDER_FLAG_INHERIT_RT = 0x800,
  84. /**
  85. * @FLAT_BINDER_FLAG_TXN_SECURITY_CTX: request security contexts
  86. *
  87. * Only when set, causes senders to include their security
  88. * context
  89. */
  90. FLAT_BINDER_FLAG_TXN_SECURITY_CTX = 0x1000,
  91. };
  92. #ifdef BINDER_IPC_32BIT
  93. typedef __u32 binder_size_t;
  94. typedef __u32 binder_uintptr_t;
  95. #else
  96. typedef __u64 binder_size_t;
  97. typedef __u64 binder_uintptr_t;
  98. #endif
  99. /**
  100. * struct binder_object_header - header shared by all binder metadata objects.
  101. * @type: type of the object
  102. */
  103. struct binder_object_header {
  104. __u32 type;
  105. };
  106. /*
  107. * This is the flattened representation of a Binder object for transfer
  108. * between processes. The 'offsets' supplied as part of a binder transaction
  109. * contains offsets into the data where these structures occur. The Binder
  110. * driver takes care of re-writing the structure type and data as it moves
  111. * between processes.
  112. */
  113. struct flat_binder_object {
  114. struct binder_object_header hdr;
  115. __u32 flags;
  116. /* 8 bytes of data. */
  117. union {
  118. binder_uintptr_t binder; /* local object */
  119. __u32 handle; /* remote object */
  120. };
  121. /* extra data associated with local object */
  122. binder_uintptr_t cookie;
  123. };
  124. /**
  125. * struct binder_fd_object - describes a filedescriptor to be fixed up.
  126. * @hdr: common header structure
  127. * @pad_flags: padding to remain compatible with old userspace code
  128. * @pad_binder: padding to remain compatible with old userspace code
  129. * @fd: file descriptor
  130. * @cookie: opaque data, used by user-space
  131. */
  132. struct binder_fd_object {
  133. struct binder_object_header hdr;
  134. __u32 pad_flags;
  135. union {
  136. binder_uintptr_t pad_binder;
  137. __u32 fd;
  138. };
  139. binder_uintptr_t cookie;
  140. };
  141. /* struct binder_buffer_object - object describing a userspace buffer
  142. * @hdr: common header structure
  143. * @flags: one or more BINDER_BUFFER_* flags
  144. * @buffer: address of the buffer
  145. * @length: length of the buffer
  146. * @parent: index in offset array pointing to parent buffer
  147. * @parent_offset: offset in @parent pointing to this buffer
  148. *
  149. * A binder_buffer object represents an object that the
  150. * binder kernel driver can copy verbatim to the target
  151. * address space. A buffer itself may be pointed to from
  152. * within another buffer, meaning that the pointer inside
  153. * that other buffer needs to be fixed up as well. This
  154. * can be done by setting the BINDER_BUFFER_FLAG_HAS_PARENT
  155. * flag in @flags, by setting @parent buffer to the index
  156. * in the offset array pointing to the parent binder_buffer_object,
  157. * and by setting @parent_offset to the offset in the parent buffer
  158. * at which the pointer to this buffer is located.
  159. */
  160. struct binder_buffer_object {
  161. struct binder_object_header hdr;
  162. __u32 flags;
  163. binder_uintptr_t buffer;
  164. binder_size_t length;
  165. binder_size_t parent;
  166. binder_size_t parent_offset;
  167. };
  168. enum {
  169. BINDER_BUFFER_FLAG_HAS_PARENT = 0x01,
  170. };
  171. /* struct binder_fd_array_object - object describing an array of fds in a buffer
  172. * @hdr: common header structure
  173. * @pad: padding to ensure correct alignment
  174. * @num_fds: number of file descriptors in the buffer
  175. * @parent: index in offset array to buffer holding the fd array
  176. * @parent_offset: start offset of fd array in the buffer
  177. *
  178. * A binder_fd_array object represents an array of file
  179. * descriptors embedded in a binder_buffer_object. It is
  180. * different from a regular binder_buffer_object because it
  181. * describes a list of file descriptors to fix up, not an opaque
  182. * blob of memory, and hence the kernel needs to treat it differently.
  183. *
  184. * An example of how this would be used is with Android's
  185. * native_handle_t object, which is a struct with a list of integers
  186. * and a list of file descriptors. The native_handle_t struct itself
  187. * will be represented by a struct binder_buffer_objct, whereas the
  188. * embedded list of file descriptors is represented by a
  189. * struct binder_fd_array_object with that binder_buffer_object as
  190. * a parent.
  191. */
  192. struct binder_fd_array_object {
  193. struct binder_object_header hdr;
  194. __u32 pad;
  195. binder_size_t num_fds;
  196. binder_size_t parent;
  197. binder_size_t parent_offset;
  198. };
  199. /*
  200. * On 64-bit platforms where user code may run in 32-bits the driver must
  201. * translate the buffer (and local binder) addresses appropriately.
  202. */
  203. struct binder_write_read {
  204. binder_size_t write_size; /* bytes to write */
  205. binder_size_t write_consumed; /* bytes consumed by driver */
  206. binder_uintptr_t write_buffer;
  207. binder_size_t read_size; /* bytes to read */
  208. binder_size_t read_consumed; /* bytes consumed by driver */
  209. binder_uintptr_t read_buffer;
  210. };
  211. /* Use with BINDER_VERSION, driver fills in fields. */
  212. struct binder_version {
  213. /* driver protocol version -- increment with incompatible change */
  214. __s32 protocol_version;
  215. };
  216. /* This is the current protocol version. */
  217. #ifdef BINDER_IPC_32BIT
  218. #define BINDER_CURRENT_PROTOCOL_VERSION 7
  219. #else
  220. #define BINDER_CURRENT_PROTOCOL_VERSION 8
  221. #endif
  222. /*
  223. * Use with BINDER_GET_NODE_DEBUG_INFO, driver reads ptr, writes to all fields.
  224. * Set ptr to NULL for the first call to get the info for the first node, and
  225. * then repeat the call passing the previously returned value to get the next
  226. * nodes. ptr will be 0 when there are no more nodes.
  227. */
  228. struct binder_node_debug_info {
  229. binder_uintptr_t ptr;
  230. binder_uintptr_t cookie;
  231. __u32 has_strong_ref;
  232. __u32 has_weak_ref;
  233. };
  234. struct binder_node_info_for_ref {
  235. __u32 handle;
  236. __u32 strong_count;
  237. __u32 weak_count;
  238. __u32 reserved1;
  239. __u32 reserved2;
  240. __u32 reserved3;
  241. };
  242. struct binder_freeze_info {
  243. __u32 pid;
  244. __u32 enable;
  245. __u32 timeout_ms;
  246. };
  247. struct binder_frozen_status_info {
  248. __u32 pid;
  249. /* process received sync transactions since last frozen
  250. * bit 0: received sync transaction after being frozen
  251. * bit 1: new pending sync transaction during freezing
  252. */
  253. __u32 sync_recv;
  254. /* process received async transactions since last frozen */
  255. __u32 async_recv;
  256. };
  257. /* struct binder_extened_error - extended error information
  258. * @id: identifier for the failed operation
  259. * @command: command as defined by binder_driver_return_protocol
  260. * @param: parameter holding a negative errno value
  261. *
  262. * Used with BINDER_GET_EXTENDED_ERROR. This extends the error information
  263. * returned by the driver upon a failed operation. Userspace can pull this
  264. * data to properly handle specific error scenarios.
  265. */
  266. struct binder_extended_error {
  267. __u32 id;
  268. __u32 command;
  269. __s32 param;
  270. };
  271. #define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
  272. #define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
  273. #define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
  274. #define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32)
  275. #define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32)
  276. #define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
  277. #define BINDER_VERSION _IOWR('b', 9, struct binder_version)
  278. #define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
  279. #define BINDER_GET_NODE_INFO_FOR_REF _IOWR('b', 12, struct binder_node_info_for_ref)
  280. #define BINDER_SET_CONTEXT_MGR_EXT _IOW('b', 13, struct flat_binder_object)
  281. #define BINDER_FREEZE _IOW('b', 14, struct binder_freeze_info)
  282. #define BINDER_GET_FROZEN_INFO _IOWR('b', 15, struct binder_frozen_status_info)
  283. #define BINDER_ENABLE_ONEWAY_SPAM_DETECTION _IOW('b', 16, __u32)
  284. #define BINDER_GET_EXTENDED_ERROR _IOWR('b', 17, struct binder_extended_error)
  285. /*
  286. * NOTE: Two special error codes you should check for when calling
  287. * in to the driver are:
  288. *
  289. * EINTR -- The operation has been interupted. This should be
  290. * handled by retrying the ioctl() until a different error code
  291. * is returned.
  292. *
  293. * ECONNREFUSED -- The driver is no longer accepting operations
  294. * from your process. That is, the process is being destroyed.
  295. * You should handle this by exiting from your process. Note
  296. * that once this error code is returned, all further calls to
  297. * the driver from any thread will return this same code.
  298. */
  299. enum transaction_flags {
  300. TF_ONE_WAY = 0x01, /* this is a one-way call: async, no return */
  301. TF_ROOT_OBJECT = 0x04, /* contents are the component's root object */
  302. TF_STATUS_CODE = 0x08, /* contents are a 32-bit status code */
  303. TF_ACCEPT_FDS = 0x10, /* allow replies with file descriptors */
  304. TF_CLEAR_BUF = 0x20, /* clear buffer on txn complete */
  305. TF_UPDATE_TXN = 0x40, /* update the outdated pending async txn */
  306. };
  307. struct binder_transaction_data {
  308. /* The first two are only used for bcTRANSACTION and brTRANSACTION,
  309. * identifying the target and contents of the transaction.
  310. */
  311. union {
  312. /* target descriptor of command transaction */
  313. __u32 handle;
  314. /* target descriptor of return transaction */
  315. binder_uintptr_t ptr;
  316. } target;
  317. binder_uintptr_t cookie; /* target object cookie */
  318. __u32 code; /* transaction command */
  319. /* General information about the transaction. */
  320. __u32 flags;
  321. __kernel_pid_t sender_pid;
  322. __kernel_uid32_t sender_euid;
  323. binder_size_t data_size; /* number of bytes of data */
  324. binder_size_t offsets_size; /* number of bytes of offsets */
  325. /* If this transaction is inline, the data immediately
  326. * follows here; otherwise, it ends with a pointer to
  327. * the data buffer.
  328. */
  329. union {
  330. struct {
  331. /* transaction data */
  332. binder_uintptr_t buffer;
  333. /* offsets from buffer to flat_binder_object structs */
  334. binder_uintptr_t offsets;
  335. } ptr;
  336. __u8 buf[8];
  337. } data;
  338. };
  339. struct binder_transaction_data_secctx {
  340. struct binder_transaction_data transaction_data;
  341. binder_uintptr_t secctx;
  342. };
  343. struct binder_transaction_data_sg {
  344. struct binder_transaction_data transaction_data;
  345. binder_size_t buffers_size;
  346. };
  347. struct binder_ptr_cookie {
  348. binder_uintptr_t ptr;
  349. binder_uintptr_t cookie;
  350. };
  351. struct binder_handle_cookie {
  352. __u32 handle;
  353. binder_uintptr_t cookie;
  354. } __packed;
  355. struct binder_pri_desc {
  356. __s32 priority;
  357. __u32 desc;
  358. };
  359. struct binder_pri_ptr_cookie {
  360. __s32 priority;
  361. binder_uintptr_t ptr;
  362. binder_uintptr_t cookie;
  363. };
  364. enum binder_driver_return_protocol {
  365. BR_ERROR = _IOR('r', 0, __s32),
  366. /*
  367. * int: error code
  368. */
  369. BR_OK = _IO('r', 1),
  370. /* No parameters! */
  371. BR_TRANSACTION_SEC_CTX = _IOR('r', 2,
  372. struct binder_transaction_data_secctx),
  373. /*
  374. * binder_transaction_data_secctx: the received command.
  375. */
  376. BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
  377. BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
  378. /*
  379. * binder_transaction_data: the received command.
  380. */
  381. BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
  382. /*
  383. * not currently supported
  384. * int: 0 if the last bcATTEMPT_ACQUIRE was not successful.
  385. * Else the remote object has acquired a primary reference.
  386. */
  387. BR_DEAD_REPLY = _IO('r', 5),
  388. /*
  389. * The target of the last transaction (either a bcTRANSACTION or
  390. * a bcATTEMPT_ACQUIRE) is no longer with us. No parameters.
  391. */
  392. BR_TRANSACTION_COMPLETE = _IO('r', 6),
  393. /*
  394. * No parameters... always refers to the last transaction requested
  395. * (including replies). Note that this will be sent even for
  396. * asynchronous transactions.
  397. */
  398. BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
  399. BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
  400. BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
  401. BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
  402. /*
  403. * void *: ptr to binder
  404. * void *: cookie for binder
  405. */
  406. BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
  407. /*
  408. * not currently supported
  409. * int: priority
  410. * void *: ptr to binder
  411. * void *: cookie for binder
  412. */
  413. BR_NOOP = _IO('r', 12),
  414. /*
  415. * No parameters. Do nothing and examine the next command. It exists
  416. * primarily so that we can replace it with a BR_SPAWN_LOOPER command.
  417. */
  418. BR_SPAWN_LOOPER = _IO('r', 13),
  419. /*
  420. * No parameters. The driver has determined that a process has no
  421. * threads waiting to service incoming transactions. When a process
  422. * receives this command, it must spawn a new service thread and
  423. * register it via bcENTER_LOOPER.
  424. */
  425. BR_FINISHED = _IO('r', 14),
  426. /*
  427. * not currently supported
  428. * stop threadpool thread
  429. */
  430. BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
  431. /*
  432. * void *: cookie
  433. */
  434. BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
  435. /*
  436. * void *: cookie
  437. */
  438. BR_FAILED_REPLY = _IO('r', 17),
  439. /*
  440. * The last transaction (either a bcTRANSACTION or
  441. * a bcATTEMPT_ACQUIRE) failed (e.g. out of memory). No parameters.
  442. */
  443. BR_FROZEN_REPLY = _IO('r', 18),
  444. /*
  445. * The target of the last transaction (either a bcTRANSACTION or
  446. * a bcATTEMPT_ACQUIRE) is frozen. No parameters.
  447. */
  448. BR_ONEWAY_SPAM_SUSPECT = _IO('r', 19),
  449. /*
  450. * Current process sent too many oneway calls to target, and the last
  451. * asynchronous transaction makes the allocated async buffer size exceed
  452. * detection threshold. No parameters.
  453. */
  454. };
  455. enum binder_driver_command_protocol {
  456. BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
  457. BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
  458. /*
  459. * binder_transaction_data: the sent command.
  460. */
  461. BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
  462. /*
  463. * not currently supported
  464. * int: 0 if the last BR_ATTEMPT_ACQUIRE was not successful.
  465. * Else you have acquired a primary reference on the object.
  466. */
  467. BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
  468. /*
  469. * void *: ptr to transaction data received on a read
  470. */
  471. BC_INCREFS = _IOW('c', 4, __u32),
  472. BC_ACQUIRE = _IOW('c', 5, __u32),
  473. BC_RELEASE = _IOW('c', 6, __u32),
  474. BC_DECREFS = _IOW('c', 7, __u32),
  475. /*
  476. * int: descriptor
  477. */
  478. BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
  479. BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
  480. /*
  481. * void *: ptr to binder
  482. * void *: cookie for binder
  483. */
  484. BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
  485. /*
  486. * not currently supported
  487. * int: priority
  488. * int: descriptor
  489. */
  490. BC_REGISTER_LOOPER = _IO('c', 11),
  491. /*
  492. * No parameters.
  493. * Register a spawned looper thread with the device.
  494. */
  495. BC_ENTER_LOOPER = _IO('c', 12),
  496. BC_EXIT_LOOPER = _IO('c', 13),
  497. /*
  498. * No parameters.
  499. * These two commands are sent as an application-level thread
  500. * enters and exits the binder loop, respectively. They are
  501. * used so the binder can have an accurate count of the number
  502. * of looping threads it has available.
  503. */
  504. BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14,
  505. struct binder_handle_cookie),
  506. /*
  507. * int: handle
  508. * void *: cookie
  509. */
  510. BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15,
  511. struct binder_handle_cookie),
  512. /*
  513. * int: handle
  514. * void *: cookie
  515. */
  516. BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
  517. /*
  518. * void *: cookie
  519. */
  520. BC_TRANSACTION_SG = _IOW('c', 17, struct binder_transaction_data_sg),
  521. BC_REPLY_SG = _IOW('c', 18, struct binder_transaction_data_sg),
  522. /*
  523. * binder_transaction_data_sg: the sent command.
  524. */
  525. };
  526. #endif /* _UAPI_LINUX_BINDER_H */