1 /*-
2 * Copyright (c) 2013 Dmitry Chagin
3 * Copyright (c) 2004 Tim J. Robbins
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 2000 Marcel Moolenaar
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/clock.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/imgact.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/mutex.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
57 #include <sys/systm.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/wait.h>
61
62 #include <security/mac/mac_framework.h>
63
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/ufsmount.h>
67
68 #include <machine/frame.h>
69 #include <machine/md_var.h>
70 #include <machine/pcb.h>
71 #include <machine/psl.h>
72 #include <machine/segments.h>
73 #include <machine/specialreg.h>
74
75 #include <vm/pmap.h>
76 #include <vm/vm.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80
81 #include <amd64/linux/linux.h>
82 #include <amd64/linux/linux_proto.h>
83 #include <compat/linux/linux_emul.h>
84 #include <compat/linux/linux_file.h>
85 #include <compat/linux/linux_ipc.h>
86 #include <compat/linux/linux_misc.h>
87 #include <compat/linux/linux_mmap.h>
88 #include <compat/linux/linux_signal.h>
89 #include <compat/linux/linux_util.h>
90
91 #include <x86/include/sysarch.h>
92
93 int
linux_execve(struct thread * td,struct linux_execve_args * args)94 linux_execve(struct thread *td, struct linux_execve_args *args)
95 {
96 struct image_args eargs;
97 char *path;
98 int error;
99
100 LCONVPATHEXIST(td, args->path, &path);
101
102 LINUX_CTR(execve);
103
104 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
105 args->envp);
106 free(path, M_TEMP);
107 if (error == 0)
108 error = linux_common_execve(td, &eargs);
109 return (error);
110 }
111
112 int
linux_set_upcall_kse(struct thread * td,register_t stack)113 linux_set_upcall_kse(struct thread *td, register_t stack)
114 {
115
116 if (stack)
117 td->td_frame->tf_rsp = stack;
118
119 /*
120 * The newly created Linux thread returns
121 * to the user space by the same path that a parent do.
122 */
123 td->td_frame->tf_rax = 0;
124 return (0);
125 }
126
127 int
linux_mmap2(struct thread * td,struct linux_mmap2_args * args)128 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
129 {
130
131 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
132 args->flags, args->fd, args->pgoff));
133 }
134
135 int
linux_mprotect(struct thread * td,struct linux_mprotect_args * uap)136 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
137 {
138
139 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
140 }
141
142 int
linux_iopl(struct thread * td,struct linux_iopl_args * args)143 linux_iopl(struct thread *td, struct linux_iopl_args *args)
144 {
145 int error;
146
147 LINUX_CTR(iopl);
148
149 if (args->level > 3)
150 return (EINVAL);
151 if ((error = priv_check(td, PRIV_IO)) != 0)
152 return (error);
153 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
154 return (error);
155 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
156 (args->level * (PSL_IOPL / 3));
157
158 return (0);
159 }
160
161 int
linux_rt_sigsuspend(struct thread * td,struct linux_rt_sigsuspend_args * uap)162 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
163 {
164 l_sigset_t lmask;
165 sigset_t sigmask;
166 int error;
167
168 LINUX_CTR2(rt_sigsuspend, "%p, %ld",
169 uap->newset, uap->sigsetsize);
170
171 if (uap->sigsetsize != sizeof(l_sigset_t))
172 return (EINVAL);
173
174 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
175 if (error)
176 return (error);
177
178 linux_to_bsd_sigset(&lmask, &sigmask);
179 return (kern_sigsuspend(td, sigmask));
180 }
181
182 int
linux_pause(struct thread * td,struct linux_pause_args * args)183 linux_pause(struct thread *td, struct linux_pause_args *args)
184 {
185 struct proc *p = td->td_proc;
186 sigset_t sigmask;
187
188 LINUX_CTR(pause);
189
190 PROC_LOCK(p);
191 sigmask = td->td_sigmask;
192 PROC_UNLOCK(p);
193 return (kern_sigsuspend(td, sigmask));
194 }
195
196 int
linux_sigaltstack(struct thread * td,struct linux_sigaltstack_args * uap)197 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
198 {
199 stack_t ss, oss;
200 l_stack_t lss;
201 int error;
202
203 memset(&lss, 0, sizeof(lss));
204 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
205
206 if (uap->uss != NULL) {
207 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
208 if (error)
209 return (error);
210
211 ss.ss_sp = PTRIN(lss.ss_sp);
212 ss.ss_size = lss.ss_size;
213 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
214 }
215 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
216 (uap->uoss != NULL) ? &oss : NULL);
217 if (!error && uap->uoss != NULL) {
218 lss.ss_sp = PTROUT(oss.ss_sp);
219 lss.ss_size = oss.ss_size;
220 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
221 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
222 }
223
224 return (error);
225 }
226
227 int
linux_arch_prctl(struct thread * td,struct linux_arch_prctl_args * args)228 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args)
229 {
230 struct pcb *pcb;
231 int error;
232
233 pcb = td->td_pcb;
234 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr);
235
236 switch (args->code) {
237 case LINUX_ARCH_SET_GS:
238 if (args->addr < VM_MAXUSER_ADDRESS) {
239 set_pcb_flags(pcb, PCB_FULL_IRET);
240 pcb->pcb_gsbase = args->addr;
241 td->td_frame->tf_gs = _ugssel;
242 error = 0;
243 } else
244 error = EPERM;
245 break;
246 case LINUX_ARCH_SET_FS:
247 if (args->addr < VM_MAXUSER_ADDRESS) {
248 set_pcb_flags(pcb, PCB_FULL_IRET);
249 pcb->pcb_fsbase = args->addr;
250 td->td_frame->tf_fs = _ufssel;
251 error = 0;
252 } else
253 error = EPERM;
254 break;
255 case LINUX_ARCH_GET_FS:
256 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr),
257 sizeof(args->addr));
258 break;
259 case LINUX_ARCH_GET_GS:
260 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr),
261 sizeof(args->addr));
262 break;
263 default:
264 error = EINVAL;
265 }
266 return (error);
267 }
268
269 int
linux_set_cloned_tls(struct thread * td,void * desc)270 linux_set_cloned_tls(struct thread *td, void *desc)
271 {
272 struct pcb *pcb;
273
274 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
275 return (EPERM);
276
277 pcb = td->td_pcb;
278 pcb->pcb_fsbase = (register_t)desc;
279 td->td_frame->tf_fs = _ufssel;
280
281 return (0);
282 }
283