A Look through the Android Stack



Similar documents
Mobile Devices - An Introduction to the Android Operating Environment. Design, Architecture, and Performance Implications

Android Operating System:

Android Fundamentals 1

Research and Design of Universal and Open Software Development Platform for Digital Home

Mobile Devices - An Introduction to the Android Operating Environment. Design, Architecture, and Performance Implications

Android Operating System

Programming the Android Platform. Logistics

Example of Standard API

ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi

Android Security (and Not) Internals

Lecture 1 Introduction to Android

Android Architecture For Beginners

Overview. The Android operating system is like a cake consisting of various layers.

An Introduction to Android Application Development. Serdar Akın, Haluk Tüfekçi

Overview of CS 282 & Android

Analysis of advanced issues in mobile security in android operating system

Application of Android OS as Real-time Control Platform**

Chapter 14 Virtual Machines

Lecture 17: Mobile Computing Platforms: Android. Mythili Vutukuru CS 653 Spring 2014 March 24, Monday

A Short Introduction to Android

A Survey on Android vs. Linux

ELEC 377. Operating Systems. Week 1 Class 3

Real Time Programming: Concepts

ODROID Multithreading in Android

Android Binder. Android Interprocess Communication. Thorsten Schreiber First Advisor: Juraj Somorovsky Second Advisor: Daniel Bußmeyer

RoverPal - A Mobile Payment Application

Introduction to Android Window System

Operating System Components and Services

Introduction to Android

AppScope: Application Energy Metering Framework for Android Smartphones using Kernel Activity Monitoring

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

CHAPTER 15: Operating Systems: An Overview

The Case for SE Android. Stephen Smalley Trust Mechanisms (R2X) National Security Agency

Performance Analysis of Android Platform

Lecture 25 Symbian OS

Introduction to Android

Understanding Android s Security Framework

Deep Inside Android. OpenExpo Zurich September 25 th, Gilles Printemps - Senior Architect. Copyright 2007 Esmertec AG.

ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi

Android Programming and Security

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Red Hat Linux Internals

An Introduction to Android

Lecture 5. User-Mode Linux. Jeff Dike. November 7, Operating Systems Practical. OSP Lecture 5, UML 1/33

Android Architecture. Alexandra Harrison & Jake Saxton

An Introduction to Android. Huang Xuguang Database Lab. Inha University

Chapter 6, The Operating System Machine Level

Технологии Java. Android: Введение. Кузнецов Андрей Николаевич. Санкт-Петербургский Государственный Политехнический Университет

System Structures. Services Interface Structure

Embedded Systems. 6. Real-Time Operating Systems

Linux Driver Devices. Why, When, Which, How?

Using Chroot to Bring Linux Applications to Android

Hacking your Droid ADITYA GUPTA

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Linux Kernel Architecture

Security Enhanced (SE) Android: Bringing Flexible MAC to Android

SYSTEM ecos Embedded Configurable Operating System

Virtual Private Systems for FreeBSD

Chapter 2 System Structures

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

Instrumentation for Linux Event Log Analysis

How To Write A Windows Operating System (Windows) (For Linux) (Windows 2) (Programming) (Operating System) (Permanent) (Powerbook) (Unix) (Amd64) (Win2) (X

Virtual Hosting & Virtual Machines

Android Basics. Xin Yang

Architecture (SOSP 2011) 11/11/2011 Minsung Jang

A Comparative Study on Vega-HTTP & Popular Open-source Web-servers

The Android Platform

ANDROID OPERATING SYSTEM

Mobile Operating Systems. Week I

CELLS A Virtual Mobile Smartphone Architecture

Operating Systems 4 th Class

Mobility Introduction Android. Duration 16 Working days Start Date 1 st Oct 2013

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

Chapter 3 Operating-System Structures

Creating and Using Databases for Android Applications

Android in Action. Second Edition. Revised Edition of Unlocking Android MANNING. (74 w. long.) W. FRANK ABLESON CHRIS KING ROBI SEN.

REAL TIME OPERATING SYSTEMS. Lesson-10:

Embedded Linux Platform Developer

Cisco Application-Centric Infrastructure (ACI) and Linux Containers

Operating System Organization. Purpose of an OS

Frysk The Systems Monitoring and Debugging Tool. Andrew Cagney

Intro to Docker and Containers

Computer Science 4302 Operating Systems. Student Learning Outcomes

Survey of Filesystems for Embedded Linux. Presented by Gene Sally CELF

Introduction to Android. CSG250 Wireless Networks Fall, 2008

1 Organization of Operating Systems

General Introduction

Android Application Development - Exam Sample

Praktikum Entwicklung Mediensysteme (für Master)

ITG Software Engineering

Java and Real Time Storage Applications

theguard! ApplicationManager System Windows Data Collector

Chapter 13 Embedded Operating Systems

ANDROID BASED MOBILE APPLICATION DEVELOPMENT and its SECURITY

Docker : devops, shared registries, HPC and emerging use cases. François Moreews & Olivier Sallou

Synthesis for Developing Apps on Mobile Platforms

Transcription:

A Look through the Android Stack A Look through the Android Stack Free Electrons Maxime Ripard Free Electrons Embedded Linux Developers c Copyright 2004-2012, Free Electrons. Creative Commons BY-SA 3.0 license. Latest update: July 12, 2012. Document updates and sources: Corrections, suggestions, contributions and translations are welcome! Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 1/36

Maxime Ripard Embedded Linux engineer and trainer at Free Electrons since 2011 Embedded Linux development: kernel and driver development, system integration, boot time and power consumption optimization, etc. Embedded Linux, Embedded Android and driver development training, with materials freely available under a Creative Commons license. http://free-electrons.com Contributor to Buildroot, a simple open-source embedded build system Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 2/36

What is Android? Mobile OS bought by Google in 2005 Opened up to the world through the Android Open Source Project the 21 October 2008 with the release of Android 1.0 (mostly) under the Apache 2.0 License Has deep foundation into the open-source/free software world, even if a huge part has been written from scratch: uses Linux, WebKit, SQLite, OpenSSL, etc. Is now one of the most successful mobile OS in the world Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 3/36

Architecture Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 4/36

Booting Up... Booting Up... Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 5/36

Booting Up... The Linux Kernel Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 6/36

The Linux Kernel Used as the foundation of the Android system Numerous additions from the stock Linux, including new IPC (Inter-Process Communication) mechanisms, alternative power management mechanism, new drivers and various additions across the kernel These changes are beginning to go into the staging/ area of the kernel, as of 3.4, after being a complete fork for a long time Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 7/36

Wakelocks Android s answer to these power management constraints is wakelocks One of the most famous Android changes, because of the flame wars it spawned The main idea is instead of letting the user decide when the devices need to go to sleep, the kernel is set to suspend as soon and as often as possible. In the same time, Android allows applications and kernel drivers to voluntarily prevent the system from going to suspend, keeping it awake (thus the name wakelock) This implies to write the applications and drivers to use the wakelock API. Applications do so through the abstraction provided by the API Drivers must do it themselves, which prevents to directly submit them to the vanilla kernel Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 8/36

Binder RPC/IPC mechanism Takes its roots from BeOS and the OpenBinder project, which some of the current Android engineers worked on Adds remote object invocation capabilities to the Linux Kernel One of the very basic functionalities of Android. Without it, Android cannot work. Every call to the system servers go through Binder, just like every communication between applications, and even communication between the components of a single application. Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 9/36

Binder Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 10/36

Ashmem ndk/docs/system/libc/sysv-ipc.html illustrates all the love Android developers have for the shared memory mechanism in Linux Ashmem is the response to the flaws exposed Notable differences are: Reference counting so that the kernel can reclaim resources which are no longer in use There is also a mechanism in place to allow the kernel to shrink shared memory regions when the system is under memory pressure. The standard use of Ashmem in Android is that a process opens a shared memory region and share the obtained file descriptor through Binder. Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 11/36

Low Memory Killer When the system goes out of memory, Linux throws the OOM Killer to cleanup memory greedy processes However, this behaviour is not predictable at all, and can kill very important components of a phone (Telephony stack, Graphic subsystem, etc) instead of low priority processes (Angry Birds) The main idea is to have another process killer, that kicks in before the OOM Killer and takes into account the time since the application was last used and the priority of the component for the system It uses various thresholds, so that it first notifies applications so that they can save their state, then begins to kill non-critical background processes, and then the foreground applications As it is run to free memory before the OOM Killer, the latter will never be run, as the system will never run out of memory Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 12/36

Various additions Android also has a lot of minor features added to the Linux kernel: RAM Console, a RAM-based console that survives a reboot to hold kernel logs pmem, a physically contiguous memory allocator, written specifically for the HTC G1, to allocate heaps used for 2D hardware acceleration Alarm Timers, which combines Real-Time Clock with the high-resolution timers to wake up a process even when the device is in suspend. Paranoid Network, which restricts access to the network interfaces only to a given GID ADB YAFFS2 Timed GPIOs Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 13/36

Booting Up... Init Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 14/36

Android s init Once again, Google has developed his own instead of relying on an existing one. However, it has some interesting features, as it can also be seen as a daemon on the system it manages device hotplugging, with basic permissions rules for device files, and actions at device plugging and unplugging: set particular permissions to the device files, mount the filesystems, start a daemon, etc. it monitors the services it started, so that if they crash, it can restart them, reboot the device, etc. it monitors system properties so that you can take actions when a particular one is modified Basically, it is quite comparable to sysvinit + udev Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 15/36

Android startup Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 16/36

Booting Up... Dalvik and Zygote Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 17/36

Dalvik Dalvik is the virtual machine, executing Android applications It is an interpreter written in C/C++, and is designed to be portable, lightweight and run well on mobile devices It is also designed to allow several instances of it to be run at the same time while consuming as little memory as possible Two execution modes portable: the interpreter is written in C, quite slow, but should work on all platforms fast: Uses the mterp mechanism, to define routines either in assembly or in C optimized for a specific platform. Instruction dispatching is also done by computing the handler address from the opcode number It uses the Apache Harmony Java framework for its core libraries Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 18/36

Zygote Dalvik is started by Zygote frameworks/base/cmds/app_process At boot, Zygote is started by init, it then Initializes a virtual machine in its address space Loads all the basic Java classes in memory Starts the system server Waits for connections on a UNIX socket When a new application should be started: Android connects to Zygote through the socket to request the start of a new application Zygote forks The child process loads the new application and start executing it Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 19/36

Booting Up... Service Manager and Various Services Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 20/36

System Server boot Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 21/36

The first step: system_server.c Located in frameworks/base/cmds/system_server Started by Zygote through the SystemServer Starts all the various native services: SurfaceFlinger SensorService AudioFlinger MediaPlayerService CameraService AudioPolicyService It then calls back the SystemServer object s init2 function to go on with the initialization Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 22/36

Java Services Initialization Located in frameworks/base/services/java/com/ android/server/systemserver.java Starts all the different Java services in a different thread by registering them into the Service Manager PowerManager, ActivityManager (also handles the ContentProviders), PackageManager, BatteryService, LightsService, VibratorService, AlarmManager, WindowManager, BluetoothService, DevicePolicyManager, StatusBarManager, InputMethodManager, ConnectivityService, MountService, NotificationManager, LocationManager, AudioService,... If you wish to add a new system service, you will need to add it to one of these two parts to register it at boot time Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 23/36

Communication within the system Communication within the system Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 24/36

IPCs On modern systems, each process has its own address space, allowing to isolate data This allows for better stability and security: only a given process can access its address space. If another process tries to access it, the kernel will detect it and kill this process. However, interactions between processes are sometimes needed, that s what IPCs are for. On classic Linux systems, several IPC mechanisms are used: Signals Semaphores Sockets Message queues Pipes Shared memory Android, however, uses mostly: Binder Ashmem and Sockets Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 25/36

Binder 1/2 Uses shared memory for high performance Uses reference counting to garbage collect objects no longer in use Data are sent through parcels, which is some kind of serialization Used across the whole system, e.g., clients connect to the window manager through Binder, which in turn connects to SurfaceFlinger using Binder Each object has an identity, which does not change, even if you pass it to other processes. Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 26/36

Binder 2/2 This is useful if you want to separate components in distinct processes, or to manage several components of a single process (i.e. Activity s Windows). Object identity is also used for security. Some token passed correspond to specific permissions. Another security model to enforce permissions is for every transaction to check on the calling UID. Binder also supports one-way and two-way messages Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 27/36

Binder Mechanism Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 28/36

Binder Implementation 1/2 Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 29/36

Binder Implementation 2/2 Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 30/36

Android Interface Definition Language (AIDL) Very similar to any other Interface Definition Language you might have encountered Describes a programming interface for the client and the server to communicate using IPCs Looks a lot like Java interfaces. Several types are already defined, however, and you can t extend this like what you can do in Java: All Java primitive types (int, long, boolean, etc.) String CharSequence Parcelable List of one of the previous types Map Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 31/36

Intents Intents are a high-level use of Binder They describe the intention to do something They are used extensively across Android Activities, Services and BroadcastReceivers are started using intents Two types of intents: explicit The developer designates the target by its name implicit There is no explicit target for the Intent. The system will find the best target for the Intent by itself, possibly asking the user what to do if there are several matches Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 32/36

Wrap-up Wrap-up Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 33/36

Interactions in the System Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 34/36

Example: VibratorService Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 35/36

Questions? Our training slides are available on http://free-electrons.com/doc/training/android/ Maxime Ripard maxime.ripard@free-electrons.com PDF and sources of these slides will be available on http://free-electrons.com/pub/conferences/2012/lsm/ Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 36/36