1 /* $NetBSD: cpuset.c,v 1.21 2019/05/11 11:53:55 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _STANDALONE
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: cpuset.c,v 1.21 2019/05/11 11:53:55 maxv Exp $");
36 #endif /* LIBC_SCCS and not lint */
37
38 #ifdef _LIBC
39 #include "namespace.h"
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/sched.h>
44 #ifdef _KERNEL
45 #include <sys/kmem.h>
46 #include <lib/libkern/libkern.h>
47 #include <sys/atomic.h>
48 #else
49 #include <errno.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <sys/sysctl.h>
53 #endif
54
55 #define CPUSET_SHIFT 5
56 #define CPUSET_MASK 31
57 #define CPUSET_NENTRIES(nc) (((nc) >> CPUSET_SHIFT) + 1)
58 #ifndef __lint__
59 #define CPUSET_SIZE(n) (sizeof( \
60 struct { \
61 uint32_t bits[0]; \
62 }) + sizeof(uint32_t) * (n))
63 #else
64 #define CPUSET_SIZE(n) 0
65 #endif
66
67 struct _cpuset {
68 uint32_t bits[0];
69 };
70
71 #ifndef _KERNEL
72
73 static size_t cpuset_size = 0;
74 static size_t cpuset_nentries = 0;
75
76 size_t
77 /*ARGSUSED*/
_cpuset_size(const cpuset_t * c)78 _cpuset_size(const cpuset_t *c)
79 {
80
81 return cpuset_size;
82 }
83
84 void
_cpuset_zero(cpuset_t * c)85 _cpuset_zero(cpuset_t *c)
86 {
87
88 memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0]));
89 }
90
91 int
_cpuset_isset(cpuid_t i,const cpuset_t * c)92 _cpuset_isset(cpuid_t i, const cpuset_t *c)
93 {
94 const unsigned long j = i >> CPUSET_SHIFT;
95
96 if (j >= cpuset_nentries) {
97 errno = EINVAL;
98 return -1;
99 }
100 return ((1U << (unsigned int)(i & CPUSET_MASK)) & c->bits[j]) != 0;
101 }
102
103 int
_cpuset_set(cpuid_t i,cpuset_t * c)104 _cpuset_set(cpuid_t i, cpuset_t *c)
105 {
106 const unsigned long j = i >> CPUSET_SHIFT;
107
108 if (j >= cpuset_nentries) {
109 errno = EINVAL;
110 return -1;
111 }
112 c->bits[j] |= 1U << (unsigned int)(i & CPUSET_MASK);
113 return 0;
114 }
115
116 int
_cpuset_clr(cpuid_t i,cpuset_t * c)117 _cpuset_clr(cpuid_t i, cpuset_t *c)
118 {
119 const unsigned long j = i >> CPUSET_SHIFT;
120
121 if (j >= cpuset_nentries) {
122 errno = EINVAL;
123 return -1;
124 }
125 c->bits[j] &= ~(1U << (unsigned int)(i & CPUSET_MASK));
126 return 0;
127 }
128
129 cpuset_t *
_cpuset_create(void)130 _cpuset_create(void)
131 {
132
133 if (cpuset_size == 0) {
134 static int mib[2] = { CTL_HW, HW_NCPU };
135 size_t len;
136 u_int nc;
137
138 len = sizeof(nc);
139 if (sysctl(mib, (unsigned int)__arraycount(mib), &nc, &len,
140 NULL, 0) == -1)
141 return NULL;
142
143 cpuset_nentries = CPUSET_NENTRIES(nc);
144 cpuset_size = CPUSET_SIZE(cpuset_nentries);
145 }
146 return calloc(1, cpuset_size);
147 }
148
149 void
_cpuset_destroy(cpuset_t * c)150 _cpuset_destroy(cpuset_t *c)
151 {
152
153 free(c);
154 }
155
156 #endif
157 #endif
158