⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dscr.h

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 H
字号:
/***************************************************************************
**     File name   : dscr.h
**     Author      : x.cheng
**     Create date :
**
**	  Comments:
**		描述符的相关定义...
**	
**     Revisions:
**     $Log: dscr.h,v $
**     Revision 1.2  2005/08/02 15:33:33  x.cheng
**     for update segment regs, new funciton
**
**     Revision 1.1.1.1  2005/07/27 06:53:15  x.cheng
**     add into repositories
**
**
***************************************************************************/
#ifndef __DSCR_H__
#define __DSCR_H__

#include "const.h"

/**********selector defined here*************/
//! This is the code selector for the kernel.
#define KERNEL_CODE		0x08
//! This is the data selector for the kernel.
//! REMEBER!!! It is defined also into kernel/drv/inc/def_traps.h!
#define KERNEL_DATA		0x10
//! This is the stack selector for the kernel.
#define KERNEL_STACK	0x10

//! This is the selector for the user code segment.
#define USER_CODE		0x18 
//! This is the selector for the user data segment.
#define USER_DATA		0x20
//! This is the selector for the user stack segment.
#define USER_STACK		0x20
/*********************************************/
/*在head.asm中,GDT项目的最多个数是256个  */
#define GDTE_MAX_SIZE	256
/*一个 GDT项目占8个字节   */
#define GDT_ENTRY_SIZE	8

/***segment access byte's flag***/
#define ACS_PRESENT		0x80	/* presend segment */
#define ACS_CSEG		0x18	/* code segment */
#define ACS_DSEG		0x10	/* data segment */
#define ACS_CONFORM		0x04	/* conforming segment */
#define ACS_READABLE	0x02	/* readable segment */
#define ACS_WRITEABLE	0x02	/* writeable segment */

/***ready-made values***/
#define ACS_CODESEG		(ACS_PRESENT | ACS_CSEG | ACS_READABLE)
#define ACS_DATASEG		(ACS_PRESENT | ACS_DSEG | ACS_WRITEABLE)
#define ACS_STACKSEG	(ACS_PRESENT | ACS_DSEG | ACS_WRITEABLE)
#define ACS_TSSSEG		(ACS_PRESENT | 0x09)	//A standard TSS segment.
#define ACS_REALSEG		(ACS_CSEG | ACS_PRESENT | ACS_READABLE)	//A real-mode area segment.
 
/***************************/
#define GDT_NUL		0
#define GDT_CODE	1
#define GDT_DATA	2
#define GDT_TMP		3

#define LDT_NUL		0
#define LDT_CODE	1
#define LDT_DATA	2

#pragma	pack (push, 1)		/* align structors to a byte boundary */

/***segment descriptor definition***/
typedef struct Descritptor_Struct{
	unsigned short	uiLimit,
					uiBaseLO;
	unsigned char	ucBaseMI,
					ucAccess,
					ucAttribs,
					ucBaseHI;
} __attribute__ ((packed)) ts_SegDescriptor;

/***GDTR registor definition***/
typedef struct Gdtr_Struct{
	unsigned short uiLimit;			/* 2 bytes */
	unsigned long  ulBase;			/* 4 bytes */
} __attribute__ ((packed)) ts_GDTR;

#pragma pack (pop)		/* align structors to default boudary */

//! Update segment registers with kernel selectors.
static inline void UpdateSegmentRegs()
{
	__asm__ __volatile__(
		"ljmp %0, $1f\n"
		"nop\n"
		"1: mov %1, %%ds\n"
		"mov %1, %%es\n"
		"mov %1, %%ss\n"
		"nop\n"
		"mov %1, %%fs\n"
		"mov %1, %%gs\n"
		: : "i"(KERNEL_CODE), "r"(KERNEL_DATA) );
}

/**************************************
 * the following is refencing to
 * k_init.asm
 **************************************/
extern ts_SegDescriptor astIDT[256];
extern ts_SegDescriptor astGDT[256];
extern unsigned long aulPageDir[1024];

/* static inline function for GDT routine */
/*										  */
/**********************************************
 * Initialize a GDT entry.
 * uiLimit - the size of the memory segment.
 * ulBase - the starting linear address of the memory segment.
 * ucAccess
 * ucAttribs
 * return : the selector of the new GDT entry.
 ***********************************************/
static inline unsigned short uiSetupGdtEntry(unsigned short uiLimit, unsigned long ulBase,
											 unsigned char ucAccess, unsigned char ucAttribs)
{
	// Skip the dummy entry (#0).
	register unsigned short i = 1;
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);
	for(; i < GDTE_MAX_SIZE; i++) {
		if ( !(astGDT[i].ucAccess) ) {
			astGDT[i].uiLimit = uiLimit;
			astGDT[i].uiBaseLO = ulBase & 0xFFFF;
			astGDT[i].ucBaseMI = (ulBase >> 16) & 0xFF;
			astGDT[i].ucBaseHI = (ulBase >> 24) & 0xFF;
			astGDT[i].ucAccess = ucAccess;
			astGDT[i].ucAttribs = ucAttribs;

			RestoreEflags(ulFlags);

			// return the selector
			return (i* GDT_ENTRY_SIZE);
		}
	}

	return NULL;
}
/****************************************************
 * Remove a GDT entry.
 * uiSelector - the selector to be removed from the GDT
 *****************************************************/
static inline void vRemoveGdtEntry(unsigned short uiSelector)
{
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);

	astGDT[uiSelector/GDT_ENTRY_SIZE].uiLimit = 0;
	astGDT[uiSelector/GDT_ENTRY_SIZE].uiBaseLO = 0;
	astGDT[uiSelector/GDT_ENTRY_SIZE].ucBaseMI = 0;
	astGDT[uiSelector/GDT_ENTRY_SIZE].ucBaseHI = 0;
	astGDT[uiSelector/GDT_ENTRY_SIZE].ucAccess = 0;
	astGDT[uiSelector/GDT_ENTRY_SIZE].ucAttribs = 0;

	RestoreEflags(ulFlags);
}

#endif   /* end of __DSCR_H__ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -