Page 1 of 1

Loading gdt

Posted: Sun Mar 22, 2020 6:57 pm
by zecarlos
I've read tutorials on GDT several times but I can't find the problem.
Right after loading the GDT in protected mode when updating the ss register the system resets and restarts. (Along with the BochsDBG report, are the values of the sregs records loaded in LoaderStage2 in real mode.)
Can someone please help fix this problem?
the code can be found at <https://github.com/zecarlos1957/learnos>
Please need help

Code: Select all

========================================================================
                       Bochs x86 Emulator 2.6.9
               Built from SVN snapshot on April 9, 2017
                  Compiled on Apr  9 2017 at 09:49:25
========================================================================
00000000000i[      ] reading configuration from bochsrc.txt
00000000000e[      ] bochsrc.txt:737: ataX-master/slave CHS set to 0/0/0 - autodetection enabled
00000000000i[      ] installing win32 module as the Bochs GUI
00000000000i[      ] using log file bochsout.txt
Next at t=0
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0
<bochs:1> lb 0x103e70
<bochs:2> c
(0) Breakpoint 1, 0x0000000000103e70 in ?? ()
Next at t=112966319
(0) [0x000000103e70] 0008:0000000000103e70 (unk. ctxt): mov eax, dword ptr ss:[e
sp+4] ; 8b442404
<bochs:3> sreg
es:0x0010, dh=0x00cf9300, dl=0x0000ffff, valid=1
        Data segment, base=0x00000000, limit=0xffffffff, Read/Write, Accessed
cs:0x0008, dh=0x00cf9b00, dl=0x0000ffff, valid=1
        Code segment, base=0x00000000, limit=0xffffffff, Execute/Read, Non-Confo
rming, Accessed, 32-bit
ss:0x0010, dh=0x00cf9300, dl=0x0000ffff, valid=31
        Data segment, base=0x00000000, limit=0xffffffff, Read/Write, Accessed
ds:0x0010, dh=0x00cf9300, dl=0x0000ffff, valid=31
        Data segment, base=0x00000000, limit=0xffffffff, Read/Write, Accessed
fs:0x07c0, dh=0x00009300, dl=0x7c00ffff, valid=1
        Data segment, base=0x00007c00, limit=0x0000ffff, Read/Write, Accessed
gs:0x07c0, dh=0x00009300, dl=0x7c00ffff, valid=1
        Data segment, base=0x00007c00, limit=0x0000ffff, Read/Write, Accessed
ldtr:0x0000, dh=0x00008200, dl=0x0000ffff, valid=1
tr:0x0000, dh=0x00008b00, dl=0x0000ffff, valid=1
gdtr:base=0x00000000000005f3, limit=0x17
idtr:base=0x0000000000000000, limit=0x3ff
 
-------------------------------------------------------------------------------------------------------------------------------------------------

(0) [0x000000103e70] 0008:0000000000103e70 (unk. ctxt): mov eax, dword ptr ss:[esp+4] ; 8b442404
<bochs:4> s
Next at t=112966320
(0) [0x000000103e74] 0008:0000000000103e74 (unk. ctxt): lgdt ds:[eax]  ; 0f0110
<bochs:5>
Next at t=112966321
(0) [0x000000103e77] 0008:0000000000103e77 (unk. ctxt): mov ax, 0x0010  ; 66b81000
<bochs:6>
Next at t=112966322
(0) [0x000000103e7b] 0008:0000000000103e7b (unk. ctxt): mov ds, ax  ; 8ed8
<bochs:7>
Next at t=112966323
(0) [0x000000103e7d] 0008:0000000000103e7d (unk. ctxt): mov es, ax  ; 8ec0
<bochs:8>
Next at t=112966324
(0) [0x000000103e7f] 0008:0000000000103e7f (unk. ctxt): mov fs, ax  ; 8ee0
<bochs:9>
Next at t=112966325
(0) [0x000000103e81] 0008:0000000000103e81 (unk. ctxt): mov gs, ax  ; 8ee8
<bochs:10>
Next at t=112966326
(0) [0x000000103e83] 0008:0000000000103e83 (unk. ctxt): mov ss, ax  ; 8ed0
<bochs:11>
(0).[112966326] [0x000000103e83] 0008:0000000000103e83 (unk. ctxt): mov ss, ax             ; 8ed0
Next at t=112966327
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0

Re: Loading gdt

Posted: Mon Mar 23, 2020 1:12 pm
by Octocontrabass
Use the Bochs debugger to print the contents of the GDT after your LGDT instruction. The command is "info gdt". Does it look correct to you? If you don't see any problems, post the debugger output here.

Re: Loading gdt

Posted: Mon Mar 23, 2020 4:52 pm
by zecarlos
Thanks for answering.
Well, something is wrong, everything has invalid address.
I have to check. thank you my friend.

funny! IDT has the same problem. It looks like add a shift to the left in addresses.
The correct idt base address is 0x10b020


the BochsDBG report:
error: IDTR+8*158 points to invalid linear address 0x00000000b0200000
bx_dbg_read_linear: physical memory read error (phy=0x0000b02004f8, lin=0x000000
00b02004f8)
error: IDTR+8*159 points to invalid linear address 0x00000000b0200000
bx_dbg_read_linear: physical memory read error (phy=0x0000b0200500, lin=0x000000

Re: Loading gdt

Posted: Tue Mar 24, 2020 4:58 am
by zecarlos
I think I understood the problem, but I don't know how to solve it.
This problem only happens when loading the GDT and IDT. Following the BochsDBG report, the instruction "lea eax, [ebp-16]" worked well, eax has the correct <descriptor> address.

Now, info idt 0 should show the IRQ0 timer handler address, which is 0x10b020. There is a stange left shift 16 here. How can this happen?

Code: Select all

========================================================================
                       Bochs x86 Emulator 2.6.9
               Built from SVN snapshot on April 9, 2017
                  Compiled on Apr  9 2017 at 09:49:25
========================================================================
Next at t=0
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0
<bochs:1> lb 0x1002b7
<bochs:2> c
(0) Breakpoint 1, 0x00000000001002b7 in ?? ()
Next at t=117210189
(0) [0x0000001002b7] 0008:00000000001002b7 (unk. ctxt): lea eax, dword ptr ss:[ebp-16] ; 8d45f0
<bochs:3> s
Next at t=117210190
(0) [0x0000001002ba] 0008:00000000001002ba (unk. ctxt): lidt ds:[eax]  ; 0f0118
<bochs:4> r
rax: 00000000_0008ff54 rcx: 00000000_001040b8
rdx: 00000000_00000001 rbx: 00000000_0000093a
rsp: 00000000_0008ff3c rbp: 00000000_0008ff64
rsi: 00000000_00009200 rdi: 00000000_00106200
r8 : 00000000_00000000 r9 : 00000000_00000000
r10: 00000000_00000000 r11: 00000000_00000000
r12: 00000000_00000000 r13: 00000000_00000000
r14: 00000000_00000000 r15: 00000000_00000000
rip: 00000000_001002ba
eflags 0x00000046: id vip vif ac vm rf nt IOPL=0 of df if tf sf ZF af PF cf
<bochs:5> s
Next at t=117210191
(0) [0x0000001002bd] 0008:00000000001002bd (unk. ctxt): nop  ; 90
<bochs:6>
Next at t=117210192
(0) [0x0000001002be] 0008:00000000001002be (unk. ctxt): leave  ; c9
<bochs:7> info idt 0
Interrupt Descriptor Table (base=0x00000000b0200000, limit=2048):
bx_dbg_read_linear: physical memory read error (phy=0x0000b0200000, lin=0x000000
00b0200000)
error: IDTR+8*0 points to invalid linear address 0x00000000b0200000
<bochs:8>

Re: Loading gdt

Posted: Tue Mar 24, 2020 7:58 am
by Gigasoft
Please check the manual on how to use the LGDT and LIDT instructions correctly.

Re: Loading gdt

Posted: Wed Mar 25, 2020 7:14 am
by zecarlos
Dear Sir
I made some progress in my investigation.
Yesterday I said there seemed to be a shift to the left, but it’s not true. It seems to be a problem of alignment of the descriptor structure that should occupy 2 + 4 bytes and actually occupies 8 bytes. Please check this BochsDBG report

Code: Select all

(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0
<bochs:1> lb 0x1002b7
<bochs:2> c
(0) Breakpoint 1, 0x00000000001002b7 in ?? ()
Next at t=99025317
(0) [0x0000001002b7] 0008:00000000001002b7 (unk. ctxt): mov eax, dword ptr ss:[ebp+8] ; 8b4508
<bochs:3> s
Next at t=99025318
(0) [0x0000001002ba] 0008:00000000001002ba (unk. ctxt): mov word ptr ss:[ebp-8], ax ; 668945f8
<bochs:4>
Next at t=99025319
(0) [0x0000001002be] 0008:00000000001002be (unk. ctxt): mov eax, dword ptr ss:[ebp+12] ; 8b450c
<bochs:5>
Next at t=99025320
(0) [0x0000001002c1] 0008:00000000001002c1 (unk. ctxt): mov dword ptr ss:[ebp-4], eax ; 8945fc
<bochs:6>
Next at t=99025321
(0) [0x0000001002c4] 0008:00000000001002c4 (unk. ctxt): lea eax, dword ptr ss:[ebp-8] ; 8d45f8
<bochs:7>
Next at t=99025322
(0) [0x0000001002c7] 0008:00000000001002c7 (unk. ctxt): lidt ds:[eax]
  ; 0f0118
<bochs:8> x /4hx 0x8ff5c
[bochs]:
0x000000000008ff5c <bogus+       0>:    0x0800  0x0010  0x6060  0x0010
<bochs:9>
In last line there is the data from descriptor structure.
The second word is the problem, shouldn't exist.
the correct format is 0x800 0x6060 0x0010

Can you help me fix this issue?
the code can be found at <https://github.com/zecarlos1957/learnos>

Re: Loading gdt

Posted: Thu Mar 26, 2020 12:46 am
by Octocontrabass
It looks like your compiler is ignoring the struct packing attribute. Try using a cross compiler.