浏览代码

完善ipi通信机制

fslongjin 3 年之前
父节点
当前提交
777932704d

+ 16 - 5
kernel/arch/x86_64/x86_64_ipi.c

@@ -18,20 +18,31 @@ void ipi_send_IPI(uint32_t dest_mode, uint32_t deliver_status, uint32_t level, u
 
     // x2APIC下,ICR寄存器地址为0x830
     // xAPIC下则为0xfee00300(31-0) 0xfee00310 (63-32)
-    if (apic_type)  // x2APIC
+    if (apic_type) // x2APIC
     {
         icr_entry.destination.x2apic_destination = destination;
         wrmsr(0x830, *(unsigned long *)&icr_entry); // 发送ipi
     }
-    else    // xAPIC
+    else // xAPIC
     {
 
         icr_entry.destination.apic_destination.dest_field = destination & 0xff;
         icr_entry.destination.apic_destination.res_4 = 0;
         // 先向高32bit写数据,然后再向低32bit写数据,不能调转
-        *(uint32_t*)(APIC_LOCAL_APIC_VIRT_BASE_ADDR+0x310) = (uint32_t)(((*(ul*)&icr_entry)>>32)&0xffff);
-        *(uint32_t*)(APIC_LOCAL_APIC_VIRT_BASE_ADDR+0x300) = (uint32_t)((*(ul*)&icr_entry)&0xffff);
+        *(uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + 0x310) = (uint32_t)(((*(ul *)&icr_entry) >> 32) & 0xffff);
+        *(uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + 0x300) = (uint32_t)((*(ul *)&icr_entry) & 0xffff);
     }
+}
 
-    
+int ipi_regiserIPI(uint64_t irq_num, void *arg,
+                   void (*handler)(uint64_t irq_num, uint64_t param, struct pt_regs *regs),
+                   uint64_t param,  hardware_intr_controller *controller, char *irq_name)
+{
+    irq_desc_t *p = &SMP_IPI_desc[irq_num-200];
+    p->controller = NULL;   // 由于ipi不涉及到具体的硬件操作,因此不需要controller
+    p->irq_name = irq_name;
+    p->parameter = param;
+    p->flags = 0;
+    p->handler = handler;
+    return 0;
 }

+ 19 - 4
kernel/arch/x86_64/x86_64_ipi.h

@@ -4,9 +4,9 @@
  * @brief 多核通信驱动
  * @version 0.1
  * @date 2022-04-07
- * 
+ *
  * @copyright Copyright (c) 2022
- * 
+ *
  */
 
 #pragma once
@@ -16,7 +16,7 @@
 
 /**
  * @brief 发送ipi消息
- * 
+ *
  * @param dest_mode 目标模式
  * @param deliver_status 投递模式
  * @param level 信号驱动电平
@@ -28,4 +28,19 @@
  * @param destination 投递目标
  */
 void ipi_send_IPI(uint32_t dest_mode, uint32_t deliver_status, uint32_t level, uint32_t trigger,
-                         uint32_t vector, uint32_t deliver_mode, uint32_t dest_shorthand, bool apic_type,uint32_t destination);
+                  uint32_t vector, uint32_t deliver_mode, uint32_t dest_shorthand, bool apic_type, uint32_t destination);
+
+/**
+ * @brief ipi中断处理注册函数
+ * 
+ * @param irq_num 中断向量号
+ * @param arg 参数
+ * @param handler 处理函数
+ * @param param 参数
+ * @param controller 当前为NULL 
+ * @param irq_name ipi中断名
+ * @return int 成功:0
+ */
+int ipi_regiserIPI(uint64_t irq_num, void *arg,
+                   void (*handler)(uint64_t irq_num, uint64_t param, struct pt_regs *regs),
+                   uint64_t param, hardware_intr_controller *controller, char *irq_name);

+ 5 - 9
kernel/driver/interrupt/apic/apic.c

@@ -482,17 +482,13 @@ void do_IRQ(struct pt_regs *rsp, ul number)
     else if (number > 0x80)
 
     {
-        //printk_color(RED, BLACK, "SMP IPI [ %d ]\n", number);
+        // printk_color(RED, BLACK, "SMP IPI [ %d ]\n", number);
         apic_local_apic_edge_ack(number);
-        if (number == 0xc8) // 来自BSP的HPET中断消息
+        
         {
-            sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies -= 2;
-            ++(current_pcb->virtual_runtime);
-
-            if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
-                current_pcb->flags |= PROC_NEED_SCHED;
-
-            //printk_color(RED, BLACK, "CPU_exec_task_jiffies:%d current_pcb = %#018lx\t current_pcb->thread=%#018lx\n", sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies, (ul)current_pcb, (ul)current_pcb->thread);
+            irq_desc_t * irq = &SMP_IPI_desc[number-200];
+            if(irq->handler!=NULL)
+                irq->handler(number, irq->parameter, rsp);
         }
     }
     else

+ 1 - 20
kernel/driver/timers/HPET/HPET.c

@@ -65,26 +65,7 @@ void HPET_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
         if (container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list)->expire_jiffies <= timer_jiffies)
             set_softirq_status(TIMER_SIRQ);
 
-        switch (current_pcb->priority)
-        {
-        case 0:
-        case 1:
-            --sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies;
-            ++current_pcb->virtual_runtime;
-            break;
-        case 2:
-        default:
-            sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies -= 2;
-            current_pcb->virtual_runtime += 2;
-            break;
-        }
-
-        /* 由于目前只有BSP处理器会收到HPET中断,因此这里只会标记BSP处理器的进程需要调度
-        if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
-        {
-            current_pcb->flags |= PROC_NEED_SCHED;
-        }
-        */
+        sched_update_jiffies();
 
         break;
 

+ 24 - 0
kernel/sched/sched.c

@@ -101,6 +101,30 @@ void sched_cfs()
     }
 }
 
+/**
+ * @brief 当时钟中断到达时,更新时间片
+ *
+ */
+void sched_update_jiffies()
+{
+    switch (current_pcb->priority)
+    {
+    case 0:
+    case 1:
+        --sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies;
+        ++current_pcb->virtual_runtime;
+        break;
+    case 2:
+    default:
+        sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies -= 2;
+        current_pcb->virtual_runtime += 2;
+        break;
+    }
+    // 时间片耗尽,标记可调度
+    if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
+        current_pcb->flags |= PROC_NEED_SCHED;
+}
+
 /**
  * @brief 初始化进程调度器
  *

+ 7 - 1
kernel/sched/sched.h

@@ -39,4 +39,10 @@ struct process_control_block *sched_cfs_dequeue();
  * @brief 初始化进程调度器
  * 
  */
-void sched_init();
+void sched_init();
+
+/**
+ * @brief 当时钟中断到达时,更新时间片
+ * 
+ */
+void sched_update_jiffies();

+ 16 - 2
kernel/smp/ipi.h

@@ -6,10 +6,9 @@
 #error "error type of arch!"
 #endif
 
-
 /**
  * @brief 发送ipi消息
- * 
+ *
  * @param dest_mode 目标模式
  * @param deliver_status 投递模式
  * @param level 信号驱动电平
@@ -22,3 +21,18 @@
  */
 extern void ipi_send_IPI(uint32_t dest_mode, uint32_t deliver_status, uint32_t level, uint32_t trigger,
                          uint32_t vector, uint32_t deliver_mode, uint32_t dest_shorthand, bool apic_type, uint32_t destination);
+
+/**
+ * @brief ipi中断处理注册函数
+ * 
+ * @param irq_num 中断向量号
+ * @param arg 参数
+ * @param handler 处理函数
+ * @param param 参数
+ * @param controller 当前为NULL 
+ * @param irq_name ipi中断名
+ * @return int 成功:0
+ */
+extern int ipi_regiserIPI(uint64_t irq_num, void *arg,
+                      void (*handler)(uint64_t irq_num, uint64_t param, struct pt_regs *regs),
+                      uint64_t param, hardware_intr_controller *controller, char *irq_name);

+ 18 - 5
kernel/smp/smp.c

@@ -7,8 +7,12 @@
 #include "../process/process.h"
 #include "../process/spinlock.h"
 
+#include <sched/sched.h>
+
 #include "ipi.h"
 
+void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); // 由BSP转发的HPET中断处理函数
+
 static spinlock_t multi_core_starting_lock; // 多核启动锁
 
 static struct acpi_Processor_Local_APIC_Structure_t *proc_local_apic_structs[MAX_SUPPORTED_PROCESSOR_NUM];
@@ -37,6 +41,9 @@ void smp_init()
         set_intr_gate(i, 0, SMP_interrupt_table[i - 200]);
 
     memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
+    
+    // 注册接收bsp处理器的hpet中断转发的处理函数
+    ipi_regiserIPI(0xc8, NULL, &ipi_0xc8_handler, NULL, NULL, "IPI 0xc8");
 
     ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
 
@@ -130,13 +137,13 @@ void smp_ap_start()
     current_pcb->priority = 2;
     current_pcb->virtual_runtime = 0;
 
-    current_pcb->thread = (struct thread_struct *)(current_pcb + 1);    // 将线程结构体放置在pcb后方
+    current_pcb->thread = (struct thread_struct *)(current_pcb + 1); // 将线程结构体放置在pcb后方
     current_pcb->thread->rbp = _stack_start;
     current_pcb->thread->rsp = _stack_start;
     current_pcb->thread->fs = KERNEL_DS;
     current_pcb->thread->gs = KERNEL_DS;
     current_pcb->cpu_id = current_starting_cpu;
-    
+
     initial_proc[proc_current_cpu_id] = current_pcb;
 
     load_TR(10 + current_starting_cpu * 2);
@@ -145,13 +152,19 @@ void smp_ap_start()
     spin_unlock(&multi_core_starting_lock);
     current_pcb->preempt_count = 0;
     sti();
-    
-    if(proc_current_cpu_id == 1)
+
+    if (proc_current_cpu_id == 1)
         process_init();
-    while(1)
+    while (1)
     {
         printk_color(BLACK, WHITE, "CPU:%d IDLE process.\n", proc_current_cpu_id);
     }
     while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt
         hlt();
+}
+
+// 由BSP转发的HPET中断处理函数
+void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs)
+{
+    sched_update_jiffies();
 }