binder.h 16 KB

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