ESSENTIAL Linux for EMBEDDED Developers
Total Page:16
File Type:pdf, Size:1020Kb
ESSENTIAL Linux For EMBEDDED Developers The open source way! An overview of Linux environment Linux Evolution What is Linux? Distributions Linux Virtualization Linux Everywhere The Linux kernel archives http://en.wikipedia.org/wiki/Linux GNU/Linux • Recursive acronym “GNU’s not UNIX” • http://www.gnu.org/ not http://www.gnu.com/ • Richard Stallman (1983) Goal a free Unix • Known for Free Software movement, GNU, Emacs, gcc • Never really released GNU operating system • Linus 1992 open sourced Linux Kernel and named GNU/Linux • Free Software Foundation • http://www.fsf.org/ http://en.wikipedia.org/wiki/GNU Typical Linux SYSTEM Layout System Reserved Rootfs Kernel Bootloader Hardware Linux Distributions •Redhat/fedora/Centos • Most popular, good all around choice • Fedora – community supported • Enterprise Redhat – corporate supported •Debian/Ubuntu/Mint • Completely noncommercial • Massive package selection and easy management • Not as user friendly, but improving •SuSe • IBM Preferred Linux – z/Series Linux of choice Embedded LINUX The Blackfin uClinux Distribution by Analog Devices – a fork of the uClinux distribution for Blackfin processors Embedded Alley - see http://www.embeddedalley.com/ Lineo Solutions uLinux MontaVista Linux - see http://www.mvista.com/products_services.php Pengutronix - see http://www.pengutronix.de/oselas/bsp/index_en.html RidgeRun Linux - see http://www.ridgerun.com/sdk.shtml TimeSys Linux- see http://www.timesys.com/embedded-linux/linuxlink Wind River - see http://www.windriver.com/products/linux/ Digi Embedded Linux for Digi's ARM based modules Virtualization Guest Operating System Virtualization Shared Kernel Virtualization VMware Server and VirtualBox. Linux VServer, Solaris Zones and Containers, and OpenVZ See L4Linux CoLinux MkLinux Kernel Level Virtualization Hypervisor Virtualization (UML) and (KVM). Xen, VMware ESX Server and Microsoft's Hyper-V technology Linux KERNEL TODAY Kernel.org Linux Kernel Information l The Linux kernel version numbers consist of three numbers separated by decimals, such as 2.2.14. The first number is the major version number. The second number is the minor revision number. The third number is the patch level version l There are two stages of kernel releases: “stable” and “development”. Development kernels end in an odd number (2.3, 2.5, …), stable or production kernels end in an even number (2.4, 2.6,3.0). l Once a kernel is deemed stable, it will move from an odd to even second number for release (e.g., from 2.3.51 to 2.4.0). l You can get a good sense of what the future production state of Linux will be by looking at the development kernel. l http://www.kernel.org Application interactions in Linux The Kernel Architecture overview and address space view Application and modes of operations, Kernel and User address Spaces System call, entry and exit points, low level view, strace, parameter passing and kernel implementations Structure: The “Core” Linux Kernel Applications System Libraries (libc) System Call Interface I/O Related Process Related File Systems Scheduler Networking Memory Management Modules Device Drivers IPC Architecture-Dependent Code Hardware Operative Modes • To avoid having applications that constantly crashed, newer OSs were designed with 2 different operative modes: • Kernel Mode: • the machine operates with critical data structure, direct hardware (IN/OUT or memory mapped), direct memory, IRQ, DMA, and so on. • User Mode: • users can run applications. User mode & Kernel mode (32 bit) 0xFFFFFFFF Kernel Address Space 1G Vmlinux 0xC1000000 Init 0xBFFFFFFF | Mdm | Mdm | User Address Space 3G gnome-terminal | Bash | App.exe 0x00000000 Operative Modes • Kernel Mode "prevents" User Mode applications from damaging the system or its features. • Modern microprocessors implement in hardware at least 2 different states. For ex. under Intel, 4 states determine the PL (Privilege Level). It is possible to use 0,1,2,3 states, with 0 used in Kernel Mode. • Linux/Unix OS requires only 2 privilege levels, and we will use such a paradigm as point of reference Switching from User Mode to Kernel Mode-1 • When do we switch? • Once we understand that there are 2 different modes, we have to know when we switch from one to the other. • Typically, there are 2 points of switching: 1. When calling a System Call: 2. When an IRQ (or exception) comes Let’s observe the user/kernel space • use vmstat to grasp recent process context Library call vs System Call in Linux Wrapper and Wrapper based System call , Entry and Exit Points Example of wrapper to a system call flow System calls • The main interface between the kernel and userspace is the set of system calls • About ~300 system calls that provides the main kernel services • File and device operations, networking operations, inter- process communication, process management, memory mapping, timers, threads, synchronization primitives, etc. • This interface is stable over time: only new system calls can be added by the kernel developers • This system call interface is wrapped by the C library, and userspace applications usually never make a system call directly but rather use the corresponding C library function System Calls: read • C example: count = read(fd,buffer,nbyte) • push parameters on stack read library procedure register • call library code X (read) count = read (fd , buffer , nbytes) memory (stack) application buffer • put system call number in register nbytes buffer user space fd • call kernel (TRAP) kernel space • kernel examines system call number • finds requested system call handler • system call execute requested operation handler • return to library and clean up • increase instruction pointer X • remove parameters from stack sys_read() • resume process Kernel Entry and Exit app app Library Code exceptions (error traps) System Call Interface trap 80h trap / system boot interrupt call scheduler table table Kernel device interrupt page faults dialog Devices System Calls vs. Library Calls • man 2 • historical evolution of # of calls • Unix 6e (~50), Solaris 7 (~250) • Linux 2.0 (~160), Linux 2.2 ( ~190), Linux 2.4 (~220) • library calls vs. system call possibilities: • library call never invokes system call • library call sometimes invokes system call • library call always invokes system call • system call not available via library • can invoke system call “directly” via assembly code Cost of Crossing the “Kernel Barrier” • More than a procedure call • Less than a context switch • Costs: • Establishing kernel stack • Validating parameters • Kernel mapped to user address space? Implementation Example: “Hello, world!” .data # section declaration msg: .string "Hello, world!\n" # our dear string len = . - msg # length of our dear string .text # section declaration # we must export the entry point to the ELF linker or .global _start # loader. They conventionally recognize _start as their # entry point. Use ld -e foo to override the default. _start: # write our string to stdout movl $len,%edx # third argument: message length movl $msg,%ecx # second argument: pointer to message to write movl $1,%ebx # first argument: file handle (stdout) movl $4,%eax # system call number (sys_write) int $0x80 # call kernel # and exit movl $0,%ebx # first argument: exit code movl $1,%eax # system call number (sys_exit) int $0x80 # call kernel Linux System Calls (1) Invoked by executing int $0x80. • Programmed exception vector number 128. • CPU switches to kernel mode & executes a kernel function. • Calling process passes syscall number identifying system call in eax register (on Intel processors). • Syscall handler responsible for: • Saving registers on kernel mode stack. • Invoking syscall service routine. • Exiting by calling ret_from_sys_call(). Parameter Passing • On the 32-bit Intel 80x86: • 6 registers are used to store syscall parameters. • eax (syscall number). • ebx, ecx, edx, esi, edi store parameters to syscall service routine, identified by syscall number. Interacting with Modules App_1 App_2 App_N /dev/device_nodes Vmlinux module.ko Basic utility, filter & developer command essentials Understanding Root File System Hierarchy Linux File systems on desktop • Historically Linux had no fs of its own and formally had minix fs running on it • Later adpoted the the second extended file system formally known as ext2fs • Has been enhanced to ext3fs, ext4fs • Now the Linux 3.X is looking to have Btrfs • Btrfs is a new copy on write filesystem for Linux aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration Linux Tree Hierarchy / /root /bin /proc /usr /sbin /dev /opt /home /src /home/ram linux /home/sita / - (unnamed) the actual root /usr – usr utilities and application /sbin – special commands /privileged /root – a home area for root /dev – device node for external devices /bin – utility commands /opt – optional applications /home – usually all you do is here /proc – a bogus fs for Linux kernel /var – spool , log messages etc The Shell • Command interpreter in original Unix • Read command • Perhaps pre-process command • Fork/execute • Return exit status of command • A little history revisited • Bourne Shell (sh) • C chell • Korn shell • Bourne Again shell (bash) Entry level commands • man • date • which • cal • whatis • who • info • w • apropos • id • Write • mesg • bc PiPEs (|) & FILTER PROCESS • ps • grep • nice • sort • sleep • tr • at • cut • nohup • paste • kill • more • ctrl+c • less • ctrl+z • head • fg • tail • bg • nl • top • tee • vmstat • wc FILEs/misc DIR/FILE • chmod