Page 1 of 2
how to use LDT
Posted: Wed Jun 06, 2012 4:39 am
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.
Re: how to use LDT
Posted: Wed Jun 06, 2012 4:54 am
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.
Re: how to use LDT
Posted: Wed Jun 06, 2012 5:57 am
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
rather
it generates the same fault, GP.
anyway,thank you.i will try bochs.
Re: how to use LDT
Posted: Wed Jun 06, 2012 6:11 am
by amd64pager
ckzippo wrote:
byte 6 is all 0.
i use
rather
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)
Re: how to use LDT
Posted: Wed Jun 06, 2012 6:35 am
by ckzippo
amd64pager wrote:ckzippo wrote:
byte 6 is all 0.
i use
rather
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.
Re: how to use LDT
Posted: Wed Jun 06, 2012 6:43 am
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)
Re: how to use LDT
Posted: Wed Jun 06, 2012 7:08 am
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:
and this:
Code: Select all
.byte 0xff,0xff,0xc1,0x00
.byte 0x10,0x98,0xcf,0x00
the result is also GP.
Thank you.

Re: how to use LDT
Posted: Thu Jun 07, 2012 5:12 am
by amd64pager
Could you tell me what is your descriptors base,limit,etc.?
Maybe I could rewrite it for you.
Re: how to use LDT
Posted: Thu Jun 07, 2012 12:39 pm
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.
Re: how to use LDT
Posted: Thu Jun 07, 2012 7:32 pm
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 :
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.
Re: how to use LDT
Posted: Thu Jun 07, 2012 7:35 pm
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.
Re: how to use LDT
Posted: Fri Jun 08, 2012 1:21 am
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
Re: how to use LDT
Posted: Fri Jun 08, 2012 1:25 am
by amd64pager
Re: how to use LDT
Posted: Fri Jun 08, 2012 5:55 am
by Combuster
anyway,thank you.i will try bochs.
Have you yet?
Re: how to use LDT
Posted: Fri Jun 08, 2012 7:31 am
by ckzippo
amd64pager wrote:Also , try:
we can not use
according to the Intel Manual,
we should use
can not use an imm like $0x50
and this indeed cause error "operand type mismatch for lldt"
thank you for your advice.
