Finish up OS topics Group plans. Today Finish up and review Linux device driver stuff – Walk example again – See how it all goes together – Discuss talking.

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

Linux device-driver issues
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
RT_FIFO, Device driver.
Sogang University Advanced Operating Systems (Linux Device Drivers) Advanced Operating Systems (Linux Device Drivers) Sang Gue Oh, Ph.D.
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 - /
Computer System Laboratory
USERSPACE I/O Reporter: R 張凱富.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Building and Running Modules Sarah Diesburg COP 5641.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
52 Advanced Operating Systems Writing Device Drivers.
Embedded Systems Programming Writing Device Drivers.
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
Tutorial and Demos on Linux Virtual Machine
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Device Drivers In Linux © Gregory Kesden Fall 2000.
EECS 498 Advanced Embedded Systems Lecture 4: Linux device drivers and loadable kernel modules.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
M. Muztaba Fuad Advanced Operating System Project Device Drivers.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
Operating System Program 5 I/O System DMA Device Driver.
CS 6560 Operating System Design Lecture 13 Finish File Systems Block I/O Layer.
For OS Experiments. What Do We Need? A Computer &
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Modules and Device Drivers (ESP – Fall 2014) Computer Science & Engineering Department.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Embedded Linux. Linux? A POSIX-compliant and widely deployed desktop/server operating system licensed under the GPL – POSIX Unix-like environment (shell,
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
UNIX Files File organization and a few primitives.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Implementing System Calls CS552 Kartik Gopalan. CS552/BU/Spring2008 Steps in writing a system call 1.Create an entry for the system call in the kernel’s.
Sogang University Advanced Operating Systems (Enhanced Device Driver Operations) Advanced Operating Systems (Enhanced Device Driver Operations) Sang Gue.
K ERNEL D EVELOPMENT CSC585 Class Project Dawn Nelson December 2009.
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.
Linux Device Driver 2009/04/08. Reference Book Another Reference Book Embedded Linux Primer: A Practical, Real-World Approach By Christopher Hallinan.
Interfacing Device Drivers with the Kernel
How to write a MSGQ Transport (MQT) Overview Nov 29, 2005 Todd Mullanix.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
I/O Software CS 537 – Introduction to Operating Systems.
1 Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems.
Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.
OPERATING SYSTEMS COURSE THE HEBREW UNIVERSITY SPRING FUSE File System.
Virtual Memory Mohammad H. Mofrad February 23, 2016
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 4: Managing File System State Dr. Xiao Qin Auburn University.
Embedded Linux.
Device Driver_Skeleton
Introduction to Developing Embedded Linux Device Drivers
Linux Kernel Module Programming
Computer System Laboratory
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
Embedded Linux.
Embedded Linux.
ECE 391 Exam 2 HKN Review Session
Operation System Program 4
Intro to Kernel Modules and /proc
Embedded Linux.
Review and Q/A.
Kernel – Device Drivers (part 2)
CS 6560 Operating System Design Kernel Loadable Modules
Implementation of Embedded OS
Computer System Laboratory
Loadable Kernel Modules
Presentation transcript:

Finish up OS topics Group plans

Today Finish up and review Linux device driver stuff – Walk example again – See how it all goes together – Discuss talking to MMIO RTOS (on board) – Deferred interrupts Discussion of terminology and what’s going on. – Using semaphores to protect a resource Project overviews (time allowing) – Each group talks for seconds. – Elevator pitch.

Linux Device drivers

Linux Devices: Use Model Everything is a file – So open, close, read and write all defined Example: – UART Read/write model makes sense here. – Read from any data buffered. – Write data out Just treat /dev/ttyS0 (standard name for UART0) as a file. But need more sometimes – Set baud rate, parity bits, etc. – We’ll hit that in a bit. Basically, file model isn’t perfect.

Linux Device Driver use case example fd = fopen(“/dev/ttyS0”, “r”); – You can now read from that serial port. If nothing new, you get an empty file. Need to take care—data once read is gone. Obviously can open it for writing – And can do both at the same time (which is kinda neat, but not all that file-ish)

Steps in getting a LKM up Create a module and compile it. Create a device

Creating a module All modules need to define functions that are to be run when: – The module is loaded into the kernel – The module is removed from the kernel We just write C code (see next slide) We need to compile it as a kernel module. – We invoke the kernel’s makefile. – sudo make –C /lib/modules/xxx/build M=$PWD modules This makes (as root) using the makefile in the path specified. Makes all C files in the directory you started in* Creates.ko (rather than.o) file Xxx is some kernel version/directory Modules

Dumb but complete module #include MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(" Hello World!\n"); return 0; } static void hello_exit(void) { printk(" Bye world!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE – Required. – Short list of allowed licenses. printk() – Kernel print. Prints message to console and to log. indicates high priority message, so it gets logged. module_init() – Tells system what module to call when we first load the module. module_exit() – Same but called when module released. Modules

The above module doesn’t do anything… It isn’t a useful module in any sense of the word. – Just showing the minimum one needs to do. Well plus some printing to logs so we can see that the code is actually getting run.

Modules: Listing, loading and removing From the command line: – lsmod List modules. – insmod Insert module into kernel – Adds to list of available modules Causes function specified by module_init() to be called. – rmmod Removes module from kernel Modules

lsmod Module Size Used by memory hello binfmt_misc bridge stp bridge bnep video Modules

insmod Very (very) simple – insmod xxxxx.ko Says to insert the module into the kernel Modules

Better example: One character device We are going to create a device that is just a single byte of memory. – Whatever the last thing you wrote to it, is what will be read. For example – $ echo -n abcdef >/dev/memory – Followed by $ cat /dev/memory Prints an “f”. Silly, but not unreasonable. – It’s also printing some stuff to the log. Not a great idea in a real device, but handy here. Almost entirely from Modules: single character memory example

includes /* Necessary includes for device drivers */ #include #include #include /* printk() */ #include /* kmalloc() */ #include /* everything... */ #include /* error codes */ #include /* size_t */ #include #include /* O_ACCMODE */ #include /* cli(), *_flags */ #include /* copy_from/to_user */ Modules: single character memory example

License and function prototypes MODULE_LICENSE("Dual BSD/GPL"); int memory_open (struct inode *inode, struct file *filp); int memory_release (struct inode *inode, struct file *filp); ssize_t memory_read (struct file *filp, char *buf, size_t count, loff_t *f_pos); ssize_t memory_write (struct file *filp, char *buf, size_t count, loff_t *f_pos); void memory_exit (void); int memory_init (void); Modules: single character memory example

Setting up the standard interface struct file_operations memory_fops = { read: memory_read, write: memory_write, open:memory_open, release: memory_release }; struct file_operations fops = {.read = memory_read,.write = memory_write,.open = memory_open,.release = memory_release }; This is a weird bit of C syntax. – Initializes struct elements. So “read” member is now “memory_read” – Technically unsupported these days? gcc supports it though Modules: single character memory example 2 nd one is standard (C99). 1 st one older and gcc feature initially

file_operations struct struct file_operations { ssize_t(*read) (struct file *, char __user *, size_t, loff_t *); ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t(*readv) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t(*writev) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t(*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void __user *); ssize_t(*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long,unsigned long,unsigned long); }; Modules: single character memory example

file_operations: A few members struct file_operations { ssize_t(*read) (struct file *, char __user *, size_t, loff_t *); ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *); }; Modules: single character memory example

Set up init and exit Some globals module_init(memory_init); module_exit(memory_exit); int memory_major = 60; char *memory_buffer; Modules: single character memory example

memory_init int memory_init(void) { int result; result = register_chrdev(memory_major, "memory", &memory_fops); if (result memory: cannot obtain major number %d\n", memory_major); return result; } /* Allocating memory for the buffer */ memory_buffer = kmalloc (1, GFP_KERNEL); if (!memory_buffer) { result = -ENOMEM; goto fail; } memset(memory_buffer, 0, 1); // initialize 1 byte with 0s. printk(" Inserting memory module\n"); return 0; fail: memory_exit(); return result; } Kmalloc does what you’d expect. The flag provides rules about where and how To get the memory. See makelinux.com/ldd3/chp-8-sect-1 60, via global Device name, need not be the same as in /dev Name of file_operations structure. Modules: single character memory example

memory_exit void memory_exit(void) { unregister_chrdev(memory_major, "memory"); if (memory_buffer) { kfree(memory_buffer); } Modules: single character memory example

Open and release (close) int memory_open (struct inode *inode, struct file *filp) { printk(" Minor: %d\n", MINOR(inode->i_rdev)); return 0; } int memory_release (struct inode *inode, struct file *filp) { return 0; } Modules: single character memory example

ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { /* Transfering data to user space */ copy_to_user (buf,memory_buffer,1); /* Changing reading position as best suits */ if (*f_pos == 0) { *f_pos+=1; return 1; } else { return 0; } copy_to_user copies to a location in userspace (the first argument) from kernel space (the second argument), a specific number of bytes. Recall virtual memory… f_pos is the file position. What do you think happens if you don’t change *f_pos? Modules: single character memory example

memory_write ssize_t memory_write( struct file *filp, char *buf, size_t count, loff_t *f_pos) { char *tmp; tmp=buf+count-1; copy_from_user(memory_buffer,tmp,1); return 1; } Modules: single character memory example

Steps in getting a LKM up Create a module and compile it. – Requires the Kernel source and LKM support Which can be a royal pain in the rear to get working. Hopefully supported nicely by your board’s Linux build – Raspberry Pi—easy money – Beagle Bone (2 years ago) one of the two employees in charge needed a few hours to get it working. Now let’s look at creating the device

Creating a device mknod /dev/memory c 60 0 – Creates a device named /dev/memory – Major number 60 – Minor number 0 Minor numbers are passed to the driver to distinguish different hardware with the same driver. – Or, potentially, the same hardware with different parameters (as the floppy example) Need to change permissions appropriately! – Example: chmod 644 /dev/memory – What does that do? Modules: devices

ioctl Truth is that we might need to do a lot of things that the file interface just doesn’t work for. e.g.: – Set baud rate. – Do a full-duplex (bi-directional) transfer over SPI So we have ioctl int device_ioctl( struct inode *inode, /* see include/linux/fs.h */ struct file *file, /* ditto */ unsigned int ioctl_num, /* number and param for ioctl */ unsigned long ioctl_param /* Generally a pointer!*/ );

Example of use ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode); if (ret == -1) pabort("can't get spi mode"); ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't set bits per word"); ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't get bits per word"); /* * max speed hz */ ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't get max speed hz");

OS things Interrupt and semaphore issues were confusing to people in lab. – Want to touch on again.

Deferred Interrupts Think about wanting to split task in half. – First-Level Interrupt Handler (FLIH) aka upper half Cause jitter Could lose data – Second-Level Interrupt Handler aka lower half or bottom half Lower priority

Semaphore examples