2022-02-10 05:45:38 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @file process.h
|
|
|
|
|
|
* @author longjin
|
|
|
|
|
|
* @brief 进程
|
|
|
|
|
|
* @date 2022-01-29
|
|
|
|
|
|
*
|
|
|
|
|
|
* @copyright Copyright (c) 2022
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2022-07-17 07:41:19 +00:00
|
|
|
|
#include <common/cpu.h>
|
|
|
|
|
|
#include <common/glib.h>
|
|
|
|
|
|
#include <syscall/syscall.h>
|
2022-02-10 05:45:38 +00:00
|
|
|
|
#include "ptrace.h"
|
2022-05-04 15:20:39 +00:00
|
|
|
|
#include <common/errno.h>
|
2022-04-26 16:39:02 +00:00
|
|
|
|
#include <filesystem/VFS/VFS.h>
|
2022-07-31 06:17:26 +00:00
|
|
|
|
#include <common/wait_queue.h>
|
2022-08-11 10:11:10 +00:00
|
|
|
|
#include <mm/mm-types.h>
|
2022-08-15 08:39:10 +00:00
|
|
|
|
|
2022-08-19 17:05:15 +00:00
|
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
2022-08-14 17:42:34 +00:00
|
|
|
|
#include <arch/x86_64/current.h>
|
2022-08-15 08:39:10 +00:00
|
|
|
|
#else
|
|
|
|
|
|
#error Unsupported architecture!
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2022-08-14 17:42:34 +00:00
|
|
|
|
#include "proc-types.h"
|
2022-02-10 05:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
// 设置初始进程的PCB
|
|
|
|
|
|
#define INITIAL_PROC(proc) \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
{ \
|
|
|
|
|
|
.state = PROC_UNINTERRUPTIBLE, \
|
|
|
|
|
|
.flags = PF_KTHREAD, \
|
2022-06-09 08:27:55 +00:00
|
|
|
|
.preempt_count = 0, \
|
|
|
|
|
|
.signal = 0, \
|
|
|
|
|
|
.cpu_id = 0, \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
.mm = &initial_mm, \
|
|
|
|
|
|
.thread = &initial_thread, \
|
2022-05-06 07:29:42 +00:00
|
|
|
|
.addr_limit = 0xffffffffffffffff, \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
.pid = 0, \
|
2022-04-13 03:14:49 +00:00
|
|
|
|
.priority = 2, \
|
2022-06-09 08:27:55 +00:00
|
|
|
|
.virtual_runtime = 0, \
|
2022-05-04 15:20:39 +00:00
|
|
|
|
.fds = {0}, \
|
|
|
|
|
|
.next_pcb = &proc, \
|
2022-05-31 13:55:06 +00:00
|
|
|
|
.parent_pcb = &proc, \
|
|
|
|
|
|
.exit_code = 0, \
|
|
|
|
|
|
.wait_child_proc_exit = 0 \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
}
|
2022-02-10 05:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 任务状态段结构体
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2022-08-14 17:42:34 +00:00
|
|
|
|
|
2022-02-10 05:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
// 设置初始进程的tss
|
|
|
|
|
|
#define INITIAL_TSS \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
{ \
|
|
|
|
|
|
.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 \
|
|
|
|
|
|
}
|
2022-04-13 03:14:49 +00:00
|
|
|
|
|
2022-08-14 17:42:34 +00:00
|
|
|
|
|
2022-02-10 05:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
#define GET_CURRENT_PCB \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
"movq %rsp, %rbx \n\t" \
|
|
|
|
|
|
"andq $-32768, %rbx\n\t"
|
2022-02-10 05:45:38 +00:00
|
|
|
|
|
2022-04-10 13:30:16 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 切换进程上下文
|
|
|
|
|
|
* 先把rbp和rax保存到栈中,然后将rsp和rip保存到prev的thread结构体中
|
|
|
|
|
|
* 然后调用__switch_to切换栈,配置其他信息,最后恢复下一个进程的rax rbp。
|
|
|
|
|
|
*/
|
2022-02-12 14:14:51 +00:00
|
|
|
|
|
|
|
|
|
|
#define switch_proc(prev, next) \
|
|
|
|
|
|
do \
|
|
|
|
|
|
{ \
|
2022-04-26 16:39:02 +00:00
|
|
|
|
__asm__ __volatile__("pushq %%rbp \n\t" \
|
2022-04-11 09:15:24 +00:00
|
|
|
|
"pushq %%rax \n\t" \
|
|
|
|
|
|
"movq %%rsp, %0 \n\t" \
|
|
|
|
|
|
"movq %2, %%rsp \n\t" \
|
2022-04-26 16:39:02 +00:00
|
|
|
|
"leaq switch_proc_ret_addr(%%rip), %%rax \n\t" \
|
2022-04-11 09:15:24 +00:00
|
|
|
|
"movq %%rax, %1 \n\t" \
|
|
|
|
|
|
"pushq %3 \n\t" \
|
|
|
|
|
|
"jmp __switch_to \n\t" \
|
2022-04-26 16:39:02 +00:00
|
|
|
|
"switch_proc_ret_addr: \n\t" \
|
2022-04-11 09:15:24 +00:00
|
|
|
|
"popq %%rax \n\t" \
|
|
|
|
|
|
"popq %%rbp \n\t" \
|
2022-02-12 14:14:51 +00:00
|
|
|
|
: "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
|
|
|
|
|
|
: "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
|
|
|
|
|
|
: "memory"); \
|
|
|
|
|
|
} while (0)
|
2022-02-10 05:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 初始化系统的第一个进程
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
void process_init();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief fork当前进程
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param regs 新的寄存器值
|
|
|
|
|
|
* @param clone_flags 克隆标志
|
|
|
|
|
|
* @param stack_start 堆栈开始地址
|
|
|
|
|
|
* @param stack_size 堆栈大小
|
|
|
|
|
|
* @return unsigned long
|
|
|
|
|
|
*/
|
2022-04-13 03:14:49 +00:00
|
|
|
|
unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size);
|
|
|
|
|
|
|
2022-05-04 15:20:39 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 根据pid获取进程的pcb
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param pid
|
|
|
|
|
|
* @return struct process_control_block*
|
|
|
|
|
|
*/
|
|
|
|
|
|
struct process_control_block *process_get_pcb(long pid);
|
|
|
|
|
|
|
2022-05-20 14:53:47 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 将进程加入到调度器的就绪队列中
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param pcb 进程的pcb
|
|
|
|
|
|
*/
|
|
|
|
|
|
void process_wakeup(struct process_control_block *pcb);
|
|
|
|
|
|
|
2022-07-12 04:01:51 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 将进程加入到调度器的就绪队列中,并标志当前进程需要被调度
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param pcb 进程的pcb
|
|
|
|
|
|
*/
|
|
|
|
|
|
void process_wakeup_immediately(struct process_control_block *pcb);
|
|
|
|
|
|
|
2022-05-31 13:55:06 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 使当前进程去执行新的代码
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param regs 当前进程的寄存器
|
|
|
|
|
|
* @param path 可执行程序的路径
|
|
|
|
|
|
* @param argv 参数列表
|
|
|
|
|
|
* @param envp 环境变量
|
|
|
|
|
|
* @return ul 错误码
|
|
|
|
|
|
*/
|
|
|
|
|
|
ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[]);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 释放进程的页表
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param pcb 要被释放页表的进程
|
|
|
|
|
|
* @return uint64_t
|
|
|
|
|
|
*/
|
|
|
|
|
|
uint64_t process_exit_mm(struct process_control_block *pcb);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 进程退出时执行的函数
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param code 返回码
|
|
|
|
|
|
* @return ul
|
|
|
|
|
|
*/
|
|
|
|
|
|
ul process_do_exit(ul code);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 当子进程退出后向父进程发送通知
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
void process_exit_notify();
|
|
|
|
|
|
|
2022-07-26 07:44:01 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 初始化内核进程
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param fn 目标程序的地址
|
|
|
|
|
|
* @param arg 向目标程序传入的参数
|
|
|
|
|
|
* @param flags
|
|
|
|
|
|
* @return int
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags);
|
|
|
|
|
|
|
2022-05-04 15:20:39 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 切换页表
|
|
|
|
|
|
* @param prev 前一个进程的pcb
|
|
|
|
|
|
* @param next 下一个进程的pcb
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2022-05-05 06:14:34 +00:00
|
|
|
|
#define process_switch_mm(next_pcb) \
|
|
|
|
|
|
do \
|
|
|
|
|
|
{ \
|
|
|
|
|
|
asm volatile("movq %0, %%cr3 \n\t" ::"r"(next_pcb->mm->pgd) \
|
|
|
|
|
|
: "memory"); \
|
2022-05-04 15:20:39 +00:00
|
|
|
|
} while (0)
|
2022-08-11 10:11:10 +00:00
|
|
|
|
// flush_tlb();
|
2022-05-04 15:20:39 +00:00
|
|
|
|
|
2022-04-13 09:58:06 +00:00
|
|
|
|
// 获取当前cpu id
|
|
|
|
|
|
#define proc_current_cpu_id (current_pcb->cpu_id)
|
|
|
|
|
|
|
|
|
|
|
|
extern unsigned long head_stack_start; // 导出内核层栈基地址(定义在head.S)
|
|
|
|
|
|
extern ul _stack_start;
|
2022-04-14 11:26:46 +00:00
|
|
|
|
extern void ret_from_intr(void); // 导出从中断返回的函数(定义在entry.S)
|
2022-04-13 03:14:49 +00:00
|
|
|
|
|
|
|
|
|
|
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;
|
2022-05-31 13:55:06 +00:00
|
|
|
|
extern struct process_control_block *initial_proc[MAX_CPU_NUM];
|
2022-08-20 13:47:41 +00:00
|
|
|
|
int process_fd_alloc(struct vfs_file_t *file);
|