Bugfixing the x86_64 port
authorJohn Hodge <[email protected]>
Sun, 1 Aug 2010 01:39:09 +0000 (09:39 +0800)
committerJohn Hodge <[email protected]>
Sun, 1 Aug 2010 01:39:09 +0000 (09:39 +0800)
Kernel/arch/x86_64/Makefile
Kernel/arch/x86_64/desctab.asm
Kernel/arch/x86_64/proc.c
Kernel/arch/x86_64/start64.asm
Kernel/include/threads.h
Kernel/lib.c
Kernel/vfs/fs/root.c
Kernel/vfs/main.c

index f539e25..647a49a 100644 (file)
@@ -5,9 +5,9 @@
 
 MAX_CPUS := 4
 
-CPPFLAGS       := -DMAX_CPUS=$(MAX_CPUS)
+CPPFLAGS       := -DMAX_CPUS=$(MAX_CPUS) -D USE_MP=0
 CFLAGS         := $(KERNEL_CFLAGS) -mno-sse -mno-mmx
-ASFLAGS                := -f elf64 -D MAX_CPUS=$(MAX_CPUS)
+ASFLAGS                := -f elf64 -D MAX_CPUS=$(MAX_CPUS) -D USE_MP=0
 LDFLAGS        := -nostdlib -nodefaultlibs
 
 ifeq ($(ARCH),amd64)
index cf1b0f6..816d14e 100644 (file)
@@ -80,7 +80,7 @@ Desctab_Init:
        %endrep
        
        ; Install IRQs
-       SETIDT  0xF0, Irq0
+       SETIDT  0xF0, SchedulerIRQ
        SETIDT  0xF1, Irq1
        SETIDT  0xF2, Irq2
        SETIDT  0xF3, Irq3
@@ -321,20 +321,43 @@ IrqCommon:
 [extern Proc_Scheduler]
 [global SchedulerIRQ]
 SchedulerIRQ:
-       ; TODO: Find Current CPU
        PUSH_GPR
        ;PUSH_FPU
        ;PUSH_XMM
        
-       xor rsi, rsi
-       mov rdi, MM_LOCALAPIC+0x20
-       mov esi, [rdi]
+       ; Save Thread Pointer
+       mov rax, dr0
+       push rax
+       
+       ; Get the CPU Number
+       mov rdi, dr1
+       ; Call the Scheduler
        call Proc_Scheduler
        
+       ; Restore Thread Pointer
+       pop rax
+       mov dr0, rax
+       
+       ; Send EOI (To either the APIC or the PIC)
+       %if USE_MP
+       test ebx, ebx
+       jnz .sendEOI
+       %endif
+       ; PIC
+       mov al, 0x20
+       out 0x20, al            ; ACK IRQ
+       %if USE_MP
+       jmp .ret
+       ; APIC
+.sendEOI:
+       mov eax, DWORD [gpMP_LocalAPIC]
+       mov DWORD [eax+0x0B0], 0
+       %endif
+.ret:
+       
        ;POP_XMM
        ;POP_FPU
        POP_GPR
-       add rsp, 8*2
        iretq
 
 [section .data]
index 8e7d24c..81b8eeb 100644 (file)
@@ -289,9 +289,14 @@ void ArchThreads_Init(void)
        }
        #endif
        
+       // Set Debug registers
+       __asm__ __volatile__ ("mov %0, %%db0" : : "r"(&gThreadZero));
+       __asm__ __volatile__ ("mov %%rax, %%db1" : : "a"(0));
+       
        gaCPUs[0].Current = &gThreadZero;
        
        gThreadZero.MemState.CR3 = (Uint)gInitialPML4 - KERNEL_BASE;
+       gThreadZero.CurCPU = 0;
        
        // Set timer frequency
        outb(0x43, 0x34);       // Set Channel 0, Low/High, Rate Generator
@@ -303,6 +308,8 @@ void ArchThreads_Init(void)
        
        // Change Stacks
        Proc_ChangeStack();
+       
+       Log("Multithreading initialised");
 }
 
 #if USE_MP
@@ -387,11 +394,13 @@ void Proc_Start(void)
        
        // Set current task
        gaCPUs[0].Current = &gThreadZero;
+       gaCPUs[0].Current->CurCPU = 0;
        
        // Start Interrupts (and hence scheduler)
        __asm__ __volatile__("sti");
        #endif
        MM_FinishVirtualInit();
+       Log("Multithreading started");
 }
 
 /**
@@ -726,7 +735,7 @@ void Proc_Scheduler(int CPU)
        
        // If the spinlock is set, let it complete
        if(IS_LOCKED(&glThreadListLock))        return;
-               
+       
        // Get current thread
        thread = gaCPUs[CPU].Current;
        
index 7617d6a..d0cc503 100644 (file)
@@ -25,6 +25,9 @@ start64:
        jmp rax
 .himem:
        
+       xor rax, rax
+       mov dr0, rax    ; Set CPU0
+       
        ; Clear the screen
        mov rax, 0x1F201F201F201F20     ; Set the screen to White on blue, space (4 characters)
        mov edi, 0xB8000
index b7d9bad..4a62166 100644 (file)
@@ -18,13 +18,11 @@ typedef struct sMessage
 typedef struct sThread
 {
        // --- threads.c's
-       //  0
        struct sThread  *Next;  //!< Next thread in list
        tSpinlock       IsLocked;       //!< Thread's spinlock
        volatile int    Status;         //!< Thread Status
         int    RetStatus;      //!< Return Status
        
-       // 16
        Uint    TID;    //!< Thread ID
        Uint    TGID;   //!< Thread Group (Process)
        Uint    PTID;   //!< Parent Thread ID
@@ -32,20 +30,16 @@ typedef struct sThread
        char    *ThreadName;    //!< Name of thread
        
        // --- arch/proc.c's responsibility
-       // 40
        //! Kernel Stack Base
        tVAddr  KernelStack;
        
-       // 44 (x86)
        //! Memory Manager State
        tMemoryState    MemState;
        
-       // 48 (x86)
        //! State on task switch
        tTaskState      SavedState;
        
        // --- threads.c's
-       // 60
         int    CurFaultNum;    //!< Current fault number, 0: none
        tVAddr  FaultHandler;   //!< Fault Handler
        
@@ -57,7 +51,6 @@ typedef struct sThread
        
        Uint    Config[NUM_CFG_ENTRIES];        //!< Per-process configuration
        
-       // --- proc.c's
        volatile int    CurCPU;
 } tThread;
 
index 9923d7c..586a2e8 100644 (file)
@@ -270,14 +270,14 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                {
                case 'd':
                case 'i':
-                       #if BITS == 32
-                       if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
-                       #else
-                       if( (Sint)val < 0 ) {
-                       #endif
+                       if( isLongLong && val >> 63 ) {
                                PUTCH('-');
                                val = -val;
                        }
+                       else if( !isLongLong && val >> 31 ) {
+                               PUTCH('-');
+                               val = -(Sint32)val;
+                       }
                        itoa(p, val, 10, minSize, pad);
                        goto printString;
                case 'u':
index c52313d..c613cfd 100644 (file)
@@ -20,11 +20,8 @@ tRamFS_File  *Root_int_AllocFile(void);
 
 // === GLOBALS ===
 tVFS_Driver    gRootFS_Info = {
-       "rootfs", 0,
-       Root_InitDevice,
-       NULL,   // Unmount
-       NULL
-};
+       "rootfs", 0, Root_InitDevice, NULL, NULL
+       };
 tRamFS_File    RootFS_Files[MAX_FILES];
 tVFS_ACL       RootFS_DirACLs[3] = {
        {{0,0}, {0,VFS_PERM_ALL}},      // Owner (Root)
index eee0e10..b447f04 100644 (file)
@@ -104,6 +104,7 @@ tVFS_Driver *VFS_GetFSByName(char *Name)
        
        for(;drv;drv=drv->Next)
        {
+               Log("strcmp('%s' (%p), '%s') == 0?", drv->Name, drv->Name, Name);
                if(strcmp(drv->Name, Name) == 0)
                        return drv;
        }

UCC git Repository :: git.ucc.asn.au