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 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Copyright 2015 Joyent, Inc.
  28  */
  29 
  30 #ifndef _LX_BRAND_H
  31 #define _LX_BRAND_H
  32 
  33 #ifndef _ASM
  34 #include <sys/types.h>
  35 #include <sys/cpuvar.h>
  36 #include <sys/zone.h>
  37 #include <sys/ksocket.h>
  38 #endif
  39 
  40 #ifdef  __cplusplus
  41 extern "C" {
  42 #endif
  43 
  44 #define LX_BRANDNAME    "lx"
  45 
  46 /*
  47  * Brand uname info
  48  */
  49 #define LX_UNAME_SYSNAME        "Linux"
  50 #define LX_UNAME_RELEASE_2_6    "2.6.18"
  51 #define LX_UNAME_RELEASE_2_4    "2.4.21"
  52 #define LX_UNAME_VERSION        "BrandZ virtual linux"
  53 #define LX_UNAME_MACHINE32      "i686"
  54 #define LX_UNAME_MACHINE64      "x86_64"
  55 
  56 #define LX_LIB_PATH32   "/native/usr/lib/lx_brand.so.1"
  57 #define LX_LIB_PATH64   "/native/usr/lib/amd64/lx_brand.so.1"
  58 
  59 #if defined(_LP64)
  60 #define LX_LIB_PATH             LX_LIB_PATH64
  61 #define LX_UNAME_MACHINE        LX_UNAME_MACHINE64
  62 #else
  63 #define LX_LIB_PATH             LX_LIB_PATH32
  64 #define LX_UNAME_MACHINE        LX_UNAME_MACHINE32
  65 #endif
  66 
  67 /*
  68  * This must be large enough for both the 32-bit table and 64-bit table.
  69  */
  70 #define LX_NSYSCALLS            358
  71 
  72 /*
  73  * brand(2) subcommands
  74  *
  75  * Everything >= 128 is a brand-specific subcommand.
  76  * > 192 is reserved for in-kernel emulated system calls.
  77  */
  78 #define B_LPID_TO_SPAIR         128
  79 #define B_GET_CURRENT_CONTEXT   129
  80 #define B_EMULATION_DONE        130
  81 #define B_PTRACE_KERNEL         131
  82 #define B_SET_AFFINITY_MASK     132
  83 #define B_GET_AFFINITY_MASK     133
  84 #define B_PTRACE_CLONE_BEGIN    134
  85 #define B_PTRACE_STOP_FOR_OPT   135
  86 #define B_UNSUPPORTED           136
  87 #define B_STORE_ARGS            137
  88 #define B_GETPID                138
  89 #define B_JUMP_TO_LINUX         139
  90 #define B_SET_THUNK_PID         140
  91 #define B_EXIT_AS_SIG           141
  92 #define B_HELPER_WAITID         142
  93 #define B_HELPER_CLONE          143
  94 #define B_HELPER_SETGROUPS      144
  95 #define B_HELPER_SIGQUEUE       145
  96 #define B_HELPER_TGSIGQUEUE     146
  97 #define B_SET_NATIVE_STACK      147
  98 #define B_SIGEV_THREAD_ID       148
  99 
 100 #ifndef _ASM
 101 /*
 102  * Support for Linux PTRACE_SETOPTIONS handling.
 103  */
 104 typedef enum lx_ptrace_options {
 105         LX_PTRACE_O_TRACESYSGOOD =      0x0001,
 106         LX_PTRACE_O_TRACEFORK =         0x0002,
 107         LX_PTRACE_O_TRACEVFORK =        0x0004,
 108         LX_PTRACE_O_TRACECLONE =        0x0008,
 109         LX_PTRACE_O_TRACEEXEC =         0x0010,
 110         LX_PTRACE_O_TRACEVFORKDONE =    0x0020,
 111         LX_PTRACE_O_TRACEEXIT =         0x0040,
 112         LX_PTRACE_O_TRACESECCOMP =      0x0080
 113 } lx_ptrace_options_t;
 114 
 115 #define LX_PTRACE_O_ALL                                                 \
 116         (LX_PTRACE_O_TRACESYSGOOD | LX_PTRACE_O_TRACEFORK |             \
 117         LX_PTRACE_O_TRACEVFORK | LX_PTRACE_O_TRACECLONE |               \
 118         LX_PTRACE_O_TRACEEXEC | LX_PTRACE_O_TRACEVFORKDONE |            \
 119         LX_PTRACE_O_TRACEEXIT | LX_PTRACE_O_TRACESECCOMP)
 120 #endif /* !_ASM */
 121 
 122 /* siginfo si_status for traced events */
 123 #define LX_PTRACE_EVENT_FORK            0x100
 124 #define LX_PTRACE_EVENT_VFORK           0x200
 125 #define LX_PTRACE_EVENT_CLONE           0x300
 126 #define LX_PTRACE_EVENT_EXEC            0x400
 127 #define LX_PTRACE_EVENT_VFORK_DONE      0x500
 128 #define LX_PTRACE_EVENT_EXIT            0x600
 129 #define LX_PTRACE_EVENT_SECCOMP         0x700
 130 
 131 /*
 132  * Brand-private values for the "pr_what" member of lwpstatus, for use with the
 133  * PR_BRAND stop reason.  These reasons are validated in lx_stop_notify();
 134  * update it if you add new reasons here.
 135  */
 136 #define LX_PR_SYSENTRY          1
 137 #define LX_PR_SYSEXIT           2
 138 #define LX_PR_SIGNALLED         3
 139 #define LX_PR_EVENT             4
 140 
 141 
 142 #define LX_VERSION_1            1
 143 #define LX_VERSION              LX_VERSION_1
 144 
 145 #define LX_KERN_VERSION_NUM     ZONE_ATTR_BRAND_ATTRS
 146 
 147 /*
 148  * Aux vector containing phdr of Linux executable and ehdr of interpreter
 149  * (if any), both of which are used by lx_librtld_db to ascertain r_debug.
 150  * We repurpose the 3rd brand-specific aux vector slot for the Linux
 151  * AT_SYSINFO_EHDR entry (we modify the a_type in the brand library).
 152  */
 153 #define AT_SUN_BRAND_LX_PHDR    AT_SUN_BRAND_AUX1
 154 #define AT_SUN_BRAND_LX_INTERP  AT_SUN_BRAND_AUX2
 155 #define AT_SUN_BRAND_LX_SYSINFO_EHDR    AT_SUN_BRAND_AUX3
 156 
 157 /* Aux vector containing hz value */
 158 #define AT_CLKTCK       17
 159 /* Aux vector containing vDSO addr */
 160 #define AT_SYSINFO_EHDR 33
 161 
 162 /*
 163  * Usermode emulation routines are run on an alternate stack allocated by
 164  * the brand library.  Every LWP in a process will incur this overhead beyond
 165  * the regular thread stack:
 166  */
 167 #define LX_NATIVE_STACK_PAGE_COUNT      64
 168 
 169 /*
 170  * When returning in a new child process created with vfork(2) (or CLONE_VFORK)
 171  * we discard some of the native stack to prevent corruption of the parent
 172  * emulation state.
 173  */
 174 #define LX_NATIVE_STACK_VFORK_GAP       0x3000
 175 
 176 #ifndef _ASM
 177 
 178 extern struct brand lx_brand;
 179 
 180 typedef struct lx_brand_registration {
 181         uint_t lxbr_version;            /* version number */
 182         void *lxbr_handler;             /* base address of handler */
 183         uint32_t lxbr_flags;            /* LX_PROC_* registration flags */
 184 } lx_brand_registration_t;
 185 
 186 typedef struct lx_brand_registration32 {
 187         uint_t lxbr_version;            /* version number */
 188         uint32_t lxbr_handler;          /* base address of handler */
 189         uint32_t lxbr_flags;            /* LX_PROC_* registration flags */
 190 } lx_brand_registration32_t;
 191 
 192 #ifdef __amd64
 193 
 194 typedef struct lx_regs {
 195         long lxr_fs;
 196         long lxr_rdi;
 197         long lxr_rsi;
 198         long lxr_rbp;
 199         long lxr_rsp;
 200         long lxr_rbx;
 201         long lxr_rdx;
 202         long lxr_rcx;
 203         long lxr_rax;
 204         long lxr_r8;
 205         long lxr_r9;
 206         long lxr_r10;
 207         long lxr_r11;
 208         long lxr_r12;
 209         long lxr_r13;
 210         long lxr_r14;
 211         long lxr_r15;
 212         long lxr_rip;
 213 
 214         long lxr_orig_rax;
 215 } lx_regs_t;
 216 
 217 typedef struct lx_regs32 {
 218         uint32_t lxr_gs;
 219         uint32_t lxr_edi;
 220         uint32_t lxr_esi;
 221         uint32_t lxr_ebp;
 222         uint32_t lxr_esp;
 223         uint32_t lxr_ebx;
 224         uint32_t lxr_edx;
 225         uint32_t lxr_ecx;
 226         uint32_t lxr_eax;
 227         uint32_t lxr_eip;
 228 
 229         uint32_t lxr_orig_eax;
 230 } lx_regs32_t;
 231 
 232 #else /* ! __amd64 */
 233 
 234 typedef struct lx_regs {
 235         long lxr_gs;
 236         long lxr_edi;
 237         long lxr_esi;
 238         long lxr_ebp;
 239         long lxr_esp;
 240         long lxr_ebx;
 241         long lxr_edx;
 242         long lxr_ecx;
 243         long lxr_eax;
 244         long lxr_eip;
 245 
 246         long lxr_orig_eax;
 247 } lx_regs_t;
 248 
 249 #endif /* __amd64 */
 250 
 251 #ifdef __amd64
 252 /*
 253  * The 64-bit native "user_regs_struct" Linux structure.
 254  */
 255 typedef struct lx_user_regs {
 256         long lxur_r15;
 257         long lxur_r14;
 258         long lxur_r13;
 259         long lxur_r12;
 260         long lxur_rbp;
 261         long lxur_rbx;
 262         long lxur_r11;
 263         long lxur_r10;
 264         long lxur_r9;
 265         long lxur_r8;
 266         long lxur_rax;
 267         long lxur_rcx;
 268         long lxur_rdx;
 269         long lxur_rsi;
 270         long lxur_rdi;
 271         long lxur_orig_rax;
 272         long lxur_rip;
 273         long lxur_xcs;
 274         long lxur_rflags;
 275         long lxur_rsp;
 276         long lxur_xss;
 277         long lxur_xfs_base;
 278         long lxur_xgs_base;
 279         long lxur_xds;
 280         long lxur_xes;
 281         long lxur_xfs;
 282         long lxur_xgs;
 283 } lx_user_regs_t;
 284 
 285 #if defined(_KERNEL) && defined(_SYSCALL32_IMPL)
 286 /*
 287  * 64-bit kernel view of the 32-bit "user_regs_struct" Linux structure.
 288  */
 289 typedef struct lx_user_regs32 {
 290         int32_t lxur_ebx;
 291         int32_t lxur_ecx;
 292         int32_t lxur_edx;
 293         int32_t lxur_esi;
 294         int32_t lxur_edi;
 295         int32_t lxur_ebp;
 296         int32_t lxur_eax;
 297         int32_t lxur_xds;
 298         int32_t lxur_xes;
 299         int32_t lxur_xfs;
 300         int32_t lxur_xgs;
 301         int32_t lxur_orig_eax;
 302         int32_t lxur_eip;
 303         int32_t lxur_xcs;
 304         int32_t lxur_eflags;
 305         int32_t lxur_esp;
 306         int32_t lxur_xss;
 307 } lx_user_regs32_t;
 308 #endif /* defined(_KERNEL) && defined(_SYSCALL32_IMPL) */
 309 
 310 #else /* !__amd64 */
 311 /*
 312  * The 32-bit native "user_regs_struct" Linux structure.
 313  */
 314 typedef struct lx_user_regs {
 315         long lxur_ebx;
 316         long lxur_ecx;
 317         long lxur_edx;
 318         long lxur_esi;
 319         long lxur_edi;
 320         long lxur_ebp;
 321         long lxur_eax;
 322         long lxur_xds;
 323         long lxur_xes;
 324         long lxur_xfs;
 325         long lxur_xgs;
 326         long lxur_orig_eax;
 327         long lxur_eip;
 328         long lxur_xcs;
 329         long lxur_eflags;
 330         long lxur_esp;
 331         long lxur_xss;
 332 } lx_user_regs_t;
 333 #endif /* __amd64 */
 334 
 335 #endif /* _ASM */
 336 
 337 /*
 338  * GDT usage
 339  */
 340 #define GDT_TLSMIN      (GDT_BRANDMIN)
 341 #define GDT_TLSMAX      (GDT_TLSMIN + 2)
 342 #define LX_TLSNUM       (GDT_TLSMAX - GDT_TLSMIN)
 343 
 344 #ifndef _ASM
 345 
 346 /*
 347  * Stores information needed by the lx linker to launch the main
 348  * lx executable.
 349  */
 350 typedef struct lx_elf_data64 {
 351         uintptr_t       ed_phdr;
 352         uintptr_t       ed_phent;
 353         uintptr_t       ed_phnum;
 354         uintptr_t       ed_entry;
 355         uintptr_t       ed_base;
 356         uintptr_t       ed_ldentry;
 357 } lx_elf_data64_t;
 358 
 359 typedef struct lx_elf_data32 {
 360         uint32_t        ed_phdr;
 361         uint32_t        ed_phent;
 362         uint32_t        ed_phnum;
 363         uint32_t        ed_entry;
 364         uint32_t        ed_base;
 365         uint32_t        ed_ldentry;
 366 } lx_elf_data32_t;
 367 
 368 #if defined(_LP64)
 369 typedef lx_elf_data64_t lx_elf_data_t;
 370 #else
 371 typedef lx_elf_data32_t lx_elf_data_t;
 372 #endif
 373 
 374 typedef enum lx_proc_flags {
 375         /* flags configurable via brandsys() and members of LX_PROC_ALL */
 376         LX_PROC_INSTALL_MODE    = 0x01,
 377         LX_PROC_STRICT_MODE     = 0x02,
 378         /* internal flags */
 379         LX_PROC_CHILD_DEATHSIG  = 0x04
 380 } lx_proc_flags_t;
 381 
 382 #define LX_PROC_ALL     (LX_PROC_INSTALL_MODE | LX_PROC_STRICT_MODE)
 383 
 384 #ifdef  _KERNEL
 385 
 386 #define LX_RLFAKE_LOCKS         0
 387 #define LX_RLFAKE_NICE          1
 388 #define LX_RLFAKE_RTPRIO        2
 389 #define LX_RLFAKE_RTTIME        3
 390 
 391 #define LX_RLFAKE_NLIMITS       4
 392 
 393 #define LX_RLIM64_INFINITY      (~0ULL)
 394 
 395 typedef struct {
 396         uint64_t        rlim_cur;
 397         uint64_t        rlim_max;
 398 } lx_rlimit64_t;
 399 
 400 typedef struct lx_proc_data {
 401         uintptr_t l_handler;    /* address of user-space handler */
 402         pid_t l_ppid;           /* pid of originating parent proc */
 403         uint64_t l_ptrace;      /* process being observed with ptrace */
 404         lx_elf_data_t l_elf_data; /* ELF data for linux executable */
 405         /* signal to deliver to parent when this thread group dies */
 406         int l_signal;
 407         /* native signal to deliver to process when parent dies */
 408         int l_parent_deathsig;
 409         lx_proc_flags_t l_flags;
 410 
 411         lx_rlimit64_t l_fake_limits[LX_RLFAKE_NLIMITS];
 412 } lx_proc_data_t;
 413 
 414 #endif  /* _KERNEL */
 415 
 416 /*
 417  * A data type big enough to bitmap all Linux possible cpus.
 418  * The bitmap size is defined as 1024 cpus in the Linux 2.4 and 2.6 man pages
 419  * for sched_getaffinity() and sched_getaffinity().
 420  */
 421 #define LX_NCPU         (1024)
 422 #define LX_AFF_ULONGS   (LX_NCPU / (8 * sizeof (ulong_t)))
 423 typedef ulong_t lx_affmask_t[LX_AFF_ULONGS];
 424 
 425 /* Max. length of kernel version string */
 426 #define LX_VERS_MAX     16
 427 
 428 /* Length of proc boot_id string */
 429 #define LX_BOOTID_LEN   37
 430 
 431 /*
 432  * Flag values for uc_brand_data[0] in the ucontext_t:
 433  */
 434 #define LX_UC_STACK_NATIVE      0x00001
 435 #define LX_UC_STACK_BRAND       0x00002
 436 #define LX_UC_RESTORE_NATIVE_SP 0x00010
 437 #define LX_UC_FRAME_IS_SYSCALL  0x00100
 438 #define LX_UC_RESTART_SYSCALL   0x01000
 439 #define LX_UC_IGNORE_LINK       0x10000
 440 
 441 #ifdef  _KERNEL
 442 
 443 typedef struct lx_lwp_data lx_lwp_data_t;
 444 
 445 /*
 446  * Flag values for "lxpa_flags" on a ptrace(2) accord.
 447  */
 448 typedef enum lx_accord_flags {
 449         LX_ACC_TOMBSTONE = 0x01
 450 } lx_accord_flags_t;
 451 
 452 /*
 453  * Flags values for "br_ptrace_flags" in the LWP-specific data.
 454  */
 455 typedef enum lx_ptrace_flags {
 456         LX_PTF_SYSCALL = 0x01,
 457         LX_PTF_EXITING = 0x02,
 458         LX_PTF_STOPPING = 0x04,
 459         LX_PTF_INHERIT = 0x08,
 460         LX_PTF_STOPPED = 0x10,
 461         LX_PTF_PARENT_WAIT = 0x20,
 462         LX_PTF_CLDPEND = 0x40,
 463         LX_PTF_CLONING = 0x80,
 464         LX_PTF_WAITPEND = 0x100
 465 } lx_ptrace_flags_t;
 466 
 467 /*
 468  * A ptrace(2) accord represents the relationship between a tracer LWP and the
 469  * set of LWPs that it is tracing: the tracees.  This data structure belongs
 470  * primarily to the tracer, but is reference counted so that it may be freed by
 471  * whoever references it last.
 472  */
 473 typedef struct lx_ptrace_accord {
 474         kmutex_t                lxpa_lock;
 475         uint_t                  lxpa_refcnt;
 476         lx_accord_flags_t       lxpa_flags;
 477 
 478         /*
 479          * The tracer must hold "pidlock" while clearing these fields for
 480          * exclusion of waitid(), etc.
 481          */
 482         lx_lwp_data_t           *lxpa_tracer;
 483         kcondvar_t              *lxpa_cvp;
 484 
 485         /*
 486          * The "lxpa_tracees_lock" mutex protects the tracee list.
 487          */
 488         kmutex_t                lxpa_tracees_lock;
 489         list_t                  lxpa_tracees;
 490 } lx_ptrace_accord_t;
 491 
 492 /*
 493  * These values are stored in the per-LWP data for a tracee when it is attached
 494  * to a tracer.  They record the method that was used to attach.
 495  */
 496 typedef enum lx_ptrace_attach {
 497         LX_PTA_NONE = 0x00,             /* not attached */
 498         LX_PTA_ATTACH = 0x01,           /* due to tracer using PTRACE_ATTACH */
 499         LX_PTA_TRACEME = 0x02,          /* due to child using PTRACE_TRACEME */
 500         LX_PTA_INHERIT_CLONE = 0x04,    /* due to PTRACE_CLONE clone(2) flag */
 501         LX_PTA_INHERIT_OPTIONS = 0x08   /* due to PTRACE_SETOPTIONS options */
 502 } lx_ptrace_attach_t;
 503 
 504 typedef enum lx_stack_mode {
 505         LX_STACK_MODE_PREINIT = 0,
 506         LX_STACK_MODE_INIT,
 507         LX_STACK_MODE_NATIVE,
 508         LX_STACK_MODE_BRAND
 509 } lx_stack_mode_t;
 510 
 511 struct lx_pid {
 512         pid_t   s_pid;                  /* the SunOS pid and ... */
 513         id_t    s_tid;                  /* ... tid pair */
 514         pid_t   l_pid;                  /* the corresponding linux pid */
 515         time_t  l_start;                /* birthday of this pid */
 516         struct pid *l_pidp;
 517         struct lx_pid *stol_next;       /* link in stol hash table */
 518         struct lx_pid *ltos_next;       /* link in ltos hash table */
 519 };
 520 
 521 /*
 522  * lx-specific data in the klwp_t
 523  */
 524 struct lx_lwp_data {
 525         uint_t  br_lwp_flags;           /* misc. flags */
 526         klwp_t  *br_lwp;                /* back pointer to container lwp */
 527         int     br_signal;              /* signal to send to parent when */
 528                                         /* clone()'ed child terminates */
 529         int     br_exitwhy;             /* reason for thread (process) exit */
 530         int     br_exitwhat;            /* exit code / killing signal */
 531         lx_affmask_t br_affinitymask;   /* bitmask of CPU sched affinities */
 532         struct user_desc br_tls[LX_TLSNUM];
 533                         /* descriptors used by libc for TLS */
 534         ulong_t br_lx_fsbase;           /* lx fsbase for 64-bit thread ptr */
 535         ulong_t br_ntv_fsbase;          /* native fsbase 64-bit thread ptr */
 536         ulong_t br_lx_gsbase;           /* lx user-land gsbase */
 537         ulong_t br_ntv_gsbase;          /* native user-land gsbase */
 538         pid_t   br_pid;                 /* converted pid for this thread */
 539         pid_t   br_tgid;                /* thread group ID for this thread */
 540         pid_t   br_ppid;                /* parent pid for this thread */
 541         id_t    br_ptid;                /* parent tid for this thread */
 542         void    *br_clear_ctidp;        /* clone thread id ptr */
 543         void    *br_set_ctidp;          /* clone thread id ptr */
 544         void    *br_robust_list;        /* robust lock list, if any */
 545 
 546         /*
 547          * The following struct is used by some system calls to pass extra
 548          * flags into the kernel without impinging on the namespace for
 549          * illumos.
 550          */
 551         void    *br_scall_args;
 552         int     br_args_size; /* size in bytes of br_scall_args */
 553 
 554         boolean_t br_waitid_emulate;
 555         int br_waitid_flags;
 556 
 557         lx_ptrace_flags_t br_ptrace_flags; /* ptrace flags for this LWP */
 558         lx_ptrace_options_t br_ptrace_options; /* PTRACE_SETOPTIONS options */
 559         lx_ptrace_options_t br_ptrace_clone_option; /* current clone(2) type */
 560 
 561         lx_ptrace_attach_t br_ptrace_attach; /* how did we get attached */
 562         lx_ptrace_accord_t *br_ptrace_accord; /* accord for this tracer LWP */
 563         lx_ptrace_accord_t *br_ptrace_tracer; /* accord tracing this LWP */
 564         list_node_t br_ptrace_linkage;  /* linkage for lxpa_tracees list */
 565 
 566         ushort_t br_ptrace_whystop;     /* stop reason, 0 for no stop */
 567         ushort_t br_ptrace_whatstop;    /* stop sub-reason */
 568 
 569         int32_t br_ptrace_stopsig;      /* stop signal, 0 for no signal */
 570         /*
 571          * Track the last (native) signal number processed by a ptrace.
 572          * This allows the tracee to properly handle ignored signals after
 573          * the tracer has been notified and the tracee restarted.
 574          */
 575         int32_t br_ptrace_donesig;
 576         uintptr_t br_ptrace_stopucp;    /* usermode ucontext_t pointer */
 577 
 578         uint_t  br_ptrace_event;
 579         ulong_t br_ptrace_eventmsg;
 580 
 581         int     br_syscall_num;         /* current system call number */
 582         boolean_t br_syscall_restart;   /* should restart on EINTR */
 583 
 584         /*
 585          * Store the LX_STACK_MODE for this LWP, and the current extent of the
 586          * native (emulation) stack.  This is similar, in principle, to the
 587          * sigaltstack mechanism for signal handling.  We also use this mode
 588          * flag to determine how to process system calls from this LWP.
 589          */
 590         lx_stack_mode_t br_stack_mode;
 591         uintptr_t br_ntv_stack;
 592         uintptr_t br_ntv_stack_current;
 593 
 594         /*
 595          * If this pid is set, we return it with getpid().  This allows the
 596          * thunking server to interpose on the pid returned to the Linux
 597          * syslog software.
 598          */
 599         pid_t   br_lx_thunk_pid;
 600 
 601         /*
 602          * If strict mode is enabled (via LX_STRICT in the environment), any
 603          * call to lx_unsupported() will set this boolean to B_TRUE.  This will
 604          * cause us to drop SIGSYS on the LWP as it attempts to return to
 605          * usermode.
 606          */
 607         boolean_t br_strict_failure;
 608 
 609         /*
 610          * Some syscalls emulated in-kernel still call back out to the
 611          * userspace emulation for certain functions.  When that is the case,
 612          * the syscall_return logic must be bypassed at the end of the
 613          * in-kernel syscall code.  The NORMALRETURN and JUSTRETURN constants
 614          * are used to choose the behavior.
 615          */
 616         char br_eosys;
 617 
 618         /*
 619          * Hold a pre-allocated lx_pid structure to be used during lx_initlwp.
 620          */
 621         struct lx_pid *br_lpid;
 622 
 623         /*
 624          * ID of the cgroup this thread belongs to.
 625          */
 626         uint_t br_cgroupid;
 627 };
 628 
 629 /*
 630  * Upper limit on br_args_size, low because this value can persist until
 631  * overridden with another value, and the size is given from userland.
 632  */
 633 #define LX_BR_ARGS_SIZE_MAX     (1024)
 634 
 635 /* brand specific data */
 636 typedef struct lx_zone_data {
 637         char lxzd_kernel_version[LX_VERS_MAX];
 638         ksocket_t lxzd_ioctl_sock;
 639         char lxzd_bootid[LX_BOOTID_LEN];        /* procfs boot_id */
 640 } lx_zone_data_t;
 641 
 642 #define BR_CPU_BOUND    0x0001
 643 
 644 #define ttolxlwp(t)     ((struct lx_lwp_data *)ttolwpbrand(t))
 645 #define lwptolxlwp(l)   ((struct lx_lwp_data *)lwptolwpbrand(l))
 646 #define ttolxproc(t)    \
 647         (((t)->t_procp->p_brand == &lx_brand) ? \
 648         (struct lx_proc_data *)(t)->t_procp->p_brand_data : NULL)
 649 #define ptolxproc(p)    \
 650         (((p)->p_brand == &lx_brand) ? \
 651         (struct lx_proc_data *)(p)->p_brand_data : NULL)
 652 #define ztolxzd(z)              \
 653         (((z)->zone_brand == &lx_brand) ?  \
 654         (lx_zone_data_t *)(z)->zone_brand_data : NULL)
 655 
 656 /* Macro for converting to system call arguments. */
 657 #define LX_ARGS(scall) ((struct lx_##scall##_args *)\
 658         (ttolxlwp(curthread)->br_scall_args))
 659 
 660 /*
 661  * Determine the upper bound on the system call number:
 662  */
 663 #if defined(_LP64)
 664 #define LX_MAX_SYSCALL(lwp)                                             \
 665         ((lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) ?                  \
 666             lx_nsysent64 : lx_nsysent32)
 667 #else
 668 #define LX_MAX_SYSCALL(lwp)     lx_nsysent32
 669 #endif
 670 
 671 extern char *lx_get_zone_kern_version(zone_t *);
 672 extern int lx_kern_version_cmp(zone_t *, const char *);
 673 
 674 extern void lx_lwp_set_native_stack_current(lx_lwp_data_t *, uintptr_t);
 675 extern void lx_divert(klwp_t *, uintptr_t);
 676 extern int lx_runexe(klwp_t *, void *);
 677 extern void lx_switch_to_native(klwp_t *);
 678 extern int lx_regs_to_userregs(lx_lwp_data_t *, void *);
 679 extern int lx_uc_to_userregs(lx_lwp_data_t *, void *, void *);
 680 extern int lx_userregs_to_regs(lx_lwp_data_t *lwpd, void *);
 681 extern int lx_userregs_to_uc(lx_lwp_data_t *lwpd, void *, void *);
 682 
 683 extern int lx_syscall_enter(void);
 684 extern int lx_syscall_return(klwp_t *, int, long);
 685 
 686 extern void lx_trace_sysenter(int, uintptr_t *);
 687 extern void lx_trace_sysreturn(int, long);
 688 
 689 extern void lx_emulate_user(klwp_t *, int, uintptr_t *);
 690 #if defined(_SYSCALL32_IMPL)
 691 extern void lx_emulate_user32(klwp_t *, int, uintptr_t *);
 692 #endif
 693 
 694 extern int lx_debug;
 695 #define lx_print        if (lx_debug) printf
 696 
 697 extern void lx_pid_assign(kthread_t *, struct lx_pid *);
 698 extern void lx_pid_reassign(kthread_t *);
 699 extern void lx_pid_rele(pid_t, id_t);
 700 extern pid_t lx_lpid_to_spair(pid_t, pid_t *, id_t *);
 701 extern pid_t lx_lwp_ppid(klwp_t *, pid_t *, id_t *);
 702 extern void lx_pid_init(void);
 703 extern void lx_pid_fini(void);
 704 
 705 /*
 706  * In-Kernel Linux System Call Description.
 707  */
 708 typedef struct lx_sysent {
 709         char    *sy_name;
 710         long    (*sy_callc)();
 711         char    sy_flags;
 712         char    sy_narg;
 713 } lx_sysent_t;
 714 
 715 #if defined(_LP64)
 716 extern lx_sysent_t lx_sysent64[LX_NSYSCALLS + 1];
 717 extern int lx_nsysent64;
 718 #endif
 719 extern lx_sysent_t lx_sysent32[LX_NSYSCALLS + 1];
 720 extern int lx_nsysent32;
 721 
 722 #endif  /* _KERNEL */
 723 #endif /* _ASM */
 724 
 725 #ifdef  __cplusplus
 726 }
 727 #endif
 728 
 729 #endif  /* _LX_BRAND_H */