Browse Source

Fix a stack leak. Prevent the kernel stack from overflowing.

coderain 5 years ago
parent
commit
260aea5e3f
3 changed files with 12 additions and 6 deletions
  1. 1 1
      kernel/include/thread.h
  2. 3 1
      kernel/src/interrupt.c
  3. 8 4
      kernel/src/thread.c

+ 1 - 1
kernel/include/thread.h

@@ -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
 

+ 3 - 1
kernel/src/interrupt.c

@@ -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)

+ 8 - 4
kernel/src/thread.c

@@ -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)
             {