Page 1 of 2

Help with tutorial? [Paging]

Posted: Sat Aug 15, 2009 6:02 pm
by GeniusCobyWalker
I have followed this tutorial: http://www.jamesmolloy.co.uk/tutorial_h ... aging.html
Step-by-step.

I wrote code to fit my OS.

But, I just don't get it. I would google it, but I don't know what to google.

Can someone help?

Here is the problem:
All the commands work it's just...
How do I save data to memory?
Malloc allocates a page, how do I read and write to a frame?

I have been working on this about a week. Any help would be appreciated.
(I don't get why I can't understand this)

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 7:22 am
by ehenkes
How do I save data to memory?
"Save" into RAM? Better "save" to a permanent data storage system. :D

Perhaps this chart helps you a little bit with understanding paging on a whole:
http://www.henkessoft.de/OS_Dev/Bilder/ ... Bitset.PNG

(k)malloc should be reserved for the heap.

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 7:31 am
by mnovotny
Paging is completely transparent for application programs. You access memory as ordinary (via mov instruction) and paging mechanism will translate address for you.
Read Intel/AMD manuals. :)

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 1:42 pm
by GeniusCobyWalker
So can I save variables to frames? or what are they for?

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 2:27 pm
by neon
GeniusCobyWalker wrote:So can I save variables to frames? or what are they for?
A frame is just a block of physical memory, nothing special to it. It can be used to store anything that can be in physical memory: a loaded file or part of one, data areas, dynamic memory allocations, et al.

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 3:24 pm
by GeniusCobyWalker
OK, by looking at that tutorial,

how would I:
save int A to MEM
and load int B from that spot in MEM

any ideas?

I have no Idea why i'm not getting this :?

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 3:59 pm
by pcmattman
Paging splits the address space into blocks of pages (which are normally 4096 bytes on x86). These blocks are used by different parts of your OS.

So you have your physical memory manager, which allocates physical pages from real RAM. Then your virtual memory manager, which maps virtual addresses to these pages.

On top of that, you build things like your heap (malloc/free), which splits pages into small allocation units. You would also build things like executable loaders on top of this - mapping loaded pages to the addresses in the program headers of an ELF, for instance.

EDIT: Once you have your heap built upon a VMM (which is built upon your PMM)...
save int A to MEM
and load int B from that spot in MEM
Is just...

Code: Select all

int a = 1234;

......

int *p = (int*) malloc(sizeof(int));
*p = a;
int b = *p;

Re: Help with tutorial?

Posted: Sun Aug 16, 2009 4:29 pm
by neon
GeniusCobyWalker wrote:I have no Idea why i'm not getting this :?
...Its just basic dynamic memory allocation...

To save int A to memory:

Code: Select all

int A;
char* p = malloc (sizeof int);
*p = A;
load B from that same location in memory:

Code: Select all

int B = *p;

Re: Help with tutorial?

Posted: Mon Aug 17, 2009 1:46 am
by Solar
Maybe Memory management from the Wiki can help?

Re: Help with tutorial?

Posted: Sat Aug 29, 2009 6:43 pm
by GeniusCobyWalker
I hate to bring up old threads but,
The ban was horrible.

Anyway,
You all have made me realize my skills so I am now taking a C Programming class at College.
Luna's progress will continue as soon as pointers are covered in class.

Thanks ;)

Re: Help with tutorial?

Posted: Sun Aug 30, 2009 9:37 pm
by kop99
Linux's memory management is so good.
You can understand and reference it. It's good experience.

Re: Help with tutorial?

Posted: Sun Aug 30, 2009 10:57 pm
by ~
kop99 wrote:Linux's memory management is so good.
You can understand and reference it. It's good experience.
What do you mean? Is there some book called "Linux's memory management", or are you trying to say that the memory management from Linux sources is readily comprehensible for anyone who knows a bit of C alone?

=============================
GeniusCobyWalker wrote:Luna's progress will continue as soon as pointers are covered in class.
There are several intricacies about pointers, but if you already know basic C maybe you could study again the different parts of the language, as there are little things that one could not know yet.

I had to study a copy of "C by Discovery" by L. S. Foster, and studied like 75% of if from the first chapter and up (the whole chapters). It took me like one month and 1 week, but I greatly improved my understanding of C. I mostly was also in need to understand the pointer usage and syntax. These are the chapters it contains:

Chapter 1: Getting Started

Chapter 2: Gaining Control
Regular things like if, for, etc.

Chapter 3: Basic data Types

Chapter 4: Pointers and Arrays
Here is the main theory about pointers. This is what an assembly programmer would like to study more from this book carefuly to convert something like mov byte[var],1 to var=1, and *pointer=1 I think that should be the same as mov edi,[pointer] ;;; mov byte[edi],1.

Chapter 5: Strings

Chapter 6: Keeping Control
More things like do-while, switch, goto, etc.

Chapter 7: Structuring the Data
Structures, arrays, pointers, bit fields, enum, unions, etc.

Chapter 8: Intraprogram Communication
Didn't study it very much.

Chapter 9: Multidimensional Arrays and Double Indirection
This talks about arrays and compares them with pointers, and also talks about double indirection and arrays of pointers

Chapter 10: Input and Output
I almost didn't study it but the name says it.

Chapter 11: C Library and Preprocessor Facilities
Explains some standard functions from the standard C library as well as how to use the preprocessor.

PART II: The Programmer's Handbook
This is a reference of things like identifiers, operator precedence, etc.

======================================================

If I remember correctly, one of the curious things that was explained is that, if a normal variable like an integer "i" is referenced with "&i" it will be referencing its address, NOT its value, and then if it's referenced as "*&i" would be accessing the value of the variable as if it had plainly been using "i", because the "&i" part would return the address of the variable and the * part would point to whatever is at that memory address,

It also talks about pointer arithmetic and the implications of the different combinations of operators in the syntax precedence.

Things like the differences of:

*pointer
*(pointer+5)
*pointer+5


Other things are the fact that doing something like int *intarray is better than int intarray[100] because *intarray only is a pointer (maybe 32 bits) but intarray[100] has to allocate 100 integer elements from the beginning.

The fact that pointing with a pointer of some type to an arbitrary memory location causes that location to be interpreted as a data of that type.

The fact that the "strings" in printf are like put apart like in assembler and then referenced with a pointer to an array of null-terminated chars, or something like that. That whenever something is put as in 'A' that character is considered a character in C, and if it is "A" it is considered a null-terminated string in C.

The fact that myvar[51] could be accessed as *(myvar+51).

One of the good things of C pointers, contrary to assembly "pointers" is that C pointers do the math. For example, if an integer is 4 bytes and we access *(myint+5) in assembly we won't be accessing [myint+5] but [myint+(5*4)], where 4 is the size of the data type that is referenced by the pointer of that type.

I think that other strange think is the difference between something like *a+=1 and (*a)+=1.

And a lot of other small details.

Some sample source code can be found here:

http://web.archive.org/web/200107110343 ... Discovery/

Re: Help with tutorial?

Posted: Mon Aug 31, 2009 9:16 pm
by kop99
What do you mean? Is there some book called "Linux's memory management", or are you trying to say that the memory management from Linux sources is readily comprehensible for anyone who knows a bit of C alone?
There are many book for LMM...
"Understanding the Linux Virtual Memory Manager" and "Understanding the Linux Kernel 3rd Edition"... :lol:

:shock: Help with tutorial?

Posted: Fri Sep 25, 2009 6:26 pm
by GeniusCobyWalker
Once again, I hate bringing up old threads.

I did the MM by that Tutorial Line-by-line and it's still not working
It says "Page Fault" right after being Initialized. I can't even print "Welcome to Luna"

Has anyone else used that Tutorial and had success?

Re: Help with tutorial?

Posted: Fri Sep 25, 2009 7:01 pm
by neon
I have not used his tutorial but I do know it works. Understanding paging is easy, getting it working effectively is hard - I hope you mastered pointers before attempting it.

If it page faults as soon as enabling paging, it means that your running code is not identity mapped before enabling paging. I cant help much after without looking at code. (I assume and hope its not copied+pasted.)