The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: AeroASM on April 24, 2005, 02:35:04 PM

Title: Task Switching and the Stack
Post by: AeroASM on April 24, 2005, 02:35:04 PM
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?
Title: Re: Task Switching and the Stack
Post by: thomasantony on April 24, 2005, 02:59:44 PM
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
Title: Re: Task Switching and the Stack
Post by: 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?
Title: Re: Task Switching and the Stack
Post by: roticv on April 24, 2005, 05:36:24 PM
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.
Title: Re: Task Switching and the Stack
Post by: arafel on April 24, 2005, 06:29:04 PM
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.
Title: Re: Task Switching and the Stack
Post by: 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.)
Title: Re: Task Switching and the Stack
Post by: arafel on April 24, 2005, 07:44:22 PM
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.
Title: Re: Task Switching and the Stack
Post by: thomasantony on April 25, 2005, 04:59:43 AM
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
Title: Re: Task Switching and the Stack
Post by: AeroASM on April 25, 2005, 06:36:44 AM
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?
Title: Re: Task Switching and the Stack
Post by: Tedd on April 25, 2005, 10:03:25 AM
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 ::)
Title: Re: Task Switching and the Stack
Post by: AeroASM on April 25, 2005, 11:41:47 AM
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.
Title: Re: Task Switching and the Stack
Post by: roticv on April 25, 2005, 12:13:28 PM
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.
Title: Re: Task Switching and the Stack
Post by: AeroASM on April 25, 2005, 01:36:00 PM
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
Title: Re: Task Switching and the Stack
Post by: Opcode on April 25, 2005, 01:54:31 PM
Hey...

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

Regards,
Opcode

Title: Re: Task Switching and the Stack
Post by: AeroASM on April 25, 2005, 01:56:57 PM
I have the Intel manuals but do not understand them.
Title: Re: Task Switching and the Stack
Post by: Opcode on April 25, 2005, 01:57:41 PM
http://www.amazon.com/exec/obidos/tg/detail/-/0201721872/102-2695169-7548952?v=glance
http://www.amazon.com/exec/obidos/tg/detail/-/0735610215/ref=pd_sim_b_3/102-2695169-7548952?%5Fencoding=UTF8&v=glance

and

http://www.windowsitlibrary.com/Content/356/04/1.html
Title: Re: Task Switching and the Stack
Post by: The Dude of Dudes on April 26, 2005, 03:48:18 AM
Hi AeroAsm,

Re: Sharing Stack Parameters among different processes

When you pass Stack Params to a Windows API function, the OS copies a pointer to them in edx and then does a switch to Ring0, which has it's own stack. Each Process has a user and Kernel stack for security reasons.

As far as implementing a memory manager, paging would be a lot more work than you think. If this is only a hobby OS, and you're not worried about security I would just use Segmentation (paging disabled). This way, the address you specify with segment/offset translates directly into a physical one. ie. 0000:FFFF = FFFF physical. If paging is enabled, the processor translates segment/offset to a linear addrress and then uses a page directory (and maybe a page table depending on the page size you choose) to get the  actual physical address. YOU have to create and update these page tables/directories when memory is allocated, when page faults occur (program tries to access an address that does not have an entry in the page table), when memory is freed, when memory sits 'idle' and should be paged out to a swapfile. It would be very cool to write a Memory Manager that uses paging, though!
Title: Re: Task Switching and the Stack
Post by: thomasantony on April 26, 2005, 06:15:37 AM
See the tuts at

http://www.cyberscriptorium.com/osjournal/cgi-bin/index.pl?action=listtopic&topic=2

Thomas :U