6 Commits fad7052d1e ... 49d75449f0

Author SHA1 Message Date
  coderain 49d75449f0 Pass the machine state from the interrupt for kernel crashes. 6 years ago
  coderain c22193bc7c Fix a minor bug in __crt_vstrprintf. 6 years ago
  coderain c7aa8cb1d6 The assembler needs to know the instruction size. 6 years ago
  coderain bb838ae01d Merge branch 'unit_tests' 6 years ago
  coderain ab00d5b6f4 Update the node balance when deleting a duplicate in the AVL tree. 6 years ago
  coderain 045be778f3 Begin implementing unit tests. 6 years ago
10 changed files with 39 additions and 184 deletions
  1. 1 1
      .gitignore
  2. 14 4
      common.mk
  3. 2 2
      crt/src/printf.c
  4. 9 9
      kernel/include/cpu.h
  5. 4 4
      kernel/src/exception.c
  6. 1 0
      sdk/avltree.h
  7. 4 7
      tests/.gitignore
  8. 4 22
      tests/Makefile
  9. 0 135
      tests/hello.asm
  10. 0 0
      tests/unit_tests/Makefile

+ 1 - 1
.gitignore

@@ -20,7 +20,7 @@
 !library
 !library/*
 !tests
-!tests/*
+!tests/**
 !manager
 !manager/*
 !drivers

+ 14 - 4
common.mk

@@ -17,7 +17,9 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-ARCH := i686-elf
+ifndef ARCH
+	ARCH := i686-elf
+endif
 
 # Directories
 SRCDIR       := src
@@ -30,9 +32,17 @@ PROJECT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
 export PATH  := $(PROJECT_ROOT)/$(TOOLSDIR)/$(TOOLSROOTDIR)/bin:$(PATH)
 
 # Compilers and tools
-CC   := $(ARCH)-gcc
+
 ASM  := nasm
-LINK := $(ARCH)-ld
+
+ifneq ($(ARCH), host)
+	CC   := $(ARCH)-gcc
+	LINK := $(ARCH)-ld
+	LDFLAGS += -eprocess_startup
+else
+	CC   := gcc
+	LINK := gcc
+endif
 
 ifeq ($(DEBUG), yes)
     CFLAGS += -g -DDEBUG
@@ -80,7 +90,7 @@ $(OUTPUT_KERNEL): $(OBJECTS) $(ADDITIONAL_OBJECTS)
 	$(LINK) -o $@ $(OBJECTS) $(LDFLAGS)
 
 $(OUTPUT_PROGRAM): $(OBJECTS)
-	$(LINK) -eprocess_startup -o $@ $(OBJECTS) $(LDFLAGS)
+	$(LINK) -o $@ $(OBJECTS) $(LDFLAGS)
 
 $(OUTPUT_STATIC_LIB): $(OBJECTS)
 	$(AR) rcs $@ $^

+ 2 - 2
crt/src/printf.c

@@ -131,11 +131,11 @@ static inline int __crt_vstrprintf(__crt_stream_or_string_t *str, const char *fo
             }
             else if (*ptr == 'l')
             {
-                variable_size--;
+                variable_size++;
 
                 if (*++ptr == 'l')
                 {
-                    variable_size--;
+                    variable_size++;
                     ptr++;
                 }
             }

+ 9 - 9
kernel/include/cpu.h

@@ -66,7 +66,7 @@ enum
 
 typedef word_t port_t;
 
-#define IO_PORT_FUNCTIONS(type)                                         \
+#define IO_PORT_FUNCTIONS(type, prefix)                                 \
     static inline type##_t cpu_read_port_##type(port_t port)            \
     {                                                                   \
         type##_t value;                                                 \
@@ -82,7 +82,7 @@ typedef word_t port_t;
     static inline void cpu_read_port_buffer_##type(port_t port, type##_t *buffer, size_t size) \
     {                                                                   \
         __asm__ volatile("cld\n"                                        \
-                         "rep; ins\n"                                   \
+                         "rep; ins" prefix "\n"                         \
                          :"+D"(buffer), "+c"(size)                      \
                          : "d"(port)                                    \
                          : "cc");                                       \
@@ -91,15 +91,15 @@ typedef word_t port_t;
     static inline void cpu_write_port_buffer_##type(port_t port, const type##_t *buffer, size_t size) \
     {                                                                   \
         __asm__ volatile("cld\n"                                        \
-                       "rep; outs\n"                                    \
-                       : "+S"(buffer), "+c"(size)                       \
-                       : "d"(port)                                      \
-                       : "cc");                                         \
+                         "rep; outs " prefix "\n"                       \
+                         : "+S"(buffer), "+c"(size)                     \
+                         : "d"(port)                                    \
+                         : "cc");                                       \
     }
 
-IO_PORT_FUNCTIONS(byte)
-IO_PORT_FUNCTIONS(word)
-IO_PORT_FUNCTIONS(dword)
+IO_PORT_FUNCTIONS(byte, "b")
+IO_PORT_FUNCTIONS(word, "w")
+IO_PORT_FUNCTIONS(dword, "l")
 
 static inline uintptr_t cpu_read_master_control_register(void)
 {

+ 4 - 4
kernel/src/exception.c

@@ -34,7 +34,7 @@ static const char *exception_names[] = {
     "Memory Access Fault",
 };
 
-static void raise_exception_internal(thread_t *thread, processor_mode_t mode, exception_info_t *info)
+static void raise_exception_internal(thread_t *thread, processor_mode_t mode, exception_info_t *info, registers_t *exception_regs)
 {
     if (mode == USER_MODE)
     {
@@ -76,7 +76,7 @@ static void raise_exception_internal(thread_t *thread, processor_mode_t mode, ex
         }
         else
         {
-            KERNEL_CRASH_WITH_REGS(exception_names[info->number], thread->last_context);
+            KERNEL_CRASH_WITH_REGS(exception_names[info->number], exception_regs);
         }
     }
 }
@@ -157,7 +157,7 @@ static void exception_handler(registers_t *regs, byte_t int_num)
 
     thread_t *thread = get_current_thread();
     if (thread == NULL) KERNEL_CRASH_WITH_REGS(exception_names[info.number], regs);
-    raise_exception_internal(thread, previous_mode, &info);
+    raise_exception_internal(thread, previous_mode, &info, regs);
 }
 
 sysret_t syscall_raise_exception(handle_t thread_handle, const exception_info_t *info)
@@ -189,7 +189,7 @@ sysret_t syscall_raise_exception(handle_t thread_handle, const exception_info_t
         if (!reference_by_handle(thread_handle, OBJECT_THREAD, (object_t**)&thread)) return ERR_INVALID;
     }
 
-    raise_exception_internal(thread, USER_MODE, &safe_info);
+    raise_exception_internal(thread, USER_MODE, &safe_info, NULL);
 
     dereference(&thread->header);
     return ERR_SUCCESS;

+ 1 - 0
sdk/avltree.h

@@ -276,6 +276,7 @@ static void avl_tree_remove(avl_tree_t *tree, avl_node_t *node)
         node->next_equal->left = node->left;
         node->next_equal->right = node->right;
         node->next_equal->prev_equal = NULL;
+        node->next_equal->balance = node->balance;
 
         if (node->parent)
         {

+ 4 - 7
tests/.gitignore

@@ -1,7 +1,4 @@
-*
-
-# Include the following files:
-!.gitignore
-!Makefile
-!mkfat.sh
-!*.asm
+# Ignore output files
+unit_tests/obj
+unit_tests/dep
+unit_tests/run

+ 4 - 22
tests/Makefile

@@ -1,7 +1,7 @@
 #
 # Makefile
 #
-# Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
+# Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -17,26 +17,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-# Compilers and tools
-ASM = nasm
-
-# Flags
-ASMFLAGS = -faout
-
-# Input and output files
-SOURCES = $(wildcard *.asm)
-OBJECTS = $(patsubst %.asm, %.prg, $(SOURCES))
-
-.PHONY: all clean
-
-all: floppy.img
-
-%.prg: %.asm Makefile
-	$(ASM) $(ASMFLAGS) -o $@ $<
-
-floppy.img: $(OBJECTS) mkfat.sh Makefile
-	./mkfat.sh 2>/dev/null
+all:
+	make -C unit_tests all
 
 clean:
-	find $(OBJDIR) -name \*.prg -delete
-	rm floppy.img
+	make -C unit_tests clean

+ 0 - 135
tests/hello.asm

@@ -1,135 +0,0 @@
-;
-; hello.asm
-;
-; Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
-;
-; This program is free software: you can redistribute it and/or modify
-; it under the terms of the GNU Affero General Public License as
-; published by the Free Software Foundation, either version 3 of the
-; License, or (at your option) any later version.
-;
-; This program is distributed in the hope that it will be useful,
-; but WITHOUT ANY WARRANTY; without even the implied warranty of
-; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-; GNU Affero General Public License for more details.
-;
-; You should have received a copy of the GNU Affero General Public License
-; along with this program.  If not, see <http://www.gnu.org/licenses/>.
-;
-
-SYSCALL_ALLOC_MEMORY              EQU 0
-SYSCALL_CLOCK_GET_TIME            EQU 1
-SYSCALL_CLOCK_SET_TIME            EQU 2
-SYSCALL_CLOSE_OBJECT              EQU 3
-SYSCALL_COMMIT_MEMORY             EQU 4
-SYSCALL_CREATE_MEMORY_SECTION     EQU 5
-SYSCALL_CREATE_PROCESS            EQU 6
-SYSCALL_CREATE_SEMAPHORE          EQU 7
-SYSCALL_CREATE_THREAD             EQU 8
-SYSCALL_CREATE_USER               EQU 9
-SYSCALL_DELETE_FILE               EQU 10
-SYSCALL_DELETE_USER               EQU 11
-SYSCALL_DEVICE_IOCTL              EQU 12
-SYSCALL_DUPLICATE_HANDLE          EQU 13
-SYSCALL_ENUM_PROCESSES            EQU 14
-SYSCALL_FLUSH_MEMORY_SECTION      EQU 15
-SYSCALL_FREE_MEMORY               EQU 16
-SYSCALL_GET_EXCEPTION_INFO        EQU 17
-SYSCALL_GET_MILLISECONDS          EQU 18
-SYSCALL_GET_NANOSECONDS           EQU 19
-SYSCALL_GET_PROCESS_ID            EQU 20
-SYSCALL_GET_THREAD_ID             EQU 21
-SYSCALL_GET_USER_ID               EQU 22
-SYSCALL_LIST_DIRECTORY            EQU 23
-SYSCALL_LOGON_USER                EQU 24
-SYSCALL_MAP_MEMORY_SECTION        EQU 25
-SYSCALL_MOUNT                     EQU 26
-SYSCALL_OPEN_FILE                 EQU 27
-SYSCALL_OPEN_MEMORY_SECTION       EQU 28
-SYSCALL_OPEN_PIPE                 EQU 29
-SYSCALL_OPEN_PROCESS              EQU 30
-SYSCALL_OPEN_THREAD               EQU 31
-SYSCALL_POWER_CONTROL             EQU 32
-SYSCALL_QUERY_FILE                EQU 33
-SYSCALL_QUERY_HANDLE              EQU 34
-SYSCALL_QUERY_PROCESS             EQU 35
-SYSCALL_QUERY_THREAD              EQU 36
-SYSCALL_QUERY_USER                EQU 37
-SYSCALL_RAISE_EXCEPTION           EQU 38
-SYSCALL_READ_FILE                 EQU 39
-SYSCALL_READ_MEMORY               EQU 40
-SYSCALL_READ_PIPE                 EQU 41
-SYSCALL_RELEASE_SEMAPHORE         EQU 42
-SYSCALL_RESTORE_EXCEPTION_HANDLER EQU 43
-SYSCALL_REVERT_USER               EQU 44
-SYSCALL_SAVE_EXCEPTION_HANDLER    EQU 45
-SYSCALL_SET_MEMORY_FLAGS          EQU 46
-SYSCALL_SET_USER_ID               EQU 47
-SYSCALL_SLEEP                     EQU 48
-SYSCALL_TERMINATE                 EQU 49
-SYSCALL_TERMINATE_THREAD          EQU 50
-SYSCALL_UNCOMMIT_MEMORY           EQU 51
-SYSCALL_UNMOUNT                   EQU 52
-SYSCALL_WAIT_PROCESS              EQU 53
-SYSCALL_WAIT_SEMAPHORE            EQU 54
-SYSCALL_WAIT_THREAD               EQU 55
-SYSCALL_WRITE_FILE                EQU 56
-SYSCALL_WRITE_MEMORY              EQU 57
-SYSCALL_WRITE_PIPE                EQU 58
-SYSCALL_YIELD_QUANTUM             EQU 59
-
-bits 32
-
-section .text
-
-start:                  push 0
-                        push 0
-                        push handle
-                        push name
-                        push SYSCALL_OPEN_FILE
-                        call syscall
-                        add esp, 20
-                        push 4
-                        push address
-                        push 12
-                        push framebuffer
-                        push 0xC9000004
-                        push dword [handle]
-                        push SYSCALL_DEVICE_IOCTL
-                        call syscall
-                        add esp, 28
-                        or eax, eax
-                        jnz .stop
-                        mov edi, dword [address]
-                        lea esi, [edi + 160]
-                        mov ecx, 960
-                        cld
-                        rep movsd
-                        mov eax, 0x07200720
-                        mov ecx, 40
-                        rep stosd
-                        sub edi, 160
-                        mov esi, message
-                        mov ecx, 13
-                        rep movsw
-.stop:                  xor eax, eax
-                        not eax
-                        push eax
-                        push eax
-                        push SYSCALL_SLEEP
-                        call syscall
-                        add esp, 12
-                        jmp .stop
-
-%include "../library/src/syscall.asm"
-
-section .data
-
-handle:                 DD 0
-name:                   DB '@Video0', 0
-framebuffer:            DD 0x10000000
-                        DD 0
-                        DD 80 * 25 * 2
-address:                DD 0
-message:                DB 'H', 7, 'e', 7, 'l', 7, 'l', 7, 'o', 7, ',', 7, ' ',
-                        DB  7, 'w', 7, 'o', 7, 'r', 7, 'l', 7, 'd', 7, '!', 7

+ 0 - 0
tests/unit_tests/Makefile


Some files were not shown because too many files changed in this diff