Where to put SS
Where to put SS
Hi
Once in pmode with flat memory model and 4 flat segments, Which segment should I set the SS selector to?
Thanks
Once in pmode with flat memory model and 4 flat segments, Which segment should I set the SS selector to?
Thanks
Re:Where to put SS
IC
Where abouts in this segment is the stack placed? How can I tell so I don't accidentally overwrite it with some data?
Where abouts in this segment is the stack placed? How can I tell so I don't accidentally overwrite it with some data?
Re:Where to put SS
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.
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.
Re:Where to put SS
does having paging enabled affect this, especially as I plan to use paged virtual memory.
Re:Where to put SS
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.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Where to put SS
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
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
Re:Where to put SS
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.
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.
Re:Where to put SS
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.and setting ESP 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.