2 * FreeBSD setup_initial_stack() implementation.
4 * Copyright (c) 2013-14 Stacey D. Son
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef TARGET_OS_STACK_H
21 #define TARGET_OS_STACK_H
23 #include <sys/param.h>
24 #include "target_arch_sigtramp.h"
25 #include "qemu/guest-random.h"
26 #include "user/tswap-target.h"
29 * The initial FreeBSD stack is as follows:
30 * (see kern/kern_exec.c exec_copyout_strings() )
32 * Hi Address -> char **ps_argvstr (struct ps_strings for ps, w, etc.)
33 * unsigned ps_nargvstr
35 * PS_STRINGS -> unsigned ps_nenvstr
37 * machine dependent sigcode (sv_sigcode of size
40 * execpath (absolute image path for rtld)
42 * SSP Canary (sizeof(long) * 8)
44 * page sizes array (usually sizeof(u_long) )
46 * "destp" -> argv, env strings (up to 262144 bytes)
48 static inline int setup_initial_stack(struct bsd_binprm
*bprm
,
49 abi_ulong
*ret_addr
, abi_ulong
*stringp
)
52 abi_ulong stack_hi_addr
;
53 size_t execpath_len
, stringspace
;
54 abi_ulong destp
, argvp
, envp
, p
;
55 struct target_ps_strings ps_strs
;
56 char canary
[sizeof(abi_long
) * 8];
58 stack_hi_addr
= p
= target_stkbas
+ target_stksiz
;
60 /* Save some space for ps_strings. */
61 p
-= sizeof(struct target_ps_strings
);
63 /* Add machine dependent sigcode. */
64 p
-= TARGET_SZSIGCODE
;
65 if (setup_sigtramp(p
, (unsigned)offsetof(struct target_sigframe
, sf_uc
),
66 TARGET_FREEBSD_NR_sigreturn
)) {
71 execpath_len
= strlen(bprm
->fullpath
) + 1;
72 p
-= roundup(execpath_len
, sizeof(abi_ulong
));
73 if (memcpy_to_target(p
, bprm
->fullpath
, execpath_len
)) {
78 /* Add canary for SSP. */
79 qemu_guest_getrandom_nofail(canary
, sizeof(canary
));
80 p
-= roundup(sizeof(canary
), sizeof(abi_ulong
));
81 if (memcpy_to_target(p
, canary
, sizeof(canary
))) {
85 /* Add page sizes array. */
86 p
-= sizeof(abi_ulong
);
87 if (put_user_ual(TARGET_PAGE_SIZE
, p
)) {
92 * Deviate from FreeBSD stack layout: force stack to new page here
93 * so that signal trampoline is not sharing the page with user stack
94 * frames. This is actively harmful in qemu as it marks pages with
95 * code it translated as read-only, which is somewhat problematic
96 * for user trying to use the stack as intended.
98 p
= rounddown(p
, TARGET_PAGE_SIZE
);
100 /* Calculate the string space needed */
102 for (i
= 0; i
< bprm
->argc
; ++i
) {
103 stringspace
+= strlen(bprm
->argv
[i
]) + 1;
105 for (i
= 0; i
< bprm
->envc
; ++i
) {
106 stringspace
+= strlen(bprm
->envp
[i
]) + 1;
108 if (stringspace
> TARGET_ARG_MAX
) {
112 /* Make room for the argv and envp strings */
113 destp
= rounddown(p
- stringspace
, sizeof(abi_ulong
));
114 p
= argvp
= destp
- (bprm
->argc
+ bprm
->envc
+ 2) * sizeof(abi_ulong
);
115 /* Remember the strings pointer */
120 * Add argv strings. Note that the argv[] vectors are added by
121 * loader_build_argptr()
123 /* XXX need to make room for auxargs */
124 ps_strs
.ps_argvstr
= tswapl(argvp
);
125 ps_strs
.ps_nargvstr
= tswap32(bprm
->argc
);
126 for (i
= 0; i
< bprm
->argc
; ++i
) {
127 size_t len
= strlen(bprm
->argv
[i
]) + 1;
129 if (memcpy_to_target(destp
, bprm
->argv
[i
], len
)) {
133 if (put_user_ual(destp
, argvp
)) {
137 argvp
+= sizeof(abi_ulong
);
140 if (put_user_ual(0, argvp
)) {
145 * Add env strings. Note that the envp[] vectors are added by
146 * loader_build_argptr().
148 envp
= argvp
+ sizeof(abi_ulong
);
149 ps_strs
.ps_envstr
= tswapl(envp
);
150 ps_strs
.ps_nenvstr
= tswap32(bprm
->envc
);
151 for (i
= 0; i
< bprm
->envc
; ++i
) {
152 size_t len
= strlen(bprm
->envp
[i
]) + 1;
154 if (memcpy_to_target(destp
, bprm
->envp
[i
], len
)) {
158 if (put_user_ual(destp
, envp
)) {
162 envp
+= sizeof(abi_ulong
);
165 if (put_user_ual(0, envp
)) {
169 if (memcpy_to_target(stack_hi_addr
- sizeof(ps_strs
), &ps_strs
,
182 #endif /* TARGET_OS_STACK_H */