i386-linux.c revision 31899
1329099Skevans/* 2185222Ssam * Copryight 1997 Sean Eric Fagan 3330449Seadler * 4330449Seadler * Redistribution and use in source and binary forms, with or without 5330449Seadler * modification, are permitted provided that the following conditions 6185222Ssam * are met: 7185222Ssam * 1. Redistributions of source code must retain the above copyright 8185222Ssam * notice, this list of conditions and the following disclaimer. 9185222Ssam * 2. Redistributions in binary form must reproduce the above copyright 10185222Ssam * notice, this list of conditions and the following disclaimer in the 11185222Ssam * documentation and/or other materials provided with the distribution. 12185222Ssam * 3. All advertising materials mentioning features or use of this software 13185222Ssam * must display the following acknowledgement: 14185222Ssam * This product includes software developed by Sean Eric Fagan 15185222Ssam * 4. Neither the name of the author may be used to endorse or promote 16185222Ssam * products derived from this software without specific prior written 17185222Ssam * permission. 18185222Ssam * 19185222Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20185222Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21185222Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22185222Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23185222Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24185222Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25185222Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26185222Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27185222Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28185222Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29185222Ssam * SUCH DAMAGE. 30185222Ssam * 31185222Ssam */ 32185222Ssam 33185222Ssam/* 34185222Ssam * Linux/i386-specific system call handling. Given how much of this code 35185222Ssam * is taken from the freebsd equivalent, I can probably put even more of 36185222Ssam * it in support routines that can be used by any personality support. 37185222Ssam */ 38186334Ssam/* 39186334Ssam * $Id: i386-linux.c,v 1.1 1997/12/06 05:22:54 sef Exp $ 40185222Ssam */ 41185222Ssam#include <stdio.h> 42185222Ssam#include <stdlib.h> 43185222Ssam#include <string.h> 44185222Ssam#include <errno.h> 45185222Ssam#include <err.h> 46185222Ssam#include <signal.h> 47185222Ssam#include <fcntl.h> 48329099Skevans#include <unistd.h> 49329099Skevans#include <sys/ioctl.h> 50329099Skevans#include <sys/pioctl.h> 51329099Skevans#include <machine/reg.h> 52329099Skevans#include <machine/psl.h> 53329099Skevans 54329099Skevans#include "syscall.h" 55329099Skevans 56329099Skevansstatic int fd = -1; 57185222Ssamstatic int cpid = -1; 58185222Ssamextern int Procfd; 59185222Ssam 60185222Ssamextern FILE *outfile; 61185222Ssam#include "linux_syscalls.h" 62185222Ssam 63185222Ssamstatic int nsyscalls = 64185222Ssam sizeof(linux_syscallnames) / sizeof(linux_syscallnames[0]); 65185222Ssam 66185222Ssam/* See the comment in i386-fbsd.c about this structure. */ 67185222Ssamstatic struct linux_syscall { 68185222Ssam struct syscall *sc; 69185222Ssam char *name; 70329099Skevans int number; 71329099Skevans unsigned long args[5]; 72329099Skevans int nargs; /* number of arguments -- *not* number of words! */ 73185222Ssam char **s_args; /* the printable arguments */ 74329099Skevans} lsc; 75185222Ssam 76185222Ssamstatic inline void 77185222Ssamclear_lsc() { 78185222Ssam if (lsc.s_args) { 79185222Ssam int i; 80 for (i = 0; i < lsc.nargs; i++) 81 if (lsc.s_args[i]) 82 free(lsc.s_args[i]); 83 free(lsc.s_args); 84 } 85 memset(&lsc, 0, sizeof(lsc)); 86} 87 88void 89i386_linux_syscall_entry(int pid, int nargs) { 90 char buf[32]; 91 struct reg regs = { 0 }; 92 int syscall; 93 int i; 94 int memfd; 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 char *tmp; 153 154#ifdef DEBUG 155 fprintf(stderr, "syscall %s(", lsc.name); 156#endif 157 for (i = 0; i < lsc.nargs ; i++) { 158#ifdef DEBUG 159 fprintf(stderr, "0x%x%s", 160 sc ? 161 lsc.args[sc->args[i].offset] 162 : lsc.args[i], 163 i < (lsc.nargs - 1) ? "," : ""); 164#endif 165 if (sc && !(sc->args[i].type & OUT)) { 166 lsc.s_args[i] = print_arg(Procfd, &sc->args[i], lsc.args); 167 } 168 } 169#ifdef DEBUG 170 fprintf(stderr, ")\n"); 171#endif 172 } 173 174 if (!strcmp(lsc.name, "linux_execve") || !strcmp(lsc.name, "exit")) { 175 print_syscall(outfile, lsc.name, lsc.nargs, lsc.s_args); 176 } 177 178 return; 179} 180 181/* 182 * Linux syscalls return negative errno's, we do positive and map them 183 */ 184int bsd_to_linux_errno[] = { 185 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 186 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 187 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 188 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 189 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 190 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 191 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 192 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 193 -6, 194}; 195 196void 197i386_linux_syscall_exit(int pid, int syscall) { 198 char buf[32]; 199 struct reg regs; 200 int retval; 201 int i; 202 int errorp; 203 struct syscall *sc; 204 205 if (fd == -1 || pid != cpid) { 206 sprintf(buf, "/proc/%d/regs", pid); 207 fd = open(buf, O_RDONLY); 208 if (fd == -1) { 209 fprintf(outfile, "-- CANNOT READ REGISTERS --\n"); 210 return; 211 } 212 cpid = pid; 213 } 214 215 lseek(fd, 0L, 0); 216 if (read(fd, ®s, sizeof(regs)) != sizeof(regs)) 217 return; 218 219 retval = regs.r_eax; 220 errorp = !!(regs.r_eflags & PSL_C); 221 222 sc = lsc.sc; 223 if (!sc) { 224 for (i = 0; i < lsc.nargs; i++) { 225 lsc.s_args[i] = malloc(12); 226 sprintf(lsc.s_args[i], "0x%x", lsc.args[i]); 227 } 228 } else { 229 for (i = 0; i < sc->nargs; i++) { 230 char *temp; 231 if (sc->args[i].type & OUT) { 232 if (errorp) { 233 temp = malloc(12); 234 sprintf(temp, "0x%x", lsc.args[sc->args[i].offset]); 235 } else { 236 temp = print_arg(Procfd, &sc->args[i], lsc.args); 237 } 238 lsc.s_args[i] = temp; 239 } 240 } 241 } 242 print_syscall(outfile, lsc.name, lsc.nargs, lsc.s_args); 243 if (errorp) { 244 for (i = 0; i < sizeof(bsd_to_linux_errno) / sizeof(int); i++) 245 if (retval == bsd_to_linux_errno[i]) 246 break; 247 fprintf(outfile, "errno %d '%s'\n", retval, strerror(i)); 248 } else { 249 fprintf(outfile, "returns %d (0x%x)\n", retval, retval); 250 } 251 clear_lsc(); 252 return; 253} 254