123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #include "process.h"
- #include "../exception/gate.h"
- #include "../common/printk.h"
- #include "../common/kprint.h"
- #include "../syscall/syscall.h"
- #include "../syscall/syscall_num.h"
- void __switch_to(struct process_control_block *prev, struct process_control_block *next)
- {
- initial_tss[0].rsp0 = next->thread->rbp;
- set_tss64(TSS64_Table, initial_tss[0].rsp0, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1,
- initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7);
- __asm__ __volatile__("movq %%fs, %0 \n\t"
- : "=a"(prev->thread->fs));
- __asm__ __volatile__("movq %%gs, %0 \n\t"
- : "=a"(prev->thread->gs));
- __asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
- __asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
- }
- void user_level_function()
- {
- kinfo("Program (user_level_function) is runing...");
- kinfo("Try to enter syscall id 15...");
- enter_syscall(15, 0, 0, 0, 0, 0, 0, 0, 0);
- enter_syscall(SYS_PRINTF, (ul) "test_sys_printf\n", 0, 0, 0, 0, 0, 0, 0);
- kinfo("Return from syscall id 15...");
- while (1)
- ;
- }
- ul do_execve(struct pt_regs *regs)
- {
-
- regs->rdx = 0x800000;
- regs->rcx = 0xa00000;
- regs->rax = 1;
- regs->ds = 0;
- regs->es = 0;
- kdebug("do_execve is running...");
-
- memcpy((void *)0x800000, user_level_function, 1024);
- return 0;
- }
- ul initial_kernel_thread(ul arg)
- {
- kinfo("initial proc running...\targ:%#018lx", arg);
- struct pt_regs *regs;
- current_pcb->thread->rip = (ul)ret_from_system_call;
- current_pcb->thread->rsp = (ul)current_pcb + STACK_SIZE - sizeof(struct pt_regs);
- regs = (struct pt_regs *)current_pcb->thread->rsp;
-
- __asm__ __volatile__("movq %1, %%rsp \n\t"
- "pushq %2 \n\t"
- "jmp do_execve \n\t" ::"D"(regs),
- "m"(current_pcb->thread->rsp), "m"(current_pcb->thread->rip)
- : "memory");
- return 1;
- }
- ul do_exit(ul code)
- {
- kinfo("thread_exiting..., code is %#018lx.", code);
- while (1)
- ;
- }
- extern void kernel_thread_func(void);
- __asm__(
- "kernel_thread_func: \n\t"
- " popq %r15 \n\t"
- " popq %r14 \n\t"
- " popq %r13 \n\t"
- " popq %r12 \n\t"
- " popq %r11 \n\t"
- " popq %r10 \n\t"
- " popq %r9 \n\t"
- " popq %r8 \n\t"
- " popq %rbx \n\t"
- " popq %rcx \n\t"
- " popq %rdx \n\t"
- " popq %rsi \n\t"
- " popq %rdi \n\t"
- " popq %rbp \n\t"
- " popq %rax \n\t"
- " movq %rax, %ds \n\t"
- " popq %rax \n\t"
- " movq %rax, %es \n\t"
- " popq %rax \n\t"
- " addq $0x38, %rsp \n\t"
-
- " movq %rdx, %rdi \n\t"
- " callq *%rbx \n\t"
- " movq %rax, %rdi \n\t"
- " callq do_exit \n\t");
- int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags)
- {
- struct pt_regs regs;
- memset(®s, 0, sizeof(regs));
-
- regs.rbx = (ul)fn;
-
- regs.rdx = (ul)arg;
- regs.ds = KERNEL_DS;
- regs.es = KERNEL_DS;
- regs.cs = KERNEL_CS;
- regs.ss = KERNEL_DS;
-
- regs.rflags = (1 << 9);
-
- regs.rip = (ul)kernel_thread_func;
- return do_fork(®s, flags, 0, 0);
- }
- void process_init()
- {
- initial_mm.pgd = (pml4t_t *)global_CR3;
- initial_mm.code_addr_start = memory_management_struct.kernel_code_start;
- initial_mm.code_addr_end = memory_management_struct.kernel_code_end;
- initial_mm.data_addr_start = (ul)&_data;
- initial_mm.data_addr_end = memory_management_struct.kernel_data_end;
- initial_mm.rodata_addr_start = (ul)&_rodata;
- initial_mm.rodata_addr_end = (ul)&_erodata;
- initial_mm.brk_start = 0;
- initial_mm.brk_end = memory_management_struct.kernel_end;
- initial_mm.stack_start = _stack_start;
-
- set_tss64(TSS64_Table, initial_thread.rbp, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1, initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7);
- initial_tss[0].rsp0 = initial_thread.rbp;
-
- list_init(&initial_proc_union.pcb.list);
- kernel_thread(initial_kernel_thread, 10, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);
- initial_proc_union.pcb.state = PROC_RUNNING;
-
- struct process_control_block *p = container_of(list_next(¤t_pcb->list), struct process_control_block, list);
-
- switch_proc(current_pcb, p);
- }
- unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size)
- {
- struct process_control_block *tsk = NULL;
-
- struct Page *pp = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED | PAGE_KERNEL);
- tsk = (struct process_control_block *)phys_2_virt(pp->addr_phys);
- memset(tsk, 0, sizeof(*tsk));
-
- *tsk = *current_pcb;
-
- list_init(&tsk->list);
- list_add(&initial_proc_union.pcb.list, &tsk->list);
- ++(tsk->pid);
- tsk->state = PROC_UNINTERRUPTIBLE;
-
- struct thread_struct *thd = (struct thread_struct *)(tsk + 1);
- tsk->thread = thd;
-
- memcpy((void *)((ul)tsk + STACK_SIZE - sizeof(struct pt_regs)), regs, sizeof(struct pt_regs));
-
- thd->rbp = (ul)tsk + STACK_SIZE;
- thd->rip = regs->rip;
- thd->rsp = (ul)tsk + STACK_SIZE - sizeof(struct pt_regs);
- thd->fs = KERNEL_DS;
- thd->gs = KERNEL_DS;
-
- if (!(tsk->flags & PF_KTHREAD))
- thd->rip = regs->rip = (ul)ret_from_system_call;
- tsk->state = PROC_RUNNING;
- return 0;
- }
|