gcc compiler warningS
Posted: Sat May 06, 2023 5:55 pm
Hello all,
I have to say thank you for your work. It is a great help to have a way to follow to learning operating system.
I also have to apologize as i'm french, my english should be a little weird
This is my first post, i will hard try so, i think not the last one.
I hope that my first question isn't so stupid... i have some warning that i suppose are relevant in classic user development but not in kernel mode development.
i have found a piece of code on the xv6 MIT project that look like :
my compiler said :
kernel/gdt.c:42:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pd[1] = (uint16_t)g; // 32 bit base
^
kernel/gdt.c:43:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pd[2] = (uint16_t)g >> 16; // 32 bit base
also i have found that code :
that i have rework as
My compiler say :
kernel/gdt.c:54:22: note: in expansion of macro ‘SET_GDT’
gdt[SEG_UDATA] = SET_GDT(0, 0xffffffff, STA_W + (1 << 4) + (DPL_USER << 5) ,0b0011);
I don"t understand what is about.
Bref, i have lot of warning that i don't know how to fix i that look to not appear a lot after some research on the web.
-------------------------------------------------
I have to say thank you for your work. It is a great help to have a way to follow to learning operating system.
I also have to apologize as i'm french, my english should be a little weird
This is my first post, i will hard try so, i think not the last one.
I hope that my first question isn't so stupid... i have some warning that i suppose are relevant in classic user development but not in kernel mode development.
i have found a piece of code on the xv6 MIT project that look like :
Code: Select all
void lgdt(struct Gdt *g, int size)
{
volatile uint16_t pd[3];
pd[0] = size-1; // 16 bit limit
pd[1] = (uint16_t)g; // 32 bit base
pd[2] = (uint16_t)g >> 16; // 32 bit base
asm volatile("lgdt (%0)" : : "r" (pd));
}
kernel/gdt.c:42:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pd[1] = (uint16_t)g; // 32 bit base
^
kernel/gdt.c:43:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pd[2] = (uint16_t)g >> 16; // 32 bit base
also i have found that code :
Code: Select all
#define SEG(type, base, lim, dpl) (struct segdesc) \
{ ((lim) >> 12) & 0xffff, (uint)(base) & 0xffff, \
((uint)(base) >> 16) & 0xff, type, 1, dpl, 1, \
(uint)(lim) >> 28, 0, 0, 1, 1, (uint)(base) >> 24 }
Code: Select all
#define SET_GDT(base, limit, accessByte, flag) (struct Gdt){ \
limit & 0xffff, \
base & 0xffffff, \
accessByte & 0xff, \
(limit >> 16) & 0xf, \
flag & 0xff, \
(base >> 24) & 0xff \
}
kernel/gdt.c:54:22: note: in expansion of macro ‘SET_GDT’
gdt[SEG_UDATA] = SET_GDT(0, 0xffffffff, STA_W + (1 << 4) + (DPL_USER << 5) ,0b0011);
I don"t understand what is about.
Bref, i have lot of warning that i don't know how to fix i that look to not appear a lot after some research on the web.
-------------------------------------------------
Code: Select all
#include <kernel/tty.h>
#include <kernel/gdt.h>
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define DPL_USER 0x3 // User DPL
// Application segment type bits
#define STA_X 0x8 // Executable segment
#define STA_W 0x2 // Writeable (non-executable segments)
#define STA_R 0x2 // Readable (executable segments)
// System segment type bits
#define STS_T32A 0x9 // Available 32-bit TSS
#define STS_IG32 0xE // 32-bit Interrupt Gate
#define STS_TG32 0xF // 32-bit Trap Gate
// various segment selectors.
#define SEG_KCODE 1 // kernel code
#define SEG_KDATA 2 // kernel data+stack
#define SEG_UCODE 3 // user code
#define SEG_UDATA 4 // user data+stack
#define SEG_TSS 5 // this process's task state
#define SET_GDT(base, limit, accessByte, flag) (struct Gdt){ \
limit & 0xffff, \
base & 0xffffff, \
accessByte & 0xff, \
(limit >> 16) & 0xf, \
flag & 0xff, \
(base >> 24) & 0xff \
}
void lgdt(struct Gdt *g, int size)
{
volatile uint16_t pd[3];
pd[0] = size-1; // 16 bit limit
pd[1] = (uint16_t)g; // 32 bit base
pd[2] = (uint16_t)g >> 16; // 32 bit base
asm volatile("lgdt (%0)" : : "r" (pd));
}
void createGdt(){
struct Gdt* gdt;
gdt[SEG_KCODE] = SET_GDT(0, 0xffffffff, (STA_X|STA_R) + (1 << 4) + (0 << 5) ,0b0011);
gdt[SEG_KDATA] = SET_GDT(0, 0xffffffff, STA_W + (1 << 4) + (0 << 5) ,0b0011);
gdt[SEG_UCODE] = SET_GDT(0, 0xffffffff, (STA_X|STA_R) + (1 << 4) + (DPL_USER << 5) ,0b0011);
gdt[SEG_UDATA] = SET_GDT(0, 0xffffffff, STA_W + (1 << 4) + (DPL_USER << 5) ,0b0011);
lgdt(gdt, sizeof(gdt));
}
Code: Select all
#ifndef GDT
#define GDT
struct Gdt
{
unsigned int limit:16;
unsigned int base:24;
unsigned int accessByte:8;
unsigned int limit2:4;
unsigned int flags:4;
unsigned int base2:8;
};
void createGdt();
#endif
Code: Select all
#include <stdio.h>
#include <kernel/tty.h>
#include <kernel/gdt.h>
void kernel_main(void) {
terminal_initialize();
printf("Hello, kernel World!\n");
createGdt();
}
kernel/gdt.c: In function ‘lgdt’:
kernel/gdt.c:42:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pd[1] = (uint16_t)g; // 32 bit base
^
kernel/gdt.c:43:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pd[2] = (uint16_t)g >> 16; // 32 bit base
^
kernel/gdt.c: In function ‘createGdt’:
kernel/gdt.c:31:14: warning: suggest parentheses around ‘+’ in operand of ‘&’ [-Wparentheses]
accessByte & 0xff, \
^
kernel/gdt.c:51:22: note: in expansion of macro ‘SET_GDT’
gdt[SEG_KCODE] = SET_GDT(0, 0xffffffff, (STA_X|STA_R) + (1 << 4) + (0 << 5) ,0b0011);
^~~~~~~
kernel/gdt.c:31:14: warning: suggest parentheses around ‘+’ in operand of ‘&’ [-Wparentheses]
accessByte & 0xff, \
^
kernel/gdt.c:52:22: note: in expansion of macro ‘SET_GDT’
gdt[SEG_KDATA] = SET_GDT(0, 0xffffffff, STA_W + (1 << 4) + (0 << 5) ,0b0011);
^~~~~~~
kernel/gdt.c:31:14: warning: suggest parentheses around ‘+’ in operand of ‘&’ [-Wparentheses]
accessByte & 0xff, \
^
kernel/gdt.c:53:22: note: in expansion of macro ‘SET_GDT’
gdt[SEG_UCODE] = SET_GDT(0, 0xffffffff, (STA_X|STA_R) + (1 << 4) + (DPL_USER << 5) ,0b0011);
^~~~~~~
kernel/gdt.c:31:14: warning: suggest parentheses around ‘+’ in operand of ‘&’ [-Wparentheses]
accessByte & 0xff, \
^
kernel/gdt.c:54:22: note: in expansion of macro ‘SET_GDT’
gdt[SEG_UDATA] = SET_GDT(0, 0xffffffff, STA_W + (1 << 4) + (DPL_USER << 5) ,0b0011);
^~~~~~~
kernel/gdt.c:51:8: warning: ‘gdt’ is used uninitialized in this function [-Wuninitialized]
gdt[SEG_KCODE] = SET_GDT(0, 0xffffffff, (STA_X|STA_R) + (1 << 4) + (0 << 5) ,0b0011);