linux_machdep.c revision 69379
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 69379 2000-11-30 05:23:49Z marcel $
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 == 0)
262		return (ESRCH);
263
264	p2->p_sigparent = exit_signal;
265	p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
266
267#ifdef DEBUG
268	printf ("linux_clone(%ld): successful rfork to %ld\n", (long)p->p_pid,
269	    (long)p2->p_pid);
270#endif
271
272	return (0);
273}
274
275/* XXX move */
276struct linux_mmap_argv {
277	linux_caddr_t addr;
278	int len;
279	int prot;
280	int flags;
281	int fd;
282	int pos;
283};
284
285#define STACK_SIZE  (2 * 1024 * 1024)
286#define GUARD_SIZE  (4 * PAGE_SIZE)
287
288int
289linux_mmap(struct proc *p, struct linux_mmap_args *args)
290{
291	struct mmap_args /* {
292		caddr_t addr;
293		size_t len;
294		int prot;
295		int flags;
296		int fd;
297		long pad;
298		off_t pos;
299	} */ bsd_args;
300	int error;
301	struct linux_mmap_argv linux_args;
302
303	error = copyin(args->ptr, &linux_args, sizeof(linux_args));
304	if (error)
305		return (error);
306
307#ifdef DEBUG
308	printf("Linux-emul(%ld): mmap(%p, %d, %d, 0x%08x, %d, %d)",
309	    (long)p->p_pid, (void *)linux_args.addr, linux_args.len,
310	    linux_args.prot, linux_args.flags, linux_args.fd, linux_args.pos);
311#endif
312
313	bsd_args.flags = 0;
314	if (linux_args.flags & LINUX_MAP_SHARED)
315		bsd_args.flags |= MAP_SHARED;
316	if (linux_args.flags & LINUX_MAP_PRIVATE)
317		bsd_args.flags |= MAP_PRIVATE;
318	if (linux_args.flags & LINUX_MAP_FIXED)
319		bsd_args.flags |= MAP_FIXED;
320	if (linux_args.flags & LINUX_MAP_ANON)
321		bsd_args.flags |= MAP_ANON;
322	if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
323		bsd_args.flags |= MAP_STACK;
324
325		/* The linux MAP_GROWSDOWN option does not limit auto
326		 * growth of the region.  Linux mmap with this option
327		 * takes as addr the inital BOS, and as len, the initial
328		 * region size.  It can then grow down from addr without
329		 * limit.  However, linux threads has an implicit internal
330		 * limit to stack size of STACK_SIZE.  Its just not
331		 * enforced explicitly in linux.  But, here we impose
332		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
333		 * region, since we can do this with our mmap.
334		 *
335		 * Our mmap with MAP_STACK takes addr as the maximum
336		 * downsize limit on BOS, and as len the max size of
337		 * the region.  It them maps the top SGROWSIZ bytes,
338		 * and autgrows the region down, up to the limit
339		 * in addr.
340		 *
341		 * If we don't use the MAP_STACK option, the effect
342		 * of this code is to allocate a stack region of a
343		 * fixed size of (STACK_SIZE - GUARD_SIZE).
344		 */
345
346		/* This gives us TOS */
347		bsd_args.addr = linux_args.addr + linux_args.len;
348
349		if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
350			/* Some linux apps will attempt to mmap
351			 * thread stacks near the top of their
352			 * address space.  If their TOS is greater
353			 * than vm_maxsaddr, vm_map_growstack()
354			 * will confuse the thread stack with the
355			 * process stack and deliver a SEGV if they
356			 * attempt to grow the thread stack past their
357			 * current stacksize rlimit.  To avoid this,
358			 * adjust vm_maxsaddr upwards to reflect
359			 * the current stacksize rlimit rather
360			 * than the maximum possible stacksize.
361			 * It would be better to adjust the
362			 * mmap'ed region, but some apps do not check
363			 * mmap's return value.
364			 */
365			p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
366			    p->p_rlimit[RLIMIT_STACK].rlim_cur;
367		}
368
369		/* This gives us our maximum stack size */
370		if (linux_args.len > STACK_SIZE - GUARD_SIZE)
371			bsd_args.len = linux_args.len;
372		else
373			bsd_args.len  = STACK_SIZE - GUARD_SIZE;
374
375		/* This gives us a new BOS.  If we're using VM_STACK, then
376		 * mmap will just map the top SGROWSIZ bytes, and let
377		 * the stack grow down to the limit at BOS.  If we're
378		 * not using VM_STACK we map the full stack, since we
379		 * don't have a way to autogrow it.
380		 */
381		bsd_args.addr -= bsd_args.len;
382	} else {
383		bsd_args.addr = linux_args.addr;
384		bsd_args.len  = linux_args.len;
385	}
386
387	bsd_args.prot = linux_args.prot | PROT_READ;	/* always required */
388	if (linux_args.flags & LINUX_MAP_ANON)
389		bsd_args.fd = -1;
390	else
391		bsd_args.fd = linux_args.fd;
392	bsd_args.pos = linux_args.pos;
393	bsd_args.pad = 0;
394
395#ifdef DEBUG
396	printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n", (void *)bsd_args.addr,
397	    bsd_args.len, bsd_args.prot, bsd_args.flags, bsd_args.fd,
398	    (int)bsd_args.pos);
399#endif
400
401	return (mmap(p, &bsd_args));
402}
403
404int
405linux_pipe(struct proc *p, struct linux_pipe_args *args)
406{
407	int error;
408	int reg_edx;
409
410#ifdef DEBUG
411	printf("Linux-emul(%ld): pipe(*)\n", (long)p->p_pid);
412#endif
413
414	reg_edx = p->p_retval[1];
415	error = pipe(p, 0);
416	if (error) {
417		p->p_retval[1] = reg_edx;
418		return (error);
419	}
420
421	error = copyout(p->p_retval, args->pipefds, 2*sizeof(int));
422	if (error) {
423		p->p_retval[1] = reg_edx;
424		return (error);
425	}
426
427	p->p_retval[1] = reg_edx;
428	p->p_retval[0] = 0;
429	return (0);
430}
431
432int
433linux_ioperm(struct proc *p, struct linux_ioperm_args *args)
434{
435	struct sysarch_args sa;
436	struct i386_ioperm_args *iia;
437	caddr_t sg;
438
439	sg = stackgap_init();
440	iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
441	iia->start = args->start;
442	iia->length = args->length;
443	iia->enable = args->enable;
444	sa.op = I386_SET_IOPERM;
445	sa.parms = (char *)iia;
446	return (sysarch(p, &sa));
447}
448
449int
450linux_iopl(struct proc *p, struct linux_iopl_args *args)
451{
452	int error;
453
454	if (args->level < 0 || args->level > 3)
455		return (EINVAL);
456	if ((error = suser(p)) != 0)
457		return (error);
458	if (securelevel > 0)
459		return (EPERM);
460	p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
461	    (args->level * (PSL_IOPL / 3));
462	return (0);
463}
464
465int
466linux_modify_ldt(p, uap)
467	struct proc *p;
468	struct linux_modify_ldt_args *uap;
469{
470	int error;
471	caddr_t sg;
472	struct sysarch_args args;
473	struct i386_ldt_args *ldt;
474	struct linux_descriptor ld;
475	union descriptor *desc;
476
477	sg = stackgap_init();
478
479	if (uap->ptr == NULL)
480		return (EINVAL);
481
482	switch (uap->func) {
483	case 0x00: /* read_ldt */
484		ldt = stackgap_alloc(&sg, sizeof(*ldt));
485		ldt->start = 0;
486		ldt->descs = uap->ptr;
487		ldt->num = uap->bytecount / sizeof(union descriptor);
488		args.op = I386_GET_LDT;
489		args.parms = (char*)ldt;
490		error = sysarch(p, &args);
491		p->p_retval[0] *= sizeof(union descriptor);
492		break;
493	case 0x01: /* write_ldt */
494	case 0x11: /* write_ldt */
495		if (uap->bytecount != sizeof(ld))
496			return (EINVAL);
497
498		error = copyin(uap->ptr, &ld, sizeof(ld));
499		if (error)
500			return (error);
501
502		ldt = stackgap_alloc(&sg, sizeof(*ldt));
503		desc = stackgap_alloc(&sg, sizeof(*desc));
504		ldt->start = ld.entry_number;
505		ldt->descs = desc;
506		ldt->num = 1;
507		desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
508		desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
509		desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
510		desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
511		desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
512			(ld.contents << 2);
513		desc->sd.sd_dpl = 3;
514		desc->sd.sd_p = (ld.seg_not_present ^ 1);
515		desc->sd.sd_xx = 0;
516		desc->sd.sd_def32 = ld.seg_32bit;
517		desc->sd.sd_gran = ld.limit_in_pages;
518		args.op = I386_SET_LDT;
519		args.parms = (char*)ldt;
520		error = sysarch(p, &args);
521		break;
522	default:
523		error = EINVAL;
524		break;
525	}
526
527	if (error == EOPNOTSUPP) {
528		printf("linux: modify_ldt needs kernel option USER_LDT\n");
529		error = ENOSYS;
530	}
531
532	return (error);
533}
534
535int
536linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
537{
538	linux_osigaction_t osa;
539	linux_sigaction_t act, oact;
540	int error;
541
542#ifdef DEBUG
543	printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n", (long)p->p_pid,
544	       args->sig, (void *)args->nsa, (void *)args->osa);
545#endif
546
547	if (args->nsa != NULL) {
548		error = copyin(args->nsa, &osa, sizeof(linux_osigaction_t));
549		if (error)
550			return (error);
551		act.lsa_handler = osa.lsa_handler;
552		act.lsa_flags = osa.lsa_flags;
553		act.lsa_restorer = osa.lsa_restorer;
554		LINUX_SIGEMPTYSET(act.lsa_mask);
555		act.lsa_mask.__bits[0] = osa.lsa_mask;
556	}
557
558	error = linux_do_sigaction(p, args->sig, args->nsa ? &act : NULL,
559	    args->osa ? &oact : NULL);
560
561	if (args->osa != NULL && !error) {
562		osa.lsa_handler = oact.lsa_handler;
563		osa.lsa_flags = oact.lsa_flags;
564		osa.lsa_restorer = oact.lsa_restorer;
565		osa.lsa_mask = oact.lsa_mask.__bits[0];
566		error = copyout(&osa, args->osa, sizeof(linux_osigaction_t));
567	}
568
569	return (error);
570}
571
572/*
573 * Linux has two extra args, restart and oldmask.  We dont use these,
574 * but it seems that "restart" is actually a context pointer that
575 * enables the signal to happen with a different register set.
576 */
577int
578linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
579{
580	struct sigsuspend_args bsd;
581	sigset_t *sigmask;
582	linux_sigset_t mask;
583	caddr_t sg = stackgap_init();
584
585#ifdef DEBUG
586	printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
587	       (long)p->p_pid, (unsigned long)args->mask);
588#endif
589
590	sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
591	LINUX_SIGEMPTYSET(mask);
592	mask.__bits[0] = args->mask;
593	linux_to_bsd_sigset(&mask, sigmask);
594	bsd.sigmask = sigmask;
595	return (sigsuspend(p, &bsd));
596}
597
598int
599linux_rt_sigsuspend(p, uap)
600	struct proc *p;
601	struct linux_rt_sigsuspend_args *uap;
602{
603	linux_sigset_t lmask;
604	sigset_t *bmask;
605	struct sigsuspend_args bsd;
606	caddr_t sg = stackgap_init();
607	int error;
608
609#ifdef DEBUG
610	printf("Linux-emul(%ld): rt_sigsuspend(%p, %d)\n", (long)p->p_pid,
611	       (void *)uap->newset, uap->sigsetsize);
612#endif
613
614	if (uap->sigsetsize != sizeof(linux_sigset_t))
615		return (EINVAL);
616
617	error = copyin(uap->newset, &lmask, sizeof(linux_sigset_t));
618	if (error)
619		return (error);
620
621	bmask = stackgap_alloc(&sg, sizeof(sigset_t));
622	linux_to_bsd_sigset(&lmask, bmask);
623	bsd.sigmask = bmask;
624	return (sigsuspend(p, &bsd));
625}
626
627int
628linux_pause(struct proc *p, struct linux_pause_args *args)
629{
630	struct sigsuspend_args bsd;
631	sigset_t *sigmask;
632	caddr_t sg = stackgap_init();
633
634#ifdef DEBUG
635	printf("Linux-emul(%d): pause()\n", p->p_pid);
636#endif
637
638	sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
639	*sigmask = p->p_sigmask;
640	bsd.sigmask = sigmask;
641	return (sigsuspend(p, &bsd));
642}
643
644int
645linux_sigaltstack(p, uap)
646	struct proc *p;
647	struct linux_sigaltstack_args *uap;
648{
649	struct sigaltstack_args bsd;
650	stack_t *ss, *oss;
651	linux_stack_t lss;
652	int error;
653	caddr_t sg = stackgap_init();
654
655#ifdef DEBUG
656	printf("Linux-emul(%ld): sigaltstack(%p, %p)\n",
657	    (long)p->p_pid, uap->uss, uap->uoss);
658#endif
659
660	if (uap->uss == NULL) {
661		ss = NULL;
662	} else {
663		error = copyin(uap->uss, &lss, sizeof(linux_stack_t));
664		if (error)
665			return (error);
666
667		ss = stackgap_alloc(&sg, sizeof(stack_t));
668		ss->ss_sp = lss.ss_sp;
669		ss->ss_size = lss.ss_size;
670		ss->ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
671	}
672	oss = (uap->uoss != NULL)
673	    ? stackgap_alloc(&sg, sizeof(stack_t))
674	    : NULL;
675
676	bsd.ss = ss;
677	bsd.oss = oss;
678	error = sigaltstack(p, &bsd);
679
680	if (!error && oss != NULL) {
681		lss.ss_sp = oss->ss_sp;
682		lss.ss_size = oss->ss_size;
683		lss.ss_flags = bsd_to_linux_sigaltstack(oss->ss_flags);
684		error = copyout(&lss, uap->uoss, sizeof(linux_stack_t));
685	}
686
687	return (error);
688}
689