Fix a stack leak. Prevent the kernel stack from overflowing.
authorcoderain <coderain@sdf.org>
Tue, 25 Sep 2018 13:48:53 +0000 (15:48 +0200)
committercoderain <coderain@sdf.org>
Tue, 25 Sep 2018 13:48:53 +0000 (15:48 +0200)
kernel/include/thread.h
kernel/src/interrupt.c
kernel/src/thread.c

index 748019990ea58b9ef532dcb42c24f2108c7034dc..dddc2eaa81e13b8d6f6e4b92cdbd5339fbe89714 100644 (file)
@@ -31,7 +31,7 @@
 
 #define QUANTUM 30
 #define MAX_THREADS 2097152
-#define KERNEL_STACK_SIZE 262144
+#define KERNEL_STACK_SIZE 0x40000
 
 #define SAFE_EFLAGS_MASK 0x00000CD5
 
index e7dce9f22182a23968f368f96e85d1b4658c9e97..ebb9469c069760d9b0e62ac1bbd755d49e601375 100644 (file)
@@ -22,6 +22,7 @@
 #include <lock.h>
 #include <thread.h>
 #include <cpu.h>
+#include <log.h>
 
 static byte_t isr_stubs[IDT_NUM_INTERRUPTS * ISR_STUB_SIZE];
 static idt_entry_t idt[IDT_NUM_INTERRUPTS];
@@ -30,8 +31,9 @@ static interrupt_handler_t handlers[IDT_NUM_INTERRUPTS];
 static void idt_main_handler(byte_t interrupt_num, registers_t regs)
 {
     regs.esp += 16;
-    if (handlers[interrupt_num].procedure == NULL) return;
+    if (SEGMENT_RPL(regs.cs) != 0) regs.esp += 8;
 
+    if (handlers[interrupt_num].procedure == NULL) return;
     thread_t *thread = get_current_thread();
 
     if (thread)
index a3fa995c888249b554ad5efcfc43eef033856e43..b8300dbfbaf20e3bbcec12a7ca65a7b2cc3a7da5 100644 (file)
@@ -253,6 +253,8 @@ void thread_lazy_fpu(void)
     asm volatile ("clts");
 }
 
+#include <log.h>
+
 void scheduler(registers_t *regs)
 {
     int i;
@@ -284,20 +286,22 @@ found:
         ASSERT(next_thread != NULL);
         list_remove(&next_thread->in_queue_list);
 
+        if (current_thread->tid != 0) ASSERT(current_thread->kernel_esp >= (uintptr_t)current_thread->kernel_stack);
+        if (next_thread->tid != 0) ASSERT(next_thread->kernel_esp >= (uintptr_t)next_thread->kernel_stack);
+
         if (current_thread != next_thread)
         {
             memcpy(&current_thread->state.regs, regs, sizeof(registers_t));
-
             current_thread->kernel_esp = regs->esp;
-            current_thread->state.regs.esp = ((registers_ext_t*)regs)->esp3;
+            if (SEGMENT_RPL(regs->cs) != 0) current_thread->state.regs.esp = ((registers_ext_t*)regs)->esp3;
 
             set_kernel_esp(next_thread->kernel_esp);
 
-            asm volatile ("pushl %eax\n"
+            /*asm volatile ("pushl %eax\n"
                           "movl %cr4, %eax\n"
                           "orb $0x08, %al\n"
                           "movl %eax, %cr4\n"
-                          "popl %eax\n");
+                          "popl %eax\n");*/
 
             if (SEGMENT_RPL(next_thread->state.regs.cs) != 0)
             {