GDT issue

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
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

GDT issue

Post 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.
User avatar
pini
Member
Member
Posts: 26
Joined: Wed Oct 18, 2006 5:12 am
Location: Nancy, France

Post by pini »

There shouldn't be any problem as long as your gdt and gdtr aren't erased in memory
Don't think you can. Know you can.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post 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?
User avatar
pini
Member
Member
Posts: 26
Joined: Wed Oct 18, 2006 5:12 am
Location: Nancy, France

Post 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.
Don't think you can. Know you can.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post 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?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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:
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post 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?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post 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?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

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