flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > How do I alloc memory?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
chorus



Joined: 16 Mar 2004
Posts: 23
Hello,
a simple question for everybody. I'm just starting with Linux programming, and want to know a very simple thing: How do you alloc memory in Linux using syscalls (int 80h). I went through the syscalls and didn't see anything obvious like getmem or something like that. I see functions like mmap and brk but these don't seem quite right...

For example, how would I allocate 1MB of memory, and then free it?

Any help is much appreciated. Thanks

--Chorus
Post 15 Jul 2004, 17:49
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 863
Location: Colombia
Look at first lines of system.inc on fasm for linux sources.
Post 16 Jul 2004, 04:15
View user's profile Send private message Yahoo Messenger Reply with quote
fasm9



Joined: 19 Jun 2003
Posts: 439
i am also wondering, i saw system.inc, now, i want to know how this works>.<, any examples?
Post 16 Jul 2004, 06:56
View user's profile Send private message Reply with quote
chorus



Joined: 16 Mar 2004
Posts: 23
Thank you for the pointer pelaillo. I took a look at the source code. This is my understanding of things but it's not very clear... Hopefully someone can tell me if I've got this right or not.

Code:
init_memory:
        xor     ebx,ebx
     mov     eax,45
      int     0x80
        mov     [additional_memory],eax
    


Find out where the data segment starts (?) by calling brk with 0 as the parameter. Not sure how this works, because the documentation I have says brk will return 0 upon a successful call and error otherwise. Furthermore it doesn't mention anything about calling brk with NULL pointer... (I'm getting this from "man 2 brk" at the command prompt)

Code:
  
    mov     ebx,buffer
  mov     eax,116
     int     0x80
    


Find out some system information by calling sysinfo. Namely freeram [buffer+14h] and freeswap [buffer+24h]

Code:
    allocate_memory:
       mov     ebx,[additional_memory]
     mov     eax,dword [buffer+14h]
      add     eax,dword [buffer+24h]
      mov     edx,[memory_setting]
        shl     edx,10
      jz      memory_size_ok
      cmp     eax,edx
     jbe     memory_size_ok
      mov     eax,edx
    memory_size_ok:
  add     ebx,eax
     mov     eax,45
      int     0x80
        mov     [memory_end],eax
    


From what I can tell, this grows the data segment of the application. If memory_setting is zero, we'll end up allocating all the memory in the system (freeram + freeswap) !??! If memory_setting is not zero, then we limit the amount of memory to however much memory_setting is in kilobytes.

Code:
       sub     eax,[additional_memory]
     jz      not_enough_memory
   shr     eax,3
       imul    eax,3
       add     eax,[additional_memory]
     mov     [additional_memory_end],eax
 mov     [memory_start],eax
  ret
    

This I'm not sure I understand at all. Apparently, the first 3/8 of whatever amount we allocated is designated to the range [additional_memory,additional_memory_end] and the last 5/8 to [memory_start,memory_end].
Don't know what that's about, maybe it's specific to the actual FASM executable.

So if I got things straight it works like this:

1) Call brk with NULL ptr to get a pointer to the current end of data segment
2) Add however much I want to add (i.e., the number of bytes to commit)
3) Call brk with the new end of data segment.
4) The pointer to the new memory is the old end of data segment and the size is given

Can anyone tell me if this is that more or less right?

Thanks,
--Chorus
Post 16 Jul 2004, 17:00
View user's profile Send private message Reply with quote
Freejack



Joined: 12 Sep 2003
Posts: 3
Location: Some Asylum...somewhere.
Well...calling brk is one way to do it. I'm kind of a lazy bastard. I like to call mmap, get the pointer, and go hog wild with the memory pool.

mapargs: times 6 rd %
mappedptr: rd %

mov esp, [mapargs]
mov [esp + 4], size
....blah blah blah

mov eax, 90
int 0x80

mov [mappedptr], eax


Yeah, this is crap code, but it's the general idea.
Post 22 Sep 2004, 08:34
View user's profile Send private message Reply with quote
skingston



Joined: 11 Dec 2004
Posts: 19
How does mmap work?
Apparently it is meant to "map a file or device into memory". How can you use it to just allocate memory?

As you can probably guess, I'm fairly new to linux & I need some help. I tried writing my own malloc-type function in fasm using brk to resize the data segment and using a linked list to keep track of each block of memory that is "allocated" so that they can each be freeded and re-used by my program, in any order. The problem is I got a segmentation fault. I probably made a mistake somewhere. Now I'm looking for a simplier way... does anyone know how the c malloc function works?
Post 18 Mar 2005, 09:34
View user's profile Send private message Reply with quote
Dryobates



Joined: 13 Jul 2003
Posts: 46
Location: Poland
skingston wrote:
How does mmap work?
Apparently it is meant to "map a file or device into memory". How can you use it to just allocate memory?


Normaly mmap alocates memory for file and then put there file. When you use mmap with anon mapping, then you allocate memory, but no file is moved there.

I'm also prefer mmap over brk Smile

_________________
There's one more bug... Smile
Image
Post 20 Mar 2005, 15:10
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
skingston



Joined: 11 Dec 2004
Posts: 19
Can someone provide me with an example of using mmap for allocating memory?
Post 10 Feb 2006, 03:41
View user's profile Send private message Reply with quote
skingston



Joined: 11 Dec 2004
Posts: 19
This is what I tried:
mov [mmap_params.start],0
mov [mmap_params.length],4000 ; is this in bytes?
mov [mmap_params.prot],PROT_READ+PROT_WRITE
mov [mmap_params.flags],MAP_ANONYMOUS;+MAP_FIXED
mov [mmap_params.fd],0
mov [mmap_params.offset],0
mov eax,90
mov ebx,mmap_params
int 0x80
cmp eax,0-LAST_ERROR_NUM
jae mem_error
mov [buffer],eax

I got the return value of -22 (Invalid argument)
Post 10 Feb 2006, 07:03
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
This is the basic code I use in one of my programs
zerofd is the file handle of /dev/zero (which I opened earlier)
eax contains the size (amount i want to allocate).

Code:
pushd 0 [zerofd] 2 3 eax 0
mov eax, 90
mov ebx, esp
int 0x80
add esp, 24    


I know i should use constants instead of just the numbers.. but you know.. Rolling Eyes

About your code, i dont know what that mmap_structure looks like, but if its layed out the same way that you apply variables to it there, then it just looks like your FD and size are mixed up.

Hope that helps
Post 13 Feb 2006, 13:32
View user's profile Send private message Reply with quote
Quantum



Joined: 24 Jun 2005
Posts: 122
http://www.lxhp.in-berlin.de/lhpsysc0a.html

brk appears to be " common to the LINUX (2.2/4), FREEBSD, NETBSD, OPENBSD, BEOS and ATHEOS operating systems".

mmap isn't mark "P" in that list.

So, I guess brk is more portable than mmap.
Post 26 May 2006, 01:12
View user's profile Send private message Reply with quote
yyc650102



Joined: 01 Jun 2006
Posts: 11
I use brk to allocate all free memory under single user mode (pure 64bit environment, Floppy Linux) and try to access all memory that I requested.

ex.
loop:
mov rsi, [Free_ds_seg_Start];
mov rax, qword [ds:rsi];
add rsi, 08h;
cmp rsi, [Free_ds_seg_End];
jb loop;

However, I got the feedback of "Out of Memory" from Linux Kernel when rsi approch the last around 5% of free memory.

Can anybody tell me what's going on ?

Thanks a lot !!
Post 01 Jun 2006, 08:57
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3172
Location: Denmark
yyc650102: AFAIK linux tends to more or less blindly grant most memory requests, and let the VMM handle out-of-memory situations, instead of failing memory requests at alloc time.
Post 01 Jun 2006, 10:59
View user's profile Send private message Visit poster's website Reply with quote
yyc650102



Joined: 01 Jun 2006
Posts: 11
f0dder:

Thank you for you explanation. So, the informations I get from brk and sysinfo is not reliable.

Because I want to develop memory test program under pure 64 bit Linux envieonment (it means that I can develop very complex test patterns with plenty of 64bit GPRs), I have to get all available free memory and exact range. (I disabled the SWAP in kernel.)

Therefore, how can I get free memory with exact range?
Post 01 Jun 2006, 16:42
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3172
Location: Denmark
Custom kernel, baby Smile

Have you checked www.memtest86.com? Only 32bit though, afaik. I dunno if 64bit really buys you anything.

_________________
Image - carpe noctem
Post 01 Jun 2006, 17:52
View user's profile Send private message Visit poster's website Reply with quote
yyc650102



Joined: 01 Jun 2006
Posts: 11
f0dder:
Thanks for your hint! In fact, I read memtest86's code already, but I only focus on reading its patterns before. I will try to read the part that I want to understand.

In real 64bit long mode. I can use all 16 64bits General Purpose Registers, 8 64bits MMX registers and 16 XMM Registers. All segment registers are referred to a ZERO selector. It means that the addresses model in 64 bit mode is flat, I can caculate the real fail address without special procedures.(for DRAM debug purpose) That's really attractive to me.

For a long time, it's quite easy for me to use Pass32 to write test programs (Pass32 provide an easy-to-use DOS-Extender which is called PRO32). However, it supports only up to 4GB of memory. It's not enough for me to test memory on some special systems.(ex. Server, Workstation...) Generally, most of those system support more than 16GB of memory. So, I have no choice but to use TASM again to enter PMODE by myself. Then, enable damn Page-Mode and handle interrupts to access more than 4GB of memory. For me, it's hell. Evil or Very Mad Anyway, I did it. Unfortunately, one single DIMM module with more than 4GB of capacity is available right now.(Many systems have more than 16 DIMM slots) In Page Mode, the momory is not flat and it dosen't suppory more than 64GB of memory. In addition, if I want to access all memory under page mode, I have to switch pages angin , again and again......

With Linux, I don't need to pay much attention on dealing miscellaneous problems with hardware. With FASM, I can use all available instructions without entering OP codes by myself.

So, 64bit long mode is my savior!! Very Happy Linux + FASM is my best choice!! Laughing
Post 05 Jun 2006, 06:38
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
you can look at FASMLIB for another example how to call mmap(), it's file fasmlib/linux/mem.inc in archive.

For future i plan to implemnt my own heap using brk() (like libc), because mmap() has minimal alloc size of 4096 on x86 - so 100 dynamic string take over 400KB for example.
Post 12 Sep 2006, 06:32
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3172
Location: Denmark
vid wrote:
you can look at FASMLIB for another example how to call mmap(), it's file fasmlib/linux/mem.inc in archive.

For future i plan to implemnt my own heap using brk() (like libc), because mmap() has minimal alloc size of 4096 on x86 - so 100 dynamic string take over 400KB for example.


Do it the proper way and allocate larger mmap() "arenas", then do a heap manager ontop of that Smile

_________________
Image - carpe noctem
Post 12 Sep 2006, 07:00
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
no preblem, jsut... what's "problem" with brk(). isn't it the same?
Post 12 Sep 2006, 07:19
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3172
Location: Denmark
brk() simply "expands your program memory" - so it's a bit hard to de-allocated brk memory, you can only ever free the last allocated block. With mmap(), at least you can free arbitrary blocks - so even though you really need to write a heap manager on top of both of these low-level allocators, imho mmap() is best.

_________________
Image - carpe noctem
Post 12 Sep 2006, 07:22
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.