Kernel/x86_64 - Fix task switching
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / lib.c
index 385b677..60770f6 100644 (file)
@@ -2,6 +2,7 @@
  */
 #include <acess.h>
 #include <arch.h>
+#include <drv_serial.h>
 
 #define DEBUG_TO_E9    1
 #define DEBUG_TO_SERIAL        1
@@ -19,6 +20,7 @@ extern void   *Proc_GetCurThread(void);
 
 // === PROTOTYPEs ===
  int   putDebugChar(char ch);
+void   Debug_SerialIRQHandler(int irq, void *unused);
 
 // === CODE ===
 /**
@@ -86,6 +88,8 @@ void SHORTLOCK(struct sShortSpinlock *Lock)
                Lock->Depth ++;
                return ;
        }
+       #else
+       ASSERT( !CPU_HAS_LOCK(Lock) );
        #endif
        
        // Wait for another CPU to release
@@ -144,19 +148,35 @@ void SHORTREL(struct sShortSpinlock *Lock)
        #endif
 }
 
+void __AtomicTestSetLoop(Uint *Ptr, Uint Value)
+{
+       __ASM__(
+               "1:\n\t"
+               "xor %%eax, %%eax;\n\t"
+               "lock cmpxchg %0, (%1);\n\t"    // if( Ptr==0 ) { ZF=1; Ptr=Value } else { ZF=0; _=Ptr }
+               "jnz 1b;\n\t"
+               :: "r"(Value), "r"(Ptr)
+               : "eax" // EAX clobbered
+               );
+}
+
 // === DEBUG IO ===
 #if USE_GDB_STUB
+void initGdbSerial(void)
+{
+       outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
+       outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
+       outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
+       outb(GDB_SERIAL_PORT + 1, 0x00);    //  (base is         (hi byte)
+       outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit (8N1)
+       outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
+       outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
+       gbDebug_SerialSetup = 1;
+}
 int putDebugChar(char ch)
 {
        if(!gbGDB_SerialSetup) {
-               outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
-               outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
-               outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
-               outb(GDB_SERIAL_PORT + 1, 0x00);    //  (base is         (hi byte)
-               outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit (8N1)
-               outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
-               outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
-               gbDebug_SerialSetup = 1;
+               initGdbSerial();
        }
        while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
        outb(GDB_SERIAL_PORT, ch);
@@ -165,20 +185,25 @@ int putDebugChar(char ch)
 int getDebugChar(void)
 {
        if(!gbGDB_SerialSetup) {
-               outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
-               outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
-               outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
-               outb(GDB_SERIAL_PORT + 1, 0x00);    //                   (hi byte)
-               outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
-               outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
-               outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
-               gbDebug_SerialSetup = 1;
+               initGdbSerial();
        }
        while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0)     ;
        return inb(GDB_SERIAL_PORT);
 }
 #endif
 
+void Debug_SerialIRQHandler(int irq, void *unused)
+{
+       if( (inb(SERIAL_PORT+5) & 0x01) == 0 ) {
+               Debug("Serial no data");
+               return ;
+       }
+       
+       char ch = inb(SERIAL_PORT);
+       //Debug("Serial RX 0x%x", ch);
+       Serial_ByteReceived(gSerial_KernelDebugPort, ch);
+}
+
 void Debug_PutCharDebug(char ch)
 {
        #if DEBUG_TO_E9
@@ -194,7 +219,9 @@ void Debug_PutCharDebug(char ch)
                outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
                outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
                outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
+               outb(SERIAL_PORT + 1, 0x05);    // Enable ERBFI (Rx Full), ELSI (Line Status)
                gbDebug_SerialSetup = 1;
+               IRQ_AddHandler(4, Debug_SerialIRQHandler, NULL);
        }
        while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
        outb(SERIAL_PORT, ch);
@@ -345,11 +372,11 @@ void *memset(void *__dest, int __val, size_t __count)
                __asm__ __volatile__ ("rep stosb" : : "D"(__dest),"a"(__val),"c"(__count));
        else {
                Uint8   *dst = __dest;
+               size_t  qwords = __count / 8;
+               size_t  trail_bytes = __count % 8;
 
-               __asm__ __volatile__ ("rep stosq" : : "D"(dst),"a"(0),"c"(__count/8));
-               dst += __count & ~7;
-               __count = __count & 7;
-               while( __count-- )
+               __asm__ __volatile__ ("rep stosq" : "=D"(dst) : "D"(dst),"a"(0),"c"(qwords));
+               while( trail_bytes-- )
                        *dst++ = 0;
        }
        return __dest;

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