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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <thread.h>
33 #include <sys/auxv.h>
34 #include <sys/brand.h>
35 #include <sys/inttypes.h>
36 #include <sys/lwp.h>
37 #include <sys/syscall.h>
38 #include <sys/systm.h>
39 #include <sys/utsname.h>
40 #include <sys/systeminfo.h>
41 #include <sys/zone.h>
42 #include <sys/stat.h>
43 #include <sys/mntent.h>
44 #include <sys/ctfs.h>
45 #include <sys/priv.h>
46 #include <sys/acctctl.h>
47 #include <libgen.h>
48 #include <bsm/audit.h>
49 #include <sys/crypto/ioctl.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/ucontext.h>
53 #include <sys/mntio.h>
54 #include <sys/mnttab.h>
55 #include <atomic.h>
56 
57 #include <s10_brand.h>
58 #include <s10_misc.h>
59 
60 /*
61  * Principles of emulation 101.
62  *
63  *
64  * *** Setting errno
65  *
66  * Just don't do it.  This emulation library is loaded onto a
67  * seperate link map from the application who's address space we're
68  * running in.  We have our own private copy of libc, so there for,
69  * the errno value accessible from here is is also private and changing
70  * it will not affect any errno value that the processes who's address
71  * space we are running in will see.  To return an error condition we
72  * should return the negated errno value we'd like the system to return.
73  * For more information about this see the comment in s10_handler().
74  * Basically, when we return to the caller that initiated the system
75  * call it's their responsibility to set errno.
76  *
77  *
78  * *** Recursion Considerations
79  *
80  * When emulating system calls we need to be very careful about what
81  * library calls we invoke.  Library calls should be kept to a minimum.
82  * One issue is that library calls can invoke system calls, so if we're
83  * emulating a system call and we invoke a library call that depends on
84  * that system call we will probably enter a recursive loop, which would
85  * be bad.
86  *
87  *
88  * *** Return Values.
89  *
90  * When declaring new syscall emulation functions, it is very important
91  * to to set the proper RV_* flags in the s10_sysent_table.  Upon failure,
92  * syscall emulation fuctions should return an errno value.  Upon success
93  * syscall emulation functions should return 0 and set the sysret_t return
94  * value parameters accordingly.
95  *
96  * There are five possible syscall macro wrappers used in the kernel's system
97  * call sysent table.  These turn into the following return values:
98  *	SYSENT_CL	-> SYSENT_C or SYSENT_CI
99  *	SYSENT_C	SE_64RVAL		RV_DEFAULT
100  *	SYSENT_CI	SE_32RVAL1		RV_DEFAULT
101  *	SYSENT_2CI	SE_32RVAL1|SE_32RVAL2	RV_32RVAL2
102  *	SYSENT_AP	SE_64RVAL		RV_64RVAL
103  *
104  *
105  * *** Agent lwp considerations
106  *
107  * It is currently impossible to do any emulation for these system call
108  * when they are being invoked on behalf of an agent lwp.  To understand why
109  * it's impossible you have to understand how agent lwp syscalls work.
110  *
111  * The agent lwp syscall process works as follows:
112  *   1  The controlling process stops the target.
113  *   2  The controlling process injects an agent lwp which is also stopped.
114  *      This agent lwp assumes the userland stack and register values
115  *      of another stopped lwp in the current process.
116  *   3  The controlling process configures the agent lwp to start
117  *      executing the requested system call.
118  *   4  The controlling process configure /proc to stop the agent lwp when
119  *      it enters the requested system call.
120  *   5  The controlling processes allows the agent lwp to start executing.
121  *   6  The agent lwp traps into the kernel to perform the requested system
122  *      call and immediately stop.
123  *   7  The controlling process copies all the arguments for the requested
124  *      system call onto the agent lwp's stack.
125  *   8  The controlling process configures /proc to stop the agent lwp
126  *      when it completes the requested system call.
127  *   9  The controlling processes allows the agent lwp to start executing.
128  *  10  The agent lwp executes the system call and then stop before returning
129  *      to userland.
130  *  11  The controlling process copies the return value and return arguments
131  *      back from the agent lwps stack.
132  *  12  The controlling process destroys the agent lwp and restarts
133  *      the target process.
134  *
135  * The fundamental problem is that when the agent executes the request
136  * system call in step 5, if we're emulating that system call then the
137  * lwp is redirected back to our emulation layer without blocking
138  * in the kernel.  But our emulation layer can't access the arguments
139  * for the system call because they haven't been copied to the stack
140  * yet and they still only exist in the controlling processes address
141  * space.  This prevents us from being able to do any emulation of
142  * agent lwp system calls.  Hence, currently our brand trap interposition
143  * callback (s10_brand_syscall_callback_common) will detect if a system
144  * call is being made by an agent lwp, and if this is the case it will
145  * never redirect the system call to this emulation library.
146  *
147  * In the future, if this proves to be a problem the the easiest solution
148  * would probably be to replace the branded versions of these application
149  * with their native counterparts.  Ie,  truss, plimit, and pfiles could be
150  * replace with wrapper scripts that execute the native versions of these
151  * applications.  In the case of plimit and pfiles this should be pretty
152  * strait forward.  Truss would probably be more tricky since it can
153  * execute applications which would be branded applications, so in that
154  * case it might be necessary to create a loadable library which could
155  * be LD_PRELOADed into truss and this library would interpose on the
156  * exec() system call to allow truss to correctly execute branded
157  * processes.  It should be pointed out that this solution could work
158  * because "native agent lwps" (ie, agent lwps created by native
159  * processes) can be treated differently from "branded aged lwps" (ie,
160  * agent lwps created by branded processes), since native agent lwps
161  * would presumably be making native system calls and hence not need
162  * any interposition.
163  *
164  */
165 
166 static zoneid_t zoneid;
167 static boolean_t emul_global_zone = B_FALSE;
168 static s10_emul_bitmap_t emul_bitmap;
169 pid_t zone_init_pid;
170 
171 /*
172  * S10_FEATURE_IS_PRESENT is a macro that helps facilitate conditional
173  * emulation.  For each constant N defined in the s10_emulated_features
174  * enumeration in usr/src/uts/common/brand/solaris10/s10_brand.h,
175  * S10_FEATURE_IS_PRESENT(N) is true iff the feature/backport represented by N
176  * is present in the Solaris 10 image hosted within the zone.  In other words,
177  * S10_FEATURE_IS_PRESENT(N) is true iff the file /usr/lib/brand/solaris10/M,
178  * where M is the enum value of N, was present in the zone when the zone booted.
179  *
180  *
181  * *** Sample Usage
182  *
183  * Suppose that you need to backport a fix to Solaris 10 and there is
184  * emulation in place for the fix.  Suppose further that the emulation won't be
185  * needed if the fix is backported (i.e., if the fix is present in the hosted
186  * Solaris 10 environment, then the brand won't need the emulation).  Then if
187  * you add a constant named "S10_FEATURE_X" to the end of the
188  * s10_emulated_features enumeration that represents the backported fix and
189  * S10_FEATURE_X evaluates to four, then you should create a file named
190  * /usr/lib/brand/solaris10/4 as part of your backport.  Additionally, you
191  * should retain the aforementioned emulation but modify it so that it's
192  * performed only when S10_FEATURE_IS_PRESENT(S10_FEATURE_X) is false.  Thus the
193  * emulation function should look something like the following:
194  *
195  *	static int
196  *	my_emul_function(sysret_t *rv, ...)
197  *	{
198  *		if (S10_FEATURE_IS_PRESENT(S10_FEATURE_X)) {
199  *			// Don't emulate
200  *			return (__systemcall(rv, ...));
201  *		} else {
202  *			// Emulate whatever needs to be emulated when the
203  *			// backport isn't present in the Solaris 10 image.
204  *		}
205  *	}
206  */
207 #define	S10_FEATURE_IS_PRESENT(s10_emulated_features_constant)	\
208 	((emul_bitmap[(s10_emulated_features_constant) >> 3] &	\
209 	(1 << ((s10_emulated_features_constant) & 0x7))) != 0)
210 
211 #define	EMULATE(cb, args)	{ (sysent_cb_t)(cb), (args) }
212 #define	NOSYS			EMULATE(s10_unimpl, (0 | RV_DEFAULT))
213 
214 typedef long (*sysent_cb_t)();
215 typedef struct s10_sysent_table {
216 	sysent_cb_t	st_callc;
217 	uintptr_t	st_args;
218 } s10_sysent_table_t;
219 s10_sysent_table_t s10_sysent_table[];
220 
221 #define	S10_UTS_RELEASE	"5.10"
222 #define	S10_UTS_VERSION	"Generic_Virtual"
223 
224 /*LINTED: static unused*/
225 static volatile int		s10_abort_err;
226 /*LINTED: static unused*/
227 static volatile const char	*s10_abort_msg;
228 /*LINTED: static unused*/
229 static volatile const char	*s10_abort_file;
230 /*LINTED: static unused*/
231 static volatile int		s10_abort_line;
232 
233 extern int errno;
234 
235 /*ARGSUSED*/
236 void
237 _s10_abort(int err, const char *msg, const char *file, int line)
238 {
239 	sysret_t rval;
240 
241 	/* Save the error message into convenient globals */
242 	s10_abort_err = err;
243 	s10_abort_msg = msg;
244 	s10_abort_file = file;
245 	s10_abort_line = line;
246 
247 	/* kill ourselves */
248 	abort();
249 
250 	/* If abort() didn't work, try something stronger. */
251 	(void) __systemcall(&rval, SYS_lwp_kill + 1024, _lwp_self(), SIGKILL);
252 }
253 
254 static int
255 s10_uucopy(const void *from, void *to, size_t size)
256 {
257 	sysret_t rval;
258 
259 	if (__systemcall(&rval, SYS_uucopy + 1024, from, to, size) != 0)
260 		return (EFAULT);
261 	return (0);
262 }
263 
264 /*
265  * ATTENTION: uucopystr() does NOT ensure that string are null terminated!
266  */
267 static int
268 s10_uucopystr(const void *from, void *to, size_t size)
269 {
270 	sysret_t rval;
271 
272 	if (__systemcall(&rval, SYS_uucopystr + 1024, from, to, size) != 0)
273 		return (EFAULT);
274 	return (0);
275 }
276 
277 /*
278  * Figures out the PID of init for the zone.  Also returns a boolean
279  * indicating whether this process currently has that pid: if so,
280  * then at this moment, we are init.
281  */
282 static boolean_t
283 get_initpid_info(void)
284 {
285 	pid_t pid;
286 	sysret_t rval;
287 	int err;
288 
289 	/*
290 	 * Determine the current process PID and the PID of the zone's init.
291 	 * We use care not to call getpid() here, because we're not supposed
292 	 * to call getpid() until after the program is fully linked-- the
293 	 * first call to getpid() is a signal from the linker to debuggers
294 	 * that linking has been completed.
295 	 */
296 	if ((err = __systemcall(&rval, SYS_brand,
297 	    B_S10_PIDINFO, &pid, &zone_init_pid)) != 0) {
298 		s10_abort(err, "Failed to get init's pid");
299 	}
300 
301 	/*
302 	 * Note that we need to be cautious with the pid we get back--
303 	 * it should not be stashed and used in place of getpid(), since
304 	 * we might fork(2).  So we keep zone_init_pid and toss the pid
305 	 * we otherwise got.
306 	 */
307 	if (pid == zone_init_pid)
308 		return (B_TRUE);
309 
310 	return (B_FALSE);
311 }
312 
313 /*
314  * This function is defined to be NOSYS but it won't be called from the
315  * the kernel since the NOSYS system calls are not enabled in the kernel.
316  * Thus, the only time this function is called is directly from within the
317  * indirect system call path.
318  */
319 /*ARGSUSED*/
320 static long
321 s10_unimpl(sysret_t *rv, uintptr_t p1)
322 {
323 	sysret_t rval;
324 
325 	/*
326 	 * We'd like to print out some kind of error message here like
327 	 * "unsupported syscall", but we can't because it's not safe to
328 	 * assume that stderr or STDERR_FILENO actually points to something
329 	 * that is a terminal, and if we wrote to those files we could
330 	 * inadvertantly write to some applications open files, which would
331 	 * be bad.
332 	 *
333 	 * Normally, if an application calls an invalid system call
334 	 * it get a SIGSYS sent to it.  So we'll just go ahead and send
335 	 * ourselves a signal here.  Note that this is far from ideal since
336 	 * if the application has registered a signal handler, that signal
337 	 * handler may recieve a ucontext_t as the third parameter to
338 	 * indicate the context of the process when the signal was
339 	 * generated, and in this case that context will not be what the
340 	 * application is expecting.  Hence, we should probably create a
341 	 * brandsys() kernel function that can deliver the signal to us
342 	 * with the correct ucontext_t.
343 	 */
344 	(void) __systemcall(&rval, SYS_lwp_kill + 1024, _lwp_self(), SIGSYS);
345 	return (ENOSYS);
346 }
347 
348 #if defined(__sparc) && !defined(__sparcv9)
349 /*
350  * Yuck.  For 32-bit sparc applications, handle indirect system calls.
351  * Note that we declare this interface to use the maximum number of
352  * system call arguments.  If we recieve a system call that uses less
353  * arguments, then the additional arguments will be garbage, but they
354  * will also be ignored so that should be ok.
355  */
356 static long
357 s10_indir(sysret_t *rv, int code,
358     uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
359     uintptr_t a5, uintptr_t a6, uintptr_t a7)
360 {
361 	s10_sysent_table_t *sst = &(s10_sysent_table[code]);
362 
363 	s10_assert(code < NSYSCALL);
364 	switch (sst->st_args & NARGS_MASK) {
365 	case 0:
366 		return ((sst->st_callc)(rv));
367 	case 1:
368 		return ((sst->st_callc)(rv, a0));
369 	case 2:
370 		return ((sst->st_callc)(rv, a0, a1));
371 	case 3:
372 		return ((sst->st_callc)(rv, a0, a1, a2));
373 	case 4:
374 		return ((sst->st_callc)(rv, a0, a1, a2, a3));
375 	case 5:
376 		return ((sst->st_callc)(rv, a0, a1, a2, a3, a4));
377 	case 6:
378 		return ((sst->st_callc)(rv, rv, a0, a1, a2, a3, a4, a5));
379 	case 7:
380 		return ((sst->st_callc)(rv, a0, a1, a2, a3, a4, a5, a6));
381 	case 8:
382 		return ((sst->st_callc)(rv, a0, a1, a2, a3, a4, a5, a6, a7));
383 	}
384 	s10_abort(0, "invalid entry in s10_sysent_table");
385 	return (EINVAL);
386 }
387 #endif /* __sparc && !__sparcv9 */
388 
389 /* Free the thread-local storage provided my mntfs_get_mntentbuf() */
390 static void
391 mntfs_free_mntentbuf(void *arg)
392 {
393 	struct mntentbuf *embufp = arg;
394 
395 	if (embufp == NULL)
396 		return;
397 	if (embufp->mbuf_emp)
398 		free(embufp->mbuf_emp);
399 	if (embufp->mbuf_buf)
400 		free(embufp->mbuf_buf);
401 	bzero(embufp, sizeof (struct mntentbuf));
402 	free(embufp);
403 }
404 
405 /* Provide the thread-local storage required by mntfs_ioctl() */
406 static struct mntentbuf *
407 mntfs_get_mntentbuf(size_t size)
408 {
409 	static mutex_t keylock;
410 	static thread_key_t key;
411 	static int once_per_keyname = 0;
412 	void *tsd = NULL;
413 	struct mntentbuf *embufp;
414 
415 	/* Create the key. */
416 	if (!once_per_keyname) {
417 		(void) mutex_lock(&keylock);
418 		if (!once_per_keyname) {
419 			if (thr_keycreate(&key, mntfs_free_mntentbuf)) {
420 				(void) mutex_unlock(&keylock);
421 				return (NULL);
422 			} else {
423 				once_per_keyname++;
424 			}
425 		}
426 		(void) mutex_unlock(&keylock);
427 	}
428 
429 	/*
430 	 * The thread-specific datum for this key is the address of a struct
431 	 * mntentbuf. If this is the first time here then we allocate the struct
432 	 * and its contents, and associate its address with the thread; if there
433 	 * are any problems then we abort.
434 	 */
435 	if (thr_getspecific(key, &tsd))
436 		return (NULL);
437 	if (tsd == NULL) {
438 		if (!(embufp = calloc(1, sizeof (struct mntentbuf))) ||
439 		    !(embufp->mbuf_emp = malloc(sizeof (struct extmnttab))) ||
440 		    thr_setspecific(key, embufp)) {
441 			mntfs_free_mntentbuf(embufp);
442 			return (NULL);
443 		}
444 	} else {
445 		embufp = tsd;
446 	}
447 
448 	/* Return the buffer, resizing it if necessary. */
449 	if (size > embufp->mbuf_bufsize) {
450 		if (embufp->mbuf_buf)
451 			free(embufp->mbuf_buf);
452 		if ((embufp->mbuf_buf = malloc(size)) == NULL) {
453 			embufp->mbuf_bufsize = 0;
454 			return (NULL);
455 		} else {
456 			embufp->mbuf_bufsize = size;
457 		}
458 	}
459 	return (embufp);
460 }
461 
462 /*
463  * The MNTIOC_GETMNTENT command in this release differs from that in Solaris 10.
464  * Previously, the command would copy a pointer to a struct extmnttab to an
465  * address provided as an argument. The pointer would be somewhere within a
466  * mapping already present within the user's address space. In addition, the
467  * text to which the struct's members pointed would also be within a
468  * pre-existing mapping. Now, the user is required to allocate memory for both
469  * the struct and the text buffer, and to pass the address of each within a
470  * struct mntentbuf. In order to conceal these details from a Solaris 10 client
471  * we allocate some thread-local storage in which to create the necessary data
472  * structures; this is static, thread-safe memory that will be cleaned up
473  * without the caller's intervention.
474  *
475  * MNTIOC_GETEXTMNTENT and MNTIOC_GETMNTANY are new in this release; they should
476  * not work for older clients.
477  */
478 int
479 mntfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
480 {
481 	int err;
482 	struct stat statbuf;
483 	struct mntentbuf *embufp;
484 	static size_t bufsize = MNT_LINE_MAX;
485 
486 	if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &statbuf)) != 0)
487 		return (err);
488 	if (strcmp(statbuf.st_fstype, MNTTYPE_MNTFS) != 0)
489 		return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
490 
491 	if (cmd == MNTIOC_GETEXTMNTENT || cmd == MNTIOC_GETMNTANY)
492 		return (EINVAL);
493 
494 	if ((embufp = mntfs_get_mntentbuf(bufsize)) == NULL)
495 		return (ENOMEM);
496 
497 	/*
498 	 * MNTIOC_GETEXTMNTENT advances the file pointer once it has
499 	 * successfully copied out the result to the address provided. We
500 	 * therefore need to check the user-supplied address now since the
501 	 * one we'll be providing is guaranteed to work.
502 	 */
503 	if (s10_uucopy(&embufp->mbuf_emp, (void *)arg, sizeof (void *)) != 0)
504 		return (EFAULT);
505 
506 	/*
507 	 * Keep retrying for as long as we fail for want of a large enough
508 	 * buffer.
509 	 */
510 	for (;;) {
511 		if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes,
512 		    MNTIOC_GETEXTMNTENT, embufp)) != 0)
513 			return (err);
514 
515 		if (rval->sys_rval1 == MNTFS_TOOLONG) {
516 			/* The buffer wasn't large enough. */
517 			(void) atomic_swap_ulong((unsigned long *)&bufsize,
518 			    2 * embufp->mbuf_bufsize);
519 			if ((embufp = mntfs_get_mntentbuf(bufsize)) == NULL)
520 				return (ENOMEM);
521 		} else {
522 			break;
523 		}
524 	}
525 
526 	if (s10_uucopy(&embufp->mbuf_emp, (void *)arg, sizeof (void *)) != 0)
527 		return (EFAULT);
528 
529 	return (0);
530 }
531 
532 /*
533  * Assign the structure member value from the s (source) structure to the
534  * d (dest) structure.
535  */
536 #define	struct_assign(d, s, val)	(((d).val) = ((s).val))
537 
538 /*
539  * The CRYPTO_GET_FUNCTION_LIST parameter structure crypto_function_list_t
540  * changed between S10 and Nevada, so we have to emulate the old S10
541  * crypto_function_list_t structure when interposing on the ioctl syscall.
542  */
543 typedef struct s10_crypto_function_list {
544 	boolean_t fl_digest_init;
545 	boolean_t fl_digest;
546 	boolean_t fl_digest_update;
547 	boolean_t fl_digest_key;
548 	boolean_t fl_digest_final;
549 
550 	boolean_t fl_encrypt_init;
551 	boolean_t fl_encrypt;
552 	boolean_t fl_encrypt_update;
553 	boolean_t fl_encrypt_final;
554 
555 	boolean_t fl_decrypt_init;
556 	boolean_t fl_decrypt;
557 	boolean_t fl_decrypt_update;
558 	boolean_t fl_decrypt_final;
559 
560 	boolean_t fl_mac_init;
561 	boolean_t fl_mac;
562 	boolean_t fl_mac_update;
563 	boolean_t fl_mac_final;
564 
565 	boolean_t fl_sign_init;
566 	boolean_t fl_sign;
567 	boolean_t fl_sign_update;
568 	boolean_t fl_sign_final;
569 	boolean_t fl_sign_recover_init;
570 	boolean_t fl_sign_recover;
571 
572 	boolean_t fl_verify_init;
573 	boolean_t fl_verify;
574 	boolean_t fl_verify_update;
575 	boolean_t fl_verify_final;
576 	boolean_t fl_verify_recover_init;
577 	boolean_t fl_verify_recover;
578 
579 	boolean_t fl_digest_encrypt_update;
580 	boolean_t fl_decrypt_digest_update;
581 	boolean_t fl_sign_encrypt_update;
582 	boolean_t fl_decrypt_verify_update;
583 
584 	boolean_t fl_seed_random;
585 	boolean_t fl_generate_random;
586 
587 	boolean_t fl_session_open;
588 	boolean_t fl_session_close;
589 	boolean_t fl_session_login;
590 	boolean_t fl_session_logout;
591 
592 	boolean_t fl_object_create;
593 	boolean_t fl_object_copy;
594 	boolean_t fl_object_destroy;
595 	boolean_t fl_object_get_size;
596 	boolean_t fl_object_get_attribute_value;
597 	boolean_t fl_object_set_attribute_value;
598 	boolean_t fl_object_find_init;
599 	boolean_t fl_object_find;
600 	boolean_t fl_object_find_final;
601 
602 	boolean_t fl_key_generate;
603 	boolean_t fl_key_generate_pair;
604 	boolean_t fl_key_wrap;
605 	boolean_t fl_key_unwrap;
606 	boolean_t fl_key_derive;
607 
608 	boolean_t fl_init_token;
609 	boolean_t fl_init_pin;
610 	boolean_t fl_set_pin;
611 
612 	boolean_t prov_is_limited;
613 	uint32_t prov_hash_threshold;
614 	uint32_t prov_hash_limit;
615 } s10_crypto_function_list_t;
616 
617 typedef struct s10_crypto_get_function_list {
618 	uint_t				fl_return_value;
619 	crypto_provider_id_t		fl_provider_id;
620 	s10_crypto_function_list_t	fl_list;
621 } s10_crypto_get_function_list_t;
622 
623 /*
624  * The structure returned by the CRYPTO_GET_FUNCTION_LIST ioctl on /dev/crypto
625  * increased in size due to:
626  *	6482533 Threshold for HW offload via PKCS11 interface
627  * between S10 and Nevada.  This is a relatively simple process of filling
628  * in the S10 structure fields with the Nevada data.
629  *
630  * We stat the device to make sure that the ioctl is meant for /dev/crypto.
631  *
632  */
633 static int
634 crypto_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
635 {
636 	int				err;
637 	s10_crypto_get_function_list_t	s10_param;
638 	crypto_get_function_list_t	native_param;
639 	static dev_t			crypto_dev = (dev_t)-1;
640 	struct stat			sbuf;
641 
642 	if (crypto_dev == (dev_t)-1) {
643 		if ((err = __systemcall(rval, SYS_stat + 1024, "/dev/crypto",
644 		    &sbuf)) != 0)
645 			goto nonemuioctl;
646 		crypto_dev = major(sbuf.st_rdev);
647 	}
648 	if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &sbuf)) != 0)
649 		return (err);
650 	/* Each open fd of /dev/crypto gets a new minor device. */
651 	if (major(sbuf.st_rdev) != crypto_dev)
652 		goto nonemuioctl;
653 
654 	if (s10_uucopy((const void *)arg, &s10_param, sizeof (s10_param)) != 0)
655 		return (EFAULT);
656 	struct_assign(native_param, s10_param, fl_provider_id);
657 	if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd,
658 	    &native_param)) != 0)
659 		return (err);
660 
661 	struct_assign(s10_param, native_param, fl_return_value);
662 	struct_assign(s10_param, native_param, fl_provider_id);
663 
664 	struct_assign(s10_param, native_param, fl_list.fl_digest_init);
665 	struct_assign(s10_param, native_param, fl_list.fl_digest);
666 	struct_assign(s10_param, native_param, fl_list.fl_digest_update);
667 	struct_assign(s10_param, native_param, fl_list.fl_digest_key);
668 	struct_assign(s10_param, native_param, fl_list.fl_digest_final);
669 
670 	struct_assign(s10_param, native_param, fl_list.fl_encrypt_init);
671 	struct_assign(s10_param, native_param, fl_list.fl_encrypt);
672 	struct_assign(s10_param, native_param, fl_list.fl_encrypt_update);
673 	struct_assign(s10_param, native_param, fl_list.fl_encrypt_final);
674 
675 	struct_assign(s10_param, native_param, fl_list.fl_decrypt_init);
676 	struct_assign(s10_param, native_param, fl_list.fl_decrypt);
677 	struct_assign(s10_param, native_param, fl_list.fl_decrypt_update);
678 	struct_assign(s10_param, native_param, fl_list.fl_decrypt_final);
679 
680 	struct_assign(s10_param, native_param, fl_list.fl_mac_init);
681 	struct_assign(s10_param, native_param, fl_list.fl_mac);
682 	struct_assign(s10_param, native_param, fl_list.fl_mac_update);
683 	struct_assign(s10_param, native_param, fl_list.fl_mac_final);
684 
685 	struct_assign(s10_param, native_param, fl_list.fl_sign_init);
686 	struct_assign(s10_param, native_param, fl_list.fl_sign);
687 	struct_assign(s10_param, native_param, fl_list.fl_sign_update);
688 	struct_assign(s10_param, native_param, fl_list.fl_sign_final);
689 	struct_assign(s10_param, native_param, fl_list.fl_sign_recover_init);
690 	struct_assign(s10_param, native_param, fl_list.fl_sign_recover);
691 
692 	struct_assign(s10_param, native_param, fl_list.fl_verify_init);
693 	struct_assign(s10_param, native_param, fl_list.fl_verify);
694 	struct_assign(s10_param, native_param, fl_list.fl_verify_update);
695 	struct_assign(s10_param, native_param, fl_list.fl_verify_final);
696 	struct_assign(s10_param, native_param, fl_list.fl_verify_recover_init);
697 	struct_assign(s10_param, native_param, fl_list.fl_verify_recover);
698 
699 	struct_assign(s10_param, native_param,
700 	    fl_list.fl_digest_encrypt_update);
701 	struct_assign(s10_param, native_param,
702 	    fl_list.fl_decrypt_digest_update);
703 	struct_assign(s10_param, native_param, fl_list.fl_sign_encrypt_update);
704 	struct_assign(s10_param, native_param,
705 	    fl_list.fl_decrypt_verify_update);
706 
707 	struct_assign(s10_param, native_param, fl_list.fl_seed_random);
708 	struct_assign(s10_param, native_param, fl_list.fl_generate_random);
709 
710 	struct_assign(s10_param, native_param, fl_list.fl_session_open);
711 	struct_assign(s10_param, native_param, fl_list.fl_session_close);
712 	struct_assign(s10_param, native_param, fl_list.fl_session_login);
713 	struct_assign(s10_param, native_param, fl_list.fl_session_logout);
714 
715 	struct_assign(s10_param, native_param, fl_list.fl_object_create);
716 	struct_assign(s10_param, native_param, fl_list.fl_object_copy);
717 	struct_assign(s10_param, native_param, fl_list.fl_object_destroy);
718 	struct_assign(s10_param, native_param, fl_list.fl_object_get_size);
719 	struct_assign(s10_param, native_param,
720 	    fl_list.fl_object_get_attribute_value);
721 	struct_assign(s10_param, native_param,
722 	    fl_list.fl_object_set_attribute_value);
723 	struct_assign(s10_param, native_param, fl_list.fl_object_find_init);
724 	struct_assign(s10_param, native_param, fl_list.fl_object_find);
725 	struct_assign(s10_param, native_param, fl_list.fl_object_find_final);
726 
727 	struct_assign(s10_param, native_param, fl_list.fl_key_generate);
728 	struct_assign(s10_param, native_param, fl_list.fl_key_generate_pair);
729 	struct_assign(s10_param, native_param, fl_list.fl_key_wrap);
730 	struct_assign(s10_param, native_param, fl_list.fl_key_unwrap);
731 	struct_assign(s10_param, native_param, fl_list.fl_key_derive);
732 
733 	struct_assign(s10_param, native_param, fl_list.fl_init_token);
734 	struct_assign(s10_param, native_param, fl_list.fl_init_pin);
735 	struct_assign(s10_param, native_param, fl_list.fl_set_pin);
736 
737 	struct_assign(s10_param, native_param, fl_list.prov_is_limited);
738 	struct_assign(s10_param, native_param, fl_list.prov_hash_threshold);
739 	struct_assign(s10_param, native_param, fl_list.prov_hash_limit);
740 
741 	return (s10_uucopy(&s10_param, (void *)arg, sizeof (s10_param)));
742 
743 nonemuioctl:
744 	return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
745 }
746 
747 /*
748  * The process contract CT_TGET and CT_TSET parameter structure ct_param_t
749  * changed between S10 and Nevada, so we have to emulate the old S10
750  * ct_param_t structure when interposing on the ioctl syscall.
751  */
752 typedef struct s10_ct_param {
753 	uint32_t ctpm_id;
754 	uint32_t ctpm_pad;
755 	uint64_t ctpm_value;
756 } s10_ct_param_t;
757 
758 /*
759  * We have to emulate process contract ioctls for init(1M) because the
760  * ioctl parameter structure changed between S10 and Nevada.  This is
761  * a relatively simple process of filling Nevada structure fields,
762  * shuffling values, and initiating a native system call.
763  *
764  * For now, we'll assume that all consumers of CT_TGET and CT_TSET will
765  * need emulation.  We'll issue a stat to make sure that the ioctl
766  * is meant for the contract file system.
767  *
768  */
769 static int
770 ctfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
771 {
772 	int err;
773 	s10_ct_param_t s10param;
774 	ct_param_t param;
775 	struct stat statbuf;
776 
777 	if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &statbuf)) != 0)
778 		return (err);
779 	if (strcmp(statbuf.st_fstype, MNTTYPE_CTFS) != 0)
780 		return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
781 
782 	if (s10_uucopy((const void *)arg, &s10param, sizeof (s10param)) != 0)
783 		return (EFAULT);
784 	param.ctpm_id = s10param.ctpm_id;
785 	param.ctpm_size = sizeof (uint64_t);
786 	param.ctpm_value = &s10param.ctpm_value;
787 	if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, &param))
788 	    != 0)
789 		return (err);
790 
791 	if (cmd == CT_TGET)
792 		return (s10_uucopy(&s10param, (void *)arg, sizeof (s10param)));
793 
794 	return (0);
795 }
796 
797 typedef struct s10_zfs_cmd {
798 	char		zc_name[MAXPATHLEN];
799 	char		zc_value[MAXPATHLEN * 2];
800 	char		zc_string[MAXNAMELEN];
801 	uint64_t	zc_guid;
802 	uint64_t	zc_nvlist_conf;		/* really (char *) */
803 	uint64_t	zc_nvlist_conf_size;
804 	uint64_t	zc_nvlist_src;		/* really (char *) */
805 	uint64_t	zc_nvlist_src_size;
806 	uint64_t	zc_nvlist_dst;		/* really (char *) */
807 	uint64_t	zc_nvlist_dst_size;
808 	uint64_t	zc_cookie;
809 	uint64_t	zc_objset_type;
810 	uint64_t	zc_perm_action;
811 	uint64_t 	zc_history;		/* really (char *) */
812 	uint64_t 	zc_history_len;
813 	uint64_t	zc_history_offset;
814 	uint64_t	zc_obj;
815 	/* Solaris Next added zc_iflags member here */
816 	zfs_share_t	zc_share;
817 	dmu_objset_stats_t zc_objset_stats;
818 	struct drr_begin zc_begin_record;
819 	zinject_record_t zc_inject_record;
820 } s10_zfs_cmd_t;
821 
822 /*
823  * There is a difference in the zfs_cmd_t ioctl parameter between S10 and
824  * Solaris Next so we need to translate between the two structures when
825  * making ZFS ioctls.
826  */
827 static int
828 zfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
829 {
830 	int				err;
831 	s10_zfs_cmd_t			s10_param;
832 	zfs_cmd_t			native_param;
833 	static dev_t			zfs_dev = (dev_t)-1;
834 	struct stat			sbuf;
835 
836 	if (zfs_dev == (dev_t)-1) {
837 		if ((err = __systemcall(rval, SYS_stat + 1024, "/dev/zfs",
838 		    &sbuf)) != 0)
839 			goto nonemuioctl;
840 		zfs_dev = major(sbuf.st_rdev);
841 	}
842 	if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &sbuf)) != 0)
843 		return (err);
844 	if (major(sbuf.st_rdev) != zfs_dev)
845 		goto nonemuioctl;
846 
847 	if (s10_uucopy((const void *)arg, &s10_param, sizeof (s10_param)) != 0)
848 		return (EFAULT);
849 
850 	bcopy((const void *)s10_param.zc_name, (void *)native_param.zc_name,
851 	    sizeof (s10_param.zc_name));
852 	bcopy((const void *)s10_param.zc_value, (void *)native_param.zc_value,
853 	    sizeof (s10_param.zc_value));
854 	bcopy((const void *)s10_param.zc_string, (void *)native_param.zc_string,
855 	    sizeof (s10_param.zc_string));
856 	struct_assign(native_param, s10_param, zc_guid);
857 	struct_assign(native_param, s10_param, zc_nvlist_conf);
858 	struct_assign(native_param, s10_param, zc_nvlist_conf_size);
859 	struct_assign(native_param, s10_param, zc_nvlist_src);
860 	struct_assign(native_param, s10_param, zc_nvlist_src_size);
861 	struct_assign(native_param, s10_param, zc_nvlist_dst);
862 	struct_assign(native_param, s10_param, zc_nvlist_dst_size);
863 	struct_assign(native_param, s10_param, zc_cookie);
864 	struct_assign(native_param, s10_param, zc_objset_type);
865 	struct_assign(native_param, s10_param, zc_perm_action);
866 	struct_assign(native_param, s10_param, zc_history);
867 	struct_assign(native_param, s10_param, zc_history_len);
868 	struct_assign(native_param, s10_param, zc_history_offset);
869 	struct_assign(native_param, s10_param, zc_obj);
870 	native_param.zc_iflags = 0;
871 	struct_assign(native_param, s10_param, zc_share);
872 	struct_assign(native_param, s10_param, zc_objset_stats);
873 	struct_assign(native_param, s10_param, zc_begin_record);
874 	struct_assign(native_param, s10_param, zc_inject_record);
875 
876 	err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, &native_param);
877 
878 	bcopy((const void *)native_param.zc_name, (void *)s10_param.zc_name,
879 	    sizeof (s10_param.zc_name));
880 	bcopy((const void *)native_param.zc_value, (void *)s10_param.zc_value,
881 	    sizeof (s10_param.zc_value));
882 	bcopy((const void *)native_param.zc_string, (void *)s10_param.zc_string,
883 	    sizeof (s10_param.zc_string));
884 	struct_assign(s10_param, native_param, zc_guid);
885 	struct_assign(s10_param, native_param, zc_nvlist_conf);
886 	struct_assign(s10_param, native_param, zc_nvlist_conf_size);
887 	struct_assign(s10_param, native_param, zc_nvlist_src);
888 	struct_assign(s10_param, native_param, zc_nvlist_src_size);
889 	struct_assign(s10_param, native_param, zc_nvlist_dst);
890 	struct_assign(s10_param, native_param, zc_nvlist_dst_size);
891 	struct_assign(s10_param, native_param, zc_cookie);
892 	struct_assign(s10_param, native_param, zc_objset_type);
893 	struct_assign(s10_param, native_param, zc_perm_action);
894 	struct_assign(s10_param, native_param, zc_history);
895 	struct_assign(s10_param, native_param, zc_history_len);
896 	struct_assign(s10_param, native_param, zc_history_offset);
897 	struct_assign(s10_param, native_param, zc_obj);
898 	struct_assign(s10_param, native_param, zc_share);
899 	struct_assign(s10_param, native_param, zc_objset_stats);
900 	struct_assign(s10_param, native_param, zc_begin_record);
901 	struct_assign(s10_param, native_param, zc_inject_record);
902 
903 	(void) s10_uucopy(&s10_param, (void *)arg, sizeof (s10_param));
904 	return (err);
905 
906 nonemuioctl:
907 	return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
908 }
909 
910 int
911 s10_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
912 {
913 	switch (cmd) {
914 	case CRYPTO_GET_FUNCTION_LIST:
915 		return (crypto_ioctl(rval, fdes, cmd, arg));
916 	case CT_TGET:
917 		/*FALLTHRU*/
918 	case CT_TSET:
919 		return (ctfs_ioctl(rval, fdes, cmd, arg));
920 	case MNTIOC_GETMNTENT:
921 		/*FALLTHRU*/
922 	case MNTIOC_GETEXTMNTENT:
923 		/*FALLTHRU*/
924 	case MNTIOC_GETMNTANY:
925 		return (mntfs_ioctl(rval, fdes, cmd, arg));
926 	}
927 
928 	if ((cmd & 0xff00) == ZFS_IOC)
929 		return (zfs_ioctl(rval, fdes, cmd, arg));
930 
931 	return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg));
932 }
933 
934 /*
935  * Unfortunately, pwrite()'s behavior differs between S10 and Nevada when
936  * applied to files opened with O_APPEND.  The offset argument is ignored and
937  * the buffer is appended to the target file in S10, whereas the current file
938  * position is ignored in Nevada (i.e., pwrite() acts as though the target file
939  * wasn't opened with O_APPEND).  This is a result of the fix for CR 6655660
940  * (pwrite() must ignore the O_APPEND/FAPPEND flag).
941  *
942  * We emulate the old S10 pwrite() behavior by checking whether the target file
943  * was opened with O_APPEND.  If it was, then invoke the write() system call
944  * instead of pwrite(); otherwise, invoke the pwrite() system call as usual.
945  */
946 static int
947 s10_pwrite(sysret_t *rval, int fd, const void *bufferp, size_t num_bytes,
948     off_t offset)
949 {
950 	int err;
951 
952 	if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0)
953 		return (err);
954 	if (rval->sys_rval1 & O_APPEND)
955 		return (__systemcall(rval, SYS_write + 1024, fd, bufferp,
956 		    num_bytes));
957 	return (__systemcall(rval, SYS_pwrite + 1024, fd, bufferp, num_bytes,
958 	    offset));
959 }
960 
961 #ifndef	_LP64
962 /*
963  * This is the large file version of the pwrite() system call for 32-bit
964  * processes.  This exists for the same reason that s10_pwrite() exists; see
965  * the comment above s10_pwrite().
966  */
967 static int
968 s10_pwrite64(sysret_t *rval, int fd, const void *bufferp, size32_t num_bytes,
969     uint32_t offset_1, uint32_t offset_2)
970 {
971 	int err;
972 
973 	if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0)
974 		return (err);
975 	if (rval->sys_rval1 & O_APPEND)
976 		return (__systemcall(rval, SYS_write + 1024, fd, bufferp,
977 		    num_bytes));
978 	return (__systemcall(rval, SYS_pwrite64 + 1024, fd, bufferp,
979 	    num_bytes, offset_1, offset_2));
980 }
981 #endif	/* !_LP64 */
982 
983 #define	S10_AC_PROC		(0x1 << 28)
984 #define	S10_AC_TASK		(0x2 << 28)
985 #define	S10_AC_FLOW		(0x4 << 28)
986 #define	S10_AC_MODE(x)		((x) & 0xf0000000)
987 #define	S10_AC_OPTION(x)	((x) & 0x0fffffff)
988 
989 /*
990  * The mode shift, mode mask and option mask for acctctl have changed.  The
991  * mode is currently the top full byte and the option is the lower 3 full bytes.
992  */
993 int
994 s10_acctctl(sysret_t *rval, int cmd, void *buf, size_t bufsz)
995 {
996 	int mode = S10_AC_MODE(cmd);
997 	int option = S10_AC_OPTION(cmd);
998 
999 	switch (mode) {
1000 	case S10_AC_PROC:
1001 		mode = AC_PROC;
1002 		break;
1003 	case S10_AC_TASK:
1004 		mode = AC_TASK;
1005 		break;
1006 	case S10_AC_FLOW:
1007 		mode = AC_FLOW;
1008 		break;
1009 	default:
1010 		return (S10_TRUSS_POINT_3(rval, SYS_acctctl, EINVAL, cmd, buf,
1011 		    bufsz));
1012 	}
1013 
1014 	return (__systemcall(rval, SYS_acctctl + 1024, mode | option, buf,
1015 	    bufsz));
1016 }
1017 
1018 /*
1019  * The Audit Policy parameters have changed due to:
1020  *    6466722 audituser and AUDIT_USER are defined, unused, undocumented and
1021  *            should be removed.
1022  *
1023  * In S10 we had the following flag:
1024  *	#define AUDIT_USER 0x0040
1025  * which doesn't exist in Solaris Next where the subsequent flags are shifted
1026  * down.  For example, in S10 we had:
1027  *	#define AUDIT_GROUP     0x0080
1028  * but on Solaris Next we have:
1029  *	#define AUDIT_GROUP     0x0040
1030  * AUDIT_GROUP has the value AUDIT_USER had in S10 and all of the subsequent
1031  * bits are also shifted one place.
1032  *
1033  * When we're getting or setting the Audit Policy parameters we need to
1034  * shift the outgoing or incoming bits into their proper positions.  Since
1035  * S10_AUDIT_USER was always unused, we always clear that bit on A_GETPOLICY.
1036  *
1037  * The command we care about, BSM_AUDITCTL, passes the most parameters (3),
1038  * so declare this function to take up to 4 args and just pass them on.
1039  * The number of parameters for s10_auditsys needs to be equal to the BSM_*
1040  * subcommand that has the most parameters, since we want to pass all
1041  * parameters through, regardless of which subcommands we interpose on.
1042  *
1043  * Note that the auditsys system call uses the SYSENT_AP macro wrapper instead
1044  * of the more common SYSENT_CI macro.  This means the return value is a
1045  * SE_64RVAL so the syscall table uses RV_64RVAL.
1046  */
1047 
1048 #define	S10_AUDIT_HMASK	0xffffffc0
1049 #define	S10_AUDIT_LMASK	0x3f
1050 
1051 int
1052 s10_auditsys(sysret_t *rval, int bsmcmd, intptr_t a0, intptr_t a1, intptr_t a2)
1053 {
1054 	int	err;
1055 	uint_t	m;
1056 
1057 	if (bsmcmd != BSM_AUDITCTL)
1058 		return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, a1,
1059 		    a2));
1060 
1061 	if ((int)a0 == A_GETPOLICY) {
1062 		if ((err = __systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0,
1063 		    &m, a2)) != 0)
1064 			return (err);
1065 		m = ((m & S10_AUDIT_HMASK) << 1) | (m & S10_AUDIT_LMASK);
1066 		if (s10_uucopy(&m, (void *)a1, sizeof (m)) != 0)
1067 			return (EFAULT);
1068 		return (0);
1069 
1070 	} else if ((int)a0 == A_SETPOLICY) {
1071 		if (s10_uucopy((const void *)a1, &m, sizeof (m)) != 0)
1072 			return (EFAULT);
1073 		m = ((m >> 1) & S10_AUDIT_HMASK) | (m & S10_AUDIT_LMASK);
1074 		return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, &m,
1075 		    a2));
1076 	}
1077 
1078 	return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, a1, a2));
1079 }
1080 
1081 /*
1082  * Determine whether the executable passed to SYS_exec or SYS_execve is a
1083  * native executable.  The s10_npreload.so invokes the B_S10_NATIVE brand
1084  * operation which patches up the processes exec info to eliminate any trace
1085  * of the wrapper.  That will make pgrep and other commands that examine
1086  * process' executable names and command-line parameters work properly.
1087  */
1088 static int
1089 s10_exec_native(sysret_t *rval, const char *fname, const char **argp,
1090     const char **envp)
1091 {
1092 	const char *filename = fname;
1093 	char path[64];
1094 	int err;
1095 
1096 	/* Get a copy of the executable we're trying to run */
1097 	path[0] = '\0';
1098 	(void) s10_uucopystr(filename, path, sizeof (path));
1099 
1100 	/* Check if we're trying to run a native binary */
1101 	if (strncmp(path, "/.SUNWnative/usr/lib/brand/solaris10/s10_native",
1102 	    sizeof (path)) != 0)
1103 		return (0);
1104 
1105 	/* Skip the first element in the argv array */
1106 	argp++;
1107 
1108 	/*
1109 	 * The the path of the dynamic linker is the second parameter
1110 	 * of s10_native_exec().
1111 	 */
1112 	if (s10_uucopy(argp, &filename, sizeof (char *)) != 0)
1113 		return (EFAULT);
1114 
1115 	/* If an exec call succeeds, it never returns */
1116 	err = __systemcall(rval, SYS_brand + 1024, B_EXEC_NATIVE, filename,
1117 	    argp, envp, NULL, NULL, NULL);
1118 	s10_assert(err != 0);
1119 	return (err);
1120 }
1121 
1122 /*
1123  * Interpose on the SYS_exec syscall to detect native wrappers.
1124  */
1125 int
1126 s10_exec(sysret_t *rval, const char *fname, const char **argp)
1127 {
1128 	int err;
1129 
1130 	if ((err = s10_exec_native(rval, fname, argp, NULL)) != 0)
1131 		return (err);
1132 
1133 	/* If an exec call succeeds, it never returns */
1134 	err = __systemcall(rval, SYS_exec + 1024, fname, argp);
1135 	s10_assert(err != 0);
1136 	return (err);
1137 }
1138 
1139 /*
1140  * Interpose on the SYS_execve syscall to detect native wrappers.
1141  */
1142 int
1143 s10_execve(sysret_t *rval, const char *fname, const char **argp,
1144     const char **envp)
1145 {
1146 	int err;
1147 
1148 	if ((err = s10_exec_native(rval, fname, argp, envp)) != 0)
1149 		return (err);
1150 
1151 	/* If an exec call succeeds, it never returns */
1152 	err = __systemcall(rval, SYS_execve + 1024, fname, argp, envp);
1153 	s10_assert(err != 0);
1154 	return (err);
1155 }
1156 
1157 /*
1158  * S10's issetugid() syscall is now a subcode to privsys().
1159  */
1160 static int
1161 s10_issetugid(sysret_t *rval)
1162 {
1163 	return (__systemcall(rval, SYS_privsys + 1024, PRIVSYS_ISSETUGID,
1164 	    0, 0, 0, 0, 0));
1165 }
1166 
1167 /*
1168  * New last arg "block" flag should be zero.  The block flag is used by
1169  * the Opensolaris AIO implementation, which is now part of libc.
1170  */
1171 static int
1172 s10_sigqueue(sysret_t *rval, pid_t pid, int signo, void *value, int si_code)
1173 {
1174 	return (__systemcall(rval, SYS_sigqueue + 1024, pid, signo, value,
1175 	    si_code, 0));
1176 }
1177 
1178 static long
1179 s10_uname(sysret_t *rv, uintptr_t p1)
1180 {
1181 	struct utsname un, *unp = (struct utsname *)p1;
1182 	int rev, err;
1183 
1184 	if ((err = __systemcall(rv, SYS_uname + 1024, &un)) != 0)
1185 		return (err);
1186 
1187 	rev = atoi(&un.release[2]);
1188 	s10_assert(rev >= 11);
1189 	bzero(un.release, _SYS_NMLN);
1190 	(void) strlcpy(un.release, S10_UTS_RELEASE, _SYS_NMLN);
1191 	bzero(un.version, _SYS_NMLN);
1192 	(void) strlcpy(un.version, S10_UTS_VERSION, _SYS_NMLN);
1193 
1194 	/* copy out the modified uname info */
1195 	return (s10_uucopy(&un, unp, sizeof (un)));
1196 }
1197 
1198 int
1199 s10_sysinfo(sysret_t *rv, int command, char *buf, long count)
1200 {
1201 	char *value;
1202 	int len;
1203 
1204 	/*
1205 	 * We must interpose on the sysinfo(2) commands SI_RELEASE and
1206 	 * SI_VERSION; all others get passed to the native sysinfo(2)
1207 	 * command.
1208 	 */
1209 	switch (command) {
1210 		case SI_RELEASE:
1211 			value = S10_UTS_RELEASE;
1212 			break;
1213 
1214 		case SI_VERSION:
1215 			value = S10_UTS_VERSION;
1216 			break;
1217 
1218 		default:
1219 			/*
1220 			 * The default action is to pass the command to the
1221 			 * native sysinfo(2) syscall.
1222 			 */
1223 			return (__systemcall(rv, SYS_systeminfo + 1024,
1224 			    command, buf, count));
1225 	}
1226 
1227 	len = strlen(value) + 1;
1228 	if (count > 0) {
1229 		if (s10_uucopystr(value, buf, count) != 0)
1230 			return (EFAULT);
1231 
1232 		/* Assure NULL termination of buf as s10_uucopystr() doesn't. */
1233 		if (len > count && s10_uucopy("\0", buf + (count - 1), 1) != 0)
1234 			return (EFAULT);
1235 	}
1236 
1237 	/*
1238 	 * On success, sysinfo(2) returns the size of buffer required to hold
1239 	 * the complete value plus its terminating NULL byte.
1240 	 */
1241 	(void) S10_TRUSS_POINT_3(rv, SYS_systeminfo, 0, command, buf, count);
1242 	rv->sys_rval1 = len;
1243 	rv->sys_rval2 = 0;
1244 	return (0);
1245 }
1246 
1247 #ifdef	__x86
1248 #ifdef	__amd64
1249 /*
1250  * 64-bit x86 LWPs created by SYS_lwp_create start here if they need to set
1251  * their %fs registers to the legacy Solaris 10 selector value.
1252  *
1253  * This function does three things:
1254  *
1255  *	1.  Trap to the kernel so that it can set %fs to the legacy Solaris 10
1256  *	    selector value.
1257  *	2.  Read the LWP's true entry point (the entry point supplied by libc
1258  *	    when SYS_lwp_create was invoked) from %r14.
1259  *	3.  Eliminate this function's stack frame and pass control to the LWP's
1260  *	    true entry point.
1261  *
1262  * See the comment above s10_lwp_create_correct_fs() (see below) for the reason
1263  * why this function exists.
1264  */
1265 /*ARGSUSED*/
1266 static void
1267 s10_lwp_create_entry_point(void *ulwp_structp)
1268 {
1269 	sysret_t rval;
1270 
1271 	/*
1272 	 * The new LWP's %fs register is initially zero, but libc won't
1273 	 * function correctly when %fs is zero.  Change the LWP's %fs register
1274 	 * via SYS_brand.
1275 	 */
1276 	(void) __systemcall(&rval, SYS_brand + 1024, B_S10_FSREGCORRECTION);
1277 
1278 	/*
1279 	 * Jump to the true entry point, which is stored in %r14.
1280 	 * Remove our stack frame before jumping so that
1281 	 * s10_lwp_create_entry_point() won't be seen in stack traces.
1282 	 *
1283 	 * NOTE: s10_lwp_create_entry_point() pushes %r12 onto its stack frame
1284 	 * so that it can use it as a temporary register.  We don't restore %r12
1285 	 * in this assembly block because we don't care about its value (and
1286 	 * neither does _lwp_start()).  Besides, the System V ABI AMD64
1287 	 * Actirecture Processor Supplement doesn't specify that %r12 should
1288 	 * have a special value when LWPs start, so we can ignore its value when
1289 	 * we jump to the true entry point.  Furthermore, %r12 is a callee-saved
1290 	 * register, so the true entry point should push %r12 onto its stack
1291 	 * before using the register.  We ignore %r14 after we read it for
1292 	 * similar reasons.
1293 	 *
1294 	 * NOTE: The compiler will generate a function epilogue for this
1295 	 * function despite the fact that the LWP will never execute it.
1296 	 * We could hand-code this entire function in assembly to eliminate
1297 	 * the epilogue, but the epilogue is only three or four instructions,
1298 	 * so we wouldn't save much space.  Besides, why would we want
1299 	 * to create yet another ugly, hard-to-maintain assembly function when
1300 	 * we could write most of it in C?
1301 	 */
1302 	__asm__ __volatile__(
1303 	    "movq %0, %%rdi\n\t"	/* pass ulwp_structp as arg1 */
1304 	    "movq %%rbp, %%rsp\n\t"	/* eliminate the stack frame */
1305 	    "popq %%rbp\n\t"
1306 	    "jmp *%%r14\n\t"		/* jump to the true entry point */
1307 	    : : "r" (ulwp_structp));
1308 	/*NOTREACHED*/
1309 }
1310 
1311 /*
1312  * The S10 libc expects that %fs will be nonzero for new 64-bit x86 LWPs but the
1313  * Nevada kernel clears %fs for such LWPs.  Unforunately, new LWPs do not issue
1314  * SYS_lwp_private (see s10_lwp_private() below) after they are created, so
1315  * we must ensure that new LWPs invoke a brand operation that sets %fs to a
1316  * nonzero value immediately after their creation.
1317  *
1318  * The easiest way to do this is to make new LWPs start at a special function,
1319  * s10_lwp_create_entry_point() (see its definition above), that invokes the
1320  * brand operation that corrects %fs.  We'll store the entry points of new LWPs
1321  * in their %r14 registers so that s10_lwp_create_entry_point() can find and
1322  * call them after invoking the special brand operation.  %r14 is a callee-saved
1323  * register; therefore, any functions invoked by s10_lwp_create_entry_point()
1324  * and all functions dealing with signals (e.g., sigacthandler()) will preserve
1325  * %r14 for s10_lwp_create_entry_point().
1326  *
1327  * The Nevada kernel can safely work with nonzero %fs values because the kernel
1328  * configures per-thread %fs segment descriptors so that the legacy %fs selector
1329  * value will still work.  See the comment in lwp_load() regarding %fs and
1330  * %fsbase in 64-bit x86 processes.
1331  *
1332  * This emulation exists thanks to CRs 6467491 and 6501650.
1333  */
1334 static int
1335 s10_lwp_create_correct_fs(sysret_t *rval, ucontext_t *ucp, int flags,
1336     id_t *new_lwp)
1337 {
1338 	ucontext_t s10_uc;
1339 
1340 	/*
1341 	 * Copy the supplied ucontext_t structure to the local stack
1342 	 * frame and store the new LWP's entry point (the value of %rip
1343 	 * stored in the ucontext_t) in the new LWP's %r14 register.
1344 	 * Then make s10_lwp_create_entry_point() the new LWP's entry
1345 	 * point.
1346 	 */
1347 	if (s10_uucopy(ucp, &s10_uc, sizeof (s10_uc)) != 0)
1348 		return (EFAULT);
1349 	s10_uc.uc_mcontext.gregs[REG_R14] = s10_uc.uc_mcontext.gregs[REG_RIP];
1350 	s10_uc.uc_mcontext.gregs[REG_RIP] = (greg_t)s10_lwp_create_entry_point;
1351 
1352 	/*
1353 	 * Issue SYS_lwp_create to create the new LWP.  We pass the
1354 	 * modified ucontext_t to make sure that the new LWP starts at
1355 	 * s10_lwp_create_entry_point().
1356 	 */
1357 	return (__systemcall(rval, SYS_lwp_create + 1024, &s10_uc,
1358 	    flags, new_lwp));
1359 }
1360 #endif	/* __amd64 */
1361 
1362 /*
1363  * This function is invoked on x86 systems when SYS_lwp_create is issued but no
1364  * %fs register correction is necessary.
1365  *
1366  * See the comment above s10_lwp_create_correct_fs() above for more details.
1367  */
1368 static int
1369 s10_lwp_create(sysret_t *rval, ucontext_t *ucp, int flags, id_t *new_lwp)
1370 {
1371 	return (__systemcall(rval, SYS_lwp_create + 1024, ucp, flags, new_lwp));
1372 }
1373 
1374 /*
1375  * SYS_lwp_private is issued by libc_init() to set %fsbase in 64-bit x86
1376  * processes.  The Nevada kernel sets %fs to zero but the S10 libc expects
1377  * %fs to be nonzero.  We'll pass the issued system call to the kernel untouched
1378  * and invoke a brand operation to set %fs to the legacy S10 selector value.
1379  *
1380  * This emulation exists thanks to CRs 6467491 and 6501650.
1381  */
1382 static int
1383 s10_lwp_private(sysret_t *rval, int cmd, int which, uintptr_t base)
1384 {
1385 #ifdef	__amd64
1386 	int err;
1387 
1388 	/*
1389 	 * The current LWP's %fs register should be zero.  Determine whether the
1390 	 * Solaris 10 libc with which we're working functions correctly when %fs
1391 	 * is zero by calling thr_main() after issuing the SYS_lwp_private
1392 	 * syscall.  If thr_main() barfs (returns -1), then change the LWP's %fs
1393 	 * register via SYS_brand and patch s10_sysent_table so that issuing
1394 	 * SYS_lwp_create executes s10_lwp_create_correct_fs() rather than the
1395 	 * default s10_lwp_create().  s10_lwp_create_correct_fs() will
1396 	 * guarantee that new LWPs will have correct %fs values.
1397 	 */
1398 	if ((err = __systemcall(rval, SYS_lwp_private + 1024, cmd, which,
1399 	    base)) != 0)
1400 		return (err);
1401 	if (thr_main() == -1) {
1402 		/*
1403 		 * SYS_lwp_private is only issued by libc_init(), which is
1404 		 * executed when libc is first loaded by ld.so.1.  Thus we
1405 		 * are guaranteed to be single-threaded at this point.  Even
1406 		 * if we were multithreaded at this point, writing a 64-bit
1407 		 * value to the st_callc field of a s10_sysent_table
1408 		 * entry is guaranteed to be atomic on 64-bit x86 chips
1409 		 * as long as the field is not split across cache lines
1410 		 * (It shouldn't be.).  See chapter 8, section 1.1 of
1411 		 * "The Intel 64 and IA32 Architectures Software Developer's
1412 		 * Manual," Volume 3A for more details.
1413 		 */
1414 		s10_sysent_table[SYS_lwp_create].st_callc =
1415 		    (sysent_cb_t)s10_lwp_create_correct_fs;
1416 		return (__systemcall(rval, SYS_brand + 1024,
1417 		    B_S10_FSREGCORRECTION));
1418 	}
1419 	return (0);
1420 #else	/* !__amd64 */
1421 	return (__systemcall(rval, SYS_lwp_private + 1024, cmd, which, base));
1422 #endif	/* !__amd64 */
1423 }
1424 #endif	/* __x86 */
1425 
1426 /*
1427  * The Opensolaris versions of lwp_mutex_timedlock() and lwp_mutex_trylock()
1428  * add an extra argument to the interfaces, a uintptr_t value for the mutex's
1429  * mutex_owner field.  The Solaris 10 libc assigns the mutex_owner field at
1430  * user-level, so we just make the extra argument be zero in both syscalls.
1431  */
1432 
1433 static int
1434 s10_lwp_mutex_timedlock(sysret_t *rval, lwp_mutex_t *lp, timespec_t *tsp)
1435 {
1436 	return (__systemcall(rval, SYS_lwp_mutex_timedlock + 1024, lp, tsp, 0));
1437 }
1438 
1439 static int
1440 s10_lwp_mutex_trylock(sysret_t *rval, lwp_mutex_t *lp)
1441 {
1442 	return (__systemcall(rval, SYS_lwp_mutex_trylock + 1024, lp, 0));
1443 }
1444 
1445 /*
1446  * If the emul_global_zone flag is set then emulate some aspects of the
1447  * zone system call.  In particular, emulate the global zone ID on the
1448  * ZONE_LOOKUP subcommand and emulate some of the global zone attributes
1449  * on the ZONE_GETATTR subcommand.  If the flag is not set or we're performing
1450  * some other operation, simply pass the calls through.
1451  */
1452 int
1453 s10_zone(sysret_t *rval, int cmd, void *arg1, void *arg2, void *arg3,
1454     void *arg4)
1455 {
1456 	char		*aval;
1457 	int		len;
1458 	zoneid_t	zid;
1459 	int		attr;
1460 	char		*buf;
1461 	size_t		bufsize;
1462 
1463 	/*
1464 	 * We only emulate the zone syscall for a subset of specific commands,
1465 	 * otherwise we just pass the call through.
1466 	 */
1467 	if (!emul_global_zone)
1468 		return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2,
1469 		    arg3, arg4));
1470 
1471 	switch (cmd) {
1472 	case ZONE_LOOKUP:
1473 		(void) S10_TRUSS_POINT_1(rval, SYS_zone, 0, cmd);
1474 		rval->sys_rval1 = GLOBAL_ZONEID;
1475 		rval->sys_rval2 = 0;
1476 		return (0);
1477 
1478 	case ZONE_GETATTR:
1479 		zid = (zoneid_t)(uintptr_t)arg1;
1480 		attr = (int)(uintptr_t)arg2;
1481 		buf = (char *)arg3;
1482 		bufsize = (size_t)arg4;
1483 
1484 		/*
1485 		 * If the request is for the global zone then we're emulating
1486 		 * that, otherwise pass this thru.
1487 		 */
1488 		if (zid != GLOBAL_ZONEID)
1489 			goto passthru;
1490 
1491 		switch (attr) {
1492 		case ZONE_ATTR_NAME:
1493 			aval = GLOBAL_ZONENAME;
1494 			break;
1495 
1496 		case ZONE_ATTR_BRAND:
1497 			aval = NATIVE_BRAND_NAME;
1498 			break;
1499 		default:
1500 			/*
1501 			 * We only emulate a subset of the attrs, use the
1502 			 * real zone id to pass thru the rest.
1503 			 */
1504 			arg1 = (void *)(uintptr_t)zoneid;
1505 			goto passthru;
1506 		}
1507 
1508 		(void) S10_TRUSS_POINT_5(rval, SYS_zone, 0, cmd, zid, attr,
1509 		    buf, bufsize);
1510 
1511 		len = strlen(aval) + 1;
1512 		if (len > bufsize)
1513 			return (ENAMETOOLONG);
1514 
1515 		if (buf != NULL) {
1516 			if (len == 1) {
1517 				if (s10_uucopy("\0", buf, 1) != 0)
1518 					return (EFAULT);
1519 			} else {
1520 				if (s10_uucopystr(aval, buf, len) != 0)
1521 					return (EFAULT);
1522 
1523 				/*
1524 				 * Assure NULL termination of "buf" as
1525 				 * s10_uucopystr() does NOT.
1526 				 */
1527 				if (s10_uucopy("\0", buf + (len - 1), 1) != 0)
1528 					return (EFAULT);
1529 			}
1530 		}
1531 
1532 		rval->sys_rval1 = len;
1533 		rval->sys_rval2 = 0;
1534 		return (0);
1535 
1536 	default:
1537 		break;
1538 	}
1539 
1540 passthru:
1541 	return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2, arg3,
1542 	    arg4));
1543 }
1544 
1545 /*
1546  * Close a libc file handle, but don't actually close the underlying
1547  * file descriptor.
1548  */
1549 static void
1550 s10_close_fh(FILE *file)
1551 {
1552 	int fd, fd_new;
1553 
1554 	if (file == NULL)
1555 		return;
1556 
1557 	if ((fd = fileno(file)) < 0)
1558 		return;
1559 
1560 	fd_new = dup(fd);
1561 	if (fd_new == -1)
1562 		return;
1563 
1564 	(void) fclose(file);
1565 	(void) dup2(fd_new, fd);
1566 	(void) close(fd_new);
1567 }
1568 
1569 /*ARGSUSED*/
1570 int
1571 s10_init(int argc, char *argv[], char *envp[])
1572 {
1573 	sysret_t		rval;
1574 	s10_brand_reg_t		reg;
1575 	s10_elf_data_t		sed;
1576 	auxv_t			*ap;
1577 	uintptr_t		*p;
1578 	int			i, err;
1579 	char			*bname;
1580 
1581 	/* Sanity check our translation table return value codes */
1582 	for (i = 0; i < NSYSCALL; i++) {
1583 		s10_sysent_table_t *est = &(s10_sysent_table[i]);
1584 		s10_assert(BIT_ONLYONESET(est->st_args & RV_MASK));
1585 	}
1586 
1587 	/*
1588 	 * We need to shutdown all libc stdio.  libc stdio normally goes to
1589 	 * file descriptors, but since we're actually part of a another
1590 	 * process we don't own these file descriptors and we can't make
1591 	 * any assumptions about their state.
1592 	 */
1593 	s10_close_fh(stdin);
1594 	s10_close_fh(stdout);
1595 	s10_close_fh(stderr);
1596 
1597 	/*
1598 	 * Cache the pid of the zone's init process and determine if
1599 	 * we're init(1m) for the zone.  Remember: we might be init
1600 	 * now, but as soon as we fork(2) we won't be.
1601 	 */
1602 	(void) get_initpid_info();
1603 
1604 	/* get the current zoneid */
1605 	err = __systemcall(&rval, SYS_zone, ZONE_LOOKUP, NULL);
1606 	s10_assert(err == 0);
1607 	zoneid = (zoneid_t)rval.sys_rval1;
1608 
1609 	/* Get the zone's emulation bitmap. */
1610 	if ((err = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
1611 	    S10_EMUL_BITMAP, emul_bitmap, sizeof (emul_bitmap))) != 0) {
1612 		s10_abort(err, "The zone's patch level is unsupported");
1613 		/*NOTREACHED*/
1614 	}
1615 
1616 	bname = basename(argv[0]);
1617 
1618 	/*
1619 	 * In general we want the S10 commands that are zone-aware to continue
1620 	 * to behave as they normally do within a zone.  Since these commands
1621 	 * are zone-aware, they should continue to "do the right thing".
1622 	 * However, some zone-aware commands aren't going to work the way
1623 	 * we expect them to inside the branded zone.  In particular, the pkg
1624 	 * and patch commands will not properly manage all pkgs/patches
1625 	 * unless the commands think they are running in the global zone.  For
1626 	 * these commands we want to emulate the global zone.
1627 	 *
1628 	 * We don't do any emulation for pkgcond since it is typically used
1629 	 * in pkg/patch postinstall scripts and we want those scripts to do
1630 	 * the right thing inside a zone.
1631 	 *
1632 	 * One issue is the handling of hollow pkgs.  Since the pkgs are
1633 	 * hollow, they won't use pkgcond in their postinstall scripts.  These
1634 	 * pkgs typically are installing drivers so we handle that by
1635 	 * replacing add_drv and rem_drv in the s10_boot script.
1636 	 */
1637 	if (strcmp("pkgadd", bname) == 0 || strcmp("pkgrm", bname) == 0 ||
1638 	    strcmp("patchadd", bname) == 0 || strcmp("patchrm", bname) == 0)
1639 		emul_global_zone = B_TRUE;
1640 
1641 	/*
1642 	 * Register our syscall emulation table with the kernel.
1643 	 * Note that we don't have to do invoke (syscall_number + 1024)
1644 	 * until we've actually establised a syscall emulation callback
1645 	 * handler address, which is what we're doing with this brand
1646 	 * syscall.
1647 	 */
1648 	reg.sbr_version = S10_VERSION;
1649 	reg.sbr_handler = (caddr_t)s10_handler;
1650 	if ((err = __systemcall(&rval, SYS_brand, B_REGISTER, &reg)) != 0) {
1651 		s10_abort(err, "Failed to brand current process");
1652 		/*NOTREACHED*/
1653 	}
1654 
1655 	/* Get data about the executable we're running from the kernel. */
1656 	if ((err = __systemcall(&rval, SYS_brand + 1024,
1657 	    B_ELFDATA, (void *)&sed)) != 0) {
1658 		s10_abort(err,
1659 		    "Failed to get required brand ELF data from the kernel");
1660 		/*NOTREACHED*/
1661 	}
1662 
1663 	/*
1664 	 * Find the aux vector on the stack.
1665 	 */
1666 	p = (uintptr_t *)envp;
1667 	while (*p != NULL)
1668 		p++;
1669 
1670 	/*
1671 	 * p is now pointing at the 0 word after the environ pointers.
1672 	 * After that is the aux vectors.
1673 	 *
1674 	 * The aux vectors are currently pointing to the brand emulation
1675 	 * library and associated linker.  We're going to change them to
1676 	 * point to the brand executable and associated linker (or to no
1677 	 * linker for static binaries).  This matches the process data
1678 	 * stored within the kernel and visible from /proc, which was
1679 	 * all setup in s10_elfexec().  We do this so that when a debugger
1680 	 * attaches to the process it sees the process as a normal solaris
1681 	 * process, this brand emulation library and everything on it's
1682 	 * link map will not be visible, unless our librtld_db plugin
1683 	 * is used.  Note that this is very different from how Linux
1684 	 * branded processes are implemented within lx branded zones.
1685 	 * In that situation, the primary linkmap of the process is the
1686 	 * brand emulation libraries linkmap, not the Linux applications
1687 	 * linkmap.
1688 	 *
1689 	 * We also need to clear the AF_SUN_NOPLM flag from the AT_SUN_AUXFLAGS
1690 	 * aux vector.  This flag told our linker that we don't have a
1691 	 * primary link map.  Now that our linker is done initializing, we
1692 	 * want to clear this flag before we transfer control to the
1693 	 * applications copy of the linker, since we want that linker to have
1694 	 * a primary link map which will be the link map for the application
1695 	 * we're running.
1696 	 */
1697 	p++;
1698 	for (ap = (auxv_t *)p; ap->a_type != AT_NULL; ap++) {
1699 		switch (ap->a_type) {
1700 			case AT_BASE:
1701 				/* Hide AT_BASE if static binary */
1702 				if (sed.sed_base == NULL) {
1703 					ap->a_type = AT_IGNORE;
1704 					ap->a_un.a_val = NULL;
1705 				} else {
1706 					ap->a_un.a_val = sed.sed_base;
1707 				}
1708 				break;
1709 			case AT_ENTRY:
1710 				ap->a_un.a_val = sed.sed_entry;
1711 				break;
1712 			case AT_PHDR:
1713 				ap->a_un.a_val = sed.sed_phdr;
1714 				break;
1715 			case AT_PHENT:
1716 				ap->a_un.a_val = sed.sed_phent;
1717 				break;
1718 			case AT_PHNUM:
1719 				ap->a_un.a_val = sed.sed_phnum;
1720 				break;
1721 			case AT_SUN_AUXFLAGS:
1722 				ap->a_un.a_val &= ~AF_SUN_NOPLM;
1723 				break;
1724 			case AT_SUN_EMULATOR:
1725 				/*
1726 				 * ld.so.1 inspects AT_SUN_EMULATOR to see if
1727 				 * if it is the linker for the brand emulation
1728 				 * library.  Hide AT_SUN_EMULATOR, as the
1729 				 * linker we are about to jump to is the linker
1730 				 * for the binary.
1731 				 */
1732 				ap->a_type = AT_IGNORE;
1733 				ap->a_un.a_val = NULL;
1734 				break;
1735 			case AT_SUN_LDDATA:
1736 				/* Hide AT_SUN_LDDATA if static binary */
1737 				if (sed.sed_lddata == NULL) {
1738 					ap->a_type = AT_IGNORE;
1739 					ap->a_un.a_val = NULL;
1740 				} else {
1741 					ap->a_un.a_val = sed.sed_lddata;
1742 				}
1743 				break;
1744 			default:
1745 				break;
1746 		}
1747 	}
1748 
1749 	s10_runexe(argv, sed.sed_ldentry);
1750 	/*NOTREACHED*/
1751 	s10_abort(0, "s10_runexe() returned");
1752 	return (-1);
1753 }
1754 
1755 /*
1756  * This table must have at least NSYSCALL entries in it.
1757  *
1758  * The second parameter of each entry in the s10_sysent_table
1759  * contains the number of parameters and flags that describe the
1760  * syscall return value encoding.  See the block comments at the
1761  * top of this file for more information about the syscall return
1762  * value flags and when they should be used.
1763  */
1764 s10_sysent_table_t s10_sysent_table[] = {
1765 #if defined(__sparc) && !defined(__sparcv9)
1766 	EMULATE(s10_indir, 9 | RV_64RVAL),	/*  0 */
1767 #else /* !__sparc || __sparcv9 */
1768 	NOSYS,					/*  0 */
1769 #endif /* !__sparc || __sparcv9 */
1770 	NOSYS,					/*   1 */
1771 	NOSYS,					/*   2 */
1772 	NOSYS,					/*   3 */
1773 	NOSYS,					/*   4 */
1774 	NOSYS,					/*   5 */
1775 	NOSYS,					/*   6 */
1776 	NOSYS,					/*   7 */
1777 	NOSYS,					/*   8 */
1778 	NOSYS,					/*   9 */
1779 	NOSYS,					/*  10 */
1780 	EMULATE(s10_exec, 2 | RV_DEFAULT),	/*  11 */
1781 	NOSYS,					/*  12 */
1782 	NOSYS,					/*  13 */
1783 	NOSYS,					/*  14 */
1784 	NOSYS,					/*  15 */
1785 	NOSYS,					/*  16 */
1786 	NOSYS,					/*  17 */
1787 	NOSYS,					/*  18 */
1788 	NOSYS,					/*  19 */
1789 	NOSYS,					/*  20 */
1790 	NOSYS,					/*  21 */
1791 	NOSYS,					/*  22 */
1792 	NOSYS,					/*  23 */
1793 	NOSYS,					/*  24 */
1794 	NOSYS,					/*  25 */
1795 	NOSYS,					/*  26 */
1796 	NOSYS,					/*  27 */
1797 	NOSYS,					/*  28 */
1798 	NOSYS,					/*  29 */
1799 	NOSYS,					/*  30 */
1800 	NOSYS,					/*  31 */
1801 	NOSYS,					/*  32 */
1802 	NOSYS,					/*  33 */
1803 	NOSYS,					/*  34 */
1804 	NOSYS,					/*  35 */
1805 	NOSYS,					/*  36 */
1806 	NOSYS,					/*  37 */
1807 	NOSYS,					/*  38 */
1808 	NOSYS,					/*  39 */
1809 	NOSYS,					/*  40 */
1810 	NOSYS,					/*  41 */
1811 	NOSYS,					/*  42 */
1812 	NOSYS,					/*  43 */
1813 	NOSYS,					/*  44 */
1814 	NOSYS,					/*  45 */
1815 	NOSYS,					/*  46 */
1816 	NOSYS,					/*  47 */
1817 	NOSYS,					/*  48 */
1818 	NOSYS,					/*  49 */
1819 	NOSYS,					/*  50 */
1820 	NOSYS,					/*  51 */
1821 	NOSYS,					/*  52 */
1822 	NOSYS,					/*  53 */
1823 	EMULATE(s10_ioctl, 3 | RV_DEFAULT),	/*  54 */
1824 	NOSYS,					/*  55 */
1825 	NOSYS,					/*  56 */
1826 	NOSYS,					/*  57 */
1827 	NOSYS,					/*  58 */
1828 	EMULATE(s10_execve, 3 | RV_DEFAULT),	/*  59 */
1829 	NOSYS,					/*  60 */
1830 	NOSYS,					/*  61 */
1831 	NOSYS,					/*  62 */
1832 	NOSYS,					/*  63 */
1833 	NOSYS,					/*  64 */
1834 	NOSYS,					/*  65 */
1835 	NOSYS,					/*  66 */
1836 	NOSYS,					/*  67 */
1837 	NOSYS,					/*  68 */
1838 	NOSYS,					/*  69 */
1839 	NOSYS,					/*  70 */
1840 	EMULATE(s10_acctctl, 3 | RV_DEFAULT),	/*  71 */
1841 	NOSYS,					/*  72 */
1842 	NOSYS,					/*  73 */
1843 	NOSYS,					/*  74 */
1844 	EMULATE(s10_issetugid, 0 | RV_DEFAULT),	/*  75 */
1845 	NOSYS,					/*  76 */
1846 	NOSYS,					/*  77 */
1847 	NOSYS,					/*  78 */
1848 	NOSYS,					/*  79 */
1849 	NOSYS,					/*  80 */
1850 	NOSYS,					/*  81 */
1851 	NOSYS,					/*  82 */
1852 	NOSYS,					/*  83 */
1853 	NOSYS,					/*  84 */
1854 	NOSYS,					/*  85 */
1855 	NOSYS,					/*  86 */
1856 	NOSYS,					/*  87 */
1857 	NOSYS,					/*  88 */
1858 	NOSYS,					/*  89 */
1859 	NOSYS,					/*  90 */
1860 	NOSYS,					/*  91 */
1861 	NOSYS,					/*  92 */
1862 	NOSYS,					/*  93 */
1863 	NOSYS,					/*  94 */
1864 	NOSYS,					/*  95 */
1865 	NOSYS,					/*  96 */
1866 	NOSYS,					/*  97 */
1867 	NOSYS,					/*  98 */
1868 	NOSYS,					/*  99 */
1869 	NOSYS,					/* 100 */
1870 	NOSYS,					/* 101 */
1871 	NOSYS,					/* 102 */
1872 	NOSYS,					/* 103 */
1873 	NOSYS,					/* 104 */
1874 	NOSYS,					/* 105 */
1875 	NOSYS,					/* 106 */
1876 	NOSYS,					/* 107 */
1877 	NOSYS,					/* 108 */
1878 	NOSYS,					/* 109 */
1879 	NOSYS,					/* 110 */
1880 	NOSYS,					/* 111 */
1881 	NOSYS,					/* 112 */
1882 	NOSYS,					/* 113 */
1883 	NOSYS,					/* 114 */
1884 	NOSYS,					/* 115 */
1885 	NOSYS,					/* 116 */
1886 	NOSYS,					/* 117 */
1887 	NOSYS,					/* 118 */
1888 	NOSYS,					/* 119 */
1889 	NOSYS,					/* 120 */
1890 	NOSYS,					/* 121 */
1891 	NOSYS,					/* 122 */
1892 	NOSYS,					/* 123 */
1893 	NOSYS,					/* 124 */
1894 	NOSYS,					/* 125 */
1895 	NOSYS,					/* 126 */
1896 	NOSYS,					/* 127 */
1897 	NOSYS,					/* 128 */
1898 	NOSYS,					/* 129 */
1899 	NOSYS,					/* 130 */
1900 	NOSYS,					/* 131 */
1901 	NOSYS,					/* 132 */
1902 	NOSYS,					/* 133 */
1903 	NOSYS,					/* 134 */
1904 	EMULATE(s10_uname, 1 | RV_DEFAULT),	/* 135 */
1905 	NOSYS,					/* 136 */
1906 	NOSYS,					/* 137 */
1907 	NOSYS,					/* 138 */
1908 	EMULATE(s10_sysinfo, 3 | RV_DEFAULT),	/* 139 */
1909 	NOSYS,					/* 140 */
1910 	NOSYS,					/* 141 */
1911 	NOSYS,					/* 142 */
1912 	NOSYS,					/* 143 */
1913 	NOSYS,					/* 144 */
1914 	NOSYS,					/* 145 */
1915 	NOSYS,					/* 146 */
1916 	NOSYS,					/* 147 */
1917 	NOSYS,					/* 148 */
1918 	NOSYS,					/* 149 */
1919 	NOSYS,					/* 150 */
1920 	NOSYS,					/* 151 */
1921 	NOSYS,					/* 152 */
1922 	NOSYS,					/* 153 */
1923 	NOSYS,					/* 154 */
1924 	NOSYS,					/* 155 */
1925 	NOSYS,					/* 156 */
1926 	NOSYS,					/* 157 */
1927 	NOSYS,					/* 158 */
1928 #ifdef	__x86
1929 	EMULATE(s10_lwp_create, 3 | RV_DEFAULT), /* 159 */
1930 #else	/* !__x86 */
1931 	NOSYS,					/* 159 */
1932 #endif	/* !__x86 */
1933 	NOSYS,					/* 160 */
1934 	NOSYS,					/* 161 */
1935 	NOSYS,					/* 162 */
1936 	NOSYS,					/* 163 */
1937 	NOSYS,					/* 164 */
1938 	NOSYS,					/* 165 */
1939 #ifdef	__x86
1940 	EMULATE(s10_lwp_private, 3 | RV_DEFAULT), /* 166 */
1941 #else	/* !__x86 */
1942 	NOSYS,					/* 166 */
1943 #endif	/* !__x86 */
1944 	NOSYS,					/* 167 */
1945 	NOSYS,					/* 168 */
1946 	NOSYS,					/* 169 */
1947 	NOSYS,					/* 170 */
1948 	NOSYS,					/* 171 */
1949 	NOSYS,					/* 172 */
1950 	NOSYS,					/* 173 */
1951 	EMULATE(s10_pwrite, 4 | RV_DEFAULT),	/* 174 */
1952 	NOSYS,					/* 175 */
1953 	NOSYS,					/* 176 */
1954 	NOSYS,					/* 177 */
1955 	NOSYS,					/* 178 */
1956 	NOSYS,					/* 179 */
1957 	NOSYS,					/* 180 */
1958 	NOSYS,					/* 181 */
1959 	NOSYS,					/* 182 */
1960 	NOSYS,					/* 183 */
1961 	NOSYS,					/* 184 */
1962 	NOSYS,					/* 185 */
1963 	EMULATE(s10_auditsys, 4 | RV_64RVAL),	/* 186 */
1964 	NOSYS,					/* 187 */
1965 	NOSYS,					/* 188 */
1966 	NOSYS,					/* 189 */
1967 	EMULATE(s10_sigqueue, 4 | RV_DEFAULT),	/* 190 */
1968 	NOSYS,					/* 191 */
1969 	NOSYS,					/* 192 */
1970 	NOSYS,					/* 193 */
1971 	NOSYS,					/* 194 */
1972 	NOSYS,					/* 195 */
1973 	NOSYS,					/* 196 */
1974 	NOSYS,					/* 197 */
1975 	NOSYS,					/* 198 */
1976 	NOSYS,					/* 199 */
1977 	NOSYS,					/* 200 */
1978 	NOSYS,					/* 201 */
1979 	NOSYS,					/* 202 */
1980 	NOSYS,					/* 203 */
1981 	NOSYS,					/* 204 */
1982 	NOSYS,					/* 205 */
1983 	NOSYS,					/* 206 */
1984 	NOSYS,					/* 207 */
1985 	NOSYS,					/* 208 */
1986 	NOSYS,					/* 209 */
1987 	EMULATE(s10_lwp_mutex_timedlock, 2 | RV_DEFAULT),	/* 210 */
1988 	NOSYS,					/* 211 */
1989 	NOSYS,					/* 212 */
1990 	NOSYS,					/* 213 */
1991 	NOSYS,					/* 214 */
1992 	NOSYS,					/* 215 */
1993 	NOSYS,					/* 216 */
1994 	NOSYS,					/* 217 */
1995 	NOSYS,					/* 218 */
1996 	NOSYS,					/* 219 */
1997 	NOSYS,					/* 220 */
1998 	NOSYS,					/* 221 */
1999 	NOSYS,					/* 222 */
2000 #ifdef	_LP64
2001 	NOSYS,					/* 223 */
2002 #else	/* !_LP64 */
2003 	EMULATE(s10_pwrite64, 5 | RV_DEFAULT),	/* 223 */
2004 #endif	/* !_LP64 */
2005 	NOSYS,					/* 224 */
2006 	NOSYS,					/* 225 */
2007 	NOSYS,					/* 226 */
2008 	EMULATE(s10_zone, 5 | RV_DEFAULT),	/* 227 */
2009 	NOSYS,					/* 228 */
2010 	NOSYS,					/* 229 */
2011 	NOSYS,					/* 230 */
2012 	NOSYS,					/* 231 */
2013 	NOSYS,					/* 232 */
2014 	NOSYS,					/* 233 */
2015 	NOSYS,					/* 234 */
2016 	NOSYS,					/* 235 */
2017 	NOSYS,					/* 236 */
2018 	NOSYS,					/* 237 */
2019 	NOSYS,					/* 238 */
2020 	NOSYS,					/* 239 */
2021 	NOSYS,					/* 240 */
2022 	NOSYS,					/* 241 */
2023 	NOSYS,					/* 242 */
2024 	NOSYS,					/* 243 */
2025 	NOSYS,					/* 244 */
2026 	NOSYS,					/* 245 */
2027 	NOSYS,					/* 246 */
2028 	NOSYS,					/* 247 */
2029 	NOSYS,					/* 248 */
2030 	NOSYS,					/* 249 */
2031 	NOSYS,					/* 250 */
2032 	EMULATE(s10_lwp_mutex_trylock, 1 | RV_DEFAULT),		/* 251 */
2033 	NOSYS,					/* 252 */
2034 	NOSYS,					/* 253 */
2035 	NOSYS,					/* 254 */
2036 	NOSYS					/* 255 */
2037 };
2038