From 9194f26eba9e7ce3c27863dabddafe46fcfdba58 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Wed, 2 Jul 2008 17:25:05 +0100 Subject: [PATCH] x86 hvm: Fix RTC handling. 1. Clean up initialisation/destruction. 2. Better handle per-domain time-offset changes. Signed-off-by: Keir Fraser --- xen/arch/ia64/xen/fw_emul.c | 5 ++++ xen/arch/x86/domain.c | 1 + xen/arch/x86/hvm/hvm.c | 20 +++++++++---- xen/arch/x86/hvm/rtc.c | 53 ++++++++++++++++++----------------- xen/arch/x86/time.c | 7 +++++ xen/common/domctl.c | 2 +- xen/include/asm-x86/hvm/vpt.h | 4 +-- xen/include/xen/time.h | 2 ++ 8 files changed, 59 insertions(+), 35 deletions(-) diff --git a/xen/arch/ia64/xen/fw_emul.c b/xen/arch/ia64/xen/fw_emul.c index dd138ea643..3b64bd14d4 100644 --- a/xen/arch/ia64/xen/fw_emul.c +++ b/xen/arch/ia64/xen/fw_emul.c @@ -1061,6 +1061,11 @@ errout: return status; } +void domain_set_time_offset(struct domain *d, int32_t time_offset_seconds) +{ + d->time_offset_seconds = time_offset_seconds; +} + static efi_status_t efi_emulate_set_time( unsigned long tv_addr, IA64FAULT *fault) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 0cc4ba44ed..b6a8497206 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -452,6 +452,7 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags) return 0; fail: + d->is_dying = DOMDYING_dead; free_xenheap_page(d->shared_info); if ( paging_initialised ) paging_final_teardown(d); diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 3a060d7a75..6f410bfd04 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -314,6 +314,8 @@ int hvm_domain_initialise(struct domain *d) stdvga_init(d); + rtc_init(d); + hvm_init_ioreq_page(d, &d->arch.hvm_domain.ioreq); hvm_init_ioreq_page(d, &d->arch.hvm_domain.buf_ioreq); @@ -326,6 +328,8 @@ int hvm_domain_initialise(struct domain *d) return 0; fail2: + rtc_deinit(d); + stdvga_deinit(d); vioapic_deinit(d); fail1: hvm_destroy_cacheattr_region_list(d); @@ -337,16 +341,21 @@ void hvm_domain_relinquish_resources(struct domain *d) hvm_destroy_ioreq_page(d, &d->arch.hvm_domain.ioreq); hvm_destroy_ioreq_page(d, &d->arch.hvm_domain.buf_ioreq); - pit_deinit(d); + /* Stop all asynchronous timer actions. */ rtc_deinit(d); - pmtimer_deinit(d); - hpet_deinit(d); - stdvga_deinit(d); + if ( d->vcpu[0] != NULL ) + { + pit_deinit(d); + pmtimer_deinit(d); + hpet_deinit(d); + } } void hvm_domain_destroy(struct domain *d) { hvm_funcs.domain_destroy(d); + rtc_deinit(d); + stdvga_deinit(d); vioapic_deinit(d); hvm_destroy_cacheattr_region_list(d); } @@ -658,7 +667,6 @@ int hvm_vcpu_initialise(struct vcpu *v) { /* NB. All these really belong in hvm_domain_initialise(). */ pit_init(v, cpu_khz); - rtc_init(v, RTC_PORT(0)); pmtimer_init(v); hpet_init(v); @@ -2131,7 +2139,7 @@ static void hvm_s3_suspend(struct domain *d) domain_pause(d); domain_lock(d); - if ( (d->vcpu[0] == NULL) || + if ( d->is_dying || (d->vcpu[0] == NULL) || test_and_set_bool(d->arch.hvm_domain.is_s3_suspended) ) { domain_unlock(d); diff --git a/xen/arch/x86/hvm/rtc.c b/xen/arch/x86/hvm/rtc.c index 9060af806c..6193a9ad81 100644 --- a/xen/arch/x86/hvm/rtc.c +++ b/xen/arch/x86/hvm/rtc.c @@ -186,16 +186,9 @@ static void rtc_set_time(RTCState *s) static void rtc_copy_date(RTCState *s) { const struct tm *tm = &s->current_tm; - struct domain *d = vrtc_domain(s); ASSERT(spin_is_locked(&s->lock)); - if ( s->time_offset_seconds != d->time_offset_seconds ) - { - s->current_tm = gmtime(get_localtime(d)); - s->time_offset_seconds = d->time_offset_seconds; - } - s->hw.cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec); s->hw.cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min); if ( s->hw.cmos_data[RTC_REG_B] & RTC_24H ) @@ -237,16 +230,9 @@ static void rtc_next_second(RTCState *s) { struct tm *tm = &s->current_tm; int days_in_month; - struct domain *d = vrtc_domain(s); ASSERT(spin_is_locked(&s->lock)); - if ( s->time_offset_seconds != d->time_offset_seconds ) - { - s->current_tm = gmtime(get_localtime(d)); - s->time_offset_seconds = d->time_offset_seconds; - } - tm->tm_sec++; if ( (unsigned)tm->tm_sec >= 60 ) { @@ -474,46 +460,61 @@ static int rtc_load(struct domain *d, hvm_domain_context_t *h) HVM_REGISTER_SAVE_RESTORE(RTC, rtc_save, rtc_load, 1, HVMSR_PER_DOM); +void rtc_reset(struct domain *d) +{ + RTCState *s = domain_vrtc(d); + + destroy_periodic_time(&s->pt); + s->pt.source = PTSRC_isa; +} -void rtc_init(struct vcpu *v, int base) +void rtc_init(struct domain *d) { - RTCState *s = vcpu_vrtc(v); + RTCState *s = domain_vrtc(d); spin_lock_init(&s->lock); - s->pt.source = PTSRC_isa; + init_timer(&s->second_timer, rtc_update_second, s, smp_processor_id()); + init_timer(&s->second_timer2, rtc_update_second2, s, smp_processor_id()); + + register_portio_handler(d, RTC_PORT(0), 2, handle_rtc_io); + + rtc_reset(d); + + spin_lock(&s->lock); s->hw.cmos_data[RTC_REG_A] = RTC_REF_CLCK_32KHZ | 6; /* ~1kHz */ s->hw.cmos_data[RTC_REG_B] = RTC_24H; s->hw.cmos_data[RTC_REG_C] = 0; s->hw.cmos_data[RTC_REG_D] = RTC_VRT; - s->current_tm = gmtime(get_localtime(v->domain)); + s->current_tm = gmtime(get_localtime(d)); - spin_lock(&s->lock); rtc_copy_date(s); - spin_unlock(&s->lock); - - init_timer(&s->second_timer, rtc_update_second, s, v->processor); - init_timer(&s->second_timer2, rtc_update_second2, s, v->processor); s->next_second_time = NOW() + 1000000000ULL; + stop_timer(&s->second_timer); set_timer(&s->second_timer2, s->next_second_time); - register_portio_handler(v->domain, base, 2, handle_rtc_io); + spin_unlock(&s->lock); } void rtc_deinit(struct domain *d) { RTCState *s = domain_vrtc(d); + spin_barrier(&s->lock); + destroy_periodic_time(&s->pt); kill_timer(&s->second_timer); kill_timer(&s->second_timer2); } -void rtc_reset(struct domain *d) +void rtc_update_clock(struct domain *d) { RTCState *s = domain_vrtc(d); - destroy_periodic_time(&s->pt); + + spin_lock(&s->lock); + s->current_tm = gmtime(get_localtime(d)); + spin_unlock(&s->lock); } diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c index fe5bef24ac..507b139bf8 100644 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -745,6 +745,13 @@ void update_domain_wallclock_time(struct domain *d) spin_unlock(&wc_lock); } +void domain_set_time_offset(struct domain *d, int32_t time_offset_seconds) +{ + d->time_offset_seconds = time_offset_seconds; + if ( is_hvm_domain(d) ) + rtc_update_clock(d); +} + int cpu_frequency_change(u64 freq) { struct cpu_time *t = &this_cpu(cpu_time); diff --git a/xen/common/domctl.c b/xen/common/domctl.c index 0581a737b1..13db2152e7 100644 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -787,7 +787,7 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domctl_t) u_domctl) break; } - d->time_offset_seconds = op->u.settimeoffset.time_offset_seconds; + domain_set_time_offset(d, op->u.settimeoffset.time_offset_seconds); rcu_unlock_domain(d); ret = 0; } diff --git a/xen/include/asm-x86/hvm/vpt.h b/xen/include/asm-x86/hvm/vpt.h index 24026fb118..db58653411 100644 --- a/xen/include/asm-x86/hvm/vpt.h +++ b/xen/include/asm-x86/hvm/vpt.h @@ -118,7 +118,6 @@ typedef struct RTCState { struct timer second_timer; struct timer second_timer2; struct periodic_time pt; - int32_t time_offset_seconds; spinlock_t lock; } RTCState; @@ -176,10 +175,11 @@ void pit_reset(struct domain *d); void pit_init(struct vcpu *v, unsigned long cpu_khz); void pit_stop_channel0_irq(PITState * pit); void pit_deinit(struct domain *d); -void rtc_init(struct vcpu *v, int base); +void rtc_init(struct domain *d); void rtc_migrate_timers(struct vcpu *v); void rtc_deinit(struct domain *d); void rtc_reset(struct domain *d); +void rtc_update_clock(struct domain *d); void pmtimer_init(struct vcpu *v); void pmtimer_deinit(struct domain *d); diff --git a/xen/include/xen/time.h b/xen/include/xen/time.h index f11caa2ba4..31204e09a9 100644 --- a/xen/include/xen/time.h +++ b/xen/include/xen/time.h @@ -61,6 +61,8 @@ extern void do_settime( extern void send_timer_event(struct vcpu *v); +void domain_set_time_offset(struct domain *d, int32_t time_offset_seconds); + #endif /* __XEN_TIME_H__ */ /* -- 2.30.2