深入底层:一文彻底搞懂 Android Binder 通信的 6 步物理全流程

4 阅读6分钟

在 Android 系统中,Binder 是跨进程通信(IPC)的绝对灵魂。很多人知道 Binder 是基于 C/S 架构的,也知道它比 Socket 快,但如果你去深挖它在物理内存和内核态/用户态之间的流转,你会发现其设计精妙绝伦。

本文抛开复杂的 C++ 源码,直接从物理内存和系统调度的视角,用 6 个步骤彻底拆解 Binder 的通信全流程。

📌 前置认知:关于那“1MB 的共享内存”

在看流程之前,必须纠正一个常见的认知误区:这 1MB 的映射内存(Binder 缓冲区),绝对不是通信时才临时开辟的,而是在 Client 和 Server 进程一启动(初始化 ProcessState)时,就通过 mmap 系统调用,提前在内核空间分配好,并同时映射到各自用户空间的。  每个进程都有且只有这么一块。

掌握了这个前提,我们来看通信的完整流程:

🚀 Binder 物理通信 6 步全流程

1. 客户端发起请求(准备数据)
当客户端(Client)需要调用另一个进程时,它首先会在自己的用户空间里,将请求数据打包成一个普通的 Parcel 对象。随后,通过 ioctl 系统调用(svc 指令),引发 CPU 特权级从 EL0(用户态)陷入 EL1(内核态),向 Binder 驱动发送请求。
(注:数据此时在 Client 的普通用户态内存中,还没进共享内存)

2. 一次拷贝(Client 核心魔法)
Binder 驱动接管后,确认目标进程是 Server。它将 Client 发送过来的数据,直接从 Client 的用户空间,拷贝一次,写入到 Server 进程早就准备好的、大小约 1MB 的内核缓冲区里(这块缓冲区已经映射到了 Server 的用户空间)。
(注:传统 Socket 需要“用户态->内核->用户态”两次拷贝,Binder 在这里省掉了一次)

3. 任务入队与线程唤醒
数据拷完后,Binder 驱动会在内核中生成一个任务节点(binder_work),将其插入到 Server 进程专属的 todo 队列中。随后,Binder 驱动唤醒 Server 中处于休眠状态的某个 Binder 线程(Server 通常维护 16 个 Binder 线程的池子)。

4. Server 执行任务(回到用户态)
被唤醒的 Server Binder 线程此时依然在内核态,它从 todo 队列里取出任务单,然后带着数据通过 eret 指令降权,返回 EL0 用户态。在用户态按图索骥,执行对应的 onTransact 方法(比如执行 AMS 的 startActivity 逻辑)。
执行完成后,Server 同样会在自己的用户空间新建一个 Parcel,将返回值写入其中,再次陷入内核态发起系统调用。

5. 回传拷贝(Server 返回数据)
Binder 驱动拿到 Server 的返回数据,再次执行“一次拷贝”魔法:将数据从 Server 的用户空间,直接拷贝到 Client 进程早就映射好的那块 1MB 共享内存中。

6. 唤醒客户端(读取结果)
最后,Binder 驱动将回包任务插入 Client 的 todo 队列,唤醒休眠的 Client 线程。Client 醒来后,直接从自己的 1MB 共享内存中读取结果,跨进程调用圆满结束。

💡 架构师总结:为什么 Binder 快且安全?

通过上述 6 步,你会发现 Binder 其实是对称的:

  • 控制面 :依靠 todo 队列和内核线程池唤醒,实现了高并发下的任务排队与调度。
  • 数据面 :依靠 mmap 内存映射,去程和回程各只有 1 次拷贝,实现了极致的零拷贝性能(对接收方而言)。

更巧妙的是,为什么不让两端直接映射同一块物理内存(连那一次拷贝都省了)?因为安全。内核的这一次拷贝,既是数据的搬运,也是对跨进程权限和数据的强行拦截与校验,完美兼顾了性能与安全隔离。



🇬🇧 English Version

In the Android ecosystem, Binder is the absolute soul of Inter-Process Communication (IPC). Most developers know it follows a Client-Server (C/S) architecture and is faster than traditional Sockets. But have you ever wondered about the physical memory layout and the Kernel/User space transitions happening under the hood?

Let's bypass the complex C++ source code and dissect the entire Binder communication process from the perspective of physical memory and OS scheduling, broken down into 6 steps.

📌 Prerequisite: The "1MB Shared Memory" Misconception

Before diving into the flow, we must correct a common misunderstanding: The ~1MB mapped memory (Binder buffer) is NOT allocated on-demand during communication. Instead, it is allocated via the mmap system call into the Kernel Space and simultaneously mapped into the User Space of both the Client and Server processes the moment they start (during ProcessState initialization). Every process has exactly one such buffer.

With that in mind, here is the complete 6-step physical communication flow:

🚀 The 6-Step Binder Communication Flow

Step 1: The Client Initiates the Request (Data Preparation)
When the Client needs to call another process, it first packs the request data into a standard Parcel object within its own User Space. Then, it triggers an ioctl system call (using the svcinstruction), causing the CPU privilege level to escalate from EL0 (User Mode) to EL1 (Kernel Mode), sending the request to the Binder driver.
(Note: At this point, the data is in the Client's regular User Space, not yet in the shared memory).

Step 2: The "One-Copy" Magic (Client -> Server)
Once the Binder driver takes over and identifies the target Server process, it performs the magic: it copies the data exactly ONCE from the Client's User Space directly into the ~1MB Kernel buffer allocated for the Server process (which is already mapped into the Server's User Space).
(Note: Traditional Sockets require two copies: User A -> Kernel -> User B. Binder eliminates one).

Step 3: Task Enqueue and Thread Wake-up
After the copy, the Binder driver generates a task node (binder_work) in the Kernel and appends it to the Server's dedicated todo queue. Then, the driver wakes up an idle Binder thread from the Server's Binder thread pool (typically 16 threads).

Step 4: Server Executes the Task (Returning to User Space)
The awakened Server Binder thread is still in Kernel Mode. It fetches the task from the todoqueue, carries the data, and executes the eret instruction to drop privileges back to EL0 (User Mode). It then executes the corresponding onTransact method (e.g., triggering AMS's startActivity logic).
Once done, the Server creates a new Parcel in its own User Space with the return values, and executes a system call to enter Kernel Mode again.

Step 5: The Reply Copy (Server -> Client)
The Binder driver receives the reply data and repeats the "one-copy" magic: it copies the data from the Server's User Space directly into the 1MB shared memory buffer pre-mapped in the Client's User Space.

Step 6: Wake Up the Client (Reading the Result)
Finally, the Binder driver places the reply task into the Client's todo queue and wakes up the sleeping Client thread. The Client wakes up and reads the result directly from its own mapped shared memory, concluding the IPC process.

💡 Architect's Summary: Why is Binder Fast AND Secure?

Looking at these 6 steps, Binder's symmetry becomes clear:

  • Control Plane: Relies on the todo queue and Kernel thread pool wake-ups to handle high-concurrency task scheduling.
  • Data Plane: Relies on mmap memory mapping. Each direction (Request and Reply) requires only exactly ONE copy, achieving ultimate zero-copy performance for the receiver.

You might wonder: Since we are already using memory mapping, why not map the EXACT SAME physical memory page for both Client and Server to achieve true "Zero-Copy"?
The answer is Security. If they shared the exact same physical page, a malicious Client could directly modify the Server's memory (e.g., the system's AMS), crashing the OS. That single copy performed by the Kernel acts not just as a data transfer, but as a strict security boundary and permission check, perfectly balancing extreme performance with absolute isolation.