summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/syscall/p_online.c
blob: e3156aeab1fa6b8b41d060ff92482a3c85dcd1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2019 Joyent, Inc.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/var.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/kstat.h>
#include <sys/uadmin.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <sys/debug.h>
#include <sys/policy.h>
#include <sys/smt.h>

/*
 * CPU state diagram
 *
 *                  P_SPARE
 * P_POWEROFF <---> P_OFFLINE <---> P_ONLINE <---> P_NOINTR
 *                  P_FAULTED
 *                  P_DISABLED
 */
int
p_online_internal_locked(processorid_t cpun, int new_status, int *old_status)
{
	cpu_t	*cp;
	int	status;
	int	error = 0;
	int	flags = 0;

	ASSERT(MUTEX_HELD(&cpu_lock));

	if (cpun == P_ALL_SIBLINGS) {
		if (new_status != P_DISABLED) {
			error = EINVAL;
			goto out;
		}

		return (smt_disable());
	}

	if ((cp = cpu_get(cpun)) == NULL) {
		error = EINVAL;
		goto out;
	}

	if (new_status & P_FORCED)
		flags = CPU_FORCED;
	*old_status = status = cpu_get_state(cp); /* get processor status */
	new_status &= ~P_FORCED;

	/*
	 * Perform credentials check.
	 */
	switch (new_status) {
	case P_STATUS:
		goto out;
	case P_ONLINE:
	case P_OFFLINE:
	case P_NOINTR:
	case P_FAULTED:
	case P_SPARE:
		if (secpolicy_ponline(CRED()) != 0)
			error = EPERM;
		break;
	case P_DISABLED:
	default:
		error = EINVAL;
		break;
	}

	if (error)
		goto out;

	/*
	 * return 0 if the CPU is already in the desired new state.
	 */
	if (status == new_status)
		goto out;

	switch (new_status) {
	case P_ONLINE:
		switch (status) {
		case P_POWEROFF:
			/*
			 * If CPU is powered off, power it on.
			 */
			if (error = cpu_poweron(cp))
				break;
			ASSERT(cpu_get_state(cp) == P_OFFLINE);
			/* FALLTHROUGH */
		case P_DISABLED:
		case P_OFFLINE:
		case P_FAULTED:
		case P_SPARE:
			/*
			 * If CPU is in one of the offline states,
			 * bring it online.
			 */
			error = cpu_online(cp, flags);
			break;
		case P_NOINTR:
			cpu_intr_enable(cp);
			break;
		}
		break;

	case P_OFFLINE:
		switch (status) {
		case P_NOINTR:
			/*
			 * Before we take the CPU offline, we first enable I/O
			 * interrupts.
			 */
			cpu_intr_enable(cp);
			/* FALLTHROUGH */
		case P_ONLINE:
		case P_DISABLED:
		case P_FAULTED:
		case P_SPARE:
			/*
			 * CPU is online, or in a special offline state.
			 * Take it offline.
			 */
			error = cpu_offline(cp, flags);
			break;
		case P_POWEROFF:
			/*
			 * If CPU is powered off, power it on.
			 */
			error = cpu_poweron(cp);
			break;
		}
		break;

	case P_NOINTR:
		switch (status) {
		case P_POWEROFF:
			/*
			 * if CPU is powered off, power it on.
			 */
			if (error = cpu_poweron(cp))
				break;
			ASSERT(cpu_get_state(cp) == P_OFFLINE);
			/* FALLTHROUGH */
		case P_DISABLED:
		case P_OFFLINE:
		case P_FAULTED:
		case P_SPARE:
			/*
			 * First, bring the CPU online.
			 */
			if (error = cpu_online(cp, flags))
				break;
			/* FALLTHROUGH */
		case P_ONLINE:
			/*
			 * CPU is now online.  Try to disable interrupts.
			 */
			error = cpu_intr_disable(cp);
			break;
		}
		break;

	case P_FAULTED:
		switch (status) {
		case P_POWEROFF:
			/*
			 * If CPU is powered off, power it on.
			 */
			if (error = cpu_poweron(cp))
				break;
			ASSERT(cpu_get_state(cp) == P_OFFLINE);
			/*FALLTHROUGH*/
		case P_DISABLED:
		case P_OFFLINE:
		case P_ONLINE:
		case P_NOINTR:
		case P_SPARE:
			/*
			 * Mark this CPU as faulted.
			 */
			error = cpu_faulted(cp, flags);
			break;
		}
		break;

	case P_SPARE:
		switch (status) {
		case P_POWEROFF:
			/*
			 * If CPU is powered off, power it on.
			 */
			if (error = cpu_poweron(cp))
				break;
			ASSERT(cpu_get_state(cp) == P_OFFLINE);
			/*FALLTHROUGH*/
		case P_DISABLED:
		case P_OFFLINE:
		case P_FAULTED:
		case P_ONLINE:
		case P_NOINTR:
			/*
			 * Mark this CPU as a spare.
			 */
			error = cpu_spare(cp, flags);
			break;
		}
		break;
	}
out:
	return (error);
}

int
p_online_internal(processorid_t cpun, int new_status, int *old_status)
{
	int rc;

	mutex_enter(&cpu_lock);		/* protects CPU states */
	rc = p_online_internal_locked(cpun, new_status, old_status);
	mutex_exit(&cpu_lock);

	return (rc);
}

/*
 * p_online(2) - get/change processor operational status.
 *
 *   As noted in os/cpu.c, the P_ONLINE and other state constants are for use
 *   only in this system call path and other paths conveying CPU state to
 *   userland.  In general, other kernel consumers should be using the accessor
 *   functions in uts/common/os/cpu.c.
 */
int
p_online(processorid_t cpun, int new_status)
{
	int ret;
	int old_status;

	ret = p_online_internal(cpun, new_status, &old_status);
	if (ret != 0)
		return (set_errno(ret));
	return (old_status);
}