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/cloudabi64_types.h>
41 #include <compat/cloudabi64/cloudabi64_util.h>
44 cloudabi64_copyout_strings(struct image_params *imgp)
46 struct image_args *args;
50 /* Copy out program arguments. */
52 len = args->begin_envv - args->begin_argv;
53 begin = rounddown2(imgp->sysent->sv_usrstack - len, sizeof(register_t));
54 copyout(args->begin_argv, (void *)begin, len);
55 return ((register_t *)begin);
59 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
64 void *argdata, *canary;
69 * CloudABI executables do not store the FreeBSD OS release
70 * number in their header. Set the OS release number to the
71 * latest version of FreeBSD, so that system calls behave as if
75 td->td_proc->p_osrel = __FreeBSD_version;
77 /* Store canary for stack smashing protection. */
78 argdata = *stack_base;
79 arc4rand(canarybuf, sizeof(canarybuf), 0);
80 *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
82 error = copyout(canarybuf, canary, sizeof(canarybuf));
87 * Compute length of program arguments. As the argument data is
88 * binary safe, we had to add a trailing null byte in
89 * exec_copyin_data_fds(). Undo this by reducing the length.
91 args = (Elf64_Auxargs *)imgp->auxargs;
92 argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
96 /* Write out an auxiliary vector. */
97 cloudabi64_auxv_t auxv[] = {
98 #define VAL(type, val) { .a_type = (type), .a_val = (val) }
99 #define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
100 PTR(CLOUDABI_AT_ARGDATA, argdata),
101 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
102 VAL(CLOUDABI_AT_BASE, args->base),
103 PTR(CLOUDABI_AT_CANARY, canary),
104 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
105 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
106 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
107 PTR(CLOUDABI_AT_PHDR, args->phdr),
108 VAL(CLOUDABI_AT_PHNUM, args->phnum),
109 VAL(CLOUDABI_AT_TID, td->td_tid),
112 { .a_type = CLOUDABI_AT_NULL },
114 *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
115 error = copyout(auxv, *stack_base, sizeof(auxv));
119 /* Reserve space for storing the TCB. */
120 *stack_base -= howmany(sizeof(cloudabi64_tcb_t), sizeof(register_t));
125 cloudabi64_modevent(module_t mod, int type, void *data)
130 if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
131 printf("Failed to add CloudABI ELF brand handler\n");
136 if (elf64_brand_inuse(&cloudabi64_brand))
138 if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
139 printf("Failed to remove CloudABI ELF brand handler\n");
148 static moduledata_t cloudabi64_module = {
154 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
155 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);
156 FEATURE(cloudabi64, "CloudABI 64bit support");