Help with tutorial? [Paging]

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.
User avatar
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

Help with tutorial? [Paging]

Post 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)
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: Help with tutorial?

Post 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.
User avatar
mnovotny
Member
Member
Posts: 27
Joined: Mon Aug 10, 2009 2:54 am
Location: Slovakia

Re: Help with tutorial?

Post 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. :)
Keep the world with all its sin
It's not fit for livin' in
User avatar
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

Re: Help with tutorial?

Post by GeniusCobyWalker »

So can I save variables to frames? or what are they for?
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Help with tutorial?

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

Re: Help with tutorial?

Post 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 :?
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Help with tutorial?

Post 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;
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Help with tutorial?

Post 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;
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Help with tutorial?

Post by Solar »

Maybe Memory management from the Wiki can help?
Every good solution is obvious once you've found it.
User avatar
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

Re: Help with tutorial?

Post 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 ;)
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Help with tutorial?

Post by kop99 »

Linux's memory management is so good.
You can understand and reference it. It's good experience.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Help with tutorial?

Post 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/
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Help with tutorial?

Post 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:
User avatar
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

:shock: Help with tutorial?

Post 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?
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Help with tutorial?

Post 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.)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply