The xv6-rev7 (JOS) GDT problem

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.
Post Reply
gapry
Posts: 10
Joined: Sat Aug 25, 2012 7:42 pm

The xv6-rev7 (JOS) GDT problem

Post by gapry »

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
--------------------------------------------------------------------------

The complete part: http://pdos.csail.mit.edu/6.828/2012/xv6/xv6-rev7.pdf
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: The xv6-rev7 (JOS) GDT problem

Post by iansjack »

The Intel (or AMD) programmer's reference manuals explain the format of descriptor table entries.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: The xv6-rev7 (JOS) GDT problem

Post by Kazinsal »

Post Reply