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