2008-03-05 09:52:00 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Microsoft Research Singularity
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
//
|
|
|
|
// This file is an implementation of Interfaces/Hal/HalClock.csi
|
|
|
|
//
|
|
|
|
// Having some form of time working when one or processors may be in the
|
|
|
|
// debugger makes this hard and expensive.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if SINGULARITY_MP
|
|
|
|
#error "This file is not for MP builds."
|
|
|
|
#endif // SINGULARITY_MP
|
|
|
|
|
|
|
|
namespace Microsoft.Singularity.Hal
|
|
|
|
{
|
|
|
|
using Microsoft.Singularity.Hal.Acpi;
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
public class HalClockApic : HalClock
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
Apic apic;
|
|
|
|
PMClock pmClock;
|
2008-11-17 18:29:00 -05:00
|
|
|
RTClockApic rtClock;
|
2008-03-05 09:52:00 -05:00
|
|
|
HpetClock hpetClock;
|
|
|
|
|
|
|
|
long lastKernelTicks;
|
|
|
|
ulong tscSnapshot;
|
|
|
|
bool tscSnapshotValid = false;
|
|
|
|
long ticksSnapshot;
|
|
|
|
int tickScale = 0;
|
|
|
|
const int tickRoll = 24;
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
internal HalClockApic(Apic apic, RTClockApic rtClock, PMClock /* ! */ pmClock)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
this.apic = apic;
|
|
|
|
this.rtClock = rtClock;
|
|
|
|
this.pmClock = pmClock;
|
|
|
|
this.hpetClock = null;
|
|
|
|
this.tscSnapshot = 0;
|
|
|
|
this.ticksSnapshot = 0;
|
|
|
|
int ticksPerKernelTick =
|
|
|
|
(int)(Processor.CyclesPerSecond / 10000000);
|
|
|
|
this.tickScale = (1 << tickRoll) / ticksPerKernelTick;
|
|
|
|
lastKernelTicks = InternalGetKernelTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
[NoHeapAllocation]
|
2008-11-17 18:29:00 -05:00
|
|
|
public override long GetKernelTicks()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
bool en = Processor.DisableInterrupts();
|
|
|
|
try {
|
|
|
|
long kernelTicks = InternalGetKernelTicks();
|
|
|
|
if (kernelTicks >= lastKernelTicks ||
|
|
|
|
(kernelTicks < 0 && lastKernelTicks > 0)) {
|
|
|
|
lastKernelTicks = kernelTicks;
|
|
|
|
return kernelTicks;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Skew from switching between tsc and underlying clock
|
|
|
|
DebugStub.Assert(lastKernelTicks - kernelTicks < 5000000);
|
|
|
|
return lastKernelTicks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Processor.RestoreInterrupts(en);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[NoHeapAllocation]
|
|
|
|
private long InternalGetKernelTicks()
|
|
|
|
{
|
|
|
|
if (!tscSnapshotValid) {
|
2008-11-17 18:29:00 -05:00
|
|
|
if (hpetClock == null) {
|
2008-03-05 09:52:00 -05:00
|
|
|
ticksSnapshot = (long) pmClock.GetKernelTicks();
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
ticksSnapshot = (long) hpetClock.GetKernelTicks();
|
|
|
|
}
|
|
|
|
tscSnapshot = Processor.CycleCount;
|
|
|
|
tscSnapshotValid = true;
|
|
|
|
|
|
|
|
return ticksSnapshot;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
long tscDelta = (long)(Processor.CycleCount - tscSnapshot);
|
|
|
|
long tickDelta = (tscDelta * tickScale) >> tickRoll;
|
|
|
|
return ticksSnapshot + tickDelta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte Interrupt
|
|
|
|
{
|
|
|
|
[NoHeapAllocation]
|
|
|
|
get { return rtClock.Interrupt; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[NoHeapAllocation]
|
2008-11-17 18:29:00 -05:00
|
|
|
public override void ClearInterrupt()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
Microsoft.Singularity.Hal.Platform p = Microsoft.Singularity.Hal.Platform.ThePlatform;
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
if (hpetClock == null) {
|
|
|
|
pmClock.Update();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hpetClock.Update();
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
rtClock.ClearInterrupt();
|
2008-03-05 09:52:00 -05:00
|
|
|
tscSnapshotValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void SwitchToHpetClock(HpetClock hc)
|
|
|
|
{
|
|
|
|
// Change rt clock interrupt frequency to appropriate
|
|
|
|
// rate for HPET main clock.
|
|
|
|
rtClock.SetFrequency(HpetClock.UpdateFrequency(hc.Hpet));
|
|
|
|
hpetClock = hc;
|
|
|
|
DebugStub.Print("Hal switching to HpetClock.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
[NoHeapAllocation]
|
2008-11-17 18:29:00 -05:00
|
|
|
public override void CpuResumeFromHaltEvent()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
tscSnapshotValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
[NoHeapAllocation]
|
2008-11-17 18:29:00 -05:00
|
|
|
public override long GetRtcTime()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
return rtClock.GetBootTime() + GetKernelTicks();
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
public override void SetRtcTime(long newRtcTime)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
rtClock.SetRtcTime(newRtcTime, GetKernelTicks());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|