sys_machdep.c revision 280258
1/*-
2 * Copyright (c) 2001 Jake Burkholder.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/sys/sparc64/sparc64/sys_machdep.c 280258 2015-03-19 13:37:36Z rwatson $
27 */
28
29#include "opt_capsicum.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/capsicum.h>
34#include <sys/lock.h>
35#include <sys/malloc.h>
36#include <sys/mutex.h>
37#include <sys/proc.h>
38#include <sys/sysproto.h>
39
40#include <machine/md_var.h>
41#include <machine/utrap.h>
42#include <machine/sysarch.h>
43
44static int sparc_sigtramp_install(struct thread *td, char *args);
45static int sparc_utrap_install(struct thread *td, char *args);
46
47#ifndef	_SYS_SYSPROTO_H_
48struct sysarch_args {
49	int	op;
50	char	*parms;
51};
52#endif
53
54int
55sysarch(struct thread *td, struct sysarch_args *uap)
56{
57	int error;
58
59#ifdef CAPABILITY_MODE
60	/*
61	 * When adding new operations, add a new case statement here to
62	 * explicitly indicate whether or not the operation is safe to
63	 * perform in capability mode.
64	 */
65	if (IN_CAPABILITY_MODE(td)) {
66		switch (uap->op) {
67		case SPARC_SIGTRAMP_INSTALL:
68		case SPARC_UTRAP_INSTALL:
69			break;
70
71		default:
72#ifdef KTRACE
73			if (KTRPOINT(td, KTR_CAPFAIL))
74				ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL);
75#endif
76			return (ECAPMODE);
77		}
78	}
79#endif
80
81	mtx_lock(&Giant);
82	switch (uap->op) {
83	case SPARC_SIGTRAMP_INSTALL:
84		error = sparc_sigtramp_install(td, uap->parms);
85		break;
86	case SPARC_UTRAP_INSTALL:
87		error = sparc_utrap_install(td, uap->parms);
88		break;
89	default:
90		error = EINVAL;
91		break;
92	}
93	mtx_unlock(&Giant);
94	return (error);
95}
96
97static int
98sparc_sigtramp_install(struct thread *td, char *args)
99{
100	struct sparc_sigtramp_install_args sia;
101	struct proc *p;
102	int error;
103
104	p = td->td_proc;
105	if ((error = copyin(args, &sia, sizeof(sia))) != 0)
106		return (error);
107	if (sia.sia_old != NULL) {
108		if (suword(sia.sia_old, (long)p->p_md.md_sigtramp) != 0)
109			return (EFAULT);
110	}
111	p->p_md.md_sigtramp = sia.sia_new;
112	return (0);
113}
114
115static int
116sparc_utrap_install(struct thread *td, char *args)
117{
118	struct sparc_utrap_install_args uia;
119	struct sparc_utrap_args ua;
120	struct md_utrap *ut;
121	int error;
122	int i;
123
124	ut = td->td_proc->p_md.md_utrap;
125	if ((error = copyin(args, &uia, sizeof(uia))) != 0)
126		return (error);
127	if (uia.num < 0 || uia.num > UT_MAX ||
128	    (uia.handlers == NULL && uia.num > 0))
129		return (EINVAL);
130	for (i = 0; i < uia.num; i++) {
131		if ((error = copyin(&uia.handlers[i], &ua, sizeof(ua))) != 0)
132			return (error);
133		if (ua.type != UTH_NOCHANGE &&
134		    (ua.type < 0 || ua.type >= UT_MAX))
135			return (EINVAL);
136		if (ua.old_deferred != NULL) {
137			if ((error = suword(ua.old_deferred, 0)) != 0)
138				return (error);
139		}
140		if (ua.old_precise != NULL) {
141			error = suword(ua.old_precise,
142			    ut != NULL ? (long)ut->ut_precise[ua.type] : 0);
143			if (error != 0)
144				return (error);
145		}
146		if (ua.type != UTH_NOCHANGE) {
147			if (ut == NULL) {
148				ut = utrap_alloc();
149				td->td_proc->p_md.md_utrap = ut;
150			}
151			ut->ut_precise[ua.type] = ua.new_precise;
152		}
153	}
154	return (0);
155}
156