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 /*
  23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 #include <sys/errno.h>
  27 #include <sys/exec.h>
  28 #include <sys/kmem.h>
  29 #include <sys/modctl.h>
  30 #include <sys/model.h>
  31 #include <sys/proc.h>
  32 #include <sys/syscall.h>
  33 #include <sys/systm.h>
  34 #include <sys/thread.h>
  35 #include <sys/cmn_err.h>
  36 #include <sys/archsystm.h>
  37 #include <sys/pathname.h>
  38 
  39 #include <sys/machbrand.h>
  40 #include <sys/brand.h>
  41 #include "sn1_brand.h"
  42 
  43 char *sn1_emulation_table = NULL;
  44 
  45 void    sn1_init_brand_data(zone_t *);
  46 void    sn1_free_brand_data(zone_t *);
  47 void    sn1_setbrand(proc_t *);
  48 int     sn1_getattr(zone_t *, int, void *, size_t *);
  49 int     sn1_setattr(zone_t *, int, void *, size_t);
  50 int     sn1_brandsys(int, int64_t *, uintptr_t, uintptr_t, uintptr_t,
  51                 uintptr_t, uintptr_t, uintptr_t);
  52 void    sn1_copy_procdata(proc_t *, proc_t *);
  53 void    sn1_proc_exit(struct proc *, klwp_t *);
  54 void    sn1_exec();
  55 int     sn1_initlwp(klwp_t *);
  56 void    sn1_forklwp(klwp_t *, klwp_t *);
  57 void    sn1_freelwp(klwp_t *);
  58 void    sn1_lwpexit(klwp_t *);
  59 int     sn1_elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
  60         long *, int, caddr_t, cred_t *, int);
  61 
  62 /* sn1 brand */
  63 struct brand_ops sn1_brops = {
  64         sn1_init_brand_data,
  65         sn1_free_brand_data,
  66         sn1_brandsys,
  67         sn1_setbrand,
  68         sn1_getattr,
  69         sn1_setattr,
  70         sn1_copy_procdata,
  71         sn1_proc_exit,
  72         sn1_exec,
  73         lwp_setrval,
  74         sn1_initlwp,
  75         sn1_forklwp,
  76         sn1_freelwp,
  77         sn1_lwpexit,
  78         sn1_elfexec,
  79         NULL,
  80         NULL,
  81         NSIG,
  82 };
  83 
  84 #ifdef  sparc
  85 
  86 struct brand_mach_ops sn1_mops = {
  87         sn1_brand_syscall_callback,
  88         sn1_brand_syscall32_callback
  89 };
  90 
  91 #else   /* sparc */
  92 
  93 #ifdef  __amd64
  94 
  95 struct brand_mach_ops sn1_mops = {
  96         sn1_brand_sysenter_callback,
  97         NULL,
  98         sn1_brand_int91_callback,
  99         sn1_brand_syscall_callback,
 100         sn1_brand_syscall32_callback,
 101         NULL
 102 };
 103 
 104 #else   /* ! __amd64 */
 105 
 106 struct brand_mach_ops sn1_mops = {
 107         sn1_brand_sysenter_callback,
 108         NULL,
 109         NULL,
 110         sn1_brand_syscall_callback,
 111         NULL,
 112         NULL
 113 };
 114 #endif  /* __amd64 */
 115 
 116 #endif  /* _sparc */
 117 
 118 struct brand    sn1_brand = {
 119         BRAND_VER_1,
 120         "sn1",
 121         &sn1_brops,
 122         &sn1_mops
 123 };
 124 
 125 static struct modlbrand modlbrand = {
 126         &mod_brandops,              /* type of module */
 127         "Solaris N-1 Brand",    /* description of module */
 128         &sn1_brand          /* driver ops */
 129 };
 130 
 131 static struct modlinkage modlinkage = {
 132         MODREV_1, (void *)&modlbrand, NULL
 133 };
 134 
 135 void
 136 sn1_setbrand(proc_t *p)
 137 {
 138         brand_solaris_setbrand(p, &sn1_brand);
 139 }
 140 
 141 /* ARGSUSED */
 142 int
 143 sn1_getattr(zone_t *zone, int attr, void *buf, size_t *bufsize)
 144 {
 145         return (EINVAL);
 146 }
 147 
 148 /* ARGSUSED */
 149 int
 150 sn1_setattr(zone_t *zone, int attr, void *buf, size_t bufsize)
 151 {
 152         return (EINVAL);
 153 }
 154 
 155 /*ARGSUSED*/
 156 int
 157 sn1_brandsys(int cmd, int64_t *rval, uintptr_t arg1, uintptr_t arg2,
 158     uintptr_t arg3, uintptr_t arg4, uintptr_t arg5, uintptr_t arg6)
 159 {
 160         int     res;
 161 
 162         *rval = 0;
 163 
 164         res = brand_solaris_cmd(cmd, arg1, arg2, arg3, &sn1_brand, SN1_VERSION);
 165         if (res >= 0)
 166                 return (res);
 167 
 168         return (EINVAL);
 169 }
 170 
 171 void
 172 sn1_copy_procdata(proc_t *child, proc_t *parent)
 173 {
 174         brand_solaris_copy_procdata(child, parent, &sn1_brand);
 175 }
 176 
 177 void
 178 sn1_proc_exit(struct proc *p, klwp_t *l)
 179 {
 180         brand_solaris_proc_exit(p, l, &sn1_brand);
 181 }
 182 
 183 void
 184 sn1_exec()
 185 {
 186         brand_solaris_exec(&sn1_brand);
 187 }
 188 
 189 int
 190 sn1_initlwp(klwp_t *l)
 191 {
 192         return (brand_solaris_initlwp(l, &sn1_brand));
 193 }
 194 
 195 void
 196 sn1_forklwp(klwp_t *p, klwp_t *c)
 197 {
 198         brand_solaris_forklwp(p, c, &sn1_brand);
 199 }
 200 
 201 void
 202 sn1_freelwp(klwp_t *l)
 203 {
 204         brand_solaris_freelwp(l, &sn1_brand);
 205 }
 206 
 207 void
 208 sn1_lwpexit(klwp_t *l)
 209 {
 210         brand_solaris_lwpexit(l, &sn1_brand);
 211 }
 212 
 213 /*ARGSUSED*/
 214 void
 215 sn1_free_brand_data(zone_t *zone)
 216 {
 217 }
 218 
 219 /*ARGSUSED*/
 220 void
 221 sn1_init_brand_data(zone_t *zone)
 222 {
 223 }
 224 
 225 int
 226 sn1_elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap,
 227         int level, long *execsz, int setid, caddr_t exec_file, cred_t *cred,
 228         int brand_action)
 229 {
 230         return (brand_solaris_elfexec(vp, uap, args, idatap, level, execsz,
 231             setid, exec_file, cred, brand_action, &sn1_brand, SN1_BRANDNAME,
 232             SN1_LIB, SN1_LIB32, SN1_LINKER, SN1_LINKER32));
 233 }
 234 
 235 int
 236 _init(void)
 237 {
 238         int err;
 239 
 240         /*
 241          * Set up the table indicating which system calls we want to
 242          * interpose on.  We should probably build this automatically from
 243          * a list of system calls that is shared with the user-space
 244          * library.
 245          */
 246         sn1_emulation_table = kmem_zalloc(NSYSCALL, KM_SLEEP);
 247         sn1_emulation_table[SYS_read] = 1;                      /*   3 */
 248         sn1_emulation_table[SYS_write] = 1;                     /*   4 */
 249         sn1_emulation_table[SYS_time] = 1;                      /*  13 */
 250         sn1_emulation_table[SYS_getpid] = 1;                    /*  20 */
 251         sn1_emulation_table[SYS_mount] = 1;                     /*  21 */
 252         sn1_emulation_table[SYS_getuid] = 1;                    /*  24 */
 253         sn1_emulation_table[SYS_times] = 1;                     /*  43 */
 254         sn1_emulation_table[SYS_getgid] = 1;                    /*  47 */
 255         sn1_emulation_table[SYS_utssys] = 1;                    /*  57 */
 256         sn1_emulation_table[SYS_waitid] = 1;                    /* 107 */
 257         sn1_emulation_table[SYS_uname] = 1;                     /* 135 */
 258 
 259         err = mod_install(&modlinkage);
 260         if (err) {
 261                 cmn_err(CE_WARN, "Couldn't install brand module");
 262                 kmem_free(sn1_emulation_table, NSYSCALL);
 263         }
 264 
 265         return (err);
 266 }
 267 
 268 int
 269 _info(struct modinfo *modinfop)
 270 {
 271         return (mod_info(&modlinkage, modinfop));
 272 }
 273 
 274 int
 275 _fini(void)
 276 {
 277         return (brand_solaris_fini(&sn1_emulation_table, &modlinkage,
 278             &sn1_brand));
 279 }