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/fcntl.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/linker_set.h> 43 #include <sys/mutex.h> 44 #include <sys/namei.h> 45 #include <sys/proc.h> 46 #include <sys/syscallsubr.h> 47 #include <sys/systm.h> 48 #include <sys/vnode.h> 49 50 #include <machine/stdarg.h> 51 52 #include <compat/linux/linux_util.h> 53 #ifdef COMPAT_LINUX32 54 #include <machine/../linux32/linux.h> 55 #else 56 #include <machine/../linux/linux.h> 57 #endif 58 59 const char linux_emul_path[] = "/compat/linux"; 60 61 /* 62 * Search an alternate path before passing pathname arguments on to 63 * system calls. Useful for keeping a separate 'emulation tree'. 64 * 65 * If cflag is set, we check if an attempt can be made to create the 66 * named file, i.e. we check if the directory it should be in exists. 67 */ 68 int 69 linux_emul_convpath(td, path, pathseg, pbuf, cflag, dfd) 70 struct thread *td; 71 const char *path; 72 enum uio_seg pathseg; 73 char **pbuf; 74 int cflag; 75 int dfd; 76 { 77 78 return (kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 79 cflag, dfd)); 80 } 81 82 void 83 linux_msg(const struct thread *td, const char *fmt, ...) 84 { 85 va_list ap; 86 struct proc *p; 87 88 p = td->td_proc; 89 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 90 va_start(ap, fmt); 91 vprintf(fmt, ap); 92 va_end(ap); 93 printf("\n"); 94 } 95 96 struct device_element 97 { 98 TAILQ_ENTRY(device_element) list; 99 struct linux_device_handler entry; 100 }; 101 102 static TAILQ_HEAD(, device_element) devices = 103 TAILQ_HEAD_INITIALIZER(devices); 104 105 static struct linux_device_handler null_handler = 106 { "mem", "mem", "null", "null", 1, 3, 1}; 107 108 DATA_SET(linux_device_handler_set, null_handler); 109 110 char * 111 linux_driver_get_name_dev(device_t dev) 112 { 113 struct device_element *de; 114 const char *device_name = device_get_name(dev); 115 116 if (device_name == NULL) 117 return NULL; 118 TAILQ_FOREACH(de, &devices, list) { 119 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) 120 return (de->entry.linux_driver_name); 121 } 122 123 return NULL; 124 } 125 126 int 127 linux_driver_get_major_minor(char *node, int *major, int *minor) 128 { 129 struct device_element *de; 130 131 if (node == NULL || major == NULL || minor == NULL) 132 return 1; 133 TAILQ_FOREACH(de, &devices, list) { 134 if (strcmp(node, de->entry.bsd_device_name) == 0) { 135 *major = de->entry.linux_major; 136 *minor = de->entry.linux_minor; 137 return 0; 138 } 139 } 140 141 return 1; 142 } 143 144 char * 145 linux_get_char_devices() 146 { 147 struct device_element *de; 148 char *temp, *string, *last; 149 char formated[256]; 150 int current_size = 0, string_size = 1024; 151 152 MALLOC(string, char *, string_size, M_LINUX, M_WAITOK); 153 string[0] = '\000'; 154 last = ""; 155 TAILQ_FOREACH(de, &devices, list) { 156 if (!de->entry.linux_char_device) 157 continue; 158 temp = string; 159 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 160 last = de->entry.bsd_driver_name; 161 162 snprintf(formated, sizeof(formated), "%3d %s\n", 163 de->entry.linux_major, 164 de->entry.linux_device_name); 165 if (strlen(formated) + current_size 166 >= string_size) { 167 string_size *= 2; 168 MALLOC(string, char *, string_size, 169 M_LINUX, M_WAITOK); 170 bcopy(temp, string, current_size); 171 FREE(temp, M_LINUX); 172 } 173 strcat(string, formated); 174 current_size = strlen(string); 175 } 176 } 177 178 return string; 179 } 180 181 void 182 linux_free_get_char_devices(char *string) 183 { 184 FREE(string, M_LINUX); 185 } 186 187 static int linux_major_starting = 200; 188 189 int 190 linux_device_register_handler(struct linux_device_handler *d) 191 { 192 struct device_element *de; 193 194 if (d == NULL) 195 return (EINVAL); 196 197 MALLOC(de, struct device_element *, sizeof(*de), 198 M_LINUX, M_WAITOK); 199 if (d->linux_major < 0) { 200 d->linux_major = linux_major_starting++; 201 } 202 bcopy(d, &de->entry, sizeof(*d)); 203 204 /* Add the element to the list, sorted on span. */ 205 TAILQ_INSERT_TAIL(&devices, de, list); 206 207 return (0); 208 } 209 210 int 211 linux_device_unregister_handler(struct linux_device_handler *d) 212 { 213 struct device_element *de; 214 215 if (d == NULL) 216 return (EINVAL); 217 218 TAILQ_FOREACH(de, &devices, list) { 219 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 220 TAILQ_REMOVE(&devices, de, list); 221 FREE(de, M_LINUX); 222 return (0); 223 } 224 } 225 226 return (EINVAL); 227 } 228