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