xref: /netbsd-src/sys/arch/arm/arm/linux_syscall.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: linux_syscall.c,v 1.11 2004/08/01 14:19:00 jdolecek Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
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 by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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 /*
40  * Copyright (c) 1994-1998 Mark Brinicombe.
41  * Copyright (c) 1994 Brini.
42  * All rights reserved.
43  *
44  * This code is derived from software written for Brini by Mark Brinicombe
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by Mark Brinicombe
57  *	for the NetBSD Project.
58  * 4. The name of the company nor the name of the author may be used to
59  *    endorse or promote products derived from this software without specific
60  *    prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
63  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
64  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
65  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
66  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
67  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
68  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  * ARMLinux emulation: syscall entry handling
75  */
76 
77 #include "opt_ktrace.h"
78 #include "opt_systrace.h"
79 #include "opt_syscall_debug.h"
80 
81 #include <sys/param.h>
82 
83 __KERNEL_RCSID(0, "$NetBSD: linux_syscall.c,v 1.11 2004/08/01 14:19:00 jdolecek Exp $");
84 
85 #include <sys/device.h>
86 #include <sys/errno.h>
87 #include <sys/kernel.h>
88 #include <sys/reboot.h>
89 #include <sys/signalvar.h>
90 #include <sys/systm.h>
91 #include <sys/user.h>
92 #ifdef KTRACE
93 #include <sys/ktrace.h>
94 #endif
95 #ifdef SYSTRACE
96 #include <sys/systrace.h>
97 #endif
98 
99 #include <uvm/uvm_extern.h>
100 
101 #include <machine/cpu.h>
102 #include <machine/frame.h>
103 #include <machine/pcb.h>
104 #include <arm/swi.h>
105 
106 #include <compat/linux/common/linux_errno.h>
107 #include <compat/linux/linux_syscall.h>
108 
109 /* ARMLinux has some system calls of its very own. */
110 #define LINUX_ARM_NR_BASE	0x9f0000
111 #define LINUX_SYS_ARMBASE	0x000180 /* Must agree with syscalls.master */
112 
113 void linux_syscall_intern(struct proc *);
114 void linux_syscall_plain(struct trapframe *, struct lwp *, u_int32_t);
115 void linux_syscall_fancy(struct trapframe *, struct lwp *, u_int32_t);
116 
117 void
118 linux_syscall_intern(struct proc *p)
119 {
120 #ifdef KTRACE
121 	if (p->p_traceflag & (KTRFAC_SYSCALL | KTRFAC_SYSRET)) {
122 		p->p_md.md_syscall = linux_syscall_fancy;
123 		return;
124 	}
125 #endif
126 #ifdef SYSTRACE
127 	if (p->p_flag & P_SYSTRACE) {
128 		p->p_md.md_syscall = linux_syscall_fancy;
129 		return;
130 	}
131 #endif
132 	p->p_md.md_syscall = linux_syscall_plain;
133 }
134 
135 void
136 linux_syscall_plain(trapframe_t *frame, struct lwp *l, u_int32_t insn)
137 {
138 	const struct sysent *callp;
139 	struct proc *p = l->l_proc;
140 	int code, error;
141 	u_int nargs;
142 	register_t *args, rval[2];
143 
144 	code = insn & 0x00ffffff;
145 	/* Remap ARM-specific syscalls onto the end of the standard range. */
146 	if (code > LINUX_ARM_NR_BASE)
147 		code = code - LINUX_ARM_NR_BASE + LINUX_SYS_ARMBASE;
148 	code &= LINUX_SYS_NSYSENT - 1;
149 
150 	/* Linux passes all arguments in order in registers, which is nice. */
151 	args = &frame->tf_r0;
152 	callp = p->p_emul->e_sysent + code;
153 	nargs = callp->sy_argsize / sizeof(register_t);
154 
155 	rval[0] = 0;
156 	rval[1] = 0;
157 	error = (*callp->sy_call)(l, args, rval);
158 
159 	switch (error) {
160 	case 0:
161 		frame->tf_r0 = rval[0];
162 		break;
163 
164 	case ERESTART:
165 		/* Reconstruct the pc to point at the swi.  */
166  		frame->tf_pc -= INSN_SIZE;
167 		break;
168 
169 	case EJUSTRETURN:
170 		/* nothing to do */
171 		break;
172 
173 	default:
174 		error = native_to_linux_errno[error];
175 		frame->tf_r0 = error;
176 		break;
177 	}
178 
179 	userret(l);
180 }
181 
182 void
183 linux_syscall_fancy(trapframe_t *frame, struct lwp *l, u_int32_t insn)
184 {
185 	const struct sysent *callp;
186 	struct proc *p = l->l_proc;
187 	int code, error;
188 	u_int nargs;
189 	register_t *args, rval[2];
190 
191 	code = insn & 0x00ffffff;
192 	/* Remap ARM-specific syscalls onto the end of the standard range. */
193 	if (code > LINUX_ARM_NR_BASE)
194 		code = code - LINUX_ARM_NR_BASE + LINUX_SYS_ARMBASE;
195 	code &= LINUX_SYS_NSYSENT - 1;
196 
197 	/* Linux passes all arguments in order in registers, which is nice. */
198 	args = &frame->tf_r0;
199 	callp = p->p_emul->e_sysent + code;
200 	nargs = callp->sy_argsize / sizeof(register_t);
201 
202 	if ((error = trace_enter(l, code, code, NULL, args)) != 0)
203 		goto bad;
204 
205 	rval[0] = 0;
206 	rval[1] = 0;
207 	error = (*callp->sy_call)(l, args, rval);
208 
209 	switch (error) {
210 	case 0:
211 		frame->tf_r0 = rval[0];
212 		break;
213 
214 	case ERESTART:
215 		/* Reconstruct the pc to point at the swi.  */
216  		frame->tf_pc -= INSN_SIZE;
217 		break;
218 
219 	case EJUSTRETURN:
220 		/* nothing to do */
221 		break;
222 
223 	default:
224 	bad:
225 		error = native_to_linux_errno[error];
226 		frame->tf_r0 = error;
227 		break;
228 	}
229 
230 	trace_exit(l, code, args, rval, error);
231 
232 	userret(l);
233 }
234