From owner-freebsd-hackers@FreeBSD.ORG Mon Aug 4 12:14:47 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id F1F1437B404 for ; Mon, 4 Aug 2003 12:14:46 -0700 (PDT) Received: from mail.speakeasy.net (mail7.speakeasy.net [216.254.0.207]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4FE7843FA3 for ; Mon, 4 Aug 2003 12:14:46 -0700 (PDT) (envelope-from jhb@FreeBSD.org) Received: (qmail 7943 invoked from network); 4 Aug 2003 19:14:45 -0000 Received: from unknown (HELO server.baldwin.cx) ([216.27.160.63]) (envelope-sender )encrypted SMTP for ; 4 Aug 2003 19:14:45 -0000 Received: from laptop.baldwin.cx (gw1.twc.weather.com [216.133.140.1]) by server.baldwin.cx (8.12.9/8.12.9) with ESMTP id h74JEh9s018156; Mon, 4 Aug 2003 15:14:44 -0400 (EDT) (envelope-from jhb@FreeBSD.org) Message-ID: X-Mailer: XFMail 1.5.4 on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: <20030731201227.28952.qmail@neuroflux.com> Date: Mon, 04 Aug 2003 15:15:04 -0400 (EDT) From: John Baldwin To: Ryan Sommers cc: freebsd-hackers@freebsd.org Subject: RE: Assembly Syscall Question X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Aug 2003 19:14:47 -0000 On 31-Jul-2003 Ryan Sommers wrote: > When making a system call to the kernel why is it necessary to push the > syscall value onto the stack when you don't call another function? > > Example: > > access.the.bsd.kernel: > int 80h > ret > > func: > mov eax, 4 ; Write > call access.the.bsd.kernel > ; End > > Works. However: > func: > mov eax, 4 ; Write > int 80h > ; End > > Doesn't. > > Now, if you change it to: > > func: > mov eax, 4 ; Write > push eax > int 80h > ; End > > It does work. I was able to find, "By default, the FreeBSD kernel uses the C > calling convention. Further, although the kernel is accessed using int 80h, > it is assumed the program will call a function that issues int 80h, rather > than issuing int 80h directly," in the developer's handbook. But I can't > figure out why the second example doesn't work. Is the call instruction > pushing the value onto the stack in addition to pushing the instruction > pointer on? > > Thank you in advance. > PS I'm not on the list. First off, why are you using asm for userland stuff? Secondly, the kernel assumes that all the other arguments besides the syscall to execute (i.e. %eax) are passed on the user stack. Thus, it has to have a set location relative to the user stack pointer to find the arguments. It allows for a return IP from a call instruction to be at the top of the stack. You can tell this by looking at syscall() in sys/i386/i386/trap.c: params = (caddr_t)frame.tf_esp + sizeof(int); code = frame.tf_eax; orig_tf_eflags = frame.tf_eflags; params is a userland pointer to the function arguments. Adding the sizeof(int) skips over the saved return address, or in your 3rd case, the dummy %eax value. -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/