qemu/tests/megasas-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for LSI MegaRAID
   3 *
   4 * Copyright (c) 2017 Red Hat Inc.
   5 *
   6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   7 * See the COPYING file in the top-level directory.
   8 */
   9
  10#include "qemu/osdep.h"
  11#include "libqtest.h"
  12#include "qemu/bswap.h"
  13#include "libqos/libqos-pc.h"
  14#include "libqos/libqos-spapr.h"
  15
  16static QOSState *qmegasas_start(const char *extra_opts)
  17{
  18    QOSState *qs;
  19    const char *arch = qtest_get_arch();
  20    const char *cmd = "-drive id=hd0,if=none,file=null-co://,format=raw "
  21                      "-device megasas,id=scsi0,addr=04.0 "
  22                      "-device scsi-hd,bus=scsi0.0,drive=hd0 %s";
  23
  24    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
  25        qs = qtest_pc_boot(cmd, extra_opts ? : "");
  26        global_qtest = qs->qts;
  27        return qs;
  28    }
  29
  30    g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
  31    exit(EXIT_FAILURE);
  32}
  33
  34static void qmegasas_stop(QOSState *qs)
  35{
  36    qtest_shutdown(qs);
  37}
  38
  39/* Tests only initialization so far. TODO: Replace with functional tests */
  40static void pci_nop(void)
  41{
  42    QOSState *qs;
  43
  44    qs = qmegasas_start(NULL);
  45    qmegasas_stop(qs);
  46}
  47
  48/* This used to cause a NULL pointer dereference.  */
  49static void megasas_pd_get_info_fuzz(void)
  50{
  51    QPCIDevice *dev;
  52    QOSState *qs;
  53    QPCIBar bar;
  54    uint32_t context[256];
  55    uint64_t context_pa;
  56    int i;
  57
  58    qs = qmegasas_start(NULL);
  59    dev = qpci_device_find(qs->pcibus, QPCI_DEVFN(4,0));
  60    g_assert(dev != NULL);
  61
  62    qpci_device_enable(dev);
  63    bar = qpci_iomap(dev, 0, NULL);
  64
  65    memset(context, 0, sizeof(context));
  66    context[0] = cpu_to_le32(0x05050505);
  67    context[1] = cpu_to_le32(0x01010101);
  68    for (i = 2; i < ARRAY_SIZE(context); i++) {
  69        context[i] = cpu_to_le32(0x41414141);
  70    }
  71    context[6] = cpu_to_le32(0x02020000);
  72    context[7] = cpu_to_le32(0);
  73
  74    context_pa = qmalloc(qs, sizeof(context));
  75    memwrite(context_pa, context, sizeof(context));
  76    qpci_io_writel(dev, bar, 0x40, context_pa);
  77
  78    g_free(dev);
  79    qmegasas_stop(qs);
  80}
  81
  82int main(int argc, char **argv)
  83{
  84    g_test_init(&argc, &argv, NULL);
  85    qtest_add_func("/megasas/pci/nop", pci_nop);
  86    qtest_add_func("/megasas/dcmd/pd-get-info/fuzz", megasas_pd_get_info_fuzz);
  87
  88    return g_test_run();
  89}
  90