]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/cloudabi64/cloudabi64_module.c
Add missing semicolon and properly wrap macro argument.
[FreeBSD/FreeBSD.git] / sys / compat / cloudabi64 / cloudabi64_module.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/module.h>
33 #include <sys/proc.h>
34 #include <sys/smp.h>
35 #include <sys/sysctl.h>
36 #include <sys/sysent.h>
37 #include <sys/systm.h>
38
39 #include <contrib/cloudabi/cloudabi64_types.h>
40
41 #include <compat/cloudabi64/cloudabi64_util.h>
42
43 register_t *
44 cloudabi64_copyout_strings(struct image_params *imgp)
45 {
46         struct image_args *args;
47         uintptr_t begin;
48         size_t len;
49
50         /* Copy out program arguments. */
51         args = imgp->args;
52         len = args->begin_envv - args->begin_argv;
53         begin = rounddown2(imgp->sysent->sv_usrstack - len, sizeof(register_t));
54         copyout(args->begin_argv, (void *)begin, len);
55         return ((register_t *)begin);
56 }
57
58 int
59 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
60 {
61         char canarybuf[64];
62         Elf64_Auxargs *args;
63         struct thread *td;
64         void *argdata, *canary;
65         size_t argdatalen;
66         int error;
67
68         /*
69          * CloudABI executables do not store the FreeBSD OS release
70          * number in their header. Set the OS release number to the
71          * latest version of FreeBSD, so that system calls behave as if
72          * called natively.
73          */
74         td = curthread;
75         td->td_proc->p_osrel = __FreeBSD_version;
76
77         /* Store canary for stack smashing protection. */
78         argdata = *stack_base;
79         arc4rand(canarybuf, sizeof(canarybuf), 0);
80         *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
81         canary = *stack_base;
82         error = copyout(canarybuf, canary, sizeof(canarybuf));
83         if (error != 0)
84                 return (error);
85
86         /*
87          * Compute length of program arguments. As the argument data is
88          * binary safe, we had to add a trailing null byte in
89          * exec_copyin_data_fds(). Undo this by reducing the length.
90          */
91         args = (Elf64_Auxargs *)imgp->auxargs;
92         argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
93         if (argdatalen > 0)
94                 --argdatalen;
95
96         /* Write out an auxiliary vector. */
97         cloudabi64_auxv_t auxv[] = {
98 #define VAL(type, val)  { .a_type = (type), .a_val = (val) }
99 #define PTR(type, ptr)  { .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
100                 PTR(CLOUDABI_AT_ARGDATA, argdata),
101                 VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
102                 VAL(CLOUDABI_AT_BASE, args->base),
103                 PTR(CLOUDABI_AT_CANARY, canary),
104                 VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
105                 VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
106                 VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
107                 PTR(CLOUDABI_AT_PHDR, args->phdr),
108                 VAL(CLOUDABI_AT_PHNUM, args->phnum),
109                 VAL(CLOUDABI_AT_TID, td->td_tid),
110 #undef VAL
111 #undef PTR
112                 { .a_type = CLOUDABI_AT_NULL },
113         };
114         *stack_base -= howmany(sizeof(auxv), sizeof(register_t));
115         error = copyout(auxv, *stack_base, sizeof(auxv));
116         if (error != 0)
117                 return (error);
118
119         /* Reserve space for storing the TCB. */
120         *stack_base -= howmany(sizeof(cloudabi64_tcb_t), sizeof(register_t));
121         return (0);
122 }
123
124 static int
125 cloudabi64_modevent(module_t mod, int type, void *data)
126 {
127
128         switch (type) {
129         case MOD_LOAD:
130                 if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
131                         printf("Failed to add CloudABI ELF brand handler\n");
132                         return (EINVAL);
133                 }
134                 return (0);
135         case MOD_UNLOAD:
136                 if (elf64_brand_inuse(&cloudabi64_brand))
137                         return (EBUSY);
138                 if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
139                         printf("Failed to remove CloudABI ELF brand handler\n");
140                         return (EINVAL);
141                 }
142                 return (0);
143         default:
144                 return (EOPNOTSUPP);
145         }
146 }
147
148 static moduledata_t cloudabi64_module = {
149         "cloudabi64",
150         cloudabi64_modevent,
151         NULL
152 };
153
154 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
155 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);
156 FEATURE(cloudabi64, "CloudABI 64bit support");