Wednesday, March 26, 2014

[OSDI] Adding a New System Call in Linux Kernel

Steps

  1. modify arch/x86/kernel/syscall_table_32.S
  2. modify arch/x86/include/asm/unistd_32.h
  3. modify arch/x86/include/asm/syscalls.h
  4. modify arch/x86/kernel/Makefile
  5. add and implement arch/x86/kernel/hello.c
  6. make && sudo make modules_install && sudo make install && sudo reboot (Compile Kernel)

modify arch/x86/kernel/syscall_table_32.S

this file contains all the syscall names, so add a new line for our new system call at the end of the file
add .long sys_mycall at the end

modify arch/x86/include/asm/unistd_32.h

add #define __NR_mycall 338 before #ifdef __KERNEL__
modify #define NR_syscalls <number of system calls + 1>

modify arch/x86/include/asm/syscalls.h

the interface (declaration) of the system calls
add asm_linkage int mycall(void); after asmlinkage long sys_mmap(...);

modify arch/x86/kernel/Makefile

make sure the new system call will be compiled
add obj-y += mycall.o after obj-y := process...

add and implement arch/x86/kernel/hello.c

#include <linux/kernel.h>
#include <linux/linkage.h>
asmlinkage int sys_mycall(void) {
    printk("Hello, how are you?\n");
    return 0;
}

Test

  • modify /usr/include/asm/unistd_32.h
  • modify /usr/include/bits/syscall.h
  • write a program to test it
  • see the output in dmesg
modify /usr/include/asm/unistd_32.h
add #define __NR_mycall 341 at the end, before #endif
modify /usr/include/bits/syscall.h
add #define SYS_mycall __NR_mycall at the end of the file
write a program to test it

#include <syscall.h>#include <stdio.h>
int main() {
    int r;
    r = syscall(__NR_mycall);
    printf("return value = %d\n", r);
    return 0;
}

see the output in dmesg
simply type "dmesg" to check if the output is there

Reference

Tuesday, March 25, 2014

[OSDI] Compile Kernel (Commands)

Compile Kernel

  • cp ./.config ./.config.bk
  • cp /boot/config-<tab> ./.config
  • make menuconfig
  • make ###compiling to create a compressed kernel image
  • make modules ###compiling to kernel modules
  • sudo make modules_install ###install kernel modules
  • sudo make install ###install kernel itself

Reference

Monday, March 24, 2014

Add a New User with Home and in Sudoer

Ha, I can't memorise this one, so I am taking the note here.
  • sudo useradd -d /home/heron -m heron
  • sudo passwd heron
  • sudo adduser heron sudo
    • (Fedora) gpasswd wheel -a heron
    • (Fedora) visudo -> remove '#' in front of %wheel

Thursday, March 20, 2014

TODO and FIXME List in VIM

Programmers like to leave "TODO" or "FIXME" in comments to remind themselves that there's something unfinished or unsolved. It's not only important for reminding, but also a good way to tell notify the other co-workers: "there's something we need to fix", which is a better way instead of email/IMs.

To setup the "TODO" and "FIXME" list in VIM is slightly simple, just add the following line in ~/.vimrc:
command Todo noautocmd vimgrep /TODO\|FIXME/j ** | cw
Type ":Todo" in VIM to obtain the list in VIM.



Reference

GNU Split Screen

Notes for split screen commands

  • ctrl+a shift+\ : create new vertically split window
  • ctrl+a shift+s : create new horizontally split window
  • ctrl+a Tab : move cursor to the other window
  • ctrl+a shift+x : close current window

*Enable vertical split on Mac: http://old.evanmeagher.net/2010/12/patching-screen-with-vertical-split-in-os
* Meet bug ("pty.c:38:26: fatal error: sys/stropts.h: No such file or directory") on Fedora: http://savannah.gnu.org/bugs/?36693

Tuesday, March 18, 2014

[OSDI] Ways for Protecting Shared Data/Process Problems


Ways for protecting shared data/process problems
  1. atomic operation: http://www.threadingbuildingblocks.org/docs/help/tbb_userguide/Atomic_Operations.htm
    • apply a single variable as a lock, check it before entering critical session
  2. disable interrupt: http://stackoverflow.com/questions/3427546/what-is-meant-by-disabling-interrupts
    • sometimes it's not necessary
    • costs a lot
  3. interrupt mask
    • device drive interrupt handler
  4. program reentry
  5. disable kernel preemption
  6. spin lock
    • implemented by atomic operation
    • run for loop while it's locked

Wednesday, March 12, 2014

Create New Git Repo

Yep, this should be basic, but I always can't memorise the commands. Here, I am writing them down.

p.s. let's use git@heron.nctucs.net:~/heron-web.git for example.

On Git Server Side

  • mkdir heron-web.git
  • cd heron-web.git/
  • git init --bare

In My Local Code Directory

  • cd <path-to-the-code>
  • git init
  • vim .gitignore
  • git add -A
  • git commit -a -m "init commit"
  • git remote add original git@heron.nctucs.net:~/heron-web.git
  • git push -u original master

Clone in Other Place

  • git clone git@heron.nctucs.net:~/heron-web.git
  • cd heron-web