xref: /freebsd/sys/arm/arm/syscall.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*	$NetBSD: fault.c,v 1.45 2003/11/20 14:44:36 scw Exp $	*/
2 
3 /*-
4  * Copyright 2004 Olivier Houchard
5  * Copyright 2003 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Steve C. Woodford for Wasabi Systems, Inc.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*-
39  * Copyright (c) 1994-1997 Mark Brinicombe.
40  * Copyright (c) 1994 Brini.
41  * All rights reserved.
42  *
43  * This code is derived from software written for Brini by Mark Brinicombe
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by Brini.
56  * 4. The name of the company nor the name of the author may be used to
57  *    endorse or promote products derived from this software without specific
58  *    prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  * RiscBSD kernel project
73  *
74  * fault.c
75  *
76  * Fault handlers
77  *
78  * Created      : 28/11/94
79  */
80 
81 
82 #include <sys/cdefs.h>
83 __FBSDID("$FreeBSD$");
84 
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/proc.h>
88 #include <sys/lock.h>
89 #include <sys/mutex.h>
90 #include <sys/syscall.h>
91 #include <sys/sysent.h>
92 #include <sys/signalvar.h>
93 #include <sys/ptrace.h>
94 #include <sys/pioctl.h>
95 
96 #include <machine/frame.h>
97 
98 void swi_handler(struct trapframe *);
99 
100 static __inline void
101 call_trapsignal(struct thread *td, int sig, u_long code)
102 {
103 	ksiginfo_t ksi;
104 
105 	ksiginfo_init_trap(&ksi);
106 	ksi.ksi_signo = sig;
107 	ksi.ksi_code = (int)code;
108 	trapsignal(td, &ksi);
109 }
110 
111 int
112 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
113 {
114 	struct proc *p;
115 	register_t *ap;
116 	int error;
117 
118 	sa->code = td->td_frame->tf_r7;
119 	ap = &td->td_frame->tf_r0;
120 	if (sa->code == SYS_syscall) {
121 		sa->code = *ap++;
122 		sa->nap--;
123 	} else if (sa->code == SYS___syscall) {
124 		sa->code = ap[_QUAD_LOWWORD];
125 		sa->nap -= 2;
126 		ap += 2;
127 	}
128 	p = td->td_proc;
129 	if (p->p_sysent->sv_mask)
130 		sa->code &= p->p_sysent->sv_mask;
131 	if (sa->code >= p->p_sysent->sv_size)
132 		sa->callp = &p->p_sysent->sv_table[0];
133 	else
134 		sa->callp = &p->p_sysent->sv_table[sa->code];
135 	sa->narg = sa->callp->sy_narg;
136 	error = 0;
137 	memcpy(sa->args, ap, sa->nap * sizeof(register_t));
138 	if (sa->narg > sa->nap) {
139 		error = copyin((void *)td->td_frame->tf_usr_sp, sa->args +
140 		    sa->nap, (sa->narg - sa->nap) * sizeof(register_t));
141 	}
142 	if (error == 0) {
143 		td->td_retval[0] = 0;
144 		td->td_retval[1] = 0;
145 	}
146 	return (error);
147 }
148 
149 #include "../../kern/subr_syscall.c"
150 
151 static void
152 syscall(struct thread *td, struct trapframe *frame)
153 {
154 	struct syscall_args sa;
155 	int error;
156 
157 	sa.nap = 4;
158 
159 	error = syscallenter(td, &sa);
160 	KASSERT(error != 0 || td->td_ar == NULL,
161 	    ("returning from syscall with td_ar set!"));
162 	syscallret(td, error, &sa);
163 }
164 
165 void
166 swi_handler(struct trapframe *frame)
167 {
168 	struct thread *td = curthread;
169 
170 	td->td_frame = frame;
171 
172 	td->td_pticks = 0;
173 	/*
174 	 * Make sure the program counter is correctly aligned so we
175 	 * don't take an alignment fault trying to read the opcode.
176 	 * XXX: Fix for Thumb mode
177 	 */
178 	if (__predict_false(((frame->tf_pc - INSN_SIZE) & 3) != 0)) {
179 		call_trapsignal(td, SIGILL, 0);
180 		userret(td, frame);
181 		return;
182 	}
183 	/*
184 	 * Enable interrupts if they were enabled before the exception.
185 	 * Since all syscalls *should* come from user mode it will always
186 	 * be safe to enable them, but check anyway.
187 	 */
188 	if (td->td_md.md_spinlock_count == 0) {
189 		if (__predict_true(frame->tf_spsr & PSR_I) == 0)
190 			enable_interrupts(PSR_I);
191 		if (__predict_true(frame->tf_spsr & PSR_F) == 0)
192 			enable_interrupts(PSR_F);
193 	}
194 
195 	syscall(td, frame);
196 }
197