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