《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.4 KiB

  1. #ifndef __KERN_PROCESS_PROC_H__
  2. #define __KERN_PROCESS_PROC_H__
  3. #include <defs.h>
  4. #include <list.h>
  5. #include <trap.h>
  6. #include <memlayout.h>
  7. // process's state in his life cycle
  8. enum proc_state {
  9. PROC_UNINIT = 0, // uninitialized
  10. PROC_SLEEPING, // sleeping
  11. PROC_RUNNABLE, // runnable(maybe running)
  12. PROC_ZOMBIE, // almost dead, and wait parent proc to reclaim his resource
  13. };
  14. // Saved registers for kernel context switches.
  15. // Don't need to save all the %fs etc. segment registers,
  16. // because they are constant across kernel contexts.
  17. // Save all the regular registers so we don't need to care
  18. // which are caller save, but not the return register %eax.
  19. // (Not saving %eax just simplifies the switching code.)
  20. // The layout of context must match code in switch.S.
  21. struct context {
  22. uint32_t eip;
  23. uint32_t esp;
  24. uint32_t ebx;
  25. uint32_t ecx;
  26. uint32_t edx;
  27. uint32_t esi;
  28. uint32_t edi;
  29. uint32_t ebp;
  30. };
  31. #define PROC_NAME_LEN 15
  32. #define MAX_PROCESS 4096
  33. #define MAX_PID (MAX_PROCESS * 2)
  34. extern list_entry_t proc_list;
  35. struct proc_struct {
  36. enum proc_state state; // Process state
  37. int pid; // Process ID
  38. int runs; // the running times of Proces
  39. uintptr_t kstack; // Process kernel stack
  40. volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  41. struct proc_struct *parent; // the parent process
  42. struct mm_struct *mm; // Process's memory management field
  43. struct context context; // Switch here to run process
  44. struct trapframe *tf; // Trap frame for current interrupt
  45. uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  46. uint32_t flags; // Process flag
  47. char name[PROC_NAME_LEN + 1]; // Process name
  48. list_entry_t list_link; // Process link list
  49. list_entry_t hash_link; // Process hash list
  50. int exit_code; // exit code (be sent to parent proc)
  51. uint32_t wait_state; // waiting state
  52. struct proc_struct *cptr, *yptr, *optr; // relations between processes
  53. };
  54. #define PF_EXITING 0x00000001 // getting shutdown
  55. #define WT_CHILD (0x00000001 | WT_INTERRUPTED)
  56. #define WT_INTERRUPTED 0x80000000 // the wait state could be interrupted
  57. #define le2proc(le, member) \
  58. to_struct((le), struct proc_struct, member)
  59. extern struct proc_struct *idleproc, *initproc, *current;
  60. void proc_init(void);
  61. void proc_run(struct proc_struct *proc);
  62. int kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags);
  63. char *set_proc_name(struct proc_struct *proc, const char *name);
  64. char *get_proc_name(struct proc_struct *proc);
  65. void cpu_idle(void) __attribute__((noreturn));
  66. struct proc_struct *find_proc(int pid);
  67. int do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf);
  68. int do_exit(int error_code);
  69. int do_yield(void);
  70. int do_execve(const char *name, size_t len, unsigned char *binary, size_t size);
  71. int do_wait(int pid, int *code_store);
  72. int do_kill(int pid);
  73. #endif /* !__KERN_PROCESS_PROC_H__ */