projects/linux·blob master · refs

107 lines · 2.4 KiB

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 */
16int init_rust_binderfs(void);
17
18struct dentry;
19struct inode;
20struct dentry *rust_binderfs_create_proc_file(struct inode *nodp, int pid);
21void 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
28typedef void *rust_binder_transaction;
29typedef void *rust_binder_process;
30typedef void *rust_binder_node;
31
32struct rb_process_layout {
33 size_t arc_offset;
34 size_t task;
35};
36
37struct 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
46struct rb_node_layout {
47 size_t arc_offset;
48 size_t debug_id;
49 size_t ptr;
50};
51
52struct rust_binder_layout {
53 struct rb_transaction_layout t;
54 struct rb_process_layout p;
55 struct rb_node_layout n;
56};
57
58extern const struct rust_binder_layout RUST_BINDER_LAYOUT;
59
60static 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
65static inline u32 rust_binder_transaction_code(rust_binder_transaction t)
66{
67 return *(u32 *) (t + RUST_BINDER_LAYOUT.t.code);
68}
69
70static inline u32 rust_binder_transaction_flags(rust_binder_transaction t)
71{
72 return *(u32 *) (t + RUST_BINDER_LAYOUT.t.flags);
73}
74
75// Nullable!
76static 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 p;
83}
84
85static 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
92static 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
97static 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
102static inline binder_uintptr_t rust_binder_node_ptr(rust_binder_node t)
103{
104 return *(binder_uintptr_t *) (t + RUST_BINDER_LAYOUT.n.ptr);
105}
106
107#endif