]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/cloudabi64/cloudabi64_sysvec.c
MFV r288408:
[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/exec.h>
31 #include <sys/imgact.h>
32 #include <sys/imgact_elf.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/proc.h>
36 #include <sys/smp.h>
37 #include <sys/sysctl.h>
38 #include <sys/sysent.h>
39 #include <sys/systm.h>
40
41 #include <vm/pmap.h>
42 #include <vm/vm.h>
43
44 #include <machine/frame.h>
45 #include <machine/pcb.h>
46 #include <machine/pmap.h>
47 #include <machine/psl.h>
48 #include <machine/vmparam.h>
49
50 #include <compat/cloudabi/cloudabi_util.h>
51
52 #include <compat/cloudabi64/cloudabi64_syscall.h>
53 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
54 #include <compat/cloudabi64/cloudabi64_util.h>
55
56 extern const char *cloudabi64_syscallnames[];
57 extern struct sysent cloudabi64_sysent[];
58
59 static register_t *
60 cloudabi64_copyout_strings(struct image_params *imgp)
61 {
62         uintptr_t begin;
63         size_t len;
64
65         /* Copy out program arguments. */
66         len = imgp->args->begin_envv - imgp->args->begin_argv;
67         begin = rounddown2(USRSTACK - len, sizeof(register_t));
68         copyout(imgp->args->begin_argv, (void *)begin, len);
69         return ((register_t *)begin);
70 }
71
72 static int
73 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
74 {
75         char canarybuf[64];
76         Elf64_Auxargs *args;
77         struct thread *td;
78         void *argdata, *canary;
79         size_t argdatalen;
80         int error;
81
82         /*
83          * CloudABI executables do not store the FreeBSD OS release
84          * number in their header. Set the OS release number to the
85          * latest version of FreeBSD, so that system calls behave as if
86          * called natively.
87          */
88         td = curthread;
89         td->td_proc->p_osrel = __FreeBSD_version;
90
91         /* Store canary for stack smashing protection. */
92         argdata = *stack_base;
93         arc4rand(canarybuf, sizeof(canarybuf), 0);
94         *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
95         canary = *stack_base;
96         error = copyout(canarybuf, canary, sizeof(canarybuf));
97         if (error != 0)
98                 return (error);
99
100         /*
101          * Compute length of program arguments. As the argument data is
102          * binary safe, we had to add a trailing null byte in
103          * exec_copyin_data_fds(). Undo this by reducing the length.
104          */
105         args = (Elf64_Auxargs *)imgp->auxargs;
106         argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
107         if (argdatalen > 0)
108                 --argdatalen;
109
110         /* Write out an auxiliary vector. */
111         cloudabi64_auxv_t auxv[] = {
112 #define VAL(type, val)  { .a_type = (type), .a_val = (val) }
113 #define PTR(type, ptr)  { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
114                 PTR(CLOUDABI_AT_ARGDATA, argdata),
115                 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
116                 PTR(CLOUDABI_AT_CANARY, canary),
117                 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
118                 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
119                 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
120                 PTR(CLOUDABI_AT_PHDR, args->phdr),
121                 VAL(CLOUDABI_AT_PHNUM, args->phnum),
122                 VAL(CLOUDABI_AT_TID, td->td_tid),
123 #undef VAL
124 #undef PTR
125                 { .a_type = CLOUDABI_AT_NULL },
126         };
127         *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
128         return (copyout(auxv, *stack_base, sizeof(auxv)));
129 }
130
131 static int
132 cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
133 {
134         struct trapframe *frame = td->td_frame;
135
136         /* Obtain system call number. */
137         sa->code = frame->tf_rax;
138         if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
139                 return (ENOSYS);
140         sa->callp = &cloudabi64_sysent[sa->code];
141
142         /* Fetch system call arguments. */
143         sa->args[0] = frame->tf_rdi;
144         sa->args[1] = frame->tf_rsi;
145         sa->args[2] = frame->tf_rdx;
146         sa->args[3] = frame->tf_rcx; /* Actually %r10. */
147         sa->args[4] = frame->tf_r8;
148         sa->args[5] = frame->tf_r9;
149
150         /* Default system call return values. */
151         td->td_retval[0] = 0;
152         td->td_retval[1] = frame->tf_rdx;
153         return (0);
154 }
155
156 static void
157 cloudabi64_set_syscall_retval(struct thread *td, int error)
158 {
159         struct trapframe *frame = td->td_frame;
160
161         switch (error) {
162         case 0:
163                 /* System call succeeded. */
164                 frame->tf_rax = td->td_retval[0];
165                 frame->tf_rdx = td->td_retval[1];
166                 frame->tf_rflags &= ~PSL_C;
167                 break;
168         case ERESTART:
169                 /* Restart system call. */
170                 frame->tf_rip -= frame->tf_err;
171                 frame->tf_r10 = frame->tf_rcx;
172                 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
173                 break;
174         case EJUSTRETURN:
175                 break;
176         default:
177                 /* System call returned an error. */
178                 frame->tf_rax = cloudabi_convert_errno(error);
179                 frame->tf_rflags |= PSL_C;
180                 break;
181         }
182 }
183
184 static void
185 cloudabi64_schedtail(struct thread *td)
186 {
187         struct trapframe *frame = td->td_frame;
188
189         /* Initial register values for processes returning from fork. */
190         frame->tf_rax = CLOUDABI_PROCESS_CHILD;
191         frame->tf_rdx = td->td_tid;
192 }
193
194 void
195 cloudabi64_thread_setregs(struct thread *td,
196     const cloudabi64_threadattr_t *attr)
197 {
198         struct trapframe *frame;
199         stack_t stack;
200
201         /* Perform standard register initialization. */
202         stack.ss_sp = (void *)attr->stack;
203         stack.ss_size = attr->stack_size;
204         cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
205
206         /*
207          * Pass in the thread ID of the new thread and the argument
208          * pointer provided by the parent thread in as arguments to the
209          * entry point.
210          */
211         frame = td->td_frame;
212         frame->tf_rdi = td->td_tid;
213         frame->tf_rsi = attr->argument;
214 }
215
216 static struct sysentvec cloudabi64_elf_sysvec = {
217         .sv_size                = CLOUDABI64_SYS_MAXSYSCALL,
218         .sv_table               = cloudabi64_sysent,
219         .sv_fixup               = cloudabi64_fixup,
220         .sv_name                = "CloudABI ELF64",
221         .sv_coredump            = elf64_coredump,
222         .sv_pagesize            = PAGE_SIZE,
223         .sv_minuser             = VM_MIN_ADDRESS,
224         .sv_maxuser             = VM_MAXUSER_ADDRESS,
225         .sv_usrstack            = USRSTACK,
226         .sv_stackprot           = VM_PROT_READ | VM_PROT_WRITE,
227         .sv_copyout_strings     = cloudabi64_copyout_strings,
228         .sv_flags               = SV_ABI_CLOUDABI | SV_CAPSICUM,
229         .sv_set_syscall_retval  = cloudabi64_set_syscall_retval,
230         .sv_fetch_syscall_args  = cloudabi64_fetch_syscall_args,
231         .sv_syscallnames        = cloudabi64_syscallnames,
232         .sv_schedtail           = cloudabi64_schedtail,
233 };
234
235 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
236
237 static Elf64_Brandinfo cloudabi64_brand = {
238         .brand          = ELFOSABI_CLOUDABI,
239         .machine        = EM_X86_64,
240         .sysvec         = &cloudabi64_elf_sysvec,
241         .compat_3_brand = "CloudABI",
242 };
243
244 static int
245 cloudabi64_modevent(module_t mod, int type, void *data)
246 {
247
248         switch (type) {
249         case MOD_LOAD:
250                 if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
251                         printf("Failed to add CloudABI ELF brand handler\n");
252                         return (EINVAL);
253                 }
254                 return (0);
255         case MOD_UNLOAD:
256                 if (elf64_brand_inuse(&cloudabi64_brand))
257                         return (EBUSY);
258                 if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
259                         printf("Failed to remove CloudABI ELF brand handler\n");
260                         return (EINVAL);
261                 }
262                 return (0);
263         default:
264                 return (EOPNOTSUPP);
265         }
266 }
267
268 static moduledata_t cloudabi64_module = {
269         "cloudabi64",
270         cloudabi64_modevent,
271         NULL
272 };
273
274 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
275 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);
276 FEATURE(cloudabi64, "CloudABI 64bit support");