🐛 修复了无法切换进程、无法进行浮点运算的bug(将main.c中的init函数名进行修改)

This commit is contained in:
fslongjin 2022-02-12 22:14:51 +08:00
parent 26c23e0e65
commit d1671bc121
19 changed files with 501 additions and 391 deletions

View File

@ -7,6 +7,12 @@
"font.h": "c", "font.h": "c",
"trap.h": "c", "trap.h": "c",
"gate.h": "c", "gate.h": "c",
"process.h": "c" "process.h": "c",
"cpu.h": "c",
"mm.h": "c",
"glib.h": "c",
"asm.h": "c",
"memory.h": "c",
"irq.h": "c"
} }
} }

View File

@ -4,12 +4,9 @@
#define __ASM__ #define __ASM__
#define ENTRY(name)\
.global name; \
name:
#endif
// 符号名 // 符号名
#define SYMBOL_NAME(X) X #define SYMBOL_NAME(X) X
@ -23,3 +20,9 @@
#define asmlinkage __attribute__((regparm(0))) #define asmlinkage __attribute__((regparm(0)))
#define ____cacheline_aligned __attribute__((__aligned__(L1_CACHE_BYTES))) #define ____cacheline_aligned __attribute__((__aligned__(L1_CACHE_BYTES)))
#define ENTRY(name) \
.global SYMBOL_NAME(name); \
SYMBOL_NAME_LABEL(name)
#endif

View File

@ -1,4 +1,3 @@
#pragma once #pragma once
unsigned char font_ascii[256][16]= unsigned char font_ascii[256][16]=
@ -315,4 +314,3 @@ unsigned char font_ascii[256][16]=
}; };

View File

@ -63,17 +63,18 @@ static inline void list_init(struct List *list)
list->prev = list; list->prev = list;
} }
static inline void list_add(struct List *entry, struct List *node)
{
/** /**
* @brief node插入到entry后面 * @brief
* @param entry * @param entry
* @param node * @param node
*/ **/
static inline void list_add(struct List *entry, struct List *node)
{
node->next = entry->next; node->next = entry->next;
node->next->prev = node;
node->prev = entry; node->prev = entry;
node->next->prev = node;
entry->next = node; entry->next = node;
} }
@ -89,6 +90,22 @@ static inline void list_append(struct List *entry, struct List *node)
list_add(tail, node); list_add(tail, node);
} }
void list_add_to_behind(struct List * entry,struct List * new) ////add to entry behind
{
new->next = entry->next;
new->prev = entry;
new->next->prev = new;
entry->next = new;
}
void list_add_to_before(struct List * entry,struct List * new) ////add to entry behind
{
new->next = entry;
entry->prev->next = new;
new->prev = entry->prev;
entry->prev = new;
}
static inline void list_del(struct List *entry) static inline void list_del(struct List *entry)
{ {
/** /**
@ -123,7 +140,8 @@ static inline struct List* list_prev(struct List *entry)
{ {
if (entry->prev != NULL) if (entry->prev != NULL)
return entry->prev; return entry->prev;
else return NULL; else
return NULL;
} }
/** /**
@ -136,7 +154,8 @@ static inline struct List* list_next(struct List *entry)
{ {
if (entry->next != NULL) if (entry->next != NULL)
return entry->next; return entry->next;
else return NULL; else
return NULL;
} }
//计算字符串的长度经过测试该版本比采用repne/scasb汇编的运行速度快16.8%左右) //计算字符串的长度经过测试该版本比采用repne/scasb汇编的运行速度快16.8%左右)
@ -199,7 +218,7 @@ void *memcpy(void *dst, void *src, long Num)
"movsb \n\t" "movsb \n\t"
"3: \n\t" "3: \n\t"
: "=&c"(d0), "=&D"(d1), "=&S"(d2) : "=&c"(d0), "=&D"(d1), "=&S"(d2)
: "0"(Num / 8), "q"(Num), "1"(src), "2"(dst) : "0"(Num / 8), "q"(Num), "1"(dst), "2"(src)
: "memory"); : "memory");
return dst; return dst;
} }
@ -254,3 +273,73 @@ void io_out32(unsigned short port, unsigned int value)
: "a"(value), "d"(port) : "a"(value), "d"(port)
: "memory"); : "memory");
} }
/**
* @brief rsp寄存器的值
*
* @return unsigned* rsp的值的指针
*/
unsigned long *get_rsp()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rsp, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief rbp寄存器的值
*
* @return unsigned* rbp的值的指针
*/
unsigned long *get_rbp()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rbp, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief ds寄存器的值
*
* @return unsigned* ds的值的指针
*/
unsigned long *get_ds()
{
ul *tmp;
__asm__ __volatile__(
"movq %%ds, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief rax寄存器的值
*
* @return unsigned* rax的值的指针
*/
unsigned long *get_rax()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rax, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief rbx寄存器的值
*
* @return unsigned* rbx的值的指针
*/
unsigned long *get_rbx()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rbx, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}

View File

@ -358,6 +358,7 @@ static int vsprintf(char *buf, const char *fmt, va_list args)
if (precision < 0) if (precision < 0)
precision = 3; precision = 3;
str = write_float_point_num(str, va_arg(args, double), field_width, precision, flags); str = write_float_point_num(str, va_arg(args, double), field_width, precision, flags);
break; break;

View File

@ -160,7 +160,7 @@ ENTRY(undefined_opcode)
jmp Err_Code jmp Err_Code
// 7 #NM FPU // 7 #NM FPU
ENTRY(dev_not_available) ENTRY(dev_not_avaliable)
pushq $0 pushq $0
pushq %rax pushq %rax
leaq do_dev_not_avaliable(%rip), %rax // leaq do_dev_not_avaliable(%rip), %rax //

View File

@ -32,6 +32,7 @@ extern unsigned int TSS64_Table[26];
* @param ist * @param ist
* @param code_addr * @param code_addr
*/ */
void set_gate(ul *gate_selector_addr, ul attr, unsigned char ist, ul *code_addr) void set_gate(ul *gate_selector_addr, ul attr, unsigned char ist, ul *code_addr)
{ {
ul __d0=0, __d1=0; ul __d0=0, __d1=0;
@ -56,6 +57,10 @@ void set_gate(ul *gate_selector_addr, ul attr, unsigned char ist, ul *code_addr)
*(gate_selector_addr + 1) = __d1; *(gate_selector_addr + 1) = __d1;
} }
/** /**
* @brief * @brief
* @param n TSS基地址在GDT中的第几项 * @param n TSS基地址在GDT中的第几项

View File

@ -124,7 +124,7 @@ void init_irq()
* @param rsp * @param rsp
* @param number * @param number
*/ */
void do_IRQ(ul rsp, ul number) void do_IRQ(struct pt_regs *regs, ul number)
{ {
unsigned char x; unsigned char x;
switch (number) switch (number)

View File

@ -13,6 +13,7 @@
#include "../common/glib.h" #include "../common/glib.h"
#include "../process/ptrace.h"
/** /**
* @brief * @brief
@ -26,4 +27,4 @@ void init_irq();
* @param rsp * @param rsp
* @param number * @param number
*/ */
void do_IRQ(ul rsp, ul number); void do_IRQ(struct pt_regs* rsp, ul number);

View File

@ -1,5 +1,6 @@
#include "trap.h" #include "trap.h"
#include "gate.h" #include "gate.h"
#include "../process/ptrace.h"
void init_sys_vector() void init_sys_vector()
{ {
@ -10,7 +11,7 @@ void init_sys_vector()
set_system_trap_gate(4, 1, overflow); set_system_trap_gate(4, 1, overflow);
set_system_trap_gate(5, 1, bounds); set_system_trap_gate(5, 1, bounds);
set_trap_gate(6, 1, undefined_opcode); set_trap_gate(6, 1, undefined_opcode);
set_trap_gate(7, 1, dev_not_available); set_trap_gate(7, 1, dev_not_avaliable);
set_trap_gate(8, 1, double_fault); set_trap_gate(8, 1, double_fault);
set_trap_gate(9, 1, coprocessor_segment_overrun); set_trap_gate(9, 1, coprocessor_segment_overrun);
set_trap_gate(10, 1, invalid_TSS); set_trap_gate(10, 1, invalid_TSS);
@ -30,132 +31,132 @@ void init_sys_vector()
} }
// 0 #DE 除法错误 // 0 #DE 除法错误
void do_divide_error(unsigned long rsp, unsigned long error_code) void do_divide_error(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 1 #DB 调试异常 // 1 #DB 调试异常
void do_debug(unsigned long rsp, unsigned long error_code) void do_debug(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR / TRAP"); printk_color(RED, BLACK, "ERROR / TRAP");
printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 2 不可屏蔽中断 // 2 不可屏蔽中断
void do_nmi(unsigned long rsp, unsigned long error_code) void do_nmi(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(BLUE, BLACK, "INT"); printk_color(BLUE, BLACK, "INT");
printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 3 #BP 断点异常 // 3 #BP 断点异常
void do_int3(unsigned long rsp, unsigned long error_code) void do_int3(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(YELLOW, BLACK, "TRAP"); printk_color(YELLOW, BLACK, "TRAP");
printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 4 #OF 溢出异常 // 4 #OF 溢出异常
void do_overflow(unsigned long rsp, unsigned long error_code) void do_overflow(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(YELLOW, BLACK, "TRAP"); printk_color(YELLOW, BLACK, "TRAP");
printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 5 #BR 越界异常 // 5 #BR 越界异常
void do_bounds(unsigned long rsp, unsigned long error_code) void do_bounds(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 6 #UD 无效/未定义的机器码 // 6 #UD 无效/未定义的机器码
void do_undefined_opcode(unsigned long rsp, unsigned long error_code) void do_undefined_opcode(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 7 #NM 设备异常FPU不存在 // 7 #NM 设备异常FPU不存在
void do_dev_not_avaliable(unsigned long rsp, unsigned long error_code) void do_dev_not_avaliable(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 8 #DF 双重错误 // 8 #DF 双重错误
void do_double_fault(unsigned long rsp, unsigned long error_code) void do_double_fault(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "Terminate"); printk_color(RED, BLACK, "Terminate");
printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 9 协处理器越界(保留) // 9 协处理器越界(保留)
void do_coprocessor_segment_overrun(unsigned long rsp, unsigned long error_code) void do_coprocessor_segment_overrun(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 10 #TS 无效的TSS段 // 10 #TS 无效的TSS段
void do_invalid_TSS(unsigned long rsp, unsigned long error_code) void do_invalid_TSS(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("["); printk("[");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
printk_color(YELLOW, BLACK, "Information:\n"); printk_color(YELLOW, BLACK, "Information:\n");
// 解析错误码 // 解析错误码
@ -181,43 +182,43 @@ void do_invalid_TSS(unsigned long rsp, unsigned long error_code)
} }
// 11 #NP 段不存在 // 11 #NP 段不存在
void do_segment_not_exists(unsigned long rsp, unsigned long error_code) void do_segment_not_exists(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 12 #SS SS段错误 // 12 #SS SS段错误
void do_stack_segment_fault(unsigned long rsp, unsigned long error_code) void do_stack_segment_fault(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 13 #GP 通用保护性异常 // 13 #GP 通用保护性异常
void do_general_protection(unsigned long rsp, unsigned long error_code) void do_general_protection(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
return; return;
while (1) while (1)
; ;
} }
// 14 #PF 页故障 // 14 #PF 页故障
void do_page_fault(unsigned long rsp, unsigned long error_code) void do_page_fault(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long cr2 = 0; unsigned long cr2 = 0;
// 先保存cr2寄存器的值避免由于再次触发页故障而丢失值 // 先保存cr2寄存器的值避免由于再次触发页故障而丢失值
@ -225,10 +226,10 @@ void do_page_fault(unsigned long rsp, unsigned long error_code)
__asm__ __volatile__("movq %%cr2, %0" __asm__ __volatile__("movq %%cr2, %0"
: "=r"(cr2)::"memory"); : "=r"(cr2)::"memory");
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_page_fault(14),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\tCR2:%#18lx\n", error_code, rsp, *rip, cr2); printk(" ] do_page_fault(14),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\tCR2:%#18lx\n", error_code, regs->rsp, regs->rip, cr2);
printk_color(YELLOW, BLACK, "Information:\n"); printk_color(YELLOW, BLACK, "Information:\n");
if (!(error_code & 0x01)) if (!(error_code & 0x01))
@ -257,60 +258,60 @@ void do_page_fault(unsigned long rsp, unsigned long error_code)
// 15 Intel保留请勿使用 // 15 Intel保留请勿使用
// 16 #MF x87FPU错误 // 16 #MF x87FPU错误
void do_x87_FPU_error(unsigned long rsp, unsigned long error_code) void do_x87_FPU_error(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 17 #AC 对齐检测 // 17 #AC 对齐检测
void do_alignment_check(unsigned long rsp, unsigned long error_code) void do_alignment_check(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 18 #MC 机器检测 // 18 #MC 机器检测
void do_machine_check(unsigned long rsp, unsigned long error_code) void do_machine_check(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 19 #XM SIMD浮点异常 // 19 #XM SIMD浮点异常
void do_SIMD_exception(unsigned long rsp, unsigned long error_code) void do_SIMD_exception(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;
} }
// 20 #VE 虚拟化异常 // 20 #VE 虚拟化异常
void do_virtualization_exception(unsigned long rsp, unsigned long error_code) void do_virtualization_exception(struct pt_regs * regs, unsigned long error_code)
{ {
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ "); printk("[ ");
printk_color(RED, BLACK, "ERROR"); printk_color(RED, BLACK, "ERROR");
printk(" ] do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip); printk(" ] do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1) while (1)
; ;

View File

@ -33,7 +33,7 @@ void bounds();
// 未定义的操作数 // 未定义的操作数
void undefined_opcode(); void undefined_opcode();
// 设备不可用 // 设备不可用
void dev_not_available(); void dev_not_avaliable();
void double_fault(); void double_fault();
void coprocessor_segment_overrun(); void coprocessor_segment_overrun();
void invalid_TSS(); void invalid_TSS();

View File

@ -53,7 +53,7 @@ entry64:
movq %rax, %gs movq %rax, %gs
movq %rax, %ss movq %rax, %ss
movq $0xffff800000007e00, %rsp //rsp movq _stack_start(%rip), %rsp //rsp
setup_IDT: setup_IDT:
leaq m_ignore_int(%rip), %rdx // ignore_int8B leaq m_ignore_int(%rip), %rdx // ignore_int8B

View File

@ -29,7 +29,7 @@ SECTIONS
} }
. = ALIGN(32768); . = ALIGN(32768);
.data.init_proc : { *(.data.init_proc) } .data.init_proc_union : { *(.data.init_proc_union) }
.bss : .bss :
{ {
_bss = .; _bss = .;

View File

@ -11,10 +11,11 @@
#include "mm/mm.h" #include "mm/mm.h"
#include "process/process.h" #include "process/process.h"
int *FR_address = (int *)0xffff800000a00000; //帧缓存区的地址 unsigned int *FR_address = (unsigned int *)0xffff800000a00000; //帧缓存区的地址
// char fxsave_region[512] __attribute__((aligned(16))); // char fxsave_region[512] __attribute__((aligned(16)));
struct memory_desc memory_management_struct = {{0}, 0}; struct memory_desc memory_management_struct = {{0}, 0};
//struct Global_Memory_Descriptor memory_management_struct = {{0}, 0};
void show_welcome() void show_welcome()
{ {
@ -35,43 +36,16 @@ void show_welcome()
printk_color(0x00e0ebeb, 0x00e0ebeb, " \n\n"); printk_color(0x00e0ebeb, 0x00e0ebeb, " \n\n");
} }
void test_printk()
{
//测试直接输出
printk("\nTesting printk...\n");
//测试输出单个字符
printk("%c\n", 't');
//测试输出字符串%s
printk("%s\n", "xxx");
//测试输出数字
printk("%d %ld %lld\n", 1, 2, 3);
//测试输出两个百分号
printk("%%\n");
//测试输出\t
printk("\nTesting tab...\n");
printk("date\t\tname\tscore\n");
printk("2022-01-01\tDavid\t99\n");
printk("2022-01-01\tJohn\t95\n");
//测试输出八进制
printk("\nTest base 8 : %d --> %#o\n", 255, 255);
//测试输出十六进制
printk("\nTest base 16 : %d --> %#x\n", 255, 255);
printk("\nTest base 16 : %d --> %#X\n", 255, 255);
}
// 测试内存管理单元 // 测试内存管理单元
void test_mmm() /*
void test_mm()
{ {
kinfo("Testing memory management unit..."); kinfo("Testing memory management unit...");
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1)); //printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1));
kinfo("Try to allocate 64 memory pages."); kinfo("Try to allocate 64 memory pages.");
struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL); struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
/*
for (int i = 0; i <= 65; ++i) for (int i = 0; i <= 65; ++i)
{ {
printk("page%d\tattr:%#018lx\tphys_addr:%#018lx\t", i, page->attr, page->addr_phys); printk("page%d\tattr:%#018lx\tphys_addr:%#018lx\t", i, page->attr, page->addr_phys);
@ -79,12 +53,14 @@ void test_mmm()
if (((i + 1) % 2) == 0) if (((i + 1) % 2) == 0)
printk("\n"); printk("\n");
} }
*/
printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1)); printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1));
} }
*/
void init() // 初始化系统各模块
void system_initialize()
{ {
// 初始化printk // 初始化printk
init_printk(1440, 900, FR_address, 1440 * 900 * 4, 8, 16); init_printk(1440, 900, FR_address, 1440 * 900 * 4, 8, 16);
@ -93,34 +69,34 @@ void init()
// 初始化任务状态段表 // 初始化任务状态段表
ul tss_item_addr = 0xffff800000007c00; ul tss_item_addr = 0xffff800000007c00;
set_TSS64(tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr,
set_TSS64(_stack_start, _stack_start, _stack_start, tss_item_addr, tss_item_addr,
tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr); tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr);
// 初始化中断描述符表 // 初始化中断描述符表
init_sys_vector(); init_sys_vector();
//asm volatile(" fxsave %0 " ::"m"(fxsave_region));
// 初始化内存管理单元 // 初始化内存管理单元
mm_init(); mm_init();
// 初始化中断模块 // 初始化中断模块
init_irq(); init_irq();
process_init(); process_init();
} }
//操作系统内核从这里开始执行 //操作系统内核从这里开始执行
void Start_Kernel(void) void Start_Kernel(void)
{ {
init(); system_initialize();
// show_welcome(); // show_welcome();
// test_mm(); // test_mm();
//test_printk();
//int t = 1 / 0; // 测试异常处理模块能否正常工作 触发除法错误
// int t = *(int *)0xffff80000aa00000; // 触发页故障
while (1) while (1)
; ;

View File

@ -8,6 +8,12 @@ ul total_2M_pages = 0;
void mm_init() void mm_init()
{ {
kinfo("Initializing memory management unit..."); kinfo("Initializing memory management unit...");
// 设置内核程序不同部分的起止地址
memory_management_struct.kernel_code_start = (ul)&_text;
memory_management_struct.kernel_code_end = (ul)&_etext;
memory_management_struct.kernel_data_end = (ul)&_edata;
memory_management_struct.kernel_end = (ul)&_end;
// 实模式下获取到的信息的起始地址转换为ARDS指针 // 实模式下获取到的信息的起始地址转换为ARDS指针
struct ARDS *ards_ptr = (struct ARDS *)0xffff800000007e00; struct ARDS *ards_ptr = (struct ARDS *)0xffff800000007e00;
@ -51,11 +57,7 @@ void mm_init()
} }
kinfo("Total amounts of 2M pages : %ld.", total_2M_pages); kinfo("Total amounts of 2M pages : %ld.", total_2M_pages);
// 设置内核程序不同部分的起止地址
memory_management_struct.kernel_code_start = (ul)&_text;
memory_management_struct.kernel_code_end = (ul)&_etext;
memory_management_struct.kernel_data_end = (ul)&_edata;
memory_management_struct.kernel_end = (ul)&_end;
// 物理地址空间的最大地址包含了物理内存、内存空洞、ROM等 // 物理地址空间的最大地址包含了物理内存、内存空洞、ROM等
ul max_addr = memory_management_struct.e820[memory_management_struct.len_e820].BaseAddr + memory_management_struct.e820[memory_management_struct.len_e820].Length; ul max_addr = memory_management_struct.e820[memory_management_struct.len_e820].BaseAddr + memory_management_struct.e820[memory_management_struct.len_e820].Length;
@ -63,7 +65,7 @@ void mm_init()
// bmp的指针指向截止位置的4k对齐的上边界防止修改了别的数据 // bmp的指针指向截止位置的4k对齐的上边界防止修改了别的数据
memory_management_struct.bmp = (unsigned long *)((memory_management_struct.kernel_end + PAGE_4K_SIZE - 1) & PAGE_4K_MASK); memory_management_struct.bmp = (unsigned long *)((memory_management_struct.kernel_end + PAGE_4K_SIZE - 1) & PAGE_4K_MASK);
memory_management_struct.bits_size = max_addr >> PAGE_2M_SHIFT; // 物理地址空间的最大页面数 memory_management_struct.bits_size = max_addr >> PAGE_2M_SHIFT; // 物理地址空间的最大页面数
memory_management_struct.bmp_len = ((unsigned long)((max_addr >> PAGE_2M_SHIFT) + sizeof(unsigned long) * 8 - 1) / 8) & (~(sizeof(unsigned long) - 1)); // bmp由多少个unsigned long变量组成 memory_management_struct.bmp_len = (((unsigned long)(max_addr >> PAGE_2M_SHIFT) + sizeof(unsigned long) * 8 - 1) / 8) & (~(sizeof(unsigned long) - 1)); // bmp由多少个unsigned long变量组成
// 初始化bitmap 先将整个bmp空间全部置位。稍后再将可用物理内存页复位。 // 初始化bitmap 先将整个bmp空间全部置位。稍后再将可用物理内存页复位。
memset(memory_management_struct.bmp, 0xff, memory_management_struct.bmp_len); memset(memory_management_struct.bmp, 0xff, memory_management_struct.bmp_len);
@ -188,11 +190,11 @@ void mm_init()
printk_color(INDIGO, BLACK, "**cr3:\t%#018lx\n", *phys_2_virt(*(phys_2_virt(cr3)) & (~0xff)) & (~0xff)); printk_color(INDIGO, BLACK, "**cr3:\t%#018lx\n", *phys_2_virt(*(phys_2_virt(cr3)) & (~0xff)) & (~0xff));
*/ */
/*
// 消除一致性页表映射将页目录PML4E的前10项清空 // 消除一致性页表映射将页目录PML4E的前10项清空
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
*(phys_2_virt(global_CR3) + i) = 0UL; *(phys_2_virt(global_CR3) + i) = 0UL;
*/
flush_tlb(); flush_tlb();
@ -307,6 +309,7 @@ struct Page *alloc_pages(unsigned int zone_select, int num, ul flags)
page_init(x, flags); page_init(x, flags);
} }
// 成功分配了页面,返回第一个页面的指针 // 成功分配了页面,返回第一个页面的指针
//printk("start page num=%d\n",start_page_num);
return (struct Page *)(memory_management_struct.pages_struct + start_page_num); return (struct Page *)(memory_management_struct.pages_struct + start_page_num);
} }
} }

View File

@ -29,7 +29,6 @@
// 虚拟地址与物理地址转换 // 虚拟地址与物理地址转换
#define virt_2_phys(addr) ((unsigned long)(addr)-KERNEL_BASE_ADDR) #define virt_2_phys(addr) ((unsigned long)(addr)-KERNEL_BASE_ADDR)
#define phys_2_virt(addr) ((unsigned long *)((unsigned long)(addr) + KERNEL_BASE_ADDR)) #define phys_2_virt(addr) ((unsigned long *)((unsigned long)(addr) + KERNEL_BASE_ADDR))
#define Phy_To_Virt(addr) ((unsigned long *)((unsigned long)(addr) + KERNEL_BASE_ADDR))
#define Virt_To_2M_Page(kaddr) (memory_management_struct.pages_struct + (virt_2_phys(kaddr) >> PAGE_2M_SHIFT)) #define Virt_To_2M_Page(kaddr) (memory_management_struct.pages_struct + (virt_2_phys(kaddr) >> PAGE_2M_SHIFT))
#define Phy_to_2M_Page(kaddr) (memory_management_struct.pages_struct + ((unsigned long)(kaddr) >> PAGE_2M_SHIFT)) #define Phy_to_2M_Page(kaddr) (memory_management_struct.pages_struct + ((unsigned long)(kaddr) >> PAGE_2M_SHIFT))
@ -219,6 +218,8 @@ struct Page *alloc_pages(unsigned int zone_select, int num, ul flags);
*/ */
unsigned long page_clean(struct Page *page); unsigned long page_clean(struct Page *page);
/** /**
* @brief * @brief
* *
@ -227,3 +228,26 @@ typedef struct
{ {
unsigned long pml4t; unsigned long pml4t;
} pml4t_t; } pml4t_t;
#define mk_pml4t(addr, attr) ((unsigned long)(addr) | (unsigned long)(attr))
#define set_pml4t(mpl4tptr, mpl4tval) (*(mpl4tptr) = (mpl4tval))
typedef struct
{
unsigned long pdpt;
} pdpt_t;
#define mk_pdpt(addr, attr) ((unsigned long)(addr) | (unsigned long)(attr))
#define set_pdpt(pdptptr, pdptval) (*(pdptptr) = (pdptval))
typedef struct
{
unsigned long pdt;
} pdt_t;
#define mk_pdt(addr, attr) ((unsigned long)(addr) | (unsigned long)(attr))
#define set_pdt(pdtptr, pdtval) (*(pdtptr) = (pdtval))
typedef struct
{
unsigned long pt;
} pt_t;
#define mk_pt(addr, attr) ((unsigned long)(addr) | (unsigned long)(attr))
#define set_pt(ptptr, ptval) (*(ptptr) = (ptval))

View File

@ -1,30 +1,9 @@
#include "process.h" #include "process.h"
#include "../exception/gate.h" #include "../exception/gate.h"
#include "../common/printk.h" #include "../common/printk.h"
#include "../common/kprint.h" #include "../common/kprint.h"
void test_mm()
{
kinfo("Testing memory management unit...");
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1));
kinfo("Try to allocate 64 memory pages.");
struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
for (int i = 0; i <= 65; ++i)
{
printk("page%d\tattr:%#018lx\tphys_addr:%#018lx\t", i, page->attr, page->addr_phys);
++page;
if (((i + 1) % 2) == 0)
printk("\n");
}
printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1));
}
/** /**
* @brief * @brief
* *
@ -33,6 +12,7 @@ void test_mm()
* *
* fs和gs寄存器 * fs和gs寄存器
*/ */
void __switch_to(struct process_control_block *prev, struct process_control_block *next) void __switch_to(struct process_control_block *prev, struct process_control_block *next)
{ {
initial_tss[0].rsp0 = next->thread->rbp; initial_tss[0].rsp0 = next->thread->rbp;
@ -47,8 +27,6 @@ void __switch_to(struct process_control_block *prev, struct process_control_bloc
__asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs)); __asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
__asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs)); __asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
printk("prev->thread->rbp=%#018lx\n", prev->thread->rbp);
printk("next->thread->rbp=%#018lx\n", next->thread->rbp);
} }
/** /**
@ -59,7 +37,7 @@ void __switch_to(struct process_control_block *prev, struct process_control_bloc
*/ */
ul init(ul arg) ul init(ul arg)
{ {
printk("initial proc running...\targ:%#018lx\n", arg); kinfo("initial proc running...\targ:%#018lx", arg);
return 1; return 1;
} }
@ -82,8 +60,8 @@ ul do_exit(ul code)
* rsp位于栈顶 * rsp位于栈顶
* 7unsigned long的大小pt_regs * 7unsigned long的大小pt_regs
*/ */
extern void kernel_thread_func(void);
extern void kernel_thread_func(void);
__asm__ ( __asm__ (
"kernel_thread_func: \n\t" "kernel_thread_func: \n\t"
" popq %r15 \n\t" " popq %r15 \n\t"
@ -94,7 +72,7 @@ __asm__(
" popq %r10 \n\t" " popq %r10 \n\t"
" popq %r9 \n\t" " popq %r9 \n\t"
" popq %r8 \n\t" " popq %r8 \n\t"
" popq %rbx \n\t" // 在kernel_thread中将程序执行地址保存在了rbx " popq %rbx \n\t"
" popq %rcx \n\t" " popq %rcx \n\t"
" popq %rdx \n\t" " popq %rdx \n\t"
" popq %rsi \n\t" " popq %rsi \n\t"
@ -106,11 +84,13 @@ __asm__(
" movq %rax, %es \n\t" " movq %rax, %es \n\t"
" popq %rax \n\t" " popq %rax \n\t"
" addq $0x38, %rsp \n\t" " addq $0x38, %rsp \n\t"
// ======================= // /////////////////////////////////
" movq %rdx, %rdi \n\t" " movq %rdx, %rdi \n\t"
" callq *%rbx \n\t" " callq *%rbx \n\t"
" movq %rax, %rdi \n\t" " movq %rax, %rdi \n\t"
" callq do_exit \n\t"); " callq do_exit \n\t"
);
/** /**
* @brief * @brief
@ -120,9 +100,9 @@ __asm__(
* @param flags * @param flags
* @return int * @return int
*/ */
int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags) int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags)
{ {
//struct Page *page = alloc_pages(ZONE_NORMAL, 2, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
struct pt_regs regs; struct pt_regs regs;
memset(&regs, 0, sizeof(regs)); memset(&regs, 0, sizeof(regs));
@ -136,18 +116,20 @@ int kernel_thread(unsigned long (* fn)(unsigned long), unsigned long arg, unsign
regs.cs = KERNEL_CS; regs.cs = KERNEL_CS;
regs.ss = KERNEL_DS; regs.ss = KERNEL_DS;
// 置位中断使能标志位 // 置位中断使能标志位
regs.rflags = (1 << 9); regs.rflags = (1 << 9);
// rip寄存器指向内核线程的引导程序 // rip寄存器指向内核线程的引导程序
regs.rip = (ul)kernel_thread_func; regs.rip = (ul)kernel_thread_func;
return (int)do_fork(&regs, flags, 0, 0); return do_fork(&regs, flags, 0, 0);
} }
void process_init() void process_init()
{ {
initial_mm.pgd = (pml4t_t *)global_CR3; initial_mm.pgd = (pml4t_t *)global_CR3;
initial_mm.code_addr_start = memory_management_struct.kernel_code_start; initial_mm.code_addr_start = memory_management_struct.kernel_code_start;
@ -164,23 +146,23 @@ void process_init()
initial_mm.stack_start = _stack_start; initial_mm.stack_start = _stack_start;
// 初始化进程和tss // 初始化进程和tss
set_TSS64(initial_thread.rbp, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1, initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7); set_TSS64(initial_thread.rbp, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1, initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7);
initial_tss[0].rsp0 = initial_thread.rbp; initial_tss[0].rsp0 = initial_thread.rbp;
// 初始化进程的循环链表 // 初始化进程的循环链表
list_init(&initial_proc_union.pcb.list); list_init(&initial_proc_union.pcb.list);
test_mm();
kernel_thread(init, 10, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); // 初始化内核进程 kernel_thread(init, 10, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); // 初始化内核进程
initial_proc_union.pcb.state = PROC_RUNNING; initial_proc_union.pcb.state = PROC_RUNNING;
// 获取新的进程的pcb // 获取新的进程的pcb
struct process_control_block *p = container_of(list_next(&current_pcb->list), struct process_control_block, list); struct process_control_block *p = container_of(list_next(&current_pcb->list), struct process_control_block, list);
// 切换到新的内核线程
switch_proc(current_pcb, p); switch_proc(current_pcb, p);
} }
@ -193,34 +175,29 @@ void process_init()
* @param stack_size * @param stack_size
* @return unsigned long * @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)
{ {
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1));
struct process_control_block *tsk = NULL; struct process_control_block *tsk = NULL;
//printk("alloc_pages,bmp %#018lx\n", *(memory_management_struct.bmp));
// 获取一个物理页并在这个物理页内初始化pcb // 获取一个物理页并在这个物理页内初始化pcb
struct Page *p = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL); struct Page *pp = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
printk("22\n");
//kinfo("alloc_pages,bmp:%#018lx", *(memory_management_struct.bmp));
tsk = (struct process_control_block *)((unsigned long)(p->addr_phys) + (0xffff800000000000UL));
//printk("phys_addr\t%#018lx\n",p->addr_phys); tsk = (struct process_control_block *)phys_2_virt(pp->addr_phys);
printk("virt_addr\t%#018lx\n",(unsigned long)(p->addr_phys) + (0xffff800000000000UL));
//kinfo("pcb addr:%#018lx", (ul)tsk);
memset(tsk, 0, sizeof(*tsk)); memset(tsk, 0, sizeof(*tsk));
printk("33\n");
// 将当前进程的pcb复制到新的pcb内 // 将当前进程的pcb复制到新的pcb内
*tsk = *current_pcb; *tsk = *current_pcb;
// 将进程加入循环链表 // 将进程加入循环链表
list_init(&tsk->list); list_init(&tsk->list);
printk("44\n");
list_append(&initial_proc_union.pcb.list, &tsk->list); list_add(&initial_proc_union.pcb.list, &tsk->list);
printk("5\n");
++(tsk->pid); ++(tsk->pid);
tsk->state = PROC_UNINTERRUPTIBLE; tsk->state = PROC_UNINTERRUPTIBLE;
@ -231,6 +208,7 @@ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned
// 将寄存器信息存储到进程的内核栈空间的顶部 // 将寄存器信息存储到进程的内核栈空间的顶部
memcpy((void *)((ul)tsk + STACK_SIZE - sizeof(struct pt_regs)), regs, sizeof(struct pt_regs)); memcpy((void *)((ul)tsk + STACK_SIZE - sizeof(struct pt_regs)), regs, sizeof(struct pt_regs));
// 设置进程的内核栈 // 设置进程的内核栈
thd->rbp = (ul)tsk + STACK_SIZE; thd->rbp = (ul)tsk + STACK_SIZE;
thd->rip = regs->rip; thd->rip = regs->rip;
@ -241,6 +219,6 @@ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned
thd->rip = regs->rip = (ul)ret_from_intr; thd->rip = regs->rip = (ul)ret_from_intr;
tsk->state = PROC_RUNNING; tsk->state = PROC_RUNNING;
printk("1111\n");
return 0; return 0;
} }

View File

@ -150,7 +150,7 @@ struct thread_struct initial_thread;
} }
// 初始化 初始进程的union ,并将其链接到.data.init_proc段内 // 初始化 初始进程的union ,并将其链接到.data.init_proc段内
union proc_union initial_proc_union __attribute__((__section__(".data.init_proc"))) = {INITIAL_PROC(initial_proc_union.pcb)}; union proc_union initial_proc_union __attribute__((__section__(".data.init_proc_union"))) = {INITIAL_PROC(initial_proc_union.pcb)};
struct process_control_block *initial_proc[CPU_NUM] = {&initial_proc_union.pcb, 0}; struct process_control_block *initial_proc[CPU_NUM] = {&initial_proc_union.pcb, 0};
@ -220,7 +220,8 @@ struct process_control_block *get_current_pcb()
: "=r"(current) : "=r"(current)
: "0"(~32767UL)); : "0"(~32767UL));
return current; return current;
} };
#define current_pcb get_current_pcb() #define current_pcb get_current_pcb()
@ -233,13 +234,15 @@ struct process_control_block *get_current_pcb()
* rbp和rax保存到栈中rsp和rip保存到prev的thread结构体中 * rbp和rax保存到栈中rsp和rip保存到prev的thread结构体中
* __switch_to切换栈rax rbp * __switch_to切换栈rax rbp
*/ */
#define switch_proc(prev, next) \ #define switch_proc(prev, next) \
do \ do \
{ \ { \
\
__asm__ __volatile__("pushq %%rbp \n\t" \ __asm__ __volatile__("pushq %%rbp \n\t" \
"pushq %%rax \n\t" \ "pushq %%rax \n\t" \
"movq %%rsp, %0 \n\t" \ "movq %%rsp, %0 \n\t" \
"movq %2, %%rax \n\t" \ "movq %2, %%rsp \n\t" \
"leaq 1f(%%rip), %%rax \n\t" \ "leaq 1f(%%rip), %%rax \n\t" \
"movq %%rax, %1 \n\t" \ "movq %%rax, %1 \n\t" \
"pushq %3 \n\t" \ "pushq %3 \n\t" \

View File

@ -1,30 +1,52 @@
#pragma once /***************************************************
#include "../common/glib.h" *
// 进程执行现场的寄存器状态 *
* MINE
*
* 使
*
*
*
*
* EMail: 345538255@qq.com
*
*
***************************************************/
#ifndef __PTRACE_H__
#define __PTRACE_H__
/*
*/
struct pt_regs struct pt_regs
{ {
ul r15; unsigned long r15;
ul r14; unsigned long r14;
ul r13; unsigned long r13;
ul r12; unsigned long r12;
ul r11; unsigned long r11;
ul r10; unsigned long r10;
ul r9; unsigned long r9;
ul r8; unsigned long r8;
ul rbx; unsigned long rbx;
ul rcx; unsigned long rcx;
ul rdx; unsigned long rdx;
ul rsi; unsigned long rsi;
ul rdi; unsigned long rdi;
ul rbp; unsigned long rbp;
ul ds; unsigned long ds;
ul es; unsigned long es;
ul rax; unsigned long rax;
ul func; unsigned long func;
ul err_code; unsigned long errcode;
ul rip; unsigned long rip;
ul cs; unsigned long cs;
ul rflags; unsigned long rflags;
ul rsp; unsigned long rsp;
ul ss; unsigned long ss;
}; };
#endif