1 // SPDX-License-Identifier: GPL-2.0 2 3 // Copyright (C) 2025 Google LLC. 4 5 use crate::transaction::Transaction; 6 7 use core::ptr; 8 9 use kernel::bindings::{rust_binder_transaction, task_struct}; 10 use kernel::error::Result; 11 use kernel::ffi::{c_int, c_uint, c_ulong}; 12 use kernel::task::Task; 13 use kernel::tracepoint::declare_trace; 14 15 declare_trace! { 16 unsafe fn binder_ioctl(cmd: c_uint, arg: c_ulong); 17 unsafe fn binder_ioctl_done(ret: c_int); 18 unsafe fn binder_read_done(ret: c_int); 19 unsafe fn binder_write_done(ret: c_int); 20 unsafe fn binder_wait_for_work(proc_work: bool, transaction_stack: bool, thread_todo: bool); 21 unsafe fn binder_transaction(reply: bool, t: rust_binder_transaction, thread: *mut task_struct); 22 unsafe fn binder_transaction_received(t: rust_binder_transaction); 23 unsafe fn binder_transaction_fd_send(t_debug_id: c_int, fd: c_int, offset: usize); 24 unsafe fn binder_transaction_fd_recv(t_debug_id: c_int, fd: c_int, offset: usize); 25 unsafe fn binder_command(cmd: u32); 26 unsafe fn binder_return(ret: u32); 27 unsafe fn binder_transaction_alloc_buf( 28 debug_id: c_int,data_size: usize, offset_size: usize, extra_buffers_size: usize); 29 unsafe fn binder_transaction_buffer_release(debug_id: c_int); 30 unsafe fn binder_transaction_failed_buffer_release(debug_id: c_int); 31 unsafe fn binder_transaction_update_buffer_release(debug_id: c_int); 32 } 33 34 #[inline] 35 fn raw_transaction(t: &Transaction) -> rust_binder_transaction { 36 ptr::from_ref(t).cast_mut().cast() 37 } 38 39 #[inline] 40 fn to_errno(ret: Result) -> i32 { 41 match ret { 42 Ok(()) => 0, 43 Err(err) => err.to_errno(), 44 } 45 } 46 47 #[inline] 48 pub(crate) fn trace_ioctl(cmd: u32, arg: usize) { 49 // SAFETY: Always safe to call. 50 unsafe { binder_ioctl(cmd, arg as c_ulong) } 51 } 52 53 #[inline] 54 pub(crate) fn trace_ioctl_done(ret: Result) { 55 // SAFETY: Always safe to call. 56 unsafe { binder_ioctl_done(to_errno(ret)) } 57 } 58 #[inline] 59 pub(crate) fn trace_read_done(ret: Result) { 60 // SAFETY: Always safe to call. 61 unsafe { binder_read_done(to_errno(ret)) } 62 } 63 #[inline] 64 pub(crate) fn trace_write_done(ret: Result) { 65 // SAFETY: Always safe to call. 66 unsafe { binder_write_done(to_errno(ret)) } 67 } 68 69 #[inline] 70 pub(crate) fn trace_wait_for_work(proc_work: bool, transaction_stack: bool, thread_todo: bool) { 71 // SAFETY: Always safe to call. 72 unsafe { binder_wait_for_work(proc_work, transaction_stack, thread_todo) } 73 } 74 75 #[inline] 76 pub(crate) fn trace_transaction(reply: bool, t: &Transaction, thread: Option<&Task>) { 77 let thread = match thread { 78 Some(thread) => thread.as_ptr(), 79 None => core::ptr::null_mut(), 80 }; 81 // SAFETY: The raw transaction is valid for the duration of this call. The thread pointer is 82 // valid or null. 83 unsafe { binder_transaction(reply, raw_transaction(t), thread) } 84 } 85 86 #[inline] 87 pub(crate) fn trace_transaction_received(t: &Transaction) { 88 // SAFETY: The raw transaction is valid for the duration of this call. 89 unsafe { binder_transaction_received(raw_transaction(t)) } 90 } 91 92 #[inline] 93 pub(crate) fn trace_transaction_fd_send(t_debug_id: usize, fd: u32, offset: usize) { 94 // SAFETY: This function is always safe to call. 95 unsafe { binder_transaction_fd_send(t_debug_id as c_int, fd as c_int, offset) } 96 } 97 #[inline] 98 pub(crate) fn trace_transaction_fd_recv(t_debug_id: usize, fd: u32, offset: usize) { 99 // SAFETY: This function is always safe to call. 100 unsafe { binder_transaction_fd_recv(t_debug_id as c_int, fd as c_int, offset) } 101 } 102 103 #[inline] 104 pub(crate) fn trace_command(cmd: u32) { 105 // SAFETY: This function is always safe to call. 106 unsafe { binder_command(cmd) } 107 } 108 #[inline] 109 pub(crate) fn trace_return(ret: u32) { 110 // SAFETY: This function is always safe to call. 111 unsafe { binder_return(ret) } 112 } 113 #[inline] 114 pub(crate) fn trace_transaction_alloc_buf( 115 debug_id: usize, 116 data_size: usize, 117 offset_size: usize, 118 extra_buffers_size: usize, 119 ) { 120 // SAFETY: The `data` pointer is valid. 121 unsafe { 122 binder_transaction_alloc_buf( 123 debug_id as c_int, 124 data_size, 125 offset_size, 126 extra_buffers_size, 127 ) 128 } 129 } 130 #[inline] 131 pub(crate) fn trace_transaction_buffer_release(debug_id: usize) { 132 // SAFETY: This function is always safe to call. 133 unsafe { binder_transaction_buffer_release(debug_id as c_int) } 134 } 135 #[inline] 136 pub(crate) fn trace_transaction_failed_buffer_release(debug_id: usize) { 137 // SAFETY: This function is always safe to call. 138 unsafe { binder_transaction_failed_buffer_release(debug_id as c_int) } 139 } 140 #[inline] 141 pub(crate) fn trace_transaction_update_buffer_release(debug_id: usize) { 142 // SAFETY: This function is always safe to call. 143 unsafe { binder_transaction_update_buffer_release(debug_id as c_int) } 144 } 145