News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Task Switching and the Stack

Started by AeroASM, April 24, 2005, 02:35:04 PM

Previous topic - Next topic

AeroASM

When Windows does a task switch, it saves every register in a structure (thread context?) so when the program next executes, it is in the same state as before. What about the stack? The stack must be global, because you can pass the address of a stack variable to another program and it works. But when the program next comes back to execute, it is in trouble because the stack will be different.

Also, how come every PE program has an entry point of 001B:00401000? without two programs occupying the same memory space?

thomasantony

Hi,
  It is a big mystery called Virtual Memory :bdg . All programs it windows fells like they are the only ones in memory (along with the OS of course). This is implemented using things called Page tables. Each process hasd its own PAge Directory and Page Tables. The page table entries define the address of a 4kB block. and they are taken in order. So the first one adress 0-4kB next one 4-8kb like that. The thing is that the vitrual addres is NOT equal to Physical address. The Page entry for address 1 Gb  may point to a physical address somewhere like 100 Mb or so. So the programs  cannot interfere with each other's memory.  Since the page tables are also saved in a context along with the stack address. The stack address of two processes may be same but will not point to the same area in physical memory. This translation of virtual to physical addreses is done by a Memory Unit in the CPU and has very little overhead. It pays to learn OS Programming :wink . I am wrestling with the memory management module of my OS now. :green2 . Hope I have made myself clear. See the DOCs on memory management and paging at www.osdever.net to make this more clear.

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

AeroASM

I had a feeling that might be the answer. It looks to me like paging is going to be a big problem for me (Bieb and I are making an OS) because it feels very illogical and complicated.. Do you think it is safe to use segmentation with only 512Mb RAM?

roticv

Quote from: AeroASM on April 24, 2005, 03:52:07 PM
I had a feeling that might be the answer. It looks to me like paging is going to be a big problem for me (Bieb and I are making an OS) because it feels very illogical and complicated.. Do you think it is safe to use segmentation with only 512Mb RAM?
What do you mean by whether it is safe to use segmentation? I don't think the size of the RAM make any difference.

arafel

roticv is right. Size of the available ram doesn't matter when choosing a memory management scheme. You should choose whenever to use Paging, Segmentation or other variations basing on what you need. For example implementing Virtual Memory will provide much better protection for processes and advanced features like swapping to disk, while segmented memory lacks those features.

AeroASM

roticv is wrong. Size of the available ram does matter when choosing a memory management scheme. With Paging you can have as much memory as you like because you have a swapfile. With segmentation you are limited to 512MB (or whatever amount of ram you have.)

arafel

In both cases you are actually limited to the real amount of memory. You shouldn't rely on something that can or cannot be - like swapfile (of course it's different when the swapfile usage is an integrated part of the system). And size of memory does not really matter unless you're talking about embedded systems with very little memory where one scheme can benefit instead of other.

thomasantony

If you are making a protected mode OS, you should go for paging and virtual memory. IMO All the work done in implementing segmentation is not worth it. As for the OS. where have you reached in terms of progress?

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

AeroASM

I was under the impression that paging is a lot more work than segmentation, and if I am going to use paging I may as well have a swapfile. If size of memory does not matter, why  do people pay for more memory?

Tedd

Quote from: AeroASM on April 25, 2005, 06:36:44 AM
If size of memory does not matter, why do people pay for more memory?

Because repeatedly copying to/from the swap-file is ssslllllloooowwwwww ::)
No snowflake in an avalanche feels responsible.

AeroASM

Why does no one on this forum ever understand my irony! (grrrr)

arafel said size of memory does not matter unless you have like 5k to work with - this is wrong, as Tedd said. Therefore please quote from arafel, not me.

thomas: "All the work done in implementing segmentation..."

What are you on about? I am trying to decide whether to use paging or segmentation:

                                          Segmentation                           Paging

Advantages:                         Little work to be done               Much work to be done

Disadvantages:                      Limited by RAM                         Not limited by RAM

Does the disadvantage of segmentation outweigh the advantage (I have 512Mb RAM)?

My OS hasn't started yet.

roticv

Quote from: AeroASM on April 24, 2005, 07:26:50 PM
roticv is wrong. Size of the available ram does matter when choosing a memory management scheme. With Paging you can have as much memory as you like because you have a swapfile. With segmentation you are limited to 512MB (or whatever amount of ram you have.)
You know that is pure rubbish to me. Paging CAN be used together with segmentation. Quoting Intel Manual for P4 volume 3 section 3.2.4 "Paging can be used with any of the segmentation models described in Figures 3-2, 3-3 and 3-4." Are you asking whether to use flat model or to use multi-segment model? Refer to your Intel Manual if you do not believe me.

AeroASM

HOw would I know that is rubbish to you? I do not understand any of this, except the most basic flat memory model where linear address = segment base address = offset

Opcode

Hey...

What about try to read some basic books about memory management in modern operating systems
before asking these basic questions?

Regards,
Opcode


AeroASM

I have the Intel manuals but do not understand them.