xref: /netbsd/sys/arch/arm/arm/linux_syscall.c (revision 6550d01e)
1 /*	$NetBSD: linux_syscall.c,v 1.23 2009/11/21 20:32:17 rmind 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.23 2009/11/21 20:32:17 rmind 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/syscallvar.h>
81 
82 #include <uvm/uvm_extern.h>
83 
84 #include <machine/cpu.h>
85 #include <machine/frame.h>
86 #include <machine/pcb.h>
87 #include <arm/swi.h>
88 
89 #include <compat/linux/common/linux_errno.h>
90 #include <compat/linux/linux_syscall.h>
91 
92 /* ARMLinux has some system calls of its very own. */
93 #define LINUX_ARM_NR_BASE	0x9f0000
94 #define LINUX_SYS_ARMBASE	0x000180 /* Must agree with syscalls.master */
95 
96 void linux_syscall_intern(struct proc *);
97 void linux_syscall_plain(struct trapframe *, struct lwp *, u_int32_t);
98 void linux_syscall_fancy(struct trapframe *, struct lwp *, u_int32_t);
99 
100 void
101 linux_syscall_intern(struct proc *p)
102 {
103 
104 	if (trace_is_enabled(p))
105 		p->p_md.md_syscall = linux_syscall_fancy;
106 	else
107 		p->p_md.md_syscall = linux_syscall_plain;
108 }
109 
110 void
111 linux_syscall_plain(trapframe_t *frame, struct lwp *l, u_int32_t insn)
112 {
113 	const struct sysent *callp;
114 	struct proc *p = l->l_proc;
115 	int code, error;
116 	register_t *args, rval[2];
117 
118 	code = insn & 0x00ffffff;
119 	/* Remap ARM-specific syscalls onto the end of the standard range. */
120 	if (code > LINUX_ARM_NR_BASE)
121 		code = code - LINUX_ARM_NR_BASE + LINUX_SYS_ARMBASE;
122 	code &= LINUX_SYS_NSYSENT - 1;
123 
124 	/* Linux passes all arguments in order in registers, which is nice. */
125 	args = &frame->tf_r0;
126 	callp = p->p_emul->e_sysent + code;
127 
128 	rval[0] = 0;
129 	rval[1] = 0;
130 	error = sy_call(callp, l, args, rval);
131 
132 	switch (error) {
133 	case 0:
134 		frame->tf_r0 = rval[0];
135 		break;
136 
137 	case ERESTART:
138 		/* Reconstruct the pc to point at the swi.  */
139  		frame->tf_pc -= INSN_SIZE;
140 		break;
141 
142 	case EJUSTRETURN:
143 		/* nothing to do */
144 		break;
145 
146 	default:
147 		error = native_to_linux_errno[error];
148 		frame->tf_r0 = error;
149 		break;
150 	}
151 
152 	userret(l);
153 }
154 
155 void
156 linux_syscall_fancy(trapframe_t *frame, struct lwp *l, u_int32_t insn)
157 {
158 	const struct sysent *callp;
159 	struct proc *p = l->l_proc;
160 	int code, error;
161 	register_t *args, rval[2];
162 
163 	code = insn & 0x00ffffff;
164 	/* Remap ARM-specific syscalls onto the end of the standard range. */
165 	if (code > LINUX_ARM_NR_BASE)
166 		code = code - LINUX_ARM_NR_BASE + LINUX_SYS_ARMBASE;
167 	code &= LINUX_SYS_NSYSENT - 1;
168 
169 	/* Linux passes all arguments in order in registers, which is nice. */
170 	args = &frame->tf_r0;
171 	callp = p->p_emul->e_sysent + code;
172 
173 	if ((error = trace_enter(code, args, callp->sy_narg)) != 0)
174 		goto out;
175 
176 	rval[0] = 0;
177 	rval[1] = 0;
178 	error = sy_call(callp, l, args, rval);
179 out:
180 	switch (error) {
181 	case 0:
182 		frame->tf_r0 = rval[0];
183 		break;
184 
185 	case ERESTART:
186 		/* Reconstruct the pc to point at the swi.  */
187  		frame->tf_pc -= INSN_SIZE;
188 		break;
189 
190 	case EJUSTRETURN:
191 		/* nothing to do */
192 		break;
193 
194 	default:
195 		error = native_to_linux_errno[error];
196 		frame->tf_r0 = error;
197 		break;
198 	}
199 
200 	trace_exit(code, rval, error);
201 
202 	userret(l);
203 }
204