123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- #pragma once
- #include <common/cpu.h>
- #include <common/glib.h>
- #include <mm/mm.h>
- #include <syscall/syscall.h>
- #include "ptrace.h"
- #include <common/errno.h>
- #include <filesystem/VFS/VFS.h>
- #include <process/wait_queue.h>
- #define PROC_MAX_FD_NUM 16
- #define STACK_SIZE 32768
- #define PROC_RUNNING (1 << 0)
- #define PROC_INTERRUPTIBLE (1 << 1)
- #define PROC_UNINTERRUPTIBLE (1 << 2)
- #define PROC_ZOMBIE (1 << 3)
- #define PROC_STOPPED (1 << 4)
- #define KERNEL_CS (0x08)
- #define KERNEL_DS (0x10)
- #define USER_CS (0x28)
- #define USER_DS (0x30)
- #define CLONE_FS (1 << 0)
- #define CLONE_SIGNAL (1 << 1)
- #define CLONE_VM (1 << 2)
- struct mm_struct
- {
- pml4t_t *pgd;
-
- ul code_addr_start, code_addr_end;
-
- ul data_addr_start, data_addr_end;
-
- ul rodata_addr_start, rodata_addr_end;
-
- uint64_t bss_start, bss_end;
-
- ul brk_start, brk_end;
-
- ul stack_start;
- };
- struct thread_struct
- {
-
- ul rbp;
-
- ul rip;
-
- ul rsp;
- ul fs, gs;
- ul cr2;
-
- ul trap_num;
-
- ul err_code;
- };
- #define PF_KTHREAD (1UL << 0)
- #define PF_NEED_SCHED (1UL << 1)
- #define PF_VFORK (1UL << 2)
- struct process_control_block
- {
-
- volatile long state;
-
- unsigned long flags;
- int64_t preempt_count;
- long signal;
- long cpu_id;
-
- struct mm_struct *mm;
-
- struct thread_struct *thread;
-
- struct List list;
-
-
-
- uint64_t addr_limit;
- long pid;
- long priority;
- int64_t virtual_runtime;
-
-
- struct vfs_file_t *fds[PROC_MAX_FD_NUM];
-
- struct process_control_block *next_pcb;
-
- struct process_control_block *parent_pcb;
- int32_t exit_code;
- wait_queue_node_t wait_child_proc_exit;
- };
- union proc_union
- {
- struct process_control_block pcb;
- ul stack[STACK_SIZE / sizeof(ul)];
- } __attribute__((aligned(8)));
- #define INITIAL_PROC(proc) \
- { \
- .state = PROC_UNINTERRUPTIBLE, \
- .flags = PF_KTHREAD, \
- .preempt_count = 0, \
- .signal = 0, \
- .cpu_id = 0, \
- .mm = &initial_mm, \
- .thread = &initial_thread, \
- .addr_limit = 0xffffffffffffffff, \
- .pid = 0, \
- .priority = 2, \
- .virtual_runtime = 0, \
- .fds = {0}, \
- .next_pcb = &proc, \
- .parent_pcb = &proc, \
- .exit_code = 0, \
- .wait_child_proc_exit = 0 \
- }
- struct tss_struct
- {
- unsigned int reserved0;
- ul rsp0;
- ul rsp1;
- ul rsp2;
- ul reserved1;
- ul ist1;
- ul ist2;
- ul ist3;
- ul ist4;
- ul ist5;
- ul ist6;
- ul ist7;
- ul reserved2;
- unsigned short reserved3;
-
- unsigned short io_map_base_addr;
- } __attribute__((packed));
- #define INITIAL_TSS \
- { \
- .reserved0 = 0, \
- .rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
- .rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
- .rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
- .reserved1 = 0, \
- .ist1 = 0xffff800000007c00, \
- .ist2 = 0xffff800000007c00, \
- .ist3 = 0xffff800000007c00, \
- .ist4 = 0xffff800000007c00, \
- .ist5 = 0xffff800000007c00, \
- .ist6 = 0xffff800000007c00, \
- .ist7 = 0xffff800000007c00, \
- .reserved2 = 0, \
- .reserved3 = 0, \
- .io_map_base_addr = 0 \
- }
- struct process_control_block *get_current_pcb()
- {
- struct process_control_block *current = NULL;
-
- __asm__ __volatile__("andq %%rsp, %0 \n\t"
- : "=r"(current)
- : "0"(~32767UL));
- return current;
- };
- #define current_pcb get_current_pcb()
- #define GET_CURRENT_PCB \
- "movq %rsp, %rbx \n\t" \
- "andq $-32768, %rbx\n\t"
-
- #define switch_proc(prev, next) \
- do \
- { \
- __asm__ __volatile__("pushq %%rbp \n\t" \
- "pushq %%rax \n\t" \
- "movq %%rsp, %0 \n\t" \
- "movq %2, %%rsp \n\t" \
- "leaq switch_proc_ret_addr(%%rip), %%rax \n\t" \
- "movq %%rax, %1 \n\t" \
- "pushq %3 \n\t" \
- "jmp __switch_to \n\t" \
- "switch_proc_ret_addr: \n\t" \
- "popq %%rax \n\t" \
- "popq %%rbp \n\t" \
- : "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
- : "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
- : "memory"); \
- } while (0)
- void process_init();
- unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);
- struct process_control_block *process_get_pcb(long pid);
- void process_wakeup(struct process_control_block *pcb);
- void process_wakeup_immediately(struct process_control_block *pcb);
- ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[]);
- uint64_t process_exit_mm(struct process_control_block *pcb);
- ul process_do_exit(ul code);
- void process_exit_notify();
- int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags);
- #define process_switch_mm(next_pcb) \
- do \
- { \
- asm volatile("movq %0, %%cr3 \n\t" ::"r"(next_pcb->mm->pgd) \
- : "memory"); \
- } while (0)
- #define proc_current_cpu_id (current_pcb->cpu_id)
- extern unsigned long head_stack_start;
- extern ul _stack_start;
- extern void ret_from_intr(void);
- extern struct tss_struct initial_tss[MAX_CPU_NUM];
- extern struct mm_struct initial_mm;
- extern struct thread_struct initial_thread;
- extern union proc_union initial_proc_union;
- extern struct process_control_block *initial_proc[MAX_CPU_NUM];
|