🐛 修复了无法切换进程、无法进行浮点运算的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",
"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"
}
}

View File

@ -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

View File

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

View File

@ -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;
}

View File

@ -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;

View File

@ -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 //

View File

@ -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中的第几项

View File

@ -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)

View File

@ -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);

View File

@ -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)
;

View File

@ -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();

View File

@ -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_int8B

View File

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

View File

@ -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();
}
//操作系统内核从这里开始执行
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;
}
}

View File

@ -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);
}
}

View File

@ -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))

View File

@ -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位于栈顶
* 7unsigned 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" // 在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");
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"
);
/**
* @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));
//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);
// 获取一个物理页并在这个物理页内初始化pcb
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);
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;
}
}

View File

@ -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 rbp; // in tss rsp0
// 内核层代码指针
ul rip;
// 内核层栈指针
ul rsp;
ul fs, gs;
ul fs, gs;
ul cr2;
// 异常号
ul trap_num;
// 错误码
ul err_code;
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

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
{
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