Page 1 of 1

getting a variable's segment and offset address using gcc

Posted: Wed Apr 20, 2011 9:34 pm
by fatman2021
Does anyone knows how to get a variable's segment and offset address using c... I need the information for a VESA driver that I am writing and I will be using protected mode....

Re: getting a variable's segment and offset address using gc

Posted: Wed Apr 20, 2011 10:28 pm
by Solar
GCC assumes a flat memory model, so unless you've done something really tricky, your segment base will be zero, and the address of the variable is the offset.

Or did I miss something here?

Re: getting a variable's segment and offset address using gc

Posted: Thu Apr 21, 2011 12:34 am
by Combuster
VBE bios calls require real mode. GCC expects protected mode. Something seems wrong with your design.

Re: getting a variable's segment and offset address using gc

Posted: Thu Apr 21, 2011 12:42 am
by fatman2021
Combuster wrote:VBE bios calls require real mode. GCC expects protected mode. Something seems wrong with your design.
My OS is designed to switch to real mode every time it needs to to make a VBE BIOS call.

Re: getting a variable's segment and offset address using gc

Posted: Thu Apr 21, 2011 1:52 am
by Combuster
Which means the actual problem is not GCC, but translating from protected mode addresses to real mode addresses.

You do know how physical addresses are calculated in Real Mode, right? You do know how they are calculated in protected mode? Therefore you will be able to apply one equation to get a physical address, and then the other in reverse to get a segment:offset pair. A good math exercise for practice.

Re: getting a variable's segment and offset address using gc

Posted: Thu Apr 21, 2011 5:28 am
by fatman2021
no, because address conversion is taken care by the interrupt handler:

Code: Select all

[bits 32]



global int32, _int32



struc regs16_t

	.di	resw 1

	.si	resw 1

	.bp	resw 1

	.sp resw 1

	.bx	resw 1

	.dx	resw 1

	.cx	resw 1

	.ax	resw 1

	.gs	resw 1

	.fs	resw 1

	.es	resw 1

	.ds	resw 1

	.ef resw 1

endstruc



%define INT32_BASE                             0x7C00

%define REBASE(x)                              (((x) - reloc) + INT32_BASE)

%define GDTENTRY(x)                            ((x) << 3)

%define CODE32                                 GDTENTRY(1)	; 0x08

%define DATA32                                 GDTENTRY(2)	; 0x10

%define CODE16                                 GDTENTRY(3)	; 0x18

%define DATA16                                 GDTENTRY(4)	; 0x20

%define STACK16                                (INT32_BASE - regs16_t_size)





section .text

	int32: use32                               ; by Napalm

	_int32:

		cli                                    ; disable interrupts

		pusha                                  ; save register state to 32bit stack

		mov  esi, reloc                        ; set source to code below

		mov  edi, INT32_BASE                   ; set destination to new base address

		mov  ecx, (int32_end - reloc)          ; set copy size to our codes size

		cld                                    ; clear direction flag (so we copy forward)

		rep  movsb                             ; do the actual copy (relocate code to low 16bit space)

		jmp INT32_BASE                         ; jump to new code location

	reloc: use32                               ; by Napalm

		mov  [REBASE(stack32_ptr)], esp        ; save 32bit stack pointer

		sidt [REBASE(idt32_ptr)]               ; save 32bit idt pointer

		sgdt [REBASE(gdt32_ptr)]               ; save 32bit gdt pointer

		lgdt [REBASE(gdt16_ptr)]               ; load 16bit gdt pointer

		lea  esi, [esp+0x24]                   ; set position of intnum on 32bit stack

		lodsd                                  ; read intnum into eax

		mov  [REBASE(ib)], al                  ; set intrrupt immediate byte from our arguments 

		mov  esi, [esi]                        ; read regs pointer in esi as source

		mov  edi, STACK16                      ; set destination to 16bit stack

		mov  ecx, regs16_t_size                ; set copy size to our struct size

		mov  esp, edi                          ; save destination to as 16bit stack offset

		rep  movsb                             ; do the actual copy (32bit stack to 16bit stack)

		jmp  word CODE16:REBASE(p_mode16)      ; switch to 16bit selector (16bit protected mode)

	p_mode16: use16

		mov  ax, DATA16                        ; get our 16bit data selector

		mov  ds, ax                            ; set ds to 16bit selector

		mov  es, ax                            ; set es to 16bit selector

		mov  fs, ax                            ; set fs to 16bit selector

		mov  gs, ax                            ; set gs to 16bit selector

		mov  ss, ax                            ; set ss to 16bit selector

		mov  eax, cr0                          ; get cr0 so we can modify it

		and  al,  ~0x01                        ; mask off PE bit to turn off protected mode

		mov  cr0, eax                          ; set cr0 to result

		jmp  word 0x0000:REBASE(r_mode16)      ; finally set cs:ip to enter real-mode

	r_mode16: use16

		xor  ax, ax                            ; set ax to zero

		mov  ds, ax                            ; set ds so we can access idt16

		mov  ss, ax                            ; set ss so they the stack is valid

		lidt [REBASE(idt16_ptr)]               ; load 16bit idt

		mov  bx, 0x0870                        ; master 8 and slave 112

		call resetpic                          ; set pic's the to real-mode settings

		popa                                   ; load general purpose registers from 16bit stack

		pop  gs                                ; load gs from 16bit stack

		pop  fs                                ; load fs from 16bit stack

		pop  es                                ; load es from 16bit stack

		pop  ds                                ; load ds from 16bit stack

		sti                                    ; enable interrupts

		db 0xCD                                ; opcode of INT instruction with immediate byte

	ib: db 0x00

		cli                                    ; disable interrupts

		xor  sp, sp                            ; zero sp so we can reuse it

		mov  ss, sp                            ; set ss so the stack is valid

		mov  sp, INT32_BASE                    ; set correct stack position so we can copy back

		pushf                                  ; save eflags to 16bit stack

		push ds                                ; save ds to 16bit stack

		push es                                ; save es to 16bit stack

		push fs                                ; save fs to 16bit stack

		push gs                                ; save gs to 16bit stack

		pusha                                  ; save general purpose registers to 16bit stack

		mov  bx, 0x2028                        ; master 32 and slave 40

		call resetpic                          ; restore the pic's to protected mode settings

		mov  eax, cr0                          ; get cr0 so we can modify it

		inc  eax                               ; set PE bit to turn on protected mode

		mov  cr0, eax                          ; set cr0 to result

		jmp  dword CODE32:REBASE(p_mode32)     ; switch to 32bit selector (32bit protected mode)

	p_mode32: use32

		mov  ax, DATA32                        ; get our 32bit data selector

		mov  ds, ax                            ; reset ds selector

		mov  es, ax                            ; reset es selector

		mov  fs, ax                            ; reset fs selector

		mov  gs, ax                            ; reset gs selector

		mov  ss, ax                            ; reset ss selector

		lgdt [REBASE(gdt32_ptr)]               ; restore 32bit gdt pointer

		lidt [REBASE(idt32_ptr)]               ; restore 32bit idt pointer

		mov  esp, [REBASE(stack32_ptr)]        ; restore 32bit stack pointer

		mov  esi, STACK16                      ; set copy source to 16bit stack

		lea  edi, [esp+0x28]                   ; set position of regs pointer on 32bit stack

		mov  edi, [edi]                        ; use regs pointer in edi as copy destination

		mov  ecx, regs16_t_size                ; set copy size to our struct size

		cld                                    ; clear direction flag (so we copy forward)

		rep  movsb                             ; do the actual copy (16bit stack to 32bit stack)

		popa                                   ; restore registers

		sti                                    ; enable interrupts

		ret                                    ; return to caller

		

	resetpic:                                  ; reset's 8259 master and slave pic vectors

		push ax                                ; expects bh = master vector, bl = slave vector

		mov  al, 0x11                          ; 0x11 = ICW1_INIT | ICW1_ICW4

		out  0x20, al                          ; send ICW1 to master pic

		out  0xA0, al                          ; send ICW1 to slave pic

		mov  al, bh                            ; get master pic vector param

		out  0x21, al                          ; send ICW2 aka vector to master pic

		mov  al, bl                            ; get slave pic vector param

		out  0xA1, al                          ; send ICW2 aka vector to slave pic

		mov  al, 0x04                          ; 0x04 = set slave to IRQ2

		out  0x21, al                          ; send ICW3 to master pic

		shr  al, 1                             ; 0x02 = tell slave its on IRQ2 of master

		out  0xA1, al                          ; send ICW3 to slave pic

		shr  al, 1                             ; 0x01 = ICW4_8086

		out  0x21, al                          ; send ICW4 to master pic

		out  0xA1, al                          ; send ICW4 to slave pic

		pop  ax                                ; restore ax from stack

		ret                                    ; return to caller

		

	stack32_ptr:                               ; address in 32bit stack after we

		dd 0x00000000                          ;   save all general purpose registers

		

	idt32_ptr:                                 ; IDT table pointer for 32bit access

		dw 0x0000                              ; table limit (size)

		dd 0x00000000                          ; table base address

		

	gdt32_ptr:                                 ; GDT table pointer for 32bit access

		dw 0x0000                              ; table limit (size)

		dd 0x00000000                          ; table base address

		

	idt16_ptr:                                 ; IDT table pointer for 16bit access

		dw 0x03FF                              ; table limit (size)

		dd 0x00000000                          ; table base address

		

	gdt16_base:                                ; GDT descriptor table

		.null:                                 ; 0x00 - null segment descriptor

			dd 0x00000000                      ; must be left zero'd

			dd 0x00000000                      ; must be left zero'd

			

		.code32:                               ; 0x01 - 32bit code segment descriptor 0xFFFFFFFF

			dw 0xFFFF                          ; limit  0:15

			dw 0x0000                          ; base   0:15

			db 0x00                            ; base  16:23

			db 0x9A                            ; present, iopl/0, code, execute/read

			db 0xCF                            ; 4Kbyte granularity, 32bit selector; limit 16:19

			db 0x00                            ; base  24:31

			

		.data32:                               ; 0x02 - 32bit data segment descriptor 0xFFFFFFFF

			dw 0xFFFF                          ; limit  0:15

			dw 0x0000                          ; base   0:15

			db 0x00                            ; base  16:23

			db 0x92                            ; present, iopl/0, data, read/write

			db 0xCF                            ; 4Kbyte granularity, 32bit selector; limit 16:19

			db 0x00                            ; base  24:31

			

		.code16:                               ; 0x03 - 16bit code segment descriptor 0x000FFFFF

			dw 0xFFFF                          ; limit  0:15

			dw 0x0000                          ; base   0:15

			db 0x00                            ; base  16:23

			db 0x9A                            ; present, iopl/0, code, execute/read

			db 0x0F                            ; 1Byte granularity, 16bit selector; limit 16:19

			db 0x00                            ; base  24:31

			

		.data16:                               ; 0x04 - 16bit data segment descriptor 0x000FFFFF

			dw 0xFFFF                          ; limit  0:15

			dw 0x0000                          ; base   0:15

			db 0x00                            ; base  16:23

			db 0x92                            ; present, iopl/0, data, read/write

			db 0x0F                            ; 1Byte granularity, 16bit selector; limit 16:19

			db 0x00                            ; base  24:31

			

	gdt16_ptr:                                 ; GDT table pointer for 16bit access

		dw gdt16_ptr - gdt16_base - 1          ; table limit (size)

		dd gdt16_base                          ; table base address

		

	int32_end:                                 ; end marker (so we can copy the code)

	

	

How to pass a pointer to real mode

Posted: Thu Apr 21, 2011 9:44 am
by Combuster
no, because address conversion is taken care by the interrupt handler:
Well, no. The handler does not do any conversion, it fixes poor design and a lack of a linker script by manually offsetting all memory operands (which are fixed) to a different location than what the rest of the kernel is executed. The code you posted copies the 32-bit registers to the stack and then reinterprets them as 16-bit register file in real mode (which is a kind of hack since it reinterprets 8x32-bit registers as 8x16-bit registers and some 16-bit segment registers), so the value for es:bx or whatever pointer argument is going to be whatever argument you called the interrupt with. You'll have to define a position reachable by both real mode and protected mode code, and use that as the argument.

I also believe your question comes as a result of copying some code (a perfect match with coffee-os) without truly understanding it. Go back to your original problem: calling a real mode interrupt, and determine what the register contents should be without even considering the code needed to get to real mode and getting the values into the registers there.

Re: getting a variable's segment and offset address using gc

Posted: Fri Apr 22, 2011 2:16 am
by qw
After all, it's not that difficult. Basically:

Address = base address + offset

PM: base address = whatever is in the descriptor cache
RM: base address = segment address * 16

The base address in the descriptor cache matches the one in the descriptor table (if the latter is not changed) so you may fetch it from there.

Paging may complicate things a bit, because there is no paging in real mode. But you may use V86 mode instead.

Re: getting a variable's segment and offset address using gc

Posted: Sun Apr 24, 2011 3:19 pm
by Combuster
Hobbes wrote:PM: base address = whatever is in the descriptor cache
RM: base address = segment address * 16
Not completely. It's always the base value from the descriptor cache by design. The difference is that in PM, when you modify a segment register the descriptor cache will contain the base value stored in the relevant GDT entry, while in real mode, it will afterwards contains selector * 16.

Unless you are changing operating modes or GDT entries, it's often convenient to just ignore the descriptor cache as it is most of the time just another level of indirection. That makes the base address equal segment register * 16 in real mode, and gdt/ldt[...].base in protected mode.

Re: getting a variable's segment and offset address using gc

Posted: Tue Apr 26, 2011 5:41 am
by qw
EDIT:

Address = base address + offset

Base address = whatever is in the descriptor cache

Whatever is in the descriptor cache =
RM: segment address * 16
PM: fetched from the descriptor table (LDT or GDT and entry # according to the selector)
the moment a value was loaded into the segment register

Thanks, Combuster. I added the descriptor cache while my thoughts were already halfway.