Misc changes
authorJohn Hodge <[email protected]>
Sat, 2 Apr 2011 17:19:04 +0000 (01:19 +0800)
committerJohn Hodge <[email protected]>
Sat, 2 Apr 2011 17:19:04 +0000 (01:19 +0800)
- 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
Kernel/arch/x86/include/arch.h
Kernel/arch/x86/proc.asm
Kernel/arch/x86/proc.c
Kernel/threads.c
Modules/IPStack/main.c
Modules/Network/NE2000/ne2000.c
Modules/Network/RTL8139/main.c [deleted file]
Modules/Network/RTL8139/rtl8139.c [new file with mode: 0644]
Modules/Storage/ATA/io.c
Usermode/Applications/Makefile.tpl

index f9813ed..8d8a14a 100644 (file)
@@ -82,12 +82,12 @@ typedef Uint64      tPAddr;
 typedef Uint32 tVAddr;
 
 typedef struct {
 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 {
 } tRegs;
 
 typedef struct {
index a07bdad..87dcd61 100644 (file)
@@ -92,8 +92,10 @@ SchedulerBase:
        %endif
        
        call Proc_Scheduler
        %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
 
        %if USE_MP
        test ebx, ebx
index a204b49..a85b0ca 100644 (file)
@@ -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 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);
 
 // === 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_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 ===
 void   Proc_Scheduler(int CPU);
 
 // === GLOBALS ===
@@ -850,6 +853,69 @@ void Proc_CallFaultHandler(tThread *Thread)
        for(;;);
 }
 
        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
 /**
  * \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
        #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
        #endif
        
        // Update Kernel Stack pointer
index d24af08..b483830 100644 (file)
@@ -33,6 +33,7 @@ const enum eConfigTypes       cCONFIG_TYPES[] = {
 // === IMPORTS ===
 extern void    ArchThreads_Init(void);
 extern void    Proc_CallFaultHandler(tThread *Thread);
 // === IMPORTS ===
 extern void    ArchThreads_Init(void);
 extern void    Proc_CallFaultHandler(tThread *Thread);
+extern void    Proc_DumpThreadCPUState(tThread *Thread);
 extern int     GetCPUNum(void);
 
 // === PROTOTYPES ===
 extern int     GetCPUNum(void);
 
 // === PROTOTYPES ===
@@ -788,7 +789,6 @@ void Threads_AddActive(tThread *Thread)
        
        // Set state
        Thread->Status = THREAD_STAT_ACTIVE;
        
        // 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];
        // 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
        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 );
        #endif
        
        SHORTREL( &glThreadListLock );
@@ -836,13 +847,12 @@ tThread *Threads_RemActive(void)
        
        ret->Next = NULL;
        ret->Remaining = 0;
        
        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
        
        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
        
                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);
                                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
                }
        
        #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
                        #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
                        # 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;
                                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);
                }
                        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 ];
                
                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
        }
        
        // ---
        }
        
        // ---
index 2b04a6a..6349995 100644 (file)
@@ -112,6 +112,10 @@ int IPStack_Install(char **Arguments)
                                        UnHex(addrData, size, addr);
                                        
                                        tInterface      *iface = IPStack_AddInterface(dev, "");
                                        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;
                                        iface->Type = iType;
                                        memcpy(iface->Address, addrData, size);
                                        iface->SubnetBits = iBits;
index 387e539..3924ba8 100644 (file)
@@ -75,9 +75,7 @@ typedef struct sNe2k_Card {
         int    NextRXPage;     //!< Next expected RX page
        
         int    NextMemPage;    //!< Next Card Memory page to use
         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
        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)
                        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
                        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);
                        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
                        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,
                        
                        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/main.c
deleted file mode 100644 (file)
index 0031925..0000000
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * Acess2 RTL8139 Driver
- * - By John Hodge (thePowersGang)
- * 
- * main.c - Driver Core
- */
-#define        DEBUG   0
-#define VERSION        ((0<<8)|50)
-#include <acess.h>
-#include <modules.h>
-#include <fs_devfs.h>
-#include <drv_pci.h>
-#include <tpl_drv_network.h>
-
-// === CONSTANTS ===
-enum eRTL8139_Regs
-{
-       MAC0, MAC1, MAC2,
-       MAC3, MAC4, MAC5,
-       MAR0    = 0x08,
-       MAR1, MAR2, MAR3,
-       MAR4, MAR5, MAR6, MAR7,
-       
-       RBSTART = 0x30, //!< Recieve Buffer Start (DWord)
-       // 0x31, 0x32, 0x33
-       
-       // ??, ??, ??, RST, RE, TE, ??, ??
-       CMD     = 0x37,
-       IMR     = 0x3C,
-       ISR     = 0x3E,
-       
-       RCR     = 0x44,
-       
-       CONFIG1 = 0x52
-};
-
-// === TYPES ===
-typedef struct sCard
-{
-       Uint16  IOBase;
-       Uint8   IRQ;
-       
-        int    NumWaitingPackets;
-       
-       void    *ReceiveBuffer;
-       tPAddr  PhysReceiveBuffer;
-       
-       char    Name[2];
-       tVFS_Node       Node;
-       Uint8   MacAddr[6];
-}      tCard;
-
-// === PROTOTYPES ===
- int   RTL8139_Install(char **Options);
-char   *RTL8139_ReadDir(tVFS_Node *Node, int Pos);
-tVFS_Node      *RTL8139_FindDir(tVFS_Node *Node, const char *Filename);
- int   RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
-Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
-Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
-void   RTL8139_IRQHandler(int Num);
-
-// === GLOBALS ===
-MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
-tDevFS_Driver  gRTL8139_DriverInfo = {
-       NULL, "RTL8139",
-       {
-       .NumACLs = 1,
-       .ACLs = &gVFS_ACL_EveryoneRX,
-       .Flags = VFS_FFLAG_DIRECTORY,
-       .ReadDir = RTL8139_ReadDir,
-       .FindDir = RTL8139_FindDir,
-       .IOCtl = RTL8139_RootIOCtl
-       }
-};
- int   giRTL8139_CardCount;
-tCard  *gpRTL8139_Cards;
-
-// === CODE ===
-/**
- * \brief Installs the RTL8139 Driver
- */
-int RTL8139_Install(char **Options)
-{
-        int    id = -1;
-        int    i = 0;
-       Uint16  base;
-       
-       giRTL8139_CardCount = PCI_CountDevices( 0x10EC, 0x8139, 0 );
-       
-       gpRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
-       
-       
-       while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
-       {
-               base = PCI_AssignPort( id, 0, 0x100 );
-               gpRTL8139_Cards[i].IOBase = base;
-               gpRTL8139_Cards[i].IRQ = PCI_GetIRQ( id );
-               
-               // Install IRQ Handler
-               IRQ_AddHandler(gpRTL8139_Cards[ k ].IRQ, RTL8136_IRQHandler);
-               
-               // Power on
-               outb( base + CONFIG1, 0x00 );
-               // Reset (0x10 to CMD)
-               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
-               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
-               outl(base + RCR, 0x0F);
-               
-               outb(base + CMD, 0x0C); // Recive Enable and Transmit Enable
-               
-               // Get the card's MAC address
-               gpRTL8139_Cards[ i ].MacAddr[0] = inb(base+MAC0);
-               gpRTL8139_Cards[ i ].MacAddr[1] = inb(base+MAC1);
-               gpRTL8139_Cards[ i ].MacAddr[2] = inb(base+MAC2);
-               gpRTL8139_Cards[ i ].MacAddr[3] = inb(base+MAC3);
-               gpRTL8139_Cards[ i ].MacAddr[4] = inb(base+MAC4);
-               gpRTL8139_Cards[ i ].MacAddr[5] = inb(base+MAC5);
-               
-               // Set VFS Node
-               gpRTL8139_Cards[ i ].Name[0] = '0'+i;
-               gpRTL8139_Cards[ i ].Name[1] = '\0';
-               gpRTL8139_Cards[ i ].Node.ImplPtr = &gpRTL8139_Cards[ i ];
-               gpRTL8139_Cards[ i ].Node.NumACLs = 0;
-               gpRTL8139_Cards[ i ].Node.CTime = now();
-               gpRTL8139_Cards[ i ].Node.Write = RTL8139_Write;
-               gpRTL8139_Cards[ i ].Node.Read = RTL8139_Read;
-               gpRTL8139_Cards[ i ].Node.IOCtl = RTL8139_IOCtl;
-               
-               Log_Log("RTL8139", "Card %i 0x%04x %02x:%02x:%02x:%02x:%02x:%02x",
-                       i, base,
-                       gpRTL8139_Cards[ i ].MacAddr[0], gpRTL8139_Cards[ i ].MacAddr[1],
-                       gpRTL8139_Cards[ i ].MacAddr[2], gpRTL8139_Cards[ i ].MacAddr[3],
-                       gpRTL8139_Cards[ i ].MacAddr[4], gpRTL8139_Cards[ i ].MacAddr[5]
-                       );
-               
-               i ++;
-       }
-       return MODULE_ERR_OK;
-}
-
-// --- Root Functions ---
-char *RTL8139_ReadDir(tVFS_Node *Node, int Pos)
-{
-       if( Pos < 0 || Pos > giRTL8139_CardCount )      return NULL;
-       
-       return strdup( gpRTL8139_Cards[Pos].Name );
-}
-
-tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename)
-{
-       //TODO: It might be an idea to supprt >10 cards
-       if(Filename[0] == '\0' || Filename[0] != '\0')  return NULL;
-       if(Filename[0] < '0' || Filename[0] > '9')      return NULL;
-       return &gpRTL8139_Cards[ Filename[0]-'0' ].Node;
-}
-
-const char *csaRTL8139_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
-int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg)
-{
-       switch(ID)
-       {
-       BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_RootIOCtls);
-       }
-       return 0;
-}
-
-// --- File Functions ---
-Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
-{
-       return 0;
-}
-
-Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
-{
-       return 0;
-}
-
-const char *csaRTL8139_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
-int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Arg)
-{
-       switch(ID)
-       {
-       BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_NodeIOCtls);
-       }
-       return 0;
-}
diff --git a/Modules/Network/RTL8139/rtl8139.c b/Modules/Network/RTL8139/rtl8139.c
new file mode 100644 (file)
index 0000000..254ad11
--- /dev/null
@@ -0,0 +1,198 @@
+/*
+ * Acess2 RTL8139 Driver
+ * - By John Hodge (thePowersGang)
+ */
+#define        DEBUG   0
+#define VERSION        ((0<<8)|50)
+#include <acess.h>
+#include <modules.h>
+#include <fs_devfs.h>
+#include <drv_pci.h>
+#include <tpl_drv_network.h>
+
+// === CONSTANTS ===
+enum eRTL8139_Regs
+{
+       MAC0, MAC1, MAC2,
+       MAC3, MAC4, MAC5,
+       MAR0    = 0x08,
+       MAR1, MAR2, MAR3,
+       MAR4, MAR5, MAR6, MAR7,
+       
+       RBSTART = 0x30, //!< Recieve Buffer Start (DWord)
+       // 0x31, 0x32, 0x33
+       
+       // ??, ??, ??, RST, RE, TE, ??, ??
+       CMD     = 0x37,
+       IMR     = 0x3C,
+       ISR     = 0x3E,
+       
+       RCR     = 0x44,
+       
+       CONFIG1 = 0x52
+};
+
+// === TYPES ===
+typedef struct sCard
+{
+       Uint16  IOBase;
+       Uint8   IRQ;
+       
+        int    NumWaitingPackets;
+       
+       void    *ReceiveBuffer;
+       tPAddr  PhysReceiveBuffer;
+       
+       char    Name[2];
+       tVFS_Node       Node;
+       Uint8   MacAddr[6];
+}      tCard;
+
+// === PROTOTYPES ===
+ int   RTL8139_Install(char **Options);
+char   *RTL8139_ReadDir(tVFS_Node *Node, int Pos);
+tVFS_Node      *RTL8139_FindDir(tVFS_Node *Node, const char *Filename);
+ int   RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
+Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
+Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
+void   RTL8139_IRQHandler(int Num);
+
+// === GLOBALS ===
+MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
+tDevFS_Driver  gRTL8139_DriverInfo = {
+       NULL, "RTL8139",
+       {
+       .NumACLs = 1,
+       .ACLs = &gVFS_ACL_EveryoneRX,
+       .Flags = VFS_FFLAG_DIRECTORY,
+       .ReadDir = RTL8139_ReadDir,
+       .FindDir = RTL8139_FindDir,
+       .IOCtl = RTL8139_RootIOCtl
+       }
+};
+ int   giRTL8139_CardCount;
+tCard  *gpRTL8139_Cards;
+
+// === CODE ===
+/**
+ * \brief Installs the RTL8139 Driver
+ */
+int RTL8139_Install(char **Options)
+{
+        int    id = -1;
+        int    i = 0;
+       Uint16  base;
+       
+       giRTL8139_CardCount = PCI_CountDevices( 0x10EC, 0x8139, 0 );
+       
+       gpRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
+       
+       
+       while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
+       {
+               base = PCI_AssignPort( id, 0, 0x100 );
+               gpRTL8139_Cards[i].IOBase = base;
+               gpRTL8139_Cards[i].IRQ = PCI_GetIRQ( id );
+               
+               // Install IRQ Handler
+               IRQ_AddHandler(gpRTL8139_Cards[ k ].IRQ, RTL8136_IRQHandler);
+               
+               // Power on
+               outb( base + CONFIG1, 0x00 );
+
+               // Reset (0x10 to CMD)
+               outb( base + CMD, 0x10 );       
+               while( inb(base + CMD) & 0x10 ) ;
+               
+               // 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);
+       
+               // Recive Enable and Transmit Enable    
+               outb(base + CMD, 0x0C);
+               
+               // Get the card's MAC address
+               gpRTL8139_Cards[ i ].MacAddr[0] = inb(base+MAC0);
+               gpRTL8139_Cards[ i ].MacAddr[1] = inb(base+MAC1);
+               gpRTL8139_Cards[ i ].MacAddr[2] = inb(base+MAC2);
+               gpRTL8139_Cards[ i ].MacAddr[3] = inb(base+MAC3);
+               gpRTL8139_Cards[ i ].MacAddr[4] = inb(base+MAC4);
+               gpRTL8139_Cards[ i ].MacAddr[5] = inb(base+MAC5);
+               
+               // Set VFS Node
+               gpRTL8139_Cards[ i ].Name[0] = '0'+i;
+               gpRTL8139_Cards[ i ].Name[1] = '\0';
+               gpRTL8139_Cards[ i ].Node.ImplPtr = &gpRTL8139_Cards[ i ];
+               gpRTL8139_Cards[ i ].Node.NumACLs = 0;
+               gpRTL8139_Cards[ i ].Node.CTime = now();
+               gpRTL8139_Cards[ i ].Node.Write = RTL8139_Write;
+               gpRTL8139_Cards[ i ].Node.Read = RTL8139_Read;
+               gpRTL8139_Cards[ i ].Node.IOCtl = RTL8139_IOCtl;
+               
+               Log_Log("RTL8139", "Card %i 0x%04x %02x:%02x:%02x:%02x:%02x:%02x",
+                       i, base,
+                       gpRTL8139_Cards[ i ].MacAddr[0], gpRTL8139_Cards[ i ].MacAddr[1],
+                       gpRTL8139_Cards[ i ].MacAddr[2], gpRTL8139_Cards[ i ].MacAddr[3],
+                       gpRTL8139_Cards[ i ].MacAddr[4], gpRTL8139_Cards[ i ].MacAddr[5]
+                       );
+               
+               i ++;
+       }
+       return MODULE_ERR_OK;
+}
+
+// --- Root Functions ---
+char *RTL8139_ReadDir(tVFS_Node *Node, int Pos)
+{
+       if( Pos < 0 || Pos > giRTL8139_CardCount )      return NULL;
+       
+       return strdup( gpRTL8139_Cards[Pos].Name );
+}
+
+tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename)
+{
+       //TODO: It might be an idea to supprt >10 cards
+       if(Filename[0] == '\0' || Filename[0] != '\0')  return NULL;
+       if(Filename[0] < '0' || Filename[0] > '9')      return NULL;
+       return &gpRTL8139_Cards[ Filename[0]-'0' ].Node;
+}
+
+const char *csaRTL8139_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
+int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg)
+{
+       switch(ID)
+       {
+       BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_RootIOCtls);
+       }
+       return 0;
+}
+
+// --- File Functions ---
+Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
+{
+       return 0;
+}
+
+Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
+{
+       return 0;
+}
+
+const char *csaRTL8139_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
+int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Arg)
+{
+       switch(ID)
+       {
+       BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_NodeIOCtls);
+       }
+       return 0;
+}
index c8f4fa9..6ed0409 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Disk Input/Output control
  */
  *
  * Disk Input/Output control
  */
-#define DEBUG  1
+#define DEBUG  0
 #include <acess.h>
 #include <modules.h>   // Needed for error codes
 #include <drv_pci.h>
 #include <acess.h>
 #include <modules.h>   // Needed for error codes
 #include <drv_pci.h>
index 9d47931..424d865 100644 (file)
@@ -26,7 +26,7 @@ install: $(_BIN)
 
 $(_BIN): $(OBJ)
        @mkdir -p $(dir $(_BIN))
 
 $(_BIN): $(OBJ)
        @mkdir -p $(dir $(_BIN))
-       @echo --- $(LD) -o $@
+       @echo [LD] -o $@
 ifneq ($(_DBGMAKEFILE),)
        $(LD) -g $(LDFLAGS) -o $@ $(OBJ) -Map Map.txt
 else
 ifneq ($(_DBGMAKEFILE),)
        $(LD) -g $(LDFLAGS) -o $@ $(OBJ) -Map Map.txt
 else
@@ -35,7 +35,7 @@ endif
        @$(DISASM) $(_BIN) > $(BIN).dsm
 
 $(OBJ): $(_OBJPREFIX)%.o: %.c
        @$(DISASM) $(_BIN) > $(BIN).dsm
 
 $(OBJ): $(_OBJPREFIX)%.o: %.c
-       @echo --- GCC -o $@
+       @echo [CC] -o $@
 ifneq ($(_OBJPREFIX),)
        @mkdir -p $(_OBJPREFIX)
 endif
 ifneq ($(_OBJPREFIX),)
        @mkdir -p $(_OBJPREFIX)
 endif

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