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>
| 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: head/sys/compat/linux/linux_util.c 258541 2013-11-25 07:38:45Z attilio $");
| 33__FBSDID("$FreeBSD: head/sys/compat/linux/linux_util.c 283421 2015-05-24 15:51:18Z dchagin $");
|
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/kernel.h> 43#include <sys/linker_set.h> 44#include <sys/mutex.h> 45#include <sys/namei.h> 46#include <sys/proc.h> 47#include <sys/sdt.h> 48#include <sys/syscallsubr.h> 49#include <sys/systm.h> 50#include <sys/vnode.h> 51 52#include <machine/stdarg.h> 53 54#include <compat/linux/linux_util.h>
| 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/kernel.h> 43#include <sys/linker_set.h> 44#include <sys/mutex.h> 45#include <sys/namei.h> 46#include <sys/proc.h> 47#include <sys/sdt.h> 48#include <sys/syscallsubr.h> 49#include <sys/systm.h> 50#include <sys/vnode.h> 51 52#include <machine/stdarg.h> 53 54#include <compat/linux/linux_util.h>
|
55#ifdef COMPAT_LINUX32 56#include <machine/../linux32/linux.h> 57#else 58#include <machine/../linux/linux.h> 59#endif
| |
60
| 55
|
61#include <compat/linux/linux_dtrace.h>
| 56MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
|
62 63const char linux_emul_path[] = "/compat/linux"; 64
| 57 58const char linux_emul_path[] = "/compat/linux"; 59
|
65/* DTrace init */ 66LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 67 68/** 69 * DTrace probes in this module. 70 */ 71LIN_SDT_PROBE_DEFINE5(util, linux_emul_convpath, entry, "const char *", 72 "enum uio_seg", "char **", "int", "int"); 73LIN_SDT_PROBE_DEFINE1(util, linux_emul_convpath, return, "int"); 74LIN_SDT_PROBE_DEFINE1(util, linux_msg, entry, "const char *"); 75LIN_SDT_PROBE_DEFINE0(util, linux_msg, return); 76LIN_SDT_PROBE_DEFINE2(util, linux_driver_get_name_dev, entry, "device_t", 77 "const char *"); 78LIN_SDT_PROBE_DEFINE0(util, linux_driver_get_name_dev, nullcall); 79LIN_SDT_PROBE_DEFINE1(util, linux_driver_get_name_dev, return, "char *"); 80LIN_SDT_PROBE_DEFINE3(util, linux_driver_get_major_minor, entry, "char *", 81 "int *", "int *"); 82LIN_SDT_PROBE_DEFINE0(util, linux_driver_get_major_minor, nullcall); 83LIN_SDT_PROBE_DEFINE1(util, linux_driver_get_major_minor, notfound, "char *"); 84LIN_SDT_PROBE_DEFINE3(util, linux_driver_get_major_minor, return, "int", 85 "int", "int"); 86LIN_SDT_PROBE_DEFINE0(util, linux_get_char_devices, entry); 87LIN_SDT_PROBE_DEFINE1(util, linux_get_char_devices, return, "char *"); 88LIN_SDT_PROBE_DEFINE1(util, linux_free_get_char_devices, entry, "char *"); 89LIN_SDT_PROBE_DEFINE0(util, linux_free_get_char_devices, return); 90LIN_SDT_PROBE_DEFINE1(util, linux_device_register_handler, entry, 91 "struct linux_device_handler *"); 92LIN_SDT_PROBE_DEFINE1(util, linux_device_register_handler, return, "int"); 93LIN_SDT_PROBE_DEFINE1(util, linux_device_unregister_handler, entry, 94 "struct linux_device_handler *"); 95LIN_SDT_PROBE_DEFINE1(util, linux_device_unregister_handler, return, "int"); 96
| |
97/* 98 * Search an alternate path before passing pathname arguments on to 99 * system calls. Useful for keeping a separate 'emulation tree'. 100 * 101 * If cflag is set, we check if an attempt can be made to create the 102 * named file, i.e. we check if the directory it should be in exists. 103 */ 104int 105linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg, 106 char **pbuf, int cflag, int dfd) 107{ 108 int retval; 109
| 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 */ 67int 68linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg, 69 char **pbuf, int cflag, int dfd) 70{ 71 int retval; 72
|
110 LIN_SDT_PROBE5(util, linux_emul_convpath, entry, path, pathseg, pbuf, 111 cflag, dfd); 112
| |
113 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 114 cflag, dfd); 115
| 73 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 74 cflag, dfd); 75
|
116 LIN_SDT_PROBE1(util, linux_emul_convpath, return, retval);
| |
117 return (retval); 118} 119 120void 121linux_msg(const struct thread *td, const char *fmt, ...) 122{ 123 va_list ap; 124 struct proc *p; 125
| 76 return (retval); 77} 78 79void 80linux_msg(const struct thread *td, const char *fmt, ...) 81{ 82 va_list ap; 83 struct proc *p; 84
|
126 LIN_SDT_PROBE1(util, linux_msg, entry, fmt); 127
| |
128 p = td->td_proc; 129 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 130 va_start(ap, fmt); 131 vprintf(fmt, ap); 132 va_end(ap); 133 printf("\n");
| 85 p = td->td_proc; 86 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 87 va_start(ap, fmt); 88 vprintf(fmt, ap); 89 va_end(ap); 90 printf("\n");
|
134 135 LIN_SDT_PROBE0(util, linux_msg, return);
| |
136} 137 138struct device_element 139{ 140 TAILQ_ENTRY(device_element) list; 141 struct linux_device_handler entry; 142}; 143 144static TAILQ_HEAD(, device_element) devices = 145 TAILQ_HEAD_INITIALIZER(devices); 146 147static struct linux_device_handler null_handler = 148 { "mem", "mem", "null", "null", 1, 3, 1}; 149 150DATA_SET(linux_device_handler_set, null_handler); 151 152char * 153linux_driver_get_name_dev(device_t dev) 154{ 155 struct device_element *de; 156 const char *device_name = device_get_name(dev); 157
| 91} 92 93struct device_element 94{ 95 TAILQ_ENTRY(device_element) list; 96 struct linux_device_handler entry; 97}; 98 99static TAILQ_HEAD(, device_element) devices = 100 TAILQ_HEAD_INITIALIZER(devices); 101 102static struct linux_device_handler null_handler = 103 { "mem", "mem", "null", "null", 1, 3, 1}; 104 105DATA_SET(linux_device_handler_set, null_handler); 106 107char * 108linux_driver_get_name_dev(device_t dev) 109{ 110 struct device_element *de; 111 const char *device_name = device_get_name(dev); 112
|
158 LIN_SDT_PROBE2(util, linux_driver_get_name_dev, entry, dev, 159 device_name); 160 161 if (device_name == NULL) { 162 LIN_SDT_PROBE0(util, linux_driver_get_name_dev, nullcall); 163 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL);
| 113 if (device_name == NULL)
|
164 return NULL;
| 114 return NULL;
|
165 }
| |
166 TAILQ_FOREACH(de, &devices, list) {
| 115 TAILQ_FOREACH(de, &devices, list) {
|
167 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) { 168 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, 169 de->entry.linux_driver_name);
| 116 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
|
170 return (de->entry.linux_driver_name);
| 117 return (de->entry.linux_driver_name);
|
171 }
| |
172 } 173
| 118 } 119
|
174 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL); 175 return NULL;
| 120 return (NULL);
|
176} 177 178int 179linux_driver_get_major_minor(const char *node, int *major, int *minor) 180{ 181 struct device_element *de; 182
| 121} 122 123int 124linux_driver_get_major_minor(const char *node, int *major, int *minor) 125{ 126 struct device_element *de; 127
|
183 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, entry, node, major, 184 minor); 185 186 if (node == NULL || major == NULL || minor == NULL) { 187 LIN_SDT_PROBE0(util, linux_driver_get_major_minor, nullcall); 188 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 1, 189 0, 0);
| 128 if (node == NULL || major == NULL || minor == NULL)
|
190 return 1;
| 129 return 1;
|
191 }
| |
192 193 if (strlen(node) > strlen("pts/") && 194 strncmp(node, "pts/", strlen("pts/")) == 0) { 195 unsigned long devno; 196 197 /* 198 * Linux checks major and minors of the slave device 199 * to make sure it's a pty device, so let's make him 200 * believe it is. 201 */ 202 devno = strtoul(node + strlen("pts/"), NULL, 10); 203 *major = 136 + (devno / 256); 204 *minor = devno % 256; 205
| 130 131 if (strlen(node) > strlen("pts/") && 132 strncmp(node, "pts/", strlen("pts/")) == 0) { 133 unsigned long devno; 134 135 /* 136 * Linux checks major and minors of the slave device 137 * to make sure it's a pty device, so let's make him 138 * believe it is. 139 */ 140 devno = strtoul(node + strlen("pts/"), NULL, 10); 141 *major = 136 + (devno / 256); 142 *minor = devno % 256; 143
|
206 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 0, 207 *major, *minor); 208 return 0;
| 144 return (0);
|
209 } 210 211 TAILQ_FOREACH(de, &devices, list) { 212 if (strcmp(node, de->entry.bsd_device_name) == 0) { 213 *major = de->entry.linux_major; 214 *minor = de->entry.linux_minor;
| 145 } 146 147 TAILQ_FOREACH(de, &devices, list) { 148 if (strcmp(node, de->entry.bsd_device_name) == 0) { 149 *major = de->entry.linux_major; 150 *minor = de->entry.linux_minor;
|
215 216 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, 217 return, 0, *major, *minor); 218 return 0;
| 151 return (0);
|
219 } 220 } 221
| 152 } 153 } 154
|
222 LIN_SDT_PROBE1(util, linux_driver_get_major_minor, notfound, node); 223 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 1, 0, 0); 224 return 1;
| 155 return (1);
|
225} 226 227char * 228linux_get_char_devices() 229{ 230 struct device_element *de; 231 char *temp, *string, *last; 232 char formated[256]; 233 int current_size = 0, string_size = 1024; 234
| 156} 157 158char * 159linux_get_char_devices() 160{ 161 struct device_element *de; 162 char *temp, *string, *last; 163 char formated[256]; 164 int current_size = 0, string_size = 1024; 165
|
235 LIN_SDT_PROBE0(util, linux_get_char_devices, entry); 236
| |
237 string = malloc(string_size, M_LINUX, M_WAITOK); 238 string[0] = '\000'; 239 last = ""; 240 TAILQ_FOREACH(de, &devices, list) { 241 if (!de->entry.linux_char_device) 242 continue; 243 temp = string; 244 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 245 last = de->entry.bsd_driver_name; 246 247 snprintf(formated, sizeof(formated), "%3d %s\n", 248 de->entry.linux_major, 249 de->entry.linux_device_name); 250 if (strlen(formated) + current_size 251 >= string_size) { 252 string_size *= 2; 253 string = malloc(string_size, 254 M_LINUX, M_WAITOK); 255 bcopy(temp, string, current_size); 256 free(temp, M_LINUX); 257 } 258 strcat(string, formated); 259 current_size = strlen(string); 260 } 261 } 262
| 166 string = malloc(string_size, M_LINUX, M_WAITOK); 167 string[0] = '\000'; 168 last = ""; 169 TAILQ_FOREACH(de, &devices, list) { 170 if (!de->entry.linux_char_device) 171 continue; 172 temp = string; 173 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 174 last = de->entry.bsd_driver_name; 175 176 snprintf(formated, sizeof(formated), "%3d %s\n", 177 de->entry.linux_major, 178 de->entry.linux_device_name); 179 if (strlen(formated) + current_size 180 >= string_size) { 181 string_size *= 2; 182 string = malloc(string_size, 183 M_LINUX, M_WAITOK); 184 bcopy(temp, string, current_size); 185 free(temp, M_LINUX); 186 } 187 strcat(string, formated); 188 current_size = strlen(string); 189 } 190 } 191
|
263 LIN_SDT_PROBE1(util, linux_get_char_devices, return, string); 264 return string;
| 192 return (string);
|
265} 266 267void 268linux_free_get_char_devices(char *string) 269{ 270
| 193} 194 195void 196linux_free_get_char_devices(char *string) 197{ 198
|
271 LIN_SDT_PROBE1(util, linux_get_char_devices, entry, string); 272
| |
273 free(string, M_LINUX);
| 199 free(string, M_LINUX);
|
274 275 LIN_SDT_PROBE0(util, linux_get_char_devices, return);
| |
276} 277 278static int linux_major_starting = 200; 279 280int 281linux_device_register_handler(struct linux_device_handler *d) 282{ 283 struct device_element *de; 284
| 200} 201 202static int linux_major_starting = 200; 203 204int 205linux_device_register_handler(struct linux_device_handler *d) 206{ 207 struct device_element *de; 208
|
285 LIN_SDT_PROBE1(util, linux_device_register_handler, entry, d); 286 287 if (d == NULL) { 288 LIN_SDT_PROBE1(util, linux_device_register_handler, return, 289 EINVAL);
| 209 if (d == NULL)
|
290 return (EINVAL);
| 210 return (EINVAL);
|
291 }
| |
292 293 de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 294 if (d->linux_major < 0) { 295 d->linux_major = linux_major_starting++; 296 } 297 bcopy(d, &de->entry, sizeof(*d)); 298 299 /* Add the element to the list, sorted on span. */ 300 TAILQ_INSERT_TAIL(&devices, de, list); 301
| 211 212 de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 213 if (d->linux_major < 0) { 214 d->linux_major = linux_major_starting++; 215 } 216 bcopy(d, &de->entry, sizeof(*d)); 217 218 /* Add the element to the list, sorted on span. */ 219 TAILQ_INSERT_TAIL(&devices, de, list); 220
|
302 LIN_SDT_PROBE1(util, linux_device_register_handler, return, 0);
| |
303 return (0); 304} 305 306int 307linux_device_unregister_handler(struct linux_device_handler *d) 308{ 309 struct device_element *de; 310
| 221 return (0); 222} 223 224int 225linux_device_unregister_handler(struct linux_device_handler *d) 226{ 227 struct device_element *de; 228
|
311 LIN_SDT_PROBE1(util, linux_device_unregister_handler, entry, d); 312 313 if (d == NULL) { 314 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return, 315 EINVAL);
| 229 if (d == NULL)
|
316 return (EINVAL);
| 230 return (EINVAL);
|
317 }
| |
318 319 TAILQ_FOREACH(de, &devices, list) { 320 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 321 TAILQ_REMOVE(&devices, de, list); 322 free(de, M_LINUX); 323
| 231 232 TAILQ_FOREACH(de, &devices, list) { 233 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 234 TAILQ_REMOVE(&devices, de, list); 235 free(de, M_LINUX); 236
|
324 LIN_SDT_PROBE1(util, linux_device_unregister_handler, 325 return, 0);
| |
326 return (0); 327 } 328 } 329
| 237 return (0); 238 } 239 } 240
|
330 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return, EINVAL);
| |
331 return (EINVAL); 332}
| 241 return (EINVAL); 242}
|