linux_machdep.c revision 71494
1/*-
2 * Copyright (c) 2000 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/i386/linux/linux_machdep.c 71494 2001-01-24 00:23:30Z jhb $
29 */
30
31#include <sys/param.h>
32#include <sys/mman.h>
33#include <sys/proc.h>
34#include <sys/sysproto.h>
35#include <sys/systm.h>
36#include <sys/unistd.h>
37#include <sys/resource.h>
38#include <sys/resourcevar.h>
39
40#include <machine/frame.h>
41#include <machine/psl.h>
42#include <machine/segments.h>
43#include <machine/sysarch.h>
44
45#include <vm/vm.h>
46#include <sys/lock.h>
47#include <vm/pmap.h>
48#include <vm/vm_map.h>
49
50#include <i386/linux/linux.h>
51#include <i386/linux/linux_proto.h>
52#include <compat/linux/linux_ipc.h>
53#include <compat/linux/linux_signal.h>
54#include <compat/linux/linux_util.h>
55
56struct linux_descriptor {
57	unsigned int  entry_number;
58	unsigned long base_addr;
59	unsigned int  limit;
60	unsigned int  seg_32bit:1;
61	unsigned int  contents:2;
62	unsigned int  read_exec_only:1;
63	unsigned int  limit_in_pages:1;
64	unsigned int  seg_not_present:1;
65	unsigned int  useable:1;
66};
67
68struct linux_select_argv {
69	int nfds;
70	fd_set *readfds;
71	fd_set *writefds;
72	fd_set *exceptfds;
73	struct timeval *timeout;
74};
75
76int
77linux_to_bsd_sigaltstack(int lsa)
78{
79	int bsa = 0;
80
81	if (lsa & LINUX_SS_DISABLE)
82		bsa |= SS_DISABLE;
83	if (lsa & LINUX_SS_ONSTACK)
84		bsa |= SS_ONSTACK;
85	return (bsa);
86}
87
88int
89bsd_to_linux_sigaltstack(int bsa)
90{
91	int lsa = 0;
92
93	if (bsa & SS_DISABLE)
94		lsa |= LINUX_SS_DISABLE;
95	if (bsa & SS_ONSTACK)
96		lsa |= LINUX_SS_ONSTACK;
97	return (lsa);
98}
99
100int
101linux_execve(struct proc *p, struct linux_execve_args *args)
102{
103	struct execve_args bsd;
104	caddr_t sg;
105
106	sg = stackgap_init();
107	CHECKALTEXIST(p, &sg, args->path);
108
109#ifdef DEBUG
110        printf("Linux-emul(%d): execve(%s)\n",
111	    p->p_pid, args->path);
112#endif
113
114	bsd.fname = args->path;
115	bsd.argv = args->argp;
116	bsd.envv = args->envp;
117	return (execve(p, &bsd));
118}
119
120int
121linux_ipc(struct proc *p, struct linux_ipc_args *args)
122{
123	switch (args->what) {
124	case LINUX_SEMOP:
125		return (linux_semop(p, args));
126	case LINUX_SEMGET:
127		return (linux_semget(p, args));
128	case LINUX_SEMCTL:
129		return (linux_semctl(p, args));
130	case LINUX_MSGSND:
131		return (linux_msgsnd(p, args));
132	case LINUX_MSGRCV:
133		return (linux_msgrcv(p, args));
134	case LINUX_MSGGET:
135		return (linux_msgget(p, args));
136	case LINUX_MSGCTL:
137		return (linux_msgctl(p, args));
138	case LINUX_SHMAT:
139		return (linux_shmat(p, args));
140	case LINUX_SHMDT:
141		return (linux_shmdt(p, args));
142	case LINUX_SHMGET:
143		return (linux_shmget(p, args));
144	case LINUX_SHMCTL:
145		return (linux_shmctl(p, args));
146	}
147
148	uprintf("LINUX: 'ipc' typ=%d not implemented\n", args->what);
149	return (ENOSYS);
150}
151
152int
153linux_select(struct proc *p, struct linux_select_args *args)
154{
155	struct linux_select_argv linux_args;
156	struct linux_newselect_args newsel;
157	int error;
158
159#ifdef SELECT_DEBUG
160	printf("Linux-emul(%ld): select(%x)\n", (long)p->p_pid, args->ptr);
161#endif
162
163	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
164	if (error)
165		return (error);
166
167	newsel.nfds = linux_args.nfds;
168	newsel.readfds = linux_args.readfds;
169	newsel.writefds = linux_args.writefds;
170	newsel.exceptfds = linux_args.exceptfds;
171	newsel.timeout = linux_args.timeout;
172	return (linux_newselect(p, &newsel));
173}
174
175int
176linux_fork(struct proc *p, struct linux_fork_args *args)
177{
178	int error;
179
180#ifdef DEBUG
181	printf("Linux-emul(%ld): fork()\n", (long)p->p_pid);
182#endif
183
184	if ((error = fork(p, (struct fork_args *)args)) != 0)
185		return (error);
186
187	if (p->p_retval[1] == 1)
188		p->p_retval[0] = 0;
189	return (0);
190}
191
192int
193linux_vfork(struct proc *p, struct linux_vfork_args *args)
194{
195	int error;
196
197#ifdef DEBUG
198	printf("Linux-emul(%ld): vfork()\n", (long)p->p_pid);
199#endif
200
201	if ((error = vfork(p, (struct vfork_args *)args)) != 0)
202		return (error);
203	/* Are we the child? */
204	if (p->p_retval[1] == 1)
205		p->p_retval[0] = 0;
206	return (0);
207}
208
209#define CLONE_VM	0x100
210#define CLONE_FS	0x200
211#define CLONE_FILES	0x400
212#define CLONE_SIGHAND	0x800
213#define CLONE_PID	0x1000
214
215int
216linux_clone(struct proc *p, struct linux_clone_args *args)
217{
218	int error, ff = RFPROC;
219	struct proc *p2;
220	int exit_signal;
221	vm_offset_t start;
222	struct rfork_args rf_args;
223
224#ifdef DEBUG
225	if (args->flags & CLONE_PID)
226		printf("linux_clone(%ld): CLONE_PID not yet supported\n",
227		    (long)p->p_pid);
228	printf("linux_clone(%ld): invoked with flags %x and stack %x\n",
229	    (long)p->p_pid, (unsigned int)args->flags,
230	    (unsigned int)args->stack);
231#endif
232
233	if (!args->stack)
234		return (EINVAL);
235
236	exit_signal = args->flags & 0x000000ff;
237	if (exit_signal >= LINUX_NSIG)
238		return (EINVAL);
239
240	if (exit_signal <= LINUX_SIGTBLSZ)
241		exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
242
243	/* RFTHREAD probably not necessary here, but it shouldn't hurt */
244	ff |= RFTHREAD;
245
246	if (args->flags & CLONE_VM)
247		ff |= RFMEM;
248	if (args->flags & CLONE_SIGHAND)
249		ff |= RFSIGSHARE;
250	if (!(args->flags & CLONE_FILES))
251		ff |= RFFDG;
252
253	error = 0;
254	start = 0;
255
256	rf_args.flags = ff;
257	if ((error = rfork(p, &rf_args)) != 0)
258		return (error);
259
260	p2 = pfind(p->p_retval[0]);
261	if (p2 == NULL)
262		return (ESRCH);
263
264	PROC_LOCK(p2);
265	p2->p_sigparent = exit_signal;
266	PROC_UNLOCK(p2);
267	p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
268
269#ifdef DEBUG
270	printf ("linux_clone(%ld): successful rfork to %ld\n", (long)p->p_pid,
271	    (long)p2->p_pid);
272#endif
273
274	return (0);
275}
276
277/* XXX move */
278struct linux_mmap_argv {
279	linux_caddr_t addr;
280	int len;
281	int prot;
282	int flags;
283	int fd;
284	int pos;
285};
286
287#define STACK_SIZE  (2 * 1024 * 1024)
288#define GUARD_SIZE  (4 * PAGE_SIZE)
289
290int
291linux_mmap(struct proc *p, struct linux_mmap_args *args)
292{
293	struct mmap_args /* {
294		caddr_t addr;
295		size_t len;
296		int prot;
297		int flags;
298		int fd;
299		long pad;
300		off_t pos;
301	} */ bsd_args;
302	int error;
303	struct linux_mmap_argv linux_args;
304
305	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
306	if (error)
307		return (error);
308
309#ifdef DEBUG
310	printf("Linux-emul(%ld): mmap(%p, %d, %d, 0x%08x, %d, %d)",
311	    (long)p->p_pid, (void *)linux_args.addr, linux_args.len,
312	    linux_args.prot, linux_args.flags, linux_args.fd, linux_args.pos);
313#endif
314
315	bsd_args.flags = 0;
316	if (linux_args.flags & LINUX_MAP_SHARED)
317		bsd_args.flags |= MAP_SHARED;
318	if (linux_args.flags & LINUX_MAP_PRIVATE)
319		bsd_args.flags |= MAP_PRIVATE;
320	if (linux_args.flags & LINUX_MAP_FIXED)
321		bsd_args.flags |= MAP_FIXED;
322	if (linux_args.flags & LINUX_MAP_ANON)
323		bsd_args.flags |= MAP_ANON;
324	if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
325		bsd_args.flags |= MAP_STACK;
326
327		/* The linux MAP_GROWSDOWN option does not limit auto
328		 * growth of the region.  Linux mmap with this option
329		 * takes as addr the inital BOS, and as len, the initial
330		 * region size.  It can then grow down from addr without
331		 * limit.  However, linux threads has an implicit internal
332		 * limit to stack size of STACK_SIZE.  Its just not
333		 * enforced explicitly in linux.  But, here we impose
334		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
335		 * region, since we can do this with our mmap.
336		 *
337		 * Our mmap with MAP_STACK takes addr as the maximum
338		 * downsize limit on BOS, and as len the max size of
339		 * the region.  It them maps the top SGROWSIZ bytes,
340		 * and autgrows the region down, up to the limit
341		 * in addr.
342		 *
343		 * If we don't use the MAP_STACK option, the effect
344		 * of this code is to allocate a stack region of a
345		 * fixed size of (STACK_SIZE - GUARD_SIZE).
346		 */
347
348		/* This gives us TOS */
349		bsd_args.addr = linux_args.addr + linux_args.len;
350
351		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
352			/* Some linux apps will attempt to mmap
353			 * thread stacks near the top of their
354			 * address space.  If their TOS is greater
355			 * than vm_maxsaddr, vm_map_growstack()
356			 * will confuse the thread stack with the
357			 * process stack and deliver a SEGV if they
358			 * attempt to grow the thread stack past their
359			 * current stacksize rlimit.  To avoid this,
360			 * adjust vm_maxsaddr upwards to reflect
361			 * the current stacksize rlimit rather
362			 * than the maximum possible stacksize.
363			 * It would be better to adjust the
364			 * mmap'ed region, but some apps do not check
365			 * mmap's return value.
366			 */
367			mtx_assert(&Giant, MA_OWNED);
368			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
369			    p->p_rlimit[RLIMIT_STACK].rlim_cur;
370		}
371
372		/* This gives us our maximum stack size */
373		if (linux_args.len > STACK_SIZE - GUARD_SIZE)
374			bsd_args.len = linux_args.len;
375		else
376			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
377
378		/* This gives us a new BOS.  If we're using VM_STACK, then
379		 * mmap will just map the top SGROWSIZ bytes, and let
380		 * the stack grow down to the limit at BOS.  If we're
381		 * not using VM_STACK we map the full stack, since we
382		 * don't have a way to autogrow it.
383		 */
384		bsd_args.addr -= bsd_args.len;
385	} else {
386		bsd_args.addr = linux_args.addr;
387		bsd_args.len  = linux_args.len;
388	}
389
390	bsd_args.prot = linux_args.prot | PROT_READ;	/* always required */
391	if (linux_args.flags & LINUX_MAP_ANON)
392		bsd_args.fd = -1;
393	else
394		bsd_args.fd = linux_args.fd;
395	bsd_args.pos = linux_args.pos;
396	bsd_args.pad = 0;
397
398#ifdef DEBUG
399	printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n", (void *)bsd_args.addr,
400	    bsd_args.len, bsd_args.prot, bsd_args.flags, bsd_args.fd,
401	    (int)bsd_args.pos);
402#endif
403
404	return (mmap(p, &bsd_args));
405}
406
407int
408linux_pipe(struct proc *p, struct linux_pipe_args *args)
409{
410	int error;
411	int reg_edx;
412
413#ifdef DEBUG
414	printf("Linux-emul(%ld): pipe(*)\n", (long)p->p_pid);
415#endif
416
417	reg_edx = p->p_retval[1];
418	error = pipe(p, 0);
419	if (error) {
420		p->p_retval[1] = reg_edx;
421		return (error);
422	}
423
424	error = copyout(p->p_retval, args->pipefds, 2*sizeof(int));
425	if (error) {
426		p->p_retval[1] = reg_edx;
427		return (error);
428	}
429
430	p->p_retval[1] = reg_edx;
431	p->p_retval[0] = 0;
432	return (0);
433}
434
435int
436linux_ioperm(struct proc *p, struct linux_ioperm_args *args)
437{
438	struct sysarch_args sa;
439	struct i386_ioperm_args *iia;
440	caddr_t sg;
441
442	sg = stackgap_init();
443	iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
444	iia->start = args->start;
445	iia->length = args->length;
446	iia->enable = args->enable;
447	sa.op = I386_SET_IOPERM;
448	sa.parms = (char *)iia;
449	return (sysarch(p, &sa));
450}
451
452int
453linux_iopl(struct proc *p, struct linux_iopl_args *args)
454{
455	int error;
456
457	if (args->level < 0 || args->level > 3)
458		return (EINVAL);
459	if ((error = suser(p)) != 0)
460		return (error);
461	if (securelevel > 0)
462		return (EPERM);
463	p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
464	    (args->level * (PSL_IOPL / 3));
465	return (0);
466}
467
468int
469linux_modify_ldt(p, uap)
470	struct proc *p;
471	struct linux_modify_ldt_args *uap;
472{
473	int error;
474	caddr_t sg;
475	struct sysarch_args args;
476	struct i386_ldt_args *ldt;
477	struct linux_descriptor ld;
478	union descriptor *desc;
479
480	sg = stackgap_init();
481
482	if (uap->ptr == NULL)
483		return (EINVAL);
484
485	switch (uap->func) {
486	case 0x00: /* read_ldt */
487		ldt = stackgap_alloc(&sg, sizeof(*ldt));
488		ldt->start = 0;
489		ldt->descs = uap->ptr;
490		ldt->num = uap->bytecount / sizeof(union descriptor);
491		args.op = I386_GET_LDT;
492		args.parms = (char*)ldt;
493		error = sysarch(p, &args);
494		p->p_retval[0] *= sizeof(union descriptor);
495		break;
496	case 0x01: /* write_ldt */
497	case 0x11: /* write_ldt */
498		if (uap->bytecount != sizeof(ld))
499			return (EINVAL);
500
501		error = copyin(uap->ptr, &ld, sizeof(ld));
502		if (error)
503			return (error);
504
505		ldt = stackgap_alloc(&sg, sizeof(*ldt));
506		desc = stackgap_alloc(&sg, sizeof(*desc));
507		ldt->start = ld.entry_number;
508		ldt->descs = desc;
509		ldt->num = 1;
510		desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
511		desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
512		desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
513		desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
514		desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
515			(ld.contents << 2);
516		desc->sd.sd_dpl = 3;
517		desc->sd.sd_p = (ld.seg_not_present ^ 1);
518		desc->sd.sd_xx = 0;
519		desc->sd.sd_def32 = ld.seg_32bit;
520		desc->sd.sd_gran = ld.limit_in_pages;
521		args.op = I386_SET_LDT;
522		args.parms = (char*)ldt;
523		error = sysarch(p, &args);
524		break;
525	default:
526		error = EINVAL;
527		break;
528	}
529
530	if (error == EOPNOTSUPP) {
531		printf("linux: modify_ldt needs kernel option USER_LDT\n");
532		error = ENOSYS;
533	}
534
535	return (error);
536}
537
538int
539linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
540{
541	linux_osigaction_t osa;
542	linux_sigaction_t act, oact;
543	int error;
544
545#ifdef DEBUG
546	printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n", (long)p->p_pid,
547	       args->sig, (void *)args->nsa, (void *)args->osa);
548#endif
549
550	if (args->nsa != NULL) {
551		error = copyin(args->nsa, &osa, sizeof(linux_osigaction_t));
552		if (error)
553			return (error);
554		act.lsa_handler = osa.lsa_handler;
555		act.lsa_flags = osa.lsa_flags;
556		act.lsa_restorer = osa.lsa_restorer;
557		LINUX_SIGEMPTYSET(act.lsa_mask);
558		act.lsa_mask.__bits[0] = osa.lsa_mask;
559	}
560
561	error = linux_do_sigaction(p, args->sig, args->nsa ? &act : NULL,
562	    args->osa ? &oact : NULL);
563
564	if (args->osa != NULL && !error) {
565		osa.lsa_handler = oact.lsa_handler;
566		osa.lsa_flags = oact.lsa_flags;
567		osa.lsa_restorer = oact.lsa_restorer;
568		osa.lsa_mask = oact.lsa_mask.__bits[0];
569		error = copyout(&osa, args->osa, sizeof(linux_osigaction_t));
570	}
571
572	return (error);
573}
574
575/*
576 * Linux has two extra args, restart and oldmask.  We dont use these,
577 * but it seems that "restart" is actually a context pointer that
578 * enables the signal to happen with a different register set.
579 */
580int
581linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
582{
583	struct sigsuspend_args bsd;
584	sigset_t *sigmask;
585	linux_sigset_t mask;
586	caddr_t sg = stackgap_init();
587
588#ifdef DEBUG
589	printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
590	       (long)p->p_pid, (unsigned long)args->mask);
591#endif
592
593	sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
594	LINUX_SIGEMPTYSET(mask);
595	mask.__bits[0] = args->mask;
596	linux_to_bsd_sigset(&mask, sigmask);
597	bsd.sigmask = sigmask;
598	return (sigsuspend(p, &bsd));
599}
600
601int
602linux_rt_sigsuspend(p, uap)
603	struct proc *p;
604	struct linux_rt_sigsuspend_args *uap;
605{
606	linux_sigset_t lmask;
607	sigset_t *bmask;
608	struct sigsuspend_args bsd;
609	caddr_t sg = stackgap_init();
610	int error;
611
612#ifdef DEBUG
613	printf("Linux-emul(%ld): rt_sigsuspend(%p, %d)\n", (long)p->p_pid,
614	       (void *)uap->newset, uap->sigsetsize);
615#endif
616
617	if (uap->sigsetsize != sizeof(linux_sigset_t))
618		return (EINVAL);
619
620	error = copyin(uap->newset, &lmask, sizeof(linux_sigset_t));
621	if (error)
622		return (error);
623
624	bmask = stackgap_alloc(&sg, sizeof(sigset_t));
625	linux_to_bsd_sigset(&lmask, bmask);
626	bsd.sigmask = bmask;
627	return (sigsuspend(p, &bsd));
628}
629
630int
631linux_pause(struct proc *p, struct linux_pause_args *args)
632{
633	struct sigsuspend_args bsd;
634	sigset_t *sigmask;
635	caddr_t sg = stackgap_init();
636
637#ifdef DEBUG
638	printf("Linux-emul(%d): pause()\n", p->p_pid);
639#endif
640
641	sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
642	PROC_LOCK(p);
643	*sigmask = p->p_sigmask;
644	PROC_UNLOCK(p);
645	bsd.sigmask = sigmask;
646	return (sigsuspend(p, &bsd));
647}
648
649int
650linux_sigaltstack(p, uap)
651	struct proc *p;
652	struct linux_sigaltstack_args *uap;
653{
654	struct sigaltstack_args bsd;
655	stack_t *ss, *oss;
656	linux_stack_t lss;
657	int error;
658	caddr_t sg = stackgap_init();
659
660#ifdef DEBUG
661	printf("Linux-emul(%ld): sigaltstack(%p, %p)\n",
662	    (long)p->p_pid, uap->uss, uap->uoss);
663#endif
664
665	if (uap->uss == NULL) {
666		ss = NULL;
667	} else {
668		error = copyin(uap->uss, &lss, sizeof(linux_stack_t));
669		if (error)
670			return (error);
671
672		ss = stackgap_alloc(&sg, sizeof(stack_t));
673		ss->ss_sp = lss.ss_sp;
674		ss->ss_size = lss.ss_size;
675		ss->ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
676	}
677	oss = (uap->uoss != NULL)
678	    ? stackgap_alloc(&sg, sizeof(stack_t))
679	    : NULL;
680
681	bsd.ss = ss;
682	bsd.oss = oss;
683	error = sigaltstack(p, &bsd);
684
685	if (!error && oss != NULL) {
686		lss.ss_sp = oss->ss_sp;
687		lss.ss_size = oss->ss_size;
688		lss.ss_flags = bsd_to_linux_sigaltstack(oss->ss_flags);
689		error = copyout(&lss, uap->uoss, sizeof(linux_stack_t));
690	}
691
692	return (error);
693}
694