From: John Hodge Date: Wed, 21 Apr 2010 04:29:04 +0000 (+0800) Subject: Random fixes, disabled debug in vterm, random crash disappeared, also doing fixes... X-Git-Tag: rel0.06~233 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=7d2f8503550796a3cd42d01f944c2baa49a2fcfa;p=tpg%2Facess2.git Random fixes, disabled debug in vterm, random crash disappeared, also doing fixes on TCP stack --- diff --git a/Kernel/Makefile.BuildNum b/Kernel/Makefile.BuildNum index bb3445a4..bacdc56c 100644 --- a/Kernel/Makefile.BuildNum +++ b/Kernel/Makefile.BuildNum @@ -1 +1 @@ -BUILD_NUM = 1868 +BUILD_NUM = 1933 diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index 3b5ceb2f..62980674 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -779,6 +779,14 @@ void Proc_Scheduler(int CPU) #else __asm__ __volatile__ ("mov %0, %%cr3"::"a"(thread->MemState.CR3)); #endif + + #if 0 + if(thread->SavedState.ESP > 0xC0000000 + && thread->SavedState.ESP < thread->KernelStack-0x2000) { + Log_Warning("Proc", "Possible bad ESP %p (PID %i)", thread->SavedState.ESP); + } + #endif + // Switch threads __asm__ __volatile__ ( "mov %1, %%esp\n\t" // Restore ESP diff --git a/Kernel/debug.c b/Kernel/debug.c index 4b3aeb77..e8e181d5 100644 --- a/Kernel/debug.c +++ b/Kernel/debug.c @@ -52,11 +52,14 @@ int getDebugChar() return inb(GDB_SERIAL_PORT); } +volatile int gbInPutChar = 0; + static void Debug_Putchar(char ch) { - if(giDebug_KTerm != -1) - VFS_Write(giDebug_KTerm, 1, &ch); - + #if DEBUG_TO_E9 + __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) ); + #endif + #if DEBUG_TO_SERIAL if(!gbDebug_SerialSetup) { outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts @@ -71,10 +74,12 @@ static void Debug_Putchar(char ch) while( (inb(SERIAL_PORT + 5) & 0x20) == 0 ); outb(SERIAL_PORT, ch); #endif - - #if DEBUG_TO_E9 - __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) ); - #endif + + if(gbInPutChar) return ; + gbInPutChar = 1; + if(giDebug_KTerm != -1) + VFS_Write(giDebug_KTerm, 1, &ch); + gbInPutChar = 0; } static void Debug_Puts(char *Str) @@ -272,10 +277,16 @@ void Panic(char *Fmt, ...) void Debug_SetKTerminal(char *File) { - if(giDebug_KTerm != -1) - VFS_Close(giDebug_KTerm); - giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE); - Log_Log("Debug", "Opened '%s' as 0x%x", File, giDebug_KTerm); + int tmp; + if(giDebug_KTerm != -1) { + tmp = giDebug_KTerm; + giDebug_KTerm = -1; + VFS_Close(tmp); + } + tmp = VFS_Open(File, VFS_OPENFLAG_WRITE); + Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp); + giDebug_KTerm = tmp; + Log_Log("Debug", "Returning to %p", __builtin_return_address(0)); } void Debug_Enter(char *FuncName, char *ArgTypes, ...) diff --git a/Kernel/drv/vga.c b/Kernel/drv/vga.c index b0c96623..700eae9e 100644 --- a/Kernel/drv/vga.c +++ b/Kernel/drv/vga.c @@ -1,6 +1,7 @@ /* * Acess2 VGA Controller Driver */ +#define DEBUG 0 #include #include #include diff --git a/Kernel/drv/vterm.c b/Kernel/drv/vterm.c index 92fb6f98..fe2ccd99 100644 --- a/Kernel/drv/vterm.c +++ b/Kernel/drv/vterm.c @@ -1,7 +1,7 @@ /* * Acess2 Virtual Terminal Driver */ -#define DEBUG 1 +#define DEBUG 0 #include #include #include @@ -104,7 +104,6 @@ tVTerm *gpVT_CurTerm = &gVT_Terminals[0]; // --- Video State --- short giVT_RealWidth; //!< Real Width short giVT_RealHeight; //!< Real Height - int gbVT_TextMode = 1; // --- Driver Handles --- char *gsVT_OutputDevice = NULL; char *gsVT_InputDevice = NULL; @@ -152,7 +151,6 @@ int VT_Install(char **Arguments) strcpy(gsVT_InputDevice, args[1]); args ++; break; - } } } @@ -194,6 +192,7 @@ int VT_Install(char **Arguments) // Set kernel output to VT0 Debug_SetKTerminal("/Devices/VTerm/0"); + Log_Log("VTerm", "Returning %i", MODULE_ERR_OK); return MODULE_ERR_OK; } @@ -237,7 +236,7 @@ tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name) { int num; - //ENTER("pNode sName", Node, Name); + ENTER("pNode sName", Node, Name); // Open the input and output files if needed if(giVT_OutputDevHandle == -2) VT_InitOutput(); @@ -255,7 +254,7 @@ tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name) return NULL; } // Return node - //LEAVE('p', &gVT_Terminals[num].Node); + LEAVE('p', &gVT_Terminals[num].Node); return &gVT_Terminals[num].Node; } @@ -321,8 +320,8 @@ Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) } break; - case TERM_MODE_FB: - //case TERM_MODE_: + //case TERM_MODE_FB: + default: while(pos < Length) { while(term->InputRead == term->InputWrite) Threads_Yield(); @@ -344,8 +343,6 @@ Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { tVTerm *term = &gVT_Terminals[ Node->Inode ]; - //ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer); - // Write switch( term->Mode ) { @@ -376,7 +373,6 @@ Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) } } - //LEAVE('i', 0); return 0; } @@ -905,7 +901,7 @@ void VT_int_PutChar(tVTerm *Term, Uint32 Ch) memcpy( Term->Text, &Term->Text[Term->Width], - Term->Width*(Term->Height-1)*VT_SCROLLBACK*sizeof(tVT_Char) + (Term->Width*Term->Height*VT_SCROLLBACK-Term->Width)*sizeof(tVT_Char) ); // Clear last row @@ -915,11 +911,13 @@ void VT_int_PutChar(tVTerm *Term, Uint32 Ch) Term->Text[ base + i ].Colour = Term->CurColour; } + //LOG("Scrolled buffer"); VT_int_UpdateScreen( Term, 1 ); } else if(Term->WritePos > Term->Width*Term->Height+Term->ViewPos) { Term->ViewPos += Term->Width; + //LOG("Scrolled screen"); VT_int_UpdateScreen( Term, 1 ); } else @@ -939,41 +937,27 @@ void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll ) if( Term->Mode == TERM_MODE_TEXT ) { - if(gbVT_TextMode) - { - if(UpdateAll) { - VFS_WriteAt( - giVT_OutputDevHandle, - 0, - Term->Width*Term->Height*sizeof(tVT_Char), - &Term->Text[Term->ViewPos] - ); - } else { - int pos = Term->WritePos - Term->WritePos % Term->Width; - VFS_WriteAt( - giVT_OutputDevHandle, - (pos - Term->ViewPos)*sizeof(tVT_Char), - Term->Width*sizeof(tVT_Char), - &Term->Text[pos] - ); - } - } - else - { - //TODO: Do VT Rendered Text - #if 0 - if( UpdateAll ) { - VT_RenderText(0, Term->Width*Term->Height, &Term->Text[Term->ViewPos]); - } - else { - int pos = Term->WritePos - Term->WritePos % Term->Width; - VT_RenderText( - pos - Term->ViewPos, - Term->Width, - &Term->Text[pos] - ); - } - #endif + if(UpdateAll) { + //LOG("UpdateAll = 1"); + //LOG("VFS_WriteAt(0x%x, 0, %i*sizeof(tVT_Char), &Term->Text[%i])", + // giVT_OutputDevHandle, Term->Width*Term->Height, Term->ViewPos); + VFS_WriteAt( + giVT_OutputDevHandle, + 0, + Term->Width*Term->Height*sizeof(tVT_Char), + &Term->Text[Term->ViewPos] + ); + } else { + int pos = Term->WritePos - Term->WritePos % Term->Width; + //LOG("UpdateAll = 0"); + //LOG("VFS_WriteAt(0x%x, %i*sizeof(tVT_Char), %i*sizeof(tVT_Char), &Term->Text[%i])", + // giVT_OutputDevHandle, (pos - Term->ViewPos), Term->Width, pos); + VFS_WriteAt( + giVT_OutputDevHandle, + (pos - Term->ViewPos)*sizeof(tVT_Char), + Term->Width*sizeof(tVT_Char), + &Term->Text[pos] + ); } } else diff --git a/Kernel/modules.c b/Kernel/modules.c index eb742d47..68d5ba00 100644 --- a/Kernel/modules.c +++ b/Kernel/modules.c @@ -136,6 +136,7 @@ int Module_int_Initialise(tModule *Module) LEAVE_RET('i', ret); return ret; } + LOG("ret = %i", ret); // Remove from loading list gLoadingModules = gLoadingModules->Next; diff --git a/Kernel/vfs/open.c b/Kernel/vfs/open.c index d2035713..d5e940ec 100644 --- a/Kernel/vfs/open.c +++ b/Kernel/vfs/open.c @@ -759,6 +759,8 @@ tVFS_Handle *VFS_GetHandle(int FD) { tVFS_Handle *h; + //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD); + if(FD < 0) return NULL; if(FD & VFS_KERNEL_FLAG) { @@ -771,6 +773,7 @@ tVFS_Handle *VFS_GetHandle(int FD) } if(h->Node == NULL) return NULL; + //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h); return h; } diff --git a/Modules/Display/VESA/main.c b/Modules/Display/VESA/main.c index e87ca058..47bcf63b 100644 --- a/Modules/Display/VESA/main.c +++ b/Modules/Display/VESA/main.c @@ -37,7 +37,7 @@ tDevFS_Driver gVesa_DriverStruct = { }; tSpinlock glVesa_Lock; tVM8086 *gpVesa_BiosState; - int giVesaCurrentMode = -1; + int giVesaCurrentMode = 0; int giVesaCurrentFormat = VIDEO_BUFFMT_TEXT; int giVesaDriverId = -1; char *gVesaFramebuffer = (void*)0xC00A0000; @@ -109,6 +109,7 @@ int Vesa_Install(char **Arguments) gVesa_Modes[i].fbSize = 0; } + gVesa_Modes[i].pitch = modeinfo->pitch; gVesa_Modes[i].width = modeinfo->Xres; gVesa_Modes[i].height = modeinfo->Yres; gVesa_Modes[i].bpp = modeinfo->bpp; @@ -152,8 +153,8 @@ Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Default Text mode if( giVesaCurrentMode == 0 ) { - Uint8 *fb = (Uint8 *)(KERNEL_BASE|0xB8000); - Uint32 *buf = Buffer; + Uint16 *fb = (Uint16*)(KERNEL_BASE|0xB8000); + tVT_Char *chars = Buffer; int rem; if( giVesaCurrentFormat != VIDEO_BUFFMT_TEXT ) { @@ -162,31 +163,31 @@ Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) return -1; } - if( Offset + Length > 25*80*8 ) { + if( Offset + Length > 25*80*sizeof(tVT_Char) ) { Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow"); LEAVE('i', 0); return 0; } - fb += 2*Offset; - for(rem = Length / sizeof(tVT_Char); rem --; fb += 2) + fb += 2*(Offset/sizeof(tVT_Char)); + LOG("fb = %p", fb); + for(rem = Length / sizeof(tVT_Char); rem --; fb ++, chars++) { - if( *buf < 0x80 ) - *fb = *buf & 0x7F; + if( chars->Ch < 0x80 ) + *fb = chars->Ch & 0x7F; else *fb = 0x00; - buf ++; - fb[1] = 0; - fb[1] |= (*buf & 0x888) == 0x888 ? 0x8 : 0; - fb[1] |= (*buf & 0x700) > 0x300 ? 0x4 : 0; - fb[1] |= (*buf & 0x070) > 0x030 ? 0x2 : 0; - fb[1] |= (*buf & 0x007) > 0x003 ? 0x1 : 0; - fb[1] |= (*buf & 0x888000) == 0x888000 ? 0x80 : 0; - fb[1] |= (*buf & 0x700000) > 0x300000 ? 0x40 : 0; - fb[1] |= (*buf & 0x070000) > 0x030000 ? 0x20 : 0; - fb[1] |= (*buf & 0x007000) > 0x003000 ? 0x10 : 0; - buf ++; + *fb |= (chars->FGCol & 0x888) == 0x888 ? 0x8 : 0; + *fb |= (chars->FGCol & 0x700) > 0x300 ? 0x4 : 0; + *fb |= (chars->FGCol & 0x070) > 0x030 ? 0x2 : 0; + *fb |= (chars->FGCol & 0x007) > 0x003 ? 0x1 : 0; + *fb |= (chars->BGCol & 0x888) == 0x888 ? 0x80 : 0; + *fb |= (chars->BGCol & 0x700) > 0x300 ? 0x40 : 0; + *fb |= (chars->BGCol & 0x070) > 0x030 ? 0x20 : 0; + *fb |= (chars->BGCol & 0x007) > 0x003 ? 0x10 : 0; + //LOG("%08x (%03x,%03x) = %04x", + // chars->Ch, chars->BGCol, chars->FGCol, *fb); } Length /= sizeof(tVT_Char); Length *= sizeof(tVT_Char); @@ -213,8 +214,10 @@ Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) Offset /= sizeof(tVT_Char); dest = (void*)gVesaFramebuffer; - x = (Offset % gVesa_Modes[giVesaCurrentMode].width) * giVT_CharWidth; - y = (Offset / gVesa_Modes[giVesaCurrentMode].width) * giVT_CharHeight; + LOG("gVesa_Modes[%i].width = %i", giVesaCurrentMode, gVesa_Modes[giVesaCurrentMode].width); + x = (Offset % (gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth)) * giVT_CharWidth; + y = (Offset / (gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth)) * giVT_CharHeight; + LOG("(x,y) = (%i,%i)", x, y); // Sanity Check if(y > gVesa_Modes[giVesaCurrentMode].height) { diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c index a6fb60e4..90f3dc96 100644 --- a/Modules/IPStack/tcp.c +++ b/Modules/IPStack/tcp.c @@ -64,30 +64,6 @@ void TCP_Initialise() IPv4_RegisterCallback(IP4PROT_TCP, TCP_GetPacket); } -/** - * \brief Open a connection to another host using TCP - * \param Conn Connection structure - */ -void TCP_StartConnection(tTCPConnection *Conn) -{ - tTCPHeader hdr; - - Conn->State = TCP_ST_SYN_SENT; - - hdr.SourcePort = htons(Conn->LocalPort); - hdr.DestPort = htons(Conn->RemotePort); - Conn->NextSequenceSend = rand(); - hdr.SequenceNumber = htonl(Conn->NextSequenceSend); - hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4; - hdr.Flags = TCP_FLAG_SYN; - hdr.WindowSize = htons(TCP_WINDOW_SIZE); // Max - hdr.Checksum = 0; // TODO - hdr.UrgentPointer = 0; - - TCP_SendPacket( Conn, sizeof(tTCPHeader), &hdr ); - return ; -} - /** * \brief Sends a packet from the specified connection, calculating the checksums * \param Conn Connection @@ -302,7 +278,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head if( Connection->State == TCP_ST_SYN_SENT ) { - if( Header->Flags & (TCP_FLAG_SYN|TCP_FLAG_ACK) ) { + if( (Header->Flags & (TCP_FLAG_SYN|TCP_FLAG_ACK)) == (TCP_FLAG_SYN|TCP_FLAG_ACK) ) { Header->DestPort = Header->SourcePort; Header->SourcePort = htons(Connection->LocalPort); @@ -313,8 +289,8 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head Header->DataOffset = (sizeof(tTCPHeader)/4) << 4; Log_Log("TCP", "ACKing SYN-ACK"); TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); + Connection->State = TCP_ST_OPEN; } - Connection->State = TCP_ST_OPEN; } // Get length of data @@ -329,7 +305,8 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head // TODO: Check what to do here if(Header->Flags & TCP_FLAG_FIN) { if( Connection->State == TCP_ST_FIN_SENT ) { - + Connection->State = TCP_ST_FINISHED; + return ; } else { Connection->State = TCP_ST_FINISHED; @@ -730,6 +707,8 @@ tVFS_Node *TCP_Client_Init(tInterface *Interface) conn->Node.IOCtl = TCP_Client_IOCtl; conn->Node.Close = TCP_Client_Close; + conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE ); + LOCK(&glTCP_OutbountCons); conn->Next = gTCP_OutbountCons; gTCP_OutbountCons = conn; @@ -750,9 +729,12 @@ Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buff size_t len; ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer); + LOG("conn = %p", conn); + LOG("conn->State = %i", conn->State); // Check if connection is open - while( conn->State == TCP_ST_HALFOPEN ) Threads_Yield(); + while( conn->State == TCP_ST_HALFOPEN || conn->State == TCP_ST_SYN_SENT ) + Threads_Yield(); if( conn->State != TCP_ST_OPEN ) { LEAVE('i', 0); return 0; @@ -794,10 +776,9 @@ void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, void *Dat packet->DataOffset = (sizeof(tTCPHeader)/4)*16; packet->WindowSize = TCP_WINDOW_SIZE; - //packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); - packet->AcknowlegementNumber = 0; + packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); packet->SequenceNumber = htonl(Connection->NextSequenceSend); - packet->Flags = TCP_FLAG_PSH; // Hey, ACK if you can! + packet->Flags = TCP_FLAG_PSH|TCP_FLAG_ACK; // Hey, ACK if you can! memcpy(packet->Options, Data, Length); @@ -817,7 +798,8 @@ Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buf ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer); // Check if connection is open - while( conn->State == TCP_ST_HALFOPEN ) Threads_Yield(); + while( conn->State == TCP_ST_HALFOPEN || conn->State == TCP_ST_SYN_SENT ) + Threads_Yield(); if( conn->State != TCP_ST_OPEN ) { LEAVE('i', 0); return 0; @@ -835,6 +817,33 @@ Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buf return Length; } +/** + * \brief Open a connection to another host using TCP + * \param Conn Connection structure + */ +void TCP_StartConnection(tTCPConnection *Conn) +{ + tTCPHeader hdr; + + Conn->State = TCP_ST_SYN_SENT; + + hdr.SourcePort = htons(Conn->LocalPort); + hdr.DestPort = htons(Conn->RemotePort); + Conn->NextSequenceSend = rand(); + hdr.SequenceNumber = htonl(Conn->NextSequenceSend); + hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4; + hdr.Flags = TCP_FLAG_SYN; + hdr.WindowSize = htons(TCP_WINDOW_SIZE); // Max + hdr.Checksum = 0; // TODO + hdr.UrgentPointer = 0; + + TCP_SendPacket( Conn, sizeof(tTCPHeader), &hdr ); + + Conn->NextSequenceSend ++; + Conn->State = TCP_ST_SYN_SENT; + return ; +} + /** * \brief Control a client socket */ @@ -881,7 +890,7 @@ int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data) return 0; case 7: // Connect - if(conn->LocalPort == -1) + if(conn->LocalPort == 0xFFFF) conn->LocalPort = TCP_GetUnusedPort(); if(conn->RemotePort == -1) return 0; @@ -895,5 +904,23 @@ int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data) void TCP_Client_Close(tVFS_Node *Node) { - free(Node->ImplPtr); + tTCPConnection *conn = Node->ImplPtr; + tTCPHeader packet; + + packet.SourcePort = htons(conn->LocalPort); + packet.DestPort = htons(conn->RemotePort); + packet.DataOffset = (sizeof(tTCPHeader)/4)*16; + packet.WindowSize = TCP_WINDOW_SIZE; + + packet.AcknowlegementNumber = 0; + packet.SequenceNumber = htonl(conn->NextSequenceSend); + packet.Flags = TCP_FLAG_FIN; + + conn->State = TCP_ST_FIN_SENT; + + TCP_SendPacket( conn, sizeof(tTCPHeader), &packet ); + + while( conn->State == TCP_ST_FIN_SENT ) Threads_Yield(); + + free(conn); }