From 5b30828a565826fa7f336cf2ce1906e47e6c340b Mon Sep 17 00:00:00 2001
From: Jie Tang <tangjie18@huawei.com>
Date: Thu, 14 May 2020 17:05:42 +0800
Subject: [PATCH 19/28] get_cpu_model: verify command for aarch64 to get
 supported cpu models.

For example, aarch64 use 'qemu-kvm -M virt -cpu ?' command to get supported cpu models.

Signed-off-by: Jie Tang <tangjie18@huawei.com>
---
 virttest/cpu.py     | 15 ++++++++++++---
 virttest/qemu_vm.py | 20 +++++++++++++++-----
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/virttest/cpu.py b/virttest/cpu.py
index 2d34bf2e..9df28cbe 100644
--- a/virttest/cpu.py
+++ b/virttest/cpu.py
@@ -1204,11 +1204,14 @@ def cpus_parser(cpulist):
         return sorted(list(cpus_set))
 
 
-def get_qemu_cpu_models(qemu_binary):
+def get_qemu_cpu_models(qemu_binary, extra_params=None):
     """Get listing of CPU models supported by QEMU
     Get list of CPU models by parsing the output of <qemu> -cpu '?'
     """
-    cmd = qemu_binary + " -cpu '?'"
+    if extra_params is not None:
+        cmd = qemu_binary + extra_params + " -cpu '?'"
+    else:
+        cmd = qemu_binary + " -cpu '?'"
     result = process.run(cmd, verbose=False)
     return extract_qemu_cpu_models(results_stdout_52lts(result))
 
@@ -1229,7 +1232,13 @@ def get_qemu_best_cpu_model(params):
     """
     host_cpu_models = get_host_cpu_models()
     qemu_binary = utils_misc.get_qemu_binary(params)
-    qemu_cpu_models = get_qemu_cpu_models(qemu_binary)
+    # aarch64 use 'qemu_binary -M machine_type -cpu ?' to get supported cpu models.
+    if platform.machine() == 'aarch64':
+        machine_type = params.get("machine_type").split(':', 1)[1]
+        extra_params = " -M" + " %s" % machine_type
+        qemu_cpu_models = get_qemu_cpu_models(qemu_binary, extra_params)
+    else:
+        qemu_cpu_models = get_qemu_cpu_models(qemu_binary)
     # Let's try to find a suitable model on the qemu list
     for host_cpu_model in host_cpu_models:
         if host_cpu_model in qemu_cpu_models:
diff --git a/virttest/qemu_vm.py b/virttest/qemu_vm.py
index 892a8891..35872912 100644
--- a/virttest/qemu_vm.py
+++ b/virttest/qemu_vm.py
@@ -13,6 +13,7 @@ import re
 import random
 import sys
 import math
+import platform
 
 from functools import partial, reduce
 from operator import mul
@@ -1409,11 +1410,20 @@ class VM(virt_vm.BaseVM):
                                                                  verbose=False,
                                                                  ignore_status=True,
                                                                  shell=True)).split(',')[0]
-        support_cpu_model = decode_to_text(process.system_output("%s -cpu \\?" % qemu_binary,
-                                                                 verbose=False,
-                                                                 ignore_status=True,
-                                                                 shell=True),
-                                           errors='replace')
+        # aarch64 use "qemu-kvm -M machine_type -cpu ?" to get supported cpu models.
+        if platform.machine() == 'aarch64':
+            machine_type = params.get("machine_type").split(':', 1)[1]
+            support_cpu_model = decode_to_text(process.system_output("%s -M %s -cpu \\? " % (qemu_binary, machine_type),
+                                                                     verbose=False,
+                                                                     ignore_status=True,
+                                                                     shell=True),
+                                               errors='replace')
+        else:
+            support_cpu_model = decode_to_text(process.system_output("%s -cpu \\?" % qemu_binary,
+                                                                     verbose=False,
+                                                                     ignore_status=True,
+                                                                     shell=True),
+                                               errors='replace')
 
         self.last_driver_index = 0
         # init the dict index_in_use
-- 
2.23.0

