6 کامیت‌ها 46c61c61ea ... fa23adff9e

نویسنده SHA1 پیام تاریخ
  coderain fa23adff9e Implement events using semaphores. 6 سال پیش
  coderain 93a356d7da Fix pin_memory. 6 سال پیش
  coderain 260aea5e3f Fix a stack leak. Prevent the kernel stack from overflowing. 6 سال پیش
  coderain c25156ad46 Make sizeof(long) the default heap alignment. 6 سال پیش
  coderain fce977c0d1 Get rid of exception_return 6 سال پیش
  coderain ae4db17b61 Fix a bug in common.mk. 6 سال پیش
10فایلهای تغییر یافته به همراه28 افزوده شده و 35 حذف شده
  1. 2 2
      common.mk
  2. 5 3
      crt/src/malloc.c
  3. 1 1
      kernel/include/thread.h
  4. 1 2
      kernel/src/exception.c
  5. 3 3
      kernel/src/heap.c
  6. 1 17
      kernel/src/helpers.asm
  7. 3 1
      kernel/src/interrupt.c
  8. 4 2
      kernel/src/memory/memory.c
  9. 8 4
      kernel/src/thread.c
  10. 0 0
      sdk/semaphore.h

+ 2 - 2
common.mk

@@ -38,7 +38,7 @@ ASM  := nasm
 ifneq ($(ARCH), host)
 	CC   := $(ARCH)-gcc
 	LINK := $(ARCH)-ld
-	LDFLAGS += -eprocess_startup
+	LDFLAGS_PROGRAM += -eprocess_startup
 else
 	CC   := gcc
 	LINK := gcc
@@ -90,7 +90,7 @@ $(OUTPUT_KERNEL): $(OBJECTS) $(ADDITIONAL_OBJECTS)
 	$(LINK) -o $@ $(OBJECTS) $(LDFLAGS)
 
 $(OUTPUT_PROGRAM): $(OBJECTS)
-	$(LINK) -o $@ $(OBJECTS) $(LDFLAGS)
+	$(LINK) -o $@ $(OBJECTS) $(LDFLAGS) $(LDFLAGS_PROGRAM)
 
 $(OUTPUT_STATIC_LIB): $(OBJECTS)
 	$(AR) rcs $@ $^

+ 5 - 3
crt/src/malloc.c

@@ -293,6 +293,8 @@ void *__crt_heap_realloc(__crt_heap_t *heap, void *ptr, size_t alignment, size_t
 
         heap_header_t *new_block = (heap_header_t*)(aligned_start - sizeof(heap_header_t));
         memmove(new_block, hole, sizeof(heap_header_t));
+        if (heap->next_offset == (uintptr_t)hole - (uintptr_t)heap->base) heap->next_offset += padding;
+
         hole = new_block;
         hole->size -= padding;
     }
@@ -332,17 +334,17 @@ void *aligned_alloc(size_t alignment, size_t size)
 
 void *realloc(void *ptr, size_t size)
 {
-    return __crt_heap_realloc(__crt_default_heap, ptr, 1, size);
+    return __crt_heap_realloc(__crt_default_heap, ptr, sizeof(long), size);
 }
 
 void *malloc(size_t size)
 {
-    return __crt_heap_realloc(__crt_default_heap, NULL, 1, size);
+    return __crt_heap_realloc(__crt_default_heap, NULL, sizeof(long), size);
 }
 
 void free(void *ptr)
 {
-    __crt_heap_realloc(__crt_default_heap, ptr, 1, 0);
+    __crt_heap_realloc(__crt_default_heap, ptr, sizeof(long), 0);
 }
 
 void *calloc(size_t nmemb, size_t size)

+ 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
 

+ 1 - 2
kernel/src/exception.c

@@ -75,8 +75,7 @@ static void raise_exception_internal(thread_t *thread, processor_mode_t mode, ex
         if (thread->kernel_handler.eip)
         {
             thread->kernel_handler.eax = 1;
-            thread->in_kernel--;
-            exception_return(thread->kernel_handler);
+            memcpy(exception_regs, &thread->kernel_handler, sizeof(registers_t));
         }
         else
         {

+ 3 - 3
kernel/src/heap.c

@@ -45,17 +45,17 @@ static void heap_problem(int problem)
 
 void *heap_realloc(heap_t *heap, void *ptr, uintptr_t size)
 {
-    return __crt_heap_realloc(&heap->crt, ptr, 1, size);
+    return __crt_heap_realloc(&heap->crt, ptr, sizeof(long), size);
 }
 
 void *heap_alloc(heap_t *heap, uintptr_t size)
 {
-    return __crt_heap_realloc(&heap->crt, NULL, 1, size);
+    return __crt_heap_realloc(&heap->crt, NULL, sizeof(long), size);
 }
 
 void heap_free(heap_t *heap, void *ptr)
 {
-    __crt_heap_realloc(&heap->crt, ptr, 1, 0);
+    __crt_heap_realloc(&heap->crt, ptr, sizeof(long), 0);
 }
 
 dword_t heap_create(heap_t *heap, uintptr_t start, uintptr_t end, dword_t flags, dword_t magic)

+ 1 - 17
kernel/src/helpers.asm

@@ -23,7 +23,6 @@ bits 32
 
 global init_cpu_gdt
 global vm86_start
-global exception_return
 global save_kernel_handler
 global syscall_function
 global reschedule
@@ -86,21 +85,6 @@ vm86_start:             pushfd
                         iret
 
 ;********************************************************************************
-; void exception_return(register_t regs)
-;********************************************************************************
-exception_return:       lea esi, [esp + 0x04]
-                        mov edi, dword [esp + 0x14]
-                        sub edi, 13 * 4
-                        mov esp, edi
-                        mov ecx, 13
-                        cld
-                        rep movsd
-                        add esp, 4
-                        popad
-                        add esp, 4
-                        iret
-
-;********************************************************************************
 ; void save_kernel_handler(exception_handler_t *old_handler)
 ;********************************************************************************
 save_kernel_handler:    pushfd
@@ -151,7 +135,7 @@ reschedule:             push cs
                         cli
                         pop eax
                         mov dword [esp + 44], eax
-                        add dword [esp + 12], 16
+                        add dword [esp + 12], 20
                         push ds
                         push esp
                         call scheduler

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

+ 4 - 2
kernel/src/memory/memory.c

@@ -956,7 +956,8 @@ dword_t pin_memory(const void *virtual, void **pinned, uintptr_t size, bool_t lo
     uintptr_t i;
     uintptr_t virt_addr = PAGE_ALIGN((uintptr_t)virtual);
     void *address = (void*)PAGE_ALIGN((uintptr_t)*pinned);
-    size = PAGE_ALIGN_UP(size) >> 12;
+    size = 1 + ((((uintptr_t)virtual + size - 1) - virt_addr) >> 12);
+    if (commit_pages((void*)virt_addr, size * PAGE_SIZE) != ERR_SUCCESS) return ERR_BADPTR;
 
     memory_address_space_t *address_space = check_usermode(virtual, 1) ? &get_current_process()->memory_space : &kernel_address_space;
     lock_acquire_shared(&address_space->lock);
@@ -978,6 +979,7 @@ dword_t pin_memory(const void *virtual, void **pinned, uintptr_t size, bool_t lo
     {
         void *virt_page = (void*)(virt_addr + i * PAGE_SIZE);
         void *phys_page = get_physical_address(virt_page);
+        ASSERT(phys_page != INVALID_PAGE);
 
         if (lock_contents)
         {
@@ -987,7 +989,7 @@ dword_t pin_memory(const void *virtual, void **pinned, uintptr_t size, bool_t lo
             set_page_flags(virt_page, get_page_flags(virt_page) & ~PAGE_WRITABLE);
         }
 
-        dword_t ret = map_page(phys_page, (void*)(real_address + i), new_flags);
+        dword_t ret = map_page(phys_page, (void*)(real_address + i * PAGE_SIZE), new_flags);
         ASSERT(ret == ERR_SUCCESS);
         reference_page(phys_page);
     }

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

+ 0 - 0
sdk/semaphore.h


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است