flat assembler
Message board for the users of flat assembler.

Index > OS Construction > GDT question

Author
Thread Post new topic Reply to topic
christiandy



Joined: 03 Mar 2011
Posts: 25
Location: 101
christiandy 25 Jun 2011, 07:44
Hi there,
I've mad my GDT. there are 6 entry null descriptor, linear , system code, system data, real data, real code, user code, user data
Code:
; null descriptor
gdt: dw 0                    ; limit 15:0
    dw 0                    ; base 15:0
     db 0                    ; base 23:16
    db 0                    ; type
      db 0                    ; limit 19:16, flags
    db 0                    ; base 31:24
; linear data segment descriptor
LINEAR_SEL      equ     $-gdt
       dw 0xFFFF               ; limit 0xFFFFF
     dw 0                    ; base for this one is always 0
     db 0
        db 0x92                 ; present, ring 0, data, expand-up, writable
        db 0xCF                 ; page-granular, 32-bit
     db 0
; code segment descriptor
SYS_CODE_SEL       equ     $-gdt
gdt2:      dw 0xFFFF
   dw 0                    ; (base gets set above)
     db 0
        db 0x9A                 ; present, ring 0, code, non-conforming, readable
   db 0xCF
     db 0
; data segment descriptor
SYS_DATA_SEL       equ     $-gdt
gdt3:      dw 0xFFFF
   dw 0                    ; (base gets set above)
     db 0
        db 0x92                 ; present, ring 0, data, expand-up, writable
        db 0xCF
     db 0
; code segment descriptor
REAL_CODE_SEL      equ     $-gdt
gdt4:      dw 0xFFFF
   dw 0                    ; (base gets set above)
     db 0
        db 0x9A                 ; present, ring 0, code, non-conforming, readable
   db 0                    ; byte-granular, 16-bit
     db 0
; data segment descriptor that is 'appropriate' for real mode
; (16-bit, limit=0xFFFF)
REAL_DATA_SEL    equ     $-gdt
gdt5:      dw 0xFFFF
   dw 0                    ; (base gets set above)
     db 0
        db 0x92                 ; present, ring 0, data, expand-up, writable
        db 0                    ; byte-granular, 16-bit
     db 0
USER_CODE_SEL   equ     $-gdt+3
gdt6:    dw 0xFFFF
   dw 0                    ; (base gets set above)
     db 0
        db 0xFA                 ; present, ring 3, code, non-conforming, readable
   db 0xCF
     db 0
; data segment descriptor
USER_DATA_SEL      equ     $-gdt+3
gdt7:    dw 0xFFFF
   dw 0                    ; (base gets set above)
     db 0
        db 0xF2                 ; present, ring 3, data, expand-up, writable
        db 0xCF
     db 0
; user TSS
USER_TSS  equ     $-gdt
gdt8:      dw 103
      dw 0                    ; set to tss
        db 0
        db 0xE9                 ; present, ring 3, 32-bit available TSS
     db 0
        db 0
gdt_end:    


I'm confuse about calculating the base address. I use same base address for sys code, sys data, user data, user code, real code, and real data.
Code:
shl ebx,4            ; EBX=segment << 4
    lea eax,[ebx]           ; =linear address of segment base
   mov [gdt2 + 2],ax
   mov [gdt3 + 2],ax
   mov [gdt4 + 2],ax
   mov [gdt5 + 2],ax
   shr eax,16
  mov [gdt2 + 4],al
   mov [gdt3 + 4],al
   mov [gdt4 + 4],al
   mov [gdt5 + 4],al
   mov [gdt2 + 7],ah
   mov [gdt3 + 7],ah
   mov [gdt4 + 7],ah
   mov [gdt5 + 7],ah
    

But my concern is, the memory overlaping if i running an application program, because user code and data have a same base address with sys code and data. will it happen?

Thanks,
Best regard.
Post 25 Jun 2011, 07:44
View user's profile Send private message AIM Address Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 25 Jun 2011, 09:45
Be careful. In fasm the equ directive generates symbolic constants! You can use = or label instead of it. For example, look at my code:
Code:
macro set name,value { label name at value }
...

GDT:
  dq 0
  ...
  set KCODE,$-GDT
  desc 0,0FFFFFh,DF_CODE32
  ...
  set GDT_SIZE,$-GDT
    


What is the reason to have "real code" and "real data"? In modern systems (on monolithic kernels) higher half kernel with FLAT memory model and page level protection is used. So only virtual (linear) addresses are used that coincide with offsets in all code/data segments. Additionally kernel could be protected by limitation of user code/data segment size. For example, look at my code:
Code:
  set KSTART_TINDEX,512 ; 768
  set KSTART_PINDEX,KSTART_TINDEX shl 10
  ...

  set ACODE,$-GDT+SF_ARPL
  desc 0,KSTART_PINDEX-1,DF_CODE32+DF_APL
    

If you place 16-bit (RM) initialization code within first 64 kb of memory space you can use only linear addresses.
Post 25 Jun 2011, 09:45
View user's profile Send private message Reply with quote
christiandy



Joined: 03 Mar 2011
Posts: 25
Location: 101
christiandy 25 Jun 2011, 12:01
i think real code an real data is use when i need to use BIOS interrupt. Anw it's okay to set user and system descriptor in the same base address?
Post 25 Jun 2011, 12:01
View user's profile Send private message AIM Address Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 25 Jun 2011, 13:55
Yes.
Post 25 Jun 2011, 13:55
View user's profile Send private message Reply with quote
christiandy



Joined: 03 Mar 2011
Posts: 25
Location: 101
christiandy 25 Jun 2011, 17:33
will the user and system descriptor will overlaping memory each other?
Post 25 Jun 2011, 17:33
View user's profile Send private message AIM Address Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 25 Jun 2011, 18:33
Usually yes but not necessarily. Look at my code attentively. Kernel descriptor (assigned with KCODE selector) describes both user space and kernel space. But user descriptor (assigned with ACODE selector) describes only user space.
Post 25 Jun 2011, 18:33
View user's profile Send private message Reply with quote
christiandy



Joined: 03 Mar 2011
Posts: 25
Location: 101
christiandy 25 Jun 2011, 18:51
what if, my system data's base address is 50H. if i access sys data:9ffb0H which is if I calculate that it refer to A0000H (CMIIW). does it will access a video memory which is locate on A0000H? If that so is it better for me to set a base address on memory > 1MB? sorry I slow in learning
Post 25 Jun 2011, 18:51
View user's profile Send private message AIM Address Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 26 Jun 2011, 09:41
Yes but it's old stuff. As I said above modern practice is to use FLAT segments and paging. For example, I have videobuffer physically located at 0xA0000 somewhere within kernel space started at 0x80000000 (0xC0000000). Base address of all code/data segments should be 0.
Post 26 Jun 2011, 09:41
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.