1 // Per-CPU state 2 struct cpu { 3 uchar id; // Local APIC ID; index into cpus[] below 4 struct context *scheduler; // swtch() here to enter scheduler 5 struct taskstate ts; // Used by x86 to find stack for interrupt 6 struct segdesc gdt[NSEGS]; // x86 global descriptor table 7 volatile uint started; // Has the CPU started? 8 int ncli; // Depth of pushcli nesting. 9 int intena; // Were interrupts enabled before pushcli? 10 11 // Cpu-local storage variables; see below 12 struct cpu *cpu; 13 struct proc *proc; // The currently-running process. 14 }; 15 16 extern struct cpu cpus[NCPU]; 17 extern int ncpu; 18 19 // Per-CPU variables, holding pointers to the 20 // current cpu and to the current process. 21 // The asm suffix tells gcc to use "%gs:0" to refer to cpu 22 // and "%gs:4" to refer to proc. seginit sets up the 23 // %gs segment register so that %gs refers to the memory 24 // holding those two variables in the local cpu's struct cpu. 25 // This is similar to how thread-local variables are implemented 26 // in thread libraries such as Linux pthreads. 27 extern struct cpu *cpu asm("%gs:0"); // &cpus[cpunum()] 28 extern struct proc *proc asm("%gs:4"); // cpus[cpunum()].proc 29 30 //PAGEBREAK: 17 31 // Saved registers for kernel context switches. 32 // Don't need to save all the segment registers (%cs, etc), 33 // because they are constant across kernel contexts. 34 // Don't need to save %eax, %ecx, %edx, because the 35 // x86 convention is that the caller has saved them. 36 // Contexts are stored at the bottom of the stack they 37 // describe; the stack pointer is the address of the context. 38 // The layout of the context matches the layout of the stack in swtch.S 39 // at the "Switch stacks" comment. Switch doesn't save eip explicitly, 40 // but it is on the stack and allocproc() manipulates it. 41 struct context { 42 uint edi; 43 uint esi; 44 uint ebx; 45 uint ebp; 46 uint eip; 47 }; 48 49 enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; 50 51 // Per-process state 52 struct proc { 53 uint sz; // Size of process memory (bytes) 54 pde_t* pgdir; // Page table 55 char *kstack; // Bottom of kernel stack for this process 56 enum procstate state; // Process state 57 int pid; // Process ID 58 struct proc *parent; // Parent process 59 struct trapframe *tf; // Trap frame for current syscall 60 struct context *context; // swtch() here to run process 61 void *chan; // If non-zero, sleeping on chan 62 int killed; // If non-zero, have been killed 63 struct file *ofile[NOFILE]; // Open files 64 struct inode *cwd; // Current directory 65 char name[16]; // Process name (debugging) 66 }; 67 68 // Process memory is laid out contiguously, low addresses first: 69 // text 70 // original data and bss 71 // fixed-size stack 72 // expandable heap 73