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/kernel.h>
32 #include <sys/sysent.h>
37 #include <machine/frame.h>
38 #include <machine/pcb.h>
39 #include <machine/pmap.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_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
53 struct trapframe *frame = td->td_frame;
56 /* Obtain system call number. */
57 sa->code = frame->tf_x[8];
58 if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
60 sa->callp = &cloudabi64_sysent[sa->code];
62 /* Fetch system call arguments. */
63 for (i = 0; i < MAXARGS; i++)
64 sa->args[i] = frame->tf_x[i];
66 /* Default system call return values. */
68 td->td_retval[1] = frame->tf_x[1];
73 cloudabi64_set_syscall_retval(struct thread *td, int error)
75 struct trapframe *frame = td->td_frame;
79 /* System call succeeded. */
80 frame->tf_x[0] = td->td_retval[0];
81 frame->tf_x[1] = td->td_retval[1];
82 frame->tf_spsr &= ~PSR_C;
85 /* Restart system call. */
91 /* System call returned an error. */
92 frame->tf_x[0] = cloudabi_convert_errno(error);
93 frame->tf_spsr |= PSR_C;
99 cloudabi64_schedtail(struct thread *td)
101 struct trapframe *frame = td->td_frame;
104 * Initial register values for processes returning from fork.
105 * Make sure that we only set these values when forking, not
106 * when creating a new thread.
108 if ((td->td_pflags & TDP_FORKING) != 0) {
109 frame->tf_x[0] = CLOUDABI_PROCESS_CHILD;
110 frame->tf_x[1] = td->td_tid;
115 cloudabi64_thread_setregs(struct thread *td,
116 const cloudabi64_threadattr_t *attr)
118 struct trapframe *frame;
121 /* Perform standard register initialization. */
122 stack.ss_sp = (void *)attr->stack;
123 stack.ss_size = attr->stack_size;
124 cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
127 * Pass in the thread ID of the new thread and the argument
128 * pointer provided by the parent thread in as arguments to the
131 frame = td->td_frame;
132 frame->tf_x[0] = td->td_tid;
133 frame->tf_x[1] = attr->argument;
136 static struct sysentvec cloudabi64_elf_sysvec = {
137 .sv_size = CLOUDABI64_SYS_MAXSYSCALL,
138 .sv_table = cloudabi64_sysent,
139 .sv_fixup = cloudabi64_fixup,
140 .sv_name = "CloudABI ELF64",
141 .sv_coredump = elf64_coredump,
142 .sv_pagesize = PAGE_SIZE,
143 .sv_minuser = VM_MIN_ADDRESS,
144 .sv_maxuser = VM_MAXUSER_ADDRESS,
145 .sv_usrstack = USRSTACK,
146 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
147 .sv_copyout_strings = cloudabi64_copyout_strings,
148 .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM,
149 .sv_set_syscall_retval = cloudabi64_set_syscall_retval,
150 .sv_fetch_syscall_args = cloudabi64_fetch_syscall_args,
151 .sv_syscallnames = cloudabi64_syscallnames,
152 .sv_schedtail = cloudabi64_schedtail,
155 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
157 Elf64_Brandinfo cloudabi64_brand = {
158 .brand = ELFOSABI_CLOUDABI,
159 .machine = EM_AARCH64,
160 .sysvec = &cloudabi64_elf_sysvec,
161 .compat_3_brand = "CloudABI",