HW3: System Calls

This homework asks you to extend the xv6 kernel with several simple system calls.

You will program the xv6 operating system, so you should use the same setup as for the HW2: Xv6 boot.

Part 1 (40%): Memtop system call

In this part of the homework you will add a new system call to the xv6 kernel. The main point of the exercise is for you to see some of the different pieces of the system call machinery.

Your new system call will print the stats about available and used system memory process.

Specifically, your new system call will have the following interface:

 int memtop(); 
It takes no arguments and returns the amount of memory available in the systm. When you invoke it from your test program mtop should print the number in bytes on the console:
$ mtop
available memory: 29712384
In order to test your system call you should create a user-level program mtop that calls your new system call. In order to make your new mtop program available to run from the xv6 shell, look at how other programs are implemented, e.g., ls and wc and make appropriate modifications to the Makefile such that the new application gets compiled, linked, and added to the xv6 filesystem.

When you're done, you should be able to invoke your mtop program from the shell. You can follow the following example template for bt.c, but feel free to extend it in any way you like:

#include "types.h"
#include "stat.h"
#include "user.h"

int
main(int argc, char *argv[])
{
  /* Syscall invocation here */

  exit();
}

In order to make your new mtop program available to run from the xv6 shell, add _mtop to the UPROGS definition in Makefile.

Your strategy for making the memtop system call should be to clone all of the pieces of code that are specific to some existing system call, for example the "uptime" system call or "read". You should grep for uptime in all the source files, using grep -n uptime *.[chS].

Some hints

To count up the available system memory, you should walk the linked list used by the memory allocator and count how many pages are still available on that list.

Part 2 (60%): Print memory stats for each process running in the system

Extend your mtop tool with support for listing used memory information for all processes running on the system. For that you should implement yet another system call getmeminfo() that returns the amount of memory allocated by the process with a specific process id (pid). Specifically, your new system call will have the following interface:

 int getmeminfo(int pid, char *name, int len); 
Where pid is the process id of the process for which you're trying to get the memory usage, name is the pointer to a buffer where the system call can copy the name of the process with that pid, and len is the length of the name buffer. The system call returns the number of bytes used by the process.

You will have to query the system repeatedly for all running processes in the system. Note that you don't know the process ids of all running processes in the system. However, there is a trick: in xv6 pids are monothonically growing starting from 1 to 2147483648 (the max positive interger value). Hence you can detect the max pid at the moment by forking a new process and capturing the pid of that new process. Then you can invoke your getmeminfo system call for all pids from 1 to the pid that you've got. For each existing process you should count up the number of pages allocated for the process. For that you should recursively walk the page table of the process and count up all allocated pages. The mtop program should call your new getmeminfo() system call.

When you're done, typing mtop in the xv6 shell prompt should print all processes running in the system and their memory usage.

$ mtop
available memory: 29712384
pid: 1, name: init, mem: 319488
pid: 2, name: sh, mem: 315392
pid: 5, name: mtop, mem: 307200
...

Some hints

To walk the process page table you should implement a function similar to walkpgdir().

Submit

Submit your answers on Canvas HW3 System calls as a compressed tar file of your xv6 source tree (after running make clean). You can use the following command to create a compressed tar file (if you submit extra credit assignments, put a short hw3.txt readme file describing what you've done inside your archive).

openlab$ cd xv6-public
openlab$ make clean
openlab$ cd ..
openlab$ tar -czvf hw4.tgz xv6-public
Updated: May, 2019