qemu/tests/test-x86-cpuid.c
<<
>>
Prefs
   1/*
   2 *  Test code for x86 CPUID and Topology functions
   3 *
   4 *  Copyright (c) 2012 Red Hat Inc.
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24
  25#include "qemu/osdep.h"
  26#include <glib.h>
  27
  28#include "hw/i386/topology.h"
  29
  30static void test_topo_bits(void)
  31{
  32    /* simple tests for 1 thread per core, 1 core per socket */
  33    g_assert_cmpuint(apicid_smt_width(1, 1), ==, 0);
  34    g_assert_cmpuint(apicid_core_width(1, 1), ==, 0);
  35
  36    g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 0), ==, 0);
  37    g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 1), ==, 1);
  38    g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 2), ==, 2);
  39    g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 3), ==, 3);
  40
  41
  42    /* Test field width calculation for multiple values
  43     */
  44    g_assert_cmpuint(apicid_smt_width(1, 2), ==, 1);
  45    g_assert_cmpuint(apicid_smt_width(1, 3), ==, 2);
  46    g_assert_cmpuint(apicid_smt_width(1, 4), ==, 2);
  47
  48    g_assert_cmpuint(apicid_smt_width(1, 14), ==, 4);
  49    g_assert_cmpuint(apicid_smt_width(1, 15), ==, 4);
  50    g_assert_cmpuint(apicid_smt_width(1, 16), ==, 4);
  51    g_assert_cmpuint(apicid_smt_width(1, 17), ==, 5);
  52
  53
  54    g_assert_cmpuint(apicid_core_width(30, 2), ==, 5);
  55    g_assert_cmpuint(apicid_core_width(31, 2), ==, 5);
  56    g_assert_cmpuint(apicid_core_width(32, 2), ==, 5);
  57    g_assert_cmpuint(apicid_core_width(33, 2), ==, 6);
  58
  59
  60    /* build a weird topology and see if IDs are calculated correctly
  61     */
  62
  63    /* This will use 2 bits for thread ID and 3 bits for core ID
  64     */
  65    g_assert_cmpuint(apicid_smt_width(6, 3), ==, 2);
  66    g_assert_cmpuint(apicid_core_width(6, 3), ==, 3);
  67    g_assert_cmpuint(apicid_pkg_offset(6, 3), ==, 5);
  68
  69    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 0), ==, 0);
  70    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 1), ==, 1);
  71    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 2), ==, 2);
  72
  73    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 1 * 3 + 0), ==,
  74                     (1 << 2) | 0);
  75    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 1 * 3 + 1), ==,
  76                     (1 << 2) | 1);
  77    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 1 * 3 + 2), ==,
  78                     (1 << 2) | 2);
  79
  80    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 2 * 3 + 0), ==,
  81                     (2 << 2) | 0);
  82    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 2 * 3 + 1), ==,
  83                     (2 << 2) | 1);
  84    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 2 * 3 + 2), ==,
  85                     (2 << 2) | 2);
  86
  87    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 5 * 3 + 0), ==,
  88                     (5 << 2) | 0);
  89    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 5 * 3 + 1), ==,
  90                     (5 << 2) | 1);
  91    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 5 * 3 + 2), ==,
  92                     (5 << 2) | 2);
  93
  94    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 1 * 6 * 3 + 0 * 3 + 0), ==,
  95                     (1 << 5));
  96    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 1 * 6 * 3 + 1 * 3 + 1), ==,
  97                     (1 << 5) | (1 << 2) | 1);
  98    g_assert_cmpuint(x86_apicid_from_cpu_idx(6, 3, 3 * 6 * 3 + 5 * 3 + 2), ==,
  99                     (3 << 5) | (5 << 2) | 2);
 100}
 101
 102int main(int argc, char **argv)
 103{
 104    g_test_init(&argc, &argv, NULL);
 105
 106    g_test_add_func("/cpuid/topology/basic", test_topo_bits);
 107
 108    g_test_run();
 109
 110    return 0;
 111}
 112