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>
33 #include <sys/sysent.h>
38 #include <machine/frame.h>
39 #include <machine/pcb.h>
40 #include <machine/vmparam.h>
42 #include <compat/cloudabi/cloudabi_util.h>
44 #include <compat/cloudabi64/cloudabi64_syscall.h>
45 #include <compat/cloudabi64/cloudabi64_util.h>
47 extern const char *cloudabi64_syscallnames[];
48 extern struct sysent cloudabi64_sysent[];
51 cloudabi64_fixup_tcb(register_t **stack_base, struct image_params *imgp)
56 /* Place auxiliary vector and TCB on the stack. */
57 error = cloudabi64_fixup(stack_base, imgp);
62 * On x86-64, the TCB is referred to by %fs:0. Take some space
63 * from the top of the stack to store a single element array,
64 * containing a pointer to the TCB. %fs base will point to this.
66 tcbptr = (register_t)*stack_base;
67 return (copyout(&tcbptr, --*stack_base, sizeof(tcbptr)));
71 cloudabi64_proc_setregs(struct thread *td, struct image_params *imgp,
74 struct trapframe *regs;
76 exec_setregs(td, imgp, stack);
79 * The stack now contains a pointer to the TCB, the TCB itself,
80 * and the auxiliary vector. Let %rdx point to the auxiliary
81 * vector, and set %fs base to the address of the TCB.
84 regs->tf_rdi = stack + sizeof(register_t) +
85 roundup(sizeof(cloudabi64_tcb_t), sizeof(register_t));
86 (void)cpu_set_user_tls(td, TO_PTR(stack));
90 cloudabi64_fetch_syscall_args(struct thread *td)
92 struct trapframe *frame;
93 struct syscall_args *sa;
98 /* Obtain system call number. */
99 sa->code = frame->tf_rax;
100 if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
102 sa->callp = &cloudabi64_sysent[sa->code];
103 sa->narg = sa->callp->sy_narg;
105 /* Fetch system call arguments. */
106 sa->args[0] = frame->tf_rdi;
107 sa->args[1] = frame->tf_rsi;
108 sa->args[2] = frame->tf_rdx;
109 sa->args[3] = frame->tf_rcx; /* Actually %r10. */
110 sa->args[4] = frame->tf_r8;
111 sa->args[5] = frame->tf_r9;
113 /* Default system call return values. */
114 td->td_retval[0] = 0;
115 td->td_retval[1] = frame->tf_rdx;
120 cloudabi64_set_syscall_retval(struct thread *td, int error)
122 struct trapframe *frame = td->td_frame;
126 /* System call succeeded. */
127 frame->tf_rax = td->td_retval[0];
128 frame->tf_rdx = td->td_retval[1];
129 frame->tf_rflags &= ~PSL_C;
132 /* Restart system call. */
133 frame->tf_rip -= frame->tf_err;
134 frame->tf_r10 = frame->tf_rcx;
135 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
140 /* System call returned an error. */
141 frame->tf_rax = cloudabi_convert_errno(error);
142 frame->tf_rflags |= PSL_C;
148 cloudabi64_schedtail(struct thread *td)
150 struct trapframe *frame = td->td_frame;
152 /* Initial register values for processes returning from fork. */
153 frame->tf_rax = CLOUDABI_PROCESS_CHILD;
154 frame->tf_rdx = td->td_tid;
158 cloudabi64_thread_setregs(struct thread *td,
159 const cloudabi64_threadattr_t *attr, uint64_t tcb)
161 struct trapframe *frame;
167 * On x86-64, the TCB is referred to by %fs:0. Take some space
168 * from the top of the stack to store a single element array,
169 * containing a pointer to the TCB. %fs base will point to this.
171 tcbptr = rounddown(attr->stack + attr->stack_len - sizeof(tcbptr),
173 error = copyout(&tcb, (void *)tcbptr, sizeof(tcb));
177 /* Perform standard register initialization. */
178 stack.ss_sp = TO_PTR(attr->stack);
179 stack.ss_size = tcbptr - attr->stack;
180 cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack);
183 * Pass in the thread ID of the new thread and the argument
184 * pointer provided by the parent thread in as arguments to the
187 frame = td->td_frame;
188 frame->tf_rdi = td->td_tid;
189 frame->tf_rsi = attr->argument;
191 return (cpu_set_user_tls(td, TO_PTR(tcbptr)));
194 static struct sysentvec cloudabi64_elf_sysvec = {
195 .sv_size = CLOUDABI64_SYS_MAXSYSCALL,
196 .sv_table = cloudabi64_sysent,
197 .sv_fixup = cloudabi64_fixup_tcb,
198 .sv_name = "CloudABI ELF64",
199 .sv_coredump = elf64_coredump,
200 .sv_minuser = VM_MIN_ADDRESS,
201 /* Keep top page reserved to work around AMD Ryzen stability issues. */
202 .sv_maxuser = VM_MAXUSER_ADDRESS - PAGE_SIZE,
203 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
204 .sv_copyout_strings = cloudabi64_copyout_strings,
205 .sv_setregs = cloudabi64_proc_setregs,
206 .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_LP64,
207 .sv_set_syscall_retval = cloudabi64_set_syscall_retval,
208 .sv_fetch_syscall_args = cloudabi64_fetch_syscall_args,
209 .sv_syscallnames = cloudabi64_syscallnames,
210 .sv_schedtail = cloudabi64_schedtail,
213 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
215 Elf64_Brandinfo cloudabi64_brand = {
216 .brand = ELFOSABI_CLOUDABI,
217 .machine = EM_X86_64,
218 .sysvec = &cloudabi64_elf_sysvec,
219 .flags = BI_CAN_EXEC_DYN | BI_BRAND_ONLY_STATIC,