1 /*-
2 * Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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
23 * SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/imgact.h>
31 #include <sys/kernel.h>
32 #include <sys/proc.h>
33 #include <sys/sysent.h>
34
35 #include <vm/vm.h>
36 #include <vm/pmap.h>
37
38 #include <machine/frame.h>
39 #include <machine/pcb.h>
40 #include <machine/vmparam.h>
41
42 #include <compat/cloudabi/cloudabi_util.h>
43
44 #include <compat/cloudabi32/cloudabi32_syscall.h>
45 #include <compat/cloudabi32/cloudabi32_util.h>
46
47 extern const char *cloudabi32_syscallnames[];
48 extern struct sysent cloudabi32_sysent[];
49
50 static void
cloudabi32_proc_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)51 cloudabi32_proc_setregs(struct thread *td, struct image_params *imgp,
52 uintptr_t stack)
53 {
54 struct trapframe *regs;
55
56 regs = td->td_frame;
57 memset(regs, 0, sizeof(*regs));
58 regs->tf_x[0] =
59 stack + roundup(sizeof(cloudabi32_tcb_t), sizeof(register_t));
60 regs->tf_x[13] = STACKALIGN(stack);
61 regs->tf_elr = imgp->entry_addr;
62 regs->tf_spsr |= PSR_AARCH32;
63 (void)cpu_set_user_tls(td, TO_PTR(stack));
64 }
65
66 static int
cloudabi32_fetch_syscall_args(struct thread * td)67 cloudabi32_fetch_syscall_args(struct thread *td)
68 {
69 struct trapframe *frame;
70 struct syscall_args *sa;
71 int error;
72
73 frame = td->td_frame;
74 sa = &td->td_sa;
75
76 /* Obtain system call number. */
77 sa->code = frame->tf_x[0];
78 if (sa->code >= CLOUDABI32_SYS_MAXSYSCALL)
79 return (ENOSYS);
80 sa->callp = &cloudabi32_sysent[sa->code];
81
82 /*
83 * Fetch system call arguments.
84 *
85 * The vDSO has already made sure that the arguments are
86 * eight-byte aligned. Pointers and size_t parameters are
87 * zero-extended. This makes it possible to copy in the
88 * arguments directly. As long as the call doesn't use 32-bit
89 * data structures, we can just invoke the same system call
90 * implementation used by 64-bit processes.
91 */
92 error = copyin((void *)frame->tf_x[2], sa->args,
93 sa->callp->sy_narg * sizeof(sa->args[0]));
94 if (error != 0)
95 return (error);
96
97 /* Default system call return values. */
98 td->td_retval[0] = 0;
99 td->td_retval[1] = 0;
100 return (0);
101 }
102
103 static void
cloudabi32_set_syscall_retval(struct thread * td,int error)104 cloudabi32_set_syscall_retval(struct thread *td, int error)
105 {
106 struct trapframe *frame = td->td_frame;
107
108 switch (error) {
109 case 0:
110 /*
111 * System call succeeded.
112 *
113 * Simply copy out the 64-bit return values into the
114 * same buffer provided for system call arguments. The
115 * vDSO will copy them to the right spot, truncating
116 * pointers and size_t values to 32 bits.
117 */
118 if (copyout(td->td_retval, (void *)frame->tf_x[2],
119 sizeof(td->td_retval)) == 0) {
120 frame->tf_x[0] = 0;
121 frame->tf_spsr &= ~PSR_C;
122 } else {
123 frame->tf_x[0] = CLOUDABI_EFAULT;
124 frame->tf_spsr |= PSR_C;
125 }
126 break;
127 case ERESTART:
128 /* Restart system call. */
129 frame->tf_elr -= 4;
130 break;
131 case EJUSTRETURN:
132 break;
133 default:
134 /* System call returned an error. */
135 frame->tf_x[0] = cloudabi_convert_errno(error);
136 frame->tf_spsr |= PSR_C;
137 break;
138 }
139 }
140
141 static void
cloudabi32_schedtail(struct thread * td)142 cloudabi32_schedtail(struct thread *td)
143 {
144 struct trapframe *frame = td->td_frame;
145 register_t retval[2];
146
147 /* Return values for processes returning from fork. */
148 if ((td->td_pflags & TDP_FORKING) != 0) {
149 retval[0] = CLOUDABI_PROCESS_CHILD;
150 retval[1] = td->td_tid;
151 copyout(retval, (void *)frame->tf_x[2], sizeof(retval));
152 }
153 frame->tf_spsr |= PSR_AARCH32;
154 }
155
156 int
cloudabi32_thread_setregs(struct thread * td,const cloudabi32_threadattr_t * attr,uint32_t tcb)157 cloudabi32_thread_setregs(struct thread *td,
158 const cloudabi32_threadattr_t *attr, uint32_t tcb)
159 {
160 struct trapframe *frame;
161
162 /*
163 * Pass in the thread ID of the new thread and the argument
164 * pointer provided by the parent thread in as arguments to the
165 * entry point.
166 */
167 frame = td->td_frame;
168 memset(frame, 0, sizeof(*frame));
169 frame->tf_x[0] = td->td_tid;
170 frame->tf_x[1] = attr->argument;
171 frame->tf_x[13] = STACKALIGN(attr->stack + attr->stack_len);
172 frame->tf_elr = attr->entry_point;
173
174 /* Set up TLS. */
175 return (cpu_set_user_tls(td, TO_PTR(tcb)));
176 }
177
178 static struct sysentvec cloudabi32_elf_sysvec = {
179 .sv_size = CLOUDABI32_SYS_MAXSYSCALL,
180 .sv_table = cloudabi32_sysent,
181 .sv_fixup = cloudabi32_fixup,
182 .sv_name = "CloudABI ELF32",
183 .sv_coredump = elf32_coredump,
184 .sv_minuser = VM_MIN_ADDRESS,
185 .sv_maxuser = (uintmax_t)1 << 32,
186 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
187 .sv_copyout_strings = cloudabi32_copyout_strings,
188 .sv_setregs = cloudabi32_proc_setregs,
189 .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_ILP32,
190 .sv_set_syscall_retval = cloudabi32_set_syscall_retval,
191 .sv_fetch_syscall_args = cloudabi32_fetch_syscall_args,
192 .sv_syscallnames = cloudabi32_syscallnames,
193 .sv_schedtail = cloudabi32_schedtail,
194 };
195
196 INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec);
197
198 Elf32_Brandinfo cloudabi32_brand = {
199 .brand = ELFOSABI_CLOUDABI,
200 .machine = EM_ARM,
201 .sysvec = &cloudabi32_elf_sysvec,
202 .flags = BI_BRAND_ONLY_STATIC,
203 };
204