Exemple #1
0
 private bool Wait(WakeUp wakeUp)
 {
     try
     {
         CompilerIntrinsics.Cli();
         if (waitSemaphore == null)
         {
             waitSemaphore = Kernel.kernel.NewSemaphore(0);
         }
         uint depth = lockDepth;
         uint cur   = Kernel.CurrentThread;
         lockHolder = 0xffffffff;
         lockDepth  = 0;
         lockSemaphore.SignalInterruptsDisabled();
         bool gotIt = waitSemaphore.WaitInterruptsDisabled(wakeUp);
         lockSemaphore.WaitInterruptsDisabled();
         lockDepth  = depth;
         lockHolder = cur;
         return(gotIt);
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #2
0
    internal static long GetUtcTimeInterruptsDisabled()
    {
        CompilerIntrinsics.Cli(); // TODO: superfluous
        long tsc = NucleusCalls.Rdtsc();

        return(unchecked (tsc >> 8));
    }
Exemple #3
0
 internal bool WaitInterruptsDisabled(WakeUp wakeUp)
 {
     CompilerIntrinsics.Cli(); // TODO: superfluous
     capacity--;
     if (capacity < 0)
     {
         if (wakeUp != null)
         {
             if (wakeUp.time <= 0)
             {
                 return(false);
             }
             uint cur = Kernel.CurrentThread;
             wakeUp.thread        = Kernel.kernel.threadTable[cur];
             wakeUp.queue         = waiters;
             wakeUp.thread.wakeUp = wakeUp;
         }
         Kernel.kernel.EnqueueAndYield(waiters);
         if (wakeUp != null)
         {
             if (wakeUp.thread.wakeUp == null)
             {
                 // We timed out
                 return(false);
             }
             wakeUp.thread.wakeUp = null;
         }
     }
     return(true);
 }
Exemple #4
0
 internal static void Throw()
 {
     CompilerIntrinsics.Cli();
     NucleusCalls.Throw();
     while (true)
     {
     }
 }
Exemple #5
0
 internal void WaitInterruptsDisabled()
 {
     CompilerIntrinsics.Cli(); // TODO: superfluous
     capacity--;
     if (capacity < 0)
     {
         Kernel.kernel.EnqueueAndYield(waiters);
     }
 }
Exemple #6
0
 public uint Read32(int offset)
 {
     try {
         CompilerIntrinsics.Cli();
         return(NucleusCalls.PciMemRead32(id, (uint)offset));
     }
     finally {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #7
0
 public void Write32(int offset, uint val)
 {
     try {
         CompilerIntrinsics.Cli();
         NucleusCalls.PciMemWrite32(id, (uint)offset, val);
     }
     finally {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #8
0
    private void KernelMain()
    {
        CompilerIntrinsics.Cli(); // TODO: superfluous
        for (uint i = 0; i < 50 * 80; i++)
        {
            NucleusCalls.VgaTextWrite(i, 0);
        }

        Thread init = new Thread(new InitThread(), true);

        _NewThread(init);

        uint x = 0xe100;
        uint y = 0;

        while (true)
        {
            CompilerIntrinsics.Cli(); // TODO: superfluous
            NucleusCalls.VgaTextWrite(79, x);
            x++;

            // Schedule thread, wait for exception or interrupt
            ScheduleNextThread();

            // CurrentThread received exception, interrupt, or exited voluntarily
            uint   cid = CurrentThread;
            Thread t   = threadTable[cid];

            CompilerIntrinsics.Cli(); // TODO: superfluous
            uint cState = NucleusCalls.GetStackState(cid);
            if (cState == STACK_EMPTY)
            {
                // Thread received an exception.  Kill it.
                t.alive = false;
            }

            CompilerIntrinsics.Cli(); // TODO: superfluous
            if (t.alive)
            {
                // Thread was interrupted.  It's still ready to run.
                NucleusCalls.SendEoi();
                CheckWakeUp();
                CompilerIntrinsics.Cli(); // TODO: superfluous
                NucleusCalls.StartTimer(0);
                readyQueue.Enqueue(t);
            }
            else
            {
                NucleusCalls.VgaTextWrite(78, (uint)(0x5b00 + 0x1100 * (y++) + 48 + cid));
                // Thread is dead.  Dead threads always jump back to stack 0.
                threadTable[cid] = null;
                NucleusCalls.ResetStack(cid);
            }
        }
    }
Exemple #9
0
/*TODO
 *      public byte Read8(int offset) {
 *          return (byte)(Read32(offset));
 *      }
 *
 *      public ushort Read16(int offset) {
 *          return (ushort)(Read32(offset));
 *      }
 */

        public uint Read32(int offset)
        {
            System.VTable.Assert((offset & 3) == 0);
            try {
                CompilerIntrinsics.Cli();
                return(NucleusCalls.PciConfigRead32(id, (uint)offset));
            }
            finally {
                CompilerIntrinsics.Sti();
            }
        }
Exemple #10
0
 internal static void Print(char c)
 {
     CompilerIntrinsics.Cli();
     NucleusCalls.VgaTextWrite(offset, (uint)(0x1e00 | c));
     offset++;
     if (offset == 40 * 80)
     {
         offset = 80;
     }
     NucleusCalls.VgaTextWrite(offset, (uint)(0x3b00 | 33));
     CompilerIntrinsics.Sti();
 }
Exemple #11
0
 internal static uint TryReadKeyboard()
 {
     try
     {
         CompilerIntrinsics.Cli();
         return(NucleusCalls.TryReadKeyboard());
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #12
0
 internal static void DebugPrintHex(uint screenOffset, uint hexMessage)
 {
     try
     {
         CompilerIntrinsics.Cli();
         NucleusCalls.DebugPrintHex(screenOffset, hexMessage);
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #13
0
 public void Reset()
 {
     try
     {
         CompilerIntrinsics.Cli();
         isSet = false;
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #14
0
 internal static long Rdtsc()
 {
     try
     {
         CompilerIntrinsics.Cli();
         return(NucleusCalls.Rdtsc());
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #15
0
 internal PciMemory(uint id)
 {
     this.id = id;
     try {
         CompilerIntrinsics.Cli();
         size = (int)NucleusCalls.PciMemSetup(id);
     }
     finally {
         CompilerIntrinsics.Sti();
     }
     System.DebugStub.Print("PCI size = 0x" + size.ToString("X") + ". ");
 }
Exemple #16
0
 internal static void VgaTextWrite(uint position, uint data)
 {
     try
     {
         CompilerIntrinsics.Cli();
         NucleusCalls.VgaTextWrite(position, data);
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #17
0
 private static Array AllocateVectorInterruptsDisabled(VTable vtable, int len)
 {
     CompilerIntrinsics.Cli(); // TODO: superfluous
     for (;;)
     {
         Array o = AllocateVector(vtable, len);
         if (o != null)
         {
             return(o);
         }
         throw null;
     }
 }
Exemple #18
0
 private static Object AllocateObjectInterruptsDisabled(VTable vtable)
 {
     CompilerIntrinsics.Cli(); // TODO: superfluous
     for (;;)
     {
         Object o = AllocateObject(vtable);
         if (o != null)
         {
             return(o);
         }
         throw null;
     }
 }
Exemple #19
0
        internal void SignalInterruptsDisabled()
        {
            CompilerIntrinsics.Cli(); // TODO: superfluous
            capacity++;
            Thread t = waiters.Dequeue();

            if (t != null)
            {
                ThreadQueue ready = Kernel.kernel.readyQueue;
                ready.Enqueue(t);
                Kernel.kernel.EnqueueAndYield(ready);
            }
        }
Exemple #20
0
    public Thread NewThread(ThreadStart start)
    {
        Thread t = new Thread(start);

        try
        {
            CompilerIntrinsics.Cli();
            return(_NewThread(t));
        }
        finally
        {
            CompilerIntrinsics.Sti();
        }
    }
Exemple #21
0
 private static Array AllocateVectorInterruptsEnabled(VTable vtable, int len)
 {
     CompilerIntrinsics.Cli();
     for (;;)
     {
         Array o = AllocateVector(vtable, len);
         if (o != null)
         {
             CompilerIntrinsics.Sti();
             return(o);
         }
         Kernel.Collect();
         CompilerIntrinsics.Cli(); // TODO: superfluous
     }
 }
Exemple #22
0
 private static Object AllocateObjectInterruptsEnabled(VTable vtable)
 {
     CompilerIntrinsics.Cli();
     for (;;)
     {
         Object o = AllocateObject(vtable);
         if (o != null)
         {
             CompilerIntrinsics.Sti();
             return(o);
         }
         Kernel.Collect();
         CompilerIntrinsics.Cli(); // TODO: superfluous
     }
 }
Exemple #23
0
    private void ThreadMain(uint id)
    {
        Thread t = threadTable[id];

        CompilerIntrinsics.Sti();
        t.start.Run();
        CompilerIntrinsics.Cli();
        t.alive = false;
        NucleusCalls.YieldTo(0);

        // Should never be reached:
        NucleusCalls.DebugPrintHex(0, 0xdead0002);
        while (true)
        {
        }
    }
Exemple #24
0
 public void Wait()
 {
     try
     {
         CompilerIntrinsics.Cli();
         capacity--;
         if (capacity < 0)
         {
             Kernel.kernel.EnqueueAndYield(waiters);
         }
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #25
0
    public override void Run()
    {
        int nIter = 1048576;

        if (me == 0)
        {
            myId = Kernel.CurrentThread;
            Thread otherT  = kernel.NewThread(other);
            uint   otherId = otherT.id;
            kernel.Yield();
            try
            {
                CompilerIntrinsics.Cli();
                NucleusCalls.DebugPrintHex(50, 0);
                long t1 = NucleusCalls.Rdtsc();
                for (int i = 0; i < nIter; i++)
                {
                    NucleusCalls.YieldTo(otherId);
                }
                long t2   = NucleusCalls.Rdtsc();
                uint diff = (uint)((t2 - t1) >> 20);
                NucleusCalls.DebugPrintHex(50, diff);
            }
            finally
            {
                CompilerIntrinsics.Sti();
            }
            doneSemaphore.Signal();
        }
        else
        {
            uint otherId = other.myId;
            kernel.Yield();
            try
            {
                CompilerIntrinsics.Cli();
                for (int i = 0; i < nIter; i++)
                {
                    NucleusCalls.YieldTo(otherId);
                }
            }
            finally
            {
                CompilerIntrinsics.Sti();
            }
        }
    }
Exemple #26
0
 public bool TryWait()
 {
     try
     {
         CompilerIntrinsics.Cli();
         if (capacity <= 0)
         {
             return(false);
         }
         capacity--;
         return(true);
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #27
0
 internal static void Setup()
 {
     System.DebugStub.Print("Dma.Setup 1. ");
     try {
         CompilerIntrinsics.Cli();
         ioMemory   = NucleusCalls.PciDmaBuffer();
         ioPhysical = NucleusCalls.PciDmaPhysicalAddr();
         // The IO-MMU tables map 2MB, aligned by 2MB
         uint superPageSize = 0x200000;
         uint ioSuperPage   = (ioPhysical + 2 * superPageSize) - (ioPhysical & (superPageSize - 1));
         allocOffset = (int)(ioSuperPage - ioPhysical);
     }
     finally {
         CompilerIntrinsics.Sti();
     }
     System.DebugStub.Print("Dma.Setup 2. ioPhysical = 0x" + ioPhysical.ToString("X") + " allocOffset = 0x" + allocOffset.ToString("X") + ". ");
 }
Exemple #28
0
 public void Set()
 {
     try
     {
         CompilerIntrinsics.Cli();
         isSet = true;
         while (waitSemaphore.capacity != 0)
         {
             // signal a waiter
             waitSemaphore.SignalInterruptsDisabled();
         }
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #29
0
 public void Release()
 {
     try
     {
         CompilerIntrinsics.Cli();
         if (lockDepth > 0)
         {
             lockDepth--;
             return;
         }
         lockHolder = 0xffffffff;
         lockSemaphore.SignalInterruptsDisabled();
     }
     finally
     {
         CompilerIntrinsics.Sti();
     }
 }
Exemple #30
0
 internal DmaMemory(int length)
 {
     System.VTable.Assert(length >= 0);
     try {
         CompilerIntrinsics.Cli();
         this.offset = allocOffset;
         this.length = length;
         if (allocOffset + length <= ioMemory.Length)
         {
             allocOffset += length;
         }
     } finally {
         CompilerIntrinsics.Sti();
     }
     if (offset + length > ioMemory.Length)
     {
         throw new System.Exception("DmaMemory");
     }
 }