xref: /linux/drivers/android/binder/rust_binder.h (revision 94c37d42cb7ca362aee9633bec2dbeed787edf3e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2025 Google, Inc.
4  */
5 
6 #ifndef _LINUX_RUST_BINDER_H
7 #define _LINUX_RUST_BINDER_H
8 
9 #include <uapi/linux/android/binder.h>
10 #include <uapi/linux/android/binderfs.h>
11 
12 /*
13  * These symbols are exposed by `rust_binderfs.c` and exist here so that Rust
14  * Binder can call them.
15  */
16 int init_rust_binderfs(void);
17 
18 struct dentry;
19 struct inode;
20 struct dentry *rust_binderfs_create_proc_file(struct inode *nodp, int pid);
21 void rust_binderfs_remove_file(struct dentry *dentry);
22 
23 /*
24  * The internal data types in the Rust Binder driver are opaque to C, so we use
25  * void pointer typedefs for these types.
26  */
27 
28 typedef void *rust_binder_transaction;
29 typedef void *rust_binder_process;
30 typedef void *rust_binder_node;
31 
32 struct rb_process_layout {
33 	size_t arc_offset;
34 	size_t task;
35 };
36 
37 struct rb_transaction_layout {
38 	size_t debug_id;
39 	size_t code;
40 	size_t flags;
41 	size_t from_thread;
42 	size_t to_proc;
43 	size_t target_node;
44 };
45 
46 struct rb_node_layout {
47 	size_t arc_offset;
48 	size_t debug_id;
49 	size_t ptr;
50 };
51 
52 struct rust_binder_layout {
53 	struct rb_transaction_layout t;
54 	struct rb_process_layout p;
55 	struct rb_node_layout n;
56 };
57 
58 extern const struct rust_binder_layout RUST_BINDER_LAYOUT;
59 
60 static inline size_t rust_binder_transaction_debug_id(rust_binder_transaction t)
61 {
62 	return *(size_t *) (t + RUST_BINDER_LAYOUT.t.debug_id);
63 }
64 
65 static inline u32 rust_binder_transaction_code(rust_binder_transaction t)
66 {
67 	return *(u32 *) (t + RUST_BINDER_LAYOUT.t.code);
68 }
69 
70 static inline u32 rust_binder_transaction_flags(rust_binder_transaction t)
71 {
72 	return *(u32 *) (t + RUST_BINDER_LAYOUT.t.flags);
73 }
74 
75 // Nullable!
76 static inline rust_binder_node rust_binder_transaction_target_node(rust_binder_transaction t)
77 {
78 	void *p = *(void **) (t + RUST_BINDER_LAYOUT.t.target_node);
79 
80 	if (p)
81 		p = p + RUST_BINDER_LAYOUT.n.arc_offset;
82 	return NULL;
83 }
84 
85 static inline rust_binder_process rust_binder_transaction_to_proc(rust_binder_transaction t)
86 {
87 	void *p = *(void **) (t + RUST_BINDER_LAYOUT.t.to_proc);
88 
89 	return p + RUST_BINDER_LAYOUT.p.arc_offset;
90 }
91 
92 static inline struct task_struct *rust_binder_process_task(rust_binder_process t)
93 {
94 	return *(struct task_struct **) (t + RUST_BINDER_LAYOUT.p.task);
95 }
96 
97 static inline size_t rust_binder_node_debug_id(rust_binder_node t)
98 {
99 	return *(size_t *) (t + RUST_BINDER_LAYOUT.n.debug_id);
100 }
101 
102 #endif
103