OS161: Unknown syscall -1

When working on OS161 system calls, you'll probably see a bunch of this error, especially you haven't implemented _exit syscall and try to do some basic user programs, e.g., p /bin/true.

Note, this problem has been fixed in OS/161 version 1.99.07.

more ...



OS161: Arguments Passing in System Call

One principle of kernel programming is that: do not trust anything users passed in. Since we assume that users are bad, they will do anything they can to crash the kernel (just as $OS161_SRC/user/testbin/badcall/badcall.c do). So we need pay special attention to the arguments of the system calls, especially the pointers.

more ...




OS161 File Operation Overview

In user space, when open a file, user program will get a file descriptor (a integer) that represent that file. User can use this descriptor to perform various operations on this file: read, write, seek, etc. As I see it, this design is quite clean in that:

  • Hide most of the details from user, for both safety and simplicity

  • Enable more high level abstraction: everything (socket, pipe..) is a file

The file descriptor is actually an index to kernel space structure that contains all the details of opened files. So at kernel side, we need to do a lot bookkeeping stuff.

more ...


OS161 pid Management

There are many way to manage each process's pid. Here is the way I do it.

I decided to make minimal modification to $OS161_SRC/kern/thread/thread.c, in case anything is ruined. So I only add two things to the thread module. One is I add a t_pid field to struct thread so that getpid system call is trivial. Another is I add a call of pid_alloc in thread_alloc to initialize new thread's t_pid. That's it. No more touch on the thread module.

more ...

OS161 execv System Call

Basically, execv do more or less the same thing with runprogram in $OS161_SRC/kern/syscall/runprogram.c. The overall flow of sys_execv are:

  1. Copy arguments from user space into kernel buffer
  2. Open the executable, create a new address space and load the elf into it
  3. Copy the arguments from kernel buffer into user stack
  4. Return user mode using enter_new_process
more ...

OS161 fork System Call

If you're not already familiar with UNIX fork system call, here is it's function description and its entry on Wikipedia.

Basically, in sys_fork, we need to do the follow things:

  1. Copy parent's trap frame, and pass it to child thread
  2. Copy parent's address space
  3. Create child thread (using thread_fork)
  4. Copy parent's file table into child
  5. Parent returns with child's pid immediately
  6. Child returns with 0

So, let's get started.

more ...