android internals 04 - “androdized” kernel, bionic, logging subsystem (rev_1.1)
DESCRIPTION
Course: Android Internals Lecture 4: “Androdized” kernel, Bionic, Logging subsystemTRANSCRIPT
Android internalsEgor ElizarovSPbSU 2012
Egor Elizarov SPbSU 20122
Legal info
Android internals by Egor Elizarov is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
You are free to – copy, distribute, display, and perform the work
– make derivative works Under the following conditions
– Attribution. You must give the original author credit
– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one
All pictures and trademarks are the property of their respective owners. Use of these trademarks and pictures is subject to owners permissions.
Corrections, suggestions, contributions and translations are welcome!
Egor Elizarov SPbSU 20123
Lecture 4
“Androidized” kernel,
Bionic C library,
Logging system
yegor.yelizarov(at)gmail.com
http://vk.com/android_internalsRev: 1.1Last update: 05/30/2012
Egor Elizarov SPbSU 20124
Previous time
Makefile structure
Android.mk structure
Build system stages
Steps to add new module
Emulator; Qemu; KVM; ADB
Target file system structure
Egor Elizarov SPbSU 20125
Kernel types
Monolithic kernels
Microkernels
Hybrid kernels
Nanokernels
Exokernels
Egor Elizarov SPbSU 20126
Linux kernel
First release 5 October 1991
Vanilla – repository of Linus Torvalds
Monolithic kernel with loadable modules support
Languages: C, ASM
Egor Elizarov SPbSU 20127
Linux drivers
Code in drivers/ directory
Can be build-in or modules (*.ko)
Devices usually have corresponding element in /dev directory
Network devices doesn't have /dev/* elements
Egor Elizarov SPbSU 20128
“Androidized” kernel
Based on Vanilla
Not in mainline until 3.x kernels
Usually board specific
Have android features
Egor Elizarov SPbSU 20129
System calls
Egor Elizarov SPbSU 201210
SysFS & DevFS
Egor Elizarov SPbSU 201211
Android / kernel interaction
System calls (Ex: Bionic routines)
SysFS (Ex: Power management)
DevFS (Ex: Volume deamon)
ProcFS (Ex: Debug system)
Netlink (Ex: Event hub)
Egor Elizarov SPbSU 201212
Wakelocks
Early suspend (Go to suspend as often as possible)
/sys/power/wake_lock, /sys/power/wake_unlock
Could not suspend if wakelock occupied
Wake locks have labels (Ex: PowerManagerService)
Egor Elizarov SPbSU 201213
Low memory killer
Behind Linux Out Of Memory logic
Allow to set different OOM thresholds for different kinds of processes
Set up user-space policies during init
Policies are adjusted in runtime by Activity Manager
kernel/drivers/staging/android/lowmemorykiller.c
Egor Elizarov SPbSU 201214
Binder
RPC/IPC akin to COM
Derived from OpenBinder project
Transmit “parcels” between processes
/dev/binder & /proc/binder
Code: kernel/drivers/staging/android/binder.c
Egor Elizarov SPbSU 201215
Anonymous SHared MEMory
Derived from POSIX SHM part of SysV IPC
Named memory block that is shared between processes
Have reference counting to destroy memory regions
Shrink memory regions if system has lack of memory
Egor Elizarov SPbSU 201216
Alarm
Based on Linux High-Resolution Timer & Real-Time Clock
Allow set system time and adjust alarms
Use RTC to exit suspend and HRT to get precise alarm
/dev/alarm & /dev/rtc/ & /sys/class/rtc
Egor Elizarov SPbSU 201217
Logger
Light weight logger
Use three ring buffer queues inside kernel
No file writes or context switching operations
/dev/log/*
Code: kernel/drivers/staging/android/logger.c
Egor Elizarov SPbSU 201218
More android features
Paranoid networking
Ram console
Physical memory (pmem)
Timed output / timed gpio
etc.
Egor Elizarov SPbSU 201219
Getting & building kernel
git clone https://android.googlesource.com/kernel/goldfish.git
cd goldfish
git checkout -b android-goldfish-2.6.29 origin/android-goldfish-2.6.29
make goldfish_defconfig
make menuconfig
make ARCH=arm CROSS_COMPILE=$ANDROID_ROOT/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- uImage -j4
Egor Elizarov SPbSU 201220
Bionic
Small LibC implementation
Partially POSIX compatible
BSD license
Support for x86, ARM, ARM thumb
Egor Elizarov SPbSU 201221
Bionic features
Not compatible with GNU libc
Fast pthread implementation (based on futexes)
Access to property values
User/group management
Egor Elizarov SPbSU 201222
Not in Bionic
Wide chars
C++ exceptions (limited implementation since NDKr5)
C++ STL
Full pthread
Egor Elizarov SPbSU 201223
Adding syscall
./bionic/libc/README.TXT
Find syscall number in kernel (unistd.h)
Add line to ./bionic/libc/SYSCALLS.TXT
Run bionic/libc/tools/gensyscalls.py
Add wrapper to bionic if needed
Egor Elizarov SPbSU 201224
Logging system
Egor Elizarov SPbSU 201225
Logging in code
Kernel: printk()
Java: Log.*, SLog.*, EventLog.*
Egor Elizarov SPbSU 201226
Logging in code (2)
Native code: LOG* (LOGE)
Define for native code: LOG_TAG, LOG_NDEBUG
Egor Elizarov SPbSU 201227
Obtaining logs
dmesg
logcat
adb logcat
Egor Elizarov SPbSU 201228
Next time
Dalvik VM
Android application live cycle
Source: http://android-developers.blogspot.com/
Egor Elizarov SPbSU 201229
Useful links
http://vk.com/android_internals
K. Yaghmour. Embedded Android. Early Release, O'Reilly, October 2011
http://wiki.osdev.org/Kernels
http://www.kernel.org/
http://cs736-android.pbworks.com/w/page/5834465/ASHMEM
http://elinux.org/Android_Kernel_Features
R. Love. Linux kernel development. 3rd edition, Addison-Westley, June 2010
http://source.android.com/source/building-kernels.html
$ANDROID_ROOT/bionic/libc/README
http://codingrelic.geekhold.com/2008/11/six-million-dollar-libc.html
Egor Elizarov SPbSU 201230
Thanks to
Sergey Matyukevich for review and advices (www.linkedin.com/pub/sergey-matyukevich/31/889/769)
Nikolay F. Fominykh for review and advices
Nikita Shulga for advices and notes (http://www.linkedin.com/pub/nikita-shulga/8/582/287)
Grigory Tolstolytkin for advices and notes (http://www.linkedin.com/pub/grigory-tolstolytkin/2a/b41/74)