Page 1 of 1

OSdev: GDT table

Posted: Sat Oct 19, 2024 3:53 pm
by derva
Hello guys, I'm just few days into OS development and so far I found a way to tackle every challenge, but this one I'm not sure what to do?

So, I'm created some simple OS, print "Hello world" message, on it boot it etc (start stuff)

Next step is Global Descriptor Table
https://wiki.osdev.org/Global_Descriptor_Table
https://wiki.osdev.org/GDT_Tutorial#Wha ... t_In_a_GDT

I get GDT struct and make something

gdt.h

Code: Select all


#include<stdint.h>

struct gdt_segment {
	uint16_t limit_low;
	uint16_t base_low;
	uint8_t base_middle;
	uint8_t access_byte;
	uint8_t limit_high;
	uint8_t flags;
	uint8_t base_high;
} __attribute__((packed));

struct gdt_ptr_struct {
	uint16_t limit;
	//unsigned int base;
	struct gdt_segment*  base;
} __attribute__((packed));

struct gdt_flag{};

void initSegment(uint8_t num, uint32_t limit, uint32_t base, uint8_t access, uint8_t flags);
void initGdt();

extern void gdt_flush(void* gdt_ptr);
gdt.c

Code: Select all

#include "gdt.h"

struct gdt_segment gdt_entries[5];
struct gdt_ptr_struct gdt_ptr;

void initSegment(uint8_t num, uint32_t limit, uint32_t base, uint8_t access, uint8_t flags) {
	gdt_ptr.limit = (sizeof(struct gdt_segment) * 5) - 1;
	gdt_ptr.base = gdt_entries;
	//gdt_entries[num]
	gdt_entries[num].limit_low = limit & 0xFFFF;
	gdt_entries[num].limit_high = (limit >> 16) & 0x0F;
	gdt_entries[num].base_low = base & 0xFFFF;
	gdt_entries[num].base_middle = (base >> 16) & 0xFF;
	gdt_entries[num].base_high = (base >> 8 ) & 0xFF;
	gdt_entries[num].access_byte = access;
	gdt_entries[num].flags = flags;

	gdt_flush(&gdt_ptr);

}

void initGdt() {
	initSegment(0, 0, 0, 0, 0);
	initSegment(1, 0xFFFFFFFF, 0, 0x9A, 0xCF); //kernel code
	initSegment(2, 0xFFFFFFFF, 0, 0x92, 0xCF); //kernel data segment
	initSegment(3, 0xFFFFFFFF, 0, 0xFA, 0xCF); //user code
	initSegment(4, 0xFFFFFFFF, 0, 0xF2, 0xCF); //user code

}
but i'm having trouble with my gdt.s (assembler code) not sure what should i do here. I know I need to load my GDT into memory, but i don't know

when i try to as my gdt.c

Code: Select all

test@test: ~/opt/cross/bin/i686-elf-as gdt.s 
gdt.s: Assembler messages:
gdt.s:1: Error: no such instruction: `gdtr DW 0'
gdt.s:2: Error: no such instruction: `dd 0'
gdt.s:5: Error: operand size mismatch for `mov'
gdt.s:6: Error: operand size mismatch for `mov'
gdt.s:7: Error: operand size mismatch for `mov'
gdt.s:8: Error: operand size mismatch for `add'
gdt.s:9: Error: operand size mismatch for `mov'
I found this http://175.107.63.117/sic/uploads/Chapter%2002-MAL.pdf
but I don't know, any hints would be appreaciated :)

Thanks,
derva

Re: OSdev: GDT table

Posted: Sat Oct 19, 2024 6:01 pm
by Octocontrabass
derva wrote: Sat Oct 19, 2024 3:53 pm

Code: Select all

struct gdt_segment {
Unrelated to your current problem, this struct is nine bytes long when it needs to be eight bytes long.
derva wrote: Sat Oct 19, 2024 3:53 pm

Code: Select all

gdt.s:1: Error: no such instruction: `gdtr DW 0'
That code was written for a different assembler. If you want to use GAS, you need to rewrite it using the syntax and directives that GAS understands.