singrdk/base/Kernel/Singularity/Processor.cs

950 lines
31 KiB
C#
Raw Normal View History

2008-03-05 09:52:00 -05:00
////////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Processor.cs
//
// Note:
//
//#define DEBUG_EXCEPTIONS
//#define DEBUG_INTERRUPTS
//#define DEBUG_DISPATCH_TIMER
//#define DEBUG_DISPATCH_IO
2008-11-17 18:29:00 -05:00
//#define CHECK_DISABLE_INTERRUPTS
2008-03-05 09:52:00 -05:00
// #define SINGULARITY_ASMP
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Threading;
2008-11-17 18:29:00 -05:00
using Microsoft.Singularity.Eventing;
2008-03-05 09:52:00 -05:00
using Microsoft.Singularity.Hal;
using Microsoft.Singularity.Io;
2008-11-17 18:29:00 -05:00
using Microsoft.Singularity.Isal;
2008-03-05 09:52:00 -05:00
using Microsoft.Singularity.Memory;
using Microsoft.Singularity.Scheduling;
2008-11-17 18:29:00 -05:00
using Microsoft.Singularity.V1.Threads;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
// For Abi Call
2008-03-05 09:52:00 -05:00
// using Microsoft.Singularity.V1.Services;
namespace Microsoft.Singularity
{
2008-11-17 18:29:00 -05:00
2008-03-05 09:52:00 -05:00
[CLSCompliant(false)]
public enum ProcessorEvent : ushort
{
Exception = 0,
Resume = 1,
Interrupt = 2
}
[CLSCompliant(false)]
[CCtorIsRunDuringStartup]
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("referenced in hal.cpp/processor.cpp")]
2008-03-05 09:52:00 -05:00
public class Processor
{
2008-11-17 18:29:00 -05:00
// Callback object for context switching
static ResumeThreadCallback resumeThreadCallback;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
private ProcessorLogger ProcessorLog = null;
private ProcessorCounter processorCounter = null;
internal SamplingProfiler Profiler = null;
internal bool nextSampleIdle = false;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
internal static bool IsSamplingEnabled = false;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
//
// This is called by HalDevicesApic.StartApProcessors() when
// initializing additional physical AP processors to set its
// hardware state when its started.
//
public void InitializeKernelThreadState(Thread thread,
UIntPtr kernelStackBegin,
UIntPtr kernelStackLimit)
{
kernelThread = thread;
kernelStackBegin = kernelStackBegin;
kernelStackLimit = kernelStackLimit;
}
2008-03-05 09:52:00 -05:00
public HalTimer Timer
{
[NoHeapAllocation]
get { return timer; }
}
2008-11-17 18:29:00 -05:00
public HalClock Clock
{
[NoHeapAllocation]
get { return clock; }
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
internal static void InitializeProcessorTable(int cpus)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
// use the full value initially
ExpectedProcessors = cpus;
processorTable = new Processor[cpus];
2008-03-05 09:52:00 -05:00
for (int i = 0; i < processorTable.Length; i++) {
processorTable[i] = new Processor(i);
}
DebugStub.WriteLine("Processors: {0} of {1}",
2008-11-17 18:29:00 -05:00
__arglist(processorTable.Length, cpus));
2008-03-05 09:52:00 -05:00
}
internal static void AllocateStack(UIntPtr size, out UIntPtr begin, out UIntPtr limit)
{
Kernel.Waypoint(818);
size = MemoryManager.PagePad(size);
limit = MemoryManager.KernelAllocate(
MemoryManager.PagesFromBytes(size), null, 0, System.GCs.PageType.Stack);
begin = limit + size;
Kernel.Waypoint(819);
}
private Processor(int index)
{
processorIndex = index;
2008-11-17 18:29:00 -05:00
2008-03-05 09:52:00 -05:00
if (interruptCounts == null) {
interruptCounts = new int [256];
}
2008-11-17 18:29:00 -05:00
ProcessorLog = ProcessorLogger.Create("ProcessorLogger:"+index.ToString());
processorCounter = ProcessorCounter.Create("ProcessorCounters:"+index.ToString(), 256);
DebugStub.WriteLine("Processor: {0}", __arglist(index));
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
public void EnableProfiling()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
if (SamplingEnabled()) {
Profiler = SamplingProfiler.Create("SampleProfiler:" + Id.ToString(),
32, // maximum stack depth
Kernel.ProfilerBufferSize); // sampling buffer size
DebugStub.WriteLine("Sampling profiler enabled");
}
}
public static Processor EnableProcessor(int processorId)
{
Processor p = processorTable[processorId];
p.Initialize(processorId);
return p;
}
private unsafe void Initialize(int processorId)
{
uint DefaultStackSize = 0xA000;
2008-03-05 09:52:00 -05:00
processorTable[processorId] = this;
2008-11-17 18:29:00 -05:00
context = (ProcessorContext*) Isa.GetCurrentCpu();
DebugStub.WriteLine("Processor context: {0} {1:x8}",
__arglist(processorId, Kernel.AddressOf(context)));
2008-03-05 09:52:00 -05:00
context->UpdateAfterGC(this);
2008-11-17 18:29:00 -05:00
if (0 != processorId) {
Thread.BindKernelThread(kernelThread,
kernelStackBegin,
kernelStackLimit);
}
AllocateStack(DefaultStackSize,
out context->cpuRecord.interruptStackBegin,
out context->cpuRecord.interruptStackLimit);
2008-03-05 09:52:00 -05:00
Tracing.Log(Tracing.Debug, "Initialized Processor {0}",
(UIntPtr)processorId);
2008-11-17 18:29:00 -05:00
2008-03-05 09:52:00 -05:00
Tracing.Log(Tracing.Debug, "asmInterruptStack={0:x}..{1:x}",
2008-11-17 18:29:00 -05:00
context->cpuRecord.interruptStackBegin,
context->cpuRecord.interruptStackLimit);
#if false
DebugStub.WriteLine("proc{0}: InterruptStack={1:x}..{2:x}",
__arglist(
processorId,
context->cpuRecord.interruptStackBegin,
context->cpuRecord.interruptStackLimit
));
#endif
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
Interlocked.Increment(ref runningCpus);
2008-03-05 09:52:00 -05:00
MpExecution.AddProcessorContext(context);
2008-11-17 18:29:00 -05:00
// Need to allocate this callback object outside of NoThreadAllocation region
if (processorId == 0) {
resumeThreadCallback = new ResumeThreadCallback();
}
Isa.EnableCycleCounter();
}
///
/// <summary>
/// Initialize dispatcher
/// </summary>
///
/// <param name="processorId">Id of the processor dispatcher belongs to</param>
///
public static void InitializeDispatcher(int processorId)
{
// Create a processor dispatcher
processorTable[processorId].dispatcher = new ProcessorDispatcher();
// Initialize dispatcher
processorTable[processorId].dispatcher.Initialize(processorTable[processorId]);
}
///
/// <summary>
/// Activate Timer
/// </summary>
///
///
public static void ActivateTimer(int processorId)
{
processorTable[processorId].timer.SetNextInterrupt(TimeSpan.FromMilliseconds(5));
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
public void Uninitialize(int processorId)
{
Tracing.Log(Tracing.Debug, "UnInitializing Processor {0}",
(UIntPtr)processorId);
Interlocked.Decrement(ref runningCpus);
2008-11-17 18:29:00 -05:00
// #if DEBUG
// Interrupts should be off now
if (!InterruptsDisabled()) {
DebugStub.WriteLine("Processor::Uninitialize AP Processor does not have interrupts disabled\n");
DebugStub.Break();
}
// #endif // DBG
2008-03-05 09:52:00 -05:00
// Processor is out of commission
HaltUntilInterrupt();
2008-11-17 18:29:00 -05:00
// #if DEBUG
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
DebugStub.WriteLine("Processor::Uninitialize: AP processor woke up on shutdown!\n");
DebugStub.Break();
// #endif // DBG
2008-03-05 09:52:00 -05:00
}
public void AddPic(HalPic pic)
{
Tracing.Log(Tracing.Audit, "AddPic({0})\n",
Kernel.TypeName(pic));
this.pic = pic;
2008-11-17 18:29:00 -05:00
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
[NoHeapAllocation]
public HalPic GetPic()
{
return this.pic;
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
public void AddTimer(byte interrupt, HalTimer timer)
{
Tracing.Log(Tracing.Audit, "AddTimer({0}) on {1}\n",
Kernel.TypeName(timer), interrupt);
this.timer = timer;
this.timerInterrupt = interrupt;
}
[NoHeapAllocation]
public void AddClock(byte interrupt, HalClock clock)
{
Tracing.Log(Tracing.Audit, "AddClock({0}) on {1}\n",
Kernel.TypeName(clock), interrupt);
this.clock = clock;
this.clockInterrupt = interrupt;
}
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
public static void AddMemory(HalMemory aHalMemory)
2008-03-05 09:52:00 -05:00
{
Tracing.Log(Tracing.Audit, "AddHalMemory({0})\n",
Kernel.TypeName(aHalMemory));
halMemory = aHalMemory;
}
[NoHeapAllocation]
internal unsafe void Display()
{
int stackVariable;
UIntPtr currentStack = new UIntPtr(&stackVariable);
unchecked {
Tracing.Log(Tracing.Debug, "Interrupt stack: {0:x} {1:x}..{2:x} uses",
currentStack,
2008-11-17 18:29:00 -05:00
context->cpuRecord.interruptStackBegin,
context->cpuRecord.interruptStackLimit);
2008-03-05 09:52:00 -05:00
}
}
// Returns the processor that the calling thread is running on.
2008-11-17 18:29:00 -05:00
// TODO:Needs to be fixed. (Added for consistency)
2008-03-05 09:52:00 -05:00
public static Processor CurrentProcessor
{
[NoHeapAllocation]
get { return GetCurrentProcessor(); }
}
2008-11-17 18:29:00 -05:00
///
/// <summary>
/// Retrieve current dispatcher
/// </summary>
public ProcessorDispatcher Dispatcher
{
[NoHeapAllocation]
get { return dispatcher; }
}
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
public static int GetCurrentProcessorId()
{
return GetCurrentProcessor().Id;
}
public unsafe int Id
{
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
get {
return context->cpuRecord.id;
}
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
public static Processor GetProcessor(int i)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
if (null == processorTable && i == 0) {
return CurrentProcessor;
}
else {
return processorTable[i];
}
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
public static int CpuCount
2008-03-05 09:52:00 -05:00
{
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
get { return null == processorTable ? 1 : processorTable.Length; }
}
[NoHeapAllocation]
public static void HaltUntilInterrupt()
{
Platform.ThePlatform.Halt();
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
public int GetInterruptCount(byte interrupt)
{
return interruptCounts[interrupt];
}
public int GetIrqCount(byte irq)
{
HalPic pic = CurrentProcessor.pic;
2008-11-17 18:29:00 -05:00
// Only set on native hal
if (pic == null) {
return 0;
}
2008-03-05 09:52:00 -05:00
return interruptCounts[pic.IrqToInterrupt(irq)];
}
public static byte GetMaxIrq()
{
2008-11-17 18:29:00 -05:00
HalPic pic = CurrentProcessor.pic;
// This is not set on halhyper or halwin32
if (pic == null) {
return 0;
}
2008-03-05 09:52:00 -05:00
return CurrentProcessor.pic.MaximumIrq;
}
public static ulong CyclesPerSecond
{
[NoHeapAllocation]
get { return GetCurrentProcessor().cyclesPerSecond; }
[NoHeapAllocation]
set { GetCurrentProcessor().cyclesPerSecond = value; }
}
public static ulong CycleCount
{
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
get { return Isa.GetCycleCount(); }
2008-03-05 09:52:00 -05:00
}
//////////////////////////////////////////////////////////////////////
//
//
[NoHeapAllocation]
public static bool SamplingEnabled()
{
2008-11-17 18:29:00 -05:00
return (Kernel.ProfilerBufferSize != 0);
2008-03-05 09:52:00 -05:00
}
internal static void StartSampling()
{
2008-11-17 18:29:00 -05:00
if (SamplingEnabled()) {
IsSamplingEnabled = true;
}
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal void NextSampleIsIdle()
2008-03-05 09:52:00 -05:00
{
nextSampleIdle = true;
}
//////////////////////////////////////////////////// External Methods.
//
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static Processor GetCurrentProcessor()
{
unsafe {
return GetCurrentProcessorContext()->processor;
}
}
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("output to header : called by c code")]
internal static unsafe ThreadContext * GetCurrentThreadContext()
{
unsafe {
return (ThreadContext *) Isa.GetCurrentThread();
}
}
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("output to header : called by c code")]
internal static unsafe ProcessorContext * GetCurrentProcessorContext()
{
unsafe {
return (ProcessorContext *) Isa.GetCurrentCpu();
}
}
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static Thread GetCurrentThread()
{
unsafe {
return GetCurrentThreadContext()->thread;
}
}
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static void SetCurrentThreadContext(ref ThreadContext context)
{
unsafe {
fixed (ThreadContext *c = &context) {
Isa.SetCurrentThread(ref c->threadRecord);
}
}
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
///
/// <summary>
/// Verify if thread currently is running on interrupt stack
/// </summary>
///
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
[Inline]
internal bool IsOnInterruptStack(Thread currentThread)
{
return Isa.IsRunningOnInterruptStack;
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
internal bool InInterruptContext
{
[NoHeapAllocation]
get {
return Isa.InInterruptContext;
}
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("defined in halforgc.asm")]
2008-03-05 09:52:00 -05:00
[MethodImpl(MethodImplOptions.InternalCall)]
2008-11-17 18:29:00 -05:00
[GCAnnotation(GCOption.GCFRIEND)]
2008-03-05 09:52:00 -05:00
[StackBound(32)]
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static extern void SwitchToThreadContext(ref ThreadContext oldContext, ref ThreadContext newContext);
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
private class ResumeThreadCallback : Isa.ICallback
{
internal override UIntPtr Callback(UIntPtr param)
{
unsafe {
ThreadContext* newContext = (ThreadContext *) param;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
// Switch our thread context, synchronizing with the dispatcher as necessary.
ProcessorDispatcher.TransferToThreadContext(ref *GetCurrentThreadContext(),
ref *newContext);
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
// Resume in the new context. Note that this call does not return.
newContext->threadRecord.spill.Resume();
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
return 0;
}
}
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("referenced by halforgc.asm")]
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
[GCAnnotation(GCOption.NOGC)]
2008-11-17 18:29:00 -05:00
internal static unsafe void SwitchToThreadContextNoGC(ref ThreadContext newContext)
{
// Interrupts should be disabled at this point
VTable.Assert(Processor.InterruptsDisabled());
// Save appears to returns twice: once with true on this thread after
// the save, and once with false when the context is restored.
if (GetCurrentThreadContext()->threadRecord.spill.Save()) {
// Initial return from save; time to swap in the new context.
// Must do this on the interrupt stack, since once we release the
// dispatch lock the saved context is free to run (and we would
// be on the same stack.)
fixed (ThreadContext *c = &newContext) {
// Note that this does not return.
Isa.CallbackOnInterruptStack(resumeThreadCallback, (UIntPtr) c);
}
}
// Saved context will resume here
}
2008-03-05 09:52:00 -05:00
[MethodImpl(MethodImplOptions.InternalCall)]
[GCAnnotation(GCOption.NOGC)]
[StackBound(32)]
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static extern void TestSaveLoad(ref ThreadContext newContext);
2008-03-05 09:52:00 -05:00
[MethodImpl(MethodImplOptions.InternalCall)]
[GCAnnotation(GCOption.NOGC)]
[StackBound(32)]
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal static extern void TestSave(ref ThreadContext newContext);
2008-03-05 09:52:00 -05:00
//////////////////////////////////////////////////////////////////////
//
// These methods are currently marked external because they are used
// by device drivers. We need a tool to verify that device drivers
// are in fact using them correctly!
//
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("accessed by C++")]
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
[GCAnnotation(GCOption.NOGC)]
2008-11-17 18:29:00 -05:00
public static bool DisableLocalPreemption()
{
return DisableInterrupts();
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("accessed by C++")]
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
[GCAnnotation(GCOption.NOGC)]
2008-11-17 18:29:00 -05:00
public static void RestoreLocalPreemption(bool enabled)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
RestoreInterrupts(enabled);
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("accessed by C++")]
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
[GCAnnotation(GCOption.NOGC)]
public static bool DisableInterrupts()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
#if CHECK_DISABLE_INTERRUPTS
bool wasDisabled = InterruptsDisabled();
#endif
bool result = Isa.DisableInterrupts();
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
#if CHECK_DISABLE_INTERRUPTS
if (result && wasDisabled) {
DebugStub.Break();
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
#endif
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
return result;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
[AccessedByRuntime("accessed by C++")]
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
[GCAnnotation(GCOption.NOGC)]
public static void RestoreInterrupts(bool enabled)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
int i = 0;
try {
#if CHECK_DISABLE_INTERRUPTS
if (!InterruptsDisabled()) {
2008-03-05 09:52:00 -05:00
DebugStub.Break();
}
#endif
2008-11-17 18:29:00 -05:00
i = 1;
if (enabled) {
i = 2;
// Processor flag should be turned off before this call
if (GetCurrentProcessor().InInterruptContext) {
2008-03-05 09:52:00 -05:00
DebugStub.Break();
2008-11-17 18:29:00 -05:00
}
i = 3;
Isa.EnableInterrupts();
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
i = 5;
#if CHECK_DISABLE_INTERRUPTS
if (enabled && InterruptsDisabled()) {
2008-03-05 09:52:00 -05:00
DebugStub.Break();
}
#endif
}
2008-11-17 18:29:00 -05:00
catch(Exception e) {
DebugStub.Break();
2008-03-05 09:52:00 -05:00
}
}
2008-11-17 18:29:00 -05:00
// Use this method for assertions only!
[AccessedByRuntime("accessed by C++")]
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
[GCAnnotation(GCOption.NOGC)]
public static bool InterruptsDisabled()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
return Platform.ThePlatform.AreInterruptsDisabled();
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
public unsafe void IncrementInterruptCounts(int interrupt)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
NumInterrupts++;
NumExceptions++;
interruptCounts[interrupt]++;
if (ProcessorLog != null) {
// ProcessorLog.Log(interrupt);
processorCounter.Buffer[interrupt].Hits += 1;
}
2008-03-05 09:52:00 -05:00
}
[NoHeapAllocation]
internal static unsafe void UpdateAfterGC(Thread currentThread)
{
// Update the processor pointers in processor contexts
for (int i = 0; i < Processor.processorTable.Length; i++) {
Processor p = Processor.processorTable[i];
if (p != null) {
p.context->UpdateAfterGC(p);
}
}
// Ensure that Thread.CurrentThread returns new thread object
SetCurrentThreadContext(ref currentThread.context);
}
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
internal void ActivateDispatcher()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
if (Platform.Cpu(processorIndex) != null){
// Wake processor
Platform.ThePlatform.WakeNow(processorIndex);
}
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
///
2008-03-05 09:52:00 -05:00
/// <summary>
2008-11-17 18:29:00 -05:00
/// Set next alarm: timer that we are interested in
2008-03-05 09:52:00 -05:00
/// </summary>
2008-11-17 18:29:00 -05:00
///
/// <param name="delta">Time until the next time we would like to be awaken</param>
///
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
2008-11-17 18:29:00 -05:00
public void SetNextTimerInterrupt(TimeSpan delta)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
// Make sure that interrupts are disabled
bool iflag = Processor.DisableInterrupts();
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
TimeSpan start = delta;
if (delta < timer.MinInterruptInterval) {
delta = timer.MinInterruptInterval;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
if (delta > timer.MaxInterruptInterval) {
delta = timer.MaxInterruptInterval;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
#if false
DebugStub.WriteLine("-- SetNextTimerInterrupt(delta={0}, start={1} [min={2},max={3})",
__arglist(delta.Ticks,
start.Ticks,
timer.MinInterruptInterval.Ticks,
timer.MaxInterruptInterval.Ticks));
#endif
timer.SetNextInterrupt(delta);
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
// Restore interrupts if necessary
Processor.RestoreInterrupts(iflag);
2008-03-05 09:52:00 -05:00
}
//////////////////////////////////////////////////////////////////////
//
// These (native) methods manipulate the local processor's paging
// hardware. They can be used even before Processor.Initialize()
// has been called.
//
internal static void EnablePaging(AddressSpace bootstrapSpace)
{
2008-11-17 18:29:00 -05:00
Isa.EnablePaging((uint)bootstrapSpace.PdptPage.Value);
2008-03-05 09:52:00 -05:00
}
internal static void ChangeAddressSpace(AddressSpace space)
{
2008-11-17 18:29:00 -05:00
Isa.ChangePageTableRoot((uint)space.PdptPage.Value);
2008-03-05 09:52:00 -05:00
}
internal static void InvalidateTLBEntry(UIntPtr pageAddr)
{
DebugStub.Assert(MemoryManager.IsPageAligned(pageAddr));
2008-11-17 18:29:00 -05:00
Isa.InvalidateTLBEntry(pageAddr);
2008-03-05 09:52:00 -05:00
}
internal static AddressSpace GetCurrentAddressSpace()
{
2008-11-17 18:29:00 -05:00
return new AddressSpace(new PhysicalAddress(Isa.GetPageTableRoot()));
2008-03-05 09:52:00 -05:00
}
//
2008-11-17 18:29:00 -05:00
public static HalMemory.ProcessorAffinity[] GetProcessorAffinity()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
HalMemory.ProcessorAffinity[] processors =
2008-03-05 09:52:00 -05:00
halMemory.GetProcessorAffinity();
return processors;
}
2008-11-17 18:29:00 -05:00
public static HalMemory.MemoryAffinity[] GetMemoryAffinity()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
HalMemory.MemoryAffinity[] memories =
2008-03-05 09:52:00 -05:00
halMemory.GetMemoryAffinity();
return memories;
}
2008-11-17 18:29:00 -05:00
public static unsafe void EnableMoreProcessors(int cpus)
{
ExpectedProcessors = cpus;
Platform.ThePlatform.EnableMoreCpus(cpus);
StartApProcessors(cpus);
}
2008-03-05 09:52:00 -05:00
/// <summary> Start application processors. </summary>
[System.Diagnostics.Conditional("SINGULARITY_MP")]
2008-11-17 18:29:00 -05:00
public static void StartApProcessors(int cpuCount)
2008-03-05 09:52:00 -05:00
{
// At this point only the BSP is running.
Tracing.Log(Tracing.Debug, "Processor.StartApProcessors()");
2008-11-17 18:29:00 -05:00
Platform.StartApProcessors(cpuCount);
2008-03-05 09:52:00 -05:00
// At this point the BSP and APs are running.
}
/// <summary> Stop application processors. </summary>
[NoHeapAllocation]
[System.Diagnostics.Conditional("SINGULARITY_MP")]
public static void StopApProcessors()
{
2008-11-17 18:29:00 -05:00
//
// Note: This should go into a HAL interface and this
// code confined to Platform.cs
//
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
// At this point the BSP and APs are running.
2008-03-05 09:52:00 -05:00
Tracing.Log(Tracing.Debug, "Processor.StopApProcessors()");
2008-11-17 18:29:00 -05:00
if (Processor.GetRunningProcessorCount() > 1) {
//
// This stops them in MpExecution in a halt state with
// interrupts off.
//
Platform.BroadcastFixedIPI((byte)Isal.IX.EVectors.HaltApProcessors, true);
}
2008-03-05 09:52:00 -05:00
while (GetRunningProcessorCount() != 1) {
2008-11-17 18:29:00 -05:00
// Thread.Sleep(100); Thread.Sleep needs NoHeapAllocation annotation
2008-03-05 09:52:00 -05:00
Thread.Yield();
}
2008-11-17 18:29:00 -05:00
//
// We must reset the AP Processors since a debug entry
// will generated a NMI which will wake them up from HALT,
// and they may start executing code again while the kernel
// is still shutting down.
//
Platform.ResetApProcessors();
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
DebugStub.RevertToUniprocessor();
2008-03-05 09:52:00 -05:00
// At this point only the BSP is running.
}
/// <summary> Gets the number of processors in use by
/// the system. </summary>
[NoHeapAllocation]
public static int GetRunningProcessorCount()
{
return runningCpus;
}
/// <summary> Gets the total number of processors known
/// to the system. This includes processors not
/// currently in use by the system. </summary>
[NoHeapAllocation]
public static int GetProcessorCount()
{
2008-11-17 18:29:00 -05:00
return Platform.GetProcessorCount();
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
//
//public static int GetDomainCount()
//{
// HalMemory.ProcessorAffinity[] processors =
// halMemory.GetProcessorAffinity();
// uint domain = 0;
// for (int i = 0; i < processors.Length; i++) {
// if (processors[i].domain > domain) {
// domain = processors[i].domain;
// }
// }
// domain++; // domain number starts from 0
// return (int)domain;
//}
//
public static bool PerProcessorAddressSpaceDisabled()
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
return halMemory.PerProcessorAddressSpaceDisabled();
2008-03-05 09:52:00 -05:00
}
public static bool HasAffinityInfo()
{
2008-11-17 18:29:00 -05:00
HalMemory.ProcessorAffinity[] processors = halMemory.GetProcessorAffinity();
HalMemory.MemoryAffinity[] memories = halMemory.GetMemoryAffinity();
2008-03-05 09:52:00 -05:00
if (processors == null || memories == null) {
return false;
}
return true;
}
2008-11-17 18:29:00 -05:00
// return cpuId if context is not null
2008-03-05 09:52:00 -05:00
[NoHeapAllocation]
public unsafe int ValidProcessorId(int i)
{
if (context != null) {
2008-11-17 18:29:00 -05:00
return context->cpuRecord.id;
2008-03-05 09:52:00 -05:00
}
else {
return -1;
}
}
2008-11-17 18:29:00 -05:00
///
/// <summary>
/// Is system MP or UP
/// </summary>
///
public static bool IsUP
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
[NoHeapAllocation]
get
{
return CpuCount == 1;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
}
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
///<summary> Count of the number of processors expected to be running </summary>
public static int ExpectedProcessors;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
///<summary> Global processor table </summary>
public static Processor[] processorTable;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Processor contexts </summary>
internal unsafe ProcessorContext* context;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
///<summary> Processor dispatcher </summary>
[AccessedByRuntime("referenced from c++")]
internal ProcessorDispatcher dispatcher;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
//<summary> Hardware pic, or its emulation </summary>
private HalPic pic;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Per processor HalTimer </summary>
internal HalTimer timer = null;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Per processor HalClock </summary>
internal HalClock clock = null;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Hal memory interface </summary>
private static HalMemory halMemory;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Id of a timer interrupt </summary>
internal byte timerInterrupt;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Id of a clock interrupt</summary>
internal byte clockInterrupt;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Shows if a processor currently in a context of interrupt </summary>
private bool inInterruptContext = false;
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
/// <summary> Shows if a processor currently in halt state </summary>
private bool halted = false;
///<summary> Processor Index </summary>
private int processorIndex;
/// <summary> A number of exception occured on this processor </summary>
public uint NumExceptions = 0;
/// <summary> A number of interrupts occrued on this processor </summary>
public uint NumInterrupts = 0;
/// <summary> A number of context switches </summary>
public uint NumContextSwitches = 0;
/// <summary> A interrupt statistics per processor </summary>
internal int[] interruptCounts;
/// <summary> A number of cycles per second on a given processor </summary>
private ulong cyclesPerSecond;
/// <summary> A number of active Processors </summary>
private static int runningCpus = 0;
/// <summary> An initial per processor kernel thread </summary>
private Thread kernelThread;
/// <summary>Beginning of kernel thread stack </summary>
private UIntPtr kernelStackBegin = 0;
/// <summary> Limit of kernel thread stack </summary>
private UIntPtr kernelStackLimit = 0;
2008-03-05 09:52:00 -05:00
}
}