diff --git a/kernel/common/cpu.h b/kernel/common/cpu.h index bfe5b7d7..e1300096 100644 --- a/kernel/common/cpu.h +++ b/kernel/common/cpu.h @@ -57,5 +57,6 @@ void cpu_init(void); struct cpu_core_info { uint64_t stack_start; // 栈基地址 + uint64_t ist_stack_start; // IST栈基地址 uint64_t tss_vaddr; // tss地址 }cpu_core_info[MAX_CPU_NUM]; \ No newline at end of file diff --git a/kernel/driver/acpi/acpi.h b/kernel/driver/acpi/acpi.h index f2dfaa3b..1b792e26 100644 --- a/kernel/driver/acpi/acpi.h +++ b/kernel/driver/acpi/acpi.h @@ -127,7 +127,7 @@ struct acpi_Processor_Local_APIC_Structure_t struct apic_Interrupt_Controller_Structure_header_t header; unsigned char ACPI_Processor_UID; // 处理器的local apic id - unsigned char ACPI_ID; + unsigned char local_apic_id; //详见 ACPI Specification Version 6.3, Table 5-47 uint flags; }; diff --git a/kernel/exception/gate.h b/kernel/exception/gate.h index 758bcf67..01065907 100644 --- a/kernel/exception/gate.h +++ b/kernel/exception/gate.h @@ -26,7 +26,7 @@ struct gate_struct extern struct desc_struct GDT_Table[]; // GDT_Table是head.S中的GDT_Table extern struct gate_struct IDT_Table[]; // IDT_Table是head.S中的IDT_Table -extern unsigned int TSS64_Table[26]; +//extern unsigned int TSS64_Table[26]; struct gdtr { diff --git a/kernel/exception/trap.c b/kernel/exception/trap.c index 3cad74c5..5debb717 100644 --- a/kernel/exception/trap.c +++ b/kernel/exception/trap.c @@ -2,14 +2,14 @@ #include "gate.h" #include "../process/ptrace.h" #include "../common/kprint.h" - +#include // 0 #DE 除法错误 void do_divide_error(struct pt_regs *regs, unsigned long error_code) { //kerror("do_divide_error(0)"); - kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -20,7 +20,7 @@ void do_debug(struct pt_regs *regs, unsigned long error_code) { printk("[ "); printk_color(RED, BLACK, "ERROR / TRAP"); - printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -32,7 +32,7 @@ void do_nmi(struct pt_regs *regs, unsigned long error_code) printk("[ "); printk_color(BLUE, BLACK, "INT"); - printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -44,7 +44,7 @@ void do_int3(struct pt_regs *regs, unsigned long error_code) printk("[ "); printk_color(YELLOW, BLACK, "TRAP"); - printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -56,7 +56,7 @@ void do_overflow(struct pt_regs *regs, unsigned long error_code) printk("[ "); printk_color(YELLOW, BLACK, "TRAP"); - printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -66,7 +66,7 @@ void do_overflow(struct pt_regs *regs, unsigned long error_code) void do_bounds(struct pt_regs *regs, unsigned long error_code) { - kerror("do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -76,7 +76,7 @@ void do_bounds(struct pt_regs *regs, unsigned long error_code) void do_undefined_opcode(struct pt_regs *regs, unsigned long error_code) { - kerror("do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx", error_code, regs->rsp, regs->rip); + kerror("do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -86,7 +86,7 @@ void do_undefined_opcode(struct pt_regs *regs, unsigned long error_code) void do_dev_not_avaliable(struct pt_regs *regs, unsigned long error_code) { - kerror("do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -98,7 +98,7 @@ void do_double_fault(struct pt_regs *regs, unsigned long error_code) printk("[ "); printk_color(RED, BLACK, "Terminate"); - printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -108,7 +108,7 @@ void do_double_fault(struct pt_regs *regs, unsigned long error_code) void do_coprocessor_segment_overrun(struct pt_regs *regs, unsigned long error_code) { - kerror("do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -120,7 +120,7 @@ void do_invalid_TSS(struct pt_regs *regs, unsigned long error_code) printk("["); printk_color(RED, BLACK, "ERROR"); - printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); printk_color(YELLOW, BLACK, "Information:\n"); // 解析错误码 @@ -149,7 +149,7 @@ void do_invalid_TSS(struct pt_regs *regs, unsigned long error_code) void do_segment_not_exists(struct pt_regs *regs, unsigned long error_code) { - kerror("do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -159,7 +159,7 @@ void do_segment_not_exists(struct pt_regs *regs, unsigned long error_code) void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code) { - kerror("do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -170,7 +170,7 @@ void do_general_protection(struct pt_regs *regs, unsigned long error_code) { - kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); if (error_code & 0x01) printk_color(RED, BLACK, "The exception occurred during delivery of an event external to the program,such as an interrupt or an earlier exception.\n"); @@ -233,7 +233,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long error_code) void do_x87_FPU_error(struct pt_regs *regs, unsigned long error_code) { - kerror("do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -243,7 +243,7 @@ void do_x87_FPU_error(struct pt_regs *regs, unsigned long error_code) void do_alignment_check(struct pt_regs *regs, unsigned long error_code) { - kerror("do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -253,7 +253,7 @@ void do_alignment_check(struct pt_regs *regs, unsigned long error_code) void do_machine_check(struct pt_regs *regs, unsigned long error_code) { - kerror("do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -263,7 +263,7 @@ void do_machine_check(struct pt_regs *regs, unsigned long error_code) void do_SIMD_exception(struct pt_regs *regs, unsigned long error_code) { - kerror("do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; @@ -273,7 +273,7 @@ void do_SIMD_exception(struct pt_regs *regs, unsigned long error_code) void do_virtualization_exception(struct pt_regs *regs, unsigned long error_code) { - kerror("do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); + kerror("do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); while (1) ; diff --git a/kernel/head.S b/kernel/head.S index f413eac8..2861f5ee 100644 --- a/kernel/head.S +++ b/kernel/head.S @@ -316,7 +316,7 @@ ENTRY(_start64) lidt IDT_POINTER(%rip) //lidt $IDT_POINTER movq GDT_POINTER(%rip), %r12 - movq _stack_start(%rip), %rsp + movq head_stack_start(%rip), %rsp // 分支,判断是否为apu movq $0x1b, %rcx // 根据IA32_APIC_BASE.BSP[8]标志位判断处理器是否为apu @@ -368,7 +368,7 @@ entry64: movq %rax, %gs movq %rax, %ss - movq _stack_start(%rip), %rsp //rsp的地址 + movq head_stack_start(%rip), %rsp //rsp的地址 // 重新加载GDT和IDT,加载到高地址 leaq GDT_Table(%rip), %r8 @@ -423,7 +423,7 @@ repeat_set_idt: SetUp_TSS64: // == 设置64位的任务状态段表 === //rdx保存高8B, rax保存低8B - leaq TSS64_Table(%rip), %rdx + leaq TSS64_Table(%rip), %rdx // 获取定义在process.c中的initial_tss[0]的地址 movq $0xffff800000000000, %r8 addq %r8, %rdx @@ -541,7 +541,7 @@ go_to_ignore_int: .quad ignore_int -ENTRY(_stack_start) +ENTRY(head_stack_start) .quad initial_proc_union + 32768 diff --git a/kernel/main.c b/kernel/main.c index 51b3bd1d..0b12aaf5 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -88,15 +88,15 @@ void system_initialize() // 重新加载gdt和idt ul tss_item_addr = (ul)phys_2_virt(0x7c00); - kdebug("TSS64_Table=%#018lx", (void *)TSS64_Table); - kdebug("&TSS64_Table=%#018lx", (void *)&TSS64_Table); + _stack_start = head_stack_start; // 保存init proc的栈基地址(由于之后取消了地址重映射,因此必须在这里重新保存) kdebug("_stack_start=%#018lx", _stack_start); + load_TR(10); // 加载TR寄存器 - set_tss64((uint *)phys_2_virt(TSS64_Table), _stack_start, _stack_start, _stack_start, tss_item_addr, + set_tss64((uint *)&initial_tss[0], _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 = (uint64_t)phys_2_virt((uint64_t)TSS64_Table); + cpu_core_info[0].tss_vaddr = (uint64_t)&initial_tss[0]; kdebug("cpu_core_info[0].tss_vaddr=%#018lx", cpu_core_info[0].tss_vaddr); kdebug("cpu_core_info[0].stack_start%#018lx", cpu_core_info[0].stack_start); @@ -106,6 +106,19 @@ void system_initialize() // 初始化内存管理单元 mm_init(); + + // =========== 重新设置initial_tss[0]的ist + uchar *ptr = (uchar*)kmalloc(STACK_SIZE, 0)+STACK_SIZE; + ((struct process_control_block*)(ptr-STACK_SIZE))->cpu_id = 0; + + initial_tss[0].ist1 = (ul)ptr; + initial_tss[0].ist2 = (ul)ptr; + initial_tss[0].ist3 = (ul)ptr; + initial_tss[0].ist4 = (ul)ptr; + initial_tss[0].ist5 = (ul)ptr; + initial_tss[0].ist6 = (ul)ptr; + initial_tss[0].ist7 = (ul)ptr; + // =========================== acpi_init(); @@ -137,10 +150,10 @@ void system_initialize() HPET_init(); - show_welcome(); + while(1) { - printk_color(ORANGE, BLACK, "Initial_proc\n"); + printk_color(ORANGE, BLACK, "i\n"); } } @@ -158,6 +171,8 @@ void Start_Kernel(void) reload_gdt(); reload_idt(); + // 重新设置TSS描述符 + set_tss_descriptor(10, (void *)(&initial_tss[0])); mb2_info &= 0xffffffff; mb2_magic &= 0xffffffff; diff --git a/kernel/process/process.c b/kernel/process/process.c index 10168235..f4833036 100644 --- a/kernel/process/process.c +++ b/kernel/process/process.c @@ -8,8 +8,9 @@ #include #include -extern void system_call(void); +extern void system_call(void); +ul _stack_start; // initial proc的栈基地址(虚拟地址) struct mm_struct initial_mm = {0}; struct thread_struct initial_thread = { @@ -40,9 +41,9 @@ struct tss_struct initial_tss[MAX_CPU_NUM] = {[0 ... MAX_CPU_NUM - 1] = INITIAL_ void __switch_to(struct process_control_block *prev, struct process_control_block *next) { - initial_tss[0].rsp0 = next->thread->rbp; - set_tss64((uint *)phys_2_virt(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); + initial_tss[proc_current_cpu_id].rsp0 = next->thread->rbp; + //set_tss64((uint *)phys_2_virt(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" : "=a"(prev->thread->fs)); @@ -52,12 +53,13 @@ void __switch_to(struct process_control_block *prev, struct process_control_bloc __asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs)); __asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs)); //wrmsr(0x175, next->thread->rbp); + uint color; + if(proc_current_cpu_id == 0) + color = WHITE; + else + color = YELLOW; - // kdebug("next=%#018lx", next); - // kdebug("initial_tss[0].rsp1=%#018lx", initial_tss[0].rsp1); - // kdebug("prev->thread->rsp0:%#018lx\n", prev->thread->rbp); - // kdebug("next->thread->rsp0:%#018lx\n", next->thread->rbp); - // kdebug("next->thread->rip:%#018lx\n", next->thread->rip); + } /** @@ -73,7 +75,7 @@ void user_level_function() // enter_syscall(SYS_PRINTF, (ul) "test_sys_printf\n", 0, 0, 0, 0, 0, 0, 0); //while(1); long ret = 0; - // color_printk(RED,BLACK,"user_level_function task is running\n"); + // printk_color(RED,BLACK,"user_level_function task is running\n"); char string[] = "User level process.\n"; /* @@ -291,8 +293,8 @@ void process_init() initial_mm.brk_start = 0; initial_mm.brk_end = memory_management_struct.kernel_end; - initial_mm.stack_start = *(ul *)phys_2_virt(&_stack_start); - + initial_mm.stack_start = _stack_start; + /* // 向MSR寄存器组中的 IA32_SYSENTER_CS寄存器写入内核的代码段的地址 wrmsr(0x174, KERNEL_CS); @@ -303,9 +305,9 @@ void process_init() wrmsr(0x176, (ul)system_call); */ // 初始化进程和tss - set_tss64((uint *)phys_2_virt(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); + //set_tss64((uint *)phys_2_virt(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; + initial_tss[proc_current_cpu_id].rsp0 = initial_thread.rbp; /* kdebug("initial_thread.rbp=%#018lx", initial_thread.rbp); kdebug("initial_tss[0].rsp1=%#018lx", initial_tss[0].rsp1); @@ -357,6 +359,7 @@ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned tsk->priority = 2; tsk->preempt_count = 0; ++(tsk->pid); + tsk->cpu_id = proc_current_cpu_id; tsk->state = PROC_UNINTERRUPTIBLE; list_init(&tsk->list); list_add(&initial_proc_union.pcb.list, &tsk->list); diff --git a/kernel/process/process.h b/kernel/process/process.h index f314baa4..a7bf3f7e 100644 --- a/kernel/process/process.h +++ b/kernel/process/process.h @@ -16,8 +16,6 @@ #include "../syscall/syscall.h" #include "ptrace.h" - - // 进程的内核栈大小 32K #define STACK_SIZE 32768 @@ -100,6 +98,7 @@ struct process_control_block unsigned long flags; int64_t preempt_count; // 持有的自旋锁的数量 long signal; + long cpu_id; // 当前进程在哪个CPU核心上运行 // 内存空间分布结构体, 记录内存页表和程序段信息 struct mm_struct *mm; @@ -126,7 +125,6 @@ union proc_union ul stack[STACK_SIZE / sizeof(ul)]; } __attribute__((aligned(8))); - // 设置初始进程的PCB #define INITIAL_PROC(proc) \ { \ @@ -140,12 +138,9 @@ union proc_union .signal = 0, \ .priority = 2, \ .preempt_count = 0, \ + .cpu_id = 0 \ } - - - - /** * @brief 任务状态段结构体 * @@ -190,7 +185,6 @@ struct tss_struct .io_map_base_addr = 0 \ } - // 获取当前的pcb struct process_control_block *get_current_pcb() { @@ -250,7 +244,11 @@ void process_init(); */ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size); -extern unsigned long _stack_start; // 导出内核层栈基地址(定义在head.S) +// 获取当前cpu id +#define proc_current_cpu_id (current_pcb->cpu_id) + +extern unsigned long head_stack_start; // 导出内核层栈基地址(定义在head.S) +extern ul _stack_start; extern void ret_from_intr(void); // 导出从中断返回的函数(定义在entry.S) extern struct tss_struct initial_tss[MAX_CPU_NUM]; diff --git a/kernel/sched/sched.c b/kernel/sched/sched.c index 44457e57..c7841213 100644 --- a/kernel/sched/sched.c +++ b/kernel/sched/sched.c @@ -72,7 +72,6 @@ void sched_cfs() break; } } - switch_proc(current_pcb, proc); } else // 不进行切换 diff --git a/kernel/smp/smp.c b/kernel/smp/smp.c index 89246840..c29b3144 100644 --- a/kernel/smp/smp.c +++ b/kernel/smp/smp.c @@ -24,7 +24,7 @@ void smp_init() apic_get_ics(ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC, tmp_vaddr, &total_processor_num); - //kdebug("processor num=%d", total_processor_num); + // kdebug("processor num=%d", total_processor_num); for (int i = 0; i < total_processor_num; ++i) proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]); @@ -44,25 +44,36 @@ void smp_init() { if (proc_local_apic_structs[i]->ACPI_Processor_UID == 0) --total_processor_num; + if (proc_local_apic_structs[i]->local_apic_id > total_processor_num) + continue; + spin_lock(&multi_core_starting_lock); - current_starting_cpu = i; + current_starting_cpu = proc_local_apic_structs[i]->local_apic_id; - 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; + 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]->local_apic_id, proc_local_apic_structs[i]->flags); - cpu_core_info[i].tss_vaddr = (uint64_t)kmalloc(128, 0); + // 为每个AP处理器分配栈空间 + cpu_core_info[current_starting_cpu].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE; + cpu_core_info[current_starting_cpu].ist_stack_start = (uint64_t)(kmalloc(STACK_SIZE, 0)) + STACK_SIZE; + memset((void *)cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE, 0, STACK_SIZE); + memset((void *)cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE, 0, STACK_SIZE); - set_tss_descriptor(10 + (i * 2), (void *)virt_2_phys(cpu_core_info[i].tss_vaddr)); + // 设置ap处理器的中断栈及内核栈中的cpu_id + ((struct process_control_block *)(cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id; + ((struct process_control_block *)(cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id; + + cpu_core_info[current_starting_cpu].tss_vaddr = (uint64_t)&initial_tss[current_starting_cpu]; + + memset(&initial_tss[current_starting_cpu], 0, sizeof(struct tss_struct)); + + set_tss_descriptor(10 + (current_starting_cpu * 2), (void *)(cpu_core_info[current_starting_cpu].tss_vaddr)); + + set_tss64((uint *)cpu_core_info[current_starting_cpu].tss_vaddr, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start, + cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_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("phys_2_virt(GDT_Table)=%#018lx",phys_2_virt(GDT_Table)); - //kdebug("GDT Table %#018lx, \t %#018lx", *(ul *)(phys_2_virt(GDT_Table) + 10 + i * 2), *(ul *)(phys_2_virt(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)); // 连续发送两次start-up IPI - ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->ACPI_ID); - ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->ACPI_ID); + ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->local_apic_id); + ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->local_apic_id); } while (num_cpu_started != total_processor_num) @@ -70,9 +81,8 @@ void smp_init() : "memory"); kinfo("Cleaning page table remapping...\n"); - + // 由于ap处理器初始化过程需要用到0x00处的地址,因此初始化完毕后才取消内存地址的重映射 - //todo: 取消低0-2M的地址映射 for (int i = 0; i < 128; ++i) { @@ -80,7 +90,6 @@ void smp_init() } kinfo("Successfully cleaned page table remapping!\n"); - } /** @@ -111,9 +120,11 @@ void smp_ap_start() load_TR(10 + current_starting_cpu * 2); sti(); - //kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table)); + // kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table)); memset(current_pcb, 0, sizeof(struct process_control_block)); spin_unlock(&multi_core_starting_lock); + + int a = 1 / 0; while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt hlt(); } \ No newline at end of file diff --git a/kernel/smp/smp.h b/kernel/smp/smp.h index 2ad1cbe2..cc958b6d 100644 --- a/kernel/smp/smp.h +++ b/kernel/smp/smp.h @@ -7,6 +7,8 @@ #define MAX_SUPPORTED_PROCESSOR_NUM 1024 + + extern uchar _apu_boot_start[]; extern uchar _apu_boot_end[]; /**