DragonOS/kernel/process/process.h

217 lines
7.0 KiB
C
Raw Normal View History

/**
* @file process.h
* @author longjin
* @brief
* @date 2022-01-29
*
* @copyright Copyright (c) 2022
*
*/
#pragma once
#include <common/cpu.h>
#include <common/glib.h>
#include <syscall/syscall.h>
#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>
#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
#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"
// 设置初始进程的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, \
2022-05-06 07:29:42 +00:00
.addr_limit = 0xffffffffffffffff, \
.pid = 0, \
2022-04-13 03:14:49 +00:00
.priority = 2, \
.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 \
}
/**
* @brief
*
*/
2022-08-14 17:42:34 +00:00
// 设置初始进程的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 \
}
2022-04-13 03:14:49 +00:00
2022-08-14 17:42:34 +00:00
#define GET_CURRENT_PCB \
"movq %rsp, %rbx \n\t" \
"andq $-32768, %rbx\n\t"
/**
* @brief
* rbp和rax保存到栈中rsp和rip保存到prev的thread结构体中
* __switch_to切换栈rax rbp
*/
#define switch_proc(prev, next) \
do \
{ \
2022-04-26 16:39:02 +00:00
__asm__ __volatile__("pushq %%rbp \n\t" \
"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" \
"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" \
"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
*
*/
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);
/**
* @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
*
*/
#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;
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);