flat assembler
Message board for the users of flat assembler.
![]() Goto page 1, 2 Next |
Author |
|
pelaillo 16 Jul 2004, 04:15
Look at first lines of system.inc on fasm for linux sources.
|
|||
![]() |
|
fasm9 16 Jul 2004, 06:56
i am also wondering, i saw system.inc, now, i want to know how this works>.<, any examples?
|
|||
![]() |
|
chorus 16 Jul 2004, 17:00
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 |
|||
![]() |
|
Freejack 22 Sep 2004, 08:34
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. |
|||
![]() |
|
skingston 18 Mar 2005, 09:34
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? |
|||
![]() |
|
Dryobates 20 Mar 2005, 15:10
skingston wrote: How does mmap work? 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 ![]() |
|||
![]() |
|
skingston 10 Feb 2006, 03:41
Can someone provide me with an example of using mmap for allocating memory?
|
|||
![]() |
|
skingston 10 Feb 2006, 07:03
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) |
|||
![]() |
|
gunblade 13 Feb 2006, 13:32
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.. ![]() 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 |
|||
![]() |
|
Quantum 26 May 2006, 01:12
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. |
|||
![]() |
|
yyc650102 01 Jun 2006, 08:57
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 !! |
|||
![]() |
|
f0dder 01 Jun 2006, 10:59
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.
|
|||
![]() |
|
yyc650102 01 Jun 2006, 16:42
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? |
|||
![]() |
|
f0dder 01 Jun 2006, 17:52
Custom kernel, baby
![]() Have you checked www.memtest86.com? Only 32bit though, afaik. I dunno if 64bit really buys you anything. _________________ carpe noctem |
|||
![]() |
|
yyc650102 05 Jun 2006, 06:38
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. ![]() 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!! ![]() ![]() |
|||
![]() |
|
vid 12 Sep 2006, 06:32
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. |
|||
![]() |
|
f0dder 12 Sep 2006, 07:00
vid wrote: you can look at FASMLIB for another example how to call mmap(), it's file fasmlib/linux/mem.inc in archive. Do it the proper way and allocate larger mmap() "arenas", then do a heap manager ontop of that ![]() _________________ carpe noctem |
|||
![]() |
|
vid 12 Sep 2006, 07:19
no preblem, jsut... what's "problem" with brk(). isn't it the same?
|
|||
![]() |
|
f0dder 12 Sep 2006, 07:22
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.
_________________ carpe noctem |
|||
![]() |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.