brand.c revision 59f2ff5c96304fcfa3d97e66fbe1c521f42ac103
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * CDDL HEADER START
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * The contents of this file are subject to the terms of the
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Common Development and Distribution License (the "License").
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * You may not use this file except in compliance with the License.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * See the License for the specific language governing permissions
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * and limitations under the License.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * When distributing Covered Code, include this CDDL HEADER in each
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * If applicable, add the following below this CDDL HEADER, with the
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * fields enclosed by brackets "[]" replaced with your own identifying
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * information: Portions Copyright [yyyy] [name of copyright owner]
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * CDDL HEADER END
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Use is subject to license terms.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn#pragma ident "%Z%%M% %I% %E% SMI"
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn#if defined(__sparcv9)
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp#else /* !__sparcv9 */
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp#endif /* !__sparcv9 */
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Used to maintain a list of all the brands currently loaded into the
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * Used to enable brand platform specific interposition code
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edpextern void brand_plat_interposition_init(void);
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * This lock protects the integrity of the brand list.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "Branded zones are not allowed on labeled systems.");
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "brand '%s' was built to run on older versions "
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "of Solaris.",
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "brand '%s' was built to run on a newer version "
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "of Solaris.",
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn /* Sanity checks */
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn /* Add the brand to the list of loaded brands. */
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Check to be sure we haven't already registered this brand.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn for (scan = brand_list; scan != NULL; scan = scan->bl_next) {
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn if (strcmp(brand->b_name, scan->bl_brand->b_name) == 0) {
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "Invalid attempt to load a second instance of "
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn return (0);
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * The kernel module implementing this brand is being unloaded, so remove
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * it from the list of active brands.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn /* Sanity checks */
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn for (list = brand_list; list != NULL; list = list->bl_next) {
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn cmn_err(CE_WARN, "Brand %s wasn't registered", brand->b_name);
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn cmn_err(CE_WARN, "Unregistering brand %s which is still in use",
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn /* Remove brand from the list */
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn return (0);
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Record that a zone of this brand has been instantiated. If the kernel
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * module implementing this brand's functionality is not present, this
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * routine attempts to load the module as a side effect.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn "Branded zones are not allowed on labeled systems.");
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * We make at most two passes through this loop. The first time
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * through, we're looking to see if this is a new user of an
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * already loaded brand. If the brand hasn't been loaded, we
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * call ddi_modopen() to force it to be loaded and then make a
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * second pass through the list of brands. If we don't find the
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * brand the second time through it means that the modname
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * specified in the brand_attr structure doesn't provide the brand
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * specified in the brandname field. This would suggest a bug in
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * the brand's config.xml file. We close the module and return
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * 'NULL' to the caller.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn for (;;) {
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Search list of loaded brands
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * We didn't find that the requested brand has been loaded
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * yet, so we trigger the load of the appropriate kernel
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * module and search the list again.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * If we found the matching brand, bump its reference count.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * Return the number of zones currently using this brand.
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn for (list = brand_list; list != NULL; list = list->bl_next) {
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * We should only be called from exec(), when we know the process
9acbbeaf2a1ffe5c14b244867d427714fab43c5cnn * is single-threaded.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp#if defined(__sparcv9)
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * Currently, only sparc has platform level brand syscall interposition.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * On x86 we're able to enable syscall interposition on a per-cpu basis
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * when a branded thread is scheduled to run on a cpu.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp/* Local variables needed for dynamic syscall interposition support */
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp/* Trap Table syscall entry hot patch points */
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edpextern void syscall_trap_patch_point(void);
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edpextern void syscall_trap32_patch_point(void);
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp/* Alternate syscall entry handlers used when branded zones are running */
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edpextern void syscall_wrapper(void);
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edpextern void syscall_wrapper32(void);
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp/* Macros used to facilitate sparcv9 instruction generation */
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp ((((uintptr_t)(to) - (uintptr_t)(from)) >> 2) & 0x3fffff)
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp mutex_init(&brand_interposition_lock, NULL, MUTEX_DEFAULT, NULL);
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp/*ARGSUSED*/
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * This is the first branded zone that is being enabled on
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * this system.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * Before we hot patch the kernel save the current instructions
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * so that we can restore them if all branded zones on the
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * system are shutdown.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * Modify the trap table at the patch points.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * We basically replace the first instruction at the patch
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * point with a ba,a instruction that will transfer control
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * to syscall_wrapper or syscall_wrapper32 for 64-bit and
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * 32-bit syscalls respectively. It's important to note that
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * the annul bit is set in the branch so we don't execute
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * the instruction directly following the one we're patching
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * during the branch's delay slot.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * It also doesn't matter that we're not atomically updating both
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * the 64 and 32 bit syscall paths at the same time since there's
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * no actual branded processes running on the system yet.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp hot_patch_kernel_text((caddr_t)syscall_trap_patch_point,
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp BA_A_INSTR | DISP22(syscall_trap_patch_point, syscall_wrapper),
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp hot_patch_kernel_text((caddr_t)syscall_trap32_patch_point,
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp BA_A_INSTR | DISP22(syscall_trap32_patch_point, syscall_wrapper32),
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp/*ARGSUSED*/
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * The last branded zone on this system has been shutdown.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * Restore the original instructions at the trap table syscall
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * patch points to disable the brand syscall interposition
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp * mechanism.
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp hot_patch_kernel_text((caddr_t)syscall_trap_patch_point,
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp hot_patch_kernel_text((caddr_t)syscall_trap32_patch_point,
59f2ff5c96304fcfa3d97e66fbe1c521f42ac103edp#endif /* __sparcv9 */