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.

Slides:



Advertisements
Similar presentations
CSE 105 Structured Programming Language (C)
Advertisements

DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Linux device-driver issues
Computer System Laboratory
CSE 303 Lecture 16 Multi-file (larger) programs
Building and Running Modules Sarah Diesburg COP 5641.
Building and Running Modules Linux Kernel Programming CIS 4930/COP 5641.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Creating a device-file node An introduction to some privileged Linux system-calls (needed for an upcoming programming exercise)
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
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.
Embedded Systems Programming Writing Device Drivers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Dynamic kernel patching Could we implement a new Linux system-call without modifying any officially released kernel sources?
CS 635 Advanced Systems Programming Spring 2003 Professor Allan B. Cruse University of San Francisco.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
The ‘net_device’ structure A look at the main kernel-object concerned with a Linux system’s network interface controller.
Scientific Visualization Using imagery to aid humans in understanding a complicated phenomenon.
Rapid prototyping of LKMs Configuring our Linux systems for the quick creation and use of loadable kernel modules.
CS 635 Advanced Systems Programming Spring 2005 Professor Allan B. Cruse University of San Francisco.
CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.
Add a New System Call to Linux. Hw1 Add a New System Call to Linux and Compile Kernel Add a New System Call to Linux by Kernel Module.
Guide To UNIX Using Linux Third Edition
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 9.
1 Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
Operating System Program 5 I/O System DMA Device Driver.
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Programming With C.
Python From the book “Think Python”
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.
Kyu Ho Park Sept. 22, Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Data Display Debugger (DDD)
LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
ICOM Noack Linux kernel structure Kernel code structure How it boots itself All the system calls are available System is configured Process handling.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
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
Multiple File Compilation and linking By Bhumik Sapara.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Getting Started with the Kernel. Obtaining the Kernel Source
Building programs LinuxChix-KE. What happens in your CPU? ● It executes a small set of instructions called "machine code" ● Each instruction is just a.
Add a New System Call to Linux
OS – Ex 1 Nezer J. Zaidenberg.
Getting Started with the Kernel
Computer System Laboratory
Makefiles Caryl Rahn.
Linux Kernel Module Programming
Computer Engineering 1nd Semester
Introduction to C Programming Language
CS 6560 Operating System Design
Your first C and C++ programs
Linux Architecture Overview.
CS 6560 Operating System Design Kernel Loadable Modules
Implementation of Embedded OS
Computer System Laboratory
Compiler vs linker The compiler translates one .c file into a .o file
SPL – PS1 Introduction to C++.
The ‘asm’ construct An introduction to the GNU C/C++ compiler’s obscure syntax for doing inline assembly language.
Presentation transcript:

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

The ‘extensibility’ imperative A modern OS needs the ability to evolve It will need to support new devices It will need to allow ‘bugs’ to be repaired It will need to permit performance gains Otherwise: suffer early obsolescence!

Two Extensibility Mechanisms ‘Open Source’ programming ‘Installable’ kernel modules

‘Open Source’ Source text for the Linux kernel is on disk (usual directory-location is ‘/usr/src/linux’) Majority of the sources are written in ‘C’ (so ‘portable’ across CPU architectures) Some are written in assembly languages (for nearly twenty different architectures) Files are grouped into major categories (kernel, mm, fs, net, ipc, lib, drivers, init) You could edit and recompile your kernel

Installable kernel modules Great mechanism for kernel ‘extensibility’ Neat tool for studying how kernel works Kernel can be modified while it’s running Unnecessary to recompile and then reboot But inherently unsafe: programming bugs in the kernel can cause system crashes!

Some ‘superuser’ privileges Since modifying a running kernel is ‘risky’, only authorized ‘system administrators’ are normally allowed to install kernel modules But our systems are specially configured to permit you to install or remove modules This is purely for ‘educational’ purposes (so you should use this privilege wisely)

Linux module structure Two ‘module administration’ functions are mandatory components in every module plus Appropriate ‘module service’ functions and their supporting kernel data-structures are optional components in particular modules also Recent kernels require a Module License!

A minimal module-template #include int init_module( void ) { // code here gets called during module installation } void cleanup_module( void ) { // code here gets called during module removal } MODULE_LICENSE(“GPL”);

How to compile a module You could directly invoke the C-compiler: $ gcc –c –O –D__KERNEL__ -DMODULE –I/lib/modules/2.4.26/build/include mod.c OR: you can use the ‘make’ utility: $ make mod.o

The ‘printk()’ function Kernel modules cannot call any functions in the C runtime library (e.g., ‘printf()’) But similar kernel versions of important functions are provided (e.g., ‘printk()’) Syntax and semantics are slightly different (e.g., priority and message-destination) Capabilities may be somewhat restricted (e.g., printk() can’t show floating-point)

Simple module example #include int init_module( void ) { printk( “ Hello, world!\n” ); return 0;// SUCCESS } void cleanup_module( void ) { printk( “ Goodbye everyone\n” ); } MODULE_LICENSE(“GPL”);

How to install and remove root# /sbin/insmod hello.o root# /sbin/rmmod hello

A non-trivial module-example Let’s see how we can use kernel functions to create our own ‘/proc’ file Easy if we use ‘create_proc_read_entry()’ during module-initialization (and then use ‘remove_proc_entry()’ during cleanup) Prototypes in the header We’ll show the current value of a volatile kernel variable, named ‘jiffies’

jiffies unsigned long volatile jiffies; global kernel variable (used by scheduler) Initialized to zero when system reboots Gets incremented when timer interrupts So it counts ‘clock-ticks’ since cpu restart ‘tick-frequency’ is architecture dependent

Writing the ‘jiffies.c’ module We need to declare a name for pseudo-file static char modname[ ] = “jiffies”; We need to define a ‘proc_read’ function (see code on the following slide) We need to ‘register’ our filename, along with its ‘read’ method, in ‘init_module()’ We need to ‘unregister’ our pseudo-file in ‘cleanup_module()’

Our module’s ‘read’ method static int my_proc_read( char *buf, char **start, off_t off, int count, int *eof, void *data ) { return sprintf( buf, “jiffies=%lu\n”, jiffies ); }

Our ‘initialization’ function int init_module( void ) { create_proc_read_entry( modname, 0, NULL, my_proc_read, NULL ); return 0; }

Our ‘cleanup’ function void cleanup_module( void ) { remove_proc_entry( modname, NULL ); }

In-class exercise #1 Use an editor (e.g., ‘vi’) to create a source file in C (named ‘jiffies.c’) for your module Use the ‘make’ command to compile your module’s source-file into an object-file Use the ‘insmod’ command to install your module object-file into the running kernel Use the ‘cat’ command to display ‘jiffies’ Then use ‘rmmod’ to remove your module

Makefile We need it to automate module compiles Otherwise we type a VERY long command To compile ‘jiffies.c’ type: $ make jiffies.o Our Makefile defines some ‘implicit rules’ ‘make’ works by doing pattern-matching

Rule syntax targets … : dependencies … commands...

Pattern Rule example CC = gcc INCLUDE = /lib/modules/2.4.26/build/include CFLAGS = -c –O CFLAGS += -D__KERNEL__ -DMODULE # primary pattern rule %.o : %.c %.h $(CC) -I$(INCLUDE) $CFLAGS $< # fallback pattern rule %.o : %.c $(CC) -I$(INCLUDE) $CFLAGS $<

‘Makefile’ is on class website You can download the ‘Makefile’ from our website: Put the ‘Makefile’ in your current working directory (along with your module source) Then you can compile by typing this short command:$ make jiffies.o

In-class exercise #2 Use your knowledge of standard C library functions (e.g., open(), read(), close() ) to write an application-program (name it ‘showjifs.cpp’) which reads the information from your ‘proc/jiffies’ pseudo-file and then prints it on the screen Use a program-loop to re-read the pseudo file multiple times How rapidly does ‘jiffies’ value increase?