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

📄 klib.c

📁 别人的根据linux0.11改的一个小的操作系统
💻 C
字号:
/*
 *klib.c
 *Some kernel functions,user have no right to call
 */
#include "def.h"
#include "time.h"
#include "sched.h"
#include "stdio.h"
#include "system.h"
/*
 * Print a string.
 * entry : str->string pointer.
 */
void printk(char *str)
{
	while(*str){
		if(*str=='\n'){
			setCursor(0,getCursorRow()+1);
			str++;
			continue;
		}
		putchark(*str);
		str++;
	}
}

/*
 * Get system start time.
 * parse time format from a time struct.
 */
long getStartTime(struct t_time *time)
{
	return (long)(((time->tm_year*365+
		time->tm_mon*30+
		time->tm_day)*24+
		time->tm_hour)*3600+
		time->tm_min*60+
		time->tm_sec);
}

/*
 * fill TSS descriptor as the descriptor number(n),
 * the system GDT be located at constant memory area.
 * we can compute the descriptor address use the number.
 */
void set_tss_desc(int n,char *addr)
{
	n*=2;
	unsigned long temp = (unsigned long)addr;
	desc[n] = (struct desc_struct *)(_gdt+(4+n)*8);
	desc[n]->limit = (unsigned short)(0x68-1);
	desc[n]->baseL = (unsigned short)(temp&0xffff);
	desc[n]->baseM = (unsigned char)((temp>>16)&0xff);
	desc[n]->access = (unsigned char)0x89;
	desc[n]->gran = (unsigned char)0x00;
	desc[n]->baseH = (unsigned char)((temp>>24)&0xff);
}
/*
 * fill LDT descriptor as the descriptor number(n).
 * same as the TSS descriptor,we can compute the 
 * descriptor address use the number.
 */
void set_ldt_desc(int n,char *addr)
{
	n*=2;
	n++;
	unsigned long temp = (unsigned long)addr;
	desc[n] = (struct desc_struct *)(_gdt+(4+n)*8);
	desc[n]->limit = (unsigned short)(0x20-1);
	desc[n]->baseL = (unsigned short)(temp&0xffff);
	desc[n]->baseM = (unsigned char)((temp>>16)&0xff);
	desc[n]->access = (unsigned char)0x82;
	desc[n]->gran = (unsigned char)0x00;
	desc[n]->baseH = (unsigned char)((temp>>24)&0xff);
}

/*
 * fill LDT as the parameter.
 * entry :  _ldt->LDT base address.
 *          limit->code | data segment length.
 *          base_addr-> segment base address.
 */
void set_ldt(char *_ldt,unsigned long limit,char *base_addr)
{
	struct desc_struct *ldt;
	unsigned long n;
	ldt = (struct desc_struct *)_ldt;
	n = (unsigned long)base_addr;
	ldt[0].limit = (unsigned short)0;
	ldt[0].baseL = (unsigned short)0;
	ldt[0].baseM = (unsigned char)0;
	ldt[0].access = (unsigned char)0;
	ldt[0].gran = (unsigned char)0;
	ldt[0].baseH = (unsigned char)0;

	ldt[1].limit = (unsigned short)(limit&0xffff);
	ldt[1].baseL = (unsigned short)(n&0xffff);
	ldt[1].baseM = (unsigned char)((n>>16)&0xff);
	ldt[1].access = (unsigned char)0xfa;
	ldt[1].gran = (unsigned char)0xc0;
	ldt[1].baseH=(unsigned char)((n>>24)&0xff);

	ldt[2].limit = (unsigned short)(limit&0xffff);
	ldt[2].baseL = (unsigned short)(n&0xffff);
	ldt[2].baseM = (unsigned char)((n>>16)&0xff);
	ldt[2].access = (unsigned char)0xf2;
	ldt[2].gran = (unsigned char)0xc0;
	ldt[2].baseH = (unsigned char)((n>>24)&0xff);
	
	ldt[3].limit = (unsigned short)0;
	ldt[3].baseL = (unsigned short)0;
	ldt[3].baseM = (unsigned char)0;
	ldt[3].access = (unsigned char)0;
	ldt[3].gran = (unsigned char)0;
	ldt[3].baseH = (unsigned char)0;
}

/*
 * fill trap gate descriptor,also use for interrupt 
 * descriptor.
 * entry:   num-> interrupt number .
 *          ptr-> interrupt routine address.
 */
void set_trap(int num,char *ptr)
{
	struct gate_struct *gs;
	unsigned long temp = (unsigned long)ptr;
	gs = IDT_ADDR;
	gs += (long)num;
	gs->addrL = (unsigned short)(temp&0x0000ffff);
	gs->segsel = 0x08;
	gs->sitems = (unsigned char)0x00;
	gs->type = (unsigned char)0x8e;
	gs->addrH = ((temp>>16)&0x0000ffff);
}

⌨️ 快捷键说明

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