Where to put SS

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.
Post Reply
srg

Where to put SS

Post by srg »

Hi

Once in pmode with flat memory model and 4 flat segments, Which segment should I set the SS selector to?

Thanks
DynatOS

Re:Where to put SS

Post by DynatOS »

A ring-0 one with lots of room :)
srg

Re:Where to put SS

Post by srg »

Data segment I assume?
DynatOS

Re:Where to put SS

Post by DynatOS »

Yes. Usually DS,ES,FS,GS and SS are the same... all data segments.
srg

Re:Where to put SS

Post by srg »

IC

Where abouts in this segment is the stack placed? How can I tell so I don't accidentally overwrite it with some data?
DynatOS

Re:Where to put SS

Post by DynatOS »

At the top of the segment most likely. Set the SS to the appropriate Segment, and ESP to the appropriate offset. The stack decrements for each push and increments for each pop. The best way to make sure you know how big it is, is to manual manage it by checking the ESP every so often.

Say for example you have a kernel at 0001:0000, and it is 512K long... you could get away with setting the SS to the same as DS and setting ESP to 0x0009FFFF, that gives it well over 128K of space. Depending on how your kernel works, this should be more than enough, you shouldn't need to manage it unless some bad code has been spun. You can reinforce it though by checking if the limit of your kernel is <= ESP-10, if it is... do something about it.
srg

Re:Where to put SS

Post by srg »

does having paging enabled affect this, especially as I plan to use paged virtual memory.
DynatOS

Re:Where to put SS

Post by DynatOS »

If you plan on using paging, don't set the kernel nor the stack in pagable memory, reserve a static code and data section for those.
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:Where to put SS

Post by Pype.Clicker »

what i suggest for a safe kernel environment is to create a separate segment for the task, but that will have the same base address as the data segment (so that pointers can be kept flat).

however, by turning your stack segment to EXPAND_DOWN mode, you can define the maximum valid location for ESP, and therefore protect yourself from kernel stack overflow

Because a kernel stack overflow cannot be handled by a normal exception (i mean a interrupt-like one, which just push error code on the stack etc .. as we have no stack ;) ), the handler for STACK FAULT will have to be handled by a dedicated task (this is what the crash task is about in Clicker ;)

Hope this opened new pathes
srg

Re:Where to put SS

Post by srg »

When you mean pageable memory, I'm assuming you mean memory that can be paged out to disk.

hmm I had hoped that after setting up the four segments, I could then effectively forget about segmentation rather than have to make more segemnts. I'm asuming this is what say Linux and NT do. I don't really want to have more than those 4 unless that's not how to do it.
Tim

Re:Where to put SS

Post by Tim »

and setting ESP to 0x0009FFFF
It's a bad idea to set ESP to a number that isn't a multiple of 4. In this case, you'd set ESP to 0x000A0000, assuming that the stack spans bytes 0x00000000 to 0x0009FFFF.

Remember that when the CPU pushes a (32-bit) value, it decrements ESP by 4 then writes to [ESP]. If ESP=0x0009FFFF then the first value will be written at 0x0009FFFB, the second at 0x0009FFF7, and so on. The CPU doesn't like accessing unaligned data like this.
Post Reply