0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslで、最小のカーネルをコンパイル その23

Posted at

概要

wslで、kernel.elfのコンパイル、やってみた。
GDTを作りたい。

GDTとは。

サンプルコード

#include <stdio.h>


#define D_LDT   0x200   /* LDT segment        */
#define D_TASK  0x500   /* Task gate          */
#define D_TSS   0x900   /* TSS                */
#define D_CALL  0x0C00  /* 386 call gate      */
#define D_INT   0x0E00  /* 386 interrupt gate */
#define D_TRAP  0x0F00  /* 386 trap gate      */
#define D_DATA  0x1000  /* Data segment       */
#define D_CODE  0x1800  /* Code segment       */
#define D_DPL3         0x6000   /* DPL3 or mask for DPL                    */
#define D_DPL2         0x4000   /* DPL2 or mask for DPL                    */
#define D_DPL1         0x2000   /* DPL1 or mask for DPL                    */
#define D_PRESENT      0x8000   /* Present                                 */
#define D_NOT_PRESENT  0x8000   /* Not Present                             */
#define D_ACC      0x100  /* Accessed (Data or Code)          */
#define D_WRITE    0x200  /* Writable (Data segments only)    */
#define D_READ     0x200  /* Readable (Code segments only)    */
#define D_BUSY     0x200  /* Busy (TSS only)                  */
#define D_EXDOWN   0x400  /* Expand down (Data segments only) */
#define D_CONFORM  0x400  /* Conforming (Code segments only)  */
#define D_BIG      0x40	  /* Default to 32 bit mode           */
#define D_BIG_LIM  0x80	  /* Limit is in 4K units             */
struct x86_desc {
   unsigned short limit_low;     /* limit 0..15    */
   unsigned short base_low;      /* base  0..15    */
   unsigned char base_med;       /* base  16..23   */
   unsigned char access;         /* access byte    */
   unsigned int limit_high:4;    /* limit 16..19   */
   unsigned int granularity:4;   /* granularity    */
   unsigned char base_high;      /* base 24..31    */
} __attribute__ ((packed));
struct x86_gate {
   unsigned short offset_low;   /* offset 0..15    */
   unsigned short selector;     /* selector        */	
   unsigned short access;       /* access flags    */
   unsigned short offset_high;  /* offset 16..31   */
} __attribute__ ((packed));
union DT_entry {
   struct x86_desc desc;     /* Normal descriptor */
   struct x86_gate gate;     /* Gate descriptor   */
   unsigned long long dummy; /* Any other info    */
};
#define desc_table(name, length)    union DT_entry name[length] = 
#define stnd_desc(base, limit, control)     {desc: {(limit & 0xffff), (base & 0xffff), ((base >> 16) & 0xff), ((control+D_PRESENT) >> 8), (limit >> 16), ((control & 0xff) >> 4), (base >> 24)}}
desc_table(GDT, 3) {
	{dummy:0},
	stnd_desc(0, 0xFFFFF, (D_CODE + D_READ + D_BIG + D_BIG_LIM)),
	stnd_desc(0, 0xFFFFF, (D_DATA + D_WRITE + D_BIG + D_BIG_LIM)),
};
struct {
	unsigned short limit __attribute__ ((packed));
	union DT_entry *idt __attribute__ ((packed));
} loadgdt = { (3 * sizeof(union DT_entry) - 1), GDT };

int main(void){
    
    printf("ok");
}



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?