summaryrefslogtreecommitdiff
path: root/sys/compat/netbsd32/netbsd32_sysctl.c
blob: 604fab82bb109c6a2278713ea9ae99cec76c76f5 (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
/*	$NetBSD: netbsd32_sysctl.c,v 1.4 2001/11/13 02:09:09 lukem Exp $	*/

/*
 * Copyright (c) 1998, 2001 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.4 2001/11/13 02:09:09 lukem Exp $");

#if defined(_KERNEL_OPT)
#include "opt_ddb.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <sys/syscallargs.h>
#include <sys/proc.h>
#define	__SYSCTL_PRIVATE
#include <sys/sysctl.h>

#include <uvm/uvm_extern.h>

#include <compat/netbsd32/netbsd32.h>
#include <compat/netbsd32/netbsd32_syscall.h>
#include <compat/netbsd32/netbsd32_syscallargs.h>
#include <compat/netbsd32/netbsd32_conv.h>

#if defined(DDB)
#include <ddb/ddbvar.h>
#endif

int uvm_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
int kern_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);

/*
 * uvm_sysctl32: sysctl hook into UVM system, handling special 32-bit
 * sensitive calls.
 */
int
uvm_sysctl32(name, namelen, oldp, oldlenp, newp, newlen, p)
	int *name;
	u_int namelen;
	void *oldp;
	size_t *oldlenp;
	void *newp;
	size_t newlen;
	struct proc *p;
{
	struct netbsd32_loadavg av32;

	/* all sysctl names at this level are terminal */
	if (namelen != 1)
		return (ENOTDIR);		/* overloaded */

	switch (name[0]) {
	case VM_LOADAVG:
		netbsd32_from_loadavg(&av32, &averunnable);
		return (sysctl_rdstruct(oldp, oldlenp, newp, &av32,
		    sizeof(av32)));

	default:
		return (EOPNOTSUPP);
	}
	/* NOTREACHED */
}

/*
 * kern_sysctl32: sysctl hook into KERN system, handling special 32-bit
 * sensitive calls.
 */
int
kern_sysctl32(name, namelen, oldp, oldlenp, newp, newlen, p)
	int *name;
	u_int namelen;
	void *oldp;
	size_t *oldlenp;
	void *newp;
	size_t newlen;
	struct proc *p;
{
	struct netbsd32_timeval bt32;

	/* All sysctl names at this level, except for a few, are terminal. */
	switch (name[0]) {
#if 0
	case KERN_PROC:
	case KERN_PROC2:
	case KERN_PROF:
	case KERN_MBUF:
	case KERN_PROC_ARGS:
	case KERN_SYSVIPC_INFO:
		/* Not terminal. */
		break;
#endif
	default:
		if (namelen != 1)
			return (ENOTDIR);	/* overloaded */
	}

	switch (name[0]) {
	case KERN_BOOTTIME:
		netbsd32_from_timeval(&boottime, &bt32);
		return (sysctl_rdstruct(oldp, oldlenp, newp, &bt32,
		    sizeof(struct netbsd32_timeval)));

	default:
		return (EOPNOTSUPP);
	}
	/* NOTREACHED */
}

int
netbsd32___sysctl(p, v, retval)
	struct proc *p;
	void *v;
	register_t *retval;
{
	struct netbsd32___sysctl_args /* {
		syscallarg(netbsd32_intp) name;
		syscallarg(u_int) namelen;
		syscallarg(netbsd32_voidp) old;
		syscallarg(netbsd32_size_tp) oldlenp;
		syscallarg(netbsd32_voidp) new;
		syscallarg(netbsd32_size_t) newlen;
	} */ *uap = v;
	int error;
	netbsd32_size_t savelen = 0;
	size_t oldlen = 0;
	sysctlfn *fn;
	int name[CTL_MAXNAME];

/*
 * Some of these sysctl functions do their own copyin/copyout.
 * We need to disable or emulate the ones that need their
 * arguments converted.
 */

	if (SCARG(uap, new) != NULL &&
	    (error = suser(p->p_ucred, &p->p_acflag)))
		return (error);
	/*
	 * all top-level sysctl names are non-terminal
	 */
	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
		return (EINVAL);
	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
		       SCARG(uap, namelen) * sizeof(int));
	if (error)
		return (error);

	switch (name[0]) {
	case CTL_KERN:
		switch (name[1]) {
#if 0
		case KERN_FILE:
		case KERN_NTPTIME:
		case KERN_SYSVIPC_INFO:
#endif
		case KERN_BOOTTIME:
			fn = kern_sysctl32;
			break;
		default:
			fn = kern_sysctl;
			break;
		}
		break;
	case CTL_HW:
		fn = hw_sysctl;
		break;
	case CTL_VM:
		switch (name[1]) {
		case VM_LOADAVG:
			fn = uvm_sysctl32;	/* need to convert a `long' */
			break;
		default:
			fn = uvm_sysctl;
			break;
		}
		break;
	case CTL_NET:
		fn = net_sysctl;
		break;
	case CTL_VFS:
		fn = vfs_sysctl;
		break;
	case CTL_MACHDEP:
		fn = cpu_sysctl;
		break;
#ifdef DEBUG
	case CTL_DEBUG:
		fn = debug_sysctl;
		break;
#endif
#ifdef DDB
	case CTL_DDB:
		fn = ddb_sysctl;
		break;
#endif
	case CTL_PROC:
		fn = proc_sysctl;
		break;
	default:
		return (EOPNOTSUPP);
	}

	/*
	 * XXX Hey, we wire `old', but what about `new'?
	 */

	if (SCARG(uap, oldlenp) &&
	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen,
	     sizeof(savelen))))
		return (error);
	if (SCARG(uap, old) != NULL) {
		error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
		if (error)
			return (error);
		error = uvm_vslock(p, (void *)(u_long)SCARG(uap, old), savelen,
		    VM_PROT_READ|VM_PROT_WRITE);
		if (error) {
			(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
			return error;
		}
		oldlen = savelen;
	}
	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, 
		      (void *)(u_long)SCARG(uap, old), &oldlen, 
		      (void *)(u_long)SCARG(uap, new), SCARG(uap, newlen), p);
	if (SCARG(uap, old) != NULL) {
		uvm_vsunlock(p, (void *)(u_long)SCARG(uap, old), savelen);
		(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
	}
	savelen = oldlen;
	if (error)
		return (error);
	if (SCARG(uap, oldlenp))
		error = copyout(&savelen,
		    (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
	return (error);
}