]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/cloudabi64/cloudabi64_sysvec.c
linux(4): Regen for clone3 system call.
[FreeBSD/FreeBSD.git] / sys / amd64 / cloudabi64 / cloudabi64_sysvec.c
1 /*-
2  * Copyright (c) 2015 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/cloudabi64/cloudabi64_syscall.h>
46 #include <compat/cloudabi64/cloudabi64_util.h>
47
48 extern const char *cloudabi64_syscallnames[];
49 extern struct sysent cloudabi64_sysent[];
50
51 static int
52 cloudabi64_fixup_tcb(uintptr_t *stack_base, struct image_params *imgp)
53 {
54         int error;
55         register_t tcbptr;
56
57         /* Place auxiliary vector and TCB on the stack. */
58         error = cloudabi64_fixup(stack_base, imgp);
59         if (error != 0)
60                 return (error);
61
62         /*
63          * On x86-64, the TCB is referred to by %fs:0. Take some space
64          * from the top of the stack to store a single element array,
65          * containing a pointer to the TCB. %fs base will point to this.
66          */
67         tcbptr = (register_t)*stack_base;
68         *stack_base -= sizeof(tcbptr);
69         return (copyout(&tcbptr, (void *)*stack_base, sizeof(tcbptr)));
70 }
71
72 static void
73 cloudabi64_proc_setregs(struct thread *td, struct image_params *imgp,
74     uintptr_t stack)
75 {
76         struct trapframe *regs;
77
78         exec_setregs(td, imgp, stack);
79
80         /*
81          * The stack now contains a pointer to the TCB, the TCB itself,
82          * and the auxiliary vector. Let %rdx point to the auxiliary
83          * vector, and set %fs base to the address of the TCB.
84          */
85         regs = td->td_frame;
86         regs->tf_rdi = stack + sizeof(register_t) +
87             roundup(sizeof(cloudabi64_tcb_t), sizeof(register_t));
88         (void)cpu_set_user_tls(td, TO_PTR(stack));
89 }
90
91 static int
92 cloudabi64_fetch_syscall_args(struct thread *td)
93 {
94         struct trapframe *frame;
95         struct syscall_args *sa;
96
97         frame = td->td_frame;
98         sa = &td->td_sa;
99
100         /* Obtain system call number. */
101         sa->code = frame->tf_rax;
102         if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
103                 return (ENOSYS);
104         sa->callp = &cloudabi64_sysent[sa->code];
105
106         /* Fetch system call arguments. */
107         sa->args[0] = frame->tf_rdi;
108         sa->args[1] = frame->tf_rsi;
109         sa->args[2] = frame->tf_rdx;
110         sa->args[3] = frame->tf_rcx; /* Actually %r10. */
111         sa->args[4] = frame->tf_r8;
112         sa->args[5] = frame->tf_r9;
113
114         /* Default system call return values. */
115         td->td_retval[0] = 0;
116         td->td_retval[1] = frame->tf_rdx;
117         return (0);
118 }
119
120 static void
121 cloudabi64_set_syscall_retval(struct thread *td, int error)
122 {
123         struct trapframe *frame = td->td_frame;
124
125         switch (error) {
126         case 0:
127                 /* System call succeeded. */
128                 frame->tf_rax = td->td_retval[0];
129                 frame->tf_rdx = td->td_retval[1];
130                 frame->tf_rflags &= ~PSL_C;
131                 break;
132         case ERESTART:
133                 /* Restart system call. */
134                 frame->tf_rip -= frame->tf_err;
135                 frame->tf_r10 = frame->tf_rcx;
136                 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
137                 break;
138         case EJUSTRETURN:
139                 break;
140         default:
141                 /* System call returned an error. */
142                 frame->tf_rax = cloudabi_convert_errno(error);
143                 frame->tf_rflags |= PSL_C;
144                 break;
145         }
146 }
147
148 static void
149 cloudabi64_schedtail(struct thread *td)
150 {
151         struct trapframe *frame = td->td_frame;
152
153         /* Initial register values for processes returning from fork. */
154         frame->tf_rax = CLOUDABI_PROCESS_CHILD;
155         frame->tf_rdx = td->td_tid;
156 }
157
158 int
159 cloudabi64_thread_setregs(struct thread *td,
160     const cloudabi64_threadattr_t *attr, uint64_t tcb)
161 {
162         struct trapframe *frame;
163         stack_t stack;
164         uint64_t tcbptr;
165         int error;
166
167         /*
168          * On x86-64, the TCB is referred to by %fs:0. Take some space
169          * from the top of the stack to store a single element array,
170          * containing a pointer to the TCB. %fs base will point to this.
171          */
172         tcbptr = rounddown(attr->stack + attr->stack_len - sizeof(tcbptr),
173             _Alignof(tcbptr));
174         error = copyout(&tcb, (void *)tcbptr, sizeof(tcb));
175         if (error != 0)
176                 return (error);
177
178         /* Perform standard register initialization. */
179         stack.ss_sp = TO_PTR(attr->stack);
180         stack.ss_size = tcbptr - attr->stack;
181         cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack);
182
183         /*
184          * Pass in the thread ID of the new thread and the argument
185          * pointer provided by the parent thread in as arguments to the
186          * entry point.
187          */
188         frame = td->td_frame;
189         frame->tf_rdi = td->td_tid;
190         frame->tf_rsi = attr->argument;
191
192         return (cpu_set_user_tls(td, TO_PTR(tcbptr)));
193 }
194
195 static struct sysentvec cloudabi64_elf_sysvec = {
196         .sv_size                = CLOUDABI64_SYS_MAXSYSCALL,
197         .sv_table               = cloudabi64_sysent,
198         .sv_fixup               = cloudabi64_fixup_tcb,
199         .sv_name                = "CloudABI ELF64",
200         .sv_coredump            = elf64_coredump,
201         .sv_elf_core_osabi      = ELFOSABI_FREEBSD,
202         .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
203         .sv_elf_core_prepare_notes = elf64_prepare_notes,
204         .sv_minuser             = VM_MIN_ADDRESS,
205         /* Keep top page reserved to work around AMD Ryzen stability issues. */
206         .sv_maxuser             = VM_MAXUSER_ADDRESS - PAGE_SIZE,
207         .sv_stackprot           = VM_PROT_READ | VM_PROT_WRITE,
208         .sv_copyout_strings     = cloudabi64_copyout_strings,
209         .sv_setregs             = cloudabi64_proc_setregs,
210         .sv_flags               = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_LP64,
211         .sv_set_syscall_retval  = cloudabi64_set_syscall_retval,
212         .sv_fetch_syscall_args  = cloudabi64_fetch_syscall_args,
213         .sv_syscallnames        = cloudabi64_syscallnames,
214         .sv_schedtail           = cloudabi64_schedtail,
215         .sv_set_fork_retval     = x86_set_fork_retval,
216 };
217
218 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
219
220 Elf64_Brandinfo cloudabi64_brand = {
221         .brand          = ELFOSABI_CLOUDABI,
222         .machine        = EM_X86_64,
223         .sysvec         = &cloudabi64_elf_sysvec,
224         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_ONLY_STATIC,
225 };