#GP when trying to load 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
User avatar
voidf0xb3
Posts: 7
Joined: Fri Mar 16, 2012 11:32 am
Location: Moscow, Russia

#GP when trying to load SS

Post by voidf0xb3 »

Greetings, OSDev forumites!

I’m currently attempting to write a simple OS, and I’ve encountered one strange problem. I try to load a value into SS with the following code (running in Ring 0 aka CPL=0):

Code: Select all

mov word [0502h], 0018h
mov dword [0504h], 00010000h
lss esp, [0502h]
mov ebp, 00010000h
It’s supposed to establish a stack starting at address 0000FFFFh, but instead, lss causes a general protection exception. Here’s my GDT content:
  • 0008h: Execute/read 32-bit code segment; Base=00000000h; Limit=FFFFF000h; DPL=0; Present (FF FF 00 00 00 9A CF 00). CS points here.
  • 0010h: Read/write 32-bit data segment; Base=00000000h; Limit=FFFFF000h; DPL=3; Present (FF FF 00 00 00 F2 CF 00). DS, ES, FS, GS point here.
  • 0018h: Read/write 32-bit data segment; Base=00000000h; Limit=FFFFF000h; DPL=0; Present (FF FF 00 00 00 92 CF 00). I'm trying to make SS point here.
Therefore, I expect that this exception is not caused by:
  • anything pointing outside the segment (because it covers the whole address space);
  • wrong segment type (it’s a read-write data segment);
  • privilege level problems (its DPL equals the current CPL, as it should be for SS).
I’m also sure there’s nothing important at addresses 0502h to 0507h (although that’s part of GDT’s null segment descriptor).

So the question is: what could be the reason for this exception?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: #GP when trying to load SS

Post by bluemoon »

Intel manual wrote: For LSS instruction:

Protected Mode Exceptions
#GP(0)
If a NULL selector is loaded into the SS register.
If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.
If the DS, ES, FS, or GS register is used to access memory and it contains a NULL segment selector.

#GP(selector)
If the SS register is being loaded and any of the following is true: the segment selector index is not within the descriptor table limits, the segment selector RPL is not equal to CPL, the segment is a non-writable data segment, or DPL is not equal to CPL.
These should be the only possible causes, so, are you sure?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: #GP when trying to load SS

Post by Brendan »

Hi,
ThePhantom wrote:

Code: Select all

mov word [0502h], 0018h
mov dword [0504h], 00010000h
lss esp, [0502h]
That looks around the wrong way to me. Try:

Code: Select all

mov dword [0502h], 00010000h
mov word [0506h], 0018h
lss esp, [0502h]

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.
User avatar
voidf0xb3
Posts: 7
Joined: Fri Mar 16, 2012 11:32 am
Location: Moscow, Russia

Re: #GP when trying to load SS

Post by voidf0xb3 »

That seems to be it. Thanks a lot! Somehow I couldn’t find any mention about the order of these parts in Intel’s manual.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: #GP when trying to load SS

Post by bluemoon »

It's stated on Volume 1, 4.3 POINTER DATA TYPES. (Figure 4.4 on my manual)
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: #GP when trying to load SS

Post by egos »

ThePhantom wrote:Greetings, OSDev forumites!
Hi, countryman! Here is some info for you:
- in FLAT data and stack segments usually have the same descriptor;
- lss is a powerful instruction but there is other more suitable one in this context:

Code: Select all

  mov ss,DATA_SEL
  mov esp,10000h
  mov ebp,esp
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
voidf0xb3
Posts: 7
Joined: Fri Mar 16, 2012 11:32 am
Location: Moscow, Russia

Re: #GP when trying to load SS

Post by voidf0xb3 »

egos wrote:Hi, countryman! Here is some info for you:
- in FLAT data and stack segments usually have the same descriptor;
Shouldn’t there be (at least) separate descriptors for Ring 0 and Ring 3 stacks? As far as I understand, the stack segment descriptor’s DPL should be the same as the current CPL (Intel manual, volume 3, section 5.7).

Although there’s no Ring 3 code right now…
- lss is a powerful instruction but there is other more suitable one in this context:

Code: Select all

  mov ss,DATA_SEL
  mov esp,10000h
  mov ebp,esp
OK.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: #GP when trying to load SS

Post by egos »

ThePhantom wrote:Although there’s no Ring 3 code right now…
That's it! Besides, no reason to use descriptors with different DPL values for Ring 0 data and stack.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply