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