kernel/types.h
Included by 47 files
kernel/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/string.c, kernel/syscall.c, kernel/sysfile.c, kernel/sysproc.c, kernel/trap.c, kernel/uart.c, kernel/virtio_disk.c, kernel/vm.c, mkfs/mkfs.c, user/cat.c, user/dorphan.c, user/echo.c, user/forktest.c, user/forphan.c, user/grep.c, user/grind.c, user/init.c, user/kill.c, user/ln.c, user/logstress.c, user/ls.c, user/mkdir.c, user/printf.c, user/rm.c, user/sh.c, user/stressfs.c, user/sync.c, user/ulib.c, user/umalloc.c, user/usertests.c, user/wc.c, user/zombie.cAbout this file
Short names for C’s integer types, used everywhere in the kernel, in user programs and in
mkfs. xv6 defines its own short names instead of using <stdint.h>. That header
would in fact be available (it is one of the few headers a freestanding (vs. hosted) C compiler
supplies), but xv6 uses no standard headers at all, and the same file is shared with
mkfs, a normal host program.
The names that end in a number say exactly how many bits the type has. That matters in
an operating system: a CSR (control and status register), a register and a page-table entry are all 64 bits, and
on-disk structures must have the same layout every time. On 64-bit RISC-V (the “LP64”
model GCC uses here) char is 8 bits, short 16, int 32 and long 64, which is what
these typedefs rely on.
The file has no include guard. Standard C99 does not allow repeating a typedef (GCC
accepts it unless -pedantic is given); xv6 keeps it simple by including this file
once, first, in every .c file, before headers that use these names.
Read next: kernel/param.h, kernel/riscv.h.
Unsigned types with short names
Abbreviations for the unsigned types. The kernel mostly deals with quantities that cannot be negative (sizes, counts, block numbers), so it uses unsigned types for them.
Unsigned types of an exact width
These are the names to use when the width matters: uint8 for a byte, uint16 and
uint32 for fields of device registers and on-disk structures, uint64 for
addresses, registers and page-table entries. They play the role of the
standard uint8_t … uint64_t.
uint64 is the kernel’s workhorse type: every address, CSR value and PTE is one.
It is unsigned long, 64 bits on RV64. The kernel converts pointers to uint64 and
back freely, because the two have the same width.
The type of a page-table entry pointer used in a few places (walk and
kernel/exec.c). The name means “page directory entry”, a leftover from the x86
version of xv6, where the top-level page table was called a page directory. It is the
same type as pte_t (uint64), which the rest of the kernel uses.