Kernel – Device Drivers (part 2)

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Computer System Laboratory
Building and Running Modules Sarah Diesburg COP 5641.
The Kernel Abstraction. Challenge: Protection How do we execute code with restricted privileges? – Either because the code is buggy or if it might be.
Embedded Systems Programming Writing Device Drivers.
OS Spring’03 Introduction Operating Systems Spring 2003.
CS 300 – Lecture 23 Intro to Computer Architecture / Assembly Language Virtual Memory Pipelining.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
OS Spring’04 Introduction Operating Systems Spring 2004.
1 Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:
Operating System Program 5 I/O System DMA Device Driver.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
For OS Experiments. What Do We Need? A Computer &
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Embedded Linux Day 2. Stuffs HW1 posted today – Shooting for 1-2 hours. – Review scheduling stuff & licensing. HW0 in lab Sign up for group meetings for.
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
I/O Software CS 537 – Introduction to Operating Systems.
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
1 Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
Virtual Memory Mohammad H. Mofrad February 23, 2016
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
Introduction to Operating Systems Concepts
CS/COE 0449 (term 2174) Jarrett Billingsley
OS – Ex 1 Nezer J. Zaidenberg.
Programs, Instructions, and Registers
Introduction to Operating Systems
Interrupts and signals
Linux Kernel Module Programming
CS 6560: Operating Systems Design
Process Memory COMP 40: Machine Structure and
Programs – Loading and Running an Executable
Mechanism: Limited Direct Execution
Computer System Laboratory
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
OS – Process Creation and Destruction
Swapping Segmented paging allows us to have non-contiguous allocations
Lecture 1 Runtime environments.
Want to play a game? – Linux Kernel Modules
Welcome to OS Class!.
ECE 391 Exam 2 HKN Review Session
Intro to Multiprocessing
Kthreads, Mutexes, and Debugging
Pseudo-ops, Debugging, etc.
Introduction to Operating Systems
C – Scope, Lifetime, and the Stack
CS 0449 Jarrett Billingsley
Intro to Kernel Modules and /proc
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CS 6560 Operating System Design
CS/COE 0449 Jarrett Billingsley
Lecture Topics: 11/1 General Operating System Concepts Processes
Architectural Support for OS
CS/COE 0449 Jarrett Billingsley
CS 6560 Operating System Design Kernel Loadable Modules
Programs – Loading and Running an Executable
Implementation of Embedded OS
Computer System Laboratory
Architectural Support for OS
System Calls System calls are the user API to the OS
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Presentation transcript:

Kernel – Device Drivers (part 2) CS/COE 0449 Jarrett Billingsley

Class announcements blllruughh project 4 is now due the 20th, yay when it's up like tomorrow hahah yeah right. it's me. CS449

Programming and loading a kernel module CS449

Like user mode programs, but not inside the kernel, there are a lot of little differences. there's no C stdlib at all! you also can't use floats. Stack #include <stdio.h> the stack is tiny. printk("%f", 1.0) alloca() and VLAs are a bad idea. there are only the functions that the kernel provides. that seems like a weird restriction, but it's to make context switches faster. so you wanna use the heap… but there's no malloc/free. printk() kmalloc() kfree() float registers are huge, and not saving them saves time. copy_to_user() - the kernel functions are usually subtly different, like how printk requires a "severity" as its first parameter. - context switches mostly involve swapping out the values of all the registers, and float registers, well… - the AVX registers on modern x64 CPUs total 1 KILOBYTE of data. yeah. not great for performance. CS449

The module lifecycle Kernel the two commands insmod and rmmod load and unload modules. insmod after the module is loaded, the kernel calls its init function. Kernel rmmod rmmod call's the module's exit function and unloads the module. we can use lsmod (/sbin/lsmod on thoth) to get a listing of loaded modules. - yes, we're talking about the older Linux 2.6 module system. whatever. the concepts are important. - also tons of stuff still uses 2.6… for better or for worse CS449

Module dependencies just like user mode libraries, modules can depend on other modules. usb.ko driver.ko managing these dependencies yourself would be a pain. a driver for your weird USB game controller will depend on the USB module. the modprobe command deals with this for us. /lib/modules/*/modules.dep is a listing of these dependencies. CS449

Hello, Kernel! let's look at a very simple module: the hello world module. #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello, world!\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, world!\n"); module_init(hello_init); module_exit(hello_exit); Linux is picky about licensing… 0 means success. these lines tell the kernel what the init and exit functions are. CS449

Compiling it the Linux kernel comes with all the include files, libraries, linker scripts etc. needed to compile kernel modules. we just need to use a little Makefile (inside hellomod.tar)… and use this command: make ARCH=i386 if you forget the ARCH, it'll make a 64-bit module… now I can load it into my little Linux VM! don't worry, you'll learn how to do all this for project 5. CS449

A software device driver CS449

A more substantial example the hello_dev example shows a very simple driver. in its init function, it calls misc_register(), a kernel function to say "hey, I offer you a gift of this device." the hello_fops struct tells the kernel about the device. the hello_read function… well, it's the other side of read()! Kernel Space User Space hello_read() { ... copy_to_user() } char buf[10]; f = fopen("/dev/hello", "r"); fread(&buf, 10, f); since we're the kernel, we can copy stuff directly into the user's memory! CS449

Things to watch out for init() { misc_register(&hello_dev); say we had two devices in this module. init() { misc_register(&hello_dev); misc_register(&cats_dev); } hello_read() {...} cats_read() {...} the instant this is loaded… this function might be called!!! even before init() finishes!!! your module is also responsible for managing its own memory. if your module leaks memory… - restart the program. - except "the program" here is "your entire computer." well, what's the only way to clean up a memory leak in C? CS449

Demonstration let's look at the hello_dev example. when loaded, it creates a device… which doesn't actually show up in /dev/ yet. it does show up in /sys/class/misc. if we cat /sys/class/misc/hello/dev we get 10:63 these are the major and minor device numbers the OS gave it. now we run this command: mknod /dev/hello c 10 63 and if we do this: cat /dev/hello it prints our message!!! CS449

User-mode Drivers CS449

Wait, what? doesn't that seem like a contradiction? user-mode processes can't access hardware. ...directly. 😜 remember the virtual filesystems? we can read/write the "files" in /dev and /sys to control kernel modules what if we took this further? CS449

root admins users daemons Privileges on a POSIX system, there are many users, and groups of users. each user group gets privileges: capabilities that other groups may not have. root admins users daemons root can do almost anything. admins have fewer privileges than root, but more than most users. daemons are "special" processes that run in the background who have higher-than-usual privileges. - btw if you cringe at the word "privilege", here's what it means: - "the things that you never have to worry about because of who you are." - e.g. if you are straight, you never have to worry about being disowned by your family for marrying the person you love. - (unless they do for other similarly stupid reasons.) privileges like… reading and writing to the files in /dev and /sys! CS449

How user-mode drivers work libusb is a common user-mode driver framework. with it, you can control USB devices from user-mode daemons. 3. the user-mode driver handles the request and sends the data back to libusb. 1. a program accesses a software device. 2. the libusb driver forwards the request to the user-mode driver. User Space (daemon) User Space (user) open("/sys/...") fread(...) open("/dev/thing") Kernel Space libusb Syscall handler 4. libusb sends the data back to the original process. CS449

Why would we do this? remember what was scary about kernel modules? well, if our user-mode driver is evil or broken… big deal! so what! it'll crash, because it's a user-mode process. your device might stop working, but oh well. restart the driver! they're much easier to develop. you just saw how many steps there are to making a kernel module. and you have to have the privileges to install them. in user mode, you can use any language, use any user-mode library, you can debug them in gdb, and compile and run them much more easily. CS449

But of course there are downsides what'd you notice about that diagram? how many times did we cross the user/kernel mode barrier? once to do the original syscall… once to get up into the driver… once for the driver to do a syscall… once for the OS to return to the driver… once for the driver to return data to libusb… and once for libusb to return to the original process. CONTEXT SWITCHES GALORE this makes them much slower than kernel-mode drivers buuut sometimes it's worth it. you don't need crazy speed for a thing that blinks a light when you get an email. CS449

Excuse me… CPU one more thing user-mode drivers can't do: this is a hardware interrupt. doot doot doo, running a process… Code Memory Process CPU they're used for very low-latency asynchronous notifications. oh hey, I finished reading that data you wanted only the kernel is allowed to respond to them! oh! hey, kernel-mode driver! I'm on itttt CS449