天天看點

linux源代碼剖析之kernelMakefile for the FREAX-kernel.Note! Dependencies are done automagically by ‘make dep’, which alsoremoves any old dependencies. DON’T put your own dependencies hereunless it’s something special (ie not a .c file).

kernel

asm.s

.globl _divide_error,_debug,_nmi,_int3,_overflow,_bounds,_invalid_op

.globl _device_not_available,_double_fault,_coprocessor_segment_overrun

.globl _invalid_TSS,_segment_not_present,_stack_segment

.globl _general_protection,_coprocessor_error,_reserved

_divide_error:

pushl $_do_divide_error

no_error_code:

xchgl %eax,(%esp)

pushl %ebx

pushl %ecx

pushl %edx

pushl %edi

pushl %esi

pushl %ebp

push %ds

push %es

push %fs

pushl $0 # “error code”

lea 44(%esp),%edx

pushl %edx

movl $0x10,%edx

mov %dx,%ds

mov %dx,%es

mov %dx,%fs

call *%eax

addl $8,%esp

pop %fs

pop %es

pop %ds

popl %ebp

popl %esi

popl %edi

popl %edx

popl %ecx

popl %ebx

popl %eax

iret

_debug:

pushl $_do_int3 # _do_debug

jmp no_error_code

_nmi:

pushl $_do_nmi

jmp no_error_code

_int3:

pushl $_do_int3

jmp no_error_code

_overflow:

pushl $_do_overflow

jmp no_error_code

_bounds:

pushl $_do_bounds

jmp no_error_code

_invalid_op:

pushl $_do_invalid_op

jmp no_error_code

math_emulate:

popl %eax

pushl $_do_device_not_available

jmp no_error_code

_device_not_available:

pushl %eax

movl %cr0,%eax

bt $2,%eax # EM (math emulation bit)

jc math_emulate

clts # clear TS so that we can use math

movl _current,%eax

cmpl _last_task_used_math,%eax

je 1f # shouldn’t happen really …

pushl %ecx

pushl %edx

push %ds

movl $0x10,%eax

mov %ax,%ds

call _math_state_restore

pop %ds

popl %edx

popl %ecx

1: popl %eax

iret

_coprocessor_segment_overrun:

pushl $_do_coprocessor_segment_overrun

jmp no_error_code

_reserved:

pushl $_do_reserved

jmp no_error_code

_coprocessor_error:

pushl $_do_coprocessor_error

jmp no_error_code

_double_fault:

pushl $_do_double_fault

error_code:

xchgl %eax,4(%esp) # error code <-> %eax

xchgl %ebx,(%esp) # &function <-> %ebx

pushl %ecx

pushl %edx

pushl %edi

pushl %esi

pushl %ebp

push %ds

push %es

push %fs

pushl %eax # error code

lea 44(%esp),%eax # offset

pushl %eax

movl $0x10,%eax

mov %ax,%ds

mov %ax,%es

mov %ax,%fs

call *%ebx

addl $8,%esp

pop %fs

pop %es

pop %ds

popl %ebp

popl %esi

popl %edi

popl %edx

popl %ecx

popl %ebx

popl %eax

iret

_invalid_TSS:

pushl $_do_invalid_TSS

jmp error_code

_segment_not_present:

pushl $_do_segment_not_present

jmp error_code

_stack_segment:

pushl $_do_stack_segment

jmp error_code

_general_protection:

pushl $_do_general_protection

jmp error_code

console.c

#include <linux/sched.h>

#include <linux/tty.h>

#include <asm/io.h>

#include <asm/system.h>

#define SCREEN_START 0xb8000

#define SCREEN_END 0xc0000

#define LINES 25

#define COLUMNS 80

#define NPAR 16

extern void keyboard_interrupt(void);

static unsigned long origin=SCREEN_START;

static unsigned long scr_end=SCREEN_START+LINESCOLUMNS2;

static unsigned long pos;

static unsigned long x,y;

static unsigned long top=0,bottom=LINES;

static unsigned long lines=LINES,columns=COLUMNS;

static unsigned long state=0;

static unsigned long npar,par[NPAR];

static unsigned long ques=0;

static unsigned char attr=0x07;

#define RESPONSE “\033[?1;2c”

static inline void gotoxy(unsigned int new_x,unsigned int new_y)

{

if (new_x>=columns || new_y>=lines)

return;

x=new_x;

y=new_y;

pos=origin+((y*columns+x)<<1);

}

static inline void set_origin(void)

{

cli();

outb_p(12,0x3d4);

outb_p(0xff&((origin-SCREEN_START)>>9),0x3d5);

outb_p(13,0x3d4);

outb_p(0xff&((origin-SCREEN_START)>>1),0x3d5);

sti();

}

static void scrup(void)

{

if (!top && bottom==lines) {

origin += columns<<1;

pos += columns<<1;

scr_end += columns<<1;

if (scr_end>SCREEN_END) {

asm(“cld\n\t”

“rep\n\t”

“movsl\n\t”

“movl _columns,%1\n\t”

“rep\n\t”

“stosw”

::“a” (0x0720),

“c” ((lines-1)*columns>>1),

“D” (SCREEN_START),

“S” (origin)

:“cx”,“di”,“si”);

scr_end -= origin-SCREEN_START;

pos -= origin-SCREEN_START;

origin = SCREEN_START;

} else {

asm(“cld\n\t”

“rep\n\t”

“stosl”

::“a” (0x07200720),

“c” (columns>>1),

“D” (scr_end-(columns<<1))

:“cx”,“di”);

}

set_origin();

} else {

asm(“cld\n\t”

“rep\n\t”

“movsl\n\t”

“movl _columns,%%ecx\n\t”

“rep\n\t”

“stosw”

::“a” (0x0720),

“c” ((bottom-top-1)*columns>>1),

“D” (origin+(columns<<1)top),

“S” (origin+(columns<<1)(top+1))

:“cx”,“di”,“si”);

}

}

static void scrdown(void)

{

asm(“std\n\t”

“rep\n\t”

“movsl\n\t”

“addl $2,%%edi\n\t”

“movl _columns,%%ecx\n\t”

“rep\n\t”

“stosw”

::“a” (0x0720),

“c” ((bottom-top-1)*columns>>1),

“D” (origin+(columns<<1)bottom-4),

“S” (origin+(columns<<1)(bottom-1)-4)

:“ax”,“cx”,“di”,“si”);

}

static void lf(void)

{

if (y+1<bottom) {

y++;

pos += columns<<1;

return;

}

scrup();

}

static void ri(void)

{

if (y>top) {

y–;

pos -= columns<<1;

return;

}

scrdown();

}

static void cr(void)

{

pos -= x<<1;

x=0;

}

static void del(void)

{

if (x) {

pos -= 2;

x–;

*(unsigned short *)pos = 0x0720;

}

}

static void csi_J(int par)

{

long count asm(“cx”);

long start asm(“di”);

switch (par) {
	case 0:	/* erase from cursor to end of display */
		count = (scr_end-pos)>>1;
		start = pos;
		break;
	case 1:	/* erase from start to cursor */
		count = (pos-origin)>>1;
		start = origin;
		break;
	case 2: /* erase whole display */
		count = columns*lines;
		start = origin;
		break;
	default:
		return;
}
__asm__("cld\n\t"
	"rep\n\t"
	"stosw\n\t"
	::"c" (count),
	"D" (start),"a" (0x0720)
	:"cx","di");
           

}

static void csi_K(int par)

{

long count asm(“cx”);

long start asm(“di”);

switch (par) {
	case 0:	/* erase from cursor to end of line */
		if (x>=columns)
			return;
		count = columns-x;
		start = pos;
		break;
	case 1:	/* erase from start of line to cursor */
		start = pos - (x<<1);
		count = (x<columns)?x:columns;
		break;
	case 2: /* erase whole line */
		start = pos - (x<<1);
		count = columns;
		break;
	default:
		return;
}
__asm__("cld\n\t"
	"rep\n\t"
	"stosw\n\t"
	::"c" (count),
	"D" (start),"a" (0x0720)
	:"cx","di");
           

}

void csi_m(void)

{

int i;

for (i=0;i<=npar;i++)
	switch (par[i]) {
		case 0:attr=0x07;break;
		case 1:attr=0x0f;break;
		case 4:attr=0x0f;break;
		case 7:attr=0x70;break;
		case 27:attr=0x07;break;
	}
           

}

static inline void set_cursor(void)

{

cli();

outb_p(14,0x3d4);

outb_p(0xff&((pos-SCREEN_START)>>9),0x3d5);

outb_p(15,0x3d4);

outb_p(0xff&((pos-SCREEN_START)>>1),0x3d5);

sti();

}

static void respond(struct tty_struct * tty)

{

char * p = RESPONSE;

cli();
while (*p) {
	PUTCH(*p,tty->read_q);
	p++;
}
sti();
copy_to_cooked(tty);
           

}

static void insert_char(void)

{

int i=x;

unsigned short tmp,old=0x0720;

unsigned short * p = (unsigned short *) pos;

while (i++<columns) {
	tmp=*p;
	*p=old;
	old=tmp;
	p++;
}
           

}

static void insert_line(void)

{

int oldtop,oldbottom;

oldtop=top;
oldbottom=bottom;
top=y;
bottom=lines;
scrdown();
top=oldtop;
bottom=oldbottom;
           

}

static void delete_char(void)

{

int i;

unsigned short * p = (unsigned short *) pos;

if (x>=columns)
	return;
i = x;
while (++i < columns) {
	*p = *(p+1);
	p++;
}
*p=0x0720;
           

}

static void delete_line(void)

{

int oldtop,oldbottom;

oldtop=top;
oldbottom=bottom;
top=y;
bottom=lines;
scrup();
top=oldtop;
bottom=oldbottom;
           

}

static void csi_at(int nr)

{

if (nr>columns)

nr=columns;

else if (!nr)

nr=1;

while (nr–)

insert_char();

}

static void csi_L(int nr)

{

if (nr>lines)

nr=lines;

else if (!nr)

nr=1;

while (nr–)

insert_line();

}

static void csi_P(int nr)

{

if (nr>columns)

nr=columns;

else if (!nr)

nr=1;

while (nr–)

delete_char();

}

static void csi_M(int nr)

{

if (nr>lines)

nr=lines;

else if (!nr)

nr=1;

while (nr–)

delete_line();

}

static int saved_x=0;

static int saved_y=0;

static void save_cur(void)

{

saved_x=x;

saved_y=y;

}

static void restore_cur(void)

{

x=saved_x;

y=saved_y;

pos=origin+((y*columns+x)<<1);

}

void con_write(struct tty_struct * tty)

{

int nr;

char c;

nr = CHARS(tty->write_q);
while (nr--) {
	GETCH(tty->write_q,c);
	switch(state) {
		case 0:
			if (c>31 && c<127) {
				if (x>=columns) {
					x -= columns;
					pos -= columns<<1;
					lf();
				}
				__asm__("movb _attr,%%ah\n\t"
					"movw %%ax,%1\n\t"
					::"a" (c),"m" (*(short *)pos)
					:"ax");
				pos += 2;
				x++;
			} else if (c==27)
				state=1;
			else if (c==10 || c==11 || c==12)
				lf();
			else if (c==13)
				cr();
			else if (c==ERASE_CHAR(tty))
				del();
			else if (c==8) {
				if (x) {
					x--;
					pos -= 2;
				}
			} else if (c==9) {
				c=8-(x&7);
				x += c;
				pos += c<<1;
				if (x>columns) {
					x -= columns;
					pos -= columns<<1;
					lf();
				}
				c=9;
			}
			break;
		case 1:
			state=0;
			if (c=='[')
				state=2;
			else if (c=='E')
				gotoxy(0,y+1);
			else if (c=='M')
				ri();
			else if (c=='D')
				lf();
			else if (c=='Z')
				respond(tty);
			else if (x=='7')
				save_cur();
			else if (x=='8')
				restore_cur();
			break;
		case 2:
			for(npar=0;npar<NPAR;npar++)
				par[npar]=0;
			npar=0;
			state=3;
			if (ques=(c=='?'))
				break;
		case 3:
			if (c==';' && npar<NPAR-1) {
				npar++;
				break;
			} else if (c>='0' && c<='9') {
				par[npar]=10*par[npar]+c-'0';
				break;
			} else state=4;
		case 4:
			state=0;
			switch(c) {
				case 'G': case '`':
					if (par[0]) par[0]--;
					gotoxy(par[0],y);
					break;
				case 'A':
					if (!par[0]) par[0]++;
					gotoxy(x,y-par[0]);
					break;
				case 'B': case 'e':
					if (!par[0]) par[0]++;
					gotoxy(x,y+par[0]);
					break;
				case 'C': case 'a':
					if (!par[0]) par[0]++;
					gotoxy(x+par[0],y);
					break;
				case 'D':
					if (!par[0]) par[0]++;
					gotoxy(x-par[0],y);
					break;
				case 'E':
					if (!par[0]) par[0]++;
					gotoxy(0,y+par[0]);
					break;
				case 'F':
					if (!par[0]) par[0]++;
					gotoxy(0,y-par[0]);
					break;
				case 'd':
					if (par[0]) par[0]--;
					gotoxy(x,par[0]);
					break;
				case 'H': case 'f':
					if (par[0]) par[0]--;
					if (par[1]) par[1]--;
					gotoxy(par[1],par[0]);
					break;
				case 'J':
					csi_J(par[0]);
					break;
				case 'K':
					csi_K(par[0]);
					break;
				case 'L':
					csi_L(par[0]);
					break;
				case 'M':
					csi_M(par[0]);
					break;
				case 'P':
					csi_P(par[0]);
					break;
				case '@':
					csi_at(par[0]);
					break;
				case 'm':
					csi_m();
					break;
				case 'r':
					if (par[0]) par[0]--;
					if (!par[1]) par[1]=lines;
					if (par[0] < par[1] &&
					    par[1] <= lines) {
						top=par[0];
						bottom=par[1];
					}
					break;
				case 's':
					save_cur();
					break;
				case 'u':
					restore_cur();
					break;
			}
	}
}
set_cursor();
           

}

void con_init(void)

{

register unsigned char a;

gotoxy(*(unsigned char )(0x90000+510),(unsigned char *)(0x90000+511));

set_trap_gate(0x21,&keyboard_interrupt);

outb_p(inb_p(0x21)&0xfd,0x21);

a=inb_p(0x61);

outb_p(a|0x80,0x61);

outb(a,0x61);

}

exit.c

#include <errno.h>

#include <signal.h>

#include <sys/wait.h>

#include <linux/sched.h>

#include <linux/kernel.h>

#include <linux/tty.h>

#include <asm/segment.h>

int sys_pause(void);

int sys_close(int fd);

void release(struct task_struct * p)

{

int i;

if (!p)
	return;
for (i=1 ; i<NR_TASKS ; i++)
	if (task[i]==p) {
		task[i]=NULL;
		free_page((long)p);
		schedule();
		return;
	}
panic("trying to release non-existent task");
           

}

static inline void send_sig(long sig,struct task_struct * p,int priv)

{

if (!p || sig<1 || sig>32)

return;

if (priv ||

current->uidp->uid ||

current->euidp->uid ||

current->uidp->euid ||

current->euidp->euid)

p->signal |= (1<<(sig-1));

}

void do_kill(long pid,long sig,int priv)

{

struct task_struct **p = NR_TASKS + task;

if (!pid) while (--p > &FIRST_TASK) {
	if (*p && (*p)->pgrp == current->pid)
		send_sig(sig,*p,priv);
} else if (pid>0) while (--p > &FIRST_TASK) {
	if (*p && (*p)->pid == pid)
		send_sig(sig,*p,priv);
} else if (pid == -1) while (--p > &FIRST_TASK)
	send_sig(sig,*p,priv);
else while (--p > &FIRST_TASK)
	if (*p && (*p)->pgrp == -pid)
		send_sig(sig,*p,priv);
           

}

int sys_kill(int pid,int sig)

{

do_kill(pid,sig,!(current->uid || current->euid));

return 0;

}

int do_exit(long code)

{

int i;

free_page_tables(get_base(current->ldt[1]),get_limit(0x0f));
free_page_tables(get_base(current->ldt[2]),get_limit(0x17));
for (i=0 ; i<NR_TASKS ; i++)
	if (task[i] && task[i]->father == current->pid)
		task[i]->father = 0;
for (i=0 ; i<NR_OPEN ; i++)
	if (current->filp[i])
		sys_close(i);
iput(current->pwd);
current->pwd=NULL;
iput(current->root);
current->root=NULL;
if (current->leader && current->tty >= 0)
	tty_table[current->tty].pgrp = 0;
if (last_task_used_math == current)
	last_task_used_math = NULL;
if (current->father) {
	current->state = TASK_ZOMBIE;
	do_kill(current->father,SIGCHLD,1);
	current->exit_code = code;
} else
	release(current);
schedule();
return (-1);	/* just to suppress warnings */
           

}

int sys_exit(int error_code)

{

return do_exit((error_code&0xff)<<8);

}

int sys_waitpid(pid_t pid,int * stat_addr, int options)

{

int flag=0;

struct task_struct ** p;

verify_area(stat_addr,4);
           

repeat:

for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)

if (*p && *p != current &&

(pid==-1 || (*p)->pidpid ||

(pid0 && (*p)->pgrpcurrent->pgrp) ||

(pid<0 && (*p)->pgrp-pid)))

if ((*p)->father == current->pid) {

flag=1;

if ((*p)->state==TASK_ZOMBIE) {

put_fs_long((*p)->exit_code,

(unsigned long *) stat_addr);

current->cutime += (*p)->utime;

current->cstime += (*p)->stime;

flag = (*p)->pid;

release(*p);

return flag;

}

}

if (flag) {

if (options & WNOHANG)

return 0;

sys_pause();

if (!(current->signal &= ~(1<<(SIGCHLD-1))))

goto repeat;

else

return -EINTR;

}

return -ECHILD;

}

fork.c

#include <errno.h>

#include <linux/sched.h>

#include <linux/kernel.h>

#include <asm/segment.h>

#include <asm/system.h>

extern void write_verify(unsigned long address);

long last_pid=0;

void verify_area(void * addr,int size)

{

unsigned long start;

start = (unsigned long) addr;
size += start & 0xfff;
start &= 0xfffff000;
start += get_base(current->ldt[2]);
while (size>0) {
	size -= 4096;
	write_verify(start);
	start += 4096;
}
           

}

int copy_mem(int nr,struct task_struct * p)

{

unsigned long old_data_base,new_data_base,data_limit;

unsigned long old_code_base,new_code_base,code_limit;

code_limit=get_limit(0x0f);
data_limit=get_limit(0x17);
old_code_base = get_base(current->ldt[1]);
old_data_base = get_base(current->ldt[2]);
if (old_data_base != old_code_base)
	panic("We don't support separate I&D");
if (data_limit < code_limit)
	panic("Bad data_limit");
new_data_base = new_code_base = nr * 0x4000000;
set_base(p->ldt[1],new_code_base);
set_base(p->ldt[2],new_data_base);
if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
	free_page_tables(new_data_base,data_limit);
	return -ENOMEM;
}
return 0;
           

}

int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,

long ebx,long ecx,long edx,

long fs,long es,long ds,

long eip,long cs,long eflags,long esp,long ss)

{

struct task_struct *p;

int i;

struct file *f;

p = (struct task_struct *) get_free_page();

if (!p)

return -EAGAIN;

*p = current; / NOTE! this doesn’t copy the supervisor stack /

p->state = TASK_RUNNING;

p->pid = last_pid;

p->father = current->pid;

p->counter = p->priority;

p->signal = 0;

p->alarm = 0;

p->leader = 0; / process leadership doesn’t inherit /

p->utime = p->stime = 0;

p->cutime = p->cstime = 0;

p->start_time = jiffies;

p->tss.back_link = 0;

p->tss.esp0 = PAGE_SIZE + (long) p;

p->tss.ss0 = 0x10;

p->tss.eip = eip;

p->tss.eflags = eflags;

p->tss.eax = 0;

p->tss.ecx = ecx;

p->tss.edx = edx;

p->tss.ebx = ebx;

p->tss.esp = esp;

p->tss.ebp = ebp;

p->tss.esi = esi;

p->tss.edi = edi;

p->tss.es = es & 0xffff;

p->tss.cs = cs & 0xffff;

p->tss.ss = ss & 0xffff;

p->tss.ds = ds & 0xffff;

p->tss.fs = fs & 0xffff;

p->tss.gs = gs & 0xffff;

p->tss.ldt = _LDT(nr);

p->tss.trace_bitmap = 0x80000000;

if (last_task_used_math == current)

asm(“fnsave %0”::“m” (p->tss.i387));

if (copy_mem(nr,p)) {

free_page((long) p);

return -EAGAIN;

}

for (i=0; i<NR_OPEN;i++)

if (f=p->filp[i])

f->f_count++;

if (current->pwd)

current->pwd->i_count++;

if (current->root)

current->root->i_count++;

set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));

set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));

task[nr] = p; / do this last, just in case */

return last_pid;

}

int find_empty_process(void)

{

int i;

repeat:
	if ((++last_pid)<0) last_pid=1;
	for(i=0 ; i<NR_TASKS ; i++)
		if (task[i] && task[i]->pid == last_pid) goto repeat;
for(i=1 ; i<NR_TASKS ; i++)
	if (!task[i])
		return i;
return -EAGAIN;
           

}

hd.c

#include <linux/config.h>

#include <linux/sched.h>

#include <linux/fs.h>

#include <linux/kernel.h>

#include <linux/hdreg.h>

#include <asm/system.h>

#include <asm/io.h>

#include <asm/segment.h>

#define MAX_ERRORS 5

#define MAX_HD 2

#define NR_REQUEST 32

static struct hd_i_struct{

int head,sect,cyl,wpcom,lzone,ctl;

} hd_info[]= { HD_TYPE };

#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct)))

static struct hd_struct {

long start_sect;

long nr_sects;

} hd[5*MAX_HD]={{0,0},};

static struct hd_request {

int hd;

int nsector;

int sector;

int head;

int cyl;

int cmd;

int errors;

struct buffer_head * bh;

struct hd_request * next;

} request[NR_REQUEST];

#define IN_ORDER(s1,s2)

((s1)->hd<(s2)->hd || (s1)->hd==(s2)->hd &&

((s1)->cyl<(s2)->cyl || (s1)->cyl==(s2)->cyl &&

((s1)->head<(s2)->head || (s1)->head==(s2)->head &&

((s1)->sector<(s2)->sector))))

static struct hd_request * this_request = NULL;

static int sorting=0;

static void do_request(void);

static void reset_controller(void);

static void rw_abs_hd(int rw,unsigned int nr,unsigned int sec,unsigned int head,

unsigned int cyl,struct buffer_head * bh);

void hd_init(void);

#define port_read(port,buf,nr)

asm(“cld;rep;insw”::“d” (port),“D” (buf),“c” (nr):“cx”,“di”)

#define port_write(port,buf,nr)

asm(“cld;rep;outsw”::“d” (port),“S” (buf),“c” (nr):“cx”,“si”)

extern void hd_interrupt(void);

static struct task_struct * wait_for_request=NULL;

static inline void lock_buffer(struct buffer_head * bh)

{

if (bh->b_lock)

printk(“hd.c: buffer multiply locked\n”);

bh->b_lock=1;

}

static inline void unlock_buffer(struct buffer_head * bh)

{

if (!bh->b_lock)

printk(“hd.c: free buffer being unlocked\n”);

bh->b_lock=0;

wake_up(&bh->b_wait);

}

static inline void wait_on_buffer(struct buffer_head * bh)

{

cli();

while (bh->b_lock)

sleep_on(&bh->b_wait);

sti();

}

void rw_hd(int rw, struct buffer_head * bh)

{

unsigned int block,dev;

unsigned int sec,head,cyl;

block = bh->b_blocknr << 1;
dev = MINOR(bh->b_dev);
if (dev >= 5*NR_HD || block+2 > hd[dev].nr_sects)
	return;
block += hd[dev].start_sect;
dev /= 5;
__asm__("divl %4":"=a" (block),"=d" (sec):"0" (block),"1" (0),
	"r" (hd_info[dev].sect));
__asm__("divl %4":"=a" (cyl),"=d" (head):"0" (block),"1" (0),
	"r" (hd_info[dev].head));
rw_abs_hd(rw,dev,sec+1,head,cyl,bh);
           

}

int sys_setup(void)

{

static int callable = 1;

int i,drive;

struct partition *p;

if (!callable)
	return -1;
callable = 0;
for (drive=0 ; drive<NR_HD ; drive++) {
	rw_abs_hd(READ,drive,1,0,0,(struct buffer_head *) start_buffer);
	if (!start_buffer->b_uptodate) {
		printk("Unable to read partition table of drive %d\n\r",
			drive);
		panic("");
	}
	if (start_buffer->b_data[510] != 0x55 || (unsigned char)
	    start_buffer->b_data[511] != 0xAA) {
		printk("Bad partition table on drive %d\n\r",drive);
		panic("");
	}
	p = 0x1BE + (void *)start_buffer->b_data;
	for (i=1;i<5;i++,p++) {
		hd[i+5*drive].start_sect = p->start_sect;
		hd[i+5*drive].nr_sects = p->nr_sects;
	}
}
printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":"");
mount_root();
return (0);
           

}

void (*do_hd)(void) = NULL;

static int controller_ready(void)

{

int retries=1000;

while (--retries && (inb(HD_STATUS)&0xc0)!=0x40);
return (retries);
           

}

static int win_result(void)

{

int i=inb(HD_STATUS);

if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT))
	== (READY_STAT | SEEK_STAT))
	return(0); /* ok */
if (i&1) i=inb(HD_ERROR);
return (1);
           

}

static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,

unsigned int head,unsigned int cyl,unsigned int cmd,

void (*intr_addr)(void))

{

register int port asm(“dx”);

if (drive>1 || head>15)
	panic("Trying to write bad sector");
if (!controller_ready())
	panic("HD controller not ready");
do_hd = intr_addr;
outb(_CTL,HD_CMD);
port=HD_DATA;
outb_p(_WPCOM,++port);
outb_p(nsect,++port);
outb_p(sect,++port);
outb_p(cyl,++port);
outb_p(cyl>>8,++port);
outb_p(0xA0|(drive<<4)|head,++port);
outb(cmd,++port);
           

}

static int drive_busy(void)

{

unsigned int i;

for (i = 0; i < 100000; i++)
	if (READY_STAT == (inb(HD_STATUS) & (BUSY_STAT | READY_STAT)))
		break;
i = inb(HD_STATUS);
i &= BUSY_STAT | READY_STAT | SEEK_STAT;
if (i == READY_STAT | SEEK_STAT)
	return(0);
printk("HD controller times out\n\r");
return(1);
           

}

static void reset_controller(void)

{

int i;

outb(4,HD_CMD);
for(i = 0; i < 1000; i++) nop();
outb(0,HD_CMD);
for(i = 0; i < 10000 && drive_busy(); i++) /* nothing */;
if (drive_busy())
	printk("HD-controller still busy\n\r");
if((i = inb(ERR_STAT)) != 1)
	printk("HD-controller reset failed: %02x\n\r",i);
           

}

static void reset_hd(int nr)

{

reset_controller();

hd_out(nr,_SECT,_SECT,_HEAD-1,_CYL,WIN_SPECIFY,&do_request);

}

void unexpected_hd_interrupt(void)

{

panic(“Unexpected HD interrupt\n\r”);

}

static void bad_rw_intr(void)

{

int i = this_request->hd;

if (this_request->errors++ >= MAX_ERRORS) {
	this_request->bh->b_uptodate = 0;
	unlock_buffer(this_request->bh);
	wake_up(&wait_for_request);
	this_request->hd = -1;
	this_request=this_request->next;
}
reset_hd(i);
           

}

static void read_intr(void)

{

if (win_result()) {

bad_rw_intr();

return;

}

port_read(HD_DATA,this_request->bh->b_data+

512*(this_request->nsector&1),256);

this_request->errors = 0;

if (–this_request->nsector)

return;

this_request->bh->b_uptodate = 1;

this_request->bh->b_dirt = 0;

wake_up(&wait_for_request);

unlock_buffer(this_request->bh);

this_request->hd = -1;

this_request=this_request->next;

do_request();

}

static void write_intr(void)

{

if (win_result()) {

bad_rw_intr();

return;

}

if (–this_request->nsector) {

port_write(HD_DATA,this_request->bh->b_data+512,256);

return;

}

this_request->bh->b_uptodate = 1;

this_request->bh->b_dirt = 0;

wake_up(&wait_for_request);

unlock_buffer(this_request->bh);

this_request->hd = -1;

this_request=this_request->next;

do_request();

}

static void do_request(void)

{

int i,r;

if (sorting)
	return;
if (!this_request) {
	do_hd=NULL;
	return;
}
if (this_request->cmd == WIN_WRITE) {
	hd_out(this_request->hd,this_request->nsector,this_request->
		sector,this_request->head,this_request->cyl,
		this_request->cmd,&write_intr);
	for(i=0 ; i<3000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++)
		/* nothing */ ;
	if (!r) {
		reset_hd(this_request->hd);
		return;
	}
	port_write(HD_DATA,this_request->bh->b_data+
		512*(this_request->nsector&1),256);
} else if (this_request->cmd == WIN_READ) {
	hd_out(this_request->hd,this_request->nsector,this_request->
		sector,this_request->head,this_request->cyl,
		this_request->cmd,&read_intr);
} else
	panic("unknown hd-command");
           

}

static void add_request(struct hd_request * req)

{

struct hd_request * tmp;

if (req->nsector != 2)

panic(“nsector!=2 not implemented”);

if (!do_hd)

do_request();

}

void rw_abs_hd(int rw,unsigned int nr,unsigned int sec,unsigned int head,

unsigned int cyl,struct buffer_head * bh)

{

struct hd_request * req;

if (rw!=READ && rw!=WRITE)
	panic("Bad hd command, must be R/W");
lock_buffer(bh);
           

repeat:

for (req=0+request ; req<NR_REQUEST+request ; req++)

if (req->hd<0)

break;

if (reqNR_REQUEST+request) {

sleep_on(&wait_for_request);

goto repeat;

}

req->hd=nr;

req->nsector=2;

req->sector=sec;

req->head=head;

req->cyl=cyl;

req->cmd = ((rwREAD)?WIN_READ:WIN_WRITE);

req->bh=bh;

req->errors=0;

req->next=NULL;

add_request(req);

wait_on_buffer(bh);

}

void hd_init(void)

{

int i;

for (i=0 ; i<NR_REQUEST ; i++) {
	request[i].hd = -1;
	request[i].next = NULL;
}
for (i=0 ; i<NR_HD ; i++) {
	hd[i*5].start_sect = 0;
	hd[i*5].nr_sects = hd_info[i].head*
			hd_info[i].sect*hd_info[i].cyl;
}
set_trap_gate(0x2E,&hd_interrupt);
outb_p(inb_p(0x21)&0xfb,0x21);
outb(inb_p(0xA1)&0xbf,0xA1);
           

}

keyboard.s

.text

.globl _keyboard_interrupt

head = 4

tail = 8

proc_list = 12

buf = 16

mode: .byte 0

e0: .byte 0

inb $0x60,%al

cmpb $0xe0,%al

je set_e0

cmpb $0xe1,%al

je set_e1

call key_table(,%eax,4)

movb $0,e0

e0_e1: inb $0x61,%al

jmp 1f

1: jmp 1f

1: orb $0x80,%al

jmp 1f

1: jmp 1f

1: outb %al,$0x61

jmp 1f

1: jmp 1f

1: andb $0x7F,%al

outb %al,$0x61

movb $0x20,%al

outb %al,$0x20

pushl $0

call _do_tty_interrupt

addl $4,%esp

pop %es

pop %ds

popl %edx

popl %ecx

popl %ebx

popl %eax

iret

set_e0: movb $1,e0

jmp e0_e1

set_e1: movb $2,e0

jmp e0_e1

put_queue:

pushl %ecx

pushl %edx

movl _table_list,%edx # read-queue for console

movl head(%edx),%ecx

1: movb %al,buf(%edx,%ecx)

incl %ecx

andl $size-1,%ecx

cmpl tail(%edx),%ecx # buffer full - discard everything

je 3f

shrdl $8,%ebx,%eax

je 2f

shrl $8,%ebx

jmp 1b

2: movl %ecx,head(%edx)

movl proc_list(%edx),%ecx

testl %ecx,%ecx

je 3f

movl $0,(%ecx)

3: popl %edx

popl %ecx

ret

ctrl: movb $0x04,%al

jmp 1f

alt: movb $0x10,%al

1: cmpb $0,e0

je 2f

addb %al,%al

2: orb %al,mode

ret

unctrl: movb $0x04,%al

jmp 1f

unalt: movb $0x10,%al

1: cmpb $0,e0

je 2f

addb %al,%al

2: notb %al

andb %al,mode

ret

lshift:

orb $0x01,mode

ret

unlshift:

andb $0xfe,mode

ret

rshift:

orb $0x02,mode

ret

unrshift:

andb $0xfd,mode

ret

caps: testb $0x80,mode

jne 1f

xorb $4,leds

xorb $0x40,mode

orb $0x80,mode

set_leds:

call kb_wait

movb $0xed,%al

outb %al,$0x60

call kb_wait

movb leds,%al

outb %al,$0x60

ret

uncaps: andb $0x7f,mode

ret

scroll:

xorb $1,leds

jmp set_leds

num: xorb $2,leds

jmp set_leds

jne cur

xorl %ebx,%ebx

movb num_table(%eax),%al

jmp put_queue

1: ret

cur: movb cur_table(%eax),%al

cmpb $‘9,%al

ja ok_cur

movb $’~,%ah

ok_cur: shll $16,%eax

movw $0x5b1b,%ax

xorl %ebx,%ebx

jmp put_queue

num_table:

.ascii “789 456 1230,”

cur_table:

.ascii “HA5 DGC YB623”

jl end_func

movl func_table(,%eax,4),%eax

xorl %ebx,%ebx

jmp put_queue

end_func:

ret

func_table:

.long 0x415b5b1b,0x425b5b1b,0x435b5b1b,0x445b5b1b

.long 0x455b5b1b,0x465b5b1b,0x475b5b1b,0x485b5b1b

.long 0x495b5b1b,0x4a5b5b1b,0x4b5b5b1b,0x4c5b5b1b

key_map:

.byte 0,27

.ascii “1234567890+’”

.byte 127,9

.ascii “qwertyuiop}”

.byte 0,10,0

.ascii “asdfghjkl|{”

.byte 0,0

.ascii “'zxcvbnm,.-”

.byte 0,’,0,32 / 36-39 /

.fill 16,1,0 / 3A-49 /

.byte ‘-,0,0,0,’+ / 4A-4E /

.byte 0,0,0,0,0,0,0 / 4F-55 */

.byte '<

.fill 10,1,0

shift_map:

.byte 0,27

.ascii “!”#$%&/()=?`"

.byte 127,9

.ascii “QWERTYUIOP]^”

.byte 10,0

.ascii “ASDFGHJKL\[”

.byte 0,0

.ascii "ZXCVBNM;:_"

.byte 0,’,0,32

.byte '>

.fill 10,1,0

alt_map:

.byte 0,0

.ascii “\0@\0$\0\0{[]}\\0”

.byte 0,0

.byte 0,0,0,0,0,0,0,0,0,0,0

.byte '~,10,0

.byte 0,0,0,0,0,0,0,0,0,0,0

.byte 0,0

.byte 0,0,0,0,0,0,0,0,0,0,0

.byte 0,0,0,0

.byte '|

.fill 10,1,0

je 4f

orb $0x80,%al

4: andl $0xff,%eax

xorl %ebx,%ebx

call put_queue

none: ret

minus: cmpb $1,e0

jne do_self

movl $’/,%eax

xorl %ebx,%ebx

jmp put_queue

outb %al,$0x64

die: jmp die

Makefile

Makefile for the FREAX-kernel.

Note! Dependencies are done automagically by ‘make dep’, which also

removes any old dependencies. DON’T put your own dependencies here

unless it’s something special (ie not a .c file).

AR =gar

AS =gas

LD =gld

LDFLAGS =-s -x

CC =gcc

CFLAGS =-Wall -O -fstrength-reduce -fomit-frame-pointer -fcombine-regs

-finline-functions -mstring-insns -nostdinc -I…/include

CPP =gcc -E -nostdinc -I…/include

.c.s:

$(CC) $(CFLAGS)

-S -o $.s $<

.s.o:

$(AS) -c -o $.o $<

.c.o:

$(CC) $(CFLAGS)

-c -o $*.o $<

OBJS = sched.o system_call.o traps.o asm.o fork.o

panic.o printk.o vsprintf.o tty_io.o console.o

keyboard.o rs_io.o hd.o sys.o exit.o serial.o

mktime.o

kernel.o: $(OBJS)

$(LD) -r -o kernel.o $(OBJS)

sync

clean:

rm -f core *.o *.a tmp_make

for i in *.c;do rm -f

basename $$i .c

.s;done

dep:

sed ‘/### Dependencies/q’ < Makefile > tmp_make

(for i in *.c;do echo -n

echo $$i | sed 's,\.c,\.s,'

" ";

$(CPP) -M $$i;done) >> tmp_make

cp tmp_make Makefile

Dependencies:

console.s console.o : console.c …/include/linux/sched.h …/include/linux/head.h

…/include/linux/fs.h …/include/sys/types.h …/include/linux/mm.h

…/include/linux/tty.h …/include/termios.h …/include/asm/io.h

…/include/asm/system.h

exit.s exit.o : exit.c …/include/errno.h …/include/signal.h

…/include/sys/types.h …/include/sys/wait.h …/include/linux/sched.h

…/include/linux/head.h …/include/linux/fs.h …/include/linux/mm.h

…/include/linux/kernel.h …/include/linux/tty.h …/include/termios.h

…/include/asm/segment.h

fork.s fork.o : fork.c …/include/errno.h …/include/linux/sched.h

…/include/linux/head.h …/include/linux/fs.h …/include/sys/types.h

…/include/linux/mm.h …/include/linux/kernel.h …/include/asm/segment.h

…/include/asm/system.h

hd.s hd.o : hd.c …/include/linux/config.h …/include/linux/sched.h

…/include/linux/head.h …/include/linux/fs.h …/include/sys/types.h

…/include/linux/mm.h …/include/linux/kernel.h …/include/linux/hdreg.h

…/include/asm/system.h …/include/asm/io.h …/include/asm/segment.h

mktime.s mktime.o : mktime.c …/include/time.h

panic.s panic.o : panic.c …/include/linux/kernel.h

printk.s printk.o : printk.c …/include/stdarg.h …/include/stddef.h

…/include/linux/kernel.h

sched.s sched.o : sched.c …/include/linux/sched.h …/include/linux/head.h

…/include/linux/fs.h …/include/sys/types.h …/include/linux/mm.h

…/include/linux/kernel.h …/include/signal.h …/include/linux/sys.h

…/include/asm/system.h …/include/asm/io.h …/include/asm/segment.h

serial.s serial.o : serial.c …/include/linux/tty.h …/include/termios.h

…/include/linux/sched.h …/include/linux/head.h …/include/linux/fs.h

…/include/sys/types.h …/include/linux/mm.h …/include/asm/system.h

…/include/asm/io.h

sys.s sys.o : sys.c …/include/errno.h …/include/linux/sched.h

…/include/linux/head.h …/include/linux/fs.h …/include/sys/types.h

…/include/linux/mm.h …/include/linux/tty.h …/include/termios.h

…/include/linux/kernel.h …/include/asm/segment.h …/include/sys/times.h

…/include/sys/utsname.h

traps.s traps.o : traps.c …/include/string.h …/include/linux/head.h

…/include/linux/sched.h …/include/linux/fs.h …/include/sys/types.h

…/include/linux/mm.h …/include/linux/kernel.h …/include/asm/system.h

…/include/asm/segment.h

tty_io.s tty_io.o : tty_io.c …/include/ctype.h …/include/errno.h

…/include/signal.h …/include/sys/types.h …/include/linux/sched.h

…/include/linux/head.h …/include/linux/fs.h …/include/linux/mm.h

…/include/linux/tty.h …/include/termios.h …/include/asm/segment.h

…/include/asm/system.h

vsprintf.s vsprintf.o : vsprintf.c …/include/stdarg.h …/include/string.h

mktime.c

#include <time.h>

/*

  • This isn’t the library routine, it is only used in the kernel.
  • as such, we don’t care about years<1970 etc, but assume everything
  • is ok. Similarly, TZ etc is happily ignored. We just do everything
  • as easily as possible. Let’s find something public for the library
  • routines (although I think minix times is public).

    /

    /

  • PS. I hate whoever though up the year 1970 - couldn’t they have gotten
  • a leap-year instead? I also hate Gregorius, pope or no. I’m grumpy.

    /

    #define MINUTE 60

    #define HOUR (60MINUTE)

    #define DAY (24HOUR)

    #define YEAR (365DAY)

/* interestingly, we assume leap-years /

static int month[12] = {

0,

DAY(31),

DAY*(31+29),

DAY*(31+29+31),

DAY*(31+29+31+30),

DAY*(31+29+31+30+31),

DAY*(31+29+31+30+31+30),

DAY*(31+29+31+30+31+30+31),

DAY*(31+29+31+30+31+30+31+31),

DAY*(31+29+31+30+31+30+31+31+30),

DAY*(31+29+31+30+31+30+31+31+30+31),

DAY*(31+29+31+30+31+30+31+31+30+31+30)

};

long kernel_mktime(struct tm * tm)

{

long res;

int year;

year = tm->tm_year - 70;
           

#include <linux/kernel.h>

volatile void panic(const char * s)

{

printk(“Kernel panic: %s\n\r”,s);

for(;😉;

}

printk.c

#include <stdarg.h>

#include <stddef.h>

#include <linux/kernel.h>

static char buf[1024];

int printk(const char *fmt, …)

{

va_list args;

int i;

va_start(args, fmt);
i=vsprintf(buf,fmt,args);
va_end(args);
__asm__("push %%fs\n\t"
	"push %%ds\n\t"
	"pop %%fs\n\t"
	"pushl %0\n\t"
	"pushl $_buf\n\t"
	"pushl $0\n\t"
	"call _tty_write\n\t"
	"addl $8,%%esp\n\t"
	"popl %0\n\t"
	"pop %%fs"
	::"r" (i):"ax","cx","dx");
return i;
           

}

rs_io.s

.text

.globl _rs1_interrupt,_rs2_interrupt

size = 1024

rs_addr = 0

head = 4

tail = 8

proc_list = 12

buf = 16

startup = 256

pop %ds

pop %es

popl %eax

popl %ebx

popl %ecx

popl %edx

addl $4,%esp # jump over _table_list entry

iret

jmp_table:

.long modem_status,write_char,read_char,line_status

.align 2

modem_status:

addl $6,%edx

inb %dx,%al

ret

.align 2

line_status:

addl $5,%edx

inb %dx,%al

ret

.align 2

read_char:

inb %dx,%al

movl %ecx,%edx

subl $_table_list,%edx

shrl $3,%edx

movl (%ecx),%ecx # read-queue

movl head(%ecx),%ebx

movb %al,buf(%ecx,%ebx)

incl %ebx

andl $size-1,%ebx

cmpl tail(%ecx),%ebx

je 1f

movl %ebx,head(%ecx)

pushl %edx

call _do_tty_interrupt

addl $4,%esp

1: ret

.align 2

write_char:

movl 4(%ecx),%ecx # write-queue

movl head(%ecx),%ebx

subl tail(%ecx),%ebx

andl $size-1,%ebx # nr chars in queue

je write_buffer_empty

cmpl $startup,%ebx

ja 1f

movl proc_list(%ecx),%ebx # wake up sleeping process

testl %ebx,%ebx # is there any?

je 1f

movl $0,(%ebx)

1: movl tail(%ecx),%ebx

movb buf(%ecx,%ebx),%al

outb %al,%dx

incl %ebx

andl $size-1,%ebx

movl %ebx,tail(%ecx)

cmpl head(%ecx),%ebx

je write_buffer_empty

ret

.align 2

write_buffer_empty:

movl proc_list(%ecx),%ebx # wake up sleeping process

testl %ebx,%ebx # is there any?

je 1f

movl $0,(%ebx)

1: incl %edx

inb %dx,%al

jmp 1f

1: jmp 1f

1: andb $0xd,%al

outb %al,%dx

ret

sched.c

#include <linux/sched.h>

#include <linux/kernel.h>

#include <signal.h>

#include <linux/sys.h>

#include <asm/system.h>

#include <asm/io.h>

#include <asm/segment.h>

#define LATCH (1193180/HZ)

extern void mem_use(void);

extern int timer_interrupt(void);

extern int system_call(void);

union task_union {

struct task_struct task;

char stack[PAGE_SIZE];

};

static union task_union init_task = {INIT_TASK,};

long volatile jiffies=0;

long startup_time=0;

struct task_struct *current = &(init_task.task), *last_task_used_math = NULL;

struct task_struct * task[NR_TASKS] = {&(init_task.task), };

long user_stack [ PAGE_SIZE>>2 ] ;

struct {

long * a;

short b;

} stack_start = { & user_stack [PAGE_SIZE>>2] , 0x10 };

void math_state_restore()

{

if (last_task_used_math)

asm(“fnsave %0”::“m” (last_task_used_math->tss.i387));

if (current->used_math)

asm(“frstor %0”::“m” (current->tss.i387));

else {

asm(“fninit”:😃;

current->used_math=1;

}

last_task_used_math=current;

}

void schedule(void)

{

int i,next,c;

struct task_struct ** p;

for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
	if (*p) {
		if ((*p)->alarm && (*p)->alarm < jiffies) {
				(*p)->signal |= (1<<(SIGALRM-1));
				(*p)->alarm = 0;
			}
		if ((*p)->signal && (*p)->state==TASK_INTERRUPTIBLE)
			(*p)->state=TASK_RUNNING;
	}
           
while (1) {
	c = -1;
	next = 0;
	i = NR_TASKS;
	p = &task[NR_TASKS];
	while (--i) {
		if (!*--p)
			continue;
		if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
			c = (*p)->counter, next = i;
	}
	if (c) break;
	for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
		if (*p)
			(*p)->counter = ((*p)->counter >> 1) +
					(*p)->priority;
}
switch_to(next);
           

}

int sys_pause(void)

{

current->state = TASK_INTERRUPTIBLE;

schedule();

return 0;

}

void sleep_on(struct task_struct **p)

{

struct task_struct *tmp;

if (!p)
	return;
if (current == &(init_task.task))
	panic("task[0] trying to sleep");
tmp = *p;
*p = current;
current->state = TASK_UNINTERRUPTIBLE;
schedule();
if (tmp)
	tmp->state=0;
           

}

void interruptible_sleep_on(struct task_struct **p)

{

struct task_struct *tmp;

if (!p)
	return;
if (current == &(init_task.task))
	panic("task[0] trying to sleep");
tmp=*p;
*p=current;
           

repeat: current->state = TASK_INTERRUPTIBLE;

schedule();

if (*p && *p != current) {

(**p).state=0;

goto repeat;

}

*p=NULL;

if (tmp)

tmp->state=0;

}

void wake_up(struct task_struct **p)

{

if (p && *p) {

(**p).state=0;

*p=NULL;

}

}

void do_timer(long cpl)

{

if (cpl)

current->utime++;

else

current->stime++;

if ((–current->counter)>0) return;

current->counter=0;

if (!cpl) return;

schedule();

}

int sys_alarm(long seconds)

{

current->alarm = (seconds>0)?(jiffies+HZ*seconds):0;

return seconds;

}

int sys_getpid(void)

{

return current->pid;

}

int sys_getppid(void)

{

return current->father;

}

int sys_getuid(void)

{

return current->uid;

}

int sys_geteuid(void)

{

return current->euid;

}

int sys_getgid(void)

{

return current->gid;

}

int sys_getegid(void)

{

return current->egid;

}

int sys_nice(long increment)

{

if (current->priority-increment>0)

current->priority -= increment;

return 0;

}

int sys_signal(long signal,long addr,long restorer)

{

long i;

switch (signal) {
	case SIGHUP: case SIGINT: case SIGQUIT: case SIGILL:
	case SIGTRAP: case SIGABRT: case SIGFPE: case SIGUSR1:
	case SIGSEGV: case SIGUSR2: case SIGPIPE: case SIGALRM:
	case SIGCHLD:
		i=(long) current->sig_fn[signal-1];
		current->sig_fn[signal-1] = (fn_ptr) addr;
		current->sig_restorer = (fn_ptr) restorer;
		return i;
	default: return -1;
}
           

}

void sched_init(void)

{

int i;

struct desc_struct * p;

set_tss_desc(gdt+FIRST_TSS_ENTRY,&(init_task.task.tss));
set_ldt_desc(gdt+FIRST_LDT_ENTRY,&(init_task.task.ldt));
p = gdt+2+FIRST_TSS_ENTRY;
for(i=1;i<NR_TASKS;i++) {
	task[i] = NULL;
	p->a=p->b=0;
	p++;
	p->a=p->b=0;
	p++;
}
ltr(0);
lldt(0);
outb_p(0x36,0x43);		/* binary, mode 3, LSB/MSB, ch 0 */
outb_p(LATCH & 0xff , 0x40);	/* LSB */
outb(LATCH >> 8 , 0x40);	/* MSB */
set_intr_gate(0x20,&timer_interrupt);
outb(inb_p(0x21)&~0x01,0x21);
set_system_gate(0x80,&system_call);
           

}

serial.c

#include <linux/tty.h>

#include <linux/sched.h>

#include <asm/system.h>

#include <asm/io.h>

#define WAKEUP_CHARS (TTY_BUF_SIZE/4)

extern void rs1_interrupt(void);

extern void rs2_interrupt(void);

static void init(int port)

{

outb_p(0x80,port+3);

}

void rs_init(void)

{

set_intr_gate(0x24,rs1_interrupt);

set_intr_gate(0x23,rs2_interrupt);

init(tty_table[1].read_q.data);

init(tty_table[2].read_q.data);

outb(inb_p(0x21)&0xE7,0x21);

}

void rs_write(struct tty_struct * tty)

{

cli();

if (!EMPTY(tty->write_q))

outb(inb_p(tty->write_q.data+1)|0x02,tty->write_q.data+1);

sti();

}

sys.c

#include <errno.h>

#include <linux/sched.h>

#include <linux/tty.h>

#include <linux/kernel.h>

#include <asm/segment.h>

#include <sys/times.h>

#include <sys/utsname.h>

int sys_ftime()

{

return -ENOSYS;

}

int sys_mknod()

{

return -ENOSYS;

}

int sys_break()

{

return -ENOSYS;

}

int sys_mount()

{

return -ENOSYS;

}

int sys_umount()

{

return -ENOSYS;

}

int sys_ustat(int dev,struct ustat * ubuf)

{

return -1;

}

int sys_ptrace()

{

return -ENOSYS;

}

int sys_stty()

{

return -ENOSYS;

}

int sys_gtty()

{

return -ENOSYS;

}

int sys_rename()

{

return -ENOSYS;

}

int sys_prof()

{

return -ENOSYS;

}

int sys_setgid(int gid)

{

if (current->euid && current->uid)

if (current->gidgid || current->sgidgid)

current->egid=gid;

else

return -EPERM;

else

current->gid=current->egid=gid;

return 0;

}

int sys_acct()

{

return -ENOSYS;

}

int sys_phys()

{

return -ENOSYS;

}

int sys_lock()

{

return -ENOSYS;

}

int sys_mpx()

{

return -ENOSYS;

}

int sys_ulimit()

{

return -ENOSYS;

}

int sys_time(long * tloc)

{

int i;

i = CURRENT_TIME;
if (tloc) {
	verify_area(tloc,4);
	put_fs_long(i,(unsigned long *)tloc);
}
return i;
           

}

int sys_setuid(int uid)

{

if (current->euid && current->uid)

if (uidcurrent->uid || current->suidcurrent->uid)

current->euid=uid;

else

return -EPERM;

else

current->euid=current->uid=uid;

return 0;

}

int sys_stime(long * tptr)

{

if (current->euid && current->uid)

return -1;

startup_time = get_fs_long((unsigned long *)tptr) - jiffies/HZ;

return 0;

}

int sys_times(struct tms * tbuf)

{

if (!tbuf)

return jiffies;

verify_area(tbuf,sizeof *tbuf);

put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);

put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);

put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);

put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);

return jiffies;

}

int sys_brk(unsigned long end_data_seg)

{

if (end_data_seg >= current->end_code &&

end_data_seg < current->start_stack - 16384)

current->brk = end_data_seg;

return current->brk;

}

int sys_setpgid(int pid, int pgid)

{

int i;

if (!pid)

pid = current->pid;

if (!pgid)

pgid = pid;

for (i=0 ; i<NR_TASKS ; i++)

if (task[i] && task[i]->pid==pid) {

if (task[i]->leader)

return -EPERM;

if (task[i]->session != current->session)

return -EPERM;

task[i]->pgrp = pgid;

return 0;

}

return -ESRCH;

}

int sys_getpgrp(void)

{

return current->pgrp;

}

int sys_setsid(void)

{

if (current->uid && current->euid)

return -EPERM;

if (current->leader)

return -EPERM;

current->leader = 1;

current->session = current->pgrp = current->pid;

current->tty = -1;

return current->pgrp;

}

int sys_uname(struct utsname * name)

{

static struct utsname thisname = {

“linux .0”,“nodename”,"release ","version ","machine "

};

int i;

if (!name) return -1;
verify_area(name,sizeof *name);
for(i=0;i<sizeof *name;i++)
	put_fs_byte(((char *) &thisname)[i],i+(char *) name);
return (0);
           

}

int sys_umask(int mask)

{

int old = current->umask;

current->umask = mask & 0777;
return (old);
           

}

system_call.s

SIG_CHLD = 17

EAX = 0x00

EBX = 0x04

ECX = 0x08

EDX = 0x0C

FS = 0x10

ES = 0x14

DS = 0x18

EIP = 0x1C

CS = 0x20

EFLAGS = 0x24

OLDESP = 0x28

OLDSS = 0x2C

state = 0 # these are offsets into the task-struct.

counter = 4

priority = 8

signal = 12

restorer = 16 # address of info-restorer

sig_fn = 20 # table of 32 signal addresses

nr_system_calls = 67

.globl _system_call,_sys_fork,_timer_interrupt,_hd_interrupt,_sys_execve

.align 2

bad_sys_call:

movl $-1,%eax

iret

.align 2

reschedule:

pushl $ret_from_sys_call

jmp _schedule

.align 2

_system_call:

cmpl $nr_system_calls-1,%eax

ja bad_sys_call

push %ds

push %es

push %fs

pushl %edx

pushl %ecx # push %ebx,%ecx,%edx as parameters

pushl %ebx # to the system call

movl $0x10,%edx # set up ds,es to kernel space

mov %dx,%ds

mov %dx,%es

movl $0x17,%edx # fs points to local data space

mov %dx,%fs

call _sys_call_table(,%eax,4)

pushl %eax

movl _current,%eax

cmpl $0,state(%eax) # state

jne reschedule

cmpl $0,counter(%eax) # counter

je reschedule

ret_from_sys_call:

movl _current,%eax # task[0] cannot have signals

cmpl _task,%eax

je 3f

movl CS(%esp),%ebx # was old code segment supervisor

testl $3,%ebx # mode? If so - don’t check signals

je 3f

cmpw $0x17,OLDSS(%esp) # was stack segment = 0x17 ?

jne 3f

2: movl signal(%eax),%ebx # signals (bitmap, 32 signals)

bsfl %ebx,%ecx # %ecx is signal nr, return if none

je 3f

btrl %ecx,%ebx # clear it

movl %ebx,signal(%eax)

movl sig_fn(%eax,%ecx,4),%ebx # %ebx is signal handler address

cmpl $1,%ebx

jb default_signal # 0 is default signal handler - exit

je 2b # 1 is ignore - find next signal

movl $0,sig_fn(%eax,%ecx,4) # reset signal handler address

incl %ecx

xchgl %ebx,EIP(%esp) # put new return address on stack

subl $28,OLDESP(%esp)

movl OLDESP(%esp),%edx # push old return address on stack

pushl %eax # but first check that it’s ok.

pushl %ecx

pushl $28

pushl %edx

call _verify_area

popl %edx

addl $4,%esp

popl %ecx

popl %eax

movl restorer(%eax),%eax

movl %eax,%fs:(%edx) # flag/reg restorer

movl %ecx,%fs:4(%edx) # signal nr

movl EAX(%esp),%eax

movl %eax,%fs:8(%edx) # old eax

movl ECX(%esp),%eax

movl %eax,%fs:12(%edx) # old ecx

movl EDX(%esp),%eax

movl %eax,%fs:16(%edx) # old edx

movl EFLAGS(%esp),%eax

movl %eax,%fs:20(%edx) # old eflags

movl %ebx,%fs:24(%edx) # old return addr

3: popl %eax

popl %ebx

popl %ecx

popl %edx

pop %fs

pop %es

pop %ds

iret

default_signal:

incl %ecx

cmpl $SIG_CHLD,%ecx

je 2b

pushl %ecx

call _do_exit # remember to set bit 7 when dumping core

addl $4,%esp

jmp 3b

.align 2

_timer_interrupt:

push %ds # save ds,es and put kernel data space

push %es # into them. %fs is used by _system_call

push %fs

pushl %edx # we save %eax,%ecx,%edx as gcc doesn’t

pushl %ecx # save those across function calls. %ebx

pushl %ebx # is saved as we use that in ret_sys_call

pushl %eax

movl $0x10,%eax

mov %ax,%ds

mov %ax,%es

movl $0x17,%eax

mov %ax,%fs

incl _jiffies

movb $0x20,%al # EOI to interrupt controller #1

outb %al,$0x20

movl CS(%esp),%eax

andl $3,%eax # %eax is CPL (0 or 3, 0=supervisor)

pushl %eax

call _do_timer # ‘do_timer(long CPL)’ does everything from

addl $4,%esp # task switching to accounting …

jmp ret_from_sys_call

.align 2

_sys_execve:

lea EIP(%esp),%eax

pushl %eax

call _do_execve

addl $4,%esp

ret

.align 2

_sys_fork:

call _find_empty_process

testl %eax,%eax

js 1f

push %gs

pushl %esi

pushl %edi

pushl %ebp

pushl %eax

call _copy_process

addl $20,%esp

1: ret

_hd_interrupt:

pushl %eax

pushl %ecx

pushl %edx

push %ds

push %es

push %fs

movl $0x10,%eax

mov %ax,%ds

mov %ax,%es

movl $0x17,%eax

mov %ax,%fs

movb $0x20,%al

outb %al,$0x20 # EOI to interrupt controller #1

jmp 1f # give port chance to breathe

1: jmp 1f

1: outb %al,$0xA0 # same to controller #2

movl _do_hd,%eax

testl %eax,%eax

jne 1f

movl $_unexpected_hd_interrupt,%eax

1: call *%eax # “interesting” way of handling intr.

pop %fs

pop %es

pop %ds

popl %edx

popl %ecx

popl %eax

iret

traps.c

#include <string.h>

#include <linux/head.h>

#include <linux/sched.h>

#include <linux/kernel.h>

#include <asm/system.h>

#include <asm/segment.h>

#define get_seg_byte(seg,addr) ({

register char __res;

asm(“push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs”

:"=a" (__res):“0” (seg),“m” (*(addr)));

__res;})

#define get_seg_long(seg,addr) ({

register unsigned long __res;

asm(“push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs”

:"=a" (__res):“0” (seg),“m” (*(addr)));

__res;})

#define _fs() ({

register unsigned short __res;

asm(“mov %%fs,%%ax”:"=a" (__res)😃;

__res;})

int do_exit(long code);

void page_exception(void);

void divide_error(void);

void debug(void);

void nmi(void);

void int3(void);

void overflow(void);

void bounds(void);

void invalid_op(void);

void device_not_available(void);

void double_fault(void);

void coprocessor_segment_overrun(void);

void invalid_TSS(void);

void segment_not_present(void);

void stack_segment(void);

void general_protection(void);

void page_fault(void);

void coprocessor_error(void);

void reserved(void);

static void die(char * str,long esp_ptr,long nr)

{

long * esp = (long *) esp_ptr;

int i;

printk("%s: %04x\n\r",str,nr&0xffff);
printk("EIP:\t%04x:%p\nEFLAGS:\t%p\nESP:\t%04x:%p\n",
	esp[1],esp[0],esp[2],esp[4],esp[3]);
printk("fs: %04x\n",_fs());
printk("base: %p, limit: %p\n",get_base(current->ldt[1]),get_limit(0x17));
if (esp[4] == 0x17) {
	printk("Stack: ");
	for (i=0;i<4;i++)
		printk("%p ",get_seg_long(0x17,i+(long *)esp[3]));
	printk("\n");
}
str(i);
printk("Pid: %d, process nr: %d\n\r",current->pid,0xffff & i);
for(i=0;i<10;i++)
	printk("%02x ",0xff & get_seg_byte(esp[1],(i+(char *)esp[0])));
printk("\n\r");
do_exit(11);		/* play segment exception */
           

}

void do_double_fault(long esp, long error_code)

{

die(“double fault”,esp,error_code);

}

void do_general_protection(long esp, long error_code)

{

die(“general protection”,esp,error_code);

}

void do_divide_error(long esp, long error_code)

{

die(“divide error”,esp,error_code);

}

void do_int3(long * esp, long error_code,

long fs,long es,long ds,

long ebp,long esi,long edi,

long edx,long ecx,long ebx,long eax)

{

int tr;

__asm__("str %%ax":"=a" (tr):"0" (0));
printk("eax\t\tebx\t\tecx\t\tedx\n\r%8x\t%8x\t%8x\t%8x\n\r",
	eax,ebx,ecx,edx);
printk("esi\t\tedi\t\tebp\t\tesp\n\r%8x\t%8x\t%8x\t%8x\n\r",
	esi,edi,ebp,(long) esp);
printk("\n\rds\tes\tfs\ttr\n\r%4x\t%4x\t%4x\t%4x\n\r",
	ds,es,fs,tr);
printk("EIP: %8x   CS: %4x  EFLAGS: %8x\n\r",esp[0],esp[1],esp[2]);
           

}

void do_nmi(long esp, long error_code)

{

die(“nmi”,esp,error_code);

}

void do_debug(long esp, long error_code)

{

die(“debug”,esp,error_code);

}

void do_overflow(long esp, long error_code)

{

die(“overflow”,esp,error_code);

}

void do_bounds(long esp, long error_code)

{

die(“bounds”,esp,error_code);

}

void do_invalid_op(long esp, long error_code)

{

die(“invalid operand”,esp,error_code);

}

void do_device_not_available(long esp, long error_code)

{

die(“device not available”,esp,error_code);

}

void do_coprocessor_segment_overrun(long esp, long error_code)

{

die(“coprocessor segment overrun”,esp,error_code);

}

void do_invalid_TSS(long esp,long error_code)

{

die(“invalid TSS”,esp,error_code);

}

void do_segment_not_present(long esp,long error_code)

{

die(“segment not present”,esp,error_code);

}

void do_stack_segment(long esp,long error_code)

{

die(“stack segment”,esp,error_code);

}

void do_coprocessor_error(long esp, long error_code)

{

die(“coprocessor error”,esp,error_code);

}

void do_reserved(long esp, long error_code)

{

die(“reserved (15,17-31) error”,esp,error_code);

}

void trap_init(void)

{

int i;

set_trap_gate(0,&divide_error);
set_trap_gate(1,&debug);
set_trap_gate(2,&nmi);
set_system_gate(3,&int3);	/* int3-5 can be called from all */
set_system_gate(4,&overflow);
set_system_gate(5,&bounds);
set_trap_gate(6,&invalid_op);
set_trap_gate(7,&device_not_available);
set_trap_gate(8,&double_fault);
set_trap_gate(9,&coprocessor_segment_overrun);
set_trap_gate(10,&invalid_TSS);
set_trap_gate(11,&segment_not_present);
set_trap_gate(12,&stack_segment);
set_trap_gate(13,&general_protection);
set_trap_gate(14,&page_fault);
set_trap_gate(15,&reserved);
set_trap_gate(16,&coprocessor_error);
for (i=17;i<32;i++)
	set_trap_gate(i,&reserved);
           

}

tty_io.c

#include <ctype.h>

#include <errno.h>

#include <signal.h>

#define ALRMMASK (1<<(SIGALRM-1))

#include <linux/sched.h>

#include <linux/tty.h>

#include <asm/segment.h>

#include <asm/system.h>

#define _L_FLAG(tty,f) ((tty)->termios.c_lflag & f)

#define _I_FLAG(tty,f) ((tty)->termios.c_iflag & f)

#define _O_FLAG(tty,f) ((tty)->termios.c_oflag & f)

#define L_CANON(tty) _L_FLAG((tty),ICANON)

#define L_ISIG(tty) _L_FLAG((tty),ISIG)

#define L_ECHO(tty) _L_FLAG((tty),ECHO)

#define L_ECHOE(tty) _L_FLAG((tty),ECHOE)

#define L_ECHOK(tty) _L_FLAG((tty),ECHOK)

#define L_ECHOCTL(tty) _L_FLAG((tty),ECHOCTL)

#define L_ECHOKE(tty) _L_FLAG((tty),ECHOKE)

#define I_UCLC(tty) _I_FLAG((tty),IUCLC)

#define I_NLCR(tty) _I_FLAG((tty),INLCR)

#define I_CRNL(tty) _I_FLAG((tty),ICRNL)

#define I_NOCR(tty) _I_FLAG((tty),IGNCR)

#define O_POST(tty) _O_FLAG((tty),OPOST)

#define O_NLCR(tty) _O_FLAG((tty),ONLCR)

#define O_CRNL(tty) _O_FLAG((tty),OCRNL)

#define O_NLRET(tty) _O_FLAG((tty),ONLRET)

#define O_LCUC(tty) _O_FLAG((tty),OLCUC)

struct tty_struct tty_table[] = {

{

{0,

OPOST|ONLCR,

{0x2f8,0,0,0,""},

{0,0,0,0,""}

}

};

struct tty_queue * table_list[]={

&tty_table[0].read_q, &tty_table[0].write_q,

&tty_table[1].read_q, &tty_table[1].write_q,

&tty_table[2].read_q, &tty_table[2].write_q

};

void tty_init(void)

{

rs_init();

con_init();

}

void tty_intr(struct tty_struct * tty, int signal)

{

int i;

if (tty->pgrp <= 0)
	return;
for (i=0;i<NR_TASKS;i++)
	if (task[i] && task[i]->pgrp==tty->pgrp)
		task[i]->signal |= 1<<(signal-1);
           

}

static void sleep_if_empty(struct tty_queue * queue)

{

cli();

while (!current->signal && EMPTY(*queue))

interruptible_sleep_on(&queue->proc_list);

sti();

}

static void sleep_if_full(struct tty_queue * queue)

{

if (!FULL(*queue))

return;

cli();

while (!current->signal && LEFT(*queue)<128)

interruptible_sleep_on(&queue->proc_list);

sti();

}

void copy_to_cooked(struct tty_struct * tty)

{

signed char c;

while (!EMPTY(tty->read_q) && !FULL(tty->secondary)) {
	GETCH(tty->read_q,c);
	if (c==13)
		if (I_CRNL(tty))
			c=10;
		else if (I_NOCR(tty))
			continue;
		else ;
	else if (c==10 && I_NLCR(tty))
		c=13;
	if (I_UCLC(tty))
		c=tolower(c);
	if (L_CANON(tty)) {
		if (c==ERASE_CHAR(tty)) {
			if (EMPTY(tty->secondary) ||
			   (c=LAST(tty->secondary))==10 ||
			   c==EOF_CHAR(tty))
				continue;
			if (L_ECHO(tty)) {
				if (c<32)
					PUTCH(127,tty->write_q);
				PUTCH(127,tty->write_q);
				tty->write(tty);
			}
			DEC(tty->secondary.head);
			continue;
		}
		if (c==STOP_CHAR(tty)) {
			tty->stopped=1;
			continue;
		}
		if (c==START_CHAR(tty)) {
			tty->stopped=0;
			continue;
		}
	}
	if (!L_ISIG(tty)) {
		if (c==INTR_CHAR(tty)) {
			tty_intr(tty,SIGINT);
			continue;
		}
	}
	if (c==10 || c==EOF_CHAR(tty))
		tty->secondary.data++;
	if (L_ECHO(tty)) {
		if (c==10) {
			PUTCH(10,tty->write_q);
			PUTCH(13,tty->write_q);
		} else if (c<32) {
			if (L_ECHOCTL(tty)) {
				PUTCH('^',tty->write_q);
				PUTCH(c+64,tty->write_q);
			}
		} else
			PUTCH(c,tty->write_q);
		tty->write(tty);
	}
	PUTCH(c,tty->secondary);
}
wake_up(&tty->secondary.proc_list);
           

}

int tty_read(unsigned channel, char * buf, int nr)

{

struct tty_struct * tty;

char c, * b=buf;

int minimum,time,flag=0;

long oldalarm;

if (channel>2 || nr<0) return -1;
tty = &tty_table[channel];
oldalarm = current->alarm;
time = (unsigned) 10*tty->termios.c_cc[VTIME];
minimum = (unsigned) tty->termios.c_cc[VMIN];
if (time && !minimum) {
	minimum=1;
	if (flag=(!oldalarm || time+jiffies<oldalarm))
		current->alarm = time+jiffies;
}
if (minimum>nr)
	minimum=nr;
while (nr>0) {
	if (flag && (current->signal & ALRMMASK)) {
		current->signal &= ~ALRMMASK;
		break;
	}
	if (current->signal)
		break;
	if (EMPTY(tty->secondary) || (L_CANON(tty) &&
	!tty->secondary.data && LEFT(tty->secondary)>20)) {
		sleep_if_empty(&tty->secondary);
		continue;
	}
	do {
		GETCH(tty->secondary,c);
		if (c==EOF_CHAR(tty) || c==10)
			tty->secondary.data--;
		if (c==EOF_CHAR(tty) && L_CANON(tty))
			return (b-buf);
		else {
			put_fs_byte(c,b++);
			if (!--nr)
				break;
		}
	} while (nr>0 && !EMPTY(tty->secondary));
	if (time && !L_CANON(tty))
		if (flag=(!oldalarm || time+jiffies<oldalarm))
			current->alarm = time+jiffies;
		else
			current->alarm = oldalarm;
	if (L_CANON(tty)) {
		if (b-buf)
			break;
	} else if (b-buf >= minimum)
		break;
}
current->alarm = oldalarm;
if (current->signal && !(b-buf))
	return -EINTR;
return (b-buf);
           

}

int tty_write(unsigned channel, char * buf, int nr)

{

static cr_flag=0;

struct tty_struct * tty;

char c, *b=buf;

if (channel>2 || nr<0) return -1;
tty = channel + tty_table;
while (nr>0) {
	sleep_if_full(&tty->write_q);
	if (current->signal)
		break;
	while (nr>0 && !FULL(tty->write_q)) {
		c=get_fs_byte(b);
		if (O_POST(tty)) {
			if (c=='\r' && O_CRNL(tty))
				c='\n';
			else if (c=='\n' && O_NLRET(tty))
				c='\r';
			if (c=='\n' && !cr_flag && O_NLCR(tty)) {
				cr_flag = 1;
				PUTCH(13,tty->write_q);
				continue;
			}
			if (O_LCUC(tty))
				c=toupper(c);
		}
		b++; nr--;
		cr_flag = 0;
		PUTCH(c,tty->write_q);
	}
	tty->write(tty);
	if (nr>0)
		schedule();
}
return (b-buf);
           

}

void do_tty_interrupt(int tty)

{

copy_to_cooked(tty_table+tty);

}

vsprintf.c

#include <stdarg.h>

#include <string.h>

#define is_digit© (© >= ‘0’ && © <= ‘9’)

static int skip_atoi(const char **s)

{

int i=0;

while (is_digit(**s))
	i = i*10 + *((*s)++) - '0';
return i;
           

}

#define ZEROPAD 1

#define do_div(n,base) ({

int __res;

asm(“divl %4”:"=a" (n),"=d" (__res):“0” (n),“1” (0),“r” (base));

__res; })

static char * number(char * str, int num, int base, int size, int precision

,int type)

{

char c,sign,tmp[36];

const char *digits=“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

int i;

if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
if (type&LEFT) type &= ~ZEROPAD;
if (base<2 || base>36)
	return 0;
c = (type & ZEROPAD) ? '0' : ' ' ;
if (type&SIGN && num<0) {
	sign='-';
	num = -num;
} else
	sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
if (sign) size--;
if (type&SPECIAL)
	if (base==16) size -= 2;
	else if (base==8) size--;
i=0;
if (num==0)
	tmp[i++]='0';
else while (num!=0)
	tmp[i++]=digits[do_div(num,base)];
if (i>precision) precision=i;
size -= precision;
if (!(type&(ZEROPAD+LEFT)))
	while(size-->0)
		*str++ = ' ';
if (sign)
	*str++ = sign;
if (type&SPECIAL)
	if (base==8)
		*str++ = '0';
	else if (base==16) {
		*str++ = '0';
		*str++ = digits[33];
	}
if (!(type&LEFT))
	while(size-->0)
		*str++ = c;
while(i<precision--)
	*str++ = '0';
while(i-->0)
	*str++ = tmp[i];
while(size-->0)
	*str++ = ' ';
return str;
           

}

int vsprintf(char *buf, const char *fmt, va_list args)

{

int len;

int i;

char * str;

char *s;

int *ip;

int flags;		/* flags to number() */

int field_width;	/* width of output field */
int precision;		/* min. # of digits for integers; max
			   number of chars for from string */
int qualifier;		/* 'h', 'l', or 'L' for integer fields */

for (str=buf ; *fmt ; ++fmt) {
	if (*fmt != '%') {
		*str++ = *fmt;
		continue;
	}
		
	/* process flags */
	flags = 0;
	repeat:
		++fmt;		/* this also skips first '%' */
		switch (*fmt) {
			case '-': flags |= LEFT; goto repeat;
			case '+': flags |= PLUS; goto repeat;
			case ' ': flags |= SPACE; goto repeat;
			case '#': flags |= SPECIAL; goto repeat;
			case '0': flags |= ZEROPAD; goto repeat;
			}
	
	/* get field width */
	field_width = -1;
	if (is_digit(*fmt))
		field_width = skip_atoi(&fmt);
	else if (*fmt == '*') {
		/* it's the next argument */
		field_width = va_arg(args, int);
		if (field_width < 0) {
			field_width = -field_width;
			flags |= LEFT;
		}
	}

	/* get the precision */
	precision = -1;
	if (*fmt == '.') {
		++fmt;	
		if (is_digit(*fmt))
			precision = skip_atoi(&fmt);
		else if (*fmt == '*') {
			/* it's the next argument */
			precision = va_arg(args, int);
		}
		if (precision < 0)
			precision = 0;
	}

	/* get the conversion qualifier */
	qualifier = -1;
	if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
		qualifier = *fmt;
		++fmt;
	}

	switch (*fmt) {
	case 'c':
		if (!(flags & LEFT))
			while (--field_width > 0)
				*str++ = ' ';
		*str++ = (unsigned char) va_arg(args, int);
		while (--field_width > 0)
			*str++ = ' ';
		break;

	case 's':
		s = va_arg(args, char *);
		len = strlen(s);
		if (precision < 0)
			precision = len;
		else if (len > precision)
			len = precision;

		if (!(flags & LEFT))
			while (len < field_width--)
				*str++ = ' ';
		for (i = 0; i < len; ++i)
			*str++ = *s++;
		while (len < field_width--)
			*str++ = ' ';
		break;

	case 'o':
		str = number(str, va_arg(args, unsigned long), 8,
			field_width, precision, flags);
		break;

	case 'p':
		if (field_width == -1) {
			field_width = 8;
			flags |= ZEROPAD;
		}
		str = number(str,
			(unsigned long) va_arg(args, void *), 16,
			field_width, precision, flags);
		break;

	case 'x':
		flags |= SMALL;
	case 'X':
		str = number(str, va_arg(args, unsigned long), 16,
			field_width, precision, flags);
		break;

	case 'd':
	case 'i':
		flags |= SIGN;
	case 'u':
		str = number(str, va_arg(args, unsigned long), 10,
			field_width, precision, flags);
		break;

	case 'n':
		ip = va_arg(args, int *);
		*ip = (str - buf);
		break;

	default:
		if (*fmt != '%')
			*str++ = '%';
		if (*fmt)
			*str++ = *fmt;
		else
			--fmt;
		break;
	}
}
*str = '\0';
return str-buf;
           

}

繼續閱讀