.\" $NetBSD: rdmsr.9,v 1.4 2017/02/17 22:31:08 christos Exp $
.\"
.\" Copyright (c) 2011 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jukka Ruohonen.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd February 17, 2017
.Dt RDMSR 9 x86
.Os
.Sh NAME
.Nm msr ,
.Nm rdmsr ,
.Nm rdmsr_safe ,
.Nm wrmsr
.Nd functions for x86 MSRs
.Sh SYNOPSIS
.In x86/cpufunc.h
.Ft uint64_t
.Fn rdmsr "u_int msr"
.\" .Ft uint64_t
.\" .Fn rdmsr_locked "u_int msr"
.Ft int
.Fn rdmsr_safe "u_int msr" "uint64_t *valp"
.Ft void
.Fn wrmsr "u_int msr" "uint64_t val"
.\" .Ft void
.\" .Fn wrmsr_locked "u_int msr" "u_int" "uint64_t val"
.Sh DESCRIPTION
The
.Dv RDMSR
instruction reads from a x86 model-specific register
.Pq Dv MSR .
Conversely, the
.Dv WRMSR
instruction is used to write to a
.Dv MSR .
In
.Nx
the
.Fn rdmsr ,
.Fn rdmsr_safe ,
and
.Fn wrmsr
functions are used to access
.Dv MSRs .
The header
.In x86/specialreg.h
includes definitions for some of the commonly used MSRs,
that is, control registers that are present
in some x86 processor models but unavailable in others.
.Sh FUNCTIONS
.Bl -tag -width abcd
.It Fn rdmsr "msr"
Returns the value read from
.Fa msr .
.\" .It Fn rdmsr_locked "msr"
.It Fn rdmsr_safe "msr" "valp"
The
.Fn rdmsr_safe
function is a safer variant of
.Fn rdmsr .
Upon successful completion,
the function returns zero and the value read from the register
.Fa msr
is returned in
.Fa valp .
If a fault occurs while accessing
.Fa msr ,
.Fn rdmsr_safe
returns
.Dv EFAULT .
.It Fn wrmsr "msr" "val"
The
.Fn wrmsr
function writes
.Fa val
to the register
.Fa msr .
.\" .It Fn wrmsr_locked "msr" "xxx" "val"
.El
.Pp
Note that even though
.Fn rdmsr_safe
provides support for reading
.Dv MSRs
in a safe manner, it is still a good practice
to always verify that the given model-specific register
is present by using the
.Dv CPUID
instruction, available in
.Nx
via
.Fn x86_cpuid .
.Sh SEE ALSO
.Xr rdtsc 9 ,
.Xr x86/x86_msr_xcall 9