能进入用户态程序,但无法正确进行系统调用

This commit is contained in:
fslongjin 2022-04-10 23:53:07 +08:00
parent d78db8225c
commit c09c6d4e66
6 changed files with 19 additions and 10 deletions

View File

@ -106,7 +106,7 @@ Err_Code:
ENTRY(system_call) ENTRY(system_call)
// sysenter // sysenter
sti; sti;
hlt
subq $0x38, %rsp subq $0x38, %rsp
cld; cld;

View File

@ -168,7 +168,7 @@ void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code)
// 13 #GP 通用保护性异常 // 13 #GP 通用保护性异常
void do_general_protection(struct pt_regs *regs, unsigned long error_code) void do_general_protection(struct pt_regs *regs, unsigned long error_code)
{ {
hlt();
kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
if (error_code & 0x01) if (error_code & 0x01)
printk_color(RED, BLACK, "The exception occurred during delivery of an event external to the program,such as an interrupt or an earlier exception.\n"); printk_color(RED, BLACK, "The exception occurred during delivery of an event external to the program,such as an interrupt or an earlier exception.\n");

View File

@ -51,10 +51,18 @@ void user_level_function()
// kinfo("Program (user_level_function) is runing..."); // kinfo("Program (user_level_function) is runing...");
// kinfo("Try to enter syscall id 15..."); // kinfo("Try to enter syscall id 15...");
// enter_syscall(15, 0, 0, 0, 0, 0, 0, 0, 0); // enter_syscall(15, 0, 0, 0, 0, 0, 0, 0, 0);
hlt();
enter_syscall(SYS_PRINTF, (ul) "test_sys_printf\n", 0, 0, 0, 0, 0, 0, 0); //enter_syscall(SYS_PRINTF, (ul) "test_sys_printf\n", 0, 0, 0, 0, 0, 0, 0);
long ret = 0;
// color_printk(RED,BLACK,"user_level_function task is running\n");
char string[]="Hello World!\n";
kinfo("Return from syscall id 15..."); __asm__ __volatile__ ( "leaq sysexit_return_address(%%rip), %%rdx \n\t"
"movq %%rsp, %%rcx \n\t"
"sysenter \n\t"
"sysexit_return_address: \n\t"
:"=a"(ret):"0"(1),"D"(string):"memory");
//kinfo("Return from syscall id 15...");
while (1) while (1)
; ;
@ -83,7 +91,7 @@ ul do_execve(struct pt_regs *regs)
uint64_t addr = 0x800000UL; uint64_t addr = 0x800000UL;
unsigned long * tmp = phys_2_virt((unsigned long *)((unsigned long)get_CR3() & (~0xfffUL)) + (( addr>> PAGE_GDT_SHIFT) & 0x1ff)); unsigned long * tmp = phys_2_virt((unsigned long *)((unsigned long)get_CR3() & (~0xfffUL)) + (( addr>> PAGE_GDT_SHIFT) & 0x1ff));
unsigned long * virtual = kmalloc(PAGE_4K_SIZE, 0); unsigned long * virtual = kmalloc(PAGE_4K_SIZE, 0);
set_pml4t(tmp, mk_pml4t(virt_2_phys(virtual), PAGE_USER_PGT)); set_pml4t(tmp, mk_pml4t(virt_2_phys(virtual), PAGE_USER_PGT));

View File

@ -107,6 +107,6 @@ void sched_init()
memset(&sched_cfs_ready_queue, 0, sizeof(struct sched_queue_t)); memset(&sched_cfs_ready_queue, 0, sizeof(struct sched_queue_t));
list_init(&sched_cfs_ready_queue.proc_queue.list); list_init(&sched_cfs_ready_queue.proc_queue.list);
sched_cfs_ready_queue.count = 1; // 因为存在IDLE进程因此为1 sched_cfs_ready_queue.count = 1; // 因为存在IDLE进程因此为1
sched_cfs_ready_queue.cpu_exec_proc_jiffies = 4; sched_cfs_ready_queue.cpu_exec_proc_jiffies = 10;
sched_cfs_ready_queue.proc_queue.virtual_runtime = 0x7fffffffffffffff; sched_cfs_ready_queue.proc_queue.virtual_runtime = 0x7fffffffffffffff;
} }

View File

@ -31,7 +31,7 @@ void smp_init()
//*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码) //*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
// 将引导程序复制到物理地址0x20000处 // 将引导程序复制到物理地址0x20000处
memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start); memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
// 设置多核IPI中断门 // 设置多核IPI中断门
for (int i = 200; i < 210; ++i) for (int i = 200; i < 210; ++i)
set_intr_gate(i, 2, SMP_interrupt_table[i - 200]); set_intr_gate(i, 2, SMP_interrupt_table[i - 200]);
@ -73,7 +73,7 @@ void smp_init()
// 由于ap处理器初始化过程需要用到0x00处的地址因此初始化完毕后才取消内存地址的重映射 // 由于ap处理器初始化过程需要用到0x00处的地址因此初始化完毕后才取消内存地址的重映射
//todo: 取消低0-2M的地址映射 //todo: 取消低0-2M的地址映射
for (int i = 1; i < 128; ++i) for (int i = 0; i < 128; ++i)
{ {
*(ul *)(phys_2_virt(global_CR3) + i) = 0UL; *(ul *)(phys_2_virt(global_CR3) + i) = 0UL;
@ -112,7 +112,7 @@ void smp_ap_start()
sti(); sti();
kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table)); kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table));
memset(current_pcb, 0, sizeof(struct process_control_block));
spin_unlock(&multi_core_starting_lock); spin_unlock(&multi_core_starting_lock);
while (1) // 这里要循环hlt原因是当收到中断后核心会被唤醒处理完中断之后不会自动hlt while (1) // 这里要循环hlt原因是当收到中断后核心会被唤醒处理完中断之后不会自动hlt
hlt(); hlt();

View File

@ -65,6 +65,7 @@ long enter_syscall(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, u
*/ */
ul sys_printf(struct pt_regs *regs) ul sys_printf(struct pt_regs *regs)
{ {
while(1);
if(regs->r9 == 0 &&regs->r10 == 0) if(regs->r9 == 0 &&regs->r10 == 0)
printk((char*)regs->r8); printk((char*)regs->r8);
else printk_color(regs->r9, regs->r10, (char*)regs->r8); else printk_color(regs->r9, regs->r10, (char*)regs->r8);