🐛 解决了ap处理器无法处理异常的bug(原因是设置错了TSS表)

This commit is contained in:
fslongjin
2022-04-06 18:36:56 +08:00
parent b7df2805ed
commit cc39911d4d
8 changed files with 56 additions and 96 deletions

View File

@ -36,7 +36,7 @@ clock: sync=none, time0=local, rtc_sync=0
# no loader
log: -
logprefix: %t%e%d
debug: action=ignore#, cpu0=report
debug: action=ignore, cpu0=report
info: action=report
error: action=report
panic: action=ask

View File

@ -148,19 +148,20 @@ void set_system_trap_gate(unsigned int n, unsigned char ist, void *addr)
* @brief 初始化TSS表的内容
*
*/
void set_TSS64(ul rsp0, ul rsp1, ul rsp2, ul ist1, ul ist2, ul ist3, ul ist4, ul ist5, ul ist6, ul ist7)
void set_tss64(unsigned int * Table,unsigned long rsp0,unsigned long rsp1,unsigned long rsp2,unsigned long ist1,unsigned long ist2,unsigned long ist3,
unsigned long ist4,unsigned long ist5,unsigned long ist6,unsigned long ist7)
{
*(ul *)(TSS64_Table + 1) = rsp0;
*(ul *)(TSS64_Table + 3) = rsp1;
*(ul *)(TSS64_Table + 5) = rsp2;
*(unsigned long *)(Table+1) = rsp0;
*(unsigned long *)(Table+3) = rsp1;
*(unsigned long *)(Table+5) = rsp2;
*(ul *)(TSS64_Table + 9) = ist1;
*(ul *)(TSS64_Table + 11) = ist2;
*(ul *)(TSS64_Table + 13) = ist3;
*(ul *)(TSS64_Table + 15) = ist4;
*(ul *)(TSS64_Table + 17) = ist5;
*(ul *)(TSS64_Table + 19) = ist6;
*(ul *)(TSS64_Table + 21) = ist7;
*(unsigned long *)(Table+9) = ist1;
*(unsigned long *)(Table+11) = ist2;
*(unsigned long *)(Table+13) = ist3;
*(unsigned long *)(Table+15) = ist4;
*(unsigned long *)(Table+17) = ist5;
*(unsigned long *)(Table+19) = ist6;
*(unsigned long *)(Table+21) = ist7;
}
#endif

View File

@ -59,7 +59,8 @@ void sys_vector_init()
void do_divide_error(struct pt_regs *regs, unsigned long error_code)
{
kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
kerror("do_divide_error(0)");
//kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;

View File

@ -342,8 +342,10 @@ ENTRY(_start64)
// === GDTR ====
lgdt GDT_POINTER(%rip) //rip, PICposition independent code
//lgdt $GDT_POINTER
// === IDTR ====
lidt IDT_POINTER(%rip)
//lidt $IDT_POINTER
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
@ -602,6 +604,7 @@ GDT_Table:
.fill 100, 8, 0 // 10-11 TSS(9) 80 TSS128bit
GDT_END:
.global GDT_POINTER
GDT_POINTER:
GDT_LIMIT: .word GDT_END - GDT_Table - 1 // GDT
GDT_BASE: .quad GDT_Table
@ -613,6 +616,7 @@ IDT_Table:
.fill 512, 8, 0 // 512*8IDT
IDT_END:
.global IDT_POINTER
IDT_POINTER:
IDT_LIMIT: .word IDT_END - IDT_Table - 1
IDT_BASE: .quad IDT_Table

View File

@ -149,11 +149,11 @@ void system_initialize()
load_TR(10); // 加载TR寄存器
ul tss_item_addr = 0x7c00;
set_TSS64((ul)&TSS64_Table, _stack_start, _stack_start, _stack_start, tss_item_addr,
tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr);
set_tss64(TSS64_Table, _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);
cpu_core_info[0].stack_start = _stack_start;
cpu_core_info[0].tss_vaddr = (ul)TSS64_Table;
cpu_core_info[0].tss_vaddr = &TSS64_Table;
// 初始化中断描述符表
sys_vector_init();
@ -165,11 +165,8 @@ void system_initialize()
// 初始化中断模块
irq_init();
kdebug("23232");
smp_init();
kdebug("12121221212");
//smp_ap_start();
hlt();
// 先初始化系统调用模块
syscall_init();
@ -185,7 +182,6 @@ void system_initialize()
// 再初始化进程模块。顺序不能调转
// process_init();
}
//操作系统内核从这里开始执行
@ -228,8 +224,7 @@ void Start_Kernel(void)
analyze_mousecode();
}
*/
while (1)
;
hlt();
}
void ignore_int()

View File

@ -18,7 +18,7 @@
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,
set_tss64(TSS64_Table, 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"
@ -201,7 +201,7 @@ 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);
set_tss64(TSS64_Table, 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;

View File

@ -64,31 +64,27 @@ _apu_code32:
mov %ax, %fs
mov %ax, %gs
//
leal (_apu_boot_tmp_stack_end - _apu_boot_base)(%esi), %eax
movl %eax, %esp
// 1. PAE
mov %cr4, %eax
or $(1<<5), %eax
mov %eax, %cr4
/*
movl $enter_head_from_ap_boot, %eax
jmpl *%eax
hlt
//
leal (_apu_boot_tmp_stack_end - _apu_boot_base)(%esi), %eax
movl %eax, %esp
*/
// open PAE
movl %cr4, %eax
bts $5, %eax
movl %eax, %cr4
//
movl $pml4, %eax // bsp32
movl %eax, %cr3
// enable long mode
movl $0xC0000080, %ecx
rdmsr
@ -97,8 +93,8 @@ _apu_code32:
// enable PE and paging
mov %cr0, %eax
or $(1<<31), %eax
or $(1<<0), %eax
bts $0, %eax
bts $31, %eax
mov %eax, %cr0
// 64
@ -115,6 +111,7 @@ _apu_code64:
movq %rax, %fs
movq %rax, %gs
//now enable SSE and the like
movq %cr0, %rax
and $0xFFFB, %ax //clear coprocessor emulation CR0.EM

View File

@ -34,7 +34,7 @@ void smp_init()
icr_entry.res_2 = 0;
icr_entry.res_3 = 0;
for (int i = 1; i < total_processor_num; ++i) // i从1开始不初始化bsp
for (int i = 1; i < 2; ++i) // i从1开始不初始化bsp
{
current_starting_cpu = i;
@ -47,11 +47,15 @@ void smp_init()
kdebug("[core %d] acpi processor UID=%d, APIC ID=%d, flags=%#010lx", i, proc_local_apic_structs[i]->ACPI_Processor_UID, proc_local_apic_structs[i]->ACPI_ID, proc_local_apic_structs[i]->flags);
// 为每个AP处理器分配栈空间、tss空间
cpu_core_info[i].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
cpu_core_info[i].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0);
kdebug("cpu_core_info[i].stack_start =%#018lx", (uint64_t)kmalloc(STACK_SIZE, 0));
cpu_core_info[i].stack_start += STACK_SIZE;
kdebug("cpu_core_info[i].stack_base =%#018lx", (uint64_t)kmalloc(STACK_SIZE, 0));
cpu_core_info[i].tss_vaddr = (uint64_t)kmalloc(128, 0);
set_tss_descriptor(10 + (i * 2), (void *)(cpu_core_info[i].tss_vaddr));
set_TSS64(cpu_core_info[i].tss_vaddr, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start);
set_tss64((uint*)cpu_core_info[i].tss_vaddr, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start,cpu_core_info[i].stack_start);
kdebug("GDT Table %#018lx, \t %#018lx", GDT_Table[10 + i * 2], GDT_Table[10 + i * 2 + 1]);
kdebug("(cpu_core_info[i].tss_vaddr)=%#018lx", (cpu_core_info[i].tss_vaddr));
kdebug("(cpu_core_info[i].stack_start)=%#018lx", (cpu_core_info[i].stack_start));
@ -66,10 +70,8 @@ void smp_init()
wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
}
hlt();
}
/**
@ -79,62 +81,22 @@ void smp_init()
void smp_ap_start()
{
// 切换栈基地址
// uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
//uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
__asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
: "memory");
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
: "memory");
/*
__asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(stack_start)
: "memory");
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
: "memory");*/
ksuccess("AP core successfully started!");
kdebug("current=%d", current_starting_cpu);
apic_init_ap_core_local_apic();
// apic_init_ap_core_local_apic();
/*
kinfo("Initializing AP-core's local apic...");
uint eax, edx;
// 启用xAPIC 和x2APIC
__asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
"rdmsr \n\t"
"bts $10, %%rax \n\t"
"bts $11, %%rax \n\t"
"wrmsr \n\t"
"movq $0x1b, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
// kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
// 检测是否成功启用xAPIC和x2APIC
if (eax & 0xc00)
kinfo("xAPIC & x2APIC enabled!");
// 设置SVR寄存器开启local APIC、禁止EOI广播
// enable SVR[8]
__asm__ __volatile__("movq $0x80f, %%rcx \n\t"
"rdmsr \n\t"
"bts $8, %%rax \n\t"
// "bts $12, %%rax\n\t"
"wrmsr \n\t"
"movq $0x80f, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)
:
: "memory");
if (eax & 0x100)
printk_color(RED, YELLOW, "SVR[8] enabled\n");
if (edx & 0x1000)
printk_color(RED, YELLOW, "SVR[12] enabled\n");
// get local APIC ID
__asm__ __volatile__("movq $0x802, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)
:
: "memory");
printk_color(RED, YELLOW, "x2APIC ID:%#010x\n", eax);
*/
load_TR(10 + current_starting_cpu * 2);
sti();
kdebug("IDT_addr = %#018lx", &IDT_Table);