i386-linux.c revision 32355
1/* 2 * Copryight 1997 Sean Eric Fagan 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. All advertising materials mentioning features or use of this software 13 * must display the following acknowledgement: 14 * This product includes software developed by Sean Eric Fagan 15 * 4. Neither the name of the author may be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#ifndef lint 33static const char rcsid[] = 34 "$Id: i386-linux.c,v 1.3 1998/01/05 07:30:25 charnier Exp $"; 35#endif /* not lint */ 36 37/* 38 * Linux/i386-specific system call handling. Given how much of this code 39 * is taken from the freebsd equivalent, I can probably put even more of 40 * it in support routines that can be used by any personality support. 41 */ 42 43#include <errno.h> 44#include <fcntl.h> 45#include <signal.h> 46#include <stdio.h> 47#include <stdlib.h> 48#include <string.h> 49#include <unistd.h> 50#include <sys/ioctl.h> 51#include <sys/pioctl.h> 52#include <i386/include/reg.h> 53#include <i386/include/psl.h> 54 55#include "syscall.h" 56 57static int fd = -1; 58static int cpid = -1; 59extern int Procfd; 60 61extern FILE *outfile; 62#include "linux_syscalls.h" 63 64static int nsyscalls = 65 sizeof(linux_syscallnames) / sizeof(linux_syscallnames[0]); 66 67/* See the comment in i386-fbsd.c about this structure. */ 68static struct linux_syscall { 69 struct syscall *sc; 70 char *name; 71 int number; 72 unsigned long args[5]; 73 int nargs; /* number of arguments -- *not* number of words! */ 74 char **s_args; /* the printable arguments */ 75} lsc; 76 77static inline void 78clear_lsc() { 79 if (lsc.s_args) { 80 int i; 81 for (i = 0; i < lsc.nargs; i++) 82 if (lsc.s_args[i]) 83 free(lsc.s_args[i]); 84 free(lsc.s_args); 85 } 86 memset(&lsc, 0, sizeof(lsc)); 87} 88 89void 90i386_linux_syscall_entry(int pid, int nargs) { 91 char buf[32]; 92 struct reg regs = { 0 }; 93 int syscall; 94 int i; 95 struct syscall *sc; 96 97 if (fd == -1 || pid != cpid) { 98 sprintf(buf, "/proc/%d/regs", pid); 99 fd = open(buf, O_RDWR); 100 if (fd == -1) { 101 fprintf(outfile, "-- CANNOT READ REGISTERS --\n"); 102 return; 103 } 104 cpid = pid; 105 } 106 107 clear_lsc(); 108 lseek(fd, 0L, 0); 109 i = read(fd, ®s, sizeof(regs)); 110 syscall = regs.r_eax; 111 112 lsc.number = syscall; 113 lsc.name = 114 (syscall < 0 || syscall > nsyscalls) ? NULL : linux_syscallnames[syscall]; 115 if (!lsc.name) { 116 fprintf (outfile, "-- UNKNOWN SYSCALL %d\n", syscall); 117 } 118 119 if (nargs == 0) 120 return; 121 122 /* 123 * Linux passes syscall arguments in registers, not 124 * on the stack. Fortunately, we've got access to the 125 * register set. Note that we don't bother checking the 126 * number of arguments. And what does linux do for syscalls 127 * that have more than five arguments? 128 */ 129 130 lsc.args[0] = regs.r_ebx; 131 lsc.args[1] = regs.r_ecx; 132 lsc.args[2] = regs.r_edx; 133 lsc.args[3] = regs.r_esi; 134 lsc.args[4] = regs.r_edi; 135 136 sc = get_syscall(lsc.name); 137 if (sc) { 138 lsc.nargs = sc->nargs; 139 } else { 140#ifdef DEBUG 141 fprintf(outfile, "unknown syscall %s -- setting args to %d\n", 142 lsc.name, nargs); 143#endif 144 lsc.nargs = nargs; 145 } 146 147 lsc.s_args = malloc((1+lsc.nargs) * sizeof(char*)); 148 memset(lsc.s_args, 0, lsc.nargs * sizeof(char*)); 149 lsc.sc = sc; 150 151 if (lsc.name) { 152 153#ifdef DEBUG 154 fprintf(stderr, "syscall %s(", lsc.name); 155#endif 156 for (i = 0; i < lsc.nargs ; i++) { 157#ifdef DEBUG 158 fprintf(stderr, "0x%x%s", 159 sc ? 160 lsc.args[sc->args[i].offset] 161 : lsc.args[i], 162 i < (lsc.nargs - 1) ? "," : ""); 163#endif 164 if (sc && !(sc->args[i].type & OUT)) { 165 lsc.s_args[i] = print_arg(Procfd, &sc->args[i], lsc.args); 166 } 167 } 168#ifdef DEBUG 169 fprintf(stderr, ")\n"); 170#endif 171 } 172 173 if (!strcmp(lsc.name, "linux_execve") || !strcmp(lsc.name, "exit")) { 174 print_syscall(outfile, lsc.name, lsc.nargs, lsc.s_args); 175 } 176 177 return; 178} 179 180/* 181 * Linux syscalls return negative errno's, we do positive and map them 182 */ 183int bsd_to_linux_errno[] = { 184 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 185 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 186 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 187 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 188 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 189 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 190 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 191 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 192 -6, 193}; 194 195void 196i386_linux_syscall_exit(int pid, int syscall) { 197 char buf[32]; 198 struct reg regs; 199 int retval; 200 int i; 201 int errorp; 202 struct syscall *sc; 203 204 if (fd == -1 || pid != cpid) { 205 sprintf(buf, "/proc/%d/regs", pid); 206 fd = open(buf, O_RDONLY); 207 if (fd == -1) { 208 fprintf(outfile, "-- CANNOT READ REGISTERS --\n"); 209 return; 210 } 211 cpid = pid; 212 } 213 214 lseek(fd, 0L, 0); 215 if (read(fd, ®s, sizeof(regs)) != sizeof(regs)) 216 return; 217 218 retval = regs.r_eax; 219 errorp = !!(regs.r_eflags & PSL_C); 220 221 sc = lsc.sc; 222 if (!sc) { 223 for (i = 0; i < lsc.nargs; i++) { 224 lsc.s_args[i] = malloc(12); 225 sprintf(lsc.s_args[i], "0x%x", lsc.args[i]); 226 } 227 } else { 228 for (i = 0; i < sc->nargs; i++) { 229 char *temp; 230 if (sc->args[i].type & OUT) { 231 if (errorp) { 232 temp = malloc(12); 233 sprintf(temp, "0x%x", lsc.args[sc->args[i].offset]); 234 } else { 235 temp = print_arg(Procfd, &sc->args[i], lsc.args); 236 } 237 lsc.s_args[i] = temp; 238 } 239 } 240 } 241 print_syscall(outfile, lsc.name, lsc.nargs, lsc.s_args); 242 if (errorp) { 243 for (i = 0; i < sizeof(bsd_to_linux_errno) / sizeof(int); i++) 244 if (retval == bsd_to_linux_errno[i]) 245 break; 246 fprintf(outfile, "errno %d '%s'\n", retval, strerror(i)); 247 } else { 248 fprintf(outfile, "returns %d (0x%x)\n", retval, retval); 249 } 250 clear_lsc(); 251 return; 252} 253