EmbeddedSoftware2
EmbeddedSoftware2
Applications
Architecture-Dependent Code
Hardware
ARCHITECTURAL APPROACHES
• Monolithic.
• Layered.
• Modularized.
• Micro-kernel.
• Virtual machine.
LINUX SOURCE TREE LAYOUT
/usr/src/linux scripts
Documentation
acorn 802
atm adfs asm-alpha appletalk
alpha block affs adfs atm
asm-arm affs
arm cdrom autofs asm-generic ax25
i386 char autofs4 autofs bridge
asm-i386 autofs4
ia64 dio bfs asm-ia64 core
m68k fc4 code bfs decnet
asm-m68k code
mips i2c cramfs asm-mips econet
mips64 i2o devfs cramfs ethernet
asm-mips64 devfs
ppc ide devpts linux ipv4
s390 ieee1394 efs devpts ipv6
math-emu efs
sh isdn ext2 net ipx
sparc macintosh fat ext2 irda
pcmcia fat
sparc64 misc hfs scsi khttpd
net hpfs hfs lapb
video … hpfs …
… … …
LINUX/ARCH
• Contains:
• virtual filesystem (VFS) framework.
• subdirectories for actual filesystems.
• vfs-related files:
• exec.c, binfmt_*.c - files for mapping new process images.
• devices.c, blk_dev.c – device registration, block device support.
• super.c, filesystems.c.
• inode.c, dcache.c, namei.c, buffer.c, file_table.c.
• open.c, read_write.c, select.c, pipe.c, fifo.c.
• fcntl.c, ioctl.c, locks.c, dquot.c, stat.c.
LINUX/INCLUDE
• include/asm-*:
• Architecture-dependent include subdirectories.
• include/linux:
• Header info needed both by the kernel and user apps.
• Usually linked to /usr/include/linux.
• Kernel-only portions guarded by #ifdefs
• #ifdef __KERNEL__
• /* kernel stuff */
• #endif
• Other directories:
• math-emu, net, pcmcia, scsi, video.
LINUX/INIT
• Scripts for:
• Menu-based kernel configuration.
• Kernel patching.
• Generating kernel documentation.
SUMMARY
Power Power
on off
Boot Kernel OS RUN! Shut
Init Init down
BOOT TERMINOLOGY
• Loader:
• Program that moves bits from disk (usually)
to memory and then transfers CPU control to the newly
“loaded” bits (executable).
• Bootloader / Bootstrap:
• Program that loads the “first program” (the kernel).
• Boot PROM / PROM Monitor / BIOS:
• Persistent code that is “already loaded” on power-up.
• Boot Manager:
• Program that lets you choose the “first program” to load.
LILO: LINUX LOADER
boot=/dev/hda
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
default=linux
image=/boot/vmlinuz-2.2.12-20
label=linux
initrd=/boot/initrd-2.2.12-20.img
read-only
root=/dev/hda1
/SBIN/INIT
• Net booting:
• Remote root (Diskless-root-HOWTO).
• Diskless boot (Diskless-HOWTO).
SUMMARY
msg:
.string "Hello, world!\n" # our dear string
len = . - msg # length of our dear string
_start:
# and exit
• Main files:
• arch/i386/kernel/entry.S
• System call and low-level fault handling routines.
• include/asm-i386/unistd.h
• System call numbers and macros.
• kernel/sys.c
• System call service routines.
ARCH/I386/KERNEL/ENTRY.S
.data
ENTRY(sys_call_table)
.long SYMBOL_NAME(sys_ni_syscall) /* 0 - old "setup()" system
call*/
.long SYMBOL_NAME(sys_exit)
.long SYMBOL_NAME(sys_fork)
.long SYMBOL_NAME(sys_read)
.long SYMBOL_NAME(sys_write)
Note: This picture is excerpted from Write a Linux Hardware Device Driver, Andrew O’Shauqhnessy, Unix world
LOADABLE KERNEL MODULE (LKM)
• Device drivers
• Filesystem driver (one for ext2, MSDOS FAT16, 32, NFS)
• System calls
• Network Drivers
• TTY line disciplines. special terminal devices.
• Executable interpreters.
BASIC LKM (PROGRAM)
• Interrupt based
DEVICE DRIVER INTERFACE
Note: This picture is excerpted from Write a Linux Hardware Device Driver, Andrew O’Shauqhnessy, Unix world
VSF & MAJOR NUMBER
• Device number
• Major (used to VFS mapping to right functions)
• Minor (sub-devices)
• mknod /dev/stk c 38 0
• ls –l /dev/tty
• crw-rw-rw- 1 root root 5, 0 Apr 21 18:33 /dev/tty
REGISTER AND UNREGISTER DEVICE
}
void cleanup_module(void) /*used for a clean shutdown*/
...
}
REGISTER AND UNREGISTER DEVICE
• compile
-Wall -DMODULE -D__KERNEL__ -DLINUX –DDEBUG -I /usr/include/linux/version.h -
I/lib/modules/`uname -r`/build/include
• end_request() • kfree*()
• Called when a request has been satisfied or aborted • Frees memory previously allocated with kmalloc()
• free_irq() • kmalloc()
• Frees an IRQ previously acquired with request_irq() or • Allocates a chu nk of memory no larger than 4096 bytes.
irqaction()
• MAJOR()
• get_user*() • Reports the major device number for a device.
• Allows a driver to access data in user space, a memory
area distinct from the kernel • MINOR()
• Reports the minor device number for a device.
• inb(), inb_p()
• Reads a byte from a port. Here, inb() goes as fast as it
can, while inb_p() pauses before returning.
KERNEL FUNCTIONS
• memcpy_*fs() • select_wait()
• Copies chunks of memory between user space • Adds a process to the proper select_wait
and kernel space queue.
• outb(), outb_p()
• *sleep_on()
• Writes a byte to a port. Here, outb() goes as fast • Sleeps on an event, puts a wait_queue entry in
as it can, while outb_p() pauses before returning. the list so that the process can be awakened
• printk() on that event.
• A version of printf() for the kernel.
• sti()
• put_user*() • Allows interrupts to be acknowledged.
• Allows a driver to write data in user space.
• sys_get*()
• register_*dev() • System calls used to get information regarding
• Registers a device with the kernel. the process, user, or group.
• request_irq() • wake_up*()
• Requests an IRQ from the kernel, and, if • Wakes up a process that has been put to sleep
successful, installs an IRQ interrupt handler. by the matching *sleep_on() function.
LINUX INTERRUPT
HANDLING
INTERRUPTS