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/sysent.h>
38 #include <sys/systm.h>
43 #include <machine/frame.h>
44 #include <machine/pcb.h>
45 #include <machine/pmap.h>
46 #include <machine/psl.h>
47 #include <machine/vmparam.h>
49 #include <compat/cloudabi/cloudabi_util.h>
51 #include <compat/cloudabi64/cloudabi64_syscall.h>
52 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
53 #include <compat/cloudabi64/cloudabi64_util.h>
55 extern const char *cloudabi64_syscallnames[];
56 extern struct sysent cloudabi64_sysent[];
59 cloudabi64_copyout_strings(struct image_params *imgp)
64 /* Copy out program arguments. */
65 len = imgp->args->begin_envv - imgp->args->begin_argv;
66 begin = rounddown2(USRSTACK - len, sizeof(register_t));
67 copyout(imgp->args->begin_argv, (void *)begin, len);
68 return ((register_t *)begin);
72 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
77 void *argdata, *canary;
82 * CloudABI executables do not store the FreeBSD OS release
83 * number in their header. Set the OS release number to the
84 * latest version of FreeBSD, so that system calls behave as if
88 td->td_proc->p_osrel = __FreeBSD_version;
90 /* Store canary for stack smashing protection. */
91 argdata = *stack_base;
92 arc4rand(canarybuf, sizeof(canarybuf), 0);
93 *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
95 error = copyout(canarybuf, canary, sizeof(canarybuf));
100 * Compute length of program arguments. As the argument data is
101 * binary safe, we had to add a trailing null byte in
102 * exec_copyin_data_fds(). Undo this by reducing the length.
104 args = (Elf64_Auxargs *)imgp->auxargs;
105 argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
109 /* Write out an auxiliary vector. */
110 cloudabi64_auxv_t auxv[] = {
111 #define VAL(type, val) { .a_type = (type), .a_val = (val) }
112 #define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
113 PTR(CLOUDABI_AT_ARGDATA, argdata),
114 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
115 PTR(CLOUDABI_AT_CANARY, canary),
116 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
117 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
118 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
119 PTR(CLOUDABI_AT_PHDR, args->phdr),
120 VAL(CLOUDABI_AT_PHNUM, args->phnum),
121 VAL(CLOUDABI_AT_TID, td->td_tid),
124 { .a_type = CLOUDABI_AT_NULL },
126 *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
127 return (copyout(auxv, *stack_base, sizeof(auxv)));
131 cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
133 struct trapframe *frame = td->td_frame;
135 /* Obtain system call number. */
136 sa->code = frame->tf_rax;
137 if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
139 sa->callp = &cloudabi64_sysent[sa->code];
141 /* Fetch system call arguments. */
142 sa->args[0] = frame->tf_rdi;
143 sa->args[1] = frame->tf_rsi;
144 sa->args[2] = frame->tf_rdx;
145 sa->args[3] = frame->tf_rcx; /* Actually %r10. */
146 sa->args[4] = frame->tf_r8;
147 sa->args[5] = frame->tf_r9;
149 /* Default system call return values. */
150 td->td_retval[0] = 0;
151 td->td_retval[1] = frame->tf_rdx;
156 cloudabi64_set_syscall_retval(struct thread *td, int error)
158 struct trapframe *frame = td->td_frame;
162 /* System call succeeded. */
163 frame->tf_rax = td->td_retval[0];
164 frame->tf_rdx = td->td_retval[1];
165 frame->tf_rflags &= ~PSL_C;
168 /* Restart system call. */
169 frame->tf_rip -= frame->tf_err;
170 frame->tf_r10 = frame->tf_rcx;
171 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
176 /* System call returned an error. */
177 frame->tf_rax = cloudabi_convert_errno(error);
178 frame->tf_rflags |= PSL_C;
184 cloudabi64_schedtail(struct thread *td)
186 struct trapframe *frame = td->td_frame;
188 /* Initial register values for processes returning from fork. */
189 frame->tf_rax = CLOUDABI_PROCESS_CHILD;
190 frame->tf_rdx = td->td_tid;
194 cloudabi64_thread_setregs(struct thread *td,
195 const cloudabi64_threadattr_t *attr)
197 struct trapframe *frame;
200 /* Perform standard register initialization. */
201 stack.ss_sp = (void *)attr->stack;
202 stack.ss_size = attr->stack_size;
203 cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
206 * Pass in the thread ID of the new thread and the argument
207 * pointer provided by the parent thread in as arguments to the
210 frame = td->td_frame;
211 frame->tf_rdi = td->td_tid;
212 frame->tf_rsi = attr->argument;
215 static struct sysentvec cloudabi64_elf_sysvec = {
216 .sv_size = CLOUDABI64_SYS_MAXSYSCALL,
217 .sv_table = cloudabi64_sysent,
218 .sv_fixup = cloudabi64_fixup,
219 .sv_name = "CloudABI ELF64",
220 .sv_coredump = elf64_coredump,
221 .sv_pagesize = PAGE_SIZE,
222 .sv_minuser = VM_MIN_ADDRESS,
223 .sv_maxuser = VM_MAXUSER_ADDRESS,
224 .sv_usrstack = USRSTACK,
225 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
226 .sv_copyout_strings = cloudabi64_copyout_strings,
227 .sv_flags = SV_ABI_CLOUDABI,
228 .sv_set_syscall_retval = cloudabi64_set_syscall_retval,
229 .sv_fetch_syscall_args = cloudabi64_fetch_syscall_args,
230 .sv_syscallnames = cloudabi64_syscallnames,
231 .sv_schedtail = cloudabi64_schedtail,
234 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
236 static Elf64_Brandinfo cloudabi64_brand = {
237 .brand = ELFOSABI_CLOUDABI,
238 .machine = EM_X86_64,
239 .sysvec = &cloudabi64_elf_sysvec,
240 .compat_3_brand = "CloudABI",
244 cloudabi64_modevent(module_t mod, int type, void *data)
249 if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
250 printf("Failed to add CloudABI ELF brand handler\n");
255 if (elf64_brand_inuse(&cloudabi64_brand))
257 if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
258 printf("Failed to remove CloudABI ELF brand handler\n");
267 static moduledata_t cloudabi64_module = {
273 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
274 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);