123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #pragma once
- #include "../common/cpu.h"
- #include "../common/glib.h"
- #include "../mm/mm.h"
- #include "../syscall/syscall.h"
- #include "ptrace.h"
- extern unsigned long _stack_start;
- extern void ret_from_intr(void);
- #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 (1 << 0)
- struct process_control_block
- {
-
- struct List list;
-
- volatile long state;
-
- unsigned long flags;
-
- struct mm_struct *mm;
-
- struct thread_struct *thread;
-
-
-
- ul addr_limit;
-
- long pid;
-
- long counter;
-
- long signal;
-
- long priority;
- };
- union proc_union
- {
- struct process_control_block pcb;
- ul stack[STACK_SIZE / sizeof(ul)];
- } __attribute__((aligned(8)));
- struct mm_struct initial_mm;
- struct thread_struct initial_thread;
- #define INITIAL_PROC(proc) \
- { \
- .state = PROC_UNINTERRUPTIBLE, \
- .flags = PF_KTHREAD, \
- .mm = &initial_mm, \
- .thread = &initial_thread, \
- .addr_limit = 0xffff800000000000, \
- .pid = 0, \
- .counter = 1, \
- .signal = 0, \
- .priority = 0 \
- }
- union proc_union initial_proc_union __attribute__((__section__(".data.init_proc_union"))) = {INITIAL_PROC(initial_proc_union.pcb)};
- struct process_control_block *initial_proc[MAX_CPU_NUM] = {&initial_proc_union.pcb, 0};
- struct mm_struct initial_mm = {0};
- struct thread_struct initial_thread =
- {
- .rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
- .rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
- .fs = KERNEL_DS,
- .gs = KERNEL_DS,
- .cr2 = 0,
- .trap_num = 0,
- .err_code = 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 tss_struct initial_tss[MAX_CPU_NUM] = {[0 ... MAX_CPU_NUM - 1] = INITIAL_TSS};
- 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 1f(%%rip), %%rax \n\t" \
- "movq %%rax, %1 \n\t" \
- "pushq %3 \n\t" \
- "jmp __switch_to \n\t" \
- "1: \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);
|