xref: /illumos-gate/usr/src/compat/bhyve/sys/cpuset.h (revision d0b3c59ba652f183eeec1414dd9fbdc56bf05cc8)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2014 Pluribus Networks Inc.
14  * Copyright 2019 Joyent, Inc.
15  */
16 
17 #ifndef _COMPAT_FREEBSD_SYS_CPUSET_H_
18 #define	_COMPAT_FREEBSD_SYS_CPUSET_H_
19 
20 #define	NOCPU			-1
21 
22 #ifdef	_KERNEL
23 
24 #include <sys/_cpuset.h>
25 
26 #define	CPU_SET(cpu, set)		cpuset_add((set), (cpu))
27 #define	CPU_SETOF(cpu, set)		cpuset_only((set), (cpu))
28 #define	CPU_ZERO(set)			cpuset_zero((cpuset_t *)(set))
29 #define	CPU_CLR(cpu, set)		cpuset_del((set), (cpu))
30 #define	CPU_EMPTY(set)			cpuset_isnull((set))
31 #define	CPU_FFS(set)			cpusetobj_ffs(set)
32 #define	CPU_ISSET(cpu, set)		cpu_in_set((cpuset_t *)(set), (cpu))
33 #define	CPU_AND(dst, src)		cpuset_and(			\
34 						(cpuset_t *)(dst),	\
35 						(cpuset_t *)(src))
36 #define	CPU_OR(dst, src)		cpuset_or(			\
37 						(cpuset_t *)(dst),	\
38 						(cpuset_t *)(src))
39 #define	CPU_CMP(set1, set2)		(cpuset_isequal(		\
40 						(cpuset_t *)(set1),	\
41 						(cpuset_t *)(set2)) == 0)
42 #define	CPU_SET_ATOMIC(cpu, set)	cpuset_atomic_add(		\
43 						(cpuset_t *)(set),	\
44 						(cpu))
45 #define	CPU_CLR_ATOMIC(cpu, set)	cpuset_atomic_del(		\
46 						(cpuset_t *)(set),	\
47 						(cpu))
48 
49 #define	CPU_SET_ATOMIC_ACQ(cpu, set)	cpuset_atomic_add((set), (cpu))
50 
51 
52 int	cpusetobj_ffs(const cpuset_t *set);
53 
54 #else
55 
56 #include <sys/bitmap.h>
57 #include <machine/atomic.h>
58 #include <machine/cpufunc.h>
59 
60 /* For now, assume NCPU of 256 */
61 #define	CPU_SETSIZE			(256)
62 
63 typedef struct {
64 	ulong_t _bits[BT_BITOUL(CPU_SETSIZE)];
65 } cpuset_t;
66 
67 static __inline int
cpuset_isempty(const cpuset_t * set)68 cpuset_isempty(const cpuset_t *set)
69 {
70 	uint_t i;
71 
72 	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
73 		if (set->_bits[i] != 0)
74 			return (0);
75 	}
76 	return (1);
77 }
78 
79 static __inline void
cpuset_zero(cpuset_t * dst)80 cpuset_zero(cpuset_t *dst)
81 {
82 	uint_t i;
83 
84 	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
85 		dst->_bits[i] = 0;
86 	}
87 }
88 
89 static __inline int
cpuset_isequal(cpuset_t * s1,cpuset_t * s2)90 cpuset_isequal(cpuset_t *s1, cpuset_t *s2)
91 {
92 	uint_t i;
93 
94 	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
95 		if (s1->_bits[i] != s2->_bits[i])
96 			return (0);
97 	}
98 	return (1);
99 }
100 
101 static __inline uint_t
cpusetobj_ffs(const cpuset_t * set)102 cpusetobj_ffs(const cpuset_t *set)
103 {
104 	uint_t i, cbit;
105 
106 	cbit = 0;
107 	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
108 		if (set->_bits[i] != 0) {
109 			cbit = ffsl(set->_bits[i]);
110 			cbit += i * sizeof (set->_bits[0]);
111 			break;
112 		}
113 	}
114 	return (cbit);
115 }
116 
117 
118 #define	CPU_SET(cpu, setp)		BT_SET((setp)->_bits, cpu)
119 #define	CPU_CLR(cpu, setp)		BT_CLEAR((setp)->_bits, cpu)
120 #define	CPU_ZERO(setp)			cpuset_zero((setp))
121 #define	CPU_CMP(set1, set2)		(cpuset_isequal(		\
122 						(cpuset_t *)(set1),	\
123 						(cpuset_t *)(set2)) == 0)
124 #define	CPU_FFS(set)			cpusetobj_ffs(set)
125 #define	CPU_ISSET(cpu, setp)		BT_TEST((setp)->_bits, cpu)
126 #define	CPU_EMPTY(setp)			cpuset_isempty((setp))
127 #define	CPU_SET_ATOMIC(cpu, setp)	\
128 	atomic_set_long(&(BT_WIM((setp)->_bits, cpu)), BT_BIW(cpu))
129 #define	CPU_CLR_ATOMIC(cpu, setp)	\
130 	atomic_clear_long(&(BT_WIM((setp)->_bits, cpu)), BT_BIW(cpu))
131 
132 #endif
133 
134 #endif	/* _COMPAT_FREEBSD_SYS_CPUSET_H_ */
135