x86: allow reading MSR_IA32_TSC with XENPF_resource_op

Memory bandwidth monitoring requires system time information returned
along with the monitoring counter to verify the correctness of the
counter value and to calculate the time elapsed between two samplings.

Add MSR_IA32_TSC to the read path and it returns scaled system time(ns)
instead of raw timestamp to elimanate the needs to convert. The return
time is obfuscated with booting random to eliminate the potential abuse
of it. RESOURCE_ACCESS_MAX_ENTRIES is also increased to 3 so MSR_IA32_TSC
can be used together with an MSR write/read operation pair.

Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>

Fix uninitialized variable build error.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
This commit is contained in:
Chao Peng authored and Jan Beulich committed 2015-01-28 16:36:36 +01:00
1 parent c2ba001caf
commit f6c9698d7f
4 files changed
+57 -3

No files matched your search

+35 -3
View File
@@ -61,7 +61,7 @@ long cpu_down_helper(void *data);
long core_parking_helper(void *data);
uint32_t get_cur_idle_nums(void);
#define RESOURCE_ACCESS_MAX_ENTRIES 2
#define RESOURCE_ACCESS_MAX_ENTRIES 3
struct xen_resource_access {
unsigned int nr_done;
unsigned int nr_entries;
@@ -75,6 +75,7 @@ static bool_t allow_access_msr(unsigned int msr)
/* MSR for CMT, refer to chapter 17.14 of Intel SDM. */
case MSR_IA32_CMT_EVTSEL:
case MSR_IA32_CMT_CTR:
case MSR_IA32_TSC:
return 1;
}
@@ -124,6 +125,7 @@ static void resource_access(void *info)
{
struct xen_resource_access *ra = info;
unsigned int i;
u64 tsc = 0;
for ( i = 0; i < ra->nr_done; i++ )
{
@@ -133,10 +135,40 @@ static void resource_access(void *info)
switch ( entry->u.cmd )
{
case XEN_RESOURCE_OP_MSR_READ:
ret = rdmsr_safe(entry->idx, entry->val);
if ( unlikely(entry->idx == MSR_IA32_TSC) )
{
/* Return obfuscated scaled time instead of raw timestamp */
entry->val = get_s_time_fixed(tsc)
+ SECONDS(boot_random) - boot_random;
ret = 0;
}
else
{
unsigned long flags = 0;
/*
* If next entry is MSR_IA32_TSC read, then the actual rdtscll
* is performed together with current entry, with IRQ disabled.
*/
bool_t read_tsc = (i < ra->nr_done - 1 &&
unlikely(entry[1].idx == MSR_IA32_TSC));
if ( unlikely(read_tsc) )
local_irq_save(flags);
ret = rdmsr_safe(entry->idx, entry->val);
if ( unlikely(read_tsc) )
{
rdtscll(tsc);
local_irq_restore(flags);
}
}
break;
case XEN_RESOURCE_OP_MSR_WRITE:
ret = wrmsr_safe(entry->idx, entry->val);
if ( unlikely(entry->idx == MSR_IA32_TSC) )
ret = -EPERM;
else
ret = wrmsr_safe(entry->idx, entry->val);
break;
default:
BUG();
+9
View File
@@ -1,9 +1,11 @@
#include <xen/init.h>
#include <xen/percpu.h>
#include <xen/random.h>
#include <xen/time.h>
#include <asm/random.h>
static DEFINE_PER_CPU(unsigned int, seed);
unsigned int __read_mostly boot_random;
unsigned int get_random(void)
{
@@ -27,3 +29,10 @@ unsigned int get_random(void)
return val;
}
static int __init init_boot_random(void)
{
boot_random = get_random();
return 0;
}
__initcall(init_boot_random);
+10
View File
@@ -540,6 +540,16 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_core_parking_t);
#define XEN_RESOURCE_OP_MSR_READ 0
#define XEN_RESOURCE_OP_MSR_WRITE 1
/*
* Specially handled MSRs:
* - MSR_IA32_TSC
* READ: Returns the scaled system time(ns) instead of raw timestamp. In
* multiple entry case, if other MSR read is followed by a MSR_IA32_TSC
* read, then both reads are guaranteed to be performed atomically (with
* IRQ disabled). The return time indicates the point of reading that MSR.
* WRITE: Not supported.
*/
struct xenpf_resource_entry {
union {
uint32_t cmd; /* IN: XEN_RESOURCE_OP_* */
+3
View File
@@ -3,4 +3,7 @@
unsigned int get_random(void);
/* The value keeps unchange once initialized for each booting */
extern unsigned int boot_random;
#endif /* __XEN_RANDOM_H__ */
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.