how to use LDT

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.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

how to use LDT

Post by ckzippo »

I want to use LDT. so i define a new selector in GDT.

Code: Select all

 
   mov	$gdt, %eax
	addl $FIRST_LDT_SELECTOR, %eax
	movw	$(ldt0end - ldt0 - 1), (%eax)
	movl	 $ldt0, %ebx
	movw	%bx, 2(%eax)
	shr    $16, %ebx
	movb	%bl, 4(%eax)
	movb	%bh, 7(%eax)
	movb	$0x82,5(%eax)
the code above is to set the descriptor in GDT.
and ldt is defined as below:

Code: Select all

ldt0:
	.quad 0x0000000000000000	# first is not in use
	.quad 0x00cf981000c1ffff
ldt0end:
then , i use the following:

Code: Select all

  movl	$FIRST_LDT_SELECTOR, %eax
	lldt	%ax                      # error
   jmp   $0x0c, $0
i use qemu to debug, and it crashes when lldt is excuted.
i'm confused with this problem several days.

Any help will be great appreciated.Thank you.
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: how to use LDT

Post by Combuster »

I suggest that you try bochs, it provides much more detailed output when something goes wrong. It also has a debugger version where you can actually inspect the GDT and LDT in detail at the location of the crash.

As far as the code is concerned, it occurs to me that byte 6 of the GDT entry is left undefined.
"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 ]
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to use LDT

Post by ckzippo »

Combuster wrote:I suggest that you try bochs, it provides much more detailed output when something goes wrong. It also has a debugger version where you can actually inspect the GDT and LDT in detail at the location of the crash.

As far as the code is concerned, it occurs to me that byte 6 of the GDT entry is left undefined.
byte 6 is all 0.
i use

Code: Select all

movw $0x82,5(%eax)
rather

Code: Select all

movb $0x82,5(%eax)
it generates the same fault, GP.
anyway,thank you.i will try bochs.
User avatar
amd64pager
Member
Member
Posts: 73
Joined: Fri Nov 25, 2011 8:27 am
Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1

Re: how to use LDT

Post by amd64pager »

ckzippo wrote:
byte 6 is all 0.
i use

Code: Select all

movw $0x82,5(%eax)
rather

Code: Select all

movb $0x82,5(%eax)
it generates the same fault, GP.
anyway,thank you.i will try bochs.
Try

Code: Select all

movb $0x82,5(%eax)
movb $0x00,6(%eax)
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to use LDT

Post by ckzippo »

amd64pager wrote:
ckzippo wrote:
byte 6 is all 0.
i use

Code: Select all

movw $0x82,5(%eax)
rather

Code: Select all

movb $0x82,5(%eax)
it generates the same fault, GP.
anyway,thank you.i will try bochs.
Try

Code: Select all

movb $0x82,5(%eax)
movb $0x00,6(%eax)
yes,i tested.But the same,GP. :)
thanks.
User avatar
amd64pager
Member
Member
Posts: 73
Joined: Fri Nov 25, 2011 8:27 am
Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1

Re: how to use LDT

Post by amd64pager »

Instead of using this:
ckzippo wrote: and ldt is defined as below:

Code: Select all

ldt0:
	.quad 0x0000000000000000	# first is not in use
	.quad 0x00cf981000c1ffff
ldt0end:
maybe you could break it down into bytes and words.

EDIT:I think my old bootsector(an year ago,I think) must have failed because of this(triple faulted)
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to use LDT

Post by ckzippo »

amd64pager wrote:Instead of using this:
ckzippo wrote: and ldt is defined as below:

Code: Select all

ldt0:
	.quad 0x0000000000000000	# first is not in use
	.quad 0x00cf981000c1ffff
ldt0end:
maybe you could break it down into bytes and words.

EDIT:I think my old bootsector(an year ago,I think) must have failed because of this(triple faulted)
Does this matter? i checked the memory in qemu. i find whether break the quad down to bytes and words or not,the organisation
in memory is the same.
Also, i try this:

Code: Select all

.word 0xffff,0x00c1,0x9810,0x00cf
and this:

Code: Select all

.byte 0xff,0xff,0xc1,0x00
.byte 0x10,0x98,0xcf,0x00
the result is also GP.
Thank you. :)
User avatar
amd64pager
Member
Member
Posts: 73
Joined: Fri Nov 25, 2011 8:27 am
Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1

Re: how to use LDT

Post by amd64pager »

Could you tell me what is your descriptors base,limit,etc.?
Maybe I could rewrite it for you.
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
rdos
Member
Member
Posts: 3307
Joined: Wed Oct 01, 2008 1:55 pm

Re: how to use LDT

Post by rdos »

Compared with my own create ldt code, and the code to create the ldt-descriptor seems to be ok. What I wonder is what the descriptor index is, and what the GDT-limit is? Since you create the descriptor within a flat memory model, you might succeed with doing that without faults even if you are outside of the GDT-limit. That would explain the GPF. The load of the LDT should not fail when offset 5 is 0x82.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to use LDT

Post by ckzippo »

amd64pager wrote:Could you tell me what is your descriptors base,limit,etc.?
Maybe I could rewrite it for you.
i defined GDT as :

Code: Select all

struct segdesc_s gdt[GDT_SIZE]
and marco GDT_SIZE = 100;
the selector of LDT is FIRST_LDT_SELECTOR,whose value is 0x50.
the base of GDT is gdt, and the limit is GDT_SIZE * 8 -1
i think i set GDT right ,because when i use other selectors in GDT,it is OK.

the base of LDT is ldt0, which is defined as:

Code: Select all

ldt0:
     .quad 0x0000000000000000   # first is not in use
     .quad 0x00cf9a000000ffff       #selector is 0x0C,it's local code segment descriptor
ldt0end:


the limit of LDT is:
ldt0end - ldt0 - 1
then i use a jmp to jmp to LDT.

Code: Select all

movl $FIRST_LDT_SELECTOR, %eax
lldt  %ax                           #error
jmp $0x0c,$task0
task0:
the error occured lldt %ax.

i write exception handlers in my kernel.
i set 13th exception handler to print "General Protection"
i set 16th exception handler to print "FPU Floating Point Error(Math Fault)"

it first print Geneal Protection, and after a while , it print FPU Floating point Error

thank you.
Last edited by ckzippo on Thu Jun 07, 2012 7:37 pm, edited 1 time in total.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to use LDT

Post by ckzippo »

rdos wrote:Compared with my own create ldt code, and the code to create the ldt-descriptor seems to be ok. What I wonder is what the descriptor index is, and what the GDT-limit is? Since you create the descriptor within a flat memory model, you might succeed with doing that without faults even if you are outside of the GDT-limit. That would explain the GPF. The load of the LDT should not fail when offset 5 is 0x82.
the selector is 0x50, and GDT limit is 100*8 -1 . And it's not outside the GDT table.
Thank you.
User avatar
amd64pager
Member
Member
Posts: 73
Joined: Fri Nov 25, 2011 8:27 am
Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1

Re: how to use LDT

Post by amd64pager »

If the source operand is 0, the LDTR is marked invalid and all references to descriptors in the LDT (except by the LAR, VERR, VERW or LSL instructions) cause a general protection excep-tion (#GP).
-from http://faydoc.tripod.com/cpu/lldt.htm
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
User avatar
amd64pager
Member
Member
Posts: 73
Joined: Fri Nov 25, 2011 8:27 am
Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1

Re: how to use LDT

Post by amd64pager »

Also , try:

Code: Select all

lldt $0x50
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
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: how to use LDT

Post by Combuster »

anyway,thank you.i will try bochs.
Have you yet?
"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 ]
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to use LDT

Post by ckzippo »

amd64pager wrote:Also , try:

Code: Select all

lldt $0x50
we can not use

Code: Select all

lldt $0x50
according to the Intel Manual,
we should use

Code: Select all

LLDT r/m16
can not use an imm like $0x50

and this indeed cause error "operand type mismatch for lldt"

thank you for your advice. :)
Post Reply