1 /*- 2 * Copyright (c) 1994 Christos Zoulas 3 * Copyright (c) 1995 Frank van der Linden 4 * Copyright (c) 1995 Scott Bartram 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_compat.h" 36 37 #include <sys/param.h> 38 #include <sys/bus.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/linker_set.h> 42 #include <sys/mutex.h> 43 #include <sys/namei.h> 44 #include <sys/proc.h> 45 #include <sys/syscallsubr.h> 46 #include <sys/systm.h> 47 #include <sys/vnode.h> 48 49 #include <machine/stdarg.h> 50 51 #include <compat/linux/linux_util.h> 52 #ifdef COMPAT_LINUX32 53 #include <machine/../linux32/linux.h> 54 #else 55 #include <machine/../linux/linux.h> 56 #endif 57 58 const char linux_emul_path[] = "/compat/linux"; 59 60 /* 61 * Search an alternate path before passing pathname arguments on to 62 * system calls. Useful for keeping a separate 'emulation tree'. 63 * 64 * If cflag is set, we check if an attempt can be made to create the 65 * named file, i.e. we check if the directory it should be in exists. 66 */ 67 int 68 linux_emul_convpath(td, path, pathseg, pbuf, cflag) 69 struct thread *td; 70 char *path; 71 enum uio_seg pathseg; 72 char **pbuf; 73 int cflag; 74 { 75 76 return (kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 77 cflag)); 78 } 79 80 void 81 linux_msg(const struct thread *td, const char *fmt, ...) 82 { 83 va_list ap; 84 struct proc *p; 85 86 p = td->td_proc; 87 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 88 va_start(ap, fmt); 89 vprintf(fmt, ap); 90 va_end(ap); 91 printf("\n"); 92 } 93 94 struct device_element 95 { 96 TAILQ_ENTRY(device_element) list; 97 struct linux_device_handler entry; 98 }; 99 100 static TAILQ_HEAD(, device_element) devices = 101 TAILQ_HEAD_INITIALIZER(devices); 102 103 static struct linux_device_handler null_handler = 104 { "mem", "mem", "null", "null", 1, 3, 1}; 105 106 DATA_SET(linux_device_handler_set, null_handler); 107 108 char * 109 linux_driver_get_name_dev(device_t dev) 110 { 111 struct device_element *de; 112 const char *device_name = device_get_name(dev); 113 114 if (device_name == NULL) 115 return NULL; 116 TAILQ_FOREACH(de, &devices, list) { 117 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) 118 return (de->entry.linux_driver_name); 119 } 120 121 return NULL; 122 } 123 124 int 125 linux_driver_get_major_minor(char *node, int *major, int *minor) 126 { 127 struct device_element *de; 128 129 if (node == NULL || major == NULL || minor == NULL) 130 return 1; 131 TAILQ_FOREACH(de, &devices, list) { 132 if (strcmp(node, de->entry.bsd_device_name) == 0) { 133 *major = de->entry.linux_major; 134 *minor = de->entry.linux_minor; 135 return 0; 136 } 137 } 138 139 return 1; 140 } 141 142 char * 143 linux_get_char_devices() 144 { 145 struct device_element *de; 146 char *temp, *string, *last; 147 char formated[256]; 148 int current_size = 0, string_size = 1024; 149 150 MALLOC(string, char *, string_size, M_LINUX, M_WAITOK); 151 string[0] = '\000'; 152 last = ""; 153 TAILQ_FOREACH(de, &devices, list) { 154 if (!de->entry.linux_char_device) 155 continue; 156 temp = string; 157 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 158 last = de->entry.bsd_driver_name; 159 160 snprintf(formated, sizeof(formated), "%3d %s\n", 161 de->entry.linux_major, 162 de->entry.linux_device_name); 163 if (strlen(formated) + current_size 164 >= string_size) { 165 string_size *= 2; 166 MALLOC(string, char *, string_size, 167 M_LINUX, M_WAITOK); 168 bcopy(temp, string, current_size); 169 FREE(temp, M_LINUX); 170 } 171 strcat(string, formated); 172 current_size = strlen(string); 173 } 174 } 175 176 return string; 177 } 178 179 void 180 linux_free_get_char_devices(char *string) 181 { 182 FREE(string, M_LINUX); 183 } 184 185 static int linux_major_starting = 200; 186 187 int 188 linux_device_register_handler(struct linux_device_handler *d) 189 { 190 struct device_element *de; 191 192 if (d == NULL) 193 return (EINVAL); 194 195 MALLOC(de, struct device_element *, sizeof(*de), 196 M_LINUX, M_WAITOK); 197 if (d->linux_major < 0) { 198 d->linux_major = linux_major_starting++; 199 } 200 bcopy(d, &de->entry, sizeof(*d)); 201 202 /* Add the element to the list, sorted on span. */ 203 TAILQ_INSERT_TAIL(&devices, de, list); 204 205 return (0); 206 } 207 208 int 209 linux_device_unregister_handler(struct linux_device_handler *d) 210 { 211 struct device_element *de; 212 213 if (d == NULL) 214 return (EINVAL); 215 216 TAILQ_FOREACH(de, &devices, list) { 217 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 218 TAILQ_REMOVE(&devices, de, list); 219 FREE(de, M_LINUX); 220 return (0); 221 } 222 } 223 224 return (EINVAL); 225 } 226