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

📄 7db.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <u.h>#include <libc.h>#include <bio.h>#include <mach.h>/* * Alpha-specific debugger interface */static 	char	*alphaexcep(Map*, Rgetter);static	int	alphafoll(Map*, uvlong, Rgetter, uvlong*);static	int	alphainst(Map*, uvlong, char, char*, int);static	int	alphadas(Map*, uvlong, char*, int);static	int	alphainstlen(Map*, uvlong);/* *	Debugger interface */Machdata alphamach ={	{0x80, 0, 0, 0},		/* break point */	4,			/* break point size */	leswab,			/* short to local byte order */	leswal,			/* long to local byte order */	leswav,			/* vlong to local byte order */	risctrace,		/* C traceback */	riscframe,		/* Frame finder */	alphaexcep,		/* print exception */	0,			/* breakpoint fixup */	leieeesftos,		/* single precision float printer */	leieeedftos,		/* double precisioin float printer */	alphafoll,		/* following addresses */	alphainst,		/* print instruction */	alphadas,		/* dissembler */	alphainstlen,		/* instruction size */};static char *illegaltype[] = {	"breakpoint",	"bugchk",	"gentrap",	"fen",	"illegal instruction",};static char *alphaexcep(Map *map, Rgetter rget){	ulong type, a0, a1;	static char buf[256];	type = (*rget)(map, "TYPE");	a0 = (*rget)(map, "A0");	a1 = (*rget)(map, "A1");/*	a2 = (*rget)(map, "A2"); */	switch (type) {	case 1:	/* arith */		sprint(buf, "trap: arithmetic trap 0x%lux", a0);		break;	case 2:	/* bad instr or FEN */		if (a0 <= 4)			return illegaltype[a0];		else			sprint(buf, "illegal instr trap, unknown type %lud", a0);		break;	case 3:	/* intr */		sprint(buf, "interrupt type %lud", a0);		break;	case 4:	/* memory fault */		sprint(buf, "fault %s addr=0x%lux", (a1&1)?"write":"read", a0);		break;	case 5:	/* syscall() */		return "system call";	case 6:	/* alignment fault */		sprint(buf, "unaligned op 0x%lux addr 0x%lux", a1, a0);		break;	default:	/* cannot happen */		sprint(buf, "unknown exception type %lud", type);		break;	}	return buf;}	/* alpha disassembler and related functions */static	char FRAMENAME[] = ".frame";typedef struct {	uvlong addr;	uchar op;			/* bits 31-26 */	uchar ra;			/* bits 25-21 */	uchar rb;			/* bits 20-16 */	uchar rc;			/* bits 4-0 */	long mem;			/* bits 15-0 */	long branch;			/* bits 20-0 */	uchar function;			/* bits 11-5 */	uchar literal;			/* bits 20-13 */	uchar islit;			/* bit 12 */	uchar fpfn;			/* bits 10-5 */	uchar fpmode;			/* bits 15-11 */	long w0;	long w1;	int size;			/* instruction size */	char *curr;			/* fill point in buffer */	char *end;			/* end of buffer */	char *err;			/* error message */} Instr;static Map *mymap;static intdecode(uvlong pc, Instr *i){	ulong w;	if (get4(mymap, pc, &w) < 0) {		werrstr("can't read instruction: %r");		return -1;	}	i->addr = pc;	i->size = 1;	i->op = (w >> 26) & 0x3F;	i->ra = (w >> 21) & 0x1F;	i->rb = (w >> 16) & 0x1F;	i->rc = w & 0x1F;	i->function = (w >> 5) & 0x7F;	i->mem = w & 0xFFFF;	if (i->mem & 0x8000)		i->mem -= 0x10000;	i->branch = w & 0x1FFFFF;	if (i->branch & 0x100000)		i->branch -= 0x200000;	i->function = (w >> 5) & 0x7F;	i->literal = (w >> 13) & 0xFF;	i->islit = (w >> 12) & 0x01;	i->fpfn = (w >> 5) & 0x3F;	i->fpmode = (w >> 11) & 0x1F;	i->w0 = w;	return 1;}static intmkinstr(uvlong pc, Instr *i){/*	Instr x; */	if (decode(pc, i) < 0)		return -1;#ifdef	frommips/* we probably want to do something like this for alpha... */	/*	 * if it's a LUI followed by an ORI,	 * it's an immediate load of a large constant.	 * fix the LUI immediate in any case.	 */	if (i->op == 0x0F) {		if (decode(pc+4, &x) < 0)			return 0;		i->immediate <<= 16;		if (x.op == 0x0D && x.rs == x.rt && x.rt == i->rt) {			i->immediate |= (x.immediate & 0xFFFF);			i->w1 = x.w0;			i->size++;			return 1;		}	}#endif	return 1;}#pragma	varargck	argpos	bprint		2static voidbprint(Instr *i, char *fmt, ...){	va_list arg;	va_start(arg, fmt);	i->curr = vseprint(i->curr, i->end, fmt, arg);	va_end(arg);}typedef struct Opcode Opcode;struct Opcode {	char *mnemonic;	void (*f)(Opcode *, Instr *);	char *ken;};static void format(char *, Instr *, char *);static intplocal(Instr *i, char *m, char r, int store){	int offset;	char *reg;	Symbol s;	if (!findsym(i->addr, CTEXT, &s) || !findlocal(&s, FRAMENAME, &s))		return 0;	if (s.value > i->mem) {		if(!getauto(&s, s.value-i->mem, CAUTO, &s))			return 0;		reg = "(SP)";		offset = i->mem;	} else {		offset = i->mem-s.value-8;		if (!getauto(&s, offset, CPARAM, &s))			return 0;		reg = "(FP)";	}	if (store)		bprint(i, "%s\t%c%d,%s+%d%s", m, r, i->ra, s.name, offset, reg);	else		bprint(i, "%s\t%s+%d%s,%c%d", m, s.name, offset, reg, r, i->ra);	return 1;}static void_load(Opcode *o, Instr *i, char r){	char *m;	m = o->mnemonic;	if (i->rb == 30 && plocal(i, m, r, 0))		return;	if (i->rb == 29 && mach->sb) {		bprint(i, "%s\t", m);		i->curr += symoff(i->curr, i->end-i->curr, i->mem+mach->sb, CANY);		bprint(i, "(SB),%c%d", r, i->ra);		return;	}	format(m, i, o->ken);}static voidload(Opcode *o, Instr *i){	_load(o, i, 'R');}static voidloadf(Opcode *o, Instr *i){	_load(o, i, 'F');}static void_store(Opcode *o, Instr *i, char r){	char *m;	m = o->mnemonic;	if (i->rb == 30 && plocal(i, m, r, 1))		return;	if (i->rb == 29 && mach->sb) {		bprint(i, "%s\t%c%d,", m, r, i->ra);		i->curr += symoff(i->curr, i->end-i->curr, i->mem+mach->sb, CANY);		bprint(i, "(SB)");		return;	}	format(o->mnemonic, i, o->ken);}static voidstore(Opcode *o, Instr *i){	_store(o, i, 'R');}static voidstoref(Opcode *o, Instr *i){	_store(o, i, 'F');}static voidmisc(Opcode *o, Instr *i){	char *f;	USED(o);	switch (i->mem&0xFFFF) {	case 0x0000:		f = "TRAPB";		break;	case 0x4000:		f = "MB";		break;	case 0x8000:		f = "FETCH\t0(R%b)";		break;	case 0xA000:		f = "FETCH_M\t0(R%b)";		break;	case 0xC000:		f = "RPCC\tR%a";		break;	case 0xE000:		f = "RC\tR%a";		break;	case 0xF000:		f = "RS\tR%a";		break;	default:		f = "%w";	}	format(0, i, f);}static char	*jmpcode[4] = { "JMP", "JSR", "RET", "JSR_COROUTINE" };static voidjmp(Opcode *o, Instr *i){	int hint;	char *m;	USED(o);	hint = (i->mem >> 14) & 3;	m = jmpcode[hint];	if (i->ra == 31) {		if (hint == 2 && i->rb == 29)			bprint(i, m);		else			format(m, i, "(R%b)");	}	else		format(m, i, "R%a,(R%b)");}static voidbr(Opcode *o, Instr *i){	if (i->ra == 31)		format(o->mnemonic, i, "%B");	else		format(o->mnemonic, i, o->ken);}static voidbsr(Opcode *o, Instr *i){	if (i->ra == 26)		format(o->mnemonic, i, "%B");	else		format(o->mnemonic, i, o->ken);}static voidmult(Opcode *o, Instr *i){	char *m;	switch (i->function) {	case 0x00:		m = "MULL";		break;	case 0x20:		m = "MULQ";		break;	case 0x40:		m = "MULL/V";		break;	case 0x60:		m = "MULQ/V";		break;	case 0x30:		m = "UMULH";		break;	default:		format("???", i, "%w");		return;	}	format(m, i, o->ken);}static char	alphaload[] = "%l,R%a";static char	alphafload[] = "%l,F%a";static char	alphastore[] = "R%a,%l";static char	alphafstore[] = "F%a,%l";static char	alphabranch[] = "R%a,%B";static char	alphafbranch[] = "F%a,%B";static char	alphaint[] = "%v,R%a,R%c";static char	alphafp[] = "F%b,F%a,F%c";static char	alphafp2[] = "F%b,F%c";static char	alphaxxx[] = "%w";static Opcode opcodes[64] = {	"PAL",		0,	alphaxxx,	"OPC01",	0,	alphaxxx,	"OPC02",	0,	alphaxxx,	"OPC03",	0,	alphaxxx,	"OPC04",	0,	alphaxxx,	"OPC05",	0,	alphaxxx,	"OPC06",	0,	alphaxxx,	"OPC07",	0,	alphaxxx,	"MOVQA",	load,	alphaload,	"MOVQAH",	load,	alphaload,	"MOVBU",	load,	alphaload,		/* v 3 */	"MOVQU",	load,	alphaload,	"MOVWU",	load,	alphaload,		/* v 3 */	"MOVWU",	store,	alphastore,		/* v 3 */	"MOVBU",	store,	alphastore,		/* v 3 */	"MOVQU",	store,	alphastore,	0,		0,	0,			/* int arith */	0,		0,	0,			/* logical */	0,		0,	0,			/* shift */	0,		mult,	alphaint,	"OPC14",	0,	alphaxxx,	"vax",		0,	alphafp,		/* vax */	0,		0,	0,			/* ieee */	0,		0,	0,			/* fp */	0,		misc,	alphaxxx,	"PAL19 [HW_MFPR]",0,	alphaxxx,	"JSR",		jmp,	0,	"PAL1B [HW_LD]",0,	alphaxxx,	"OPC1C",	0,	alphaxxx,	"PAL1D [HW_MTPR]",0,	alphaxxx,	"PAL1E [HW_REI]",0,	alphaxxx,	"PAL1F [HW_ST]",0,	alphaxxx,	"MOVF",		loadf,	alphafload,	"MOVG",		loadf,	alphafload,	"MOVS",		loadf,	alphafload,	"MOVT",		loadf,	alphafload,	"MOVF",		storef,	alphafstore,	"MOVG",		storef,	alphafstore,	"MOVS",		storef,	alphafstore,	"MOVT",		storef,	alphafstore,	"MOVL",		load,	alphaload,	"MOVQ",		load,	alphaload,	"MOVLL",	load,	alphaload,	"MOVQL",	load,	alphaload,	"MOVL",		store,	alphastore,	"MOVQ",		store,	alphastore,	"MOVLC",	store,	alphastore,	"MOVQC",	store,	alphastore,	"JMP",		br,	alphabranch,	"FBEQ",		0,	alphafbranch,	"FBLT",		0,	alphafbranch,	"FBLE",		0,	alphafbranch,	"JSR",		bsr,	alphabranch,	"FBNE",		0,	alphafbranch,	"FBGE",		0,	alphafbranch,	"FBGT",		0,	alphafbranch,	"BLBC",		0,	alphafbranch,	"BEQ",		0,	alphabranch,	"BLT",		0,	alphabranch,	"BLE",		0,	alphabranch,	"BLBS",		0,	alphabranch,	"BNE",		0,	alphabranch,	"BGE",		0,	alphabranch,	"BGT",		0,	alphabranch,};static Opcode fpopcodes[64] = {	"???",		0,	alphaxxx,	"???",		0,	alphaxxx,	"???",		0,	alphaxxx,	"???",		0,	alphaxxx,	"???",		0,	alphaxxx,

⌨️ 快捷键说明

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