1898b0535SWarner Losh /*- 2d66a5066SPeter Wemm * Copyright (c) 1994 Christos Zoulas 3d66a5066SPeter Wemm * Copyright (c) 1995 Frank van der Linden 4d66a5066SPeter Wemm * Copyright (c) 1995 Scott Bartram 5d66a5066SPeter Wemm * All rights reserved. 6d66a5066SPeter Wemm * 7d66a5066SPeter Wemm * Redistribution and use in source and binary forms, with or without 8d66a5066SPeter Wemm * modification, are permitted provided that the following conditions 9d66a5066SPeter Wemm * are met: 10d66a5066SPeter Wemm * 1. Redistributions of source code must retain the above copyright 11d66a5066SPeter Wemm * notice, this list of conditions and the following disclaimer. 12d66a5066SPeter Wemm * 2. Redistributions in binary form must reproduce the above copyright 13d66a5066SPeter Wemm * notice, this list of conditions and the following disclaimer in the 14d66a5066SPeter Wemm * documentation and/or other materials provided with the distribution. 15d66a5066SPeter Wemm * 3. The name of the author may not be used to endorse or promote products 16d66a5066SPeter Wemm * derived from this software without specific prior written permission 17d66a5066SPeter Wemm * 18d66a5066SPeter Wemm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19d66a5066SPeter Wemm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20d66a5066SPeter Wemm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21d66a5066SPeter Wemm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22d66a5066SPeter Wemm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23d66a5066SPeter Wemm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24d66a5066SPeter Wemm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25d66a5066SPeter Wemm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26d66a5066SPeter Wemm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27d66a5066SPeter Wemm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28d66a5066SPeter Wemm * 29d66a5066SPeter Wemm * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp 30d66a5066SPeter Wemm */ 31d66a5066SPeter Wemm 3216dbc7f2SDavid E. O'Brien #include <sys/cdefs.h> 3316dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3416dbc7f2SDavid E. O'Brien 359b44bfc5SAlexander Leidinger #include "opt_compat.h" 369b44bfc5SAlexander Leidinger 37d66a5066SPeter Wemm #include <sys/param.h> 38060e4882SDoug Ambrisko #include <sys/bus.h> 3957b4252eSKonstantin Belousov #include <sys/fcntl.h> 40d4364109SBruce Evans #include <sys/lock.h> 41d4364109SBruce Evans #include <sys/malloc.h> 4219e252baSAlexander Leidinger #include <sys/kernel.h> 43060e4882SDoug Ambrisko #include <sys/linker_set.h> 44d4364109SBruce Evans #include <sys/mutex.h> 45d66a5066SPeter Wemm #include <sys/namei.h> 46d66a5066SPeter Wemm #include <sys/proc.h> 4719e252baSAlexander Leidinger #include <sys/sdt.h> 4825771ec2SJohn Baldwin #include <sys/syscallsubr.h> 49d4364109SBruce Evans #include <sys/systm.h> 50d66a5066SPeter Wemm #include <sys/vnode.h> 51d66a5066SPeter Wemm 52e15583ceSAlfred Perlstein #include <machine/stdarg.h> 53e15583ceSAlfred Perlstein 54ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h> 55762e6b85SEivind Eklund 5667d39748SDmitry Chagin MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); 57e16fe1c7SDmitry Chagin MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures"); 58e0d3ea8cSDmitry Chagin MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes"); 59e0d3ea8cSDmitry Chagin MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futex waiting proc"); 6019e252baSAlexander Leidinger 61d66a5066SPeter Wemm const char linux_emul_path[] = "/compat/linux"; 62d66a5066SPeter Wemm 63d66a5066SPeter Wemm /* 64802e08a3SAlexander Leidinger * Search an alternate path before passing pathname arguments on to 65802e08a3SAlexander Leidinger * system calls. Useful for keeping a separate 'emulation tree'. 66d66a5066SPeter Wemm * 67802e08a3SAlexander Leidinger * If cflag is set, we check if an attempt can be made to create the 68802e08a3SAlexander Leidinger * named file, i.e. we check if the directory it should be in exists. 69d66a5066SPeter Wemm */ 70d66a5066SPeter Wemm int 7119e252baSAlexander Leidinger linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg, 7219e252baSAlexander Leidinger char **pbuf, int cflag, int dfd) 73ff766321SIan Dowse { 7419e252baSAlexander Leidinger int retval; 75d66a5066SPeter Wemm 7619e252baSAlexander Leidinger retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 7719e252baSAlexander Leidinger cflag, dfd); 7819e252baSAlexander Leidinger 7919e252baSAlexander Leidinger return (retval); 80d66a5066SPeter Wemm } 81e15583ceSAlfred Perlstein 82e15583ceSAlfred Perlstein void 83e15583ceSAlfred Perlstein linux_msg(const struct thread *td, const char *fmt, ...) 84e15583ceSAlfred Perlstein { 85e15583ceSAlfred Perlstein va_list ap; 86e15583ceSAlfred Perlstein struct proc *p; 87e15583ceSAlfred Perlstein 88e15583ceSAlfred Perlstein p = td->td_proc; 89e15583ceSAlfred Perlstein printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 90e15583ceSAlfred Perlstein va_start(ap, fmt); 91e15583ceSAlfred Perlstein vprintf(fmt, ap); 92e15583ceSAlfred Perlstein va_end(ap); 93e15583ceSAlfred Perlstein printf("\n"); 94e15583ceSAlfred Perlstein } 95060e4882SDoug Ambrisko 96060e4882SDoug Ambrisko struct device_element 97060e4882SDoug Ambrisko { 98060e4882SDoug Ambrisko TAILQ_ENTRY(device_element) list; 99060e4882SDoug Ambrisko struct linux_device_handler entry; 100060e4882SDoug Ambrisko }; 101060e4882SDoug Ambrisko 102060e4882SDoug Ambrisko static TAILQ_HEAD(, device_element) devices = 103060e4882SDoug Ambrisko TAILQ_HEAD_INITIALIZER(devices); 104060e4882SDoug Ambrisko 105060e4882SDoug Ambrisko static struct linux_device_handler null_handler = 106060e4882SDoug Ambrisko { "mem", "mem", "null", "null", 1, 3, 1}; 107060e4882SDoug Ambrisko 108060e4882SDoug Ambrisko DATA_SET(linux_device_handler_set, null_handler); 109060e4882SDoug Ambrisko 110060e4882SDoug Ambrisko char * 111060e4882SDoug Ambrisko linux_driver_get_name_dev(device_t dev) 112060e4882SDoug Ambrisko { 113060e4882SDoug Ambrisko struct device_element *de; 114060e4882SDoug Ambrisko const char *device_name = device_get_name(dev); 115060e4882SDoug Ambrisko 11667d39748SDmitry Chagin if (device_name == NULL) 117060e4882SDoug Ambrisko return NULL; 118060e4882SDoug Ambrisko TAILQ_FOREACH(de, &devices, list) { 11967d39748SDmitry Chagin if (strcmp(device_name, de->entry.bsd_driver_name) == 0) 120060e4882SDoug Ambrisko return (de->entry.linux_driver_name); 121060e4882SDoug Ambrisko } 122060e4882SDoug Ambrisko 12367d39748SDmitry Chagin return (NULL); 124060e4882SDoug Ambrisko } 125060e4882SDoug Ambrisko 126060e4882SDoug Ambrisko int 1277870adb6SEd Schouten linux_driver_get_major_minor(const char *node, int *major, int *minor) 128060e4882SDoug Ambrisko { 129060e4882SDoug Ambrisko struct device_element *de; 1302d347b2eSConrad Meyer unsigned long devno; 131f3792e07STijl Coosemans size_t sz; 132060e4882SDoug Ambrisko 13367d39748SDmitry Chagin if (node == NULL || major == NULL || minor == NULL) 134060e4882SDoug Ambrisko return 1; 135a147e6caSEd Schouten 136f3792e07STijl Coosemans sz = sizeof("pts/") - 1; 137f3792e07STijl Coosemans if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') { 138a147e6caSEd Schouten /* 139a147e6caSEd Schouten * Linux checks major and minors of the slave device 140a147e6caSEd Schouten * to make sure it's a pty device, so let's make him 141a147e6caSEd Schouten * believe it is. 142a147e6caSEd Schouten */ 143f3792e07STijl Coosemans devno = strtoul(node + sz, NULL, 10); 144a147e6caSEd Schouten *major = 136 + (devno / 256); 145a147e6caSEd Schouten *minor = devno % 256; 14667d39748SDmitry Chagin return (0); 147a147e6caSEd Schouten } 148a147e6caSEd Schouten 149f3792e07STijl Coosemans sz = sizeof("drm/") - 1; 150f3792e07STijl Coosemans if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') { 151f3792e07STijl Coosemans devno = strtoul(node + sz, NULL, 10); 1522d347b2eSConrad Meyer *major = 226 + (devno / 256); 1532d347b2eSConrad Meyer *minor = devno % 256; 1542d347b2eSConrad Meyer return (0); 1552d347b2eSConrad Meyer } 1562d347b2eSConrad Meyer 157060e4882SDoug Ambrisko TAILQ_FOREACH(de, &devices, list) { 158060e4882SDoug Ambrisko if (strcmp(node, de->entry.bsd_device_name) == 0) { 159060e4882SDoug Ambrisko *major = de->entry.linux_major; 160060e4882SDoug Ambrisko *minor = de->entry.linux_minor; 16167d39748SDmitry Chagin return (0); 162060e4882SDoug Ambrisko } 163060e4882SDoug Ambrisko } 164060e4882SDoug Ambrisko 16567d39748SDmitry Chagin return (1); 166060e4882SDoug Ambrisko } 167060e4882SDoug Ambrisko 168060e4882SDoug Ambrisko char * 169060e4882SDoug Ambrisko linux_get_char_devices() 170060e4882SDoug Ambrisko { 171060e4882SDoug Ambrisko struct device_element *de; 172060e4882SDoug Ambrisko char *temp, *string, *last; 173060e4882SDoug Ambrisko char formated[256]; 174060e4882SDoug Ambrisko int current_size = 0, string_size = 1024; 175060e4882SDoug Ambrisko 1761ede983cSDag-Erling Smørgrav string = malloc(string_size, M_LINUX, M_WAITOK); 177060e4882SDoug Ambrisko string[0] = '\000'; 178060e4882SDoug Ambrisko last = ""; 179060e4882SDoug Ambrisko TAILQ_FOREACH(de, &devices, list) { 180060e4882SDoug Ambrisko if (!de->entry.linux_char_device) 181060e4882SDoug Ambrisko continue; 182060e4882SDoug Ambrisko temp = string; 183060e4882SDoug Ambrisko if (strcmp(last, de->entry.bsd_driver_name) != 0) { 184060e4882SDoug Ambrisko last = de->entry.bsd_driver_name; 185060e4882SDoug Ambrisko 186060e4882SDoug Ambrisko snprintf(formated, sizeof(formated), "%3d %s\n", 187060e4882SDoug Ambrisko de->entry.linux_major, 188060e4882SDoug Ambrisko de->entry.linux_device_name); 189060e4882SDoug Ambrisko if (strlen(formated) + current_size 190060e4882SDoug Ambrisko >= string_size) { 191060e4882SDoug Ambrisko string_size *= 2; 1921ede983cSDag-Erling Smørgrav string = malloc(string_size, 193060e4882SDoug Ambrisko M_LINUX, M_WAITOK); 194060e4882SDoug Ambrisko bcopy(temp, string, current_size); 1951ede983cSDag-Erling Smørgrav free(temp, M_LINUX); 196060e4882SDoug Ambrisko } 197060e4882SDoug Ambrisko strcat(string, formated); 198060e4882SDoug Ambrisko current_size = strlen(string); 199060e4882SDoug Ambrisko } 200060e4882SDoug Ambrisko } 201060e4882SDoug Ambrisko 20267d39748SDmitry Chagin return (string); 203060e4882SDoug Ambrisko } 204060e4882SDoug Ambrisko 205060e4882SDoug Ambrisko void 206060e4882SDoug Ambrisko linux_free_get_char_devices(char *string) 207060e4882SDoug Ambrisko { 20819e252baSAlexander Leidinger 2091ede983cSDag-Erling Smørgrav free(string, M_LINUX); 210060e4882SDoug Ambrisko } 211060e4882SDoug Ambrisko 212060e4882SDoug Ambrisko static int linux_major_starting = 200; 213060e4882SDoug Ambrisko 214060e4882SDoug Ambrisko int 215060e4882SDoug Ambrisko linux_device_register_handler(struct linux_device_handler *d) 216060e4882SDoug Ambrisko { 217060e4882SDoug Ambrisko struct device_element *de; 218060e4882SDoug Ambrisko 21967d39748SDmitry Chagin if (d == NULL) 22019e252baSAlexander Leidinger return (EINVAL); 22119e252baSAlexander Leidinger 22219e252baSAlexander Leidinger de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 223060e4882SDoug Ambrisko if (d->linux_major < 0) { 224060e4882SDoug Ambrisko d->linux_major = linux_major_starting++; 225060e4882SDoug Ambrisko } 226060e4882SDoug Ambrisko bcopy(d, &de->entry, sizeof(*d)); 227060e4882SDoug Ambrisko 228060e4882SDoug Ambrisko /* Add the element to the list, sorted on span. */ 229060e4882SDoug Ambrisko TAILQ_INSERT_TAIL(&devices, de, list); 230060e4882SDoug Ambrisko 231060e4882SDoug Ambrisko return (0); 232060e4882SDoug Ambrisko } 233060e4882SDoug Ambrisko 234060e4882SDoug Ambrisko int 235060e4882SDoug Ambrisko linux_device_unregister_handler(struct linux_device_handler *d) 236060e4882SDoug Ambrisko { 237060e4882SDoug Ambrisko struct device_element *de; 238060e4882SDoug Ambrisko 23967d39748SDmitry Chagin if (d == NULL) 240060e4882SDoug Ambrisko return (EINVAL); 241060e4882SDoug Ambrisko 242060e4882SDoug Ambrisko TAILQ_FOREACH(de, &devices, list) { 243060e4882SDoug Ambrisko if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 244060e4882SDoug Ambrisko TAILQ_REMOVE(&devices, de, list); 2451ede983cSDag-Erling Smørgrav free(de, M_LINUX); 24619e252baSAlexander Leidinger 247060e4882SDoug Ambrisko return (0); 248060e4882SDoug Ambrisko } 249060e4882SDoug Ambrisko } 250060e4882SDoug Ambrisko 251060e4882SDoug Ambrisko return (EINVAL); 252060e4882SDoug Ambrisko } 253