Looking at kernel objects How a character-mode Linux device driver can be useful in viewing a ‘net_device’ structure.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Lecture 101 Lecture 10: Kernel Modules and Device Drivers ECE 412: Microcomputer Laboratory.
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
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.
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.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
The ATA/IDE Interface Can we write a character-mode device driver for the hard disk?
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
The ‘system-call’ interface We see how an application program can invoke privileged kernel services.
Venturing into the x86’s System Management Mode An introduction to concepts needed for writing and testing an interrupt-handler for SMIs.
Memory Management Design & Implementation Segmentation Chapter 4.
UNIX’s “grand illusion” How Linux makes a hardware device appear to be a ‘file’
Linux Memory Issues An introduction to some low-level and some high-level memory management concepts.
IA32 Paging Scheme Introduction to the Pentium’s support for “virtual” memory.
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
11/13/01CS-550 Presentation - Overview of Microsoft disk operating system. 1 An Overview of Microsoft Disk Operating System.
Character Driver Issues Implementing ‘/dev/physmem’
Timeout for some demos An instructive look at how the Linux Operating System sets up system data-structures.
SiS 315 Graphics Engine Introduction to some capabilities of graphics accelerator hardware.
Standard C Libraries Application Programmming Interface to System-Calls.
Memory Management (II)
The ‘ioctl’ driver-function On implementing ‘show’ and ‘hide’ for the SiS 315 hardware cursor.
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.
A “real” network driver? We want to construct a Linux network device-driver that is able to send and receive packets.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Chapter 3.2 : Virtual Memory
Our ‘nic.c’ module We create a ‘character-mode’ device-driver for the 82573L NIC to use in futrure experiments.
The ‘net_device’ structure A look at the main kernel-object concerned with a Linux system’s network interface controller.
Managing physical memory
Rapid prototyping of LKMs Configuring our Linux systems for the quick creation and use of loadable kernel modules.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Our ‘nic.c’ module We create a ‘character-mode’ device-driver for the 82573L NIC to use in future experiments.
The ‘ioctl’ driver-function On implementing a way to query and modify our UART’s baudrate via the ‘device-file’ abstraction.
A device-driver for Video Memory Introduction to basic principles of the PC’s graphics display.
Access to High Memory Introduction to the kernel functions ‘kmap()’ and ‘kunmap()
Introduction to the Intel x86’s support for “virtual” memory
What’s needed to transmit? A look at the minimum steps required for programming our anchor nic’s to send packets.
Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati. Writing/Registering to /proc Character Device Driver – Characteristics and functionality – Basic IO functions.
Linux Memory Management How does the Linux kernel keep track of the Virtual Memory Areas that each process uses?
Hardware-address filtering How can we send packets to just one node on our ‘anchor’ cluster?
The ‘zero-copy’ initiative A look at the ‘zero-copy’ concept and an x86 Linux implementation for the case of outgoing packets.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
An Introduction to Device Drivers Sarah Diesburg COP 5641 / CIS 4930.
File System. NET+OS 6 File System Architecture Design Goals File System Layer Design Storage Services Layer Design RAM Services Layer Design Flash Services.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Topics covered: Memory subsystem CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
Interfacing Device Drivers with the Kernel
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Sem 2v2 Chapter 5 Router Startup and Setup. A router initializes by loading the bootstrap, the operating system, and a configuration file. If the router.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Linux Kernel Development Memory Management Pavel Sorokin Gyeongsang National University
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Computer Organization
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
Memory Allocation CS 217.
Operating Systems Chapter 5: Input/Output Management
Memory Management Tasks
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Looking at kernel objects How a character-mode Linux device driver can be useful in viewing a ‘net_device’ structure

Our ‘/proc/netdevs’ pseudo-file We wrote a Loadable Kernel Module that creates a pseudo-file allowing users to see some information about the kernel’s data lo struct net_device eth0 struct net_device eth1 struct net_device

The LKM’s source-code #include char modname[ ] = “netdevs”; … MODULE_LICENSE(“GPL”); netdevs.c module_init() module_exit() my_get_info() some header-files some global data this module’s ‘payload’ function required module administration functions

User-space/Kernel-space ‘cat’ application program open read write (etc) standard runtime library operating system kernel netdevs.ko installable module LINUX privilege barrier user-space (restricted privileges) kernel-space (unrestricted privileges)

Linux device drivers There is another kind of LKM, written to control the system’s hardware devices rather than merely to expose information Its code-structure will depend on the type of hardware device it is intended to control –‘character’ devices –‘block’ devices –‘network interface’ devices

Hardware’s operations In order to write the software that controls a particular device, the programmer needs to know details about its capabilities and about its mechanisms for being controlled This information is found in programming manuals, produced by the manufacturer These manuals may or not be available to the general public (often are ‘proprietary’)

A few devices are ‘simple’ If a particular device’s operations are very simple to understand, we may not need to consult the manufacturer’s documentation (just use ‘common sense’ and guesswork) EXAMPLE: The computer system’s main memory offers us an easy-to-understand hardware component for which we can directly write a device-driver module

‘dram.c’ Two benefits of having a device-driver for the computer’s physical memory are: –We can directly look at kernel data-structures using ‘unprivileged’ application-programs –We get to see the general code-structure for Linux device-drivers in the simplest of cases

Using our ‘fileview’ utility Our previous ‘netdevs.c’ module tells us where the ‘struct net_device’ objects are located in our system’s physical memory So we can use ‘fileview’ to inspect these kernel data-structures once we’ve loaded our ‘dram.ko’ device-driver into the kernel Timeout for an in-class demonstration

The code-structure for ‘dram.c’ #include … char modname[ ] = “dram”; int my_major = 85; … MODULE_LICENSE(“GPL”); dram.c module_init() module_exit() my_read() some header-files some global data this module’s ‘payload’ (its ‘method’ functions and its ‘file_operations’ structure) required module administration functions my_llseek() my_fops

Kernel’s ‘helper-functions’ The Linux kernel provides quite a few aids to the authors of device-driver code: – ‘register_chrdev()’ and ‘unregister_chrdev()’ – ‘copy_to_user()’ and ‘copy_from_user()’ – ‘kmap()’ and ‘kunmap()’ The kernel also exports some of its ‘global variables’ (which drivers can reference): – ‘num_physpages’ and ‘mem_map[ ]’

Memory-mapping user space kernel space CPU’s virtual address-space HMA 896-MB physical RAM There is more physical RAM in our classroom’s systems than can be ‘mapped’ into the available address-range for kernel virtual addresses = persistent mapping = transient mappings

What does ‘kmap()’ do? The ‘kmap()’ helper-function allows your driver to create a temporary mapping for any one 4-KB ‘page’ of physical memory to some unused virtual address in kernel- space, then later ‘kunmap()’ lets your driver discard that mapping when it’s no longer needed (so there will be available that kernel-address for later reuse)

The ‘mem_map[ ]’ array The kernel creates an array of structures, named ‘mem_map[ ]’, whose entries hold detailed information about how each 4KB page of physical RAM is now being used The global variable named ‘phys_mem’ stores the total number of array-entries, and hence can be used by your driver to determine the amount of installed RAM

The function-prototypes void *kmap( struct page *page_ptr ); This function accepts a pointer to an entry of type ‘struct page’ in the kernel’s ‘mem_map[ ]’ array, and returns a kernel address where that page of physical RAM has been temporarily ‘mapped’ void kunmap( void *virt_addr ); This function accepts an address where the kernel temporarily has mapped a page of physical RAM and it deletes that mapping, thus freeing the address for reuse later when the kernel is asked to setup a different temporary mapping of physical RAM into kernel-space

Our driver ‘read()’ method… It has to support the traditional stream-of- bytes paradigm, so a ‘sanity check’ will be needed for the caller’s argument-values ssize_t my_read( struct file *file, char *buf, size_t count, loff_t *pos ); // There’s nothing to be ‘read’ beyond the end of physical RAM if ( *pos >= dram_size ) return 0; number of bytes that caller wants to read the current position of the file-pointer Physical RAM *pos dram_size

‘read()’ method (continued) Our driver has to accommodate the CPU’s ‘page-granular’ memory-architecture, and the ‘kmap()’ function’s ability to map one- page-at-a-time *pos intpage_number = *pos / PAGE_SIZE; intpage_indent = *pos % PAGE_SIZE; if ( page_indent + count > PAGE_SIZE ) count = PAGE_SIZE – page_indent; struct page*pp = &mem_map[ page_number ]; void*from = kmap( pp ) + page_indent; intmore = copy_to_user( buf, from, count );

Another argument-value pitfall… It is possible that the caller did not supply a large-enough buffer for the amount of data that is supposed to be transferred That potential ‘buffer-overflow’ problem could be detected during execution of the ‘copy_to_user()’ helper-function, if fewer than ‘count’ bytes can be copied without triggering a ‘segmentation violation’

The driver’s solution… The ‘copy_to_user()’ function return the number of bytes that remain to be copied (normally this is zero: all copying got done) But if it’s NOT zero, the driver’s duty is to notify the user that a ‘segmentation fault’ error occurred – but AFTER ‘kunmap()’ intmore = copy_to_user( buf, from, count ); // first unmap the page, then notify the user if necessary kunmap( pp ); if ( more ) return –EFAULT;

The ‘llseek()’ method Our ‘dram.c’ driver needs to implement its own ‘llseek()’ function, in order to allow an application-program to ‘seek’ to the end of the device-file (so it will know what total amount of physical RAM is installed) This feature is used by our ‘fileview’ tool when a user hits the -key, and to display the total size for the device-file

‘llseek()’ implementation unsigned intdram_size;// equals PAGE_SIZE * num_physpages loff_t my_llseek( struct file *file, loff_t offset, int whence ) { loff_tnewpos = -1; switch ( whence ) { case 0:newpos = offset; break; // SEEK_SET case 1:newpos = file->f_pos + offset; break; // SEEK_CUR case 2:newpos = dram_size + offset; break; // SEEK_END } if (( newpos dram_size )) return –EINVAL; file->f_pos = newpos; returnnewpos; }

Demo: ‘vwnetdev.cpp’ This application makes use of information from the ‘/proc/netdevs’ pseudo-file, plus the information that can be read from the computer’s physical memory using the capabilities implemented by our ‘dram.c’ device-driver It lets a user view the ‘struct net_device’ object for a specified network-interface

Our ‘offsets.c’ module This module creates a pseudo-file that can help a user to interpret the hexadecimal output produced by ‘vwnetdev’ It shows the locations within a ‘net_device’ structure for some structure-members of particular significance for network device drivers (which we shall explore next time)

In-class exercise #1 One of the ‘struct net_device’ fields that is significant in a Linux network device driver is the ‘get_stats’ function-pointer field Modify our ‘offsets.c’ module so that the pseudo-file this module creates will include the offset for the ‘get_stats’ member Turn in a printout of the enhanced output (created using our ‘ljpages’ printing tool); be sure your name is handwritten on it

In-class exercise #2 Take a look at our kernel’s definition for a ‘struct net_device’ object, in header-file: and identify three additional member-fields that you would like to show the offsets for Then implement the display of those three offsets (by adding code to our ‘offsets.c’)