Programming in Kernel. And assignment #1

Similar documents
Project 1. Fast correction

Hint #1. Define a syscall

Bash exercices. Write 6 random numbers between 0 and 9 in a file named randoms then display them in a sorted way on the screen.

Operating Systems 16 - CS 323 Assignment #2

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

CS Fundamentals of Programming II Fall Very Basic UNIX

University of Colorado at Colorado Springs CS4500/ Fall 2018 Operating Systems Project 1 - System Calls and Processes

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Project 1: Syscalls for synchronization 1

System Calls (Φροντιστήριο για την 3η σειρά) Dimitris Deyannis

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

CSC209. Software Tools and Systems Programming.

Exercise 1: Basic Tools

QEMU and the Linux Kernel

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Problem Set 1: Unix Commands 1

CS354 gdb Tutorial Written by Chris Feilbach

CENG 334 Computer Networks. Laboratory I Linux Tutorial

2 Initialize a git repository on your machine, add a README file, commit and push

CSC209. Software Tools and Systems Programming.

Submitting your Work using GIT

Installing and Using Docker Toolbox for Mac OSX and Windows

CSE 451: Operating Systems. Sec$on 2 Interrupts, system calls, and project 1

git commit --amend git rebase <base> git reflog git checkout -b Create and check out a new branch named <branch>. Drop the -b

A Guide to Condor. Joe Antognini. October 25, Condor is on Our Network What is an Our Network?

CISC 220 fall 2011, set 1: Linux basics

Working with Basic Linux. Daniel Balagué

Operating Systems (234123) Spring 2017 (Homework Wet 1) Homework 1 Wet

CSE 15L Winter Midterm :) Review

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Project No. 2: Process Scheduling in Linux Submission due: April 12, 2013, 11:59pm

The Unix Shell & Shell Scripts

Part A: Setup your kernel development environment

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

A Kernel Compiling Adventure By Grant Nelson

Contents Coding standard Debugging tool Text editor Version control system

Introduction to Linux

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Spring Modern Computer Science in a Unix Like Environment CIS c

Operating Systems, Assignment 2 Threads and Synchronization

Introduction to Linux

INF322 Operating Systems

Operating Systems Linux 1-2 Measurements Background material

Reviewing gcc, make, gdb, and Linux Editors 1

CS197U: A Hands on Introduction to Unix

Lab 4: Interrupts and Realtime

Introduction to Unix: Fundamental Commands

Exercise Sheet 2. (Classifications of Operating Systems)

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Bash command shell language interpreter

Open up a terminal, make sure you are in your home directory, and run the command.

Lab 4. Out: Friday, February 25th, 2005

Introduction to remote command line Linux. Research Computing Team University of Birmingham

Linux Kernel Compilation

Section 2: Developer tools and you. Alex Mariakakis (staff-wide)

Lab #2 Physics 91SI Spring 2013

15-122: Principles of Imperative Computation

CSE 391 Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

Chapter-3. Introduction to Unix: Fundamental Commands

Introduction: What is Unix?

Using GitHub to Share with SparkFun a

Linux shell scripting Getting started *

24 Writing Your First Script

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

CS 460 Linux Tutorial

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

An introduction to git

Getting started with Hugs on Linux

BitcoinMonster Masternode Linux VPS Tutorial - Vultr VPS Created By : Samshak Donet Mon: MKX8PFz1uvBkwNDTXtUuj6KinudhsKZh1K

Computer Science 62 Lab 8

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Introduction to Lab Practicals (Lab Intro 3) Access Control, Synchronisation and Remote Access

Using RANCID. Contents. 1 Introduction Goals Notes Install rancid Add alias Configure rancid...

What s NetBeans? Like Eclipse:

CENG393 Computer Networks Labwork 1

Assignment 1: Build Environment

Introduction to the Linux Command Line

System Architecture 2008/09 Programming Assignment 3. 2 Overview on System/161 Memory Management Hardware

Compiling Software on UNIX. System Administration Decal Spring 2009 Lecture #4 George Wu Slides prepared by Joshua Kwan

Hitchhiker s Guide to VLSI Design with Cadence & Synopsys

Intro to Linux & Command Line

Project 2 Linux Kernel Hacking

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Task-Oriented Solutions to Over 175 Common Problems. Covers. Eclipse 3.0. Eclipse CookbookTM. Steve Holzner

Read Source Code the HTML Way

COMS 6100 Class Notes 3

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS 261 Recitation 1 Compiling C on UNIX

CptS 360 (System Programming) Unit 3: Development Tools

Unix. 1 tblgrant projects 0 Jun 17 15:40 doc1.txt. documents]$ touch doc2.txt documents]$ ls -l total 0

File System Interface. ICS332 Operating Systems

CSE 390a Lecture 3. Multi-user systems; remote login; editors; users/groups; permissions

A Brief Introduction to Unix

Pico Computing. M 501 / M 503 Getting Started Guide. March 7, Overview 1. 2 System Requirements 1. 3 Ubuntu Linux Configuration 2

Portions adapted from A Visual Guide to Version Control. Introduction to CVS

SD Card with Eclipse/Emulator

Transcription:

Programming in Kernel And assignment #1

How to find something in the kernel arch/x86 has every hardware-specific code for the 32- bit Intel processors. other arch/xxx directory can be ignored launch cscope -R in your target directory, use "Find this C symbol" to locate any use of a function/structure field/macro/variable... use "Find this global definition" to locate where the body of the function/structure declaration/... sits. enjoy the other features, too :) or Use an IDE In fact, it s complimentary. I use the twos

Macros Kernel programmers love macros. Macros are replaced by some piece of code by the preprocessor, and does not suffer from stack overhead of functions. if (unlikely(pagedirty(page))) { unlikely (and the contrary, likely) is a simple macro, which optimise code for the else branch of a condition (no jump). You may encounter some functions that doesn t exist anywhere Like PageDirty They are defined by another macro, but never explicitly defined : PAGEFLAG(Dirty, dirty) Will make PageDirty Available Some IDE like Eclipse (and I m told Netbeans) understand this. But not cscope, and searching the files after PageDirty will not work either.

Define For the same reasons, they love define. The.config file is a big definer. You ll encounter some things like this : #ifdef CONFIG_SMP struct plist_node pushable_tasks; #endif It means that if CONFIG_SMP is enabled in the config file, the code is taken into account, if not it s like if it never existed How to know if it s activated? host % cat.config grep CONFIG_SMP CONFIG_SMP=y

IDE As said, any file editor like gedit (or vim/nano/emacs if you like to suffer) will work. Helping you with cscope in another window is a good idea. But you may benefit from an IDE. I personally (and it s probably not the best) use Eclipse. You can CTRL-CLICK a function/macro/variable like PageDirty to jump to its definition. It s really up to you.

Working with bros Don t even try Dropbox SVN is probably the easiest. GIT is more powerful. If you have a server, you can use libapache-mod-davsvn. But there are online providers : http://offers.assembla.com for SVN and https://github.com/ for GIT. SVN quick ref : svn co http://.../os Get all the files svn update Get all changes from the server svn commit -m Scheduler can now have greenlights Send all changes to the server

Debug dump_stack() will show you all the calls which led where it is called. printk() will display a line in the syslog. It s the same arguments than syslog. It may be a good idea to launch a virtual $ tail -f /var/log/syslog in a console before doing your tests in the kernel, to see what is going on. printk(kern_crit bla bla bla will display the line in the current terminal, as if it was a very critical error (usefull for debug)

GDB If you got an error with a dump_stack like : schedule+0xc0

GDB (con t) Find the file containing the function with cscope Launch GDB on the binary file (binary.o) Type list *(function+offset) Example for the last screen : Find that schedule is in sched.c with cscope host $ cd kernel/ host $ gdb sched.o GNU gdb (GDB) Fedora 7.6.1-41.fc19 (...) Reading symbols from (...)done. (gdb) list *( schedule+0xc0) 0x18150 is in schedule (kernel/sched.c:3232). 3227 #ifdef CONFIG_SMP 3228 3229 /* assumes rq->lock is held */ 3230 static inline void pre_schedule(struct rq *rq, struct task_struct *prev) 3231 { 3232 if (prev->sched_class->pre_schedule) //The error was a NULL pointer... 3233 prev->sched_class->pre_schedule(rq, prev); 3234 } 3235 (gdb) quit

References https://www.kernel.org/doc/documentation http://www.win.tue.nl/~aeb/linux/lk/lk.html Scheduler : http://joshaas.net/linux/linux_cpu_scheduler.pdf References : http://isis.poly.edu/kulesh/stuff/src/klist/ You will find a lot of outdated documentation. The kernel evolve slowly, but pay attention to the version they re talking about. The scheduler is quite the same since 2.6.8.1 and the introduction of the completly fair scheduler.

It s too slow In order Do not compile in virtual machine Are you using -j(ncores + 1) to compile on all cores? Do not rebuild modules! Juste make ARCH=i386 bzimage -j5 Do not copy them each time. You have to do the build&copy only when you change the kernel.config. Normally never... Make shell script to compile your kernel, export variables, install, push files by ssh and probably another script in the virtual machine to copy thoses files, update initramfs, update grub, A recompilation should take two or fress shell lines from you. I personnaly pushed the vice to making bash alias which are s to launch sshfs and log, k to update the kernel and l to launch my test program... Buy a SSD Buy a new processor (Core i5 or i7), but the SSD will make the greatest price/speed tradeoff

First assignment : syscall Goal Add a system call setgreentask(pid_t pid) that mark a task by its pid as the green task You will give informations by using printk about it in some places/event : [GREEN] Green task is now %d (where %d is the pid) when you set a task as green [GREEN] Green task is picked when the scheduler is giving the green task to the main scheduler function. [GREEN] Green task is enqueued When the task is enqueued into any scheduler [GREEN] Green task is dequeued When the task is dequeued out of any scheduler [GREEN] Green task is back into fair scheduler When the fair scheduler (and only that one) is informed that it should make accounts for a un-scheduled task.

sys_setgreentask(pid_t pid) is assigned the first available syscall number in the 3.2.35 kernel. receive the pid of a task (can be the same task than the task which make the syscall or a different) stores the green task as a global variable called greentask Make adequate checks to be sure that the arguments make sense It returns -1 if a problem occurred, and should use printk to tell something about it It returns 1 if a task was already a green task, without replacing it (should use printk too) It returns 0 if all went okay setgreentask(0) is used to un-green the current task

Submit a patch that can be applied to the reference Linux 3.2.35 source code to get your modified source code. add comments to the existing code to document your discoveries. (no report asked) mail your g{groupnr}-source.patch.gz to tom.barbette@ulg.ac.be by the 04/03/2014 23:59 from your ULg email. with [INFO-0940] Assignment #1 as subject. include your co-worker's e-mail address in CC.

Obligations Respect the exact strings for printk, and the exact procedure for mail. Stay on my virtual image, with the 32bit ubuntu OS, and one virtual processor. But I will never ask you the image, do what you want on it. The patch have to be clean! No adding of file~, of.o or any other unwanted files. Check it with a text editor before sending it! But you can leave your comments about discoveries like //okay, it s here the scheduler remove a task from the queue

How to make your patch 1. copy candidate files: rsync --exclude "cscope.out" --exclude "scripts/genksyms/*.*.[ch]" -- exclude ".tmp_*" --exclude "*.o" --exclude "*.ko" -av working-3.2.0 release-3.2.0 2. cleanup any trailing binary: cd release-3.2.0 ; make ARCH=i386 distclean 3. install reference sources nearby (tar jxvf linux-source-3.2.0.tar.bz2) 4. make the patch: diff -burn linux-source-3.2.0 release-3.2.0 > group42.patch (you can test your patch with patch -p1 --dry-run <../group42.patch in linuxsource-3.2.0) (you no longer need linux-source-3.2.0 nor release-3.2.0...) Remember to check that the patch is clean. You can also try things make ARCH=i386 mrproper or make ARCH=i386 clean, check the doc...

How to pick a System call number arch/x86/include/asm/unistd_32.h has #defines for every syscall, plus NR_syscalls. That's for C programs. arch/x86/kernel/syscall_table_32.s provides pointer to each sys_{syscallname} entry point in the kernel. you must declare your handler with asmlinkage int sys_{syscallname}({args}) NOTE: user-space entries have to be double-checked! A BSOD cannot occur on a modern computer, you can t say It s the user which misused my syscall! new syscalls always affect the core kernel, and can't be defined simply through a module.

Using a syscall in a C program #include <sys/syscall.h> int main() {... int result = syscall(249, arg1, arg2, arg2);... } where 249 is the number chosen. Result is the value your syscall will return. More info? man syscall. Without knowing it, when programming you call functions which call syscall You use it all the time!

Word about ms8xx Account : www.student.montefiore.ulg.ac.be You ve got public space in /virtualbox, just make a folder group0x in it. You can use /tmp too (but /tmp will be emptied at each reboot ) ls -l reports permissions for owner, group and others as rwxr-xr-- on ms8xx, you're all in the same group! chmod go-rwx file restricts all access i.e. permissions become rw------- for a file. getfacl file lists additional control list setfacl -m u:jerry:rw- would grant me (user: jerry) read/write access to an otherwise restricted resource. if that's not crystal-clear to you, better check the manpages or http://computernetworkingnotes. com/managing-file-system-security/acl.html