SlideShare uma empresa Scribd logo
1 de 56
ECE 424
Embedded Systems
Design
Embedded Linux Overview
Chapter 8
Ning Weng
What’s so special about Linux?
• Multiple choices vs. sole source
• Source code freely available
• Robust and reliable
• Modular, configurable, scalable
• Superb support for networking and Internet
• No runtime licenses
• Large pool of skilled developers
Ning Weng ECE 424 2
What is a good Embedded OS?
• Modular
• Scalable
• Configurable
• Small footprint
• CPU support
• Device drivers
• Etc.
Ning Weng ECE 424 3
Commercial Embedded Linux
• AMIRIX Embedded Linux
─derived from Debian
• Coollogic Coollinux
─combines Linux and Java for Internet apps
• Coventive Xlinux
─kernel can be as small as 143KB
• Esfia RedBlue Linux
─400K, designed for wireless apps
• And many others
Ning Weng ECE 424 4
Open Source Embedded Linux
• Embedded Debian Project
─convert Debian to an embedded OS
• ETLinux
─for PC104 SBC’s
• uCLinux
─for microprocessors that don’t have MM
• uLinux (muLinux)
─fits on a single floppy
Ning Weng ECE 424 5
What’s so special about Linux?
Ning Weng ECE 424 6
Tool Chains
• Necessary to build OS and apps
• Most common are the GNU tools
• Normally the target and host machine compile and build
with the same environment
─ Host: the machine on which you develop your applications
─ Target: the machine for which you develop your applications
─ Native development (same) or cross development (different)
Ning Weng ECE 424 7
Tool Chains
Ning Weng ECE 424 8
Getting Tool
Ning Weng ECE 424 9
Anatomy of Embedded Linux
• Kernel
• Device Drivers
• Root File System
Ning Weng ECE 424 10
Packages Dependencies
FIGURE 8.1 Package Dependencies for the Bash shell – Bash Package.
Ning Weng ECE 424 11
The Kernel
Kernel steps:
• Download the source tree
• Run the tool to create the kernel .config
• Build the kernel
End kernel steps
• Root file system
• Busybox
• C library
• Boot sequence
Ning Weng ECE 424 12
The Kernel Steps
Ning Weng ECE 424 13
Sample Directories in Kernel Tree
Ning Weng ECE 424 14
The Kernel (kernel step 2)
Three options are generated in the
.config file:
• CONFIG_FEATURE_XX=y
• #CONFIG_FEATURE_XX not set
• CONFIG_FEATURE_XX=m
EX: Xscale Intel IXP435 BSP configuration change
• machine_is_ixp425()
• CONFIG_MACH_IXP425
• MACH_TYPE_IXP425
Ning Weng ECE 424 15
The Kernel (kernel step 3)
Ning Weng ECE 424 16
• Why Compressed kernel image?
Root File System
• the filesystem that is contained on the same partition on which the
root directory is located,
• the filesystem on which all the other filesystems are mounted (i.e.,
logically attached to the system) as the system is booted up (i.e.,
started up).
• Filesystem Hierarchy Standard (FHS)
─ /bin
─ /dev
─ /etc
─ /lib
─ /lib/modules
─ /proc
─ /root
─ /sbin
─ /sys
─ /tmp
─ /usr
─ /var
Ning Weng ECE 424 17
Busybox
• BusyBox combines tiny versions of many common UNIX
utilities into a single small executable.
• It provides replacements for most of the utilities you
usually find in GNU fileutils, shellutils, etc.
• The utilities in BusyBox generally have fewer options
than their full-featured GNU cousins; however, the
options that are included provide the expected
functionality and behave very much like their GNU
counterparts.
• BusyBox provides a fairly complete environment for any
small or embedded system.
Ning Weng ECE 424 18
Static or Dynamic Link
Ning Weng ECE 424 19
The Kernel (C Library)
• Libc: standard
• GLIBC: GNU C Library
• EGLIBC: Embedded GLIBC
• uCLIBC: much smaller than GLIBC
• Bionic C: used by Android
Ning Weng ECE 424 20
The Kernel (Boot Sequence)
• BIOS or early firmware
─ The first code execute by cpu after out o reset
─ Initializing memory and boot devices
• Boot loader
─ Elilo/grub2
─ Find the kernel and copy into memory and handoff to kernel
• Kernel image
─ bzImage
─ Mass storage, along with root file system and application
─ Dedicated flash area
• Root file system
─ Applications, libraries and scripts
─ Example: NFS: a directory on the host as root file system of
target
Ning Weng ECE 424 21
Debugging
• Debugging Applications (GDB, Kdevelop, Eclipse)
• Kernel debugging
• QEMU Kernel Debugging
Ning Weng ECE 424 22
Driver Development
Functions of device driver:
• Abstracts the hardware
• Manages privilege
• Enables multiplexed access
• Martials Data from an application’s process to kernel
space
• Provides security
Ning Weng ECE 424 23
Character Driver Model
Ning Weng ECE 424 24
Driver Demo
Ning Weng ECE 424 25
Device Driver
• General PCI device drivers Steps
1. Enable device
2. Request memory-mapped I/O Regions
3. Set the DMA mask size
4. Allocate and Initialize shared control data
5. Access device configuration space (if needed)
6. Manage the allocation of MSI/x interrupt vectors
7. Initialize the non-PCI capabilities
8. Register with other kernel sub systems
9. Enable the device for processing
Note: In addition to the above, networking drivers must register functions to
allow TCP/IP networking stack to interact with the adaptor to transmit and
receive packets.
Ning Weng ECE 424 26
Driver Development (interrupt handling & deferred work)
Interrupts:
• Legacy Interrupts (INTA/ INTB/ INTC/ INTD)
• Message Signal Interrupts (MSI)
• Message Signal Interrupts eXtension (MSIx)
Methods to defer work from interrupt handler:
• SoftIRQs
• Tasklets =>
• Work Queues
Ning Weng ECE 424 27
Ning Weng ECE 424 28
Memory Management
Ning Weng ECE 424 29
Synchronization/Locking
Primitives for synchronization and locking mechanisms to
race free code
1. Atomic Operation: runs without being interrupted
1. Use processor atomic instructions such as TSL (test set and
lock), and Locked CMPXCHG (locked compare and exchange)
Ning Weng ECE 424 30
Synchronization/Locking
2. Spinlock: lock with busy wait
Ning Weng ECE 424 31
Synchronization/Locking
3. Semaphore: lock with blocking wait (sleep)
Ning Weng ECE 424 32
Conclusion
• Tool Chains
• The Kernel
• Debugging
• Driver Development
• Memory Management
• Synchronization/Locking
Ning Weng ECE 424 33
Announcement
• Next class: Power Optimization
• Exam ii: 10/31
Ning Weng ECE 424 34
Embedded Linux Programming
• Cross-compiling (By ARM’s example)
Source
Code
Files
(a.c, b.c)
ARM
Object
Files
(a.o, b.o)
Cross-
Compile
Link
ARM
Executable
File
(hello)
ARM
Library
Files
(libm.a)
Linux# arm-elf-gcc a.c –o a.o
Linux# arm-elf-gcc b.c –o b.o
Linux# arm-elf-ld a.o b.o –lm –o hello
Embedded Linux Programming
• Setup cross compile environment
─ For Linux
• Download and install the Linux toolchain for your
target board such as arm-elf- tools.
• Example: Toolchain for ARM
– First, download from uClinux.org or somewhere.
Embedded Linux Programming
– Second, install it to the proper directory. (eg. /usr/local/)
You have the toolchain
installed on your system.
Extract the tools from
downloaded package.
Embedded Linux Programming
─ For Windows
• Ordinarily, you have to install CYGWIN to provide
a Linux-like environment on Windows.
Embedded Linux Programming
• Download and install the toolchain as described
before.
• Note that the toolchain must be compiled for
CYGWIN.
Figure:
Cygwin provides
a Linux-like
Environment.
Embedded Linux Programming
• Linux system programming
─ Low-level File I/O
• open(), read(), write(), close(), creat(), fnctl() …
#include <unistd.h>
#include <stdlib.h>
…
int main()
{
…
/* Open /tmp/in.txt and /tmp/out.txt*/
fd1 = open(“/tmp/in.txt”, O_RDONLY | O_CREAT);
fd2 = open(“/tmp/out.txt”, O_WRONLY | O_CREAT);
if ((read(fd1, buffer, sizeof(buffer)) != sizeof(buffer))
…
if ((write(fd2, buffer, sizeof(s)) != sizeof(s))
…
close(fd1); close(fd2);
}
Embedded Linux Programming
─ Process
• execl(), fork(), exit(), system(), wait(), getpid() …
#include <unistd.h>
…
int main()
{
pid_t new_pid;
new_pid = fork();
switch (new_pid) {
case -1 :
printf ("fork failedn"); exit(1); break;
case 0 :
printf ("This is the child process.pid = %dn“, getpid()); break;
default:
printf ("This is the parent process, pid = %d.n“, getpid());
}
return 0;
}
Embedded Linux Programming
─ Thread
• pthread_create(), pthread_join(),
pthread_cancel() …
#include <pthread.h>
…
/* Prints x’s to stderr. The parameter is unused. Does not return. */
void* print_xs (void* unused)
{
while (1)
fputc (‘x’, stderr);
}
int main ()
{
pthread_t thread_id;
/* Create a new thread to run the print_xs function. */
pthread_create (&thread_id, NULL, &print_xs, NULL);
/* Print o’s continuously to stderr. */
while (1)
fputc (‘o’, stderr);
return 0;
}
Embedded Linux Programming
─ IPC
• mmap(), munmap(), msgctl(), msgget(),
msgsnd() …
…
int main (int argc, char* const argv[])
{
…
void* file_mem;
…
/* Prepare a file large enough to hold an unsigned integer. */
fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
lseek (fd, LENGTH+1, SEEK_SET);
…
/* Create the memory mapping. */
file_mem = mmap (0, LENGTH, PROT_WRITE, MAP_SHARED, fd, 0);
…
/* Write a random integer to memory-mapped area. */
sprintf((char*) file_mem, “%dn”, random_range (-100, 100));
/* Release the memory (unnecessary because the program exits). */
munmap (file_mem, LENGTH);
return 0;
}
Embedded Linux Programming
─ Signal
• signal(), alarm(), kill(), pause(), sleep() …
#include <signal.h>
…
void ouch (int sig)
{
printf ("OUCH! I got signal %dn", sig);
signal (SIGINT, SIG_DFL);
}
main()
{
signal (SIGINT, ouch); /* Install handler for SIGINT */
while(1) /* Infinite loop until Ctrl + C is pressed */
{
printf ("Hello World!n");
sleep(1);
}
}
Embedded Linux Programming
─ Socket
• socket(), accept(), connect(), recv(), send() …
#include <sys/types.h>
…
main()
{
…
/* Create a socket … */
sd = socket(AF_INET,SOCK_STREAM,0);
…
/* Accept for connections and return a new socket description id
for handling the connection */
newsd = accept(sd, (struct sockaddr *) &ser_cli, &addrlen);
if(newsd < 0)
{
printf("cannot accept n");
exit(1);
}
…
}
Embedded Linux Programming
• uClinux for Linux programmers [11]
─ Important issue  Do not support VM.
─ Each process must be located at a place in memory
where it can be run.
─ The area of process memory must be contiguous.
─ Cannot increase the size of its available memory at
runtime.
─ ELF executable file format is unsupported
 FLAT format instead.
Embedded Linux Programming
─ The implementation of mmap() within the kernel is also quite
different.
─ The only filesystem that currently guarantees that files are stored
contiguously  romfs.
─ Only read-only mappings can be shared
 To avoid the allocation of memory.
─ Copy-on-write feature is unsupported
 Use vfork() instead of fork(). (Discuss later)
─ The stack must be allocated at compile time
 Must be aware of the stack requirements.
Embedded Linux Programming
─ fork() vs. vfork()
.
.
.
fork()
.
.
.
Data
.
.
write()
.
.
Dynamic
allocated
Copy-on-write
Non-blocking
.
.
.
fork()
.
.
.
Data
.
.
write()
.
exit()
Use parent’s stack and data
may corrupt the data or
the stack in the parent.
Suspended
Continue
executing
fork() vfork()Parent Parent
Child Child
Embedded Linux Programming
• Example: A DHCP Client: udhcp (script.c)
void run_script(struct dhcpMessage *packet, const char *name)
{
…
envp = fill_envp(packet);
/* call script */
pid = vfork();
if (pid) { /* Parent */
waitpid(pid, NULL, 0);
…
} else if (pid == 0) { /* Child */
/* exec script */
execle(client_config.script, client_config.script, name, NULL, envp);
exit(1);
}
}
Embedded Linux Programming
• Linux device driver fundamentals [12]
Figure:
The split view
of the kernel.
Embedded Linux Programming
• The role of device driver
─ To allow interaction with hardware devices.
─ Providing mechanism, not policy.
• What capabilities are to be provided? 
mechanism
• How those capabilities can be used?  policy
• Writing a Linux device driver
─ Pre-requisites
• C programming
• Microprocessor programming
─ Important concepts
• User space vs. kernel space
Embedded Linux
Programming• Execution paths: From user to kernel
Memory
b
STANDARD C
LIBRARY
MATH
LIBRARYAPPLICATION (mpg123)
Memory
Management
FilesystemsNetworking
Architecture
Dependent
Code
Memory
Manager
File System
Devices
Character
Devices
Network
Subsystem
OPERATING SYSTEM
Process
Management
Device
Control
Network Interfaces
CPU
Disk
malloc
_sbrk
fprintf
vfprintf
write
read
_isnan
sin
pow
Decoder
I/O
HTTP
Network
Initialization
socket
tan
log
wait
rand
qsort
scanf
valloc
Embedded Linux Programming
• Classes of devices
─ Characters devices
• Can be accessed as a stream of bytes.
• Such a driver usually implements at least the open, close, read, and
write system calls.
• Example: RTC driver.
─ Block devices
• A device (e.g., a disk) that can host a filesystem.
• Example: Ramdisk driver.
─ Network interfaces
• In charge of sending and receiving data packets, driven by the
network subsystem of the kernel.
• Example: Network card driver.
Embedded Linux Programming
• Kernel Module: Life and Death
Figure:
Linking a module
to the kernel. [12]
Embedded Linux Programming
• The first kernel module “Hello, world”
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(“Dual BSD/GPL”);
static int hello_init(void)
{
printk(KERN_ALERT “Hello, worldn”);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT “Goodbye, cruel worldn”);
}
module_init(hello_init);
module_exit(hello_exit);
Embedded Linux Programming
• Some other types of kernel modules
─ USB Module
─ Serial Module
─ SCSI Module
─ PCI Module
─ I2C Module
─ Misc Module
─ …
• Topics you also need to be concerned about
─ Memory allocating
─ Interrupt handling
─ Concurrency and race condition
─ I/O accessing
─ Time, delays and deferred work

Mais conteĂşdo relacionado

Mais procurados

Dfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshopDfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshop
Tamas K Lengyel
 
AOS Lab 12: Network Communication
AOS Lab 12: Network CommunicationAOS Lab 12: Network Communication
AOS Lab 12: Network Communication
Zubair Nabi
 
Virtualization which isn't: LXC (Linux Containers)
Virtualization which isn't: LXC (Linux Containers)Virtualization which isn't: LXC (Linux Containers)
Virtualization which isn't: LXC (Linux Containers)
Dobrica Pavlinušić
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
Zubair Nabi
 

Mais procurados (20)

Hardware Probing in the Linux Kernel
Hardware Probing in the Linux KernelHardware Probing in the Linux Kernel
Hardware Probing in the Linux Kernel
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Dfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshopDfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshop
 
Linuxcon Barcelon 2012: LXC Best Practices
Linuxcon Barcelon 2012: LXC Best PracticesLinuxcon Barcelon 2012: LXC Best Practices
Linuxcon Barcelon 2012: LXC Best Practices
 
AOS Lab 12: Network Communication
AOS Lab 12: Network CommunicationAOS Lab 12: Network Communication
AOS Lab 12: Network Communication
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernel
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Workshop - Linux Memory Analysis with Volatility
Workshop - Linux Memory Analysis with VolatilityWorkshop - Linux Memory Analysis with Volatility
Workshop - Linux Memory Analysis with Volatility
 
Linux Kernel Tour
Linux Kernel TourLinux Kernel Tour
Linux Kernel Tour
 
Virtualization which isn't: LXC (Linux Containers)
Virtualization which isn't: LXC (Linux Containers)Virtualization which isn't: LXC (Linux Containers)
Virtualization which isn't: LXC (Linux Containers)
 
De-Anonymizing Live CDs through Physical Memory Analysis
De-Anonymizing Live CDs through Physical Memory AnalysisDe-Anonymizing Live CDs through Physical Memory Analysis
De-Anonymizing Live CDs through Physical Memory Analysis
 
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecksKernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
 
Device Drivers and Running Modules
Device Drivers and Running ModulesDevice Drivers and Running Modules
Device Drivers and Running Modules
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
OpenWrt From Top to Bottom
OpenWrt From Top to BottomOpenWrt From Top to Bottom
OpenWrt From Top to Bottom
 
Linux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingLinux Kernel and Driver Development Training
Linux Kernel and Driver Development Training
 
Linux Kernel I/O Schedulers
Linux Kernel I/O SchedulersLinux Kernel I/O Schedulers
Linux Kernel I/O Schedulers
 

Semelhante a Lec 10-linux-review

Semelhante a Lec 10-linux-review (20)

Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
Building Embedded Linux Full Tutorial for ARM
Building Embedded Linux Full Tutorial for ARMBuilding Embedded Linux Full Tutorial for ARM
Building Embedded Linux Full Tutorial for ARM
 
Dataplane programming with eBPF: architecture and tools
Dataplane programming with eBPF: architecture and toolsDataplane programming with eBPF: architecture and tools
Dataplane programming with eBPF: architecture and tools
 
NXP IMX6 Processor - Embedded Linux
NXP IMX6 Processor - Embedded LinuxNXP IMX6 Processor - Embedded Linux
NXP IMX6 Processor - Embedded Linux
 
General Purpose GPU Computing
General Purpose GPU ComputingGeneral Purpose GPU Computing
General Purpose GPU Computing
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and Insights
 
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
 
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh naik linux_kernel_internals
Ganesh naik linux_kernel_internals
 
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh naik linux_kernel_internals
Ganesh naik linux_kernel_internals
 
Systemd: the modern Linux init system you will learn to love
Systemd: the modern Linux init system you will learn to loveSystemd: the modern Linux init system you will learn to love
Systemd: the modern Linux init system you will learn to love
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
 
embedded-linux-120203.pdf
embedded-linux-120203.pdfembedded-linux-120203.pdf
embedded-linux-120203.pdf
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
LSA2 - 02 Namespaces
LSA2 - 02  NamespacesLSA2 - 02  Namespaces
LSA2 - 02 Namespaces
 
Gentoo Linux, or Why in the World You Should Compile Everything
Gentoo Linux, or Why in the World You Should Compile EverythingGentoo Linux, or Why in the World You Should Compile Everything
Gentoo Linux, or Why in the World You Should Compile Everything
 
Module 4 Embedded Linux
Module 4 Embedded LinuxModule 4 Embedded Linux
Module 4 Embedded Linux
 
Intro To Gentoo Embedded Cclug
Intro To Gentoo Embedded CclugIntro To Gentoo Embedded Cclug
Intro To Gentoo Embedded Cclug
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
System Device Tree and Lopper: Concrete Examples - ELC NA 2022
System Device Tree and Lopper: Concrete Examples - ELC NA 2022System Device Tree and Lopper: Concrete Examples - ELC NA 2022
System Device Tree and Lopper: Concrete Examples - ELC NA 2022
 

Último

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Último (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 

Lec 10-linux-review

  • 1. ECE 424 Embedded Systems Design Embedded Linux Overview Chapter 8 Ning Weng
  • 2. What’s so special about Linux? • Multiple choices vs. sole source • Source code freely available • Robust and reliable • Modular, configurable, scalable • Superb support for networking and Internet • No runtime licenses • Large pool of skilled developers Ning Weng ECE 424 2
  • 3. What is a good Embedded OS? • Modular • Scalable • Configurable • Small footprint • CPU support • Device drivers • Etc. Ning Weng ECE 424 3
  • 4. Commercial Embedded Linux • AMIRIX Embedded Linux ─derived from Debian • Coollogic Coollinux ─combines Linux and Java for Internet apps • Coventive Xlinux ─kernel can be as small as 143KB • Esfia RedBlue Linux ─400K, designed for wireless apps • And many others Ning Weng ECE 424 4
  • 5. Open Source Embedded Linux • Embedded Debian Project ─convert Debian to an embedded OS • ETLinux ─for PC104 SBC’s • uCLinux ─for microprocessors that don’t have MM • uLinux (muLinux) ─fits on a single floppy Ning Weng ECE 424 5
  • 6. What’s so special about Linux? Ning Weng ECE 424 6
  • 7. Tool Chains • Necessary to build OS and apps • Most common are the GNU tools • Normally the target and host machine compile and build with the same environment ─ Host: the machine on which you develop your applications ─ Target: the machine for which you develop your applications ─ Native development (same) or cross development (different) Ning Weng ECE 424 7
  • 10. Anatomy of Embedded Linux • Kernel • Device Drivers • Root File System Ning Weng ECE 424 10
  • 11. Packages Dependencies FIGURE 8.1 Package Dependencies for the Bash shell – Bash Package. Ning Weng ECE 424 11
  • 12. The Kernel Kernel steps: • Download the source tree • Run the tool to create the kernel .config • Build the kernel End kernel steps • Root file system • Busybox • C library • Boot sequence Ning Weng ECE 424 12
  • 13. The Kernel Steps Ning Weng ECE 424 13
  • 14. Sample Directories in Kernel Tree Ning Weng ECE 424 14
  • 15. The Kernel (kernel step 2) Three options are generated in the .config file: • CONFIG_FEATURE_XX=y • #CONFIG_FEATURE_XX not set • CONFIG_FEATURE_XX=m EX: Xscale Intel IXP435 BSP configuration change • machine_is_ixp425() • CONFIG_MACH_IXP425 • MACH_TYPE_IXP425 Ning Weng ECE 424 15
  • 16. The Kernel (kernel step 3) Ning Weng ECE 424 16 • Why Compressed kernel image?
  • 17. Root File System • the filesystem that is contained on the same partition on which the root directory is located, • the filesystem on which all the other filesystems are mounted (i.e., logically attached to the system) as the system is booted up (i.e., started up). • Filesystem Hierarchy Standard (FHS) ─ /bin ─ /dev ─ /etc ─ /lib ─ /lib/modules ─ /proc ─ /root ─ /sbin ─ /sys ─ /tmp ─ /usr ─ /var Ning Weng ECE 424 17
  • 18. Busybox • BusyBox combines tiny versions of many common UNIX utilities into a single small executable. • It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc. • The utilities in BusyBox generally have fewer options than their full-featured GNU cousins; however, the options that are included provide the expected functionality and behave very much like their GNU counterparts. • BusyBox provides a fairly complete environment for any small or embedded system. Ning Weng ECE 424 18
  • 19. Static or Dynamic Link Ning Weng ECE 424 19
  • 20. The Kernel (C Library) • Libc: standard • GLIBC: GNU C Library • EGLIBC: Embedded GLIBC • uCLIBC: much smaller than GLIBC • Bionic C: used by Android Ning Weng ECE 424 20
  • 21. The Kernel (Boot Sequence) • BIOS or early firmware ─ The first code execute by cpu after out o reset ─ Initializing memory and boot devices • Boot loader ─ Elilo/grub2 ─ Find the kernel and copy into memory and handoff to kernel • Kernel image ─ bzImage ─ Mass storage, along with root file system and application ─ Dedicated flash area • Root file system ─ Applications, libraries and scripts ─ Example: NFS: a directory on the host as root file system of target Ning Weng ECE 424 21
  • 22. Debugging • Debugging Applications (GDB, Kdevelop, Eclipse) • Kernel debugging • QEMU Kernel Debugging Ning Weng ECE 424 22
  • 23. Driver Development Functions of device driver: • Abstracts the hardware • Manages privilege • Enables multiplexed access • Martials Data from an application’s process to kernel space • Provides security Ning Weng ECE 424 23
  • 24. Character Driver Model Ning Weng ECE 424 24
  • 25. Driver Demo Ning Weng ECE 424 25
  • 26. Device Driver • General PCI device drivers Steps 1. Enable device 2. Request memory-mapped I/O Regions 3. Set the DMA mask size 4. Allocate and Initialize shared control data 5. Access device configuration space (if needed) 6. Manage the allocation of MSI/x interrupt vectors 7. Initialize the non-PCI capabilities 8. Register with other kernel sub systems 9. Enable the device for processing Note: In addition to the above, networking drivers must register functions to allow TCP/IP networking stack to interact with the adaptor to transmit and receive packets. Ning Weng ECE 424 26
  • 27. Driver Development (interrupt handling & deferred work) Interrupts: • Legacy Interrupts (INTA/ INTB/ INTC/ INTD) • Message Signal Interrupts (MSI) • Message Signal Interrupts eXtension (MSIx) Methods to defer work from interrupt handler: • SoftIRQs • Tasklets => • Work Queues Ning Weng ECE 424 27
  • 28. Ning Weng ECE 424 28
  • 30. Synchronization/Locking Primitives for synchronization and locking mechanisms to race free code 1. Atomic Operation: runs without being interrupted 1. Use processor atomic instructions such as TSL (test set and lock), and Locked CMPXCHG (locked compare and exchange) Ning Weng ECE 424 30
  • 31. Synchronization/Locking 2. Spinlock: lock with busy wait Ning Weng ECE 424 31
  • 32. Synchronization/Locking 3. Semaphore: lock with blocking wait (sleep) Ning Weng ECE 424 32
  • 33. Conclusion • Tool Chains • The Kernel • Debugging • Driver Development • Memory Management • Synchronization/Locking Ning Weng ECE 424 33
  • 34. Announcement • Next class: Power Optimization • Exam ii: 10/31 Ning Weng ECE 424 34
  • 35. Embedded Linux Programming • Cross-compiling (By ARM’s example) Source Code Files (a.c, b.c) ARM Object Files (a.o, b.o) Cross- Compile Link ARM Executable File (hello) ARM Library Files (libm.a) Linux# arm-elf-gcc a.c –o a.o Linux# arm-elf-gcc b.c –o b.o Linux# arm-elf-ld a.o b.o –lm –o hello
  • 36. Embedded Linux Programming • Setup cross compile environment ─ For Linux • Download and install the Linux toolchain for your target board such as arm-elf- tools. • Example: Toolchain for ARM – First, download from uClinux.org or somewhere.
  • 37. Embedded Linux Programming – Second, install it to the proper directory. (eg. /usr/local/) You have the toolchain installed on your system. Extract the tools from downloaded package.
  • 38. Embedded Linux Programming ─ For Windows • Ordinarily, you have to install CYGWIN to provide a Linux-like environment on Windows.
  • 39. Embedded Linux Programming • Download and install the toolchain as described before. • Note that the toolchain must be compiled for CYGWIN. Figure: Cygwin provides a Linux-like Environment.
  • 40. Embedded Linux Programming • Linux system programming ─ Low-level File I/O • open(), read(), write(), close(), creat(), fnctl() … #include <unistd.h> #include <stdlib.h> … int main() { … /* Open /tmp/in.txt and /tmp/out.txt*/ fd1 = open(“/tmp/in.txt”, O_RDONLY | O_CREAT); fd2 = open(“/tmp/out.txt”, O_WRONLY | O_CREAT); if ((read(fd1, buffer, sizeof(buffer)) != sizeof(buffer)) … if ((write(fd2, buffer, sizeof(s)) != sizeof(s)) … close(fd1); close(fd2); }
  • 41. Embedded Linux Programming ─ Process • execl(), fork(), exit(), system(), wait(), getpid() … #include <unistd.h> … int main() { pid_t new_pid; new_pid = fork(); switch (new_pid) { case -1 : printf ("fork failedn"); exit(1); break; case 0 : printf ("This is the child process.pid = %dn“, getpid()); break; default: printf ("This is the parent process, pid = %d.n“, getpid()); } return 0; }
  • 42. Embedded Linux Programming ─ Thread • pthread_create(), pthread_join(), pthread_cancel() … #include <pthread.h> … /* Prints x’s to stderr. The parameter is unused. Does not return. */ void* print_xs (void* unused) { while (1) fputc (‘x’, stderr); } int main () { pthread_t thread_id; /* Create a new thread to run the print_xs function. */ pthread_create (&thread_id, NULL, &print_xs, NULL); /* Print o’s continuously to stderr. */ while (1) fputc (‘o’, stderr); return 0; }
  • 43. Embedded Linux Programming ─ IPC • mmap(), munmap(), msgctl(), msgget(), msgsnd() … … int main (int argc, char* const argv[]) { … void* file_mem; … /* Prepare a file large enough to hold an unsigned integer. */ fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); lseek (fd, LENGTH+1, SEEK_SET); … /* Create the memory mapping. */ file_mem = mmap (0, LENGTH, PROT_WRITE, MAP_SHARED, fd, 0); … /* Write a random integer to memory-mapped area. */ sprintf((char*) file_mem, “%dn”, random_range (-100, 100)); /* Release the memory (unnecessary because the program exits). */ munmap (file_mem, LENGTH); return 0; }
  • 44. Embedded Linux Programming ─ Signal • signal(), alarm(), kill(), pause(), sleep() … #include <signal.h> … void ouch (int sig) { printf ("OUCH! I got signal %dn", sig); signal (SIGINT, SIG_DFL); } main() { signal (SIGINT, ouch); /* Install handler for SIGINT */ while(1) /* Infinite loop until Ctrl + C is pressed */ { printf ("Hello World!n"); sleep(1); } }
  • 45. Embedded Linux Programming ─ Socket • socket(), accept(), connect(), recv(), send() … #include <sys/types.h> … main() { … /* Create a socket … */ sd = socket(AF_INET,SOCK_STREAM,0); … /* Accept for connections and return a new socket description id for handling the connection */ newsd = accept(sd, (struct sockaddr *) &ser_cli, &addrlen); if(newsd < 0) { printf("cannot accept n"); exit(1); } … }
  • 46. Embedded Linux Programming • uClinux for Linux programmers [11] ─ Important issue  Do not support VM. ─ Each process must be located at a place in memory where it can be run. ─ The area of process memory must be contiguous. ─ Cannot increase the size of its available memory at runtime. ─ ELF executable file format is unsupported  FLAT format instead.
  • 47. Embedded Linux Programming ─ The implementation of mmap() within the kernel is also quite different. ─ The only filesystem that currently guarantees that files are stored contiguously  romfs. ─ Only read-only mappings can be shared  To avoid the allocation of memory. ─ Copy-on-write feature is unsupported  Use vfork() instead of fork(). (Discuss later) ─ The stack must be allocated at compile time  Must be aware of the stack requirements.
  • 48. Embedded Linux Programming ─ fork() vs. vfork() . . . fork() . . . Data . . write() . . Dynamic allocated Copy-on-write Non-blocking . . . fork() . . . Data . . write() . exit() Use parent’s stack and data may corrupt the data or the stack in the parent. Suspended Continue executing fork() vfork()Parent Parent Child Child
  • 49. Embedded Linux Programming • Example: A DHCP Client: udhcp (script.c) void run_script(struct dhcpMessage *packet, const char *name) { … envp = fill_envp(packet); /* call script */ pid = vfork(); if (pid) { /* Parent */ waitpid(pid, NULL, 0); … } else if (pid == 0) { /* Child */ /* exec script */ execle(client_config.script, client_config.script, name, NULL, envp); exit(1); } }
  • 50. Embedded Linux Programming • Linux device driver fundamentals [12] Figure: The split view of the kernel.
  • 51. Embedded Linux Programming • The role of device driver ─ To allow interaction with hardware devices. ─ Providing mechanism, not policy. • What capabilities are to be provided?  mechanism • How those capabilities can be used?  policy • Writing a Linux device driver ─ Pre-requisites • C programming • Microprocessor programming ─ Important concepts • User space vs. kernel space
  • 52. Embedded Linux Programming• Execution paths: From user to kernel Memory b STANDARD C LIBRARY MATH LIBRARYAPPLICATION (mpg123) Memory Management FilesystemsNetworking Architecture Dependent Code Memory Manager File System Devices Character Devices Network Subsystem OPERATING SYSTEM Process Management Device Control Network Interfaces CPU Disk malloc _sbrk fprintf vfprintf write read _isnan sin pow Decoder I/O HTTP Network Initialization socket tan log wait rand qsort scanf valloc
  • 53. Embedded Linux Programming • Classes of devices ─ Characters devices • Can be accessed as a stream of bytes. • Such a driver usually implements at least the open, close, read, and write system calls. • Example: RTC driver. ─ Block devices • A device (e.g., a disk) that can host a filesystem. • Example: Ramdisk driver. ─ Network interfaces • In charge of sending and receiving data packets, driven by the network subsystem of the kernel. • Example: Network card driver.
  • 54. Embedded Linux Programming • Kernel Module: Life and Death Figure: Linking a module to the kernel. [12]
  • 55. Embedded Linux Programming • The first kernel module “Hello, world” #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit);
  • 56. Embedded Linux Programming • Some other types of kernel modules ─ USB Module ─ Serial Module ─ SCSI Module ─ PCI Module ─ I2C Module ─ Misc Module ─ … • Topics you also need to be concerned about ─ Memory allocating ─ Interrupt handling ─ Concurrency and race condition ─ I/O accessing ─ Time, delays and deferred work