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>
31 #include <sys/imgact.h>
32 #include <sys/imgact_elf.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
37 #include <sys/sysctl.h>
38 #include <sys/sysent.h>
39 #include <sys/systm.h>
44 #include <machine/frame.h>
45 #include <machine/pcb.h>
46 #include <machine/pmap.h>
47 #include <machine/psl.h>
48 #include <machine/vmparam.h>
50 #include <compat/cloudabi/cloudabi_util.h>
52 #include <compat/cloudabi64/cloudabi64_syscall.h>
53 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
54 #include <compat/cloudabi64/cloudabi64_util.h>
56 extern const char *cloudabi64_syscallnames[];
57 extern struct sysent cloudabi64_sysent[];
60 cloudabi64_copyout_strings(struct image_params *imgp)
65 /* Copy out program arguments. */
66 len = imgp->args->begin_envv - imgp->args->begin_argv;
67 begin = rounddown2(USRSTACK - len, sizeof(register_t));
68 copyout(imgp->args->begin_argv, (void *)begin, len);
69 return ((register_t *)begin);
73 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
78 void *argdata, *canary;
83 * CloudABI executables do not store the FreeBSD OS release
84 * number in their header. Set the OS release number to the
85 * latest version of FreeBSD, so that system calls behave as if
89 td->td_proc->p_osrel = __FreeBSD_version;
91 /* Store canary for stack smashing protection. */
92 argdata = *stack_base;
93 arc4rand(canarybuf, sizeof(canarybuf), 0);
94 *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
96 error = copyout(canarybuf, canary, sizeof(canarybuf));
101 * Compute length of program arguments. As the argument data is
102 * binary safe, we had to add a trailing null byte in
103 * exec_copyin_data_fds(). Undo this by reducing the length.
105 args = (Elf64_Auxargs *)imgp->auxargs;
106 argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
110 /* Write out an auxiliary vector. */
111 cloudabi64_auxv_t auxv[] = {
112 #define VAL(type, val) { .a_type = (type), .a_val = (val) }
113 #define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
114 PTR(CLOUDABI_AT_ARGDATA, argdata),
115 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
116 PTR(CLOUDABI_AT_CANARY, canary),
117 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
118 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
119 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
120 PTR(CLOUDABI_AT_PHDR, args->phdr),
121 VAL(CLOUDABI_AT_PHNUM, args->phnum),
122 VAL(CLOUDABI_AT_TID, td->td_tid),
125 { .a_type = CLOUDABI_AT_NULL },
127 *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
128 return (copyout(auxv, *stack_base, sizeof(auxv)));
132 cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
134 struct trapframe *frame = td->td_frame;
136 /* Obtain system call number. */
137 sa->code = frame->tf_rax;
138 if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
140 sa->callp = &cloudabi64_sysent[sa->code];
142 /* Fetch system call arguments. */
143 sa->args[0] = frame->tf_rdi;
144 sa->args[1] = frame->tf_rsi;
145 sa->args[2] = frame->tf_rdx;
146 sa->args[3] = frame->tf_rcx; /* Actually %r10. */
147 sa->args[4] = frame->tf_r8;
148 sa->args[5] = frame->tf_r9;
150 /* Default system call return values. */
151 td->td_retval[0] = 0;
152 td->td_retval[1] = frame->tf_rdx;
157 cloudabi64_set_syscall_retval(struct thread *td, int error)
159 struct trapframe *frame = td->td_frame;
163 /* System call succeeded. */
164 frame->tf_rax = td->td_retval[0];
165 frame->tf_rdx = td->td_retval[1];
166 frame->tf_rflags &= ~PSL_C;
169 /* Restart system call. */
170 frame->tf_rip -= frame->tf_err;
171 frame->tf_r10 = frame->tf_rcx;
172 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
177 /* System call returned an error. */
178 frame->tf_rax = cloudabi_convert_errno(error);
179 frame->tf_rflags |= PSL_C;
185 cloudabi64_schedtail(struct thread *td)
187 struct trapframe *frame = td->td_frame;
189 /* Initial register values for processes returning from fork. */
190 frame->tf_rax = CLOUDABI_PROCESS_CHILD;
191 frame->tf_rdx = td->td_tid;
195 cloudabi64_thread_setregs(struct thread *td,
196 const cloudabi64_threadattr_t *attr)
198 struct trapframe *frame;
201 /* Perform standard register initialization. */
202 stack.ss_sp = (void *)attr->stack;
203 stack.ss_size = attr->stack_size;
204 cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
207 * Pass in the thread ID of the new thread and the argument
208 * pointer provided by the parent thread in as arguments to the
211 frame = td->td_frame;
212 frame->tf_rdi = td->td_tid;
213 frame->tf_rsi = attr->argument;
216 static struct sysentvec cloudabi64_elf_sysvec = {
217 .sv_size = CLOUDABI64_SYS_MAXSYSCALL,
218 .sv_table = cloudabi64_sysent,
219 .sv_fixup = cloudabi64_fixup,
220 .sv_name = "CloudABI ELF64",
221 .sv_coredump = elf64_coredump,
222 .sv_pagesize = PAGE_SIZE,
223 .sv_minuser = VM_MIN_ADDRESS,
224 .sv_maxuser = VM_MAXUSER_ADDRESS,
225 .sv_usrstack = USRSTACK,
226 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
227 .sv_copyout_strings = cloudabi64_copyout_strings,
228 .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM,
229 .sv_set_syscall_retval = cloudabi64_set_syscall_retval,
230 .sv_fetch_syscall_args = cloudabi64_fetch_syscall_args,
231 .sv_syscallnames = cloudabi64_syscallnames,
232 .sv_schedtail = cloudabi64_schedtail,
235 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
237 static Elf64_Brandinfo cloudabi64_brand = {
238 .brand = ELFOSABI_CLOUDABI,
239 .machine = EM_X86_64,
240 .sysvec = &cloudabi64_elf_sysvec,
241 .compat_3_brand = "CloudABI",
245 cloudabi64_modevent(module_t mod, int type, void *data)
250 if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
251 printf("Failed to add CloudABI ELF brand handler\n");
256 if (elf64_brand_inuse(&cloudabi64_brand))
258 if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
259 printf("Failed to remove CloudABI ELF brand handler\n");
268 static moduledata_t cloudabi64_module = {
274 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
275 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);
276 FEATURE(cloudabi64, "CloudABI 64bit support");