Core Components
iSH consists of four major subsystems that work together to provide a Linux environment:Emulator
x86/x86_64 CPU instruction emulation using a threaded code interpreter
Kernel
Linux syscall implementation and translation to iOS/Darwin APIs
Filesystem
Virtual filesystem layer with fake database and real file passthrough
Terminal
TTY/PTY subsystem for terminal emulation and I/O
System Architecture Diagram
Data Flow
Here’s how a typical operation flows through the system:Emulation
The x86 emulator executes the program’s instructions, maintaining CPU state (registers, flags, etc.)
Syscall Trap
When the program makes a Linux syscall (e.g.,
getdents64), it triggers an interrupt:- x86:
int 0x80(INT_SYSCALL) - x86_64:
syscallinstruction (INT_SYSCALL64)
Syscall Translation
The kernel layer translates the Linux syscall to iOS/Darwin equivalents or emulates it directly
Filesystem Access
The filesystem layer handles the request using the appropriate backend (fake DB, real FS, or special FS)
Component Interaction
Emulator ↔ Kernel
The emulator and kernel communicate through the interrupt handling mechanism:kernel/calls.c:490
Kernel ↔ Filesystem
The kernel accesses files through the mount system and filesystem operations:Memory Management
The MMU (Memory Management Unit) and TLB (Translation Lookaside Buffer) provide fast memory access:- MMU: Translates guest virtual addresses to host memory pointers
- TLB: Caches recent translations to avoid repeated lookups
- Page Faults: Handled through the
INT_GPFinterrupt when accessing unmapped memory
Key Design Decisions
Threaded Code Interpreter
Threaded Code Interpreter
Instead of a traditional switch-based interpreter or JIT compilation, iSH uses a threaded code technique where each instruction is compiled to a sequence of function pointers (gadgets). Each gadget ends with a tail call to the next, providing 3-5x speedup over switch dispatch.See Emulation Details for more information.
Hybrid Filesystem
Hybrid Filesystem
iSH uses both a SQLite database (for metadata like permissions) and real filesystem passthrough (for actual data). This allows:
- Proper UNIX permissions on iOS
- Integration with iOS Documents and file sharing
- Special filesystems like /proc and /dev
Syscall Translation
Syscall Translation
Rather than emulating the entire Linux kernel, iSH translates Linux syscalls to their iOS/Darwin equivalents where possible, and emulates them when necessary. This provides better performance and integration with the host OS.See Syscall Handling for implementation details.
Source Code Structure
The name “asbestos” for the gadget system is a reference to the inherent danger of working with low-level assembly code and compiler/linker quirks. As noted in the README: “Long-term exposure to this code may cause loss of sanity.”
Performance Considerations
- TLB Caching: Frequently accessed memory pages are cached in the TLB to avoid MMU translation overhead
- Gadget Arrays: Pre-compiled instruction sequences avoid interpretation overhead
- Direct Syscall Translation: When possible, Linux syscalls map directly to iOS APIs without emulation
- Lazy Evaluation: Many operations are deferred until actually needed (e.g., directory handles)
Next Steps
Emulation
Deep dive into CPU emulation and the gadget system
Syscalls
Learn how Linux syscalls are translated to iOS
Filesystem
Explore the hybrid filesystem architecture