Switching to protected mode - pointer GDT not working

Programming, for all ages and all languages.
Post Reply
ThatGuy2244
Posts: 15
Joined: Wed Feb 02, 2011 2:15 pm

Switching to protected mode - pointer GDT not working

Post by ThatGuy2244 »

My pointer to my GDT for switching to 32 - bit protected mode seems to not be working, I have isolated the problem to be:

Code: Select all

mov eax, 0
	mov ax, ds
	shl eax, 4
	add eax, GDTStart
This is that part of my code that translates the segment:offset of my DOUBLE WORD pointer to my GDT structure into a linear address, but I can't find out whats wrong with it. Just in case it helps here is the rest of my GDT stuff:

Code: Select all

cli
	mov eax, 0
	mov ax, ds
	shl eax, 4
	add eax, GDTStart
	mov [GDTInfo + 2], eax
	lgdt [GDTInfo]
	mov eax, cr0
	or eax, 1
	mov cr0, eax
	mov ax, 0x10
	mov ds, ax
	mov ss, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	jmp 0x08:pmode
	
	GDTInfo:
	dw GDTEnd - GDTStart - 1
	dd GDTStart
	
	GDTStart:
	dd 0x00000000 ;Descriptor 0 Null
	dd 0x00000000
	dw 0xffff ;Descriptor 1 Code
	dw 0x0000
	db 0x00
	db 10011010b
	db 11001111b
	db 0x00
	dw 0xffff ;Descriptor 2 Data
	dw 0x0000
	db 0x00
	db 10010010b
	db 11001111b
	db 0x00
	GDTEnd:
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Switching to protected mode - pointer GDT not working

Post by thepowersgang »

Is that the correct address? What is the value of DS at runtime, and what is the value of GDTStart?
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
ThatGuy2244
Posts: 15
Joined: Wed Feb 02, 2011 2:15 pm

Re: Switching to protected mode - pointer GDT not working

Post by ThatGuy2244 »

thepowersgang wrote:Is that the correct address? What is the value of DS at runtime, and what is the value of GDTStart?
Yes I do know the value of DS at run time that's why I move ds into ax and the value of GDTStart does not matter I don't think as long as it is referenced correctly in 32-bit linear format.
ThatGuy2244
Posts: 15
Joined: Wed Feb 02, 2011 2:15 pm

Re: Switching to protected mode - pointer GDT not working

Post by ThatGuy2244 »

I don't know what you mean by hard facts, but if you mean the actual address number that the GDT is located, then I don't know that, but that should not matter because all my code is relative to my GDT and not based on static address. What I can tell you is that this file is a .COM file and is loaded by DOS to location 0x100 and before you think this is the solution to my problem earlier in my program I have "[org 0x100]" which automatically offsets all my addresses by 0x100 (in nasm).
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Switching to protected mode - pointer GDT not working

Post by bluemoon »

ThatGuy2244 wrote:My pointer to my GDT for switching to 32 - bit protected mode seems to not be working
So what's the exact error? have you tried single stepping into the code to see how far it go, and perhaps dump register values? GDB is your friend.
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: Switching to protected mode - pointer GDT not working

Post by Gigasoft »

The GDT pointer looks fine to me, however, your segments both start at address 0, instead of the PSP address.

(By the way: you can use mov eax,ds, this automatically zero extends it.)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Switching to protected mode - pointer GDT not working

Post by Combuster »

In other words:

Code: Select all

   jmp 0x08:pmode
jumps to somewhere in the IVT, and not where your program is loaded
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Switching to protected mode - pointer GDT not working

Post by Chandra »

Looks like OP needs PIC tips.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
ThatGuy2244
Posts: 15
Joined: Wed Feb 02, 2011 2:15 pm

Re: Switching to protected mode - pointer GDT not working

Post by ThatGuy2244 »

bluemoon wrote:
ThatGuy2244 wrote:My pointer to my GDT for switching to 32 - bit protected mode seems to not be working
So what's the exact error? have you tried single stepping into the code to see how far it go, and perhaps dump register values? GDB is your friend.
The exact error is "Illegal Descriptor type 0 for int 0" -- Note that this is an error given by the emulator (DOSBOX) and not the assembler.

Combuster wrote:In other words:

Code: Select all

   jmp 0x08:pmode
jumps to somewhere in the IVT, and not where your program is loaded
If I understand you correctly then you are implying that I have to convert the pmode pointer into a linear address (assuming that it is a seg:offset). I don't think I would have to do this because my pmode pointer is written for 32 bit code - meaning that it would be a linear address already.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Switching to protected mode - pointer GDT not working

Post by Combuster »

I don't think I would have to do this because my pmode pointer is written for 32 bit code - meaning that it would be a linear address already.
You really haven't understood the point of your previous thread haven't you? (And btw, why create a new one for the exact same problem)

Homework:
what is a virtual address?
what is a linear address?
what is a physical address?
How does the assembler know which of the three you want?
Why is "loaded to location 0x100" an insufficient remark?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
ThatGuy2244
Posts: 15
Joined: Wed Feb 02, 2011 2:15 pm

Re: Switching to protected mode - pointer GDT not working

Post by ThatGuy2244 »

I realize that 32 bit pointer are not linear, but do point in this case in a linear way since all my descriptors start at physical address 0.
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: Switching to protected mode - pointer GDT not working

Post by Gigasoft »

But the labels aren't relative to address 0, but to the old CS << 4. The assembler does not know the physical address where the file will be loaded. Your descriptors have to start at CS << 4, or else you have to add this every time you reference a label.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Switching to protected mode - pointer GDT not working

Post by Combuster »

ThatGuy2244 wrote:all my descriptors start at physical address 0.
They don't, not by far. What's the value of CS on line 1?

EDIT: and since you couldn't be bothered to do my first set of homework properly, here's more. I won't help you further with this problem until you at least attempted the entire sequence:
what's the value of CS on line 1 of your code? (use a debugger or print it to the screen)
what's the segment base of CS on line 1?
what is the IP on line 1?
calculate the linear address of the code on line 1. What is it?
how many bytes are the jump and the start of the code apart from another?
what is the value of cs after the jump?
what is the value of ip after the jump?
what is the segment base of cs after the jump?
calculate the linear address of the code jumped to. What is it?
How far is this place from the linear address at the start of your code?
Is this right? what have you learned?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Switching to protected mode - pointer GDT not working

Post by bluemoon »

ThatGuy2244 wrote:
Combuster wrote:In other words:

Code: Select all

   jmp 0x08:pmode
jumps to somewhere in the IVT, and not where your program is loaded
If I understand you correctly then you are implying that I have to convert the pmode pointer into a linear address (assuming that it is a seg:offset). I don't think I would have to do this because my pmode pointer is written for 32 bit code - meaning that it would be a linear address already.
What's the point of asking question if you don't think other has point you to the issue?
The label pmode has the value of ORG + offset from beginning of code, in you .COM program and few lines of code it's probably has address lower than 1024, while the actual memory location is obviously different.
Post Reply