123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #pragma once
- #include "../common/cpu.h"
- #include "../common/glib.h"
- #include "../mm/mm.h"
- #include "../syscall/syscall.h"
- #include "ptrace.h"
- #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_FILES (1 << 1)
- #define CLONE_SIGNAL (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;
-
- 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 PROC_NEED_SCHED (1UL << 1)
- 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;
- long virtual_runtime;
- };
- 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, \
- .mm = &initial_mm, \
- .thread = &initial_thread, \
- .addr_limit = 0xffff800000000000, \
- .pid = 0, \
- .virtual_runtime = 0, \
- .signal = 0, \
- .priority = 2, \
- .preempt_count = 0, \
- .cpu_id = 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__("cli \n\t" \
- "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" \
- "sti \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);
- #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];
|