abstraction via the os device drivers
play

Abstraction via the OS Device Drivers Jonathan Misurda - PDF document

Abstraction via the OS Device Drivers Jonathan Misurda jmisurda@cs.pitt.edu Software Layers Device Drivers User User space program User User level I/O software & libraries Kernel space Device independent OS software Operating


  1. Abstraction via the OS Device Drivers Jonathan Misurda jmisurda@cs.pitt.edu Software Layers Device Drivers User User space program User User ‐ level I/O software & libraries Kernel space Device ‐ independent OS software Operating Rest of the OS Device drivers system (kernel) Keyboard Disk Interrupt handlers driver driver Hardware Keyboard Disk controller controller Types of Devices Mechanism vs. Policy • Block Devices • Mechanism – What capabilities to have (Algorithm) – A device that stores data in fixed ‐ sized blocks, each uniquely addressed, and can be randomly accessed • Policy – How to use a mechanism – E.g., Disks, Flash Drives (Parameters) • Character Devices – Device that delivers or accepts a stream of • Drivers should be flexible by only providing characters mechanisms not policies – E.g., Keyboard, mouse, terminal

  2. Device Drivers in Linux /dev • Can be compiled into the kernel • Character and block devices can be exposed via a filesystem • /dev/ typically contains “files” that represent • Can be loaded dynamically as Modules the different devices on a system • /dev/console – the console • /dev/fd/ ‐ a process’s open file descriptors /proc Sysfs • Virtual filesystem with information about the • Exports information about devices and drivers system and running programs to userspace, • Can configure aspects of device • /proc/cpuinfo – text “file” containing CPU /sys/ • information Hello World Module Build and Run #include <linux/init.h> % make #include <linux/module.h> make[1]: Entering directory `/usr/src/linux ‐ 2.6.10' MODULE_LICENSE("Dual BSD/GPL"); CC [M] /home/ldd3/src/misc ‐ modules/hello.o static int hello_init(void) Building modules, stage 2. { MODPOST printk(KERN_ALERT "Hello, world\n"); CC /home/ldd3/src/misc ‐ modules/hello.mod.o return 0; LD [M] /home/ldd3/src/misc ‐ modules/hello.ko } make[1]: Leaving directory `/usr/src/linux ‐ 2.6.10' static void hello_exit(void) % su { root# insmod ./hello.ko printk(KERN_ALERT "Goodbye, cruel world\n"); Hello, world } root# rmmod hello module_init(hello_init); Goodbye cruel world module_exit(hello_exit); root#

  3. Module Helper Programs Why printk? • insmod – loads a module • The kernel does not have access to libraries • rmmod – unloads a module • Can’t use printf or many other standard functions (FILE stuff, strtok, etc.) • lsmod – lists what modules are loaded • Modules are linked against the kernel only • modprobe – loads a module checking dependencies • Kernel provides useful set of common functions like strcpy, strcat, etc. MODULE_LICENSE Loading a Module • Informs the kernel what license the module source code is under • Affects which symbols (functions, variables, etc.) it may access in the kernel • A GPL ‐ licensed module can access everything • Certain (or not specifying one) module license will “taint” the kernel Things you can’t do in the kernel Error Handling int __init my_init_function(void) • Stack allocate big arrays { int err; – The stack is small, maybe only a single page (4KB) /* registration takes a pointer and a name */ err = register_this(ptr1, “driver”); – Use kmalloc to allocate heap space if (err) goto fail_this; err = register_that(ptr2, “driver”); if (err) goto fail_that; • Floating point arithmetic err = register_those(ptr3, “driver”); if (err) goto fail_those; – Context switch into the kernel does not save return 0; /* success */ floating point registers fail_those: unregister_that(ptr2, “driver”); fail_that: unregister_this(ptr1, “driver”); fail_this: return err; /* propagate the error */ }

  4. Driver Stacking Race Conditions • The kernel will make calls into your module while your initialization function is still running Module Parameters Parameter Types insmod hellop howmany=10 whom=“Mom” • bool, invbool (int) • charp static char *whom = "world"; • int, long, short static int howmany = 1; • uint, ulong, ushort module_param(howmany, int, S_IRUGO); module_param(whom, charp, S_IRUGO); • Array parameters, where the values are supplied as a comma ‐ separated list: – module_param_array(name,type,num,perm); Permissions Makefile • Module parameters show up as files in the obj ‐ m := hello_dev.o sysfs entry KDIR := /u/SysLab/shared/linux ‐ 2.6.23.1 – If perm is set to 0, there is no sysfs entry at all PWD := $(shell pwd) • S_IRUGO – Read Only default: $(MAKE) ‐ C $(KDIR) M=$(PWD) modules • S_IRUGO|S_IWUSR – Writeable by root

  5. User Space Drivers • FUSE – Filesystem in User Space • Newest Kernels support other user space drivers • Advantages?

Recommend


More recommend