1794a9a6cSJohn Baldwin.\" Copyright (c) 2008 Yahoo!, Inc. 2794a9a6cSJohn Baldwin.\" All rights reserved. 3794a9a6cSJohn Baldwin.\" Written by: John Baldwin <jhb@FreeBSD.org> 4794a9a6cSJohn Baldwin.\" 5794a9a6cSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without 6794a9a6cSJohn Baldwin.\" modification, are permitted provided that the following conditions 7794a9a6cSJohn Baldwin.\" are met: 8794a9a6cSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright 9794a9a6cSJohn Baldwin.\" notice, this list of conditions and the following disclaimer. 10794a9a6cSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright 11794a9a6cSJohn Baldwin.\" notice, this list of conditions and the following disclaimer in the 12794a9a6cSJohn Baldwin.\" documentation and/or other materials provided with the distribution. 13794a9a6cSJohn Baldwin.\" 3. Neither the name of the author nor the names of any co-contributors 14794a9a6cSJohn Baldwin.\" may be used to endorse or promote products derived from this software 15794a9a6cSJohn Baldwin.\" without specific prior written permission. 16794a9a6cSJohn Baldwin.\" 17794a9a6cSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18794a9a6cSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19794a9a6cSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20794a9a6cSJohn Baldwin.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21794a9a6cSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22794a9a6cSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23794a9a6cSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24794a9a6cSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25794a9a6cSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26794a9a6cSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27794a9a6cSJohn Baldwin.\" SUCH DAMAGE. 28794a9a6cSJohn Baldwin.\" 29794a9a6cSJohn Baldwin.\" $FreeBSD$ 30794a9a6cSJohn Baldwin.\" 31ccd8bad0SRobert Watson.Dd February 28, 2010 32794a9a6cSJohn Baldwin.Dt KVM_GETPCPU 3 33794a9a6cSJohn Baldwin.Os 34794a9a6cSJohn Baldwin.Sh NAME 35ccd8bad0SRobert Watson.Nm kvm_dpcpu_setcpu 36794a9a6cSJohn Baldwin.Nm kvm_getmaxcpu , 37794a9a6cSJohn Baldwin.Nm kvm_getpcpu 38794a9a6cSJohn Baldwin.Nd access per-CPU data 39794a9a6cSJohn Baldwin.Sh LIBRARY 40794a9a6cSJohn Baldwin.Lb libkvm 41794a9a6cSJohn Baldwin.Sh SYNOPSIS 42794a9a6cSJohn Baldwin.In sys/param.h 43794a9a6cSJohn Baldwin.In sys/pcpu.h 44794a9a6cSJohn Baldwin.In sys/sysctl.h 45794a9a6cSJohn Baldwin.In kvm.h 46794a9a6cSJohn Baldwin.Ft int 47ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu "kvm_t *kd" "u_int cpu" 48ccd8bad0SRobert Watson.Ft int 49794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu "kvm_t *kd" 50794a9a6cSJohn Baldwin.Ft void * 51794a9a6cSJohn Baldwin.Fn kvm_getpcpu "kvm_t *kd" "int cpu" 52794a9a6cSJohn Baldwin.Sh DESCRIPTION 53794a9a6cSJohn BaldwinThe 54ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu , 55ccd8bad0SRobert Watson.Fn kvm_getmaxcpu , 56794a9a6cSJohn Baldwinand 57794a9a6cSJohn Baldwin.Fn kvm_getpcpu 58794a9a6cSJohn Baldwinfunctions are used to access the per-CPU data of active processors in the 59794a9a6cSJohn Baldwinkernel indicated by 60794a9a6cSJohn Baldwin.Fa kd . 61ccd8bad0SRobert WatsonPer-CPU storage comes in two flavours: data stored directly in a 62ccd8bad0SRobert Watson.Vt "struct pcpu" 63ccd8bad0SRobert Watsonassociated with each CPU, and dynamic per-CPU storage (DPCPU), in which a 64ccd8bad0SRobert Watsonsingle kernel symbol refers to different data depending on what CPU it is 65ccd8bad0SRobert Watsonaccessed from. 66ccd8bad0SRobert Watson.Pp 67794a9a6cSJohn BaldwinThe 68794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu 69794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel. 70ccd8bad0SRobert Watson.Pp 71794a9a6cSJohn BaldwinThe 72794a9a6cSJohn Baldwin.Fn kvm_getpcpu 73794a9a6cSJohn Baldwinfunction returns a buffer holding the per-CPU data for a single CPU. 74794a9a6cSJohn BaldwinThis buffer is described by the 75794a9a6cSJohn Baldwin.Vt "struct pcpu" 76794a9a6cSJohn Baldwintype. 77794a9a6cSJohn BaldwinThe caller is responsible for releasing the buffer via a call to 78794a9a6cSJohn Baldwin.Xr free 3 79794a9a6cSJohn Baldwinwhen it is no longer needed. 80794a9a6cSJohn BaldwinIf 81794a9a6cSJohn Baldwin.Fa cpu 82794a9a6cSJohn Baldwinis not active, then 83794a9a6cSJohn Baldwin.Dv NULL 84794a9a6cSJohn Baldwinis returned instead. 85ccd8bad0SRobert Watson.Pp 86ccd8bad0SRobert WatsonSymbols for dynamic per-CPU data are accessed via 87ccd8bad0SRobert Watson.Xr kvm_nlist 3 88ccd8bad0SRobert Watsonas with other symbols. 89ccd8bad0SRobert Watson.Nm libkvm 90ccd8bad0SRobert Watsonmaintains a notion of the "current CPU", set by 91ccd8bad0SRobert Watson.Xr kvm_dpcpu_setcpu , 92ccd8bad0SRobert Watsonwhich defaults to 0. 93ccd8bad0SRobert WatsonOnce another CPU is selected, 94ccd8bad0SRobert Watson.Xr kvm_nlist 3 95ccd8bad0SRobert Watsonwill return pointers to that data on the appropriate CPU. 96cd11f01eSJohn Baldwin.Sh CACHING 97ccd8bad0SRobert Watson.Fn kvm_getmaxcpu 98ccd8bad0SRobert Watsonand 9963d46d1dSUlrich Spörlein.Fn kvm_getpcpu 100ccd8bad0SRobert Watsoncache the nlist values for various kernel variables which are 101cd11f01eSJohn Baldwinreused in successive calls. 102cd11f01eSJohn BaldwinYou may call either function with 103cd11f01eSJohn Baldwin.Fa kd 104cd11f01eSJohn Baldwinset to 105cd11f01eSJohn Baldwin.Dv NULL 106cd11f01eSJohn Baldwinto clear this cache. 107794a9a6cSJohn Baldwin.Sh RETURN VALUES 108794a9a6cSJohn BaldwinOn success, the 109794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu 110794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel. 111794a9a6cSJohn BaldwinIf an error occurs, 112794a9a6cSJohn Baldwinit returns -1 instead. 113794a9a6cSJohn Baldwin.Pp 114794a9a6cSJohn BaldwinOn success, the 115794a9a6cSJohn Baldwin.Fn kvm_getpcpu 116794a9a6cSJohn Baldwinfunction returns a pointer to an allocated buffer or 117794a9a6cSJohn Baldwin.Dv NULL. 118794a9a6cSJohn BaldwinIf an error occurs, 119794a9a6cSJohn Baldwinit returns -1 instead. 120794a9a6cSJohn Baldwin.Pp 121ccd8bad0SRobert WatsonOn success, the 122ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu 123ccd8bad0SRobert Watsoncall returns 0; if an error occurs, it returns -1 instead. 124ccd8bad0SRobert Watson.Pp 125ccd8bad0SRobert WatsonIf any function encounters an error, 126794a9a6cSJohn Baldwinthen an error message may be retrieved via 127794a9a6cSJohn Baldwin.Xr kvm_geterr 3. 128794a9a6cSJohn Baldwin.Sh SEE ALSO 129794a9a6cSJohn Baldwin.Xr free 3 , 130794a9a6cSJohn Baldwin.Xr kvm 3 131