Page 1 of 1

Memory management

Posted: Mon Apr 02, 2012 1:07 pm
by Samathy
hey
so, I've finished my Bootloader and am moving on to my kernel the only thing is, i don't understand how to manage my memory.
i wish to load my two (custom file system) file tables right after the bootloader ( both tables are 512 bytes exactly ) and then load my kernel (currently 512 bytes but quickly getting bigger) right after that and then be able to use the memory after the kernel to load programs into ( its a single tasking OS so they'll only ever be one program loaded right after the kernel.)
i have tried reading the tutorials on memory management on the OSdev site but still don't quite understand how to go about implementing this into my OS.

two questions:
1: is how i want to manage my memory a good idea/will it work?
2:do you know any easy to understand memory management tutorials/information that can help me or can you explain in simple terms what i need to know?

additional info-
my OS is ALL in 16bit NASM real mode assembly.

thanks in advance.

Samathy

Re: Memory management

Posted: Mon Apr 02, 2012 1:19 pm
by VolTeK
Samathy wrote:hey
so, I've finished my Bootloader and am moving on to my kernel the only thing is, i don't understand how to manage my memory.
i wish to load my two (custom file system) file tables right after the bootloader ( both tables are 512 bytes exactly ) and then load my kernel (currently 512 bytes but quickly getting bigger) right after that and then be able to use the memory after the kernel to load programs into ( its a single tasking OS so they'll only ever be one program loaded right after the kernel.)
i have tried reading the tutorials on memory management on the OSdev site but still don't quite understand how to go about implementing this into my OS.

two questions:
1: is how i want to manage my memory a good idea/will it work?
2:do you know any easy to understand memory management tutorials/information that can help me or can you explain in simple terms what i need to know?

additional info-
my OS is ALL in 16bit NASM real mode assembly.

thanks in advance.

Samathy
As you know, real mode memory uses the segment:offset model.


My system uses a block table, each block is a byte, 0x0 means unused, 0x1 means used. Each block represents 1 paragraph or block.

Say you store all of the blocks on the table in a buffer
We want 2 blocks.



0x1 0x1 0x0 0x0 0x1 <- the 3rd block is free. so we could take its offset address, 3, subtract it by 1 and add it to the segment address of user memory. Say that is at 0x10000 (0x1000:0000), the free memory is located at 0x1002:0000 (0x1000:0000, and 0x1001:0000 are used).


The problem with this can be speed.



Deallocation can be done by storing the information of the allocation in the paragraph before the actual allocation. For example, say the user asks for 5 blocks

We will allocate 6, Block1|Block2|Block3|Block4|Block5|Block6| The user will get an address to block 2

For better understanding, they get the address 0x1007, where it actually begins at 0x1006. We will store the allocations information at 0x1006:0000 this includes the offset address to the block, and the size of the allocation. The deallocation function can use this information to clean off the part of the block table the allocation uses. However, its Obvious as i have stated in many threads, there are problems with this.

If the user writes too much then what is asked for, that user may corrupt the information block of the allocation in front of it, it could even corrupt its own block.


This is all how i do it tho, haven't had a problem but there is much more to it.

Re: Memory management

Posted: Tue Apr 03, 2012 2:04 pm
by Samathy
hey,
i still dont understand segment:offset in general but i'll have another read of the OSDev tutorials and anything else i can find and i'll hopefully make sense of it eventually. And then when i understand it all a little better i'll have another go at implementing your idea into my OS.

thanks for your help :)

Samathy

Re: Memory management

Posted: Tue Apr 03, 2012 8:46 pm
by bubach
In real mode, each part/number of the segment represents 16 bytes in real RAM, while the offset is byte for byte. So to access byte 0x32 (50 decimal) in RAM there's more than one way. Since it's inside the first 64 kilobyte of RAM, the address fits in the offset only, so you can do:

seg ...... offset
0x0000:0x0032

But you can also do any of these to access the same memory:
seg ...... offset
0x0001:0x0022
0x0002:0x0012
0x0003:0x0002


So to calculate a seg:off address you multiply the segment with 16 and add the offset. Since one RAM location can have several segment:offset values it's confusing at first, but you need to know it. Having code running at RAM address 0x30, means that you can make sure that the code and data segments CS/DS are set to 0x0003 with an "ORG 0" or have the CS/DS values set to 0x0000 with an ORG statement set to "0x30". The org is basically telling the assembler the offset of your code and data, while the CS and DS registers holds the segment.

So, thats about it. Hope I didn't make any serious misstakes here - in that case I'll blame alcohol. And the time being 04:43. :lol:

Re: Memory management

Posted: Wed Apr 04, 2012 2:06 pm
by Samathy
hey
ah, that makes sense...
Thanks for your help people, much appreciated. im much clearer on the topic now :)

Samathy