From patchwork Thu Mar 15 13:37:45 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 146963 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8B0D9B6FA1 for ; Fri, 16 Mar 2012 00:40:26 +1100 (EST) Received: from localhost ([::1]:36675 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S8Aum-0002zU-Dw for incoming@patchwork.ozlabs.org; Thu, 15 Mar 2012 09:40:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:57613) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S8AuX-0002sT-MT for qemu-devel@nongnu.org; Thu, 15 Mar 2012 09:40:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S8Au5-00063b-6G for qemu-devel@nongnu.org; Thu, 15 Mar 2012 09:40:09 -0400 Received: from e34.co.us.ibm.com ([32.97.110.152]:54417) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S8Au4-000635-US for qemu-devel@nongnu.org; Thu, 15 Mar 2012 09:39:41 -0400 Received: from /spool/local by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 15 Mar 2012 07:39:36 -0600 Received: from d03dlp02.boulder.ibm.com (9.17.202.178) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 15 Mar 2012 07:38:38 -0600 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by d03dlp02.boulder.ibm.com (Postfix) with ESMTP id D33833E4004A for ; Thu, 15 Mar 2012 07:38:36 -0600 (MDT) Received: from d03av05.boulder.ibm.com (d03av05.boulder.ibm.com [9.17.195.85]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q2FDcIhP157088 for ; Thu, 15 Mar 2012 07:38:19 -0600 Received: from d03av05.boulder.ibm.com (loopback [127.0.0.1]) by d03av05.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q2FDc3pU029023 for ; Thu, 15 Mar 2012 07:38:03 -0600 Received: from titi.austin.rr.com (sig-9-65-116-158.mts.ibm.com [9.65.116.158]) by d03av05.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q2FDbpEQ028311; Thu, 15 Mar 2012 07:38:02 -0600 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Thu, 15 Mar 2012 08:37:45 -0500 Message-Id: <1331818666-31718-9-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1331818666-31718-1-git-send-email-aliguori@us.ibm.com> References: <1331818666-31718-1-git-send-email-aliguori@us.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12031513-1780-0000-0000-000003FC4FC2 X-IBM-ISS-SpamDetectors: X-IBM-ISS-DetailInfo: BY=3.00000256; HX=3.00000185; KW=3.00000007; PH=3.00000001; SC=3.00000001; SDB=6.00122236; UDB=6.00029410; UTC=2012-03-15 13:39:34 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.152 Cc: Paolo Bonzini , Anthony Liguori Subject: [Qemu-devel] [PATCH 8/9] qtest: add rtc-test test-case (v3) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Anthony Liguori Signed-off-by: Paolo Bonzini --- v1 -> v2 - fix set_alarm_time (Paolo) v2 -> v3 - merge IRQ intercept support (Paolo) --- tests/Makefile | 2 +- tests/rtc-test.c | 268 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+), 1 deletions(-) create mode 100644 tests/rtc-test.c diff --git a/tests/Makefile b/tests/Makefile index 254c06d..8c83207 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -15,7 +15,7 @@ CHECKS += test-string-output-visitor$(EXESUF) TEST_SCRIPTS = $(SRC_PATH)/tests/qemu-iotests-quick.sh -HW_TESTS= +HW_TESTS=tests/rtc-test TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS))) diff --git a/tests/rtc-test.c b/tests/rtc-test.c new file mode 100644 index 0000000..8280f45 --- /dev/null +++ b/tests/rtc-test.c @@ -0,0 +1,268 @@ +/* + * QTest + * + * Copyright IBM, Corp. 2012 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ +#include "libqtest.h" +#include "hw/mc146818rtc_regs.h" + +#include +#include +#include +#include +#include + +static uint8_t base = 0x70; + +static int bcd2dec(int value) +{ + return (((value >> 4) & 0x0F) * 10) + (value & 0x0F); +} + +static int dec2bcd(int value) +{ + return ((value / 10) << 4) | (value % 10); +} + +static uint8_t cmos_read(uint8_t reg) +{ + outb(base + 0, reg); + return inb(base + 1); +} + +static void cmos_write(uint8_t reg, uint8_t val) +{ + outb(base + 0, reg); + outb(base + 1, val); +} + +static int tm_cmp(struct tm *lhs, struct tm *rhs) +{ + time_t a, b; + struct tm d1, d2; + + memcpy(&d1, lhs, sizeof(d1)); + memcpy(&d2, rhs, sizeof(d2)); + + a = mktime(&d1); + b = mktime(&d2); + + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } + + return 0; +} + +#if 0 +static void print_tm(struct tm *tm) +{ + printf("%04d-%02d-%02d %02d:%02d:%02d\n", + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff); +} +#endif + +static void cmos_get_date_time(struct tm *date) +{ + int base_year = 2000, hour_offset; + int sec, min, hour, mday, mon, year; + time_t ts; + struct tm dummy; + + sec = cmos_read(RTC_SECONDS); + min = cmos_read(RTC_MINUTES); + hour = cmos_read(RTC_HOURS); + mday = cmos_read(RTC_DAY_OF_MONTH); + mon = cmos_read(RTC_MONTH); + year = cmos_read(RTC_YEAR); + + if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) { + sec = bcd2dec(sec); + min = bcd2dec(min); + hour = bcd2dec(hour); + mday = bcd2dec(mday); + mon = bcd2dec(mon); + year = bcd2dec(year); + hour_offset = 80; + } else { + hour_offset = 0x80; + } + + if ((cmos_read(0x0B) & REG_B_24H) == 0) { + if (hour >= hour_offset) { + hour -= hour_offset; + hour += 12; + } + } + + ts = time(NULL); + localtime_r(&ts, &dummy); + + date->tm_isdst = dummy.tm_isdst; + date->tm_sec = sec; + date->tm_min = min; + date->tm_hour = hour; + date->tm_mday = mday; + date->tm_mon = mon - 1; + date->tm_year = base_year + year - 1900; + date->tm_gmtoff = 0; + + ts = mktime(date); +} + +static void check_time(int wiggle) +{ + struct tm start, date[4], end; + struct tm *datep; + time_t ts; + + /* + * This check assumes a few things. First, we cannot guarantee that we get + * a consistent reading from the wall clock because we may hit an edge of + * the clock while reading. To work around this, we read four clock readings + * such that at least two of them should match. We need to assume that one + * reading is corrupt so we need four readings to ensure that we have at + * least two consecutive identical readings + * + * It's also possible that we'll cross an edge reading the host clock so + * simply check to make sure that the clock reading is within the period of + * when we expect it to be. + */ + + ts = time(NULL); + gmtime_r(&ts, &start); + + cmos_get_date_time(&date[0]); + cmos_get_date_time(&date[1]); + cmos_get_date_time(&date[2]); + cmos_get_date_time(&date[3]); + + ts = time(NULL); + gmtime_r(&ts, &end); + + if (tm_cmp(&date[0], &date[1]) == 0) { + datep = &date[0]; + } else if (tm_cmp(&date[1], &date[2]) == 0) { + datep = &date[1]; + } else if (tm_cmp(&date[2], &date[3]) == 0) { + datep = &date[2]; + } else { + g_assert_not_reached(); + } + + if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) { + time_t t, s; + + start.tm_isdst = datep->tm_isdst; + + t = mktime(datep); + s = mktime(&start); + if (t < s) { + g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t)); + } else { + g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s)); + } + + g_assert_cmpint(ABS(t - s), <=, wiggle); + } +} + +static int wiggle = 2; + +static void bcd_check_time(void) +{ + /* Set BCD mode */ + cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM); + check_time(wiggle); +} + +static void dec_check_time(void) +{ + /* Set DEC mode */ + cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM); + check_time(wiggle); +} + +static void set_alarm_time(struct tm *tm) +{ + int sec; + + sec = tm->tm_sec; + + if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) { + sec = dec2bcd(sec); + } + + cmos_write(RTC_SECONDS_ALARM, sec); + cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE); + cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE); +} + +static void alarm_time(void) +{ + struct tm now; + time_t ts; + int i; + + ts = time(NULL); + gmtime_r(&ts, &now); + + /* set DEC mode */ + cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM); + + g_assert(!get_irq(RTC_ISA_IRQ)); + + now.tm_sec = (now.tm_sec + 2) % 60; + set_alarm_time(&now); + cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE); + + for (i = 0; i < 2 + wiggle; i++) { + if (get_irq(RTC_ISA_IRQ)) { + break; + } + + sleep(1); + } + + g_assert(get_irq(RTC_ISA_IRQ)); +} + +int main(int argc, char **argv) +{ + const char *arch; + QTestState *s = NULL; + int ret; + + g_test_init(&argc, &argv, NULL); + + arch = qtest_get_arch(); + /* These tests only work on i386 and x86_64 */ + if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { + s = qtest_start("-vnc none"); + qtest_irq_intercept_in(s, "ioapic"); + + qtest_add_func("/rtc/bcd/check-time", bcd_check_time); + qtest_add_func("/rtc/dec/check-time", dec_check_time); + qtest_add_func("/rtc/alarm-time", alarm_time); + } else { + g_test_message("Skipping unsupported arch `%s'\n", arch); + } + + ret = g_test_run(); + + if (s) { + qtest_quit(s); + } + + return ret; +}