OS Perspective on Android
Total Page:16
File Type:pdf, Size:1020Kb
OS Perspective on Android David E. Culler CS162 – Operating Systems and Systems Programming http://cs162.eecs.berkeley.edu/ Lecture 27 December 5, 2019 cs162 Recall: What is an operating system? • Special layer oF soFtware that provides application soFtware access to hardware resources – Convenient abstraction of complex hardware devices – Protected access to Shared resources – Security and Authentication – Communication amongst logical entities appln appln appln OS Hardware 12/5/19 UCB CS162 Fa19 L27 2 In OS news this term … 12/5/19 UCB CS162 Fa19 L27 3 State oF things • Pixel 4 shipped in Oct 2019 with Linux kernel 4.14, an LTS release from November 2017 and will be stuck there – “Thanks to kernel updates never happening, this means every new release of Android usually has to support the last three years of LTS (Long Term Support) kernel releases (the minimum for Android 10 is 4.9, a 2016 release). – it is still supporting kernel 3.18, which is five years old now. • Linux Kernel 5.4 mainline 11/25/2019 12/5/19 UCB CS162 Fa19 L27 4 But, what do you think ? • What direction will the Future of Linux take? • We have seen so many of the enhancements driven by the needs of the cloud – Proportional share scheduling – Cgroups / Containers – KVM – paravirtualization For virtual machines – Concurrency support – Journaling file system • Versus single-user small form factor android experience • Will software engineering aspects of THE most common platForm dictate? 12/5/19 UCB CS162 Fa19 L27 5 The System “stack” you now know Application runtime User libc subsystems process ctrl syscall handlers scheduler mem mgmt syscall tbl intrpt handlers Kernel file sys intr tbl networking Page Tables user / grp Drivers regs Processor Memory Devices Hardware • /dev 12/5/19 UCB CS162 Fa19 L27 6 What’s in Android ? • Tiers of the Android system architecture • Linux kernel is the part at the bottom – How is Linux used? – How is it extended for this setting? – What matters in the OS? • What OS Functionality is outside the kernel? • How do the demands on it difFer? https://source.android.com/devices/architecture 12/5/19 UCB CS162 Fa19 L27 7 A somewhat diFFerent picture • Kernel provides process &mem mgmt., File system, & Drivers – Does not run Linux binaries – No libc – Syscalls ? • Single “user” => UID per App – In security sandbox + VM • DiFFerent user navigation experience – Threaded thru Apps • “Binder” IPC is the key 12/5/19 UCB CS162 Fa19 L27 8 THE User (you) vs UID • Underlying Linux is multi-user • Each Android App is assigned a distinct Linux UID – Permissions per App – By default, only the App can access its files (User permissions) • Each App runs as its own Linux process* – Android starts the process whenever any of the App’s components need to be executed – Not just main() or when you click on its icon, but … • Each process has its own VM – Like JVM, not “OS virtual machine”. Bytecode interpreter. – Dalvik VM => Android Run Time (ART) * Can arrange for multiple Apps to share UID, but they also run as one process, share the same VM. Must be signed with same certificate 12/5/19 UCB CS162 Fa19 L27 9 The Android Mantra • “The mobile-app experience diFFers From its desktop counterpart in that a user's interaction with the app doesn't always begin in the same place. Instead, the user journey often begins non-deterministically.” • The user experience threads through various apps, each potentially in direct interaction along the way – taking over the entire small screen • Apps build on the Functionality in others • A more cooperative ecosystem … hmm 12/5/19 UCB CS162 Fa19 L27 10 Components oF Android App User Interactions Activities APP Services Broadcast Receivers Providers Content System • Not just exec main() and syscall … • Lots of entry points (call backs) • Process started “whenever needed” 12/5/19 UCB CS162 Fa19 L27 11 App Components / Entry Points • Activities – Entry point for interacting with the User. – Represents a single screen with a user interface • Services – Run in the background. No user interface • Content Providers – Entry point for publishing named data items, identified by URI scheme – Manages shared App data that can be stored in file system, SQLite, on the Web, or on any accessible persistent storage • Broadcast Receivers – Entry point for system events. Outside regular App flow 12/5/19 UCB CS162 Fa19 L27 12 “The Operating System ?” • The Syscalls Recall: A Kind of Narrow Waist • The processor/mem Word ProcessinG Compilers Web Browsers resources delivered to Email Web Servers processes and threads via Databases Application / Service Portable OS Library OS scheduling User System Call Interface System • The hardware resources Portable OS Kernel supporting high level Software Platform support, Device Drivers Hardware x86 PowerPC ARM abstractions via drivers, PCI file systems, etc. Ethernet (10/100/1000) 802.11 a/b/G/n SCSI IDE Graphics • The system libraries (libc) 9/12/19 cs162 fa19 L5 23 • The system utility apps – bash, ps, top, … • The convention For utilizing file structure – /etc, /proc, /bin, … 12/5/19 UCB CS162 Fa19 L27 13 Extending the OS w/o new syscalls ? Application runtime open /dev/subsys read, write, ioctl on fd User libc subsystems process ctrl syscall handlers scheduler mem mgmt syscall tbl intrpt handlers Kernel file sys intr tbl networking Page Tables user / grp Drivers regs Processor Memory Devices Hardware • Drivers - add modules to kernel that communicate with user process thru file descriptor ops 12/5/19 UCB CS162 Fa19 L27 14 Closed Hardware • Primarily ARM, ARM64 • x86 & x86_64 – but only the microprocessor, not the x86 peripherals that intrinsic to PCs, Laptops, and Servers • Audio, Camera, Graphics, WiFi, GPS, Flash, Power management • No paging, no HDDs, no external busses, … 12/5/19 UCB CS162 Fa19 L27 15 The Linux Kernel – Major Changes • Binder – Android SpeciFic IPC / RPC mechanism – THE key to the entire application Framework • ashmem - Anonymous (or Android) Shared Memory – driver-based augment mmap (replace shmat) for small memory & extensive process sharing • PMEM – process memory allocator – Large physically contiguous memory blocks mapped into user space • logger – system logging Facility • wakelocks – User-controlled means of controlling whether system enters low-power states and what wakes it up • Out-of-memory handling – Kill processes under low memory conditions • Paranoid networking – restrict I/O to certain proc’s • Alarm timers, Timed gpio from user space • yafFs2 Flash File system, … https://elinux.org/Android_Kernel_Features Android Systems Programming 12/5/19 UCB CS162 Fa19 L27 16 ashmem (“/dev/ashmem”, O_RDWR) Application open ioctl on fd to set name and size runtime mmap on fd to establish region User ??? subsystems process ctrl syscall handlers scheduler mem mgmt syscall tbl intrpt handlers Kernel file sys intr tbl networking Page Tables user / grp Drivers regs Processor Memory Devices Hardware • Named regions known independent oF Fork/exec • ReFerence counted, clean up, low mem reclaim 12/5/19 UCB CS162 Fa19 L27 17 Bionic – the glibc replacement • Design philosophy – KEEP IT REALLY SIMPLE – Lightweight wrapper around kernel facilities • BSD libc + Linux-speciFic bits (w/o GPL) – Threads, process, signals, and bit more • pthread bundled in, not separate – Based on futexes – very short code paths in common case – Only implements simple, most useful parts » No process-shared mutex or CV, read/write locks,, pthread_cancel, … » Limited thread-local storage – pthread_mutex_t and pthread_cond_t are only 4 bytes • No multicore support • No sysV IPC, semaphores, shared memory segments, message queues, … 12/5/19 UCB CS162 Fa19 L27 18 Aside: Linux FUTEX • Kernel does not provide lock/unlock syscall • Locks are implemented as eFFicient (eg. test-and- test-and-set) spin-locks at user level using atomic hardware primitives – Common case: grab an uncontended lock – make it fast – Restructure app if it is not common (e.g., ticket locks) • But don’t spin long holding the CPU when other thread is holding the lock – Syscall traps to the kernel until a certain condition becomes true, i.e. a change occurs that might free the lock • FUTEX is not exposed in libc, it is used within pthread implementation • Three states for lock implementation – Free, busy-no-waiters, busy-waiters (why ?) 12/5/19 UCB CS162 Fa19 L27 19 FUTEX cont #include <linux/futex.h> #include <sys/time.h> int futex(int *uaddr, int futex_op, int val, const struct timespec *timeout ); • uaddr points to a 32-bit value in user space – placed in region of shared memory, w/I process or created using mmap(2) or shmat(2) across processes • futex_op – FUTEX_WAIT – if val == *uadder sleep till FUTEX_WAIT » Atomic check that condition still holds – FUTEX_WAKE – wake up at most val waiting threads – FUTEX_FD, FUTEX_WAKE_OP, FUTEX_CMP_REQUEUE • timeout – ptr to a timespec structure that specifies a timeout for the op 12/5/19 UCB CS162 Fa19 L27 20 Adjusting the OS/User contract Application runtime User libc Lock Object subsystems process ctrl syscall handlers scheduler mem mgmt syscall tbl intrpt handlers Kernel file sys intr tbl networking Page Tables user / grp Drivers regs Processor Memory Devices Hardware • User level deals with locks & synch • OS mantains thread waiters list & schedules 12/5/19 UCB CS162 Fa19 L27 21 Android-SpeciFic Features in its Linux • System Properties: Key/Value store shared by all processes – Like windows “registry” – Alternate to /proc, /etc . • User/Group management – No /etc/passwd or /etc/groups – groups used