xv6, line by line
kernel/defs.h

kernel/defs.h

C header · 186 lines · annotated 100% · kernel · upstream

Included by 22 fileskernel/bio.c, kernel/console.c, kernel/exec.c, kernel/file.c, kernel/fs.c, kernel/kalloc.c, kernel/log.c, kernel/main.c, kernel/pipe.c, kernel/plic.c, kernel/printk.c, kernel/proc.c, kernel/sleeplock.c, kernel/spinlock.c, kernel/start.c, kernel/syscall.c, kernel/sysfile.c, kernel/sysproc.c, kernel/trap.c, kernel/uart.c, kernel/virtio_disk.c, kernel/vm.c
About this file

One header that declares (almost) every kernel function that is called from a file other than the one defining it. Nearly every kernel .c file includes it, so any kernel code can call, say, kalloc or acquire without including a header per subsystem.

It is organized as one section per source file, each headed by a comment naming the file, which makes it a handy index of the kernel: to find where a function lives, find its section here. The sections are in rough alphabetical order, with plic.c and virtio_disk.c added at the end.

defs.h has no include guard and includes nothing itself. It uses types defined elsewhere (uint and uint64 from kernel/types.h, pagetable_t and pte_t from kernel/riscv.h), so every file includes those headers before defs.h.

Not everything is here. Functions used only inside their own file are usually static and absent; the sys_* handlers are declared in kernel/syscall.c, their only user; and a few cross-file names are declared where they are used (for example forkret refers to userret with its own extern).

Read alongside: any kernel .c file.

1// clang-format off
2struct buf;
3struct context;
4struct file;
5struct inode;
6struct pipe;
7struct proc;
8struct spinlock;
9struct sleeplock;
10struct stat;
11struct superblock;
13// bio.c
14void binit(void);
15struct buf* bread(uint, uint);
16void brelse(struct buf*);
17void bwrite(struct buf*);
18void bpin(struct buf*);
19void bunpin(struct buf*);
21// console.c
22void consoleinit(void);
23void consoleintr(int);
24void consputc(int);
26// exec.c
27int kexec(char*, char**);
29// file.c
30struct file* filealloc(void);
31void fileclose(struct file*);
32struct file* filedup(struct file*);
33void fileinit(void);
34int fileread(struct file*, uint64, int n);
35int filestat(struct file*, uint64 addr);
36int filewrite(struct file*, uint64, int n);
38// fs.c
39void fsinit(int);
40int dirlink(struct inode*, char*, uint);
41struct inode* dirlookup(struct inode*, char*, uint*);
42struct inode* ialloc(uint, short);
43struct inode* idup(struct inode*);
44void iinit();
45void ilock(struct inode*);
46void iput(struct inode*);
47void iunlock(struct inode*);
48void iunlockput(struct inode*);
49void iupdate(struct inode*);
50int namecmp(const char*, const char*);
51struct inode* namei(char*);
52struct inode* nameiparent(char*, char*);
53int readi(struct inode*, int, uint64, uint, uint);
54void stati(struct inode*, struct stat*);
55int writei(struct inode*, int, uint64, uint, uint);
56void itrunc(struct inode*);
57void ireclaim(int);
59// kalloc.c
60void* kalloc(void);
61void kfree(void *);
62void kinit(void);
64// log.c
65void initlog(int, struct superblock*);
66void log_write(struct buf*);
67void begin_op(void);
68void end_op(void);
70// pipe.c
71int pipealloc(struct file**, struct file**);
72void pipeclose(struct pipe*, int);
73int piperead(struct pipe*, uint64, int);
74int pipewrite(struct pipe*, uint64, int);
76// printk.c
77int printk(char*, ...) __attribute__ ((format (printf, 1, 2)));
78void panic(char*) __attribute__((noreturn));
79void printkinit(void);
81// proc.c
82int cpuid(void);
83void kexit(int);
84int kfork(void);
85int growproc(int);
89int kkill(int);
90int killed(struct proc*);
91void setkilled(struct proc*);
92struct cpu* mycpu(void);
93struct proc* myproc();
94void procinit(void);
95void scheduler(void) __attribute__((noreturn));
96void sched(void);
97void sleep_prepare(void*);
98void sleep(void);
99void userinit(void);
101void wakeup(void*);
102void yield(void);
105void procdump(void);
107// swtch.S
108void swtch(struct context*, struct context*);
110// spinlock.c
111void acquire(struct spinlock*);
112int holding(struct spinlock*);
113void initlock(struct spinlock*, char*);
114void release(struct spinlock*);
115void push_off(void);
116void pop_off(void);
118// sleeplock.c
119void acquiresleep(struct sleeplock*);
120void releasesleep(struct sleeplock*);
122void initsleeplock(struct sleeplock*, char*);
124// string.c
125int memcmp(const void*, const void*, uint);
126void* memmove(void*, const void*, uint);
127void* memset(void*, int, uint);
128char* safestrcpy(char*, const char*, int);
129int strlen(const char*);
130int strncmp(const char*, const char*, uint);
131char* strncpy(char*, const char*, int);
133// syscall.c
134void argint(int, int*);
135int argstr(int, char*, int);
136void argaddr(int, uint64 *);
137int fetchstr(uint64, char*, int);
139void syscall();
141// trap.c
142extern uint ticks;
143void trapinit(void);
144void trapinithart(void);
145extern struct spinlock tickslock;
146void prepare_return(void);
148// uart.c
149void uartinit(void);
150void uartintr(void);
151void uartwrite(char [], int);
152void uartputc_sync(int);
154// vm.c
155void kvminit(void);
156void kvminithart(void);
174// plic.c
175void plicinit(void);
176void plicinithart(void);
177int plic_claim(void);
178void plic_complete(int);
180// virtio_disk.c
182void virtio_disk_rw(struct buf *, int);
185// number of elements in fixed-size array
186#define NELEM(x) (sizeof(x) / sizeof((x)[0]))