From owner-freebsd-hackers@FreeBSD.ORG Wed Jan 10 14:39:26 2007 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 1F0BF16A40F for ; Wed, 10 Jan 2007 14:39:26 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (66-23-211-162.clients.speedfactory.net [66.23.211.162]) by mx1.freebsd.org (Postfix) with ESMTP id 9FC7C13C442 for ; Wed, 10 Jan 2007 14:39:25 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from zion.baldwin.cx (zion.baldwin.cx [192.168.0.7]) (authenticated bits=0) by server.baldwin.cx (8.13.6/8.13.6) with ESMTP id l0AEdGQ2085036; Wed, 10 Jan 2007 09:39:17 -0500 (EST) (envelope-from jhb@freebsd.org) From: John Baldwin To: freebsd-hackers@freebsd.org Date: Wed, 10 Jan 2007 08:55:48 -0500 User-Agent: KMail/1.9.4 References: <20070110012250.HM.0000000000000G0@chewy509.bos-mail-wwl8.lycos.com> In-Reply-To: <20070110012250.HM.0000000000000G0@chewy509.bos-mail-wwl8.lycos.com> MIME-Version: 1.0 Content-Disposition: inline Message-Id: <200701100855.49186.jhb@freebsd.org> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [192.168.0.1]); Wed, 10 Jan 2007 09:39:17 -0500 (EST) X-Virus-Scanned: ClamAV 0.88.3/2432/Wed Jan 10 08:12:31 2007 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=4.2 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx Cc: darran kartaschew Subject: Re: sbrk vs mmap X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jan 2007 14:39:26 -0000 On Wednesday 10 January 2007 01:22, darran kartaschew wrote: > > Hi Guys, > I'm having some issues with rewriting a simple malloc() function to be > with FreeBSD (AMD64). This is part of porting an application from > Linux > to FreeBSD. > After pulling my hair out for a while, I've found that the sbrk() > system call just returns "45 - Operation Not Supported" error, > irrespective of the parameters passed to it. (I've found the source > for sbrk() and see that it's not implemented). > So I decided to try using mmap() instead. All memory allocations don't > have to be continuous, so mmap() will suffice. The problem is I'm > getting an invalid file handle error? According to the man page, if > you > use MAP_ANON you're just allocating a block of memory without linking > to a file, and a handle of -1 should be supplied... Any way code is as > follows: > memInit: > mov r4, 0 ; don't care where the memory is allocated > mov r5, 1048576 ; alloc 1MB > mov r3, 3 ; RW access to memory > mov r2, 4096 ; MAP_ANON - not a file > mov r8d, -1 ; -1 for file handle if using MAP_ANON > mov r9, 0 ; ignored for MAP_ANON > mov r0, 197 ; mmap(); > syscall > mov qword [_mmap], r0 ; save address so we can release it on exit; > ret > It fails with an EBADF (9) ; Bad File Descriptor error... > Note: r0 = rax, r1 = rbx, r2 = rcx, r3 = rdx, r4 = rdi, r5 = rsi, r6 = > rbp, r7 = rsp. Various parameters for mmap() are found in mman.h>. > So does anyone have an example of a working call to mmap() or tell me > what's wrong with the above code? > I've done up a test C program that simple calls mmap(), after > tracing through the compiled C program using gdb I can't see that > I'm doing anything different to what gcc/glibc are doing? (except > the macro expansion that's in libc which adds an additional > 0 to the top of the stack). > PS. FASM 1.66 running on FreeBSD 6.1 (AMD64). > PPS. This is NOT a homework assignment! (tm) :P Is there a particular reason you have to use assembly and not C? You can call C functions from assembly and vice versa. You also forgot to include one of MAP_SHARED or MAP_PRIVATE in your flags. -- John Baldwin