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