[SYSK1] Linux Kernel Projects

2018 Project Assignment

Assignment 1 - Hello World

Repository

You must push your code to:

git@git.cri.epita.net:p/2019-sys/hello64-LOGIN.git

You must change LOGIN with yours. You have to upload your ssh-key to accounts.cri.epita.net.

Every exercises should be done before 31 of August.

Hello World

The main objective is to write a little bootloader+kernel code that will write on the serial line from x86_64 code.

You can use any language you want, just 1 rule, you need to try to use as little assembly code as possible. (advice: C or C++ will be simpler, but you can try anything you want).

For your build system, you can use anything you want. I’ll try to build with:

  • make
  • cmake
  • autotools

you need to output your code in hello64.

Your code will be launched with something like:

$ qemu-system-x86_64 -enable-kvm -fda hello64 -nographic -serial stdio -monitor none

In order to validate your code, you will have to link an object file to your kernel.

This will be build like this:

$ EXTRA_OBJECT=file.o make

Thie object file will be 64bit code and will try to use your printf implementation. It’ll look like this:

void win(int (*printf_ptr)(const char *fmt, ...))
{
        printf_ptr("%s\n", "Hello World");
}

It expects to have a complete printf implementation that will be able to output on the first serial line.

You’ll have to do multiple things for this. Here is a sample list of the tasks:

  • from the bootloader, you’ll have to enable the A20 line (look into int $0x15 (ax=0x2401)) in order to have 2Mb of memory
  • you will probably need to find the available ram zones. (int $0x15 (ax=0xe820))
  • you’ll need to load your code from disk (int $0x13)
  • move into 32bit mode
  • load 32bit mode code
  • enable an idt for error checking
  • move into 64bit mode
  • load 64bit idt
  • use the first serial line to display some text (out $0x3f8)
  • implement printf (look into the linux boot code)
  • call the external object code

Bonus

If you are feeling adventurous, you can also remove the firmware (your code will be loaded with -bios qemu option).

Old project

Assignment 1 - proctree()

Rules & Submission

  • We will check your branch “proctree” in your linux repository.
  • Your patches must have the prefix “proctree”, for example [PATCH proctree v2].
  • You should always be based on the linus master branch.

proctree()

For this assignment you will have to create a syscall that allow users to gather the process tree of a task:

ssize_t proctree(pid_t pid, char *buffer, size_t size);

It will dump into buffer a struct proctree for each task under pid, in dfs order (prefix). It will return the number of bytes written in buffer or -1 on error (and set errno appropriatly). All struct written must be complete. It must always try to fill the buffer.

#define PROCTREE_STATE_STOPPED  (1 << 0)
#define PROCTREE_STATE_ACTIVE   (1 << 1) /* running/wait_interruptible */
#define PROCTREE_STATE_TRACED   (1 << 2)
#define PROCTREE_STATE_ZOMBIE   (1 << 3)

struct proctree {
        __kernel_pid_t first_sibling;
        __kernel_pid_t first_child;
        __kernel_pid_t parent;
        __kernel_pid_t real_parent;
        __kernel_pid_t pid;
        __kernel_pid_t tgid;
        __kernel_uid_t uid;
        __kernel_uid_t euid;
        __kernel_gid_t gid;
        __kernel_gid_t egid;
        __kernel_pid_t tracer;
        __u32 exit_state;
        __u32 exit_code;
        __u64 sigmask;
        __u32 state; /* flags defined by PROCTREE_STATE_* */
};
  • struct proctree must be in a kernel public header usable with #include <linux/proctree.h> from userspace.

  • Pay attention to the permissions and credentials, a task can “see” another task if:

    • current task is executed by root
    • current user is the same as the pid
    • current user is allowed to ptrace this pid.
  • It must work for all architecture, and all ABIs (look into s390, ppc, mips, arm & x86 for example)

  • this syscall should be activated by CONFIG_SYSCALL_PROCTREE, enabled by default, and be protected under CONFIG_EXPERT.

  • the syscall number should be: 1081 for generic, 386 on x86 32bit, 334 for x86_64 & x32

Tests

  • You must create lava tests for all your code.
  • Your tests should cover all your code (have a look at CONFIG_GCOV_KERNEL)
  • lava tests should be in epita/proctree.yaml in your test repository.

Userland Examples

You will now have to use proctree (the example binary), you will have to create scripts/code that will output some process trees.

You will find the binary & example output here: git://git.lse.epita.fr/sys-kernel-proctree.git

[SYSK2] Linux Kernel Projects

System & Virtualization project - MyKvm

Repository

You must push your code before February 5th to:

git@git.cri.epita.net:p/2018-sys/kvm-LOGIN.git

You must change LOGIN with yours. You have to upload your ssh-key to accounts.cri.epita.net.

Assignment

Create a small vm tool using kvm. The primary goal is to be able to boot linux, with minimal support.

  • Don’t forget to add a README file explaining how to build/use your code, especially if its differ from the assignment.
  • Code that will not compile on my computer will be automatically discarded (Archlinux x86_64, up to date)
  • As usual it must contain a proper build system, and should contain some tests.

Basic command line for the tool:

my-kvm bzImage
my-kvm -h
my-kvm bzImage root=/dev/vda
my-kvm --initrd initramfs bzImage root=/dev/vda
my-kvm -m $ram --initrd initramfs bzImage console=ttyS0

Recomended Steps

  • Create a basic vm
  • Boot some code directly in PM mode
  • Add serial handling
  • Add bzImage loader
  • Add command line and initramfs after

Be sure to take your time and test every step. Ideally, you should implement some kind of multiboot support to be able to test the first steps and validate your code. With multiboot support, you’ll be able to boot your code samples with qemu too.

You will probably need to have some kind of firmware to emulate some BIOS calls that linux need/wants. Pay attention to these, and explain your approach in the README.

git://git.lse.epita.fr/users/laskar_g/minimal-linux.git contains a linux configuration with minimal support. Is it not up to date, but will be a start.

A simple example that uses kvm: example-kvm.c.