Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati. Kernel Module Writing/Registering to /proc FS Payload of kernel module Reading CMOS Data – Real Time CMOS.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

CT213 – Computing system Organization
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.
Computer System Laboratory
Devices and Drivers (Chapter 7) Khattab Alhabashi UNIX System Administration.
USERSPACE I/O Reporter: R 張凱富.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Avishai Wool lecture Introduction to Systems Programming Lecture 8 Input-Output.
04/14/2008CSCI 315 Operating Systems Design1 I/O Systems Notice: The slides for this lecture have been largely based on those accompanying the textbook.
On working with LKMs Using Linux Kernel Modules to quickly export privileged kernel information to ordinary users.
How to make a pseudo-file As a follow-up to our first lab, we examine the steps needed to create our own ‘/proc’ file.
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
Firewall Lab Zutao Zhu 02/05/2010. Outline Preliminaries getopt LKM /proc filesystem Netfilter.
1 Device Management The von Neumann Architecture System Architecture Device Management Polling Interrupts DMA operating systems.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati. Writing/Registering to /proc Character Device Driver – Characteristics and functionality – Basic IO functions.
1 I/O Management in Representative Operating Systems.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Copyright ©: Nahrstedt, Angrave, Abdelzaher
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
Linux Device Driver NCTU OS Lab Chi-Wei Yang. Outline b Overall Architecture b Character Driver b Block Driver b Network Driver b Support Functions.
M. Muztaba Fuad Advanced Operating System Project Device Drivers.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Operating System Program 5 I/O System DMA Device Driver.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 8-1: I/O Management Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Segmentation & O/S Input/Output Chapter 4 & 5 Tuesday, April 3, 2007.
CSC 322 Operating Systems Concepts Lecture - 25: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Hardware Definitions –Port: Point of connection –Bus: Interface Daisy Chain (A=>B=>…=>X) Shared Direct Device Access –Controller: Device Electronics –Registers:
I/O Systems I/O Hardware Application I/O Interface
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
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.
Device Drivers CPU I/O Interface Device Driver DEVICECONTROL OPERATIONSDATA TRANSFER OPERATIONS Disk Seek to Sector, Track, Cyl. Seek Home Position.
OS2014 PROJECT 2 Supplemental Information. Outline Sequence Diagram of Project 2 Kernel Modules Kernel Sockets Work Queues Synchronization.
Chapter 13 – I/O Systems (Pgs ). Devices  Two conflicting properties A. Growing uniformity in interfaces (both h/w and s/w): e.g., USB, TWAIN.
Interfacing Device Drivers with the Kernel
I/O, Devices & Device Drivers I/O subsystem It is the responsibility of the I/O subsystem of an OS to schedule, manage, & control the interactions between.
4P13 Week 12 Talking Points Device Drivers 1.Auto-configuration and initialization routines 2.Routines for servicing I/O requests (the top half)
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
Device Driver Concepts Digital UNIX Internals II Device Driver Concepts Chapter 13.
Chapter 13: I/O Systems Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Jan 2, 2005 I/O through system calls Protection.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 2.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 12: I/O Systems I/O hardwared Application I/O Interface Kernel I/O.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Introduction to Operating Systems Concepts
Module 12: I/O Systems I/O hardware Application I/O Interface
Chapter 13: I/O Systems Modified by Dr. Neerja Mhaskar for CS 3SH3.
Chapter 12: File System Implementation
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
CS703 - Advanced Operating Systems
Chapter 2: The Linux System Part 2
I/O Systems I/O Hardware Application I/O Interface
Operating System Concepts
13: I/O Systems I/O hardwared Application I/O Interface
CS703 - Advanced Operating Systems
2017 Jan Sun Mon Tue Wed Thu Fri Sat
Chapter 5: I/O Systems.
Jan Sun Mon Tue Wed Thu Fri Sat
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Computer System Laboratory
Loadable Kernel Modules
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
Module 12: I/O Systems I/O hardwared Application I/O Interface
Presentation transcript:

Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati

Kernel Module Writing/Registering to /proc FS Payload of kernel module Reading CMOS Data – Real Time CMOS Clock Type of devices and drivers – Character – Block – Network/Stream

Kernel and Device driver Device Driver Types CMOS Reading CMOS Clock and printing at /proc

application standard “runtime” libraries standard “runtime” libraries call ret user spacekernel space Operating System kernel Operating System kernel syscall sysret module Linux allows us to write our own installable kernel modules and add them to a running system call ret

#include // for printk() int init( void ){ printk( "\n Kello, everybody! \n\n" ); return 0; } void exit( void ){ printk( "\n Goodbye now... \n\n" ); } MODULE_LICENSE("GPL"); module_init(init); module_exit(exit); #include // for printk() int init( void ){ printk( "\n Kello, everybody! \n\n" ); return 0; } void exit( void ){ printk( "\n Goodbye now... \n\n" ); } MODULE_LICENSE("GPL"); module_init(init); module_exit(exit);

We can write code to implement our own ‘pseudo’ files, located in ‘/proc’ directory We do this by adding a ‘payload’ function to a Linux Kernel Module, and by including calls to special kernel-functions within our module- init and our module-exit routines These special kernel-functions serve to ‘register’, and ‘unregister’, our payload

When an application-program (like ‘mycat’) tries to read our pseudo-file, the kernel will call our ‘get_info()’ function, passing it four function arguments -- and will expect it to return an integer value: int get_info( char *buf, char **start, off_t off, int count ); pointer to a kernel buffer current file-pointer offset pointer (optional) to module’ own buffer size of space available in the kernel’s buffer function should return the number of bytes it has written into its buffer

The kernel provides a function you module can call to print formatted text into a buffer It resembles a standard C library-function: int sprintf( char *dstn, const char *fmt, ); pointer to destination formatting specification string list of the argument-values to format will return the number of characters that were printed to the destination-buffer int len = sprintf( buf, “count = %d \n”, count ); Example:

Your module-initialization function should ‘register’ the module’s ‘get_info()’ function: create_proc_info_entry( modname, 0, NULL, get_info ); Your cleanup should do an ‘unregister’: remove_proc_entry( modname, NULL ); the name for your proc file the file-access attributes (0=default) directory where file will reside (NULL=default) function-pointer to your module’s ‘callback’ routine file’s name directory

#include // for init_module() #include // for create_proc_info_entry() #include // for inb(), outb() char modname[] = "cmos"; unsigned char cmos[10]; char *day[] = { "", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" }; char *month[] = { "", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; #include // for init_module() #include // for create_proc_info_entry() #include // for inb(), outb() char modname[] = "cmos"; unsigned char cmos[10]; char *day[] = { "", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" }; char *month[] = { "", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

Current seconds Alarm seconds Current minutes Alarm minutes Current hours Alarm hours Day-of-the-Week Date of the Month Current Month Current Year 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 Range is Range is or Range is 1..7 (Sunday=7) Range is Range is (January=1) Range is 0..99

int my_get_info( char *buf, char **start, off_t off, int count ) { ……. // show the current time and date len += sprintf( buf+len, "CMOS Real-Time Clock:\ %02X:%02X:%02X on ", mos[4], cmos[2],cmos[0]); // cur h,m,s len += sprintf( buf+len, " %s, ", day[ cmos[6] ] ); // day-name len += sprintf( buf+len, "%02X", cmos[7] ); // day-number // convert 'cmos[ 8 ]' from BCD-format to integer-format month_index = ((cmos[ 8 ] & 0xF0)>>4)*10 + (cmos[ 8 ] & 0x0F); len += sprintf( buf+len, " %s", month[ month_index ] ); // month-name len += sprintf( buf+len, " 20%02X\n ", cmos[9] ); // year-number return len; } int my_get_info( char *buf, char **start, off_t off, int count ) { ……. // show the current time and date len += sprintf( buf+len, "CMOS Real-Time Clock:\ %02X:%02X:%02X on ", mos[4], cmos[2],cmos[0]); // cur h,m,s len += sprintf( buf+len, " %s, ", day[ cmos[6] ] ); // day-name len += sprintf( buf+len, "%02X", cmos[7] ); // day-number // convert 'cmos[ 8 ]' from BCD-format to integer-format month_index = ((cmos[ 8 ] & 0xF0)>>4)*10 + (cmos[ 8 ] & 0x0F); len += sprintf( buf+len, " %s", month[ month_index ] ); // month-name len += sprintf( buf+len, " 20%02X\n ", cmos[9] ); // year-number return len; }

int my_get_info( char *buf, char **start, off_t off, int count ) { int i, len = 0; int month_index; // input and store the first ten CMOS entries for (i = 0; i < 10; i++) { outb( i, 0x70 ); cmos[i] = inb( 0x71 ); } ………………….. int my_get_info( char *buf, char **start, off_t off, int count ) { int i, len = 0; int month_index; // input and store the first ten CMOS entries for (i = 0; i < 10; i++) { outb( i, 0x70 ); cmos[i] = inb( 0x71 ); } ………………….. Here’s “C” code to read the N-th location: { intdatum;// storage for a CMOS data-value outb( N, 0x70 );// select cell number N for access datum = inb( 0x71 );// input value from selected location } Here’s “C” code to read the N-th location: { intdatum;// storage for a CMOS data-value outb( N, 0x70 );// select cell number N for access datum = inb( 0x71 );// input value from selected location }

static int __init my_init( void ){ printk( " \nInstalling \'%s\' module\n", modname ); create_proc_read_entry( modname, 0, NULL, my_get_info, NULL ); return 0; //SUCCESS } static void __exit my_exit(void ) { remove_proc_entry( modname, NULL ); printk( " Removing \'%s\' module\n", modname ); } module_init( my_init ); module_exit( my_exit ); MODULE_LICENSE("GPL"); static int __init my_init( void ){ printk( " \nInstalling \'%s\' module\n", modname ); create_proc_read_entry( modname, 0, NULL, my_get_info, NULL ); return 0; //SUCCESS } static void __exit my_exit(void ) { remove_proc_entry( modname, NULL ); printk( " Removing \'%s\' module\n", modname ); } module_init( my_init ); module_exit( my_exit ); MODULE_LICENSE("GPL");

A device driver is a kernel module responsible for managing low-level I/O operations for a particular hardware device. Block device drivers manage devices with physically addressable storage media, such as disks. All other devices are considered character devices

VFS (Virtual File System) Driver Types – Basic: Character (ADC, Line printer) Block (SCSI DISK) Network – Special Stream Terminal

System Call Interface VFS File System Buffer Cache Buffer Cache Block Device Driver Block Device Driver Character Device Driver Character Device Driver Network Device Driver Network Device Driver Network Protocol Network Protocol Socket Hardware

Operation Modes – polling – interrupt – DMA

All drivers are required to implement the loadable module entry points – init () // (load) – finialise () //unload – info () // Gather information of device Drivers should allocate and initialize any global resources in init() and release their resources in finilise().

Device Properties – can’t be randomly accessed – can’t be buffered – usually are slow devices Export Interface – file_operations Data Flow in read/write

Character device drivers normally perform I/O in a byte stream. Examples of devices using character drivers include tape drives and serial ports. Character device drivers can also provide additional interfaces not present in block drivers, – I/O control (ioctl) commands – memory mapping – device polling.

FunctionMeanings Lseekto change the current read/write position in a file Readto retrieve data from the device WriteSends data to the devic ReaddirNULL for device files; reading dirs & only useful to FS. Pollback end of two system calls, poll and select, used to inquire a device is readable or writable or in some special state Ioctlto issue device-specific commands Mmapto request a mapping of device mem to a process's address space Openfirst operation performed on the device file Flush.. Lock.. Release.. Fsync.. Fasync..

Block Devices that support a file system are known as block devices. Drivers written for these devices are known as block device drivers. Block device drivers take a file system request, in the form of a buffer structure, – And issue the I/O operations to the disk to transfer the specified block. The main interface to the file system is the strategy routine

Device Properties – can be randomly accessed – accessed in units of blocks – to speed up access, buffer cache is used Export Interface – file_operations – request_fn() Data Flow in read/write

Processes Read/Write System Call Buffer Cache Invoke Request Function Check buffer upto date Done Call Scheduler Call Scheduler

Drivers for character and block devices export a cb_ops structure which defines the driver entry points for block device access and character device access. Both types of drivers are required to support open and close.

Block drivers are required to support strategy, while character drivers can choose to implement whatever mix of – read, write, ioctl, mmap, or devmap – These entry points as appropriate for the type of device. Character drivers can also support a polling interface through – ch_poll – as well as asynchronous I/O through aread and awrite.

STREAMS is a separate programming model for writing a character driver. Devices that receive data asynchronously (such as terminal and network devices) are suited to a STREAMS implementation. STREAMS device drivers must provide the loading and autoconfiguration support

Drivers are required to implement these Entry points for device autoconfiguration. – Attach – Detach, – Getinfo Drivers might need to implement probe if the driver supports devices that are not self identifying, such as SCSI target devices

Export Interface Data Flow in read/write

User Application Berkley Socket API INET Socket TCP UDP IP ARP ICMP Network Device Driver Network Device Transmit Data: Xmit Receive Data Int. Handler Receive Data Int. Handler

I/O ports reservations – request_region() Memory Allocations – kmalloc(), vmalloc(), get_free_page() Interrupt Handler Registration – request_irq() Data Transfer between User/Kernel – memcpy_fromfs()

Download mmake.cpp and cmos.c Course Website, tested on Fedora 12 Compile mmake.cpp using ‘make’ Then compile cmos.c using ‘make’ Install ‘cmos.ko’ (and see printk-message) See $cat /proc/cmos