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]
22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
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>
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
= {
50 * Used to maintain a list of all the brands currently loaded into the
55 struct brand_list
*bl_next
;
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
;
69 mutex_init(&brand_list_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
70 p0
.p_brand
= &native_brand
;
74 brand_register(brand_t
*brand
)
76 struct brand_list
*list
, *scan
;
81 if (brand
->b_version
!= SUPPORTED_BRAND_VERSION
) {
82 if (brand
->b_version
< SUPPORTED_BRAND_VERSION
) {
84 "brand '%s' was built to run on older versions "
89 "brand '%s' was built to run on a newer version "
97 if (brand
->b_name
== NULL
|| brand
->b_ops
== NULL
||
98 brand
->b_ops
->b_brandsys
== NULL
) {
99 cmn_err(CE_WARN
, "Malformed brand");
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) {
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
));
123 list
->bl_brand
= brand
;
125 list
->bl_next
= brand_list
;
128 mutex_exit(&brand_list_lock
);
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
;
143 if (brand
== NULL
|| brand
->b_name
== NULL
) {
144 cmn_err(CE_WARN
, "Malformed brand");
149 mutex_enter(&brand_list_lock
);
151 for (list
= brand_list
; list
!= NULL
; list
= list
->bl_next
) {
152 if (list
->bl_brand
== brand
)
158 cmn_err(CE_WARN
, "Brand %s wasn't registered", brand
->b_name
);
159 mutex_exit(&brand_list_lock
);
163 if (list
->bl_refcnt
> 0) {
164 cmn_err(CE_WARN
, "Unregistering brand %s which is still in use",
166 mutex_exit(&brand_list_lock
);
170 /* Remove brand from the list */
172 prev
->bl_next
= list
->bl_next
;
174 brand_list
= list
->bl_next
;
177 mutex_exit(&brand_list_lock
);
179 kmem_free(list
, sizeof (struct brand_list
));
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.
190 brand_register_zone(struct brand_attr
*attr
)
192 struct brand_list
*l
= NULL
;
193 ddi_modhandle_t hdl
= NULL
;
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.
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)
218 if ((l
!= NULL
) || (hdl
!= NULL
))
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
);
238 * If we found the matching brand, bump its reference count.
243 mutex_exit(&brand_list_lock
);
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
;
260 mutex_enter(&brand_list_lock
);
261 for (l
= brand_list
; l
!= NULL
; l
= l
->bl_next
)
262 if (l
->bl_brand
== bp
) {
266 mutex_exit(&brand_list_lock
);
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);
284 mutex_exit(&brand_list_lock
);
288 brand_setbrand(proc_t
*p
)
290 brand_t
*bp
= p
->p_zone
->zone_brand
;
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
);
302 ASSERT(PROC_IS_BRANDED(p
));
303 BROP(p
)->b_setbrand(p
);
307 brand_clearbrand(proc_t
*p
, boolean_t no_lwps
)
309 brand_t
*bp
= p
->p_zone
->zone_brand
;
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.
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.
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
;
358 * Return -1 if the cmd was not handled by this function.
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
;
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
)
380 /* For all other operations this must be a branded process. */
381 if (p
->p_brand
== &native_brand
)
384 ASSERT(p
->p_brand
== pbrand
);
385 ASSERT(p
->p_brand_data
!= NULL
);
387 spd
= (brand_proc_data_t
*)p
->p_brand_data
;
391 err
= exec_common((char *)arg1
, (const char **)arg2
,
392 (const char **)arg3
, EBA_NATIVE
);
396 * Get the address of the user-space system call handler from
397 * the user process and attach it to the proc structure.
400 if (p
->p_model
== DATAMODEL_NATIVE
) {
401 if (copyin((void *)arg1
, ®
, sizeof (reg
)) != 0)
406 brand_common_reg32_t reg32
;
408 if (copyin((void *)arg1
, ®32
, sizeof (reg32
)) != 0)
410 reg
.sbr_version
= reg32
.sbr_version
;
411 reg
.sbr_handler
= (caddr_t
)(uintptr_t)reg32
.sbr_handler
;
415 if (reg
.sbr_version
!= brandvers
)
417 spd
->spd_handler
= reg
.sbr_handler
;
421 if (p
->p_model
== DATAMODEL_NATIVE
) {
422 if (copyout(&spd
->spd_elf_data
, (void *)arg1
,
423 sizeof (brand_elf_data_t
)) != 0)
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
))
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
452 * Common arguments seen with truss are:
454 * arg1: syscall number
458 return ((arg2
== 0) ? 0 : set_errno((uint_t
)arg2
));
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
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
;
485 restoreexecenv(struct execenv
*ep
, stack_t
*sp
)
487 klwp_t
*lwp
= ttolwp(curthread
);
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
;
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
)
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
;
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
;
538 args
->emulator
= brandlib32
;
539 linker
= brandlinker32
;
543 if ((err
= lookupname(args
->emulator
, UIO_SYSSPACE
, FOLLOW
,
544 NULLVPP
, &nvp
)) != 0) {
545 uprintf("%s: not found.", args
->emulator
);
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
);
575 err
= elf32exec(nvp
, uap
, args
, idatap
, INTP_MAXDEPTH
+ 1,
576 execsz
, setid
, exec_file
, cred
, brand_action
);
581 restoreexecenv(&origenv
, &orig_sigaltstack
);
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
) {
594 sed
.sed_lddata
= up
->u_auxv
[i
].a_un
.a_val
;
597 sed
.sed_base
= up
->u_auxv
[i
].a_un
.a_val
;
600 sed
.sed_entry
= up
->u_auxv
[i
].a_un
.a_val
;
603 sed
.sed_phdr
= up
->u_auxv
[i
].a_un
.a_val
;
606 sed
.sed_phent
= up
->u_auxv
[i
].a_un
.a_val
;
609 sed
.sed_phnum
= up
->u_auxv
[i
].a_un
.a_val
;
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
);
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;
637 uphdr_vaddr
= uphdr_vaddr32
;
641 restoreexecenv(&origenv
, &orig_sigaltstack
);
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
651 if (uphdr_vaddr
== (Addr
)-1)
652 sedp
->sed_phdr
= voffset
+ ehdr
.e_phoff
;
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
;
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
);
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
);
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;
698 uphdr_vaddr
= uphdr_vaddr32
;
703 restoreexecenv(&origenv
, &orig_sigaltstack
);
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
;
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
730 sedp
->sed_ldentry
= ehdr
.e_entry
;
731 sedp
->sed_entry
= ehdr
.e_entry
;
732 sedp
->sed_lddata
= 0;
736 * A shared object with no interpreter, we use
737 * the calculated address from above.
739 sedp
->sed_ldentry
= sedp
->sed_entry
;
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()
753 env
.ex_bssbase
= (caddr_t
)0;
754 env
.ex_brkbase
= (caddr_t
)0;
760 env
.ex_magic
= elfmagic
;
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)
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)
784 auxv32_t auxflags_auxv32
;
786 if (copyin(args
->auxp_auxflags
, &auxflags_auxv32
,
787 sizeof (auxflags_auxv32
)) != 0)
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)
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)
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)
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
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
++) {
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
;
856 val
= sedp
->sed_base
;
859 val
= sedp
->sed_entry
;
862 val
= sedp
->sed_phdr
;
865 val
= sedp
->sed_phent
;
868 val
= sedp
->sed_phnum
;
871 val
= sedp
->sed_lddata
;
877 up
->u_auxv
[i
].a_un
.a_val
= val
;
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
;
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
)
931 * If there are any zones using this brand, we can't allow it
934 if (brand_zone_count(pbrand
))
937 kmem_free(*emul_table
, NSYSCALL
);
940 err
= mod_remove(modlinkage
);
942 cmn_err(CE_WARN
, "Couldn't unload brand module");
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
);
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
);
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;
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
1002 ASSERT(p
->p_tlist
!= p
->p_tlist
->t_forw
);
1004 l
->lwp_brand
= NULL
;
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.
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
;
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
);