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