|
@@ -9,45 +9,27 @@
|
|
|
*/
|
|
|
|
|
|
#pragma once
|
|
|
-#include <common/cpu.h>
|
|
|
-#include <common/glib.h>
|
|
|
-#include <syscall/syscall.h>
|
|
|
#include "ptrace.h"
|
|
|
+#include <common/cpu.h>
|
|
|
#include <common/errno.h>
|
|
|
-#include <filesystem/VFS/VFS.h>
|
|
|
+#include <common/glib.h>
|
|
|
#include <common/wait_queue.h>
|
|
|
+#include <filesystem/VFS/VFS.h>
|
|
|
#include <mm/mm-types.h>
|
|
|
+#include <syscall/syscall.h>
|
|
|
|
|
|
-#if ARCH(I386) || ARCH(X86_64)
|
|
|
-#include <arch/x86_64/current.h>
|
|
|
-#else
|
|
|
-#error Unsupported architecture!
|
|
|
-#endif
|
|
|
+#include <asm/current.h>
|
|
|
|
|
|
#include "proc-types.h"
|
|
|
|
|
|
// 设置初始进程的PCB
|
|
|
-#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, \
|
|
|
- .worker_private = NULL, \
|
|
|
- .policy = SCHED_NORMAL \
|
|
|
- }
|
|
|
+#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, .worker_private = NULL, .policy = SCHED_NORMAL \
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @brief 任务状态段结构体
|
|
@@ -55,28 +37,19 @@
|
|
|
*/
|
|
|
|
|
|
// 设置初始进程的tss
|
|
|
-#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 \
|
|
|
- }
|
|
|
-
|
|
|
-#define GET_CURRENT_PCB \
|
|
|
- "movq %rsp, %rbx \n\t" \
|
|
|
- "andq $-32768, %rbx\n\t"
|
|
|
+#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 \
|
|
|
+ }
|
|
|
+
|
|
|
+#define GET_CURRENT_PCB \
|
|
|
+ "movq %rsp, %rbx \n\t" \
|
|
|
+ "andq $-32768, %rbx\n\t"
|
|
|
|
|
|
/**
|
|
|
* @brief 切换进程上下文
|
|
@@ -84,24 +57,24 @@
|
|
|
* 然后调用__switch_to切换栈,配置其他信息,最后恢复下一个进程的rax rbp。
|
|
|
*/
|
|
|
|
|
|
-#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)
|
|
|
+#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)
|
|
|
|
|
|
/**
|
|
|
* @brief 初始化系统的第一个进程
|
|
@@ -118,7 +91,8 @@ void process_init();
|
|
|
* @param stack_size 堆栈大小
|
|
|
* @return unsigned long
|
|
|
*/
|
|
|
-unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);
|
|
|
+unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start,
|
|
|
+ unsigned long stack_size);
|
|
|
|
|
|
/**
|
|
|
* @brief 根据pid获取进程的pcb
|
|
@@ -196,12 +170,11 @@ int process_release_pcb(struct process_control_block *pcb);
|
|
|
* @param next 下一个进程的pcb
|
|
|
*
|
|
|
*/
|
|
|
-#define process_switch_mm(next_pcb) \
|
|
|
- do \
|
|
|
- { \
|
|
|
- asm volatile("movq %0, %%cr3 \n\t" ::"r"(next_pcb->mm->pgd) \
|
|
|
- : "memory"); \
|
|
|
- } while (0)
|
|
|
+#define process_switch_mm(next_pcb) \
|
|
|
+ do \
|
|
|
+ { \
|
|
|
+ asm volatile("movq %0, %%cr3 \n\t" ::"r"(next_pcb->mm->pgd) : "memory"); \
|
|
|
+ } while (0)
|
|
|
// flush_tlb();
|
|
|
|
|
|
// 获取当前cpu id
|