These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / transitions / librm_test.c
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * Real mode transition self-tests
29  *
30  * This file allows for easy measurement of the time taken to perform
31  * real mode transitions, which may have a substantial overhead when
32  * running under a hypervisor.
33  *
34  */
35
36 /* Forcibly enable assertions */
37 #undef NDEBUG
38
39 #include <ipxe/test.h>
40 #include <ipxe/profile.h>
41 #include <realmode.h>
42
43 /** Number of sample iterations for profiling */
44 #define PROFILE_COUNT 4096
45
46 /** Protected-to-real mode transition profiler */
47 static struct profiler p2r_profiler __profiler = { .name = "p2r" };
48
49 /** Real-to-protected mode transition profiler */
50 static struct profiler r2p_profiler __profiler = { .name = "r2p" };
51
52 /** Real-mode call profiler */
53 static struct profiler real_call_profiler __profiler = { .name = "real_call" };
54
55 /** Protected-mode call profiler */
56 static struct profiler prot_call_profiler __profiler = { .name = "prot_call" };
57
58 /**
59  * Dummy protected-mode function
60  */
61 static void librm_test_prot_call ( void ) {
62         /* Do nothing */
63 }
64
65 /**
66  * Perform real mode transition self-tests
67  *
68  */
69 static void librm_test_exec ( void ) {
70         unsigned int i;
71         unsigned long timestamp;
72         unsigned long started;
73         unsigned long stopped;
74         unsigned int discard_d;
75
76         /* Profile mode transitions.  We want to profile each
77          * direction of the transition separately, so perform an RDTSC
78          * while in real mode and tweak the profilers' start/stop
79          * times appropriately.
80          */
81         for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
82                 profile_start ( &p2r_profiler );
83                 __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t" )
84                                        : "=a" ( timestamp ), "=d" ( discard_d )
85                                        : );
86                 profile_start_at ( &r2p_profiler, timestamp );
87                 profile_stop ( &r2p_profiler );
88                 profile_stop_at ( &p2r_profiler, timestamp );
89         }
90
91         /* Profile complete real-mode call cycle */
92         for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
93                 profile_start ( &real_call_profiler );
94                 __asm__ __volatile__ ( REAL_CODE ( "" ) : : );
95                 profile_stop ( &real_call_profiler );
96         }
97
98         /* Profile complete protected-mode call cycle */
99         for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
100                 __asm__ __volatile__ ( REAL_CODE ( "rdtsc\n\t"
101                                                    "movl %0, %2\n\t"
102                                                    "pushl %3\n\t"
103                                                    "pushw %%cs\n\t"
104                                                    "call prot_call\n\t"
105                                                    "addw $4, %%sp\n\t"
106                                                    "rdtsc\n\t" )
107                                        : "=a" ( stopped ), "=d" ( discard_d ),
108                                          "=r" ( started )
109                                        : "i" ( librm_test_prot_call ) );
110                 profile_start_at ( &prot_call_profiler, started );
111                 profile_stop_at ( &prot_call_profiler, stopped );
112         }
113 }
114
115 /** Real mode transition self-test */
116 struct self_test librm_test __self_test = {
117         .name = "librm",
118         .exec = librm_test_exec,
119 };
120
121 REQUIRING_SYMBOL ( librm_test );
122 REQUIRE_OBJECT ( test );