]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/cloudabi64/cloudabi64_sysvec.c
Merge libxo 0.4.6
[FreeBSD/FreeBSD.git] / sys / arm64 / 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/pcb.h>
40 #include <machine/vmparam.h>
41
42 #include <compat/cloudabi/cloudabi_util.h>
43
44 #include <compat/cloudabi64/cloudabi64_syscall.h>
45 #include <compat/cloudabi64/cloudabi64_util.h>
46
47 extern const char *cloudabi64_syscallnames[];
48 extern struct sysent cloudabi64_sysent[];
49
50 static void
51 cloudabi64_proc_setregs(struct thread *td, struct image_params *imgp,
52     unsigned long stack)
53 {
54         struct trapframe *regs;
55
56         exec_setregs(td, imgp, stack);
57
58         /*
59          * The stack now contains a pointer to the TCB and the auxiliary
60          * vector. Let x0 point to the auxiliary vector, and set
61          * tpidr_el0 to the TCB.
62          */
63         regs = td->td_frame;
64         regs->tf_x[0] = td->td_retval[0] =
65             stack + roundup(sizeof(cloudabi64_tcb_t), sizeof(register_t));
66         (void)cpu_set_user_tls(td, (void *)stack);
67 }
68
69 static int
70 cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
71 {
72         struct trapframe *frame = td->td_frame;
73         int i;
74
75         /* Obtain system call number. */
76         sa->code = frame->tf_x[8];
77         if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
78                 return (ENOSYS);
79         sa->callp = &cloudabi64_sysent[sa->code];
80
81         /* Fetch system call arguments. */
82         for (i = 0; i < MAXARGS; i++)
83                 sa->args[i] = frame->tf_x[i];
84
85         /* Default system call return values. */
86         td->td_retval[0] = 0;
87         td->td_retval[1] = frame->tf_x[1];
88         return (0);
89 }
90
91 static void
92 cloudabi64_set_syscall_retval(struct thread *td, int error)
93 {
94         struct trapframe *frame = td->td_frame;
95
96         switch (error) {
97         case 0:
98                 /* System call succeeded. */
99                 frame->tf_x[0] = td->td_retval[0];
100                 frame->tf_x[1] = td->td_retval[1];
101                 frame->tf_spsr &= ~PSR_C;
102                 break;
103         case ERESTART:
104                 /* Restart system call. */
105                 frame->tf_elr -= 4;
106                 break;
107         case EJUSTRETURN:
108                 break;
109         default:
110                 /* System call returned an error. */
111                 frame->tf_x[0] = cloudabi_convert_errno(error);
112                 frame->tf_spsr |= PSR_C;
113                 break;
114         }
115 }
116
117 static void
118 cloudabi64_schedtail(struct thread *td)
119 {
120         struct trapframe *frame = td->td_frame;
121
122         /*
123          * Initial register values for processes returning from fork.
124          * Make sure that we only set these values when forking, not
125          * when creating a new thread.
126          */
127         if ((td->td_pflags & TDP_FORKING) != 0) {
128                 frame->tf_x[0] = CLOUDABI_PROCESS_CHILD;
129                 frame->tf_x[1] = td->td_tid;
130         }
131 }
132
133 int
134 cloudabi64_thread_setregs(struct thread *td,
135     const cloudabi64_threadattr_t *attr, uint64_t tcb)
136 {
137         struct trapframe *frame;
138         stack_t stack;
139
140         /* Perform standard register initialization. */
141         stack.ss_sp = (void *)attr->stack;
142         stack.ss_size = attr->stack_size;
143         cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
144
145         /*
146          * Pass in the thread ID of the new thread and the argument
147          * pointer provided by the parent thread in as arguments to the
148          * entry point.
149          */
150         frame = td->td_frame;
151         frame->tf_x[0] = td->td_tid;
152         frame->tf_x[1] = attr->argument;
153
154         /* Set up TLS. */
155         return (cpu_set_user_tls(td, (void *)tcb));
156 }
157
158 static struct sysentvec cloudabi64_elf_sysvec = {
159         .sv_size                = CLOUDABI64_SYS_MAXSYSCALL,
160         .sv_table               = cloudabi64_sysent,
161         .sv_fixup               = cloudabi64_fixup,
162         .sv_name                = "CloudABI ELF64",
163         .sv_coredump            = elf64_coredump,
164         .sv_pagesize            = PAGE_SIZE,
165         .sv_minuser             = VM_MIN_ADDRESS,
166         .sv_maxuser             = VM_MAXUSER_ADDRESS,
167         .sv_usrstack            = USRSTACK,
168         .sv_stackprot           = VM_PROT_READ | VM_PROT_WRITE,
169         .sv_copyout_strings     = cloudabi64_copyout_strings,
170         .sv_setregs             = cloudabi64_proc_setregs,
171         .sv_flags               = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_LP64,
172         .sv_set_syscall_retval  = cloudabi64_set_syscall_retval,
173         .sv_fetch_syscall_args  = cloudabi64_fetch_syscall_args,
174         .sv_syscallnames        = cloudabi64_syscallnames,
175         .sv_schedtail           = cloudabi64_schedtail,
176 };
177
178 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
179
180 Elf64_Brandinfo cloudabi64_brand = {
181         .brand          = ELFOSABI_CLOUDABI,
182         .machine        = EM_AARCH64,
183         .sysvec         = &cloudabi64_elf_sysvec,
184         .flags          = BI_CAN_EXEC_DYN,
185         .compat_3_brand = "CloudABI",
186 };