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 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2015 Joyent, Inc.  All rights reserved.
  29  */
  30 
  31 #ifndef _SYS_LX_H
  32 #define _SYS_LX_H
  33 
  34 #include <stdio.h>
  35 #include <alloca.h>
  36 #include <dirent.h>
  37 #include <sys/types.h>
  38 #include <sys/param.h>
  39 #include <sys/lwp.h>
  40 
  41 #include <sys/lx_brand.h>
  42 #include <sys/lx_thread.h>
  43 
  44 #include <lx_errno.h>
  45 
  46 #ifdef __cplusplus
  47 extern "C" {
  48 #endif
  49 
  50 extern char lx_release[LX_VERS_MAX];
  51 extern char lx_cmd_name[MAXNAMLEN];
  52 extern pid_t zoneinit_pid;
  53 
  54 /*
  55  * Values Linux expects for init
  56  */
  57 #define LX_INIT_PGID    1
  58 #define LX_INIT_SID     1
  59 #define LX_INIT_PID     1
  60 
  61 /*
  62  * Codes to reboot(2).
  63  */
  64 #define LINUX_REBOOT_MAGIC1             0xfee1dead
  65 #define LINUX_REBOOT_MAGIC2             672274793
  66 #define LINUX_REBOOT_MAGIC2A            85072278
  67 #define LINUX_REBOOT_MAGIC2B            369367448
  68 #define LINUX_REBOOT_MAGIC2C            537993216
  69 
  70 /*
  71  * This was observed as coming from Red Hat's init process, but it's not in
  72  * their reboot(2) man page.
  73  */
  74 #define LINUX_REBOOT_MAGIC2D            0x28121969
  75 
  76 #define LINUX_REBOOT_CMD_RESTART        0x1234567
  77 #define LINUX_REBOOT_CMD_HALT           0xcdef0123
  78 #define LINUX_REBOOT_CMD_POWER_OFF      0x4321fedc
  79 #define LINUX_REBOOT_CMD_RESTART2       0xa1b2c3d4
  80 #define LINUX_REBOOT_CMD_CAD_ON         0x89abcdef
  81 #define LINUX_REBOOT_CMD_CAD_OFF        0
  82 
  83 /*
  84  * the maximum length of messages to be output with lx_msg(), lx_err(),
  85  * lx_debug(), or lx_unsupported().
  86  */
  87 #define LX_MSG_MAXLEN                   (128 + MAXPATHLEN)
  88 
  89 /*
  90  * Linux scheduler priority ranges.
  91  */
  92 #define LX_SCHED_PRIORITY_MIN_OTHER     0
  93 #define LX_SCHED_PRIORITY_MAX_OTHER     0
  94 #define LX_SCHED_PRIORITY_MIN_RRFIFO    1
  95 #define LX_SCHED_PRIORITY_MAX_RRFIFO    99
  96 
  97 /*
  98  * Constants to indicate who getrusage() should return information about.
  99  */
 100 #define LX_RUSAGE_SELF          0
 101 #define LX_RUSAGE_CHILDREN      (-1)
 102 #define LX_RUSAGE_BOTH          (-2)
 103 #define LX_RUSAGE_THREAD        1
 104 
 105 /*
 106  * Based on code from brand_misc.h, but use of that is incompatible with the
 107  * lx brand.
 108  *
 109  * These macros invoke a brandsys subcommand, B_TRUSS_POINT, which makes it
 110  * easy to debug with DTrace.
 111  */
 112 #define B_TRUSS_POINT   6
 113 
 114 #define B_TRACE_POINT_5(a0, a1, a2, a3, a4) \
 115         (void) syscall(SYS_brand, B_TRUSS_POINT, (a0), (a1), (a2), (a3), (a4))
 116 
 117 #define B_TRACE_POINT_4(a0, a1, a2, a3) \
 118         B_TRACE_POINT_5((a0), (a1), (a2), (a3), 0)
 119 
 120 #define B_TRACE_POINT_3(a0, a1, a2) \
 121         B_TRACE_POINT_5((a0), (a1), (a2), 0, 0)
 122 
 123 #define B_TRACE_POINT_2(a0, a1) \
 124         B_TRACE_POINT_5((a0), (a1), 0, 0, 0)
 125 
 126 #define B_TRACE_POINT_1(a0) \
 127         B_TRACE_POINT_5((a0), 0, 0, 0, 0)
 128 
 129 #define B_TRACE_POINT_0() \
 130         B_TRACE_POINT_5(0, 0, 0, 0, 0)
 131 
 132 /*
 133  * Macros to access register state within a ucontext_t:
 134  */
 135 #define LX_REG(ucp, r)  ((ucp)->uc_mcontext.gregs[(r)])
 136 
 137 /*
 138  * normally we never want to write to stderr or stdout because it's unsafe
 139  * to make assumptions about the underlying file descriptors.  to protect
 140  * against writes to these file descriptors we go ahead and close them
 141  * our brand process initalization code.  but there are still occasions
 142  * where we are willing to make assumptions about our file descriptors
 143  * and write to them.  at thes times we should use one lx_msg() or
 144  * lx_msg_error()
 145  */
 146 extern void lx_msg(char *, ...);
 147 extern void lx_err(char *, ...);
 148 extern void lx_err_fatal(char *, ...);
 149 extern void lx_unsupported(char *, ...);
 150 
 151 struct ucontext;
 152 
 153 extern ucontext_t *lx_syscall_regs(void);
 154 extern uintptr_t lx_find_brand_sp(void);
 155 extern const ucontext_t *lx_find_brand_uc(void);
 156 
 157 extern void lx_jump_to_linux(ucontext_t *) __NORETURN;
 158 
 159 extern char *lx_fd_to_path(int fd, char *buf, int buf_size);
 160 extern int lx_lpid_to_spair(pid_t, pid_t *, lwpid_t *);
 161 extern int lx_lpid_to_spid(pid_t, pid_t *);
 162 
 163 extern void lx_ptrace_init();
 164 extern int lx_ptrace_wait(siginfo_t *);
 165 extern void lx_ptrace_fork(void);
 166 extern void lx_ptrace_stop_if_option(int, boolean_t, ulong_t msg, ucontext_t *);
 167 extern void lx_ptrace_clone_begin(int, boolean_t);
 168 
 169 extern int lx_check_alloca(size_t);
 170 #define SAFE_ALLOCA(sz) (lx_check_alloca(sz) ? alloca(sz) : NULL)
 171 
 172 extern int ltos_at_flag(int lflag, int allow, boolean_t enforce);
 173 
 174 extern void lx_init_tsd(lx_tsd_t *);
 175 extern int lx_alloc_stack(void **, size_t *);
 176 extern void lx_install_stack(void *, size_t, lx_tsd_t *);
 177 extern void lx_free_stack(void);
 178 extern void lx_free_other_stacks(void);
 179 extern void lx_stack_prefork(void);
 180 extern void lx_stack_postfork(void);
 181 
 182 /*
 183  * NO_UUCOPY disables calls to the uucopy* system calls to help with
 184  * debugging brand library accesses to linux application memory.
 185  */
 186 #ifdef NO_UUCOPY
 187 
 188 int uucopy_unsafe(const void *src, void *dst, size_t n);
 189 int uucopystr_unsafe(const void *src, void *dst, size_t n);
 190 
 191 #define uucopy(src, dst, n)     uucopy_unsafe((src), (dst), (n))
 192 #define uucopystr(src, dst, n)  uucopystr_unsafe((src), (dst), (n))
 193 
 194 #endif /* NO_UUCOPY */
 195 
 196 /*
 197  * We use these Private libc interfaces to defer signals during critical
 198  * sections.
 199  */
 200 extern void _sigon(void);
 201 extern void _sigoff(void);
 202 
 203 #ifdef  __cplusplus
 204 }
 205 #endif
 206 
 207 #endif  /* _SYS_LX_H */