xv6-riscv API

Public kernel routines, user library calls, syscall handlers, and user commands, with links into the source explorer.

API map

├── Small Unix-like kernel
│   ├── Processes and scheduler
│   ├── Context switching
│   ├── Virtual memory
│   ├── Traps and interrupts
│   ├── System-call dispatcher
│   ├── File descriptors
│   ├── Pipes
│   ├── File system and log
│   ├── Locks
│   ├── Device drivers
│   └── String and memory
├── Small user-space C library
│   ├── String and memory
│   │   ├── strlen()
│   │   ├── strcpy()
│   │   ├── strcmp()
│   │   ├── memset()
│   │   ├── memmove()
│   │   ├── memcpy()
│   │   └── memcmp()
│   ├── Input and output
│   │   ├── printf()
│   │   ├── fprintf()
│   │   └── gets()
│   ├── Memory allocation
│   │   ├── malloc()
│   │   ├── free()
│   │   ├── sbrk()
│   │   └── sbrklazy()
│   └── Utilities
│       ├── atoi()
│       ├── stat()
│       └── strchr()
├── 21 Unix-like system calls
│   ├── Process
│   │   ├── fork()
│   │   ├── exit()
│   │   ├── wait()
│   │   ├── kill()
│   │   ├── exec()
│   │   └── getpid()
│   ├── Memory and time
│   │   ├── sbrk()
│   │   ├── pause()
│   │   └── uptime()
│   ├── File descriptors
│   │   ├── pipe()
│   │   ├── read()
│   │   ├── fstat()
│   │   ├── dup()
│   │   ├── write()
│   │   └── close()
│   └── File system
│       ├── chdir()
│       ├── open()
│       ├── mknod()
│       ├── unlink()
│       ├── link()
│       └── mkdir()
├── Small Unix user environment
│   └── Commands and tests
│       ├── init
│       ├── sh
│       ├── cat
│       ├── echo
│       ├── grep
│       ├── ls
│       ├── wc
│       ├── mkdir
│       ├── rm
│       ├── ln
│       └── test programs
└── Header guide
    ├── Function declarations
    │   ├── kernel/defs.h
    │   └── user/user.h
    ├── Inline functions
    │   └── kernel/riscv.h
    ├── Types and structs
    │   ├── kernel/types.h
    │   ├── kernel/proc.h
    │   ├── kernel/file.h
    │   ├── kernel/fs.h
    │   ├── kernel/buf.h
    │   ├── kernel/spinlock.h
    │   ├── kernel/sleeplock.h
    │   ├── kernel/elf.h
    │   ├── kernel/virtio.h
    │   └── kernel/vm.h
    ├── Constants and macros
    │   ├── kernel/param.h
    │   ├── kernel/memlayout.h
    │   ├── kernel/syscall.h
    │   └── kernel/fcntl.h
    └── ABI shared with user space
        └── kernel/stat.h

Header guide

HeaderKindProvidesSymbols
kernel/defs.hFunctionsKernel internal function declarations.binit(), bread(), brelse(), bwrite(), bpin(), bunpin(), consoleinit(), consoleintr(), consputc(), kexec(), filealloc(), fileclose(), filedup(), fileinit(), fileread(), filestat(), filewrite(), fsinit(), dirlink(), dirlookup(), ialloc(), idup(), iinit(), ilock(), iput(), iunlock(), iunlockput(), iupdate(), namecmp(), namei(), nameiparent(), readi(), stati(), writei(), itrunc(), ireclaim(), kalloc(), kfree(), kinit(), initlog(), log_write(), begin_op(), end_op(), pipealloc(), pipeclose(), piperead(), pipewrite(), printf(), panic(), printfinit(), cpuid(), kexit(), kfork(), growproc(), proc_mapstacks(), proc_pagetable(), proc_freepagetable(), kkill(), killed(), setkilled(), mycpu(), myproc(), procinit(), scheduler(), sched(), sleep(), userinit(), kwait(), wakeup(), yield(), either_copyout(), either_copyin(), procdump(), swtch(), acquire(), holding(), initlock(), release(), push_off(), pop_off(), acquiresleep(), releasesleep(), holdingsleep(), initsleeplock(), memcmp(), memmove(), memset(), safestrcpy(), strlen(), strncmp(), strncpy(), argint(), argstr(), argaddr(), fetchstr(), fetchaddr(), syscall(), trapinit(), trapinithart(), prepare_return(), uartinit(), uartintr(), uartwrite(), uartputc_sync(), uartgetc(), kvminit(), kvminithart(), kvmmap(), mappages(), uvmcreate(), uvmalloc(), uvmdealloc(), uvmcopy(), uvmfree(), uvmunmap(), uvmclear(), walk(), walkaddr(), copyout(), copyin(), copyinstr(), ismapped(), vmfault(), plicinit(), plicinithart(), plic_claim(), plic_complete(), virtio_disk_init(), virtio_disk_rw(), virtio_disk_intr(), NELEM
user/user.hFunctionsUser syscall and small C library declarations.SBRK_ERROR, fork(), exit(), wait(), pipe(), write(), read(), close(), kill(), exec(), open(), mknod(), unlink(), fstat(), link(), mkdir(), chdir(), dup(), getpid(), sys_sbrk(), pause(), uptime(), stat(), strcpy(), strchr(), strcmp(), gets(), strlen(), memset(), atoi(), memcmp(), sbrk(), sbrklazy(), fprintf(), printf(), malloc(), free()
kernel/riscv.hInline functions + constantsCSR helpers, page-table constants, PTE helpers, and interrupt-control primitives.r_mhartid(), MSTATUS_MPP_MASK, MSTATUS_MPP_M, MSTATUS_MPP_S, MSTATUS_MPP_U, r_mstatus(), w_mstatus(), w_mepc(), SSTATUS_SPP, SSTATUS_SPIE, SSTATUS_UPIE, SSTATUS_SIE, SSTATUS_UIE, r_sstatus(), w_sstatus(), r_sip(), w_sip(), SIE_SEIE, SIE_STIE, r_sie(), w_sie(), MIE_STIE, r_mie(), w_mie(), w_sepc(), r_sepc(), r_medeleg(), w_medeleg(), r_mideleg(), w_mideleg(), w_stvec(), r_stvec(), r_stimecmp(), w_stimecmp(), r_menvcfg(), w_menvcfg(), w_pmpcfg0(), w_pmpaddr0(), SATP_SV39, MAKE_SATP, w_satp(), r_satp(), r_scause(), r_stval(), w_mcounteren(), r_mcounteren(), r_time(), intr_on(), intr_off(), intr_get(), r_sp(), r_tp(), w_tp(), r_ra(), sfence_vma(), PGSIZE, PGSHIFT, PGROUNDUP, PGROUNDDOWN, PTE_V, PTE_R, PTE_W, PTE_X, PTE_U, PA2PTE, PTE2PA, PTE_FLAGS, PXMASK, PXSHIFT, PX, MAXVA
kernel/types.hTypesFixed-width integer aliases.ushort, uchar, pde_t
kernel/proc.hTypesCPU, context, trapframe, process state, and process structures.context, cpu, trapframe, procstate, proc
kernel/file.hTypes + macrosOpen file, inode, device-switch, and device-number helpers.file, major, minor, mkdev, inode, devsw, CONSOLE
kernel/fs.hTypes + constantsOn-disk file-system layout, inode format, and directory entry format.ROOTINO, BSIZE, superblock, FSMAGIC, NDIRECT, NINDIRECT, MAXFILE, dinode, IPB, IBLOCK, BPB, BBLOCK, DIRSIZ, dirent
kernel/buf.hTypesBuffer-cache block structure.buf
kernel/spinlock.hTypesSpinlock structure.spinlock
kernel/sleeplock.hTypesSleeplock structure.sleeplock
kernel/stat.hABI types + constantsFile type constants and the stat structure.T_DIR, T_FILE, T_DEVICE, stat
kernel/elf.hTypes + constantsELF header and program-header structures.ELF_MAGIC, elfhdr, proghdr, ELF_PROG_LOAD, ELF_PROG_FLAG_EXEC, ELF_PROG_FLAG_WRITE, ELF_PROG_FLAG_READ
kernel/virtio.hTypes + constantsVirtIO MMIO, queue, and block-device constants and structures.VIRTIO_MMIO_MAGIC_VALUE, VIRTIO_MMIO_VERSION, VIRTIO_MMIO_DEVICE_ID, VIRTIO_MMIO_VENDOR_ID, VIRTIO_MMIO_DEVICE_FEATURES, VIRTIO_MMIO_DRIVER_FEATURES, VIRTIO_MMIO_QUEUE_SEL, VIRTIO_MMIO_QUEUE_NUM_MAX, VIRTIO_MMIO_QUEUE_NUM, VIRTIO_MMIO_QUEUE_READY, VIRTIO_MMIO_QUEUE_NOTIFY, VIRTIO_MMIO_INTERRUPT_STATUS, VIRTIO_MMIO_INTERRUPT_ACK, VIRTIO_MMIO_STATUS, VIRTIO_MMIO_QUEUE_DESC_LOW, VIRTIO_MMIO_QUEUE_DESC_HIGH, VIRTIO_MMIO_DRIVER_DESC_LOW, VIRTIO_MMIO_DRIVER_DESC_HIGH, VIRTIO_MMIO_DEVICE_DESC_LOW, VIRTIO_MMIO_DEVICE_DESC_HIGH, VIRTIO_CONFIG_S_ACKNOWLEDGE, VIRTIO_CONFIG_S_DRIVER, VIRTIO_CONFIG_S_DRIVER_OK, VIRTIO_CONFIG_S_FEATURES_OK, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_SCSI, VIRTIO_BLK_F_CONFIG_WCE, VIRTIO_BLK_F_MQ, VIRTIO_F_ANY_LAYOUT, VIRTIO_RING_F_INDIRECT_DESC, VIRTIO_RING_F_EVENT_IDX, NUM, virtq_desc, VRING_DESC_F_NEXT, VRING_DESC_F_WRITE, virtq_avail, virtq_used_elem, virtq_used, VIRTIO_BLK_T_IN, VIRTIO_BLK_T_OUT, virtio_blk_req
kernel/param.hConstantsCompile-time xv6 limits.NPROC, NCPU, NOFILE, NFILE, NINODE, NDEV, ROOTDEV, MAXARG, MAXOPBLOCKS, LOGBLOCKS, NBUF, FSSIZE, MAXPATH, USERSTACK
kernel/memlayout.hConstants + macrosPhysical and virtual address layout.UART0, UART0_IRQ, VIRTIO0, VIRTIO0_IRQ, PLIC, PLIC_PRIORITY, PLIC_PENDING, PLIC_SENABLE, PLIC_SPRIORITY, PLIC_SCLAIM, KERNBASE, PHYSTOP, TRAMPOLINE, KSTACK, TRAPFRAME
kernel/syscall.hConstantsSystem-call numbers.SYS_fork, SYS_exit, SYS_wait, SYS_pipe, SYS_read, SYS_kill, SYS_exec, SYS_fstat, SYS_chdir, SYS_dup, SYS_getpid, SYS_sbrk, SYS_pause, SYS_uptime, SYS_open, SYS_write, SYS_mknod, SYS_unlink, SYS_link, SYS_mkdir, SYS_close
kernel/vm.hTypes + constantsPage-table type aliases and page-size constants.SBRK_EAGER, SBRK_LAZY
kernel/fcntl.hConstantsOpen flag constants.O_RDONLY, O_WRONLY, O_RDWR, O_CREATE, O_TRUNC

Small Unix-like Kernel

Generated from kernel/defs.h; each entry links to the implementation when one exists in the checked-in source.

Processes and scheduler

DeclarationHeaderDescription
int kexec(char*, char**);kernel/defs.h:26Replace the current process image with an ELF program.
int cpuid(void);kernel/defs.h:81Return the current hart's CPU index.
void kexit(int);kernel/defs.h:82Terminate the current process and wake its parent.
int kfork(void);kernel/defs.h:83Create a child process by copying the current process.
int growproc(int);kernel/defs.h:84Grow or shrink the current process address space.
void proc_mapstacks(pagetable_t);kernel/defs.h:85Map fixed kernel stacks for every process slot.
pagetable_t proc_pagetable(struct proc *);kernel/defs.h:86Create a user page table with trampoline mappings.
void proc_freepagetable(pagetable_t, uint64);kernel/defs.h:87Free a process page table and its user pages.
int kkill(int);kernel/defs.h:88Mark a process so it exits when it next reaches user mode.
int killed(struct proc*);kernel/defs.h:89Report whether a process has been marked for termination.
void setkilled(struct proc*);kernel/defs.h:90Set a process's killed flag.
struct cpu* mycpu(void);kernel/defs.h:91Return the current CPU structure.
struct proc* myproc();kernel/defs.h:92Return the process running on the current CPU.
void procinit(void);kernel/defs.h:93Initialize process table locks and kernel stacks.
void scheduler(void) __attribute__((noreturn));kernel/defs.h:94Run the per-CPU process scheduling loop.
void sched(void);kernel/defs.h:95Switch from the current process to the scheduler.
void sleep(void*, struct spinlock*);kernel/defs.h:96Atomically sleep on a channel while releasing a lock.
void userinit(void);kernel/defs.h:97Create the first user process.
int kwait(uint64);kernel/defs.h:98Wait for a child process to exit and collect its status.
void wakeup(void*);kernel/defs.h:99Wake processes sleeping on a channel.
void yield(void);kernel/defs.h:100Give up the CPU and become runnable again.
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);kernel/defs.h:101Copy to either user memory or kernel memory.
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);kernel/defs.h:102Copy from either user memory or kernel memory.
void procdump(void);kernel/defs.h:103Print a process table snapshot for debugging.

Context switching

DeclarationHeaderDescription
void swtch(struct context*, struct context*);kernel/defs.h:106Save one kernel context and restore another.

Virtual memory

DeclarationHeaderDescription
void* kalloc(void);kernel/defs.h:59Allocate and return one zeroed physical page.
void kfree(void *);kernel/defs.h:60Return one physical page to the allocator freelist.
void kinit(void);kernel/defs.h:61Initialize the physical page allocator.
void kvminit(void);kernel/defs.h:154Build the kernel page table.
void kvminithart(void);kernel/defs.h:155Install the kernel page table on the current hart.
void kvmmap(pagetable_t, uint64, uint64, uint64, int);kernel/defs.h:156Map a physical range into the kernel page table.
int mappages(pagetable_t, uint64, uint64, uint64, int);kernel/defs.h:157Install page-table entries for a virtual address range.
pagetable_t uvmcreate(void);kernel/defs.h:158Allocate an empty user page table.
uint64 uvmalloc(pagetable_t, uint64, uint64, int);kernel/defs.h:159Grow a user address space and allocate pages.
uint64 uvmdealloc(pagetable_t, uint64, uint64);kernel/defs.h:160Shrink a user address space.
int uvmcopy(pagetable_t, pagetable_t, uint64);kernel/defs.h:161Copy a parent address space into a child page table.
void uvmfree(pagetable_t, uint64);kernel/defs.h:162Free a user page table and all mapped pages.
void uvmunmap(pagetable_t, uint64, uint64, int);kernel/defs.h:163Remove user page-table mappings and optionally free pages.
void uvmclear(pagetable_t, uint64);kernel/defs.h:164Clear user access on one page-table entry.
pte_t * walk(pagetable_t, uint64, int);kernel/defs.h:165Find or allocate a page-table entry for a virtual address.
uint64 walkaddr(pagetable_t, uint64);kernel/defs.h:166Translate a user virtual address to a physical address.
int copyout(pagetable_t, uint64, char *, uint64);kernel/defs.h:167Copy bytes from kernel memory into a user virtual address.
int copyin(pagetable_t, char *, uint64, uint64);kernel/defs.h:168Copy bytes from a user virtual address into kernel memory.
int copyinstr(pagetable_t, char *, uint64, uint64);kernel/defs.h:169Copy a null-terminated user string into kernel memory.
int ismapped(pagetable_t, uint64);kernel/defs.h:170Report whether a user virtual address has a valid PTE.
uint64 vmfault(pagetable_t, uint64, int);kernel/defs.h:171Handle a lazy-allocation page fault.

Traps and interrupts

DeclarationHeaderDescription
void trapinit(void);kernel/defs.h:141Initialize global trap handling state.
void trapinithart(void);kernel/defs.h:142Install the kernel trap vector on the current hart.
void prepare_return(void);kernel/defs.h:144Prepare trapframe and registers for return to user mode.

System-call dispatcher

DeclarationHeaderDescription
void argint(int, int*);kernel/defs.h:132Read an integer syscall argument from the current trapframe.
int argstr(int, char*, int);kernel/defs.h:133Copy a string syscall argument from user memory.
void argaddr(int, uint64 *);kernel/defs.h:134Read a pointer-sized syscall argument from the current trapframe.
int fetchstr(uint64, char*, int);kernel/defs.h:135Fetch a null-terminated string from user memory.
int fetchaddr(uint64, uint64*);kernel/defs.h:136Fetch a 64-bit value from the current process's user memory.
void syscall();kernel/defs.h:137Dispatch the current process's syscall number.

File descriptors

DeclarationHeaderDescription
struct file* filealloc(void);kernel/defs.h:29Allocate an unused entry from the global open-file table.
void fileclose(struct file*);kernel/defs.h:30Close a file object and release its underlying resource.
struct file* filedup(struct file*);kernel/defs.h:31Increase a file object's reference count.
void fileinit(void);kernel/defs.h:32Initialize the global open-file table lock.
int fileread(struct file*, uint64, int n);kernel/defs.h:33Read through a file, pipe, or device descriptor.
int filestat(struct file*, uint64 addr);kernel/defs.h:34Copy file metadata into a user stat structure.
int filewrite(struct file*, uint64, int n);kernel/defs.h:35Write through a file, pipe, or device descriptor.

Pipes

DeclarationHeaderDescription
int pipealloc(struct file**, struct file**);kernel/defs.h:70Create paired read and write file objects for a pipe.
void pipeclose(struct pipe*, int);kernel/defs.h:71Close one side of a pipe and wake blocked peers.
int piperead(struct pipe*, uint64, int);kernel/defs.h:72Read bytes from a pipe into user memory.
int pipewrite(struct pipe*, uint64, int);kernel/defs.h:73Write bytes from user memory into a pipe.

File system and log

DeclarationHeaderDescription
void binit(void);kernel/defs.h:13Initialize the buffer cache and its block list.
struct buf* bread(uint, uint);kernel/defs.h:14Return a locked buffer containing a disk block.
void brelse(struct buf*);kernel/defs.h:15Release a locked buffer and move it toward the MRU end.
void bwrite(struct buf*);kernel/defs.h:16Write a locked buffer back to disk.
void bpin(struct buf*);kernel/defs.h:17Prevent a cached disk block from being evicted.
void bunpin(struct buf*);kernel/defs.h:18Drop a buffer-cache pin reference.
void fsinit(int);kernel/defs.h:38Initialize file-system state from the disk superblock.
int dirlink(struct inode*, char*, uint);kernel/defs.h:39Add a directory entry that points to an inode number.
struct inode* dirlookup(struct inode*, char*, uint*);kernel/defs.h:40Find a named entry inside a directory inode.
struct inode* ialloc(uint, short);kernel/defs.h:41Allocate a fresh on-disk inode of a given type.
struct inode* idup(struct inode*);kernel/defs.h:42Increment an inode reference count.
void iinit();kernel/defs.h:43Initialize the in-memory inode cache.
void ilock(struct inode*);kernel/defs.h:44Lock an inode and load its on-disk metadata if needed.
void iput(struct inode*);kernel/defs.h:45Drop an inode reference and free it when links disappear.
void iunlock(struct inode*);kernel/defs.h:46Release a locked inode.
void iunlockput(struct inode*);kernel/defs.h:47Unlock an inode and drop one reference.
void iupdate(struct inode*);kernel/defs.h:48Write an inode's in-memory metadata to disk.
int namecmp(const char*, const char*);kernel/defs.h:49Compare xv6 directory entry names.
struct inode* namei(char*);kernel/defs.h:50Resolve a pathname to an inode.
struct inode* nameiparent(char*, char*);kernel/defs.h:51Resolve a pathname to its parent directory inode.
int readi(struct inode*, int, uint64, uint, uint);kernel/defs.h:52Read bytes from an inode into kernel or user memory.
void stati(struct inode*, struct stat*);kernel/defs.h:53Fill a stat structure from an inode.
int writei(struct inode*, int, uint64, uint, uint);kernel/defs.h:54Write bytes from kernel or user memory into an inode.
void itrunc(struct inode*);kernel/defs.h:55Free all data blocks owned by an inode.
void ireclaim(int);kernel/defs.h:56Clear inode-cache entries for a removed device.
void initlog(int, struct superblock*);kernel/defs.h:64Initialize the write-ahead log from the file-system superblock.
void log_write(struct buf*);kernel/defs.h:65Record that a buffer must be written during log commit.
void begin_op(void);kernel/defs.h:66Start a logged file-system operation, waiting if the log is full.
void end_op(void);kernel/defs.h:67Finish a logged file-system operation and commit if needed.

Locks

DeclarationHeaderDescription
void acquire(struct spinlock*);kernel/defs.h:109Take a spinlock and disable interrupt-driven preemption.
int holding(struct spinlock*);kernel/defs.h:110Report whether the current CPU holds a spinlock.
void initlock(struct spinlock*, char*);kernel/defs.h:111Initialize a spinlock with a debug name.
void release(struct spinlock*);kernel/defs.h:112Release a spinlock and restore interrupt state.
void push_off(void);kernel/defs.h:113Disable interrupts and record nested critical-section depth.
void pop_off(void);kernel/defs.h:114Restore interrupt enable state after a critical section.
void acquiresleep(struct sleeplock*);kernel/defs.h:117Take a sleeplock, sleeping until the lock becomes available.
void releasesleep(struct sleeplock*);kernel/defs.h:118Release a sleeplock and wake waiters.
int holdingsleep(struct sleeplock*);kernel/defs.h:119Report whether the current process holds a sleeplock.
void initsleeplock(struct sleeplock*, char*);kernel/defs.h:120Initialize a sleeplock with a debug name.

Device drivers

DeclarationHeaderDescription
void consoleinit(void);kernel/defs.h:21Initialize console input, console output, and UART interrupts.
void consoleintr(int);kernel/defs.h:22Handle one console input character from the UART driver.
void consputc(int);kernel/defs.h:23Write one character to the console device.
int printf(char*, ...) __attribute__ ((format (printf, 1, 2)));kernel/defs.h:76Format text to the kernel console.
void panic(char*) __attribute__((noreturn));kernel/defs.h:77Print a panic message and stop the kernel.
void printfinit(void);kernel/defs.h:78Initialize the console print lock.
void uartinit(void);kernel/defs.h:147Initialize the UART hardware.
void uartintr(void);kernel/defs.h:148Handle UART receive and transmit interrupts.
void uartwrite(char [], int);kernel/defs.h:149Write a buffer to the UART console.
void uartputc_sync(int);kernel/defs.h:150Synchronously write one character to the UART.
int uartgetc(void);kernel/defs.h:151Read one received UART character if available.
void plicinit(void);kernel/defs.h:174Configure global PLIC interrupt priorities.
void plicinithart(void);kernel/defs.h:175Enable PLIC delivery for the current hart.
int plic_claim(void);kernel/defs.h:176Claim the next pending external interrupt.
void plic_complete(int);kernel/defs.h:177Tell the PLIC an interrupt has been handled.
void virtio_disk_init(void);kernel/defs.h:180Initialize the VirtIO block device and descriptor ring.
void virtio_disk_rw(struct buf *, int);kernel/defs.h:181Submit a synchronous block read or write request.
void virtio_disk_intr(void);kernel/defs.h:182Handle completed VirtIO disk requests.

String and memory

DeclarationHeaderDescription
int memcmp(const void*, const void*, uint);kernel/defs.h:123Compare two byte ranges.
void* memmove(void*, const void*, uint);kernel/defs.h:124Copy bytes between overlapping memory ranges.
void* memset(void*, int, uint);kernel/defs.h:125Fill a memory range with one byte value.
char* safestrcpy(char*, const char*, int);kernel/defs.h:126Copy a string while always terminating the destination.
int strlen(const char*);kernel/defs.h:127Return the length of a null-terminated string.
int strncmp(const char*, const char*, uint);kernel/defs.h:128Compare bounded strings.
char* strncpy(char*, const char*, int);kernel/defs.h:129Copy a bounded string.

Small User-space C Library

Generated from the non-syscall declarations in user/user.h.

String and memory

DeclarationHeaderDescription
char* strcpy(char*, const char*);user/user.h:30Copy a null-terminated string.
void *memmove(void*, const void*, int);user/user.h:31Copy bytes between possibly overlapping memory ranges.
int strcmp(const char*, const char*);user/user.h:33Compare two null-terminated strings.
uint strlen(const char*);user/user.h:35Return the length of a null-terminated string.
void* memset(void*, int, uint);user/user.h:36Fill a memory range with one byte value.
int memcmp(const void *, const void *, uint);user/user.h:38Compare two byte ranges.
void *memcpy(void *, const void *, uint);user/user.h:39Copy bytes between non-overlapping memory ranges.

Input and output

DeclarationHeaderDescription
char* gets(char*, int max);user/user.h:34Read a line from standard input into a buffer.
void fprintf(int, const char*, ...) __attribute__ ((format (printf, 2, 3)));user/user.h:44Write formatted output to a file descriptor.
void printf(const char*, ...) __attribute__ ((format (printf, 1, 2)));user/user.h:45Write formatted output to standard output.

Memory allocation

DeclarationHeaderDescription
char* sbrk(int);user/user.h:40Grow the user heap through the normal syscall wrapper.
char* sbrklazy(int);user/user.h:41Grow the user heap using xv6's lazy-allocation mode.
void* malloc(uint);user/user.h:48Allocate bytes from the user heap.
void free(void*);user/user.h:49Return a malloc allocation to the user heap.

Utilities

DeclarationHeaderDescription
int stat(const char*, struct stat*);user/user.h:29Open a path and return its file metadata.
char* strchr(const char*, char c);user/user.h:32Find the first occurrence of a character in a string.
int atoi(const char*);user/user.h:37Parse a decimal string as an integer.

21 Unix-like System Calls

Generated from the dispatch table in kernel/syscall.c.

Process

DeclarationHeaderDescription
int fork(void);user/user.h:6Create a child process by copying the caller.
int exit(int) __attribute__((noreturn));user/user.h:7Terminate the current process with a status value.
int wait(int*);user/user.h:8Wait for a child process to exit.
int kill(int);user/user.h:13Request termination of a process by PID.
int exec(const char*, char**);user/user.h:14Load and run a new program in the current process.
int getpid(void);user/user.h:23Return the current process ID.

Memory and time

DeclarationHeaderDescription
char* sys_sbrk(int,int);user/user.h:24Grow or shrink the process heap.
int pause(int);user/user.h:25Suspend the process for a short timer interval.
int uptime(void);user/user.h:26Return the number of timer ticks since boot.

File descriptors

DeclarationHeaderDescription
int pipe(int*);user/user.h:9Create a unidirectional pipe and return two descriptors.
int read(int, void*, int);user/user.h:11Read bytes from a file descriptor.
int fstat(int fd, struct stat*);user/user.h:18Return metadata for an open file descriptor.
int dup(int);user/user.h:22Duplicate an open file descriptor.
int write(int, const void*, int);user/user.h:10Write bytes to a file descriptor.
int close(int);user/user.h:12Release an open file descriptor.

File system

DeclarationHeaderDescription
int chdir(const char*);user/user.h:21Change the current process's working directory.
int open(const char*, int);user/user.h:15Open or create a file and return a descriptor.
int mknod(const char*, short, short);user/user.h:16Create a device inode.
int unlink(const char*);user/user.h:17Remove a directory entry.
int link(const char*, const char*);user/user.h:19Create a hard link to an existing inode.
int mkdir(const char*);user/user.h:20Create a directory at a pathname.

Small Unix User Environment

User commands and test programs in user/.

CommandEntry pointDescription
catuser/cat.cCopy files or standard input to standard output.
dorphanuser/dorphan.cUser-space xv6 test or utility program.
echouser/echo.cPrint command arguments.
forktest testsuser/forktest.cUser-space xv6 test or utility program.
forphanuser/forphan.cUser-space xv6 test or utility program.
grepuser/grep.cSearch input lines with a small regular-expression matcher.
grinduser/grind.cUser-space xv6 test or utility program.
inituser/init.cFirst user process; starts the shell and reaps orphaned children.
killuser/kill.cUser-space xv6 test or utility program.
lnuser/ln.cCreate a hard link.
logstressuser/logstress.cUser-space xv6 test or utility program.
lsuser/ls.cList directory entries and file metadata.
mkdiruser/mkdir.cCreate a directory.
rmuser/rm.cRemove a file or directory entry.
shuser/sh.cSmall command shell with redirection, pipes, lists, and background jobs.
stressfsuser/stressfs.cUser-space xv6 test or utility program.
usertests testsuser/usertests.cUser-space xv6 test or utility program.
wcuser/wc.cCount lines, words, and bytes.
zombieuser/zombie.cUser-space xv6 test or utility program.