1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186 |
- #include "process.h"
- #include <common/compiler.h>
- #include <common/elf.h>
- #include <common/kprint.h>
- #include <common/kthread.h>
- #include <common/printk.h>
- #include <common/spinlock.h>
- #include <common/stdio.h>
- #include <common/string.h>
- #include <common/sys/wait.h>
- #include <common/time.h>
- #include <common/unistd.h>
- #include <debug/bug.h>
- #include <debug/traceback/traceback.h>
- #include <driver/disk/ahci/ahci.h>
- #include <driver/usb/usb.h>
- #include <driver/video/video.h>
- #include <exception/gate.h>
- #include <filesystem/devfs/devfs.h>
- #include <filesystem/fat32/fat32.h>
- #include <filesystem/rootfs/rootfs.h>
- #include <mm/slab.h>
- #include <sched/sched.h>
- #include <syscall/syscall.h>
- #include <syscall/syscall_num.h>
- #include <ktest/ktest.h>
- #include <mm/mmio.h>
- #include <common/lz4.h>
- spinlock_t process_global_pid_write_lock;
- long process_global_pid = 1;
- extern void system_call(void);
- extern void kernel_thread_func(void);
- ul _stack_start;
- extern struct mm_struct initial_mm;
- 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};
- 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 tss_struct initial_tss[MAX_CPU_NUM] = {[0 ... MAX_CPU_NUM - 1] = INITIAL_TSS};
- uint64_t process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb);
- uint64_t process_copy_files(uint64_t clone_flags, struct process_control_block *pcb);
- uint64_t process_exit_files(struct process_control_block *pcb);
- uint64_t process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb);
- uint64_t process_exit_mm(struct process_control_block *pcb);
- uint64_t process_copy_thread(uint64_t clone_flags, struct process_control_block *pcb, uint64_t stack_start, uint64_t stack_size, struct pt_regs *current_regs);
- void process_exit_thread(struct process_control_block *pcb);
- #pragma GCC push_options
- #pragma GCC optimize("O0")
- void __switch_to(struct process_control_block *prev, struct process_control_block *next)
- {
- initial_tss[proc_current_cpu_id].rsp0 = next->thread->rbp;
-
-
-
- __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));
- }
- #pragma GCC pop_options
- struct vfs_file_t *process_open_exec_file(char *path)
- {
- struct vfs_dir_entry_t *dentry = NULL;
- struct vfs_file_t *filp = NULL;
- dentry = vfs_path_walk(path, 0);
- if (dentry == NULL)
- return (void *)-ENOENT;
- if (dentry->dir_inode->attribute == VFS_IF_DIR)
- return (void *)-ENOTDIR;
- filp = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
- if (filp == NULL)
- return (void *)-ENOMEM;
- filp->position = 0;
- filp->mode = 0;
- filp->dEntry = dentry;
- filp->mode = ATTR_READ_ONLY;
- filp->file_ops = dentry->dir_inode->file_ops;
- return filp;
- }
- static int process_load_elf_file(struct pt_regs *regs, char *path)
- {
- int retval = 0;
- struct vfs_file_t *filp = process_open_exec_file(path);
- if ((long)filp <= 0 && (long)filp >= -255)
- {
-
- return (unsigned long)filp;
- }
- void *buf = kmalloc(PAGE_4K_SIZE, 0);
- memset(buf, 0, PAGE_4K_SIZE);
- uint64_t pos = 0;
- pos = filp->file_ops->lseek(filp, 0, SEEK_SET);
- retval = filp->file_ops->read(filp, (char *)buf, sizeof(Elf64_Ehdr), &pos);
- retval = 0;
- if (!elf_check(buf))
- {
- kerror("Not an ELF file: %s", path);
- retval = -ENOTSUP;
- goto load_elf_failed;
- }
- #if ARCH(X86_64)
-
- if (((Elf32_Ehdr *)buf)->e_ident[EI_CLASS] != ELFCLASS64)
- {
- kdebug("((Elf32_Ehdr *)buf)->e_ident[EI_CLASS]=%d", ((Elf32_Ehdr *)buf)->e_ident[EI_CLASS]);
- retval = -EUNSUPPORTED;
- goto load_elf_failed;
- }
- Elf64_Ehdr ehdr = *(Elf64_Ehdr *)buf;
-
- if (ehdr.e_machine != EM_AMD64)
- {
- kerror("e_machine=%d", ehdr.e_machine);
- retval = -EUNSUPPORTED;
- goto load_elf_failed;
- }
- #else
- #error Unsupported architecture!
- #endif
- if (ehdr.e_type != ET_EXEC)
- {
- kerror("Not executable file! filename=%s\tehdr->e_type=%d", path, ehdr.e_type);
- retval = -EUNSUPPORTED;
- goto load_elf_failed;
- }
-
- regs->rip = ehdr.e_entry;
- current_pcb->mm->code_addr_start = ehdr.e_entry;
-
-
- pos = ehdr.e_phoff;
-
- pos = filp->file_ops->lseek(filp, pos, SEEK_SET);
- filp->file_ops->read(filp, (char *)buf, (uint64_t)ehdr.e_phentsize * (uint64_t)ehdr.e_phnum, &pos);
- if ((unsigned long)filp <= 0)
- {
- kdebug("(unsigned long)filp=%d", (long)filp);
- retval = -ENOEXEC;
- goto load_elf_failed;
- }
- Elf64_Phdr *phdr = buf;
-
- for (int i = 0; i < ehdr.e_phnum; ++i, ++phdr)
- {
-
-
- if (phdr->p_type != PT_LOAD)
- continue;
- int64_t remain_mem_size = phdr->p_memsz;
- int64_t remain_file_size = phdr->p_filesz;
- pos = phdr->p_offset;
- uint64_t virt_base = 0;
- uint64_t beginning_offset = 0;
- if (remain_mem_size >= PAGE_2M_SIZE)
- virt_base = phdr->p_vaddr & PAGE_2M_MASK;
- else
- virt_base = phdr->p_vaddr & PAGE_4K_MASK;
- beginning_offset = phdr->p_vaddr - virt_base;
- remain_mem_size += beginning_offset;
- while (remain_mem_size > 0)
- {
-
- int64_t map_size = 0;
- if (remain_mem_size >= PAGE_2M_SIZE)
- {
- uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
- struct vm_area_struct *vma = NULL;
- int ret = mm_create_vma(current_pcb->mm, virt_base, PAGE_2M_SIZE, VM_USER | VM_ACCESS_FLAGS, NULL, &vma);
-
- if (ret == -EEXIST)
- free_pages(Phy_to_2M_Page(pa), 1);
- else
- mm_map(current_pcb->mm, virt_base, PAGE_2M_SIZE, pa);
-
- io_mfence();
- memset((void *)virt_base, 0, PAGE_2M_SIZE);
- map_size = PAGE_2M_SIZE;
- }
- else
- {
-
- map_size = ALIGN(remain_mem_size, PAGE_4K_SIZE);
-
- for (uint64_t off = 0; off < map_size; off += PAGE_4K_SIZE)
- {
- uint64_t paddr = virt_2_phys((uint64_t)kmalloc(PAGE_4K_SIZE, 0));
- struct vm_area_struct *vma = NULL;
- int val = mm_create_vma(current_pcb->mm, virt_base + off, PAGE_4K_SIZE, VM_USER | VM_ACCESS_FLAGS, NULL, &vma);
-
- if (val == -EEXIST)
- kfree(phys_2_virt(paddr));
- else
- mm_map(current_pcb->mm, virt_base + off, PAGE_4K_SIZE, paddr);
-
- io_mfence();
- memset((void *)(virt_base + off), 0, PAGE_4K_SIZE);
- }
- }
- pos = filp->file_ops->lseek(filp, pos, SEEK_SET);
- int64_t val = 0;
- if (remain_file_size > 0)
- {
- int64_t to_trans = (remain_file_size > PAGE_2M_SIZE) ? PAGE_2M_SIZE : remain_file_size;
- val = filp->file_ops->read(filp, (char *)(virt_base + beginning_offset), to_trans, &pos);
- }
- if (val < 0)
- goto load_elf_failed;
- remain_mem_size -= map_size;
- remain_file_size -= val;
- virt_base += map_size;
- }
- }
-
- regs->rsp = current_pcb->mm->stack_start;
- regs->rbp = current_pcb->mm->stack_start;
- {
- struct vm_area_struct *vma = NULL;
- uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
- int val = mm_create_vma(current_pcb->mm, current_pcb->mm->stack_start - PAGE_2M_SIZE, PAGE_2M_SIZE, VM_USER | VM_ACCESS_FLAGS, NULL, &vma);
- if (val == -EEXIST)
- free_pages(Phy_to_2M_Page(pa), 1);
- else
- mm_map_vma(vma, pa, 0, PAGE_2M_SIZE);
- }
-
- memset((void *)(current_pcb->mm->stack_start - PAGE_2M_SIZE), 0, PAGE_2M_SIZE);
- load_elf_failed:;
- if (buf != NULL)
- kfree(buf);
- return retval;
- }
- #pragma GCC push_options
- #pragma GCC optimize("O0")
- ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[])
- {
-
-
-
- if (current_pcb->flags & PF_VFORK)
- {
-
-
- struct mm_struct *new_mms = (struct mm_struct *)kmalloc(sizeof(struct mm_struct), 0);
- memset(new_mms, 0, sizeof(struct mm_struct));
- current_pcb->mm = new_mms;
-
- new_mms->pgd = (pml4t_t *)virt_2_phys(kmalloc(PAGE_4K_SIZE, 0));
-
- memset(phys_2_virt(new_mms->pgd), 0, PAGE_4K_SIZE / 2);
-
- memcpy(phys_2_virt(new_mms->pgd) + 256, phys_2_virt(initial_proc[proc_current_cpu_id]) + 256, PAGE_4K_SIZE / 2);
- }
-
- unsigned long stack_start_addr = 0x6ffff0a00000UL;
- const uint64_t brk_start_addr = 0x700000000000UL;
- process_switch_mm(current_pcb);
-
- if (!(current_pcb->flags & PF_KTHREAD))
- current_pcb->addr_limit = USER_MAX_LINEAR_ADDR;
- current_pcb->mm->code_addr_end = 0;
- current_pcb->mm->data_addr_start = 0;
- current_pcb->mm->data_addr_end = 0;
- current_pcb->mm->rodata_addr_start = 0;
- current_pcb->mm->rodata_addr_end = 0;
- current_pcb->mm->bss_start = 0;
- current_pcb->mm->bss_end = 0;
- current_pcb->mm->brk_start = brk_start_addr;
- current_pcb->mm->brk_end = brk_start_addr;
- current_pcb->mm->stack_start = stack_start_addr;
-
- process_exit_files(current_pcb);
-
- current_pcb->flags &= ~PF_VFORK;
-
- int tmp = process_load_elf_file(regs, path);
- if (tmp < 0)
- goto exec_failed;
-
- if (argv != NULL)
- {
- int argc = 0;
-
- char **dst_argv = (char **)(stack_start_addr - (sizeof(char **) << 3));
- uint64_t str_addr = (uint64_t)dst_argv;
- for (argc = 0; argc < 8 && argv[argc] != NULL; ++argc)
- {
- if (*argv[argc] == NULL)
- break;
-
- int argv_len = strnlen_user(argv[argc], 1023) + 1;
- strncpy((char *)(str_addr - argv_len), argv[argc], argv_len - 1);
- str_addr -= argv_len;
- dst_argv[argc] = (char *)str_addr;
-
- ((char *)str_addr)[argv_len] = '\0';
- }
-
- stack_start_addr = str_addr - 8;
- current_pcb->mm->stack_start = stack_start_addr;
- regs->rsp = regs->rbp = stack_start_addr;
-
- regs->rdi = argc;
- regs->rsi = (uint64_t)dst_argv;
- }
-
- regs->cs = USER_CS | 3;
- regs->ds = USER_DS | 3;
- regs->ss = USER_DS | 0x3;
- regs->rflags = 0x200246;
- regs->rax = 1;
- regs->es = 0;
- return 0;
- exec_failed:;
- process_do_exit(tmp);
- }
- #pragma GCC pop_options
- #pragma GCC push_options
- #pragma GCC optimize("O0")
- ul initial_kernel_thread(ul arg)
- {
- kinfo("initial proc running...\targ:%#018lx", arg);
- scm_enable_double_buffer();
- ahci_init();
- fat32_init();
- rootfs_umount();
-
-
-
- kinfo("LZ4 lib Version=%s", LZ4_versionString());
-
- uint64_t tpid[] = {
- ktest_start(ktest_test_bitree, 0),
- ktest_start(ktest_test_kfifo, 0),
- ktest_start(ktest_test_mutex, 0),
- ktest_start(ktest_test_idr, 0),
-
- };
- kinfo("Waiting test thread exit...");
-
- for (int i = 0; i < sizeof(tpid) / sizeof(uint64_t); ++i)
- waitpid(tpid[i], NULL, NULL);
- kinfo("All test done.");
-
- struct pt_regs *regs;
-
- cli();
- current_pcb->thread->rip = (ul)ret_from_system_call;
- current_pcb->thread->rsp = (ul)current_pcb + STACK_SIZE - sizeof(struct pt_regs);
- current_pcb->thread->fs = USER_DS | 0x3;
- barrier();
- current_pcb->thread->gs = USER_DS | 0x3;
-
- current_pcb->flags &= (~PF_KTHREAD);
- kdebug("in initial_kernel_thread: flags=%ld", current_pcb->flags);
- regs = (struct pt_regs *)current_pcb->thread->rsp;
-
- current_pcb->flags = 0;
-
-
- char init_path[] = "/shell.elf";
- uint64_t addr = (uint64_t)&init_path;
- __asm__ __volatile__("movq %1, %%rsp \n\t"
- "pushq %2 \n\t"
- "jmp do_execve \n\t" ::"D"(current_pcb->thread->rsp),
- "m"(current_pcb->thread->rsp), "m"(current_pcb->thread->rip), "S"("/shell.elf"), "c"(NULL), "d"(NULL)
- : "memory");
- return 1;
- }
- #pragma GCC pop_options
- void process_exit_notify()
- {
- wait_queue_wakeup(¤t_pcb->parent_pcb->wait_child_proc_exit, PROC_INTERRUPTIBLE);
- }
- ul process_do_exit(ul code)
- {
-
- cli();
- struct process_control_block *pcb = current_pcb;
-
- process_exit_files(pcb);
- process_exit_thread(pcb);
-
- pcb->state = PROC_ZOMBIE;
- pcb->exit_code = code;
- sti();
- process_exit_notify();
- sched();
- while (1)
- pause();
- }
- pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
- {
- struct pt_regs regs;
- barrier();
- memset(®s, 0, sizeof(regs));
- barrier();
-
- regs.rbx = (ul)fn;
-
- regs.rdx = (ul)arg;
- barrier();
- regs.ds = KERNEL_DS;
- barrier();
- regs.es = KERNEL_DS;
- barrier();
- regs.cs = KERNEL_CS;
- barrier();
- regs.ss = KERNEL_DS;
- barrier();
-
- regs.rflags = (1 << 9);
- barrier();
-
- regs.rip = (ul)kernel_thread_func;
- barrier();
-
-
-
- return do_fork(®s, flags | CLONE_VM, 0, 0);
- }
- void process_init()
- {
- kinfo("Initializing process...");
- initial_tss[proc_current_cpu_id].rsp0 = initial_thread.rbp;
-
-
- spin_init(&process_global_pid_write_lock);
-
- list_init(&initial_proc_union.pcb.list);
-
- current_pcb->virtual_runtime = 0;
- barrier();
- kernel_thread(initial_kernel_thread, 10, CLONE_FS | CLONE_SIGNAL);
- barrier();
- kthread_mechanism_init();
- initial_proc_union.pcb.state = PROC_RUNNING;
- initial_proc_union.pcb.preempt_count = 0;
- initial_proc_union.pcb.cpu_id = 0;
- initial_proc_union.pcb.virtual_runtime = (1UL << 60);
-
- current_pcb->virtual_runtime = (1UL << 60);
- }
- unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size)
- {
- int retval = 0;
- struct process_control_block *tsk = NULL;
-
- tsk = (struct process_control_block *)kmalloc(STACK_SIZE, 0);
- barrier();
- if (tsk == NULL)
- {
- retval = -ENOMEM;
- return retval;
- }
- barrier();
- memset(tsk, 0, sizeof(struct process_control_block));
- io_mfence();
-
- memcpy(tsk, current_pcb, sizeof(struct process_control_block));
- tsk->worker_private = NULL;
- io_mfence();
-
- list_init(&tsk->list);
- io_mfence();
-
- if ((current_pcb->flags & PF_KTHREAD) && stack_start != 0)
- tsk->flags |= PF_KFORK;
- if (tsk->flags & PF_KTHREAD)
- {
-
- retval = kthread_set_worker_private(tsk);
- if (IS_ERR_VALUE(retval))
- goto copy_flags_failed;
- tsk->virtual_runtime = 0;
- }
- tsk->priority = 2;
- tsk->preempt_count = 0;
-
- spin_lock(&process_global_pid_write_lock);
- tsk->pid = process_global_pid++;
- barrier();
-
- tsk->next_pcb = initial_proc_union.pcb.next_pcb;
- barrier();
- initial_proc_union.pcb.next_pcb = tsk;
- barrier();
- tsk->parent_pcb = current_pcb;
- barrier();
- spin_unlock(&process_global_pid_write_lock);
- tsk->cpu_id = proc_current_cpu_id;
- tsk->state = PROC_UNINTERRUPTIBLE;
- tsk->parent_pcb = current_pcb;
- wait_queue_init(&tsk->wait_child_proc_exit, NULL);
- barrier();
- list_init(&tsk->list);
- retval = -ENOMEM;
-
- if (process_copy_flags(clone_flags, tsk))
- goto copy_flags_failed;
-
- if (process_copy_mm(clone_flags, tsk))
- goto copy_mm_failed;
-
- if (process_copy_files(clone_flags, tsk))
- goto copy_files_failed;
-
- if (process_copy_thread(clone_flags, tsk, stack_start, stack_size, regs))
- goto copy_thread_failed;
-
- retval = tsk->pid;
- tsk->flags &= ~PF_KFORK;
-
- process_wakeup(tsk);
- return retval;
- copy_thread_failed:;
-
- process_exit_thread(tsk);
- copy_files_failed:;
-
- process_exit_files(tsk);
- copy_mm_failed:;
-
- process_exit_mm(tsk);
- copy_flags_failed:;
- kfree(tsk);
- return retval;
- return 0;
- }
- struct process_control_block *process_get_pcb(long pid)
- {
- struct process_control_block *pcb = initial_proc_union.pcb.next_pcb;
-
-
- for (; pcb != &initial_proc_union.pcb; pcb = pcb->next_pcb)
- {
- if (pcb->pid == pid)
- return pcb;
- }
- return NULL;
- }
- int process_wakeup(struct process_control_block *pcb)
- {
-
- BUG_ON(pcb == NULL);
- if (pcb == current_pcb || pcb == NULL)
- return -EINVAL;
-
- if (pcb->state & PROC_RUNNING)
- return 0;
- pcb->state |= PROC_RUNNING;
- sched_enqueue(pcb);
- return 0;
- }
- int process_wakeup_immediately(struct process_control_block *pcb)
- {
- if (pcb->state & PROC_RUNNING)
- return 0;
- int retval = process_wakeup(pcb);
- if (retval != 0)
- return retval;
-
- current_pcb->flags |= PF_NEED_SCHED;
- }
- uint64_t process_copy_flags(uint64_t clone_flags, struct process_control_block *pcb)
- {
- if (clone_flags & CLONE_VM)
- pcb->flags |= PF_VFORK;
- return 0;
- }
- uint64_t process_copy_files(uint64_t clone_flags, struct process_control_block *pcb)
- {
- int retval = 0;
-
-
- if (clone_flags & CLONE_FS)
- return retval;
-
- for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
- {
- if (current_pcb->fds[i] == NULL)
- continue;
- pcb->fds[i] = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
- memcpy(pcb->fds[i], current_pcb->fds[i], sizeof(struct vfs_file_t));
- }
- return retval;
- }
- uint64_t process_exit_files(struct process_control_block *pcb)
- {
-
- if (!(pcb->flags & PF_VFORK))
- {
- for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
- {
- if (pcb->fds[i] == NULL)
- continue;
- kfree(pcb->fds[i]);
- }
- }
-
- memset(pcb->fds, 0, sizeof(struct vfs_file_t *) * PROC_MAX_FD_NUM);
- }
- uint64_t process_copy_mm(uint64_t clone_flags, struct process_control_block *pcb)
- {
- int retval = 0;
-
- if (clone_flags & CLONE_VM)
- {
- pcb->mm = current_pcb->mm;
- return retval;
- }
-
- struct mm_struct *new_mms = (struct mm_struct *)kmalloc(sizeof(struct mm_struct), 0);
- memset(new_mms, 0, sizeof(struct mm_struct));
- memcpy(new_mms, current_pcb->mm, sizeof(struct mm_struct));
- new_mms->vmas = NULL;
- pcb->mm = new_mms;
-
- new_mms->pgd = (pml4t_t *)virt_2_phys(kmalloc(PAGE_4K_SIZE, 0));
-
- memset(phys_2_virt(new_mms->pgd), 0, PAGE_4K_SIZE / 2);
-
- memcpy(phys_2_virt(new_mms->pgd) + 256, phys_2_virt(initial_proc[proc_current_cpu_id]->mm->pgd) + 256, PAGE_4K_SIZE / 2);
- uint64_t *current_pgd = (uint64_t *)phys_2_virt(current_pcb->mm->pgd);
- uint64_t *new_pml4t = (uint64_t *)phys_2_virt(new_mms->pgd);
-
- struct vm_area_struct *vma = current_pcb->mm->vmas;
- while (vma != NULL)
- {
- if (vma->vm_end > USER_MAX_LINEAR_ADDR || vma->vm_flags & VM_DONTCOPY)
- {
- vma = vma->vm_next;
- continue;
- }
- int64_t vma_size = vma->vm_end - vma->vm_start;
-
- if (vma_size > PAGE_2M_SIZE / 2)
- {
- int page_to_alloc = (PAGE_2M_ALIGN(vma_size)) >> PAGE_2M_SHIFT;
- for (int i = 0; i < page_to_alloc; ++i)
- {
- uint64_t pa = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys;
- struct vm_area_struct *new_vma = NULL;
- int ret = mm_create_vma(new_mms, vma->vm_start + i * PAGE_2M_SIZE, PAGE_2M_SIZE, vma->vm_flags, vma->vm_ops, &new_vma);
-
- if (unlikely(ret == -EEXIST))
- free_pages(Phy_to_2M_Page(pa), 1);
- else
- mm_map_vma(new_vma, pa, 0, PAGE_2M_SIZE);
- memcpy((void *)phys_2_virt(pa), (void *)(vma->vm_start + i * PAGE_2M_SIZE), (vma_size >= PAGE_2M_SIZE) ? PAGE_2M_SIZE : vma_size);
- vma_size -= PAGE_2M_SIZE;
- }
- }
- else
- {
- uint64_t map_size = PAGE_4K_ALIGN(vma_size);
- uint64_t va = (uint64_t)kmalloc(map_size, 0);
- struct vm_area_struct *new_vma = NULL;
- int ret = mm_create_vma(new_mms, vma->vm_start, map_size, vma->vm_flags, vma->vm_ops, &new_vma);
-
- if (unlikely(ret == -EEXIST))
- kfree((void *)va);
- else
- mm_map_vma(new_vma, virt_2_phys(va), 0, map_size);
- memcpy((void *)va, (void *)vma->vm_start, vma_size);
- }
- vma = vma->vm_next;
- }
- return retval;
- }
- uint64_t process_exit_mm(struct process_control_block *pcb)
- {
- if (pcb->flags & CLONE_VM)
- return 0;
- if (pcb->mm == NULL)
- {
- kdebug("pcb->mm==NULL");
- return 0;
- }
- if (pcb->mm->pgd == NULL)
- {
- kdebug("pcb->mm->pgd==NULL");
- return 0;
- }
-
- pml4t_t *current_pgd = (pml4t_t *)phys_2_virt(pcb->mm->pgd);
-
- struct vm_area_struct *vma = pcb->mm->vmas;
- while (vma != NULL)
- {
- struct vm_area_struct *cur_vma = vma;
- vma = cur_vma->vm_next;
- uint64_t pa;
-
- mm_unmap_vma(pcb->mm, cur_vma, &pa);
- uint64_t size = (cur_vma->vm_end - cur_vma->vm_start);
-
- switch (size)
- {
- case PAGE_4K_SIZE:
- kfree(phys_2_virt(pa));
- break;
- default:
- break;
- }
- vm_area_del(cur_vma);
- vm_area_free(cur_vma);
- }
-
- kfree(current_pgd);
- if (unlikely(pcb->mm->vmas != NULL))
- {
- kwarn("pcb.mm.vmas!=NULL");
- }
-
- kfree(pcb->mm);
- return 0;
- }
- static int process_rewrite_rbp(struct pt_regs *new_regs, struct process_control_block *new_pcb)
- {
- uint64_t new_top = ((uint64_t)new_pcb) + STACK_SIZE;
- uint64_t old_top = (uint64_t)(current_pcb) + STACK_SIZE;
- uint64_t *rbp = &new_regs->rbp;
- uint64_t *tmp = rbp;
-
- if ((uint64_t)*rbp >= old_top || (uint64_t)*rbp < (old_top - STACK_SIZE))
- return 0;
- while (1)
- {
-
- uint64_t delta = old_top - *rbp;
-
- uint64_t newVal = new_top - delta;
-
- if (unlikely((uint64_t)newVal >= new_top || (uint64_t)newVal < (new_top - STACK_SIZE)))
- break;
-
- *rbp = newVal;
-
- rbp = (uint64_t *)*rbp;
- }
-
- new_regs->rsp = new_top - (old_top - new_regs->rsp);
- return 0;
- }
- uint64_t process_copy_thread(uint64_t clone_flags, struct process_control_block *pcb, uint64_t stack_start, uint64_t stack_size, struct pt_regs *current_regs)
- {
-
- struct thread_struct *thd = (struct thread_struct *)(pcb + 1);
- memset(thd, 0, sizeof(struct thread_struct));
- pcb->thread = thd;
- struct pt_regs *child_regs = NULL;
-
- if (pcb->flags & PF_KFORK)
- {
-
- uint32_t size = ((uint64_t)current_pcb) + STACK_SIZE - (uint64_t)(current_regs);
- child_regs = (struct pt_regs *)(((uint64_t)pcb) + STACK_SIZE - size);
- memcpy(child_regs, (void *)current_regs, size);
- barrier();
-
- process_rewrite_rbp(child_regs, pcb);
- }
- else
- {
- child_regs = (struct pt_regs *)((uint64_t)pcb + STACK_SIZE - sizeof(struct pt_regs));
- memcpy(child_regs, current_regs, sizeof(struct pt_regs));
- barrier();
- child_regs->rsp = stack_start;
- }
-
- child_regs->rax = 0;
- if (pcb->flags & PF_KFORK)
- thd->rbp = (uint64_t)(child_regs + 1);
- else
- thd->rbp = (uint64_t)pcb + STACK_SIZE;
-
- thd->rsp = (uint64_t)child_regs;
- thd->fs = current_pcb->thread->fs;
- thd->gs = current_pcb->thread->gs;
-
- if (pcb->flags & PF_KFORK)
- thd->rip = (uint64_t)ret_from_system_call;
- else if (pcb->flags & PF_KTHREAD && (!(pcb->flags & PF_KFORK)))
- thd->rip = (uint64_t)kernel_thread_func;
- else
- thd->rip = (uint64_t)ret_from_system_call;
- return 0;
- }
- void process_exit_thread(struct process_control_block *pcb)
- {
- }
- int process_release_pcb(struct process_control_block *pcb)
- {
- kfree(pcb);
- return 0;
- }
- int process_fd_alloc(struct vfs_file_t *file)
- {
- int fd_num = -1;
- struct vfs_file_t **f = current_pcb->fds;
- for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
- {
-
- if (f[i] == NULL)
- {
- fd_num = i;
- f[i] = file;
- break;
- }
- }
- return fd_num;
- }
|