Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / os / brand.c
blobebb167dae55bead66894629a2a5a1c7aede687c0
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/kmem.h>
26 #include <sys/errno.h>
27 #include <sys/systm.h>
28 #include <sys/cmn_err.h>
29 #include <sys/brand.h>
30 #include <sys/machbrand.h>
31 #include <sys/modctl.h>
32 #include <sys/rwlock.h>
33 #include <sys/zone.h>
34 #include <sys/pathname.h>
36 #define SUPPORTED_BRAND_VERSION BRAND_VER_1
38 struct brand_mach_ops native_mach_ops = {
39 NULL, NULL, NULL, NULL
42 brand_t native_brand = {
43 BRAND_VER_1,
44 "native",
45 NULL,
46 &native_mach_ops
50 * Used to maintain a list of all the brands currently loaded into the
51 * kernel.
53 struct brand_list {
54 int bl_refcnt;
55 struct brand_list *bl_next;
56 brand_t *bl_brand;
59 static struct brand_list *brand_list = NULL;
62 * This lock protects the integrity of the brand list.
64 static kmutex_t brand_list_lock;
66 void
67 brand_init()
69 mutex_init(&brand_list_lock, NULL, MUTEX_DEFAULT, NULL);
70 p0.p_brand = &native_brand;
73 int
74 brand_register(brand_t *brand)
76 struct brand_list *list, *scan;
78 if (brand == NULL)
79 return (EINVAL);
81 if (brand->b_version != SUPPORTED_BRAND_VERSION) {
82 if (brand->b_version < SUPPORTED_BRAND_VERSION) {
83 cmn_err(CE_WARN,
84 "brand '%s' was built to run on older versions "
85 "of Solaris.",
86 brand->b_name);
87 } else {
88 cmn_err(CE_WARN,
89 "brand '%s' was built to run on a newer version "
90 "of Solaris.",
91 brand->b_name);
93 return (EINVAL);
96 /* Sanity checks */
97 if (brand->b_name == NULL || brand->b_ops == NULL ||
98 brand->b_ops->b_brandsys == NULL) {
99 cmn_err(CE_WARN, "Malformed brand");
100 return (EINVAL);
103 list = kmem_alloc(sizeof (struct brand_list), KM_SLEEP);
105 /* Add the brand to the list of loaded brands. */
106 mutex_enter(&brand_list_lock);
109 * Check to be sure we haven't already registered this brand.
111 for (scan = brand_list; scan != NULL; scan = scan->bl_next) {
112 if (strcmp(brand->b_name, scan->bl_brand->b_name) == 0) {
113 cmn_err(CE_WARN,
114 "Invalid attempt to load a second instance of "
115 "brand %s", brand->b_name);
116 mutex_exit(&brand_list_lock);
117 kmem_free(list, sizeof (struct brand_list));
118 return (EINVAL);
123 list->bl_brand = brand;
124 list->bl_refcnt = 0;
125 list->bl_next = brand_list;
126 brand_list = list;
128 mutex_exit(&brand_list_lock);
130 return (0);
134 * The kernel module implementing this brand is being unloaded, so remove
135 * it from the list of active brands.
138 brand_unregister(brand_t *brand)
140 struct brand_list *list, *prev;
142 /* Sanity checks */
143 if (brand == NULL || brand->b_name == NULL) {
144 cmn_err(CE_WARN, "Malformed brand");
145 return (EINVAL);
148 prev = NULL;
149 mutex_enter(&brand_list_lock);
151 for (list = brand_list; list != NULL; list = list->bl_next) {
152 if (list->bl_brand == brand)
153 break;
154 prev = list;
157 if (list == NULL) {
158 cmn_err(CE_WARN, "Brand %s wasn't registered", brand->b_name);
159 mutex_exit(&brand_list_lock);
160 return (EINVAL);
163 if (list->bl_refcnt > 0) {
164 cmn_err(CE_WARN, "Unregistering brand %s which is still in use",
165 brand->b_name);
166 mutex_exit(&brand_list_lock);
167 return (EBUSY);
170 /* Remove brand from the list */
171 if (prev != NULL)
172 prev->bl_next = list->bl_next;
173 else
174 brand_list = list->bl_next;
177 mutex_exit(&brand_list_lock);
179 kmem_free(list, sizeof (struct brand_list));
181 return (0);
185 * Record that a zone of this brand has been instantiated. If the kernel
186 * module implementing this brand's functionality is not present, this
187 * routine attempts to load the module as a side effect.
189 brand_t *
190 brand_register_zone(struct brand_attr *attr)
192 struct brand_list *l = NULL;
193 ddi_modhandle_t hdl = NULL;
194 char *modname;
195 int err = 0;
198 * We make at most two passes through this loop. The first time
199 * through, we're looking to see if this is a new user of an
200 * already loaded brand. If the brand hasn't been loaded, we
201 * call ddi_modopen() to force it to be loaded and then make a
202 * second pass through the list of brands. If we don't find the
203 * brand the second time through it means that the modname
204 * specified in the brand_attr structure doesn't provide the brand
205 * specified in the brandname field. This would suggest a bug in
206 * the brand's config.xml file. We close the module and return
207 * 'NULL' to the caller.
209 for (;;) {
211 * Search list of loaded brands
213 mutex_enter(&brand_list_lock);
214 for (l = brand_list; l != NULL; l = l->bl_next)
215 if (strcmp(attr->ba_brandname,
216 l->bl_brand->b_name) == 0)
217 break;
218 if ((l != NULL) || (hdl != NULL))
219 break;
220 mutex_exit(&brand_list_lock);
223 * We didn't find that the requested brand has been loaded
224 * yet, so we trigger the load of the appropriate kernel
225 * module and search the list again.
227 modname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
228 (void) strcpy(modname, "brand/");
229 (void) strcat(modname, attr->ba_modname);
230 hdl = ddi_modopen(modname, KRTLD_MODE_FIRST, &err);
231 kmem_free(modname, MAXPATHLEN);
233 if (err != 0)
234 return (NULL);
238 * If we found the matching brand, bump its reference count.
240 if (l != NULL)
241 l->bl_refcnt++;
243 mutex_exit(&brand_list_lock);
245 if (hdl != NULL)
246 (void) ddi_modclose(hdl);
248 return ((l != NULL) ? l->bl_brand : NULL);
252 * Return the number of zones currently using this brand.
255 brand_zone_count(struct brand *bp)
257 struct brand_list *l;
258 int cnt = 0;
260 mutex_enter(&brand_list_lock);
261 for (l = brand_list; l != NULL; l = l->bl_next)
262 if (l->bl_brand == bp) {
263 cnt = l->bl_refcnt;
264 break;
266 mutex_exit(&brand_list_lock);
268 return (cnt);
271 void
272 brand_unregister_zone(struct brand *bp)
274 struct brand_list *list;
276 mutex_enter(&brand_list_lock);
277 for (list = brand_list; list != NULL; list = list->bl_next) {
278 if (list->bl_brand == bp) {
279 ASSERT(list->bl_refcnt > 0);
280 list->bl_refcnt--;
281 break;
284 mutex_exit(&brand_list_lock);
287 void
288 brand_setbrand(proc_t *p)
290 brand_t *bp = p->p_zone->zone_brand;
292 ASSERT(bp != NULL);
293 ASSERT(p->p_brand == &native_brand);
296 * We should only be called from exec(), when we know the process
297 * is single-threaded.
299 ASSERT(p->p_tlist == p->p_tlist->t_forw);
301 p->p_brand = bp;
302 ASSERT(PROC_IS_BRANDED(p));
303 BROP(p)->b_setbrand(p);
306 void
307 brand_clearbrand(proc_t *p, boolean_t no_lwps)
309 brand_t *bp = p->p_zone->zone_brand;
310 klwp_t *lwp = NULL;
311 ASSERT(bp != NULL);
312 ASSERT(!no_lwps || (p->p_tlist == NULL));
315 * If called from exec_common() or proc_exit(),
316 * we know the process is single-threaded.
317 * If called from fork_fail, p_tlist is NULL.
319 if (!no_lwps) {
320 ASSERT(p->p_tlist == p->p_tlist->t_forw);
321 lwp = p->p_tlist->t_lwp;
324 ASSERT(PROC_IS_BRANDED(p));
325 BROP(p)->b_proc_exit(p, lwp);
326 p->p_brand = &native_brand;
331 * The following functions can be shared among kernel brand modules which
332 * implement Solaris-derived brands, all of which need to do similar tasks
333 * to manage the brand.
336 #if defined(_LP64)
337 static void
338 Ehdr32to64(Elf32_Ehdr *src, Ehdr *dst)
340 bcopy(src->e_ident, dst->e_ident, sizeof (src->e_ident));
341 dst->e_type = src->e_type;
342 dst->e_machine = src->e_machine;
343 dst->e_version = src->e_version;
344 dst->e_entry = src->e_entry;
345 dst->e_phoff = src->e_phoff;
346 dst->e_shoff = src->e_shoff;
347 dst->e_flags = src->e_flags;
348 dst->e_ehsize = src->e_ehsize;
349 dst->e_phentsize = src->e_phentsize;
350 dst->e_phnum = src->e_phnum;
351 dst->e_shentsize = src->e_shentsize;
352 dst->e_shnum = src->e_shnum;
353 dst->e_shstrndx = src->e_shstrndx;
355 #endif /* _LP64 */
358 * Return -1 if the cmd was not handled by this function.
360 /*ARGSUSED*/
362 brand_solaris_cmd(int cmd, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3,
363 struct brand *pbrand, int brandvers)
365 brand_proc_data_t *spd;
366 brand_proc_reg_t reg;
367 proc_t *p = curproc;
368 int err;
371 * There is one operation that is supported for a native
372 * process; B_EXEC_BRAND. This brand operaion is redundant
373 * since the kernel assumes a native process doing an exec
374 * in a branded zone is going to run a branded processes.
375 * hence we don't support this operation.
377 if (cmd == B_EXEC_BRAND)
378 return (ENOSYS);
380 /* For all other operations this must be a branded process. */
381 if (p->p_brand == &native_brand)
382 return (ENOSYS);
384 ASSERT(p->p_brand == pbrand);
385 ASSERT(p->p_brand_data != NULL);
387 spd = (brand_proc_data_t *)p->p_brand_data;
389 switch ((cmd)) {
390 case B_EXEC_NATIVE:
391 err = exec_common((char *)arg1, (const char **)arg2,
392 (const char **)arg3, EBA_NATIVE);
393 return (err);
396 * Get the address of the user-space system call handler from
397 * the user process and attach it to the proc structure.
399 case B_REGISTER:
400 if (p->p_model == DATAMODEL_NATIVE) {
401 if (copyin((void *)arg1, &reg, sizeof (reg)) != 0)
402 return (EFAULT);
404 #if defined(_LP64)
405 else {
406 brand_common_reg32_t reg32;
408 if (copyin((void *)arg1, &reg32, sizeof (reg32)) != 0)
409 return (EFAULT);
410 reg.sbr_version = reg32.sbr_version;
411 reg.sbr_handler = (caddr_t)(uintptr_t)reg32.sbr_handler;
413 #endif /* _LP64 */
415 if (reg.sbr_version != brandvers)
416 return (ENOTSUP);
417 spd->spd_handler = reg.sbr_handler;
418 return (0);
420 case B_ELFDATA:
421 if (p->p_model == DATAMODEL_NATIVE) {
422 if (copyout(&spd->spd_elf_data, (void *)arg1,
423 sizeof (brand_elf_data_t)) != 0)
424 return (EFAULT);
426 #if defined(_LP64)
427 else {
428 brand_elf_data32_t sed32;
430 sed32.sed_phdr = spd->spd_elf_data.sed_phdr;
431 sed32.sed_phent = spd->spd_elf_data.sed_phent;
432 sed32.sed_phnum = spd->spd_elf_data.sed_phnum;
433 sed32.sed_entry = spd->spd_elf_data.sed_entry;
434 sed32.sed_base = spd->spd_elf_data.sed_base;
435 sed32.sed_ldentry = spd->spd_elf_data.sed_ldentry;
436 sed32.sed_lddata = spd->spd_elf_data.sed_lddata;
437 if (copyout(&sed32, (void *)arg1, sizeof (sed32))
438 != 0)
439 return (EFAULT);
441 #endif /* _LP64 */
442 return (0);
445 * The B_TRUSS_POINT subcommand exists so that we can see
446 * truss output from interposed system calls that return
447 * without first calling any other system call, meaning they
448 * would be invisible to truss(1).
449 * If the second argument is set non-zero, set errno to that
450 * value as well.
452 * Common arguments seen with truss are:
454 * arg1: syscall number
455 * arg2: errno
457 case B_TRUSS_POINT:
458 return ((arg2 == 0) ? 0 : set_errno((uint_t)arg2));
461 return (-1);
464 /*ARGSUSED*/
465 void
466 brand_solaris_copy_procdata(proc_t *child, proc_t *parent, struct brand *pbrand)
468 brand_proc_data_t *spd;
470 ASSERT(parent->p_brand == pbrand);
471 ASSERT(child->p_brand == pbrand);
472 ASSERT(parent->p_brand_data != NULL);
473 ASSERT(child->p_brand_data == NULL);
476 * Just duplicate all the proc data of the parent for the
477 * child
479 spd = kmem_alloc(sizeof (brand_proc_data_t), KM_SLEEP);
480 bcopy(parent->p_brand_data, spd, sizeof (brand_proc_data_t));
481 child->p_brand_data = spd;
484 static void
485 restoreexecenv(struct execenv *ep, stack_t *sp)
487 klwp_t *lwp = ttolwp(curthread);
489 setexecenv(ep);
490 lwp->lwp_sigaltstack.ss_sp = sp->ss_sp;
491 lwp->lwp_sigaltstack.ss_size = sp->ss_size;
492 lwp->lwp_sigaltstack.ss_flags = sp->ss_flags;
495 /*ARGSUSED*/
497 brand_solaris_elfexec(vnode_t *vp, execa_t *uap, uarg_t *args,
498 intpdata_t *idatap, int level, long *execsz, int setid, caddr_t exec_file,
499 cred_t *cred, int brand_action, struct brand *pbrand, char *bname,
500 char *brandlib, char *brandlib32, char *brandlinker, char *brandlinker32)
503 vnode_t *nvp;
504 Ehdr ehdr;
505 Addr uphdr_vaddr;
506 intptr_t voffset;
507 int interp;
508 int i, err;
509 struct execenv env;
510 struct execenv origenv;
511 stack_t orig_sigaltstack;
512 struct user *up = PTOU(curproc);
513 proc_t *p = ttoproc(curthread);
514 klwp_t *lwp = ttolwp(curthread);
515 brand_proc_data_t *spd;
516 brand_elf_data_t sed, *sedp;
517 char *linker;
518 uintptr_t lddata; /* lddata of executable's linker */
520 ASSERT(curproc->p_brand == pbrand);
521 ASSERT(curproc->p_brand_data != NULL);
523 spd = (brand_proc_data_t *)curproc->p_brand_data;
524 sedp = &spd->spd_elf_data;
526 args->brandname = bname;
529 * We will exec the brand library and then map in the target
530 * application and (optionally) the brand's default linker.
532 if (args->to_model == DATAMODEL_NATIVE) {
533 args->emulator = brandlib;
534 linker = brandlinker;
536 #if defined(_LP64)
537 else {
538 args->emulator = brandlib32;
539 linker = brandlinker32;
541 #endif /* _LP64 */
543 if ((err = lookupname(args->emulator, UIO_SYSSPACE, FOLLOW,
544 NULLVPP, &nvp)) != 0) {
545 uprintf("%s: not found.", args->emulator);
546 return (err);
550 * The following elf{32}exec call changes the execenv in the proc
551 * struct which includes changing the p_exec member to be the vnode
552 * for the brand library (e.g. /.native/usr/lib/foo_brand.so.1).
553 * We will eventually set the p_exec member to be the vnode for the new
554 * executable when we call setexecenv(). However, if we get an error
555 * before that call we need to restore the execenv to its original
556 * values so that when we return to the caller fop_close() works
557 * properly while cleaning up from the failed exec(). Restoring the
558 * original value will also properly decrement the 2nd VN_RELE that we
559 * took on the brand library.
561 origenv.ex_bssbase = p->p_bssbase;
562 origenv.ex_brkbase = p->p_brkbase;
563 origenv.ex_brksize = p->p_brksize;
564 origenv.ex_vp = p->p_exec;
565 orig_sigaltstack.ss_sp = lwp->lwp_sigaltstack.ss_sp;
566 orig_sigaltstack.ss_size = lwp->lwp_sigaltstack.ss_size;
567 orig_sigaltstack.ss_flags = lwp->lwp_sigaltstack.ss_flags;
569 if (args->to_model == DATAMODEL_NATIVE) {
570 err = elfexec(nvp, uap, args, idatap, INTP_MAXDEPTH + 1, execsz,
571 setid, exec_file, cred, brand_action);
573 #if defined(_LP64)
574 else {
575 err = elf32exec(nvp, uap, args, idatap, INTP_MAXDEPTH + 1,
576 execsz, setid, exec_file, cred, brand_action);
578 #endif /* _LP64 */
579 VN_RELE(nvp);
580 if (err != 0) {
581 restoreexecenv(&origenv, &orig_sigaltstack);
582 return (err);
586 * The u_auxv veCTors are set up by elfexec to point to the
587 * brand emulation library and linker. Save these so they can
588 * be copied to the specific brand aux vectors.
590 bzero(&sed, sizeof (sed));
591 for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
592 switch (up->u_auxv[i].a_type) {
593 case AT_SUN_LDDATA:
594 sed.sed_lddata = up->u_auxv[i].a_un.a_val;
595 break;
596 case AT_BASE:
597 sed.sed_base = up->u_auxv[i].a_un.a_val;
598 break;
599 case AT_ENTRY:
600 sed.sed_entry = up->u_auxv[i].a_un.a_val;
601 break;
602 case AT_PHDR:
603 sed.sed_phdr = up->u_auxv[i].a_un.a_val;
604 break;
605 case AT_PHENT:
606 sed.sed_phent = up->u_auxv[i].a_un.a_val;
607 break;
608 case AT_PHNUM:
609 sed.sed_phnum = up->u_auxv[i].a_un.a_val;
610 break;
611 default:
612 break;
615 /* Make sure the emulator has an entry point */
616 ASSERT(sed.sed_entry != 0);
617 ASSERT(sed.sed_phdr != 0);
619 bzero(&env, sizeof (env));
620 if (args->to_model == DATAMODEL_NATIVE) {
621 err = mapexec_brand(vp, args, &ehdr, &uphdr_vaddr,
622 &voffset, exec_file, &interp, &env.ex_bssbase,
623 &env.ex_brkbase, &env.ex_brksize, NULL);
625 #if defined(_LP64)
626 else {
627 Elf32_Ehdr ehdr32;
628 Elf32_Addr uphdr_vaddr32;
629 err = mapexec32_brand(vp, args, &ehdr32, &uphdr_vaddr32,
630 &voffset, exec_file, &interp, &env.ex_bssbase,
631 &env.ex_brkbase, &env.ex_brksize, NULL);
632 Ehdr32to64(&ehdr32, &ehdr);
634 if (uphdr_vaddr32 == (Elf32_Addr)-1)
635 uphdr_vaddr = (Addr)-1;
636 else
637 uphdr_vaddr = uphdr_vaddr32;
639 #endif /* _LP64 */
640 if (err != 0) {
641 restoreexecenv(&origenv, &orig_sigaltstack);
642 return (err);
646 * Save off the important properties of the executable. The
647 * brand library will ask us for this data later, when it is
648 * initializing and getting ready to transfer control to the
649 * brand application.
651 if (uphdr_vaddr == (Addr)-1)
652 sedp->sed_phdr = voffset + ehdr.e_phoff;
653 else
654 sedp->sed_phdr = voffset + uphdr_vaddr;
655 sedp->sed_entry = voffset + ehdr.e_entry;
656 sedp->sed_phent = ehdr.e_phentsize;
657 sedp->sed_phnum = ehdr.e_phnum;
659 if (interp) {
660 if (ehdr.e_type == ET_DYN) {
662 * This is a shared object executable, so we
663 * need to pick a reasonable place to put the
664 * heap. Just don't use the first page.
666 env.ex_brkbase = (caddr_t)PAGESIZE;
667 env.ex_bssbase = (caddr_t)PAGESIZE;
671 * If the program needs an interpreter (most do), map
672 * it in and store relevant information about it in the
673 * aux vector, where the brand library can find it.
675 if ((err = lookupname(linker, UIO_SYSSPACE,
676 FOLLOW, NULLVPP, &nvp)) != 0) {
677 uprintf("%s: not found.", brandlinker);
678 restoreexecenv(&origenv, &orig_sigaltstack);
679 return (err);
681 if (args->to_model == DATAMODEL_NATIVE) {
682 err = mapexec_brand(nvp, args, &ehdr,
683 &uphdr_vaddr, &voffset, exec_file, &interp,
684 NULL, NULL, NULL, &lddata);
686 #if defined(_LP64)
687 else {
688 Elf32_Ehdr ehdr32;
689 Elf32_Addr uphdr_vaddr32;
690 err = mapexec32_brand(nvp, args, &ehdr32,
691 &uphdr_vaddr32, &voffset, exec_file, &interp,
692 NULL, NULL, NULL, &lddata);
693 Ehdr32to64(&ehdr32, &ehdr);
695 if (uphdr_vaddr32 == (Elf32_Addr)-1)
696 uphdr_vaddr = (Addr)-1;
697 else
698 uphdr_vaddr = uphdr_vaddr32;
700 #endif /* _LP64 */
701 VN_RELE(nvp);
702 if (err != 0) {
703 restoreexecenv(&origenv, &orig_sigaltstack);
704 return (err);
708 * Now that we know the base address of the brand's
709 * linker, place it in the aux vector.
711 sedp->sed_base = voffset;
712 sedp->sed_ldentry = voffset + ehdr.e_entry;
713 sedp->sed_lddata = voffset + lddata;
714 } else {
716 * This program has no interpreter. The brand library
717 * will jump to the address in the AT_SUN_BRAND_LDENTRY
718 * aux vector, so in this case, put the entry point of
719 * the main executable there.
721 if (ehdr.e_type == ET_EXEC) {
723 * An executable with no interpreter, this must
724 * be a statically linked executable, which
725 * means we loaded it at the address specified
726 * in the elf header, in which case the e_entry
727 * field of the elf header is an absolute
728 * address.
730 sedp->sed_ldentry = ehdr.e_entry;
731 sedp->sed_entry = ehdr.e_entry;
732 sedp->sed_lddata = 0;
733 sedp->sed_base = 0;
734 } else {
736 * A shared object with no interpreter, we use
737 * the calculated address from above.
739 sedp->sed_ldentry = sedp->sed_entry;
740 sedp->sed_entry = 0;
741 sedp->sed_phdr = 0;
742 sedp->sed_phent = 0;
743 sedp->sed_phnum = 0;
744 sedp->sed_lddata = 0;
745 sedp->sed_base = voffset;
747 if (ehdr.e_type == ET_DYN) {
749 * Delay setting the brkbase until the
750 * first call to brk(); see elfexec()
751 * for details.
753 env.ex_bssbase = (caddr_t)0;
754 env.ex_brkbase = (caddr_t)0;
755 env.ex_brksize = 0;
760 env.ex_magic = elfmagic;
761 env.ex_vp = vp;
762 setexecenv(&env);
765 * It's time to manipulate the process aux vectors. First
766 * we need to update the AT_SUN_AUXFLAGS aux vector to set
767 * the AF_SUN_NOPLM flag.
769 if (args->to_model == DATAMODEL_NATIVE) {
770 auxv_t auxflags_auxv;
772 if (copyin(args->auxp_auxflags, &auxflags_auxv,
773 sizeof (auxflags_auxv)) != 0)
774 return (EFAULT);
776 ASSERT(auxflags_auxv.a_type == AT_SUN_AUXFLAGS);
777 auxflags_auxv.a_un.a_val |= AF_SUN_NOPLM;
778 if (copyout(&auxflags_auxv, args->auxp_auxflags,
779 sizeof (auxflags_auxv)) != 0)
780 return (EFAULT);
782 #if defined(_LP64)
783 else {
784 auxv32_t auxflags_auxv32;
786 if (copyin(args->auxp_auxflags, &auxflags_auxv32,
787 sizeof (auxflags_auxv32)) != 0)
788 return (EFAULT);
790 ASSERT(auxflags_auxv32.a_type == AT_SUN_AUXFLAGS);
791 auxflags_auxv32.a_un.a_val |= AF_SUN_NOPLM;
792 if (copyout(&auxflags_auxv32, args->auxp_auxflags,
793 sizeof (auxflags_auxv32)) != 0)
794 return (EFAULT);
796 #endif /* _LP64 */
798 /* Second, copy out the brand specific aux vectors. */
799 if (args->to_model == DATAMODEL_NATIVE) {
800 auxv_t brand_auxv[] = {
801 { AT_SUN_BRAND_AUX1, 0 },
802 { AT_SUN_BRAND_AUX2, 0 },
803 { AT_SUN_BRAND_AUX3, 0 }
806 ASSERT(brand_auxv[0].a_type ==
807 AT_SUN_BRAND_COMMON_LDDATA);
808 brand_auxv[0].a_un.a_val = sed.sed_lddata;
810 if (copyout(&brand_auxv, args->auxp_brand,
811 sizeof (brand_auxv)) != 0)
812 return (EFAULT);
814 #if defined(_LP64)
815 else {
816 auxv32_t brand_auxv32[] = {
817 { AT_SUN_BRAND_AUX1, 0 },
818 { AT_SUN_BRAND_AUX2, 0 },
819 { AT_SUN_BRAND_AUX3, 0 }
822 ASSERT(brand_auxv32[0].a_type == AT_SUN_BRAND_COMMON_LDDATA);
823 brand_auxv32[0].a_un.a_val = (uint32_t)sed.sed_lddata;
824 if (copyout(&brand_auxv32, args->auxp_brand,
825 sizeof (brand_auxv32)) != 0)
826 return (EFAULT);
828 #endif /* _LP64 */
831 * Third, the /proc aux vectors set up by elfexec() point to
832 * brand emulation library and it's linker. Copy these to the
833 * /proc brand specific aux vector, and update the regular
834 * /proc aux vectors to point to the executable (and it's
835 * linker). This will enable debuggers to access the
836 * executable via the usual /proc or elf notes aux vectors.
838 * The brand emulation library's linker will get it's aux
839 * vectors off the stack, and then update the stack with the
840 * executable's aux vectors before jumping to the executable's
841 * linker.
843 * Debugging the brand emulation library must be done from
844 * the global zone, where the librtld_db module knows how to
845 * fetch the brand specific aux vectors to access the brand
846 * emulation libraries linker.
848 for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
849 ulong_t val;
851 switch (up->u_auxv[i].a_type) {
852 case AT_SUN_BRAND_COMMON_LDDATA:
853 up->u_auxv[i].a_un.a_val = sed.sed_lddata;
854 continue;
855 case AT_BASE:
856 val = sedp->sed_base;
857 break;
858 case AT_ENTRY:
859 val = sedp->sed_entry;
860 break;
861 case AT_PHDR:
862 val = sedp->sed_phdr;
863 break;
864 case AT_PHENT:
865 val = sedp->sed_phent;
866 break;
867 case AT_PHNUM:
868 val = sedp->sed_phnum;
869 break;
870 case AT_SUN_LDDATA:
871 val = sedp->sed_lddata;
872 break;
873 default:
874 continue;
877 up->u_auxv[i].a_un.a_val = val;
878 if (val == 0) {
879 /* Hide the entry for static binaries */
880 up->u_auxv[i].a_type = AT_IGNORE;
885 * The last thing we do here is clear spd->spd_handler. This
886 * is important because if we're already a branded process and
887 * if this exec succeeds, there is a window between when the
888 * exec() first returns to the userland of the new process and
889 * when our brand library get's initialized, during which we
890 * don't want system calls to be re-directed to our brand
891 * library since it hasn't been initialized yet.
893 spd->spd_handler = NULL;
895 return (0);
898 void
899 brand_solaris_exec(struct brand *pbrand)
901 brand_proc_data_t *spd = curproc->p_brand_data;
903 ASSERT(curproc->p_brand == pbrand);
904 ASSERT(curproc->p_brand_data != NULL);
905 ASSERT(ttolwp(curthread)->lwp_brand != NULL);
908 * We should only be called from exec(), when we know the process
909 * is single-threaded.
911 ASSERT(curproc->p_tlist == curproc->p_tlist->t_forw);
913 /* Upon exec, reset our lwp brand data. */
914 (void) brand_solaris_freelwp(ttolwp(curthread), pbrand);
915 (void) brand_solaris_initlwp(ttolwp(curthread), pbrand);
918 * Upon exec, reset all the proc brand data, except for the elf
919 * data associated with the executable we are exec'ing.
921 spd->spd_handler = NULL;
925 brand_solaris_fini(char **emul_table, struct modlinkage *modlinkage,
926 struct brand *pbrand)
928 int err;
931 * If there are any zones using this brand, we can't allow it
932 * to be unloaded.
934 if (brand_zone_count(pbrand))
935 return (EBUSY);
937 kmem_free(*emul_table, NSYSCALL);
938 *emul_table = NULL;
940 err = mod_remove(modlinkage);
941 if (err)
942 cmn_err(CE_WARN, "Couldn't unload brand module");
944 return (err);
947 /*ARGSUSED*/
948 void
949 brand_solaris_forklwp(klwp_t *p, klwp_t *c, struct brand *pbrand)
951 ASSERT(p->lwp_procp->p_brand == pbrand);
952 ASSERT(c->lwp_procp->p_brand == pbrand);
954 ASSERT(p->lwp_procp->p_brand_data != NULL);
955 ASSERT(c->lwp_procp->p_brand_data != NULL);
958 * Both LWPs have already had been initialized via
959 * brand_solaris_initlwp().
961 ASSERT(p->lwp_brand != NULL);
962 ASSERT(c->lwp_brand != NULL);
965 /*ARGSUSED*/
966 void
967 brand_solaris_freelwp(klwp_t *l, struct brand *pbrand)
969 ASSERT(l->lwp_procp->p_brand == pbrand);
970 ASSERT(l->lwp_procp->p_brand_data != NULL);
971 ASSERT(l->lwp_brand != NULL);
972 l->lwp_brand = NULL;
975 /*ARGSUSED*/
977 brand_solaris_initlwp(klwp_t *l, struct brand *pbrand)
979 ASSERT(l->lwp_procp->p_brand == pbrand);
980 ASSERT(l->lwp_procp->p_brand_data != NULL);
981 ASSERT(l->lwp_brand == NULL);
982 l->lwp_brand = (void *)-1;
983 return (0);
986 /*ARGSUSED*/
987 void
988 brand_solaris_lwpexit(klwp_t *l, struct brand *pbrand)
990 proc_t *p = l->lwp_procp;
992 ASSERT(l->lwp_procp->p_brand == pbrand);
993 ASSERT(l->lwp_procp->p_brand_data != NULL);
994 ASSERT(l->lwp_brand != NULL);
997 * We should never be called for the last thread in a process.
998 * (That case is handled by brand_solaris_proc_exit().)
999 * Therefore this lwp must be exiting from a multi-threaded
1000 * process.
1002 ASSERT(p->p_tlist != p->p_tlist->t_forw);
1004 l->lwp_brand = NULL;
1007 /*ARGSUSED*/
1008 void
1009 brand_solaris_proc_exit(struct proc *p, klwp_t *l, struct brand *pbrand)
1011 ASSERT(p->p_brand == pbrand);
1012 ASSERT(p->p_brand_data != NULL);
1015 * When called from proc_exit(), we know that process is
1016 * single-threaded and free our lwp brand data.
1017 * otherwise just free p_brand_data and return.
1019 if (l != NULL) {
1020 ASSERT(p->p_tlist == p->p_tlist->t_forw);
1021 ASSERT(p->p_tlist->t_lwp == l);
1022 (void) brand_solaris_freelwp(l, pbrand);
1025 /* upon exit, free our proc brand data */
1026 kmem_free(p->p_brand_data, sizeof (brand_proc_data_t));
1027 p->p_brand_data = NULL;
1030 void
1031 brand_solaris_setbrand(proc_t *p, struct brand *pbrand)
1033 ASSERT(p->p_brand == pbrand);
1034 ASSERT(p->p_brand_data == NULL);
1037 * We should only be called from exec(), when we know the process
1038 * is single-threaded.
1040 ASSERT(p->p_tlist == p->p_tlist->t_forw);
1042 p->p_brand_data = kmem_zalloc(sizeof (brand_proc_data_t), KM_SLEEP);
1043 (void) brand_solaris_initlwp(p->p_tlist->t_lwp, pbrand);