From adbdc45e64c40f7d3a022caedeb22f5e95dcd12a Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 3 Apr 2011 01:19:04 +0800 Subject: [PATCH 1/1] Misc changes - Fixing invalid use of Unit in some places - Cleaning up drivers and build output - Improved Threading dump output - Working on thread CPU state dump (for non-scheduled threads) > TODO: Implement stack backtrace to find the location of the currently active thread. --- Kernel/Makefile.BuildNum.x86_64 | 2 +- Kernel/arch/x86/include/arch.h | 12 ++-- Kernel/arch/x86/proc.asm | 4 +- Kernel/arch/x86/proc.c | 68 ++++++++++++++++++- Kernel/threads.c | 43 ++++++++---- Modules/IPStack/main.c | 4 ++ Modules/Network/NE2000/ne2000.c | 17 +---- Modules/Network/RTL8139/{main.c => rtl8139.c} | 18 ++--- Modules/Storage/ATA/io.c | 2 +- Usermode/Applications/Makefile.tpl | 4 +- 10 files changed, 125 insertions(+), 49 deletions(-) rename Modules/Network/RTL8139/{main.c => rtl8139.c} (93%) diff --git a/Kernel/Makefile.BuildNum.x86_64 b/Kernel/Makefile.BuildNum.x86_64 index 8ac93bcd..2d4893a3 100644 --- a/Kernel/Makefile.BuildNum.x86_64 +++ b/Kernel/Makefile.BuildNum.x86_64 @@ -1 +1 @@ -BUILD_NUM = 235 +BUILD_NUM = 237 diff --git a/Kernel/arch/x86/include/arch.h b/Kernel/arch/x86/include/arch.h index f9813ed5..8d8a14a0 100644 --- a/Kernel/arch/x86/include/arch.h +++ b/Kernel/arch/x86/include/arch.h @@ -82,12 +82,12 @@ typedef Uint64 tPAddr; typedef Uint32 tVAddr; typedef struct { - Uint gs, fs, es, ds; - Uint edi, esi, ebp, kesp; - Uint ebx, edx, ecx, eax; - Uint int_num, err_code; - Uint eip, cs; - Uint eflags, esp, ss; + Uint32 gs, fs, es, ds; + Uint32 edi, esi, ebp, kesp; + Uint32 ebx, edx, ecx, eax; + Uint32 int_num, err_code; + Uint32 eip, cs; + Uint32 eflags, esp, ss; } tRegs; typedef struct { diff --git a/Kernel/arch/x86/proc.asm b/Kernel/arch/x86/proc.asm index a07bdad8..87dcd61b 100644 --- a/Kernel/arch/x86/proc.asm +++ b/Kernel/arch/x86/proc.asm @@ -92,8 +92,10 @@ SchedulerBase: %endif call Proc_Scheduler +[global scheduler_return] +scheduler_return: ; Used by some hackery in Proc_DumpThreadCPUState - add esp, 4 ; Remove Argument + add esp, 4 ; Remove CPU Number (thread is poped later) %if USE_MP test ebx, ebx diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index a204b49c..a85b0ca5 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -49,6 +49,8 @@ extern int giNextTID; extern tThread gThreadZero; extern void Isr8(void); // Double Fault extern void Proc_ReturnToUser(tVAddr Handler, Uint Argument, tVAddr KernelStack); +extern void scheduler_return; // Return address in SchedulerBase +extern void IRQCommon; // Common IRQ handler code // === PROTOTYPES === void ArchThreads_Init(void); @@ -65,6 +67,7 @@ void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char ** void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP); int Proc_Demote(Uint *Err, int Dest, tRegs *Regs); void Proc_CallFaultHandler(tThread *Thread); +void Proc_DumpThreadCPUState(tThread *Thread); void Proc_Scheduler(int CPU); // === GLOBALS === @@ -850,6 +853,69 @@ void Proc_CallFaultHandler(tThread *Thread) for(;;); } +void Proc_DumpThreadCPUState(tThread *Thread) +{ + Uint32 *stack = (void *)Thread->SavedState.EBP; // EBP = ESP after call and PUSH + + if( Thread->CurCPU > -1 ) + { + Log(" Currently running"); + return ; + } + + #if 1 + tVAddr diffFromScheduler = Thread->SavedState.EIP - (tVAddr)Proc_Scheduler; + tVAddr diffFromClone = Thread->SavedState.EIP - (tVAddr)Proc_Clone; + tVAddr diffFromSpawn = Thread->SavedState.EIP - (tVAddr)Proc_SpawnWorker; + + if( diffFromClone > 0 && diffFromClone < 512 ) // When I last checked, GetEIP was at .+0x183 + { + // Just spawned full thread + Log(" Creating full thread"); + return ; + } + + if( diffFromSpawn > 0 && diffFromSpawn < 512 ) // When I last checked, GetEIP was at .+0x99 + { + // Just spawned worker thread + Log(" Creating worker thread"); + return ; + } + + if( diffFromScheduler > 0 && diffFromScheduler < 256 ) // When I last checked, GetEIP was at .+0x60 + #else + if( stack[1] == (Uint32)&IRQCommon + 25 ) + { + tRegs *regs = (void *) stack[2]; + Log(" oldebp = 0x%08x, ret = 0x%08x, regs = 0x%x", + stack[0], stack[1], stack[2] + ); + // [EBP] = old EBP + // [EBP+0x04] = Return Addr + // [EBP+0x08] = Arg 1 (CPU Number) + // [EBP+0x0C] = Arg 2 (Thread) + // [EBP+0x10] = GS (start of tRegs) + Log(" IRQ%i from %02x:%08x", regs->int_num regs->cs, regs->eip); + } + if( stack[1] == (Uint32)&scheduler_return ) + #endif + { + // Scheduled out + tRegs *regs = (void *) &stack[4]; + Log(" oldebp = 0x%08x, ret = 0x%08x, cpu = %i, thread = 0x%x", + stack[0], stack[1], stack[2], stack[3]); + // [EBP] = old EBP + // [EBP+0x04] = Return Addr + // [EBP+0x08] = Arg 1 (CPU Number) + // [EBP+0x0C] = Arg 2 (Thread) + // [EBP+0x10] = GS (start of tRegs) + Log(" At %02x:%08x", regs->cs, regs->eip); + return ; + } + + Log(" Just created"); +} + /** * \fn void Proc_Scheduler(int CPU) * \brief Swap current thread and clears dead threads @@ -918,7 +984,7 @@ void Proc_Scheduler(int CPU) #endif #if USE_MP // MP Debug - Log("CPU = %i, Thread %p", CPU, thread); +// Log("CPU = %i, Thread %p", CPU, thread); #endif // Update Kernel Stack pointer diff --git a/Kernel/threads.c b/Kernel/threads.c index d24af082..b4838308 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -33,6 +33,7 @@ const enum eConfigTypes cCONFIG_TYPES[] = { // === IMPORTS === extern void ArchThreads_Init(void); extern void Proc_CallFaultHandler(tThread *Thread); +extern void Proc_DumpThreadCPUState(tThread *Thread); extern int GetCPUNum(void); // === PROTOTYPES === @@ -788,7 +789,6 @@ void Threads_AddActive(tThread *Thread) // Set state Thread->Status = THREAD_STAT_ACTIVE; - Thread->CurCPU = -1; // Add to active list #if SCHEDULER_TYPE == SCHED_RR_PRI Thread->Next = gaActiveThreads[Thread->Priority]; @@ -802,11 +802,22 @@ void Threads_AddActive(tThread *Thread) giNumActiveThreads ++; #if SCHEDULER_TYPE == SCHED_LOTTERY - giFreeTickets += caiTICKET_COUNTS[ Thread->Priority ]; - # if DEBUG_TRACE_TICKETS - Log("Threads_AddActive: CPU%i %p %i (%s) added, new giFreeTickets = %i", - GetCPUNum(), Thread, Thread->TID, Thread->ThreadName, giFreeTickets); - # endif + { + int delta; + // Only change the ticket count if the thread is un-scheduled + if(Thread->CurCPU != -1) + delta = 0; + else + delta = caiTICKET_COUNTS[ Thread->Priority ]; + + giFreeTickets += delta; + # if DEBUG_TRACE_TICKETS + Log("CPU%i %p (%i %s) added, new giFreeTickets = %i [+%i]", + GetCPUNum(), Thread, Thread->TID, Thread->ThreadName, + giFreeTickets, delta + ); + # endif + } #endif SHORTREL( &glThreadListLock ); @@ -836,13 +847,12 @@ tThread *Threads_RemActive(void) ret->Next = NULL; ret->Remaining = 0; - ret->CurCPU = -1; giNumActiveThreads --; // no need to decrement tickets, scheduler did it for us #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS - Log("Threads_RemActive: CPU%i %p %i (%s) removed, giFreeTickets = %i", + Log("CPU%i %p (%i %s) removed, giFreeTickets = %i [nc]", GetCPUNum(), ret, ret->TID, ret->ThreadName, giFreeTickets); #endif @@ -977,6 +987,7 @@ void Threads_DumpActive(void) Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)", thread->Status, THREAD_STAT_ACTIVE); Log(" Priority %i, Quantum %i", thread->Priority, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); + Proc_DumpThreadCPUState(thread); } #if SCHEDULER_TYPE == SCHED_RR_PRI @@ -1098,14 +1109,15 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) #if SCHEDULER_TYPE == SCHED_LOTTERY giFreeTickets += caiTICKET_COUNTS[ Last->Priority ]; # if DEBUG_TRACE_TICKETS - LogF(" CPU %i released %p (%i %s) into the pool (%i tickets in pool)\n", - CPU, Last, Last->TID, Last->ThreadName, giFreeTickets); + LogF("Log: CPU%i released %p (%i %s) into the pool (%i [+%i] tickets in pool)\n", + CPU, Last, Last->TID, Last->ThreadName, giFreeTickets, + caiTICKET_COUNTS[ Last->Priority ]); # endif #endif } #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS else - LogF(" CPU %i released %p (%i %s)->Status = %i (Released)\n", + LogF("Log: CPU%i released %p (%i %s)->Status = %i (Released)\n", CPU, Last, Last->TID, Last->ThreadName, Last->Status); #endif Last->CurCPU = -1; @@ -1164,12 +1176,13 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)", giFreeTickets, number); } - # if DEBUG_TRACE_TICKETS - LogF(" CPU%i giFreeTickets = %i, running %p (%i %s CPU=%i)\n", - CPU, giFreeTickets, thread, thread->TID, thread->ThreadName, thread->CurCPU); - # endif giFreeTickets -= caiTICKET_COUNTS[ thread->Priority ]; + # if DEBUG_TRACE_TICKETS + LogF("Log: CPU%i allocated %p (%i %s), (%i [-%i] tickets in pool), \n", + CPU, thread, thread->TID, thread->ThreadName, + giFreeTickets, caiTICKET_COUNTS[ thread->Priority ]); + # endif } // --- diff --git a/Modules/IPStack/main.c b/Modules/IPStack/main.c index 2b04a6a5..63499952 100644 --- a/Modules/IPStack/main.c +++ b/Modules/IPStack/main.c @@ -112,6 +112,10 @@ int IPStack_Install(char **Arguments) UnHex(addrData, size, addr); tInterface *iface = IPStack_AddInterface(dev, ""); + if( !iface ) { + Log_Warning("IPStack", "Unable to add interface on '%s'", dev); + continue ; + } iface->Type = iType; memcpy(iface->Address, addrData, size); iface->SubnetBits = iBits; diff --git a/Modules/Network/NE2000/ne2000.c b/Modules/Network/NE2000/ne2000.c index 387e539b..3924ba82 100644 --- a/Modules/Network/NE2000/ne2000.c +++ b/Modules/Network/NE2000/ne2000.c @@ -75,9 +75,7 @@ typedef struct sNe2k_Card { int NextRXPage; //!< Next expected RX page int NextMemPage; //!< Next Card Memory page to use - - //Uint8 Buffer[RX_BUF_SIZE*256]; - + char Name[2]; // "0" tVFS_Node Node; //!< VFS Node Uint8 MacAddr[6]; //!< Cached MAC address @@ -169,13 +167,13 @@ int Ne2k_Install(char **Options) outb( base + ISR, 0xFF ); outb( base + RCR, 0x20 ); // Reciever to Monitor outb( base + TCR, 0x02 ); // Transmitter OFF (TCR.LB = 1, Internal Loopback) + + // Read MAC Address outb( base + RBCR0, 6*4 ); // Remote Byte Count outb( base + RBCR1, 0 ); outb( base + RSAR0, 0 ); // Clear Source Address outb( base + RSAR1, 0 ); outb( base + CMD, 0x0A ); // Remote Read, Start - - // Read MAC Address gpNe2k_Cards[ k ].MacAddr[0] = inb(base+0x10);// inb(base+0x10); gpNe2k_Cards[ k ].MacAddr[1] = inb(base+0x10);// inb(base+0x10); gpNe2k_Cards[ k ].MacAddr[2] = inb(base+0x10);// inb(base+0x10); @@ -192,15 +190,6 @@ int Ne2k_Install(char **Options) outb( base+RCR, 0x0F ); // Set WRAP and allow all packet matches outb( base+TCR, 0x00 ); // Set Normal Transmitter mode outb( base+TPSR, 0x40); // Set Transmit Start - // Set MAC Address - /* - Ne2k_WriteReg(base, MAC0, gpNe2k_Cards[ k ].MacAddr[0]); - Ne2k_WriteReg(base, MAC1, gpNe2k_Cards[ k ].MacAddr[1]); - Ne2k_WriteReg(base, MAC2, gpNe2k_Cards[ k ].MacAddr[2]); - Ne2k_WriteReg(base, MAC3, gpNe2k_Cards[ k ].MacAddr[3]); - Ne2k_WriteReg(base, MAC4, gpNe2k_Cards[ k ].MacAddr[4]); - Ne2k_WriteReg(base, MAC5, gpNe2k_Cards[ k ].MacAddr[5]); - */ Log_Log("Ne2k", "Card %i 0x%04x IRQ%i %02x:%02x:%02x:%02x:%02x:%02x", k, base, gpNe2k_Cards[ k ].IRQ, diff --git a/Modules/Network/RTL8139/main.c b/Modules/Network/RTL8139/rtl8139.c similarity index 93% rename from Modules/Network/RTL8139/main.c rename to Modules/Network/RTL8139/rtl8139.c index 0031925f..254ad110 100644 --- a/Modules/Network/RTL8139/main.c +++ b/Modules/Network/RTL8139/rtl8139.c @@ -1,8 +1,6 @@ /* * Acess2 RTL8139 Driver * - By John Hodge (thePowersGang) - * - * main.c - Driver Core */ #define DEBUG 0 #define VERSION ((0<<8)|50) @@ -101,22 +99,26 @@ int RTL8139_Install(char **Options) // Power on outb( base + CONFIG1, 0x00 ); + // Reset (0x10 to CMD) - outb( base + CMD, 0x10 ); - + outb( base + CMD, 0x10 ); while( inb(base + CMD) & 0x10 ) ; - // Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500) - gpRTL8139_Cards[i].ReceiveBuffer = MM_AllocDMA( 3, 32, &gpRTL8139_Cards[i].PhysReceiveBuffer ); // Set up recieve buffer + // - Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500) + gpRTL8139_Cards[i].ReceiveBuffer = MM_AllocDMA( 3, 32, &gpRTL8139_Cards[i].PhysReceiveBuffer ); outl(base + RBSTART, (Uint32)gpRTL8139_Cards[i].PhysReceiveBuffer); // Set IMR to Transmit OK and Receive OK outw(base + IMR, 0x5); // Set recieve buffer size and recieve mask + // - Bit 7 being unset tells the card to overflow the recieve buffer if needed + // (i.e. when the packet starts at the end of the bufffer, it overflows up + // to 1500 bytes) outl(base + RCR, 0x0F); - - outb(base + CMD, 0x0C); // Recive Enable and Transmit Enable + + // Recive Enable and Transmit Enable + outb(base + CMD, 0x0C); // Get the card's MAC address gpRTL8139_Cards[ i ].MacAddr[0] = inb(base+MAC0); diff --git a/Modules/Storage/ATA/io.c b/Modules/Storage/ATA/io.c index c8f4fa97..6ed04096 100644 --- a/Modules/Storage/ATA/io.c +++ b/Modules/Storage/ATA/io.c @@ -4,7 +4,7 @@ * * Disk Input/Output control */ -#define DEBUG 1 +#define DEBUG 0 #include #include // Needed for error codes #include diff --git a/Usermode/Applications/Makefile.tpl b/Usermode/Applications/Makefile.tpl index 9d479314..424d865d 100644 --- a/Usermode/Applications/Makefile.tpl +++ b/Usermode/Applications/Makefile.tpl @@ -26,7 +26,7 @@ install: $(_BIN) $(_BIN): $(OBJ) @mkdir -p $(dir $(_BIN)) - @echo --- $(LD) -o $@ + @echo [LD] -o $@ ifneq ($(_DBGMAKEFILE),) $(LD) -g $(LDFLAGS) -o $@ $(OBJ) -Map Map.txt else @@ -35,7 +35,7 @@ endif @$(DISASM) $(_BIN) > $(BIN).dsm $(OBJ): $(_OBJPREFIX)%.o: %.c - @echo --- GCC -o $@ + @echo [CC] -o $@ ifneq ($(_OBJPREFIX),) @mkdir -p $(_OBJPREFIX) endif -- 2.20.1