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/cloudabi/cloudabi_util.h>
43 #include <compat/cloudabi64/cloudabi64_util.h>
45 extern char _binary_cloudabi64_vdso_o_start[];
46 extern char _binary_cloudabi64_vdso_o_end[];
49 cloudabi64_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 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
66 char canarybuf[64], pidbuf[16];
69 void *argdata, *canary, *pid;
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 argdata = *stack_base;
84 /* Store canary for stack smashing protection. */
85 arc4rand(canarybuf, sizeof(canarybuf), 0);
86 *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
88 error = copyout(canarybuf, canary, sizeof(canarybuf));
93 * Generate a random UUID that identifies the process. Right now
94 * we don't store this UUID in the kernel. Ideally, it should be
95 * exposed through ps(1).
97 arc4rand(pidbuf, sizeof(pidbuf), 0);
98 pidbuf[6] = (pidbuf[6] & 0x0f) | 0x40;
99 pidbuf[8] = (pidbuf[8] & 0x3f) | 0x80;
100 *stack_base -= howmany(sizeof(pidbuf), sizeof(register_t));
102 error = copyout(pidbuf, pid, sizeof(pidbuf));
107 * Compute length of program arguments. As the argument data is
108 * binary safe, we had to add a trailing null byte in
109 * exec_copyin_data_fds(). Undo this by reducing the length.
111 args = (Elf64_Auxargs *)imgp->auxargs;
112 argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
116 /* Write out an auxiliary vector. */
117 cloudabi64_auxv_t auxv[] = {
118 #define VAL(type, val) { .a_type = (type), .a_val = (val) }
119 #define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
120 PTR(CLOUDABI_AT_ARGDATA, argdata),
121 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
122 VAL(CLOUDABI_AT_BASE, args->base),
123 PTR(CLOUDABI_AT_CANARY, canary),
124 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
125 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
126 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
127 PTR(CLOUDABI_AT_PHDR, args->phdr),
128 VAL(CLOUDABI_AT_PHNUM, args->phnum),
129 PTR(CLOUDABI_AT_PID, pid),
130 PTR(CLOUDABI_AT_SYSINFO_EHDR,
131 imgp->proc->p_sysent->sv_shared_page_base),
132 VAL(CLOUDABI_AT_TID, td->td_tid),
135 { .a_type = CLOUDABI_AT_NULL },
137 *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
138 error = copyout(auxv, *stack_base, sizeof(auxv));
142 /* Reserve space for storing the TCB. */
143 *stack_base -= howmany(sizeof(cloudabi64_tcb_t), sizeof(register_t));
148 cloudabi64_modevent(module_t mod, int type, void *data)
153 cloudabi_vdso_init(cloudabi64_brand.sysvec,
154 _binary_cloudabi64_vdso_o_start,
155 _binary_cloudabi64_vdso_o_end);
156 if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
157 printf("Failed to add CloudABI ELF brand handler\n");
162 if (elf64_brand_inuse(&cloudabi64_brand))
164 if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
165 printf("Failed to remove CloudABI ELF brand handler\n");
168 cloudabi_vdso_destroy(cloudabi64_brand.sysvec);
175 static moduledata_t cloudabi64_module = {
181 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
182 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);
183 FEATURE(cloudabi64, "CloudABI 64bit support");