Quellcode durchsuchen

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

fslongjin vor 3 Jahren
Ursprung
Commit
d1671bc121

+ 7 - 1
.vscode/settings.json

@@ -7,6 +7,12 @@
         "font.h": "c",
         "trap.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"
     }
 }

+ 8 - 5
kernel/common/asm.h

@@ -4,12 +4,9 @@
 #define __ASM__
 
 
-#define ENTRY(name)\
-    .global name;    \
-    name:
 
 
-#endif
+
 
 // 符号名
 #define SYMBOL_NAME(X)	X
@@ -22,4 +19,10 @@
 
 #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

+ 0 - 2
kernel/common/font.h

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

+ 107 - 18
kernel/common/glib.h

@@ -28,7 +28,7 @@
  * @param ptr 指向结构体变量内的成员变量member的指针
  * @param type 成员变量所在的结构体
  * @param member 成员变量名
- * 
+ *
  * 方法:使用ptr减去结构体内的偏移,得到结构体变量的基地址
  */
 #define container_of(ptr, type, member)                                     \
@@ -63,17 +63,18 @@ static inline void list_init(struct List *list)
     list->prev = list;
 }
 
+/**
+ * @brief
+
+ * @param entry 给定的节点
+ * @param node 待插入的节点
+ **/
 static inline void list_add(struct List *entry, struct List *node)
 {
-    /**
-     * @brief 将node插入到entry后面
-     * @param entry 给定的节点
-     * @param node 待插入的节点
-     */
 
     node->next = entry->next;
-    node->next->prev = node;
     node->prev = entry;
+    node->next->prev = node;
     entry->next = node;
 }
 
@@ -89,6 +90,22 @@ static inline void list_append(struct List *entry, struct List *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)
 {
     /**
@@ -115,28 +132,30 @@ static inline bool list_empty(struct List *entry)
 
 /**
  * @brief 获取链表的上一个元素
- * 
- * @param entry 
+ *
+ * @param entry
  * @return 链表的上一个元素
  */
-static inline struct List* list_prev(struct List *entry)
+static inline struct List *list_prev(struct List *entry)
 {
-    if(entry->prev!=NULL)
+    if (entry->prev != NULL)
         return entry->prev;
-    else return NULL;
+    else
+        return NULL;
 }
 
 /**
  * @brief 获取链表的下一个元素
- * 
- * @param entry 
+ *
+ * @param entry
  * @return 链表的下一个元素
  */
-static inline struct List* list_next(struct List *entry)
+static inline struct List *list_next(struct List *entry)
 {
-    if(entry->next!=NULL)
+    if (entry->next != NULL)
         return entry->next;
-    else return NULL;
+    else
+        return NULL;
 }
 
 //计算字符串的长度(经过测试,该版本比采用repne/scasb汇编的运行速度快16.8%左右)
@@ -199,7 +218,7 @@ void *memcpy(void *dst, void *src, long Num)
                          "movsb	\n\t"
                          "3:	\n\t"
                          : "=&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");
     return dst;
 }
@@ -254,3 +273,73 @@ void io_out32(unsigned short port, unsigned int value)
                          : "a"(value), "d"(port)
                          : "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;
+}

+ 1 - 0
kernel/common/printk.c

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

+ 1 - 1
kernel/exception/entry.S

@@ -160,7 +160,7 @@ ENTRY(undefined_opcode)
     jmp Err_Code
 
 // 7 #NM 设备异常(FPU不存在)
-ENTRY(dev_not_available)
+ENTRY(dev_not_avaliable)
     pushq $0
     pushq %rax
     leaq do_dev_not_avaliable(%rip), %rax    // 获取中断服务程序的地址

+ 5 - 0
kernel/exception/gate.h

@@ -32,6 +32,7 @@ extern unsigned int TSS64_Table[26];
  * @param ist 中断栈表号
  * @param code_addr 指向中断服务程序的指针的地址
  */
+
 void set_gate(ul *gate_selector_addr, ul attr, unsigned char ist, ul *code_addr)
 {
     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;
 }
 
+
+
+
+
 /**
  * @brief 加载任务状态段寄存器
  * @param n TSS基地址在GDT中的第几项

+ 1 - 1
kernel/exception/irq.c

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

+ 2 - 1
kernel/exception/irq.h

@@ -13,6 +13,7 @@
 
 #include "../common/glib.h"
 
+#include "../process/ptrace.h"
 
 /**
  * @brief 初始化中断模块
@@ -26,4 +27,4 @@ void init_irq();
  * @param rsp 中断栈指针
  * @param number 中断号
  */
-void do_IRQ(ul rsp, ul number);
+void do_IRQ(struct pt_regs* rsp, ul number);

+ 62 - 61
kernel/exception/trap.c

@@ -1,5 +1,6 @@
 #include "trap.h"
 #include "gate.h"
+#include "../process/ptrace.h"
 
 void init_sys_vector()
 {
@@ -10,7 +11,7 @@ void init_sys_vector()
     set_system_trap_gate(4, 1, overflow);
     set_system_trap_gate(5, 1, bounds);
     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(9, 1, coprocessor_segment_overrun);
     set_trap_gate(10, 1, invalid_TSS);
@@ -30,132 +31,132 @@ void init_sys_vector()
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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");
     // 解析错误码
@@ -181,43 +182,43 @@ void do_invalid_TSS(unsigned long rsp, unsigned long error_code)
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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;
     while (1)
         ;
 }
 
 // 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;
     // 先保存cr2寄存器的值,避免由于再次触发页故障而丢失值
@@ -225,10 +226,10 @@ void do_page_fault(unsigned long rsp, unsigned long error_code)
     __asm__ __volatile__("movq %%cr2, %0"
                          : "=r"(cr2)::"memory");
 
-    unsigned long *rip = (unsigned long *)(rsp + 0x98);
+    
     printk("[ ");
     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");
     if (!(error_code & 0x01))
@@ -257,60 +258,60 @@ void do_page_fault(unsigned long rsp, unsigned long error_code)
 // 15 Intel保留,请勿使用
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;
 }
 
 // 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_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)
         ;

+ 1 - 1
kernel/exception/trap.h

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

+ 1 - 1
kernel/head.S

@@ -53,7 +53,7 @@ entry64:
     movq %rax, %gs
     movq %rax, %ss
 
-    movq $0xffff800000007e00, %rsp //rsp的地址
+    movq _stack_start(%rip), %rsp //rsp的地址
 
 setup_IDT:
     leaq m_ignore_int(%rip),  %rdx // 将ignore_int的地址暂时存到中段描述符的高8B

+ 1 - 1
kernel/link.lds

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

+ 26 - 50
kernel/main.c

@@ -11,16 +11,17 @@
 #include "mm/mm.h"
 #include "process/process.h"
 
-int *FR_address = (int *)0xffff800000a00000; //帧缓存区的地址
-//char fxsave_region[512] __attribute__((aligned(16)));
+unsigned int *FR_address = (unsigned int *)0xffff800000a00000; //帧缓存区的地址
+// 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()
 {
     /**
      * @brief 打印欢迎页面
-     * 
+     *
      */
 
     printk("\n\n");
@@ -35,43 +36,16 @@ void show_welcome()
     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...");
     //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);
@@ -79,48 +53,50 @@ void test_mmm()
         if (((i + 1) % 2) == 0)
             printk("\n");
     }
-    */
-    
+
+
    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);
 
     load_TR(8); // 加载TR寄存器
 
     // 初始化任务状态段表
     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);
+              
+    
 
     // 初始化中断描述符表
     init_sys_vector();
 
-    //asm volatile(" fxsave %0 " ::"m"(fxsave_region));
-    // 初始化内存管理单元
+    //  初始化内存管理单元
     mm_init();
 
     // 初始化中断模块
     init_irq();
-    process_init();
-
 
+    process_init();
 }
+
 //操作系统内核从这里开始执行
 void Start_Kernel(void)
 {
 
-    init();
-    //show_welcome();
-    //test_mm();
+    system_initialize();
     
+    // show_welcome();
+    // test_mm();
 
-    //test_printk();
-    //int t = 1 / 0; // 测试异常处理模块能否正常工作 触发除法错误
-    // int t = *(int *)0xffff80000aa00000; // 触发页故障
+   
 
     while (1)
         ;
@@ -132,4 +108,4 @@ void ignore_int()
     printk_color(YELLOW, BLACK, "WARN");
     printk("] Unknown interrupt or fault at RIP.\n");
     return;
-}
+}

+ 11 - 8
kernel/mm/mm.c

@@ -8,6 +8,12 @@ ul total_2M_pages = 0;
 void mm_init()
 {
     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指针
     struct ARDS *ards_ptr = (struct ARDS *)0xffff800000007e00;
 
@@ -51,11 +57,7 @@ void mm_init()
     }
     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等)
     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对齐的上边界(防止修改了别的数据)
     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.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空间全部置位。稍后再将可用物理内存页复位。
     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));
     */
 
-   /*
+   
     // 消除一致性页表映射,将页目录(PML4E)的前10项清空
     for (int i = 0; i < 10; ++i)
         *(phys_2_virt(global_CR3) + i) = 0UL;
-        */
+        
 
     flush_tlb();
 
@@ -307,6 +309,7 @@ struct Page *alloc_pages(unsigned int zone_select, int num, ul 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);
                 }
             }

+ 26 - 2
kernel/mm/mm.h

@@ -29,7 +29,6 @@
 // 虚拟地址与物理地址转换
 #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 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 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);
 
+
+
 /**
  * @brief 内存页表结构体
  *
@@ -226,4 +227,27 @@ unsigned long page_clean(struct Page *page);
 typedef struct
 {
     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))

+ 58 - 80
kernel/process/process.c

@@ -1,30 +1,9 @@
 #include "process.h"
 
-
 #include "../exception/gate.h"
 #include "../common/printk.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 切换进程
  *
@@ -33,22 +12,21 @@ void test_mm()
  * 由于程序在进入内核的时候已经保存了寄存器,因此这里不需要保存寄存器。
  * 这里切换fs和gs寄存器
  */
+
 void __switch_to(struct process_control_block *prev, struct process_control_block *next)
 {
     initial_tss[0].rsp0 = next->thread->rbp;
     set_TSS64(initial_tss[0].rsp0, 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);
 
-    __asm__ __volatile__("movq %%fs, %0 \n\t"
+    __asm__ __volatile__("movq	%%fs,	%0 \n\t"
                          : "=a"(prev->thread->fs));
-    __asm__ __volatile__("movq %%gs, %0 \n\t"
+    __asm__ __volatile__("movq	%%gs,	%0 \n\t"
                          : "=a"(prev->thread->gs));
 
-    __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,	%%fs \n\t" ::"a"(next->thread->fs));
+    __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)
 {
-    printk("initial proc running...\targ:%#018lx\n", arg);
+    kinfo("initial proc running...\targ:%#018lx", arg);
     return 1;
 }
 
@@ -82,35 +60,37 @@ ul do_exit(ul code)
  * 执行到这里时,rsp位于栈顶,然后弹出寄存器值
  * 弹出之后还要向上移动7个unsigned long的大小,从而弹出额外的信息(详见pt_regs)
  */
+
 extern void kernel_thread_func(void);
+__asm__ (
+"kernel_thread_func:	\n\t"
+"	popq	%r15	\n\t"
+"	popq	%r14	\n\t"	
+"	popq	%r13	\n\t"	
+"	popq	%r12	\n\t"	
+"	popq	%r11	\n\t"	
+"	popq	%r10	\n\t"	
+"	popq	%r9	\n\t"	
+"	popq	%r8	\n\t"	
+"	popq	%rbx	\n\t"	
+"	popq	%rcx	\n\t"	
+"	popq	%rdx	\n\t"	
+"	popq	%rsi	\n\t"	
+"	popq	%rdi	\n\t"	
+"	popq	%rbp	\n\t"	
+"	popq	%rax	\n\t"	
+"	movq	%rax,	%ds	\n\t"
+"	popq	%rax		\n\t"
+"	movq	%rax,	%es	\n\t"
+"	popq	%rax		\n\t"
+"	addq	$0x38,	%rsp	\n\t"
+/////////////////////////////////
+"	movq	%rdx,	%rdi	\n\t"
+"	callq	*%rbx		\n\t"
+"	movq	%rax,	%rdi	\n\t"
+"	callq	do_exit		\n\t"
+);
 
-__asm__(
-    "kernel_thread_func:    \n\t"
-    "   popq    %r15    \n\t"
-    "   popq    %r14    \n\t"
-    "   popq    %r13    \n\t"
-    "   popq    %r12    \n\t"
-    "   popq    %r11    \n\t"
-    "   popq    %r10    \n\t"
-    "   popq    %r9    \n\t"
-    "   popq    %r8    \n\t"
-    "   popq    %rbx    \n\t" // 在kernel_thread中,将程序执行地址保存在了rbx
-    "   popq    %rcx    \n\t"
-    "   popq    %rdx    \n\t"
-    "   popq    %rsi    \n\t"
-    "   popq    %rdi    \n\t"
-    "   popq    %rbp    \n\t"
-    "   popq    %rax    \n\t"
-    "   movq    %rax,   %ds\n\t"
-    "   popq    %rax    \n\t"
-    "   movq    %rax,   %es\n\t"
-    "   popq    %rax    \n\t"
-    "   addq    $0x38,  %rsp \n\t"
-    // ======================= //
-    "   movq    %rdx, %rdi  \n\t"
-    "   callq   *%rbx   \n\t"
-    "   movq    %rax,   %rdi    \n\t"
-    "   callq   do_exit \n\t");
 
 /**
  * @brief 初始化内核进程
@@ -120,9 +100,9 @@ __asm__(
  * @param flags
  * @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;
     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.ss = KERNEL_DS;
 
+
     // 置位中断使能标志位
     regs.rflags = (1 << 9);
 
     // rip寄存器指向内核线程的引导程序
     regs.rip = (ul)kernel_thread_func;
 
-    return (int)do_fork(&regs, flags, 0, 0);
+    return do_fork(&regs, flags, 0, 0);
 }
 
 void process_init()
 {
-    
+
+
     initial_mm.pgd = (pml4t_t *)global_CR3;
 
     initial_mm.code_addr_start = memory_management_struct.kernel_code_start;
@@ -164,23 +146,23 @@ void process_init()
 
     initial_mm.stack_start = _stack_start;
 
-
     // 初始化进程和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);
 
-
-
     initial_tss[0].rsp0 = initial_thread.rbp;
+   
 
     // 初始化进程的循环链表
     list_init(&initial_proc_union.pcb.list);
 
-    test_mm();
+
     kernel_thread(init, 10, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); // 初始化内核进程
     initial_proc_union.pcb.state = PROC_RUNNING;
 
     // 获取新的进程的pcb
     struct process_control_block *p = container_of(list_next(&current_pcb->list), struct process_control_block, list);
+
+	// 切换到新的内核线程
     switch_proc(current_pcb, p);
 }
 
@@ -193,34 +175,29 @@ void process_init()
  * @param stack_size 堆栈大小
  * @return unsigned long
  */
+
 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;
 
-    //printk("alloc_pages,bmp %#018lx\n", *(memory_management_struct.bmp));
-    
+
     // 获取一个物理页并在这个物理页内初始化pcb
-    struct Page *p = 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));
+    struct Page *pp = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
+
+
+    tsk = (struct process_control_block *)phys_2_virt(pp->addr_phys);
 
-    //printk("phys_addr\t%#018lx\n",p->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));
-    printk("33\n");
+
     // 将当前进程的pcb复制到新的pcb内
     *tsk = *current_pcb;
 
     // 将进程加入循环链表
     list_init(&tsk->list);
-    printk("44\n");
-    list_append(&initial_proc_union.pcb.list, &tsk->list);
-    printk("5\n");
+
+    list_add(&initial_proc_union.pcb.list, &tsk->list);
+
 
     ++(tsk->pid);
     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));
+
     // 设置进程的内核栈
     thd->rbp = (ul)tsk + STACK_SIZE;
     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;
 
     tsk->state = PROC_RUNNING;
-    printk("1111\n");
+
     return 0;
-}
+}

+ 133 - 130
kernel/process/process.h

@@ -16,7 +16,7 @@
 #include "ptrace.h"
 
 extern unsigned long _stack_start; // 导出内核层栈基地址(定义在head.S)
-extern void ret_from_intr();       // 导出从中断返回的函数(定义在entry.S)
+extern void ret_from_intr();	   // 导出从中断返回的函数(定义在entry.S)
 
 // 进程的内核栈大小 32K
 #define STACK_SIZE 32768
@@ -53,35 +53,35 @@ extern void ret_from_intr();       // 导出从中断返回的函数(定义在
  */
 struct mm_struct
 {
-    pml4t_t *pgd; // 内存页表指针
-    // 代码段空间
-    ul code_addr_start, code_addr_end;
-    // 数据段空间
-    ul data_addr_start, data_addr_end;
-    // 只读数据段空间
-    ul rodata_addr_start, rodata_addr_end;
-    // 动态内存分配区(堆区域)
-    ul brk_start, brk_end;
-    // 应用层栈基地址
-    ul stack_start;
+	pml4t_t *pgd; // 内存页表指针
+	// 代码段空间
+	ul code_addr_start, code_addr_end;
+	// 数据段空间
+	ul data_addr_start, data_addr_end;
+	// 只读数据段空间
+	ul rodata_addr_start, rodata_addr_end;
+	// 动态内存分配区(堆区域)
+	ul brk_start, brk_end;
+	// 应用层栈基地址
+	ul stack_start;
 };
 
 struct thread_struct
 {
-    // 内核层栈基指针
-    ul rbp; // in tss rsp0
-    // 内核层代码指针
-    ul rip;
-    // 内核层栈指针
-    ul rsp;
-
-    ul fs, gs;
-
-    ul cr2;
-    // 异常号
-    ul trap_num;
-    // 错误码
-    ul err_code;
+	// 内核层栈基指针
+	ul rbp; // in tss rsp0
+	// 内核层代码指针
+	ul rip;
+	// 内核层栈指针
+	ul rsp;
+
+	ul fs, gs;
+
+	ul cr2;
+	// 异常号
+	ul trap_num;
+	// 错误码
+	ul err_code;
 };
 
 // 进程标志位
@@ -93,43 +93,43 @@ struct thread_struct
  */
 struct process_control_block
 {
-    // 连接各个pcb的双向链表
-    struct List list;
+	// 连接各个pcb的双向链表
+	struct List list;
 
-    // 进程的状态
-    volatile long state;
-    // 进程标志:进程、线程、内核线程
-    unsigned long flags;
+	// 进程的状态
+	volatile long state;
+	// 进程标志:进程、线程、内核线程
+	unsigned long flags;
 
-    // 内存空间分布结构体, 记录内存页表和程序段信息
-    struct mm_struct *mm;
+	// 内存空间分布结构体, 记录内存页表和程序段信息
+	struct mm_struct *mm;
 
-    // 进程切换时保存的状态信息
-    struct thread_struct *thread;
+	// 进程切换时保存的状态信息
+	struct thread_struct *thread;
 
-    // 地址空间范围
-    // 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
-    // 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
-    ul addr_limit;
+	// 地址空间范围
+	// 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
+	// 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
+	ul addr_limit;
 
-    // 进程id
-    long pid;
+	// 进程id
+	long pid;
 
-    // 可用时间片
-    long counter;
+	// 可用时间片
+	long counter;
 
-    // 信号
-    long signal;
+	// 信号
+	long signal;
 
-    // 优先级
-    long priority;
+	// 优先级
+	long priority;
 };
 
 // 将进程的pcb和内核栈融合到一起,8字节对齐
 union proc_union
 {
-    struct process_control_block pcb;
-    ul stack[STACK_SIZE / sizeof(ul)];
+	struct process_control_block pcb;
+	ul stack[STACK_SIZE / sizeof(ul)];
 } __attribute__((aligned(8)));
 
 struct mm_struct initial_mm;
@@ -137,33 +137,33 @@ struct thread_struct initial_thread;
 
 // 设置初始进程的PCB
 #define INITIAL_PROC(proc)                \
-    {                                     \
-        .state = PROC_UNINTERRUPTIBLE,    \
-        .flags = PF_KTHREAD,              \
-        .mm = &initial_mm,                \
-        .thread = &initial_thread,        \
-        .addr_limit = 0xffff800000000000, \
-        .pid = 0,                         \
-        .counter = 1,                     \
-        .signal = 0,                      \
-        .priority = 0                     \
-    }
+	{                                     \
+		.state = PROC_UNINTERRUPTIBLE,    \
+		.flags = PF_KTHREAD,              \
+		.mm = &initial_mm,                \
+		.thread = &initial_thread,        \
+		.addr_limit = 0xffff800000000000, \
+		.pid = 0,                         \
+		.counter = 1,                     \
+		.signal = 0,                      \
+		.priority = 0                     \
+	}
 
 // 初始化 初始进程的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 mm_struct initial_mm = {0};
 struct thread_struct initial_thread =
-    {
-        .rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
-        .rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
-        .fs = KERNEL_DS,
-        .gs = KERNEL_DS,
-        .cr2 = 0,
-        .trap_num = 0,
-        .err_code = 0};
+	{
+		.rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
+		.rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
+		.fs = KERNEL_DS,
+		.gs = KERNEL_DS,
+		.cr2 = 0,
+		.trap_num = 0,
+		.err_code = 0};
 
 /**
  * @brief 任务状态段结构体
@@ -171,86 +171,89 @@ struct thread_struct initial_thread =
  */
 struct tss_struct
 {
-    unsigned int reserved0;
-    ul rsp0;
-    ul rsp1;
-    ul rsp2;
-    ul reserved1;
-    ul ist1;
-    ul ist2;
-    ul ist3;
-    ul ist4;
-    ul ist5;
-    ul ist6;
-    ul ist7;
-    ul reserved2;
-    unsigned short reserved3;
-    // io位图基地址
-    unsigned short io_map_base_addr;
+	unsigned int reserved0;
+	ul rsp0;
+	ul rsp1;
+	ul rsp2;
+	ul reserved1;
+	ul ist1;
+	ul ist2;
+	ul ist3;
+	ul ist4;
+	ul ist5;
+	ul ist6;
+	ul ist7;
+	ul reserved2;
+	unsigned short reserved3;
+	// io位图基地址
+	unsigned short io_map_base_addr;
 } __attribute__((packed)); // 使用packed表明是紧凑结构,编译器不会对成员变量进行字节对齐。
 
 // 设置初始进程的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                                             \
-    }
+	{                                                                     \
+		.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                                             \
+	}
 // 为每个核心初始化初始进程的tss
 struct tss_struct initial_tss[CPU_NUM] = {[0 ... CPU_NUM - 1] = INITIAL_TSS};
 
 // 获取当前的pcb
 struct process_control_block *get_current_pcb()
 {
-    struct process_control_block *current = NULL;
-    // 利用了当前pcb和栈空间总大小为32k大小对齐,将rsp低15位清空,即可获得pcb的起始地址
-    __asm__ __volatile__("andq %%rsp, %0   \n\t"
-                         : "=r"(current)
-                         : "0"(~32767UL));
-    return current;
-}
+	struct process_control_block *current = NULL;
+	// 利用了当前pcb和栈空间总大小为32k大小对齐,将rsp低15位清空,即可获得pcb的起始地址
+	__asm__ __volatile__("andq %%rsp, %0   \n\t"
+						 : "=r"(current)
+						 : "0"(~32767UL));
+	return current;
+};
+
 
 #define current_pcb get_current_pcb()
 
 #define GET_CURRENT_PCB    \
-    "movq %rsp, %rbx \n\t" \
-    "andq $-32768, %rbx\n\t"
+	"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                                                                                               \
-    {                                                                                                \
-        __asm__ __volatile__("pushq %%rbp  \n\t"                                                     \
-                             "pushq %%rax    \n\t"                                                   \
-                             "movq %%rsp, %0 \n\t"                                                   \
-                             "movq %2, %%rax \n\t"                                                   \
-                             "leaq 1f(%%rip), %%rax \n\t"                                            \
-                             "movq %%rax, %1 \n\t"                                                    \
-                             "pushq %3 \n\t"                                                         \
-                             "jmp __switch_to    \n\t"                                               \
-                             "1:     \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)
+
+#define switch_proc(prev, next)                                                                     \
+	do                                                                                              \
+	{                                                                                               \
+                                                                                                    \
+		__asm__ __volatile__("pushq %%rbp  \n\t"                                                    \
+							 "pushq %%rax    \n\t"                                                  \
+							 "movq %%rsp, %0 \n\t"                                                  \
+							 "movq %2, %%rsp \n\t"                                                  \
+							 "leaq 1f(%%rip), %%rax \n\t"                                           \
+							 "movq %%rax, %1 \n\t"                                                  \
+							 "pushq %3 \n\t"                                                        \
+							 "jmp __switch_to    \n\t"                                              \
+							 "1:     \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 初始化系统的第一个进程

+ 50 - 28
kernel/process/ptrace.h

@@ -1,30 +1,52 @@
-#pragma once
-#include "../common/glib.h"
-// 进程执行现场的寄存器状态
+/***************************************************
+*		版权声明
+*
+*	本操作系统名为:MINE
+*	该操作系统未经授权不得以盈利或非盈利为目的进行开发,
+*	只允许个人学习以及公开交流使用
+*
+*	代码最终所有权及解释权归田宇所有;
+*
+*	本模块作者:	田宇
+*	EMail:		[email protected]
+*
+*
+***************************************************/
+
+#ifndef __PTRACE_H__
+
+#define __PTRACE_H__
+
+/*
+
+*/
+
 struct pt_regs
 {
-    ul r15;
-    ul r14;
-    ul r13;
-    ul r12;
-    ul r11;
-    ul r10;
-    ul r9;
-    ul r8;
-    ul rbx;
-    ul rcx;
-    ul rdx;
-    ul rsi;
-    ul rdi;
-    ul rbp;
-    ul ds;
-    ul es;
-    ul rax;
-    ul func;
-    ul err_code;
-    ul rip;
-    ul cs;
-    ul rflags;
-    ul rsp;
-    ul ss;
-};
+	unsigned long r15;
+	unsigned long r14;
+	unsigned long r13;
+	unsigned long r12;
+	unsigned long r11;
+	unsigned long r10;
+	unsigned long r9;
+	unsigned long r8;
+	unsigned long rbx;
+	unsigned long rcx;
+	unsigned long rdx;
+	unsigned long rsi;
+	unsigned long rdi;
+	unsigned long rbp;
+	unsigned long ds;
+	unsigned long es;
+	unsigned long rax;
+	unsigned long func;
+	unsigned long errcode;
+	unsigned long rip;
+	unsigned long cs;
+	unsigned long rflags;
+	unsigned long rsp;
+	unsigned long ss;
+};
+
+#endif