Code Compaction of an Operating System Kernel Haifeng

  • Slides: 22
Download presentation
Code Compaction of an Operating System Kernel Haifeng He, John Trimble, Somu Perianayagam, Saumya

Code Compaction of an Operating System Kernel Haifeng He, John Trimble, Somu Perianayagam, Saumya Debray, Gregory Andrews Computer Science Department

The Problem o o Reduce the memory footprint of Linux kernel on embedded platform

The Problem o o Reduce the memory footprint of Linux kernel on embedded platform Why is this important? n n o Use general-purpose OS in embedded systems Limited amount of memory in embedded systems Goal: n Automatically reduce the size of Linux kernel

The Opportunities General. Purpose OS Embedded Systems Hardware Many devices Small, fixed set of

The Opportunities General. Purpose OS Embedded Systems Hardware Many devices Small, fixed set of devices Software Many applications Small, fixed set of applications System calls Large number Small subset How to utilize these opportunities?

The Options o Hardware configuration n n o Program analysis for code compaction n

The Options o Hardware configuration n n o Program analysis for code compaction n n o Carefully configure the kernel Still not the smallest kernel Find unreachable code Find duplications (functions, instructions) Orthogonal to hardware assisted compression (e. g. , ARM/Thumb)

The Challenges of Kernel Code Compaction o Does not follow conventions of compilergenerated code

The Challenges of Kernel Code Compaction o Does not follow conventions of compilergenerated code ?How to handle kernel code o o o Large amount indirect control flow ?How to find targets of indirect calls Multiple entry points in the kernel Implicit control flow paths n Interrupts

Our Approach o Use binary rewriting J A uniform way to handle C and

Our Approach o Use binary rewriting J A uniform way to handle C and assembly code J Whole program optimizations L Handling kernel binary is not trivial L Less information available (types, pointer aliasing) o Combine source-level analysis A hybrid technique

A Big Picture Source-Level Analysis Binary Rewriting Compile Source Code of Kernel Binary Code

A Big Picture Source-Level Analysis Binary Rewriting Compile Source Code of Kernel Binary Code Of Kernel Pointer Analysis Syscalls required by User Apps Program Call Graph Disassemble Control Flow Graph Compact Kernel Executable Kernel Compaction

Source-Level Analysis o A significant amount of hand-written assembly code in the kernel n

Source-Level Analysis o A significant amount of hand-written assembly code in the kernel n n o Can’t ignore it Interacts with C code Requires pointer analysis for both C code and assembly code n “Lift” the assembly code to source level

Approximate Decompilation o Idea n o The benefit n o Reverse engineer hand-written assembly

Approximate Decompilation o Idea n o The benefit n o Reverse engineer hand-written assembly code back to C Reuse source-level analysis for C The translation can be approximate n Can disregard aspects of assembly code that are irrelevant to the analysis

Approximate Decompilation Source Code of Kernel *. c *. S Appr. decomp. for analysis

Approximate Decompilation Source Code of Kernel *. c *. S Appr. decomp. for analysis X *. c Pointer analysis X *. c. X Program Call Graph o If pointer analysis is flow-insensitive, then instructions like cmp, condition jmp can be ignored

Pointer Analysis o o Tradeoff: precision vs. efficiency Our choice: FA analysis by Zhang

Pointer Analysis o o Tradeoff: precision vs. efficiency Our choice: FA analysis by Zhang et al. n n o Flow-insensitive and context-insensitive Field sensitive Why? n n Efficiency: almost linear Quite precise for identifying the targets of indirect function calls

Identify Reachable Code o o Compute program call graph of Linux kernel based on

Identify Reachable Code o o Compute program call graph of Linux kernel based on FA analysis Identify entry points of Linux kernel n n o startup_32 System calls invoked during kernel boot process System calls required by user applications Interrupt handlers Traverse the program call graph to identify all reachable functions

Improve the Analysis o Observation: During kernel initialization, execution is deterministic n n o

Improve the Analysis o Observation: During kernel initialization, execution is deterministic n n o Only one active thread Only depends on hardware configuration and command line options Initialization code of kernel is “static” n n n If configuration is same, we can safely remove unexecuted initialization code Use. text. init section to identify initialization code Use profiling to identify unexecuted code

Kernel Compaction o Unreachable code elimination n o Whole function abstraction n o Based

Kernel Compaction o Unreachable code elimination n o Whole function abstraction n o Based on reachable code analysis Find identical functions and leave only one instance Duplicate code elimination n Find identical instruction sequences

Experimental Setup o o o Start with a minimally configured kernel Compile the kernel

Experimental Setup o o o Start with a minimally configured kernel Compile the kernel with optimization for code size (gcc –Os) Compile kernel with and without networking n o Benchmarks: n n o Linux 2. 4. 25 and 2. 4. 31 Mi. Bench suite Busybox toolkit (used by Chanet et al. ) Implemented using PLTO

Results: Code Size Reduction Linux 2. 4. 25 Apps. Set All Sys. Calls Busybox

Results: Code Size Reduction Linux 2. 4. 25 Apps. Set All Sys. Calls Busybox Mi. Bench With Networking 12. 2% 18. 0% 19. 3% Without 14. 5% 22. 1% 23. 8%

Effects of Different Optimizations Reduction

Effects of Different Optimizations Reduction

Reduction Effects of Different Call Targets Analysis Kernels

Reduction Effects of Different Call Targets Analysis Kernels

Related Work o o o “System-wide compaction and specialization of the Linux Kernel” (LCTES’

Related Work o o o “System-wide compaction and specialization of the Linux Kernel” (LCTES’ 05) n by Chanet et al. “Kernel optimizations and prefetch with the Spike executable optimizer” (FDDO-4) n by Flower et al. “Survey of code-size reduction methods” n by Beszédes et al.

Conclusions o o o Embedded systems typically run a small fixed set of applications

Conclusions o o o Embedded systems typically run a small fixed set of applications General-purpose OSs contain features that are not needed in every application An automated technique to safely discard unnecessary code n n Source-level analysis + binary rewriting Approximate decompilation

Questions? Project website: http: //www. cs. arizona. edu/solar/

Questions? Project website: http: //www. cs. arizona. edu/solar/

Binary Rewriting of Linux Kernel o o PLTO: a binary rewriting system for Intel

Binary Rewriting of Linux Kernel o o PLTO: a binary rewriting system for Intel x 86 architecture Disassemble kernel code n n n Data embedded within executable section Implicit addressing constraints Unusual instruction sequences Applied a type-based recursive disassemble algorithm Able to disassemble 94% code