Character Driver Issues Implementing ‘/dev/physmem’

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Using our device-drivers How can an application display the data in a device special file?
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.
Creating a device-file node An introduction to some privileged Linux system-calls (needed for an upcoming programming exercise)
The ‘system-call’ interface We see how an application program can invoke privileged kernel services.
Sleeping and waking An introduction to character-mode device-driver modules for Linux.
UNIX’s “grand illusion” How Linux makes a hardware device appear to be a ‘file’
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
SiS 315 Graphics Engine Introduction to some capabilities of graphics accelerator hardware.
Standard C Libraries Application Programmming Interface to System-Calls.
OS Project #2:Random File Access  Random file access: using the lseek() system call to access files in UNIX. zTake arguments from the command line. 
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
Standard C Library Application Programming Interface to System-Calls.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
File System FAQ. Fs_init() vs. fs_mkfs() What need to be done where: FS_init() takes care of system wise FS related initialization (Related to the running.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
Scientific Visualization Using imagery to aid humans in understanding a complicated phenomenon.
The ‘ioctl’ driver-function On implementing a way to query and modify our UART’s baudrate via the ‘device-file’ abstraction.
Looking at kernel objects How a character-mode Linux device driver can be useful in viewing a ‘net_device’ structure.
Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati. Writing/Registering to /proc Character Device Driver – Characteristics and functionality – Basic IO functions.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Linux Installation and Administration – Lesson 5 Tutor: George Papamarkos Topic: Devices in Linux.
1-1 NET+OS Software Group Flash API Multiple flash memory bank support New Flash API introduction Detailed Flash API Function presentation Supporting.
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.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Files and Directories File types stat functions for file information
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Lecture 3 Introduction to Unix Systems Programming: Unix File I/O System Calls.
Linux Device Driver 2009/04/08. Reference Book Another Reference Book Embedded Linux Primer: A Practical, Real-World Approach By Christopher Hallinan.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
Interfacing Device Drivers with the Kernel
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Linux File system Implementations
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
Learners Support Publications Working with Files.
I/O Software CS 537 – Introduction to Operating Systems.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Overview Dr. Xiao Qin Auburn University
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 4: Managing File System State Dr. Xiao Qin Auburn University.
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Jonathan Walpole Computer Science Portland State University
Lecture 31: Introduction to File System
Computer System Laboratory
Week 12 - Wednesday CS222.
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
Linux Kernel Driver.
Operating System Hebrew University Spring 2004
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
File Structure Related system calls
Implementation of Embedded OS
FILE I/O File Descriptors I/O Efficiency File Sharing
Lecture Topics: 11/20 HW 7 What happens on a memory reference Traps
Week 12 - Wednesday CS222.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Character Driver Issues Implementing ‘/dev/physmem’

Goals for ‘physmem’ Let user-programs ‘read’ physical memory Make this convenient for the programmer Treat physical memory as just one big file Then can use standard C library functions: i.e., open(), lseek(), read(), close() But don’t give user-programs write-access!

The ‘mknod’ command Device-file: usually created by command # mknod /dev/physmem c ‘mknod’ takes 4 command-line arguments: the pathname of the special file ‘c’ indicates it’s a character device 251 is driver’s ID-number (i.e., major) 0 is the device’s ID-number (i.e., minor) Need ‘root’ privileges to create /dev file

Default file permissions Without ‘root’ privileges, we can’t try it But if we could, we’d see file-permissions Example: # mknod /dev/xxx c # ls -l /dev/xxx crw-r—r-- 1 root root 251, 0 25 Feb dev/xxx We don’t want users to ‘write’ to physmem

Modifying file-permissions Supervisor can change file-permissions ‘chmod’ is the privileged command to use Example: # chmod 0444 /dev/xxx # ls -l /dev/xxx cr--r—r-- 1 root root 251, 0 25 Feb dev/xxx Now the device-file has ‘read-only’ access

A ‘smarter’ driver Our drivers can create their own files: syscall: mknod( pathname, mode, devid); Our drivers can modify file-permissions: syscall: chmod( pathname, mode); Our drivers can also delete their own files: syscall: unlink( pathname );

Some manifest constants Header-file: #include #define S_IFCHR #define S_IRUSR #define S_IRGRP #define S_IROTH By ‘or’-ing the latter three values, can get: #define S_IRUGO

Driver’s ‘read()’ function Must conform to standard prototype Otherwise: can’t ‘register_chrdev()’ Return-value has type ‘ssize_t’ Function takes four arguments: struct file *filp;// pointer to file-structure char*buf; // pointer to user-buffer size_tcount; // size of user-buffer loff_t*pos; // points to file-position

Algorithm for ‘read()’ Step 1: Check for an invalid request: if ( *pos >= filesize ) return –EINVAL; Step 2: Adjust ‘count’ for remaining bytes: If ( *pos + count > filesize ) count = filesize - *pos;

Algorithm (continued) Step 3: Find location of next data bytes void *from = phys_to_virt( *pos ); Step 4: Copy data into user-space buffer more = copy_to_user( buf, from, count ); Step 5: Return error if copying unfinished if ( more ) return –EFAULT;

Algorithm (conclusion) Step 6: Advance file’s current position *pos += count; Step 7: Return number of bytes copied return count;

Driver needs ‘llseek()’ ‘fileview.cpp’ checks for size of device-file Standard C library function is ‘lseek()’: filesize = lseek( fd, 0, SEEK_END ); ‘fd’ is the file-descriptor for an opened file 0 is the ‘offset’ to seek to SEEK_END says offset is from end-of-file

Default ‘llseek()’ won’t work No way for kernel to know device’s filesize So default ‘llseek()’ will give a wrong result Driver must implement its own ‘llseek()’ For ‘/dev/physmen’ with 1GB memory: FILESIZE = 0x But more generally: FILESIZE = virt_to_phys( high_memory );

Sample ‘llseek()’ in LDD text ‘llseek()’ must handle three cases: #define SEEK_SET0 #define SEEK_CUR1 #define SEEK_END2 (See author’s example-code on page 163)

Prototype for ‘llseek()’ Function has three arguments: struct file *filp;// pointer to file-structure loff_t offset;// file-offset to seek to intwhence;// where to seek from Return-value has type ‘loff_t’ (new offset)

Algorithm for ‘llseek()’ Step 1: Use ‘switch’ statement: switch( whence ) { case 0:// for SEEK_SET case 1:// for SEEK_CUR case 2:// for SEEK_END default:// should never happen! }

Actions for ‘switch’ cases case SEEK_SET: newpos = offset; break; case SEEK_CUR: newpos = file->f_pos + offset; break; case SEEK_SET: newpos = filesize + offset; break; default: return -EINVAL;

Algotithm (conclusion) Step 2: Check for illegal ‘newpos’ value: if ( newpos < 0 ) return –EINVAL; Step 3: Update ‘f_pos’ in file-structure: filp->f_pos = newpos; Step 4: Return new file-pointer position: return newpos;

‘physmem’ is read-only If device-file lacks ‘write’ permissions, then kernel won’t call driver’s ‘write()’ function So omit write-function from device-driver in order to avoid having ‘dead code’

Consider your legacy Your driver-code may have a future life! Another programmer could ‘inherit’ it Or you, yourself, may want to ‘reuse’ it Keeping ‘dead code’ will cause confusion Omitting commentary will cause frustration Need the author’s name (and contact-info) Need code’s revision-history (when/why)