CS/DS limit?

Programming, for all ages and all languages.
Post Reply
okaox
Posts: 6
Joined: Thu Sep 24, 2009 12:59 pm

CS/DS limit?

Post by okaox »

hi my friends ...

- I activated Protected Mode x86
- I enabled a20
- Set descriptors CS/DS kernel

Code: Select all

descr_so_nulo:
    dw 0x0, 0x0, 0x0, 0x0

descr_so_code:
    dw 0xffff       ; limit[0..15]
    dw 0x0000       ; base[0..15]
    db 0x00         ; base[16..23]
    db 0x9a         ; P=1 DPL=00 S=1 Type=1010 (code read/exec)
    db 0x4f         ; G=0 D/B=1 L=0 AVL=0 Limit[16..19]
    db 0x00         ; base[24..31]

descr_so_data:
    dw 0xffff       ; limit[0..15]
    dw 0x0000       ; base[0..15]
    db 0x00         ; base[16..23]
    db 0x92         ; P=1 DPL=00 S=1 Type=0010 (data read/write)
    db 0x4f         ; G=0 D/B=1 L=0 AVL=0 Limit[16..19]
    db 0x00         ; Base[24..31]
is assumed that I can do this, but I get as output error limits in the debugger:

Code: Select all

    ; code load offset 0:800
    mov     ax, 0x08*2		; DS
    mov     ds, ax
    mov     es, ax
    mov     fs, ax
    mov     gs, ax
    mov     ss, ax

    mov     esp, 0xfffff-4	; Why? ... if Limit = 0xfffff !!!
    jmp     0x08:.aqui
.aqui:
    
    ret
What happens? :?

greetings!!!

PD: with [mov esp, 0xA0010] have no problem, more than this I get limit error ... :roll:
Last edited by okaox on Thu Sep 24, 2009 11:42 pm, edited 2 times in total.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: CS/DS limit?

Post by Brendan »

Hi,

Not sure if this is your problem or not, but...

The G (granularity) flag is used to tell the CPU if the limit has byte granularity (e.g. "limit = 0xFFFFF" means 1 MiB limit) or if the limit has page granularity (e.g. "limit = 0xFFFFF" means 1048575 pages or 4 GiB).

Basically the G (granularity) flag is clear so the segment limit is 1 MiB. For "mov esp, 0xA0010" your stack will be below the 1 MiB limit, so you'd be able to use the stack without general protection faults.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
okaox
Posts: 6
Joined: Thu Sep 24, 2009 12:59 pm

Re: CS/DS limit?

Post by okaox »

Thank you for the response...

If the granularity flag is clear, the segment size can range from 1 byte to 1 MByte, in byte increments.
If the granularity flag is set, the segment size can range from 4 KBytes to 4 GBytes, in 4-KByte increments.

But I am still wondering, Why occur the error?, if the range from A0010 to 0xFFFFF is < 1MB

greetings!!!

pd: Sorry for the english, I speak Spanish
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: CS/DS limit?

Post by pcmattman »

Code: Select all

    dw 0xffff       ; limit[0..15]
Correct me if I'm wrong, but shouldn't there be five f's there, not four?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: CS/DS limit?

Post by Brendan »

Hi,
okaox wrote:PD: with [mov esp, 0xA0010] have no problem, more than this I get limit error ... :roll:
okaox wrote:But I am still wondering, Why occur the error?, if the range from A0010 to 0xFFFFF is < 1MB
Are you *sure* it's a problem with the segment limit?

For example, is it possible that you're trying to put your stack in the middle of video display memory or ROM, and the error is caused by the CPU pushing stuff onto the stack (e.g. the return address for "call" instructions) and popping off something different later (e.g. some bytes of ROM that causes RET to return to a dodgy address and makes the CPU execute unknown instructions, that leads to lots of new and exciting ways to crash)? :-)

pcmattman wrote:

Code: Select all

    dw 0xffff       ; limit[0..15]
Correct me if I'm wrong, but shouldn't there be five f's there, not four?
In the lowest 16 bits of the limit there's only four F's. In the next 4 bits of the limit (e.g. in the "Limit[16..19]" field) there's another F...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: CS/DS limit?

Post by pcmattman »

In the lowest 16 bits of the limit there's only four F's. In the next 4 bits of the limit (e.g. in the "Limit[16..19]" field) there's another F...
Ah, of course. The whole [0..15] thing didn't really compute :(
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: CS/DS limit?

Post by qw »

Brendan wrote:Are you *sure* it's a problem with the segment limit?

For example, is it possible that you're trying to put your stack in the middle of video display memory or ROM, and the error is caused by the CPU pushing stuff onto the stack (e.g. the return address for "call" instructions) and popping off something different later (e.g. some bytes of ROM that causes RET to return to a dodgy address and makes the CPU execute unknown instructions, that leads to lots of new and exciting ways to crash)?
I'm pretty confident that Brendan is right. With a base of zero, 0xfffff - 4 is in the middle of ROM. By the way, you'd better align the stack to 0xffffc.
okaox
Posts: 6
Joined: Thu Sep 24, 2009 12:59 pm

Re: CS/DS limit?

Post by okaox »

I changed stak = 0xAFFFF
--> mov esp, 0xAFFFF

BOCHS: ¿?
Attachments
bochs.png
okaox
Posts: 6
Joined: Thu Sep 24, 2009 12:59 pm

Re: CS/DS limit?

Post by okaox »

thanks my friends !!! :D

I also thought something like this :wink:, I'm accessing an area of read-only (Upper memory area) used by some devices i/o (video, etc)
Image
sorry for the loss of time :lol:

greetings !!! :mrgreen:
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: CS/DS limit?

Post by AJ »

Hi,

Your stack pointer is fine, the return value on the stack is not. It's one of two things:

* You are deliberately trying to RET to code outside of the 0xFFFFF limit. In this case, set the granularity bit(s) in your GDT.
* You are intending to run code within the correct limits, but your stack is corrupted. What is the last value on your stack? Is it what you expect it to be?

Cheers,
Adam
Post Reply