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© 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