Working on x86_64 build
authorJohn Hodge <[email protected]>
Sun, 13 Feb 2011 04:28:32 +0000 (12:28 +0800)
committerJohn Hodge <[email protected]>
Sun, 13 Feb 2011 04:28:32 +0000 (12:28 +0800)
Kernel/Makefile
Kernel/Makefile.BuildNum.x86_64
Kernel/arch/x86_64/Makefile
Kernel/arch/x86_64/include/arch.h
Kernel/arch/x86_64/lib.c
Kernel/arch/x86_64/main.c
Kernel/arch/x86_64/time.c [new file with mode: 0644]

index 83c1240..cf4b077 100644 (file)
@@ -16,7 +16,7 @@ MAKEDEP               = $(CC) -M
 
 CPPFLAGS       += -I./include -I./arch/$(ARCHDIR)/include -D_MODULE_NAME_=\"Kernel\"
 CPPFLAGS       += -DARCH=$(ARCH) -DARCHDIR=$(ARCHDIR) -DKERNEL_VERSION=$(KERNEL_VERSION) -DBUILD_NUM=$(BUILD_NUM)
-CFLAGS         += -Wall -Werror -O3 -fno-stack-protector -fno-builtin -Wstrict-prototypes -g
+CFLAGS         += -Wall -Werror -fno-stack-protector -fno-builtin -Wstrict-prototypes -g
 ASFLAGS                += -D ARCH=\"$(ARCH)\" -D ARCHDIR=\"$(ARCHDIR)\"
 LDFLAGS                += -T arch/$(ARCHDIR)/link.ld -g
 
index d0d4f84..906b726 100644 (file)
@@ -23,7 +23,7 @@ endif
 
 A_OBJ := start32.ao start64.ao desctab.ao
 A_OBJ += main.o lib.o proc.o mm_virt.o mm_phys.o
-A_OBJ += kernelpanic.o errors.o
+A_OBJ += kernelpanic.o errors.o time.o
 A_OBJ += vm8086.o
 # rme.o
 
index 038b638..e43bd97 100644 (file)
@@ -10,8 +10,8 @@
 #define KERNEL_BASE    0xFFFFFFFF##80000000
 #define BITS   64
 
-#define STACKED_LOCKS  0
-#define LOCK_DISABLE_INTS      1
+#define STACKED_LOCKS  2       // 0: No, 1: Per-CPU, 2: Per-Thread
+#define LOCK_DISABLE_INTS      0
 
 #define INVLPTR        ((void*)0x0FFFFFFFFFFFFFFFULL)
 
index f20753b..da9955c 100644 (file)
@@ -5,6 +5,7 @@
 
 // === IMPORTS ===
 extern int     GetCPUNum(void);
+extern void    *Proc_GetCurThread(void);
 
 // === CODE ===
 /**
@@ -58,7 +59,7 @@ void SHORTLOCK(struct sShortSpinlock *Lock)
        
        #if LOCK_DISABLE_INTS
        // Save interrupt state and clear interrupts
-       __ASM__ ("pushf;\n\tpop %%rax\n\tcli" : "=a"(IF));
+       __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
        IF &= 0x200;    // AND out all but the interrupt flag
        #endif
        
@@ -85,16 +86,21 @@ void SHORTLOCK(struct sShortSpinlock *Lock)
                        : "a"(0), "r"(cpu), "r"(&Lock->Lock)
                        );
                #elif STACKED_LOCKS == 2
-               __ASM__("lock cmpxchgl %2, (%3)"
+               __ASM__("lock cmpxchgq %2, (%3)"
                        : "=a"(v)
                        : "a"(0), "r"(thread), "r"(&Lock->Lock)
                        );
                #else
                __ASM__("xchgl %0, (%2)":"=a"(v):"a"(1),"D"(&Lock->Lock));
                #endif
+               
+               #if LOCK_DISABLE_INTS
+               if( v ) __ASM__("sti"); // Re-enable interrupts
+               #endif
        }
        
        #if LOCK_DISABLE_INTS
+       __ASM__("cli");
        Lock->IF = IF;
        #endif
 }
index 7d69368..4e6001d 100644 (file)
@@ -9,7 +9,7 @@ extern void     Desctab_Init(void);
 extern void    MM_InitVirt(void);
 extern void    Heap_Install(void);
 extern void    Threads_Init(void);
-//extern void  Time_Setup(void);
+extern int     Time_Setup(void);
 extern void    System_Init(char *Commandline);
 
 extern void    MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
@@ -55,7 +55,7 @@ void kmain(Uint MbMagic, void *MbInfoPtr)
        Log_Log("Arch", "Starting threading...");
        Threads_Init();
        
-       //Time_Setup();
+       Time_Setup();
        *(Uint16*)(0xB8000) = 0x1F00|'F';
        
        Log_Log("Arch", "Starting VFS...");
diff --git a/Kernel/arch/x86_64/time.c b/Kernel/arch/x86_64/time.c
new file mode 100644 (file)
index 0000000..ec1e19d
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Acess2 Kernel
+ * Timekeeping
+ * arch/x86_64/time.c
+ */
+#include <acess.h>
+
+// === MACROS ===
+#define        TIMER_QUANTUM   100
+// 2^(15-rate), 15: 1HZ, 5: 1024Hz, 2: 8192Hz
+// (Max: 15, Min: 2) - 15 = 1Hz, 13 = 4Hz, 12 = 8Hz, 11 = 16Hz 10 = 32Hz, 2 = 8192Hz
+//#define TIMER_RATE   10      // 32 Hz
+//#define TIMER_RATE   12      // 8 Hz
+#define TIMER_RATE     14      // 2Hz
+//#define TIMER_RATE   15      // 1HZ
+#define TIMER_FREQ     (0x8000>>TIMER_RATE)    //Hz
+#define MS_PER_TICK_WHOLE      (1000/(TIMER_FREQ))
+#define MS_PER_TICK_FRACT      ((0x80000000*(1000%TIMER_FREQ))/TIMER_FREQ)
+
+// === IMPORTS ===
+extern volatile Sint64 giTimestamp;
+extern volatile Uint64 giTicks;
+extern volatile Uint64 giPartMiliseconds;
+extern void    Timer_CallTimers(void);
+
+// === GLOBALS ===
+volatile Uint64        giTime_TSCAtLastTick = 0;
+volatile Uint64        giTime_TSCPerTick = 0;
+
+// === PROTOTYPES ===
+Sint64 now(void);
+ int   Time_Setup(void);
+void   Time_Interrupt(int);
+Uint64 Time_ReadTSC(void);
+
+// === CODE ===
+/**
+ * \fn Sint64 now()
+ * \brief Return the current timestamp
+ */
+Sint64 now(void)
+{
+       Uint64  tsc = Time_ReadTSC();
+       tsc -= giTime_TSCAtLastTick;
+       tsc *= MS_PER_TICK_WHOLE;
+       if( giTime_TSCPerTick ) {
+               tsc /= giTime_TSCPerTick;
+       }
+       else
+               tsc = 0;
+       return giTimestamp + tsc;
+}
+
+/**
+ * \fn int Time_Setup(void)
+ * \brief Sets the system time from the Realtime-Clock
+ */
+int Time_Setup(void)
+{
+       Uint8   val;
+       
+       Log_Log("Timer", "RTC Timer firing at %iHz (%i divisor), %i.0x%08x",
+               TIMER_FREQ, TIMER_RATE, MS_PER_TICK_WHOLE, MS_PER_TICK_FRACT);
+       
+       outb(0x70, inb(0x70)&0x7F);     // Disable NMIs
+       __asm__ __volatile__ ("cli");   // Disable normal interrupts
+       
+       // Set IRQ8 firing rate
+       outb(0x70, 0x0A);       // Set the index to register A
+       val = inb(0x71); // Get the current value of register A
+       outb(0x70, 0x0A); // Reset index to A
+       val &= 0xF0;
+       val |= TIMER_RATE;
+       outb(0x71, val);        // Update the timer rate
+               
+       // Enable IRQ8
+       outb(0x70, 0x0B);       // Set the index to register B
+       val = inb(0x71);        // Read the current value of register B
+       outb(0x70, 0x0B);       // Set the index again (a read will reset the index to register D)
+       outb(0x71, val | 0x40); // Write the previous value or'd with 0x40. This turns on bit 6 of register D
+       
+       __asm__ __volatile__ ("sti");   // Re-enable normal interrupts
+       outb(0x70, inb(0x70)|0x80);     // Re-enable NMIs
+       
+       // Install IRQ Handler
+       IRQ_AddHandler(8, Time_Interrupt);
+       
+       // Make sure the RTC actually fires
+       outb(0x70, 0x0C); // Select register C
+       inb(0x71);      // Just throw away contents.
+       
+       return 0;
+}
+
+/**
+ * \brief Called on the timekeeping IRQ
+ * \param irq  IRQ number (unused)
+ */
+void Time_Interrupt(int irq)
+{
+       //Log("RTC Tick");
+       Uint64  curTSC = Time_ReadTSC();
+       
+       if( giTime_TSCAtLastTick )
+       {
+               giTime_TSCPerTick = curTSC - giTime_TSCAtLastTick;
+       }
+       giTime_TSCAtLastTick = curTSC;
+       
+       giTicks ++;
+       giTimestamp += MS_PER_TICK_WHOLE;
+       giPartMiliseconds += MS_PER_TICK_FRACT;
+       if(giPartMiliseconds > 0x80000000) {
+               giTimestamp ++;
+               giPartMiliseconds -= 0x80000000;
+       }
+       
+       Timer_CallTimers();
+
+       // Make sure the RTC Fires again
+       outb(0x70, 0x0C); // Select register C
+       inb(0x71);      // Just throw away contents.
+}
+
+#if 0
+/**
+ * \fn void Time_TimerThread(void)
+ */
+void Time_TimerThread(void)
+{
+       Sint64  next;
+       Threads_SetName("TIMER");
+       
+       next = giTimestamp + TIMER_QUANTUM;
+       for(;;)
+       {
+               while(giTimestamp < next)       Threads_Yield();
+               next = giTimestamp + TIMER_QUANTUM;     
+               Timer_CallTimers();
+       }
+}
+#endif
+
+Uint64 Time_ReadTSC(void)
+{
+       Uint32  a, d;
+       __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
+       return ((Uint64)d << 32) | a;
+}

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