binder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. #define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
  258. #define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
  259. #define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
  260. #define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32)
  261. #define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32)
  262. #define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
  263. #define BINDER_VERSION _IOWR('b', 9, struct binder_version)
  264. #define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
  265. #define BINDER_GET_NODE_INFO_FOR_REF _IOWR('b', 12, struct binder_node_info_for_ref)
  266. #define BINDER_SET_CONTEXT_MGR_EXT _IOW('b', 13, struct flat_binder_object)
  267. #define BINDER_FREEZE _IOW('b', 14, struct binder_freeze_info)
  268. #define BINDER_GET_FROZEN_INFO _IOWR('b', 15, struct binder_frozen_status_info)
  269. #define BINDER_ENABLE_ONEWAY_SPAM_DETECTION _IOW('b', 16, __u32)
  270. /*
  271. * NOTE: Two special error codes you should check for when calling
  272. * in to the driver are:
  273. *
  274. * EINTR -- The operation has been interupted. This should be
  275. * handled by retrying the ioctl() until a different error code
  276. * is returned.
  277. *
  278. * ECONNREFUSED -- The driver is no longer accepting operations
  279. * from your process. That is, the process is being destroyed.
  280. * You should handle this by exiting from your process. Note
  281. * that once this error code is returned, all further calls to
  282. * the driver from any thread will return this same code.
  283. */
  284. enum transaction_flags {
  285. TF_ONE_WAY = 0x01, /* this is a one-way call: async, no return */
  286. TF_ROOT_OBJECT = 0x04, /* contents are the component's root object */
  287. TF_STATUS_CODE = 0x08, /* contents are a 32-bit status code */
  288. TF_ACCEPT_FDS = 0x10, /* allow replies with file descriptors */
  289. TF_CLEAR_BUF = 0x20, /* clear buffer on txn complete */
  290. };
  291. struct binder_transaction_data {
  292. /* The first two are only used for bcTRANSACTION and brTRANSACTION,
  293. * identifying the target and contents of the transaction.
  294. */
  295. union {
  296. /* target descriptor of command transaction */
  297. __u32 handle;
  298. /* target descriptor of return transaction */
  299. binder_uintptr_t ptr;
  300. } target;
  301. binder_uintptr_t cookie; /* target object cookie */
  302. __u32 code; /* transaction command */
  303. /* General information about the transaction. */
  304. __u32 flags;
  305. pid_t sender_pid;
  306. uid_t sender_euid;
  307. binder_size_t data_size; /* number of bytes of data */
  308. binder_size_t offsets_size; /* number of bytes of offsets */
  309. /* If this transaction is inline, the data immediately
  310. * follows here; otherwise, it ends with a pointer to
  311. * the data buffer.
  312. */
  313. union {
  314. struct {
  315. /* transaction data */
  316. binder_uintptr_t buffer;
  317. /* offsets from buffer to flat_binder_object structs */
  318. binder_uintptr_t offsets;
  319. } ptr;
  320. __u8 buf[8];
  321. } data;
  322. };
  323. struct binder_transaction_data_secctx {
  324. struct binder_transaction_data transaction_data;
  325. binder_uintptr_t secctx;
  326. };
  327. struct binder_transaction_data_sg {
  328. struct binder_transaction_data transaction_data;
  329. binder_size_t buffers_size;
  330. };
  331. struct binder_ptr_cookie {
  332. binder_uintptr_t ptr;
  333. binder_uintptr_t cookie;
  334. };
  335. struct binder_handle_cookie {
  336. __u32 handle;
  337. binder_uintptr_t cookie;
  338. } __packed;
  339. struct binder_pri_desc {
  340. __s32 priority;
  341. __u32 desc;
  342. };
  343. struct binder_pri_ptr_cookie {
  344. __s32 priority;
  345. binder_uintptr_t ptr;
  346. binder_uintptr_t cookie;
  347. };
  348. enum binder_driver_return_protocol {
  349. BR_ERROR = _IOR('r', 0, __s32),
  350. /*
  351. * int: error code
  352. */
  353. BR_OK = _IO('r', 1),
  354. /* No parameters! */
  355. BR_TRANSACTION_SEC_CTX = _IOR('r', 2,
  356. struct binder_transaction_data_secctx),
  357. /*
  358. * binder_transaction_data_secctx: the received command.
  359. */
  360. BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
  361. BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
  362. /*
  363. * binder_transaction_data: the received command.
  364. */
  365. BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
  366. /*
  367. * not currently supported
  368. * int: 0 if the last bcATTEMPT_ACQUIRE was not successful.
  369. * Else the remote object has acquired a primary reference.
  370. */
  371. BR_DEAD_REPLY = _IO('r', 5),
  372. /*
  373. * The target of the last transaction (either a bcTRANSACTION or
  374. * a bcATTEMPT_ACQUIRE) is no longer with us. No parameters.
  375. */
  376. BR_TRANSACTION_COMPLETE = _IO('r', 6),
  377. /*
  378. * No parameters... always refers to the last transaction requested
  379. * (including replies). Note that this will be sent even for
  380. * asynchronous transactions.
  381. */
  382. BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
  383. BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
  384. BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
  385. BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
  386. /*
  387. * void *: ptr to binder
  388. * void *: cookie for binder
  389. */
  390. BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
  391. /*
  392. * not currently supported
  393. * int: priority
  394. * void *: ptr to binder
  395. * void *: cookie for binder
  396. */
  397. BR_NOOP = _IO('r', 12),
  398. /*
  399. * No parameters. Do nothing and examine the next command. It exists
  400. * primarily so that we can replace it with a BR_SPAWN_LOOPER command.
  401. */
  402. BR_SPAWN_LOOPER = _IO('r', 13),
  403. /*
  404. * No parameters. The driver has determined that a process has no
  405. * threads waiting to service incoming transactions. When a process
  406. * receives this command, it must spawn a new service thread and
  407. * register it via bcENTER_LOOPER.
  408. */
  409. BR_FINISHED = _IO('r', 14),
  410. /*
  411. * not currently supported
  412. * stop threadpool thread
  413. */
  414. BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
  415. /*
  416. * void *: cookie
  417. */
  418. BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
  419. /*
  420. * void *: cookie
  421. */
  422. BR_FAILED_REPLY = _IO('r', 17),
  423. /*
  424. * The the last transaction (either a bcTRANSACTION or
  425. * a bcATTEMPT_ACQUIRE) failed (e.g. out of memory). No parameters.
  426. */
  427. BR_FROZEN_REPLY = _IO('r', 18),
  428. /*
  429. * The target of the last transaction (either a bcTRANSACTION or
  430. * a bcATTEMPT_ACQUIRE) is frozen. No parameters.
  431. */
  432. BR_ONEWAY_SPAM_SUSPECT = _IO('r', 19),
  433. /*
  434. * Current process sent too many oneway calls to target, and the last
  435. * asynchronous transaction makes the allocated async buffer size exceed
  436. * detection threshold. No parameters.
  437. */
  438. };
  439. enum binder_driver_command_protocol {
  440. BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
  441. BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
  442. /*
  443. * binder_transaction_data: the sent command.
  444. */
  445. BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
  446. /*
  447. * not currently supported
  448. * int: 0 if the last BR_ATTEMPT_ACQUIRE was not successful.
  449. * Else you have acquired a primary reference on the object.
  450. */
  451. BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
  452. /*
  453. * void *: ptr to transaction data received on a read
  454. */
  455. BC_INCREFS = _IOW('c', 4, __u32),
  456. BC_ACQUIRE = _IOW('c', 5, __u32),
  457. BC_RELEASE = _IOW('c', 6, __u32),
  458. BC_DECREFS = _IOW('c', 7, __u32),
  459. /*
  460. * int: descriptor
  461. */
  462. BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
  463. BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
  464. /*
  465. * void *: ptr to binder
  466. * void *: cookie for binder
  467. */
  468. BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
  469. /*
  470. * not currently supported
  471. * int: priority
  472. * int: descriptor
  473. */
  474. BC_REGISTER_LOOPER = _IO('c', 11),
  475. /*
  476. * No parameters.
  477. * Register a spawned looper thread with the device.
  478. */
  479. BC_ENTER_LOOPER = _IO('c', 12),
  480. BC_EXIT_LOOPER = _IO('c', 13),
  481. /*
  482. * No parameters.
  483. * These two commands are sent as an application-level thread
  484. * enters and exits the binder loop, respectively. They are
  485. * used so the binder can have an accurate count of the number
  486. * of looping threads it has available.
  487. */
  488. BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14,
  489. struct binder_handle_cookie),
  490. /*
  491. * int: handle
  492. * void *: cookie
  493. */
  494. BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15,
  495. struct binder_handle_cookie),
  496. /*
  497. * int: handle
  498. * void *: cookie
  499. */
  500. BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
  501. /*
  502. * void *: cookie
  503. */
  504. BC_TRANSACTION_SG = _IOW('c', 17, struct binder_transaction_data_sg),
  505. BC_REPLY_SG = _IOW('c', 18, struct binder_transaction_data_sg),
  506. /*
  507. * binder_transaction_data_sg: the sent command.
  508. */
  509. };
  510. #endif /* _UAPI_LINUX_BINDER_H */