Some questions about PM
Some questions about PM
Hello everyone!
I have tried many tutorials anon pm and mnow I have some doubts...
* Can I have only one segment except from null descriptor...
* how is it possible to set segment lenght to 4gb if there is not that much ram is installed... or one should check for available ram before assigning length....
* do I have to program each and every interrupt in pm like divide by zero error....
I have tried many tutorials anon pm and mnow I have some doubts...
* Can I have only one segment except from null descriptor...
* how is it possible to set segment lenght to 4gb if there is not that much ram is installed... or one should check for available ram before assigning length....
* do I have to program each and every interrupt in pm like divide by zero error....
Re:Some questions about PM
You need two besides the null selector: code and data.* Can I have only one segment except from null descriptor...
* how is it possible to set segment lenght to 4gb if there is not that much ram is installed... or one should check for available ram before assigning length....
Have you looked at this page: http://www.osdever.net/tutorials/pm.php?the_id=16
Re:Some questions about PM
1. Because it doesn't care. It only cares if you try to READ OR WRITE to their....although someone else said something about the bus not being tristated...what fools!* how is it possible to set segment lenght to 4gb if there is not that much ram is installed... or one should check for available ram before assigning length....
* do I have to program each and every interrupt in pm like divide by zero error..
2. Yes you do. Or you could just leave it and anything like that that happens will tripple fault and reset your computer . That's not very protected!
Also, remapping IRQ's is a must, so look into the PIC. Unless you want a double fault 18.2 times a second .
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Some questions about PM
http://www.osdev.org/osfaq2/index.php/GdtForDummies (just in case)
Keep in mind that segment limits are just a safety check. Even if you're running on a 48MB system, you still may have the video FrameBuffer at physical address 0xFE000000 ... you might equally well have setup paging and decided to map your kernel at 0xC000000 and want to access user data that are mapped in 0x0000000..0xBFFFFFF, etc.
Keep in mind that segment limits are just a safety check. Even if you're running on a 48MB system, you still may have the video FrameBuffer at physical address 0xFE000000 ... you might equally well have setup paging and decided to map your kernel at 0xC000000 and want to access user data that are mapped in 0x0000000..0xBFFFFFF, etc.
and that's not very helpful when developing either ::)That's not very protected!
Re:Some questions about PM
* Well why do we need to have atleast two segment ( or descriptors if I am wrong) i.e. code and data?
* Secondly I read somewhere that you will also have to decalre segment for video, audio, stack etc... also. Is it really neccesary, I mean Can't I write to video 0xB800 directly using C pointers.
* I would like you people to suggest me what to do with the following...
I need the following kind of memory
|--------------------------------|
| Whole ram under on descriptor |
|--------------------------------|
Is it possible.....
I devlop in 'C'.
Please help...
* Secondly I read somewhere that you will also have to decalre segment for video, audio, stack etc... also. Is it really neccesary, I mean Can't I write to video 0xB800 directly using C pointers.
* I would like you people to suggest me what to do with the following...
I need the following kind of memory
|--------------------------------|
| Whole ram under on descriptor |
|--------------------------------|
Is it possible.....
I devlop in 'C'.
Please help...
Re:Some questions about PM
i think it is because access rights.Codemaster Snake wrote: * Well why do we need to have atleast two segment ( or descriptors if I am wrong) i.e. code and data?
If you only have a code segment you want be allowed to write data to the variables inside by the cpu.
If you only have a datasegment, you won't be allowed to execute it.
You won't need that extra segments...* Secondly I read somewhere that you will also have to decalre segment for video, audio, stack etc... also. Is it really neccesary, I mean Can't I write to video 0xB800 directly using C pointers.
The minimum requirements are null, code and data segments.
Working with pointers for video memory is fine.
I think what you need is "flat memory".* I would like you people to suggest me what to do with the following...
I need the following kind of memory
|--------------------------------|
| Whole ram under on descriptor |
|--------------------------------|
Is it possible.....
I devlop in 'C'.
Please help...
Just have the code and datasegments overlap each other completely.
Both reach from 0 to 4 gig.
That works with c, everyone uses it, and you can implement paging on top.
Re:Some questions about PM
For the stack segment (content of %ss) you normally use the same descriptor as for data (so %ds == %ss). The location of the stack inside this segment is defined by %esp anyway.Codemaster Snake wrote: * Secondly I read somewhere that you will also have to decalre segment for video, audio, stack etc... also. Is it really neccesary, I mean Can't I write to video 0xB800 directly using C pointers.
cheers Joe
Re:Some questions about PM
OK some more qusetions
* base address in gdt is selected from 0x0000L(bottom address of ram) to the top address of the amount of memory installed?
* what is code seg, data seg. why are they there, is it necassy to be there, how does pc comes to know which seg is whgat as we can give any refrence name to them?
'Freanan' pointed that I need flat mem. The answer is yes. He also told me to overlap them. So,
* I should put same base address and put same seg length in gdt for both...
Now what I understood from your posts:
|-------------|
| Top of ram |0xsomething=amount of ram
| |
| |
| end of seg2 | ---- 0x600 say
| | |
| |limit of seg2
| | |
| start of seg2| ---- 0x400 say
| |
| |
| end of seg1 | ---- 0x200 say
| | |
| |limit of seg1
| | |
| start of seg1| ---- 0x100 say
|bottom of ram|0x000
|-------------|
is above correct. I mean this is waht GDT is meant for?
* base address in gdt is selected from 0x0000L(bottom address of ram) to the top address of the amount of memory installed?
* what is code seg, data seg. why are they there, is it necassy to be there, how does pc comes to know which seg is whgat as we can give any refrence name to them?
'Freanan' pointed that I need flat mem. The answer is yes. He also told me to overlap them. So,
* I should put same base address and put same seg length in gdt for both...
Now what I understood from your posts:
|-------------|
| Top of ram |0xsomething=amount of ram
| |
| |
| end of seg2 | ---- 0x600 say
| | |
| |limit of seg2
| | |
| start of seg2| ---- 0x400 say
| |
| |
| end of seg1 | ---- 0x200 say
| | |
| |limit of seg1
| | |
| start of seg1| ---- 0x100 say
|bottom of ram|0x000
|-------------|
is above correct. I mean this is waht GDT is meant for?
Re:Some questions about PM
A codesegment is supposed to contain instructions and can therefore be read(?) and executed.
A datasegment is supposed to contain data and can thefore be read and written.
What is their sense? - They are supposed to protect code from being edited by accident and data from being executed
How does the cpu know about them?
Your os code puts the selectors of the codesegment into cs register and the data one into ds and probably ss.
Via the selektors the cpu can look up entries in the gdt which will tell it where the segments start and end.
About your other question... I did not quite understand your ascii art, but the memory layout would look like that:
0x0 == start of data and code segment
0x*amount of physical memory* == not of any relevace at this stage.
0x*4 gig* == end of code and data seg.
I strongly recommend you to search and read
"The world of protected mode"
and maybe "brans kernel development tutorial"
on osdever.net for more detailled information.
A datasegment is supposed to contain data and can thefore be read and written.
What is their sense? - They are supposed to protect code from being edited by accident and data from being executed
How does the cpu know about them?
Your os code puts the selectors of the codesegment into cs register and the data one into ds and probably ss.
Via the selektors the cpu can look up entries in the gdt which will tell it where the segments start and end.
About your other question... I did not quite understand your ascii art, but the memory layout would look like that:
0x0 == start of data and code segment
0x*amount of physical memory* == not of any relevace at this stage.
0x*4 gig* == end of code and data seg.
I strongly recommend you to search and read
"The world of protected mode"
and maybe "brans kernel development tutorial"
on osdever.net for more detailled information.
Re:Some questions about PM
1. You should have a code and data segment, though at privelege RING-0 (which probably all programs will be in if you only have one segment) you can write to a code segment.
2.It will work, though if you try to access a higher area than is installed, you might have an error (such as an NMI) but more likely your write just plain won't work. So if you write FFh (or any other value) to an area that is higher than the amount of memory installed, then when you read that location you will probably get 00h.
In fact, this is one method of checking installed memory at startup, known as probing. Write a value to each byte/word/dword and then read it back until you get a 00h instead of the value you wrote. Of course, this method is fraught with problems, especially when there are memory-mapped devices (such as VGA with A0000h and B8000h) or if the banks of memory do not follow logically (e.g., bank 1 is 00000000h-10000000h, bank 2 is 20000000h-40000000h, and bank 3 is 80000000h-A0000000h.)
3.Well, yeah! Who else will program these interrupts? And if you get a divide by zero error without setting up the IDT, your processor will not be able to generate INT 00h, so it will have a double fault (which means TWO errors have occured). But the double fault is also an interrupt, INT 8h, so since you haven't setup the IDT yet, that can't go through either. So since 3 errors have come up, you'll get a triple fault, which means the processor resets itself. Or in Bochs, you get "third error without resolution" or something like that.
It is important to get the IDT setup early on, even if you don't handle any of the IRQ's yet (you can use CLI and/or mask by directly programming the PIC). Take care that you do not have any errors of this kind before the IDT is setup, and setup the IDT As Soon As Possible!
2.It will work, though if you try to access a higher area than is installed, you might have an error (such as an NMI) but more likely your write just plain won't work. So if you write FFh (or any other value) to an area that is higher than the amount of memory installed, then when you read that location you will probably get 00h.
In fact, this is one method of checking installed memory at startup, known as probing. Write a value to each byte/word/dword and then read it back until you get a 00h instead of the value you wrote. Of course, this method is fraught with problems, especially when there are memory-mapped devices (such as VGA with A0000h and B8000h) or if the banks of memory do not follow logically (e.g., bank 1 is 00000000h-10000000h, bank 2 is 20000000h-40000000h, and bank 3 is 80000000h-A0000000h.)
3.Well, yeah! Who else will program these interrupts? And if you get a divide by zero error without setting up the IDT, your processor will not be able to generate INT 00h, so it will have a double fault (which means TWO errors have occured). But the double fault is also an interrupt, INT 8h, so since you haven't setup the IDT yet, that can't go through either. So since 3 errors have come up, you'll get a triple fault, which means the processor resets itself. Or in Bochs, you get "third error without resolution" or something like that.
It is important to get the IDT setup early on, even if you don't handle any of the IRQ's yet (you can use CLI and/or mask by directly programming the PIC). Take care that you do not have any errors of this kind before the IDT is setup, and setup the IDT As Soon As Possible!
Re:Some questions about PM
OK! Thanks one more
* say I have ram of amount 0xffff M and I have set up to segs code and data overlaping by providing smae values for base and limit in gdt... Then We know that there is video mem at 0xB800 and 0xA000.... Since they are on BIOS 640 K mem (if i am not wrong) then why does it not make any conflict with the address 0xB800 on RAM if I create a pointer to it....
Hope you people understand my question... I mean I want to know wat happens to BIOS 640K mem beside than of RAMs.... I think that gdt divides RAm mem into chunks....
* say I have ram of amount 0xffff M and I have set up to segs code and data overlaping by providing smae values for base and limit in gdt... Then We know that there is video mem at 0xB800 and 0xA000.... Since they are on BIOS 640 K mem (if i am not wrong) then why does it not make any conflict with the address 0xB800 on RAM if I create a pointer to it....
Hope you people understand my question... I mean I want to know wat happens to BIOS 640K mem beside than of RAMs.... I think that gdt divides RAm mem into chunks....
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Some questions about PM
no, that's a common mistake:
the GDT doesn't _divide_, it _translates_ ...
It says (ds==0x10) ==> "read,write,don't execute", "add 0xB8000 to any offset", "allow offsets from 0 to 64K" ... that would be your VGA text mode memory.
It can also say (ds=0x18) ==> "read,write,don't execute", "add 0 to any offset", "allow any offset from 0 to 4GB" ... that would allow access to the whole address space.
that means
read then write to the _same_ memory location: the first character of the second row.
<rant>
... and why in the name of St Dijkstra and St Shannon do you, summer-dudes, wonderfully ignore any reference to the FAQ we're pointing you ??
</rant>
the GDT doesn't _divide_, it _translates_ ...
It says (ds==0x10) ==> "read,write,don't execute", "add 0xB8000 to any offset", "allow offsets from 0 to 64K" ... that would be your VGA text mode memory.
It can also say (ds=0x18) ==> "read,write,don't execute", "add 0 to any offset", "allow any offset from 0 to 4GB" ... that would allow access to the whole address space.
that means
Code: Select all
mov ds, 0x10 ;;; hum ...i know that don't exist
mov es, 0x18 ;;; but let's pretend it does.
mov al,[0x10:160]
inc al
mov [0x18:0xb80A0],al
<rant>
... and why in the name of St Dijkstra and St Shannon do you, summer-dudes, wonderfully ignore any reference to the FAQ we're pointing you ??
</rant>
Re:Some questions about PM
Ok Thanks a lot.... I took some time and read some good book on OS design and intel architecture....
Well After all that I was still not able to understand the concept of code segment and data segment....
* why these two have to be there
* why can't we have only one segment for everything
* how does a computer comes to know that which part of a program has to go in code segment and which part has to go in data segment
* what if one don't define stack segment
* and is there any other segment has to be defined except these two...
Well After all that I was still not able to understand the concept of code segment and data segment....
* why these two have to be there
* why can't we have only one segment for everything
* how does a computer comes to know that which part of a program has to go in code segment and which part has to go in data segment
* what if one don't define stack segment
* and is there any other segment has to be defined except these two...
Re:Some questions about PM
Segments are limited to 64k. By giving the ability to keep code in one segment, data in another and stack in a third, you tripled your available RAM. (Actually there is the "E" segment, and in later x86 generations the "F" and "G" segment, so you have 6x64k RAM available in Real Mode without switching segments around.)Codemaster Snake wrote: * why these two have to be there
Another idea is protection. Make CS read-only, and forbid executing anything in the stack segment, and you just killed 90% of all existing viruses.
Oh you could, but Intel opted to leave it in there for backward compatibility.* why can't we have only one segment for everything
Look at the layout of your average ELF executable, or your linker script. You have a .code section, a .data section...* how does a computer comes to know that which part of a program has to go in code segment and which part has to go in data segment
Then you don't have a stack. D'oh.* what if one don't define stack segment
Since GCC doesn't support segments (another d'oh), you should set all segments to offset 0, limit 0xffff... (too bored to count 'f's). You get all the usual problems with self-modifying code and stack overflows, but you get a working C compiler in exchange.* and is there any other segment has to be defined except these two...
Actually, there are some tricks you could still play with segments, but until you got the hang of them, take it easy and make it a flat memory model.
Every good solution is obvious once you've found it.
Re:Some questions about PM
OK
* Is paging easier to implement and use than segmentation
* Is paging easier to implement and use than segmentation