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