2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/imgact.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/sysent.h>
37 #include <sys/systm.h>
39 #include <contrib/cloudabi/cloudabi32_types.h>
41 #include <compat/cloudabi/cloudabi_util.h>
43 #include <compat/cloudabi32/cloudabi32_util.h>
45 extern char _binary_cloudabi32_vdso_o_start[];
46 extern char _binary_cloudabi32_vdso_o_end[];
49 cloudabi32_copyout_strings(struct image_params *imgp)
51 struct image_args *args;
55 /* Copy out program arguments. */
57 len = args->begin_envv - args->begin_argv;
58 begin = rounddown2(imgp->sysent->sv_usrstack - len, sizeof(register_t));
59 copyout(args->begin_argv, (void *)begin, len);
60 return ((register_t *)begin);
64 cloudabi32_fixup(register_t **stack_base, struct image_params *imgp)
69 void *argdata, *canary;
74 * CloudABI executables do not store the FreeBSD OS release
75 * number in their header. Set the OS release number to the
76 * latest version of FreeBSD, so that system calls behave as if
80 td->td_proc->p_osrel = __FreeBSD_version;
82 /* Store canary for stack smashing protection. */
83 argdata = *stack_base;
84 arc4rand(canarybuf, sizeof(canarybuf), 0);
85 *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
87 error = copyout(canarybuf, canary, sizeof(canarybuf));
92 * Compute length of program arguments. As the argument data is
93 * binary safe, we had to add a trailing null byte in
94 * exec_copyin_data_fds(). Undo this by reducing the length.
96 args = (Elf32_Auxargs *)imgp->auxargs;
97 argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
101 /* Write out an auxiliary vector. */
102 cloudabi32_auxv_t auxv[] = {
103 #define VAL(type, val) { .a_type = (type), .a_val = (val) }
104 #define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
105 PTR(CLOUDABI_AT_ARGDATA, argdata),
106 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
107 VAL(CLOUDABI_AT_BASE, args->base),
108 PTR(CLOUDABI_AT_CANARY, canary),
109 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
110 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
111 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
112 PTR(CLOUDABI_AT_PHDR, args->phdr),
113 VAL(CLOUDABI_AT_PHNUM, args->phnum),
114 VAL(CLOUDABI_AT_TID, td->td_tid),
115 PTR(CLOUDABI_AT_SYSINFO_EHDR,
116 imgp->proc->p_sysent->sv_shared_page_base),
119 { .a_type = CLOUDABI_AT_NULL },
121 *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
122 error = copyout(auxv, *stack_base, sizeof(auxv));
126 /* Reserve space for storing the TCB. */
127 *stack_base -= howmany(sizeof(cloudabi32_tcb_t), sizeof(register_t));
132 cloudabi32_modevent(module_t mod, int type, void *data)
137 cloudabi_vdso_init(cloudabi32_brand.sysvec,
138 _binary_cloudabi32_vdso_o_start,
139 _binary_cloudabi32_vdso_o_end);
140 if (elf32_insert_brand_entry(&cloudabi32_brand) < 0) {
141 printf("Failed to add CloudABI ELF brand handler\n");
146 if (elf32_brand_inuse(&cloudabi32_brand))
148 if (elf32_remove_brand_entry(&cloudabi32_brand) < 0) {
149 printf("Failed to remove CloudABI ELF brand handler\n");
152 cloudabi_vdso_destroy(cloudabi32_brand.sysvec);
159 static moduledata_t cloudabi32_module = {
165 DECLARE_MODULE_TIED(cloudabi32, cloudabi32_module, SI_SUB_EXEC, SI_ORDER_ANY);
166 MODULE_DEPEND(cloudabi32, cloudabi, 1, 1, 1);
167 FEATURE(cloudabi32, "CloudABI 32bit support");