xv6, line by line
kernel/proc.h

kernel/proc.h

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

Included by 15 fileskernel/console.c, kernel/exec.c, kernel/file.c, kernel/fs.c, kernel/pipe.c, kernel/printk.c, kernel/proc.c, kernel/sleeplock.c, kernel/spinlock.c, kernel/syscall.c, kernel/sysfile.c, kernel/sysproc.c, kernel/trap.c, kernel/uart.c, kernel/vm.c
About this file

The data structures behind processes and CPUs. Four structs and one enum:

  • struct context: the registers swtch saves when a kernel thread is switched out.
  • struct cpu: per-CPU state, one entry per hart in cpus.
  • struct trapframe: the page where a process’s user registers are saved when it enters the kernel (trapframe).
  • enum procstate: the states a process moves through.
  • struct proc: everything the kernel knows about one process, one entry per slot in the proc table.

The most important thing to learn here is which lock protects which field. The source states it in comments on struct proc; the notes below check those comments against how kernel/proc.c and the rest of the kernel actually use each field.

Read before: kernel/spinlock.h. Read next: kernel/proc.c and kernel/swtch.S.

1// Saved registers for kernel context switches.
2struct context {
6 // callee-saved
19};
21// Per-CPU state.
22struct cpu {
23 struct proc *proc; // The process running on this cpu, or null.
24 struct context context; // swtch() here to enter scheduler().
25 int noff; // Depth of push_off() nesting.
26 int intena; // Were interrupts enabled before push_off()?
27};
29extern struct cpu cpus[NCPU];
31// per-process data for the trap handling code in trampoline.S.
32// sits in a page by itself just under the trampoline page in the
33// user page table. not specially mapped in the kernel page table.
34// uservec in trampoline.S saves user registers in the trapframe,
35// then initializes registers from the trapframe's
36// kernel_sp, kernel_hartid, kernel_satp, and jumps to kernel_trap.
37// prepare_return() and userret in trampoline.S set up
38// the trapframe's kernel_*, restore user registers from the
39// trapframe, switch to the user page table, and enter user space.
40struct trapframe {
41 /* 0 */ uint64 kernel_satp; // kernel page table
42 /* 8 */ uint64 kernel_sp; // top of process's kernel stack
43 /* 16 */ uint64 kernel_trap; // usertrap()
44 /* 24 */ uint64 epc; // saved user program counter
45 /* 32 */ uint64 kernel_hartid; // saved kernel tp
46 /* 40 */ uint64 ra;
47 /* 48 */ uint64 sp;
48 /* 56 */ uint64 gp;
49 /* 64 */ uint64 tp;
50 /* 72 */ uint64 t0;
51 /* 80 */ uint64 t1;
52 /* 88 */ uint64 t2;
53 /* 96 */ uint64 s0;
54 /* 104 */ uint64 s1;
55 /* 112 */ uint64 a0;
56 /* 120 */ uint64 a1;
57 /* 128 */ uint64 a2;
58 /* 136 */ uint64 a3;
59 /* 144 */ uint64 a4;
60 /* 152 */ uint64 a5;
61 /* 160 */ uint64 a6;
62 /* 168 */ uint64 a7;
63 /* 176 */ uint64 s2;
64 /* 184 */ uint64 s3;
65 /* 192 */ uint64 s4;
66 /* 200 */ uint64 s5;
67 /* 208 */ uint64 s6;
68 /* 216 */ uint64 s7;
69 /* 224 */ uint64 s8;
70 /* 232 */ uint64 s9;
71 /* 240 */ uint64 s10;
72 /* 248 */ uint64 s11;
73 /* 256 */ uint64 t3;
74 /* 264 */ uint64 t4;
75 /* 272 */ uint64 t5;
76 /* 280 */ uint64 t6;
77};
81// Per-process state
82struct proc {
83 struct spinlock lock;
85 // p->lock must be held when using these:
86 enum procstate state; // Process state
87 void *chan; // If non-zero, sleeping on chan
88 int killed; // If non-zero, have been killed
89 int xstate; // Exit status to be returned to parent's wait
90 int pid; // Process ID
92 // wait_lock must be held when using this:
93 struct proc *parent; // Parent process
95 // these are private to the process, so p->lock need not be held.
96 uint64 kstack; // Virtual address of kernel stack
97 uint64 sz; // Size of process memory (bytes)
98 pagetable_t pagetable; // User page table
99 struct trapframe *trapframe; // data page for trampoline.S
100 struct context context; // swtch() here to run process
101 struct file *ofile[NOFILE]; // Open files
102 struct inode *cwd; // Current directory
103 char name[16]; // Process name (debugging)
104};