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) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
24  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25  */
26 
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <thread.h>
36 #include <sys/auxv.h>
37 #include <sys/brand.h>
38 #include <sys/inttypes.h>
39 #include <sys/lwp.h>
40 #include <sys/syscall.h>
41 #include <sys/systm.h>
42 #include <sys/utsname.h>
43 #include <sys/sysconfig.h>
44 #include <sys/systeminfo.h>
45 #include <sys/zone.h>
46 #include <sys/stat.h>
47 #include <sys/mntent.h>
48 #include <sys/ctfs.h>
49 #include <sys/priv.h>
50 #include <sys/acctctl.h>
51 #include <libgen.h>
52 #include <bsm/audit.h>
53 #include <sys/crypto/ioctl.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/ucontext.h>
57 #include <sys/mntio.h>
58 #include <sys/mnttab.h>
59 #include <sys/attr.h>
60 #include <sys/lofi.h>
61 #include <atomic.h>
62 #include <sys/acl.h>
63 #include <sys/socket.h>
64 
65 #include <s10_brand.h>
66 #include <brand_misc.h>
67 #include <s10_misc.h>
68 #include <s10_signal.h>
69 
70 /*
71  * See usr/src/lib/brand/shared/brand/common/brand_util.c for general
72  * emulation notes.
73  */
74 
75 static zoneid_t zoneid;
76 static boolean_t emul_global_zone = B_FALSE;
77 static s10_emul_bitmap_t emul_bitmap;
78 pid_t zone_init_pid;
79 
80 /*
81  * S10_FEATURE_IS_PRESENT is a macro that helps facilitate conditional
82  * emulation.  For each constant N defined in the s10_emulated_features
83  * enumeration in usr/src/uts/common/brand/solaris10/s10_brand.h,
84  * S10_FEATURE_IS_PRESENT(N) is true iff the feature/backport represented by N
85  * is present in the Solaris 10 image hosted within the zone.  In other words,
86  * S10_FEATURE_IS_PRESENT(N) is true iff the file /usr/lib/brand/solaris10/M,
87  * where M is the enum value of N, was present in the zone when the zone booted.
88  *
89  *
90  * *** Sample Usage
91  *
92  * Suppose that you need to backport a fix to Solaris 10 and there is
93  * emulation in place for the fix.  Suppose further that the emulation won't be
94  * needed if the fix is backported (i.e., if the fix is present in the hosted
95  * Solaris 10 environment, then the brand won't need the emulation).  Then if
96  * you add a constant named "S10_FEATURE_X" to the end of the
97  * s10_emulated_features enumeration that represents the backported fix and
98  * S10_FEATURE_X evaluates to four, then you should create a file named
99  * /usr/lib/brand/solaris10/4 as part of your backport.  Additionally, you
100  * should retain the aforementioned emulation but modify it so that it's
101  * performed only when S10_FEATURE_IS_PRESENT(S10_FEATURE_X) is false.  Thus the
102  * emulation function should look something like the following:
103  *
104  *	static int
105  *	my_emul_function(sysret_t *rv, ...)
106  *	{
107  *		if (S10_FEATURE_IS_PRESENT(S10_FEATURE_X)) {
108  *			// Don't emulate
109  *			return (__systemcall(rv, ...));
110  *		} else {
111  *			// Emulate whatever needs to be emulated when the
112  *			// backport isn't present in the Solaris 10 image.
113  *		}
114  *	}
115  */
116 #define	S10_FEATURE_IS_PRESENT(s10_emulated_features_constant)	\
117 	((emul_bitmap[(s10_emulated_features_constant) >> 3] &	\
118 	(1 << ((s10_emulated_features_constant) & 0x7))) != 0)
119 
120 brand_sysent_table_t brand_sysent_table[];
121 
122 #define	S10_UTS_RELEASE	"5.10"
123 #define	S10_UTS_VERSION	"Generic_Virtual"
124 
125 /*
126  * If the ioctl fd's major doesn't match "major", then pass through the
127  * ioctl, since it is not the expected device.  major should be a
128  * pointer to a static dev_t initialized to -1, and devname should be
129  * the path of the device.
130  *
131  * Returns 1 if the ioctl was handled (in which case *err contains the
132  * error code), or 0 if it still needs handling.
133  */
134 static int
135 passthru_otherdev_ioctl(dev_t *majordev, const char *devname, int *err,
136     sysret_t *rval, int fdes, int cmd, intptr_t arg)
137 {
138 	struct stat sbuf;
139 
140 	if (*majordev == (dev_t)-1) {
141 		if ((*err = __systemcall(rval, SYS_fstatat + 1024,
142 		    AT_FDCWD, devname, &sbuf, 0) != 0) != 0)
143 			goto doioctl;
144 
145 		*majordev = major(sbuf.st_rdev);
146 	}
147 
148 	if ((*err = __systemcall(rval, SYS_fstatat + 1024, fdes,
149 	    NULL, &sbuf, 0)) != 0)
150 		goto doioctl;
151 
152 	if (major(sbuf.st_rdev) == *majordev)
153 		return (0);
154 
155 doioctl:
156 	*err = (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
157 	return (1);
158 }
159 
160 /*
161  * Figures out the PID of init for the zone.  Also returns a boolean
162  * indicating whether this process currently has that pid: if so,
163  * then at this moment, we are init.
164  */
165 static boolean_t
166 get_initpid_info(void)
167 {
168 	pid_t pid;
169 	sysret_t rval;
170 	int err;
171 
172 	/*
173 	 * Determine the current process PID and the PID of the zone's init.
174 	 * We use care not to call getpid() here, because we're not supposed
175 	 * to call getpid() until after the program is fully linked-- the
176 	 * first call to getpid() is a signal from the linker to debuggers
177 	 * that linking has been completed.
178 	 */
179 	if ((err = __systemcall(&rval, SYS_brand,
180 	    B_S10_PIDINFO, &pid, &zone_init_pid)) != 0) {
181 		brand_abort(err, "Failed to get init's pid");
182 	}
183 
184 	/*
185 	 * Note that we need to be cautious with the pid we get back--
186 	 * it should not be stashed and used in place of getpid(), since
187 	 * we might fork(2).  So we keep zone_init_pid and toss the pid
188 	 * we otherwise got.
189 	 */
190 	if (pid == zone_init_pid)
191 		return (B_TRUE);
192 
193 	return (B_FALSE);
194 }
195 
196 /* Free the thread-local storage provided by mntfs_get_mntentbuf(). */
197 static void
198 mntfs_free_mntentbuf(void *arg)
199 {
200 	struct mntentbuf *embufp = arg;
201 
202 	if (embufp == NULL)
203 		return;
204 	if (embufp->mbuf_emp)
205 		free(embufp->mbuf_emp);
206 	if (embufp->mbuf_buf)
207 		free(embufp->mbuf_buf);
208 	bzero(embufp, sizeof (struct mntentbuf));
209 	free(embufp);
210 }
211 
212 /* Provide the thread-local storage required by mntfs_ioctl(). */
213 static struct mntentbuf *
214 mntfs_get_mntentbuf(size_t size)
215 {
216 	static mutex_t keylock;
217 	static thread_key_t key;
218 	static int once_per_keyname = 0;
219 	void *tsd = NULL;
220 	struct mntentbuf *embufp;
221 
222 	/* Create the key. */
223 	if (!once_per_keyname) {
224 		(void) mutex_lock(&keylock);
225 		if (!once_per_keyname) {
226 			if (thr_keycreate(&key, mntfs_free_mntentbuf)) {
227 				(void) mutex_unlock(&keylock);
228 				return (NULL);
229 			} else {
230 				once_per_keyname++;
231 			}
232 		}
233 		(void) mutex_unlock(&keylock);
234 	}
235 
236 	/*
237 	 * The thread-specific datum for this key is the address of a struct
238 	 * mntentbuf. If this is the first time here then we allocate the struct
239 	 * and its contents, and associate its address with the thread; if there
240 	 * are any problems then we abort.
241 	 */
242 	if (thr_getspecific(key, &tsd))
243 		return (NULL);
244 	if (tsd == NULL) {
245 		if (!(embufp = calloc(1, sizeof (struct mntentbuf))) ||
246 		    !(embufp->mbuf_emp = malloc(sizeof (struct extmnttab))) ||
247 		    thr_setspecific(key, embufp)) {
248 			mntfs_free_mntentbuf(embufp);
249 			return (NULL);
250 		}
251 	} else {
252 		embufp = tsd;
253 	}
254 
255 	/* Return the buffer, resizing it if necessary. */
256 	if (size > embufp->mbuf_bufsize) {
257 		if (embufp->mbuf_buf)
258 			free(embufp->mbuf_buf);
259 		if ((embufp->mbuf_buf = malloc(size)) == NULL) {
260 			embufp->mbuf_bufsize = 0;
261 			return (NULL);
262 		} else {
263 			embufp->mbuf_bufsize = size;
264 		}
265 	}
266 	return (embufp);
267 }
268 
269 /*
270  * The MNTIOC_GETMNTENT command in this release differs from that in early
271  * versions of Solaris 10.
272  *
273  * Previously, the command would copy a pointer to a struct extmnttab to an
274  * address provided as an argument. The pointer would be somewhere within a
275  * mapping already present within the user's address space. In addition, the
276  * text to which the struct's members pointed would also be within a
277  * pre-existing mapping. Now, the user is required to allocate memory for both
278  * the struct and the text buffer, and to pass the address of each within a
279  * struct mntentbuf. In order to conceal these details from a Solaris 10 client
280  * we allocate some thread-local storage in which to create the necessary data
281  * structures; this is static, thread-safe memory that will be cleaned up
282  * without the caller's intervention.
283  *
284  * MNTIOC_GETEXTMNTENT and MNTIOC_GETMNTANY are new in this release; they should
285  * not work for older clients.
286  */
287 int
288 mntfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
289 {
290 	int err;
291 	struct stat statbuf;
292 	struct mntentbuf *embufp;
293 	static size_t bufsize = MNT_LINE_MAX;
294 
295 	/* Do not emulate mntfs commands from up-to-date clients. */
296 	if (S10_FEATURE_IS_PRESENT(S10_FEATURE_ALTERED_MNTFS_IOCTL))
297 		return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
298 
299 	/* Do not emulate mntfs commands directed at other file systems. */
300 	if ((err = __systemcall(rval, SYS_fstatat + 1024,
301 	    fdes, NULL, &statbuf, 0)) != 0)
302 		return (err);
303 	if (strcmp(statbuf.st_fstype, MNTTYPE_MNTFS) != 0)
304 		return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
305 
306 	if (cmd == MNTIOC_GETEXTMNTENT || cmd == MNTIOC_GETMNTANY)
307 		return (EINVAL);
308 
309 	if ((embufp = mntfs_get_mntentbuf(bufsize)) == NULL)
310 		return (ENOMEM);
311 
312 	/*
313 	 * MNTIOC_GETEXTMNTENT advances the file pointer once it has
314 	 * successfully copied out the result to the address provided. We
315 	 * therefore need to check the user-supplied address now since the
316 	 * one we'll be providing is guaranteed to work.
317 	 */
318 	if (brand_uucopy(&embufp->mbuf_emp, (void *)arg, sizeof (void *)) != 0)
319 		return (EFAULT);
320 
321 	/*
322 	 * Keep retrying for as long as we fail for want of a large enough
323 	 * buffer.
324 	 */
325 	for (;;) {
326 		if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes,
327 		    MNTIOC_GETEXTMNTENT, embufp)) != 0)
328 			return (err);
329 
330 		if (rval->sys_rval1 == MNTFS_TOOLONG) {
331 			/* The buffer wasn't large enough. */
332 			(void) atomic_swap_ulong((unsigned long *)&bufsize,
333 			    2 * embufp->mbuf_bufsize);
334 			if ((embufp = mntfs_get_mntentbuf(bufsize)) == NULL)
335 				return (ENOMEM);
336 		} else {
337 			break;
338 		}
339 	}
340 
341 	if (brand_uucopy(&embufp->mbuf_emp, (void *)arg, sizeof (void *)) != 0)
342 		return (EFAULT);
343 
344 	return (0);
345 }
346 
347 /*
348  * Assign the structure member value from the s (source) structure to the
349  * d (dest) structure.
350  */
351 #define	struct_assign(d, s, val)	(((d).val) = ((s).val))
352 
353 /*
354  * The CRYPTO_GET_FUNCTION_LIST parameter structure crypto_function_list_t
355  * changed between S10 and Nevada, so we have to emulate the old S10
356  * crypto_function_list_t structure when interposing on the ioctl syscall.
357  */
358 typedef struct s10_crypto_function_list {
359 	boolean_t fl_digest_init;
360 	boolean_t fl_digest;
361 	boolean_t fl_digest_update;
362 	boolean_t fl_digest_key;
363 	boolean_t fl_digest_final;
364 
365 	boolean_t fl_encrypt_init;
366 	boolean_t fl_encrypt;
367 	boolean_t fl_encrypt_update;
368 	boolean_t fl_encrypt_final;
369 
370 	boolean_t fl_decrypt_init;
371 	boolean_t fl_decrypt;
372 	boolean_t fl_decrypt_update;
373 	boolean_t fl_decrypt_final;
374 
375 	boolean_t fl_mac_init;
376 	boolean_t fl_mac;
377 	boolean_t fl_mac_update;
378 	boolean_t fl_mac_final;
379 
380 	boolean_t fl_sign_init;
381 	boolean_t fl_sign;
382 	boolean_t fl_sign_update;
383 	boolean_t fl_sign_final;
384 	boolean_t fl_sign_recover_init;
385 	boolean_t fl_sign_recover;
386 
387 	boolean_t fl_verify_init;
388 	boolean_t fl_verify;
389 	boolean_t fl_verify_update;
390 	boolean_t fl_verify_final;
391 	boolean_t fl_verify_recover_init;
392 	boolean_t fl_verify_recover;
393 
394 	boolean_t fl_digest_encrypt_update;
395 	boolean_t fl_decrypt_digest_update;
396 	boolean_t fl_sign_encrypt_update;
397 	boolean_t fl_decrypt_verify_update;
398 
399 	boolean_t fl_seed_random;
400 	boolean_t fl_generate_random;
401 
402 	boolean_t fl_session_open;
403 	boolean_t fl_session_close;
404 	boolean_t fl_session_login;
405 	boolean_t fl_session_logout;
406 
407 	boolean_t fl_object_create;
408 	boolean_t fl_object_copy;
409 	boolean_t fl_object_destroy;
410 	boolean_t fl_object_get_size;
411 	boolean_t fl_object_get_attribute_value;
412 	boolean_t fl_object_set_attribute_value;
413 	boolean_t fl_object_find_init;
414 	boolean_t fl_object_find;
415 	boolean_t fl_object_find_final;
416 
417 	boolean_t fl_key_generate;
418 	boolean_t fl_key_generate_pair;
419 	boolean_t fl_key_wrap;
420 	boolean_t fl_key_unwrap;
421 	boolean_t fl_key_derive;
422 
423 	boolean_t fl_init_token;
424 	boolean_t fl_init_pin;
425 	boolean_t fl_set_pin;
426 
427 	boolean_t prov_is_hash_limited;
428 	uint32_t prov_hash_threshold;
429 	uint32_t prov_hash_limit;
430 } s10_crypto_function_list_t;
431 
432 typedef struct s10_crypto_get_function_list {
433 	uint_t				fl_return_value;
434 	crypto_provider_id_t		fl_provider_id;
435 	s10_crypto_function_list_t	fl_list;
436 } s10_crypto_get_function_list_t;
437 
438 /*
439  * The structure returned by the CRYPTO_GET_FUNCTION_LIST ioctl on /dev/crypto
440  * increased in size due to:
441  *	6482533 Threshold for HW offload via PKCS11 interface
442  * between S10 and Nevada.  This is a relatively simple process of filling
443  * in the S10 structure fields with the Nevada data.
444  *
445  * We stat the device to make sure that the ioctl is meant for /dev/crypto.
446  *
447  */
448 static int
449 crypto_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
450 {
451 	int				err;
452 	s10_crypto_get_function_list_t	s10_param;
453 	crypto_get_function_list_t	native_param;
454 	static dev_t			crypto_dev = (dev_t)-1;
455 
456 	if (passthru_otherdev_ioctl(&crypto_dev, "/dev/crypto", &err,
457 	    rval, fdes, cmd, arg) == 1)
458 		return (err);
459 
460 	if (brand_uucopy((const void *)arg, &s10_param, sizeof (s10_param))
461 	    != 0)
462 		return (EFAULT);
463 	struct_assign(native_param, s10_param, fl_provider_id);
464 	if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd,
465 	    &native_param)) != 0)
466 		return (err);
467 
468 	struct_assign(s10_param, native_param, fl_return_value);
469 	struct_assign(s10_param, native_param, fl_provider_id);
470 
471 	struct_assign(s10_param, native_param, fl_list.fl_digest_init);
472 	struct_assign(s10_param, native_param, fl_list.fl_digest);
473 	struct_assign(s10_param, native_param, fl_list.fl_digest_update);
474 	struct_assign(s10_param, native_param, fl_list.fl_digest_key);
475 	struct_assign(s10_param, native_param, fl_list.fl_digest_final);
476 
477 	struct_assign(s10_param, native_param, fl_list.fl_encrypt_init);
478 	struct_assign(s10_param, native_param, fl_list.fl_encrypt);
479 	struct_assign(s10_param, native_param, fl_list.fl_encrypt_update);
480 	struct_assign(s10_param, native_param, fl_list.fl_encrypt_final);
481 
482 	struct_assign(s10_param, native_param, fl_list.fl_decrypt_init);
483 	struct_assign(s10_param, native_param, fl_list.fl_decrypt);
484 	struct_assign(s10_param, native_param, fl_list.fl_decrypt_update);
485 	struct_assign(s10_param, native_param, fl_list.fl_decrypt_final);
486 
487 	struct_assign(s10_param, native_param, fl_list.fl_mac_init);
488 	struct_assign(s10_param, native_param, fl_list.fl_mac);
489 	struct_assign(s10_param, native_param, fl_list.fl_mac_update);
490 	struct_assign(s10_param, native_param, fl_list.fl_mac_final);
491 
492 	struct_assign(s10_param, native_param, fl_list.fl_sign_init);
493 	struct_assign(s10_param, native_param, fl_list.fl_sign);
494 	struct_assign(s10_param, native_param, fl_list.fl_sign_update);
495 	struct_assign(s10_param, native_param, fl_list.fl_sign_final);
496 	struct_assign(s10_param, native_param, fl_list.fl_sign_recover_init);
497 	struct_assign(s10_param, native_param, fl_list.fl_sign_recover);
498 
499 	struct_assign(s10_param, native_param, fl_list.fl_verify_init);
500 	struct_assign(s10_param, native_param, fl_list.fl_verify);
501 	struct_assign(s10_param, native_param, fl_list.fl_verify_update);
502 	struct_assign(s10_param, native_param, fl_list.fl_verify_final);
503 	struct_assign(s10_param, native_param, fl_list.fl_verify_recover_init);
504 	struct_assign(s10_param, native_param, fl_list.fl_verify_recover);
505 
506 	struct_assign(s10_param, native_param,
507 	    fl_list.fl_digest_encrypt_update);
508 	struct_assign(s10_param, native_param,
509 	    fl_list.fl_decrypt_digest_update);
510 	struct_assign(s10_param, native_param, fl_list.fl_sign_encrypt_update);
511 	struct_assign(s10_param, native_param,
512 	    fl_list.fl_decrypt_verify_update);
513 
514 	struct_assign(s10_param, native_param, fl_list.fl_seed_random);
515 	struct_assign(s10_param, native_param, fl_list.fl_generate_random);
516 
517 	struct_assign(s10_param, native_param, fl_list.fl_session_open);
518 	struct_assign(s10_param, native_param, fl_list.fl_session_close);
519 	struct_assign(s10_param, native_param, fl_list.fl_session_login);
520 	struct_assign(s10_param, native_param, fl_list.fl_session_logout);
521 
522 	struct_assign(s10_param, native_param, fl_list.fl_object_create);
523 	struct_assign(s10_param, native_param, fl_list.fl_object_copy);
524 	struct_assign(s10_param, native_param, fl_list.fl_object_destroy);
525 	struct_assign(s10_param, native_param, fl_list.fl_object_get_size);
526 	struct_assign(s10_param, native_param,
527 	    fl_list.fl_object_get_attribute_value);
528 	struct_assign(s10_param, native_param,
529 	    fl_list.fl_object_set_attribute_value);
530 	struct_assign(s10_param, native_param, fl_list.fl_object_find_init);
531 	struct_assign(s10_param, native_param, fl_list.fl_object_find);
532 	struct_assign(s10_param, native_param, fl_list.fl_object_find_final);
533 
534 	struct_assign(s10_param, native_param, fl_list.fl_key_generate);
535 	struct_assign(s10_param, native_param, fl_list.fl_key_generate_pair);
536 	struct_assign(s10_param, native_param, fl_list.fl_key_wrap);
537 	struct_assign(s10_param, native_param, fl_list.fl_key_unwrap);
538 	struct_assign(s10_param, native_param, fl_list.fl_key_derive);
539 
540 	struct_assign(s10_param, native_param, fl_list.fl_init_token);
541 	struct_assign(s10_param, native_param, fl_list.fl_init_pin);
542 	struct_assign(s10_param, native_param, fl_list.fl_set_pin);
543 
544 	struct_assign(s10_param, native_param, fl_list.prov_is_hash_limited);
545 	struct_assign(s10_param, native_param, fl_list.prov_hash_threshold);
546 	struct_assign(s10_param, native_param, fl_list.prov_hash_limit);
547 
548 	return (brand_uucopy(&s10_param, (void *)arg, sizeof (s10_param)));
549 }
550 
551 /*
552  * The process contract CT_TGET and CT_TSET parameter structure ct_param_t
553  * changed between S10 and Nevada, so we have to emulate the old S10
554  * ct_param_t structure when interposing on the ioctl syscall.
555  */
556 typedef struct s10_ct_param {
557 	uint32_t ctpm_id;
558 	uint32_t ctpm_pad;
559 	uint64_t ctpm_value;
560 } s10_ct_param_t;
561 
562 /*
563  * We have to emulate process contract ioctls for init(1M) because the
564  * ioctl parameter structure changed between S10 and Nevada.  This is
565  * a relatively simple process of filling Nevada structure fields,
566  * shuffling values, and initiating a native system call.
567  *
568  * For now, we'll assume that all consumers of CT_TGET and CT_TSET will
569  * need emulation.  We'll issue a stat to make sure that the ioctl
570  * is meant for the contract file system.
571  *
572  */
573 static int
574 ctfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
575 {
576 	int err;
577 	s10_ct_param_t s10param;
578 	ct_param_t param;
579 	struct stat statbuf;
580 
581 	if ((err = __systemcall(rval, SYS_fstatat + 1024,
582 	    fdes, NULL, &statbuf, 0)) != 0)
583 		return (err);
584 	if (strcmp(statbuf.st_fstype, MNTTYPE_CTFS) != 0)
585 		return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
586 
587 	if (brand_uucopy((const void *)arg, &s10param, sizeof (s10param)) != 0)
588 		return (EFAULT);
589 	param.ctpm_id = s10param.ctpm_id;
590 	param.ctpm_size = sizeof (uint64_t);
591 	param.ctpm_value = &s10param.ctpm_value;
592 	if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, &param))
593 	    != 0)
594 		return (err);
595 
596 	if (cmd == CT_TGET)
597 		return (brand_uucopy(&s10param, (void *)arg,
598 		    sizeof (s10param)));
599 
600 	return (0);
601 }
602 
603 /*
604  * ZFS ioctls have changed in each Solaris 10 (S10) release as well as in
605  * Solaris Next.  The brand wraps ZFS commands so that the native commands
606  * are used, but we want to be sure no command sneaks in that uses ZFS
607  * without our knowledge.  We'll abort the process if we see a ZFS ioctl.
608  */
609 static int
610 zfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
611 {
612 	static dev_t zfs_dev = (dev_t)-1;
613 	int err;
614 
615 	if (passthru_otherdev_ioctl(&zfs_dev, ZFS_DEV, &err,
616 	    rval, fdes, cmd, arg) == 1)
617 		return (err);
618 
619 	brand_abort(0, "ZFS ioctl!");
620 	/*NOTREACHED*/
621 	return (0);
622 }
623 
624 struct s10_lofi_ioctl {
625 	uint32_t li_minor;
626 	boolean_t li_force;
627 	char li_filename[MAXPATHLEN + 1];
628 };
629 
630 static int
631 lofi_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
632 {
633 	static dev_t lofi_dev = (dev_t)-1;
634 	struct s10_lofi_ioctl s10_param;
635 	struct lofi_ioctl native_param;
636 	int err;
637 
638 	if (passthru_otherdev_ioctl(&lofi_dev, "/dev/lofictl", &err,
639 	    rval, fdes, cmd, arg) == 1)
640 		return (err);
641 
642 	if (brand_uucopy((const void *)arg, &s10_param,
643 	    sizeof (s10_param)) != 0)
644 		return (EFAULT);
645 
646 	/*
647 	 * Somewhat weirdly, EIO is what the S10 lofi driver would
648 	 * return for unrecognised cmds.
649 	 */
650 	if (cmd >= LOFI_CHECK_COMPRESSED)
651 		return (EIO);
652 
653 	bzero(&native_param, sizeof (native_param));
654 
655 	struct_assign(native_param, s10_param, li_minor);
656 	struct_assign(native_param, s10_param, li_force);
657 
658 	/*
659 	 * Careful here, this has changed from [MAXPATHLEN + 1] to
660 	 * [MAXPATHLEN].
661 	 */
662 	bcopy(s10_param.li_filename, native_param.li_filename,
663 	    sizeof (native_param.li_filename));
664 	native_param.li_filename[MAXPATHLEN - 1] = '\0';
665 
666 	err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, &native_param);
667 
668 	struct_assign(s10_param, native_param, li_minor);
669 	/* li_force is input-only */
670 
671 	bcopy(native_param.li_filename, s10_param.li_filename,
672 	    sizeof (native_param.li_filename));
673 
674 	(void) brand_uucopy(&s10_param, (void *)arg, sizeof (s10_param));
675 	return (err);
676 }
677 
678 int
679 s10_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
680 {
681 	switch (cmd) {
682 	case CRYPTO_GET_FUNCTION_LIST:
683 		return (crypto_ioctl(rval, fdes, cmd, arg));
684 	case CT_TGET:
685 		/*FALLTHRU*/
686 	case CT_TSET:
687 		return (ctfs_ioctl(rval, fdes, cmd, arg));
688 	case MNTIOC_GETMNTENT:
689 		/*FALLTHRU*/
690 	case MNTIOC_GETEXTMNTENT:
691 		/*FALLTHRU*/
692 	case MNTIOC_GETMNTANY:
693 		return (mntfs_ioctl(rval, fdes, cmd, arg));
694 	}
695 
696 	switch (cmd & ~0xff) {
697 	case ZFS_IOC:
698 		return (zfs_ioctl(rval, fdes, cmd, arg));
699 
700 	case LOFI_IOC_BASE:
701 		return (lofi_ioctl(rval, fdes, cmd, arg));
702 
703 	default:
704 		break;
705 	}
706 
707 	return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
708 }
709 
710 /*
711  * Unfortunately, pwrite()'s behavior differs between S10 and Nevada when
712  * applied to files opened with O_APPEND.  The offset argument is ignored and
713  * the buffer is appended to the target file in S10, whereas the current file
714  * position is ignored in Nevada (i.e., pwrite() acts as though the target file
715  * wasn't opened with O_APPEND).  This is a result of the fix for CR 6655660
716  * (pwrite() must ignore the O_APPEND/FAPPEND flag).
717  *
718  * We emulate the old S10 pwrite() behavior by checking whether the target file
719  * was opened with O_APPEND.  If it was, then invoke the write() system call
720  * instead of pwrite(); otherwise, invoke the pwrite() system call as usual.
721  */
722 static int
723 s10_pwrite(sysret_t *rval, int fd, const void *bufferp, size_t num_bytes,
724     off_t offset)
725 {
726 	int err;
727 
728 	if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0)
729 		return (err);
730 	if (rval->sys_rval1 & O_APPEND)
731 		return (__systemcall(rval, SYS_write + 1024, fd, bufferp,
732 		    num_bytes));
733 	return (__systemcall(rval, SYS_pwrite + 1024, fd, bufferp, num_bytes,
734 	    offset));
735 }
736 
737 #if !defined(_LP64)
738 /*
739  * This is the large file version of the pwrite() system call for 32-bit
740  * processes.  This exists for the same reason that s10_pwrite() exists; see
741  * the comment above s10_pwrite().
742  */
743 static int
744 s10_pwrite64(sysret_t *rval, int fd, const void *bufferp, size32_t num_bytes,
745     uint32_t offset_1, uint32_t offset_2)
746 {
747 	int err;
748 
749 	if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0)
750 		return (err);
751 	if (rval->sys_rval1 & O_APPEND)
752 		return (__systemcall(rval, SYS_write + 1024, fd, bufferp,
753 		    num_bytes));
754 	return (__systemcall(rval, SYS_pwrite64 + 1024, fd, bufferp,
755 	    num_bytes, offset_1, offset_2));
756 }
757 #endif	/* !_LP64 */
758 
759 /*
760  * These are convenience macros that s10_getdents_common() uses.  Both treat
761  * their arguments, which should be character pointers, as dirent pointers or
762  * dirent64 pointers and yield their d_name and d_reclen fields.  These
763  * macros shouldn't be used outside of s10_getdents_common().
764  */
765 #define	dirent_name(charptr)	((charptr) + name_offset)
766 #define	dirent_reclen(charptr)	\
767 	(*(unsigned short *)(uintptr_t)((charptr) + reclen_offset))
768 
769 /*
770  * This function contains code that is common to both s10_getdents() and
771  * s10_getdents64().  See the comment above s10_getdents() for details.
772  *
773  * rval, fd, buf, and nbyte should be passed unmodified from s10_getdents()
774  * and s10_getdents64().  getdents_syscall_id should be either SYS_getdents
775  * or SYS_getdents64.  name_offset should be the the byte offset of
776  * the d_name field in the dirent structures passed to the kernel via the
777  * syscall represented by getdents_syscall_id.  reclen_offset should be
778  * the byte offset of the d_reclen field in the aforementioned dirent
779  * structures.
780  */
781 static int
782 s10_getdents_common(sysret_t *rval, int fd, char *buf, size_t nbyte,
783     int getdents_syscall_id, size_t name_offset, size_t reclen_offset)
784 {
785 	int err;
786 	size_t buf_size;
787 	char *local_buf;
788 	char *buf_current;
789 
790 	/*
791 	 * Use a special brand operation, B_S10_ISFDXATTRDIR, to determine
792 	 * whether the specified file descriptor refers to an extended file
793 	 * attribute directory.  If it doesn't, then SYS_getdents won't
794 	 * reveal extended file attributes, in which case we can simply
795 	 * hand the syscall to the native kernel.
796 	 */
797 	if ((err = __systemcall(rval, SYS_brand + 1024, B_S10_ISFDXATTRDIR,
798 	    fd)) != 0)
799 		return (err);
800 	if (rval->sys_rval1 == 0)
801 		return (__systemcall(rval, getdents_syscall_id + 1024, fd, buf,
802 		    nbyte));
803 
804 	/*
805 	 * The file descriptor refers to an extended file attributes directory.
806 	 * We need to create a dirent buffer that's as large as buf into which
807 	 * the native SYS_getdents will store the special extended file
808 	 * attribute directory's entries.  We can't dereference buf because
809 	 * it might be an invalid pointer!
810 	 */
811 	if (nbyte > MAXGETDENTS_SIZE)
812 		nbyte = MAXGETDENTS_SIZE;
813 	local_buf = (char *)malloc(nbyte);
814 	if (local_buf == NULL) {
815 		/*
816 		 * getdents(2) doesn't return an error code indicating a memory
817 		 * allocation error and it doesn't make sense to return any of
818 		 * its documented error codes for a malloc(3C) failure.  We'll
819 		 * use ENOMEM even though getdents(2) doesn't use it because it
820 		 * best describes the failure.
821 		 */
822 		(void) B_TRUSS_POINT_3(rval, getdents_syscall_id, ENOMEM, fd,
823 		    buf, nbyte);
824 		rval->sys_rval1 = -1;
825 		rval->sys_rval2 = 0;
826 		return (EIO);
827 	}
828 
829 	/*
830 	 * Issue a native SYS_getdents syscall but use our local dirent buffer
831 	 * instead of buf.  This will allow us to examine the returned dirent
832 	 * structures immediately and copy them to buf later.  That way the
833 	 * calling process won't be able to see the dirent structures until
834 	 * we finish examining them.
835 	 */
836 	if ((err = __systemcall(rval, getdents_syscall_id + 1024, fd, local_buf,
837 	    nbyte)) != 0) {
838 		free(local_buf);
839 		return (err);
840 	}
841 	buf_size = rval->sys_rval1;
842 	if (buf_size == 0) {
843 		free(local_buf);
844 		return (0);
845 	}
846 
847 	/*
848 	 * Look for SUNWattr_ro (VIEW_READONLY) and SUNWattr_rw
849 	 * (VIEW_READWRITE) in the directory entries and remove them
850 	 * from the dirent buffer.
851 	 */
852 	for (buf_current = local_buf;
853 	    (size_t)(buf_current - local_buf) < buf_size; /* cstyle */) {
854 		if (strcmp(dirent_name(buf_current), VIEW_READONLY) != 0 &&
855 		    strcmp(dirent_name(buf_current), VIEW_READWRITE) != 0) {
856 			/*
857 			 * The dirent refers to an attribute that should
858 			 * be visible to Solaris 10 processes.  Keep it
859 			 * and examine the next entry in the buffer.
860 			 */
861 			buf_current += dirent_reclen(buf_current);
862 		} else {
863 			/*
864 			 * We found either SUNWattr_ro (VIEW_READONLY)
865 			 * or SUNWattr_rw (VIEW_READWRITE).  Remove it
866 			 * from the dirent buffer by decrementing
867 			 * buf_size by the size of the entry and
868 			 * overwriting the entry with the remaining
869 			 * entries.
870 			 */
871 			buf_size -= dirent_reclen(buf_current);
872 			(void) memmove(buf_current, buf_current +
873 			    dirent_reclen(buf_current), buf_size -
874 			    (size_t)(buf_current - local_buf));
875 		}
876 	}
877 
878 	/*
879 	 * Copy local_buf into buf so that the calling process can see
880 	 * the results.
881 	 */
882 	if ((err = brand_uucopy(local_buf, buf, buf_size)) != 0) {
883 		free(local_buf);
884 		rval->sys_rval1 = -1;
885 		rval->sys_rval2 = 0;
886 		return (err);
887 	}
888 	rval->sys_rval1 = buf_size;
889 	free(local_buf);
890 	return (0);
891 }
892 
893 /*
894  * Solaris Next added two special extended file attributes, SUNWattr_ro and
895  * SUNWattr_rw, which are called "extended system attributes".  They have
896  * special semantics (e.g., a process cannot unlink SUNWattr_ro) and should
897  * not appear in solaris10-branded zones because no Solaris 10 applications,
898  * including system commands such as tar(1), are coded to correctly handle these
899  * special attributes.
900  *
901  * This emulation function solves the aforementioned problem by emulating
902  * the getdents(2) syscall and filtering both system attributes out of resulting
903  * directory entry lists.  The emulation function only filters results when
904  * the given file descriptor refers to an extended file attribute directory.
905  * Filtering getdents(2) results is expensive because it requires dynamic
906  * memory allocation; however, the performance cost is tolerable because
907  * we don't expect Solaris 10 processes to frequently examine extended file
908  * attribute directories.
909  *
910  * The brand's emulation library needs two getdents(2) emulation functions
911  * because getdents(2) comes in two flavors: non-largefile-aware getdents(2)
912  * and largefile-aware getdents64(2).  s10_getdents() handles the non-largefile-
913  * aware case for 32-bit processes and all getdents(2) syscalls for 64-bit
914  * processes (64-bit processes use largefile-aware interfaces by default).
915  * See s10_getdents64() below for the largefile-aware getdents64(2) emulation
916  * function for 32-bit processes.
917  */
918 static int
919 s10_getdents(sysret_t *rval, int fd, struct dirent *buf, size_t nbyte)
920 {
921 	return (s10_getdents_common(rval, fd, (char *)buf, nbyte, SYS_getdents,
922 	    offsetof(struct dirent, d_name),
923 	    offsetof(struct dirent, d_reclen)));
924 }
925 
926 #ifndef	_LP64
927 /*
928  * This is the largefile-aware version of getdents(2) for 32-bit processes.
929  * This exists for the same reason that s10_getdents() exists.  See the comment
930  * above s10_getdents().
931  */
932 static int
933 s10_getdents64(sysret_t *rval, int fd, struct dirent64 *buf, size_t nbyte)
934 {
935 	return (s10_getdents_common(rval, fd, (char *)buf, nbyte,
936 	    SYS_getdents64, offsetof(struct dirent64, d_name),
937 	    offsetof(struct dirent64, d_reclen)));
938 }
939 #endif	/* !_LP64 */
940 
941 #define	S10_TRIVIAL_ACL_CNT	6
942 #define	NATIVE_TRIVIAL_ACL_CNT	3
943 
944 /*
945  * Check if the ACL qualifies as a trivial ACL based on the native
946  * interpretation.
947  */
948 static boolean_t
949 has_trivial_native_acl(int cmd, int cnt, const char *fname, int fd)
950 {
951 	int i, err;
952 	sysret_t rval;
953 	ace_t buf[NATIVE_TRIVIAL_ACL_CNT];
954 
955 	if (fname != NULL)
956 		err = __systemcall(&rval, SYS_pathconf + 1024, fname,
957 		    _PC_ACL_ENABLED);
958 	else
959 		err = __systemcall(&rval, SYS_fpathconf + 1024, fd,
960 		    _PC_ACL_ENABLED);
961 	if (err != 0 || rval.sys_rval1 != _ACL_ACE_ENABLED)
962 		return (B_FALSE);
963 
964 	/*
965 	 * If we just got the ACL cnt, we don't need to get it again, its
966 	 * passed in as the cnt arg.
967 	 */
968 	if (cmd != ACE_GETACLCNT) {
969 		if (fname != NULL) {
970 			if (__systemcall(&rval, SYS_acl + 1024, fname,
971 			    ACE_GETACLCNT, 0, NULL) != 0)
972 				return (B_FALSE);
973 		} else {
974 			if (__systemcall(&rval, SYS_facl + 1024, fd,
975 			    ACE_GETACLCNT, 0, NULL) != 0)
976 				return (B_FALSE);
977 		}
978 		cnt = rval.sys_rval1;
979 	}
980 
981 	if (cnt != NATIVE_TRIVIAL_ACL_CNT)
982 		return (B_FALSE);
983 
984 	if (fname != NULL) {
985 		if (__systemcall(&rval, SYS_acl + 1024, fname, ACE_GETACL, cnt,
986 		    buf) != 0)
987 			return (B_FALSE);
988 	} else {
989 		if (__systemcall(&rval, SYS_facl + 1024, fd, ACE_GETACL, cnt,
990 		    buf) != 0)
991 			return (B_FALSE);
992 	}
993 
994 	/*
995 	 * The following is based on the logic from the native OS
996 	 * ace_trivial_common() to determine if the native ACL is trivial.
997 	 */
998 	for (i = 0; i < cnt; i++) {
999 		switch (buf[i].a_flags & ACE_TYPE_FLAGS) {
1000 		case ACE_OWNER:
1001 		case ACE_GROUP|ACE_IDENTIFIER_GROUP:
1002 		case ACE_EVERYONE:
1003 			break;
1004 		default:
1005 			return (B_FALSE);
1006 		}
1007 
1008 		if (buf[i].a_flags & (ACE_FILE_INHERIT_ACE|
1009 		    ACE_DIRECTORY_INHERIT_ACE|ACE_NO_PROPAGATE_INHERIT_ACE|
1010 		    ACE_INHERIT_ONLY_ACE))
1011 			return (B_FALSE);
1012 
1013 		/*
1014 		 * Special check for some special bits
1015 		 *
1016 		 * Don't allow anybody to deny reading basic
1017 		 * attributes or a files ACL.
1018 		 */
1019 		if (buf[i].a_access_mask & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&
1020 		    buf[i].a_type == ACE_ACCESS_DENIED_ACE_TYPE)
1021 			return (B_FALSE);
1022 
1023 		/*
1024 		 * Delete permissions are never set by default
1025 		 */
1026 		if (buf[i].a_access_mask & (ACE_DELETE|ACE_DELETE_CHILD))
1027 			return (B_FALSE);
1028 		/*
1029 		 * only allow owner@ to have
1030 		 * write_acl/write_owner/write_attributes/write_xattr/
1031 		 */
1032 		if (buf[i].a_type == ACE_ACCESS_ALLOWED_ACE_TYPE &&
1033 		    (!(buf[i].a_flags & ACE_OWNER) && (buf[i].a_access_mask &
1034 		    (ACE_WRITE_OWNER|ACE_WRITE_ACL| ACE_WRITE_ATTRIBUTES|
1035 		    ACE_WRITE_NAMED_ATTRS))))
1036 			return (B_FALSE);
1037 
1038 	}
1039 
1040 	return (B_TRUE);
1041 }
1042 
1043 /*
1044  * The following logic is based on the S10 adjust_ace_pair_common() code.
1045  */
1046 static void
1047 s10_adjust_ace_mask(void *pair, size_t access_off, size_t pairsize, mode_t mode)
1048 {
1049 	char *datap = (char *)pair;
1050 	uint32_t *amask0 = (uint32_t *)(uintptr_t)(datap + access_off);
1051 	uint32_t *amask1 = (uint32_t *)(uintptr_t)(datap + pairsize +
1052 	    access_off);
1053 
1054 	if (mode & S_IROTH)
1055 		*amask1 |= ACE_READ_DATA;
1056 	else
1057 		*amask0 |= ACE_READ_DATA;
1058 	if (mode & S_IWOTH)
1059 		*amask1 |= ACE_WRITE_DATA|ACE_APPEND_DATA;
1060 	else
1061 		*amask0 |= ACE_WRITE_DATA|ACE_APPEND_DATA;
1062 	if (mode & S_IXOTH)
1063 		*amask1 |= ACE_EXECUTE;
1064 	else
1065 		*amask0 |= ACE_EXECUTE;
1066 }
1067 
1068 /*
1069  * Construct a trivial S10 style ACL.
1070  */
1071 static int
1072 make_trivial_s10_acl(const char *fname, int fd, ace_t *bp)
1073 {
1074 	int err;
1075 	sysret_t rval;
1076 	struct stat64 buf;
1077 	ace_t trivial_s10_acl[] = {
1078 		{(uint_t)-1, 0, ACE_OWNER, ACE_ACCESS_DENIED_ACE_TYPE},
1079 		{(uint_t)-1, ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES|
1080 		    ACE_WRITE_NAMED_ATTRS, ACE_OWNER,
1081 		    ACE_ACCESS_ALLOWED_ACE_TYPE},
1082 		{(uint_t)-1, 0, ACE_GROUP|ACE_IDENTIFIER_GROUP,
1083 		    ACE_ACCESS_DENIED_ACE_TYPE},
1084 		{(uint_t)-1, 0, ACE_GROUP|ACE_IDENTIFIER_GROUP,
1085 		    ACE_ACCESS_ALLOWED_ACE_TYPE},
1086 		{(uint_t)-1, ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES|
1087 		    ACE_WRITE_NAMED_ATTRS, ACE_EVERYONE,
1088 		    ACE_ACCESS_DENIED_ACE_TYPE},
1089 		{(uint_t)-1, ACE_READ_ACL|ACE_READ_ATTRIBUTES|
1090 		    ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE, ACE_EVERYONE,
1091 		    ACE_ACCESS_ALLOWED_ACE_TYPE}
1092 	};
1093 
1094 	if (fname != NULL) {
1095 		if ((err = __systemcall(&rval, SYS_fstatat64 + 1024, AT_FDCWD,
1096 		    fname, &buf, 0)) != 0)
1097 			return (err);
1098 	} else {
1099 		if ((err = __systemcall(&rval, SYS_fstatat64 + 1024, fd,
1100 		    NULL, &buf, 0)) != 0)
1101 			return (err);
1102 	}
1103 
1104 	s10_adjust_ace_mask(&trivial_s10_acl[0], offsetof(ace_t, a_access_mask),
1105 	    sizeof (ace_t), (buf.st_mode & 0700) >> 6);
1106 	s10_adjust_ace_mask(&trivial_s10_acl[2], offsetof(ace_t, a_access_mask),
1107 	    sizeof (ace_t), (buf.st_mode & 0070) >> 3);
1108 	s10_adjust_ace_mask(&trivial_s10_acl[4], offsetof(ace_t, a_access_mask),
1109 	    sizeof (ace_t), buf.st_mode & 0007);
1110 
1111 	if (brand_uucopy(&trivial_s10_acl, bp, sizeof (trivial_s10_acl)) != 0)
1112 		return (EFAULT);
1113 
1114 	return (0);
1115 }
1116 
1117 /*
1118  * The definition of a trivial ace-style ACL (used by ZFS and NFSv4) has been
1119  * simplified since S10.  Instead of 6 entries on a trivial S10 ACE ACL we now
1120  * have 3 streamlined entries.  The new, simpler trivial style confuses S10
1121  * commands such as 'ls -v' or 'cp -p' which don't see the expected S10 trivial
1122  * ACL entries and thus assume that there is a complex ACL on the file.
1123  *
1124  * See: PSARC/2010/029 Improved ACL interoperability
1125  *
1126  * Note that the trival ACL detection code is implemented in acl_trival() in
1127  * lib/libsec/common/aclutils.c.  It always uses the acl() syscall (not the
1128  * facl syscall) to determine if an ACL is trivial.  However, we emulate both
1129  * acl() and facl() so that the two provide consistent results.
1130  *
1131  * We don't currently try to emulate setting of ACLs since the primary
1132  * consumer of this feature is SMB or NFSv4 servers, neither of which are
1133  * supported in solaris10-branded zones.  If ACLs are used they must be set on
1134  * files using the native OS interpretation.
1135  */
1136 int
1137 s10_acl(sysret_t *rval, const char *fname, int cmd, int nentries, void *aclbufp)
1138 {
1139 	int res;
1140 
1141 	res = __systemcall(rval, SYS_acl + 1024, fname, cmd, nentries, aclbufp);
1142 
1143 	switch (cmd) {
1144 	case ACE_GETACLCNT:
1145 		if (res == 0 && has_trivial_native_acl(ACE_GETACLCNT,
1146 		    rval->sys_rval1, fname, 0)) {
1147 			rval->sys_rval1 = S10_TRIVIAL_ACL_CNT;
1148 		}
1149 		break;
1150 	case ACE_GETACL:
1151 		if (res == 0 &&
1152 		    has_trivial_native_acl(ACE_GETACL, 0, fname, 0) &&
1153 		    nentries >= S10_TRIVIAL_ACL_CNT) {
1154 			res = make_trivial_s10_acl(fname, 0, aclbufp);
1155 			rval->sys_rval1 = S10_TRIVIAL_ACL_CNT;
1156 		}
1157 		break;
1158 	}
1159 
1160 	return (res);
1161 }
1162 
1163 int
1164 s10_facl(sysret_t *rval, int fdes, int cmd, int nentries, void *aclbufp)
1165 {
1166 	int res;
1167 
1168 	res = __systemcall(rval, SYS_facl + 1024, fdes, cmd, nentries, aclbufp);
1169 
1170 	switch (cmd) {
1171 	case ACE_GETACLCNT:
1172 		if (res == 0 && has_trivial_native_acl(ACE_GETACLCNT,
1173 		    rval->sys_rval1, NULL, fdes)) {
1174 			rval->sys_rval1 = S10_TRIVIAL_ACL_CNT;
1175 		}
1176 		break;
1177 	case ACE_GETACL:
1178 		if (res == 0 &&
1179 		    has_trivial_native_acl(ACE_GETACL, 0, NULL, fdes) &&
1180 		    nentries >= S10_TRIVIAL_ACL_CNT) {
1181 			res = make_trivial_s10_acl(NULL, fdes, aclbufp);
1182 			rval->sys_rval1 = S10_TRIVIAL_ACL_CNT;
1183 		}
1184 		break;
1185 	}
1186 
1187 	return (res);
1188 }
1189 
1190 #define	S10_AC_PROC		(0x1 << 28)
1191 #define	S10_AC_TASK		(0x2 << 28)
1192 #define	S10_AC_FLOW		(0x4 << 28)
1193 #define	S10_AC_MODE(x)		((x) & 0xf0000000)
1194 #define	S10_AC_OPTION(x)	((x) & 0x0fffffff)
1195 
1196 /*
1197  * The mode shift, mode mask and option mask for acctctl have changed.  The
1198  * mode is currently the top full byte and the option is the lower 3 full bytes.
1199  */
1200 int
1201 s10_acctctl(sysret_t *rval, int cmd, void *buf, size_t bufsz)
1202 {
1203 	int mode = S10_AC_MODE(cmd);
1204 	int option = S10_AC_OPTION(cmd);
1205 
1206 	switch (mode) {
1207 	case S10_AC_PROC:
1208 		mode = AC_PROC;
1209 		break;
1210 	case S10_AC_TASK:
1211 		mode = AC_TASK;
1212 		break;
1213 	case S10_AC_FLOW:
1214 		mode = AC_FLOW;
1215 		break;
1216 	default:
1217 		return (B_TRUSS_POINT_3(rval, SYS_acctctl, EINVAL, cmd, buf,
1218 		    bufsz));
1219 	}
1220 
1221 	return (__systemcall(rval, SYS_acctctl + 1024, mode | option, buf,
1222 	    bufsz));
1223 }
1224 
1225 /*
1226  * The Audit Policy parameters have changed due to:
1227  *    6466722 audituser and AUDIT_USER are defined, unused, undocumented and
1228  *            should be removed.
1229  *
1230  * In S10 we had the following flag:
1231  *	#define AUDIT_USER 0x0040
1232  * which doesn't exist in Solaris Next where the subsequent flags are shifted
1233  * down.  For example, in S10 we had:
1234  *	#define AUDIT_GROUP     0x0080
1235  * but on Solaris Next we have:
1236  *	#define AUDIT_GROUP     0x0040
1237  * AUDIT_GROUP has the value AUDIT_USER had in S10 and all of the subsequent
1238  * bits are also shifted one place.
1239  *
1240  * When we're getting or setting the Audit Policy parameters we need to
1241  * shift the outgoing or incoming bits into their proper positions.  Since
1242  * S10_AUDIT_USER was always unused, we always clear that bit on A_GETPOLICY.
1243  *
1244  * The command we care about, BSM_AUDITCTL, passes the most parameters (3),
1245  * so declare this function to take up to 4 args and just pass them on.
1246  * The number of parameters for s10_auditsys needs to be equal to the BSM_*
1247  * subcommand that has the most parameters, since we want to pass all
1248  * parameters through, regardless of which subcommands we interpose on.
1249  *
1250  * Note that the auditsys system call uses the SYSENT_AP macro wrapper instead
1251  * of the more common SYSENT_CI macro.  This means the return value is a
1252  * SE_64RVAL so the syscall table uses RV_64RVAL.
1253  */
1254 
1255 #define	S10_AUDIT_HMASK	0xffffffc0
1256 #define	S10_AUDIT_LMASK	0x3f
1257 #define	S10_AUC_NOSPACE	0x3
1258 
1259 int
1260 s10_auditsys(sysret_t *rval, int bsmcmd, intptr_t a0, intptr_t a1, intptr_t a2)
1261 {
1262 	int	    err;
1263 	uint32_t    m;
1264 
1265 	if (bsmcmd != BSM_AUDITCTL)
1266 		return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, a1,
1267 		    a2));
1268 
1269 	if ((int)a0 == A_GETPOLICY) {
1270 		if ((err = __systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0,
1271 		    &m, a2)) != 0)
1272 			return (err);
1273 		m = ((m & S10_AUDIT_HMASK) << 1) | (m & S10_AUDIT_LMASK);
1274 		if (brand_uucopy(&m, (void *)a1, sizeof (m)) != 0)
1275 			return (EFAULT);
1276 		return (0);
1277 
1278 	} else if ((int)a0 == A_SETPOLICY) {
1279 		if (brand_uucopy((const void *)a1, &m, sizeof (m)) != 0)
1280 			return (EFAULT);
1281 		m = ((m >> 1) & S10_AUDIT_HMASK) | (m & S10_AUDIT_LMASK);
1282 		return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, &m,
1283 		    a2));
1284 	} else if ((int)a0 == A_GETCOND) {
1285 		if ((err = __systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0,
1286 		    &m, a2)) != 0)
1287 			return (err);
1288 		if (m == AUC_NOSPACE)
1289 			m = S10_AUC_NOSPACE;
1290 		if (brand_uucopy(&m, (void *)a1, sizeof (m)) != 0)
1291 			return (EFAULT);
1292 		return (0);
1293 	} else if ((int)a0 == A_SETCOND) {
1294 		if (brand_uucopy((const void *)a1, &m, sizeof (m)) != 0)
1295 			return (EFAULT);
1296 		if (m == S10_AUC_NOSPACE)
1297 			m = AUC_NOSPACE;
1298 		return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, &m,
1299 		    a2));
1300 	}
1301 
1302 	return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, a1, a2));
1303 }
1304 
1305 /*
1306  * Determine whether the executable passed to SYS_exec or SYS_execve is a
1307  * native executable.  The s10_npreload.so invokes the B_S10_NATIVE brand
1308  * operation which patches up the processes exec info to eliminate any trace
1309  * of the wrapper.  That will make pgrep and other commands that examine
1310  * process' executable names and command-line parameters work properly.
1311  */
1312 static int
1313 s10_exec_native(sysret_t *rval, const char *fname, const char **argp,
1314     const char **envp)
1315 {
1316 	const char *filename = fname;
1317 	char path[64];
1318 	int err;
1319 
1320 	/* Get a copy of the executable we're trying to run */
1321 	path[0] = '\0';
1322 	(void) brand_uucopystr(filename, path, sizeof (path));
1323 
1324 	/* Check if we're trying to run a native binary */
1325 	if (strncmp(path, "/.SUNWnative/usr/lib/brand/solaris10/s10_native",
1326 	    sizeof (path)) != 0)
1327 		return (0);
1328 
1329 	/* Skip the first element in the argv array */
1330 	argp++;
1331 
1332 	/*
1333 	 * The the path of the dynamic linker is the second parameter
1334 	 * of s10_native_exec().
1335 	 */
1336 	if (brand_uucopy(argp, &filename, sizeof (char *)) != 0)
1337 		return (EFAULT);
1338 
1339 	/* If an exec call succeeds, it never returns */
1340 	err = __systemcall(rval, SYS_brand + 1024, B_EXEC_NATIVE, filename,
1341 	    argp, envp, NULL, NULL, NULL);
1342 	brand_assert(err != 0);
1343 	return (err);
1344 }
1345 
1346 /*
1347  * Interpose on the SYS_exec syscall to detect native wrappers.
1348  */
1349 int
1350 s10_exec(sysret_t *rval, const char *fname, const char **argp)
1351 {
1352 	int err;
1353 
1354 	if ((err = s10_exec_native(rval, fname, argp, NULL)) != 0)
1355 		return (err);
1356 
1357 	/* If an exec call succeeds, it never returns */
1358 	err = __systemcall(rval, SYS_execve + 1024, fname, argp, NULL);
1359 	brand_assert(err != 0);
1360 	return (err);
1361 }
1362 
1363 /*
1364  * Interpose on the SYS_execve syscall to detect native wrappers.
1365  */
1366 int
1367 s10_execve(sysret_t *rval, const char *fname, const char **argp,
1368     const char **envp)
1369 {
1370 	int err;
1371 
1372 	if ((err = s10_exec_native(rval, fname, argp, envp)) != 0)
1373 		return (err);
1374 
1375 	/* If an exec call succeeds, it never returns */
1376 	err = __systemcall(rval, SYS_execve + 1024, fname, argp, envp);
1377 	brand_assert(err != 0);
1378 	return (err);
1379 }
1380 
1381 /*
1382  * S10's issetugid() syscall is now a subcode to privsys().
1383  */
1384 static int
1385 s10_issetugid(sysret_t *rval)
1386 {
1387 	return (__systemcall(rval, SYS_privsys + 1024, PRIVSYS_ISSETUGID,
1388 	    0, 0, 0, 0, 0));
1389 }
1390 
1391 /*
1392  * S10's socket() syscall does not split type and flags
1393  */
1394 static int
1395 s10_so_socket(sysret_t *rval, int domain, int type, int protocol,
1396     char *devpath, int version)
1397 {
1398 	if ((type & ~SOCK_TYPE_MASK) != 0) {
1399 		errno = EINVAL;
1400 		return (-1);
1401 	}
1402 	return (__systemcall(rval, SYS_so_socket + 1024, domain, type,
1403 	    protocol, devpath, version));
1404 }
1405 
1406 /*
1407  * S10's pipe() syscall has a different calling convention
1408  */
1409 static int
1410 s10_pipe(sysret_t *rval)
1411 {
1412 	int fds[2], err;
1413 	if ((err = __systemcall(rval, SYS_pipe + 1024, fds, 0)) != 0)
1414 		return (err);
1415 
1416 	rval->sys_rval1 = fds[0];
1417 	rval->sys_rval2 = fds[1];
1418 	return (0);
1419 }
1420 
1421 /*
1422  * S10's accept() syscall takes three arguments
1423  */
1424 static int
1425 s10_accept(sysret_t *rval, int sock, struct sockaddr *addr, uint_t *addrlen,
1426     int version)
1427 {
1428 	return (__systemcall(rval, SYS_accept + 1024, sock, addr, addrlen,
1429 	    version, 0));
1430 }
1431 
1432 static long
1433 s10_uname(sysret_t *rv, uintptr_t p1)
1434 {
1435 	struct utsname un, *unp = (struct utsname *)p1;
1436 	int rev, err;
1437 
1438 	if ((err = __systemcall(rv, SYS_uname + 1024, &un)) != 0)
1439 		return (err);
1440 
1441 	rev = atoi(&un.release[2]);
1442 	brand_assert(rev >= 11);
1443 	bzero(un.release, _SYS_NMLN);
1444 	(void) strlcpy(un.release, S10_UTS_RELEASE, _SYS_NMLN);
1445 	bzero(un.version, _SYS_NMLN);
1446 	(void) strlcpy(un.version, S10_UTS_VERSION, _SYS_NMLN);
1447 
1448 	/* copy out the modified uname info */
1449 	return (brand_uucopy(&un, unp, sizeof (un)));
1450 }
1451 
1452 int
1453 s10_sysconfig(sysret_t *rv, int which)
1454 {
1455 	long value;
1456 
1457 	/*
1458 	 * We must interpose on the sysconfig(2) requests
1459 	 * that deal with the realtime signal number range.
1460 	 * All others get passed to the native sysconfig(2).
1461 	 */
1462 	switch (which) {
1463 	case _CONFIG_RTSIG_MAX:
1464 		value = S10_SIGRTMAX - S10_SIGRTMIN + 1;
1465 		break;
1466 	case _CONFIG_SIGRT_MIN:
1467 		value = S10_SIGRTMIN;
1468 		break;
1469 	case _CONFIG_SIGRT_MAX:
1470 		value = S10_SIGRTMAX;
1471 		break;
1472 	default:
1473 		return (__systemcall(rv, SYS_sysconfig + 1024, which));
1474 	}
1475 
1476 	(void) B_TRUSS_POINT_1(rv, SYS_sysconfig, 0, which);
1477 	rv->sys_rval1 = value;
1478 	rv->sys_rval2 = 0;
1479 
1480 	return (0);
1481 }
1482 
1483 int
1484 s10_sysinfo(sysret_t *rv, int command, char *buf, long count)
1485 {
1486 	char *value;
1487 	int len;
1488 
1489 	/*
1490 	 * We must interpose on the sysinfo(2) commands SI_RELEASE and
1491 	 * SI_VERSION; all others get passed to the native sysinfo(2)
1492 	 * command.
1493 	 */
1494 	switch (command) {
1495 		case SI_RELEASE:
1496 			value = S10_UTS_RELEASE;
1497 			break;
1498 
1499 		case SI_VERSION:
1500 			value = S10_UTS_VERSION;
1501 			break;
1502 
1503 		default:
1504 			/*
1505 			 * The default action is to pass the command to the
1506 			 * native sysinfo(2) syscall.
1507 			 */
1508 			return (__systemcall(rv, SYS_systeminfo + 1024,
1509 			    command, buf, count));
1510 	}
1511 
1512 	len = strlen(value) + 1;
1513 	if (count > 0) {
1514 		if (brand_uucopystr(value, buf, count) != 0)
1515 			return (EFAULT);
1516 
1517 		/*
1518 		 * Assure NULL termination of buf as brand_uucopystr() doesn't.
1519 		 */
1520 		if (len > count && brand_uucopy("\0", buf + (count - 1), 1)
1521 		    != 0)
1522 			return (EFAULT);
1523 	}
1524 
1525 	/*
1526 	 * On success, sysinfo(2) returns the size of buffer required to hold
1527 	 * the complete value plus its terminating NULL byte.
1528 	 */
1529 	(void) B_TRUSS_POINT_3(rv, SYS_systeminfo, 0, command, buf, count);
1530 	rv->sys_rval1 = len;
1531 	rv->sys_rval2 = 0;
1532 	return (0);
1533 }
1534 
1535 #if defined(__x86)
1536 #if defined(__amd64)
1537 /*
1538  * 64-bit x86 LWPs created by SYS_lwp_create start here if they need to set
1539  * their %fs registers to the legacy Solaris 10 selector value.
1540  *
1541  * This function does three things:
1542  *
1543  *	1.  Trap to the kernel so that it can set %fs to the legacy Solaris 10
1544  *	    selector value.
1545  *	2.  Read the LWP's true entry point (the entry point supplied by libc
1546  *	    when SYS_lwp_create was invoked) from %r14.
1547  *	3.  Eliminate this function's stack frame and pass control to the LWP's
1548  *	    true entry point.
1549  *
1550  * See the comment above s10_lwp_create_correct_fs() (see below) for the reason
1551  * why this function exists.
1552  */
1553 /*ARGSUSED*/
1554 static void
1555 s10_lwp_create_entry_point(void *ulwp_structp)
1556 {
1557 	sysret_t rval;
1558 
1559 	/*
1560 	 * The new LWP's %fs register is initially zero, but libc won't
1561 	 * function correctly when %fs is zero.  Change the LWP's %fs register
1562 	 * via SYS_brand.
1563 	 */
1564 	(void) __systemcall(&rval, SYS_brand + 1024, B_S10_FSREGCORRECTION);
1565 
1566 	/*
1567 	 * Jump to the true entry point, which is stored in %r14.
1568 	 * Remove our stack frame before jumping so that
1569 	 * s10_lwp_create_entry_point() won't be seen in stack traces.
1570 	 *
1571 	 * NOTE: s10_lwp_create_entry_point() pushes %r12 onto its stack frame
1572 	 * so that it can use it as a temporary register.  We don't restore %r12
1573 	 * in this assembly block because we don't care about its value (and
1574 	 * neither does _lwp_start()).  Besides, the System V ABI AMD64
1575 	 * Actirecture Processor Supplement doesn't specify that %r12 should
1576 	 * have a special value when LWPs start, so we can ignore its value when
1577 	 * we jump to the true entry point.  Furthermore, %r12 is a callee-saved
1578 	 * register, so the true entry point should push %r12 onto its stack
1579 	 * before using the register.  We ignore %r14 after we read it for
1580 	 * similar reasons.
1581 	 *
1582 	 * NOTE: The compiler will generate a function epilogue for this
1583 	 * function despite the fact that the LWP will never execute it.
1584 	 * We could hand-code this entire function in assembly to eliminate
1585 	 * the epilogue, but the epilogue is only three or four instructions,
1586 	 * so we wouldn't save much space.  Besides, why would we want
1587 	 * to create yet another ugly, hard-to-maintain assembly function when
1588 	 * we could write most of it in C?
1589 	 */
1590 	__asm__ __volatile__(
1591 	    "movq %0, %%rdi\n\t"	/* pass ulwp_structp as arg1 */
1592 	    "movq %%rbp, %%rsp\n\t"	/* eliminate the stack frame */
1593 	    "popq %%rbp\n\t"
1594 	    "jmp *%%r14\n\t"		/* jump to the true entry point */
1595 	    : : "r" (ulwp_structp));
1596 	/*NOTREACHED*/
1597 }
1598 
1599 /*
1600  * The S10 libc expects that %fs will be nonzero for new 64-bit x86 LWPs but the
1601  * Nevada kernel clears %fs for such LWPs.  Unforunately, new LWPs do not issue
1602  * SYS_lwp_private (see s10_lwp_private() below) after they are created, so
1603  * we must ensure that new LWPs invoke a brand operation that sets %fs to a
1604  * nonzero value immediately after their creation.
1605  *
1606  * The easiest way to do this is to make new LWPs start at a special function,
1607  * s10_lwp_create_entry_point() (see its definition above), that invokes the
1608  * brand operation that corrects %fs.  We'll store the entry points of new LWPs
1609  * in their %r14 registers so that s10_lwp_create_entry_point() can find and
1610  * call them after invoking the special brand operation.  %r14 is a callee-saved
1611  * register; therefore, any functions invoked by s10_lwp_create_entry_point()
1612  * and all functions dealing with signals (e.g., sigacthandler()) will preserve
1613  * %r14 for s10_lwp_create_entry_point().
1614  *
1615  * The Nevada kernel can safely work with nonzero %fs values because the kernel
1616  * configures per-thread %fs segment descriptors so that the legacy %fs selector
1617  * value will still work.  See the comment in lwp_load() regarding %fs and
1618  * %fsbase in 64-bit x86 processes.
1619  *
1620  * This emulation exists thanks to CRs 6467491 and 6501650.
1621  */
1622 static int
1623 s10_lwp_create_correct_fs(sysret_t *rval, ucontext_t *ucp, int flags,
1624     id_t *new_lwp)
1625 {
1626 	ucontext_t s10_uc;
1627 
1628 	/*
1629 	 * Copy the supplied ucontext_t structure to the local stack
1630 	 * frame and store the new LWP's entry point (the value of %rip
1631 	 * stored in the ucontext_t) in the new LWP's %r14 register.
1632 	 * Then make s10_lwp_create_entry_point() the new LWP's entry
1633 	 * point.
1634 	 */
1635 	if (brand_uucopy(ucp, &s10_uc, sizeof (s10_uc)) != 0)
1636 		return (EFAULT);
1637 
1638 	s10_uc.uc_mcontext.gregs[REG_R14] = s10_uc.uc_mcontext.gregs[REG_RIP];
1639 	s10_uc.uc_mcontext.gregs[REG_RIP] = (greg_t)s10_lwp_create_entry_point;
1640 
1641 	/*  fix up the signal mask */
1642 	if (s10_uc.uc_flags & UC_SIGMASK)
1643 		(void) s10sigset_to_native(&s10_uc.uc_sigmask,
1644 		    &s10_uc.uc_sigmask);
1645 
1646 	/*
1647 	 * Issue SYS_lwp_create to create the new LWP.  We pass the
1648 	 * modified ucontext_t to make sure that the new LWP starts at
1649 	 * s10_lwp_create_entry_point().
1650 	 */
1651 	return (__systemcall(rval, SYS_lwp_create + 1024, &s10_uc,
1652 	    flags, new_lwp));
1653 }
1654 #endif	/* __amd64 */
1655 
1656 /*
1657  * SYS_lwp_private is issued by libc_init() to set %fsbase in 64-bit x86
1658  * processes.  The Nevada kernel sets %fs to zero but the S10 libc expects
1659  * %fs to be nonzero.  We'll pass the issued system call to the kernel untouched
1660  * and invoke a brand operation to set %fs to the legacy S10 selector value.
1661  *
1662  * This emulation exists thanks to CRs 6467491 and 6501650.
1663  */
1664 static int
1665 s10_lwp_private(sysret_t *rval, int cmd, int which, uintptr_t base)
1666 {
1667 #if defined(__amd64)
1668 	int err;
1669 
1670 	/*
1671 	 * The current LWP's %fs register should be zero.  Determine whether the
1672 	 * Solaris 10 libc with which we're working functions correctly when %fs
1673 	 * is zero by calling thr_main() after issuing the SYS_lwp_private
1674 	 * syscall.  If thr_main() barfs (returns -1), then change the LWP's %fs
1675 	 * register via SYS_brand and patch brand_sysent_table so that issuing
1676 	 * SYS_lwp_create executes s10_lwp_create_correct_fs() rather than the
1677 	 * default s10_lwp_create().  s10_lwp_create_correct_fs() will
1678 	 * guarantee that new LWPs will have correct %fs values.
1679 	 */
1680 	if ((err = __systemcall(rval, SYS_lwp_private + 1024, cmd, which,
1681 	    base)) != 0)
1682 		return (err);
1683 	if (thr_main() == -1) {
1684 		/*
1685 		 * SYS_lwp_private is only issued by libc_init(), which is
1686 		 * executed when libc is first loaded by ld.so.1.  Thus we
1687 		 * are guaranteed to be single-threaded at this point.  Even
1688 		 * if we were multithreaded at this point, writing a 64-bit
1689 		 * value to the st_callc field of a brand_sysent_table
1690 		 * entry is guaranteed to be atomic on 64-bit x86 chips
1691 		 * as long as the field is not split across cache lines
1692 		 * (It shouldn't be.).  See chapter 8, section 1.1 of
1693 		 * "The Intel 64 and IA32 Architectures Software Developer's
1694 		 * Manual," Volume 3A for more details.
1695 		 */
1696 		brand_sysent_table[SYS_lwp_create].st_callc =
1697 		    (sysent_cb_t)s10_lwp_create_correct_fs;
1698 		return (__systemcall(rval, SYS_brand + 1024,
1699 		    B_S10_FSREGCORRECTION));
1700 	}
1701 	return (0);
1702 #else	/* !__amd64 */
1703 	return (__systemcall(rval, SYS_lwp_private + 1024, cmd, which, base));
1704 #endif	/* !__amd64 */
1705 }
1706 #endif	/* __x86 */
1707 
1708 /*
1709  * The Opensolaris versions of lwp_mutex_timedlock() and lwp_mutex_trylock()
1710  * add an extra argument to the interfaces, a uintptr_t value for the mutex's
1711  * mutex_owner field.  The Solaris 10 libc assigns the mutex_owner field at
1712  * user-level, so we just make the extra argument be zero in both syscalls.
1713  */
1714 
1715 static int
1716 s10_lwp_mutex_timedlock(sysret_t *rval, lwp_mutex_t *lp, timespec_t *tsp)
1717 {
1718 	return (__systemcall(rval, SYS_lwp_mutex_timedlock + 1024, lp, tsp, 0));
1719 }
1720 
1721 static int
1722 s10_lwp_mutex_trylock(sysret_t *rval, lwp_mutex_t *lp)
1723 {
1724 	return (__systemcall(rval, SYS_lwp_mutex_trylock + 1024, lp, 0));
1725 }
1726 
1727 /*
1728  * If the emul_global_zone flag is set then emulate some aspects of the
1729  * zone system call.  In particular, emulate the global zone ID on the
1730  * ZONE_LOOKUP subcommand and emulate some of the global zone attributes
1731  * on the ZONE_GETATTR subcommand.  If the flag is not set or we're performing
1732  * some other operation, simply pass the calls through.
1733  */
1734 int
1735 s10_zone(sysret_t *rval, int cmd, void *arg1, void *arg2, void *arg3,
1736     void *arg4)
1737 {
1738 	char		*aval;
1739 	int		len;
1740 	zoneid_t	zid;
1741 	int		attr;
1742 	char		*buf;
1743 	size_t		bufsize;
1744 
1745 	/*
1746 	 * We only emulate the zone syscall for a subset of specific commands,
1747 	 * otherwise we just pass the call through.
1748 	 */
1749 	if (!emul_global_zone)
1750 		return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2,
1751 		    arg3, arg4));
1752 
1753 	switch (cmd) {
1754 	case ZONE_LOOKUP:
1755 		(void) B_TRUSS_POINT_1(rval, SYS_zone, 0, cmd);
1756 		rval->sys_rval1 = GLOBAL_ZONEID;
1757 		rval->sys_rval2 = 0;
1758 		return (0);
1759 
1760 	case ZONE_GETATTR:
1761 		zid = (zoneid_t)(uintptr_t)arg1;
1762 		attr = (int)(uintptr_t)arg2;
1763 		buf = (char *)arg3;
1764 		bufsize = (size_t)arg4;
1765 
1766 		/*
1767 		 * If the request is for the global zone then we're emulating
1768 		 * that, otherwise pass this thru.
1769 		 */
1770 		if (zid != GLOBAL_ZONEID)
1771 			goto passthru;
1772 
1773 		switch (attr) {
1774 		case ZONE_ATTR_NAME:
1775 			aval = GLOBAL_ZONENAME;
1776 			break;
1777 
1778 		case ZONE_ATTR_BRAND:
1779 			aval = NATIVE_BRAND_NAME;
1780 			break;
1781 		default:
1782 			/*
1783 			 * We only emulate a subset of the attrs, use the
1784 			 * real zone id to pass thru the rest.
1785 			 */
1786 			arg1 = (void *)(uintptr_t)zoneid;
1787 			goto passthru;
1788 		}
1789 
1790 		(void) B_TRUSS_POINT_5(rval, SYS_zone, 0, cmd, zid, attr,
1791 		    buf, bufsize);
1792 
1793 		len = strlen(aval) + 1;
1794 		if (len > bufsize)
1795 			return (ENAMETOOLONG);
1796 
1797 		if (buf != NULL) {
1798 			if (len == 1) {
1799 				if (brand_uucopy("\0", buf, 1) != 0)
1800 					return (EFAULT);
1801 			} else {
1802 				if (brand_uucopystr(aval, buf, len) != 0)
1803 					return (EFAULT);
1804 
1805 				/*
1806 				 * Assure NULL termination of "buf" as
1807 				 * brand_uucopystr() does NOT.
1808 				 */
1809 				if (brand_uucopy("\0", buf + (len - 1), 1) != 0)
1810 					return (EFAULT);
1811 			}
1812 		}
1813 
1814 		rval->sys_rval1 = len;
1815 		rval->sys_rval2 = 0;
1816 		return (0);
1817 
1818 	default:
1819 		break;
1820 	}
1821 
1822 passthru:
1823 	return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2, arg3,
1824 	    arg4));
1825 }
1826 
1827 /*ARGSUSED*/
1828 int
1829 brand_init(int argc, char *argv[], char *envp[])
1830 {
1831 	sysret_t		rval;
1832 	ulong_t			ldentry;
1833 	int			err;
1834 	char			*bname;
1835 
1836 	brand_pre_init();
1837 
1838 	/*
1839 	 * Cache the pid of the zone's init process and determine if
1840 	 * we're init(1m) for the zone.  Remember: we might be init
1841 	 * now, but as soon as we fork(2) we won't be.
1842 	 */
1843 	(void) get_initpid_info();
1844 
1845 	/* get the current zoneid */
1846 	err = __systemcall(&rval, SYS_zone, ZONE_LOOKUP, NULL);
1847 	brand_assert(err == 0);
1848 	zoneid = (zoneid_t)rval.sys_rval1;
1849 
1850 	/* Get the zone's emulation bitmap. */
1851 	if ((err = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
1852 	    S10_EMUL_BITMAP, emul_bitmap, sizeof (emul_bitmap))) != 0) {
1853 		brand_abort(err, "The zone's patch level is unsupported");
1854 		/*NOTREACHED*/
1855 	}
1856 
1857 	bname = basename(argv[0]);
1858 
1859 	/*
1860 	 * In general we want the S10 commands that are zone-aware to continue
1861 	 * to behave as they normally do within a zone.  Since these commands
1862 	 * are zone-aware, they should continue to "do the right thing".
1863 	 * However, some zone-aware commands aren't going to work the way
1864 	 * we expect them to inside the branded zone.  In particular, the pkg
1865 	 * and patch commands will not properly manage all pkgs/patches
1866 	 * unless the commands think they are running in the global zone.  For
1867 	 * these commands we want to emulate the global zone.
1868 	 *
1869 	 * We don't do any emulation for pkgcond since it is typically used
1870 	 * in pkg/patch postinstall scripts and we want those scripts to do
1871 	 * the right thing inside a zone.
1872 	 *
1873 	 * One issue is the handling of hollow pkgs.  Since the pkgs are
1874 	 * hollow, they won't use pkgcond in their postinstall scripts.  These
1875 	 * pkgs typically are installing drivers so we handle that by
1876 	 * replacing add_drv and rem_drv in the s10_boot script.
1877 	 */
1878 	if (strcmp("pkgadd", bname) == 0 || strcmp("pkgrm", bname) == 0 ||
1879 	    strcmp("patchadd", bname) == 0 || strcmp("patchrm", bname) == 0)
1880 		emul_global_zone = B_TRUE;
1881 
1882 	ldentry = brand_post_init(S10_VERSION, argc, argv, envp);
1883 
1884 	brand_runexe(argv, ldentry);
1885 	/*NOTREACHED*/
1886 	brand_abort(0, "brand_runexe() returned");
1887 	return (-1);
1888 }
1889 
1890 /*
1891  * This table must have at least NSYSCALL entries in it.
1892  *
1893  * The second parameter of each entry in the brand_sysent_table
1894  * contains the number of parameters and flags that describe the
1895  * syscall return value encoding.  See the block comments at the
1896  * top of this file for more information about the syscall return
1897  * value flags and when they should be used.
1898  */
1899 brand_sysent_table_t brand_sysent_table[] = {
1900 #if defined(__sparc) && !defined(__sparcv9)
1901 	EMULATE(brand_indir, 9 | RV_64RVAL),	/*  0 */
1902 #else
1903 	NOSYS,					/*  0 */
1904 #endif
1905 	NOSYS,					/*   1 */
1906 	EMULATE(s10_forkall, 0 | RV_32RVAL2),	/*   2 */
1907 	NOSYS,					/*   3 */
1908 	NOSYS,					/*   4 */
1909 	EMULATE(s10_open, 3 | RV_DEFAULT),	/*   5 */
1910 	NOSYS,					/*   6 */
1911 	EMULATE(s10_wait, 0 | RV_32RVAL2),	/*   7 */
1912 	EMULATE(s10_creat, 2 | RV_DEFAULT),	/*   8 */
1913 	EMULATE(s10_link, 2 | RV_DEFAULT),	/*   9 */
1914 	EMULATE(s10_unlink, 1 | RV_DEFAULT),	/*  10 */
1915 	EMULATE(s10_exec, 2 | RV_DEFAULT),	/*  11 */
1916 	NOSYS,					/*  12 */
1917 	NOSYS,					/*  13 */
1918 	EMULATE(s10_mknod, 3 | RV_DEFAULT),	/*  14 */
1919 	EMULATE(s10_chmod, 2 | RV_DEFAULT),	/*  15 */
1920 	EMULATE(s10_chown, 3 | RV_DEFAULT),	/*  16 */
1921 	NOSYS,					/*  17 */
1922 	EMULATE(s10_stat, 2 | RV_DEFAULT),	/*  18 */
1923 	NOSYS,					/*  19 */
1924 	NOSYS,					/*  20 */
1925 	NOSYS,					/*  21 */
1926 	EMULATE(s10_umount, 1 | RV_DEFAULT),	/*  22 */
1927 	NOSYS,					/*  23 */
1928 	NOSYS,					/*  24 */
1929 	NOSYS,					/*  25 */
1930 	NOSYS,					/*  26 */
1931 	NOSYS,					/*  27 */
1932 	EMULATE(s10_fstat, 2 | RV_DEFAULT),	/*  28 */
1933 	NOSYS,					/*  29 */
1934 	EMULATE(s10_utime, 2 | RV_DEFAULT),	/*  30 */
1935 	NOSYS,					/*  31 */
1936 	NOSYS,					/*  32 */
1937 	EMULATE(s10_access, 2 | RV_DEFAULT),	/*  33 */
1938 	NOSYS,					/*  34 */
1939 	NOSYS,					/*  35 */
1940 	NOSYS,					/*  36 */
1941 	EMULATE(s10_kill, 2 | RV_DEFAULT),	/*  37 */
1942 	NOSYS,					/*  38 */
1943 	NOSYS,					/*  39 */
1944 	NOSYS,					/*  40 */
1945 	EMULATE(s10_dup, 1 | RV_DEFAULT),	/*  41 */
1946 	EMULATE(s10_pipe, 0 | RV_32RVAL2),	/*  42 */
1947 	NOSYS,					/*  43 */
1948 	NOSYS,					/*  44 */
1949 	NOSYS,					/*  45 */
1950 	NOSYS,					/*  46 */
1951 	NOSYS,					/*  47 */
1952 	NOSYS,					/*  48 */
1953 	NOSYS,					/*  49 */
1954 	NOSYS,					/*  50 */
1955 	NOSYS,					/*  51 */
1956 	NOSYS,					/*  52 */
1957 	NOSYS,					/*  53 */
1958 	EMULATE(s10_ioctl, 3 | RV_DEFAULT),	/*  54 */
1959 	NOSYS,					/*  55 */
1960 	NOSYS,					/*  56 */
1961 	NOSYS,					/*  57 */
1962 	NOSYS,					/*  58 */
1963 	EMULATE(s10_execve, 3 | RV_DEFAULT),	/*  59 */
1964 	NOSYS,					/*  60 */
1965 	NOSYS,					/*  61 */
1966 	NOSYS,					/*  62 */
1967 	NOSYS,					/*  63 */
1968 	NOSYS,					/*  64 */
1969 	NOSYS,					/*  65 */
1970 	NOSYS,					/*  66 */
1971 	NOSYS,					/*  67 */
1972 	NOSYS,					/*  68 */
1973 	NOSYS,					/*  69 */
1974 	NOSYS,					/*  70 */
1975 	EMULATE(s10_acctctl, 3 | RV_DEFAULT),	/*  71 */
1976 	NOSYS,					/*  72 */
1977 	NOSYS,					/*  73 */
1978 	NOSYS,					/*  74 */
1979 	EMULATE(s10_issetugid, 0 | RV_DEFAULT),	/*  75 */
1980 	EMULATE(s10_fsat, 6 | RV_DEFAULT),	/*  76 */
1981 	NOSYS,					/*  77 */
1982 	NOSYS,					/*  78 */
1983 	EMULATE(s10_rmdir, 1 | RV_DEFAULT),	/*  79 */
1984 	EMULATE(s10_mkdir, 2 | RV_DEFAULT),	/*  80 */
1985 	EMULATE(s10_getdents, 3 | RV_DEFAULT),	/*  81 */
1986 	NOSYS,					/*  82 */
1987 	NOSYS,					/*  83 */
1988 	NOSYS,					/*  84 */
1989 	NOSYS,					/*  85 */
1990 	NOSYS,					/*  86 */
1991 	EMULATE(s10_poll, 3 | RV_DEFAULT),	/*  87 */
1992 	EMULATE(s10_lstat, 2 | RV_DEFAULT),	/*  88 */
1993 	EMULATE(s10_symlink, 2 | RV_DEFAULT),	/*  89 */
1994 	EMULATE(s10_readlink, 3 | RV_DEFAULT),	/*  90 */
1995 	NOSYS,					/*  91 */
1996 	NOSYS,					/*  92 */
1997 	EMULATE(s10_fchmod, 2 | RV_DEFAULT),	/*  93 */
1998 	EMULATE(s10_fchown, 3 | RV_DEFAULT),	/*  94 */
1999 	EMULATE(s10_sigprocmask, 3 | RV_DEFAULT), /*  95 */
2000 	EMULATE(s10_sigsuspend, 1 | RV_DEFAULT), /*  96 */
2001 	NOSYS,					/*  97 */
2002 	EMULATE(s10_sigaction, 3 | RV_DEFAULT),	/*  98 */
2003 	EMULATE(s10_sigpending, 2 | RV_DEFAULT), /*  99 */
2004 	NOSYS,					/* 100 */
2005 	NOSYS,					/* 101 */
2006 	NOSYS,					/* 102 */
2007 	NOSYS,					/* 103 */
2008 	NOSYS,					/* 104 */
2009 	NOSYS,					/* 105 */
2010 	NOSYS,					/* 106 */
2011 	EMULATE(s10_waitid, 4 | RV_DEFAULT),	/* 107 */
2012 	EMULATE(s10_sigsendsys, 2 | RV_DEFAULT), /* 108 */
2013 	NOSYS,					/* 109 */
2014 	NOSYS,					/* 110 */
2015 	NOSYS,					/* 111 */
2016 	NOSYS,					/* 112 */
2017 	NOSYS,					/* 113 */
2018 	NOSYS,					/* 114 */
2019 	NOSYS,					/* 115 */
2020 	NOSYS,					/* 116 */
2021 	NOSYS,					/* 117 */
2022 	NOSYS,					/* 118 */
2023 	NOSYS,					/* 119 */
2024 	NOSYS,					/* 120 */
2025 	NOSYS,					/* 121 */
2026 	NOSYS,					/* 122 */
2027 #if defined(__x86)
2028 	EMULATE(s10_xstat, 3 | RV_DEFAULT),	/* 123 */
2029 	EMULATE(s10_lxstat, 3 | RV_DEFAULT),	/* 124 */
2030 	EMULATE(s10_fxstat, 3 | RV_DEFAULT),	/* 125 */
2031 	EMULATE(s10_xmknod, 4 | RV_DEFAULT),	/* 126 */
2032 #else
2033 	NOSYS,					/* 123 */
2034 	NOSYS,					/* 124 */
2035 	NOSYS,					/* 125 */
2036 	NOSYS,					/* 126 */
2037 #endif
2038 	NOSYS,					/* 127 */
2039 	NOSYS,					/* 128 */
2040 	NOSYS,					/* 129 */
2041 	EMULATE(s10_lchown, 3 | RV_DEFAULT),	/* 130 */
2042 	NOSYS,					/* 131 */
2043 	NOSYS,					/* 132 */
2044 	NOSYS,					/* 133 */
2045 	EMULATE(s10_rename, 2 | RV_DEFAULT),	/* 134 */
2046 	EMULATE(s10_uname, 1 | RV_DEFAULT),	/* 135 */
2047 	NOSYS,					/* 136 */
2048 	EMULATE(s10_sysconfig, 1 | RV_DEFAULT),	/* 137 */
2049 	NOSYS,					/* 138 */
2050 	EMULATE(s10_sysinfo, 3 | RV_DEFAULT),	/* 139 */
2051 	NOSYS,					/* 140 */
2052 	NOSYS,					/* 141 */
2053 	NOSYS,					/* 142 */
2054 	EMULATE(s10_fork1, 0 | RV_32RVAL2),	/* 143 */
2055 	EMULATE(s10_sigtimedwait, 3 | RV_DEFAULT), /* 144 */
2056 	NOSYS,					/* 145 */
2057 	NOSYS,					/* 146 */
2058 	EMULATE(s10_lwp_sema_wait, 1 | RV_DEFAULT), /* 147 */
2059 	NOSYS,					/* 148 */
2060 	NOSYS,					/* 149 */
2061 	NOSYS,					/* 150 */
2062 	NOSYS,					/* 151 */
2063 	NOSYS,					/* 152 */
2064 	NOSYS,					/* 153 */
2065 	EMULATE(s10_utimes, 2 | RV_DEFAULT),	/* 154 */
2066 	NOSYS,					/* 155 */
2067 	NOSYS,					/* 156 */
2068 	NOSYS,					/* 157 */
2069 	NOSYS,					/* 158 */
2070 	EMULATE(s10_lwp_create, 3 | RV_DEFAULT), /* 159 */
2071 	NOSYS,					/* 160 */
2072 	NOSYS,					/* 161 */
2073 	NOSYS,					/* 162 */
2074 	EMULATE(s10_lwp_kill, 2 | RV_DEFAULT),	/* 163 */
2075 	NOSYS,					/* 164 */
2076 	EMULATE(s10_lwp_sigmask, 3 | RV_32RVAL2), /* 165 */
2077 #if defined(__x86)
2078 	EMULATE(s10_lwp_private, 3 | RV_DEFAULT), /* 166 */
2079 #else
2080 	NOSYS,					/* 166 */
2081 #endif
2082 	NOSYS,					/* 167 */
2083 	NOSYS,					/* 168 */
2084 	EMULATE(s10_lwp_mutex_lock, 1 | RV_DEFAULT), /* 169 */
2085 	NOSYS,					/* 170 */
2086 	NOSYS,					/* 171 */
2087 	NOSYS,					/* 172 */
2088 	NOSYS,					/* 173 */
2089 	EMULATE(s10_pwrite, 4 | RV_DEFAULT),	/* 174 */
2090 	NOSYS,					/* 175 */
2091 	NOSYS,					/* 176 */
2092 	NOSYS,					/* 177 */
2093 	NOSYS,					/* 178 */
2094 	NOSYS,					/* 179 */
2095 	NOSYS,					/* 180 */
2096 	NOSYS,					/* 181 */
2097 	NOSYS,					/* 182 */
2098 	NOSYS,					/* 183 */
2099 	NOSYS,					/* 184 */
2100 	EMULATE(s10_acl, 4 | RV_DEFAULT),	/* 185 */
2101 	EMULATE(s10_auditsys, 4 | RV_64RVAL),	/* 186 */
2102 	NOSYS,					/* 187 */
2103 	NOSYS,					/* 188 */
2104 	NOSYS,					/* 189 */
2105 	EMULATE(s10_sigqueue, 4 | RV_DEFAULT),	/* 190 */
2106 	NOSYS,					/* 191 */
2107 	NOSYS,					/* 192 */
2108 	NOSYS,					/* 193 */
2109 	NOSYS,					/* 194 */
2110 	NOSYS,					/* 195 */
2111 	NOSYS,					/* 196 */
2112 	NOSYS,					/* 197 */
2113 	NOSYS,					/* 198 */
2114 	NOSYS,					/* 199 */
2115 	EMULATE(s10_facl, 4 | RV_DEFAULT),	/* 200 */
2116 	NOSYS,					/* 201 */
2117 	NOSYS,					/* 202 */
2118 	NOSYS,					/* 203 */
2119 	NOSYS,					/* 204 */
2120 	EMULATE(s10_signotify, 3 | RV_DEFAULT),	/* 205 */
2121 	NOSYS,					/* 206 */
2122 	NOSYS,					/* 207 */
2123 	NOSYS,					/* 208 */
2124 	NOSYS,					/* 209 */
2125 	EMULATE(s10_lwp_mutex_timedlock, 2 | RV_DEFAULT), /* 210 */
2126 	NOSYS,					/* 211 */
2127 	NOSYS,					/* 212 */
2128 #if defined(_LP64)
2129 	NOSYS,					/* 213 */
2130 #else
2131 	EMULATE(s10_getdents64, 3 | RV_DEFAULT), /* 213 */
2132 #endif
2133 	NOSYS,					/* 214 */
2134 #if defined(_LP64)
2135 	NOSYS,					/* 215 */
2136 	NOSYS,					/* 216 */
2137 	NOSYS,					/* 217 */
2138 #else
2139 	EMULATE(s10_stat64, 2 | RV_DEFAULT),	/* 215 */
2140 	EMULATE(s10_lstat64, 2 | RV_DEFAULT),	/* 216 */
2141 	EMULATE(s10_fstat64, 2 | RV_DEFAULT),	/* 217 */
2142 #endif
2143 	NOSYS,					/* 218 */
2144 	NOSYS,					/* 219 */
2145 	NOSYS,					/* 220 */
2146 	NOSYS,					/* 221 */
2147 	NOSYS,					/* 222 */
2148 #if defined(_LP64)
2149 	NOSYS,					/* 223 */
2150 	NOSYS,					/* 224 */
2151 	NOSYS,					/* 225 */
2152 #else
2153 	EMULATE(s10_pwrite64, 5 | RV_DEFAULT),	/* 223 */
2154 	EMULATE(s10_creat64, 2 | RV_DEFAULT),	/* 224 */
2155 	EMULATE(s10_open64, 3 | RV_DEFAULT),	/* 225 */
2156 #endif
2157 	NOSYS,					/* 226 */
2158 	EMULATE(s10_zone, 5 | RV_DEFAULT),	/* 227 */
2159 	NOSYS,					/* 228 */
2160 	NOSYS,					/* 229 */
2161 	EMULATE(s10_so_socket, 5 | RV_DEFAULT),	/* 230 */
2162 	NOSYS,					/* 231 */
2163 	NOSYS,					/* 232 */
2164 	NOSYS,					/* 233 */
2165 	EMULATE(s10_accept, 4 | RV_DEFAULT),	/* 234 */
2166 	NOSYS,					/* 235 */
2167 	NOSYS,					/* 236 */
2168 	NOSYS,					/* 237 */
2169 	NOSYS,					/* 238 */
2170 	NOSYS,					/* 239 */
2171 	NOSYS,					/* 240 */
2172 	NOSYS,					/* 241 */
2173 	NOSYS,					/* 242 */
2174 	NOSYS,					/* 243 */
2175 	NOSYS,					/* 244 */
2176 	NOSYS,					/* 245 */
2177 	NOSYS,					/* 246 */
2178 	NOSYS,					/* 247 */
2179 	NOSYS,					/* 248 */
2180 	NOSYS,					/* 249 */
2181 	NOSYS,					/* 250 */
2182 	EMULATE(s10_lwp_mutex_trylock, 1 | RV_DEFAULT), /* 251 */
2183 	NOSYS,					/* 252 */
2184 	NOSYS,					/* 253 */
2185 	NOSYS,					/* 254 */
2186 	NOSYS					/* 255 */
2187 };
2188