改用int250作为系统调用

This commit is contained in:
fslongjin
2022-04-12 11:54:44 +08:00
parent 011246281a
commit 2d7b2b7048
15 changed files with 236 additions and 104 deletions

View File

@ -1,11 +1,14 @@
#include "syscall.h"
#include "../process/process.h"
#include <exception/gate.h>
#include <exception/irq.h>
// 导出系统调用入口函数定义在entry.S中
extern void system_call(void);
extern void syscall_int(void);
/**
* @brief 系统调用函数从entry.S中跳转到这里
* @brief sysenter的系统调用函数从entry.S中跳转到这里
*
* @param regs 3特权级下的寄存器值,rax存储系统调用号
* @return ul 对应的系统调用函数的地址
@ -22,17 +25,19 @@ ul system_call_function(struct pt_regs *regs)
void syscall_init()
{
kinfo("Initializing syscall...");
/*
// 向MSR寄存器组中的 IA32_SYSENTER_CS寄存器写入内核的代码段的地址
wrmsr(0x174, KERNEL_CS);
// 向MSR寄存器组中的 IA32_SYSENTER_ESP寄存器写入内核进程的rbp在syscall入口中会将rsp减去相应的数值
wrmsr(0x175, current_pcb->thread->rbp);
// 向MSR寄存器组中的 IA32_SYSENTER_EIP寄存器写入系统调用入口的地址。
wrmsr(0x176, (ul)system_call);
*/
set_system_trap_gate(250, 0, syscall_intr_table[0]); // 系统调用门
}
/*
long enter_syscall(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7)
{
long err_code;
@ -53,12 +58,66 @@ long enter_syscall(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, u
: "memory", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rcx", "rdx");
return err_code;
}
*/
ul enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7)
{
long err_code;
__asm__ __volatile__(
"movq %2, %%r8 \n\t"
"movq %3, %%r9 \n\t"
"movq %4, %%r10 \n\t"
"movq %5, %%r11 \n\t"
"movq %6, %%r12 \n\t"
"movq %7, %%r13 \n\t"
"movq %8, %%r14 \n\t"
"movq %9, %%r15 \n\t"
"int $0x80 \n\t"
: "=a"(err_code)
: "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5), "m"(arg6), "m"(arg7)
: "memory", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15");
return err_code;
}
/**
* @brief 通过中断进入系统调用
*
* @param syscall_id
* @param arg0
* @param arg1
* @param arg2
* @param arg3
* @param arg4
* @param arg5
* @param arg6
* @param arg7
* @return long
*/
/*
long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7)
{
long err_code;
__asm__ __volatile__(
"movq %2, %%r8 \n\t"
"movq %3, %%r9 \n\t"
"movq %4, %%r10 \n\t"
"movq %5, %%r11 \n\t"
"movq %6, %%r12 \n\t"
"movq %7, %%r13 \n\t"
"movq %8, %%r14 \n\t"
"movq %9, %%r15 \n\t"
"int $0x80 \n\t"
: "=a"(err_code)
: "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5), "m"(arg6), "m"(arg7)
: "memory", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rcx", "rdx");
return err_code;
}
*/
/**
* @brief 打印字符串的系统调用
*
*
* 当arg1和arg2均为0时打印黑底白字否则按照指定的前景色和背景色来打印
*
*
* @param regs 寄存器
* @param arg0 要打印的字符串
* @param arg1 前景色
@ -67,11 +126,20 @@ long enter_syscall(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, u
*/
ul sys_printf(struct pt_regs *regs)
{
//if(regs->r9 == 0 &&regs->r10 == 0)
// printk((char*)regs->r8);
//else printk_color(regs->r9, regs->r10, (char*)regs->r8);
printk_color(BLACK,WHITE,(char *)regs->rdi);
// if(regs->r9 == 0 &&regs->r10 == 0)
// printk((char*)regs->r8);
// else printk_color(regs->r9, regs->r10, (char*)regs->r8);
printk_color(BLACK, WHITE, (char *)regs->r8);
return 0;
}
// 系统调用的内核入口程序
void do_syscall_int(struct pt_regs *regs, unsigned long error_code)
{
ul ret = system_call_table[regs->rax](regs);
__asm__ __volatile__("movq %0, %%rax \n\t" ::"r"(ret)
: "memory");
}

View File

@ -9,8 +9,6 @@
#define ESYSCALL_NOT_EXISTS 1
typedef unsigned long (*system_call_t)(struct pt_regs *regs);
extern void ret_from_system_call(void); // 导出从系统调用返回的函数定义在entry.S
@ -27,7 +25,8 @@ void syscall_init();
* @param syscall_id 系统调用id
* @return long 错误码
*/
long enter_syscall(ul syscall_id,ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7);
long enter_syscall(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7);
ul enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7);
/**
* @brief 系统调用不存在时的处理函数
@ -43,9 +42,9 @@ ul system_call_not_exists(struct pt_regs *regs)
/**
* @brief 打印字符串的系统调用
*
*
* 当arg1和arg2均为0时打印黑底白字否则按照指定的前景色和背景色来打印
*
*
* @param regs 寄存器
* @param arg0 要打印的字符串
* @param arg1 前景色
@ -54,9 +53,11 @@ ul system_call_not_exists(struct pt_regs *regs)
*/
ul sys_printf(struct pt_regs *regs);
// 系统调用的内核入口程序
void do_syscall_int(struct pt_regs *regs, unsigned long error_code);
system_call_t system_call_table[MAX_SYSTEM_CALL_NUM] =
{
[0] = system_call_not_exists,
[1] = sys_printf,
[2 ... MAX_SYSTEM_CALL_NUM - 1] = system_call_not_exists
};
[2 ... MAX_SYSTEM_CALL_NUM - 1] = system_call_not_exists};