1 /*-
2 * Copyright (c) 2015-2016 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/md_var.h>
40 #include <machine/pcb.h>
41 #include <machine/vmparam.h>
42
43 #include <compat/cloudabi/cloudabi_util.h>
44
45 #include <compat/cloudabi32/cloudabi32_syscall.h>
46 #include <compat/cloudabi32/cloudabi32_util.h>
47
48 extern const char *cloudabi32_syscallnames[];
49 extern struct sysent cloudabi32_sysent[];
50
51 static int
52 cloudabi32_fixup_tcb(uintptr_t *stack_base, struct image_params *imgp)
53 {
54 int error;
55 uint32_t args[2];
56
57 /* Place auxiliary vector and TCB on the stack. */
58 error = cloudabi32_fixup(stack_base, imgp);
59 if (error != 0)
60 return (error);
61
62 /*
63 * On i386, the TCB is referred to by %gs:0. Reuse the empty
64 * space normally used by the return address (args[0]) to store
65 * a single element array, containing a pointer to the TCB. %gs
66 * base will point to this.
67 *
68 * Also let the first argument of the entry point (args[1])
69 * refer to the auxiliary vector, which is stored right after
70 * the TCB.
71 */
72 args[0] = *stack_base;
73 args[1] = *stack_base +
74 roundup(sizeof(cloudabi32_tcb_t), sizeof(register_t));
75 *stack_base -= roundup(sizeof(args), sizeof(register_t));
76 return (copyout(args, (void *)*stack_base, sizeof(args)));
77 }
78
79 static void
80 cloudabi32_proc_setregs(struct thread *td, struct image_params *imgp,
81 uintptr_t stack)
82 {
83
84 exec_setregs(td, imgp, stack);
85 (void)cpu_set_user_tls(td, TO_PTR(stack));
86 }
87
88 static int
89 cloudabi32_fetch_syscall_args(struct thread *td)
90 {
91 struct trapframe *frame;
92 struct syscall_args *sa;
93 int error;
94
95 frame = td->td_frame;
96 sa = &td->td_sa;
97
98 /* Obtain system call number. */
99 sa->code = frame->tf_eax;
100 if (sa->code >= CLOUDABI32_SYS_MAXSYSCALL)
101 return (ENOSYS);
102 sa->callp = &cloudabi32_sysent[sa->code];
103
104 /* Fetch system call arguments from the stack. */
105 error = copyin((void *)(frame->tf_esp + 4), sa->args,
106 sa->callp->sy_narg * sizeof(sa->args[0]));
107 if (error != 0)
108 return (error);
109
110 /* Default system call return values. */
111 td->td_retval[0] = 0;
112 td->td_retval[1] = frame->tf_edx;
113 return (0);
114 }
115
116 static void
117 cloudabi32_set_syscall_retval(struct thread *td, int error)
118 {
119 struct trapframe *frame = td->td_frame;
120
121 switch (error) {
122 case 0:
123 /* System call succeeded. */
124 frame->tf_eax = td->td_retval[0];
125 frame->tf_edx = td->td_retval[1];
126 frame->tf_eflags &= ~PSL_C;
127 break;
128 case ERESTART:
129 /* Restart system call. */
130 frame->tf_eip -= frame->tf_err;
131 break;
132 case EJUSTRETURN:
133 break;
134 default:
135 /* System call returned an error. */
136 frame->tf_eax = cloudabi_convert_errno(error);
137 frame->tf_eflags |= PSL_C;
138 break;
139 }
140 }
141
142 static void
143 cloudabi32_schedtail(struct thread *td)
144 {
145 struct trapframe *frame = td->td_frame;
146
147 /* Initial register values for processes returning from fork. */
148 frame->tf_eax = CLOUDABI_PROCESS_CHILD;
149 frame->tf_edx = td->td_tid;
150 }
151
152 int
153 cloudabi32_thread_setregs(struct thread *td,
154 const cloudabi32_threadattr_t *attr, uint32_t tcb)
155 {
156 stack_t stack;
157 uint32_t args[3];
158 void *frameptr;
159 int error;
160
161 /* Perform standard register initialization. */
162 stack.ss_sp = TO_PTR(attr->stack);
163 stack.ss_size = attr->stack_len - sizeof(args);
164 cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack);
165
166 /*
167 * Copy the arguments for the thread entry point onto the stack
168 * (args[1] and args[2]). Similar to process startup, use the
169 * otherwise unused return address (args[0]) for TLS.
170 */
171 args[0] = tcb;
172 args[1] = td->td_tid;
173 args[2] = attr->argument;
174 frameptr = (void *)td->td_frame->tf_esp;
175 error = copyout(args, frameptr, sizeof(args));
176 if (error != 0)
177 return (error);
178
179 return (cpu_set_user_tls(td, frameptr));
180 }
181
182 static struct sysentvec cloudabi32_elf_sysvec = {
183 .sv_size = CLOUDABI32_SYS_MAXSYSCALL,
184 .sv_table = cloudabi32_sysent,
185 .sv_fixup = cloudabi32_fixup_tcb,
186 .sv_name = "CloudABI ELF32",
187 .sv_coredump = elf32_coredump,
188 .sv_minuser = VM_MIN_ADDRESS,
189 .sv_maxuser = VM_MAXUSER_ADDRESS,
190 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
191 .sv_copyout_strings = cloudabi32_copyout_strings,
192 .sv_setregs = cloudabi32_proc_setregs,
193 .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_IA32 | SV_ILP32,
194 .sv_set_syscall_retval = cloudabi32_set_syscall_retval,
195 .sv_fetch_syscall_args = cloudabi32_fetch_syscall_args,
196 .sv_syscallnames = cloudabi32_syscallnames,
197 .sv_schedtail = cloudabi32_schedtail,
198 .sv_set_fork_retval = x86_set_fork_retval,
199 };
200
201 INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec);
202
203 Elf32_Brandinfo cloudabi32_brand = {
204 .brand = ELFOSABI_CLOUDABI,
205 .machine = EM_386,
206 .sysvec = &cloudabi32_elf_sysvec,
207 .flags = BI_BRAND_ONLY_STATIC,
208 };
Cache object: a863b87c9b163e530a944433fc5a01bb
|