Some questions about PM

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.
codemastersnake

Some questions about PM

Post by codemastersnake »

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....
beyondsociety

Re:Some questions about PM

Post by beyondsociety »

* 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....
You need two besides the null selector: code and data.

Have you looked at this page: http://www.osdever.net/tutorials/pm.php?the_id=16
Cjmovie

Re:Some questions about PM

Post by Cjmovie »

* 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..
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!

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 :).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Some questions about PM

Post by Pype.Clicker »

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.
That's not very protected!
and that's not very helpful when developing either ::)
codemastersnake

Re:Some questions about PM

Post by codemastersnake »

* 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...
Freanan

Re:Some questions about PM

Post by Freanan »

Codemaster Snake wrote: * Well why do we need to have atleast two segment ( or descriptors if I am wrong) i.e. code and data?
i think it is because access rights.
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.
* 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.
You won't need that extra segments...
The minimum requirements are null, code and data segments.
Working with pointers for video memory is fine.
* 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...
I think what you need is "flat memory".
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.
JoeKayzA

Re:Some questions about PM

Post by JoeKayzA »

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.
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.

cheers Joe
codemastersnake

Re:Some questions about PM

Post by codemastersnake »

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?
Freanan

Re:Some questions about PM

Post by Freanan »

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.
NotTheCHEAT

Re:Some questions about PM

Post by NotTheCHEAT »

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!
codemastersnake

Re:Some questions about PM

Post by codemastersnake »

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....
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Some questions about PM

Post by Pype.Clicker »

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

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
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>
codemastersnake

Re:Some questions about PM

Post by codemastersnake »

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...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Some questions about PM

Post by Solar »

Codemaster Snake wrote: * why these two have to be there
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.)

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.
* why can't we have only one segment for everything
Oh you could, but Intel opted to leave it in there for backward compatibility.
* 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
Look at the layout of your average ELF executable, or your linker script. You have a .code section, a .data section...
* what if one don't define stack segment
Then you don't have a stack. D'oh. ;)
* and is there any other segment has to be defined except these two...
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. ;)

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.
codemastersnake

Re:Some questions about PM

Post by codemastersnake »

OK

* Is paging easier to implement and use than segmentation
Post Reply