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