proc.h 3.1 KB

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