1264269Ssbruno/*- 2264269Ssbruno * Copyright (c) 2013 Stacey D. Son 3264269Ssbruno * 4264269Ssbruno * Redistribution and use in source and binary forms, with or without 5264269Ssbruno * modification, are permitted provided that the following conditions 6264269Ssbruno * are met: 7264269Ssbruno * 1. Redistributions of source code must retain the above copyright 8264269Ssbruno * notice, this list of conditions and the following disclaimer. 9264269Ssbruno * 2. Redistributions in binary form must reproduce the above copyright 10264269Ssbruno * notice, this list of conditions and the following disclaimer in the 11264269Ssbruno * documentation and/or other materials provided with the distribution. 12264269Ssbruno * 13264269Ssbruno * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14264269Ssbruno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15264269Ssbruno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16264269Ssbruno * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17264269Ssbruno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18264269Ssbruno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19264269Ssbruno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20264269Ssbruno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21264269Ssbruno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22264269Ssbruno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23264269Ssbruno * SUCH DAMAGE. 24264269Ssbruno * 25264269Ssbruno * $FreeBSD$ 26264269Ssbruno */ 27264269Ssbruno 28264269Ssbruno#ifndef _IMGACT_BINMISC_H_ 29264269Ssbruno#define _IMGACT_BINMISC_H_ 30264269Ssbruno 31264269Ssbruno/** 32264269Ssbruno * Miscellaneous binary interpreter image activator. 33264269Ssbruno */ 34264269Ssbruno 35264269Ssbruno#include <sys/param.h> /* for MAXPATHLEN */ 36264269Ssbruno 37264269Ssbruno/* 38264269Ssbruno * Imgact bin misc parameters. 39264269Ssbruno */ 40264269Ssbruno#define IBE_VERSION 1 /* struct ximgact_binmisc_entry version. */ 41264269Ssbruno#define IBE_NAME_MAX 32 /* Max size for entry name. */ 42264269Ssbruno#define IBE_MAGIC_MAX 256 /* Max size for header magic and mask. */ 43266272Ssbruno#define IBE_ARG_LEN_MAX 256 /* Max space for optional interpreter command- 44300060Spfg line argruments separated by white space */ 45264269Ssbruno#define IBE_INTERP_LEN_MAX (MAXPATHLEN + IBE_ARG_LEN_MAX) 46264269Ssbruno#define IBE_MAX_ENTRIES 64 /* Max number of interpreter entries. */ 47264269Ssbruno 48264269Ssbruno/* 49264269Ssbruno * Imgact bin misc interpreter entry flags. 50264269Ssbruno */ 51264269Ssbruno#define IBF_ENABLED 0x0001 /* Entry is active. */ 52264269Ssbruno#define IBF_USE_MASK 0x0002 /* Use mask on header magic field. */ 53264269Ssbruno 54264269Ssbruno/* 55264269Ssbruno * Used with sysctlbyname() to pass imgact bin misc entries in and out of the 56264269Ssbruno * kernel. 57264269Ssbruno */ 58264269Ssbrunotypedef struct ximgact_binmisc_entry { 59264269Ssbruno uint32_t xbe_version; /* Struct version(IBE_VERSION) */ 60264269Ssbruno uint32_t xbe_flags; /* Entry flags (IBF_*) */ 61264269Ssbruno uint32_t xbe_moffset; /* Magic offset in header */ 62264269Ssbruno uint32_t xbe_msize; /* Magic size */ 63264269Ssbruno uint32_t spare[3]; /* Spare fields for future use */ 64264269Ssbruno char xbe_name[IBE_NAME_MAX]; /* Unique interpreter name */ 65264269Ssbruno char xbe_interpreter[IBE_INTERP_LEN_MAX]; /* Interpreter path + args */ 66264269Ssbruno uint8_t xbe_magic[IBE_MAGIC_MAX]; /* Header Magic */ 67264269Ssbruno uint8_t xbe_mask[IBE_MAGIC_MAX]; /* Magic Mask */ 68264269Ssbruno} ximgact_binmisc_entry_t; 69264269Ssbruno 70264269Ssbruno/* 71264269Ssbruno * sysctl() command names. 72264269Ssbruno */ 73266272Ssbruno#define IBE_SYSCTL_NAME "kern.binmisc" 74264269Ssbruno 75264269Ssbruno#define IBE_SYSCTL_NAME_ADD IBE_SYSCTL_NAME ".add" 76264269Ssbruno#define IBE_SYSCTL_NAME_REMOVE IBE_SYSCTL_NAME ".remove" 77264269Ssbruno#define IBE_SYSCTL_NAME_DISABLE IBE_SYSCTL_NAME ".disable" 78264269Ssbruno#define IBE_SYSCTL_NAME_ENABLE IBE_SYSCTL_NAME ".enable" 79264269Ssbruno#define IBE_SYSCTL_NAME_LOOKUP IBE_SYSCTL_NAME ".lookup" 80264269Ssbruno#define IBE_SYSCTL_NAME_LIST IBE_SYSCTL_NAME ".list" 81264269Ssbruno 82264269Ssbruno#define KMOD_NAME "imgact_binmisc" 83264269Ssbruno 84264269Ssbruno/* 85266272Ssbruno * Examples of manipulating the interpreter table using sysctlbyname(3): 86264269Ssbruno * 87264269Ssbruno * #include <sys/imgact_binmisc.h> 88264269Ssbruno * 89264269Ssbruno * #define LLVM_MAGIC "BC\xc0\xde" 90264269Ssbruno * 91264269Ssbruno * #define MIPS64_ELF_MAGIC "\x7f\x45\x4c\x46\x02\x02\x01\x00\x00\x00" \ 92264269Ssbruno * "\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08" 93264269Ssbruno * #define MIPS64_ELF_MASK "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff" \ 94264269Ssbruno * "\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff" 95264269Ssbruno * 96264269Ssbruno * ximgact_binmisc_entry_t xbe, *xbep, out_xbe; 97264269Ssbruno * size_t size = 0, osize; 98264269Ssbruno * int error, i; 99264269Ssbruno * 100264269Ssbruno * // Add image activator for LLVM byte code 101264269Ssbruno * bzero(&xbe, sizeof(xbe)); 102264269Ssbruno * xbe.xbe_version = IBE_VERSION; 103264269Ssbruno * xbe.xbe_flags = IBF_ENABLED; 104264269Ssbruno * strlcpy(xbe.xbe_name, "llvm_bc", IBE_NAME_MAX); 105264269Ssbruno * strlcpy(xbe.xbe_interpreter, "/usr/bin/lli --fake-arg0=#a", 106264269Ssbruno * IBE_INTERP_LEN_MAX); 107264269Ssbruno * xbe.xbe_moffset = 0; 108264269Ssbruno * xbe.xbe_msize = 4; 109264269Ssbruno * memcpy(xbe.xbe_magic, LLVM_MAGIC, xbe.xbe_msize); 110264269Ssbruno * error = sysctlbyname(IBE_SYSCTL_NAME_ADD, NULL, NULL, &xbe, sizeof(xbe)); 111264269Ssbruno * 112264269Ssbruno * // Add image activator for mips64 ELF binaries to use qemu user mode 113264269Ssbruno * bzero(&xbe, sizeof(xbe)); 114264269Ssbruno * xbe.xbe_version = IBE_VERSION; 115264269Ssbruno * xbe.xbe_flags = IBF_ENABLED | IBF_USE_MASK; 116264269Ssbruno * strlcpy(xbe.xbe_name, "mips64elf", IBE_NAME_MAX); 117264269Ssbruno * strlcpy(xbe.xbe_interpreter, "/usr/local/bin/qemu-mips64", 118264269Ssbruno * IBE_INTERP_LEN_MAX); 119264269Ssbruno * xbe.xbe_moffset = 0; 120264269Ssbruno * xbe.xbe_msize = 20; 121264269Ssbruno * memcpy(xbe.xbe_magic, MIPS64_ELF_MAGIC, xbe.xbe_msize); 122264269Ssbruno * memcpy(xbe.xbe_mask, MIPS64_ELF_MASK, xbe.xbe_msize); 123264269Ssbruno * sysctlbyname(IBE_SYSCTL_NAME_ADD, NULL, NULL, &xbe, sizeof(xbe)); 124264269Ssbruno * 125264269Ssbruno * // Disable (OR Enable OR Remove) image activator for LLVM byte code 126264269Ssbruno * bzero(&xbe, sizeof(xbe)); 127264269Ssbruno * xbe.xbe_version = IBE_VERSION; 128264269Ssbruno * strlcpy(xbe.xbe_name, "llvm_bc", IBE_NAME_MAX); 129264269Ssbruno * error = sysctlbyname(IBE_SYSCTL_NAME_DISABLE, NULL, NULL, &xbe, sizeof(xbe)); 130266272Ssbruno * // OR sysctlbyname(IBE_SYSCTL_NAME_ENABLE, NULL, NULL, &xbe, sizeof(xbe)); 131264269Ssbruno * // OR sysctlbyname(IBE_SYSCTL_NAME_REMOVE, NULL, NULL, &xbe, sizeof(xbe)); 132264269Ssbruno * 133264269Ssbruno * // Lookup image activator "llvm_bc" 134264269Ssbruno * bzero(&xbe, sizeof(xbe)); 135264269Ssbruno * xbe.xbe_version = IBE_VERSION; 136264269Ssbruno * strlcpy(xbe.xbe_name, "llvm_bc", IBE_NAME_MAX); 137264269Ssbruno * size = sizeof(out_xbe); 138264269Ssbruno * error = sysctlbyname(IBE_SYSCTL_NAME_LOOKUP, &out_xbe, &size, &xbe, 139264269Ssbruno * sizeof(xbe)); 140264269Ssbruno * 141264269Ssbruno * // Get all the currently configured image activators and report 142264269Ssbruno * error = sysctlbyname(IBE_SYSCTL_NAME_LIST, NULL, &size, NULL, 0); 143264269Ssbruno * if (0 == error && size > 0) { 144264269Ssbruno * xbep = malloc(size); 145264269Ssbruno * while(1) { 146264269Ssbruno * osize = size; 147264269Ssbruno * error = sysctlbyname("kern.binmisc.list", xbep, &size, NULL, 0); 148264269Ssbruno * if (-1 == error && ENOMEM == errno && size == osize) { 149264269Ssbruno * // The buffer too small and needs to grow 150264269Ssbruno * size += sizeof(xbe); 151264269Ssbruno * xbep = realloc(xbep, size); 152264269Ssbruno * } else 153264269Ssbruno * break; 154264269Ssbruno * } 155264269Ssbruno * } 156264269Ssbruno * for(i = 0; i < (size / sizeof(xbe)); i++, xbep++) 157264269Ssbruno * printf("name: %s interpreter: %s flags: %s %s\n", xbep->xbe_name, 158264269Ssbruno * xbep->xbe_interpreter, (xbep->xbe_flags & IBF_ENABLED) ? 159264269Ssbruno * "ENABLED" : "", (xbep->xbe_flags & IBF_ENABLED) ? "USE_MASK" : ""); 160264269Ssbruno * 161264269Ssbruno * The sysctlbyname() calls above may return the following errors in addition 162264269Ssbruno * to the standard ones: 163264269Ssbruno * 164264269Ssbruno * [EINVAL] Invalid argument in the input ximgact_binmisc_entry_t structure. 165264269Ssbruno * [EEXIST] Interpreter entry for given name already exist in kernel list. 166264269Ssbruno * [ENOMEM] Allocating memory in the kernel failed or, in the case of 167264269Ssbruno * kern.binmisc.list, the user buffer is too small. 168264269Ssbruno * [ENOENT] Interpreter entry for given name is not found. 169264269Ssbruno * [ENOSPC] Attempted to exceed maximum number of entries (IBE_MAX_ENTRIES). 170264269Ssbruno */ 171264269Ssbruno 172264269Ssbruno#endif /* !_IMGACT_BINMISC_H_ */ 173