1 #include <linux/module.h>
2 #include <linux/preempt.h>
5 struct msr
*msrs_alloc(void)
7 struct msr
*msrs
= NULL
;
9 msrs
= alloc_percpu(struct msr
);
11 pr_warn("%s: error allocating msrs\n", __func__
);
17 EXPORT_SYMBOL(msrs_alloc
);
19 void msrs_free(struct msr
*msrs
)
23 EXPORT_SYMBOL(msrs_free
);
26 * Read an MSR with error handling
29 * @m: value to read into
31 * It returns read data only on success, otherwise it doesn't change the output
35 int msr_read(u32 msr
, struct msr
*m
)
40 err
= rdmsrl_safe(msr
, &val
);
48 * Write an MSR with error handling
53 int msr_write(u32 msr
, struct msr
*m
)
55 return wrmsrl_safe(msr
, m
->q
);
58 static inline int __flip_bit(u32 msr
, u8 bit
, bool set
)
66 err
= msr_read(msr
, &m
);
79 err
= msr_write(msr
, &m1
);
87 * Set @bit in a MSR @msr.
90 * < 0: An error was encountered.
91 * = 0: Bit was already set.
92 * > 0: Hardware accepted the MSR write.
94 int msr_set_bit(u32 msr
, u8 bit
)
96 return __flip_bit(msr
, bit
, true);
100 * Clear @bit in a MSR @msr.
103 * < 0: An error was encountered.
104 * = 0: Bit was already cleared.
105 * > 0: Hardware accepted the MSR write.
107 int msr_clear_bit(u32 msr
, u8 bit
)
109 return __flip_bit(msr
, bit
, false);