From: Konrad Rzeszutek Wilk Subject: [PATCH v4 1/3] libxl/cpumap: Add xc_cpumap_[setcpu, clearcpu, testcpu] to complement xc_cpumap_alloc. Date: Fri, 3 Apr 2015 20:43:01 -0400 Message-ID: <1428108183-28538-2-git-send-email-konrad.wilk@oracle.com> References: <1428108183-28538-1-git-send-email-konrad.wilk@oracle.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YeCB9-0006f9-0X for xen-devel@lists.xenproject.org; Sat, 04 Apr 2015 00:43:15 +0000 In-Reply-To: <1428108183-28538-1-git-send-email-konrad.wilk@oracle.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: george.dunlap@eu.citrix.com, ian.campbell@citrix.com, ian.jackson@citrix.com, wei.liu2@citrix.com, xen-devel@lists.xenproject.org Cc: Konrad Rzeszutek Wilk List-Id: xen-devel@lists.xenproject.org We export the xc_cpumap_alloc but not the bit operations. One could include 'xc_bitops.h' but that is naughty - so instead we just export the proper functions to do it on the xc_cpumap_t typedef. Signed-off-by: Konrad Rzeszutek Wilk ---- v2: Use our own macro to make sure ARM is not affected negatively --- tools/libxc/include/xenctrl.h | 9 +++++++++ tools/libxc/xc_misc.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h index 44c7ac0..0b08747 100644 --- a/tools/libxc/include/xenctrl.h +++ b/tools/libxc/include/xenctrl.h @@ -394,6 +394,15 @@ int xc_get_cpumap_size(xc_interface *xch); /* allocate a cpumap */ xc_cpumap_t xc_cpumap_alloc(xc_interface *xch); +/* clear an CPU from the cpumap. */ +void xc_cpumap_clearcpu(int cpu, xc_cpumap_t map); + +/* set an CPU in the cpumap. */ +void xc_cpumap_setcpu(int cpu, xc_cpumap_t map); + +/* Test whether the CPU in cpumap is set. */ +int xc_cpumap_testcpu(int cpu, xc_cpumap_t map); + /* * NODEMAP handling */ diff --git a/tools/libxc/xc_misc.c b/tools/libxc/xc_misc.c index be68291..e113385 100644 --- a/tools/libxc/xc_misc.c +++ b/tools/libxc/xc_misc.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "xc_bitops.h" #include "xc_private.h" #include @@ -93,6 +94,35 @@ xc_cpumap_t xc_cpumap_alloc(xc_interface *xch) return calloc(1, sz); } +/* + * xc_bitops.h has macros that do this as well - however xc_cpumap_t + * is an array of uint8 (1byte) while said macros expect an array + * of unsigned long (8 bytes). This is not a problem when setting + * an bit (as the computation will come with the same offset) - the + * issue is when clearing an bit. The aligment on ARM could affect + * other structures that are close in memory causing memory corruption. + * Hence our own macros that differ only in that we compute the + * size of the array elements (sizeof(*map)) as opposed to assuming + * it is unsigned long. + */ +#define BITS_PER_CPUMAP(map) (sizeof(*map) * 8) +#define CPUMAP_ENTRY(cpu, map) ((map))[(cpu) / BITS_PER_CPUMAP(map)] +#define CPUMAP_SHIFT(cpu, map) ((cpu) % BITS_PER_CPUMAP(map)) +void xc_cpumap_clearcpu(int cpu, xc_cpumap_t map) +{ + CPUMAP_ENTRY(cpu, map) &= ~(1U << CPUMAP_SHIFT(cpu, map)); +} + +void xc_cpumap_setcpu(int cpu, xc_cpumap_t map) +{ + CPUMAP_ENTRY(cpu, map) |= (1U << CPUMAP_SHIFT(cpu, map)); +} + +int xc_cpumap_testcpu(int cpu, xc_cpumap_t map) +{ + return (CPUMAP_ENTRY(cpu, map) >> CPUMAP_SHIFT(cpu, map)) & 1; +} + xc_nodemap_t xc_nodemap_alloc(xc_interface *xch) { int sz; -- 2.1.0