DOC PREVIEW
TAYLOR COS 421 - Syllabus

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PurposeExercisesCreating a system callCreating a system call table entryDefining a constantWriting the codeBuilding and installing your new kernelUsing your new system call1 PurposeThe purp os e of this assignment is to develop more knowledge of how to workwith the Linux kernel. Additionally, you should start to learn how the sourcecode is organized. Unfortunately, the online book you used for kernel modulesdoes not explain how to create new system calls. This is inappropriate for theaudience of the book. Fortunately, your OS book does illustrate how to do this.You can visit the book’s website or read the end of chapter two for details. Iwill provide the important details in this document, as well. Note that all pathsin this document are relative to /usr/src/linux.2 ExercisesThis assignment is broken down into two parts: modifying the kernel and ac-cessing those modifications via a user application. You should turn in the codefor both parts.2.1 Creating a system callTo create the s ystem call, you need to do three things: create an entry in thesystem call table, define a constant for that new entry location, and write thecode. With these three items you’ve got a working system call.2.1.1 Creating a system call table entryYou must edit the file in arch/i386/kernel/syscall table.S. This file is anassembly file, so it is specific to a particular architecture. That is why it islocated in the arch subdirectory. Since you are using the Intel 386 architecture,you choose the i386 directory. The directory structure under arch/i386 (or anyother architecture) mirrors the directory structure in /usr/src/linux directly.Don’t worry if you don’t understand ass embly instructions. You won’t haveto write any special code for this assignment. This file only contains the datafor various system calls. Note that the file looks something like:.dataENTRY(sys_call_table).long sys_restart_syscall /* 0 - old "setup()" system call, used for restarting */.long sys_exit.long sys_fork.long sys_read.long sys_write.long sys_open /* 5 */sys call table is an array (table) of pointers (which fit into a long datavalue) to functions that implement system calls. Each line is the next systemcall in the table. When the OS gets a system call, the user program tells the1OS which system call it would like to run with an integer n. The OS goes tothe nth element in the table and calls the function that is referenced in thatelement.You need to add a new entry to the table, so you must go to the end of thefile and add a line that looks like:.long sys_mysyscallwhere sys mysyscall is the name of the function that will implement your systemcall. You may use whatever name you wish. It is just convention for it to startwith sys and then use a descriptive name.2.1.2 Defining a constantNote that there are comments in the system call table that help you figure outwhich element you are creating at the end of table. You need to let the rest ofthe kernel know where that system call is located. For this purpose, you mustedit include/asm-i386/unistd.h. This file starts something like:#define __NR_restart_syscall 0#define __NR_ext 1#define __NR_fork 2#define __NR_read 3#define __NR_write 4This gives a symbolic constant that points to the appropriate entry in thesystem call table. At the end of this list, add your system call to this list ofdefinitions. Also, you need to increment the NR syscalls definition to nowinclude your new system call. The name of your constant should be NRfollowed by the name of the system call that is visible to user applications.Note that there is some more c ode that follows these definitions. This codeis included by applications to call the OS with the proper system call setting upthe parameters properly. For instance, there is one #define called syscall0that takes two parameters (type, and name). This creates a system call withzero parameters. It use s the defined constant to pass the right entry location tothe OS.2.1.3 Writing the codeNow, to add your system call. It is to have the following signatureint mysyscall(int flag, pid_t *pid)where flag is a boolean value that tells the system call whether to print theprocess id into the system log using printk(). The process’s id should b e storedin the pointer given in pid. Any error condition should be signaled properly viathe return value so that errno can be set by the system call handling code.This seems pretty straightforward, but there are a couple of things youshould be aware of. The biggest one is that pid is a pointer that points to some2logical address in the applications memory space. Technically, we could just usethe pointer since Linux runs in the same address space as the user applicationthat just made the system call. We must be aware of malicious users, however,that wish to break the system. They could provide us a valid pointer–not intotheir own process, but into the OS space–that would allow them to read or writememory that only the OS should know about. For this reason, Linux provideshelper functions copy from user() and copy to user() to read from and writeto user space respectively. Thes e functions return true on failure and the systemcall should give the error message -EFAULT to the calling application.Another thing to watch is where to add the code. You could add the functionto kernel/timer.c since that is where the normal system call that returns theprocess id is located. To get practice adding files to the OS, I would like youto create a brand new file. T his file should be in the kernel subdirectory sincethis is a “core” piece of functionality. You will have to modify the Makefile inthat directory to add your new file to the build procedure. The best thing to dois add it to the obj-y set of files since you will want to unconditionally compilein this new system call.The final thing to note is that this function needs to com municate with ar-chitecture specific code written in assembly language. The system call will becalled by some assembly code and return a value to that same assembly code.The calling conventions for assembly code may be different than C calling con-ventions, so you need some way to indicate to the compiler to use the assemblycalling conventions instead of the C calling conventions. gcc (which is the com-piler for the Linux kernel) does this by adding asmlinkage before the functiondefinition.2.1.4 Building and instal ling your new kernelI will not go into too much detail on this step since the


View Full Document

TAYLOR COS 421 - Syllabus

Download Syllabus
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 Syllabus 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 Syllabus 2 2 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?