Lectures‎ > ‎


Modifying xv6

  • Adding a user-level program
  • Adding a new system call

Adding a User-Level Program to xv6

  • In Makefile
    • Add <progname> to UPROGS variable
    • Use _<progname> \ (\ at the end of the line)
    • E.g., _hello \
    • Add <progname>.c to EXTRA variable
  • Create <program>.c in xv6 source directory
    • Follow include file and system call usage examples found in the other user programs such as cat.c and wc.c
  • Run
    • make
  • See if your program is in the file system
    • make qemu

Adding a System Call to xv6

  • Create a new system call number using the next highest number available found in syscall.h
    • E.g., #define SYS_mygetpid 22
  • Add a user level prototype in user.h
    • E.g., int mygetpid(void);
  • Add user-level assembly stub code in usys.S
    • E.g., SYSCALL(mygetpid)
  • Add kernel-level stub in syscall.c
    • E.g., extern int sys_mygetpid(void)
  • Add pointer to kernel syscall handler to syscalls array:
    • E.g., [SYS_mygetpid] sys_mygetpid,
  • Put your implementation of sys_mygetpid in sysproc.c (this is where sys_getpid() resides, other system calls may go in different files).
  • Write a user-level program that uses your new system call
    • Include proper .h files

hello.c code

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

int main(int argc, char **argv) 

  printf(1, "Hello, World!\n"); 
  printf(1, "getpid() = %d\n", getpid()); 
  printf(1, "mygetpid() = %d\n", mygetpid()); 

Adding a the halt system call

Add halt.c to Makefile as a user level prog:

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

int main(int argc, char *argv[]) 


Add SYS_halt following directions above.

Here is sys_halt (put into sysproc.c):

  const char s[] = "Shutdown";
  const char *p;

  outw( 0xB004, 0x0 | 0x2000 );

  for (p = s; *p != '\0'; p++)
    outb (0x8900, *p);

  return 0;