xref: /freebsd-13-stable/sys/amd64/linux32/linux32_machdep.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2002 Doug Rabson
6  * Copyright (c) 2000 Marcel Moolenaar
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
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. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/reg.h>
44 #include <sys/syscallsubr.h>
45 
46 #include <machine/frame.h>
47 #include <machine/md_var.h>
48 #include <machine/pcb.h>
49 #include <machine/psl.h>
50 #include <machine/segments.h>
51 #include <machine/specialreg.h>
52 #include <x86/ifunc.h>
53 
54 #include <vm/pmap.h>
55 #include <vm/vm.h>
56 #include <vm/vm_map.h>
57 
58 #include <security/audit/audit.h>
59 
60 #include <compat/freebsd32/freebsd32_util.h>
61 #include <amd64/linux32/linux.h>
62 #include <amd64/linux32/linux32_proto.h>
63 #include <compat/linux/linux_emul.h>
64 #include <compat/linux/linux_fork.h>
65 #include <compat/linux/linux_ipc.h>
66 #include <compat/linux/linux_misc.h>
67 #include <compat/linux/linux_mmap.h>
68 #include <compat/linux/linux_signal.h>
69 #include <compat/linux/linux_util.h>
70 
71 static void	bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
72 
73 struct l_old_select_argv {
74 	l_int		nfds;
75 	l_uintptr_t	readfds;
76 	l_uintptr_t	writefds;
77 	l_uintptr_t	exceptfds;
78 	l_uintptr_t	timeout;
79 } __packed;
80 
81 static void
bsd_to_linux_rusage(struct rusage * ru,struct l_rusage * lru)82 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
83 {
84 
85 	lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
86 	lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
87 	lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
88 	lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
89 	lru->ru_maxrss = ru->ru_maxrss;
90 	lru->ru_ixrss = ru->ru_ixrss;
91 	lru->ru_idrss = ru->ru_idrss;
92 	lru->ru_isrss = ru->ru_isrss;
93 	lru->ru_minflt = ru->ru_minflt;
94 	lru->ru_majflt = ru->ru_majflt;
95 	lru->ru_nswap = ru->ru_nswap;
96 	lru->ru_inblock = ru->ru_inblock;
97 	lru->ru_oublock = ru->ru_oublock;
98 	lru->ru_msgsnd = ru->ru_msgsnd;
99 	lru->ru_msgrcv = ru->ru_msgrcv;
100 	lru->ru_nsignals = ru->ru_nsignals;
101 	lru->ru_nvcsw = ru->ru_nvcsw;
102 	lru->ru_nivcsw = ru->ru_nivcsw;
103 }
104 
105 int
linux_copyout_rusage(struct rusage * ru,void * uaddr)106 linux_copyout_rusage(struct rusage *ru, void *uaddr)
107 {
108 	struct l_rusage lru;
109 
110 	bsd_to_linux_rusage(ru, &lru);
111 
112 	return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
113 }
114 
115 int
linux_execve(struct thread * td,struct linux_execve_args * args)116 linux_execve(struct thread *td, struct linux_execve_args *args)
117 {
118 	struct image_args eargs;
119 	char *path;
120 	int error;
121 
122 	if (!LUSECONVPATH(td)) {
123 		error = freebsd32_exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
124 		    args->argp, args->envp);
125 	} else {
126 		LCONVPATHEXIST(args->path, &path);
127 		error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
128 		    args->argp, args->envp);
129 		LFREEPATH(path);
130 	}
131 	if (error == 0)
132 		error = linux_common_execve(td, &eargs);
133 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
134 	return (error);
135 }
136 
137 CTASSERT(sizeof(struct l_iovec32) == 8);
138 
139 int
linux32_copyinuio(struct l_iovec32 * iovp,l_ulong iovcnt,struct uio ** uiop)140 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
141 {
142 	struct l_iovec32 iov32;
143 	struct iovec *iov;
144 	struct uio *uio;
145 	uint32_t iovlen;
146 	int error, i;
147 
148 	*uiop = NULL;
149 	if (iovcnt > UIO_MAXIOV)
150 		return (EINVAL);
151 	iovlen = iovcnt * sizeof(struct iovec);
152 	uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
153 	iov = (struct iovec *)(uio + 1);
154 	for (i = 0; i < iovcnt; i++) {
155 		error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
156 		if (error) {
157 			free(uio, M_IOV);
158 			return (error);
159 		}
160 		iov[i].iov_base = PTRIN(iov32.iov_base);
161 		iov[i].iov_len = iov32.iov_len;
162 	}
163 	uio->uio_iov = iov;
164 	uio->uio_iovcnt = iovcnt;
165 	uio->uio_segflg = UIO_USERSPACE;
166 	uio->uio_offset = -1;
167 	uio->uio_resid = 0;
168 	for (i = 0; i < iovcnt; i++) {
169 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
170 			free(uio, M_IOV);
171 			return (EINVAL);
172 		}
173 		uio->uio_resid += iov->iov_len;
174 		iov++;
175 	}
176 	*uiop = uio;
177 	return (0);
178 }
179 
180 int
linux32_copyiniov(struct l_iovec32 * iovp32,l_ulong iovcnt,struct iovec ** iovp,int error)181 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
182     int error)
183 {
184 	struct l_iovec32 iov32;
185 	struct iovec *iov;
186 	uint32_t iovlen;
187 	int i;
188 
189 	*iovp = NULL;
190 	if (iovcnt > UIO_MAXIOV)
191 		return (error);
192 	iovlen = iovcnt * sizeof(struct iovec);
193 	iov = malloc(iovlen, M_IOV, M_WAITOK);
194 	for (i = 0; i < iovcnt; i++) {
195 		error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
196 		if (error) {
197 			free(iov, M_IOV);
198 			return (error);
199 		}
200 		iov[i].iov_base = PTRIN(iov32.iov_base);
201 		iov[i].iov_len = iov32.iov_len;
202 	}
203 	*iovp = iov;
204 	return(0);
205 
206 }
207 
208 int
linux_readv(struct thread * td,struct linux_readv_args * uap)209 linux_readv(struct thread *td, struct linux_readv_args *uap)
210 {
211 	struct uio *auio;
212 	int error;
213 
214 	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
215 	if (error)
216 		return (error);
217 	error = kern_readv(td, uap->fd, auio);
218 	free(auio, M_IOV);
219 	return (error);
220 }
221 
222 int
linux_writev(struct thread * td,struct linux_writev_args * uap)223 linux_writev(struct thread *td, struct linux_writev_args *uap)
224 {
225 	struct uio *auio;
226 	int error;
227 
228 	error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
229 	if (error)
230 		return (error);
231 	error = kern_writev(td, uap->fd, auio);
232 	free(auio, M_IOV);
233 	return (error);
234 }
235 
236 struct l_ipc_kludge {
237 	l_uintptr_t msgp;
238 	l_long msgtyp;
239 } __packed;
240 
241 int
linux_ipc(struct thread * td,struct linux_ipc_args * args)242 linux_ipc(struct thread *td, struct linux_ipc_args *args)
243 {
244 
245 	switch (args->what & 0xFFFF) {
246 	case LINUX_SEMOP: {
247 
248 		return (kern_semop(td, args->arg1, PTRIN(args->ptr),
249 		    args->arg2, NULL));
250 	}
251 	case LINUX_SEMGET: {
252 		struct linux_semget_args a;
253 
254 		a.key = args->arg1;
255 		a.nsems = args->arg2;
256 		a.semflg = args->arg3;
257 		return (linux_semget(td, &a));
258 	}
259 	case LINUX_SEMCTL: {
260 		struct linux_semctl_args a;
261 		int error;
262 
263 		a.semid = args->arg1;
264 		a.semnum = args->arg2;
265 		a.cmd = args->arg3;
266 		error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
267 		if (error)
268 			return (error);
269 		return (linux_semctl(td, &a));
270 	}
271 	case LINUX_SEMTIMEDOP: {
272 		struct linux_semtimedop_args a;
273 
274 		a.semid = args->arg1;
275 		a.tsops = PTRIN(args->ptr);
276 		a.nsops = args->arg2;
277 		a.timeout = PTRIN(args->arg5);
278 		return (linux_semtimedop(td, &a));
279 	}
280 	case LINUX_MSGSND: {
281 		struct linux_msgsnd_args a;
282 
283 		a.msqid = args->arg1;
284 		a.msgp = PTRIN(args->ptr);
285 		a.msgsz = args->arg2;
286 		a.msgflg = args->arg3;
287 		return (linux_msgsnd(td, &a));
288 	}
289 	case LINUX_MSGRCV: {
290 		struct linux_msgrcv_args a;
291 
292 		a.msqid = args->arg1;
293 		a.msgsz = args->arg2;
294 		a.msgflg = args->arg3;
295 		if ((args->what >> 16) == 0) {
296 			struct l_ipc_kludge tmp;
297 			int error;
298 
299 			if (args->ptr == 0)
300 				return (EINVAL);
301 			error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
302 			if (error)
303 				return (error);
304 			a.msgp = PTRIN(tmp.msgp);
305 			a.msgtyp = tmp.msgtyp;
306 		} else {
307 			a.msgp = PTRIN(args->ptr);
308 			a.msgtyp = args->arg5;
309 		}
310 		return (linux_msgrcv(td, &a));
311 	}
312 	case LINUX_MSGGET: {
313 		struct linux_msgget_args a;
314 
315 		a.key = args->arg1;
316 		a.msgflg = args->arg2;
317 		return (linux_msgget(td, &a));
318 	}
319 	case LINUX_MSGCTL: {
320 		struct linux_msgctl_args a;
321 
322 		a.msqid = args->arg1;
323 		a.cmd = args->arg2;
324 		a.buf = PTRIN(args->ptr);
325 		return (linux_msgctl(td, &a));
326 	}
327 	case LINUX_SHMAT: {
328 		struct linux_shmat_args a;
329 		l_uintptr_t addr;
330 		int error;
331 
332 		a.shmid = args->arg1;
333 		a.shmaddr = PTRIN(args->ptr);
334 		a.shmflg = args->arg2;
335 		error = linux_shmat(td, &a);
336 		if (error != 0)
337 			return (error);
338 		addr = td->td_retval[0];
339 		error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
340 		td->td_retval[0] = 0;
341 		return (error);
342 	}
343 	case LINUX_SHMDT: {
344 		struct linux_shmdt_args a;
345 
346 		a.shmaddr = PTRIN(args->ptr);
347 		return (linux_shmdt(td, &a));
348 	}
349 	case LINUX_SHMGET: {
350 		struct linux_shmget_args a;
351 
352 		a.key = args->arg1;
353 		a.size = args->arg2;
354 		a.shmflg = args->arg3;
355 		return (linux_shmget(td, &a));
356 	}
357 	case LINUX_SHMCTL: {
358 		struct linux_shmctl_args a;
359 
360 		a.shmid = args->arg1;
361 		a.cmd = args->arg2;
362 		a.buf = PTRIN(args->ptr);
363 		return (linux_shmctl(td, &a));
364 	}
365 	default:
366 		break;
367 	}
368 
369 	return (EINVAL);
370 }
371 
372 int
linux_old_select(struct thread * td,struct linux_old_select_args * args)373 linux_old_select(struct thread *td, struct linux_old_select_args *args)
374 {
375 	struct l_old_select_argv linux_args;
376 	struct linux_select_args newsel;
377 	int error;
378 
379 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
380 	if (error)
381 		return (error);
382 
383 	newsel.nfds = linux_args.nfds;
384 	newsel.readfds = PTRIN(linux_args.readfds);
385 	newsel.writefds = PTRIN(linux_args.writefds);
386 	newsel.exceptfds = PTRIN(linux_args.exceptfds);
387 	newsel.timeout = PTRIN(linux_args.timeout);
388 	return (linux_select(td, &newsel));
389 }
390 
391 int
linux_set_cloned_tls(struct thread * td,void * desc)392 linux_set_cloned_tls(struct thread *td, void *desc)
393 {
394 	struct l_user_desc info;
395 	struct pcb *pcb;
396 	int error;
397 
398 	error = copyin(desc, &info, sizeof(struct l_user_desc));
399 	if (error) {
400 		linux_msg(td, "set_cloned_tls copyin info failed!");
401 	} else {
402 		/* We might copy out the entry_number as GUGS32_SEL. */
403 		info.entry_number = GUGS32_SEL;
404 		error = copyout(&info, desc, sizeof(struct l_user_desc));
405 		if (error)
406 			linux_msg(td, "set_cloned_tls copyout info failed!");
407 
408 		pcb = td->td_pcb;
409 		update_pcb_bases(pcb);
410 		pcb->pcb_gsbase = (register_t)info.base_addr;
411 		td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
412 	}
413 
414 	return (error);
415 }
416 
417 int
linux_set_upcall(struct thread * td,register_t stack)418 linux_set_upcall(struct thread *td, register_t stack)
419 {
420 
421 	if (stack)
422 		td->td_frame->tf_rsp = stack;
423 
424 	/*
425 	 * The newly created Linux thread returns
426 	 * to the user space by the same path that a parent do.
427 	 */
428 	td->td_frame->tf_rax = 0;
429 	return (0);
430 }
431 
432 int
linux_mmap2(struct thread * td,struct linux_mmap2_args * args)433 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
434 {
435 
436 	return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
437 		args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
438 		PAGE_SIZE));
439 }
440 
441 int
linux_mmap(struct thread * td,struct linux_mmap_args * args)442 linux_mmap(struct thread *td, struct linux_mmap_args *args)
443 {
444 	int error;
445 	struct l_mmap_argv linux_args;
446 
447 	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
448 	if (error)
449 		return (error);
450 
451 	return (linux_mmap_common(td, linux_args.addr, linux_args.len,
452 	    linux_args.prot, linux_args.flags, linux_args.fd,
453 	    (uint32_t)linux_args.pgoff));
454 }
455 
456 int
linux_mprotect(struct thread * td,struct linux_mprotect_args * uap)457 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
458 {
459 
460 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
461 }
462 
463 int
linux_madvise(struct thread * td,struct linux_madvise_args * uap)464 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
465 {
466 
467 	return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
468 }
469 
470 int
linux_iopl(struct thread * td,struct linux_iopl_args * args)471 linux_iopl(struct thread *td, struct linux_iopl_args *args)
472 {
473 	int error;
474 
475 	if (args->level < 0 || args->level > 3)
476 		return (EINVAL);
477 	if ((error = priv_check(td, PRIV_IO)) != 0)
478 		return (error);
479 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
480 		return (error);
481 	td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
482 	    (args->level * (PSL_IOPL / 3));
483 
484 	return (0);
485 }
486 
487 int
linux_sigaction(struct thread * td,struct linux_sigaction_args * args)488 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
489 {
490 	l_osigaction_t osa;
491 	l_sigaction_t act, oact;
492 	int error;
493 
494 	if (args->nsa != NULL) {
495 		error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
496 		if (error)
497 			return (error);
498 		act.lsa_handler = osa.lsa_handler;
499 		act.lsa_flags = osa.lsa_flags;
500 		act.lsa_restorer = osa.lsa_restorer;
501 		LINUX_SIGEMPTYSET(act.lsa_mask);
502 		act.lsa_mask.__mask = osa.lsa_mask;
503 	}
504 
505 	error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
506 	    args->osa ? &oact : NULL);
507 
508 	if (args->osa != NULL && !error) {
509 		osa.lsa_handler = oact.lsa_handler;
510 		osa.lsa_flags = oact.lsa_flags;
511 		osa.lsa_restorer = oact.lsa_restorer;
512 		osa.lsa_mask = oact.lsa_mask.__mask;
513 		error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
514 	}
515 
516 	return (error);
517 }
518 
519 /*
520  * Linux has two extra args, restart and oldmask.  We don't use these,
521  * but it seems that "restart" is actually a context pointer that
522  * enables the signal to happen with a different register set.
523  */
524 int
linux_sigsuspend(struct thread * td,struct linux_sigsuspend_args * args)525 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
526 {
527 	sigset_t sigmask;
528 	l_sigset_t mask;
529 
530 	LINUX_SIGEMPTYSET(mask);
531 	mask.__mask = args->mask;
532 	linux_to_bsd_sigset(&mask, &sigmask);
533 	return (kern_sigsuspend(td, sigmask));
534 }
535 
536 int
linux_pause(struct thread * td,struct linux_pause_args * args)537 linux_pause(struct thread *td, struct linux_pause_args *args)
538 {
539 	struct proc *p = td->td_proc;
540 	sigset_t sigmask;
541 
542 	PROC_LOCK(p);
543 	sigmask = td->td_sigmask;
544 	PROC_UNLOCK(p);
545 	return (kern_sigsuspend(td, sigmask));
546 }
547 
548 int
linux_gettimeofday(struct thread * td,struct linux_gettimeofday_args * uap)549 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
550 {
551 	struct timeval atv;
552 	l_timeval atv32;
553 	struct timezone rtz;
554 	int error = 0;
555 
556 	if (uap->tp) {
557 		microtime(&atv);
558 		atv32.tv_sec = atv.tv_sec;
559 		atv32.tv_usec = atv.tv_usec;
560 		error = copyout(&atv32, uap->tp, sizeof(atv32));
561 	}
562 	if (error == 0 && uap->tzp != NULL) {
563 		rtz.tz_minuteswest = 0;
564 		rtz.tz_dsttime = 0;
565 		error = copyout(&rtz, uap->tzp, sizeof(rtz));
566 	}
567 	return (error);
568 }
569 
570 int
linux_settimeofday(struct thread * td,struct linux_settimeofday_args * uap)571 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
572 {
573 	l_timeval atv32;
574 	struct timeval atv, *tvp;
575 	struct timezone atz, *tzp;
576 	int error;
577 
578 	if (uap->tp) {
579 		error = copyin(uap->tp, &atv32, sizeof(atv32));
580 		if (error)
581 			return (error);
582 		atv.tv_sec = atv32.tv_sec;
583 		atv.tv_usec = atv32.tv_usec;
584 		tvp = &atv;
585 	} else
586 		tvp = NULL;
587 	if (uap->tzp) {
588 		error = copyin(uap->tzp, &atz, sizeof(atz));
589 		if (error)
590 			return (error);
591 		tzp = &atz;
592 	} else
593 		tzp = NULL;
594 	return (kern_settimeofday(td, tvp, tzp));
595 }
596 
597 int
linux_getrusage(struct thread * td,struct linux_getrusage_args * uap)598 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
599 {
600 	struct rusage s;
601 	int error;
602 
603 	error = kern_getrusage(td, uap->who, &s);
604 	if (error != 0)
605 		return (error);
606 	if (uap->rusage != NULL)
607 		error = linux_copyout_rusage(&s, uap->rusage);
608 	return (error);
609 }
610 
611 int
linux_set_thread_area(struct thread * td,struct linux_set_thread_area_args * args)612 linux_set_thread_area(struct thread *td,
613     struct linux_set_thread_area_args *args)
614 {
615 	struct l_user_desc info;
616 	struct pcb *pcb;
617 	int error;
618 
619 	error = copyin(args->desc, &info, sizeof(struct l_user_desc));
620 	if (error)
621 		return (error);
622 
623 	/*
624 	 * Semantics of Linux version: every thread in the system has array
625 	 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
626 	 * This syscall loads one of the selected TLS descriptors with a value
627 	 * and also loads GDT descriptors 6, 7 and 8 with the content of
628 	 * the per-thread descriptors.
629 	 *
630 	 * Semantics of FreeBSD version: I think we can ignore that Linux has
631 	 * three per-thread descriptors and use just the first one.
632 	 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
633 	 * for loading the GDT descriptors. We use just one GDT descriptor
634 	 * for TLS, so we will load just one.
635 	 *
636 	 * XXX: This doesn't work when a user space process tries to use more
637 	 * than one TLS segment. Comment in the Linux source says wine might
638 	 * do this.
639 	 */
640 
641 	/*
642 	 * GLIBC reads current %gs and call set_thread_area() with it.
643 	 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
644 	 * we use these segments.
645 	 */
646 	switch (info.entry_number) {
647 	case GUGS32_SEL:
648 	case GUDATA_SEL:
649 	case 6:
650 	case -1:
651 		info.entry_number = GUGS32_SEL;
652 		break;
653 	default:
654 		return (EINVAL);
655 	}
656 
657 	/*
658 	 * We have to copy out the GDT entry we use.
659 	 *
660 	 * XXX: What if a user space program does not check the return value
661 	 * and tries to use 6, 7 or 8?
662 	 */
663 	error = copyout(&info, args->desc, sizeof(struct l_user_desc));
664 	if (error)
665 		return (error);
666 
667 	pcb = td->td_pcb;
668 	update_pcb_bases(pcb);
669 	pcb->pcb_gsbase = (register_t)info.base_addr;
670 	update_gdt_gsbase(td, info.base_addr);
671 
672 	return (0);
673 }
674 
675 void
bsd_to_linux_regset32(const struct reg32 * b_reg,struct linux_pt_regset32 * l_regset)676 bsd_to_linux_regset32(const struct reg32 *b_reg,
677     struct linux_pt_regset32 *l_regset)
678 {
679 
680 	l_regset->ebx = b_reg->r_ebx;
681 	l_regset->ecx = b_reg->r_ecx;
682 	l_regset->edx = b_reg->r_edx;
683 	l_regset->esi = b_reg->r_esi;
684 	l_regset->edi = b_reg->r_edi;
685 	l_regset->ebp = b_reg->r_ebp;
686 	l_regset->eax = b_reg->r_eax;
687 	l_regset->ds = b_reg->r_ds;
688 	l_regset->es = b_reg->r_es;
689 	l_regset->fs = b_reg->r_fs;
690 	l_regset->gs = b_reg->r_gs;
691 	l_regset->orig_eax = b_reg->r_eax;
692 	l_regset->eip = b_reg->r_eip;
693 	l_regset->cs = b_reg->r_cs;
694 	l_regset->eflags = b_reg->r_eflags;
695 	l_regset->esp = b_reg->r_esp;
696 	l_regset->ss = b_reg->r_ss;
697 }
698 
699 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
700 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
701 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
702 {
703 
704 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
705 	    futex_xchgl_smap : futex_xchgl_nosmap);
706 }
707 
708 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
709 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
710 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
711 {
712 
713 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
714 	    futex_addl_smap : futex_addl_nosmap);
715 }
716 
717 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
718 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
719 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
720 {
721 
722 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
723 	    futex_orl_smap : futex_orl_nosmap);
724 }
725 
726 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
727 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
728 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
729 {
730 
731 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
732 	    futex_andl_smap : futex_andl_nosmap);
733 }
734 
735 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
736 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
737 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
738 {
739 
740 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
741 	    futex_xorl_smap : futex_xorl_nosmap);
742 }
743 
744 int
linux_ptrace_peekuser(struct thread * td,pid_t pid,void * addr,void * data)745 linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data)
746 {
747 
748 	LINUX_RATELIMIT_MSG_OPT1("PTRACE_PEEKUSER offset %ld not implemented; "
749 	    "returning EINVAL", (uintptr_t)addr);
750 	return (EINVAL);
751 }
752 
753 int
linux_ptrace_pokeuser(struct thread * td,pid_t pid,void * addr,void * data)754 linux_ptrace_pokeuser(struct thread *td, pid_t pid, void *addr, void *data)
755 {
756 
757 	LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld "
758 	    "not implemented; returning EINVAL", (uintptr_t)addr);
759 	return (EINVAL);
760 }
761