引言
要真正吃透 Android 架构,我们必须跳出 API 的迷局,向下看硬件。这是一张极其微观的物理图谱——展示了当你按下手机电源键的那一瞬间,直到你的 App 在屏幕上跑起来,底层到底发生了什么。
1. 本质认知:指令的汪洋大海
不管是庞大的 Android 系统,还是我们写的 App,本质上都只是一堆躺在内存里的指令集合。ARM CPU(SoC)能执行的,永远只有极其基础的动作:比如跳转指令、读取某个内存地址的内容、写寄存器、执行寄存器的基础算术操作等(也就是汇编语言对应的指令)。所谓的 Android 软件生态,全是这些基础指令的排列组合。
2. 启动接力:从硬连线到庞然大物
按下电源键的那一刻,ARM 芯片(仅主核苏醒,其他核心处于休眠)会被硬件电路强制固定去读取一段固化在 SoC 内部的代码(BootROM)。这段代码执行后,会将手机闪存(UFS)上的 BootLoader 引导程序读入内存,并交出执行权。随后,BootLoader 承担起重任,去闪存上把庞大的 Android Linux 内核镜像(boot.img)读入内存,并跳转到内核的入口地址,至此,CPU 的控制权正式交由 Linux 内核接管。(内核随后会拉起 init 进程,进而孵化出 Zygote,最终拉起你的 App)。
3. 内存沙盒:多级页表与按需分页
为了隔离与安全,内存中维护着页表。它记录了虚拟地址对应的物理页,并标注了该页的访问权限。如果 App 尝试越权访问内核内存页,MMU(内存管理单元)硬件会直接拦截并引发段错误。因为手机虚拟内存极其庞大,Android 采用多级页表,其空间是动态按需分配的:只有当你真正访问某个虚拟地址触发缺页中断时,内核才会逐级分配页表项,并在表项中填入真实的物理内存地址,将其标记为有效。
4. 静默与中断:系统的睡眠与唤醒
Linux 内核初始化完毕后,会维护一个中断向量表。此时系统若无任务处理,会执行 WFI (Wait For Interrupt) 指令,让手机 CPU 芯片进入低功耗休眠状态,死等中断。
中断分为硬件中断(如屏幕触摸、网卡数据)和软件中断。一旦信号来临,CPU 会进行优先级判断。如果条件允许,硬件会自动保存现场(将当前寄存器状态压入内核栈),然后去中断向量表中查询执行入口,跳转到内核对应的驱动/处理代码段。执行完毕后,再从内核栈恢复现场,继续之前的工作。
5. 特权跨界:系统调用与降维服从
ARM CPU 内部维护着特权等级(Exception Level,即 EL0~EL3)。你的 App 代码运行在最低的 EL0 级,被硬件物理限制,无法直接访问手机硬件或高权限内存。当 App 需要发网络包、读写文件或进行 Binder 跨进程通信时,必须发起系统调用。
本质上,App 会将请求参数写入 CPU 通用寄存器,然后执行一条 svc(Supervisor Call)软中断指令。CPU 执行到该指令,硬件电路会瞬间将特权级提升至 EL1(内核态),并自动将当前的寄存器现场数据(如 PC 指针、状态寄存器等)压入内核栈保护起来,随后根据异常/中断向量表,跳转到内核对应的系统调用处理代码。 内核进行严格安检后执行请求(比如由 Binder 驱动分发消息),完成后将结果放回寄存器,最后执行返回指令,特权级降回 EL0,控制权交还 App。
💡 架构师的护城河
掌握这套从硬件到软件的物理图谱,是你成为一名优秀 Android 架构师的终极底牌。它能直接将你的排障思维,从“为什么我的 Java 代码崩溃了?”拉升到“是 CPU、OS 还是 Framework 的哪个物理层级出了问题?”
English Version
Introduction
To truly master Android architecture, we must look beyond the APIs and understand the hardware. Here is the microscopic physical blueprint of how an Android device actually works, from the moment you press the power button to your App running on the screen:
1. The Essence: An Ocean of Instructions
Whether it’s the massive Android OS or the App you just wrote, everything is fundamentally just a set of instructions sitting in RAM. The ARM CPU (SoC) can only execute extremely basic physical actions: jump instructions, read from a memory address, write to a register, or perform basic arithmetic. The entire Android software ecosystem is simply a giant permutation and combination of these basic assembly instructions.
2. The Boot Relay: From Hardwired Logic to a Giant OS
The moment you press the power button, only the primary ARM core wakes up (others remain asleep). The hardware circuitry forces it to read a piece of code hardcoded inside the SoC (BootROM). This code loads the BootLoader from the phone's flash storage (UFS) into RAM. The BootLoader then takes on the heavy lifting: it loads the massive Android Linux Kernel image (boot.img) into RAM, jumps to the kernel's entry point, and officially hands over CPU control to the Linux kernel. (The kernel then spawns the init process, which incubates Zygote, eventually bringing your App to life).
3. The Memory Sandbox: Multi-level Page Tables & On-Demand Paging
For isolation and security, the OS maintains page tables. They map virtual addresses to physical memory pages and dictate access permissions. If an App attempts to access kernel memory, the MMU hardware intercepts it immediately, causing a segmentation fault. Because virtual memory is massive, Android uses multi-level page tables with dynamic On-Demand Paging. Only when you access a specific virtual address and trigger a page fault does the kernel step in, allocate the page table entries, bind them to actual physical memory, and mark them as valid.
4. Idle & Interrupts: Sleep and Wake-up
Once the Linux kernel finishes initialization, it maintains an interrupt vector table. If there is no work to do, it executes the WFI (Wait For Interrupt) instruction, putting the CPU into a deep low-power sleep state.
Interrupts are divided into hardware interrupts (screen touches, network data) and software interrupts. When a signal arrives, the CPU evaluates its priority. The hardware automatically saves the execution context (pushing registers onto the kernel stack), consults the interrupt vector table, and jumps to the kernel's corresponding driver/handler code. Once finished, it restores the context and resumes previous work.
5. Privilege Escalation: System Calls & Hardware Compliance
ARM CPUs maintain Exception Levels (EL0 to EL3). Your App runs at the lowest level, EL0, and is physically restricted from directly accessing hardware or high-privilege memory. When an App needs to send network packets, read files, or perform Binder IPC, it must make a System Call.
The App writes parameters to CPU registers and executes an svc soft interrupt instruction. Upon execution, the hardware instantly escalates the privilege level to EL1 (Kernel Mode), automatically pushes the current register context (like the PC pointer) onto the kernel stack, and jumps to the corresponding system call handler via the vector table. The kernel performs strict security checks, executes the request, writes the result back, and drops the privilege level back to EL0, returning control to the App.
💡 The Architect's Moat
Understanding this hardware-to-software physical blueprint is the ultimate moat for an Android architect. It shifts your debugging mindset from "why is my Java code crashing?" to "which physical layer of the CPU, OS, or framework is failing?"