Page 1 of 1

GDT issue

Posted: Thu Feb 28, 2008 4:22 am
by Jeko
I have written code for implementing gdt. But I thinked a thing... If I setup the gdt directly at boot-time? There can be any problem?

Code: Select all

.align 4
gdtr:
	.word (gdt_end - gdt - 1)	; // GDT limit.
	.long (gdt - ADDRADJUST)	; // GDT linear address.

.align 4
gdt:
; // Dummy descriptor 0x00.
	.word 0			; // Limit 15:0
	.word 0			; // Base 15:0
	.byte 0			; // Base 23:16
	.byte 0			; // Access byte (descriptor type)
	.byte 0			; // Limits 19:16, Flags
	.byte 0			; // Base 31:24

; // Data descriptor 0x08.
	.word 0xFFFF		; // Limit 15:0
	.word 0			; // Base 15:0
	.byte 0			; // Base 23:16
	.byte 0x92         	; // Data, Present, Writeable (1,0,0,1,0010)
	.byte 0xCF		; // G=1, D=1, 0, AVL=0, 1111=F: Limit/Length (1,1,0,0,1111)
	.byte 0			; // Base 31:24

; // Code descriptor 0x10.
	.word 0xFFFF		; // Limit 15:0
	.word 0			; // Base 15:0
	.byte 0			; // Base 23:16
	.byte 0x9A		; // Code, Present, Non-conforming, Exec/read (1,0,0,1,1110)
	.byte 0xCF		; // G=1, D=1, 0, AVL=0, 1111=F: Limit/Length (1,1,0,0,1111)
	.byte 0			; // Base 31:24
gdt_end:

; Reload global descriptor table (GDT).
lgdtl	(gdtr)

; Reflush CS register.
ljmp	$0x10, $1f

; Update segment registers.
movw	$0x08, %ax
movw	%ax, %ds
movw	%ax, %es
movw	%ax, %fs
movw	%ax, %gs
Of course, I must also set gdt for usermode.

Posted: Thu Feb 28, 2008 6:33 am
by pini
There shouldn't be any problem as long as your gdt and gdtr aren't erased in memory

Posted: Thu Feb 28, 2008 7:11 am
by Jeko
pini wrote:There shouldn't be any problem as long as your gdt and gdtr aren't erased in memory
After I won't need anything related to GDT?

Posted: Thu Feb 28, 2008 1:18 pm
by pini
As long as you're in protected mode, the cpu needs the gdt, so even if your kernel doesn't need it after init, you should still preserve it from being erased.

Posted: Fri Feb 29, 2008 3:27 am
by Jeko
pini wrote:As long as you're in protected mode, the cpu needs the gdt, so even if your kernel doesn't need it after init, you should still preserve it from being erased.
Ok. I won't erase it. It's needed for CPU.

But will it be needed by the kernel? For what?

Posted: Fri Feb 29, 2008 4:30 am
by JamesM
But will it be needed by the kernel? For what?
It's essential for the running of the CPU. The kernel needs it because the kernel needs a functioning CPU. :roll:

Posted: Fri Feb 29, 2008 6:26 am
by Jeko
JamesM wrote:
But will it be needed by the kernel? For what?
It's essential for the running of the CPU. The kernel needs it because the kernel needs a functioning CPU. :roll:
ok. I already knew this. I'm asking if the gdt is setup a time and after I won't change anything related to it, or if the gdt is setup and after is changed for something

Posted: Fri Feb 29, 2008 6:28 am
by AJ
After the GDT is set up initially, you may have to change it to add TSS descriptors. Even with software multitasking, you will need one descriptor per logical core.

Cheers,
Adam

Posted: Fri Feb 29, 2008 6:35 am
by Jeko
AJ wrote:After the GDT is set up initially, you may have to change it to add TSS descriptors. Even with software multitasking, you will need one descriptor per logical core.

Cheers,
Adam
Can't I setup tss descriptor initially with the others selectors?

Posted: Fri Feb 29, 2008 6:48 am
by AJ
Yes, if you like. I set up my initial OS fairly early on (before soing any SMP stuff), but it's entirely up to you when you add TSS descriptors.

Cheers,
Adam

Posted: Fri Feb 29, 2008 7:22 am
by Jeko
AJ wrote:Yes, if you like. I set up my initial OS fairly early on (before soing any SMP stuff), but it's entirely up to you when you add TSS descriptors.

Cheers,
Adam
Will I need a TSS for each core? Because if I need a TSS for each core I must do SMP checks before setting TSS selectors.

However, have you any guide to SMP and multiprocessor?

Posted: Fri Feb 29, 2008 8:05 am
by AJ
Theres a very good guide over on osdever.net called something liek "Multiprocessing for hobby os's explained...", IIRC. It will give you all the basics. It does, however rely on MP tables (which I think are outdated now - although I use this in my OS) instead of ACPI.

You need a separate TSS for each core so that you can have separate kernel interrupt stacks for each core.

Cheers,
Adam