1 /*        $NetBSD: linux_exec_machdep.c,v 1.20 2020/09/05 16:30:10 riastradh Exp $        */
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_exec_machdep.c,v 1.20 2020/09/05 16:30:10 riastradh Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/resource.h>
38 #include <sys/proc.h>
39 #include <sys/conf.h>
40 #include <sys/exec.h>
41 #include <sys/exec_elf.h>
42 #include <sys/vnode.h>
43 #include <sys/lwp.h>
44 
45 #include <sys/cpu.h>
46 #include <machine/vmparam.h>
47 
48 #include <sys/syscallargs.h>
49 
50 #ifndef DEBUG_LINUX
51 #define DPRINTF(a)
52 #else
53 #define DPRINTF(a)  uprintf a
54 #endif
55 
56 #include <compat/linux/common/linux_types.h>
57 #include <compat/linux/common/linux_signal.h>
58 #include <compat/linux/common/linux_machdep.h>
59 #include <compat/linux/common/linux_util.h>
60 #include <compat/linux/common/linux_ioctl.h>
61 #include <compat/linux/common/linux_hdio.h>
62 #include <compat/linux/common/linux_exec.h>
63 #include <compat/linux/common/linux_errno.h>
64 #include <compat/linux/linux_syscallargs.h>
65 
66 int
linux_exec_setup_stack(struct lwp * l,struct exec_package * epp)67 linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
68 {
69           u_long max_stack_size;
70           u_long access_linear_min, access_size;
71           u_long noaccess_linear_min, noaccess_size;
72 
73 #ifndef   USRSTACK32
74 #define USRSTACK32  (0x00000000ffffffffL&~PGOFSET)
75 #endif
76 
77           if (epp->ep_flags & EXEC_32) {
78                     epp->ep_minsaddr = USRSTACK32;
79                     max_stack_size = MAXSSIZ;
80           } else {
81                     epp->ep_minsaddr = USRSTACK;
82                     max_stack_size = MAXSSIZ;
83           }
84 
85           if (epp->ep_minsaddr > LINUX_USRSTACK)
86                     epp->ep_minsaddr = LINUX_USRSTACK;
87 #ifdef DEBUG_LINUX
88           else {
89                     /*
90                      * Someone needs to make KERNBASE and TEXTADDR
91                      * java versions < 1.4.2 need the stack to be
92                      * at 0xC0000000
93                      */
94                     uprintf("Cannot setup stack to 0xC0000000, "
95                         "java will not work properly\n");
96           }
97 #endif
98           epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
99                     max_stack_size);
100           epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
101 
102           /*
103            * set up commands for stack.  note that this takes *two*, one to
104            * map the part of the stack which we can access, and one to map
105            * the part which we can't.
106            *
107            * arguably, it could be made into one, but that would require the
108            * addition of another mapping proc, which is unnecessary
109            */
110           access_size = epp->ep_ssize;
111           access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
112           noaccess_size = max_stack_size - access_size;
113           noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
114               access_size), noaccess_size);
115           if (noaccess_size > 0) {
116                     NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
117                         noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
118           }
119           KASSERT(access_size > 0);
120           NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
121               access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
122               VMCMD_STACK);
123 
124           return 0;
125 }
126 
127 int
linux_sys_set_thread_area(struct lwp * l,const struct linux_sys_set_thread_area_args * uap,register_t * retval)128 linux_sys_set_thread_area(struct lwp *l,
129     const struct linux_sys_set_thread_area_args *uap, register_t *retval)
130 {
131           /* {
132                     syscallarg(struct linux_user_desc *) desc;
133           } */
134 
135           return linux_lwp_setprivate(l, SCARG(uap, desc));
136 }
137 
138 int
linux_sys_get_thread_area(struct lwp * l,const struct linux_sys_get_thread_area_args * uap,register_t * retval)139 linux_sys_get_thread_area(struct lwp *l,
140     const struct linux_sys_get_thread_area_args *uap, register_t *retval)
141 {
142           /* {
143                     syscallarg(struct linux_user_desc *) desc;
144           } */
145 
146           /* glibc doesn't actually call this. */
147           return ENOSYS;
148 }
149