From 7077d71d4640a38f86bbc9815f7a5489eb8d9453 Mon Sep 17 00:00:00 2001 From: acevest Date: Sat, 11 May 2024 13:39:53 +0800 Subject: [PATCH] =?utf8?q?=E7=A7=BB=E9=99=A4reenter=E7=9A=84=E7=BD=97?= =?utf8?q?=E8=BE=91,=E7=94=A8=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E5=9C=A8?= =?utf8?q?=E5=BA=95=E5=8D=8A=E5=A4=84=E7=90=86=E7=9A=84=E6=96=B9=E5=BC=8F?= =?utf8?q?=E6=9B=BF=E4=BB=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/irq.h | 2 ++ include/system.h | 4 +--- kernel/clock.c | 5 ++++- kernel/irq.c | 19 ++++++++++++------- kernel/syscall.S | 1 - kernel/system.c | 2 -- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/irq.h b/include/irq.h index bec6002..c72dcdc 100644 --- a/include/irq.h +++ b/include/irq.h @@ -81,4 +81,6 @@ bool irq_disabled(); #define IRQ_CASCADE 0x02 #define IRQ_DISK 0x0E +bool in_bh(); + #endif //_IRQ_H diff --git a/include/system.h b/include/system.h index 9189c40..b4d3f01 100644 --- a/include/system.h +++ b/include/system.h @@ -77,7 +77,7 @@ extern char etext, edata, end; #define disableIRQ() cli() #define enableIRQ() sti() -#define ALIGN(x, a) (((x) + (a)-1) & ~((a)-1)) +#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) // 定义最大显存为 16MB #define VRAM_VADDR_SIZE (16 << 20) @@ -264,8 +264,6 @@ void init_task_entry(); void disk_task_entry(); void user_task_entry(); -extern volatile int reenter; - #define DEFAULT_BOOT_DELAY_TICKS 30 void boot_delay(int ticks); diff --git a/kernel/clock.c b/kernel/clock.c index e78622f..b2601f1 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -42,6 +42,7 @@ void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { // 打断后该时钟中断不应该继续减少该进程的时间片,因为这会造成该进程在后续的调底中少了实际的运行时间 if (1 == current->need_resched) { // 这种情况必然已经发生了该时钟中断打断了下半部处理程序 + // 反之时钟中断打断了下半部处理程序不一定need_resched就为1 return; } @@ -55,7 +56,9 @@ void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { assert(current->ticks >= 0); // 防止ticks被减到0后再减溢出 - add_irq_bh_handler(clk_bh_handler, NULL); + if (!in_bh()) { + add_irq_bh_handler(clk_bh_handler, NULL); + } } // 开中断执行这个函数 diff --git a/kernel/irq.c b/kernel/irq.c index b25f382..1c462e3 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -77,6 +77,12 @@ void dump_irq_nr_stack() { void irq_bh_handler(); void schedule(); +volatile bool in_bh_logic = false; + +volatile uint32_t reenter_count = 0; + +bool in_bh() { return in_bh_logic; } + __attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) { unsigned int irq = regs->irq; if (irq >= NR_IRQS) { @@ -87,7 +93,6 @@ __attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) { irq_action_t *action = p->action; assert(irq_disabled()); - reenter++; // 屏蔽当前中断 p->chip->disable(irq); @@ -100,8 +105,8 @@ __attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) { #if 1 unsigned long esp; asm("movl %%esp, %%eax" : "=a"(esp)); - printl(MPL_CURRENT, "current %08x %-6s cr3 %08x reenter %d esp %08x ticks %u", current, current->name, current->cr3, - reenter, esp, current->ticks); + printl(MPL_CURRENT, "current %08x %-6s cr3 %08x esp %08x reenter %u ticks %u", current, current->name, current->cr3, + esp, reenter_count, current->ticks); #endif while (action && action->handler) { @@ -115,25 +120,25 @@ __attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) { // 代表当前中断程序打断了前一个中断程序的“开中断处理的底半部分逻辑” // 即前一个中断处理尚未完全完成 assert(irq_disabled()); - if (reenter != 0) { - reenter--; + if (in_bh_logic) { + reenter_count++; return; } // --以上逻辑CPU处于中断禁止状态-------------------------- // 此处执行中断函数的下半部分逻辑,开中断执行 { + in_bh_logic = true; enable_irq(); irq_bh_handler(); disable_irq(); + in_bh_logic = false; } // --以下逻辑CPU处于中断禁止状态-------------------------- assert(irq_disabled()); - assert(reenter == 0); - reenter--; // 考察如果不需要调度程序,直接退出 if (current->need_resched == 0) { diff --git a/kernel/syscall.S b/kernel/syscall.S index 3b9b27f..9c8f01b 100644 --- a/kernel/syscall.S +++ b/kernel/syscall.S @@ -26,7 +26,6 @@ .text .global syscall_entry .global ret_from_fork_krnl -.extern reenter //.global syscall_exit syscall_entry: diff --git a/kernel/system.c b/kernel/system.c index 1717762..da11654 100644 --- a/kernel/system.c +++ b/kernel/system.c @@ -36,8 +36,6 @@ char idtr[6] __attribute__((__aligned__(4))); extern char _gdtr[6]; extern char _idtr[6]; -volatile int reenter = -1; - void setup_gdt() { pDesc pdesc; // change to new gdt. -- 2.44.0