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/cloudabi32_types.h>
40 
41 #include <compat/cloudabi/cloudabi_util.h>
42 
43 #include <compat/cloudabi32/cloudabi32_util.h>
44 
45 extern char _binary_cloudabi32_vdso_o_start[];
46 extern char _binary_cloudabi32_vdso_o_end[];
47 
48 register_t *
49 cloudabi32_copyout_strings(struct image_params *imgp)
50 {
51 	struct image_args *args;
52 	uintptr_t begin;
53 	size_t len;
54 
55 	/* Copy out program arguments. */
56 	args = imgp->args;
57 	len = args->begin_envv - args->begin_argv;
58 	begin = rounddown2(imgp->sysent->sv_usrstack - len, sizeof(register_t));
59 	copyout(args->begin_argv, (void *)begin, len);
60 	return ((register_t *)begin);
61 }
62 
63 int
64 cloudabi32_fixup(register_t **stack_base, struct image_params *imgp)
65 {
66 	char canarybuf[64];
67 	Elf32_Auxargs *args;
68 	struct thread *td;
69 	void *argdata, *canary;
70 	size_t argdatalen;
71 	int error;
72 
73 	/*
74 	 * CloudABI executables do not store the FreeBSD OS release
75 	 * number in their header. Set the OS release number to the
76 	 * latest version of FreeBSD, so that system calls behave as if
77 	 * called natively.
78 	 */
79 	td = curthread;
80 	td->td_proc->p_osrel = __FreeBSD_version;
81 
82 	/* Store canary for stack smashing protection. */
83 	argdata = *stack_base;
84 	arc4rand(canarybuf, sizeof(canarybuf), 0);
85 	*stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
86 	canary = *stack_base;
87 	error = copyout(canarybuf, canary, sizeof(canarybuf));
88 	if (error != 0)
89 		return (error);
90 
91 	/*
92 	 * Compute length of program arguments. As the argument data is
93 	 * binary safe, we had to add a trailing null byte in
94 	 * exec_copyin_data_fds(). Undo this by reducing the length.
95 	 */
96 	args = (Elf32_Auxargs *)imgp->auxargs;
97 	argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
98 	if (argdatalen > 0)
99 		--argdatalen;
100 
101 	/* Write out an auxiliary vector. */
102 	cloudabi32_auxv_t auxv[] = {
103 #define	VAL(type, val)	{ .a_type = (type), .a_val = (val) }
104 #define	PTR(type, ptr)	{ .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
105 		PTR(CLOUDABI_AT_ARGDATA, argdata),
106 		VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
107 		VAL(CLOUDABI_AT_BASE, args->base),
108 		PTR(CLOUDABI_AT_CANARY, canary),
109 		VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
110 		VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
111 		VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
112 		PTR(CLOUDABI_AT_PHDR, args->phdr),
113 		VAL(CLOUDABI_AT_PHNUM, args->phnum),
114 		VAL(CLOUDABI_AT_TID, td->td_tid),
115 		PTR(CLOUDABI_AT_SYSINFO_EHDR,
116 		    imgp->proc->p_sysent->sv_shared_page_base),
117 #undef VAL
118 #undef PTR
119 		{ .a_type = CLOUDABI_AT_NULL },
120 	};
121 	*stack_base -= howmany(sizeof(auxv), sizeof(register_t));
122 	error = copyout(auxv, *stack_base, sizeof(auxv));
123 	if (error != 0)
124 		return (error);
125 
126 	/* Reserve space for storing the TCB. */
127 	*stack_base -= howmany(sizeof(cloudabi32_tcb_t), sizeof(register_t));
128 	return (0);
129 }
130 
131 static int
132 cloudabi32_modevent(module_t mod, int type, void *data)
133 {
134 
135 	switch (type) {
136 	case MOD_LOAD:
137 		cloudabi_vdso_init(cloudabi32_brand.sysvec,
138 		    _binary_cloudabi32_vdso_o_start,
139 		    _binary_cloudabi32_vdso_o_end);
140 		if (elf32_insert_brand_entry(&cloudabi32_brand) < 0) {
141 			printf("Failed to add CloudABI ELF brand handler\n");
142 			return (EINVAL);
143 		}
144 		return (0);
145 	case MOD_UNLOAD:
146 		if (elf32_brand_inuse(&cloudabi32_brand))
147 			return (EBUSY);
148 		if (elf32_remove_brand_entry(&cloudabi32_brand) < 0) {
149 			printf("Failed to remove CloudABI ELF brand handler\n");
150 			return (EINVAL);
151 		}
152 		cloudabi_vdso_destroy(cloudabi32_brand.sysvec);
153 		return (0);
154 	default:
155 		return (EOPNOTSUPP);
156 	}
157 }
158 
159 static moduledata_t cloudabi32_module = {
160 	"cloudabi32",
161 	cloudabi32_modevent,
162 	NULL
163 };
164 
165 DECLARE_MODULE_TIED(cloudabi32, cloudabi32_module, SI_SUB_EXEC, SI_ORDER_ANY);
166 MODULE_DEPEND(cloudabi32, cloudabi, 1, 1, 1);
167 FEATURE(cloudabi32, "CloudABI 32bit support");
168