qemu/tests/m48t59-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for the M48T59 and M48T08 real-time clocks
   3 *
   4 * Based on MC146818 RTC test:
   5 * Copyright IBM, Corp. 2012
   6 *
   7 * Authors:
   8 *  Anthony Liguori   <aliguori@us.ibm.com>
   9 *
  10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11 * See the COPYING file in the top-level directory.
  12 *
  13 */
  14#include "libqtest.h"
  15
  16#include <glib.h>
  17#include <stdio.h>
  18#include <string.h>
  19#include <stdlib.h>
  20#include <unistd.h>
  21
  22#define RTC_SECONDS             0x9
  23#define RTC_MINUTES             0xa
  24#define RTC_HOURS               0xb
  25
  26#define RTC_DAY_OF_WEEK         0xc
  27#define RTC_DAY_OF_MONTH        0xd
  28#define RTC_MONTH               0xe
  29#define RTC_YEAR                0xf
  30
  31static uint32_t base;
  32static uint16_t reg_base = 0x1ff0; /* 0x7f0 for m48t02 */
  33static int base_year;
  34static bool use_mmio;
  35
  36static uint8_t cmos_read_mmio(uint8_t reg)
  37{
  38    uint8_t data;
  39
  40    memread(base + (uint32_t)reg_base + (uint32_t)reg, &data, 1);
  41    return data;
  42}
  43
  44static void cmos_write_mmio(uint8_t reg, uint8_t val)
  45{
  46    uint8_t data = val;
  47
  48    memwrite(base + (uint32_t)reg_base + (uint32_t)reg, &data, 1);
  49}
  50
  51static uint8_t cmos_read_ioio(uint8_t reg)
  52{
  53    outw(base + 0, reg_base + (uint16_t)reg);
  54    return inb(base + 3);
  55}
  56
  57static void cmos_write_ioio(uint8_t reg, uint8_t val)
  58{
  59    outw(base + 0, reg_base + (uint16_t)reg);
  60    outb(base + 3, val);
  61}
  62
  63static uint8_t cmos_read(uint8_t reg)
  64{
  65    if (use_mmio) {
  66        return cmos_read_mmio(reg);
  67    } else {
  68        return cmos_read_ioio(reg);
  69    }
  70}
  71
  72static void cmos_write(uint8_t reg, uint8_t val)
  73{
  74    if (use_mmio) {
  75        cmos_write_mmio(reg, val);
  76    } else {
  77        cmos_write_ioio(reg, val);
  78    }
  79}
  80
  81static int bcd2dec(int value)
  82{
  83    return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
  84}
  85
  86static int tm_cmp(struct tm *lhs, struct tm *rhs)
  87{
  88    time_t a, b;
  89    struct tm d1, d2;
  90
  91    memcpy(&d1, lhs, sizeof(d1));
  92    memcpy(&d2, rhs, sizeof(d2));
  93
  94    a = mktime(&d1);
  95    b = mktime(&d2);
  96
  97    if (a < b) {
  98        return -1;
  99    } else if (a > b) {
 100        return 1;
 101    }
 102
 103    return 0;
 104}
 105
 106#if 0
 107static void print_tm(struct tm *tm)
 108{
 109    printf("%04d-%02d-%02d %02d:%02d:%02d %+02ld\n",
 110           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
 111           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
 112}
 113#endif
 114
 115static void cmos_get_date_time(struct tm *date)
 116{
 117    int sec, min, hour, mday, mon, year;
 118    time_t ts;
 119    struct tm dummy;
 120
 121    sec = cmos_read(RTC_SECONDS);
 122    min = cmos_read(RTC_MINUTES);
 123    hour = cmos_read(RTC_HOURS);
 124    mday = cmos_read(RTC_DAY_OF_MONTH);
 125    mon = cmos_read(RTC_MONTH);
 126    year = cmos_read(RTC_YEAR);
 127
 128    sec = bcd2dec(sec);
 129    min = bcd2dec(min);
 130    hour = bcd2dec(hour);
 131    mday = bcd2dec(mday);
 132    mon = bcd2dec(mon);
 133    year = bcd2dec(year);
 134
 135    ts = time(NULL);
 136    localtime_r(&ts, &dummy);
 137
 138    date->tm_isdst = dummy.tm_isdst;
 139    date->tm_sec = sec;
 140    date->tm_min = min;
 141    date->tm_hour = hour;
 142    date->tm_mday = mday;
 143    date->tm_mon = mon - 1;
 144    date->tm_year = base_year + year - 1900;
 145#ifndef __sun__
 146    date->tm_gmtoff = 0;
 147#endif
 148
 149    ts = mktime(date);
 150}
 151
 152static void check_time(int wiggle)
 153{
 154    struct tm start, date[4], end;
 155    struct tm *datep;
 156    time_t ts;
 157
 158    /*
 159     * This check assumes a few things.  First, we cannot guarantee that we get
 160     * a consistent reading from the wall clock because we may hit an edge of
 161     * the clock while reading.  To work around this, we read four clock readings
 162     * such that at least two of them should match.  We need to assume that one
 163     * reading is corrupt so we need four readings to ensure that we have at
 164     * least two consecutive identical readings
 165     *
 166     * It's also possible that we'll cross an edge reading the host clock so
 167     * simply check to make sure that the clock reading is within the period of
 168     * when we expect it to be.
 169     */
 170
 171    ts = time(NULL);
 172    gmtime_r(&ts, &start);
 173
 174    cmos_get_date_time(&date[0]);
 175    cmos_get_date_time(&date[1]);
 176    cmos_get_date_time(&date[2]);
 177    cmos_get_date_time(&date[3]);
 178
 179    ts = time(NULL);
 180    gmtime_r(&ts, &end);
 181
 182    if (tm_cmp(&date[0], &date[1]) == 0) {
 183        datep = &date[0];
 184    } else if (tm_cmp(&date[1], &date[2]) == 0) {
 185        datep = &date[1];
 186    } else if (tm_cmp(&date[2], &date[3]) == 0) {
 187        datep = &date[2];
 188    } else {
 189        g_assert_not_reached();
 190    }
 191
 192    if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
 193        long t, s;
 194
 195        start.tm_isdst = datep->tm_isdst;
 196
 197        t = (long)mktime(datep);
 198        s = (long)mktime(&start);
 199        if (t < s) {
 200            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
 201        } else {
 202            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
 203        }
 204
 205        g_assert_cmpint(ABS(t - s), <=, wiggle);
 206    }
 207}
 208
 209static int wiggle = 2;
 210
 211static void bcd_check_time(void)
 212{
 213    if (strcmp(qtest_get_arch(), "sparc64") == 0) {
 214        base = 0x74;
 215        base_year = 1900;
 216        use_mmio = false;
 217    } else if (strcmp(qtest_get_arch(), "sparc") == 0) {
 218        base = 0x71200000;
 219        base_year = 1968;
 220        use_mmio = true;
 221    } else { /* PPC: need to map macio in PCI */
 222        g_assert_not_reached();
 223    }
 224    check_time(wiggle);
 225}
 226
 227/* success if no crash or abort */
 228static void fuzz_registers(void)
 229{
 230    unsigned int i;
 231
 232    for (i = 0; i < 1000; i++) {
 233        uint8_t reg, val;
 234
 235        reg = (uint8_t)g_test_rand_int_range(0, 16);
 236        val = (uint8_t)g_test_rand_int_range(0, 256);
 237
 238        if (reg == 7) {
 239            /* watchdog setup register, may trigger system reset, skip */
 240            continue;
 241        }
 242
 243        cmos_write(reg, val);
 244        cmos_read(reg);
 245    }
 246}
 247
 248int main(int argc, char **argv)
 249{
 250    QTestState *s = NULL;
 251    int ret;
 252
 253    g_test_init(&argc, &argv, NULL);
 254
 255    s = qtest_start("-display none -rtc clock=vm");
 256
 257    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
 258    qtest_add_func("/rtc/fuzz-registers", fuzz_registers);
 259    ret = g_test_run();
 260
 261    if (s) {
 262        qtest_quit(s);
 263    }
 264
 265    return ret;
 266}
 267