proc.h 2.9 KB

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