diff options
Diffstat (limited to 'pkg/sentry/kernel/timekeeper.go')
-rw-r--r-- | pkg/sentry/kernel/timekeeper.go | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/pkg/sentry/kernel/timekeeper.go b/pkg/sentry/kernel/timekeeper.go index 3f16c1676..4de8ac13b 100644 --- a/pkg/sentry/kernel/timekeeper.go +++ b/pkg/sentry/kernel/timekeeper.go @@ -44,14 +44,14 @@ type Timekeeper struct { // It is set only once, by SetClocks. monotonicOffset int64 `state:"nosave"` - // restored indicates that this Timekeeper was restored from a state - // file. - restored bool `state:"nosave"` + // restored, if non-nil, indicates that this Timekeeper was restored + // from a state file. The clocks are not set until restored is closed. + restored chan struct{} `state:"nosave"` // saveMonotonic is the (offset) value of the monotonic clock at the // time of save. // - // It is only valid if restored is true. + // It is only valid if restored is non-nil. // // It is only used in SetClocks after restore to compute the new // monotonicOffset. @@ -59,7 +59,7 @@ type Timekeeper struct { // saveRealtime is the value of the realtime clock at the time of save. // - // It is only valid if restored is true. + // It is only valid if restored is non-nil. // // It is only used in SetClocks after restore to compute the new // monotonicOffset. @@ -98,7 +98,7 @@ func NewTimekeeper(platform platform.Platform, paramPage platform.FileRange) (*T func (t *Timekeeper) SetClocks(c sentrytime.Clocks) { // Update the params, marking them "not ready", as we may need to // restart calibration on this new machine. - if t.restored { + if t.restored != nil { if err := t.params.Write(func() vdsoParams { return vdsoParams{} }); err != nil { @@ -135,7 +135,7 @@ func (t *Timekeeper) SetClocks(c sentrytime.Clocks) { panic("Unable to get current realtime: " + err.Error()) } - if t.restored { + if t.restored != nil { wantMonotonic = t.saveMonotonic elapsed := nowRealtime - t.saveRealtime if elapsed > 0 { @@ -145,7 +145,7 @@ func (t *Timekeeper) SetClocks(c sentrytime.Clocks) { t.monotonicOffset = wantMonotonic - nowMonotonic - if !t.restored { + if t.restored == nil { // Hold on to the initial "boot" time. t.bootTime = ktime.FromNanoseconds(nowRealtime) } @@ -153,6 +153,10 @@ func (t *Timekeeper) SetClocks(c sentrytime.Clocks) { t.mu.Lock() defer t.mu.Unlock() t.startUpdater() + + if t.restored != nil { + close(t.restored) + } } // startUpdater starts an update goroutine that keeps the clocks updated. @@ -255,7 +259,10 @@ func (t *Timekeeper) ResumeUpdates() { // GetTime returns the current time in nanoseconds. func (t *Timekeeper) GetTime(c sentrytime.ClockID) (int64, error) { if t.clocks == nil { - panic("Timekeeper used before initialized with SetClocks") + if t.restored == nil { + panic("Timekeeper used before initialized with SetClocks") + } + <-t.restored } now, err := t.clocks.GetTime(c) if err == nil && c == sentrytime.Monotonic { |