1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019 Joyent, Inc.
25  */
26 
27 #include <sys/errno.h>
28 #include <sys/exec.h>
29 #include <sys/kmem.h>
30 #include <sys/modctl.h>
31 #include <sys/model.h>
32 #include <sys/proc.h>
33 #include <sys/syscall.h>
34 #include <sys/systm.h>
35 #include <sys/thread.h>
36 #include <sys/cmn_err.h>
37 #include <sys/archsystm.h>
38 #include <sys/pathname.h>
39 
40 #include <sys/machbrand.h>
41 #include <sys/brand.h>
42 #include "sn1_brand.h"
43 
44 char *sn1_emulation_table = NULL;
45 
46 void	sn1_init_brand_data(zone_t *);
47 void	sn1_free_brand_data(zone_t *);
48 void	sn1_setbrand(proc_t *);
49 int	sn1_getattr(zone_t *, int, void *, size_t *);
50 int	sn1_setattr(zone_t *, int, void *, size_t);
51 int	sn1_brandsys(int, int64_t *, uintptr_t, uintptr_t, uintptr_t,
52 		uintptr_t, uintptr_t, uintptr_t);
53 void	sn1_copy_procdata(proc_t *, proc_t *);
54 void	sn1_proc_exit(struct proc *, klwp_t *);
55 void	sn1_exec();
56 int	sn1_initlwp(klwp_t *);
57 void	sn1_forklwp(klwp_t *, klwp_t *);
58 void	sn1_freelwp(klwp_t *);
59 void	sn1_lwpexit(klwp_t *);
60 int	sn1_elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
61 	size_t *, int, caddr_t, cred_t *, int);
62 
63 /* sn1 brand */
64 struct brand_ops sn1_brops = {
65 	sn1_init_brand_data,
66 	sn1_free_brand_data,
67 	sn1_brandsys,
68 	sn1_setbrand,
69 	sn1_getattr,
70 	sn1_setattr,
71 	sn1_copy_procdata,
72 	sn1_proc_exit,
73 	sn1_exec,
74 	lwp_setrval,
75 	sn1_initlwp,
76 	sn1_forklwp,
77 	sn1_freelwp,
78 	sn1_lwpexit,
79 	sn1_elfexec,
80 	NULL,
81 	NULL,
82 	NSIG,
83 };
84 
85 #ifdef	sparc
86 
87 struct brand_mach_ops sn1_mops = {
88 	sn1_brand_syscall_callback,
89 	sn1_brand_syscall32_callback
90 };
91 
92 #else	/* sparc */
93 
94 #ifdef	__amd64
95 
96 struct brand_mach_ops sn1_mops = {
97 	sn1_brand_sysenter_callback,
98 	sn1_brand_int91_callback,
99 	sn1_brand_syscall_callback,
100 	sn1_brand_syscall32_callback
101 };
102 
103 #else	/* ! __amd64 */
104 
105 struct brand_mach_ops sn1_mops = {
106 	sn1_brand_sysenter_callback,
107 	NULL,
108 	sn1_brand_syscall_callback,
109 	NULL
110 };
111 #endif	/* __amd64 */
112 
113 #endif	/* _sparc */
114 
115 struct brand	sn1_brand = {
116 	BRAND_VER_1,
117 	"sn1",
118 	&sn1_brops,
119 	&sn1_mops
120 };
121 
122 static struct modlbrand modlbrand = {
123 	&mod_brandops,		/* type of module */
124 	"Solaris N-1 Brand",	/* description of module */
125 	&sn1_brand		/* driver ops */
126 };
127 
128 static struct modlinkage modlinkage = {
129 	MODREV_1, (void *)&modlbrand, NULL
130 };
131 
132 void
sn1_setbrand(proc_t * p)133 sn1_setbrand(proc_t *p)
134 {
135 	brand_solaris_setbrand(p, &sn1_brand);
136 }
137 
138 /* ARGSUSED */
139 int
sn1_getattr(zone_t * zone,int attr,void * buf,size_t * bufsize)140 sn1_getattr(zone_t *zone, int attr, void *buf, size_t *bufsize)
141 {
142 	return (EINVAL);
143 }
144 
145 /* ARGSUSED */
146 int
sn1_setattr(zone_t * zone,int attr,void * buf,size_t bufsize)147 sn1_setattr(zone_t *zone, int attr, void *buf, size_t bufsize)
148 {
149 	return (EINVAL);
150 }
151 
152 /*ARGSUSED*/
153 int
sn1_brandsys(int cmd,int64_t * rval,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4,uintptr_t arg5,uintptr_t arg6)154 sn1_brandsys(int cmd, int64_t *rval, uintptr_t arg1, uintptr_t arg2,
155     uintptr_t arg3, uintptr_t arg4, uintptr_t arg5, uintptr_t arg6)
156 {
157 	int	res;
158 
159 	*rval = 0;
160 
161 	res = brand_solaris_cmd(cmd, arg1, arg2, arg3, &sn1_brand, SN1_VERSION);
162 	if (res >= 0)
163 		return (res);
164 
165 	return (EINVAL);
166 }
167 
168 void
sn1_copy_procdata(proc_t * child,proc_t * parent)169 sn1_copy_procdata(proc_t *child, proc_t *parent)
170 {
171 	brand_solaris_copy_procdata(child, parent, &sn1_brand);
172 }
173 
174 void
sn1_proc_exit(struct proc * p,klwp_t * l)175 sn1_proc_exit(struct proc *p, klwp_t *l)
176 {
177 	brand_solaris_proc_exit(p, l, &sn1_brand);
178 }
179 
180 void
sn1_exec()181 sn1_exec()
182 {
183 	brand_solaris_exec(&sn1_brand);
184 }
185 
186 int
sn1_initlwp(klwp_t * l)187 sn1_initlwp(klwp_t *l)
188 {
189 	return (brand_solaris_initlwp(l, &sn1_brand));
190 }
191 
192 void
sn1_forklwp(klwp_t * p,klwp_t * c)193 sn1_forklwp(klwp_t *p, klwp_t *c)
194 {
195 	brand_solaris_forklwp(p, c, &sn1_brand);
196 }
197 
198 void
sn1_freelwp(klwp_t * l)199 sn1_freelwp(klwp_t *l)
200 {
201 	brand_solaris_freelwp(l, &sn1_brand);
202 }
203 
204 void
sn1_lwpexit(klwp_t * l)205 sn1_lwpexit(klwp_t *l)
206 {
207 	brand_solaris_lwpexit(l, &sn1_brand);
208 }
209 
210 /*ARGSUSED*/
211 void
sn1_free_brand_data(zone_t * zone)212 sn1_free_brand_data(zone_t *zone)
213 {
214 }
215 
216 /*ARGSUSED*/
217 void
sn1_init_brand_data(zone_t * zone)218 sn1_init_brand_data(zone_t *zone)
219 {
220 }
221 
222 int
sn1_elfexec(vnode_t * vp,execa_t * uap,uarg_t * args,intpdata_t * idatap,int level,size_t * execsz,int setid,caddr_t exec_file,cred_t * cred,int brand_action)223 sn1_elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap,
224     int level, size_t *execsz, int setid, caddr_t exec_file, cred_t *cred,
225     int brand_action)
226 {
227 	return (brand_solaris_elfexec(vp, uap, args, idatap, level, execsz,
228 	    setid, exec_file, cred, brand_action, &sn1_brand, SN1_BRANDNAME,
229 	    SN1_LIB, SN1_LIB32, SN1_LINKER, SN1_LINKER32));
230 }
231 
232 int
_init(void)233 _init(void)
234 {
235 	int err;
236 
237 	/*
238 	 * Set up the table indicating which system calls we want to
239 	 * interpose on.  We should probably build this automatically from
240 	 * a list of system calls that is shared with the user-space
241 	 * library.
242 	 */
243 	sn1_emulation_table = kmem_zalloc(NSYSCALL, KM_SLEEP);
244 	sn1_emulation_table[SYS_read] = 1;			/*   3 */
245 	sn1_emulation_table[SYS_write] = 1;			/*   4 */
246 	sn1_emulation_table[SYS_time] = 1;			/*  13 */
247 	sn1_emulation_table[SYS_getpid] = 1;			/*  20 */
248 	sn1_emulation_table[SYS_mount] = 1;			/*  21 */
249 	sn1_emulation_table[SYS_getuid] = 1;			/*  24 */
250 	sn1_emulation_table[SYS_times] = 1;			/*  43 */
251 	sn1_emulation_table[SYS_getgid] = 1;			/*  47 */
252 	sn1_emulation_table[SYS_utssys] = 1;			/*  57 */
253 	sn1_emulation_table[SYS_waitid] = 1;			/* 107 */
254 	sn1_emulation_table[SYS_uname] = 1;			/* 135 */
255 
256 	err = mod_install(&modlinkage);
257 	if (err) {
258 		cmn_err(CE_WARN, "Couldn't install brand module");
259 		kmem_free(sn1_emulation_table, NSYSCALL);
260 	}
261 
262 	return (err);
263 }
264 
265 int
_info(struct modinfo * modinfop)266 _info(struct modinfo *modinfop)
267 {
268 	return (mod_info(&modlinkage, modinfop));
269 }
270 
271 int
_fini(void)272 _fini(void)
273 {
274 	return (brand_solaris_fini(&sn1_emulation_table, &modlinkage,
275 	    &sn1_brand));
276 }
277