From owner-freebsd-hackers@FreeBSD.ORG Fri Jan 12 06:47:46 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 A340B16A403 for ; Fri, 12 Jan 2007 06:47:46 +0000 (UTC) (envelope-from ssouhlal@FreeBSD.org) Received: from elvis.mu.org (elvis.mu.org [192.203.228.196]) by mx1.freebsd.org (Postfix) with ESMTP id 937F913C45A for ; Fri, 12 Jan 2007 06:47:46 +0000 (UTC) (envelope-from ssouhlal@FreeBSD.org) Received: from [192.168.0.100] (c-67-188-127-3.hsd1.ca.comcast.net [67.188.127.3]) by elvis.mu.org (Postfix) with ESMTP id 69CD91A3C1A; Thu, 11 Jan 2007 22:47:46 -0800 (PST) Message-ID: <45A72ED8.8070402@FreeBSD.org> Date: Thu, 11 Jan 2007 22:46:48 -0800 From: Suleiman Souhlal User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051204) X-Accept-Language: en-us, en MIME-Version: 1.0 To: darran kartaschew References: <20070110012250.HM.0000000000000G0@chewy509.bos-mail-wwl8.lycos.com> In-Reply-To: <20070110012250.HM.0000000000000G0@chewy509.bos-mail-wwl8.lycos.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Cc: freebsd-hackers@freebsd.org 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: Fri, 12 Jan 2007 06:47:46 -0000 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 I think you are missing a parameter. mmap (as well as pwrite, lseek, truncate and ftruncate), has a "hidden" parameter just before the offset that is ignored, due to a bug in ancient GCC versions. So, basically, you should also push a 0 on the stack. Take a look at src/sys/libc/sys/mmap.c . I have a patch to remove this useless argument, but haven't committed it yet. -- Suleiman