From 59dcfa0128761394803087761b5756927774b331 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 20 Jan 2005 07:31:25 +0000 Subject: [PATCH] Improve debuggability by eliminating system call stub in separate assembly file. Instead, implement system call stubs in inline assembly. --- src/Makefile.userprog | 1 - src/lib/user/syscall-stub.S | 6 --- src/lib/user/syscall-stub.h | 6 --- src/lib/user/syscall.c | 98 ++++++++++++++++++++++++++++++------- 4 files changed, 79 insertions(+), 32 deletions(-) delete mode 100644 src/lib/user/syscall-stub.S delete mode 100644 src/lib/user/syscall-stub.h diff --git a/src/Makefile.userprog b/src/Makefile.userprog index 2e95f9b..c5e6f3d 100644 --- a/src/Makefile.userprog +++ b/src/Makefile.userprog @@ -20,7 +20,6 @@ LIB_SRC += lib/stdio.c # I/O library. LIB_SRC += lib/stdlib.c # atoi() LIB_SRC += lib/string.c # String functions. LIB_SRC += lib/user/syscall.c # System calls. -LIB_SRC += lib/user/syscall-stub.S # System call stub. LIB_SRC += lib/user/console.c # Console code. LIB_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(LIB_SRC))) diff --git a/src/lib/user/syscall-stub.S b/src/lib/user/syscall-stub.S deleted file mode 100644 index 73afc8d..0000000 --- a/src/lib/user/syscall-stub.S +++ /dev/null @@ -1,6 +0,0 @@ - .intel_syntax noprefix - .globl syscall -syscall: - pop ecx - int 0x30 - jmp ecx diff --git a/src/lib/user/syscall-stub.h b/src/lib/user/syscall-stub.h deleted file mode 100644 index 4f6036f..0000000 --- a/src/lib/user/syscall-stub.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __LIB_USER_SYSCALL_STUB_H -#define __LIB_USER_SYSCALL_STUB_H - -int syscall (int nr, ...); - -#endif /* lib/user/syscall-stub.h */ diff --git a/src/lib/user/syscall.c b/src/lib/user/syscall.c index 89e0185..c92c812 100644 --- a/src/lib/user/syscall.c +++ b/src/lib/user/syscall.c @@ -2,113 +2,173 @@ #include "syscall-stub.h" #include "../syscall-nr.h" +/* Invokes syscall NUMBER, passing no arguments, and returns the + return value as an `int'. */ +#define syscall0(NUMBER) \ + ({ \ + int retval; \ + asm volatile \ + ("push %[number]; int 0x30; add %%esp, 4" \ + : "=a" (retval) \ + : [number] "i" (NUMBER) \ + : "memory"); \ + retval; \ + }) + +/* Invokes syscall NUMBER, passing argument ARG0, and returns the + return value as an `int'. */ +#define syscall1(NUMBER, ARG0) \ + ({ \ + int retval; \ + asm volatile \ + ("push %[arg0]; push %[number]; int 0x30; add %%esp, 8" \ + : "=a" (retval) \ + : [number] "i" (NUMBER), \ + [arg0] "g" (ARG0) \ + : "memory"); \ + retval; \ + }) + +/* Invokes syscall NUMBER, passing arguments ARG0 and ARG1, and + returns the return value as an `int'. */ +#define syscall2(NUMBER, ARG0, ARG1) \ + ({ \ + int retval; \ + asm volatile \ + ("push %[arg1]; push %[arg0]; " \ + "push %[number]; int 0x30; add %%esp, 12" \ + : "=a" (retval) \ + : [number] "i" (NUMBER), \ + [arg0] "g" (ARG0), \ + [arg1] "g" (ARG1) \ + : "memory"); \ + retval; \ + }) + +/* Invokes syscall NUMBER, passing arguments ARG0, ARG1, and + ARG2, and returns the return value as an `int'. */ +#define syscall3(NUMBER, ARG0, ARG1, ARG2) \ + ({ \ + int retval; \ + asm volatile \ + ("push %[arg2]; push %[arg1]; push %[arg0]; " \ + "push %[number]; int 0x30; add %%esp, 16" \ + : "=a" (retval) \ + : [number] "i" (NUMBER), \ + [arg0] "g" (ARG0), \ + [arg1] "g" (ARG1), \ + [arg2] "g" (ARG2) \ + : "memory"); \ + retval; \ + }) + void -halt (void) +halt (void) { - syscall (SYS_halt); + syscall0 (SYS_halt); NOT_REACHED (); } void exit (int status) { - syscall (SYS_exit, status); + syscall1 (SYS_exit, status); NOT_REACHED (); } pid_t exec (const char *file) { - return syscall (SYS_exec, file); + return (pid_t) syscall1 (SYS_exec, file); } int join (pid_t pid) { - return syscall (SYS_join, pid); + return syscall1 (SYS_join, pid); } bool create (const char *file, unsigned initial_size) { - return syscall (SYS_create, file, initial_size); + return syscall2 (SYS_create, file, initial_size); } bool remove (const char *file) { - return syscall (SYS_remove, file); + return syscall1 (SYS_remove, file); } int open (const char *file) { - return syscall (SYS_open, file); + return syscall1 (SYS_open, file); } int filesize (int fd) { - return syscall (SYS_filesize, fd); + return syscall1 (SYS_filesize, fd); } int read (int fd, void *buffer, unsigned size) { - return syscall (SYS_read, fd, buffer, size); + return syscall3 (SYS_read, fd, buffer, size); } int write (int fd, const void *buffer, unsigned size) { - return syscall (SYS_write, fd, buffer, size); + return syscall3 (SYS_write, fd, buffer, size); } void seek (int fd, unsigned position) { - syscall (SYS_seek, fd, position); + syscall2 (SYS_seek, fd, position); } unsigned tell (int fd) { - return syscall (SYS_tell, fd); + return syscall1 (SYS_tell, fd); } void close (int fd) { - syscall (SYS_close, fd); + syscall1 (SYS_close, fd); } bool mmap (int fd, void *addr, unsigned length) { - return syscall (SYS_mmap, fd, addr, length); + return syscall3 (SYS_mmap, fd, addr, length); } bool munmap (void *addr, unsigned length) { - return syscall (SYS_munmap, addr, length); + return syscall2 (SYS_munmap, addr, length); } bool chdir (const char *dir) { - return syscall (SYS_chdir, dir); + return syscall1 (SYS_chdir, dir); } bool mkdir (const char *dir) { - return syscall (SYS_mkdir, dir); + return syscall1 (SYS_mkdir, dir); } void lsdir (void) { - syscall (SYS_lsdir); + syscall0 (SYS_lsdir); } -- 2.30.2