Kernel Hacking on AndroidA quick look into the Android kernel...          Andrea Righi <andrea@betterlinux.com>
Agenda●   Overview●   Android Programming●   Android Power Management●   Q/A                Andrea Righi <andrea@betterlin...
OverviewAndrea Righi <andrea@betterlinux.com>
Introduction●   Android is a software stack for mobile devices,    such as smartphones and tablet computers●   It is devel...
Android OS●   Android consists of a:    ●   kernel (Linux, modified by Google)    ●   userspace libraries and APIs written...
Andrea Righi <andrea@betterlinux.com>
Android: kernel modifications●   binder: IPC (i.e., communication between window manager and client)●   ashmem: shared mem...
Native libraries●   Bionic libc    ●   Custom libc implementation, optimized for        embedded use (optimized for size) ...
Android Programming  Andrea Righi <andrea@betterlinux.com>
Programming model●   Android applications are written in Java    ●   Class libraries are similar to Java SE but not equal ...
Android applications●   Applications are distributed as Android    packages (.apk)●   Everything is needed to run an appli...
Typical directory tree in Android●   / (initrd – ro)    ●   /system (ro)        –   /system/bin        –   /system/etc    ...
Toolchain●   From http://developer.android.com    ●   Android SDK        http://developer.android.com/sdk/index.html    ● ...
Android emulator: goldfish●   The Android emulator runs a virtual CPU that    Google calls Goldfish.●   Goldfish executes ...
Create an Android application# create a new projectandroid create project --package com.develer.hello --activity HelloAndr...
Create a native ARM application#include <stdio.h>int main(int argc, char **argv){     printf(“Hello, world!n”);     return...
Run a custom kernel                  (Android emulator)$ git clone https://android.googlesource.com/kernel/goldfish$ git c...
Android Power Management     Andrea Righi <andrea@betterlinux.com>
Battery life●   The three most important considerations for    mobile applications are:    ●   battery life    ●   battery...
Typical use case●   Most of the time the device is in you pocket,    completely in idle (suspend)●   You pull it ouf of yo...
Android Power Management●   Android will opportunistically enter a full system    suspend state●   This forcibly stops pro...
Battery consumption   Andrea Righi <andrea@betterlinux.com>
CPU states●   Running●   Idle●   Deep sleep●   Suspend●   Powered off                  Andrea Righi <andrea@betterlinux.com>
Background activity●   Example, an application that periodically checks    emails:    ●   The device wakes up    ●   Pulls...
Battery cost example●   Cost during a given hour    ●   Full idle:         –   5mAh    ●   Network stack + CPU:         – ...
Wake locks●   Used by applications to prevent the system    from entering suspend or low-power state●   Driver API    ●   ...
Wake locks●   Custom solution not integrated with the Linux Power    Management (PM) subsystem●   Components make requests...
Wake lock: Java interfacePowerManager pm = (PowerManager)mContext.getSystemService(                      Context.POWER_SER...
Early suspend●   Suspend / resume kernel hooks called when    user-visible sleep states change●   User-space framework wri...
Early suspendstatic struct early_suspend _powersave_early_suspend = {      .suspend = powersave_early_suspend,      .resum...
Android alarm●   Tell the kernel when it should wake-up    ●   hrtimer: when the system is running    ●   RTC: when the sy...
Android alarmstatic struct alarm my_alarm;alarm_init(&my_alarm, ANDROID_ALARM_RTC_WAKEUP, my_alarm_handler);alarm_start_ra...
References●   Official Android developers website:    ●   http://developer.android.com/●   Embedded Linux wiki (Android Po...
Q/A●   Youre very welcome!                 Andrea Righi <andrea@betterlinux.com>
Upcoming SlideShare
Loading in...5
×

Workshop su Android Kernel Hacking

3,390

Published on

Android è un argomento di grande interesse nel mondo dell'informatica ma lavorare sulla piattaforma non è semplice.

Questo intervento avrà un taglio pratico e spiegherà come procurarsi gli strumenti per compilare un modulo kernel su android, come sviluppare un semplice modulo e come caricarlo sul dispositivo. Infine, si presenterà come creare un modulo più complesso usando delle API specifiche del kernel di Android.

I sorgenti del workshop sono reperibili qui:
https://github.com/arighi/mysuspend

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,390
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
93
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Workshop su Android Kernel Hacking

  1. 1. Kernel Hacking on AndroidA quick look into the Android kernel... Andrea Righi <andrea@betterlinux.com>
  2. 2. Agenda● Overview● Android Programming● Android Power Management● Q/A Andrea Righi <andrea@betterlinux.com>
  3. 3. OverviewAndrea Righi <andrea@betterlinux.com>
  4. 4. Introduction● Android is a software stack for mobile devices, such as smartphones and tablet computers● It is developed by the Open Handset Alliance led by Google● Android was listed as the best-selling smartphone platform worldwide in Q4 2010 by Canalys Andrea Righi <andrea@betterlinux.com>
  5. 5. Android OS● Android consists of a: ● kernel (Linux, modified by Google) ● userspace libraries and APIs written in C ● an application framework (Dalvik Java Virtual Machine) ● application software running inside the application framework Andrea Righi <andrea@betterlinux.com>
  6. 6. Andrea Righi <andrea@betterlinux.com>
  7. 7. Android: kernel modifications● binder: IPC (i.e., communication between window manager and client)● ashmem: shared memory (process cache)● pmem: physically contiguous memory allocator● logger: custom system logging facility● wakelock: power management (hold machine awake on a per-event basis until wakelock is released)● early suspend: suspend/resume hooks to be called when user-visible sleep state change● lowmemorykiller (aka Viking Killer): custom OOM killer tunable from userspace (memory thresholds and oom_adjust thresholds)● alarm timers: RTC-based wakeup● timed gpio: driver that allows userspace programs to access and manipulate gpio pins and restore their state automatically after a specified timeout● ram console: store kernel log to a persistent RAM area, so that after a kernel panic it can be viewed via /proc/last_kmsg● USB gadget driver (ADB)● ... Andrea Righi <andrea@betterlinux.com>
  8. 8. Native libraries● Bionic libc ● Custom libc implementation, optimized for embedded use (optimized for size) ● Dont support certain POSIX features ● Not compatible with GNU Libc (glibc) ● Native code must linked against bionc libc (or statically linked) Andrea Righi <andrea@betterlinux.com>
  9. 9. Android Programming Andrea Righi <andrea@betterlinux.com>
  10. 10. Programming model● Android applications are written in Java ● Class libraries are similar to Java SE but not equal ● Dalvik VM is a register-based architecture that dynamically translates from Java byte code into native architecture instructions● However ● Its still Linux, so we can build and run native Linux applications ● Use the JNI bridge to bind C functions into the Java code Andrea Righi <andrea@betterlinux.com>
  11. 11. Android applications● Applications are distributed as Android packages (.apk)● Everything is needed to run an application is bundled inside the .apk● Basically an “enhanced” ZIP file Andrea Righi <andrea@betterlinux.com>
  12. 12. Typical directory tree in Android● / (initrd – ro) ● /system (ro) – /system/bin – /system/etc – /system/lib – /system/usr ● /data (applications data – rw) ● /cache (applications cache – rw) ● /mnt/sdcard (removable storage – rw) Andrea Righi <andrea@betterlinux.com>
  13. 13. Toolchain● From http://developer.android.com ● Android SDK http://developer.android.com/sdk/index.html ● Android NDK http://developer.android.com/sdk/ndk/index.html● From http://www.codesourcery.com ● ARM toolchain to recompile the Linux kernel (get the one targeted at “EABI”) https://sourcery.mentor.com/sgpp/lite/arm/portal/subscription3053 ● ARM toolchain for native userspace applications (get the one targeted at "GNU/Linux") https://sourcery.mentor.com/sgpp/lite/arm/portal/subscription3057 Andrea Righi <andrea@betterlinux.com>
  14. 14. Android emulator: goldfish● The Android emulator runs a virtual CPU that Google calls Goldfish.● Goldfish executes ARM926T instructions and has hooks for input and output -- such as reading key presses from or displaying video output in the emulator● These interfaces are implemented in files specific tothe Goldfish emulator and will not be compiled into a kernel that runs on real devices. Andrea Righi <andrea@betterlinux.com>
  15. 15. Create an Android application# create a new projectandroid create project --package com.develer.hello --activity HelloAndroid --target 2 --path ./helloandroid# build in release modeant release# sign a apkjarsigner -verbose -keystore ~/certs/android.keystore ./bin/HelloAndroid-unsigned.apk arighi# align the apkzipalign -v 4 ./bin/HelloAndroid-unsigned.apk ./bin/HelloAndroid.apk# installadb install ./bin/HelloAndroid.apk# uninstalladb uninstall HelloAndroid Andrea Righi <andrea@betterlinux.com>
  16. 16. Create a native ARM application#include <stdio.h>int main(int argc, char **argv){ printf(“Hello, world!n”); return 0;}Build & run: $ arm-none-linux-gnueabi-gcc -static -o hello hello.c $ adb push hello /data/local/tmp/hello $ adb shell chmod 777 /data/local/tmp/hello $ adb shell /data/local/tmp/hello Andrea Righi <andrea@betterlinux.com>
  17. 17. Run a custom kernel (Android emulator)$ git clone https://android.googlesource.com/kernel/goldfish$ git checkout android-goldfish-2.6.29$ export PATH=/opt/toolchain/arm-eabi-2011.03-42/bin:$PATH$ make ARCH=arm CROSS_COMPILE=arm-none-eabi- goldfish_defconfig$ make ARCH=arm CROSS_COMPILE=arm-none-eabi-$ emulator -kernel arch/arm/boot/zImage -avd <AVD_NAME> Andrea Righi <andrea@betterlinux.com>
  18. 18. Android Power Management Andrea Righi <andrea@betterlinux.com>
  19. 19. Battery life● The three most important considerations for mobile applications are: ● battery life ● battery life ● and battery life● After all, if the battery is dead, no one can use your application... Andrea Righi <andrea@betterlinux.com>
  20. 20. Typical use case● Most of the time the device is in you pocket, completely in idle (suspend)● You pull it ouf of your pocket maybe once, twice in a hour ● Check the email, facebook, twitter, etc. for short “bursts” of time Andrea Righi <andrea@betterlinux.com>
  21. 21. Android Power Management● Android will opportunistically enter a full system suspend state● This forcibly stops processes from running● Your battery last longer and your phone doesnt melt in your pocket Andrea Righi <andrea@betterlinux.com>
  22. 22. Battery consumption Andrea Righi <andrea@betterlinux.com>
  23. 23. CPU states● Running● Idle● Deep sleep● Suspend● Powered off Andrea Righi <andrea@betterlinux.com>
  24. 24. Background activity● Example, an application that periodically checks emails: ● The device wakes up ● Pulls up the radio stack ● Does a DNS request ● Does a network connection ● Pulling down some data ● Parsing data on the fly (cpu) ● Takes a few seconds to the device to settle down and fall to sleep mode again Andrea Righi <andrea@betterlinux.com>
  25. 25. Battery cost example● Cost during a given hour ● Full idle: – 5mAh ● Network stack + CPU: – 350 mAh ● Background app (i.e., network update) – 6 times / h x 8 sec x 350 mAh => 4.67mAh● Even a simple innocent background app can drain the battery life quickly Andrea Righi <andrea@betterlinux.com>
  26. 26. Wake locks● Used by applications to prevent the system from entering suspend or low-power state● Driver API ● struct wake_lock name ● wake_lock_init()/wake_lock()/wake_unlock()● Userspace API ● Write lockname (and optionally lockname timeout) to /sys/power/wake_lock ● Write lockname to /sys/power/wake_unlock Andrea Righi <andrea@betterlinux.com>
  27. 27. Wake locks● Custom solution not integrated with the Linux Power Management (PM) subsystem● Components make requests to keep the power on through “wake locks” (use wake locks carefully!!!)● Support different types of wake locks: ● FULL_WAKE_LOCK: cpu on, keyboard on, screen at full brightness ● PARTIAL_WAKE_LOCK: cpu on ● SCREEN_DIM_WAKE_LOCK: screen on (but may be dimmed), keyboard backlights allowed to go off ● ... Andrea Righi <andrea@betterlinux.com>
  28. 28. Wake lock: Java interfacePowerManager pm = (PowerManager)mContext.getSystemService( Context.POWER_SERVICE);PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);wl.acquire();// ...wl.release(); Andrea Righi <andrea@betterlinux.com>
  29. 29. Early suspend● Suspend / resume kernel hooks called when user-visible sleep states change● User-space framework writes the state to /sys/power/request_state Andrea Righi <andrea@betterlinux.com>
  30. 30. Early suspendstatic struct early_suspend _powersave_early_suspend = { .suspend = powersave_early_suspend, .resume = powersave_late_resume,};register_early_suspend(&_powersave_early_suspend);unregister_early_suspend(&_powersave_early_suspend); Andrea Righi <andrea@betterlinux.com>
  31. 31. Android alarm● Tell the kernel when it should wake-up ● hrtimer: when the system is running ● RTC: when the system is suspended Andrea Righi <andrea@betterlinux.com>
  32. 32. Android alarmstatic struct alarm my_alarm;alarm_init(&my_alarm, ANDROID_ALARM_RTC_WAKEUP, my_alarm_handler);alarm_start_range(&my_alarm, expire_start, expire_end);alarm_cancel(&my_alarm); Andrea Righi <andrea@betterlinux.com>
  33. 33. References● Official Android developers website: ● http://developer.android.com/● Embedded Linux wiki (Android Portal): ● http://elinux.org/Android_Portal● The xda-developers forum: ● http://www.xda-developers.com/● mysuspend source code available at github.com: ● https://github.com/arighi/mysuspend Andrea Righi <andrea@betterlinux.com>
  34. 34. Q/A● Youre very welcome! Andrea Righi <andrea@betterlinux.com>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×