Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Binder 驅動核心數據結構梳理

編輯:關於 Android 程式設計

Binder 是 Android 系統的基石之一,其原理一直是學習中的難點。反復研讀相關資料後,我把 Binder 驅動中的關鍵數據結構做了一次系統梳理,便于日后回顧。

需要重點掌握的幾個結構:binder_node 是注冊服務在內核中的代表;binder_ref 是客戶端連接在驅動中的代表;binder_buffer 是內核緩沖區,通過 mmap 映射後可在用戶空間操作,內核也可訪問;binder_proc 是進程的代表,客戶端與服務端都有,用于管理上述結構。

binder_work:工作項

進程間通信根據 Client 與 Server 的狀態設置該值:

 struct binder_work { struct list_head entry; enum { BINDER_WORK_TRANSACTION = 1, BINDER_WORK_TRANSACTION_COMPLETE, BINDER_WORK_NODE, BINDER_WORK_DEAD_BINDER, BINDER_WORK_DEAD_BINDER_AND_CLEAR, BINDER_WORK_CLEAR_DEATH_NOTIFICATION, } type; };

其中 BINDER_WORK_DEAD_BINDER 用于 Binder 驅動檢測到 Service 組件死亡時,找到引用它的 client 進程并發送死亡通知;BINDER_WORK_CLEAR_DEATH_NOTIFICATION 用于 Client 注銷死亡通知且 Service 未死亡時,驅動找到之前注冊的 binder_ref_death 結構并修改其 work,再封裝成工作項加入 Client 進程的 todo 隊列。

binder_node:Binder 實體對象

它是 Service 中的 Binder 在內核中的代表:

 struct binder_node { int debug_id; struct binder_work work; union { struct rb_node rb_node; struct hlist_node dead_node; }; struct binder_proc *proc; struct hlist_head refs; int internal_strong_refs; int local_weak_refs; int local_strong_refs; void __user *ptr; void __user *cookie; unsigned has_strong_ref:1; unsigned pending_strong_ref:1; unsigned has_weak_ref:1; unsigned pending_weak_ref:1; unsigned has_async_transaction:1; unsigned accept_fds:1; unsigned min_priority:8; struct list_head async_todo; };

其中 ptr 指向 Service 組件內部的弱引用計數對象,cookie 指向 Service 組件的地址,refs 維護所有 Client 的 Binder 引用,async_todo 是異步事務隊列。Service 組件在驅動中的 binder_node 與 binder_ref 都維護引用計數。

binder_ref_death:死亡通知

用于描述 Client 組件的死亡通知在驅動中的代表:

 struct binder_ref_death { struct binder_work work; void __user *cookie; };

當 Binder 驅動決定向客戶端進程發送 Service 組件死亡通知時,會把該結構封裝成工作項加入 Client 進程的 todo 隊列;Client 處理時通過 work 成員區分具體是哪一種情況。

binder_ref:Binder 引用對象

它是 Client 進程中的 Binder 引用在驅動中的代表:

 struct binder_ref { int debug_id; struct rb_node rb_node_desc; struct rb_node rb_node_node; struct hlist_node node_entry; struct binder_proc *proc; struct binder_node *node; uint32_t desc; int strong; int weak; struct binder_ref_death *death; };

在 Client 進程的用戶空間,Binder 引用對象通過一個句柄值(desc)來描述,驅動可據此找到對應的 binder_ref。node 指向它所引用的 binder_node,death 則保存 Client 注冊的死亡通知。

binder_buffer:內核緩沖區

 struct binder_buffer { struct list_head entry; struct rb_node rb_node; unsigned free:1; unsigned allow_user_free:1; unsigned async_transaction:1; unsigned debug_id:29; struct binder_transaction *transaction; struct binder_node *target_node; size_t data_size; size_t offsets_size; uint8_t data[0]; };

free 為 1 表示該內核緩沖區空閒、關聯的物理頁面;allow_user_free 為 1 時,Service 組件處理完后可請求驅動釋放;data 指向大小可變的數據緩沖區,用于保存通信數據(普通數據與 Binder 對象), offsets_size 記錄每個 Binder 對象在數據緩沖區中的位置。Binder 驅動只關心其中的 Binder 對象。

binder_proc:進程代表

進程調用 open /dev/binder 時創建,并保存在全局 hash 中:

 struct binder_proc { struct hlist_node proc_node; struct rb_root threads; struct rb_root nodes; struct rb_root refs_by_desc; struct rb_root refs_by_node; int pid; struct vm_area_struct *vma; struct task_struct *tsk; struct files_struct *files; struct hlist_node deferred_work_node; int deferred_work; void *buffer; ptrdiff_t user_buffer_offset; struct list_head buffers; struct rb_root free_buffers; struct rb_root allocated_buffers; size_t free_async_space; struct page **pages; size_t buffer_size; uint32_t buffer_free; struct list_head todo; wait_queue_head_t wait; struct binder_stats stats; struct list_head delivered_death; int max_threads; int requested_threads; int requested_threads_started; int ready_threads; long default_priority; struct dentry *debugfs_entry; };

其中 buffer 是內核空間的緩沖區地址(一大塊空間,劃分為多個 binder_buffer),user_buffer_offset 是內核與用戶空間地址的差值;todo 是待處理工作項隊列,wait 用于空閒 binder 線程睡眠;requested_threads 記錄驅動主動請求進程注冊的線程數,ready_threads 記錄當前空閒的 Binder 線程數。

deferred_work 的可能取值:

 enum binder_deferred_state { BINDER_DEFERRED_PUT_FILES = 0x01, BINDER_DEFERRED_FLUSH = 0x02, BINDER_DEFERRED_RELEASE = 0x04, };

分別對應進程關閉文件描述符、喚醒線程檢查新工作項、以及不再使用 binder 機制時釋放資源的場景。Binder 驅動會為內核緩沖區分配文件描述符,進程可通過它把內核緩沖區映射到自己的地址空間。

binder_thread:線程池中的線程

 struct binder_thread { struct binder_proc *proc; struct rb_node rb_node; int pid; int looper; struct binder_transaction *transaction_stack; struct list_head todo; uint32_t return_error; uint32_t return_error2; wait_queue_head_t wait; struct binder_stats stats; };

線程注冊到 binder 驅動時,驅動會創建該結構。transaction_stack 用于棧式管理事務(誰處理就放在最前端),todo 存放 Client 進程的請求,wait 用于等待依賴線程處理結束。

線程的狀態:

 enum { BINDER_LOOPER_STATE_REGISTERED = 0x01, BINDER_LOOPER_STATE_ENTERED = 0x02, BINDER_LOOPER_STATE_EXITED = 0x04, BINDER_LOOPER_STATE_INVALID = 0x08, BINDER_LOOPER_STATE_WAITING = 0x10, BINDER_LOOPER_STATE_NEED_RETURN = 0x20 };

線程由應用程序主動注冊,通過 BC_ENTER_LOOPER 通知驅動;BINDER_LOOPER_STATE_WAITING 表示線程處于空閒狀態。

binder_transaction:通信過程

 struct binder_transaction { int debug_id; struct binder_work work; struct binder_thread *from; struct binder_transaction *from_parent; struct binder_proc *to_proc; struct binder_thread *to_thread; struct binder_transaction *to_parent; unsigned need_reply:1; struct binder_buffer *buffer; unsigned int code; unsigned int flags; long priority; long saved_priority; uid_t sender_euid; };

from 是發起事務的源線程,to_proc 與 to_thread 是負責處理該事務的進程與線程,need_reply 為 1 表示同步事務,buffer 是驅動為該事務分配的內存緩沖區。驅動為目標線程創建事務后會設置 BINDER_WORK_TRANSACTION 并加入其 todo 隊列。

交互命令與協議

應用程序通過 IO 命令與驅動交互:

 #define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
 struct binder_write_read { signed long write_size; signed long write_consumed; unsigned long write_buffer; signed long read_size; signed long read_consumed; unsigned long read_buffer; };

write_buffer 指向從用戶空間傳輸到驅動的輸入數據,read_buffer 指向用戶空間用于接收輸出的緩沖區。

進程發送給驅動的命令協議(BinderDriverCommandProtocol)包括 BC_TRANSACTION、BC_REPLY、BC_FREE_BUFFER、BC_INCREFS、BC_ACQUIRE、BC_RELEASE、BC_DECREFS、BC_REGISTER_LOOPER、BC_ENTER_LOOPER、BC_EXIT_LOOPER、BC_REQUEST_DEATH_NOTIFICATION、BC_CLEAR_DEATH_NOTIFICATION 等。源進程用 BC_TRANSACTION 請求驅動把數據傳到目標進程,目標進程處理完畢后用 BC_REPLY 把結果回傳。

驅動返回給進程的協議(BinderDriverReturnProtocol)包括 BR_ERROR、BR_OK、BR_TRANSACTION、BR_REPLY、BR_DEAD_REPLY、BR_TRANSACTION_COMPLETE、BR_INCREFS、BR_ACQUIRE、BR_SPAWN_LOOPER、BR_DEAD_BINDER、BR_FAILED_REPLY 等。例如 BR_TRANSACTION 用于通知 Server 進程處理通信請求,BR_SPAWN_LOOPER 用于通知進程向線程池增加一個線程,BR_DEAD_BINDER 用于通知 Client 進程 Service 組件已死亡。

通信數據結構

 struct binder_ptr_cookie { void *ptr; void *cookie; };
 struct binder_transaction_data { union { size_t handle; void *ptr; } target; void *cookie; unsigned int code; unsigned int flags; pid_t sender_pid; uid_t sender_euid; size_t data_size; size_t offsets_size; union { struct { const void *buffer; const void *offsets; } ptr; uint8_t buf[8]; } data; };

target 在發送時是引用對象的句柄值,返回時是 Service 組件弱引用計數的地址;data.buffer 指向真正保存通信數據的緩沖區,data.offsets 保存每個 Binder 對象的位置。

flags 的取值:

 enum transaction_flags { TF_ONE_WAY = 0x01, TF_ROOT_OBJECT = 0x04, TF_STATUS_CODE = 0x08, TF_ACCEPT_FDS = 0x10, };

TF_ONE_WAY 為 1 表示異步通信,TF_STATUS_CODE 為 1 表示數據緩沖區內容是一個 4 字節狀態碼。

flat_binder_object:扁平結構

數據緩沖區中每個 Binder 對象都用它來描述:

 struct flat_binder_object { unsigned long type; unsigned long flags; union { void *binder; signed long handle; }; void *cookie; };

type 用于區分是 Binder 實體對象、引用對象還是文件描述符,其取值如下:

 #define B_PACK_CHARS(c1, c2, c3, c4) ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) #define B_TYPE_LARGE 0x85 enum { BINDER_TYPE_BINDER = B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE), BINDER_TYPE_WEAK_BINDER = B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE), BINDER_TYPE_HANDLE = B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE), BINDER_TYPE_WEAK_HANDLE = B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE), BINDER_TYPE_FD = B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE), };

它們分別對應強類型實體、弱類型實體、強類型引用、弱類型引用與文件描述符。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved