1# Copyright (c) 2015, Intel Corporation 2# All rights reserved. 3# 4# SPDX-License-Identifier: BSD-3-Clause 5 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 9# * Redistributions of source code must retain the above copyright notice, 10# this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above copyright notice, 12# this list of conditions and the following disclaimer in the documentation 13# and/or other materials provided with the distribution. 14# * Neither the name of Intel Corporation nor the names of its contributors 15# may be used to endorse or promote products derived from this software 16# without specific prior written permission. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29# This script runs only from the biosbits VM. 30 31"""SMI latency test.""" 32 33import bits 34from collections import namedtuple 35import testsuite 36import time 37import usb 38 39def register_tests(): 40 testsuite.add_test("SMI latency test", smi_latency); 41 testsuite.add_test("SMI latency test with USB disabled via BIOS handoff", test_with_usb_disabled, runall=False); 42 43def smi_latency(): 44 MSR_SMI_COUNT = 0x34 45 46 print "Warning: touching the keyboard can affect the results of this test." 47 48 tsc_per_sec = bits.tsc_per_sec() 49 tsc_per_usec = tsc_per_sec / (1000 * 1000) 50 bins = [long(tsc_per_usec * 10**i) for i in range(9)] 51 bin_descs = [ 52 "0 < t <= 1us", 53 "1us < t <= 10us", 54 "10us < t <= 100us", 55 "100us < t <= 1ms", 56 "1ms < t <= 10ms", 57 "10ms < t <= 100ms", 58 "100ms < t <= 1s ", 59 "1s < t <= 10s ", 60 "10s < t <= 100s ", 61 "100s < t ", 62 ] 63 64 print "Starting test. Wait here, I will be back in 15 seconds." 65 (max_latency, smi_count_delta, bins) = bits.smi_latency(long(15 * tsc_per_sec), bins) 66 BinType = namedtuple('BinType', ("max", "total", "count", "times")) 67 bins = [BinType(*b) for b in bins] 68 69 testsuite.test("SMI latency < 150us to minimize risk of OS timeouts", max_latency / tsc_per_usec <= 150) 70 if not testsuite.show_detail(): 71 return 72 73 for bin, desc in zip(bins, bin_descs): 74 if bin.count == 0: 75 continue 76 testsuite.print_detail("{}; average = {}; count = {}".format(desc, bits.format_tsc(bin.total/bin.count), bin.count)) 77 deltas = (bits.format_tsc(t2 - t1) for t1,t2 in zip(bin.times, bin.times[1:])) 78 testsuite.print_detail(" Times between first few observations: {}".format(" ".join("{:>6}".format(delta) for delta in deltas))) 79 80 if smi_count_delta is not None: 81 testsuite.print_detail("{} SMI detected using MSR_SMI_COUNT (MSR {:#x})".format(smi_count_delta, MSR_SMI_COUNT)) 82 83 testsuite.print_detail("Summary of impact: observed maximum latency = {}".format(bits.format_tsc(max_latency))) 84 85def test_with_usb_disabled(): 86 if usb.handoff_to_os(): 87 smi_latency() 88 89def average_io_smi(port, value, count): 90 def f(): 91 tsc_start = bits.rdtsc() 92 bits.outb(port, value) 93 return bits.rdtsc() - tsc_start 94 counts = [f() for i in range(count)] 95 return sum(counts)/len(counts) 96 97def time_io_smi(port=0xb2, value=0, count=1000): 98 count_for_estimate = 10 99 start = time.time() 100 average_io_smi(port, value, count_for_estimate) 101 avg10 = time.time() - start 102 estimate = avg10 * count/count_for_estimate 103 if estimate > 1: 104 print "Running test, estimated time: {}s".format(int(estimate)) 105 average = average_io_smi(port, value, count) 106 print "Average of {} SMIs (via outb, port={:#x}, value={:#x}): {}".format(count, port, value, bits.format_tsc(average)) 107