Memory-system structure.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
phix

Memory-system structure.

Post by phix »

Hi!

I'm on my way writing my first kernel. And I have come to the memory part. Paging etc.

I have read all articles on memory and paging on osdever.net.
And I thing I get the point. I have actuly enabled pageing, as described on osdever.net. (the very basics)

But I'm having a hard time figuring out the way to struct all the memory stuff, and what is needed for a *minimal system.

So I wounder... what should my paging.c contain. What functions is nesessary?

alloc_pages(int num_aligned_pages)
free_pages(int num_aligned_pages)

And after that...? PhysicalMM.. VirtualMM?? What do I realy need to get to the malloc() and free().
JAAman

Re:Memory-system structure.

Post by JAAman »

at minimum, you will need a physical memory allocator/deallocator, and a virtual memory allocator/deallocator


however, you must have a way to allocate kernel space as well as user space memory (which are usually in distinct areas of the address space, and certainly must have different permissions) -- unless of course, you run apps in ring0 (not unheard of -- though i dont recommend it)



(of course, if you only ident map, and dont support multi-tasking, and dont support virtual memory swapping, then you could combine the physical and virtual memory allocation/deallocation)


ps.

these functions of course only deal in whole pages (or multiples of pages)
though you prob already know this, some people assume that malloc/free are handled in the kernel/syscalls, so i thought i should make sure
LongHorn

Re:Memory-system structure.

Post by LongHorn »

First of all you have to concentrate on physical memory manager. Decide whether simple memory allocator such as bitmap,stack is sufficient or need a sophisticated way of allocating memory. Are you going to keep track information about the pages. I am gonna implement buddy allocator.
I gone through linux source code. So my implementation might have that likeness which i don't want to have. So any suggestions.
phix

Re:Memory-system structure.

Post by phix »

hmm... oki.. I look in to physicalMM then...

But what are the steps if a user app is asking for memory, by calling malloc(). (if you want a multi-task system)

*: A new page-directory is created when app is started.
1: Kernel is checking for free pages?
2: .....?
3: .....?
4: malloc() return pointer (virtual addres).

Every time I read about thouse things I get all mixed up. :)
paulbarker

Re:Memory-system structure.

Post by paulbarker »

The kernel is generally not involved in user-level malloc. If the user requires more memory, a system call such as sbrk() is called, which on the kernel side is part of the virtual memory manager. The kernel also needs a malloc-like function, usually called kalloc().

It can take a while to get you're head round. Its one of the 3 main areas of an OS (memory manager, scheduler/threading and drivers), so it is definately worth spending some time over. Do some research and take a look at a few sources.

And to answer you're opening question, the bare minimum is no physical memory manager or virtual memory manager, free() as a no-op and malloc simple incrementing a stored pointer and returning it each time :).

For a really advanced system, find a book about Windows 2000/XP internals and for something also very advanced but not quite as much of a mess (Windows MM seems more of a bad implementation than a bad design according to what I've read), then look at Linux and the BSDs.
phix

Re:Memory-system structure.

Post by phix »

Ok.. thanks...

I have found some nice sourcecode for linux v 0.01..
I guess that is a good start..

Do you have any more tips on good books.
More general OS structure/programming and aiming for C programmers...

I'm just 21 years and have no higher level of education. So I have learned moast things by analyzing others source code... So I think I might lack some of the basics in low level programming... :)
Post Reply