The xv6-rev7 (JOS) GDT problem

85 views
Skip to first unread message

Gapry Un

unread,
Apr 1, 2013, 11:31:08 AM4/1/13
to julu...@googlegroups.com
Dear all,

It's very difficult for me to understand GDT (Global Descriptor Table) in JOS (xv6-rev7)

For example 

.word (((lim) >> 12) & 0xffff), ((base) & 0xffff); 

Why shift right 12?  Why AND 0xffff?
What do these number mean?
What does the formula mean? 

Can anyone give me some resources or tutorials or hints?

Here, It's two parts of snippet code as following for my problem.

1st Part
--------------------------------------------------------------------------
0654 #define SEG_NULLASM \
0655 .word 0, 0; \
0656 .byte 0, 0, 0, 0
0657
0658 // The 0xC0 means the limit is in 4096−byte units
0659 // and (for executable segments) 32−bit mode.
0660 #define SEG_ASM(type,base,lim) \
0661 .word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \
0662 .byte (((base) >> 16) & 0xff), (0x90 | (type)), \
0663 (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff)
0664
0665 #define STA_X 0x8 // Executable segment
0666 #define STA_E 0x4 // Expand down (non−executable segments)
0667 #define STA_C 0x4 // Conforming code segment (executable only)
0668 #define STA_W 0x2 // Writeable (non−executable segments)
0669 #define STA_R 0x2 // Readable (executable segments)
0670 #define STA_A 0x1 // Accessed

2nd Part
--------------------------------------------------------------------------
8480 # Bootstrap GDT
8481 .p2align 2 # force 4 byte alignment
8482 gdt:
8483 SEG_NULLASM # null seg
8484 SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff) # code seg
8485 SEG_ASM(STA_W, 0x0, 0xffffffff) # data seg
8486
8487 gdtdesc:
8488 .word (gdtdesc − gdt − 1) # sizeof(gdt) − 1
8489 .long gdt # address gdt
--------------------------------------------------------------------------


Best regards,

gapry

Hawx Chen

unread,
Apr 1, 2013, 8:42:17 PM4/1/13
to julu...@googlegroups.com
Everything you asked is in SPEC.





2013/4/1 Gapry Un <gap...@gmail.com>

--
您已訂閱「Google 網上論壇」的「juluOSDev-星系統社群」群組,因此我們特別傳送這封郵件通知您。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 juluosdev+...@googlegroups.com
請前往以下網址造訪這個群組:http://groups.google.com/group/juluosdev?hl=zh-TW。
如需更多選項,請前往:https://groups.google.com/groups/opt_out。
 
 



--
Best Regards
English name: Hawx Chen 
Taiwanese name: 陳膺正  

Gapry Un

unread,
Apr 2, 2013, 12:08:21 PM4/2/13
to julu...@googlegroups.com
Thank you for your hint. 

I still don't understand lim >> 12 as I've read it.

Why need to shift right 12 ? 

sung descent

unread,
Apr 3, 2013, 2:39:00 AM4/3/13
to julu...@googlegroups.com
Hi Gapry,

limit is only 20bits, but SEG_ASM limit argument can pass 0xffffffff, so shift 12bits let limit length is in 20bits.

The macro I used is from a book, for your reference.
.macro Descriptor Base, Limit, Attr
    .2byte  \Limit & 0xFFFF
    .2byte  \Base & 0xFFFF
    .byte   (\Base >> 16) & 0xFF
    .2byte  ((\Limit >> 8) & 0xF00) | (\Attr & 0xF0FF)
    .byte   (\Base >> 24) & 0xFF
.endm

usage:

base: 0
limit: 0xffffffff
gdt2: Descriptor  0, 0xfffff, DA_DRW | DA_32 | DA_LIMIT_4K






2013/4/1 Gapry Un <gap...@gmail.com>

--
您已訂閱「Google 網上論壇」的「juluOSDev- 星系統社群」群組,因此我們特別傳送這封郵件通知您。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 juluosdev+...@googlegroups.com

請前往以下網址造訪這個群組:http://groups.google.com/group/juluosdev?hl=zh-TW。
如需更多選項,請前往:https://groups.google.com/groups/opt_out。
 
 



--
Best Regards
English name: Hawx Chen 
Taiwanese name: 陳膺正  

--
您已訂閱「Google 網上論壇」的「juluOSDev- 星系統社群」群組,因此我們特別傳送這封郵件通知您。
Reply all
Reply to author
Forward
0 new messages