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

📄 system.h

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 H
字号:
/***************************************************************************
**     File name   : system.h
**     Author      : x.cheng
**     Create date :
**
**	  Comments:
**		
**	
**     Revisions:
**     $Log: system.h,v $
**     Revision 1.2  2005/08/05 15:08:03  x.cheng
**     new i386 specific instructor added
**
**     Revision 1.1.1.1  2005/07/27 06:53:15  x.cheng
**     add into repositories
**
**
***************************************************************************/
#ifndef __JCINX_INC_SYSTEM_H__
#define __JCINX_INC_SYSTEM_H__

#include "io.h"

/* interrupt dis or enable */
#define vCLI()	__asm__ ("cli" : : );
#define vSTI()	__asm__ ("sti" : : );
#define vNOP()	__asm__ ("nop" : : );
#define vIRET() __asm__ ("iret" : : );

//! Restore EFLAGS register.
#define RestoreEflags(ulFlags ) \
	__asm__ __volatile__ ("pushl %0 ; popfl" : : "g"(ulFlags) : "memory", "cc")

//! Interrupt control: save flags and disable irqs.
#define SaveEflagsAndCli(ulFlags) \
	__asm__ __volatile__ ("pushfl ; popl %0 ; cli" : "=g"(ulFlags) : : "memory")

//! Interrupt control: save flags and enable irqs.
#define SaveEflagsAndSti(ulFlags) \
	__asm__ __volatile__ ("pushfl ; popl %0 ; sti" : "=g"(ulFlags) : : "memory")


#include "dscr.h"

/***************************************************
 *设置门描述符宏函数
 *Gate_Addr 描述符地址; Type 描述符中类型域值
 *D_P_L 中的特权层值;EAddr 偏移地址
 ***************************************************/
#define _vSetGate(Gate_Addr, Type, D_P_L, EAddr)	\
	__asm__ ("movw %%dx, %%ax\n"	\
			 "movw %0, %%dx\n"	\
			 "movl %%eax, %1\n"	\
			 "movl %%edx, %2"	\
			:	\
			: "i" ((short)(0x8000+(D_P_L<<13)+(Type<<8))),	\
			  "o" (*((char*)(Gate_Addr))),	\
			  "o" (*(4+(char*)(Gate_Addr))),	\
			  "d" ((char*)(EAddr)),	\
			  "a" (0x00080000))

/***************************************************
 *设置中断门的宏函数
 *	Nb 中断号; EAddr 中断程序偏移地址
 *		类型是14, 特权级是0
 ***************************************************/
#define vSetIntrGate(Nb, EAddr)	\
	_vSetGate(&astIDT[Nb], 14, 0, EAddr)
/***************************************************
 *设置陷入门的宏函数
 *	Nb 陷入号; EAddr 中断程序偏移地址
 *		类型是15, 特权级是0
 ***************************************************/
#define vSetTrapGate(Nb, EAddr)	\
	_vSetGate(&astIDT[Nb], 15, 0, EAddr)
/***************************************************
 *设置系统调用门的宏函数
 *	Nb 陷入号; EAddr 中断程序偏移地址
 *		类型是15, 特权级是3
 ***************************************************/
#define vSetSystemGate(Nb, EAddr)	\
	_vSetGate(&astIDT[Nb], 15, 3, EAddr)


/***************************************************
 *取段Segment中地址EAddr处的一个字节
 ***************************************************/
/*#define ucGetSegmentByte(Segment, EAddr) ({	\
	unsigned char ucResult;	\
	__asm__ __volatile__("push fs\n"	\
						 "mov fs, ax\n"	\
						 "movb fs:%2, al\n"	\
						 "pop fs"	\
		: "=a"(ucResult) : "0"(Segment), "m"(*(EAddr)));	\
		ucResult; })
*/
/***************************************************
 *取段Segment中地址EAddr处的一个长字,4字节
 ***************************************************/
/*#define dwGetSegmentLong(Segment, EAddr) ({	\
	unsigned long dwResult;	\
	__asm__ __volatile__("push fs\n" "mov fs, ax\n" "mov eax, DWORD [fs:%2]\n" "pop fs"	\
		: "=a"(dwResult) : "0"(Segment), "m"(*(EAddr)));	\
		dwResult; })
*/
#define ucGetSegmentByte(Segment, EAddr) ({	\
	unsigned char ucResult;	\
	__asm__("push %%fs; mov %%ax, %%fs; movb %%fs:%2, %%al; pop %%fs"	\
			:"=a" (ucResult):"0"(Segment), "m"(*(EAddr)));	\
	ucResult; })
#define dwGetSegmentLong(Segment, EAddr) ({	\
	unsigned long dwResult;	\
	__asm__("push %%fs; mov %%ax, %%fs; movl %%fs:%2, %%eax; pop %%fs"	\
			: "=a"(dwResult) : "0"(Segment), "m"(*(EAddr)));	\
	dwResult; })


/**************************************************************
 *TASK SWITCHING inline function
 *
 *	Perform a context switch between two tasks.
 * This is an asm routine to jump to a TSS (TSS-based multitasking)
 *
 * uiTssSelector - The selector of the next task.
***************************************************************/
static inline void vJumpToTss(unsigned short uiTssSelector)
{
	static struct {
		unsigned eip : 32;	//32 bit
		unsigned cs	 : 16;  //16 bit
	} __attribute__((packed)) stTssLink = {0, 0};

	// set the TSS link
	stTssLink.cs = uiTssSelector;

	// jump to the task
	__asm__ __volatile__ ("ljmp *(%0)" : : "m" (stTssLink));
}

/*****************************************************************
 *--- CPUID ------------------------------------------------------
 *
 *Get the CPUID information.
 *	op The operation code to perform.
 *	eax EAX register value after the CPUID execution.
 *	ebx EBX register value after the CPUID execution.
 *	ecx ECX register value after the CPUID execution.
 *	edx EDX register value after the CPUID execution.
 *	\warning
 *	Not all the Intel CPUs support the CPUID instruction!!!
 *	Only some Intel486 family and subsequent Intel processors
 *	provide this method for determinig the architecture flags.
 *	Execution of CPUID on a processor that does not support this
 *	instruction will result in an invalid opcode exception.
 *
 *	To determine if it is possible to use this instruction we can
 *	use bit 21 of the EFLAGS register. If software can change the
 *	value of this flag, the CPUID instruction is executable.
 *
 *****************************************************************/
static inline void CPUID(int op, unsigned long *eax, unsigned long *ebx, unsigned long *ecx, unsigned long *edx)
{
	__asm__ __volatile__(
		"cpuid"
		:
		"=a" (*eax),
		"=b" (*ebx),
		"=c" (*ecx),
		"=d" (*edx)
		:
		"0" (op)
	);
	__asm__ __volatile__ ("" : : : "eax", "ebx", "ecx", "edx");
}


//
//	Read the timestamp counter from the model-specific register
//	and put it into two 32-bit registers.
#define RDTSC(low, high) \
	__asm__ __volatile__( "rdtsc" : "=a"(low), "=d"(high) )

//
//	Read the timestamp counter from the model-specific register
//	and put it into eax:edx registers. This is a 64-bit value.
#define RDTSCLL(ullVal) \
	__asm__ __volatile__( "rdtsc" : "=A"(ullVal) )

#endif /* end of __JCINX_INC_SYSTEM_H__ */

⌨️ 快捷键说明

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