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 <sys/auxv.h>
33 #include <sys/bitmap.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/zone.h>
41 #include <sys/stat.h>
42 #include <sys/mntent.h>
43 #include <sys/ctfs.h>
44 #include <sys/priv.h>
45 #include <sys/acctctl.h>
46 #include <libgen.h>
47
48 #include <s10_brand.h>
49 #include <s10_misc.h>
50
51 /*
52 * Principles of emulation 101.
53 *
54 *
55 * *** Setting errno
56 *
57 * Just don't do it. This emulation library is loaded onto a
58 * seperate link map from the application who's address space we're
59 * running in. We have our own private copy of libc, so there for,
60 * the errno value accessible from here is is also private and changing
61 * it will not affect any errno value that the processes who's address
62 * space we are running in will see. To return an error condition we
63 * should return the negated errno value we'd like the system to return.
64 * For more information about this see the comment in s10_handler().
65 * Basically, when we return to the caller that initiated the system
66 * call it's their responsibility to set errno.
67 *
68 *
69 * *** Recursion Considerations
70 *
71 * When emulating system calls we need to be very careful about what
72 * library calls we invoke. Library calls should be kept to a minimum.
73 * One issue is that library calls can invoke system calls, so if we're
74 * emulating a system call and we invoke a library call that depends on
75 * that system call we will probably enter a recursive loop, which would
76 * be bad.
77 *
78 *
79 * *** Return Values.
80 *
81 * When declaring new syscall emulation functions, it is very important
82 * to to set the proper RV_* flags in the s10_sysent_table. Upon failure,
83 * syscall emulation fuctions should return an errno value. Upon success
84 * syscall emulation functions should return 0 and set the sysret_t return
85 * value parameters accordingly.
86 *
87 *
88 * *** Agent lwp considerations
89 *
90 * It is currently impossible to do any emulation for these system call
91 * when they are being invoked on behalf of an agent lwp. To understand why
92 * it's impossible you have to understand how agent lwp syscalls work.
93 *
94 * The agent lwp syscall process works as follows:
95 * 1 The controlling process stops the target.
96 * 2 The controlling process injects an agent lwp which is also stopped.
97 * This agent lwp assumes the userland stack and register values
98 * of another stopped lwp in the current process.
99 * 3 The controlling process configures the agent lwp to start
100 * executing the requested system call.
101 * 4 The controlling process configure /proc to stop the agent lwp when
102 * it enters the requested system call.
103 * 5 The controlling processes allows the agent lwp to start executing.
104 * 6 The agent lwp traps into the kernel to perform the requested system
105 * call and immediately stop.
106 * 7 The controlling process copies all the arguments for the requested
107 * system call onto the agent lwp's stack.
108 * 8 The controlling process configures /proc to stop the agent lwp
109 * when it completes the requested system call.
110 * 9 The controlling processes allows the agent lwp to start executing.
111 * 10 The agent lwp executes the system call and then stop before returning
112 * to userland.
113 * 11 The controlling process copies the return value and return arguments
114 * back from the agent lwps stack.
115 * 12 The controlling process destroys the agent lwp and restarts
116 * the target process.
117 *
118 * The fundamental problem is that when the agent executes the request
119 * system call in step 5, if we're emulating that system call then the
120 * lwp is redirected back to our emulation layer without blocking
121 * in the kernel. But our emulation layer can't access the arguments
122 * for the system call because they haven't been copied to the stack
123 * yet and they still only exist in the controlling processes address
124 * space. This prevents us from being able to do any emulation of
125 * agent lwp system calls. Hence, currently our brand trap interposition
126 * callback (s10_brand_syscall_callback_common) will detect if a system
127 * call is being made by an agent lwp, and if this is the case it will
128 * never redirect the system call to this emulation library.
129 *
130 * In the future, if this proves to be a problem the the easiest solution
131 * would probably be to replace the branded versions of these application
132 * with their native counterparts. Ie, truss, plimit, and pfiles could be
133 * replace with wrapper scripts that execute the native versions of these
134 * applications. In the case of plimit and pfiles this should be pretty
135 * strait forward. Truss would probably be more tricky since it can
136 * execute applications which would be branded applications, so in that
137 * case it might be necessary to create a loadable library which could
138 * be LD_PRELOADed into truss and this library would interpose on the
139 * exec() system call to allow truss to correctly execute branded
140 * processes. It should be pointed out that this solution could work
141 * because "native agent lwps" (ie, agent lwps created by native
142 * processes) can be treated differently from "branded aged lwps" (ie,
143 * agent lwps created by branded processes), since native agent lwps
144 * would presumably be making native system calls and hence not need
145 * any interposition.
146 *
147 *
148 * *** s10 brand emulation scope considerations
149 *
150 * One of the differences between the lx brand and the s8 and s9
151 * brands, is that the s8 and s9 brands only interpose on syscalls
152 * that need some kind of emulation, where as the lx brand interposes
153 * on _all_ system calls. Lx branded system calls that don't need
154 * any emulation are then redirected back to the kernel from the
155 * userland library via the IN_KERNEL_SYSCALL macro. The lx-syscall
156 * dtrace provider depends on this behavior.
157 *
158 */
159
160 static zoneid_t zoneid;
161 static boolean_t ipshared;
162 static boolean_t emul_global_zone = B_FALSE;
163 static int emul_vers;
164 pid_t zone_init_pid;
165
166 #define EMULATE(cb, args) { (sysent_cb_t)(cb), (args) }
167 #define NOSYS EMULATE(s10_unimpl, (0 | RV_DEFAULT))
168
169 typedef long (*sysent_cb_t)();
170 typedef struct s10_sysent_table {
171 sysent_cb_t st_callc;
172 uintptr_t st_args;
173 } s10_sysent_table_t;
174 s10_sysent_table_t s10_sysent_table[];
175
176 #define S10_UTS_RELEASE "5.10"
177 #define S10_UTS_VERSION "Generic_Virtual"
178
179 /*LINTED: static unused*/
180 static volatile int s10_abort_err;
181 /*LINTED: static unused*/
182 static volatile const char *s10_abort_msg;
183 /*LINTED: static unused*/
184 static volatile const char *s10_abort_file;
185 /*LINTED: static unused*/
186 static volatile int s10_abort_line;
187
188 extern int errno;
189
190 /*ARGSUSED*/
191 void
192 _s10_abort(int err, const char *msg, const char *file, int line)
193 {
194 sysret_t rval;
195
196 /* Save the error message into convenient globals */
197 s10_abort_err = err;
198 s10_abort_msg = msg;
199 s10_abort_file = file;
200 s10_abort_line = line;
201
202 /* kill ourselves */
203 abort();
204
205 /* If abort() didn't work, try something stronger. */
206 (void) __systemcall(&rval, SYS_lwp_kill + 1024, _lwp_self(), SIGKILL);
207 }
208
209 static int
210 s10_uucopy(const void *from, void *to, size_t size)
211 {
212 sysret_t rval;
213 int err;
214
215 err = __systemcall(&rval, SYS_uucopy + 1024, from, to, size);
216 if (err == 0)
217 return (0);
218 return (EFAULT);
219 }
220
221 /*
222 * ATTENTION: uucopystr() does NOT ensure that string are null terminated!
223 */
224 static int
225 s10_uucopystr(const void *from, void *to, size_t size)
226 {
227 sysret_t rval;
228 int err;
229
230 err = __systemcall(&rval, SYS_uucopystr + 1024, from, to, size);
231 if (err == 0)
232 return (0);
233 return (EFAULT);
234 }
235
236 /*
237 * Figures out the PID of init for the zone. Also returns a boolean
238 * indicating whether this process currently has that pid: if so,
239 * then at this moment, we are init.
240 */
241 static boolean_t
242 get_initpid_info(void)
243 {
244 pid_t pid;
245 sysret_t rval;
246 int err;
247
248 /*
249 * Determine the current process PID and the PID of the zone's init.
250 * We use care not to call getpid() here, because we're not supposed
251 * to call getpid() until after the program is fully linked-- the
252 * first call to getpid() is a signal from the linker to debuggers
253 * that linking has been completed.
254 */
255 if ((err = __systemcall(&rval, SYS_brand,
256 B_S10_PIDINFO, &pid, &zone_init_pid)) != 0) {
257 s10_abort(err, "Failed to get init's pid");
258 }
259
260 /*
261 * Note that we need to be cautious with the pid we get back--
262 * it should not be stashed and used in place of getpid(), since
263 * we might fork(2). So we keep zone_init_pid and toss the pid
264 * we otherwise got.
265 */
266 if (pid == zone_init_pid)
267 return (B_TRUE);
268
269 return (B_FALSE);
270 }
271
272 /*
273 * This function is defined to be NOSYS but it won't be called from the
274 * the kernel since the NOSYS system calls are not enabled in the kernel.
275 * Thus, the only time this function is called is directly from within the
276 * indirect system call path.
277 */
278 /*ARGSUSED*/
279 static long
280 s10_unimpl(sysret_t *rv, uintptr_t p1)
281 {
282 sysret_t rval;
283
284 /*
285 * We'd like to print out some kind of error message here like
286 * "unsupported syscall", but we can't because it's not safe to
287 * assume that stderr or STDERR_FILENO actually points to something
288 * that is a terminal, and if we wrote to those files we could
289 * inadvertantly write to some applications open files, which would
290 * be bad.
291 *
292 * Normally, if an application calls an invalid system call
293 * it get a SIGSYS sent to it. So we'll just go ahead and send
294 * ourselves a signal here. Note that this is far from ideal since
295 * if the application has registered a signal handler, that signal
296 * handler may recieve a ucontext_t as the third parameter to
297 * indicate the context of the process when the signal was
298 * generated, and in this case that context will not be what the
299 * application is expecting. Hence, we should probably create a
300 * brandsys() kernel function that can deliver the signal to us
301 * with the correct ucontext_t.
302 */
303 (void) __systemcall(&rval, SYS_lwp_kill + 1024, _lwp_self(), SIGSYS);
304 return (ENOSYS);
305 }
306
307 #if defined(__sparc) && !defined(__sparcv9)
308 /*
309 * Yuck. For 32-bit sparc applications, handle indirect system calls.
310 * Note that we declare this interface to use the maximum number of
311 * system call arguments. If we recieve a system call that uses less
312 * arguments, then the additional arguments will be garbage, but they
313 * will also be ignored so that should be ok.
314 */
315 static long
316 s10_indir(sysret_t *rv, int code,
317 uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
318 uintptr_t a5, uintptr_t a6, uintptr_t a7)
319 {
320 s10_sysent_table_t *sst = &(s10_sysent_table[code]);
321
322 s10_assert(code < NSYSCALL);
323 switch (sst->st_args & NARGS_MASK) {
324 case 0:
325 return ((sst->st_callc)(rv));
326 case 1:
327 return ((sst->st_callc)(rv, a0));
328 case 2:
329 return ((sst->st_callc)(rv, a0, a1));
330 case 3:
331 return ((sst->st_callc)(rv, a0, a1, a2));
332 case 4:
333 return ((sst->st_callc)(rv, a0, a1, a2, a3));
334 case 5:
335 return ((sst->st_callc)(rv, a0, a1, a2, a3, a4));
336 case 6:
337 return ((sst->st_callc)(rv, rv, a0, a1, a2, a3, a4, a5));
338 case 7:
339 return ((sst->st_callc)(rv, a0, a1, a2, a3, a4, a5, a6));
340 case 8:
341 return ((sst->st_callc)(rv, a0, a1, a2, a3, a4, a5, a6, a7));
342 }
343 s10_abort(0, "invalid entry in s10_sysent_table");
344 return (EINVAL);
345 }
346 #endif /* __sparc && !__sparcv9 */
347
348 /*
349 * The process contract CT_TGET and CT_TSET parameter structure ct_param_t
350 * changed between S10 and Nevada, so we have to emulate the old S10
351 * ct_param_t structure when interposing on the ioctl syscall.
352 */
353 typedef struct s10_ct_param {
354 uint32_t ctpm_id;
355 uint32_t ctpm_pad;
356 uint64_t ctpm_value;
357 } s10_ct_param_t;
358
359 /*
360 * New first arg "legacy" should be set to 1.
361 */
362 static int
363 s10_getpagesizes(sysret_t *rval, size_t *buf, int nelem)
364 {
365 int err;
366
367 if ((err = __systemcall(rval, SYS_getpagesizes + 1024, 1, buf, nelem))
368 != 0)
369 return (err);
370 return (0);
371 }
372
373 int
374 s10_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg)
375 {
376 int err;
377 s10_ct_param_t s10param;
378 ct_param_t param;
379 struct stat statbuf;
380
381 /*
382 * We have to emulate process contract ioctls for init(1M) because the
383 * ioctl parameter structure changed between S10 and Nevada. This is
384 * a relatively simple process of filling Nevada structure fields,
385 * shuffling values, and initiating a native system call.
386 *
387 * For now, we'll assume that all consumers of CT_TGET and CT_TSET will
388 * need emulation. We'll issue a stat to make sure that the ioctl
389 * is meant for the contract file system.
390 *
391 */
392 switch (cmd) {
393 case CT_TGET:
394 if ((err = __systemcall(rval, SYS_fstat + 1024, fdes,
395 &statbuf)) != 0)
396 return (err);
397 if (strcmp(statbuf.st_fstype, MNTTYPE_CTFS) != 0)
398 goto nonemuioctl;
399 if (s10_uucopy((const void *)arg, &s10param,
400 sizeof (s10param)) != 0)
401 return (EFAULT);
402 param.ctpm_id = s10param.ctpm_id;
403 param.ctpm_size = sizeof (uint64_t);
404 param.ctpm_value = &s10param.ctpm_value;
405 if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes,
406 cmd, ¶m)) != 0)
407 return (err);
408 if (s10_uucopy(&s10param, (void *)arg,
409 sizeof (s10param)) != 0)
410 return (EFAULT);
411 return (0);
412 case CT_TSET:
413 if ((err = __systemcall(rval, SYS_fstat + 1024, fdes,
414 &statbuf)) != 0)
415 return (err);
416 if (strcmp(statbuf.st_fstype, MNTTYPE_CTFS) != 0)
417 goto nonemuioctl;
418 if (s10_uucopy((const void *)arg, &s10param,
419 sizeof (s10param)) != 0)
420 return (EFAULT);
421 param.ctpm_id = s10param.ctpm_id;
422 param.ctpm_size = sizeof (uint64_t);
423 param.ctpm_value = &s10param.ctpm_value;
424 if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes,
425 cmd, ¶m)) != 0)
426 return (err);
427 return (0);
428 }
429
430 nonemuioctl:
431 if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)) != 0)
432 return (err);
433 return (0);
434 }
435
436 /*
437 * Unfortunately, pwrite()'s behavior differs between S10 and Nevada when
438 * applied to files opened with O_APPEND. The offset argument is ignored and
439 * the buffer is appended to the target file in S10, whereas the current file
440 * position is ignored in Nevada (i.e., pwrite() acts as though the target file
441 * wasn't opened with O_APPEND). This is a result of the fix for CR 6655660
442 * (pwrite() must ignore the O_APPEND/FAPPEND flag).
443 *
444 * We emulate the old S10 pwrite() behavior by checking whether the target file
445 * was opened with O_APPEND. If it was, then invoke the write() system call
446 * instead of pwrite(); otherwise, invoke the pwrite() system call as usual.
447 */
448 static int
449 s10_pwrite(sysret_t *rval, int fd, const void *bufferp, size_t num_bytes,
450 off_t offset)
451 {
452 int err;
453
454 if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0)
455 return (err);
456 if (rval->sys_rval1 & O_APPEND)
457 return (__systemcall(rval, SYS_write + 1024, fd, bufferp,
458 num_bytes));
459 return (__systemcall(rval, SYS_pwrite + 1024, fd, bufferp, num_bytes,
460 offset));
461 }
462
463 #ifndef _LP64
464 /*
465 * This is the large file version of the pwrite() system call for 32-bit
466 * processes. This exists for the same reason that s10_pwrite() exists; see
467 * the comment above s10_pwrite().
468 */
469 static int
470 s10_pwrite64(sysret_t *rval, int fd, const void *bufferp, size32_t num_bytes,
471 uint32_t offset_1, uint32_t offset_2)
472 {
473 int err;
474
475 if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0)
476 return (err);
477 if (rval->sys_rval1 & O_APPEND)
478 return (__systemcall(rval, SYS_write + 1024, fd, bufferp,
479 num_bytes));
480 return (__systemcall(rval, SYS_pwrite64 + 1024, fd, bufferp,
481 num_bytes, offset_1, offset_2));
482 }
483 #endif /* !_LP64 */
484
485 #define S10_AC_PROC (0x1 << 28)
486 #define S10_AC_TASK (0x2 << 28)
487 #define S10_AC_FLOW (0x4 << 28)
488 #define S10_AC_MODE(x) ((x) & 0xf0000000)
489 #define S10_AC_OPTION(x) ((x) & 0x0fffffff)
490
491 /*
492 * The mode shift, mode mask and option mask for acctctl have changed. The
493 * mode is currently the top full byte and the option is the lower 3 full bytes.
494 */
495 int
496 s10_acctctl(sysret_t *rval, int cmd, void *buf, size_t bufsz)
497 {
498 int mode = S10_AC_MODE(cmd);
499 int option = S10_AC_OPTION(cmd);
500
501 switch (mode) {
502 case S10_AC_PROC:
503 mode = AC_PROC;
504 break;
505 case S10_AC_TASK:
506 mode = AC_TASK;
507 break;
508 case S10_AC_FLOW:
509 mode = AC_FLOW;
510 break;
511 default:
512 return (S10_TRUSS_POINT_3(rval, SYS_acctctl, EINVAL, cmd, buf,
513 bufsz));
514 }
515
516 return (__systemcall(rval, SYS_acctctl + 1024, mode | option, buf,
517 bufsz));
518 }
519
520 /*
521 * Determine whether the executable passed to SYS_exec or SYS_execve is a
522 * wrapper around a native executable. If so, then fudge the executable's
523 * name and parameters to eliminate any trace of the wrapper. This will make
524 * pgrep and other commands that examine process' executable names and
525 * command-line parameters work properly.
526 */
527 static int
528 s10_exec_native(sysret_t *rval, const char *fname, const char **argp,
529 const char **envp)
530 {
531 const char *filename = fname;
532 char path[64];
533 int err;
534
535 /* Get a copy of the executable we're trying to run */
536 path[0] = '\0';
537 (void) s10_uucopystr(filename, path, sizeof (path));
538
539 /* Check if we're trying to run a native binary */
540 if (strncmp(path, "/.SUNWnative/usr/lib/brand/solaris10/s10_native",
541 sizeof (path)) != 0)
542 return (0);
543
544 /* Skip the first element in the argv array */
545 argp++;
546
547 /*
548 * The name of the new program to execute was the second parameter
549 * passed to s10_exec_native().
550 */
551 if (s10_uucopy(argp, &filename, sizeof (char *)) != 0)
552 return (EFAULT);
553
554 /* If an exec call succeeds, it never returns */
555 err = __systemcall(rval, SYS_brand + 1024, B_EXEC_NATIVE, filename,
556 argp, envp, NULL, NULL, NULL);
557 s10_assert(err != 0);
558 return (err);
559 }
560
561 /*
562 * Interpose on the SYS_exec syscall to detect native wrappers.
563 */
564 int
565 s10_exec(sysret_t *rval, const char *fname, const char **argp)
566 {
567 int err;
568
569 if ((err = s10_exec_native(rval, fname, argp, NULL)) != 0)
570 return (err);
571
572 /* If an exec call succeeds, it never returns */
573 err = __systemcall(rval, SYS_exec + 1024, fname, argp);
574 s10_assert(err != 0);
575 return (err);
576 }
577
578 /*
579 * Interpose on the SYS_execve syscall to detect native wrappers.
580 */
581 int
582 s10_execve(sysret_t *rval, const char *fname, const char **argp,
583 const char **envp)
584 {
585 int err;
586
587 if ((err = s10_exec_native(rval, fname, argp, envp)) != 0)
588 return (err);
589
590 /* If an exec call succeeds, it never returns */
591 err = __systemcall(rval, SYS_execve + 1024, fname, argp, envp);
592 s10_assert(err != 0);
593 return (err);
594 }
595
596 /*
597 * S10's issetugid() syscall is now a subcode to privsys().
598 */
599 static int
600 s10_issetugid(sysret_t *rval)
601 {
602 int err;
603
604 if ((err = __systemcall(rval, SYS_privsys + 1024, PRIVSYS_ISSETUGID,
605 0, 0, 0, 0, 0)) != 0)
606 return (err);
607 return (0);
608 }
609
610 /*
611 * New last arg "block" flag should be zero. The block flag is used by
612 * the Opensolaris AIO implementation, which is now part of libc.
613 */
614 static int
615 s10_sigqueue(sysret_t *rval, pid_t pid, int signo, void *value, int si_code)
616 {
617 int err;
618
619 if ((err = __systemcall(rval, SYS_sigqueue + 1024, pid, signo, value,
620 si_code, 0)) != 0)
621 return (err);
622 return (0);
623 }
624
625 static long
626 s10_uname(sysret_t *rv, uintptr_t p1)
627 {
628 struct utsname un, *unp = (struct utsname *)p1;
629 int rev, err;
630
631 if ((err = __systemcall(rv, SYS_uname + 1024, &un)) != 0)
632 return (err);
633
634 rev = atoi(&un.release[2]);
635 s10_assert(rev >= 11);
636 bzero(un.release, _SYS_NMLN);
637 (void) strlcpy(un.release, S10_UTS_RELEASE, _SYS_NMLN);
638 bzero(un.version, _SYS_NMLN);
639 (void) strlcpy(un.version, S10_UTS_VERSION, _SYS_NMLN);
640
641 /* copy out the modified uname info */
642 if (s10_uucopy(&un, unp, sizeof (un)) != 0)
643 return (EFAULT);
644
645 return (0);
646 }
647
648 /*
649 * If the emul_global_zone flag is set then emulate some aspects of the
650 * zone system call. In particular, emulate the global zone ID on the
651 * ZONE_LOOKUP subcommand and emulate some of the global zone attributes
652 * on the ZONE_GETATTR subcommand. If the flag is not set or we're performing
653 * some other operation, simply pass the calls through.
654 */
655 int
656 s10_zone(sysret_t *rval, int cmd, void *arg1, void *arg2, void *arg3,
657 void *arg4)
658 {
659 char *aval;
660 int len;
661 zoneid_t zid;
662 int attr;
663 char *buf;
664 size_t bufsize;
665
666 /*
667 * We only emulate the zone syscall for a subset of specific commands,
668 * otherwise we just pass the call through.
669 */
670 if (!emul_global_zone)
671 return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2,
672 arg3, arg4));
673
674 switch (cmd) {
675 case ZONE_LOOKUP:
676 (void) S10_TRUSS_POINT_1(rval, SYS_zone, 0, cmd);
677 rval->sys_rval1 = GLOBAL_ZONEID;
678 rval->sys_rval2 = 0;
679 return (0);
680
681 case ZONE_GETATTR:
682 zid = (zoneid_t)(uintptr_t)arg1;
683 attr = (int)(uintptr_t)arg2;
684 buf = (char *)arg3;
685 bufsize = (size_t)arg4;
686
687 /*
688 * If the request is for the global zone then we're emulating
689 * that, otherwise pass this thru.
690 */
691 if (zid != GLOBAL_ZONEID)
692 goto passthru;
693
694 (void) S10_TRUSS_POINT_3(rval, SYS_zone, 0, cmd, zid, attr);
695
696 switch (attr) {
697 case ZONE_ATTR_NAME:
698 aval = GLOBAL_ZONENAME;
699 break;
700
701 case ZONE_ATTR_BRAND:
702 aval = NATIVE_BRAND_NAME;
703 break;
704 default:
705 /*
706 * We only emulate a subset of the attrs, use the
707 * real zone id to pass thru the rest.
708 */
709 arg1 = (void *)(uintptr_t)zoneid;
710 goto passthru;
711 }
712
713 len = strlen(aval) + 1;
714 if (len > bufsize)
715 return (ENAMETOOLONG);
716
717 if (buf != NULL) {
718 if (len == 1) {
719 if (s10_uucopy("\0", buf, 1) != 0)
720 return (EFAULT);
721 } else {
722 if (s10_uucopystr(aval, buf, len) != 0)
723 return (EFAULT);
724
725 /*
726 * Assure NULL termination of "buf" as
727 * s10_uucopystr() does NOT.
728 */
729 if (s10_uucopy("\0", buf + (len - 1), 1) != 0)
730 return (EFAULT);
731 }
732 }
733
734 rval->sys_rval1 = len;
735 rval->sys_rval2 = 0;
736 return (0);
737
738 default:
739 break;
740 }
741
742 passthru:
743 return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2, arg3,
744 arg4));
745 }
746
747 /*
748 * This routine is run only when the init daemon starts up, in order
749 * to do any pre-initialization needed before the environment boots.
750 */
751 static void
752 s10_init1m_handler()
753 {
754 /*
755 * Take special actions in advance of starting init(1m).
756 *
757 * XXX Nothing to do (yet).
758 */
759 }
760
761 /*
762 * Close a libc file handle, but don't actually close the underlying
763 * file descriptor.
764 */
765 static void
766 s10_close_fh(FILE *file)
767 {
768 int fd, fd_new;
769
770 if (file == NULL)
771 return;
772
773 if ((fd = fileno(file)) < 0)
774 return;
775
776 fd_new = dup(fd);
777 if (fd_new == -1)
778 return;
779
780 (void) fclose(file);
781 (void) dup2(fd_new, fd);
782 (void) close(fd_new);
783 }
784
785 /*ARGSUSED*/
786 int
787 s10_init(int argc, char *argv[], char *envp[])
788 {
789 sysret_t rval;
790 s10_brand_reg_t reg;
791 s10_elf_data_t sed;
792 auxv_t *ap;
793 uintptr_t *p;
794 int i, err;
795 ushort_t flags;
796 char *bname;
797
798 /* Sanity check our translation table return value codes */
799 for (i = 0; i < NSYSCALL; i++) {
800 s10_sysent_table_t *est = &(s10_sysent_table[i]);
801 s10_assert(BIT_ONLYONESET(est->st_args & RV_MASK));
802 }
803
804 /*
805 * We need to shutdown all libc stdio. libc stdio normally goes to
806 * file descriptors, but since we're actually part of a another
807 * process we don't own these file descriptors and we can't make
808 * any assumptions about their state.
809 */
810 s10_close_fh(stdin);
811 s10_close_fh(stdout);
812 s10_close_fh(stderr);
813
814 /*
815 * Cache the pid of the zone's init process and determine if
816 * we're init(1m) for the zone. Remember: we might be init
817 * now, but as soon as we fork(2) we won't be.
818 */
819 if (get_initpid_info()) {
820 s10_init1m_handler();
821 }
822
823 /* get the current zoneid */
824 err = __systemcall(&rval, SYS_zone, ZONE_LOOKUP, NULL);
825 s10_assert(err == 0);
826 zoneid = (zoneid_t)rval.sys_rval1;
827
828 /* Get the emulation version number. */
829 if ((err = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
830 S10_EMUL_VERSION_NUM, &emul_vers, sizeof (emul_vers))) != 0 ||
831 emul_vers != 0) {
832 s10_abort(err, "The zone's patch level is unsupported");
833 /*NOTREACHED*/
834 }
835
836 /* Figure out if this zone has a shared ip */
837 err = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
838 ZONE_ATTR_FLAGS, &flags, sizeof (flags));
839 s10_assert(err == 0);
840 ipshared = ((flags & ZF_NET_EXCL) == 0);
841
842 bname = basename(argv[0]);
843
844 /*
845 * In general we want the S10 commands that are zone-aware to continue
846 * to behave as they normally do within a zone. Since these commands
847 * are zone-aware, they should continue to "do the right thing".
848 * However, some zone-aware commands aren't going to work the way
849 * we expect them to inside the branded zone. In particular, the pkg
850 * and patch commands will not properly manage all pkgs/patches
851 * unless the commands think they are running in the global zone. For
852 * these commands we want to emulate the global zone.
853 *
854 * XXX One issue is the handling of hollow pkgs. This is not normally
855 * a problem since the p2v/v2v process handles those. However, if
856 * the user attempts to install a hollow pkg after the zone is running,
857 * the pkg code will do the wrong thing. Luckily, most of the hollow
858 * pkgs are core pkgs which will already be installed in the image
859 * before we p2v/v2v it into the zone and there should be little need
860 * to pkgadd these later.
861 */
862 if (strcmp("pkgadd", bname) == 0 || strcmp("pkgrm", bname) == 0 ||
863 strcmp("pkgcond", bname) == 0 ||
864 strcmp("patchadd", bname) == 0 || strcmp("patchrm", bname) == 0)
865 emul_global_zone = B_TRUE;
866
867 /*
868 * Register our syscall emulation table with the kernel.
869 * Note that we don't have to do invoke (syscall_number + 1024)
870 * until we've actually establised a syscall emulation callback
871 * handler address, which is what we're doing with this brand
872 * syscall.
873 */
874 reg.sbr_version = S10_VERSION;
875 reg.sbr_handler = (caddr_t)s10_handler;
876 if ((err = __systemcall(&rval, SYS_brand, B_REGISTER, ®)) != 0) {
877 s10_abort(err, "Failed to brand current process");
878 /*NOTREACHED*/
879 }
880
881 /* Get data about the executable we're running from the kernel. */
882 if ((err = __systemcall(&rval, SYS_brand + 1024,
883 B_ELFDATA, (void *)&sed)) != 0) {
884 s10_abort(err,
885 "Failed to get required brand ELF data from the kernel");
886 /*NOTREACHED*/
887 }
888
889 /*
890 * Find the aux vector on the stack.
891 */
892 p = (uintptr_t *)envp;
893 while (*p != NULL)
894 p++;
895
896 /*
897 * p is now pointing at the 0 word after the environ pointers.
898 * After that is the aux vectors.
899 *
900 * The aux vectors are currently pointing to the brand emulation
901 * library and associated linker. We're going to change them to
902 * point to the brand executable and associated linker (or to no
903 * linker for static binaries). This matches the process data
904 * stored within the kernel and visible from /proc, which was
905 * all setup in s10_elfexec(). We do this so that when a debugger
906 * attaches to the process it sees the process as a normal solaris
907 * process, this brand emulation library and everything on it's
908 * link map will not be visible, unless our librtld_db plugin
909 * is used. Note that this is very different from how Linux
910 * branded processes are implemented within lx branded zones.
911 * In that situation, the primary linkmap of the process is the
912 * brand emulation libraries linkmap, not the Linux applications
913 * linkmap.
914 *
915 * We also need to clear the AF_SUN_NOPLM flag from the AT_SUN_AUXFLAGS
916 * aux vector. This flag told our linker that we don't have a
917 * primary link map. Now that our linker is done initializing, we
918 * want to clear this flag before we transfer control to the
919 * applications copy of the linker, since we want that linker to have
920 * a primary link map which will be the link map for the application
921 * we're running.
922 */
923 p++;
924 for (ap = (auxv_t *)p; ap->a_type != AT_NULL; ap++) {
925 switch (ap->a_type) {
926 case AT_BASE:
927 /* Hide AT_BASE if static binary */
928 if (sed.sed_base == NULL) {
929 ap->a_type = AT_IGNORE;
930 ap->a_un.a_val = NULL;
931 } else {
932 ap->a_un.a_val = sed.sed_base;
933 }
934 break;
935 case AT_ENTRY:
936 ap->a_un.a_val = sed.sed_entry;
937 break;
938 case AT_PHDR:
939 ap->a_un.a_val = sed.sed_phdr;
940 break;
941 case AT_PHENT:
942 ap->a_un.a_val = sed.sed_phent;
943 break;
944 case AT_PHNUM:
945 ap->a_un.a_val = sed.sed_phnum;
946 break;
947 case AT_SUN_AUXFLAGS:
948 ap->a_un.a_val &= ~AF_SUN_NOPLM;
949 break;
950 case AT_SUN_EMULATOR:
951 /*
952 * ld.so.1 inspects AT_SUN_EMULATOR to see if
953 * if it is the linker for the brand emulation
954 * library. Hide AT_SUN_EMULATOR, as the
955 * linker we are about to jump to is the linker
956 * for the binary.
957 */
958 ap->a_type = AT_IGNORE;
959 ap->a_un.a_val = NULL;
960 break;
961 case AT_SUN_LDDATA:
962 /* Hide AT_SUN_LDDATA if static binary */
963 if (sed.sed_lddata == NULL) {
964 ap->a_type = AT_IGNORE;
965 ap->a_un.a_val = NULL;
966 } else {
967 ap->a_un.a_val = sed.sed_lddata;
968 }
969 break;
970 default:
971 break;
972 }
973 }
974
975 s10_runexe(argv, sed.sed_ldentry);
976 /*NOTREACHED*/
977 s10_abort(0, "s10_runexe() returned");
978 return (-1);
979 }
980
981 /*
982 * This table must have at least NSYSCALL entries in it.
983 *
984 * The second parameter of each entry in the s10_sysent_table
985 * contains the number of parameters and flags that describe the
986 * syscall return value encoding. See the block comments at the
987 * top of this file for more information about the syscall return
988 * value flags and when they should be used.
989 */
990 s10_sysent_table_t s10_sysent_table[] = {
991 #if defined(__sparc) && !defined(__sparcv9)
992 EMULATE(s10_indir, 9 | RV_64RVAL), /* 0 */
993 #else /* !__sparc || __sparcv9 */
994 NOSYS, /* 0 */
995 #endif /* !__sparc || __sparcv9 */
996 NOSYS, /* 1 */
997 NOSYS, /* 2 */
998 NOSYS, /* 3 */
999 NOSYS, /* 4 */
1000 NOSYS, /* 5 */
1001 NOSYS, /* 6 */
1002 NOSYS, /* 7 */
1003 NOSYS, /* 8 */
1004 NOSYS, /* 9 */
1005 NOSYS, /* 10 */
1006 EMULATE(s10_exec, 2 | RV_DEFAULT), /* 11 */
1007 NOSYS, /* 12 */
1008 NOSYS, /* 13 */
1009 NOSYS, /* 14 */
1010 NOSYS, /* 15 */
1011 NOSYS, /* 16 */
1012 NOSYS, /* 17 */
1013 NOSYS, /* 18 */
1014 NOSYS, /* 19 */
1015 NOSYS, /* 20 */
1016 NOSYS, /* 21 */
1017 NOSYS, /* 22 */
1018 NOSYS, /* 23 */
1019 NOSYS, /* 24 */
1020 NOSYS, /* 25 */
1021 NOSYS, /* 26 */
1022 NOSYS, /* 27 */
1023 NOSYS, /* 28 */
1024 NOSYS, /* 29 */
1025 NOSYS, /* 30 */
1026 NOSYS, /* 31 */
1027 NOSYS, /* 32 */
1028 NOSYS, /* 33 */
1029 NOSYS, /* 34 */
1030 NOSYS, /* 35 */
1031 NOSYS, /* 36 */
1032 NOSYS, /* 37 */
1033 NOSYS, /* 38 */
1034 NOSYS, /* 39 */
1035 NOSYS, /* 40 */
1036 NOSYS, /* 41 */
1037 NOSYS, /* 42 */
1038 NOSYS, /* 43 */
1039 NOSYS, /* 44 */
1040 NOSYS, /* 45 */
1041 NOSYS, /* 46 */
1042 NOSYS, /* 47 */
1043 NOSYS, /* 48 */
1044 NOSYS, /* 49 */
1045 NOSYS, /* 50 */
1046 NOSYS, /* 51 */
1047 NOSYS, /* 52 */
1048 NOSYS, /* 53 */
1049 EMULATE(s10_ioctl, 3 | RV_DEFAULT), /* 54 */
1050 NOSYS, /* 55 */
1051 NOSYS, /* 56 */
1052 NOSYS, /* 57 */
1053 NOSYS, /* 58 */
1054 EMULATE(s10_execve, 3 | RV_DEFAULT), /* 59 */
1055 NOSYS, /* 60 */
1056 NOSYS, /* 61 */
1057 NOSYS, /* 62 */
1058 NOSYS, /* 63 */
1059 NOSYS, /* 64 */
1060 NOSYS, /* 65 */
1061 NOSYS, /* 66 */
1062 NOSYS, /* 67 */
1063 NOSYS, /* 68 */
1064 NOSYS, /* 69 */
1065 NOSYS, /* 70 */
1066 EMULATE(s10_acctctl, 3 | RV_DEFAULT), /* 71 */
1067 NOSYS, /* 72 */
1068 EMULATE(s10_getpagesizes, 2 | RV_DEFAULT), /* 73 */
1069 NOSYS, /* 74 */
1070 EMULATE(s10_issetugid, 0 | RV_DEFAULT), /* 75 */
1071 NOSYS, /* 76 */
1072 NOSYS, /* 77 */
1073 NOSYS, /* 78 */
1074 NOSYS, /* 79 */
1075 NOSYS, /* 80 */
1076 NOSYS, /* 81 */
1077 NOSYS, /* 82 */
1078 NOSYS, /* 83 */
1079 NOSYS, /* 84 */
1080 NOSYS, /* 85 */
1081 NOSYS, /* 86 */
1082 NOSYS, /* 87 */
1083 NOSYS, /* 88 */
1084 NOSYS, /* 89 */
1085 NOSYS, /* 90 */
1086 NOSYS, /* 91 */
1087 NOSYS, /* 92 */
1088 NOSYS, /* 93 */
1089 NOSYS, /* 94 */
1090 NOSYS, /* 95 */
1091 NOSYS, /* 96 */
1092 NOSYS, /* 97 */
1093 NOSYS, /* 98 */
1094 NOSYS, /* 99 */
1095 NOSYS, /* 100 */
1096 NOSYS, /* 101 */
1097 NOSYS, /* 102 */
1098 NOSYS, /* 103 */
1099 NOSYS, /* 104 */
1100 NOSYS, /* 105 */
1101 NOSYS, /* 106 */
1102 NOSYS, /* 107 */
1103 NOSYS, /* 108 */
1104 NOSYS, /* 109 */
1105 NOSYS, /* 110 */
1106 NOSYS, /* 111 */
1107 NOSYS, /* 112 */
1108 NOSYS, /* 113 */
1109 NOSYS, /* 114 */
1110 NOSYS, /* 115 */
1111 NOSYS, /* 116 */
1112 NOSYS, /* 117 */
1113 NOSYS, /* 118 */
1114 NOSYS, /* 119 */
1115 NOSYS, /* 120 */
1116 NOSYS, /* 121 */
1117 NOSYS, /* 122 */
1118 NOSYS, /* 123 */
1119 NOSYS, /* 124 */
1120 NOSYS, /* 125 */
1121 NOSYS, /* 126 */
1122 NOSYS, /* 127 */
1123 NOSYS, /* 128 */
1124 NOSYS, /* 129 */
1125 NOSYS, /* 130 */
1126 NOSYS, /* 131 */
1127 NOSYS, /* 132 */
1128 NOSYS, /* 133 */
1129 NOSYS, /* 134 */
1130 EMULATE(s10_uname, 1 | RV_DEFAULT), /* 135 */
1131 NOSYS, /* 136 */
1132 NOSYS, /* 137 */
1133 NOSYS, /* 138 */
1134 NOSYS, /* 139 */
1135 NOSYS, /* 140 */
1136 NOSYS, /* 141 */
1137 NOSYS, /* 142 */
1138 NOSYS, /* 143 */
1139 NOSYS, /* 144 */
1140 NOSYS, /* 145 */
1141 NOSYS, /* 146 */
1142 NOSYS, /* 147 */
1143 NOSYS, /* 148 */
1144 NOSYS, /* 149 */
1145 NOSYS, /* 150 */
1146 NOSYS, /* 151 */
1147 NOSYS, /* 152 */
1148 NOSYS, /* 153 */
1149 NOSYS, /* 154 */
1150 NOSYS, /* 155 */
1151 NOSYS, /* 156 */
1152 NOSYS, /* 157 */
1153 NOSYS, /* 158 */
1154 NOSYS, /* 159 */
1155 NOSYS, /* 160 */
1156 NOSYS, /* 161 */
1157 NOSYS, /* 162 */
1158 NOSYS, /* 163 */
1159 NOSYS, /* 164 */
1160 NOSYS, /* 165 */
1161 NOSYS, /* 166 */
1162 NOSYS, /* 167 */
1163 NOSYS, /* 168 */
1164 NOSYS, /* 169 */
1165 NOSYS, /* 170 */
1166 NOSYS, /* 171 */
1167 NOSYS, /* 172 */
1168 NOSYS, /* 173 */
1169 EMULATE(s10_pwrite, 4 | RV_DEFAULT), /* 174 */
1170 NOSYS, /* 175 */
1171 NOSYS, /* 176 */
1172 NOSYS, /* 177 */
1173 NOSYS, /* 178 */
1174 NOSYS, /* 179 */
1175 NOSYS, /* 180 */
1176 NOSYS, /* 181 */
1177 NOSYS, /* 182 */
1178 NOSYS, /* 183 */
1179 NOSYS, /* 184 */
1180 NOSYS, /* 185 */
1181 NOSYS, /* 186 */
1182 NOSYS, /* 187 */
1183 NOSYS, /* 188 */
1184 NOSYS, /* 189 */
1185 EMULATE(s10_sigqueue, 4 | RV_DEFAULT), /* 190 */
1186 NOSYS, /* 191 */
1187 NOSYS, /* 192 */
1188 NOSYS, /* 193 */
1189 NOSYS, /* 194 */
1190 NOSYS, /* 195 */
1191 NOSYS, /* 196 */
1192 NOSYS, /* 197 */
1193 NOSYS, /* 198 */
1194 NOSYS, /* 199 */
1195 NOSYS, /* 200 */
1196 NOSYS, /* 201 */
1197 NOSYS, /* 202 */
1198 NOSYS, /* 203 */
1199 NOSYS, /* 204 */
1200 NOSYS, /* 205 */
1201 NOSYS, /* 206 */
1202 NOSYS, /* 207 */
1203 NOSYS, /* 208 */
1204 NOSYS, /* 209 */
1205 NOSYS, /* 210 */
1206 NOSYS, /* 211 */
1207 NOSYS, /* 212 */
1208 NOSYS, /* 213 */
1209 NOSYS, /* 214 */
1210 NOSYS, /* 215 */
1211 NOSYS, /* 216 */
1212 NOSYS, /* 217 */
1213 NOSYS, /* 218 */
1214 NOSYS, /* 219 */
1215 NOSYS, /* 220 */
1216 NOSYS, /* 221 */
1217 NOSYS, /* 222 */
1218 #ifdef _LP64
1219 NOSYS, /* 223 */
1220 #else /* !_LP64 */
1221 EMULATE(s10_pwrite64, 5 | RV_DEFAULT), /* 223 */
1222 #endif /* !_LP64 */
1223 NOSYS, /* 224 */
1224 NOSYS, /* 225 */
1225 NOSYS, /* 226 */
1226 EMULATE(s10_zone, 5 | RV_DEFAULT), /* 227 */
1227 NOSYS, /* 228 */
1228 NOSYS, /* 229 */
1229 NOSYS, /* 230 */
1230 NOSYS, /* 231 */
1231 NOSYS, /* 232 */
1232 NOSYS, /* 233 */
1233 NOSYS, /* 234 */
1234 NOSYS, /* 235 */
1235 NOSYS, /* 236 */
1236 NOSYS, /* 237 */
1237 NOSYS, /* 238 */
1238 NOSYS, /* 239 */
1239 NOSYS, /* 240 */
1240 NOSYS, /* 241 */
1241 NOSYS, /* 242 */
1242 NOSYS, /* 243 */
1243 NOSYS, /* 244 */
1244 NOSYS, /* 245 */
1245 NOSYS, /* 246 */
1246 NOSYS, /* 247 */
1247 NOSYS, /* 248 */
1248 NOSYS, /* 249 */
1249 NOSYS, /* 250 */
1250 NOSYS, /* 251 */
1251 NOSYS, /* 252 */
1252 NOSYS, /* 253 */
1253 NOSYS, /* 254 */
1254 NOSYS /* 255 */
1255 };