Unformatted text preview:

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 struct net device lo struct net device eth0 struct net device eth1 The LKM s source code netdevs c some header files include linux module h include linux proc fs h some global data char modname netdevs my get info required module administration functions module init module exit MODULE LICENSE GPL this module s payload function User space Kernel space user space restricted privileges standard runtime library application program kernel space unrestricted privileges operating system kernel open read write etc cat netdevs ko privilege barrier LINUX installable module 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 dram c some header files some global data include linux module h include linux highmem h char modname dram int my major 85 my read my llseek my fops required module administration functions module init module exit MODULE LICENSE GPL this module s payload its method functions and its file operations structure 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 persistent mapping transient mappings HMA kernel space 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 user space CPU s virtual address space 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 kernelspace 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 ofbytes 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 number of bytes that caller wants to read the current position of the file pointer There s nothing to be read beyond the end of physical RAM if pos dram size return 0 Physical RAM dram size pos read method continued pos Our driver has to accommodate the CPU s page granular memory architecture and the kmap function s ability to map onepage at a time int page number pos PAGE SIZE int page indent pos PAGE SIZE if page indent count PAGE SIZE count PAGE SIZE page indent struct page void int pp mem map page number from kmap pp page indent more 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 int more copy to user buf AFTER from count kunmap 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 END key and to display the total size for the device file llseek implementation unsigned int dram size equals PAGE SIZE num physpages loff t my llseek struct file file loff t offset int whence loff t newpos 1 switch whence case 0


View Full Document

USF CS 686 - Kernel objects

Documents in this Course
Load more
Download Kernel objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Kernel objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Kernel objects and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?