xref: /netbsd/sys/compat/netbsd32/netbsd32_vm.c (revision 3af25c06)
1 /*	$NetBSD: netbsd32_vm.c,v 1.3 2021/01/19 01:47:58 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001, 2008, 2018 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * from: NetBSD: netbsd32_netbsd.c,v 1.221 2018/12/24 20:44:39 mrg Exp
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_vm.c,v 1.3 2021/01/19 01:47:58 simonb Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/mman.h>
37 #include <sys/filedesc.h>
38 #include <sys/vfs_syscalls.h>
39 
40 #include <compat/sys/mman.h>
41 #include <compat/netbsd32/netbsd32.h>
42 #include <compat/netbsd32/netbsd32_syscall.h>
43 #include <compat/netbsd32/netbsd32_syscallargs.h>
44 #include <compat/netbsd32/netbsd32_conv.h>
45 
46 
47 int
netbsd32_mmap(struct lwp * l,const struct netbsd32_mmap_args * uap,register_t * retval)48 netbsd32_mmap(struct lwp *l, const struct netbsd32_mmap_args *uap, register_t *retval)
49 {
50 	/* {
51 		syscallarg(netbsd32_voidp) addr;
52 		syscallarg(netbsd32_size_t) len;
53 		syscallarg(int) prot;
54 		syscallarg(int) flags;
55 		syscallarg(int) fd;
56 		syscallarg(netbsd32_long) PAD;
57 		syscallarg(netbsd32_off_t) pos;
58 	} */
59 	struct sys_mmap_args ua;
60 	int error;
61 
62 	NETBSD32TOP_UAP(addr, void);
63 	NETBSD32TOX_UAP(len, size_t);
64 	NETBSD32TO64_UAP(prot);
65 	NETBSD32TO64_UAP(flags);
66 #ifdef __x86_64__
67 	/*
68 	 * Ancient kernel on x86 did not obey PROT_EXEC on i386 at least
69 	 * and ld.so did not turn it on!
70 	 */
71 	if (SCARG(&ua, flags) & COMPAT_MAP_COPY) {
72 		SCARG(&ua, flags) = MAP_PRIVATE
73 		    | (SCARG(&ua, flags) & ~COMPAT_MAP_COPY);
74 		SCARG(&ua, prot) |= PROT_EXEC;
75 	}
76 #endif
77 	NETBSD32TO64_UAP(fd);
78 	NETBSD32TOX_UAP(PAD, long);
79 	NETBSD32TOX_UAP(pos, off_t);
80 #ifdef DEBUG_MMAP
81 	printf("mmap(addr=0x%lx, len=0x%lx, prot=0x%lx, flags=0x%lx, "
82 	    "fd=%ld, pos=0x%lx);\n",
83 	    (long)SCARG(&ua, addr), (long)SCARG(&ua, len),
84 	    (long)SCARG(&ua, prot), (long)SCARG(&ua, flags),
85 	    (long)SCARG(&ua, fd), (long)SCARG(&ua, pos));
86 #endif
87 
88 	error = sys_mmap(l, &ua, retval);
89 #ifdef DEBUG_MMAP
90 	printf("mmap error = %d  *retval = %#"PRIxREGISTER"\n", error, *retval);
91 #endif
92 	if (error == 0 && (u_long)*retval > (u_long)UINT_MAX) {
93 		const char *name = curlwp->l_name;
94 
95 		if (name == NULL)
96 			name = curproc->p_comm;
97 		printf("netbsd32_mmap(%s:%u:%u): retval out of range: 0x%lx\n",
98 		    name, curproc->p_pid, curlwp->l_lid, (u_long)*retval);
99 		/* Should try to recover and return an error here. */
100 	}
101 	return error;
102 }
103